test: simplify tests that check for peers

This commit is contained in:
Frank Schroeder 2017-05-05 09:23:28 +02:00
parent 2794db5aac
commit 7732347172
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
6 changed files with 60 additions and 204 deletions

View File

@ -231,11 +231,7 @@ func TestACL_NonAuthority_NotFound(t *testing.T) {
if _, err := s2.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) })
client := rpcClient(t, s1)
defer client.Close()
@ -281,11 +277,7 @@ func TestACL_NonAuthority_Found(t *testing.T) {
if _, err := s2.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) })
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -356,11 +348,7 @@ func TestACL_NonAuthority_Management(t *testing.T) {
if _, err := s2.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) })
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -412,11 +400,7 @@ func TestACL_DownPolicy_Deny(t *testing.T) {
if _, err := s2.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) })
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -485,11 +469,7 @@ func TestACL_DownPolicy_Allow(t *testing.T) {
if _, err := s2.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) })
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -560,11 +540,7 @@ func TestACL_DownPolicy_ExtendCache(t *testing.T) {
if _, err := s2.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) })
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -1906,10 +1882,13 @@ service "service" {
}
}
func numPeers(s *Server) int {
func wantPeers(s *Server, peers int) error {
n, err := s.numPeers()
if err != nil {
panic(err)
return err
}
return n
if got, want := n, peers; got != want {
return fmt.Errorf("got %d peers want %d", got, want)
}
return nil
}

View File

@ -50,11 +50,7 @@ func testCleanupDeadServer(t *testing.T, raftVersion int) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
// Bring up a new server
@ -84,11 +80,7 @@ func testCleanupDeadServer(t *testing.T, raftVersion int) {
// Make sure the dead server is removed and we're back to 3 total peers
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
}
@ -128,11 +120,7 @@ func TestAutopilot_CleanupDeadServerPeriodic(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 4; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 4)) })
}
// Kill a non-leader server
@ -140,11 +128,7 @@ func TestAutopilot_CleanupDeadServerPeriodic(t *testing.T) {
// Should be removed from the peers automatically
for _, s := range []*Server{s1, s2, s3} {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
}
@ -178,11 +162,7 @@ func TestAutopilot_CleanupStaleRaftServer(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
testrpc.WaitForLeader(t, s1.RPC, "dc1")
@ -203,11 +183,7 @@ func TestAutopilot_CleanupStaleRaftServer(t *testing.T) {
// Wait for s4 to be removed
for _, s := range []*Server{s1, s2, s3} {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
}

View File

@ -472,11 +472,7 @@ func TestLeader_LeftServer(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
retry.Run(t, func(r *retry.R) {
@ -489,9 +485,7 @@ func TestLeader_LeftServer(t *testing.T) {
}
for _, s := range servers[1:] {
if got, want := numPeers(s), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 2)) })
}
})
}
@ -521,11 +515,7 @@ func TestLeader_LeftLeader(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
// Kill the leader!
@ -549,11 +539,7 @@ func TestLeader_LeftLeader(t *testing.T) {
continue
}
remain = s
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 2; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 2)) })
}
// Verify the old leader is deregistered
@ -629,11 +615,7 @@ func TestLeader_TombstoneGC_Reset(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
var leader *Server
@ -777,11 +759,7 @@ func TestLeader_RollRaftServer(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
// Kill the v1 server
@ -870,11 +848,7 @@ func TestLeader_ChangeServerID(t *testing.T) {
}
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
// Shut down a server, freeing up its address/port
@ -911,10 +885,6 @@ func TestLeader_ChangeServerID(t *testing.T) {
// Make sure the dead server is removed and we're back to 3 total peers
for _, s := range servers {
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s), 3; got != want {
r.Fatalf("got %d peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) })
}
}

View File

@ -350,12 +350,8 @@ func TestServer_LeaveLeader(t *testing.T) {
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 2; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
r.Check(wantPeers(s1, 2))
r.Check(wantPeers(s2, 2))
})
// Issue a leave to the leader
@ -370,12 +366,8 @@ func TestServer_LeaveLeader(t *testing.T) {
// Should lose a peer
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 1; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 1; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
r.Check(wantPeers(s1, 1))
r.Check(wantPeers(s2, 1))
})
}
@ -397,12 +389,8 @@ func TestServer_Leave(t *testing.T) {
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 2; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
r.Check(wantPeers(s1, 2))
r.Check(wantPeers(s2, 2))
})
// Issue a leave to the non-leader
@ -417,12 +405,8 @@ func TestServer_Leave(t *testing.T) {
// Should lose a peer
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 1; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 1; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
r.Check(wantPeers(s1, 1))
r.Check(wantPeers(s2, 1))
})
}
@ -477,12 +461,8 @@ func TestServer_JoinLAN_TLS(t *testing.T) {
})
// Verify Raft has established a peer
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 2; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 2; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
r.Check(wantPeers(s1, 2))
r.Check(wantPeers(s2, 2))
})
}
@ -515,12 +495,8 @@ func TestServer_Expect(t *testing.T) {
// Should have no peers yet since the bootstrap didn't occur.
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 0; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 0; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
r.Check(wantPeers(s1, 0))
r.Check(wantPeers(s2, 0))
})
// Join the third node.
@ -530,15 +506,9 @@ func TestServer_Expect(t *testing.T) {
// Now we have three servers so we should bootstrap.
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 3; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 3; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
if got, want := numPeers(s3), 3; got != want {
r.Fatalf("got %d s3 peers want %d", got, want)
}
r.Check(wantPeers(s1, 3))
r.Check(wantPeers(s2, 3))
r.Check(wantPeers(s3, 3))
})
// Make sure a leader is elected, grab the current term and then add in
@ -551,18 +521,10 @@ func TestServer_Expect(t *testing.T) {
// Wait for the new server to see itself added to the cluster.
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 4; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
if got, want := numPeers(s2), 4; got != want {
r.Fatalf("got %d s2 peers want %d", got, want)
}
if got, want := numPeers(s3), 4; got != want {
r.Fatalf("got %d s3 peers want %d", got, want)
}
if got, want := numPeers(s4), 4; got != want {
r.Fatalf("got %d s4 peers want %d", got, want)
}
r.Check(wantPeers(s1, 4))
r.Check(wantPeers(s2, 4))
r.Check(wantPeers(s3, 4))
r.Check(wantPeers(s4, 4))
})
// Make sure there's still a leader and that the term didn't change,
@ -597,24 +559,10 @@ func TestServer_BadExpect(t *testing.T) {
t.Fatalf("err: %v", err)
}
var p1 int
var p2 int
retry.
// should have no peers yet
Run(t, func(r *retry.R) {
p1, _ = s1.numPeers()
if p1 != 0 {
r.Fatalf("%d", p1)
}
})
// should have no peers yet
retry.Run(t, func(r *retry.R) {
p2, _ = s2.numPeers()
if p2 != 0 {
r.Fatalf("%d", p2)
}
r.Check(wantPeers(s1, 0))
r.Check(wantPeers(s2, 0))
})
// join the third node
@ -622,32 +570,12 @@ func TestServer_BadExpect(t *testing.T) {
t.Fatalf("err: %v", err)
}
var p3 int
retry.
// should still have no peers (because s2 is in expect=2 mode)
Run(t, func(r *retry.R) {
p1, _ = s1.numPeers()
if p1 != 0 {
r.Fatalf("%d", p1)
}
})
// should still have no peers (because s2 is in expect=2 mode)
retry.Run(t, func(r *retry.R) {
p2, _ = s2.numPeers()
if p2 != 0 {
r.Fatalf("%d", p2)
}
r.Check(wantPeers(s1, 0))
r.Check(wantPeers(s2, 0))
r.Check(wantPeers(s3, 0))
})
retry.Run(t, func(r *retry.R) {
p3, _ = s3.numPeers()
if p3 != 0 {
r.Fatalf("%d", p3)
}
})
}
type fakeGlobalResp struct{}

View File

@ -298,11 +298,7 @@ func TestServer_SessionTTL_Failover(t *testing.T) {
if _, err := s3.JoinLAN([]string{addr}); err != nil {
t.Fatalf("err: %v", err)
}
retry.Run(t, func(r *retry.R) {
if got, want := numPeers(s1), 3; got != want {
r.Fatalf("got %d s1 peers want %d", got, want)
}
})
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 3)) })
// Find the leader
var leader *Server

View File

@ -56,6 +56,13 @@ func (r *R) Error(args ...interface{}) {
r.fail = true
}
func (r *R) Check(err error) {
if err != nil {
r.log(err.Error())
r.FailNow()
}
}
func (r *R) log(s string) {
r.output = append(r.output, decorate(s))
}