From 5af32bcdd35c1ef8b70823e916e9fb68d134f64c Mon Sep 17 00:00:00 2001 From: Soshi Katsuta Date: Sun, 27 Sep 2015 23:01:45 +0900 Subject: [PATCH] commands: make `ls` to not report saved hosts to be active when $DOCKER_HOST is not set Signed-off-by: Soshi Katsuta --- commands/ls.go | 4 +- commands/ls_test.go | 89 ++++++++++++++++++++++++++++++++ drivers/fakedriver/fakedriver.go | 3 +- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/commands/ls.go b/commands/ls.go index 372559c017..27e97a391d 100644 --- a/commands/ls.go +++ b/commands/ls.go @@ -342,10 +342,10 @@ func isActive(h *host.Host) (bool, error) { dockerHost := os.Getenv("DOCKER_HOST") - notStopped := currentState != state.Stopped + running := currentState == state.Running correctURL := url == dockerHost - isActive := notStopped && correctURL + isActive := running && correctURL return isActive, nil } diff --git a/commands/ls_test.go b/commands/ls_test.go index 35e27f2f5e..b84c0eabcd 100644 --- a/commands/ls_test.go +++ b/commands/ls_test.go @@ -376,3 +376,92 @@ func TestGetHostListItems(t *testing.T) { } } } + +// issue #1908 +func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) { + orgDockerHost := os.Getenv("DOCKER_HOST") + defer func() { + cleanup() + + // revert DOCKER_HOST + os.Setenv("DOCKER_HOST", orgDockerHost) + }() + + // unset DOCKER_HOST + os.Unsetenv("DOCKER_HOST") + + hostListItemsChan := make(chan HostListItem) + + hosts := []*host.Host{ + { + Name: "foo", + DriverName: "fakedriver", + Driver: &fakedriver.FakeDriver{ + MockState: state.Running, + MockURL: "tcp://120.0.0.1:2376", + }, + HostOptions: &host.HostOptions{ + SwarmOptions: &swarm.SwarmOptions{ + Master: false, + Address: "", + Discovery: "", + }, + }, + }, + { + Name: "bar", + DriverName: "fakedriver", + Driver: &fakedriver.FakeDriver{ + MockState: state.Stopped, + }, + HostOptions: &host.HostOptions{ + SwarmOptions: &swarm.SwarmOptions{ + Master: false, + Address: "", + Discovery: "", + }, + }, + }, + { + Name: "baz", + DriverName: "fakedriver", + Driver: &fakedriver.FakeDriver{ + MockState: state.Saved, + }, + HostOptions: &host.HostOptions{ + SwarmOptions: &swarm.SwarmOptions{ + Master: false, + Address: "", + Discovery: "", + }, + }, + }, + } + + expected := map[string]struct { + state state.State + active bool + }{ + "foo": {state.Running, false}, + "bar": {state.Stopped, false}, + "baz": {state.Saved, false}, + } + + items := []HostListItem{} + for _, host := range hosts { + go getHostState(host, hostListItemsChan) + } + + for i := 0; i < len(hosts); i++ { + items = append(items, <-hostListItemsChan) + } + + for _, item := range items { + if expected[item.Name].state != item.State { + t.Fatal("Expected state did not match for item", item) + } + if expected[item.Name].active != item.Active { + t.Fatal("Expected active flag did not match for item", item) + } + } +} diff --git a/drivers/fakedriver/fakedriver.go b/drivers/fakedriver/fakedriver.go index eb1bf07a6a..afd731d0dc 100644 --- a/drivers/fakedriver/fakedriver.go +++ b/drivers/fakedriver/fakedriver.go @@ -8,6 +8,7 @@ import ( type FakeDriver struct { *drivers.BaseDriver MockState state.State + MockURL string MockName string } @@ -20,7 +21,7 @@ func (d *FakeDriver) SetConfigFromFlags(flags drivers.DriverOptions) error { } func (d *FakeDriver) GetURL() (string, error) { - return "", nil + return d.MockURL, nil } func (d *FakeDriver) GetMachineName() string {