Skip to content

Commit

Permalink
update auth test interface
Browse files Browse the repository at this point in the history
Signed-off-by: Chao Chen <[email protected]>
  • Loading branch information
chaochn47 committed Oct 26, 2022
1 parent 8ce81a1 commit 4bd9af2
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 44 deletions.
1 change: 1 addition & 0 deletions tests/common/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

func init() {
testRunner = framework.E2eTestRunner
withAuth = WithAuth
}

func WithAuth(userName, password string) config.ClientOption {
Expand Down
1 change: 1 addition & 0 deletions tests/common/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

func init() {
testRunner = framework.IntegrationTestRunner
withAuth = WithAuth
}

func WithAuth(userName, password string) config.ClientOption {
Expand Down
1 change: 1 addition & 0 deletions tests/common/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

var testRunner = framework.UnitTestRunner
var withAuth = func(userName, password string) config.ClientOption { return func(any) {} }
var clusterTestCases = []testCase{
{
name: "NoTLS",
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/cluster_downgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/coreos/go-semver/semver"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/api/v3/version"
"go.etcd.io/etcd/client/pkg/v3/fileutil"
clientv3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -122,7 +123,8 @@ func startEtcd(t *testing.T, ep e2e.EtcdProcess, execPath string) {
}

func downgradeEnable(t *testing.T, epc *e2e.EtcdProcessCluster, ver semver.Version) {
c := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
c, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)
testutils.ExecuteWithTimeout(t, 20*time.Second, func() {
err := c.DowngradeEnable(context.TODO(), ver.String())
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions tests/e2e/corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func TestPeriodicCheckDetectsCorruption(t *testing.T) {
}
})

cc := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)

for i := 0; i < 10; i++ {
err := cc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
Expand Down Expand Up @@ -164,7 +165,8 @@ func TestCompactHashCheckDetectCorruption(t *testing.T) {
}
})

cc := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)

for i := 0; i < 10; i++ {
err := cc.Put(ctx, testutil.PickKey(int64(i)), fmt.Sprint(i), config.PutOptions{})
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/ctl_v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func TestAuthority(t *testing.T) {
defer epc.Close()
endpoints := templateEndpoints(t, tc.clientURLPattern, epc)

client := e2e.NewEtcdctl(cfg, endpoints)
client, err := e2e.NewEtcdctl(cfg, endpoints)
assert.NoError(t, err)
err = client.Put(ctx, "foo", "bar", config.PutOptions{})
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/etcd_grpcproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func TestGrpcProxyAutoSync(t *testing.T) {
assert.NoError(t, proxyProc.Stop())
}()

proxyCtl := e2e.NewEtcdctl(&e2e.EtcdProcessClusterConfig{}, []string{proxyClientURL})
proxyCtl, err := e2e.NewEtcdctl(&e2e.EtcdProcessClusterConfig{}, []string{proxyClientURL})
require.NoError(t, err)
err = proxyCtl.Put(ctx, "k1", "v1", config.PutOptions{})
require.NoError(t, err)

Expand Down
14 changes: 10 additions & 4 deletions tests/e2e/v2store_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ func TestV2DeprecationSnapshotMatches(t *testing.T) {
}
snapshotCount := 10
epc := runEtcdAndCreateSnapshot(t, lastReleaseBinary, lastReleaseData, snapshotCount)
members1 := addAndRemoveKeysAndMembers(ctx, t, e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3()), snapshotCount)
cc1, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)
members1 := addAndRemoveKeysAndMembers(ctx, t, cc1, snapshotCount)
assert.NoError(t, epc.Close())
epc = runEtcdAndCreateSnapshot(t, currentReleaseBinary, currentReleaseData, snapshotCount)
members2 := addAndRemoveKeysAndMembers(ctx, t, e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3()), snapshotCount)
cc2, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)
members2 := addAndRemoveKeysAndMembers(ctx, t, cc2, snapshotCount)
assert.NoError(t, epc.Close())

