diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go index e36f02f4c5..bfa0ce0df1 100644 --- a/agent/agent_endpoint_test.go +++ b/agent/agent_endpoint_test.go @@ -1773,9 +1773,9 @@ func TestAgent_ForceLeave_ACLDeny(t *testing.T) { func TestAgent_ForceLeavePrune(t *testing.T) { t.Parallel() - a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"}) + a1 := StartTestAgent(t, TestAgent{Name: "Agent1"}) defer a1.Shutdown() - a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2"}) + a2 := StartTestAgent(t, TestAgent{Name: "Agent2"}) testrpc.WaitForLeader(t, a1.RPC, "dc1") testrpc.WaitForLeader(t, a2.RPC, "dc1") @@ -5236,14 +5236,14 @@ func TestAgentConnectCALeafCert_secondaryDC_good(t *testing.T) { assert := assert.New(t) require := require.New(t) - a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc1", HCL: ` + a1 := StartTestAgent(t, TestAgent{Name: "dc1", HCL: ` datacenter = "dc1" primary_datacenter = "dc1" `}) defer a1.Shutdown() testrpc.WaitForTestAgent(t, a1.RPC, "dc1") - a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc2", HCL: ` + a2 := StartTestAgent(t, TestAgent{Name: "dc2", HCL: ` datacenter = "dc2" primary_datacenter = "dc1" `}) diff --git a/agent/agent_test.go b/agent/agent_test.go index 83ce0aad02..89b6d5a387 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -1409,7 +1409,7 @@ func TestAgent_RestoreServiceWithAliasCheck(t *testing.T) { enable_central_service_config = false data_dir = "` + dataDir + `" ` - a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer os.RemoveAll(dataDir) defer a.Shutdown() @@ -1493,7 +1493,7 @@ node_name = "` + a.Config.NodeName + `" t.Helper() // Reload and retain former NodeID and data directory. - a2 := NewTestAgentWithFields(t, true, TestAgent{HCL: futureHCL, DataDir: dataDir}) + a2 := StartTestAgent(t, TestAgent{HCL: futureHCL, DataDir: dataDir}) defer a2.Shutdown() a = nil @@ -1757,7 +1757,7 @@ func TestAgent_HTTPCheck_EnableAgentTLSForChecks(t *testing.T) { t.Parallel() run := func(t *testing.T, ca string) { - a := NewTestAgentWithFields(t, true, TestAgent{ + a := StartTestAgent(t, TestAgent{ UseTLS: true, HCL: ` enable_agent_tls_for_checks = true @@ -1886,7 +1886,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) { bootstrap = false data_dir = "` + dataDir + `" ` + extraHCL - a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a.Shutdown() svc := &structs.NodeService{ @@ -1952,7 +1952,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) { a.Shutdown() // Should load it back during later start - a2 := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a2 := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a2.Shutdown() restored := a2.State.ServiceState(structs.NewServiceID(svc.ID, nil)) @@ -2098,7 +2098,7 @@ func testAgent_PurgeServiceOnDuplicate(t *testing.T, extraHCL string) { server = false bootstrap = false ` + extraHCL - a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a.Shutdown() svc1 := &structs.NodeService{ @@ -2114,7 +2114,7 @@ func testAgent_PurgeServiceOnDuplicate(t *testing.T, extraHCL string) { // Try bringing the agent back up with the service already // existing in the config - a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: cfg + ` + a2 := StartTestAgent(t, TestAgent{Name: "Agent2", HCL: cfg + ` service = { id = "redis" name = "redis" @@ -2142,7 +2142,7 @@ func TestAgent_PersistCheck(t *testing.T) { bootstrap = false enable_script_checks = true ` - a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer os.RemoveAll(dataDir) defer a.Shutdown() @@ -2199,7 +2199,7 @@ func TestAgent_PersistCheck(t *testing.T) { a.Shutdown() // Should load it back during later start - a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: cfg, DataDir: dataDir}) + a2 := StartTestAgent(t, TestAgent{Name: "Agent2", HCL: cfg, DataDir: dataDir}) defer a2.Shutdown() result := requireCheckExists(t, a2, check.CheckID) @@ -2251,7 +2251,7 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) { t.Parallel() nodeID := NodeID() dataDir := testutil.TempDir(t, "agent") - a := NewTestAgentWithFields(t, true, TestAgent{ + a := StartTestAgent(t, TestAgent{ DataDir: dataDir, HCL: ` node_id = "` + nodeID + `" @@ -2279,7 +2279,7 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) { a.Shutdown() // Start again with the check registered in config - a2 := NewTestAgentWithFields(t, true, TestAgent{ + a2 := StartTestAgent(t, TestAgent{ Name: "Agent2", DataDir: dataDir, HCL: ` @@ -4031,11 +4031,11 @@ func TestAgentCache_serviceInConfigFile_initialFetchErrors_Issue6521(t *testing. // index for the next query from 0 to 1 for all queries, when it should // have not done so for queries that errored. - a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"}) + a1 := StartTestAgent(t, TestAgent{Name: "Agent1"}) defer a1.Shutdown() testrpc.WaitForLeader(t, a1.RPC, "dc1") - a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: ` + a2 := StartTestAgent(t, TestAgent{Name: "Agent2", HCL: ` server = false bootstrap = false services { @@ -4127,7 +4127,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { secondaryRPCPorts := freeport.MustTake(2) defer freeport.Return(secondaryRPCPorts) - a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "bob", HCL: ` + a1 := StartTestAgent(t, TestAgent{Name: "bob", HCL: ` domain = "consul" node_name = "bob" datacenter = "dc1" @@ -4212,7 +4212,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { require.NotEmpty(r, a1.PickRandomMeshGatewaySuitableForDialing("dc1")) }) - a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "betty", HCL: ` + a2 := StartTestAgent(t, TestAgent{Name: "betty", HCL: ` domain = "consul" node_name = "betty" datacenter = "dc2" @@ -4237,7 +4237,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { defer a2.Shutdown() testrpc.WaitForTestAgent(t, a2.RPC, "dc2") - a3 := NewTestAgentWithFields(t, true, TestAgent{Name: "bonnie", HCL: ` + a3 := StartTestAgent(t, TestAgent{Name: "bonnie", HCL: ` domain = "consul" node_name = "bonnie" datacenter = "dc3" diff --git a/agent/http_test.go b/agent/http_test.go index 3760d7de65..5d8f2e3dbb 100644 --- a/agent/http_test.go +++ b/agent/http_test.go @@ -132,7 +132,7 @@ func TestHTTPServer_H2(t *testing.T) { t.Parallel() // Fire up an agent with TLS enabled. - a := NewTestAgentWithFields(t, true, TestAgent{ + a := StartTestAgent(t, TestAgent{ UseTLS: true, HCL: ` key_file = "../test/client_certs/server.key" @@ -486,7 +486,7 @@ func TestContentTypeIsJSON(t *testing.T) { func TestHTTP_wrap_obfuscateLog(t *testing.T) { t.Parallel() buf := new(bytes.Buffer) - a := NewTestAgentWithFields(t, true, TestAgent{LogOutput: buf}) + a := StartTestAgent(t, TestAgent{LogOutput: buf}) defer a.Shutdown() handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) { @@ -1288,7 +1288,7 @@ func TestHTTPServer_HandshakeTimeout(t *testing.T) { t.Parallel() // Fire up an agent with TLS enabled. - a := NewTestAgentWithFields(t, true, TestAgent{ + a := StartTestAgent(t, TestAgent{ UseTLS: true, HCL: ` key_file = "../test/client_certs/server.key" @@ -1354,7 +1354,7 @@ func TestRPC_HTTPSMaxConnsPerClient(t *testing.T) { } // Fire up an agent with TLS enabled. - a := NewTestAgentWithFields(t, true, TestAgent{ + a := StartTestAgent(t, TestAgent{ UseTLS: tc.tlsEnabled, HCL: hclPrefix + ` limits { diff --git a/agent/keyring_test.go b/agent/keyring_test.go index e1e429c429..be30704546 100644 --- a/agent/keyring_test.go +++ b/agent/keyring_test.go @@ -54,7 +54,7 @@ func TestAgent_LoadKeyrings(t *testing.T) { // Server should auto-load LAN and WAN keyring files t.Run("server with keys", func(t *testing.T) { - a2 := NewTestAgentWithFields(t, true, TestAgent{Key: key}) + a2 := StartTestAgent(t, TestAgent{Key: key}) defer a2.Shutdown() c2 := a2.consulConfig() @@ -80,7 +80,7 @@ func TestAgent_LoadKeyrings(t *testing.T) { // Client should auto-load only the LAN keyring file t.Run("client with keys", func(t *testing.T) { - a3 := NewTestAgentWithFields(t, true, TestAgent{HCL: ` + a3 := StartTestAgent(t, TestAgent{HCL: ` server = false bootstrap = false `, Key: key}) @@ -272,7 +272,7 @@ func TestAgentKeyring_ACL(t *testing.T) { key1 := "tbLJg26ZJyJ9pK3qhc9jig==" key2 := "4leC33rgtXKIVUr9Nr0snQ==" - a := NewTestAgentWithFields(t, true, TestAgent{HCL: TestACLConfig() + ` + a := StartTestAgent(t, TestAgent{HCL: TestACLConfig() + ` acl_datacenter = "dc1" acl_master_token = "root" acl_default_policy = "deny" diff --git a/agent/service_manager_test.go b/agent/service_manager_test.go index 9af915b046..e71e21f222 100644 --- a/agent/service_manager_test.go +++ b/agent/service_manager_test.go @@ -302,7 +302,7 @@ func TestServiceManager_PersistService_API(t *testing.T) { bootstrap = false data_dir = "` + dataDir + `" ` - a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a.Shutdown() // Join first @@ -465,7 +465,7 @@ func TestServiceManager_PersistService_API(t *testing.T) { serverAgent.Shutdown() // Should load it back during later start. - a2 := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a2 := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a2.Shutdown() { @@ -540,7 +540,7 @@ func TestServiceManager_PersistService_ConfigFiles(t *testing.T) { bootstrap = false ` + serviceSnippet - a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a.Shutdown() // Join first @@ -639,7 +639,7 @@ func TestServiceManager_PersistService_ConfigFiles(t *testing.T) { serverAgent.Shutdown() // Should load it back during later start. - a2 := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) + a2 := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir}) defer a2.Shutdown() { diff --git a/agent/sidecar_service_test.go b/agent/sidecar_service_test.go index a7234c8899..85f9c0a233 100644 --- a/agent/sidecar_service_test.go +++ b/agent/sidecar_service_test.go @@ -325,7 +325,7 @@ func TestAgent_sidecarServiceFromNodeService(t *testing.T) { } require := require.New(t) - a := NewTestAgentWithFields(t, true, TestAgent{Name: "jones", HCL: hcl}) + a := StartTestAgent(t, TestAgent{Name: "jones", HCL: hcl}) defer a.Shutdown() if tt.preRegister != nil { diff --git a/agent/testagent.go b/agent/testagent.go index 1d449a615c..28cac61ca5 100644 --- a/agent/testagent.go +++ b/agent/testagent.go @@ -91,20 +91,15 @@ type TestAgent struct { // The caller is responsible for calling Shutdown() to stop the agent and remove // temporary directories. func NewTestAgent(t *testing.T, hcl string) *TestAgent { - return NewTestAgentWithFields(t, true, TestAgent{HCL: hcl}) + return StartTestAgent(t, TestAgent{HCL: hcl}) } -// NewTestAgentWithFields takes a TestAgent struct with any number of fields set, -// and a boolean 'start', which indicates whether or not the TestAgent should -// be started. If no LogOutput is set, it will automatically be set to -// testutil.TestWriter(t). Name will default to t.Name() if not specified. -func NewTestAgentWithFields(t *testing.T, start bool, ta TestAgent) *TestAgent { - // copy values - a := ta - if !start { - return &a - } - +// StartTestAgent and wait for it to become available. If the agent fails to +// start the test will be marked failed and execution will stop. +// +// The caller is responsible for calling Shutdown() to stop the agent and remove +// temporary directories. +func StartTestAgent(t *testing.T, a TestAgent) *TestAgent { retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) { if err := a.Start(t); err != nil { r.Fatal(err) diff --git a/command/monitor/monitor_test.go b/command/monitor/monitor_test.go index 4dd9f8af51..982c92a736 100644 --- a/command/monitor/monitor_test.go +++ b/command/monitor/monitor_test.go @@ -11,7 +11,7 @@ import ( func TestMonitorCommand_exitsOnSignalBeforeLinesArrive(t *testing.T) { t.Parallel() - a := agent.NewTestAgentWithFields(t, true, agent.TestAgent{}) + a := agent.StartTestAgent(t, agent.TestAgent{}) defer a.Shutdown() shutdownCh := make(chan struct{}) @@ -64,7 +64,7 @@ func TestMonitorCommand_exitsOnSignalBeforeLinesArrive(t *testing.T) { func TestMonitorCommand_LogJSONValidFlag(t *testing.T) { t.Parallel() - a := agent.NewTestAgentWithFields(t, true, agent.TestAgent{}) + a := agent.StartTestAgent(t, agent.TestAgent{}) defer a.Shutdown() shutdownCh := make(chan struct{})