Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate key value Get to common framework #13740

Merged
merged 7 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 139 additions & 24 deletions tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,148 @@ 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"
)

func TestKVPut(t *testing.T) {
testRunner.BeforeTest(t)
clus := testRunner.NewCluster(t)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteWithTimeout(t, 10*time.Second, func() {
key, value := "foo", "bar"

if err := cc.Put(key, value); err != nil {
t.Fatalf("count not put key %q, err: %s", key, err)
}
resp, err := cc.Get(key, testutils.WithSerializable())
if err != nil {
t.Fatalf("count not get key %q, err: %s", key, err)
}
if len(resp.Kvs) != 1 {
t.Errorf("Unexpected lenth of response, got %d", len(resp.Kvs))
}
if string(resp.Kvs[0].Key) != key {
t.Errorf("Unexpected key, want %q, got %q", key, resp.Kvs[0].Key)
}
if string(resp.Kvs[0].Value) != value {
t.Errorf("Unexpected value, want %q, got %q", value, resp.Kvs[0].Value)
}
})
tcs := []struct {
name string
config config.ClusterConfig
}{
{
name: "NoTLS",
config: config.ClusterConfig{ClusterSize: 1},
},
{
name: "PeerTLS",
config: config.ClusterConfig{ClusterSize: 3, PeerTLS: config.ManualTLS},
},
{
name: "PeerAutoTLS",
config: config.ClusterConfig{ClusterSize: 3, 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() {
key, value := "foo", "bar"

if err := cc.Put(key, value); err != nil {
t.Fatalf("count not put key %q, err: %s", key, err)
}
resp, err := cc.Get(key, config.GetOptions{Serializable: true})
if err != nil {
t.Fatalf("count not get key %q, err: %s", key, err)
}
if len(resp.Kvs) != 1 {
t.Errorf("Unexpected lenth of response, got %d", len(resp.Kvs))
}
if string(resp.Kvs[0].Key) != key {
t.Errorf("Unexpected key, want %q, got %q", key, resp.Kvs[0].Key)
}
if string(resp.Kvs[0].Value) != value {
t.Errorf("Unexpected value, want %q, got %q", value, resp.Kvs[0].Value)
}
})
})
}
}

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: 3, PeerTLS: config.ManualTLS},
},
{
name: "PeerAutoTLS",
config: config.ClusterConfig{ClusterSize: 3, 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 = []string{"a", "b", "c", "c", "c", "foo", "foo/abc", "fop"}
wantKvs = []string{"a", "b", "c", "foo", "foo/abc", "fop"}
kvsByVersion = []string{"a", "b", "foo", "foo/abc", "fop", "c"}
reversedKvs = []string{"fop", "foo/abc", "foo", "c", "b", "a"}
)

for i := range kvs {
if err := cc.Put(kvs[i], "bar"); err != nil {
t.Fatalf("count not put key %q, err: %s", kvs[i], err)
}
}
tests := []struct {
begin string
end string
options config.GetOptions

wkv []string
}{
{begin: "a", wkv: wantKvs[:1]},
{begin: "a", options: config.GetOptions{Serializable: true}, wkv: wantKvs[:1]},
{begin: "a", options: config.GetOptions{End: "c"}, wkv: wantKvs[:2]},
{begin: "", options: config.GetOptions{Prefix: true}, wkv: wantKvs},
{begin: "", options: config.GetOptions{FromKey: true}, wkv: wantKvs},
{begin: "a", options: config.GetOptions{End: "x"}, wkv: wantKvs},
{begin: "", options: config.GetOptions{Prefix: true, Revision: 4}, wkv: kvs[:3]},
{begin: "a", options: config.GetOptions{CountOnly: true}, wkv: nil},
{begin: "foo", options: config.GetOptions{Prefix: true}, wkv: []string{"foo", "foo/abc"}},
{begin: "foo", options: config.GetOptions{FromKey: true}, wkv: []string{"foo", "foo/abc", "fop"}},
{begin: "", options: config.GetOptions{Prefix: true, Limit: 2}, wkv: wantKvs[:2]},
{begin: "", options: config.GetOptions{Prefix: true, Order: clientv3.SortAscend, SortBy: clientv3.SortByModRevision}, wkv: wantKvs},
{begin: "", options: config.GetOptions{Prefix: true, Order: clientv3.SortAscend, SortBy: clientv3.SortByVersion}, wkv: kvsByVersion},
{begin: "", options: config.GetOptions{Prefix: true, Order: clientv3.SortNone, SortBy: clientv3.SortByCreateRevision}, wkv: wantKvs},
{begin: "", options: config.GetOptions{Prefix: true, Order: clientv3.SortDescend, SortBy: clientv3.SortByCreateRevision}, wkv: reversedKvs},
{begin: "", options: config.GetOptions{Prefix: true, Order: clientv3.SortDescend, SortBy: clientv3.SortByKey}, wkv: reversedKvs},
}
for _, tt := range tests {
resp, err := cc.Get(tt.begin, tt.options)
if err != nil {
t.Fatalf("count not get key %q, err: %s", tt.begin, err)
}
kvs := testutils.KeysFromGetResponse(resp)
assert.Equal(t, tt.wkv, kvs)
}
})
})
}
}
14 changes: 0 additions & 14 deletions tests/e2e/ctl_v3_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,14 @@ import (
"go.etcd.io/etcd/tests/v3/framework/e2e"
)

