From 2588f22be7f474e82ec6ad3cb07e97f321f59318 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 31 May 2017 10:56:19 +0200 Subject: [PATCH] test: start secondary agents also via TestAgent --- command/agent/acl_test.go | 2 ++ command/agent/agent_test.go | 66 ++++++++++++------------------------- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/command/agent/acl_test.go b/command/agent/acl_test.go index fa2fe016c..5c28921f6 100644 --- a/command/agent/acl_test.go +++ b/command/agent/acl_test.go @@ -20,6 +20,8 @@ func TestACL_Bad_Config(t *testing.T) { cfg.ACLDownPolicy = "nope" cfg.DataDir = testutil.TempDir(t, "agent") + // do not use TestAgent here since we want + // the agent to fail during startup. _, err := NewAgent(cfg) if err == nil || !strings.Contains(err.Error(), "invalid ACL down policy") { t.Fatalf("err: %v", err) diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 8c93827be..f4de82afb 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -47,7 +47,7 @@ func externalIP() (string, error) { func TestAgent_MultiStartStop(t *testing.T) { for i := 0; i < 100; i++ { t.Run("", func(t *testing.T) { - t.Parallel() + t.Parallel() a := NewTestAgent(t.Name(), nil) time.Sleep(250 * time.Millisecond) a.Shutdown() @@ -964,21 +964,15 @@ func TestAgent_PersistService(t *testing.T) { a.Shutdown() // Should load it back during later start - agent2, err := NewAgent(cfg) - if err != nil { - t.Fatalf("err: %s", err) - } - if err := agent2.Start(); err != nil { - t.Fatal(err) - } - defer agent2.Shutdown() + a2 := NewTestAgent(t.Name()+"-a2", cfg) + defer a2.Shutdown() - restored, ok := agent2.state.services[svc.ID] + restored, ok := a2.state.services[svc.ID] if !ok { - t.Fatalf("bad: %#v", agent2.state.services) + t.Fatalf("bad: %#v", a2.state.services) } - if agent2.state.serviceTokens[svc.ID] != "mytoken" { - t.Fatalf("bad: %#v", agent2.state.services[svc.ID]) + if a2.state.serviceTokens[svc.ID] != "mytoken" { + t.Fatalf("bad: %#v", a2.state.services[svc.ID]) } if restored.Port != 8001 { t.Fatalf("bad: %#v", restored) @@ -1099,20 +1093,14 @@ func TestAgent_PurgeServiceOnDuplicate(t *testing.T) { } cfg.Services = []*ServiceDefinition{svc2} - agent2, err := NewAgent(cfg) - if err != nil { - t.Fatalf("err: %s", err) - } - if err := agent2.Start(); err != nil { - t.Fatal(err) - } - defer agent2.Shutdown() + a2 := NewTestAgent(t.Name()+"-a2", cfg) + defer a2.Shutdown() file := filepath.Join(a.Config.DataDir, servicesDir, stringHash(svc1.ID)) if _, err := os.Stat(file); err == nil { t.Fatalf("should have removed persisted service") } - result, ok := agent2.state.services[svc2.ID] + result, ok := a2.state.services[svc2.ID] if !ok { t.Fatalf("missing service registration") } @@ -1197,18 +1185,12 @@ func TestAgent_PersistCheck(t *testing.T) { a.Shutdown() // Should load it back during later start - agent2, err := NewAgent(cfg) - if err != nil { - t.Fatalf("err: %s", err) - } - if err := agent2.Start(); err != nil { - t.Fatal(err) - } - defer agent2.Shutdown() + a2 := NewTestAgent(t.Name()+"-a2", cfg) + defer a2.Shutdown() - result, ok := agent2.state.checks[check.CheckID] + result, ok := a2.state.checks[check.CheckID] if !ok { - t.Fatalf("bad: %#v", agent2.state.checks) + t.Fatalf("bad: %#v", a2.state.checks) } if result.Status != api.HealthCritical { t.Fatalf("bad: %#v", result) @@ -1218,11 +1200,11 @@ func TestAgent_PersistCheck(t *testing.T) { } // Should have restored the monitor - if _, ok := agent2.checkMonitors[check.CheckID]; !ok { - t.Fatalf("bad: %#v", agent2.checkMonitors) + if _, ok := a2.checkMonitors[check.CheckID]; !ok { + t.Fatalf("bad: %#v", a2.checkMonitors) } - if agent2.state.checkTokens[check.CheckID] != "mytoken" { - t.Fatalf("bad: %s", agent2.state.checkTokens[check.CheckID]) + if a2.state.checkTokens[check.CheckID] != "mytoken" { + t.Fatalf("bad: %s", a2.state.checkTokens[check.CheckID]) } } @@ -1292,20 +1274,14 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) { } cfg.Checks = []*CheckDefinition{check2} - agent2, err := NewAgent(cfg) - if err != nil { - t.Fatalf("err: %s", err) - } - if err := agent2.Start(); err != nil { - t.Fatal(err) - } - defer agent2.Shutdown() + a2 := NewTestAgent(t.Name()+"-a2", cfg) + defer a2.Shutdown() file := filepath.Join(a.Config.DataDir, checksDir, checkIDHash(check1.CheckID)) if _, err := os.Stat(file); err == nil { t.Fatalf("should have removed persisted check") } - result, ok := agent2.state.checks[check2.ID] + result, ok := a2.state.checks[check2.ID] if !ok { t.Fatalf("missing check registration") }