diff --git a/commands/active.go b/commands/active.go index 968a01964f..5941f83999 100644 --- a/commands/active.go +++ b/commands/active.go @@ -8,6 +8,7 @@ import ( "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/persist" + "github.com/docker/machine/libmachine/state" ) const ( @@ -15,7 +16,8 @@ const ( ) var ( - errNoActiveHost = errors.New("No active host found") + errNoActiveHost = errors.New("No active host found") + errActiveTimeout = errors.New("Error getting active host: timeout") ) func cmdActive(c CommandLine, api libmachine.API) error { @@ -42,10 +44,17 @@ func cmdActive(c CommandLine, api libmachine.API) error { } func activeHost(items []HostListItem) (HostListItem, error) { + timeout := false for _, item := range items { if item.ActiveHost || item.ActiveSwarm { return item, nil } + if item.State == state.Timeout { + timeout = true + } + } + if timeout { + return HostListItem{}, errActiveTimeout } return HostListItem{}, errNoActiveHost } diff --git a/commands/active_test.go b/commands/active_test.go index a35f9a1914..08a18e698e 100644 --- a/commands/active_test.go +++ b/commands/active_test.go @@ -3,6 +3,7 @@ package commands import ( "testing" + "github.com/docker/machine/libmachine/state" "github.com/stretchr/testify/assert" ) @@ -12,16 +13,19 @@ func TestCmdActiveNone(t *testing.T) { Name: "host1", ActiveHost: false, ActiveSwarm: false, + State: state.Running, }, { Name: "host2", ActiveHost: false, ActiveSwarm: false, + State: state.Running, }, { Name: "host3", ActiveHost: false, ActiveSwarm: false, + State: state.Running, }, } _, err := activeHost(hostListItems) @@ -34,16 +38,19 @@ func TestCmdActiveHost(t *testing.T) { Name: "host1", ActiveHost: false, ActiveSwarm: false, + State: state.Timeout, }, { Name: "host2", ActiveHost: true, ActiveSwarm: false, + State: state.Running, }, { Name: "host3", ActiveHost: false, ActiveSwarm: false, + State: state.Running, }, } active, err := activeHost(hostListItems) @@ -57,19 +64,47 @@ func TestCmdActiveSwarm(t *testing.T) { Name: "host1", ActiveHost: false, ActiveSwarm: false, + State: state.Running, }, { Name: "host2", ActiveHost: false, ActiveSwarm: false, + State: state.Running, }, { Name: "host3", ActiveHost: false, ActiveSwarm: true, + State: state.Running, }, } active, err := activeHost(hostListItems) assert.Equal(t, err, nil) assert.Equal(t, active.Name, "host3") } + +func TestCmdActiveTimeout(t *testing.T) { + hostListItems := []HostListItem{ + { + Name: "host1", + ActiveHost: false, + ActiveSwarm: false, + State: state.Running, + }, + { + Name: "host2", + ActiveHost: false, + ActiveSwarm: false, + State: state.Running, + }, + { + Name: "host3", + ActiveHost: false, + ActiveSwarm: false, + State: state.Timeout, + }, + } + _, err := activeHost(hostListItems) + assert.Equal(t, err, errActiveTimeout) +}