Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
Signed-off-by: Clark <[email protected]>
  • Loading branch information
clement2026 committed Sep 5, 2022
1 parent 34d9b03 commit ae04eab
Showing 1 changed file with 65 additions and 122 deletions.
187 changes: 65 additions & 122 deletions tests/common/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package common

import (
"context"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/server/v3/etcdserver"
"go.etcd.io/etcd/tests/v3/framework/testutils"
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestMemberList(t *testing.T) {
func TestMemberAdd(t *testing.T) {
testRunner.BeforeTest(t)

tcs := []struct {
learnerTcs := []struct {
name string
learner bool
}{
Expand All @@ -73,139 +73,82 @@ func TestMemberAdd(t *testing.T) {
},
}

for _, tc := range tcs {
for _, nc := range clusterTestCases {
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, nc.config)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
clus.WaitLeader(t)
var addResp *clientv3.MemberAddResponse
var err error
if tc.learner {
addResp, err = cc.MemberAddAsLearner(ctx, "newmember", []string{"http://localhost:123"})
} else {
addResp, err = cc.MemberAdd(ctx, "newmember", []string{"http://localhost:123"})
}
if nc.config.ClusterSize == 1 {
checkAddResp(t, addResp, err)
} else {
if err == nil {
t.Fatalf("expect MemberAdd to fail, got no error, addResp: %v", addResp)
}
if !strings.Contains(err.Error(), "etcdserver: unhealthy cluster") {
t.Fatalf("expected: 'etcdserver: unhealthy cluster' error, got: %s", err)
}
}
})
})
}
}
}

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

tcs := []struct {
name string
learner bool
quorumTcs := []struct {
name string
strictReconfigCheck bool
waitForQuorum bool
expectError bool
}{
{
name: "NotLearner",
learner: false,
name: "StrictReconfigCheck/WaitForQuorum",
strictReconfigCheck: true,
waitForQuorum: true,
expectError: false,
},
{
name: "Learner",
learner: true,
name: "StrictReconfigCheck/NoWaitForQuorum",
strictReconfigCheck: true,
waitForQuorum: false,
expectError: true,
},
}

for _, tc := range tcs {
for _, nc := range clusterTestCases {
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, nc.config)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
clus.WaitLeader(t)
var addResp *clientv3.MemberAddResponse
var err error
time.Sleep(etcdserver.HealthInterval)
if tc.learner {
addResp, err = cc.MemberAddAsLearner(ctx, "newmember", []string{"http://localhost:123"})
} else {
addResp, err = cc.MemberAdd(ctx, "newmember", []string{"http://localhost:123"})
}
checkAddResp(t, addResp, err)
})
})
}
}
}

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

tcs := []struct {
name string
learner bool
}{
{
name: "NotLearner",
learner: false,
name: "DisableStrictReconfigCheck/WaitForQuorum",
strictReconfigCheck: false,
waitForQuorum: true,
expectError: false,
},
{
name: "Learner",
learner: true,
name: "DisableStrictReconfigCheck/NoWaitForQuorum",
strictReconfigCheck: false,
waitForQuorum: false,
expectError: false,
},
}

for _, tc := range tcs {
for _, strictNc := range clusterTestCases {
// make a copy of strictNc and disable StrictReconfigCheck
nc := strictNc
nc.config.DisableStrictReconfigCheck = true
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, nc.config)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
clus.WaitLeader(t)
var addResp *clientv3.MemberAddResponse
var err error
if tc.learner {
addResp, err = cc.MemberAddAsLearner(ctx, "newmember", []string{"http://localhost:123"})
} else {
addResp, err = cc.MemberAdd(ctx, "newmember", []string{"http://localhost:123"})
for _, learnerTc := range learnerTcs {
for _, quorumTc := range quorumTcs {
for _, clusterTc := range clusterTestCases {
t.Run(learnerTc.name+"/"+quorumTc.name+"/"+clusterTc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c := clusterTc.config
if !quorumTc.strictReconfigCheck {
c.DisableStrictReconfigCheck = true
}
checkAddResp(t, addResp, err)
clus := testRunner.NewCluster(ctx, t, c)
defer clus.Close()
cc := clus.Client()

testutils.ExecuteUntil(ctx, t, func() {
var addResp *clientv3.MemberAddResponse
var err error
if quorumTc.waitForQuorum {
time.Sleep(etcdserver.HealthInterval)
}
if learnerTc.learner {
addResp, err = cc.MemberAddAsLearner(ctx, "newmember", []string{"http://localhost:123"})
} else {
addResp, err = cc.MemberAdd(ctx, "newmember", []string{"http://localhost:123"})
}
if quorumTc.expectError && c.ClusterSize > 1 {
// calling MemberAdd/MemberAddAsLearner on a single node will not fail,
// whether strictReconfigCheck or whether waitForQuorum
require.ErrorContains(t, err, "etcdserver: unhealthy cluster")
} else {
require.NoError(t, err, "MemberAdd failed")
if addResp.Member == nil {
t.Fatalf("MemberAdd failed, expected: member != nil, got: member == nil")
}
if addResp.Member.ID == 0 {
t.Fatalf("MemberAdd failed, expected: ID != 0, got: ID == 0")
}
if len(addResp.Member.PeerURLs) == 0 {
t.Fatalf("MemberAdd failed, expected: non-empty PeerURLs, got: empty PeerURLs")
}
}
})
})
})
}
}
}
}

func checkAddResp(t *testing.T, addResp *clientv3.MemberAddResponse, err error) {
if err != nil {
t.Fatalf("MemberAdd failed, err: %s", err)
}
if addResp.Member == nil {
t.Fatalf("MemberAdd failed, expected: member != nil, got: member == nil")
}
if addResp.Member.ID == 0 {
t.Fatalf("MemberAdd failed, expected: ID != 0, got: ID == 0")
}
if len(addResp.Member.PeerURLs) == 0 {
t.Fatalf("MemberAdd failed, expected: non-empty PeerURLs, got: empty PeerURLs")
}
}

0 comments on commit ae04eab

Please sign in to comment.