Skip to content

Commit

Permalink
tests: Migrate member tests to common framework
Browse files Browse the repository at this point in the history
Signed-off-by: Clark <[email protected]>
  • Loading branch information
clement2026 committed Jul 26, 2022
1 parent 4977877 commit e0f11c8
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 35 deletions.
175 changes: 175 additions & 0 deletions tests/common/member_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2016 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 common

import (
"context"
"errors"
"fmt"
"go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/testutils"
"net"
"sort"
"strconv"
"testing"
"time"
)

func TestMemberList(t *testing.T) {
testRunner.BeforeTest(t)

for _, tc := range clusterTestCases {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
resp, err := cc.MemberList()
if err != nil {
t.Fatalf("could not get member list, err: %s", err)
}
expectNum := len(clus.Members())
gotNum := len(resp.Members)
if expectNum != gotNum {
t.Fatalf("number of members not equal, expect: %d, got: %d", expectNum, gotNum)
}
for _, m := range resp.Members {
if len(m.ClientURLs) == 0 {
t.Fatalf("member is not started, memberId:%d, memberName:%s", m.ID, m.Name)
}
}
})
})
}
}

func TestMemberAdd(t *testing.T) {
testRunner.BeforeTest(t)

for _, tc := range clusterTestCases {
nestedCases := []struct {
name string
learner bool
}{
{
name: "NotLearner",
learner: false,
},
{
name: "Learner",
learner: true,
},
}
for _, nc := range nestedCases {
t.Run(tc.name+"/"+nc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
listResp, err := cc.MemberList()
if err != nil {
t.Fatalf("could not get member list, err: %s", err)
}
peerUrl, err := generateNonexistentPeer(listResp.Members)
var addResp *clientv3.MemberAddResponse
if nc.learner {
addResp, err = cc.MemberAddAsLearner("newmember", []string{peerUrl})
} else {
addResp, err = cc.MemberAdd("newmember", []string{peerUrl})
}
if err != nil {
t.Fatalf("could not get member list, err: %s", err)
}
if addResp.Member == nil || addResp.Member.ID == 0 {
t.Fatalf("member add failed, got nil member")
}
if addResp.Member.ID == 0 {
t.Fatalf("member add failed, got empty member id")
}
if len(addResp.Member.PeerURLs) == 0 {
t.Fatalf("member add failed, got empty peer urls")
}
})
})
}
}
}

func TestMemberUpdate(t *testing.T) {
testRunner.BeforeTest(t)

for _, tc := range clusterTestCases {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clus := testRunner.NewCluster(ctx, t, tc.config)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
resp, err := cc.MemberList()
if err != nil {
t.Fatalf("could not get member list, err: %s", err)
}
peerUrl, err := generateNonexistentPeer(resp.Members)
if err != nil {
t.Fatalf("%v", err)
}
memberID := resp.Members[0].ID
_, err = cc.MemberUpdate(memberID, []string{peerUrl})
if err != nil {
t.Fatalf("could not update member, err: %s", err)
}
})
})
}
}

