2015-12-21 20:34:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
2015-12-22 00:45:31 +00:00
|
|
|
"regexp"
|
2015-12-21 20:34:08 +00:00
|
|
|
"strconv"
|
2015-12-21 22:55:23 +00:00
|
|
|
"strings"
|
2015-12-21 20:34:08 +00:00
|
|
|
|
2015-12-28 20:15:34 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-12 07:34:19 +00:00
|
|
|
eventstestutils "github.com/docker/docker/daemon/events/testutils"
|
2015-12-21 22:55:23 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-12-21 20:34:08 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
// eventMatcher is a function that tries to match an event input.
|
2016-01-20 22:36:39 +00:00
|
|
|
// It returns true if the event matches and a map with
|
|
|
|
// a set of key/value to identify the match.
|
|
|
|
type eventMatcher func(text string) (map[string]string, bool)
|
|
|
|
|
|
|
|
// eventMatchProcessor is a function to handle an event match.
|
|
|
|
// It receives a map of key/value with the information extracted in a match.
|
|
|
|
type eventMatchProcessor func(matches map[string]string)
|
2015-12-21 20:34:08 +00:00
|
|
|
|
|
|
|
// eventObserver runs an events commands and observes its output.
|
|
|
|
type eventObserver struct {
|
2015-12-28 20:15:34 +00:00
|
|
|
buffer *bytes.Buffer
|
|
|
|
command *exec.Cmd
|
|
|
|
scanner *bufio.Scanner
|
|
|
|
startTime string
|
|
|
|
disconnectionError error
|
2015-12-21 20:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newEventObserver creates the observer and initializes the command
|
|
|
|
// without running it. Users must call `eventObserver.Start` to start the command.
|
|
|
|
func newEventObserver(c *check.C, args ...string) (*eventObserver, error) {
|
|
|
|
since := daemonTime(c).Unix()
|
2015-12-21 22:55:23 +00:00
|
|
|
return newEventObserverWithBacklog(c, since, args...)
|
|
|
|
}
|
2015-12-21 20:34:08 +00:00
|
|
|
|
2015-12-21 22:55:23 +00:00
|
|
|
// newEventObserverWithBacklog creates a new observer changing the start time of the backlog to return.
|
|
|
|
func newEventObserverWithBacklog(c *check.C, since int64, args ...string) (*eventObserver, error) {
|
2015-12-28 20:15:34 +00:00
|
|
|
startTime := strconv.FormatInt(since, 10)
|
|
|
|
cmdArgs := []string{"events", "--since", startTime}
|
2015-12-21 20:34:08 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
cmdArgs = append(cmdArgs, args...)
|
|
|
|
}
|
|
|
|
eventsCmd := exec.Command(dockerBinary, cmdArgs...)
|
|
|
|
stdout, err := eventsCmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &eventObserver{
|
2015-12-28 20:15:34 +00:00
|
|
|
buffer: new(bytes.Buffer),
|
|
|
|
command: eventsCmd,
|
|
|
|
scanner: bufio.NewScanner(stdout),
|
|
|
|
startTime: startTime,
|
2015-12-21 20:34:08 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the events command.
|
|
|
|
func (e *eventObserver) Start() error {
|
|
|
|
return e.command.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the events command.
|
|
|
|
func (e *eventObserver) Stop() {
|
|
|
|
e.command.Process.Kill()
|
2015-12-21 22:55:23 +00:00
|
|
|
e.command.Process.Release()
|
2015-12-21 20:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match tries to match the events output with a given matcher.
|
2016-01-20 22:36:39 +00:00
|
|
|
func (e *eventObserver) Match(match eventMatcher, process eventMatchProcessor) {
|
2015-12-28 20:15:34 +00:00
|
|
|
for e.scanner.Scan() {
|
|
|
|
text := e.scanner.Text()
|
2015-12-21 20:34:08 +00:00
|
|
|
e.buffer.WriteString(text)
|
|
|
|
e.buffer.WriteString("\n")
|
|
|
|
|
2016-01-20 22:36:39 +00:00
|
|
|
if matches, ok := match(text); ok {
|
|
|
|
process(matches)
|
|
|
|
}
|
2015-12-21 20:34:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:15:34 +00:00
|
|
|
err := e.scanner.Err()
|
|
|
|
if err == nil {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
|
2016-06-11 22:37:36 +00:00
|
|
|
logrus.Debugf("EventObserver scanner loop finished: %v", err)
|
2015-12-28 20:15:34 +00:00
|
|
|
e.disconnectionError = err
|
2015-12-21 20:34:08 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:15:34 +00:00
|
|
|
func (e *eventObserver) CheckEventError(c *check.C, id, event string, match eventMatcher) {
|
|
|
|
var foundEvent bool
|
|
|
|
scannerOut := e.buffer.String()
|
|
|
|
|
|
|
|
if e.disconnectionError != nil {
|
2016-04-11 18:52:34 +00:00
|
|
|
until := daemonUnixTime(c)
|
2015-12-28 20:15:34 +00:00
|
|
|
out, _ := dockerCmd(c, "events", "--since", e.startTime, "--until", until)
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
for _, e := range events {
|
2016-01-20 22:36:39 +00:00
|
|
|
if _, ok := match(e); ok {
|
2015-12-28 20:15:34 +00:00
|
|
|
foundEvent = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scannerOut = out
|
|
|
|
}
|
|
|
|
if !foundEvent {
|
|
|
|
c.Fatalf("failed to observe event `%s` for %s. Disconnection error: %v\nout:\n%v", event, id, e.disconnectionError, scannerOut)
|
|
|
|
}
|
2015-12-21 20:34:08 +00:00
|
|
|
}
|
2015-12-21 22:55:23 +00:00
|
|
|
|
|
|
|
// matchEventLine matches a text with the event regular expression.
|
2016-01-20 22:36:39 +00:00
|
|
|
// It returns the matches and true if the regular expression matches with the given id and event type.
|
|
|
|
// It returns an empty map and false if there is no match.
|
2015-12-21 22:55:23 +00:00
|
|
|
func matchEventLine(id, eventType string, actions map[string]chan bool) eventMatcher {
|
2016-01-20 22:36:39 +00:00
|
|
|
return func(text string) (map[string]string, bool) {
|
2016-03-08 00:02:35 +00:00
|
|
|
matches := eventstestutils.ScanMap(text)
|
2015-12-28 20:15:34 +00:00
|
|
|
if len(matches) == 0 {
|
2016-01-20 22:36:39 +00:00
|
|
|
return matches, false
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matchIDAndEventType(matches, id, eventType) {
|
2016-01-20 22:36:39 +00:00
|
|
|
if _, ok := actions[matches["action"]]; ok {
|
|
|
|
return matches, true
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 22:36:39 +00:00
|
|
|
return matches, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processEventMatch closes an action channel when an event line matches the expected action.
|
|
|
|
func processEventMatch(actions map[string]chan bool) eventMatchProcessor {
|
|
|
|
return func(matches map[string]string) {
|
|
|
|
if ch, ok := actions[matches["action"]]; ok {
|
2016-02-18 03:52:06 +00:00
|
|
|
ch <- true
|
2016-01-20 22:36:39 +00:00
|
|
|
}
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-03-08 00:02:35 +00:00
|
|
|
matches := eventstestutils.ScanMap(text)
|
2015-12-21 22:55:23 +00:00
|
|
|
return matches["action"]
|
|
|
|
}
|
|
|
|
|
|
|
|
// eventActionsByIDAndType returns the actions for a given id and type.
|
|
|
|
// It fails if the text is not in the event format.
|
|
|
|
func eventActionsByIDAndType(c *check.C, events []string, id, eventType string) []string {
|
|
|
|
var filtered []string
|
|
|
|
for _, event := range events {
|
2016-03-08 00:02:35 +00:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2015-12-21 22:55:23 +00:00
|
|
|
c.Assert(matches, checker.Not(checker.IsNil))
|
|
|
|
if matchIDAndEventType(matches, id, eventType) {
|
|
|
|
filtered = append(filtered, matches["action"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchIDAndEventType returns true if an event matches a given id and type.
|
|
|
|
// It also resolves names in the event attributes if the id doesn't match.
|
|
|
|
func matchIDAndEventType(matches map[string]string, id, eventType string) bool {
|
|
|
|
return matchEventID(matches, id) && matches["eventType"] == eventType
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchEventID(matches map[string]string, id string) bool {
|
|
|
|
matchID := matches["id"] == id || strings.HasPrefix(matches["id"], id)
|
|
|
|
if !matchID && matches["attributes"] != "" {
|
|
|
|
// try matching a name in the attributes
|
|
|
|
attributes := map[string]string{}
|
|
|
|
for _, a := range strings.Split(matches["attributes"], ", ") {
|
|
|
|
kv := strings.Split(a, "=")
|
|
|
|
attributes[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
matchID = attributes["name"] == id
|
|
|
|
}
|
|
|
|
return matchID
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEvents(c *check.C, out, match string) {
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
for _, event := range events {
|
2016-03-08 00:02:35 +00:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2015-12-21 22:55:23 +00:00
|
|
|
matched, err := regexp.MatchString(match, matches["action"])
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-12-28 20:15:34 +00:00
|
|
|
c.Assert(matched, checker.True, check.Commentf("Matcher: %s did not match %s", match, matches["action"]))
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEventsWithID(c *check.C, out, match, id string) {
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
for _, event := range events {
|
2016-03-08 00:02:35 +00:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2015-12-21 22:55:23 +00:00
|
|
|
c.Assert(matchEventID(matches, id), checker.True)
|
|
|
|
|
|
|
|
matched, err := regexp.MatchString(match, matches["action"])
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-12-28 20:15:34 +00:00
|
|
|
c.Assert(matched, checker.True, check.Commentf("Matcher: %s did not match %s", match, matches["action"]))
|
2015-12-21 22:55:23 +00:00
|
|
|
}
|
|
|
|
}
|