2014-06-05 10:32:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-05 13:01:52 +00:00
|
|
|
"bufio"
|
2014-07-07 15:47:06 +00:00
|
|
|
"fmt"
|
2014-06-05 10:32:19 +00:00
|
|
|
"os/exec"
|
2015-02-25 00:24:19 +00:00
|
|
|
"regexp"
|
2014-07-04 05:12:21 +00:00
|
|
|
"strconv"
|
2014-06-05 10:32:19 +00:00
|
|
|
"strings"
|
2015-04-27 19:12:23 +00:00
|
|
|
"sync"
|
2014-07-07 15:47:06 +00:00
|
|
|
"time"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-06-05 10:32:19 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsUntag(c *check.C) {
|
2014-10-23 21:30:11 +00:00
|
|
|
image := "busybox"
|
2015-04-18 16:46:47 +00:00
|
|
|
dockerCmd(c, "tag", image, "utest:tag1")
|
|
|
|
dockerCmd(c, "tag", image, "utest:tag2")
|
|
|
|
dockerCmd(c, "rmi", "utest:tag1")
|
|
|
|
dockerCmd(c, "rmi", "utest:tag2")
|
2015-02-14 09:19:57 +00:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=1")
|
|
|
|
out, exitCode, _, err := runCommandWithOutputForDuration(eventsCmd, time.Duration(time.Millisecond*200))
|
|
|
|
if exitCode != 0 || err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events - exit code %d: %s", exitCode, err)
|
2015-02-14 09:19:57 +00:00
|
|
|
}
|
2014-06-05 10:32:19 +00:00
|
|
|
events := strings.Split(out, "\n")
|
2014-10-06 14:26:55 +00:00
|
|
|
nEvents := len(events)
|
2014-06-05 10:32:19 +00: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 14:26:55 +00:00
|
|
|
for _, v := range events[nEvents-3 : nEvents-1] {
|
2014-06-05 10:32:19 +00:00
|
|
|
if !strings.Contains(v, "untag") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be untag, not %#v", v)
|
2014-06-05 10:32:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 15:47:06 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsContainerFailStartDie(c *check.C) {
|
2014-11-17 16:05:49 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _ := dockerCmd(c, "images", "-q")
|
2014-09-19 20:54:32 +00:00
|
|
|
image := strings.Split(out, "\n")[0]
|
2015-01-30 17:29:46 +00:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "run", "--name", "testeventdie", image, "blerg")
|
2014-09-19 20:54:32 +00:00
|
|
|
_, _, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if err == nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Container run with command blerg should have failed, but it did not")
|
2014-09-19 20:54:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
eventsCmd = exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2014-09-19 20:54:32 +00:00
|
|
|
out, _, _ = runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
if len(events) <= 1 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Missing expected event")
|
2014-09-19 20:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startEvent := strings.Fields(events[len(events)-3])
|
|
|
|
dieEvent := strings.Fields(events[len(events)-2])
|
|
|
|
|
|
|
|
if startEvent[len(startEvent)-1] != "start" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be start, not %#v", startEvent)
|
2014-09-19 20:54:32 +00:00
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be die, not %#v", dieEvent)
|
2014-09-19 20:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsLimit(c *check.C) {
|
2015-04-27 19:12:23 +00:00
|
|
|
|
|
|
|
var waitGroup sync.WaitGroup
|
|
|
|
errChan := make(chan error, 17)
|
|
|
|
|
|
|
|
args := []string{"run", "--rm", "busybox", "true"}
|
|
|
|
for i := 0; i < 17; i++ {
|
|
|
|
waitGroup.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer waitGroup.Done()
|
|
|
|
err := exec.Command(dockerBinary, args...).Run()
|
|
|
|
errChan <- err
|
|
|
|
}()
|
2014-07-04 05:12:21 +00:00
|
|
|
}
|
2015-04-27 19:12:23 +00:00
|
|
|
|
|
|
|
waitGroup.Wait()
|
|
|
|
close(errChan)
|
|
|
|
|
|
|
|
for err := range errChan {
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("%q failed with error: %v", strings.Join(args, " "), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2014-07-04 05:12:21 +00:00
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
|
|
|
events := strings.Split(out, "\n")
|
2014-10-06 14:26:55 +00:00
|
|
|
nEvents := len(events) - 1
|
|
|
|
if nEvents != 64 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("events should be limited to 64, but received %d", nEvents)
|
2014-07-04 05:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:54:59 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsContainerEvents(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "--rm", "busybox", "true")
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2014-08-11 05:54:59 +00:00
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2014-08-11 05:54:59 +00:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) < 4 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Missing expected event")
|
2014-08-11 05:54:59 +00:00
|
|
|
}
|
|
|
|
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" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be create, not %#v", createEvent)
|
2014-08-11 05:54:59 +00:00
|
|
|
}
|
|
|
|
if startEvent[len(startEvent)-1] != "start" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be start, not %#v", startEvent)
|
2014-08-11 05:54:59 +00:00
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be die, not %#v", dieEvent)
|
2014-08-11 05:54:59 +00:00
|
|
|
}
|
|
|
|
if destroyEvent[len(destroyEvent)-1] != "destroy" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be destroy, not %#v", destroyEvent)
|
2014-08-11 05:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-08-11 07:39:47 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "--rm", "busybox", "true")
|
2015-03-22 19:40:16 +00:00
|
|
|
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),
|
2015-04-18 16:46:47 +00:00
|
|
|
fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2015-03-22 19:40:16 +00:00
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2015-03-22 19:40:16 +00:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) < 4 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Missing expected event")
|
2015-03-22 19:40:16 +00:00
|
|
|
}
|
|
|
|
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" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be create, not %#v", createEvent)
|
2015-03-22 19:40:16 +00:00
|
|
|
}
|
|
|
|
if startEvent[len(startEvent)-1] != "start" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be start, not %#v", startEvent)
|
2015-03-22 19:40:16 +00:00
|
|
|
}
|
|
|
|
if dieEvent[len(dieEvent)-1] != "die" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be die, not %#v", dieEvent)
|
2015-03-22 19:40:16 +00:00
|
|
|
}
|
|
|
|
if destroyEvent[len(destroyEvent)-1] != "destroy" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should be destroy, not %#v", destroyEvent)
|
2015-03-22 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsImageUntagDelete(c *check.C) {
|
2014-08-11 07:39:47 +00:00
|
|
|
name := "testimageevents"
|
|
|
|
_, err := buildImage(name,
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER "docker"`,
|
|
|
|
true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-08-11 07:39:47 +00:00
|
|
|
}
|
|
|
|
if err := deleteImages(name); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-08-11 07:39:47 +00:00
|
|
|
}
|
2015-04-18 16:46:47 +00:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2014-08-11 07:39:47 +00:00
|
|
|
out, exitCode, err := runCommandWithOutput(eventsCmd)
|
|
|
|
if exitCode != 0 || err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events with exit code %d: %s", exitCode, err)
|
2014-08-11 07:39:47 +00:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
2015-02-19 21:33:23 +00:00
|
|
|
|
2014-08-11 07:39:47 +00:00
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) < 2 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Missing expected event")
|
2014-08-11 07:39:47 +00:00
|
|
|
}
|
|
|
|
untagEvent := strings.Fields(events[len(events)-2])
|
|
|
|
deleteEvent := strings.Fields(events[len(events)-1])
|
|
|
|
if untagEvent[len(untagEvent)-1] != "untag" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("untag should be untag, not %#v", untagEvent)
|
2014-08-11 07:39:47 +00:00
|
|
|
}
|
|
|
|
if deleteEvent[len(deleteEvent)-1] != "delete" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("delete should be delete, not %#v", deleteEvent)
|
2014-08-11 07:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-25 14:50:10 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsImagePull(c *check.C) {
|
|
|
|
since := daemonTime(c).Unix()
|
|
|
|
testRequires(c, Network)
|
2015-02-06 01:46:55 +00:00
|
|
|
|
2014-10-28 21:06:23 +00:00
|
|
|
pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
|
2014-10-22 13:48:02 +00:00
|
|
|
if out, _, err := runCommandWithOutput(pullCmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("pulling the hello-world image from has failed: %s, %v", out, err)
|
2014-10-22 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events",
|
|
|
|
fmt.Sprintf("--since=%d", since),
|
2015-04-18 16:46:47 +00:00
|
|
|
fmt.Sprintf("--until=%d", daemonTime(c).Unix()))
|
2014-10-22 13:48:02 +00:00
|
|
|
out, _, _ := runCommandWithOutput(eventsCmd)
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
event := strings.TrimSpace(events[len(events)-1])
|
|
|
|
|
2014-10-28 21:06:23 +00:00
|
|
|
if !strings.HasSuffix(event, "hello-world:latest: pull") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Missing pull event - got:%q", event)
|
2014-10-22 13:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsImageImport(c *check.C) {
|
|
|
|
since := daemonTime(c).Unix()
|
2014-10-22 13:48:02 +00:00
|
|
|
|
2015-04-18 12:58:57 +00:00
|
|
|
id := make(chan string)
|
|
|
|
eventImport := make(chan struct{})
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since", strconv.FormatInt(since, 10))
|
|
|
|
stdout, err := eventsCmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
err = eventsCmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
defer eventsCmd.Process.Kill()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
containerID := <-id
|
|
|
|
|
|
|
|
matchImport := regexp.MustCompile(containerID + `: import$`)
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
|
|
|
if matchImport.MatchString(scanner.Text()) {
|
|
|
|
close(eventImport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-02-14 10:27:06 +00:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-22 13:48:02 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("failed to create a container", out, err)
|
2014-10-22 13:48:02 +00:00
|
|
|
}
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-02-14 10:27:06 +00:00
|
|
|
|
|
|
|
out, _, err = runCommandPipelineWithOutput(
|
|
|
|
exec.Command(dockerBinary, "export", cleanedContainerID),
|
|
|
|
exec.Command(dockerBinary, "import", "-"),
|
|
|
|
)
|
2014-10-22 13:48:02 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Errorf("import failed with errors: %v, output: %q", err, out)
|
2014-10-22 13:48:02 +00:00
|
|
|
}
|
2015-04-18 12:58:57 +00:00
|
|
|
newContainerID := strings.TrimSpace(out)
|
|
|
|
id <- newContainerID
|
2014-10-22 13:48:02 +00:00
|
|
|
|
2015-04-18 12:58:57 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("failed to observe image import in timely fashion")
|
|
|
|
case <-eventImport:
|
|
|
|
// ignore, done
|
2014-10-22 13:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-20 19:46:48 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsFilters(c *check.C) {
|
2015-02-25 00:24:19 +00: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 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("event should match %s, got %#v, err: %v", match, eventFields, err)
|
2015-02-25 00:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-19 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
since := daemonTime(c).Unix()
|
2015-02-25 00:24:19 +00:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
|
2014-11-26 02:34:08 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2014-11-26 02:34:08 +00:00
|
|
|
}
|
2015-02-25 00:24:19 +00:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--rm", "busybox", "true"))
|
2014-11-26 02:34:08 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2014-11-26 02:34:08 +00:00
|
|
|
}
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", "event=die"))
|
2015-02-19 21:33:23 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events: %s", err)
|
2014-11-20 19:46:48 +00:00
|
|
|
}
|
2015-02-25 00:24:19 +00:00
|
|
|
parseEvents(out, "die")
|
2014-11-20 19:46:48 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", "event=die", "--filter", "event=start"))
|
2015-02-19 21:33:23 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events: %s", err)
|
2014-11-20 19:46:48 +00:00
|
|
|
}
|
2015-02-25 00:24:19 +00:00
|
|
|
parseEvents(out, "((die)|(start))")
|
|
|
|
|
|
|
|
// make sure we at least got 2 start events
|
|
|
|
count := strings.Count(out, "start")
|
2015-02-27 22:48:39 +00:00
|
|
|
if count < 2 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("should have had 2 start events but had %d, out: %s", count, out)
|
2014-11-20 19:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-02-09 21:02:53 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsFilterImageName(c *check.C) {
|
|
|
|
since := daemonTime(c).Unix()
|
2015-02-09 21:02:53 +00:00
|
|
|
|
2015-04-01 14:08:00 +00:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_1", "-d", "busybox:latest", "true"))
|
2015-02-09 21:02:53 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2015-02-09 21:02:53 +00:00
|
|
|
}
|
2015-04-06 13:21:18 +00:00
|
|
|
container1 := strings.TrimSpace(out)
|
2015-02-09 06:03:42 +00:00
|
|
|
|
2015-02-09 21:02:53 +00:00
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "container_2", "-d", "busybox", "true"))
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2015-02-09 21:02:53 +00:00
|
|
|
}
|
2015-04-06 13:21:18 +00:00
|
|
|
container2 := strings.TrimSpace(out)
|
2015-02-09 21:02:53 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
name := "busybox"
|
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", fmt.Sprintf("--since=%d", since), fmt.Sprintf("--until=%d", daemonTime(c).Unix()), "--filter", fmt.Sprintf("image=%s", name))
|
2015-04-01 14:08:00 +00:00
|
|
|
out, _, err = runCommandWithOutput(eventsCmd)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Failed to get events, error: %s(%s)", err, out)
|
2015-04-01 14:08:00 +00:00
|
|
|
}
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
if len(events) == 0 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected events but found none for the image busybox:latest")
|
2015-04-01 14:08:00 +00:00
|
|
|
}
|
|
|
|
count1 := 0
|
|
|
|
count2 := 0
|
|
|
|
|
|
|
|
for _, e := range events {
|
|
|
|
if strings.Contains(e, container1) {
|
|
|
|
count1++
|
|
|
|
} else if strings.Contains(e, container2) {
|
|
|
|
count2++
|
2015-02-09 21:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 14:08:00 +00:00
|
|
|
if count1 == 0 || count2 == 0 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected events from each container but got %d from %s and %d from %s", count1, container1, count2, container2)
|
2015-04-01 14:08:00 +00:00
|
|
|
}
|
2015-02-09 21:02:53 +00:00
|
|
|
|
2015-02-09 06:03:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsFilterContainer(c *check.C) {
|
|
|
|
since := fmt.Sprintf("%d", daemonTime(c).Unix())
|
2015-04-06 17:18:38 +00:00
|
|
|
nameID := make(map[string]string)
|
2015-02-09 06:03:42 +00:00
|
|
|
|
2015-04-06 17:18:38 +00:00
|
|
|
for _, name := range []string{"container_1", "container_2"} {
|
2015-04-23 18:43:08 +00:00
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", name, "busybox", "true"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Error: %v, Output: %s", err, out)
|
|
|
|
}
|
|
|
|
id, err := inspectField(name, "Id")
|
2015-02-09 06:03:42 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-02-09 06:03:42 +00:00
|
|
|
}
|
2015-04-23 18:43:08 +00:00
|
|
|
nameID[name] = id
|
2015-02-09 06:03:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
until := fmt.Sprintf("%d", daemonTime(c).Unix())
|
2015-02-09 06:03:42 +00:00
|
|
|
|
2015-04-06 17:18:38 +00: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 06:03:42 +00:00
|
|
|
|
2015-04-06 17:18:38 +00: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 06:03:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 17:18:38 +00: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 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-06 17:18:38 +00:00
|
|
|
}
|
2015-02-09 06:03:42 +00:00
|
|
|
|
2015-04-06 17:18:38 +00:00
|
|
|
events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
|
|
|
if err := checkEvents(ID, events); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-03-31 03:26:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 17:18:38 +00:00
|
|
|
// filter by ID's
|
|
|
|
eventsCmd = exec.Command(dockerBinary, "events", "--since", since, "--until", until, "--filter", "container="+ID)
|
|
|
|
out, _, err = runCommandWithOutput(eventsCmd)
|
2015-02-09 06:03:42 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-02-09 06:03:42 +00:00
|
|
|
}
|
2015-02-09 21:02:53 +00:00
|
|
|
|
2015-04-06 17:18:38 +00:00
|
|
|
events = strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
|
|
|
if err := checkEvents(ID, events); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-06 17:18:38 +00:00
|
|
|
}
|
2015-03-26 19:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-05 13:01:52 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestEventsStreaming(c *check.C) {
|
|
|
|
start := daemonTime(c).Unix()
|
2015-04-05 13:01:52 +00:00
|
|
|
|
|
|
|
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 20:39:02 +00:00
|
|
|
eventsCmd := exec.Command(dockerBinary, "events", "--since", strconv.FormatInt(start, 10))
|
2015-04-05 13:01:52 +00:00
|
|
|
stdout, err := eventsCmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-05 13:01:52 +00:00
|
|
|
}
|
|
|
|
err = eventsCmd.Start()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("failed to start 'docker events': %s", err)
|
2015-04-05 13:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("docker events had bad exit status: %s", err)
|
2015-04-05 13:01:52 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-01 14:08:00 +00:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox:latest", "true")
|
2015-04-05 13:01:52 +00:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2015-04-05 13:01:52 +00:00
|
|
|
}
|
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
|
|
|
id <- cleanedContainerID
|
|
|
|
|
|
|
|
select {
|
2015-04-06 20:40:56 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("failed to observe container create in timely fashion")
|
2015-04-05 13:01:52 +00:00
|
|
|
case <-eventCreate:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-04-06 20:40:56 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("failed to observe container start in timely fashion")
|
2015-04-05 13:01:52 +00:00
|
|
|
case <-eventStart:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-04-06 20:40:56 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("failed to observe container die in timely fashion")
|
2015-04-05 13:01:52 +00:00
|
|
|
case <-eventDie:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
rmCmd := exec.Command(dockerBinary, "rm", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(rmCmd)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2015-04-05 13:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-04-06 20:40:56 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("failed to observe container destroy in timely fashion")
|
2015-04-05 13:01:52 +00:00
|
|
|
case <-eventDestroy:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|