From 1a72934cd5cb369f75e7bd5714eb579a053b95a9 Mon Sep 17 00:00:00 2001 From: "Kai Qiang Wu(Kennan)" Date: Mon, 28 Mar 2016 08:43:41 +0000 Subject: [PATCH] Add label filter support for volume Since we added labels for volume, it is desired to have filter support label for volume Closes: #21416 Signed-off-by: Kai Qiang Wu(Kennan) --- daemon/list.go | 16 ++++++++++++ integration-cli/docker_cli_volume_test.go | 30 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/daemon/list.go b/daemon/list.go index 6d86cd3b48..0f1cf33a35 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -21,6 +21,7 @@ var acceptedVolumeFilterTags = map[string]bool{ "dangling": true, "name": true, "driver": true, + "label": true, } var acceptedPsFilterTags = map[string]bool{ @@ -587,6 +588,21 @@ func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) ( continue } } + if filter.Include("label") { + v, err := daemon.volumes.Get(vol.Name()) + if err != nil { + return nil, err + } + if v, ok := v.(interface { + Labels() map[string]string + }); ok { + labels := v.Labels() + + if !filter.MatchKVList("label", labels) { + continue + } + } + } retVols = append(retVols, vol) } danglingOnly := false diff --git a/integration-cli/docker_cli_volume_test.go b/integration-cli/docker_cli_volume_test.go index 9e62fc731b..bdbabb0644 100644 --- a/integration-cli/docker_cli_volume_test.go +++ b/integration-cli/docker_cli_volume_test.go @@ -344,3 +344,33 @@ func (s *DockerSuite) TestVolumeCliCreateLabelMultiple(c *check.C) { c.Assert(strings.TrimSpace(out), check.Equals, v) } } + +func (s *DockerSuite) TestVolumeCliLsFilterLabels(c *check.C) { + testVol1 := "testvolcreatelabel-1" + out, _, err := dockerCmdWithError("volume", "create", "--label", "foo=bar1", "--name", testVol1) + c.Assert(err, check.IsNil) + + testVol2 := "testvolcreatelabel-2" + out, _, err = dockerCmdWithError("volume", "create", "--label", "foo=bar2", "--name", testVol2) + c.Assert(err, check.IsNil) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo") + + // filter with label=key + c.Assert(out, checker.Contains, "testvolcreatelabel-1\n", check.Commentf("expected volume 'testvolcreatelabel-1' in output")) + c.Assert(out, checker.Contains, "testvolcreatelabel-2\n", check.Commentf("expected volume 'testvolcreatelabel-2' in output")) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=bar1") + + // filter with label=key=value + c.Assert(out, checker.Contains, "testvolcreatelabel-1\n", check.Commentf("expected volume 'testvolcreatelabel-1' in output")) + c.Assert(out, check.Not(checker.Contains), "testvolcreatelabel-2\n", check.Commentf("expected volume 'testvolcreatelabel-2 in output")) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=non-exist") + outArr := strings.Split(strings.TrimSpace(out), "\n") + c.Assert(len(outArr), check.Equals, 1, check.Commentf("\n%s", out)) + + out, _ = dockerCmd(c, "volume", "ls", "--filter", "label=foo=non-exist") + outArr = strings.Split(strings.TrimSpace(out), "\n") + c.Assert(len(outArr), check.Equals, 1, check.Commentf("\n%s", out)) +}