Merge pull request #2881 from hashicorp/f-parallel-command

Parallel nomad/command pkg tests
This commit is contained in:
Alex Dadgar
2017-07-21 11:04:19 -07:00
committed by GitHub
48 changed files with 213 additions and 100 deletions

View File

@@ -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(),
}

View File

@@ -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
}

View File

@@ -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() {

View File

@@ -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

View File

@@ -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"
@@ -102,7 +103,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 +205,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 +213,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 +252,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"
@@ -273,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)
}

View File

@@ -8,12 +8,14 @@ import (
)
func TestAgentInfoCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &AgentInfoCommand{}
}
func TestAgentInfoCommand_Run(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &AgentInfoCommand{Meta: Meta{Ui: ui}}
@@ -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}}

View File

@@ -11,12 +11,14 @@ import (
)
func TestAllocStatusCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &AllocStatusCommand{}
}
func TestAllocStatusCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
@@ -76,10 +78,9 @@ 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()
t.Parallel()
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
// Wait for a node to be ready
testutil.WaitForResult(func() (bool, error) {

View File

@@ -8,8 +8,9 @@ import (
)
func TestAgentCheckCommand_ServerHealth(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &AgentCheckCommand{Meta: Meta{Ui: ui}}

View File

@@ -4,20 +4,21 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/testutil"
"github.com/hashicorp/nomad/command/agent"
"github.com/mitchellh/cli"
)
func TestClientConfigCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &ClientConfigCommand{}
}
func TestClientConfigCommand_UpdateServers(t *testing.T) {
srv, _, url := testServer(t, func(c *testutil.TestServerConfig) {
c.Client.Enabled = true
t.Parallel()
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}}
@@ -53,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}}

View File

@@ -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 {

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -8,12 +8,14 @@ import (
)
func TestEvalStatusCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &EvalStatusCommand{}
}
func TestEvalStatusCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &EvalStatusCommand{Meta: Meta{Ui: ui}}

View File

@@ -8,12 +8,14 @@ import (
)
func TestFSCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &FSCommand{}
}
func TestFSCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &FSCommand{Meta: Meta{Ui: ui}}

View File

@@ -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,8 +45,9 @@ func TestHelpers_FormatList(t *testing.T) {
}
func TestHelpers_NodeID(t *testing.T) {
srv, _, _ := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, _ := testServer(t, false, nil)
defer srv.Shutdown()
meta := Meta{Ui: new(cli.MockUi)}
client, err := meta.Client()
@@ -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)
})

View File

@@ -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.

View File

@@ -8,12 +8,14 @@ import (
)
func TestInspectCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &InspectCommand{}
}
func TestInspectCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &InspectCommand{Meta: Meta{Ui: ui}}

View File

@@ -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)
}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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}}

View File

@@ -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)

View File

@@ -8,12 +8,14 @@ import (
)
func TestLogsCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &LogsCommand{}
}
func TestLogsCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &LogsCommand{Meta: Meta{Ui: ui}}

View File

@@ -8,6 +8,7 @@ import (
)
func TestMeta_FlagSet(t *testing.T) {
t.Parallel()
cases := []struct {
Flags FlagSetFlags
Expected []string

View File

@@ -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,8 +172,9 @@ func TestMonitor_Update_AllocModification(t *testing.T) {
}
func TestMonitor_Monitor(t *testing.T) {
srv, client, _ := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, client, _ := testServer(t, false, nil)
defer srv.Shutdown()
// Create the monitor
ui := new(cli.MockUi)
@@ -215,8 +219,9 @@ func TestMonitor_Monitor(t *testing.T) {
}
func TestMonitor_MonitorWithPrefix(t *testing.T) {
srv, client, _ := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, client, _ := testServer(t, false, nil)
defer srv.Shutdown()
// Create the monitor
ui := new(cli.MockUi)
@@ -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

View File

@@ -8,12 +8,14 @@ import (
)
func TestNodeDrainCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &NodeDrainCommand{}
}
func TestNodeDrainCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &NodeDrainCommand{Meta: Meta{Ui: ui}}

View File

@@ -5,21 +5,23 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
)
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, 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}}
@@ -65,12 +67,12 @@ 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, 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 +162,9 @@ func TestNodeStatusCommand_Run(t *testing.T) {
}
func TestNodeStatusCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}

View File

@@ -8,12 +8,14 @@ import (
)
func TestOperator_Raft_ListPeers_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &OperatorRaftListCommand{}
}
func TestOperator_Raft_ListPeers(t *testing.T) {
s, _, addr := testServer(t, nil)
defer s.Stop()
t.Parallel()
s, _, addr := testServer(t, false, nil)
defer s.Shutdown()
ui := new(cli.MockUi)
c := &OperatorRaftListCommand{Meta: Meta{Ui: ui}}

View File

@@ -8,12 +8,14 @@ import (
)
func TestOperator_Raft_RemovePeers_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &OperatorRaftRemoveCommand{}
}
func TestOperator_Raft_RemovePeer(t *testing.T) {
s, _, addr := testServer(t, nil)
defer s.Stop()
t.Parallel()
s, _, addr := testServer(t, false, nil)
defer s.Shutdown()
ui := new(cli.MockUi)
c := &OperatorRaftRemoveCommand{Meta: Meta{Ui: ui}}

View File

@@ -7,5 +7,6 @@ import (
)
func TestOperator_Raft_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &OperatorRaftCommand{}
}

View File

@@ -7,5 +7,6 @@ import (
)
func TestOperator_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &OperatorCommand{}
}

View File

@@ -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},

View File

@@ -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},

View File

@@ -7,5 +7,6 @@ import (
)
func TestServerForceLeaveCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &ServerForceLeaveCommand{}
}

View File

@@ -7,5 +7,6 @@ import (
)
func TestServerJoinCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &ServerJoinCommand{}
}

View File

@@ -8,12 +8,14 @@ import (
)
func TestServerMembersCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &ServerMembersCommand{}
}
func TestServerMembersCommand_Run(t *testing.T) {
srv, client, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, client, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &ServerMembersCommand{Meta: Meta{Ui: ui}}
@@ -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}}

View File

@@ -5,19 +5,18 @@ import (
"testing"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
)
func TestStatusCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &StatusCommand{}
}
func TestStatusCommand_Run(t *testing.T) {
srv, client, url := testServer(t, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
defer srv.Stop()
t.Parallel()
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &StatusCommand{Meta: Meta{Ui: ui}}
@@ -167,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}}

View File

@@ -8,12 +8,14 @@ import (
)
func TestStopCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &StopCommand{}
}
func TestStopCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &StopCommand{Meta: Meta{Ui: ui}}

View File

@@ -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 {

View File

@@ -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},

View File

@@ -7,5 +7,6 @@ import (
)
func TestVersionCommand_implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &VersionCommand{}
}

View File

@@ -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