Compare event nanoseconds properly to filter since a specific date.

Signed-off-by: David Calavera <david.calavera@gmail.com>
Upstream-commit: a9f2006f105340890787799a5686b9760ab6be42
Component: engine
This commit is contained in:
David Calavera
2016-03-08 17:07:58 -05:00
parent a41eec344b
commit 775d953faa
5 changed files with 164 additions and 55 deletions
@@ -12,6 +12,7 @@ import (
"sync"
"time"
"github.com/docker/docker/daemon/events/testutils"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
@@ -152,7 +153,7 @@ func (s *DockerSuite) TestEventsContainerEventsAttrSort(c *check.C) {
c.Assert(nEvents, checker.GreaterOrEqualThan, 3) //Missing expected event
matchedEvents := 0
for _, event := range events {
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
if matches["id"] != containerID {
continue
}
@@ -201,7 +202,7 @@ func (s *DockerSuite) TestEventsImageTag(c *check.C) {
c.Assert(events, checker.HasLen, 1, check.Commentf("was expecting 1 event. out=%s", out))
event := strings.TrimSpace(events[0])
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
c.Assert(matchEventID(matches, image), checker.True, check.Commentf("matches: %v\nout:\n%s", matches, out))
c.Assert(matches["action"], checker.Equals, "tag")
}
@@ -220,7 +221,7 @@ func (s *DockerSuite) TestEventsImagePull(c *check.C) {
events := strings.Split(strings.TrimSpace(out), "\n")
event := strings.TrimSpace(events[len(events)-1])
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
c.Assert(matches["id"], checker.Equals, "hello-world:latest")
c.Assert(matches["action"], checker.Equals, "pull")
@@ -245,7 +246,7 @@ func (s *DockerSuite) TestEventsImageImport(c *check.C) {
out, _ = dockerCmd(c, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", "event=import")
events := strings.Split(strings.TrimSpace(out), "\n")
c.Assert(events, checker.HasLen, 1)
matches := parseEventText(events[0])
matches := eventstestutils.ScanMap(events[0])
c.Assert(matches["id"], checker.Equals, imageRef, check.Commentf("matches: %v\nout:\n%s\n", matches, out))
c.Assert(matches["action"], checker.Equals, "import", check.Commentf("matches: %v\nout:\n%s\n", matches, out))
}
@@ -370,7 +371,7 @@ func (s *DockerSuite) TestEventsFilterContainer(c *check.C) {
return fmt.Errorf("expected 4 events, got %v", events)
}
for _, event := range events {
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
if !matchEventID(matches, id) {
return fmt.Errorf("expected event for container id %s: %s - parsed container id: %s", id, event, matches["id"])
}
@@ -3,7 +3,6 @@ package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"regexp"
@@ -11,22 +10,11 @@ import (
"strings"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/daemon/events/testutils"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
var (
reTimestamp = `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{9}(:?(:?(:?-|\+)\d{2}:\d{2})|Z)`
reEventType = `(?P<eventType>\w+)`
reAction = `(?P<action>\w+)`
reID = `(?P<id>[^\s]+)`
reAttributes = `(\s\((?P<attributes>[^\)]+)\))?`
reString = fmt.Sprintf(`\A%s\s%s\s%s\s%s%s\z`, reTimestamp, reEventType, reAction, reID, reAttributes)
// eventCliRegexp is a regular expression that matches all possible event outputs in the cli
eventCliRegexp = regexp.MustCompile(reString)
)
// eventMatcher is a function that tries to match an event input.
// It returns true if the event matches and a map with
// a set of key/value to identify the match.
@@ -131,7 +119,7 @@ func (e *eventObserver) CheckEventError(c *check.C, id, event string, match even
// It returns an empty map and false if there is no match.
func matchEventLine(id, eventType string, actions map[string]chan bool) eventMatcher {
return func(text string) (map[string]string, bool) {
matches := parseEventText(text)
matches := eventstestutils.ScanMap(text)
if len(matches) == 0 {
return matches, false
}
@@ -154,26 +142,10 @@ func processEventMatch(actions map[string]chan bool) eventMatchProcessor {
}
}
// parseEventText parses a line of events coming from the cli and returns
// the matchers in a map.
func parseEventText(text string) map[string]string {
matches := eventCliRegexp.FindAllStringSubmatch(text, -1)
md := map[string]string{}
if len(matches) == 0 {
return md
}
names := eventCliRegexp.SubexpNames()
for i, n := range matches[0] {
md[names[i]] = n
}
return md
}
// parseEventAction parses an event text and returns the action.
// It fails if the text is not in the event format.
func parseEventAction(c *check.C, text string) string {
matches := parseEventText(text)
matches := eventstestutils.ScanMap(text)
return matches["action"]
}
@@ -182,7 +154,7 @@ func parseEventAction(c *check.C, text string) string {
func eventActionsByIDAndType(c *check.C, events []string, id, eventType string) []string {
var filtered []string
for _, event := range events {
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
c.Assert(matches, checker.Not(checker.IsNil))
if matchIDAndEventType(matches, id, eventType) {
filtered = append(filtered, matches["action"])
@@ -214,7 +186,7 @@ func matchEventID(matches map[string]string, id string) bool {
func parseEvents(c *check.C, out, match string) {
events := strings.Split(strings.TrimSpace(out), "\n")
for _, event := range events {
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
matched, err := regexp.MatchString(match, matches["action"])
c.Assert(err, checker.IsNil)
c.Assert(matched, checker.True, check.Commentf("Matcher: %s did not match %s", match, matches["action"]))
@@ -224,7 +196,7 @@ func parseEvents(c *check.C, out, match string) {
func parseEventsWithID(c *check.C, out, match, id string) {
events := strings.Split(strings.TrimSpace(out), "\n")
for _, event := range events {
matches := parseEventText(event)
matches := eventstestutils.ScanMap(event)
c.Assert(matchEventID(matches, id), checker.True)
matched, err := regexp.MatchString(match, matches["action"])