tests: Migrate TestKVGet e2e test to common test framework

This commit is contained in:
Marek Siarkowicz 2022-02-18 14:23:51 +01:00
parent 81ef11ffb8
commit 74d77dbaaa
4 changed files with 103 additions and 7 deletions

View File

@ -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)
}
})
})
}
}

View File

@ -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()) }

View File

@ -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
}