mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #19761 from HackToday/enhancesortattr
Sort the attributes for events
This commit is contained in:
commit
1bc4c99372
2 changed files with 35 additions and 1 deletions
|
@ -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, ", "))
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue