From da25a3d5ceae0a2fb68007cd2640ae5ced57cee4 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 20 Jul 2017 21:07:32 -0700 Subject: [PATCH 1/4] 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 { From a77c6a4f71bd29bac022e3252391139e2d8d6805 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 20 Jul 2017 21:24:21 -0700 Subject: [PATCH 2/4] Parallel --- command/agent_info_test.go | 3 +++ command/alloc_status_test.go | 3 +++ command/check_test.go | 1 + command/client_config_test.go | 3 +++ command/data_format_test.go | 2 ++ command/deployment_fail_test.go | 2 ++ command/deployment_list_test.go | 2 ++ command/deployment_pause_test.go | 2 ++ command/deployment_promote_test.go | 2 ++ command/deployment_resume_test.go | 2 ++ command/deployment_status_test.go | 2 ++ command/eval_status_test.go | 2 ++ command/fs_test.go | 2 ++ command/helpers_test.go | 7 +++++++ command/init_test.go | 3 +++ command/inspect_test.go | 2 ++ command/integration_test.go | 3 ++- command/job_deployments_test.go | 2 ++ command/job_dispatch_test.go | 2 ++ command/job_history_test.go | 2 ++ command/job_promote_test.go | 2 ++ command/job_revert_test.go | 2 ++ command/keygen_test.go | 1 + command/logs_test.go | 2 ++ command/meta_test.go | 1 + command/monitor_test.go | 6 ++++++ command/node_drain_test.go | 2 ++ command/node_status_test.go | 4 ++++ command/operator_raft_list_test.go | 2 ++ command/operator_raft_remove_test.go | 2 ++ command/operator_raft_test.go | 1 + command/operator_test.go | 1 + command/plan_test.go | 4 ++++ command/run_test.go | 5 +++++ command/server_force_leave_test.go | 1 + command/server_join_test.go | 1 + command/server_members_test.go | 3 +++ command/status_test.go | 3 +++ command/stop_test.go | 2 ++ command/validate_test.go | 5 +++++ command/version_test.go | 1 + 41 files changed, 99 insertions(+), 1 deletion(-) diff --git a/command/agent_info_test.go b/command/agent_info_test.go index 787d312b3..99361afc3 100644 --- a/command/agent_info_test.go +++ b/command/agent_info_test.go @@ -8,10 +8,12 @@ import ( ) func TestAgentInfoCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &AgentInfoCommand{} } func TestAgentInfoCommand_Run(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() @@ -25,6 +27,7 @@ func TestAgentInfoCommand_Run(t *testing.T) { } func TestAgentInfoCommand_Fails(t *testing.T) { + t.Parallel() 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 5601e950a..1a5a0e600 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -11,10 +11,12 @@ import ( ) func TestAllocStatusCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &AllocStatusCommand{} } func TestAllocStatusCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() @@ -76,6 +78,7 @@ func TestAllocStatusCommand_Fails(t *testing.T) { } func TestAllocStatusCommand_Run(t *testing.T) { + t.Parallel() srv, client, url := testServer(t, true, nil) defer srv.Shutdown() diff --git a/command/check_test.go b/command/check_test.go index 76fdd8318..4f3e81aac 100644 --- a/command/check_test.go +++ b/command/check_test.go @@ -8,6 +8,7 @@ import ( ) func TestAgentCheckCommand_ServerHealth(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/client_config_test.go b/command/client_config_test.go index a5ee9d299..e00bb6e30 100644 --- a/command/client_config_test.go +++ b/command/client_config_test.go @@ -9,10 +9,12 @@ import ( ) func TestClientConfigCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &ClientConfigCommand{} } func TestClientConfigCommand_UpdateServers(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, true, func(c *agent.Config) { c.Server.BootstrapExpect = 0 }) @@ -52,6 +54,7 @@ func TestClientConfigCommand_UpdateServers(t *testing.T) { } func TestClientConfigCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &ClientConfigCommand{Meta: Meta{Ui: ui}} diff --git a/command/data_format_test.go b/command/data_format_test.go index 94ccdd1b8..7765a0d72 100644 --- a/command/data_format_test.go +++ b/command/data_format_test.go @@ -24,6 +24,7 @@ var ( ) func TestDataFormat(t *testing.T) { + t.Parallel() for k, v := range testFormat { fm, err := DataFormat(k, v) if err != nil { @@ -42,6 +43,7 @@ func TestDataFormat(t *testing.T) { } func TestInvalidJSONTemplate(t *testing.T) { + t.Parallel() // Invalid template {{.foo}} fm, err := DataFormat("template", "{{.foo}}") if err != nil { diff --git a/command/deployment_fail_test.go b/command/deployment_fail_test.go index 7f734b000..638954454 100644 --- a/command/deployment_fail_test.go +++ b/command/deployment_fail_test.go @@ -8,10 +8,12 @@ import ( ) func TestDeploymentFailCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &DeploymentFailCommand{} } func TestDeploymentFailCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &DeploymentFailCommand{Meta: Meta{Ui: ui}} diff --git a/command/deployment_list_test.go b/command/deployment_list_test.go index eaeb00e39..291d837a5 100644 --- a/command/deployment_list_test.go +++ b/command/deployment_list_test.go @@ -8,10 +8,12 @@ import ( ) func TestDeploymentListCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &DeploymentListCommand{} } func TestDeploymentListCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &DeploymentListCommand{Meta: Meta{Ui: ui}} diff --git a/command/deployment_pause_test.go b/command/deployment_pause_test.go index 279a4a9d2..5f495db16 100644 --- a/command/deployment_pause_test.go +++ b/command/deployment_pause_test.go @@ -8,10 +8,12 @@ import ( ) func TestDeploymentPauseCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &DeploymentPauseCommand{} } func TestDeploymentPauseCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &DeploymentPauseCommand{Meta: Meta{Ui: ui}} diff --git a/command/deployment_promote_test.go b/command/deployment_promote_test.go index b3ce0c85a..e8628699c 100644 --- a/command/deployment_promote_test.go +++ b/command/deployment_promote_test.go @@ -8,10 +8,12 @@ import ( ) func TestDeploymentPromoteCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &DeploymentPromoteCommand{} } func TestDeploymentPromoteCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &DeploymentPromoteCommand{Meta: Meta{Ui: ui}} diff --git a/command/deployment_resume_test.go b/command/deployment_resume_test.go index b6833bab7..2a319259a 100644 --- a/command/deployment_resume_test.go +++ b/command/deployment_resume_test.go @@ -8,10 +8,12 @@ import ( ) func TestDeploymentResumeCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &DeploymentResumeCommand{} } func TestDeploymentResumeCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &DeploymentResumeCommand{Meta: Meta{Ui: ui}} diff --git a/command/deployment_status_test.go b/command/deployment_status_test.go index e257b1600..d5ecd9445 100644 --- a/command/deployment_status_test.go +++ b/command/deployment_status_test.go @@ -8,10 +8,12 @@ import ( ) func TestDeploymentStatusCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &DeploymentStatusCommand{} } func TestDeploymentStatusCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &DeploymentStatusCommand{Meta: Meta{Ui: ui}} diff --git a/command/eval_status_test.go b/command/eval_status_test.go index 06c97c884..4f0ea9824 100644 --- a/command/eval_status_test.go +++ b/command/eval_status_test.go @@ -8,10 +8,12 @@ import ( ) func TestEvalStatusCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &EvalStatusCommand{} } func TestEvalStatusCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/fs_test.go b/command/fs_test.go index 49cf4eeee..e188a1a47 100644 --- a/command/fs_test.go +++ b/command/fs_test.go @@ -8,10 +8,12 @@ import ( ) func TestFSCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &FSCommand{} } func TestFSCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/helpers_test.go b/command/helpers_test.go index a4b9d61d1..a0a538c8b 100644 --- a/command/helpers_test.go +++ b/command/helpers_test.go @@ -19,6 +19,7 @@ import ( ) func TestHelpers_FormatKV(t *testing.T) { + t.Parallel() in := []string{"alpha|beta", "charlie|delta", "echo|"} out := formatKV(in) @@ -32,6 +33,7 @@ func TestHelpers_FormatKV(t *testing.T) { } func TestHelpers_FormatList(t *testing.T) { + t.Parallel() in := []string{"alpha|beta||delta"} out := formatList(in) @@ -43,6 +45,7 @@ func TestHelpers_FormatList(t *testing.T) { } func TestHelpers_NodeID(t *testing.T) { + t.Parallel() srv, _, _ := testServer(t, false, nil) defer srv.Shutdown() @@ -59,6 +62,7 @@ func TestHelpers_NodeID(t *testing.T) { } func TestHelpers_LineLimitReader_NoTimeLimit(t *testing.T) { + t.Parallel() helloString := `hello world this @@ -160,6 +164,7 @@ func (t *testReadCloser) Close() error { } func TestHelpers_LineLimitReader_TimeLimit(t *testing.T) { + t.Parallel() // Create the test reader in := &testReadCloser{data: make(chan []byte)} @@ -242,6 +247,7 @@ var ( // Test APIJob with local jobfile func TestJobGetter_LocalFile(t *testing.T) { + t.Parallel() fh, err := ioutil.TempFile("", "nomad") if err != nil { t.Fatalf("err: %s", err) @@ -267,6 +273,7 @@ func TestJobGetter_LocalFile(t *testing.T) { // Test StructJob with jobfile from HTTP Server func TestJobGetter_HTTPServer(t *testing.T) { + t.Parallel() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, job) }) diff --git a/command/init_test.go b/command/init_test.go index 2ab4e3842..4c0da57cb 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -10,10 +10,12 @@ import ( ) func TestInitCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &InitCommand{} } func TestInitCommand_Run(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &InitCommand{Meta: Meta{Ui: ui}} @@ -65,6 +67,7 @@ func TestInitCommand_Run(t *testing.T) { } func TestInitCommand_defaultJob(t *testing.T) { + t.Parallel() // Ensure the job file is always written with spaces instead of tabs. Since // the default job file is embedded in the go file, it's easy for tabs to // slip in. diff --git a/command/inspect_test.go b/command/inspect_test.go index e069415dd..12eb9f004 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -8,10 +8,12 @@ import ( ) func TestInspectCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &InspectCommand{} } func TestInspectCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/integration_test.go b/command/integration_test.go index a5a8fffda..83fae4a4c 100644 --- a/command/integration_test.go +++ b/command/integration_test.go @@ -8,6 +8,7 @@ import ( ) func TestIntegration_Command_NomadInit(t *testing.T) { + t.Parallel() tmpDir, err := ioutil.TempDir("", "nomadtest-rootsecretdir") if err != nil { t.Fatalf("unable to create tempdir for test: %v", err) @@ -25,7 +26,7 @@ func TestIntegration_Command_NomadInit(t *testing.T) { { cmd := exec.Command("nomad", "validate", "example.nomad") cmd.Dir = tmpDir - cmd.Env = []string{`NOMAD_ADDR=http://127.0.0.2:1025`} + cmd.Env = []string{`NOMAD_ADDR=http://127.0.0.1:0`} if err := cmd.Run(); err != nil { t.Fatalf("error validating example.nomad: %v", err) } diff --git a/command/job_deployments_test.go b/command/job_deployments_test.go index f46f0826e..eb365da10 100644 --- a/command/job_deployments_test.go +++ b/command/job_deployments_test.go @@ -8,10 +8,12 @@ import ( ) func TestJobDeploymentsCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &JobDeploymentsCommand{} } func TestJobDeploymentsCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui}} diff --git a/command/job_dispatch_test.go b/command/job_dispatch_test.go index f1f572a5a..d0180dfb5 100644 --- a/command/job_dispatch_test.go +++ b/command/job_dispatch_test.go @@ -8,10 +8,12 @@ import ( ) func TestJobDispatchCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &JobDispatchCommand{} } func TestJobDispatchCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &JobDispatchCommand{Meta: Meta{Ui: ui}} diff --git a/command/job_history_test.go b/command/job_history_test.go index bf8dd5dfb..3aaf6ef18 100644 --- a/command/job_history_test.go +++ b/command/job_history_test.go @@ -8,10 +8,12 @@ import ( ) func TestJobHistoryCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &JobDispatchCommand{} } func TestJobHistoryCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}} diff --git a/command/job_promote_test.go b/command/job_promote_test.go index 068887735..0eec70792 100644 --- a/command/job_promote_test.go +++ b/command/job_promote_test.go @@ -8,10 +8,12 @@ import ( ) func TestJobPromoteCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &JobPromoteCommand{} } func TestJobPromoteCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &JobPromoteCommand{Meta: Meta{Ui: ui}} diff --git a/command/job_revert_test.go b/command/job_revert_test.go index 01d9e4397..5841582d1 100644 --- a/command/job_revert_test.go +++ b/command/job_revert_test.go @@ -8,10 +8,12 @@ import ( ) func TestJobRevertCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &JobDispatchCommand{} } func TestJobRevertCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &JobRevertCommand{Meta: Meta{Ui: ui}} diff --git a/command/keygen_test.go b/command/keygen_test.go index 726dd841f..a9b3dec6d 100644 --- a/command/keygen_test.go +++ b/command/keygen_test.go @@ -8,6 +8,7 @@ import ( ) func TestKeygenCommand(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) c := &KeygenCommand{Meta: Meta{Ui: ui}} code := c.Run(nil) diff --git a/command/logs_test.go b/command/logs_test.go index d76db1a1f..489d89818 100644 --- a/command/logs_test.go +++ b/command/logs_test.go @@ -8,10 +8,12 @@ import ( ) func TestLogsCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &LogsCommand{} } func TestLogsCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/meta_test.go b/command/meta_test.go index 76ff5b3ab..e95b8cd4e 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -8,6 +8,7 @@ import ( ) func TestMeta_FlagSet(t *testing.T) { + t.Parallel() cases := []struct { Flags FlagSetFlags Expected []string diff --git a/command/monitor_test.go b/command/monitor_test.go index 249c8fce7..a6dbf2253 100644 --- a/command/monitor_test.go +++ b/command/monitor_test.go @@ -11,6 +11,7 @@ import ( ) func TestMonitor_Update_Eval(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) mon := newMonitor(ui, nil, fullId) @@ -64,6 +65,7 @@ func TestMonitor_Update_Eval(t *testing.T) { } func TestMonitor_Update_Allocs(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) mon := newMonitor(ui, nil, fullId) @@ -134,6 +136,7 @@ func TestMonitor_Update_Allocs(t *testing.T) { } func TestMonitor_Update_AllocModification(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) mon := newMonitor(ui, nil, fullId) @@ -169,6 +172,7 @@ func TestMonitor_Update_AllocModification(t *testing.T) { } func TestMonitor_Monitor(t *testing.T) { + t.Parallel() srv, client, _ := testServer(t, false, nil) defer srv.Shutdown() @@ -215,6 +219,7 @@ func TestMonitor_Monitor(t *testing.T) { } func TestMonitor_MonitorWithPrefix(t *testing.T) { + t.Parallel() srv, client, _ := testServer(t, false, nil) defer srv.Shutdown() @@ -283,6 +288,7 @@ func TestMonitor_MonitorWithPrefix(t *testing.T) { } func TestMonitor_DumpAllocStatus(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) // Create an allocation and dump its status to the UI diff --git a/command/node_drain_test.go b/command/node_drain_test.go index f52d412e0..3bc551e17 100644 --- a/command/node_drain_test.go +++ b/command/node_drain_test.go @@ -8,10 +8,12 @@ import ( ) func TestNodeDrainCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &NodeDrainCommand{} } func TestNodeDrainCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/node_status_test.go b/command/node_status_test.go index e46237b44..9900f87cf 100644 --- a/command/node_status_test.go +++ b/command/node_status_test.go @@ -11,10 +11,12 @@ import ( ) func TestNodeStatusCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &NodeStatusCommand{} } func TestNodeStatusCommand_Self(t *testing.T) { + t.Parallel() // Start in dev mode so we get a node registration srv, client, url := testServer(t, true, func(c *agent.Config) { c.NodeName = "mynode" @@ -65,6 +67,7 @@ func TestNodeStatusCommand_Self(t *testing.T) { } func TestNodeStatusCommand_Run(t *testing.T) { + t.Parallel() // Start in dev mode so we get a node registration srv, client, url := testServer(t, true, func(c *agent.Config) { c.NodeName = "mynode" @@ -159,6 +162,7 @@ func TestNodeStatusCommand_Run(t *testing.T) { } func TestNodeStatusCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/operator_raft_list_test.go b/command/operator_raft_list_test.go index eb4bfc4f0..386c9f576 100644 --- a/command/operator_raft_list_test.go +++ b/command/operator_raft_list_test.go @@ -8,10 +8,12 @@ import ( ) func TestOperator_Raft_ListPeers_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &OperatorRaftListCommand{} } func TestOperator_Raft_ListPeers(t *testing.T) { + t.Parallel() s, _, addr := testServer(t, false, nil) defer s.Shutdown() diff --git a/command/operator_raft_remove_test.go b/command/operator_raft_remove_test.go index 0592b60fb..c38e94c86 100644 --- a/command/operator_raft_remove_test.go +++ b/command/operator_raft_remove_test.go @@ -8,10 +8,12 @@ import ( ) func TestOperator_Raft_RemovePeers_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &OperatorRaftRemoveCommand{} } func TestOperator_Raft_RemovePeer(t *testing.T) { + t.Parallel() s, _, addr := testServer(t, false, nil) defer s.Shutdown() diff --git a/command/operator_raft_test.go b/command/operator_raft_test.go index a4ce6269b..73934acff 100644 --- a/command/operator_raft_test.go +++ b/command/operator_raft_test.go @@ -7,5 +7,6 @@ import ( ) func TestOperator_Raft_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &OperatorRaftCommand{} } diff --git a/command/operator_test.go b/command/operator_test.go index 485f97544..7364a7cfa 100644 --- a/command/operator_test.go +++ b/command/operator_test.go @@ -7,5 +7,6 @@ import ( ) func TestOperator_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &OperatorCommand{} } diff --git a/command/plan_test.go b/command/plan_test.go index f8aed7e5b..97b42476e 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -12,10 +12,12 @@ import ( ) func TestPlanCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &RunCommand{} } func TestPlanCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &PlanCommand{Meta: Meta{Ui: ui}} @@ -109,6 +111,7 @@ job "job1" { } func TestPlanCommand_From_STDIN(t *testing.T) { + t.Parallel() stdinR, stdinW, err := os.Pipe() if err != nil { t.Fatalf("err: %s", err) @@ -151,6 +154,7 @@ job "job1" { } func TestPlanCommand_From_URL(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &PlanCommand{ Meta: Meta{Ui: ui}, diff --git a/command/run_test.go b/command/run_test.go index ee97de38e..715778d43 100644 --- a/command/run_test.go +++ b/command/run_test.go @@ -12,10 +12,12 @@ import ( ) func TestRunCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &RunCommand{} } func TestRunCommand_Output_Json(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &RunCommand{Meta: Meta{Ui: ui}} @@ -51,6 +53,7 @@ job "job1" { } func TestRunCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &RunCommand{Meta: Meta{Ui: ui}} @@ -154,6 +157,7 @@ job "job1" { } func TestRunCommand_From_STDIN(t *testing.T) { + t.Parallel() stdinR, stdinW, err := os.Pipe() if err != nil { t.Fatalf("err: %s", err) @@ -196,6 +200,7 @@ job "job1" { } func TestRunCommand_From_URL(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &RunCommand{ Meta: Meta{Ui: ui}, diff --git a/command/server_force_leave_test.go b/command/server_force_leave_test.go index fdf651162..9f449b900 100644 --- a/command/server_force_leave_test.go +++ b/command/server_force_leave_test.go @@ -7,5 +7,6 @@ import ( ) func TestServerForceLeaveCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &ServerForceLeaveCommand{} } diff --git a/command/server_join_test.go b/command/server_join_test.go index 289006c50..f3ec540be 100644 --- a/command/server_join_test.go +++ b/command/server_join_test.go @@ -7,5 +7,6 @@ import ( ) func TestServerJoinCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &ServerJoinCommand{} } diff --git a/command/server_members_test.go b/command/server_members_test.go index 42fa36537..7dd4e6232 100644 --- a/command/server_members_test.go +++ b/command/server_members_test.go @@ -8,10 +8,12 @@ import ( ) func TestServerMembersCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &ServerMembersCommand{} } func TestServerMembersCommand_Run(t *testing.T) { + t.Parallel() srv, client, url := testServer(t, false, nil) defer srv.Shutdown() @@ -43,6 +45,7 @@ func TestServerMembersCommand_Run(t *testing.T) { } func TestMembersCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &ServerMembersCommand{Meta: Meta{Ui: ui}} diff --git a/command/status_test.go b/command/status_test.go index 7220cc38c..f348761fe 100644 --- a/command/status_test.go +++ b/command/status_test.go @@ -9,10 +9,12 @@ import ( ) func TestStatusCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &StatusCommand{} } func TestStatusCommand_Run(t *testing.T) { + t.Parallel() srv, client, url := testServer(t, true, nil) defer srv.Shutdown() @@ -164,6 +166,7 @@ func TestStatusCommand_Run(t *testing.T) { } func TestStatusCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &StatusCommand{Meta: Meta{Ui: ui}} diff --git a/command/stop_test.go b/command/stop_test.go index 10783c516..6732927a4 100644 --- a/command/stop_test.go +++ b/command/stop_test.go @@ -8,10 +8,12 @@ import ( ) func TestStopCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &StopCommand{} } func TestStopCommand_Fails(t *testing.T) { + t.Parallel() srv, _, url := testServer(t, false, nil) defer srv.Shutdown() diff --git a/command/validate_test.go b/command/validate_test.go index 2caae2a30..20902068e 100644 --- a/command/validate_test.go +++ b/command/validate_test.go @@ -12,10 +12,12 @@ import ( ) func TestValidateCommand_Implements(t *testing.T) { + t.Parallel() var _ cli.Command = &ValidateCommand{} } func TestValidateCommand(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &ValidateCommand{Meta: Meta{Ui: ui}} @@ -56,6 +58,7 @@ job "job1" { } func TestValidateCommand_Fails(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &ValidateCommand{Meta: Meta{Ui: ui}} @@ -113,6 +116,7 @@ func TestValidateCommand_Fails(t *testing.T) { } func TestValidateCommand_From_STDIN(t *testing.T) { + t.Parallel() stdinR, stdinW, err := os.Pipe() if err != nil { t.Fatalf("err: %s", err) @@ -158,6 +162,7 @@ job "job1" { } func TestValidateCommand_From_URL(t *testing.T) { + t.Parallel() ui := new(cli.MockUi) cmd := &RunCommand{ Meta: Meta{Ui: ui}, diff --git a/command/version_test.go b/command/version_test.go index 2a645690f..07af3a006 100644 --- a/command/version_test.go +++ b/command/version_test.go @@ -7,5 +7,6 @@ import ( ) func TestVersionCommand_implements(t *testing.T) { + t.Parallel() var _ cli.Command = &VersionCommand{} } From 700147c90e14b19d2402c8d8edddc447a004da1d Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 20 Jul 2017 22:34:24 -0700 Subject: [PATCH 3/4] Speed up client startup --- client/fingerprint/env_aws.go | 34 +++++++++++++++++++++---------- client/fingerprint/env_gce.go | 19 +++++++++++++---- client/fingerprint/fingerprint.go | 4 ++++ command/agent/testagent.go | 7 +++++++ 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/client/fingerprint/env_aws.go b/client/fingerprint/env_aws.go index e8b872940..efd00efbb 100644 --- a/client/fingerprint/env_aws.go +++ b/client/fingerprint/env_aws.go @@ -16,9 +16,15 @@ import ( "github.com/hashicorp/nomad/nomad/structs" ) -// This is where the AWS metadata server normally resides. We hardcode the -// "instance" path as well since it's the only one we access here. -const DEFAULT_AWS_URL = "http://169.254.169.254/latest/meta-data/" +const ( + // This is where the AWS metadata server normally resides. We hardcode the + // "instance" path as well since it's the only one we access here. + DEFAULT_AWS_URL = "http://169.254.169.254/latest/meta-data/" + + // AwsMetadataTimeout is the timeout used when contacting the AWS metadata + // service + AwsMetadataTimeout = 2 * time.Second +) // map of instance type to approximate speed, in Mbits/s // Estimates from http://stackoverflow.com/a/35806587 @@ -44,16 +50,25 @@ var ec2InstanceSpeedMap = map[*regexp.Regexp]int{ // EnvAWSFingerprint is used to fingerprint AWS metadata type EnvAWSFingerprint struct { StaticFingerprinter - logger *log.Logger + timeout time.Duration + logger *log.Logger } // NewEnvAWSFingerprint is used to create a fingerprint from AWS metadata func NewEnvAWSFingerprint(logger *log.Logger) Fingerprint { - f := &EnvAWSFingerprint{logger: logger} + f := &EnvAWSFingerprint{ + logger: logger, + timeout: AwsMetadataTimeout, + } return f } func (f *EnvAWSFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) { + // Check if we should tighten the timeout + if cfg.ReadBoolDefault(TightenNetworkTimeoutsConfig, false) { + f.timeout = 1 * time.Millisecond + } + if !f.isAWS() { return false, nil } @@ -71,9 +86,8 @@ func (f *EnvAWSFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) metadataURL = DEFAULT_AWS_URL } - // assume 2 seconds is enough time for inside AWS network client := &http.Client{ - Timeout: 2 * time.Second, + Timeout: f.timeout, Transport: cleanhttp.DefaultTransport(), } @@ -174,9 +188,8 @@ func (f *EnvAWSFingerprint) isAWS() bool { metadataURL = DEFAULT_AWS_URL } - // assume 2 seconds is enough time for inside AWS network client := &http.Client{ - Timeout: 2 * time.Second, + Timeout: f.timeout, Transport: cleanhttp.DefaultTransport(), } @@ -217,9 +230,8 @@ func (f *EnvAWSFingerprint) linkSpeed() int { metadataURL = DEFAULT_AWS_URL } - // assume 2 seconds is enough time for inside AWS network client := &http.Client{ - Timeout: 2 * time.Second, + Timeout: f.timeout, Transport: cleanhttp.DefaultTransport(), } diff --git a/client/fingerprint/env_gce.go b/client/fingerprint/env_gce.go index 6f83ed224..506412e70 100644 --- a/client/fingerprint/env_gce.go +++ b/client/fingerprint/env_gce.go @@ -18,9 +18,15 @@ import ( "github.com/hashicorp/nomad/nomad/structs" ) -// This is where the GCE metadata server normally resides. We hardcode the -// "instance" path as well since it's the only one we access here. -const DEFAULT_GCE_URL = "http://169.254.169.254/computeMetadata/v1/instance/" +const ( + // This is where the GCE metadata server normally resides. We hardcode the + // "instance" path as well since it's the only one we access here. + DEFAULT_GCE_URL = "http://169.254.169.254/computeMetadata/v1/instance/" + + // GceMetadataTimeout is the timeout used when contacting the GCE metadata + // service + GceMetadataTimeout = 2 * time.Second +) type GCEMetadataNetworkInterface struct { AccessConfigs []struct { @@ -64,7 +70,7 @@ func NewEnvGCEFingerprint(logger *log.Logger) Fingerprint { // assume 2 seconds is enough time for inside GCE network client := &http.Client{ - Timeout: 2 * time.Second, + Timeout: GceMetadataTimeout, Transport: cleanhttp.DefaultTransport(), } @@ -126,6 +132,11 @@ func checkError(err error, logger *log.Logger, desc string) error { } func (f *EnvGCEFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) { + // Check if we should tighten the timeout + if cfg.ReadBoolDefault(TightenNetworkTimeoutsConfig, false) { + f.client.Timeout = 1 * time.Millisecond + } + if !f.isGCE() { return false, nil } diff --git a/client/fingerprint/fingerprint.go b/client/fingerprint/fingerprint.go index 9706dd416..2d6d483b6 100644 --- a/client/fingerprint/fingerprint.go +++ b/client/fingerprint/fingerprint.go @@ -13,6 +13,10 @@ import ( // EmptyDuration is to be used by fingerprinters that are not periodic. const ( EmptyDuration = time.Duration(0) + + // TightenNetworkTimeoutsConfig is a config key that can be used during + // tests to tighten the timeouts for fingerprinters that make network calls. + TightenNetworkTimeoutsConfig = "test.tighten_network_timeouts" ) func init() { diff --git a/command/agent/testagent.go b/command/agent/testagent.go index 3e3d65063..0cb04bc70 100644 --- a/command/agent/testagent.go +++ b/command/agent/testagent.go @@ -14,6 +14,7 @@ import ( "time" "github.com/hashicorp/nomad/api" + "github.com/hashicorp/nomad/client/fingerprint" "github.com/hashicorp/nomad/nomad" "github.com/hashicorp/nomad/nomad/structs" sconfig "github.com/hashicorp/nomad/nomad/structs/config" @@ -278,6 +279,12 @@ func (a *TestAgent) config() *Config { config.Bootstrap = true config.BootstrapExpect = 1 + // Tighten the fingerprinter timeouts + if conf.Client.Options == nil { + conf.Client.Options = make(map[string]string) + } + conf.Client.Options[fingerprint.TightenNetworkTimeoutsConfig] = "true" + if a.ConfigCallback != nil { a.ConfigCallback(conf) } From 9bf9f59f8fd1c3be650cd9dbac8582df117a8f09 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 20 Jul 2017 22:47:10 -0700 Subject: [PATCH 4/4] Force parallelism on travis --- scripts/travis.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/travis.sh b/scripts/travis.sh index bc181faae..0c4dc4cae 100755 --- a/scripts/travis.sh +++ b/scripts/travis.sh @@ -7,7 +7,7 @@ PING_LOOP_PID=$! trap "kill $PING_LOOP_PID" EXIT HUP INT QUIT TERM -make test +GOTEST_FLAGS="-parallel=8" make test TEST_OUTPUT=$? kill $PING_LOOP_PID