Browse code

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) authored on 2016/02/17 17:59:53
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package client
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"sort"
5 6
 	"text/tabwriter"
6 7
 
7 8
 	Cli "github.com/docker/docker/cli"
... ...
@@ -72,6 +73,7 @@ func (cli *DockerCli) CmdVolumeLs(args ...string) error {
72 72
 		fmt.Fprintf(w, "\n")
73 73
 	}
74 74
 
75
+	sort.Sort(byVolumeName(volumes.Volumes))
75 76
 	for _, vol := range volumes.Volumes {
76 77
 		if *quiet {
77 78
 			fmt.Fprintln(w, vol.Name)
... ...
@@ -83,6 +85,14 @@ func (cli *DockerCli) CmdVolumeLs(args ...string) error {
83 83
 	return nil
84 84
 }
85 85
 
86
+type byVolumeName []*types.Volume
87
+
88
+func (r byVolumeName) Len() int      { return len(r) }
89
+func (r byVolumeName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
90
+func (r byVolumeName) Less(i, j int) bool {
91
+	return r[i].Name < r[j].Name
92
+}
93
+
86 94
 // CmdVolumeInspect displays low-level information on one or more volumes.
87 95
 //
88 96
 // Usage: docker volume inspect [OPTIONS] VOLUME [VOLUME...]
... ...
@@ -65,19 +65,34 @@ func (s *DockerSuite) TestVolumeCliInspectMulti(c *check.C) {
65 65
 
66 66
 func (s *DockerSuite) TestVolumeCliLs(c *check.C) {
67 67
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
68
-	out, _ := dockerCmd(c, "volume", "create")
69
-	id := strings.TrimSpace(out)
68
+	out, _ := dockerCmd(c, "volume", "create", "--name", "aaa")
70 69
 
71 70
 	dockerCmd(c, "volume", "create", "--name", "test")
72
-	dockerCmd(c, "run", "-v", prefix+"/foo", "busybox", "ls", "/")
71
+
72
+	dockerCmd(c, "volume", "create", "--name", "soo")
73
+	dockerCmd(c, "run", "-v", "soo:"+prefix+"/foo", "busybox", "ls", "/")
73 74
 
74 75
 	out, _ = dockerCmd(c, "volume", "ls")
75 76
 	outArr := strings.Split(strings.TrimSpace(out), "\n")
76 77
 	c.Assert(len(outArr), check.Equals, 4, check.Commentf("\n%s", out))
77 78
 
78
-	// Since there is no guarantee of ordering of volumes, we just make sure the names are in the output
79
-	c.Assert(strings.Contains(out, id+"\n"), check.Equals, true)
80
-	c.Assert(strings.Contains(out, "test\n"), check.Equals, true)
79
+	assertVolList(c, out, []string{"aaa", "soo", "test"})
80
+}
81
+
82
+// assertVolList checks volume retrieved with ls command
83
+// equals to expected volume list
84
+// note: out should be `volume ls [option]` result
85
+func assertVolList(c *check.C, out string, expectVols []string) {
86
+	lines := strings.Split(out, "\n")
87
+	var volList []string
88
+	for _, line := range lines[1 : len(lines)-1] {
89
+		volFields := strings.Fields(line)
90
+		// wrap all volume name in volList
91
+		volList = append(volList, volFields[1])
92
+	}
93
+
94
+	// volume ls should contains all expected volumes
95
+	c.Assert(volList, checker.DeepEquals, expectVols)
81 96
 }
82 97
 
83 98
 func (s *DockerSuite) TestVolumeCliLsFilterDangling(c *check.C) {