Skip to content

Commit

Permalink
Merge pull request #14331 from chaochn47/auth_test_framework_update
Browse files Browse the repository at this point in the history
common tests framework: cluster client creation could fail with invalid auth
  • Loading branch information
ahrtr authored Sep 30, 2022
2 parents 62d979b + 8d057ea commit 7fff4c4
Show file tree
Hide file tree
Showing 24 changed files with 298 additions and 99 deletions.
4 changes: 4 additions & 0 deletions client/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ type AuthConfig struct {
Password string `json:"password"`
}

func (cfg AuthConfig) Empty() bool {
return cfg.Username == "" && cfg.Password == ""
}

// NewClientConfig creates a Config based on the provided ConfigSpec.
func NewClientConfig(confSpec *ConfigSpec, lg *zap.Logger) (*Config, error) {
tlsCfg, err := newTLSConfig(confSpec.Secure, lg)
Expand Down
23 changes: 13 additions & 10 deletions tests/common/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -32,17 +33,18 @@ func TestAlarm(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 1, QuotaBackendBytes: int64(13 * os.Getpagesize())})
defer clus.Close()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
// test small put still works
smallbuf := strings.Repeat("a", 64)
if err := clus.Client().Put(ctx, "1st_test", smallbuf, config.PutOptions{}); err != nil {
if err := cc.Put(ctx, "1st_test", smallbuf, config.PutOptions{}); err != nil {
t.Fatalf("alarmTest: put kv error (%v)", err)
}

// write some chunks to fill up the database
buf := strings.Repeat("b", os.Getpagesize())
for {
if err := clus.Client().Put(ctx, "2nd_test", buf, config.PutOptions{}); err != nil {
if err := cc.Put(ctx, "2nd_test", buf, config.PutOptions{}); err != nil {
if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
t.Fatal(err)
}
Expand All @@ -51,20 +53,20 @@ func TestAlarm(t *testing.T) {
}

// quota alarm should now be on
alarmResp, err := clus.Client().AlarmList(ctx)
alarmResp, err := cc.AlarmList(ctx)
if err != nil {
t.Fatalf("alarmTest: Alarm error (%v)", err)
}

// check that Put is rejected when alarm is on
if err := clus.Client().Put(ctx, "3rd_test", smallbuf, config.PutOptions{}); err != nil {
if err := cc.Put(ctx, "3rd_test", smallbuf, config.PutOptions{}); err != nil {
if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
t.Fatal(err)
}
}

// get latest revision to compact
sresp, err := clus.Client().Status(ctx)
sresp, err := cc.Status(ctx)
if err != nil {
t.Fatalf("get endpoint status error: %v", err)
}
Expand All @@ -77,12 +79,12 @@ func TestAlarm(t *testing.T) {
}

// make some space
_, err = clus.Client().Compact(ctx, rvs, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
_, err = cc.Compact(ctx, rvs, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
if err != nil {
t.Fatalf("alarmTest: Compact error (%v)", err)
}

if err = clus.Client().Defragment(ctx, config.DefragOption{Timeout: 10 * time.Second}); err != nil {
if err = cc.Defragment(ctx, config.DefragOption{Timeout: 10 * time.Second}); err != nil {
t.Fatalf("alarmTest: defrag error (%v)", err)
}

Expand All @@ -92,14 +94,14 @@ func TestAlarm(t *testing.T) {
MemberID: alarm.MemberID,
Alarm: alarm.Alarm,
}
_, err = clus.Client().AlarmDisarm(ctx, alarmMember)
_, err = cc.AlarmDisarm(ctx, alarmMember)
if err != nil {
t.Fatalf("alarmTest: Alarm error (%v)", err)
}
}

// put one more key below quota
if err := clus.Client().Put(ctx, "4th_test", smallbuf, config.PutOptions{}); err != nil {
if err := cc.Put(ctx, "4th_test", smallbuf, config.PutOptions{}); err != nil {
t.Fatal(err)
}
})
Expand All @@ -115,10 +117,11 @@ func TestAlarmlistOnMemberRestart(t *testing.T) {
SnapshotCount: 5,
})
defer clus.Close()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
for i := 0; i < 6; i++ {
if _, err := clus.Client().AlarmList(ctx); err != nil {
if _, err := cc.AlarmList(ctx); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
Expand Down
13 changes: 8 additions & 5 deletions tests/common/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"time"

"github.com/stretchr/testify/assert"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -47,27 +49,28 @@ func TestCompact(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
defer clus.Close()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}}
for i := range kvs {
if err := clus.Client().Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
if err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
t.Fatalf("compactTest #%d: put kv error (%v)", i, err)
}
}
get, err := clus.Client().Get(ctx, "key", config.GetOptions{Revision: 3})
get, err := cc.Get(ctx, "key", config.GetOptions{Revision: 3})
if err != nil {
t.Fatalf("compactTest: Get kv by revision error (%v)", err)
}

getkvs := testutils.KeyValuesFromGetResponse(get)
assert.Equal(t, kvs[1:2], getkvs)

_, err = clus.Client().Compact(ctx, 4, tc.options)
_, err = cc.Compact(ctx, 4, tc.options)
if err != nil {
t.Fatalf("compactTest: Compact error (%v)", err)
}

get, err = clus.Client().Get(ctx, "key", config.GetOptions{Revision: 3})
get, err = cc.Get(ctx, "key", config.GetOptions{Revision: 3})
if err != nil {
if !strings.Contains(err.Error(), "required revision has been compacted") {
t.Fatalf("compactTest: Get compact key error (%v)", err)
Expand All @@ -76,7 +79,7 @@ func TestCompact(t *testing.T) {
t.Fatalf("expected '...has been compacted' error, got <nil>")
}

_, err = clus.Client().Compact(ctx, 2, tc.options)
_, err = cc.Compact(ctx, 2, tc.options)
if err != nil {
if !strings.Contains(err.Error(), "required revision has been compacted") {
t.Fatal(err)
Expand Down
9 changes: 6 additions & 3 deletions tests/common/defrag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"testing"
"time"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -29,20 +31,21 @@ func TestDefragOnline(t *testing.T) {
defer cancel()
options := config.DefragOption{Timeout: 10 * time.Second}
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
defer clus.Close()
var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}}
for i := range kvs {
if err := clus.Client().Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
if err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
t.Fatalf("compactTest #%d: put kv error (%v)", i, err)
}
}
_, err := clus.Client().Compact(ctx, 4, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
_, err := cc.Compact(ctx, 4, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
if err != nil {
t.Fatalf("defrag_test: compact with revision error (%v)", err)
}

if err = clus.Client().Defragment(ctx, options); err != nil {
if err = cc.Defragment(ctx, options); err != nil {
t.Fatalf("defrag_test: defrag error (%v)", err)
}
})
Expand Down
11 changes: 8 additions & 3 deletions tests/common/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"testing"
"time"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -29,8 +31,9 @@ func TestEndpointStatus(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
defer clus.Close()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
_, err := clus.Client().Status(ctx)
_, err := cc.Status(ctx)
if err != nil {
t.Fatalf("get endpoint status error: %v", err)
}
Expand All @@ -43,8 +46,9 @@ func TestEndpointHashKV(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
defer clus.Close()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
_, err := clus.Client().HashKV(ctx, 0)
_, err := cc.HashKV(ctx, 0)
if err != nil {
t.Fatalf("get endpoint hashkv error: %v", err)
}
Expand All @@ -57,8 +61,9 @@ func TestEndpointHealth(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 3})
defer clus.Close()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
if err := clus.Client().Health(ctx); err != nil {
if err := cc.Health(ctx); err != nil {
t.Fatalf("get endpoint health error: %v", err)
}
})
Expand Down
7 changes: 4 additions & 3 deletions tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/assert"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -33,7 +34,7 @@ func TestKVPut(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
key, value := "foo", "bar"
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestKVGet(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
var (
Expand Down Expand Up @@ -127,7 +128,7 @@ func TestKVDelete(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
kvs := []string{"a", "b", "c", "c/abc", "d"}
tests := []struct {
Expand Down
11 changes: 6 additions & 5 deletions tests/common/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestLeaseGrantTimeToLive(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
ttl := int64(10)
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestLeaseGrantAndList(t *testing.T) {
t.Logf("Creating cluster...")
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
t.Logf("Created cluster and client")
testutils.ExecuteUntil(ctx, t, func() {
var createdLeases []clientv3.LeaseID
Expand Down Expand Up @@ -150,7 +151,7 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
leaseResp, err := cc.Grant(ctx, 2)
Expand Down Expand Up @@ -187,7 +188,7 @@ func TestLeaseGrantKeepAliveOnce(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
leaseResp, err := cc.Grant(ctx, 2)
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestLeaseGrantRevoke(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
leaseResp, err := cc.Grant(ctx, 20)
Expand Down
4 changes: 2 additions & 2 deletions tests/common/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestMemberList(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
resp, err := cc.MemberList(ctx)
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestMemberAdd(t *testing.T) {
c.DisableStrictReconfigCheck = !quorumTc.strictReconfigCheck
clus := testRunner.NewCluster(ctx, t, c)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
var addResp *clientv3.MemberAddResponse
Expand Down
11 changes: 6 additions & 5 deletions tests/common/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -34,7 +35,7 @@ func TestRoleAdd_Simple(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))

testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "root")
Expand All @@ -52,7 +53,7 @@ func TestRoleAdd_Error(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 1})
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "test-role")
if err != nil {
Expand All @@ -75,7 +76,7 @@ func TestRootRole(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 1})
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "root")
if err != nil {
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestRoleGrantRevokePermission(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 1})
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "role1")
if err != nil {
Expand Down Expand Up @@ -140,7 +141,7 @@ func TestRoleDelete(t *testing.T) {
defer cancel()
clus := testRunner.NewCluster(ctx, t, config.ClusterConfig{ClusterSize: 1})
defer clus.Close()
cc := clus.Client()
cc := framework.MustClient(clus.Client(clientv3.AuthConfig{}))
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "role1")
if err != nil {
Expand Down
Loading

0 comments on commit 7fff4c4

Please sign in to comment.