Skip to content

Commit

Permalink
tests: Migrate noquorum kv tests to common framework
Browse files Browse the repository at this point in the history
  • Loading branch information
serathius committed Mar 2, 2022
1 parent d2e5a8c commit cd303f5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 87 deletions.
39 changes: 39 additions & 0 deletions tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,42 @@ func TestKVGet(t *testing.T) {
})
}
}

func TestKVGetNoQuorum(t *testing.T) {
testRunner.BeforeTest(t)
tcs := []struct {
name string
options config.GetOptions

wantError bool
}{
{
name: "Serializable",
options: config.GetOptions{Serializable: true},
},
{
name: "Linearizable",
options: config.GetOptions{Serializable: false, Timeout: time.Second},
wantError: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
clus := testRunner.NewCluster(t, config.ClusterConfig{ClusterSize: 3})
defer clus.Close()

clus.Members()[0].Stop()
clus.Members()[1].Stop()

cc := clus.Members()[2].Client()
testutils.ExecuteWithTimeout(t, 10*time.Second, func() {
key := "foo"
_, err := cc.Get(key, tc.options)
gotError := err != nil
if gotError != tc.wantError {
t.Fatalf("Unexpeted result, wantError: %v, gotErr: %v, err: %s", tc.wantError, gotError, err)
}
})
})
}
}
83 changes: 0 additions & 83 deletions tests/e2e/ctl_v3_kv_no_quorum_test.go

This file was deleted.

7 changes: 6 additions & 1 deletion tests/framework/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package config

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

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

type GetOptions struct {
Revision int
Expand All @@ -26,4 +30,5 @@ type GetOptions struct {
Limit int
Order clientv3.SortOrder
SortBy clientv3.SortTarget
Timeout time.Duration
}
24 changes: 24 additions & 0 deletions tests/framework/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ func (c *e2eCluster) Client() Client {
return e2eClient{e2e.NewEtcdctl(c.Cfg, c.EndpointsV3())}
}

func (c *e2eCluster) Members() (ms []Member) {
for _, proc := range c.EtcdProcessCluster.Procs {
ms = append(ms, e2eMember{EtcdProcess: proc, Cfg: c.Cfg})
}
return ms
}

type e2eClient struct {
*e2e.EtcdctlV3
}

type e2eMember struct {
e2e.EtcdProcess
Cfg *e2e.EtcdProcessClusterConfig
}

func (m e2eMember) Client() Client {
return e2eClient{e2e.NewEtcdctl(m.Cfg, m.EndpointsV3())}
}

func (m e2eMember) Start() error {
return m.Restart()
}

func (m e2eMember) Stop() {
m.EtcdProcess.Stop()
}
5 changes: 4 additions & 1 deletion tests/framework/e2e/etcdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (ctl *EtcdctlV3) DowngradeEnable(version string) error {

func (ctl *EtcdctlV3) Get(key string, o config.GetOptions) (*clientv3.GetResponse, error) {
args := ctl.cmdArgs()
if o.Timeout != 0 {
args = append(args, fmt.Sprintf("--command-timeout=%s", o.Timeout))
}
if o.Serializable {
args = append(args, "--consistency", "s")
}
Expand Down Expand Up @@ -98,7 +101,7 @@ func (ctl *EtcdctlV3) Get(key string, o config.GetOptions) (*clientv3.GetRespons
_, err := cmd.Expect("Count")
return &resp, err
}
line, err := cmd.Expect("kvs")
line, err := cmd.Expect("header")
if err != nil {
return nil, err
}
Expand Down
34 changes: 32 additions & 2 deletions tests/framework/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ type integrationCluster struct {
t testing.TB
}

func (c *integrationCluster) Members() (ms []Member) {
for _, m := range c.Cluster.Members {
ms = append(ms, integrationMember{m, c.t})
}
return ms
}

type integrationMember struct {
*integration.Member
t testing.TB
}

func (m integrationMember) Client() Client {
return integrationClient{m.Member.Client}
}

func (m integrationMember) Start() error {
return m.Member.Restart(m.t)
}

func (m integrationMember) Stop() {
m.Member.Stop(m.t)
}

func (c *integrationCluster) Close() error {
c.Terminate(c.t)
return nil
Expand All @@ -87,14 +111,20 @@ func (c *integrationCluster) Client() Client {
if err != nil {
c.t.Fatal(err)
}
return &integrationClient{cc}
return integrationClient{cc}
}

type integrationClient struct {
*clientv3.Client
}

func (c integrationClient) Get(key string, o config.GetOptions) (*clientv3.GetResponse, error) {
ctx := context.Background()
if o.Timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, o.Timeout)
defer cancel()
}
clientOpts := []clientv3.OpOption{}
if o.Revision != 0 {
clientOpts = append(clientOpts, clientv3.WithRev(int64(o.Revision)))
Expand All @@ -120,7 +150,7 @@ func (c integrationClient) Get(key string, o config.GetOptions) (*clientv3.GetRe
if o.SortBy != clientv3.SortByKey || o.Order != clientv3.SortNone {
clientOpts = append(clientOpts, clientv3.WithSort(o.SortBy, o.Order))
}
return c.Client.Get(context.Background(), key, clientOpts...)
return c.Client.Get(ctx, key, clientOpts...)
}

func (c integrationClient) Put(key, value string) error {
Expand Down
7 changes: 7 additions & 0 deletions tests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ type testRunner interface {
}

type Cluster interface {
Members() []Member
Client() Client
Close() error
}

type Member interface {
Client() Client
Start() error
Stop()
}

type Client interface {
Expand Down

0 comments on commit cd303f5

Please sign in to comment.