kv_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/lease"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. "github.com/coreos/etcd/storage/storagepb"
  25. )
  26. func TestKVPut(t *testing.T) {
  27. defer testutil.AfterTest(t)
  28. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  29. defer clus.Terminate(t)
  30. lapi := clientv3.NewLease(clus.RandClient())
  31. defer lapi.Close()
  32. kv := clientv3.NewKV(clus.RandClient())
  33. resp, err := lapi.Create(context.Background(), 10)
  34. if err != nil {
  35. t.Fatalf("failed to create lease %v", err)
  36. }
  37. tests := []struct {
  38. key, val string
  39. leaseID lease.LeaseID
  40. }{
  41. {"foo", "bar", lease.NoLease},
  42. {"hello", "world", lease.LeaseID(resp.ID)},
  43. }
  44. for i, tt := range tests {
  45. if _, err := kv.Put(tt.key, tt.val, tt.leaseID); err != nil {
  46. t.Fatalf("#%d: couldn't put %q (%v)", i, tt.key, err)
  47. }
  48. resp, err := kv.Get(tt.key, 0)
  49. if err != nil {
  50. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  51. }
  52. if len(resp.Kvs) != 1 {
  53. t.Fatalf("#%d: expected 1 key, got %d", i, len(resp.Kvs))
  54. }
  55. if !bytes.Equal([]byte(tt.val), resp.Kvs[0].Value) {
  56. t.Errorf("#%d: val = %s, want %s", i, tt.val, resp.Kvs[0].Value)
  57. }
  58. if tt.leaseID != lease.LeaseID(resp.Kvs[0].Lease) {
  59. t.Errorf("#%d: val = %d, want %d", i, tt.leaseID, resp.Kvs[0].Lease)
  60. }
  61. }
  62. }
  63. func TestKVRange(t *testing.T) {
  64. defer testutil.AfterTest(t)
  65. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  66. defer clus.Terminate(t)
  67. kv := clientv3.NewKV(clus.RandClient())
  68. keySet := []string{"a", "b", "c", "c", "c", "foo", "foo/abc", "fop"}
  69. for i, key := range keySet {
  70. if _, err := kv.Put(key, "", lease.NoLease); err != nil {
  71. t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
  72. }
  73. }
  74. resp, err := kv.Get(keySet[0], 0)
  75. if err != nil {
  76. t.Fatalf("couldn't get key (%v)", err)
  77. }
  78. wheader := resp.Header
  79. tests := []struct {
  80. begin, end string
  81. rev int64
  82. sortOption *clientv3.SortOption
  83. wantSet []*storagepb.KeyValue
  84. }{
  85. // range first two
  86. {
  87. "a", "c",
  88. 0,
  89. nil,
  90. []*storagepb.KeyValue{
  91. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  92. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  93. },
  94. },
  95. // range all with rev
  96. {
  97. "a", "x",
  98. 2,
  99. nil,
  100. []*storagepb.KeyValue{
  101. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  102. },
  103. },
  104. // range all with SortByKey, SortAscend
  105. {
  106. "a", "x",
  107. 0,
  108. &clientv3.SortOption{Target: clientv3.SortByKey, Order: clientv3.SortAscend},
  109. []*storagepb.KeyValue{
  110. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  111. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  112. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  113. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  114. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  115. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  116. },
  117. },
  118. // range all with SortByCreatedRev, SortDescend
  119. {
  120. "a", "x",
  121. 0,
  122. &clientv3.SortOption{Target: clientv3.SortByCreatedRev, Order: clientv3.SortDescend},
  123. []*storagepb.KeyValue{
  124. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  125. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  126. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  127. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  128. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  129. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  130. },
  131. },
  132. // range all with SortByModifiedRev, SortDescend
  133. {
  134. "a", "x",
  135. 0,
  136. &clientv3.SortOption{Target: clientv3.SortByModifiedRev, Order: 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. }
  147. for i, tt := range tests {
  148. resp, err := kv.Range(tt.begin, tt.end, 0, tt.rev, tt.sortOption)
  149. if err != nil {
  150. t.Fatalf("#%d: couldn't range (%v)", i, err)
  151. }
  152. if !reflect.DeepEqual(wheader, resp.Header) {
  153. t.Fatalf("#%d: wheader expected %+v, got %+v", i, wheader, resp.Header)
  154. }
  155. if !reflect.DeepEqual(tt.wantSet, resp.Kvs) {
  156. t.Fatalf("#%d: resp.Kvs expected %+v, got %+v", i, tt.wantSet, resp.Kvs)
  157. }
  158. }
  159. }