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.
This commit is contained in:
Daniel Nephin 2020-03-31 16:12:33 -04:00
parent 475659a132
commit f9f6b14533
12 changed files with 31 additions and 54 deletions

View File

@ -1773,9 +1773,9 @@ func TestAgent_ForceLeave_ACLDeny(t *testing.T) {
func TestAgent_ForceLeavePrune(t *testing.T) { func TestAgent_ForceLeavePrune(t *testing.T) {
t.Parallel() t.Parallel()
a1 := NewTestAgent(t, t.Name()+"-a1", "") a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"})
defer a1.Shutdown() 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, a1.RPC, "dc1")
testrpc.WaitForLeader(t, a2.RPC, "dc1") testrpc.WaitForLeader(t, a2.RPC, "dc1")
@ -5236,17 +5236,17 @@ func TestAgentConnectCALeafCert_secondaryDC_good(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
a1 := NewTestAgent(t, t.Name()+"-dc1", ` a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc1", HCL: `
datacenter = "dc1" datacenter = "dc1"
primary_datacenter = "dc1" primary_datacenter = "dc1"
`) `})
defer a1.Shutdown() defer a1.Shutdown()
testrpc.WaitForTestAgent(t, a1.RPC, "dc1") testrpc.WaitForTestAgent(t, a1.RPC, "dc1")
a2 := NewTestAgent(t, t.Name()+"-dc2", ` a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc2", HCL: `
datacenter = "dc2" datacenter = "dc2"
primary_datacenter = "dc1" primary_datacenter = "dc1"
`) `})
defer a2.Shutdown() defer a2.Shutdown()
testrpc.WaitForTestAgent(t, a2.RPC, "dc2") testrpc.WaitForTestAgent(t, a2.RPC, "dc2")

View File

@ -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) 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) { func TestAgent_MultiStartStop(t *testing.T) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
@ -138,7 +123,7 @@ func TestAgent_ConnectClusterIDConfig(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) err := a.Start(t)
if tt.wantErr { if tt.wantErr {
if err == nil { 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 // index for the next query from 0 to 1 for all queries, when it should
// have not done so for queries that errored. // have not done so for queries that errored.
a1 := NewTestAgent(t, t.Name()+"-a1", "") a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"})
defer a1.Shutdown() defer a1.Shutdown()
testrpc.WaitForLeader(t, a1.RPC, "dc1") testrpc.WaitForLeader(t, a1.RPC, "dc1")
a2 := NewTestAgent(t, t.Name()+"-a2", ` a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: `
server = false server = false
bootstrap = false bootstrap = false
services { services {
@ -4075,7 +4060,7 @@ services {
sidecar_service {} sidecar_service {}
} }
} }
`) `})
defer a2.Shutdown() defer a2.Shutdown()
// Starting a client agent disconnected from a server with services. // 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) secondaryRPCPorts := freeport.MustTake(2)
defer freeport.Return(secondaryRPCPorts) defer freeport.Return(secondaryRPCPorts)
a1 := NewTestAgent(t, t.Name()+"-bob", ` a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "bob", HCL: `
domain = "consul" domain = "consul"
node_name = "bob" node_name = "bob"
datacenter = "dc1" datacenter = "dc1"
@ -4159,7 +4144,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
enabled = true enabled = true
enable_mesh_gateway_wan_federation = true enable_mesh_gateway_wan_federation = true
} }
`) `})
defer a1.Shutdown() defer a1.Shutdown()
testrpc.WaitForTestAgent(t, a1.RPC, "dc1") testrpc.WaitForTestAgent(t, a1.RPC, "dc1")
@ -4227,7 +4212,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
require.NotEmpty(r, a1.PickRandomMeshGatewaySuitableForDialing("dc1")) require.NotEmpty(r, a1.PickRandomMeshGatewaySuitableForDialing("dc1"))
}) })
a2 := NewTestAgent(t, t.Name()+"-betty", ` a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "betty", HCL: `
domain = "consul" domain = "consul"
node_name = "betty" node_name = "betty"
datacenter = "dc2" datacenter = "dc2"
@ -4248,11 +4233,11 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
enabled = true enabled = true
enable_mesh_gateway_wan_federation = true enable_mesh_gateway_wan_federation = true
} }
`) `})
defer a2.Shutdown() defer a2.Shutdown()
testrpc.WaitForTestAgent(t, a2.RPC, "dc2") testrpc.WaitForTestAgent(t, a2.RPC, "dc2")
a3 := NewTestAgent(t, t.Name()+"-bonnie", ` a3 := NewTestAgentWithFields(t, true, TestAgent{Name: "bonnie", HCL: `
domain = "consul" domain = "consul"
node_name = "bonnie" node_name = "bonnie"
datacenter = "dc3" datacenter = "dc3"
@ -4273,7 +4258,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
enabled = true enabled = true
enable_mesh_gateway_wan_federation = true enable_mesh_gateway_wan_federation = true
} }
`) `})
defer a3.Shutdown() defer a3.Shutdown()
testrpc.WaitForTestAgent(t, a3.RPC, "dc3") testrpc.WaitForTestAgent(t, a3.RPC, "dc3")

