Skip to content

Commit

Permalink
clientv3/integration: add TestKVRange
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Jan 29, 2016
1 parent e552791 commit cf15842
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions clientv3/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package integration

import (
"bytes"
"reflect"
"testing"

"github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/integration"
"github.com/coreos/etcd/lease"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/storage/storagepb"
)

func TestKVPut(t *testing.T) {
Expand Down Expand Up @@ -61,3 +64,123 @@ func TestKVPut(t *testing.T) {
}
}
}

func TestKVRange(t *testing.T) {
defer testutil.AfterTest(t)

clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
defer clus.Terminate(t)

kv := clientv3.NewKV(clus.RandClient())

keySet := []string{"a", "b", "c", "c", "c", "foo", "foo/abc", "fop"}
for i, key := range keySet {
if _, err := kv.Put(key, "", lease.NoLease); err != nil {
t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
}
}

tests := []struct {
begin, end string
rev int64
sortOption *clientv3.SortOption

wantSet []*storagepb.KeyValue
}{
// range first two
{
"a", "c",
0,
nil,

[]*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
},
},
// range all with rev
{
"a", "x",
2,
nil,

[]*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
},
},
// range all with SortByKey, SortAscend
{
"a", "x",
0,
&clientv3.SortOption{Target: clientv3.SortByKey, Order: clientv3.SortAscend},

[]*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
{Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
{Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
{Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
},
},
// range all with SortByCreatedRev, SortDescend
{
"a", "x",
0,
&clientv3.SortOption{Target: clientv3.SortByCreatedRev, Order: clientv3.SortDescend},

[]*storagepb.KeyValue{
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
{Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
{Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
{Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
},
},
// range all with SortByModifiedRev, SortDescend
{
"a", "x",
0,
&clientv3.SortOption{Target: clientv3.SortByModifiedRev, Order: clientv3.SortDescend},

[]*storagepb.KeyValue{
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
{Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
{Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
{Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
},
},
}

wHeader := &pb.ResponseHeader{}
for i, tt := range tests {
resp, err := kv.Range(tt.begin, tt.end, 0, tt.rev, tt.sortOption)
if err != nil {
t.Fatalf("#%d: couldn't range (%v)", i, err)
}
if i == 0 {
wHeader.RaftTerm = resp.Header.RaftTerm
wHeader.ClusterId = resp.Header.ClusterId
wHeader.MemberId = resp.Header.MemberId
}
if tt.rev == 0 {
if int64(len(keySet)+1) != resp.Header.Revision {
t.Fatalf("#%d: resp.Header.Revision expected %d, got %d", i, len(keySet)+1, resp.Header.Revision)
}
} else {
if int64(tt.rev) != resp.Header.Revision {
t.Fatalf("#%d: resp.Header.Revision expected %d, got %d", i, tt.rev, resp.Header.Revision)
}
}
resp.Header.Revision = 0 // to compare everything except Revision
if !reflect.DeepEqual(wHeader, resp.Header) {
t.Fatalf("#%d: expected: %+v, resp.Header: %+v", i, wHeader, resp.Header)
}
if !reflect.DeepEqual(tt.wantSet, resp.Kvs) {
t.Fatalf("#%d: expected: %+v, resp.Kvs: %+v", i, tt.wantSet, resp.Kvs)
}
}
}

0 comments on commit cf15842

Please sign in to comment.