kv_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.
  14. package integration
  15. import (
  16. "bytes"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  23. "github.com/coreos/etcd/integration"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. "github.com/coreos/etcd/storage/storagepb"
  26. )
  27. func TestKVPut(t *testing.T) {
  28. defer testutil.AfterTest(t)
  29. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  30. defer clus.Terminate(t)
  31. lapi := clientv3.NewLease(clus.RandClient())
  32. defer lapi.Close()
  33. kv := clientv3.NewKV(clus.RandClient())
  34. ctx := context.TODO()
  35. resp, err := lapi.Create(context.Background(), 10)
  36. if err != nil {
  37. t.Fatalf("failed to create lease %v", err)
  38. }
  39. tests := []struct {
  40. key, val string
  41. leaseID clientv3.LeaseID
  42. }{
  43. {"foo", "bar", clientv3.NoLease},
  44. {"hello", "world", clientv3.LeaseID(resp.ID)},
  45. }
  46. for i, tt := range tests {
  47. if _, err := kv.Put(ctx, tt.key, tt.val, clientv3.WithLease(tt.leaseID)); err != nil {
  48. t.Fatalf("#%d: couldn't put %q (%v)", i, tt.key, err)
  49. }
  50. resp, err := kv.Get(ctx, tt.key)
  51. if err != nil {
  52. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  53. }
  54. if len(resp.Kvs) != 1 {
  55. t.Fatalf("#%d: expected 1 key, got %d", i, len(resp.Kvs))
  56. }
  57. if !bytes.Equal([]byte(tt.val), resp.Kvs[0].Value) {
  58. t.Errorf("#%d: val = %s, want %s", i, tt.val, resp.Kvs[0].Value)
  59. }
  60. if tt.leaseID != clientv3.LeaseID(resp.Kvs[0].Lease) {
  61. t.Errorf("#%d: val = %d, want %d", i, tt.leaseID, resp.Kvs[0].Lease)
  62. }
  63. }
  64. }
  65. func TestKVRange(t *testing.T) {
  66. defer testutil.AfterTest(t)
  67. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  68. defer clus.Terminate(t)
  69. kv := clientv3.NewKV(clus.RandClient())
  70. ctx := context.TODO()
  71. keySet := []string{"a", "b", "c", "c", "c", "foo", "foo/abc", "fop"}
  72. for i, key := range keySet {
  73. if _, err := kv.Put(ctx, key, ""); err != nil {
  74. t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
  75. }
  76. }
  77. resp, err := kv.Get(ctx, keySet[0])
  78. if err != nil {
  79. t.Fatalf("couldn't get key (%v)", err)
  80. }
  81. wheader := resp.Header
  82. tests := []struct {
  83. begin, end string
  84. rev int64
  85. opts []clientv3.OpOption
  86. wantSet []*storagepb.KeyValue
  87. }{
  88. // range first two
  89. {
  90. "a", "c",
  91. 0,
  92. nil,
  93. []*storagepb.KeyValue{
  94. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  95. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  96. },
  97. },
  98. // range first two with serializable
  99. {
  100. "a", "c",
  101. 0,
  102. []clientv3.OpOption{clientv3.WithSerializable()},
  103. []*storagepb.KeyValue{
  104. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  105. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  106. },
  107. },
  108. // range all with rev
  109. {
  110. "a", "x",
  111. 2,
  112. nil,
  113. []*storagepb.KeyValue{
  114. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  115. },
  116. },
  117. // range all with SortByKey, SortAscend
  118. {
  119. "a", "x",
  120. 0,
  121. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)},
  122. []*storagepb.KeyValue{
  123. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  124. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  125. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  126. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  127. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  128. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  129. },
  130. },
  131. // range all with SortByCreatedRev, SortDescend
  132. {
  133. "a", "x",
  134. 0,
  135. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByCreatedRev, clientv3.SortDescend)},
  136. []*storagepb.KeyValue{
  137. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  138. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  139. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  140. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  141. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  142. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  143. },
  144. },
  145. // range all with SortByModifiedRev, SortDescend
  146. {
  147. "a", "x",
  148. 0,
  149. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByModifiedRev, clientv3.SortDescend)},
  150. []*storagepb.KeyValue{
  151. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  152. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  153. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  154. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  155. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  156. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  157. },
  158. },
  159. // WithPrefix
  160. {
  161. "foo", "",
  162. 0,
  163. []clientv3.OpOption{clientv3.WithPrefix()},
  164. []*storagepb.KeyValue{
  165. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  166. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  167. },
  168. },
  169. // WithFromKey
  170. {
  171. "fo", "",
  172. 0,
  173. []clientv3.OpOption{clientv3.WithFromKey()},
  174. []*storagepb.KeyValue{
  175. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  176. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  177. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  178. },
  179. },
  180. }
  181. for i, tt := range tests {
  182. opts := []clientv3.OpOption{clientv3.WithRange(tt.end), clientv3.WithRev(tt.rev)}
  183. opts = append(opts, tt.opts...)
  184. resp, err := kv.Get(ctx, tt.begin, opts...)
  185. if err != nil {
  186. t.Fatalf("#%d: couldn't range (%v)", i, err)
  187. }
  188. if !reflect.DeepEqual(wheader, resp.Header) {
  189. t.Fatalf("#%d: wheader expected %+v, got %+v", i, wheader, resp.Header)
  190. }
  191. if !reflect.DeepEqual(tt.wantSet, resp.Kvs) {
  192. t.Fatalf("#%d: resp.Kvs expected %+v, got %+v", i, tt.wantSet, resp.Kvs)
  193. }
  194. }
  195. }
  196. func TestKVDeleteRange(t *testing.T) {
  197. defer testutil.AfterTest(t)
  198. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  199. defer clus.Terminate(t)
  200. kv := clientv3.NewKV(clus.RandClient())
  201. ctx := context.TODO()
  202. tests := []struct {
  203. key string
  204. opts []clientv3.OpOption
  205. wkeys []string
  206. }{
  207. // [a, c)
  208. {
  209. key: "a",
  210. opts: []clientv3.OpOption{clientv3.WithRange("c")},
  211. wkeys: []string{"c", "c/abc", "d"},
  212. },
  213. // >= c
  214. {
  215. key: "c",
  216. opts: []clientv3.OpOption{clientv3.WithFromKey()},
  217. wkeys: []string{"a", "b"},
  218. },
  219. // c*
  220. {
  221. key: "c",
  222. opts: []clientv3.OpOption{clientv3.WithPrefix()},
  223. wkeys: []string{"a", "b", "d"},
  224. },
  225. // *
  226. {
  227. key: "\x00",
  228. opts: []clientv3.OpOption{clientv3.WithFromKey()},
  229. wkeys: []string{},
  230. },
  231. }
  232. for i, tt := range tests {
  233. keySet := []string{"a", "b", "c", "c/abc", "d"}
  234. for j, key := range keySet {
  235. if _, err := kv.Put(ctx, key, ""); err != nil {
  236. t.Fatalf("#%d: couldn't put %q (%v)", j, key, err)
  237. }
  238. }
  239. _, err := kv.Delete(ctx, tt.key, tt.opts...)
  240. if err != nil {
  241. t.Fatalf("#%d: couldn't delete range (%v)", i, err)
  242. }
  243. resp, err := kv.Get(ctx, "a", clientv3.WithFromKey())
  244. if err != nil {
  245. t.Fatalf("#%d: couldn't get keys (%v)", i, err)
  246. }
  247. keys := []string{}
  248. for _, kv := range resp.Kvs {
  249. keys = append(keys, string(kv.Key))
  250. }
  251. if !reflect.DeepEqual(tt.wkeys, keys) {
  252. t.Errorf("#%d: resp.Kvs got %v, expected %v", i, keys, tt.wkeys)
  253. }
  254. }
  255. }
  256. func TestKVDelete(t *testing.T) {
  257. defer testutil.AfterTest(t)
  258. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  259. defer clus.Terminate(t)
  260. kv := clientv3.NewKV(clus.RandClient())
  261. ctx := context.TODO()
  262. presp, err := kv.Put(ctx, "foo", "")
  263. if err != nil {
  264. t.Fatalf("couldn't put 'foo' (%v)", err)
  265. }
  266. if presp.Header.Revision != 2 {
  267. t.Fatalf("presp.Header.Revision got %d, want %d", presp.Header.Revision, 2)
  268. }
  269. resp, err := kv.Delete(ctx, "foo")
  270. if err != nil {
  271. t.Fatalf("couldn't delete key (%v)", err)
  272. }
  273. if resp.Header.Revision != 3 {
  274. t.Fatalf("resp.Header.Revision got %d, want %d", resp.Header.Revision, 3)
  275. }
  276. gresp, err := kv.Get(ctx, "foo")
  277. if err != nil {
  278. t.Fatalf("couldn't get key (%v)", err)
  279. }
  280. if len(gresp.Kvs) > 0 {
  281. t.Fatalf("gresp.Kvs got %+v, want none", gresp.Kvs)
  282. }
  283. }
  284. func TestKVCompact(t *testing.T) {
  285. defer testutil.AfterTest(t)
  286. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  287. defer clus.Terminate(t)
  288. kv := clientv3.NewKV(clus.RandClient())
  289. ctx := context.TODO()
  290. for i := 0; i < 10; i++ {
  291. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  292. t.Fatalf("couldn't put 'foo' (%v)", err)
  293. }
  294. }
  295. err := kv.Compact(ctx, 7)
  296. if err != nil {
  297. t.Fatalf("couldn't compact kv space (%v)", err)
  298. }
  299. err = kv.Compact(ctx, 7)
  300. if err == nil || err != rpctypes.ErrCompacted {
  301. t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev)
  302. }
  303. wc := clientv3.NewWatcher(clus.RandClient())
  304. defer wc.Close()
  305. wchan := wc.Watch(ctx, "foo", clientv3.WithRev(3))
  306. if wr := <-wchan; wr.CompactRevision != 7 {
  307. t.Fatalf("wchan CompactRevision got %v, want 7", wr.CompactRevision)
  308. }
  309. if wr, ok := <-wchan; ok {
  310. t.Fatalf("wchan got %v, expected closed", wr)
  311. }
  312. err = kv.Compact(ctx, 1000)
  313. if err == nil || err != rpctypes.ErrFutureRev {
  314. t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev)
  315. }
  316. }
  317. // TestKVGetRetry ensures get will retry on disconnect.
  318. func TestKVGetRetry(t *testing.T) {
  319. defer testutil.AfterTest(t)
  320. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  321. defer clus.Terminate(t)
  322. kv := clientv3.NewKV(clus.Client(0))
  323. ctx := context.TODO()
  324. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  325. t.Fatal(err)
  326. }
  327. clus.Members[0].Stop(t)
  328. <-clus.Members[0].StopNotify()
  329. donec := make(chan struct{})
  330. go func() {
  331. // Get will fail, but reconnect will trigger
  332. gresp, gerr := kv.Get(ctx, "foo")
  333. if gerr != nil {
  334. t.Fatal(gerr)
  335. }
  336. wkvs := []*storagepb.KeyValue{
  337. {
  338. Key: []byte("foo"),
  339. Value: []byte("bar"),
  340. CreateRevision: 2,
  341. ModRevision: 2,
  342. Version: 1,
  343. },
  344. }
  345. if !reflect.DeepEqual(gresp.Kvs, wkvs) {
  346. t.Fatalf("bad get: got %v, want %v", gresp.Kvs, wkvs)
  347. }
  348. donec <- struct{}{}
  349. }()
  350. time.Sleep(100 * time.Millisecond)
  351. clus.Members[0].Restart(t)
  352. select {
  353. case <-time.After(5 * time.Second):
  354. t.Fatalf("timed out waiting for get")
  355. case <-donec:
  356. }
  357. }
  358. // TestKVPutFailGetRetry ensures a get will retry following a failed put.
  359. func TestKVPutFailGetRetry(t *testing.T) {
  360. defer testutil.AfterTest(t)
  361. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  362. defer clus.Terminate(t)
  363. kv := clientv3.NewKV(clus.Client(0))
  364. ctx := context.TODO()
  365. clus.Members[0].Stop(t)
  366. <-clus.Members[0].StopNotify()
  367. _, err := kv.Put(ctx, "foo", "bar")
  368. if err == nil {
  369. t.Fatalf("got success on disconnected put, wanted error")
  370. }
  371. donec := make(chan struct{})
  372. go func() {
  373. // Get will fail, but reconnect will trigger
  374. gresp, gerr := kv.Get(ctx, "foo")
  375. if gerr != nil {
  376. t.Fatal(gerr)
  377. }
  378. if len(gresp.Kvs) != 0 {
  379. t.Fatalf("bad get kvs: got %+v, want empty", gresp.Kvs)
  380. }
  381. donec <- struct{}{}
  382. }()
  383. time.Sleep(100 * time.Millisecond)
  384. clus.Members[0].Restart(t)
  385. select {
  386. case <-time.After(5 * time.Second):
  387. t.Fatalf("timed out waiting for get")
  388. case <-donec:
  389. }
  390. }
  391. // TestKVGetCancel tests that a context cancel on a Get terminates as expected.
  392. func TestKVGetCancel(t *testing.T) {
  393. defer testutil.AfterTest(t)
  394. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  395. defer clus.Terminate(t)
  396. oldconn := clus.Client(0).ActiveConnection()
  397. kv := clientv3.NewKV(clus.Client(0))
  398. ctx, cancel := context.WithCancel(context.TODO())
  399. cancel()
  400. resp, err := kv.Get(ctx, "abc")
  401. if err == nil {
  402. t.Fatalf("cancel on get response %v, expected context error", resp)
  403. }
  404. newconn := clus.Client(0).ActiveConnection()
  405. if oldconn != newconn {
  406. t.Fatalf("cancel on get broke client connection")
  407. }
  408. }