2014-06-05 06:32:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-07 11:47:06 -04:00
|
|
|
"fmt"
|
2014-06-05 06:32:19 -04:00
|
|
|
"os/exec"
|
2014-07-04 01:12:21 -04:00
|
|
|
"strconv"
|
2014-06-05 06:32:19 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-07-07 11:47:06 -04:00
|
|
|
"time"
|
2014-06-05 06:32:19 -04:00
|
|
|
)
|
|
|
|
|
2014-09-19 04:37:50 -04:00
|
|
|
func TestEventsUntag(t *testing.T) {
|
2014-10-23 17:30:11 -04:00
|
|
|
image := "busybox"
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "tag", image, "utest:tag1")
|
|
|
|
dockerCmd(t, "tag", image, "utest:tag2")
|
|
|
|
dockerCmd(t, "rmi", "utest:tag1")
|
|
|
|
dockerCmd(t, "rmi", "utest:tag2")
|
2014-06-05 06:32:19 -04:00
|
|
|
eventsCmd := exec.Command("timeout", "0.2", dockerBinary, "events", "--since=1")
|
2014-10-23 17:30:11 -04:00
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
2014-06-05 06:32:19 -04:00
|
|
|
events := strings.Split(out, "\n")
|
2014-10-06 10:26:55 -04:00
|
|
|
nEvents := len(events)
|
2014-06-05 06: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 10:26:55 -04:00
|
|
|
for _, v := range events[nEvents-3 : nEvents-1] {
|
2014-06-05 06: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 11:47:06 -04:00
|
|
|
|
2014-09-19 04:37:50 -04:00
|
|
|
func TestEventsPause(t *testing.T) {
|
2014-11-17 11:05:49 -05:00
|
|
|
name := "testeventpause"
|
2014-11-24 10:32:38 -05:00
|
|
|
out, _, _ := dockerCmd(t, "images", "-q")
|
2014-07-07 11:47:06 -04:00
|
|
|
image := strings.Split(out, "\n")[0]
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "run", "-d", "--name", name, image, "sleep", "2")
|
|
|
|
dockerCmd(t, "pause", name)
|
|
|
|
dockerCmd(t, "unpause", name)
|
2014-11-17 11:05:49 -05:00
|
|
|
|
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-07-07 11:47:06 -04:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
if len(events) <= 1 {
|
|
|
|
t.Fatalf("Missing expected event")
|
|
|
|
}
|
|
|
|
|
|
|
|
pauseEvent := strings.Fields(events[len(events)-3])
|
|
|
|
unpauseEvent := strings.Fields(events[len(events)-2])
|
|
|
|
|
|
|
|
if pauseEvent[len(pauseEvent)-1] != "pause" {
|
|
|
|
t.Fatalf("event should be pause, not %#v", pauseEvent)
|
|
|
|
}
|
|
|
|
if unpauseEvent[len(unpauseEvent)-1] != "unpause" {
|
2014-11-20 14:46:48 -05:00
|
|
|
t.Fatalf("event should be unpause, not %#v", unpauseEvent)
|
2014-07-07 11:47:06 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 11:05:49 -05:00
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", name)
|
|
|
|
if waitOut, _, err := runCommandWithOutput(waitCmd); err != nil {
|
|
|
|
t.Fatalf("error thrown while waiting for container: %s, %v", waitOut, err)
|
|
|
|
}
|
|
|
|
|
2014-07-07 11:47:06 -04:00
|
|
|
logDone("events - pause/unpause is logged")
|
|
|
|
}
|
2014-07-04 01:12:21 -04:00
|
|
|
|
2014-09-19 16:54:32 -04:00
|
|
|
func TestEventsContainerFailStartDie(t *testing.T) {
|
2014-11-17 11:05:49 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-11-24 10:32:38 -05:00
|
|
|
out, _, _ := dockerCmd(t, "images", "-q")
|
2014-09-19 16:54:32 -04:00
|
|
|
image := strings.Split(out, "\n")[0]
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testeventdie", image, "blerg")
|
|
|
|
_, _, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Container run with command blerg should have failed, but it did not")
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsCmd = exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - container failed to start logs die")
|
|
|
|
}
|
|
|
|
|
2014-09-19 04:37:50 -04:00
|
|
|
func TestEventsLimit(t *testing.T) {
|
2014-11-17 11:05:49 -05:00
|
|
|
defer deleteAllContainers()
|
2014-07-04 01:12:21 -04:00
|
|
|
for i := 0; i < 30; i++ {
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "run", "busybox", "echo", strconv.Itoa(i))
|
2014-07-04 01:12:21 -04:00
|
|
|
}
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
2014-10-06 10: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 01:12:21 -04:00
|
|
|
}
|
|
|
|
logDone("events - limited to 64 entries")
|
|
|
|
}
|
2014-08-11 01:54:59 -04:00
|
|
|
|
2014-09-19 04:37:50 -04:00
|
|
|
func TestEventsContainerEvents(t *testing.T) {
|
2014-11-24 10:32:38 -05:00
|
|
|
dockerCmd(t, "run", "--rm", "busybox", "true")
|
2014-08-11 01:54:59 -04:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2014-08-13 03:37:30 -04:00
|
|
|
t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2014-08-11 01: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 14:46:48 -05:00
|
|
|
t.Fatalf("event should be start, not %#v", startEvent)
|
2014-08-11 01:54:59 -04:00
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
2014-11-20 14:46:48 -05:00
|
|
|
t.Fatalf("event should be die, not %#v", dieEvent)
|
2014-08-11 01:54:59 -04:00
|
|
|
}
|
|
|
|
if destroyEvent[len(destroyEvent)-1] != "destroy" {
|
2014-11-20 14:46:48 -05:00
|
|
|
t.Fatalf("event should be destroy, not %#v", destroyEvent)
|
2014-08-11 01:54:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - container create, start, die, destroy is logged")
|
|
|
|
}
|
2014-08-11 03:39:47 -04:00
|
|
|
|
2014-09-19 04:37:50 -04:00
|
|
|
func TestEventsImageUntagDelete(t *testing.T) {
|
2014-08-11 03: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)
|
|
|
|
}
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2014-08-13 03:37:30 -04:00
|
|
|
t.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2014-08-11 03:39:47 -04:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
t.Log(events)
|
|
|
|
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 10:50:10 -04:00
|
|
|
|
2014-10-22 09:48:02 -04:00
|
|
|
func TestEventsImagePull(t *testing.T) {
|
|
|
|
since := time.Now().Unix()
|
2014-10-28 17:06:23 -04:00
|
|
|
pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
|
2014-10-22 09:48:02 -04:00
|
|
|
if out, _, err := runCommandWithOutput(pullCmd); err != nil {
|
2014-10-28 17:06:23 -04:00
|
|
|
t.Fatalf("pulling the hello-world image from has failed: %s, %v", out, err)
|
2014-10-22 09:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events",
|
|
|
|
fmt.Sprintf("--since=%d", since),
|
|
|
|
fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
event := strings.TrimSpace(events[len(events)-1])
|
|
|
|
|
2014-10-28 17:06:23 -04:00
|
|
|
if !strings.HasSuffix(event, "hello-world:latest: pull") {
|
2014-10-22 09:48:02 -04:00
|
|
|
t.Fatalf("Missing pull event - got:%q", event)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - image pull is logged")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventsImageImport(t *testing.T) {
|
|
|
|
since := time.Now().Unix()
|
|
|
|
|
2014-11-17 11:05:49 -05:00
|
|
|
defer deleteImages("cirros")
|
|
|
|
|
2014-10-22 09:48:02 -04:00
|
|
|
server, err := fileServer(map[string]string{
|
|
|
|
"/cirros.tar.gz": "/cirros.tar.gz",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
fileURL := fmt.Sprintf("%s/cirros.tar.gz", server.URL)
|
2014-11-17 11:05:49 -05:00
|
|
|
importCmd := exec.Command(dockerBinary, "import", fileURL, "cirros")
|
2014-10-22 09:48:02 -04:00
|
|
|
out, _, err := runCommandWithOutput(importCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("import failed with errors: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events",
|
|
|
|
fmt.Sprintf("--since=%d", since),
|
|
|
|
fmt.Sprintf("--until=%d", time.Now().Unix()))
|
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
event := strings.TrimSpace(events[len(events)-1])
|
|
|
|
|
|
|
|
if !strings.HasSuffix(event, ": import") {
|
|
|
|
t.Fatalf("Missing pull event - got:%q", event)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("events - image import is logged")
|
|
|
|
}
|
2014-11-20 14:46:48 -05:00
|
|
|
|
|
|
|
func TestEventsFilters(t *testing.T) {
|
2014-11-25 12:07:04 -05:00
|
|
|
since := time.Now().Unix()
|
2014-11-25 21:34:08 -05:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-11-25 12:07:04 -05:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die")
|
2014-11-20 14:46:48 -05:00
|
|
|
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) != 2 {
|
2014-11-25 12:07:04 -05:00
|
|
|
t.Fatalf("Expected 2 events, got %d: %v", len(events), events)
|
2014-11-20 14:46:48 -05:00
|
|
|
}
|
|
|
|
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])
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
|
|
|
t.Fatalf("event should be die, not %#v", dieEvent)
|
|
|
|
}
|
|
|
|
|
2014-11-25 12:07:04 -05:00
|
|
|
eventsCmd = exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", time.Now().Unix()), "--filter", "event=die", "--filter", "event=start")
|
2014-11-20 14:46:48 -05:00
|
|
|
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 {
|
2014-11-25 12:07:04 -05:00
|
|
|
t.Fatalf("Expected 4 events, got %d: %v", len(events), events)
|
2014-11-20 14:46:48 -05:00
|
|
|
}
|
|
|
|
startEvent := strings.Fields(events[len(events)-4])
|
|
|
|
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")
|
|
|
|
}
|