func TestCtlV3PutNoTLS(t *testing.T) { testCtl(t, putTest, withCfg(*e2e.NewConfigNoTLS())) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serathius I am curious why are we removing bunch of test functions? Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the tests are moved into tests/common/kv_test.go.

Copy link
Member Author

@serathius serathius Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this are exact tests that are migrated, as we are combining e2e and integration tests I remove test cases from both directories.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, cool. Thanks both!!

func TestCtlV3PutClientTLS(t *testing.T) { testCtl(t, putTest, withCfg(*e2e.NewConfigClientTLS())) }
func TestCtlV3PutClientAutoTLS(t *testing.T) {
testCtl(t, putTest, withCfg(*e2e.NewConfigClientAutoTLS()))
}
func TestCtlV3PutPeerTLS(t *testing.T) { testCtl(t, putTest, withCfg(*e2e.NewConfigPeerTLS())) }
func TestCtlV3PutTimeout(t *testing.T) { testCtl(t, putTest, withDialTimeout(0)) }
func TestCtlV3PutClientTLSFlagByEnv(t *testing.T) {
testCtl(t, putTest, withCfg(*e2e.NewConfigClientTLS()), withFlagByEnv())
}
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()) }

func TestCtlV3GetFormat(t *testing.T) { testCtl(t, getFormatTest) }
func TestCtlV3GetRev(t *testing.T) { testCtl(t, getRevTest) }
Expand Down
29 changes: 29 additions & 0 deletions tests/framework/config/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 config

import clientv3 "go.etcd.io/etcd/client/v3"

type GetOptions struct {
Revision int
End string
CountOnly bool
Serializable bool
Prefix bool
FromKey bool
Limit int
Order clientv3.SortOrder
SortBy clientv3.SortTarget
}
29 changes: 29 additions & 0 deletions tests/framework/config/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 config

type TLSConfig string

const (
NoTLS TLSConfig = ""
AutoTLS TLSConfig = "auto-tls"
ManualTLS TLSConfig = "manual-tls"
serathius marked this conversation as resolved.
Show resolved Hide resolved
)

type ClusterConfig struct {
ClusterSize int
PeerTLS TLSConfig
ClientTLS TLSConfig
}
44 changes: 32 additions & 12 deletions tests/framework/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import (
"testing"

"go.etcd.io/etcd/client/pkg/v3/testutil"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/e2e"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)

type e2eRunner struct{}
Expand All @@ -39,8 +38,37 @@ func (e e2eRunner) BeforeTest(t testing.TB) {
e2e.BeforeTest(t)
}

func (e e2eRunner) NewCluster(t testing.TB) Cluster {
epc, err := e2e.NewEtcdProcessCluster(t, e2e.ConfigStandalone(*e2e.NewConfigAutoTLS()))
func (e e2eRunner) NewCluster(t testing.TB, cfg config.ClusterConfig) Cluster {
e2eConfig := e2e.EtcdProcessClusterConfig{
InitialToken: "new",
ClusterSize: cfg.ClusterSize,
}
switch cfg.ClientTLS {
case config.NoTLS:
e2eConfig.ClientTLS = e2e.ClientNonTLS
case config.AutoTLS:
e2eConfig.IsClientAutoTLS = true
e2eConfig.ClientTLS = e2e.ClientTLS
case config.ManualTLS:
e2eConfig.IsClientAutoTLS = false
e2eConfig.ClientTLS = e2e.ClientTLS
default:
t.Fatalf("ClientTLS config %q not supported", cfg.ClientTLS)
}
switch cfg.PeerTLS {
case config.NoTLS:
e2eConfig.IsPeerTLS = false
e2eConfig.IsPeerAutoTLS = false
case config.AutoTLS:
e2eConfig.IsPeerTLS = true
e2eConfig.IsPeerAutoTLS = true
case config.ManualTLS:
e2eConfig.IsPeerTLS = true
e2eConfig.IsPeerAutoTLS = false
default:
t.Fatalf("PeerTLS config %q not supported", cfg.PeerTLS)
}
epc, err := e2e.NewEtcdProcessCluster(t, &e2eConfig)
if err != nil {
t.Fatalf("could not start etcd integrationCluster: %s", err)
}
Expand All @@ -58,11 +86,3 @@ func (c *e2eCluster) Client() Client {
type e2eClient struct {
*e2e.EtcdctlV3
}

func (c e2eClient) Get(key string, opts ...testutils.GetOption) (*clientv3.GetResponse, error) {
o := testutils.GetOptions{}
for _, opt := range opts {
opt(&o)
}
return c.EtcdctlV3.Get(key, o)
}
Loading