From f9f6b14533cd50c425676f288974011b65b6d5f0 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 31 Mar 2020 16:12:33 -0400 Subject: [PATCH] Convert the remaining calls to NewTestAgentWithFields After removing the t.Name() parameter with sed, convert the last few tests which use a custom name to call NewTestAgentWithFields instead. --- agent/agent_endpoint_test.go | 12 +++--- agent/agent_test.go | 43 ++++++------------- agent/sidecar_service_test.go | 2 +- agent/testagent.go | 10 +---- .../list/nodes/catalog_list_nodes_test.go | 2 +- command/forceleave/forceleave_test.go | 4 +- command/members/members_test.go | 2 +- .../raft/listpeers/operator_raft_list_test.go | 2 +- connect/proxy/config_test.go | 2 +- connect/resolver_test.go | 2 +- connect/service_test.go | 2 +- connect/tls_test.go | 2 +- 12 files changed, 31 insertions(+), 54 deletions(-) diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go index 70fdbe87c5..e36f02f4c5 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 := NewTestAgent(t, t.Name()+"-a1", "") + a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"}) defer a1.Shutdown() - a2 := NewTestAgent(t, t.Name()+"-a2", "") + a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2"}) testrpc.WaitForLeader(t, a1.RPC, "dc1") testrpc.WaitForLeader(t, a2.RPC, "dc1") @@ -5236,17 +5236,17 @@ func TestAgentConnectCALeafCert_secondaryDC_good(t *testing.T) { assert := assert.New(t) require := require.New(t) - a1 := NewTestAgent(t, t.Name()+"-dc1", ` + a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc1", HCL: ` datacenter = "dc1" primary_datacenter = "dc1" - `) + `}) defer a1.Shutdown() testrpc.WaitForTestAgent(t, a1.RPC, "dc1") - a2 := NewTestAgent(t, t.Name()+"-dc2", ` + a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc2", HCL: ` datacenter = "dc2" primary_datacenter = "dc1" - `) + `}) defer a2.Shutdown() testrpc.WaitForTestAgent(t, a2.RPC, "dc2") diff --git a/agent/agent_test.go b/agent/agent_test.go index f13063b117..83ce0aad02 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -80,21 +80,6 @@ func requireCheckMissingMap(t *testing.T, m interface{}, id types.CheckID) { require.NotContains(t, m, structs.NewCheckID(id, nil), "have check %q (expected missing)", id) } -func externalIP() (string, error) { - addrs, err := net.InterfaceAddrs() - if err != nil { - return "", fmt.Errorf("Unable to lookup network interfaces: %v", err) - } - for _, a := range addrs { - if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - return ipnet.IP.String(), nil - } - } - } - return "", fmt.Errorf("Unable to find a non-loopback interface") -} - func TestAgent_MultiStartStop(t *testing.T) { for i := 0; i < 10; i++ { t.Run("", func(t *testing.T) { @@ -138,7 +123,7 @@ func TestAgent_ConnectClusterIDConfig(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - a := NewTestAgentWithFields(t, false, TestAgent{HCL: tt.hcl}) + a := TestAgent{HCL: tt.hcl} err := a.Start(t) if tt.wantErr { if err == nil { @@ -4046,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 := NewTestAgent(t, t.Name()+"-a1", "") + a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"}) defer a1.Shutdown() testrpc.WaitForLeader(t, a1.RPC, "dc1") - a2 := NewTestAgent(t, t.Name()+"-a2", ` + a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: ` server = false bootstrap = false services { @@ -4075,7 +4060,7 @@ services { sidecar_service {} } } - `) + `}) defer a2.Shutdown() // Starting a client agent disconnected from a server with services. @@ -4142,7 +4127,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { secondaryRPCPorts := freeport.MustTake(2) defer freeport.Return(secondaryRPCPorts) - a1 := NewTestAgent(t, t.Name()+"-bob", ` + a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "bob", HCL: ` domain = "consul" node_name = "bob" datacenter = "dc1" @@ -4159,7 +4144,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { enabled = true enable_mesh_gateway_wan_federation = true } - `) + `}) defer a1.Shutdown() testrpc.WaitForTestAgent(t, a1.RPC, "dc1") @@ -4227,7 +4212,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { require.NotEmpty(r, a1.PickRandomMeshGatewaySuitableForDialing("dc1")) }) - a2 := NewTestAgent(t, t.Name()+"-betty", ` + a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "betty", HCL: ` domain = "consul" node_name = "betty" datacenter = "dc2" @@ -4240,19 +4225,19 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { verify_outgoing = true verify_server_hostname = true ports { - server = `+strconv.Itoa(secondaryRPCPorts[0])+` + server = ` + strconv.Itoa(secondaryRPCPorts[0]) + ` } # wanfed - primary_gateways = ["`+gwAddr+`"] + primary_gateways = ["` + gwAddr + `"] connect { enabled = true enable_mesh_gateway_wan_federation = true } - `) + `}) defer a2.Shutdown() testrpc.WaitForTestAgent(t, a2.RPC, "dc2") - a3 := NewTestAgent(t, t.Name()+"-bonnie", ` + a3 := NewTestAgentWithFields(t, true, TestAgent{Name: "bonnie", HCL: ` domain = "consul" node_name = "bonnie" datacenter = "dc3" @@ -4265,15 +4250,15 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { verify_outgoing = true verify_server_hostname = true ports { - server = `+strconv.Itoa(secondaryRPCPorts[1])+` + server = ` + strconv.Itoa(secondaryRPCPorts[1]) + ` } # wanfed - primary_gateways = ["`+gwAddr+`"] + primary_gateways = ["` + gwAddr + `"] connect { enabled = true enable_mesh_gateway_wan_federation = true } - `) + `}) defer a3.Shutdown() testrpc.WaitForTestAgent(t, a3.RPC, "dc3") diff --git a/agent/sidecar_service_test.go b/agent/sidecar_service_test.go index 943b2a59f7..a7234c8899 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, "jones", hcl) + a := NewTestAgentWithFields(t, true, TestAgent{Name: "jones", HCL: hcl}) defer a.Shutdown() if tt.preRegister != nil { diff --git a/agent/testagent.go b/agent/testagent.go index 71413ecaaf..1d449a615c 100644 --- a/agent/testagent.go +++ b/agent/testagent.go @@ -122,15 +122,7 @@ func (a *TestAgent) Start(t *testing.T) (err error) { } name := a.Name - // Many tests set Name to t.Name(), which takes up a lot of space at the - // start of the log messages. The only time we need to care about a name is - // when a test using multiple TestAgents. - // As a temporary workaround we ignore the default name and use a shorter - // default value. Tests which set a custom name for multiple agents will - // use the provided name. - // TODO: remove TestAgent.Name and accept a name arg on Start, to remove - // this workaround. - if name == "" || name == t.Name() { + if name == "" { name = "TestAgent" } diff --git a/command/catalog/list/nodes/catalog_list_nodes_test.go b/command/catalog/list/nodes/catalog_list_nodes_test.go index 4f2d2b38eb..7226629a47 100644 --- a/command/catalog/list/nodes/catalog_list_nodes_test.go +++ b/command/catalog/list/nodes/catalog_list_nodes_test.go @@ -168,7 +168,7 @@ func TestCatalogListNodesCommand_verticalBar(t *testing.T) { t.Parallel() nodeName := "name|with|bars" - a := agent.NewTestAgent(t, "", `node_name = "`+nodeName+`"`) + a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`) defer a.Shutdown() testrpc.WaitForTestAgent(t, a.RPC, "dc1") diff --git a/command/forceleave/forceleave_test.go b/command/forceleave/forceleave_test.go index c9c179d183..309e1e0f9b 100644 --- a/command/forceleave/forceleave_test.go +++ b/command/forceleave/forceleave_test.go @@ -76,9 +76,9 @@ func TestForceLeaveCommand_NoNodeWithName(t *testing.T) { func TestForceLeaveCommand_prune(t *testing.T) { t.Parallel() - a1 := agent.NewTestAgent(t, t.Name()+"-a1", ``) + a1 := agent.StartTestAgent(t, agent.TestAgent{Name: "Agent1"}) defer a1.Shutdown() - a2 := agent.NewTestAgent(t, t.Name()+"-a2", ``) + a2 := agent.StartTestAgent(t, agent.TestAgent{Name: "Agent2"}) defer a2.Shutdown() _, err := a2.JoinLAN([]string{a1.Config.SerfBindAddrLAN.String()}) diff --git a/command/members/members_test.go b/command/members/members_test.go index 84989da424..ebcea45b79 100644 --- a/command/members/members_test.go +++ b/command/members/members_test.go @@ -125,7 +125,7 @@ func TestMembersCommand_verticalBar(t *testing.T) { t.Parallel() nodeName := "name|with|bars" - a := agent.NewTestAgent(t, "", `node_name = "`+nodeName+`"`) + a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`) defer a.Shutdown() ui := cli.NewMockUi() diff --git a/command/operator/raft/listpeers/operator_raft_list_test.go b/command/operator/raft/listpeers/operator_raft_list_test.go index 424966d3d4..fbb1065980 100644 --- a/command/operator/raft/listpeers/operator_raft_list_test.go +++ b/command/operator/raft/listpeers/operator_raft_list_test.go @@ -43,7 +43,7 @@ func TestOperatorRaftListPeersCommand_verticalBar(t *testing.T) { t.Parallel() nodeName := "name|with|bars" - a := agent.NewTestAgent(t, "", `node_name = "`+nodeName+`"`) + a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`) defer a.Shutdown() ui := cli.NewMockUi() diff --git a/connect/proxy/config_test.go b/connect/proxy/config_test.go index 1ade9d305c..6ba5603089 100644 --- a/connect/proxy/config_test.go +++ b/connect/proxy/config_test.go @@ -81,7 +81,7 @@ func TestUpstreamResolverFuncFromClient(t *testing.T) { func TestAgentConfigWatcherSidecarProxy(t *testing.T) { t.Parallel() - a := agent.NewTestAgent(t, "agent_smith", ``) + a := agent.StartTestAgent(t, agent.TestAgent{Name: "agent_smith"}) defer a.Shutdown() client := a.Client() diff --git a/connect/resolver_test.go b/connect/resolver_test.go index 8cbac577db..93f08d1366 100644 --- a/connect/resolver_test.go +++ b/connect/resolver_test.go @@ -42,7 +42,7 @@ func TestStaticResolver_Resolve(t *testing.T) { func TestConsulResolver_Resolve(t *testing.T) { // Setup a local test agent to query - agent := agent.NewTestAgent(t, "test-consul", "") + agent := agent.StartTestAgent(t, agent.TestAgent{Name: "test-consul"}) defer agent.Shutdown() cfg := api.DefaultConfig() diff --git a/connect/service_test.go b/connect/service_test.go index b71f854c8d..a1e1b87289 100644 --- a/connect/service_test.go +++ b/connect/service_test.go @@ -127,7 +127,7 @@ func TestService_Dial(t *testing.T) { func TestService_ServerTLSConfig(t *testing.T) { require := require.New(t) - a := agent.NewTestAgent(t, "007", "") + a := agent.StartTestAgent(t, agent.TestAgent{Name: "007"}) defer a.Shutdown() testrpc.WaitForTestAgent(t, a.RPC, "dc1") client := a.Client() diff --git a/connect/tls_test.go b/connect/tls_test.go index 9f3cad9071..e0cdb112c1 100644 --- a/connect/tls_test.go +++ b/connect/tls_test.go @@ -152,7 +152,7 @@ func TestServerSideVerifier(t *testing.T) { apiCA2 := testCertPEMBlock(t, apiCA2PEM) // Setup a local test agent to query - agent := agent.NewTestAgent(t, "test-consul", "") + agent := agent.StartTestAgent(t, agent.TestAgent{Name: "test-consul"}) defer agent.Shutdown() testrpc.WaitForTestAgent(t, agent.RPC, "dc1")