Forráskód Böngészése

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) <wkqwu@cn.ibm.com>
Kai Qiang Wu(Kennan) 9 éve
szülő
commit
60ffd6c880
2 módosított fájl, 31 hozzáadás és 6 törlés
  1. 10 0
      api/client/volume.go
  2. 21 6
      integration-cli/docker_cli_volume_test.go

+ 10 - 0
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...]

+ 21 - 6
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) {