Skip to content

Commit

Permalink
fix broken unit test in server_test.go
Browse files Browse the repository at this point in the history
Signed-off-by: Geeta Gharpure <[email protected]>
  • Loading branch information
Geeta Gharpure authored and ahrtr committed Sep 27, 2023
1 parent 249886b commit 0f95027
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
3 changes: 3 additions & 0 deletions server/etcdserver/api/membership/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ func TestClusterGenID(t *testing.T) {
newTestMember(2, nil, "", nil),
})

be := newMembershipBackend()
cs.SetBackend(be)

cs.genID()
if cs.ID() == 0 {
t.Fatalf("cluster.ID = %v, want not 0", cs.ID())
Expand Down
58 changes: 51 additions & 7 deletions server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"go.etcd.io/etcd/server/v3/mock/mockstore"
"go.etcd.io/etcd/server/v3/mock/mockwait"
serverstorage "go.etcd.io/etcd/server/v3/storage"
"go.etcd.io/etcd/server/v3/storage/backend"
betesting "go.etcd.io/etcd/server/v3/storage/backend/testing"
"go.etcd.io/etcd/server/v3/storage/mvcc"
"go.etcd.io/etcd/server/v3/storage/schema"
Expand Down Expand Up @@ -181,13 +182,17 @@ func TestDoBadLocalAction(t *testing.T) {

// TestApplyRepeat tests that server handles repeat raft messages gracefully
func TestApplyRepeat(t *testing.T) {
lg := zaptest.NewLogger(t)
n := newNodeConfChangeCommitterStream()
n.readyc <- raft.Ready{
SoftState: &raft.SoftState{RaftState: raft.StateLeader},
}
cl := newTestCluster(t, nil)
st := v2store.New()
cl.SetStore(v2store.New())
be, _ := betesting.NewDefaultTmpBackend(t)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

cl.AddMember(&membership.Member{ID: 1234}, true)
r := newRaftNode(raftNodeConfig{
lg: zaptest.NewLogger(t),
Expand Down Expand Up @@ -490,7 +495,9 @@ func TestApplyRequest(t *testing.T) {
}

func TestApplyRequestOnAdminMemberAttributes(t *testing.T) {
cl := newTestCluster(t, []*membership.Member{{ID: 1}})
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)
cl := newTestClusterWithBackend(t, []*membership.Member{{ID: 1}}, be)
srv := &EtcdServer{
lgMu: new(sync.RWMutex),
lg: zaptest.NewLogger(t),
Expand All @@ -513,8 +520,14 @@ func TestApplyRequestOnAdminMemberAttributes(t *testing.T) {
}

func TestApplyConfChangeError(t *testing.T) {
cl := membership.NewCluster(zaptest.NewLogger(t))
lg := zaptest.NewLogger(t)
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)

cl := membership.NewCluster(lg)
cl.SetBackend(schema.NewMembershipBackend(lg, be))
cl.SetStore(v2store.New())

for i := 1; i <= 4; i++ {
cl.AddMember(&membership.Member{ID: types.ID(i)}, true)
}
Expand Down Expand Up @@ -601,8 +614,14 @@ func TestApplyConfChangeError(t *testing.T) {
}

func TestApplyConfChangeShouldStop(t *testing.T) {
cl := membership.NewCluster(zaptest.NewLogger(t))
lg := zaptest.NewLogger(t)
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)

cl := membership.NewCluster(lg)
cl.SetBackend(schema.NewMembershipBackend(lg, be))
cl.SetStore(v2store.New())

for i := 1; i <= 3; i++ {
cl.AddMember(&membership.Member{ID: types.ID(i)}, true)
}
Expand All @@ -611,7 +630,6 @@ func TestApplyConfChangeShouldStop(t *testing.T) {
Node: newNodeNop(),
transport: newNopTransporter(),
})
lg := zaptest.NewLogger(t)
srv := &EtcdServer{
lgMu: new(sync.RWMutex),
lg: lg,
Expand Down Expand Up @@ -648,13 +666,15 @@ func TestApplyConfChangeShouldStop(t *testing.T) {
// where consistIndex equals to applied index.
func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
lg := zaptest.NewLogger(t)
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)

cl := membership.NewCluster(zaptest.NewLogger(t))
cl.SetStore(v2store.New())
cl.SetBackend(schema.NewMembershipBackend(lg, be))

cl.AddMember(&membership.Member{ID: types.ID(1)}, true)

be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)
schema.CreateMetaBucket(be.BatchTx())

ci := cindex.NewConsistentIndex(be)
Expand Down Expand Up @@ -732,6 +752,9 @@ func TestApplyMultiConfChangeShouldStop(t *testing.T) {
lg := zaptest.NewLogger(t)
cl := membership.NewCluster(lg)
cl.SetStore(v2store.New())
be, _ := betesting.NewDefaultTmpBackend(t)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

for i := 1; i <= 5; i++ {
cl.AddMember(&membership.Member{ID: types.ID(i)}, true)
}
Expand Down Expand Up @@ -1248,6 +1271,8 @@ func TestConcurrentApplyAndSnapshotV3(t *testing.T) {
st := v2store.New()
cl := membership.NewCluster(lg)
cl.SetStore(st)
be, _ := betesting.NewDefaultTmpBackend(t)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

testdir := t.TempDir()
if err := os.MkdirAll(testdir+"/member/snap", 0755); err != nil {
Expand All @@ -1264,7 +1289,6 @@ func TestConcurrentApplyAndSnapshotV3(t *testing.T) {
storage: mockstorage.NewStorageRecorder(testdir),
raftStorage: rs,
})
be, _ := betesting.NewDefaultTmpBackend(t)
ci := cindex.NewConsistentIndex(be)
s := &EtcdServer{
lgMu: new(sync.RWMutex),
Expand Down Expand Up @@ -1345,6 +1369,10 @@ func TestAddMember(t *testing.T) {
cl := newTestCluster(t, nil)
st := v2store.New()
cl.SetStore(st)
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

r := newRaftNode(raftNodeConfig{
lg: lg,
Node: n,
Expand Down Expand Up @@ -1391,6 +1419,9 @@ func TestRemoveMember(t *testing.T) {
cl := newTestCluster(t, nil)
st := v2store.New()
cl.SetStore(v2store.New())
be, _ := betesting.NewDefaultTmpBackend(t)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

cl.AddMember(&membership.Member{ID: 1234}, true)
r := newRaftNode(raftNodeConfig{
lg: lg,
Expand Down Expand Up @@ -1430,13 +1461,16 @@ func TestRemoveMember(t *testing.T) {
// TestUpdateMember tests RemoveMember can propose and perform node update.
func TestUpdateMember(t *testing.T) {
lg := zaptest.NewLogger(t)
be, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, be)
n := newNodeConfChangeCommitterRecorder()
n.readyc <- raft.Ready{
SoftState: &raft.SoftState{RaftState: raft.StateLeader},
}
cl := newTestCluster(t, nil)
st := v2store.New()
cl.SetStore(st)
cl.SetBackend(schema.NewMembershipBackend(lg, be))
cl.AddMember(&membership.Member{ID: 1234}, true)
r := newRaftNode(raftNodeConfig{
lg: lg,
Expand Down Expand Up @@ -1903,6 +1937,16 @@ func newTestCluster(t testing.TB, membs []*membership.Member) *membership.RaftCl
return c
}

func newTestClusterWithBackend(t testing.TB, membs []*membership.Member, be backend.Backend) *membership.RaftCluster {
lg := zaptest.NewLogger(t)
c := membership.NewCluster(lg)
c.SetBackend(schema.NewMembershipBackend(lg, be))
for _, m := range membs {
c.AddMember(m, true)
}
return c
}

type nopTransporter struct{}

func newNopTransporter() rafthttp.Transporter {
Expand Down

0 comments on commit 0f95027

Please sign in to comment.