Selaa lähdekoodia

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) 9 vuotta sitten
vanhempi
commit
746f6af9aa
2 muutettua tiedostoa jossa 35 lisäystä ja 1 poistoa
  1. 8 1
      api/client/events.go
  2. 27 0
      integration-cli/docker_cli_events_test.go

+ 8 - 1
api/client/events.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"sort"
 	"strings"
 	"time"
 
@@ -99,7 +100,13 @@ func printOutput(event eventtypes.Message, output io.Writer) {
 
 	if len(event.Actor.Attributes) > 0 {
 		var attrs []string
-		for k, v := range event.Actor.Attributes {
+		var keys []string
+		for k := range event.Actor.Attributes {
+			keys = append(keys, k)
+		}
+		sort.Strings(keys)
+		for _, k := range keys {
+			v := event.Actor.Attributes[k]
 			attrs = append(attrs, fmt.Sprintf("%s=%s", k, v))
 		}
 		fmt.Fprintf(output, " (%s)", strings.Join(attrs, ", "))

+ 27 - 0
integration-cli/docker_cli_events_test.go

@@ -140,6 +140,33 @@ func (s *DockerSuite) TestEventsContainerEvents(c *check.C) {
 	c.Assert(containerEvents[4], checker.Equals, "destroy", check.Commentf(out))
 }
 
+func (s *DockerSuite) TestEventsContainerEventsAttrSort(c *check.C) {
+	since := daemonTime(c).Unix()
+	containerID, _ := dockerCmd(c, "run", "-d", "--name", "container-events-test", "busybox", "true")
+	containerID = strings.TrimSpace(containerID)
+
+	out, _ := dockerCmd(c, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
+	events := strings.Split(out, "\n")
+
+	nEvents := len(events)
+	c.Assert(nEvents, checker.GreaterOrEqualThan, 3) //Missing expected event
+	matchedEvents := 0
+	for _, event := range events {
+		matches := parseEventText(event)
+		if matches["id"] != containerID {
+			continue
+		}
+		if matches["eventType"] == "container" && matches["action"] == "create" {
+			matchedEvents++
+			c.Assert(out, checker.Contains, "(image=busybox, name=container-events-test)", check.Commentf("Event attributes not sorted"))
+		} else if matches["eventType"] == "container" && matches["action"] == "start" {
+			matchedEvents++
+			c.Assert(out, checker.Contains, "(image=busybox, name=container-events-test)", check.Commentf("Event attributes not sorted"))
+		}
+	}
+	c.Assert(matchedEvents, checker.Equals, 2)
+}
+
 func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *check.C) {
 	dockerCmd(c, "run", "--rm", "--name", "since-epoch-test", "busybox", "true")
 	timeBeginning := time.Unix(0, 0).Format(time.RFC3339Nano)