Rename NewTestAgentWithFields to StartTestAgent

This function now only starts the agent.

Using:

git grep -l 'StartTestAgent(t, true,' | \
        xargs sed -i -e 's/StartTestAgent(t, true,/StartTestAgent(t,/g'
This commit is contained in:
Daniel Nephin 2020-03-31 16:24:39 -04:00
parent f9f6b14533
commit e759daafdd
8 changed files with 41 additions and 46 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 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"}) a1 := StartTestAgent(t, TestAgent{Name: "Agent1"})
defer a1.Shutdown() 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, a1.RPC, "dc1")
testrpc.WaitForLeader(t, a2.RPC, "dc1") testrpc.WaitForLeader(t, a2.RPC, "dc1")
@ -5236,14 +5236,14 @@ func TestAgentConnectCALeafCert_secondaryDC_good(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) require := require.New(t)
a1 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc1", HCL: ` a1 := StartTestAgent(t, 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 := NewTestAgentWithFields(t, true, TestAgent{Name: "dc2", HCL: ` a2 := StartTestAgent(t, TestAgent{Name: "dc2", HCL: `
datacenter = "dc2" datacenter = "dc2"
primary_datacenter = "dc1" primary_datacenter = "dc1"
`}) `})

View File

@ -1409,7 +1409,7 @@ func TestAgent_RestoreServiceWithAliasCheck(t *testing.T) {
enable_central_service_config = false enable_central_service_config = false
data_dir = "` + dataDir + `" 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 os.RemoveAll(dataDir)
defer a.Shutdown() defer a.Shutdown()
@ -1493,7 +1493,7 @@ node_name = "` + a.Config.NodeName + `"
t.Helper() t.Helper()
// Reload and retain former NodeID and data directory. // 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() defer a2.Shutdown()
a = nil a = nil
@ -1757,7 +1757,7 @@ func TestAgent_HTTPCheck_EnableAgentTLSForChecks(t *testing.T) {
t.Parallel() t.Parallel()
run := func(t *testing.T, ca string) { run := func(t *testing.T, ca string) {
a := NewTestAgentWithFields(t, true, TestAgent{ a := StartTestAgent(t, TestAgent{
UseTLS: true, UseTLS: true,
HCL: ` HCL: `
enable_agent_tls_for_checks = true enable_agent_tls_for_checks = true
@ -1886,7 +1886,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) {
bootstrap = false bootstrap = false
data_dir = "` + dataDir + `" data_dir = "` + dataDir + `"
` + extraHCL ` + extraHCL
a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir})
defer a.Shutdown() defer a.Shutdown()
svc := &structs.NodeService{ svc := &structs.NodeService{
@ -1952,7 +1952,7 @@ func testAgent_PersistService(t *testing.T, extraHCL string) {
a.Shutdown() a.Shutdown()
// Should load it back during later start // 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() defer a2.Shutdown()
restored := a2.State.ServiceState(structs.NewServiceID(svc.ID, nil)) restored := a2.State.ServiceState(structs.NewServiceID(svc.ID, nil))
@ -2098,7 +2098,7 @@ func testAgent_PurgeServiceOnDuplicate(t *testing.T, extraHCL string) {
server = false server = false
bootstrap = false bootstrap = false
` + extraHCL ` + extraHCL
a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir})
defer a.Shutdown() defer a.Shutdown()
svc1 := &structs.NodeService{ 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 // Try bringing the agent back up with the service already
// existing in the config // existing in the config
a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: cfg + ` a2 := StartTestAgent(t, TestAgent{Name: "Agent2", HCL: cfg + `
service = { service = {
id = "redis" id = "redis"
name = "redis" name = "redis"
@ -2142,7 +2142,7 @@ func TestAgent_PersistCheck(t *testing.T) {
bootstrap = false bootstrap = false
enable_script_checks = true 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 os.RemoveAll(dataDir)
defer a.Shutdown() defer a.Shutdown()
@ -2199,7 +2199,7 @@ func TestAgent_PersistCheck(t *testing.T) {
a.Shutdown() a.Shutdown()
// Should load it back during later start // 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() defer a2.Shutdown()
result := requireCheckExists(t, a2, check.CheckID) result := requireCheckExists(t, a2, check.CheckID)
@ -2251,7 +2251,7 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) {
t.Parallel() t.Parallel()
nodeID := NodeID() nodeID := NodeID()
dataDir := testutil.TempDir(t, "agent") dataDir := testutil.TempDir(t, "agent")
a := NewTestAgentWithFields(t, true, TestAgent{ a := StartTestAgent(t, TestAgent{
DataDir: dataDir, DataDir: dataDir,
HCL: ` HCL: `
node_id = "` + nodeID + `" node_id = "` + nodeID + `"
@ -2279,7 +2279,7 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) {
a.Shutdown() a.Shutdown()
// Start again with the check registered in config // Start again with the check registered in config
a2 := NewTestAgentWithFields(t, true, TestAgent{ a2 := StartTestAgent(t, TestAgent{
Name: "Agent2", Name: "Agent2",
DataDir: dataDir, DataDir: dataDir,
HCL: ` 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 // 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 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent1"}) a1 := StartTestAgent(t, TestAgent{Name: "Agent1"})
defer a1.Shutdown() defer a1.Shutdown()
testrpc.WaitForLeader(t, a1.RPC, "dc1") testrpc.WaitForLeader(t, a1.RPC, "dc1")
a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: ` a2 := StartTestAgent(t, TestAgent{Name: "Agent2", HCL: `
server = false server = false
bootstrap = false bootstrap = false
services { services {
@ -4127,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 := NewTestAgentWithFields(t, true, TestAgent{Name: "bob", HCL: ` a1 := StartTestAgent(t, TestAgent{Name: "bob", HCL: `
domain = "consul" domain = "consul"
node_name = "bob" node_name = "bob"
datacenter = "dc1" datacenter = "dc1"
@ -4212,7 +4212,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
require.NotEmpty(r, a1.PickRandomMeshGatewaySuitableForDialing("dc1")) require.NotEmpty(r, a1.PickRandomMeshGatewaySuitableForDialing("dc1"))
}) })
a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "betty", HCL: ` a2 := StartTestAgent(t, TestAgent{Name: "betty", HCL: `
domain = "consul" domain = "consul"
node_name = "betty" node_name = "betty"
datacenter = "dc2" datacenter = "dc2"
@ -4237,7 +4237,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
defer a2.Shutdown() defer a2.Shutdown()
testrpc.WaitForTestAgent(t, a2.RPC, "dc2") testrpc.WaitForTestAgent(t, a2.RPC, "dc2")
a3 := NewTestAgentWithFields(t, true, TestAgent{Name: "bonnie", HCL: ` a3 := StartTestAgent(t, TestAgent{Name: "bonnie", HCL: `
domain = "consul" domain = "consul"
node_name = "bonnie" node_name = "bonnie"
datacenter = "dc3" datacenter = "dc3"

View File

@ -132,7 +132,7 @@ func TestHTTPServer_H2(t *testing.T) {
t.Parallel() t.Parallel()
// Fire up an agent with TLS enabled. // Fire up an agent with TLS enabled.
a := NewTestAgentWithFields(t, true, TestAgent{ a := StartTestAgent(t, TestAgent{
UseTLS: true, UseTLS: true,
HCL: ` HCL: `
key_file = "../test/client_certs/server.key" key_file = "../test/client_certs/server.key"
@ -486,7 +486,7 @@ func TestContentTypeIsJSON(t *testing.T) {
func TestHTTP_wrap_obfuscateLog(t *testing.T) { func TestHTTP_wrap_obfuscateLog(t *testing.T) {
t.Parallel() t.Parallel()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
a := NewTestAgentWithFields(t, true, TestAgent{LogOutput: buf}) a := StartTestAgent(t, TestAgent{LogOutput: buf})
defer a.Shutdown() defer a.Shutdown()
handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) { handler := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
@ -1288,7 +1288,7 @@ func TestHTTPServer_HandshakeTimeout(t *testing.T) {
t.Parallel() t.Parallel()
// Fire up an agent with TLS enabled. // Fire up an agent with TLS enabled.
a := NewTestAgentWithFields(t, true, TestAgent{ a := StartTestAgent(t, TestAgent{
UseTLS: true, UseTLS: true,
HCL: ` HCL: `
key_file = "../test/client_certs/server.key" key_file = "../test/client_certs/server.key"
@ -1354,7 +1354,7 @@ func TestRPC_HTTPSMaxConnsPerClient(t *testing.T) {
} }
// Fire up an agent with TLS enabled. // Fire up an agent with TLS enabled.
a := NewTestAgentWithFields(t, true, TestAgent{ a := StartTestAgent(t, TestAgent{
UseTLS: tc.tlsEnabled, UseTLS: tc.tlsEnabled,
HCL: hclPrefix + ` HCL: hclPrefix + `
limits { limits {

View File

@ -54,7 +54,7 @@ func TestAgent_LoadKeyrings(t *testing.T) {
// Server should auto-load LAN and WAN keyring files // Server should auto-load LAN and WAN keyring files
t.Run("server with keys", func(t *testing.T) { 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() defer a2.Shutdown()
c2 := a2.consulConfig() c2 := a2.consulConfig()
@ -80,7 +80,7 @@ func TestAgent_LoadKeyrings(t *testing.T) {
// Client should auto-load only the LAN keyring file // Client should auto-load only the LAN keyring file
t.Run("client with keys", func(t *testing.T) { t.Run("client with keys", func(t *testing.T) {
a3 := NewTestAgentWithFields(t, true, TestAgent{HCL: ` a3 := StartTestAgent(t, TestAgent{HCL: `
server = false server = false
bootstrap = false bootstrap = false
`, Key: key}) `, Key: key})
@ -272,7 +272,7 @@ func TestAgentKeyring_ACL(t *testing.T) {
key1 := "tbLJg26ZJyJ9pK3qhc9jig==" key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
key2 := "4leC33rgtXKIVUr9Nr0snQ==" key2 := "4leC33rgtXKIVUr9Nr0snQ=="
a := NewTestAgentWithFields(t, true, TestAgent{HCL: TestACLConfig() + ` a := StartTestAgent(t, TestAgent{HCL: TestACLConfig() + `
acl_datacenter = "dc1" acl_datacenter = "dc1"
acl_master_token = "root" acl_master_token = "root"
acl_default_policy = "deny" acl_default_policy = "deny"

View File

@ -302,7 +302,7 @@ func TestServiceManager_PersistService_API(t *testing.T) {
bootstrap = false bootstrap = false
data_dir = "` + dataDir + `" data_dir = "` + dataDir + `"
` `
a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir})
defer a.Shutdown() defer a.Shutdown()
// Join first // Join first
@ -465,7 +465,7 @@ func TestServiceManager_PersistService_API(t *testing.T) {
serverAgent.Shutdown() serverAgent.Shutdown()
// Should load it back during later start. // 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() defer a2.Shutdown()
{ {
@ -540,7 +540,7 @@ func TestServiceManager_PersistService_ConfigFiles(t *testing.T) {
bootstrap = false bootstrap = false
` + serviceSnippet ` + serviceSnippet
a := NewTestAgentWithFields(t, true, TestAgent{HCL: cfg, DataDir: dataDir}) a := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: dataDir})
defer a.Shutdown() defer a.Shutdown()
// Join first // Join first
@ -639,7 +639,7 @@ func TestServiceManager_PersistService_ConfigFiles(t *testing.T) {
serverAgent.Shutdown() serverAgent.Shutdown()
// Should load it back during later start. // 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() defer a2.Shutdown()
{ {

View File

@ -325,7 +325,7 @@ func TestAgent_sidecarServiceFromNodeService(t *testing.T) {
} }
require := require.New(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() defer a.Shutdown()
if tt.preRegister != nil { if tt.preRegister != nil {

View File

@ -91,20 +91,15 @@ type TestAgent struct {
// The caller is responsible for calling Shutdown() to stop the agent and remove // The caller is responsible for calling Shutdown() to stop the agent and remove
// temporary directories. // temporary directories.
func NewTestAgent(t *testing.T, hcl string) *TestAgent { 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, // StartTestAgent and wait for it to become available. If the agent fails to
// and a boolean 'start', which indicates whether or not the TestAgent should // start the test will be marked failed and execution will stop.
// 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. // The caller is responsible for calling Shutdown() to stop the agent and remove
func NewTestAgentWithFields(t *testing.T, start bool, ta TestAgent) *TestAgent { // temporary directories.
// copy values func StartTestAgent(t *testing.T, a TestAgent) *TestAgent {
a := ta
if !start {
return &a
}
retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) { retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) {
if err := a.Start(t); err != nil { if err := a.Start(t); err != nil {
r.Fatal(err) r.Fatal(err)

View File

@ -11,7 +11,7 @@ import (
func TestMonitorCommand_exitsOnSignalBeforeLinesArrive(t *testing.T) { func TestMonitorCommand_exitsOnSignalBeforeLinesArrive(t *testing.T) {
t.Parallel() t.Parallel()
a := agent.NewTestAgentWithFields(t, true, agent.TestAgent{}) a := agent.StartTestAgent(t, agent.TestAgent{})
defer a.Shutdown() defer a.Shutdown()
shutdownCh := make(chan struct{}) shutdownCh := make(chan struct{})
@ -64,7 +64,7 @@ func TestMonitorCommand_exitsOnSignalBeforeLinesArrive(t *testing.T) {
func TestMonitorCommand_LogJSONValidFlag(t *testing.T) { func TestMonitorCommand_LogJSONValidFlag(t *testing.T) {
t.Parallel() t.Parallel()
a := agent.NewTestAgentWithFields(t, true, agent.TestAgent{}) a := agent.StartTestAgent(t, agent.TestAgent{})
defer a.Shutdown() defer a.Shutdown()
shutdownCh := make(chan struct{}) shutdownCh := make(chan struct{})