Browse code

Sort the attributes for events

This is add support for #19559
We tried sort it in client side, and it sort follow go
sort : sorts a slice of strings in increasing order.

Signed-off-by: Kai Qiang Wu(Kennan) <wkqwu@cn.ibm.com>

Kai Qiang Wu(Kennan) authored on 2016/01/27 11:48:14
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6 6
 	"io"
7
+	"sort"
7 8
 	"strings"
8 9
 	"time"
9 10
 
... ...
@@ -99,7 +100,13 @@ func printOutput(event eventtypes.Message, output io.Writer) {
99 99
 
100 100
 	if len(event.Actor.Attributes) > 0 {
101 101
 		var attrs []string
102
-		for k, v := range event.Actor.Attributes {
102
+		var keys []string
103
+		for k := range event.Actor.Attributes {
104
+			keys = append(keys, k)
105
+		}
106
+		sort.Strings(keys)
107
+		for _, k := range keys {
108
+			v := event.Actor.Attributes[k]
103 109
 			attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
104 110
 		}
105 111
 		fmt.Fprintf(output, " (%s)", strings.Join(attrs, ", "))
... ...
@@ -140,6 +140,33 @@ func (s *DockerSuite) TestEventsContainerEvents(c *check.C) {
140 140
 	c.Assert(containerEvents[4], checker.Equals, "destroy", check.Commentf(out))
141 141
 }
142 142
 
143
+func (s *DockerSuite) TestEventsContainerEventsAttrSort(c *check.C) {
144
+	since := daemonTime(c).Unix()
145
+	containerID, _ := dockerCmd(c, "run", "-d", "--name", "container-events-test", "busybox", "true")
146
+	containerID = strings.TrimSpace(containerID)
147
+
148
+	out, _ := dockerCmd(c, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
149
+	events := strings.Split(out, "\n")
150
+
151
+	nEvents := len(events)
152
+	c.Assert(nEvents, checker.GreaterOrEqualThan, 3) //Missing expected event
153
+	matchedEvents := 0
154
+	for _, event := range events {
155
+		matches := parseEventText(event)
156
+		if matches["id"] != containerID {
157
+			continue
158
+		}
159
+		if matches["eventType"] == "container" && matches["action"] == "create" {
160
+			matchedEvents++
161
+			c.Assert(out, checker.Contains, "(image=busybox, name=container-events-test)", check.Commentf("Event attributes not sorted"))
162
+		} else if matches["eventType"] == "container" && matches["action"] == "start" {
163
+			matchedEvents++
164
+			c.Assert(out, checker.Contains, "(image=busybox, name=container-events-test)", check.Commentf("Event attributes not sorted"))
165
+		}
166
+	}
167
+	c.Assert(matchedEvents, checker.Equals, 2)
168
+}
169
+
143 170
 func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *check.C) {
144 171
 	dockerCmd(c, "run", "--rm", "--name", "since-epoch-test", "busybox", "true")
145 172
 	timeBeginning := time.Unix(0, 0).Format(time.RFC3339Nano)