assertSnapshotsMatch(t, lastReleaseData, currentReleaseData, func(data []byte) []byte {
Expand Down Expand Up @@ -144,7 +148,8 @@ func TestV2DeprecationSnapshotRecover(t *testing.T) {
}
epc := runEtcdAndCreateSnapshot(t, lastReleaseBinary, dataDir, 10)

cc := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err := e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)

lastReleaseGetResponse, err := cc.Get(ctx, "", config.GetOptions{Prefix: true})
assert.NoError(t, err)
Expand All @@ -157,7 +162,8 @@ func TestV2DeprecationSnapshotRecover(t *testing.T) {
epc, err = e2e.NewEtcdProcessCluster(context.TODO(), t, cfg)
assert.NoError(t, err)

cc = e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
cc, err = e2e.NewEtcdctl(epc.Cfg, epc.EndpointsV3())
assert.NoError(t, err)
currentReleaseGetResponse, err := cc.Get(ctx, "", config.GetOptions{Prefix: true})
assert.NoError(t, err)

Expand Down
10 changes: 7 additions & 3 deletions tests/framework/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ type e2eCluster struct {
}

func (c *e2eCluster) Client(opts ...config.ClientOption) (Client, error) {
etcdctl := e2e.NewEtcdctl(c.Cfg, c.EndpointsV3(), opts...)
return e2eClient{etcdctl}, nil
etcdctl, err := e2e.NewEtcdctl(c.Cfg, c.EndpointsV3(), opts...)
return e2eClient{etcdctl}, err
}

func (c *e2eCluster) Endpoints() []string {
Expand Down Expand Up @@ -179,7 +179,11 @@ type e2eMember struct {
}

func (m e2eMember) Client() Client {
return e2eClient{e2e.NewEtcdctl(m.Cfg, m.EndpointsV3())}
etcdctl, err := e2e.NewEtcdctl(m.Cfg, m.EndpointsV3())
if err != nil {
panic(err)
}
return e2eClient{etcdctl}
}

func (m e2eMember) Start(ctx context.Context) error {
Expand Down
6 changes: 5 additions & 1 deletion tests/framework/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,11 @@ func (epc *EtcdProcessCluster) Stop() (err error) {
}

func (epc *EtcdProcessCluster) Client() *EtcdctlV3 {
return NewEtcdctl(epc.Cfg, epc.EndpointsV3())
etcdctl, err := NewEtcdctl(epc.Cfg, epc.EndpointsV3())
if err != nil {
panic(err)
}
return etcdctl
}

func (epc *EtcdProcessCluster) Close() error {
Expand Down
53 changes: 31 additions & 22 deletions tests/framework/e2e/etcdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ import (
"io"
"strconv"
"strings"
"time"

"go.etcd.io/etcd/api/v3/authpb"
"go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
"google.golang.org/grpc"
)

type EtcdctlV3 struct {
cfg *EtcdProcessClusterConfig
endpoints []string
userName string
password string
cfg *EtcdProcessClusterConfig
endpoints []string
authConfig clientv3.AuthConfig
}

func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...config.ClientOption) *EtcdctlV3 {
func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...config.ClientOption) (*EtcdctlV3, error) {
ctl := &EtcdctlV3{
cfg: cfg,
endpoints: endpoints,
Expand All @@ -45,14 +46,28 @@ func NewEtcdctl(cfg *EtcdProcessClusterConfig, endpoints []string, opts ...confi
opt(ctl)
}

return ctl
if !ctl.authConfig.Empty() {
client, err := clientv3.New(clientv3.Config{
Endpoints: ctl.endpoints,
DialTimeout: 5 * time.Second,
DialOptions: []grpc.DialOption{grpc.WithBlock()},
Username: ctl.authConfig.Username,
Password: ctl.authConfig.Password,
})
if err != nil {
return nil, err
}
client.Close()
}

return ctl, nil
}

func WithAuth(userName, password string) config.ClientOption {
return func(c any) {
ctl := c.(*EtcdctlV3)
ctl.userName = userName
ctl.password = password
ctl.authConfig.Username = userName
ctl.authConfig.Password = password
}
}

Expand Down Expand Up @@ -300,8 +315,8 @@ func (ctl *EtcdctlV3) flags() map[string]string {
}
}
fmap["endpoints"] = strings.Join(ctl.endpoints, ",")
if ctl.userName != "" && ctl.password != "" {
fmap["user"] = ctl.userName + ":" + ctl.password
if !ctl.authConfig.Empty() {
fmap["user"] = ctl.authConfig.Username + ":" + ctl.authConfig.Password
}
return fmap
}
Expand Down Expand Up @@ -473,34 +488,28 @@ func (ctl *EtcdctlV3) AlarmDisarm(ctx context.Context, _ *clientv3.AlarmMember)
return &resp, err
}

func (ctl *EtcdctlV3) AuthEnable(ctx context.Context) (*clientv3.AuthEnableResponse, error) {
func (ctl *EtcdctlV3) AuthEnable(ctx context.Context) error {
args := []string{"auth", "enable"}
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return nil, err
return err
}
defer cmd.Close()

_, err = cmd.ExpectWithContext(ctx, "Authentication Enabled")
if err != nil {
return nil, err
}
return &clientv3.AuthEnableResponse{}, nil
return err
}

func (ctl *EtcdctlV3) AuthDisable(ctx context.Context) (*clientv3.AuthDisableResponse, error) {
func (ctl *EtcdctlV3) AuthDisable(ctx context.Context) error {
args := []string{"auth", "disable"}
cmd, err := SpawnCmd(ctl.cmdArgs(args...), nil)
if err != nil {
return nil, err
return err
}
defer cmd.Close()

_, err = cmd.ExpectWithContext(ctx, "Authentication Disabled")
if err != nil {
return nil, err
}
return &clientv3.AuthDisableResponse{}, nil
return err
}

func (ctl *EtcdctlV3) AuthStatus(ctx context.Context) (*clientv3.AuthStatusResponse, error) {
Expand Down
10 changes: 6 additions & 4 deletions tests/framework/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@ func (c integrationClient) Revoke(ctx context.Context, id clientv3.LeaseID) (*cl
return c.Client.Revoke(ctx, id)
}

func (c integrationClient) AuthEnable(ctx context.Context) (*clientv3.AuthEnableResponse, error) {
return c.Client.AuthEnable(ctx)
func (c integrationClient) AuthEnable(ctx context.Context) error {
_, err := c.Client.AuthEnable(ctx)
return err
}

func (c integrationClient) AuthDisable(ctx context.Context) (*clientv3.AuthDisableResponse, error) {
return c.Client.AuthDisable(ctx)
func (c integrationClient) AuthDisable(ctx context.Context) error {
_, err := c.Client.AuthDisable(ctx)
return err
}

func (c integrationClient) AuthStatus(ctx context.Context) (*clientv3.AuthStatusResponse, error) {
Expand Down
6 changes: 3 additions & 3 deletions tests/framework/integration/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1457,9 +1457,9 @@ func (c *Cluster) ClusterClient(t testing.TB, opts ...framecfg.ClientOption) (cl

func WithAuth(userName, password string) framecfg.ClientOption {
return func(c any) {
client := c.(*clientv3.Client)
client.Username = userName
client.Password = password
cfg := c.(*clientv3.Config)
cfg.Username = userName
cfg.Password = password
}
}

Expand Down
5 changes: 3 additions & 2 deletions tests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ type Client interface {
KeepAliveOnce(context context.Context, id clientv3.LeaseID) (*clientv3.LeaseKeepAliveResponse, error)
Revoke(context context.Context, id clientv3.LeaseID) (*clientv3.LeaseRevokeResponse, error)

AuthEnable(context context.Context) (*clientv3.AuthEnableResponse, error)
AuthDisable(context context.Context) (*clientv3.AuthDisableResponse, error)
AuthEnable(context context.Context) error
AuthDisable(context context.Context) error
AuthStatus(context context.Context) (*clientv3.AuthStatusResponse, error)
UserAdd(context context.Context, name, password string, opts config.UserAddOptions) (*clientv3.AuthUserAddResponse, error)
UserGet(context context.Context, name string) (*clientv3.AuthUserGetResponse, error)
Expand All @@ -75,6 +75,7 @@ type Client interface {
RoleList(context context.Context) (*clientv3.AuthRoleListResponse, error)
RoleRevokePermission(context context.Context, role string, key, rangeEnd string) (*clientv3.AuthRoleRevokePermissionResponse, error)
RoleDelete(context context.Context, role string) (*clientv3.AuthRoleDeleteResponse, error)

Txn(context context.Context, compares, ifSucess, ifFail []string, o config.TxnOptions) (*clientv3.TxnResponse, error)

MemberList(context context.Context) (*clientv3.MemberListResponse, error)
Expand Down

0 comments on commit 4bd9af2

Please sign in to comment.