From bef9348ca84b2ca9d2b8d1c31c45f92afc43d90e Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 7 Aug 2020 17:28:16 -0400 Subject: [PATCH] testing: remove unnecessary defers in tests The data directory is now removed by the test helper that created it. --- agent/consul/client_test.go | 64 +++++++++++++------------------------ agent/consul/leader_test.go | 3 +- agent/consul/server_test.go | 22 ++++--------- 3 files changed, 31 insertions(+), 58 deletions(-) diff --git a/agent/consul/client_test.go b/agent/consul/client_test.go index 8addc2e08..569c802a0 100644 --- a/agent/consul/client_test.go +++ b/agent/consul/client_test.go @@ -428,7 +428,7 @@ func TestClient_RPC_ConsulServerPing(t *testing.T) { func TestClient_RPC_TLS(t *testing.T) { t.Parallel() - dir1, conf1 := testServerConfig(t) + _, conf1 := testServerConfig(t) conf1.VerifyIncoming = true conf1.VerifyOutgoing = true configureTLS(conf1) @@ -436,18 +436,12 @@ func TestClient_RPC_TLS(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir1) defer s1.Shutdown() - dir2, conf2 := testClientConfig(t) + _, conf2 := testClientConfig(t) conf2.VerifyOutgoing = true configureTLS(conf2) - c1, err := newClient(t, conf2) - if err != nil { - t.Fatalf("err: %v", err) - } - defer os.RemoveAll(dir2) - defer c1.Shutdown() + c1 := newClient(t, conf2) // Try an RPC var out struct{} @@ -472,38 +466,38 @@ func TestClient_RPC_TLS(t *testing.T) { }) } -func newClient(t *testing.T, config *Config) (*Client, error) { +func newClient(t *testing.T, config *Config) *Client { + t.Helper() + c, err := tlsutil.NewConfigurator(config.ToTLSUtilConfig(), nil) - if err != nil { - return nil, err - } + require.NoError(t, err, "failed to create tls configuration") + logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{ Level: hclog.Debug, Output: testutil.NewLogBuffer(t), }) - return NewClient(config, WithLogger(logger), WithTLSConfigurator(c)) + client, err := NewClient(config, WithLogger(logger), WithTLSConfigurator(c)) + require.NoError(t, err, "failed to create client") + t.Cleanup(func() { + client.Shutdown() + }) + return client } func TestClient_RPC_RateLimit(t *testing.T) { t.Parallel() - dir1, conf1 := testServerConfig(t) + _, conf1 := testServerConfig(t) s1, err := newServer(t, conf1) if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir1) defer s1.Shutdown() testrpc.WaitForLeader(t, s1.RPC, "dc1") - dir2, conf2 := testClientConfig(t) + _, conf2 := testClientConfig(t) conf2.RPCRate = 2 conf2.RPCMaxBurst = 2 - c1, err := newClient(t, conf2) - if err != nil { - t.Fatalf("err: %v", err) - } - defer os.RemoveAll(dir2) - defer c1.Shutdown() + c1 := newClient(t, conf2) joinLAN(t, c1, s1) retry.Run(t, func(r *retry.R) { @@ -557,20 +551,14 @@ func TestClient_SnapshotRPC(t *testing.T) { func TestClient_SnapshotRPC_RateLimit(t *testing.T) { t.Parallel() - dir1, s1 := testServer(t) - defer os.RemoveAll(dir1) + _, s1 := testServer(t) defer s1.Shutdown() testrpc.WaitForLeader(t, s1.RPC, "dc1") - dir2, conf1 := testClientConfig(t) + _, conf1 := testClientConfig(t) conf1.RPCRate = 2 conf1.RPCMaxBurst = 2 - c1, err := newClient(t, conf1) - if err != nil { - t.Fatalf("err: %v", err) - } - defer os.RemoveAll(dir2) - defer c1.Shutdown() + c1 := newClient(t, conf1) joinLAN(t, c1, s1) retry.Run(t, func(r *retry.R) { @@ -593,7 +581,7 @@ func TestClient_SnapshotRPC_RateLimit(t *testing.T) { func TestClient_SnapshotRPC_TLS(t *testing.T) { t.Parallel() - dir1, conf1 := testServerConfig(t) + _, conf1 := testServerConfig(t) conf1.VerifyIncoming = true conf1.VerifyOutgoing = true configureTLS(conf1) @@ -601,18 +589,12 @@ func TestClient_SnapshotRPC_TLS(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir1) defer s1.Shutdown() - dir2, conf2 := testClientConfig(t) + _, conf2 := testClientConfig(t) conf2.VerifyOutgoing = true configureTLS(conf2) - c1, err := newClient(t, conf2) - if err != nil { - t.Fatalf("err: %v", err) - } - defer os.RemoveAll(dir2) - defer c1.Shutdown() + c1 := newClient(t, conf2) // Wait for the leader testrpc.WaitForLeader(t, s1.RPC, "dc1") diff --git a/agent/consul/leader_test.go b/agent/consul/leader_test.go index ed3409119..f8d5f91c6 100644 --- a/agent/consul/leader_test.go +++ b/agent/consul/leader_test.go @@ -1286,8 +1286,7 @@ func TestLeader_ConfigEntryBootstrap_Fail(t *testing.T) { } }() - dir, config := testServerConfig(t) - defer os.RemoveAll(dir) + _, config := testServerConfig(t) config.Build = "1.6.0" config.ConfigEntryBootstrap = []structs.ConfigEntry{ &structs.ServiceSplitterConfigEntry{ diff --git a/agent/consul/server_test.go b/agent/consul/server_test.go index 037730b70..ea4441c94 100644 --- a/agent/consul/server_test.go +++ b/agent/consul/server_test.go @@ -272,7 +272,6 @@ func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) { // cb is a function that can alter the test servers configuration prior to the server starting. func testACLServerWithConfig(t *testing.T, cb func(*Config), initReplicationToken bool) (string, *Server, rpc.ClientCodec) { dir, srv := testServerWithConfig(t, testServerACLConfig(cb)) - t.Cleanup(func() { os.RemoveAll(dir) }) t.Cleanup(func() { srv.Shutdown() }) if initReplicationToken { @@ -333,8 +332,7 @@ func newServer(t *testing.T, c *Config) (*Server, error) { func TestServer_StartStop(t *testing.T) { t.Parallel() // Start up a server and then stop it. - dir1, s1 := testServer(t) - defer os.RemoveAll(dir1) + _, s1 := testServer(t) if err := s1.Shutdown(); err != nil { t.Fatalf("err: %v", err) } @@ -348,20 +346,18 @@ func TestServer_StartStop(t *testing.T) { func TestServer_fixupACLDatacenter(t *testing.T) { t.Parallel() - dir1, s1 := testServerWithConfig(t, func(c *Config) { + _, s1 := testServerWithConfig(t, func(c *Config) { c.Datacenter = "aye" c.PrimaryDatacenter = "aye" c.ACLsEnabled = true }) - defer os.RemoveAll(dir1) defer s1.Shutdown() - dir2, s2 := testServerWithConfig(t, func(c *Config) { + _, s2 := testServerWithConfig(t, func(c *Config) { c.Datacenter = "bee" c.PrimaryDatacenter = "aye" c.ACLsEnabled = true }) - defer os.RemoveAll(dir2) defer s2.Shutdown() // Try to join @@ -1072,7 +1068,7 @@ func TestServer_RPC(t *testing.T) { func TestServer_JoinLAN_TLS(t *testing.T) { t.Parallel() - dir1, conf1 := testServerConfig(t) + _, conf1 := testServerConfig(t) conf1.VerifyIncoming = true conf1.VerifyOutgoing = true configureTLS(conf1) @@ -1080,11 +1076,10 @@ func TestServer_JoinLAN_TLS(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir1) defer s1.Shutdown() testrpc.WaitForTestAgent(t, s1.RPC, "dc1") - dir2, conf2 := testServerConfig(t) + _, conf2 := testServerConfig(t) conf2.Bootstrap = false conf2.VerifyIncoming = true conf2.VerifyOutgoing = true @@ -1093,7 +1088,6 @@ func TestServer_JoinLAN_TLS(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir2) defer s2.Shutdown() // Try to join @@ -1471,14 +1465,13 @@ func TestServer_Reload(t *testing.T) { func TestServer_RPC_RateLimit(t *testing.T) { t.Parallel() - dir1, conf1 := testServerConfig(t) + _, conf1 := testServerConfig(t) conf1.RPCRate = 2 conf1.RPCMaxBurst = 2 s1, err := newServer(t, conf1) if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir1) defer s1.Shutdown() testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -1492,7 +1485,7 @@ func TestServer_RPC_RateLimit(t *testing.T) { func TestServer_CALogging(t *testing.T) { t.Parallel() - dir1, conf1 := testServerConfig(t) + _, conf1 := testServerConfig(t) // Setup dummy logger to catch output var buf bytes.Buffer @@ -1508,7 +1501,6 @@ func TestServer_CALogging(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - defer os.RemoveAll(dir1) defer s1.Shutdown() testrpc.WaitForLeader(t, s1.RPC, "dc1")