kv_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. sortOption *clientv3.SortOption
  87. serializable bool
  88. wantSet []*storagepb.KeyValue
  89. }{
  90. // range first two
  91. {
  92. "a", "c",
  93. 0,
  94. nil,
  95. false,
  96. []*storagepb.KeyValue{
  97. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  98. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  99. },
  100. },
  101. // range first two with serializable
  102. {
  103. "a", "c",
  104. 0,
  105. nil,
  106. true,
  107. []*storagepb.KeyValue{
  108. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  109. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  110. },
  111. },
  112. // range all with rev
  113. {
  114. "a", "x",
  115. 2,
  116. nil,
  117. false,
  118. []*storagepb.KeyValue{
  119. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  120. },
  121. },
  122. // range all with SortByKey, SortAscend
  123. {
  124. "a", "x",
  125. 0,
  126. &clientv3.SortOption{Target: clientv3.SortByKey, Order: clientv3.SortAscend},
  127. false,
  128. []*storagepb.KeyValue{
  129. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  130. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  131. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  132. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  133. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  134. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  135. },
  136. },
  137. // range all with SortByCreatedRev, SortDescend
  138. {
  139. "a", "x",
  140. 0,
  141. &clientv3.SortOption{Target: clientv3.SortByCreatedRev, Order: clientv3.SortDescend},
  142. false,
  143. []*storagepb.KeyValue{
  144. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  145. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  146. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  147. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  148. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  149. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  150. },
  151. },
  152. // range all with SortByModifiedRev, SortDescend
  153. {
  154. "a", "x",
  155. 0,
  156. &clientv3.SortOption{Target: clientv3.SortByModifiedRev, Order: clientv3.SortDescend},
  157. false,
  158. []*storagepb.KeyValue{
  159. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  160. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  161. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  162. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  163. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  164. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  165. },
  166. },
  167. }
  168. for i, tt := range tests {
  169. opts := []clientv3.OpOption{clientv3.WithRange(tt.end), clientv3.WithRev(tt.rev)}
  170. if tt.sortOption != nil {
  171. opts = append(opts, clientv3.WithSort(tt.sortOption.Target, tt.sortOption.Order))
  172. }
  173. if tt.serializable == true {
  174. opts = append(opts, clientv3.WithSerializable())
  175. }
  176. resp, err := kv.Get(ctx, tt.begin, opts...)
  177. if err != nil {
  178. t.Fatalf("#%d: couldn't range (%v)", i, err)
  179. }
  180. if !reflect.DeepEqual(wheader, resp.Header) {
  181. t.Fatalf("#%d: wheader expected %+v, got %+v", i, wheader, resp.Header)
  182. }
  183. if !reflect.DeepEqual(tt.wantSet, resp.Kvs) {
  184. t.Fatalf("#%d: resp.Kvs expected %+v, got %+v", i, tt.wantSet, resp.Kvs)
  185. }
  186. }
  187. }
  188. func TestKVDeleteRange(t *testing.T) {
  189. defer testutil.AfterTest(t)
  190. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  191. defer clus.Terminate(t)
  192. kv := clientv3.NewKV(clus.RandClient())
  193. ctx := context.TODO()
  194. keySet := []string{"a", "b", "c", "c", "c", "d", "e", "f"}
  195. for i, key := range keySet {
  196. if _, err := kv.Put(ctx, key, ""); err != nil {
  197. t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
  198. }
  199. }
  200. tests := []struct {
  201. key, end string
  202. delRev int64
  203. }{
  204. {"a", "b", int64(len(keySet) + 2)}, // delete [a, b)
  205. {"d", "f", int64(len(keySet) + 3)}, // delete [d, f)
  206. }
  207. for i, tt := range tests {
  208. dresp, err := kv.Delete(ctx, tt.key, clientv3.WithRange(tt.end))
  209. if err != nil {
  210. t.Fatalf("#%d: couldn't delete range (%v)", i, err)
  211. }
  212. if dresp.Header.Revision != tt.delRev {
  213. t.Fatalf("#%d: dresp.Header.Revision got %d, want %d", i, dresp.Header.Revision, tt.delRev)
  214. }
  215. resp, err := kv.Get(ctx, tt.key, clientv3.WithRange(tt.end))
  216. if err != nil {
  217. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  218. }
  219. if len(resp.Kvs) > 0 {
  220. t.Fatalf("#%d: resp.Kvs expected none, but got %+v", i, resp.Kvs)
  221. }
  222. }
  223. }
  224. func TestKVDelete(t *testing.T) {
  225. defer testutil.AfterTest(t)
  226. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  227. defer clus.Terminate(t)
  228. kv := clientv3.NewKV(clus.RandClient())
  229. ctx := context.TODO()
  230. presp, err := kv.Put(ctx, "foo", "")
  231. if err != nil {
  232. t.Fatalf("couldn't put 'foo' (%v)", err)
  233. }
  234. if presp.Header.Revision != 2 {
  235. t.Fatalf("presp.Header.Revision got %d, want %d", presp.Header.Revision, 2)
  236. }
  237. resp, err := kv.Delete(ctx, "foo")
  238. if err != nil {
  239. t.Fatalf("couldn't delete key (%v)", err)
  240. }
  241. if resp.Header.Revision != 3 {
  242. t.Fatalf("resp.Header.Revision got %d, want %d", resp.Header.Revision, 3)
  243. }
  244. gresp, err := kv.Get(ctx, "foo")
  245. if err != nil {
  246. t.Fatalf("couldn't get key (%v)", err)
  247. }
  248. if len(gresp.Kvs) > 0 {
  249. t.Fatalf("gresp.Kvs got %+v, want none", gresp.Kvs)
  250. }
  251. }
  252. func TestKVCompact(t *testing.T) {
  253. defer testutil.AfterTest(t)
  254. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  255. defer clus.Terminate(t)
  256. kv := clientv3.NewKV(clus.RandClient())
  257. ctx := context.TODO()
  258. for i := 0; i < 10; i++ {
  259. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  260. t.Fatalf("couldn't put 'foo' (%v)", err)
  261. }
  262. }
  263. err := kv.Compact(ctx, 7)
  264. if err != nil {
  265. t.Fatalf("couldn't compact kv space (%v)", err)
  266. }
  267. err = kv.Compact(ctx, 7)
  268. if err == nil || err != v3rpc.ErrCompacted {
  269. t.Fatalf("error got %v, want %v", err, v3rpc.ErrFutureRev)
  270. }
  271. wc := clientv3.NewWatcher(clus.RandClient())
  272. defer wc.Close()
  273. wchan := wc.Watch(ctx, "foo", 3)
  274. if wr := <-wchan; wr.CompactRevision != 7 {
  275. t.Fatalf("wchan CompactRevision got %v, want 7", wr.CompactRevision)
  276. }
  277. if wr, ok := <-wchan; ok {
  278. t.Fatalf("wchan got %v, expected closed", wr)
  279. }
  280. err = kv.Compact(ctx, 1000)
  281. if err == nil || err != v3rpc.ErrFutureRev {
  282. t.Fatalf("error got %v, want %v", err, v3rpc.ErrFutureRev)
  283. }
  284. }
  285. // TestKVGetRetry ensures get will retry on disconnect.
  286. func TestKVGetRetry(t *testing.T) {
  287. defer testutil.AfterTest(t)
  288. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  289. defer clus.Terminate(t)
  290. kv := clientv3.NewKV(clus.Client(0))
  291. ctx := context.TODO()
  292. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  293. t.Fatal(err)
  294. }
  295. clus.Members[0].Stop(t)
  296. <-clus.Members[0].StopNotify()
  297. donec := make(chan struct{})
  298. go func() {
  299. // Get will fail, but reconnect will trigger
  300. gresp, gerr := kv.Get(ctx, "foo")
  301. if gerr != nil {
  302. t.Fatal(gerr)
  303. }
  304. wkvs := []*storagepb.KeyValue{
  305. {
  306. Key: []byte("foo"),
  307. Value: []byte("bar"),
  308. CreateRevision: 2,
  309. ModRevision: 2,
  310. Version: 1,
  311. },
  312. }
  313. if !reflect.DeepEqual(gresp.Kvs, wkvs) {
  314. t.Fatalf("bad get: got %v, want %v", gresp.Kvs, wkvs)
  315. }
  316. donec <- struct{}{}
  317. }()
  318. time.Sleep(100 * time.Millisecond)
  319. clus.Members[0].Restart(t)
  320. select {
  321. case <-time.After(5 * time.Second):
  322. t.Fatalf("timed out waiting for get")
  323. case <-donec:
  324. }
  325. }
  326. // TestKVPutFailGetRetry ensures a get will retry following a failed put.
  327. func TestKVPutFailGetRetry(t *testing.T) {
  328. defer testutil.AfterTest(t)
  329. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  330. defer clus.Terminate(t)
  331. kv := clientv3.NewKV(clus.Client(0))
  332. ctx := context.TODO()
  333. clus.Members[0].Stop(t)
  334. <-clus.Members[0].StopNotify()
  335. _, err := kv.Put(ctx, "foo", "bar")
  336. if err == nil {
  337. t.Fatalf("got success on disconnected put, wanted error")
  338. }
  339. donec := make(chan struct{})
  340. go func() {
  341. // Get will fail, but reconnect will trigger
  342. gresp, gerr := kv.Get(ctx, "foo")
  343. if gerr != nil {
  344. t.Fatal(gerr)
  345. }
  346. if len(gresp.Kvs) != 0 {
  347. t.Fatalf("bad get kvs: got %+v, want empty", gresp.Kvs)
  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. }