From da25a3d5ceae0a2fb68007cd2640ae5ced57cee4 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 20 Jul 2017 21:07:32 -0700 Subject: [PATCH] Switch to in-process agent --- command/agent/config.go | 2 +- command/agent/testagent.go | 11 +++++--- command/agent_info_test.go | 4 +-- command/alloc_status_test.go | 10 +++---- command/check_test.go | 4 +-- command/client_config_test.go | 7 +++-- command/eval_status_test.go | 4 +-- command/fs_test.go | 4 +-- command/helpers_test.go | 4 +-- command/inspect_test.go | 4 +-- command/logs_test.go | 4 +-- command/monitor_test.go | 8 +++--- command/node_drain_test.go | 4 +-- command/node_status_test.go | 15 +++++------ command/operator_raft_list_test.go | 4 +-- command/operator_raft_remove_test.go | 4 +-- command/server_members_test.go | 4 +-- command/status_test.go | 7 ++--- command/stop_test.go | 4 +-- command/util_test.go | 39 ++++++++-------------------- 20 files changed, 64 insertions(+), 83 deletions(-) diff --git a/command/agent/config.go b/command/agent/config.go index 36769cb30..4940719b0 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -774,7 +774,7 @@ func (c *Config) normalizeAddrs() error { addr, err = normalizeAdvertise(c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode) if err != nil { - return fmt.Errorf("Failed to parse HTTP advertise address: %v", err) + return fmt.Errorf("Failed to parse HTTP advertise address (%v, %v, %v, %v): %v", c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode, err) } c.AdvertiseAddrs.HTTP = addr diff --git a/command/agent/testagent.go b/command/agent/testagent.go index 0ec4ea1bc..3e3d65063 100644 --- a/command/agent/testagent.go +++ b/command/agent/testagent.go @@ -102,7 +102,9 @@ func (a *TestAgent) Start() *TestAgent { for i := 10; i >= 0; i-- { pickRandomPorts(a.Config) - a.Config.NodeName = fmt.Sprintf("Node %d", a.Config.Ports.RPC) + if a.Config.NodeName == "" { + a.Config.NodeName = fmt.Sprintf("Node %d", a.Config.Ports.RPC) + } // write the keyring if a.Key != "" { @@ -202,7 +204,7 @@ func (a *TestAgent) HTTPAddr() string { if a.Server == nil { return "" } - return a.Server.Addr + return "http://" + a.Server.Addr } func (a *TestAgent) Client() *api.Client { @@ -210,7 +212,7 @@ func (a *TestAgent) Client() *api.Client { conf.Address = a.HTTPAddr() c, err := api.NewClient(conf) if err != nil { - panic(fmt.Sprintf("Error creating consul API client: %s", err)) + panic(fmt.Sprintf("Error creating Nomad API client: %s", err)) } return c } @@ -249,6 +251,9 @@ func (a *TestAgent) config() *Config { config := nomad.DefaultConfig() conf.NomadConfig = config + // Set the name + conf.NodeName = a.Name + // Bind and set ports conf.BindAddr = "127.0.0.1" diff --git a/command/agent_info_test.go b/command/agent_info_test.go index fd34e206e..787d312b3 100644 --- a/command/agent_info_test.go +++ b/command/agent_info_test.go @@ -12,8 +12,8 @@ func TestAgentInfoCommand_Implements(t *testing.T) { } func TestAgentInfoCommand_Run(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}} diff --git a/command/alloc_status_test.go b/command/alloc_status_test.go index 6eb5e7aa1..5601e950a 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -15,8 +15,8 @@ func TestAllocStatusCommand_Implements(t *testing.T) { } func TestAllocStatusCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}} @@ -76,10 +76,8 @@ func TestAllocStatusCommand_Fails(t *testing.T) { } func TestAllocStatusCommand_Run(t *testing.T) { - srv, client, url := testServer(t, func(c *testutil.TestServerConfig) { - c.DevMode = true - }) - defer srv.Stop() + srv, client, url := testServer(t, true, nil) + defer srv.Shutdown() // Wait for a node to be ready testutil.WaitForResult(func() (bool, error) { diff --git a/command/check_test.go b/command/check_test.go index 00e6e53c3..76fdd8318 100644 --- a/command/check_test.go +++ b/command/check_test.go @@ -8,8 +8,8 @@ import ( ) func TestAgentCheckCommand_ServerHealth(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &AgentCheckCommand{Meta: Meta{Ui: ui}} diff --git a/command/client_config_test.go b/command/client_config_test.go index e1e96e001..a5ee9d299 100644 --- a/command/client_config_test.go +++ b/command/client_config_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/hashicorp/nomad/testutil" + "github.com/hashicorp/nomad/command/agent" "github.com/mitchellh/cli" ) @@ -13,11 +13,10 @@ func TestClientConfigCommand_Implements(t *testing.T) { } func TestClientConfigCommand_UpdateServers(t *testing.T) { - srv, _, url := testServer(t, func(c *testutil.TestServerConfig) { - c.Client.Enabled = true + srv, _, url := testServer(t, true, func(c *agent.Config) { c.Server.BootstrapExpect = 0 }) - defer srv.Stop() + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &ClientConfigCommand{Meta: Meta{Ui: ui}} diff --git a/command/eval_status_test.go b/command/eval_status_test.go index de5bd6d87..06c97c884 100644 --- a/command/eval_status_test.go +++ b/command/eval_status_test.go @@ -12,8 +12,8 @@ func TestEvalStatusCommand_Implements(t *testing.T) { } func TestEvalStatusCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &EvalStatusCommand{Meta: Meta{Ui: ui}} diff --git a/command/fs_test.go b/command/fs_test.go index 7edecd939..49cf4eeee 100644 --- a/command/fs_test.go +++ b/command/fs_test.go @@ -12,8 +12,8 @@ func TestFSCommand_Implements(t *testing.T) { } func TestFSCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &FSCommand{Meta: Meta{Ui: ui}} diff --git a/command/helpers_test.go b/command/helpers_test.go index 0681f6987..a4b9d61d1 100644 --- a/command/helpers_test.go +++ b/command/helpers_test.go @@ -43,8 +43,8 @@ func TestHelpers_FormatList(t *testing.T) { } func TestHelpers_NodeID(t *testing.T) { - srv, _, _ := testServer(t, nil) - defer srv.Stop() + srv, _, _ := testServer(t, false, nil) + defer srv.Shutdown() meta := Meta{Ui: new(cli.MockUi)} client, err := meta.Client() diff --git a/command/inspect_test.go b/command/inspect_test.go index 413e10d5c..e069415dd 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -12,8 +12,8 @@ func TestInspectCommand_Implements(t *testing.T) { } func TestInspectCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &InspectCommand{Meta: Meta{Ui: ui}} diff --git a/command/logs_test.go b/command/logs_test.go index 2400b6302..d76db1a1f 100644 --- a/command/logs_test.go +++ b/command/logs_test.go @@ -12,8 +12,8 @@ func TestLogsCommand_Implements(t *testing.T) { } func TestLogsCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &LogsCommand{Meta: Meta{Ui: ui}} diff --git a/command/monitor_test.go b/command/monitor_test.go index 3579eea06..249c8fce7 100644 --- a/command/monitor_test.go +++ b/command/monitor_test.go @@ -169,8 +169,8 @@ func TestMonitor_Update_AllocModification(t *testing.T) { } func TestMonitor_Monitor(t *testing.T) { - srv, client, _ := testServer(t, nil) - defer srv.Stop() + srv, client, _ := testServer(t, false, nil) + defer srv.Shutdown() // Create the monitor ui := new(cli.MockUi) @@ -215,8 +215,8 @@ func TestMonitor_Monitor(t *testing.T) { } func TestMonitor_MonitorWithPrefix(t *testing.T) { - srv, client, _ := testServer(t, nil) - defer srv.Stop() + srv, client, _ := testServer(t, false, nil) + defer srv.Shutdown() // Create the monitor ui := new(cli.MockUi) diff --git a/command/node_drain_test.go b/command/node_drain_test.go index 30ce9a0cb..f52d412e0 100644 --- a/command/node_drain_test.go +++ b/command/node_drain_test.go @@ -12,8 +12,8 @@ func TestNodeDrainCommand_Implements(t *testing.T) { } func TestNodeDrainCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &NodeDrainCommand{Meta: Meta{Ui: ui}} diff --git a/command/node_status_test.go b/command/node_status_test.go index 3fba4c88c..e46237b44 100644 --- a/command/node_status_test.go +++ b/command/node_status_test.go @@ -5,6 +5,7 @@ import ( "strings" "testing" + "github.com/hashicorp/nomad/command/agent" "github.com/hashicorp/nomad/testutil" "github.com/mitchellh/cli" ) @@ -15,11 +16,10 @@ func TestNodeStatusCommand_Implements(t *testing.T) { func TestNodeStatusCommand_Self(t *testing.T) { // Start in dev mode so we get a node registration - srv, client, url := testServer(t, func(c *testutil.TestServerConfig) { - c.DevMode = true + srv, client, url := testServer(t, true, func(c *agent.Config) { c.NodeName = "mynode" }) - defer srv.Stop() + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} @@ -66,11 +66,10 @@ func TestNodeStatusCommand_Self(t *testing.T) { func TestNodeStatusCommand_Run(t *testing.T) { // Start in dev mode so we get a node registration - srv, client, url := testServer(t, func(c *testutil.TestServerConfig) { - c.DevMode = true + srv, client, url := testServer(t, true, func(c *agent.Config) { c.NodeName = "mynode" }) - defer srv.Stop() + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} @@ -160,8 +159,8 @@ func TestNodeStatusCommand_Run(t *testing.T) { } func TestNodeStatusCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} diff --git a/command/operator_raft_list_test.go b/command/operator_raft_list_test.go index b96b66f50..eb4bfc4f0 100644 --- a/command/operator_raft_list_test.go +++ b/command/operator_raft_list_test.go @@ -12,8 +12,8 @@ func TestOperator_Raft_ListPeers_Implements(t *testing.T) { } func TestOperator_Raft_ListPeers(t *testing.T) { - s, _, addr := testServer(t, nil) - defer s.Stop() + s, _, addr := testServer(t, false, nil) + defer s.Shutdown() ui := new(cli.MockUi) c := &OperatorRaftListCommand{Meta: Meta{Ui: ui}} diff --git a/command/operator_raft_remove_test.go b/command/operator_raft_remove_test.go index a5954d03f..0592b60fb 100644 --- a/command/operator_raft_remove_test.go +++ b/command/operator_raft_remove_test.go @@ -12,8 +12,8 @@ func TestOperator_Raft_RemovePeers_Implements(t *testing.T) { } func TestOperator_Raft_RemovePeer(t *testing.T) { - s, _, addr := testServer(t, nil) - defer s.Stop() + s, _, addr := testServer(t, false, nil) + defer s.Shutdown() ui := new(cli.MockUi) c := &OperatorRaftRemoveCommand{Meta: Meta{Ui: ui}} diff --git a/command/server_members_test.go b/command/server_members_test.go index 41cd6b6eb..42fa36537 100644 --- a/command/server_members_test.go +++ b/command/server_members_test.go @@ -12,8 +12,8 @@ func TestServerMembersCommand_Implements(t *testing.T) { } func TestServerMembersCommand_Run(t *testing.T) { - srv, client, url := testServer(t, nil) - defer srv.Stop() + srv, client, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &ServerMembersCommand{Meta: Meta{Ui: ui}} diff --git a/command/status_test.go b/command/status_test.go index 7d00f7096..7220cc38c 100644 --- a/command/status_test.go +++ b/command/status_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/hashicorp/nomad/api" - "github.com/hashicorp/nomad/testutil" "github.com/mitchellh/cli" ) @@ -14,10 +13,8 @@ func TestStatusCommand_Implements(t *testing.T) { } func TestStatusCommand_Run(t *testing.T) { - srv, client, url := testServer(t, func(c *testutil.TestServerConfig) { - c.DevMode = true - }) - defer srv.Stop() + srv, client, url := testServer(t, true, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &StatusCommand{Meta: Meta{Ui: ui}} diff --git a/command/stop_test.go b/command/stop_test.go index 9abe0548f..10783c516 100644 --- a/command/stop_test.go +++ b/command/stop_test.go @@ -12,8 +12,8 @@ func TestStopCommand_Implements(t *testing.T) { } func TestStopCommand_Fails(t *testing.T) { - srv, _, url := testServer(t, nil) - defer srv.Stop() + srv, _, url := testServer(t, false, nil) + defer srv.Shutdown() ui := new(cli.MockUi) cmd := &StopCommand{Meta: Meta{Ui: ui}} diff --git a/command/util_test.go b/command/util_test.go index 23b7715a7..e22e0f34a 100644 --- a/command/util_test.go +++ b/command/util_test.go @@ -4,39 +4,22 @@ import ( "testing" "github.com/hashicorp/nomad/api" + "github.com/hashicorp/nomad/command/agent" "github.com/hashicorp/nomad/helper" - "github.com/hashicorp/nomad/testutil" ) -// seen is used to track which tests we have already -// marked as parallel. Marking twice causes panic. -var seen map[*testing.T]struct{} - -func init() { - seen = make(map[*testing.T]struct{}) -} - -func testServer( - t *testing.T, - cb testutil.ServerConfigCallback) (*testutil.TestServer, *api.Client, string) { - - // Always run these tests in parallel. - if _, ok := seen[t]; !ok { - seen[t] = struct{}{} - t.Parallel() - } - +func testServer(t *testing.T, runClient bool, cb func(*agent.Config)) (*agent.TestAgent, *api.Client, string) { // Make a new test server - srv := testutil.NewTestServer(t, cb) + a := agent.NewTestAgent(t.Name(), func(config *agent.Config) { + config.Client.Enabled = runClient - // Make a client - clientConf := api.DefaultConfig() - clientConf.Address = "http://" + srv.HTTPAddr - client, err := api.NewClient(clientConf) - if err != nil { - t.Fatalf("err: %s", err) - } - return srv, client, clientConf.Address + if cb != nil { + cb(config) + } + }) + + c := a.Client() + return a, c, a.HTTPAddr() } func testJob(jobID string) *api.Job {