func generateNonexistentPeer(members []*etcdserverpb.Member) (peerUrl string, err error) {
ports := make([]int, 0, 0)

// collect port of each peer
for _, m := range members {
for _, pu := range m.PeerURLs {
_, ps, err := net.SplitHostPort(pu)
if err != nil {
return "", err
}
p, err := strconv.Atoi(ps)
if err != nil {
return "", err
}
ports = append(ports, p)
}
}

if len(ports) == 0 {
return "", errors.New("no port was found from members")
}
sort.Ints(ports)
maxPort := ports[len(ports)-1]
newPort := maxPort + 11
if newPort > 65535 {
return "", errors.New("could not generate a nonexistent peer")
}
return fmt.Sprintf("http://localhost:%d", newPort), nil
}
36 changes: 1 addition & 35 deletions tests/e2e/ctl_v3_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ import (

func TestCtlV3MemberList(t *testing.T) { testCtl(t, memberListTest) }
func TestCtlV3MemberListWithHex(t *testing.T) { testCtl(t, memberListWithHexTest) }
func TestCtlV3MemberListNoTLS(t *testing.T) {
testCtl(t, memberListTest, withCfg(*e2e.NewConfigNoTLS()))
}
func TestCtlV3MemberListClientTLS(t *testing.T) {
testCtl(t, memberListTest, withCfg(*e2e.NewConfigClientTLS()))
}
func TestCtlV3MemberListClientAutoTLS(t *testing.T) {
testCtl(t, memberListTest, withCfg(*e2e.NewConfigClientAutoTLS()))
}
func TestCtlV3MemberListPeerTLS(t *testing.T) {
testCtl(t, memberListTest, withCfg(*e2e.NewConfigPeerTLS()))
}
func TestCtlV3MemberRemove(t *testing.T) {
testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig())
}
Expand All @@ -62,31 +50,9 @@ func TestCtlV3MemberRemoveClientAutoTLS(t *testing.T) {
func TestCtlV3MemberRemovePeerTLS(t *testing.T) {
testCtl(t, memberRemoveTest, withQuorum(), withNoStrictReconfig(), withCfg(*e2e.NewConfigPeerTLS()))
}
func TestCtlV3MemberAdd(t *testing.T) { testCtl(t, memberAddTest) }
func TestCtlV3MemberAddNoTLS(t *testing.T) { testCtl(t, memberAddTest, withCfg(*e2e.NewConfigNoTLS())) }
func TestCtlV3MemberAddClientTLS(t *testing.T) {
testCtl(t, memberAddTest, withCfg(*e2e.NewConfigClientTLS()))
}
func TestCtlV3MemberAddClientAutoTLS(t *testing.T) {
testCtl(t, memberAddTest, withCfg(*e2e.NewConfigClientAutoTLS()))
}
func TestCtlV3MemberAddPeerTLS(t *testing.T) {
testCtl(t, memberAddTest, withCfg(*e2e.NewConfigPeerTLS()))
}
func TestCtlV3MemberAdd(t *testing.T) { testCtl(t, memberAddTest) }
func TestCtlV3MemberAddForLearner(t *testing.T) { testCtl(t, memberAddForLearnerTest) }
func TestCtlV3MemberUpdate(t *testing.T) { testCtl(t, memberUpdateTest) }
func TestCtlV3MemberUpdateNoTLS(t *testing.T) {
testCtl(t, memberUpdateTest, withCfg(*e2e.NewConfigNoTLS()))
}
func TestCtlV3MemberUpdateClientTLS(t *testing.T) {
testCtl(t, memberUpdateTest, withCfg(*e2e.NewConfigClientTLS()))
}
func TestCtlV3MemberUpdateClientAutoTLS(t *testing.T) {
testCtl(t, memberUpdateTest, withCfg(*e2e.NewConfigClientAutoTLS()))
}
func TestCtlV3MemberUpdatePeerTLS(t *testing.T) {
testCtl(t, memberUpdateTest, withCfg(*e2e.NewConfigPeerTLS()))
}

func memberListTest(cx ctlCtx) {
if err := ctlV3MemberList(cx); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions tests/framework/e2e/etcdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func (ctl *EtcdctlV3) MemberList() (*clientv3.MemberListResponse, error) {
return &resp, err
}

func (ctl *EtcdctlV3) MemberAdd(name string, peerAddrs []string) (*clientv3.MemberAddResponse, error) {
var resp clientv3.MemberAddResponse
err := ctl.spawnJsonCmd(&resp, "member", "add", name, "--peer-urls", strings.Join(peerAddrs, ","))
return &resp, err
}

func (ctl *EtcdctlV3) MemberAddAsLearner(name string, peerAddrs []string) (*clientv3.MemberAddResponse, error) {
var resp clientv3.MemberAddResponse
err := ctl.spawnJsonCmd(&resp, "member", "add", name, "--learner", "--peer-urls", strings.Join(peerAddrs, ","))
Expand All @@ -247,6 +253,12 @@ func (ctl *EtcdctlV3) MemberRemove(id uint64) (*clientv3.MemberRemoveResponse, e
return &resp, err
}

func (ctl *EtcdctlV3) MemberUpdate(id uint64, peerAddrs []string) (*clientv3.MemberUpdateResponse, error) {
var resp clientv3.MemberUpdateResponse
err := ctl.spawnJsonCmd(&resp, "member", "update", fmt.Sprintf("%x", id), "--peer-urls", strings.Join(peerAddrs, ","))
return &resp, err
}

func (ctl *EtcdctlV3) cmdArgs(args ...string) []string {
cmdArgs := []string{CtlBinPath + "3"}
for k, v := range ctl.flags() {
Expand Down
18 changes: 18 additions & 0 deletions tests/framework/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,21 @@ func getOps(ss []string) ([]clientv3.Op, error) {
}
return ops, nil
}

func (c integrationClient) MemberList() (*clientv3.MemberListResponse, error) {
return c.Client.MemberList(c.ctx)
}

// MemberAdd name is ignored because etcdserverpb.MemberAddRequest has no such field
func (c integrationClient) MemberAdd(_ string, peerAddrs []string) (*clientv3.MemberAddResponse, error) {
return c.Client.MemberAdd(c.ctx, peerAddrs)
}

// MemberAddAsLearner name is ignored because etcdserverpb.MemberAddRequest has no such field
func (c integrationClient) MemberAddAsLearner(_ string, peerAddrs []string) (*clientv3.MemberAddResponse, error) {
return c.Client.MemberAddAsLearner(c.ctx, peerAddrs)
}

func (c integrationClient) MemberUpdate(id uint64, peerAddrs []string) (*clientv3.MemberUpdateResponse, error) {
return c.Client.MemberUpdate(c.ctx, id, peerAddrs)
}
5 changes: 5 additions & 0 deletions tests/framework/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ type Client interface {
RoleDelete(role string) (*clientv3.AuthRoleDeleteResponse, error)

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

MemberList() (*clientv3.MemberListResponse, error)
MemberAdd(name string, peerAddrs []string) (*clientv3.MemberAddResponse, error)
MemberAddAsLearner(name string, peerAddrs []string) (*clientv3.MemberAddResponse, error)
MemberUpdate(id uint64, peerAddrs []string) (*clientv3.MemberUpdateResponse, error)
}

0 comments on commit e0f11c8

Please sign in to comment.