Browse code

Fix docker volume dangling filter

The docker volume ls -f dangling=true filter was
inverted; the filtered results actually returned all
non-dangling volumes.

This fixes the filter and adds some integration tests
to test the correct behavior.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2015/09/06 07:44:29
Showing 2 changed files
... ...
@@ -381,7 +381,7 @@ func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, error) {
381 381
 
382 382
 	volumes := daemon.volumes.List()
383 383
 	for _, v := range volumes {
384
-		if filterUsed && daemon.volumes.Count(v) == 0 {
384
+		if filterUsed && daemon.volumes.Count(v) > 0 {
385 385
 			continue
386 386
 		}
387 387
 		volumesOut = append(volumesOut, volumeToAPIType(v))
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"os/exec"
5 5
 	"strings"
6 6
 
7
+	"github.com/docker/docker/integration-cli/checker"
7 8
 	"github.com/go-check/check"
8 9
 )
9 10
 
... ...
@@ -54,6 +55,40 @@ func (s *DockerSuite) TestVolumeCliLs(c *check.C) {
54 54
 	c.Assert(strings.Contains(out, "test\n"), check.Equals, true)
55 55
 }
56 56
 
57
+func (s *DockerSuite) TestVolumeCliLsFilterDangling(c *check.C) {
58
+	testRequires(c, DaemonIsLinux)
59
+
60
+	dockerCmd(c, "volume", "create", "--name", "testnotinuse1")
61
+	dockerCmd(c, "volume", "create", "--name", "testisinuse1")
62
+	dockerCmd(c, "volume", "create", "--name", "testisinuse2")
63
+
64
+	// Make sure both "created" (but not started), and started
65
+	// containers are included in reference counting
66
+	dockerCmd(c, "run", "--name", "volume-test1", "-v", "testisinuse1:/foo", "busybox", "true")
67
+	dockerCmd(c, "create", "--name", "volume-test2", "-v", "testisinuse2:/foo", "busybox", "true")
68
+
69
+	out, _ := dockerCmd(c, "volume", "ls")
70
+
71
+	// No filter, all volumes should show
72
+	c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output"))
73
+	c.Assert(out, checker.Contains, "testisinuse1\n", check.Commentf("expected volume 'testisinuse1' in output"))
74
+	c.Assert(out, checker.Contains, "testisinuse2\n", check.Commentf("expected volume 'testisinuse2' in output"))
75
+
76
+	out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=false")
77
+
78
+	// Same as above, but expicitly disabling dangling
79
+	c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output"))
80
+	c.Assert(out, checker.Contains, "testisinuse1\n", check.Commentf("expected volume 'testisinuse1' in output"))
81
+	c.Assert(out, checker.Contains, "testisinuse2\n", check.Commentf("expected volume 'testisinuse2' in output"))
82
+
83
+	out, _ = dockerCmd(c, "volume", "ls", "--filter", "dangling=true")
84
+
85
+	// Filter "dangling" volumes; ony "dangling" (unused) volumes should be in the output
86
+	c.Assert(out, checker.Contains, "testnotinuse1\n", check.Commentf("expected volume 'testnotinuse1' in output"))
87
+	c.Assert(out, check.Not(checker.Contains), "testisinuse1\n", check.Commentf("volume 'testisinuse1' in output, but not expected"))
88
+	c.Assert(out, check.Not(checker.Contains), "testisinuse2\n", check.Commentf("volume 'testisinuse2' in output, but not expected"))
89
+}
90
+
57 91
 func (s *DockerSuite) TestVolumeCliRm(c *check.C) {
58 92
 	testRequires(c, DaemonIsLinux)
59 93
 	out, _ := dockerCmd(c, "volume", "create")