-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Migrate member tests to common framework
Signed-off-by: Clark <[email protected]>
- Loading branch information
1 parent
4977877
commit e0f11c8
Showing
5 changed files
with
211 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters