Skip to content

Commit 5af32bc

Browse files
committed
commands: make ls to not report saved hosts to be active when $DOCKER_HOST is not set
Signed-off-by: Soshi Katsuta <soshi.katsuta@gmail.com>
1 parent c4cd238 commit 5af32bc

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

commands/ls.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ func isActive(h *host.Host) (bool, error) {
342342

343343
dockerHost := os.Getenv("DOCKER_HOST")
344344

345-
notStopped := currentState != state.Stopped
345+
running := currentState == state.Running
346346
correctURL := url == dockerHost
347347

348-
isActive := notStopped && correctURL
348+
isActive := running && correctURL
349349

350350
return isActive, nil
351351
}

commands/ls_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,92 @@ func TestGetHostListItems(t *testing.T) {
376376
}
377377
}
378378
}
379+
380+
// issue #1908
381+
func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
382+
orgDockerHost := os.Getenv("DOCKER_HOST")
383+
defer func() {
384+
cleanup()
385+
386+
// revert DOCKER_HOST
387+
os.Setenv("DOCKER_HOST", orgDockerHost)
388+
}()
389+
390+
// unset DOCKER_HOST
391+
os.Unsetenv("DOCKER_HOST")
392+
393+
hostListItemsChan := make(chan HostListItem)
394+
395+
hosts := []*host.Host{
396+
{
397+
Name: "foo",
398+
DriverName: "fakedriver",
399+
Driver: &fakedriver.FakeDriver{
400+
MockState: state.Running,
401+
MockURL: "tcp://120.0.0.1:2376",
402+
},
403+
HostOptions: &host.HostOptions{
404+
SwarmOptions: &swarm.SwarmOptions{
405+
Master: false,
406+
Address: "",
407+
Discovery: "",
408+
},
409+
},
410+
},
411+
{
412+
Name: "bar",
413+
DriverName: "fakedriver",
414+
Driver: &fakedriver.FakeDriver{
415+
MockState: state.Stopped,
416+
},
417+
HostOptions: &host.HostOptions{
418+
SwarmOptions: &swarm.SwarmOptions{
419+
Master: false,
420+
Address: "",
421+
Discovery: "",
422+
},
423+
},
424+
},
425+
{
426+
Name: "baz",
427+
DriverName: "fakedriver",
428+
Driver: &fakedriver.FakeDriver{
429+
MockState: state.Saved,
430+
},
431+
HostOptions: &host.HostOptions{
432+
SwarmOptions: &swarm.SwarmOptions{
433+
Master: false,
434+
Address: "",
435+
Discovery: "",
436+
},
437+
},
438+
},
439+
}
440+
441+
expected := map[string]struct {
442+
state state.State
443+
active bool
444+
}{
445+
"foo": {state.Running, false},
446+
"bar": {state.Stopped, false},
447+
"baz": {state.Saved, false},
448+
}
449+
450+
items := []HostListItem{}
451+
for _, host := range hosts {
452+
go getHostState(host, hostListItemsChan)
453+
}
454+
455+
for i := 0; i < len(hosts); i++ {
456+
items = append(items, <-hostListItemsChan)
457+
}
458+
459+
for _, item := range items {
460+
if expected[item.Name].state != item.State {
461+
t.Fatal("Expected state did not match for item", item)
462+
}
463+
if expected[item.Name].active != item.Active {
464+
t.Fatal("Expected active flag did not match for item", item)
465+
}
466+
}
467+
}

drivers/fakedriver/fakedriver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type FakeDriver struct {
99
*drivers.BaseDriver
1010
MockState state.State
11+
MockURL string
1112
MockName string
1213
}
1314

@@ -20,7 +21,7 @@ func (d *FakeDriver) SetConfigFromFlags(flags drivers.DriverOptions) error {
2021
}
2122

2223
func (d *FakeDriver) GetURL() (string, error) {
23-
return "", nil
24+
return d.MockURL, nil
2425
}
2526

2627
func (d *FakeDriver) GetMachineName() string {

0 commit comments

Comments
 (0)