From 2794526c50fc4b1c6f5c8d62acf55f276d9211f3 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 14 Jan 2019 00:29:13 +0100 Subject: [PATCH] Make TestEventsFilterLabels less flaky This test sometimes failed because the number of events received did not match the expected number: FAIL: docker_cli_events_test.go:316: DockerSuite.TestEventsFilterLabels docker_cli_events_test.go:334: c.Assert(len(events), checker.Equals, 3) ... obtained int = 2 ... expected int = 3 This patch makes the test more stable, by: - use a wider range between `--since` and `--until`. These options were set so that the client detaches after events were received, but the actual range should not matter. Changing the range will cause more events to be returned, but we're specifically looking for the container ID's, so this should not make a difference for the actual test. - use `docker create` instead of `docker run` for the containers. the containers don't have to be running to trigger an event; using `create` speeds up the test. - check the exit code of the `docker create` to verify the containers were succesfully created. Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 0e15c02465c87c82908bcc45b0c1d6bd38f27c32) Signed-off-by: Sebastiaan van Stijn Upstream-commit: 26f7e0a8b08b2890d079d4083735c05377770ca8 Component: engine --- .../integration-cli/docker_cli_events_test.go | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/components/engine/integration-cli/docker_cli_events_test.go b/components/engine/integration-cli/docker_cli_events_test.go index b042faf916..ce3dec4b61 100644 --- a/components/engine/integration-cli/docker_cli_events_test.go +++ b/components/engine/integration-cli/docker_cli_events_test.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "os" "os/exec" + "strconv" "strings" "time" @@ -314,29 +315,38 @@ func (s *DockerSuite) TestEventsFilterImageName(c *check.C) { } func (s *DockerSuite) TestEventsFilterLabels(c *check.C) { - since := daemonUnixTime(c) + since := strconv.FormatUint(uint64(daemonTime(c).Unix()), 10) label := "io.docker.testing=foo" - out, _ := dockerCmd(c, "run", "-d", "-l", label, "busybox:latest", "true") + out, exit := dockerCmd(c, "create", "-l", label, "busybox") + c.Assert(exit, checker.Equals, 0) container1 := strings.TrimSpace(out) - out, _ = dockerCmd(c, "run", "-d", "busybox", "true") + out, exit = dockerCmd(c, "create", "busybox") + c.Assert(exit, checker.Equals, 0) container2 := strings.TrimSpace(out) + // fetch events with `--until`, so that the client detaches after a second + // instead of staying attached, waiting for more events to arrive. out, _ = dockerCmd( c, "events", "--since", since, - "--until", daemonUnixTime(c), - "--filter", fmt.Sprintf("label=%s", label)) + "--until", strconv.FormatUint(uint64(daemonTime(c).Add(time.Second).Unix()), 10), + "--filter", "label="+label, + ) events := strings.Split(strings.TrimSpace(out), "\n") - c.Assert(len(events), checker.Equals, 3) + c.Assert(len(events), checker.GreaterThan, 0) + var found bool for _, e := range events { - c.Assert(e, checker.Contains, container1) + if strings.Contains(e, container1) { + found = true + } c.Assert(e, checker.Not(checker.Contains), container2) } + c.Assert(found, checker.Equals, true) } func (s *DockerSuite) TestEventsFilterImageLabels(c *check.C) {