kv_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. keySet := []string{"a", "b", "c", "c", "c", "d", "e", "f"}
  204. for i, key := range keySet {
  205. if _, err := kv.Put(ctx, key, ""); err != nil {
  206. t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
  207. }
  208. }
  209. tests := []struct {
  210. key, end string
  211. delRev int64
  212. }{
  213. {"a", "b", int64(len(keySet) + 2)}, // delete [a, b)
  214. {"d", "f", int64(len(keySet) + 3)}, // delete [d, f)
  215. }
  216. for i, tt := range tests {
  217. dresp, err := kv.Delete(ctx, tt.key, clientv3.WithRange(tt.end))
  218. if err != nil {
  219. t.Fatalf("#%d: couldn't delete range (%v)", i, err)
  220. }
  221. if dresp.Header.Revision != tt.delRev {
  222. t.Fatalf("#%d: dresp.Header.Revision got %d, want %d", i, dresp.Header.Revision, tt.delRev)
  223. }
  224. resp, err := kv.Get(ctx, tt.key, clientv3.WithRange(tt.end))
  225. if err != nil {
  226. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  227. }
  228. if len(resp.Kvs) > 0 {
  229. t.Fatalf("#%d: resp.Kvs expected none, but got %+v", i, resp.Kvs)
  230. }
  231. }
  232. }
  233. func TestKVDelete(t *testing.T) {
  234. defer testutil.AfterTest(t)
  235. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  236. defer clus.Terminate(t)
  237. kv := clientv3.NewKV(clus.RandClient())
  238. ctx := context.TODO()
  239. presp, err := kv.Put(ctx, "foo", "")
  240. if err != nil {
  241. t.Fatalf("couldn't put 'foo' (%v)", err)
  242. }
  243. if presp.Header.Revision != 2 {
  244. t.Fatalf("presp.Header.Revision got %d, want %d", presp.Header.Revision, 2)
  245. }
  246. resp, err := kv.Delete(ctx, "foo")
  247. if err != nil {
  248. t.Fatalf("couldn't delete key (%v)", err)
  249. }
  250. if resp.Header.Revision != 3 {
  251. t.Fatalf("resp.Header.Revision got %d, want %d", resp.Header.Revision, 3)
  252. }
  253. gresp, err := kv.Get(ctx, "foo")
  254. if err != nil {
  255. t.Fatalf("couldn't get key (%v)", err)
  256. }
  257. if len(gresp.Kvs) > 0 {
  258. t.Fatalf("gresp.Kvs got %+v, want none", gresp.Kvs)
  259. }
  260. }
  261. func TestKVCompact(t *testing.T) {
  262. defer testutil.AfterTest(t)
  263. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  264. defer clus.Terminate(t)
  265. kv := clientv3.NewKV(clus.RandClient())
  266. ctx := context.TODO()
  267. for i := 0; i < 10; i++ {
  268. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  269. t.Fatalf("couldn't put 'foo' (%v)", err)
  270. }
  271. }
  272. err := kv.Compact(ctx, 7)
  273. if err != nil {
  274. t.Fatalf("couldn't compact kv space (%v)", err)
  275. }
  276. err = kv.Compact(ctx, 7)
  277. if err == nil || err != v3rpc.ErrCompacted {
  278. t.Fatalf("error got %v, want %v", err, v3rpc.ErrFutureRev)
  279. }
  280. wc := clientv3.NewWatcher(clus.RandClient())
  281. defer wc.Close()
  282. wchan := wc.Watch(ctx, "foo", 3)
  283. if wr := <-wchan; wr.CompactRevision != 7 {
  284. t.Fatalf("wchan CompactRevision got %v, want 7", wr.CompactRevision)
  285. }
  286. if wr, ok := <-wchan; ok {
  287. t.Fatalf("wchan got %v, expected closed", wr)
  288. }
  289. err = kv.Compact(ctx, 1000)
  290. if err == nil || err != v3rpc.ErrFutureRev {
  291. t.Fatalf("error got %v, want %v", err, v3rpc.ErrFutureRev)
  292. }
  293. }
  294. // TestKVGetRetry ensures get will retry on disconnect.
  295. func TestKVGetRetry(t *testing.T) {
  296. defer testutil.AfterTest(t)
  297. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  298. defer clus.Terminate(t)
  299. kv := clientv3.NewKV(clus.Client(0))
  300. ctx := context.TODO()
  301. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  302. t.Fatal(err)
  303. }
  304. clus.Members[0].Stop(t)
  305. <-clus.Members[0].StopNotify()
  306. donec := make(chan struct{})
  307. go func() {
  308. // Get will fail, but reconnect will trigger
  309. gresp, gerr := kv.Get(ctx, "foo")
  310. if gerr != nil {
  311. t.Fatal(gerr)
  312. }
  313. wkvs := []*storagepb.KeyValue{
  314. {
  315. Key: []byte("foo"),
  316. Value: []byte("bar"),
  317. CreateRevision: 2,
  318. ModRevision: 2,
  319. Version: 1,
  320. },
  321. }
  322. if !reflect.DeepEqual(gresp.Kvs, wkvs) {
  323. t.Fatalf("bad get: got %v, want %v", gresp.Kvs, wkvs)
  324. }
  325. donec <- struct{}{}
  326. }()
  327. time.Sleep(100 * time.Millisecond)
  328. clus.Members[0].Restart(t)
  329. select {
  330. case <-time.After(5 * time.Second):
  331. t.Fatalf("timed out waiting for get")
  332. case <-donec:
  333. }
  334. }
  335. // TestKVPutFailGetRetry ensures a get will retry following a failed put.
  336. func TestKVPutFailGetRetry(t *testing.T) {
  337. defer testutil.AfterTest(t)
  338. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  339. defer clus.Terminate(t)
  340. kv := clientv3.NewKV(clus.Client(0))
  341. ctx := context.TODO()
  342. clus.Members[0].Stop(t)
  343. <-clus.Members[0].StopNotify()
  344. _, err := kv.Put(ctx, "foo", "bar")
  345. if err == nil {
  346. t.Fatalf("got success on disconnected put, wanted error")
  347. }
  348. donec := make(chan struct{})
  349. go func() {
  350. // Get will fail, but reconnect will trigger
  351. gresp, gerr := kv.Get(ctx, "foo")
  352. if gerr != nil {
  353. t.Fatal(gerr)
  354. }
  355. if len(gresp.Kvs) != 0 {
  356. t.Fatalf("bad get kvs: got %+v, want empty", gresp.Kvs)
  357. }
  358. donec <- struct{}{}
  359. }()
  360. time.Sleep(100 * time.Millisecond)
  361. clus.Members[0].Restart(t)
  362. select {
  363. case <-time.After(5 * time.Second):
  364. t.Fatalf("timed out waiting for get")
  365. case <-donec:
  366. }
  367. }