Remove t.Name() from TestAgent.Name

And re-add the name to the logger so that log messages from different agents
in a single can be identified.
This commit is contained in:
Daniel Nephin 2020-03-30 16:05:27 -04:00
parent 231c99f7b4
commit ad7c78f134
4 changed files with 45 additions and 34 deletions

View File

@ -139,7 +139,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 := NewTestAgentWithFields(t, false, TestAgent{HCL: tt.hcl})
err := a.Start() err := a.Start(t)
if tt.wantErr { if tt.wantErr {
if err == nil { if err == nil {
t.Fatal("expected error, got nil") t.Fatal("expected error, got nil")
@ -1773,7 +1773,6 @@ func TestAgent_HTTPCheck_EnableAgentTLSForChecks(t *testing.T) {
run := func(t *testing.T, ca string) { run := func(t *testing.T, ca string) {
a := NewTestAgentWithFields(t, true, TestAgent{ a := NewTestAgentWithFields(t, true, TestAgent{
Name: t.Name(),
UseTLS: true, UseTLS: true,
HCL: ` HCL: `
enable_agent_tls_for_checks = true enable_agent_tls_for_checks = true
@ -2130,7 +2129,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: t.Name() + "-a2", HCL: cfg + ` a2 := NewTestAgentWithFields(t, true, TestAgent{Name: "Agent2", HCL: cfg + `
service = { service = {
id = "redis" id = "redis"
name = "redis" name = "redis"
@ -2215,7 +2214,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: t.Name() + "-a2", HCL: cfg, DataDir: dataDir}) a2 := NewTestAgentWithFields(t, true, 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)
@ -2268,7 +2267,6 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) {
nodeID := NodeID() nodeID := NodeID()
dataDir := testutil.TempDir(t, "agent") dataDir := testutil.TempDir(t, "agent")
a := NewTestAgentWithFields(t, true, TestAgent{ a := NewTestAgentWithFields(t, true, TestAgent{
Name: t.Name(),
DataDir: dataDir, DataDir: dataDir,
HCL: ` HCL: `
node_id = "` + nodeID + `" node_id = "` + nodeID + `"
@ -2297,7 +2295,7 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) {
// Start again with the check registered in config // Start again with the check registered in config
a2 := NewTestAgentWithFields(t, true, TestAgent{ a2 := NewTestAgentWithFields(t, true, TestAgent{
Name: t.Name() + "-a2", Name: "Agent2",
DataDir: dataDir, DataDir: dataDir,
HCL: ` HCL: `
node_id = "` + nodeID + `" node_id = "` + nodeID + `"
@ -3223,8 +3221,8 @@ func TestAgent_reloadWatches(t *testing.T) {
func TestAgent_reloadWatchesHTTPS(t *testing.T) { func TestAgent_reloadWatchesHTTPS(t *testing.T) {
t.Parallel() t.Parallel()
a := TestAgent{Name: t.Name(), UseTLS: true} a := TestAgent{UseTLS: true}
if err := a.Start(); err != nil { if err := a.Start(t); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer a.Shutdown() defer a.Shutdown()

View File

@ -1159,12 +1159,12 @@ func TestAgentAntiEntropy_RemovingServiceAndCheck(t *testing.T) {
func TestAgentAntiEntropy_Checks_ACLDeny(t *testing.T) { func TestAgentAntiEntropy_Checks_ACLDeny(t *testing.T) {
t.Parallel() t.Parallel()
dc := "dc1" dc := "dc1"
a := &agent.TestAgent{Name: t.Name(), HCL: ` a := &agent.TestAgent{HCL: `
acl_datacenter = "` + dc + `" acl_datacenter = "` + dc + `"
acl_master_token = "root" acl_master_token = "root"
acl_default_policy = "deny" acl_default_policy = "deny"
acl_enforce_version_8 = true`} acl_enforce_version_8 = true`}
if err := a.Start(); err != nil { if err := a.Start(t); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer a.Shutdown() defer a.Shutdown()
@ -1429,10 +1429,10 @@ func TestAgent_UpdateCheck_DiscardOutput(t *testing.T) {
func TestAgentAntiEntropy_Check_DeferSync(t *testing.T) { func TestAgentAntiEntropy_Check_DeferSync(t *testing.T) {
t.Parallel() t.Parallel()
a := &agent.TestAgent{Name: t.Name(), HCL: ` a := &agent.TestAgent{HCL: `
check_update_interval = "500ms" check_update_interval = "500ms"
`} `}
if err := a.Start(); err != nil { if err := a.Start(t); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer a.Shutdown() defer a.Shutdown()
@ -1637,12 +1637,12 @@ func TestAgentAntiEntropy_NodeInfo(t *testing.T) {
nodeMeta := map[string]string{ nodeMeta := map[string]string{
"somekey": "somevalue", "somekey": "somevalue",
} }
a := &agent.TestAgent{Name: t.Name(), HCL: ` a := &agent.TestAgent{HCL: `
node_id = "40e4a748-2192-161a-0510-9bf59fe950b5" node_id = "40e4a748-2192-161a-0510-9bf59fe950b5"
node_meta { node_meta {
somekey = "somevalue" somekey = "somevalue"
}`} }`}
if err := a.Start(); err != nil { if err := a.Start(t); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer a.Shutdown() defer a.Shutdown()

View File

@ -16,6 +16,7 @@ import (
"time" "time"
metrics "github.com/armon/go-metrics" metrics "github.com/armon/go-metrics"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-hclog" "github.com/hashicorp/go-hclog"
uuid "github.com/hashicorp/go-uuid" uuid "github.com/hashicorp/go-uuid"
@ -100,15 +101,12 @@ func NewTestAgent(t *testing.T, name string, hcl string) *TestAgent {
func NewTestAgentWithFields(t *testing.T, start bool, ta TestAgent) *TestAgent { func NewTestAgentWithFields(t *testing.T, start bool, ta TestAgent) *TestAgent {
// copy values // copy values
a := ta a := ta
if a.Name == "" {
a.Name = t.Name()
}
if !start { if !start {
return &a return &a
} }
retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) { retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) {
if err := a.Start(); err != nil { if err := a.Start(t); err != nil {
r.Fatal(err) r.Fatal(err)
} }
}) })
@ -118,32 +116,45 @@ func NewTestAgentWithFields(t *testing.T, start bool, ta TestAgent) *TestAgent {
// Start starts a test agent. It returns an error if the agent could not be started. // Start starts a test agent. It returns an error if the agent could not be started.
// If no error is returned, the caller must call Shutdown() when finished. // If no error is returned, the caller must call Shutdown() when finished.
func (a *TestAgent) Start() (err error) { func (a *TestAgent) Start(t *testing.T) (err error) {
if a.Agent != nil { if a.Agent != nil {
return fmt.Errorf("TestAgent already started") return fmt.Errorf("TestAgent already started")
} }
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() {
name = "TestAgent"
}
var cleanupTmpDir = func() { var cleanupTmpDir = func() {
// Clean out the data dir if we are responsible for it before we // Clean out the data dir if we are responsible for it before we
// try again, since the old ports may have gotten written to // try again, since the old ports may have gotten written to
// the data dir, such as in the Raft configuration. // the data dir, such as in the Raft configuration.
if a.DataDir != "" { if a.DataDir != "" {
if err := os.RemoveAll(a.DataDir); err != nil { if err := os.RemoveAll(a.DataDir); err != nil {
fmt.Printf("%s Error resetting data dir: %s", a.Name, err) fmt.Printf("%s Error resetting data dir: %s", name, err)
} }
} }
} }
var hclDataDir string var hclDataDir string
if a.DataDir == "" { if a.DataDir == "" {
name := "agent" dirname := "agent"
if a.Name != "" { if name != "" {
name = a.Name + "-agent" dirname = name + "-agent"
} }
name = strings.Replace(name, "/", "_", -1) dirname = strings.Replace(dirname, "/", "_", -1)
d, err := ioutil.TempDir(TempDir, name) d, err := ioutil.TempDir(TempDir, dirname)
if err != nil { if err != nil {
return fmt.Errorf("Error creating data dir %s: %s", filepath.Join(TempDir, name), err) return fmt.Errorf("Error creating data dir %s: %s", filepath.Join(TempDir, dirname), err)
} }
// Convert windows style path to posix style path // Convert windows style path to posix style path
// to avoid illegal char escape error when hcl // to avoid illegal char escape error when hcl
@ -161,14 +172,15 @@ func (a *TestAgent) Start() (err error) {
Level: hclog.Debug, Level: hclog.Debug,
Output: logOutput, Output: logOutput,
TimeFormat: "04:05.000", TimeFormat: "04:05.000",
Name: name,
}) })
portsConfig, returnPortsFn := randomPortsSource(a.UseTLS) portsConfig, returnPortsFn := randomPortsSource(a.UseTLS)
a.returnPortsFn = returnPortsFn a.returnPortsFn = returnPortsFn
a.Config = TestConfig(logger, a.Config = TestConfig(logger,
portsConfig, portsConfig,
config.Source{Name: a.Name, Format: "hcl", Data: a.HCL}, config.Source{Name: name, Format: "hcl", Data: a.HCL},
config.Source{Name: a.Name + ".data_dir", Format: "hcl", Data: hclDataDir}, config.Source{Name: name + ".data_dir", Format: "hcl", Data: hclDataDir},
) )
defer func() { defer func() {
@ -214,7 +226,7 @@ func (a *TestAgent) Start() (err error) {
agent.ShutdownAgent() agent.ShutdownAgent()
agent.ShutdownEndpoints() agent.ShutdownEndpoints()
return fmt.Errorf("%s %s Error starting agent: %s", id, a.Name, err) return fmt.Errorf("%s %s Error starting agent: %s", id, name, err)
} }
a.Agent = agent a.Agent = agent
@ -225,7 +237,7 @@ func (a *TestAgent) Start() (err error) {
if err := a.waitForUp(); err != nil { if err := a.waitForUp(); err != nil {
cleanupTmpDir() cleanupTmpDir()
a.Shutdown() a.Shutdown()
return err return errwrap.Wrapf(name+": {{err}}", err)
} }
a.dns = a.dnsServers[0] a.dns = a.dnsServers[0]
@ -243,7 +255,7 @@ func (a *TestAgent) waitForUp() error {
var out structs.IndexedNodes var out structs.IndexedNodes
for ; !time.Now().After(deadline); time.Sleep(timer.Wait) { for ; !time.Now().After(deadline); time.Sleep(timer.Wait) {
if len(a.httpServers) == 0 { if len(a.httpServers) == 0 {
retErr = fmt.Errorf("%s: waiting for server", a.Name) retErr = fmt.Errorf("waiting for server")
continue // fail, try again continue // fail, try again
} }
if a.Config.Bootstrap && a.Config.ServerMode { if a.Config.Bootstrap && a.Config.ServerMode {
@ -260,11 +272,11 @@ func (a *TestAgent) waitForUp() error {
continue // fail, try again continue // fail, try again
} }
if !out.QueryMeta.KnownLeader { if !out.QueryMeta.KnownLeader {
retErr = fmt.Errorf("%s: No leader", a.Name) retErr = fmt.Errorf("No leader")
continue // fail, try again continue // fail, try again
} }
if out.Index == 0 { if out.Index == 0 {
retErr = fmt.Errorf("%s: Consul index is 0", a.Name) retErr = fmt.Errorf("Consul index is 0")
continue // fail, try again continue // fail, try again
} }
return nil // success return nil // success
@ -273,7 +285,7 @@ func (a *TestAgent) waitForUp() error {
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
_, err := a.httpServers[0].AgentSelf(resp, req) _, err := a.httpServers[0].AgentSelf(resp, req)
if err != nil || resp.Code != 200 { if err != nil || resp.Code != 200 {
retErr = fmt.Errorf("%s: failed OK response: %v", a.Name, err) retErr = fmt.Errorf("failed OK response: %v", err)
continue continue
} }
return nil // success return nil // success

1
go.mod
View File

@ -30,6 +30,7 @@ require (
github.com/google/tcpproxy v0.0.0-20180808230851-dfa16c61dad2 github.com/google/tcpproxy v0.0.0-20180808230851-dfa16c61dad2
github.com/hashicorp/consul/api v1.4.0 github.com/hashicorp/consul/api v1.4.0
github.com/hashicorp/consul/sdk v0.4.0 github.com/hashicorp/consul/sdk v0.4.0
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-bexpr v0.1.2 github.com/hashicorp/go-bexpr v0.1.2
github.com/hashicorp/go-checkpoint v0.0.0-20171009173528-1545e56e46de github.com/hashicorp/go-checkpoint v0.0.0-20171009173528-1545e56e46de
github.com/hashicorp/go-cleanhttp v0.5.1 github.com/hashicorp/go-cleanhttp v0.5.1