diff --git a/commands/active.go b/commands/active.go index 9d26b2fb70..587617c387 100644 --- a/commands/active.go +++ b/commands/active.go @@ -3,12 +3,9 @@ package commands import ( "errors" "fmt" - "os" - "strings" "github.com/docker/machine/libmachine/host" "github.com/docker/machine/libmachine/persist" - "github.com/docker/machine/libmachine/state" ) var ( @@ -50,17 +47,3 @@ func getActiveHost(store persist.Store) (*host.Host, error) { return nil, errors.New("Active host not found") } - -// IsActive provides a single function for determining if a host is active -// based on both the url and if the host is stopped. -func isActive(h *host.Host, currentState state.State, url string) (bool, error) { - dockerHost := os.Getenv("DOCKER_HOST") - - // TODO: hard-coding the swarm port is a travesty... - deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1) - if dockerHost == url || deSwarmedHost == url { - return currentState == state.Running, nil - } - - return false, nil -} diff --git a/commands/active_test.go b/commands/active_test.go deleted file mode 100644 index cdf1bd0f0c..0000000000 --- a/commands/active_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package commands - -import ( - "os" - "testing" - - "github.com/docker/machine/drivers/fakedriver" - "github.com/docker/machine/libmachine/host" - "github.com/docker/machine/libmachine/state" - "github.com/stretchr/testify/assert" -) - -var ( - h = host.Host{ - Name: "foo", - DriverName: "fakedriver", - Driver: &fakedriver.Driver{ - MockState: state.Running, - }, - } -) - -func TestIsActive(t *testing.T) { - cases := []struct { - dockerHost string - state state.State - expected bool - }{ - {"", state.Running, false}, - {"tcp://5.6.7.8:2376", state.Running, false}, - {"tcp://1.2.3.4:2376", state.Stopped, false}, - {"tcp://1.2.3.4:2376", state.Running, true}, - {"tcp://1.2.3.4:3376", state.Running, true}, - } - - for _, c := range cases { - os.Unsetenv("DOCKER_HOST") - if c.dockerHost != "" { - os.Setenv("DOCKER_HOST", c.dockerHost) - } - actual, err := isActive(&h, c.state, "tcp://1.2.3.4:2376") - assert.Equal(t, c.expected, actual, "IsActive(%s, \"%s\") should return %v, but didn't", c.state, c.dockerHost, c.expected) - assert.NoError(t, err) - } -} diff --git a/commands/commands.go b/commands/commands.go index 0eb4ae09b1..4ddffc71a5 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "path/filepath" - "runtime" "strings" "github.com/docker/machine/cli" @@ -447,22 +446,21 @@ func runActionWithContext(actionName string, c CommandLine) error { // sigh. func getCertPathInfoFromContext(c CommandLine) cert.PathInfo { caCertPath := c.GlobalString("tls-ca-cert") - caKeyPath := c.GlobalString("tls-ca-key") - clientCertPath := c.GlobalString("tls-client-cert") - clientKeyPath := c.GlobalString("tls-client-key") - if caCertPath == "" { caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem") } + caKeyPath := c.GlobalString("tls-ca-key") if caKeyPath == "" { caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem") } + clientCertPath := c.GlobalString("tls-client-cert") if clientCertPath == "" { clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem") } + clientKeyPath := c.GlobalString("tls-client-key") if clientKeyPath == "" { clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem") } @@ -474,20 +472,3 @@ func getCertPathInfoFromContext(c CommandLine) cert.PathInfo { ClientKeyPath: clientKeyPath, } } - -func detectShell() (string, error) { - // attempt to get the SHELL env var - shell := filepath.Base(os.Getenv("SHELL")) - - log.Debugf("shell: %s", shell) - if shell == "" { - // check for windows env and not bash (i.e. msysgit, etc) - if runtime.GOOS == "windows" { - log.Printf("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n") - } - - return "", ErrUnknownShell - } - - return shell, nil -} diff --git a/commands/commands_test.go b/commands/commands_test.go index 517cbc2dff..6df17ef6db 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -2,6 +2,7 @@ package commands import ( "errors" + "os" "strings" "testing" @@ -12,6 +13,18 @@ import ( "github.com/stretchr/testify/assert" ) +var ( + stdout *os.File +) + +func init() { + stdout = os.Stdout +} + +func cleanup() { + os.Stdout = stdout +} + func TestRunActionForeachMachine(t *testing.T) { // Assume a bunch of machines in randomly started or // stopped states. diff --git a/commands/env.go b/commands/env.go index 5c8c182ef3..aaac100b34 100644 --- a/commands/env.go +++ b/commands/env.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "strings" "text/template" @@ -208,3 +209,20 @@ func generateUsageHint(userShell string, args []string) string { return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd) } + +func detectShell() (string, error) { + // attempt to get the SHELL env var + shell := filepath.Base(os.Getenv("SHELL")) + + log.Debugf("shell: %s", shell) + if shell == "" { + // check for windows env and not bash (i.e. msysgit, etc) + if runtime.GOOS == "windows" { + log.Printf("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n") + } + + return "", ErrUnknownShell + } + + return shell, nil +} diff --git a/commands/ls.go b/commands/ls.go index a7a5cca924..33a492401c 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -261,7 +261,7 @@ func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) { close(stateCh) close(urlCh) - active, err := isActive(h, currentState, url) + active, err := isActive(currentState, url) if err != nil { log.Errorf("error determining if host is active for host %s: %s", h.Name, err) @@ -329,3 +329,17 @@ func sortHostListItemsByName(items []HostListItem) { items[i] = m[v] } } + +// IsActive provides a single function for determining if a host is active +// based on both the url and if the host is stopped. +func isActive(currentState state.State, url string) (bool, error) { + if currentState != state.Running { + return false, nil + } + + // TODO: hard-coding the swarm port is a travesty... + dockerHost := os.Getenv("DOCKER_HOST") + deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1) + + return dockerHost == url || deSwarmedHost == url, nil +} diff --git a/commands/ls_test.go b/commands/ls_test.go index 336dfd05f8..eba3ac24d8 100644 --- a/commands/ls_test.go +++ b/commands/ls_test.go @@ -13,20 +13,6 @@ import ( "github.com/stretchr/testify/assert" ) -var ( - hostTestStorePath string - stdout *os.File -) - -func init() { - stdout = os.Stdout -} - -func cleanup() { - os.Stdout = stdout - os.RemoveAll(hostTestStorePath) -} - func TestParseFiltersErrorsGivenInvalidFilter(t *testing.T) { _, err := parseFilters([]string{"foo=bar"}) assert.EqualError(t, err, "Unsupported filter key 'foo'") @@ -306,8 +292,6 @@ func captureStdout() (chan string, *os.File) { } func TestGetHostListItems(t *testing.T) { - defer cleanup() - hostListItemsChan := make(chan HostListItem) hosts := []*host.Host{ @@ -381,8 +365,6 @@ func TestGetHostListItems(t *testing.T) { func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) { orgDockerHost := os.Getenv("DOCKER_HOST") defer func() { - cleanup() - // revert DOCKER_HOST os.Setenv("DOCKER_HOST", orgDockerHost) }() @@ -465,3 +447,29 @@ func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) { } } } + +func TestIsActive(t *testing.T) { + cases := []struct { + dockerHost string + state state.State + expected bool + }{ + {"", state.Running, false}, + {"tcp://5.6.7.8:2376", state.Running, false}, + {"tcp://1.2.3.4:2376", state.Stopped, false}, + {"tcp://1.2.3.4:2376", state.Running, true}, + {"tcp://1.2.3.4:3376", state.Running, true}, + } + + for _, c := range cases { + os.Unsetenv("DOCKER_HOST") + if c.dockerHost != "" { + os.Setenv("DOCKER_HOST", c.dockerHost) + } + + actual, err := isActive(c.state, "tcp://1.2.3.4:2376") + + assert.Equal(t, c.expected, actual, "IsActive(%s, \"%s\") should return %v, but didn't", c.state, c.dockerHost, c.expected) + assert.NoError(t, err) + } +}