kv_test.go 11 KB

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