From 687ef0056204c487aa1a2aeb283e8bda37be74a1 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 7 Dec 2015 08:33:16 -0500 Subject: [PATCH] Add tests for docker events -f container. Signed-off-by: Lei Jitang (cherry picked from commit 531ecf59f5bd92b12d4548617ca7bf179c8179a3) --- .../docker_cli_events_unix_test.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/integration-cli/docker_cli_events_unix_test.go b/integration-cli/docker_cli_events_unix_test.go index c65394349e613..7935c503929f6 100644 --- a/integration-cli/docker_cli_events_unix_test.go +++ b/integration-cli/docker_cli_events_unix_test.go @@ -114,3 +114,48 @@ func (s *DockerSuite) TestEventsOOMDisableTrue(c *check.C) { c.Assert(strings.TrimSpace(out), checker.Equals, "running", check.Commentf("container should be still running")) } } + +// #18453 +func (s *DockerSuite) TestEventsContainerFilter(c *check.C) { + testRequires(c, DaemonIsLinux) + out, _ := dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top") + c1 := strings.TrimSpace(out) + waitRun(c1) + out, _ = dockerCmd(c, "run", "--name=bar", "-d", "busybox", "top") + c2 := strings.TrimSpace(out) + waitRun(c2) + out, _ = dockerCmd(c, "events", "-f", "container=foo", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix())) + c.Assert(out, checker.Contains, c1, check.Commentf("Missing event of container (foo)")) + c.Assert(out, checker.Not(checker.Contains), c2, check.Commentf("Should not contain event of container (bar)")) +} + +// #18453 +func (s *DockerSuite) TestEventsContainerFilterBeforeCreate(c *check.C) { + testRequires(c, DaemonIsLinux) + var ( + out string + ch chan struct{} + ) + ch = make(chan struct{}) + + // calculate the time it takes to create and start a container and sleep 2 seconds + // this is to make sure the docker event will recevie the event of container + since := daemonTime(c).Unix() + id, _ := dockerCmd(c, "run", "-d", "busybox", "top") + cID := strings.TrimSpace(id) + waitRun(cID) + time.Sleep(2 * time.Second) + duration := daemonTime(c).Unix() - since + + go func() { + out, _ = dockerCmd(c, "events", "-f", "container=foo", "--since=0", fmt.Sprintf("--until=%d", daemonTime(c).Unix()+2*duration)) + close(ch) + }() + // Sleep 2 second to wait docker event to start + time.Sleep(2 * time.Second) + id, _ = dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top") + cID = strings.TrimSpace(id) + waitRun(cID) + <-ch + c.Assert(out, checker.Contains, cID, check.Commentf("Missing event of container (foo)")) +}