View File

@ -325,7 +325,7 @@ func TestAgent_sidecarServiceFromNodeService(t *testing.T) {
} }
require := require.New(t) require := require.New(t)
a := NewTestAgentWithFields(t, "jones", hcl) a := NewTestAgentWithFields(t, true, TestAgent{Name: "jones", HCL: hcl})
defer a.Shutdown() defer a.Shutdown()
if tt.preRegister != nil { if tt.preRegister != nil {

View File

@ -122,15 +122,7 @@ func (a *TestAgent) Start(t *testing.T) (err error) {
} }
name := a.Name name := a.Name
// Many tests set Name to t.Name(), which takes up a lot of space at the if name == "" {
// 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() {
name = "TestAgent" name = "TestAgent"
} }

View File

@ -168,7 +168,7 @@ func TestCatalogListNodesCommand_verticalBar(t *testing.T) {
t.Parallel() t.Parallel()
nodeName := "name|with|bars" nodeName := "name|with|bars"
a := agent.NewTestAgent(t, "", `node_name = "`+nodeName+`"`) a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`)
defer a.Shutdown() defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1") testrpc.WaitForTestAgent(t, a.RPC, "dc1")

View File

@ -76,9 +76,9 @@ func TestForceLeaveCommand_NoNodeWithName(t *testing.T) {
func TestForceLeaveCommand_prune(t *testing.T) { func TestForceLeaveCommand_prune(t *testing.T) {
t.Parallel() t.Parallel()
a1 := agent.NewTestAgent(t, t.Name()+"-a1", ``) a1 := agent.StartTestAgent(t, agent.TestAgent{Name: "Agent1"})
defer a1.Shutdown() defer a1.Shutdown()
a2 := agent.NewTestAgent(t, t.Name()+"-a2", ``) a2 := agent.StartTestAgent(t, agent.TestAgent{Name: "Agent2"})
defer a2.Shutdown() defer a2.Shutdown()
_, err := a2.JoinLAN([]string{a1.Config.SerfBindAddrLAN.String()}) _, err := a2.JoinLAN([]string{a1.Config.SerfBindAddrLAN.String()})

View File

@ -125,7 +125,7 @@ func TestMembersCommand_verticalBar(t *testing.T) {
t.Parallel() t.Parallel()
nodeName := "name|with|bars" nodeName := "name|with|bars"
a := agent.NewTestAgent(t, "", `node_name = "`+nodeName+`"`) a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`)
defer a.Shutdown() defer a.Shutdown()
ui := cli.NewMockUi() ui := cli.NewMockUi()

View File

@ -43,7 +43,7 @@ func TestOperatorRaftListPeersCommand_verticalBar(t *testing.T) {
t.Parallel() t.Parallel()
nodeName := "name|with|bars" nodeName := "name|with|bars"
a := agent.NewTestAgent(t, "", `node_name = "`+nodeName+`"`) a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`)
defer a.Shutdown() defer a.Shutdown()
ui := cli.NewMockUi() ui := cli.NewMockUi()

View File

@ -81,7 +81,7 @@ func TestUpstreamResolverFuncFromClient(t *testing.T) {
func TestAgentConfigWatcherSidecarProxy(t *testing.T) { func TestAgentConfigWatcherSidecarProxy(t *testing.T) {
t.Parallel() t.Parallel()
a := agent.NewTestAgent(t, "agent_smith", ``) a := agent.StartTestAgent(t, agent.TestAgent{Name: "agent_smith"})
defer a.Shutdown() defer a.Shutdown()
client := a.Client() client := a.Client()

View File

@ -42,7 +42,7 @@ func TestStaticResolver_Resolve(t *testing.T) {
func TestConsulResolver_Resolve(t *testing.T) { func TestConsulResolver_Resolve(t *testing.T) {
// Setup a local test agent to query // 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() defer agent.Shutdown()
cfg := api.DefaultConfig() cfg := api.DefaultConfig()

View File

@ -127,7 +127,7 @@ func TestService_Dial(t *testing.T) {
func TestService_ServerTLSConfig(t *testing.T) { func TestService_ServerTLSConfig(t *testing.T) {
require := require.New(t) require := require.New(t)
a := agent.NewTestAgent(t, "007", "") a := agent.StartTestAgent(t, agent.TestAgent{Name: "007"})
defer a.Shutdown() defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1") testrpc.WaitForTestAgent(t, a.RPC, "dc1")
client := a.Client() client := a.Client()

View File

@ -152,7 +152,7 @@ func TestServerSideVerifier(t *testing.T) {
apiCA2 := testCertPEMBlock(t, apiCA2PEM) apiCA2 := testCertPEMBlock(t, apiCA2PEM)
// Setup a local test agent to query // 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() defer agent.Shutdown()
testrpc.WaitForTestAgent(t, agent.RPC, "dc1") testrpc.WaitForTestAgent(t, agent.RPC, "dc1")