diff --git a/tests/common/kv_test.go b/tests/common/kv_test.go index 0e2af0956..a2c16f89f 100644 --- a/tests/common/kv_test.go +++ b/tests/common/kv_test.go @@ -18,6 +18,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/tests/v3/framework/config" "go.etcd.io/etcd/tests/v3/framework/testutils" ) @@ -78,3 +80,76 @@ func TestKVPut(t *testing.T) { }) } } + +func TestKVGet(t *testing.T) { + testRunner.BeforeTest(t) + tcs := []struct { + name string + config config.ClusterConfig + }{ + { + name: "NoTLS", + config: config.ClusterConfig{ClusterSize: 1}, + }, + { + name: "PeerTLS", + config: config.ClusterConfig{ClusterSize: 1, PeerTLS: config.ManualTLS}, + }, + { + name: "PeerAutoTLS", + config: config.ClusterConfig{ClusterSize: 1, PeerTLS: config.AutoTLS}, + }, + { + name: "ClientTLS", + config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.ManualTLS}, + }, + { + name: "ClientAutoTLS", + config: config.ClusterConfig{ClusterSize: 1, ClientTLS: config.AutoTLS}, + }, + } + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + clus := testRunner.NewCluster(t, tc.config) + defer clus.Close() + cc := clus.Client() + + testutils.ExecuteWithTimeout(t, 10*time.Second, func() { + var ( + kvs = []testutils.KeyValue{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}} + revkvs = []testutils.KeyValue{{"key3", "val3"}, {"key2", "val2"}, {"key1", "val1"}} + ) + for i := range kvs { + if err := cc.Put(kvs[i].Key, kvs[i].Value); err != nil { + t.Fatalf("count not put key %q, err: %s", kvs[i].Key, err) + } + } + tests := []struct { + key string + options config.GetOptions + + wkv []testutils.KeyValue + }{ + {key: "key1", wkv: []testutils.KeyValue{{"key1", "val1"}}}, + {key: "", options: config.GetOptions{Prefix: true}, wkv: kvs}, + {key: "", options: config.GetOptions{FromKey: true}, wkv: kvs}, + {key: "key", options: config.GetOptions{Prefix: true}, wkv: kvs}, + {key: "key", options: config.GetOptions{Prefix: true, Limit: 2}, wkv: kvs[:2]}, + {key: "key", options: config.GetOptions{Prefix: true, Order: clientv3.SortAscend, SortBy: clientv3.SortByModRevision}, wkv: kvs}, + {key: "key", options: config.GetOptions{Prefix: true, Order: clientv3.SortAscend, SortBy: clientv3.SortByVersion}, wkv: kvs}, + {key: "key", options: config.GetOptions{Prefix: true, Order: clientv3.SortNone, SortBy: clientv3.SortByCreateRevision}, wkv: kvs}, + {key: "key", options: config.GetOptions{Prefix: true, Order: clientv3.SortDescend, SortBy: clientv3.SortByCreateRevision}, wkv: revkvs}, + {key: "key", options: config.GetOptions{Prefix: true, Order: clientv3.SortDescend, SortBy: clientv3.SortByKey}, wkv: revkvs}, + } + for _, tt := range tests { + resp, err := cc.Get(tt.key, tt.options) + if err != nil { + t.Fatalf("count not get key %q, err: %s", tt.key[0], err) + } + kvs := testutils.FromGetResponse(resp) + assert.Equal(t, tt.wkv, kvs) + } + }) + }) + } +} diff --git a/tests/e2e/ctl_v3_kv_test.go b/tests/e2e/ctl_v3_kv_test.go index 4c094d856..4c902cae7 100644 --- a/tests/e2e/ctl_v3_kv_test.go +++ b/tests/e2e/ctl_v3_kv_test.go @@ -29,13 +29,6 @@ func TestCtlV3PutClientTLSFlagByEnv(t *testing.T) { func TestCtlV3PutIgnoreValue(t *testing.T) { testCtl(t, putTestIgnoreValue) } func TestCtlV3PutIgnoreLease(t *testing.T) { testCtl(t, putTestIgnoreLease) } -func TestCtlV3Get(t *testing.T) { testCtl(t, getTest) } -func TestCtlV3GetNoTLS(t *testing.T) { testCtl(t, getTest, withCfg(*e2e.NewConfigNoTLS())) } -func TestCtlV3GetClientTLS(t *testing.T) { testCtl(t, getTest, withCfg(*e2e.NewConfigClientTLS())) } -func TestCtlV3GetClientAutoTLS(t *testing.T) { - testCtl(t, getTest, withCfg(*e2e.NewConfigClientAutoTLS())) -} -func TestCtlV3GetPeerTLS(t *testing.T) { testCtl(t, getTest, withCfg(*e2e.NewConfigPeerTLS())) } func TestCtlV3GetTimeout(t *testing.T) { testCtl(t, getTest, withDialTimeout(0)) } func TestCtlV3GetQuorum(t *testing.T) { testCtl(t, getTest, withQuorum()) } diff --git a/tests/framework/testutils/util.go b/tests/framework/testutils/execute.go similarity index 100% rename from tests/framework/testutils/util.go rename to tests/framework/testutils/execute.go diff --git a/tests/framework/testutils/kvs.go b/tests/framework/testutils/kvs.go new file mode 100644 index 000000000..d61ab6742 --- /dev/null +++ b/tests/framework/testutils/kvs.go @@ -0,0 +1,28 @@ +// Copyright 2022 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutils + +import clientv3 "go.etcd.io/etcd/client/v3" + +type KeyValue struct { + Key, Value string +} + +func FromGetResponse(resp *clientv3.GetResponse) (kvs []KeyValue) { + for _, kv := range resp.Kvs { + kvs = append(kvs, KeyValue{Key: string(kv.Key), Value: string(kv.Value)}) + } + return kvs +}