1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #10998 from jfrazelle/fix-filter-events-test-better

Fix events test so it doesnt need new daemon
This commit is contained in:
Jessie Frazelle 2015-02-27 12:05:48 -08:00
commit 2768ce0e4e

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os/exec" "os/exec"
"regexp"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -203,68 +204,43 @@ func TestEventsImageImport(t *testing.T) {
} }
func TestEventsFilters(t *testing.T) { func TestEventsFilters(t *testing.T) {
// we need a new daemon here parseEvents := func(out, match string) {
// otherwise events picks up the container from the previous events := strings.Split(out, "\n")
// function as a die event (some sort of race) events = events[:len(events)-1]
// I am not proud of this - jessfraz for _, event := range events {
d := NewDaemon(t) eventFields := strings.Fields(event)
if err := d.StartWithBusybox(); err != nil { eventName := eventFields[len(eventFields)-1]
t.Fatalf("Could not start daemon with busybox: %v", err) if ok, err := regexp.MatchString(match, eventName); err != nil || !ok {
t.Fatalf("event should match %s, got %#v, err: %v", match, eventFields, err)
}
}
} }
defer d.Stop()
since := time.Now().Unix() since := time.Now().Unix()
out, err := d.Cmd("run", "--rm", "busybox", "true") out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
if err != nil { if err != nil {
t.Fatal(out, err) t.Fatal(out, err)
} }
out, err = d.Cmd("run", "--rm", "busybox", "true") out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
if err != nil { if err != nil {
t.Fatal(out, err) t.Fatal(out, err)
} }
out, err = d.Cmd("events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die") out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die"))
if err != nil { if err != nil {
t.Fatalf("Failed to get events: %s", err) t.Fatalf("Failed to get events: %s", err)
} }
events := strings.Split(out, "\n") parseEvents(out, "die")
events = events[:len(events)-1]
if len(events) != 2 {
t.Fatalf("Expected 2 events, got %d: %v", len(events), events)
}
dieEvent := strings.Fields(events[len(events)-1])
if dieEvent[len(dieEvent)-1] != "die" {
t.Fatalf("event should be die, not %#v", dieEvent)
}
dieEvent = strings.Fields(events[len(events)-2]) out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die", "--filter", "event=start"))
if dieEvent[len(dieEvent)-1] != "die" {
t.Fatalf("event should be die, not %#v", dieEvent)
}
out, err = d.Cmd("events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die", "--filter", "event=start")
if err != nil { if err != nil {
t.Fatalf("Failed to get events: %s", err) t.Fatalf("Failed to get events: %s", err)
} }
events = strings.Split(out, "\n") parseEvents(out, "((die)|(start))")
events = events[:len(events)-1]
if len(events) != 4 { // make sure we at least got 2 start events
t.Fatalf("Expected 4 events, got %d: %v", len(events), events) count := strings.Count(out, "start")
} if count != 2 {
startEvent := strings.Fields(events[len(events)-4]) t.Fatalf("should have had 2 start events but had %d, out: %s", count, out)
if startEvent[len(startEvent)-1] != "start" {
t.Fatalf("event should be start, not %#v", startEvent)
}
dieEvent = strings.Fields(events[len(events)-3])
if dieEvent[len(dieEvent)-1] != "die" {
t.Fatalf("event should be die, not %#v", dieEvent)
}
startEvent = strings.Fields(events[len(events)-2])
if startEvent[len(startEvent)-1] != "start" {
t.Fatalf("event should be start, not %#v", startEvent)
}
dieEvent = strings.Fields(events[len(events)-1])
if dieEvent[len(dieEvent)-1] != "die" {
t.Fatalf("event should be die, not %#v", dieEvent)
} }
logDone("events - filters") logDone("events - filters")