2014-06-05 14:32:19 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-05 16:01:52 +03:00
|
|
|
"bufio"
|
2014-07-07 19:47:06 +04:00
|
|
|
"fmt"
|
2014-06-05 14:32:19 +04:00
|
|
|
"os/exec"
|
2015-02-25 03:24:19 +03:00
|
|
|
"regexp"
|
2014-07-04 09:12:21 +04:00
|
|
|
"strconv"
|
2014-06-05 14:32:19 +04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-07-07 19:47:06 +04:00
|
|
|
"time"
|
2014-06-05 14:32:19 +04:00
|
|
|
)
|
|
|
|
|
2014-09-19 12:37:50 +04:00
|
|
|
func TestEventsUntag(t *testing.T) {
|
2014-10-24 01:30:11 +04:00
|
|
|
image := "busybox"
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "tag", image, "utest:tag1")
|
|
|
|
dockerCmd(t, "tag", image, "utest:tag2")
|
|
|
|
dockerCmd(t, "rmi", "utest:tag1")
|
|
|
|
dockerCmd(t, "rmi", "utest:tag2")
|
2015-02-14 12:19:57 +03:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=1")
|
|
|
|
out, exitCode, _, err := runCommandWithOutputForDuration(eventsCmd, time.Duration(time.Millisecond*200))
|
|
|
|
if exitCode != 0 || err != nil {
|
|
|
|
t.Fatalf("Failed to get events - exit code %d: %s", exitCode, err)
|
|
|
|
}
|
2014-06-05 14:32:19 +04:00
|
|
|
events := strings.Split(out, "\n")
|
2014-10-06 18:26:55 +04:00
|
|
|
nEvents := len(events)
|
2014-06-05 14:32:19 +04:00
|
|
|
// The last element after the split above will be an empty string, so we
|
|
|
|
// get the two elements before the last, which are the untags we're
|
|
|
|
// looking for.
|
2014-10-06 18:26:55 +04:00
|
|
|
for _, v := range events[nEvents-3 : nEvents-1] {
|
2014-06-05 14:32:19 +04:00
|
|
|
if !strings.Contains(v, "untag") {
|
|
|
|
t.Fatalf("event should be untag, not %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logDone("events - untags are logged")
|
|
|
|
}
|
2014-07-07 19:47:06 +04:00
|
|
|
|
2014-09-20 00:54:32 +04:00
|
|
|
func TestEventsContainerFailStartDie(t *testing.T) {
|
2014-11-17 19:05:49 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2015-04-14 08:16:19 +03:00
|
|
|
out, _ := dockerCmd(t, "images", "-q")
|
2014-09-20 00:54:32 +04:00
|
|
|
image := strings.Split(out, "\n")[0]
|
2015-01-30 20:29:46 +03:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "run", "--name", "testeventdie", image, "blerg")
|
2014-09-20 00:54:32 +04:00
|
|
|
_, _, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Container run with command blerg should have failed, but it did not")
|
|
|
|
}
|
|
|
|
|
2015-03-11 00:19:32 +03:00
|
|
|
eventsCmd = exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
2014-09-20 00:54:32 +04:00
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
if len(events) <= 1 {
|
|
|
|
t.Fatalf("Missing expected event")
|
|
|
|
}
|
|
|
|
|
|
|
|
startEvent := strings.Fields(events[len(events)-3])
|
|
|
|
dieEvent := strings.Fields(events[len(events)-2])
|
|
|
|
|
|
|
|
if startEvent[len(startEvent)-1] != "start" {
|
|
|
|
t.Fatalf("event should be start, not %#v", startEvent)
|
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
|
|
|
t.Fatalf("event should be die, not %#v", dieEvent)
|
|
|
|
}
|
|
|
|
|
2015-02-10 02:01:52 +03:00
|
|
|
logDone("events - container unwilling to start logs die")
|
2014-09-20 00:54:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-19 12:37:50 +04:00
|
|
|
func TestEventsLimit(t *testing.T) {
|
2014-11-17 19:05:49 +03:00
|
|
|
defer deleteAllContainers()
|
2014-07-04 09:12:21 +04:00
|
|
|
for i := 0; i < 30; i++ {
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "run", "busybox", "echo", strconv.Itoa(i))
|
2014-07-04 09:12:21 +04:00
|
|
|
}
|
2015-03-11 00:19:32 +03:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
2014-07-04 09:12:21 +04:00
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
2014-10-06 18:26:55 +04:00
|
|
|
nEvents := len(events) - 1
|
|
|
|
if nEvents != 64 {
|
|
|
|
t.Fatalf("events should be limited to 64, but received %d", nEvents)
|
2014-07-04 09:12:21 +04:00
|
|
|
}
|
|
|
|
logDone("events - limited to 64 entries")
|
|
|
|
}
|
2014-08-11 09:54:59 +04:00
|
|
|
|
2014-09-19 12:37:50 +04:00
|
|
|
func TestEventsContainerEvents(t *testing.T) {
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "run", "--rm", "busybox", "true")
|
2015-03-11 00:19:32 +03:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
2014-08-11 09:54:59 +04:00
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2014-08-13 11:37:30 +04:00
|
|
|
t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2014-08-11 09:54:59 +04:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) < 4 {
|
|
|
|
t.Fatalf("Missing expected event")
|
|
|
|
}
|
|
|
|
createEvent := strings.Fields(events[len(events)-4])
|
|
|
|
startEvent := strings.Fields(events[len(events)-3])
|
|
|
|
dieEvent := strings.Fields(events[len(events)-2])
|
|
|
|
destroyEvent := strings.Fields(events[len(events)-1])
|
|
|
|
if createEvent[len(createEvent)-1] != "create" {
|
|
|
|
t.Fatalf("event should be create, not %#v", createEvent)
|
|
|
|
}
|
|
|
|
if startEvent[len(startEvent)-1] != "start" {
|
2014-11-20 22:46:48 +03:00
|
|
|
t.Fatalf("event should be start, not %#v", startEvent)
|
2014-08-11 09:54:59 +04:00
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
2014-11-20 22:46:48 +03:00
|
|
|
t.Fatalf("event should be die, not %#v", dieEvent)
|
2014-08-11 09:54:59 +04:00
|
|
|
}
|
|
|
|
if destroyEvent[len(destroyEvent)-1] != "destroy" {
|
2014-11-20 22:46:48 +03:00
|
|
|
t.Fatalf("event should be destroy, not %#v", destroyEvent)
|
2014-08-11 09:54:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - container create, start, die, destroy is logged")
|
|
|
|
}
|
2014-08-11 11:39:47 +04:00
|
|
|
|
2015-03-22 22:40:16 +03:00
|
|
|
func TestEventsContainerEventsSinceUnixEpoch(t *testing.T) {
|
|
|
|
dockerCmd(t, "run", "--rm", "busybox", "true")
|
|
|
|
timeBeginning := time.Unix(0, 0).Format(time.RFC3339Nano)
|
|
|
|
timeBeginning = strings.Replace(timeBeginning, "Z", ".000000000Z", -1)
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since='%s'", timeBeginning),
|
|
|
|
fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
|
|
|
t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) < 4 {
|
|
|
|
t.Fatalf("Missing expected event")
|
|
|
|
}
|
|
|
|
createEvent := strings.Fields(events[len(events)-4])
|
|
|
|
startEvent := strings.Fields(events[len(events)-3])
|
|
|
|
dieEvent := strings.Fields(events[len(events)-2])
|
|
|
|
destroyEvent := strings.Fields(events[len(events)-1])
|
|
|
|
if createEvent[len(createEvent)-1] != "create" {
|
|
|
|
t.Fatalf("event should be create, not %#v", createEvent)
|
|
|
|
}
|
|
|
|
if startEvent[len(startEvent)-1] != "start" {
|
|
|
|
t.Fatalf("event should be start, not %#v", startEvent)
|
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
|
|
|
t.Fatalf("event should be die, not %#v", dieEvent)
|
|
|
|
}
|
|
|
|
if destroyEvent[len(destroyEvent)-1] != "destroy" {
|
|
|
|
t.Fatalf("event should be destroy, not %#v", destroyEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - container create, start, die, destroy since Unix Epoch time")
|
|
|
|
}
|
|
|
|
|
2014-09-19 12:37:50 +04:00
|
|
|
func TestEventsImageUntagDelete(t *testing.T) {
|
2014-08-11 11:39:47 +04:00
|
|
|
name := "testimageevents"
|
|
|
|
defer deleteImages(name)
|
|
|
|
_, err := buildImage(name,
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER "docker"`,
|
|
|
|
true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := deleteImages(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-11 00:19:32 +03:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
2014-08-11 11:39:47 +04:00
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2014-08-13 11:37:30 +04:00
|
|
|
t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2014-08-11 11:39:47 +04:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
2015-02-20 00:33:23 +03:00
|
|
|
|
2014-08-11 11:39:47 +04:00
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) < 2 {
|
|
|
|
t.Fatalf("Missing expected event")
|
|
|
|
}
|
|
|
|
untagEvent := strings.Fields(events[len(events)-2])
|
|
|
|
deleteEvent := strings.Fields(events[len(events)-1])
|
|
|
|
if untagEvent[len(untagEvent)-1] != "untag" {
|
|
|
|
t.Fatalf("untag should be untag, not %#v", untagEvent)
|
|
|
|
}
|
|
|
|
if deleteEvent[len(deleteEvent)-1] != "delete" {
|
|
|
|
t.Fatalf("delete should be delete, not %#v", deleteEvent)
|
|
|
|
}
|
|
|
|
logDone("events - image untag, delete is logged")
|
|
|
|
}
|
2014-09-25 18:50:10 +04:00
|
|
|
|
2014-10-22 17:48:02 +04:00
|
|
|
func TestEventsImagePull(t *testing.T) {
|
2015-03-11 00:19:32 +03:00
|
|
|
since := daemonTime(t).Unix()
|
2015-03-26 06:44:09 +03:00
|
|
|
testRequires(t, Network)
|
2015-02-06 04:46:55 +03:00
|
|
|
|
|
|
|
defer deleteImages("hello-world")
|
|
|
|
|
2014-10-29 00:06:23 +03:00
|
|
|
pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
|
2014-10-22 17:48:02 +04:00
|
|
|
if out, _, err := runCommandWithOutput(pullCmd); err != nil {
|
2014-10-29 00:06:23 +03:00
|
|
|
t.Fatalf("pulling the hello-world image from has failed: %s, %v", out, err)
|
2014-10-22 17:48:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events",
|
|
|
|
fmt.Sprintf("--since=%d", since),
|
2015-03-11 00:19:32 +03:00
|
|
|
fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
2014-10-22 17:48:02 +04:00
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
event := strings.TrimSpace(events[len(events)-1])
|
|
|
|
|
2014-10-29 00:06:23 +03:00
|
|
|
if !strings.HasSuffix(event, "hello-world:latest: pull") {
|
2014-10-22 17:48:02 +04:00
|
|
|
t.Fatalf("Missing pull event - got:%q", event)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - image pull is logged")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventsImageImport(t *testing.T) {
|
2015-02-20 00:33:23 +03:00
|
|
|
defer deleteAllContainers()
|
2015-03-11 00:19:32 +03:00
|
|
|
since := daemonTime(t).Unix()
|
2014-10-22 17:48:02 +04:00
|
|
|
|
2015-02-14 13:27:06 +03:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-22 17:48:02 +04:00
|
|
|
if err != nil {
|
2015-02-14 13:27:06 +03:00
|
|
|
t.Fatal("failed to create a container", out, err)
|
2014-10-22 17:48:02 +04:00
|
|
|
}
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-02-14 13:27:06 +03:00
|
|
|
|
|
|
|
out, _, err = runCommandPipelineWithOutput(
|
|
|
|
exec.Command(dockerBinary, "export", cleanedContainerID),
|
|
|
|
exec.Command(dockerBinary, "import", "-"),
|
|
|
|
)
|
2014-10-22 17:48:02 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("import failed with errors: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events",
|
|
|
|
fmt.Sprintf("--since=%d", since),
|
2015-03-11 00:19:32 +03:00
|
|
|
fmt.Sprintf("--until=%d", daemonTime(t).Unix()))
|
2014-10-22 17:48:02 +04:00
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
event := strings.TrimSpace(events[len(events)-1])
|
|
|
|
|
|
|
|
if !strings.HasSuffix(event, ": import") {
|
2015-04-07 18:54:00 +03:00
|
|
|
t.Fatalf("Missing import event - got:%q", event)
|
2014-10-22 17:48:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - image import is logged")
|
|
|
|
}
|
2014-11-20 22:46:48 +03:00
|
|
|
|
|
|
|
func TestEventsFilters(t *testing.T) {
|
2015-02-25 03:24:19 +03:00
|
|
|
parseEvents := func(out, match string) {
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
for _, event := range events {
|
|
|
|
eventFields := strings.Fields(event)
|
|
|
|
eventName := eventFields[len(eventFields)-1]
|
|
|
|
if ok, err := regexp.MatchString(match, eventName); err != nil || !ok {
|
|
|
|
t.Fatalf("event should match %s, got %#v, err: %v", match, eventFields, err)
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 00:33:23 +03:00
|
|
|
}
|
|
|
|
|
2015-03-11 00:19:32 +03:00
|
|
|
since := daemonTime(t).Unix()
|
2015-02-25 03:24:19 +03:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
|
2014-11-26 05:34:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-02-25 03:24:19 +03:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
|
2014-11-26 05:34:08 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-03-11 00:19:32 +03:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", "event=die"))
|
2015-02-20 00:33:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get events: %s", err)
|
2014-11-20 22:46:48 +03:00
|
|
|
}
|
2015-02-25 03:24:19 +03:00
|
|
|
parseEvents(out, "die")
|
2014-11-20 22:46:48 +03:00
|
|
|
|
2015-03-11 00:19:32 +03:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", "event=die", "--filter", "event=start"))
|
2015-02-20 00:33:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get events: %s", err)
|
2014-11-20 22:46:48 +03:00
|
|
|
}
|
2015-02-25 03:24:19 +03:00
|
|
|
parseEvents(out, "((die)|(start))")
|
|
|
|
|
|
|
|
// make sure we at least got 2 start events
|
|
|
|
count := strings.Count(out, "start")
|
2015-02-28 01:48:39 +03:00
|
|
|
if count < 2 {
|
2015-02-25 03:24:19 +03:00
|
|
|
t.Fatalf("should have had 2 start events but had %d, out: %s", count, out)
|
2014-11-20 22:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - filters")
|
|
|
|
}
|
2015-02-10 00:02:53 +03:00
|
|
|
|
|
|
|
func TestEventsFilterImageName(t *testing.T) {
|
2015-03-11 00:19:32 +03:00
|
|
|
since := daemonTime(t).Unix()
|
2015-02-10 00:02:53 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2015-04-01 17:08:00 +03:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox:latest", "true"))
|
2015-02-10 00:02:53 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-04-06 16:21:18 +03:00
|
|
|
container1 := strings.TrimSpace(out)
|
2015-02-09 09:03:42 +03:00
|
|
|
|
2015-02-10 00:02:53 +03:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_2", "-d", "busybox", "true"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-04-06 16:21:18 +03:00
|
|
|
container2 := strings.TrimSpace(out)
|
2015-02-10 00:02:53 +03:00
|
|
|
|
2015-04-01 17:08:00 +03:00
|
|
|
s := "busybox"
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(t).Unix()), "--filter", fmt.Sprintf("image=%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")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) == 0 {
|
|
|
|
t.Fatalf("Expected events but found none for the image busybox:latest")
|
|
|
|
}
|
|
|
|
count1 := 0
|
|
|
|
count2 := 0
|
|
|
|
|
|
|
|
for _, e := range events {
|
|
|
|
if strings.Contains(e, container1) {
|
|
|
|
count1++
|
|
|
|
} else if strings.Contains(e, container2) {
|
|
|
|
count2++
|
2015-02-10 00:02:53 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 17:08:00 +03:00
|
|
|
if count1 == 0 || count2 == 0 {
|
|
|
|
t.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
|
|
|
|
}
|
2015-02-10 00:02:53 +03:00
|
|
|
|
|
|
|
logDone("events - filters using image")
|
2015-02-09 09:03:42 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
func TestEventsFilterContainer(t *testing.T) {
|
2015-02-09 09:03:42 +03:00
|
|
|
defer deleteAllContainers()
|
2015-04-06 20:18:38 +03:00
|
|
|
since := fmt.Sprintf("%d", daemonTime(t).Unix())
|
|
|
|
nameID := make(map[string]string)
|
2015-02-09 09:03:42 +03:00
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
for _, name := range []string{"container_1", "container_2"} {
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", name, "busybox", "true"))
|
2015-02-09 09:03:42 +03:00
|
|
|
if err != nil {
|
2015-04-06 20:18:38 +03:00
|
|
|
t.Fatal(err)
|
2015-02-09 09:03:42 +03:00
|
|
|
}
|
2015-04-06 20:18:38 +03:00
|
|
|
nameID[name] = strings.TrimSpace(out)
|
|
|
|
waitInspect(name, "{{.State.Runing }}", "false", 5)
|
2015-02-09 09:03:42 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
until := fmt.Sprintf("%d", daemonTime(t).Unix())
|
2015-02-09 09:03:42 +03:00
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
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)
|
|
|
|
}
|
2015-02-09 09:03:42 +03:00
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
// 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
|
2015-02-09 09:03:42 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
for name, ID := range nameID {
|
|
|
|
// filter by names
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+name)
|
|
|
|
out, _, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-09 09:03:42 +03:00
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
|
|
|
if err := checkEvents(ID, events); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-03-31 06:26:27 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
// filter by ID's
|
|
|
|
eventsCmd = exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+ID)
|
|
|
|
out, _, err = runCommandWithOutput(eventsCmd)
|
2015-02-09 09:03:42 +03:00
|
|
|
if err != nil {
|
2015-04-06 20:18:38 +03:00
|
|
|
t.Fatal(err)
|
2015-02-09 09:03:42 +03:00
|
|
|
}
|
2015-02-10 00:02:53 +03:00
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
events = strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
|
|
|
if err := checkEvents(ID, events); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-26 22:43:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 20:18:38 +03:00
|
|
|
logDone("events - filters using container name")
|
2015-03-26 22:43:00 +03:00
|
|
|
}
|
2015-04-05 16:01:52 +03:00
|
|
|
|
|
|
|
func TestEventsStreaming(t *testing.T) {
|
|
|
|
start := daemonTime(t).Unix()
|
|
|
|
|
|
|
|
finish := make(chan struct{})
|
|
|
|
defer close(finish)
|
|
|
|
id := make(chan string)
|
|
|
|
eventCreate := make(chan struct{})
|
|
|
|
eventStart := make(chan struct{})
|
|
|
|
eventDie := make(chan struct{})
|
|
|
|
eventDestroy := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
2015-04-06 23:39:02 +03:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since", strconv.FormatInt(start, 10))
|
2015-04-05 16:01:52 +03:00
|
|
|
stdout, err := eventsCmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = eventsCmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start 'docker events': %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-finish
|
|
|
|
eventsCmd.Process.Kill()
|
|
|
|
}()
|
|
|
|
|
|
|
|
containerID := <-id
|
|
|
|
|
|
|
|
matchCreate := regexp.MustCompile(containerID + `: \(from busybox:latest\) create$`)
|
|
|
|
matchStart := regexp.MustCompile(containerID + `: \(from busybox:latest\) start$`)
|
|
|
|
matchDie := regexp.MustCompile(containerID + `: \(from busybox:latest\) die$`)
|
|
|
|
matchDestroy := regexp.MustCompile(containerID + `: \(from busybox:latest\) destroy$`)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
|
|
|
switch {
|
|
|
|
case matchCreate.MatchString(scanner.Text()):
|
|
|
|
close(eventCreate)
|
|
|
|
case matchStart.MatchString(scanner.Text()):
|
|
|
|
close(eventStart)
|
|
|
|
case matchDie.MatchString(scanner.Text()):
|
|
|
|
close(eventDie)
|
|
|
|
case matchDestroy.MatchString(scanner.Text()):
|
|
|
|
close(eventDestroy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = eventsCmd.Wait()
|
|
|
|
if err != nil && !IsKilled(err) {
|
|
|
|
t.Fatalf("docker events had bad exit status: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-01 17:08:00 +03:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox:latest", "true")
|
2015-04-05 16:01:52 +03:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
id <- cleanedContainerID
|
|
|
|
|
|
|
|
select {
|
2015-04-06 23:40:56 +03:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-05 16:01:52 +03:00
|
|
|
t.Fatal("failed to observe container create in timely fashion")
|
|
|
|
case <-eventCreate:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-04-06 23:40:56 +03:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-05 16:01:52 +03:00
|
|
|
t.Fatal("failed to observe container start in timely fashion")
|
|
|
|
case <-eventStart:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-04-06 23:40:56 +03:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-05 16:01:52 +03:00
|
|
|
t.Fatal("failed to observe container die in timely fashion")
|
|
|
|
case <-eventDie:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
rmCmd := exec.Command(dockerBinary, "rm", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(rmCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-04-06 23:40:56 +03:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-05 16:01:52 +03:00
|
|
|
t.Fatal("failed to observe container destroy in timely fashion")
|
|
|
|
case <-eventDestroy:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - streamed to stdout")
|
|
|
|
}
|