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

Merge pull request #12115 from cpuguy83/fix_events_filter_test

Cleanup events filter by container test
This commit is contained in:
Alexander Morozov 2015-04-09 11:40:08 -07:00
commit b02cf5239e

View file

@ -330,89 +330,70 @@ func TestEventsFilterImageName(t *testing.T) {
logDone("events - filters using image") logDone("events - filters using image")
} }
func TestEventsFilterContainerID(t *testing.T) { func TestEventsFilterContainer(t *testing.T) {
since := daemonTime(t).Unix()
defer deleteAllContainers() defer deleteAllContainers()
since := fmt.Sprintf("%d", daemonTime(t).Unix())
nameID := make(map[string]string)
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "true")) for _, name := range []string{"container_1", "container_2"} {
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", name, "busybox", "true"))
if err != nil { if err != nil {
t.Fatal(out, err) t.Fatal(err)
} }
container1 := strings.TrimSpace(out) nameID[name] = strings.TrimSpace(out)
waitInspect(name, "{{.State.Runing }}", "false", 5)
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "true"))
if err != nil {
t.Fatal(out, err)
}
container2 := strings.TrimSpace(out)
for _, s := range []string{container1, container2, container1[:12], container2[:12]} {
if err := waitInspect(s, "{{.State.Running}}", "false", 5); err != nil {
t.Fatalf("Failed to get container %s state, error: %s", s, err)
} }
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("container=%s", s)) until := fmt.Sprintf("%d", daemonTime(t).Unix())
checkEvents := func(id string, events []string) error {
if len(events) != 3 { // create, start, die
return fmt.Errorf("expected 3 events, got %v", events)
}
for _, event := range events {
e := strings.Fields(event)
if len(e) < 3 {
return fmt.Errorf("got malformed event: %s", event)
}
// Check the id
parsedID := strings.TrimSuffix(e[1], ":")
if parsedID != id {
return fmt.Errorf("expected event for container id %s: %s - parsed container id: %s", id, event, parsedID)
}
}
return nil
}
for name, ID := range nameID {
// filter by names
eventsCmd := exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+name)
out, _, err := runCommandWithOutput(eventsCmd) out, _, err := runCommandWithOutput(eventsCmd)
if err != nil {
t.Fatalf("Failed to get events, error: %s(%s)", err, out)
}
events := strings.Split(out, "\n")
checkEvents(t, events[:len(events)-1])
}
logDone("events - filters using container id")
}
func TestEventsFilterContainerName(t *testing.T) {
since := daemonTime(t).Unix()
defer deleteAllContainers()
_, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "busybox", "true"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_2", "busybox", "true")) events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
if err := checkEvents(ID, events); err != nil {
t.Fatal(err)
}
// filter by ID's
eventsCmd = exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+ID)
out, _, err = runCommandWithOutput(eventsCmd)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, s := range []string{"container_1", "container_2"} { events = strings.Split(strings.TrimSuffix(out, "\n"), "\n")
if err := waitInspect(s, "{{.State.Running}}", "false", 5); err != nil { if err := checkEvents(ID, events); err != nil {
t.Fatalf("Failed to get container %s state, error: %s", s, err) t.Fatal(err)
} }
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("container=%s", s))
out, _, err := runCommandWithOutput(eventsCmd)
if err != nil {
t.Fatalf("Failed to get events, error : %s(%s)", err, out)
}
events := strings.Split(out, "\n")
checkEvents(t, events[:len(events)-1])
} }
logDone("events - filters using container name") logDone("events - filters using container name")
} }
func checkEvents(t *testing.T, events []string) {
if len(events) != 3 {
t.Fatalf("Expected 3 events, got %d: %v", len(events), events)
}
createEvent := strings.Fields(events[0])
if createEvent[len(createEvent)-1] != "create" {
t.Fatalf("first event should be create, not %#v", createEvent)
}
startEvent := strings.Fields(events[1])
if startEvent[len(startEvent)-1] != "start" {
t.Fatalf("second 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)
}
}
func TestEventsStreaming(t *testing.T) { func TestEventsStreaming(t *testing.T) {
start := daemonTime(t).Unix() start := daemonTime(t).Unix()