Signed-off-by: Patrik Erdes <patrik@erdes.se>
This commit is contained in:
Patrik Erdes 2016-02-18 08:51:34 +01:00
parent cf7885163c
commit e7c58ea7ae
2 changed files with 45 additions and 1 deletions

View File

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

View File

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