From 7732347172386a5e95d2d04576dc761bb773d45f Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Fri, 5 May 2017 09:23:28 +0200 Subject: [PATCH] test: simplify tests that check for peers --- consul/acl_test.go | 45 ++++---------- consul/autopilot_test.go | 36 ++--------- consul/leader_test.go | 46 +++----------- consul/server_test.go | 124 ++++++++----------------------------- consul/session_ttl_test.go | 6 +- testutil/retry/retry.go | 7 +++ 6 files changed, 60 insertions(+), 204 deletions(-) diff --git a/consul/acl_test.go b/consul/acl_test.go index f78cc6f0c6..ce864126ac 100644 --- a/consul/acl_test.go +++ b/consul/acl_test.go @@ -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 } diff --git a/consul/autopilot_test.go b/consul/autopilot_test.go index d2dd87687d..783132ad31 100644 --- a/consul/autopilot_test.go +++ b/consul/autopilot_test.go @@ -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)) }) } } diff --git a/consul/leader_test.go b/consul/leader_test.go index 880cfce4b0..e22f95a17a 100644 --- a/consul/leader_test.go +++ b/consul/leader_test.go @@ -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)) }) } } diff --git a/consul/server_test.go b/consul/server_test.go index beb59455f3..88b0f7da58 100644 --- a/consul/server_test.go +++ b/consul/server_test.go @@ -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{} diff --git a/consul/session_ttl_test.go b/consul/session_ttl_test.go index 7acef4d570..c674d15149 100644 --- a/consul/session_ttl_test.go +++ b/consul/session_ttl_test.go @@ -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 diff --git a/testutil/retry/retry.go b/testutil/retry/retry.go index ff5ff8df65..d1faa75474 100644 --- a/testutil/retry/retry.go +++ b/testutil/retry/retry.go @@ -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)) }