v3_grpc_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.package recipe
  14. package integration
  15. import (
  16. "math/rand"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/storage/storagepb"
  24. )
  25. type clusterV3 struct {
  26. *cluster
  27. conns []*grpc.ClientConn
  28. }
  29. // newClusterGRPC returns a launched cluster with a grpc client connection
  30. // for each cluster member.
  31. func newClusterGRPC(t *testing.T, cfg *clusterConfig) *clusterV3 {
  32. cfg.useV3 = true
  33. cfg.useGRPC = true
  34. clus := &clusterV3{cluster: NewClusterByConfig(t, cfg)}
  35. for _, m := range clus.Members {
  36. conn, err := NewGRPCClient(m)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. clus.conns = append(clus.conns, conn)
  41. }
  42. clus.Launch(t)
  43. return clus
  44. }
  45. func (c *clusterV3) Terminate(t *testing.T) {
  46. for _, conn := range c.conns {
  47. if err := conn.Close(); err != nil {
  48. t.Error(err)
  49. }
  50. }
  51. c.cluster.Terminate(t)
  52. }
  53. func (c *clusterV3) RandConn() *grpc.ClientConn {
  54. return c.conns[rand.Intn(len(c.conns))]
  55. }
  56. // TestV3PutOverwrite puts a key with the v3 api to a random cluster member,
  57. // overwrites it, then checks that the change was applied.
  58. func TestV3PutOverwrite(t *testing.T) {
  59. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  60. defer clus.Terminate(t)
  61. kvc := pb.NewKVClient(clus.RandConn())
  62. key := []byte("foo")
  63. reqput := &pb.PutRequest{Key: key, Value: []byte("bar")}
  64. respput, err := kvc.Put(context.TODO(), reqput)
  65. if err != nil {
  66. t.Fatalf("couldn't put key (%v)", err)
  67. }
  68. // overwrite
  69. reqput.Value = []byte("baz")
  70. respput2, err := kvc.Put(context.TODO(), reqput)
  71. if err != nil {
  72. t.Fatalf("couldn't put key (%v)", err)
  73. }
  74. if respput2.Header.Revision <= respput.Header.Revision {
  75. t.Fatalf("expected newer revision on overwrite, got %v <= %v",
  76. respput2.Header.Revision, respput.Header.Revision)
  77. }
  78. reqrange := &pb.RangeRequest{Key: key}
  79. resprange, err := kvc.Range(context.TODO(), reqrange)
  80. if err != nil {
  81. t.Fatalf("couldn't get key (%v)", err)
  82. }
  83. if len(resprange.Kvs) != 1 {
  84. t.Fatalf("expected 1 key, got %v", len(resprange.Kvs))
  85. }
  86. kv := resprange.Kvs[0]
  87. if kv.ModRevision <= kv.CreateRevision {
  88. t.Errorf("expected modRev > createRev, got %d <= %d",
  89. kv.ModRevision, kv.CreateRevision)
  90. }
  91. if !reflect.DeepEqual(reqput.Value, kv.Value) {
  92. t.Errorf("expected value %v, got %v", reqput.Value, kv.Value)
  93. }
  94. }
  95. // TestV3DeleteRange tests various edge cases in the DeleteRange API.
  96. func TestV3DeleteRange(t *testing.T) {
  97. tests := []struct {
  98. keySet []string
  99. begin string
  100. end string
  101. wantSet [][]byte
  102. }{
  103. // delete middle
  104. {
  105. []string{"foo", "foo/abc", "fop"},
  106. "foo/", "fop",
  107. [][]byte{[]byte("foo"), []byte("fop")},
  108. },
  109. // no delete
  110. {
  111. []string{"foo", "foo/abc", "fop"},
  112. "foo/", "foo/",
  113. [][]byte{[]byte("foo"), []byte("foo/abc"), []byte("fop")},
  114. },
  115. // delete first
  116. {
  117. []string{"foo", "foo/abc", "fop"},
  118. "fo", "fop",
  119. [][]byte{[]byte("fop")},
  120. },
  121. // delete tail
  122. {
  123. []string{"foo", "foo/abc", "fop"},
  124. "foo/", "fos",
  125. [][]byte{[]byte("foo")},
  126. },
  127. // delete exact
  128. {
  129. []string{"foo", "foo/abc", "fop"},
  130. "foo/abc", "",
  131. [][]byte{[]byte("foo"), []byte("fop")},
  132. },
  133. // delete none, [x,x)
  134. {
  135. []string{"foo"},
  136. "foo", "foo",
  137. [][]byte{[]byte("foo")},
  138. },
  139. }
  140. for i, tt := range tests {
  141. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  142. kvc := pb.NewKVClient(clus.RandConn())
  143. ks := tt.keySet
  144. for j := range ks {
  145. reqput := &pb.PutRequest{Key: []byte(ks[j]), Value: []byte{}}
  146. _, err := kvc.Put(context.TODO(), reqput)
  147. if err != nil {
  148. t.Fatalf("couldn't put key (%v)", err)
  149. }
  150. }
  151. dreq := &pb.DeleteRangeRequest{
  152. Key: []byte(tt.begin),
  153. RangeEnd: []byte(tt.end)}
  154. dresp, err := kvc.DeleteRange(context.TODO(), dreq)
  155. if err != nil {
  156. t.Fatalf("couldn't delete range on test %d (%v)", i, err)
  157. }
  158. rreq := &pb.RangeRequest{Key: []byte{0x0}, RangeEnd: []byte{0xff}}
  159. rresp, err := kvc.Range(context.TODO(), rreq)
  160. if err != nil {
  161. t.Errorf("couldn't get range on test %v (%v)", i, err)
  162. }
  163. if dresp.Header.Revision != rresp.Header.Revision {
  164. t.Errorf("expected revision %v, got %v",
  165. dresp.Header.Revision, rresp.Header.Revision)
  166. }
  167. keys := [][]byte{}
  168. for j := range rresp.Kvs {
  169. keys = append(keys, rresp.Kvs[j].Key)
  170. }
  171. if reflect.DeepEqual(tt.wantSet, keys) == false {
  172. t.Errorf("expected %v on test %v, got %v", tt.wantSet, i, keys)
  173. }
  174. // can't defer because tcp ports will be in use
  175. clus.Terminate(t)
  176. }
  177. }
  178. // TestV3WatchFromCurrentRevision tests Watch APIs from current revision.
  179. func TestV3WatchFromCurrentRevision(t *testing.T) {
  180. tests := []struct {
  181. putKeys []string
  182. watchRequest *pb.WatchRequest
  183. wresps []*pb.WatchResponse
  184. }{
  185. // watch the key, matching
  186. {
  187. []string{"foo"},
  188. &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Key: []byte("foo")}},
  189. []*pb.WatchResponse{
  190. {
  191. Header: &pb.ResponseHeader{Revision: 1},
  192. Created: true,
  193. },
  194. {
  195. Header: &pb.ResponseHeader{Revision: 2},
  196. Created: false,
  197. Events: []*storagepb.Event{
  198. {
  199. Type: storagepb.PUT,
  200. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  201. },
  202. },
  203. },
  204. },
  205. },
  206. // watch the key, non-matching
  207. {
  208. []string{"foo"},
  209. &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Key: []byte("helloworld")}},
  210. []*pb.WatchResponse{
  211. {
  212. Header: &pb.ResponseHeader{Revision: 1},
  213. Created: true,
  214. },
  215. },
  216. },
  217. // watch the prefix, matching
  218. {
  219. []string{"fooLong"},
  220. &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Prefix: []byte("foo")}},
  221. []*pb.WatchResponse{
  222. {
  223. Header: &pb.ResponseHeader{Revision: 1},
  224. Created: true,
  225. },
  226. {
  227. Header: &pb.ResponseHeader{Revision: 2},
  228. Created: false,
  229. Events: []*storagepb.Event{
  230. {
  231. Type: storagepb.PUT,
  232. Kv: &storagepb.KeyValue{Key: []byte("fooLong"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  233. },
  234. },
  235. },
  236. },
  237. },
  238. // watch the prefix, non-matching
  239. {
  240. []string{"foo"},
  241. &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Prefix: []byte("helloworld")}},
  242. []*pb.WatchResponse{
  243. {
  244. Header: &pb.ResponseHeader{Revision: 1},
  245. Created: true,
  246. },
  247. },
  248. },
  249. // multiple puts, one watcher with matching key
  250. {
  251. []string{"foo", "foo", "foo"},
  252. &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Key: []byte("foo")}},
  253. []*pb.WatchResponse{
  254. {
  255. Header: &pb.ResponseHeader{Revision: 1},
  256. Created: true,
  257. },
  258. {
  259. Header: &pb.ResponseHeader{Revision: 2},
  260. Created: false,
  261. Events: []*storagepb.Event{
  262. {
  263. Type: storagepb.PUT,
  264. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  265. },
  266. },
  267. },
  268. {
  269. Header: &pb.ResponseHeader{Revision: 3},
  270. Created: false,
  271. Events: []*storagepb.Event{
  272. {
  273. Type: storagepb.PUT,
  274. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 3, Version: 2},
  275. },
  276. },
  277. },
  278. {
  279. Header: &pb.ResponseHeader{Revision: 4},
  280. Created: false,
  281. Events: []*storagepb.Event{
  282. {
  283. Type: storagepb.PUT,
  284. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 4, Version: 3},
  285. },
  286. },
  287. },
  288. },
  289. },
  290. // multiple puts, one watcher with matching prefix
  291. {
  292. []string{"foo", "foo", "foo"},
  293. &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Prefix: []byte("foo")}},
  294. []*pb.WatchResponse{
  295. {
  296. Header: &pb.ResponseHeader{Revision: 1},
  297. Created: true,
  298. },
  299. {
  300. Header: &pb.ResponseHeader{Revision: 2},
  301. Created: false,
  302. Events: []*storagepb.Event{
  303. {
  304. Type: storagepb.PUT,
  305. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  306. },
  307. },
  308. },
  309. {
  310. Header: &pb.ResponseHeader{Revision: 3},
  311. Created: false,
  312. Events: []*storagepb.Event{
  313. {
  314. Type: storagepb.PUT,
  315. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 3, Version: 2},
  316. },
  317. },
  318. },
  319. {
  320. Header: &pb.ResponseHeader{Revision: 4},
  321. Created: false,
  322. Events: []*storagepb.Event{
  323. {
  324. Type: storagepb.PUT,
  325. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 4, Version: 3},
  326. },
  327. },
  328. },
  329. },
  330. },
  331. // TODO: watch and receive multiple-events from synced (need Txn)
  332. }
  333. for i, tt := range tests {
  334. clus := newClusterGRPC(t, &clusterConfig{size: 3})
  335. wAPI := pb.NewWatchClient(clus.RandConn())
  336. wStream, err := wAPI.Watch(context.TODO())
  337. if err != nil {
  338. t.Fatalf("#%d: wAPI.Watch error: %v", i, err)
  339. }
  340. if err := wStream.Send(tt.watchRequest); err != nil {
  341. t.Fatalf("#%d: wStream.Send error: %v", i, err)
  342. }
  343. kvc := pb.NewKVClient(clus.RandConn())
  344. for _, k := range tt.putKeys {
  345. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte(k), Value: []byte("bar")}); err != nil {
  346. t.Fatalf("#%d: couldn't put key (%v)", i, err)
  347. }
  348. }
  349. var createdWatchId int64
  350. for j, wresp := range tt.wresps {
  351. resp, err := wStream.Recv()
  352. if err != nil {
  353. t.Errorf("#%d.%d: wStream.Recv error: %v", i, j, err)
  354. }
  355. if resp.Header == nil {
  356. t.Fatalf("#%d.%d: unexpected nil resp.Header", i, j)
  357. }
  358. if resp.Header.Revision != wresp.Header.Revision {
  359. t.Errorf("#%d.%d: resp.Header.Revision got = %d, want = %d", i, j, resp.Header.Revision, wresp.Header.Revision)
  360. }
  361. if wresp.Created != resp.Created {
  362. t.Errorf("#%d.%d: resp.Created got = %v, want = %v", i, j, resp.Created, wresp.Created)
  363. }
  364. if resp.Created {
  365. createdWatchId = resp.WatchId
  366. }
  367. if resp.WatchId != createdWatchId {
  368. t.Errorf("#%d.%d: resp.WatchId got = %d, want = %d", i, j, resp.WatchId, createdWatchId)
  369. }
  370. if !reflect.DeepEqual(resp.Events, wresp.Events) {
  371. t.Errorf("#%d.%d: resp.Events got = %+v, want = %+v", i, j, resp.Events, wresp.Events)
  372. }
  373. }
  374. rCh := make(chan *pb.WatchResponse)
  375. go func() {
  376. resp, _ := wStream.Recv()
  377. rCh <- resp
  378. }()
  379. select {
  380. case nr := <-rCh:
  381. t.Errorf("#%d: unexpected response is received %+v", i, nr)
  382. case <-time.After(2 * time.Second):
  383. }
  384. wStream.CloseSend()
  385. rv, ok := <-rCh
  386. if rv != nil || !ok {
  387. t.Errorf("#%d: rv, ok got = %v %v, want = nil true", i, rv, ok)
  388. }
  389. // can't defer because tcp ports will be in use
  390. clus.Terminate(t)
  391. }
  392. }