From 60ffd6c880024c5ab3ad96dc79b01dccd23dd766 Mon Sep 17 00:00:00 2001 From: "Kai Qiang Wu(Kennan)" Date: Wed, 17 Feb 2016 08:59:53 +0000 Subject: [PATCH] Make volume ls output order Fixes: #20384 Add order support for volume ls to make it easy to external users to consume. Signed-off-by: Kai Qiang Wu(Kennan) --- api/client/volume.go | 10 +++++++++ integration-cli/docker_cli_volume_test.go | 27 ++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/api/client/volume.go b/api/client/volume.go index 284e30c3cb..8bb37951ee 100644 --- a/api/client/volume.go +++ b/api/client/volume.go @@ -2,6 +2,7 @@ package client import ( "fmt" + "sort" "text/tabwriter" Cli "github.com/docker/docker/cli" @@ -72,6 +73,7 @@ func (cli *DockerCli) CmdVolumeLs(args ...string) error { fmt.Fprintf(w, "\n") } + sort.Sort(byVolumeName(volumes.Volumes)) for _, vol := range volumes.Volumes { if *quiet { fmt.Fprintln(w, vol.Name) @@ -83,6 +85,14 @@ func (cli *DockerCli) CmdVolumeLs(args ...string) error { return nil } +type byVolumeName []*types.Volume + +func (r byVolumeName) Len() int { return len(r) } +func (r byVolumeName) Swap(i, j int) { r[i], r[j] = r[j], r[i] } +func (r byVolumeName) Less(i, j int) bool { + return r[i].Name < r[j].Name +} + // CmdVolumeInspect displays low-level information on one or more volumes. // // Usage: docker volume inspect [OPTIONS] VOLUME [VOLUME...] diff --git a/integration-cli/docker_cli_volume_test.go b/integration-cli/docker_cli_volume_test.go index bd84becb66..4855524797 100644 --- a/integration-cli/docker_cli_volume_test.go +++ b/integration-cli/docker_cli_volume_test.go @@ -65,19 +65,34 @@ func (s *DockerSuite) TestVolumeCliInspectMulti(c *check.C) { func (s *DockerSuite) TestVolumeCliLs(c *check.C) { prefix, _ := getPrefixAndSlashFromDaemonPlatform() - out, _ := dockerCmd(c, "volume", "create") - id := strings.TrimSpace(out) + out, _ := dockerCmd(c, "volume", "create", "--name", "aaa") dockerCmd(c, "volume", "create", "--name", "test") - dockerCmd(c, "run", "-v", prefix+"/foo", "busybox", "ls", "/") + + dockerCmd(c, "volume", "create", "--name", "soo") + dockerCmd(c, "run", "-v", "soo:"+prefix+"/foo", "busybox", "ls", "/") out, _ = dockerCmd(c, "volume", "ls") outArr := strings.Split(strings.TrimSpace(out), "\n") c.Assert(len(outArr), check.Equals, 4, check.Commentf("\n%s", out)) - // Since there is no guarantee of ordering of volumes, we just make sure the names are in the output - c.Assert(strings.Contains(out, id+"\n"), check.Equals, true) - c.Assert(strings.Contains(out, "test\n"), check.Equals, true) + assertVolList(c, out, []string{"aaa", "soo", "test"}) +} + +// assertVolList checks volume retrieved with ls command +// equals to expected volume list +// note: out should be `volume ls [option]` result +func assertVolList(c *check.C, out string, expectVols []string) { + lines := strings.Split(out, "\n") + var volList []string + for _, line := range lines[1 : len(lines)-1] { + volFields := strings.Fields(line) + // wrap all volume name in volList + volList = append(volList, volFields[1]) + } + + // volume ls should contains all expected volumes + c.Assert(volList, checker.DeepEquals, expectVols) } func (s *DockerSuite) TestVolumeCliLsFilterDangling(c *check.C) {