client_test.go 5.3 KB

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