kv_test.go 13 KB

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