Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2019/01/14 08:29:13
Showing 1 changed files
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"io/ioutil"
10 10
 	"os"
11 11
 	"os/exec"
12
+	"strconv"
12 13
 	"strings"
13 14
 	"time"
14 15
 
... ...
@@ -314,29 +315,38 @@ func (s *DockerSuite) TestEventsFilterImageName(c *check.C) {
314 314
 }
315 315
 
316 316
 func (s *DockerSuite) TestEventsFilterLabels(c *check.C) {
317
-	since := daemonUnixTime(c)
317
+	since := strconv.FormatUint(uint64(daemonTime(c).Unix()), 10)
318 318
 	label := "io.docker.testing=foo"
319 319
 
320
-	out, _ := dockerCmd(c, "run", "-d", "-l", label, "busybox:latest", "true")
320
+	out, exit := dockerCmd(c, "create", "-l", label, "busybox")
321
+	c.Assert(exit, checker.Equals, 0)
321 322
 	container1 := strings.TrimSpace(out)
322 323
 
323
-	out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
324
+	out, exit = dockerCmd(c, "create", "busybox")
325
+	c.Assert(exit, checker.Equals, 0)
324 326
 	container2 := strings.TrimSpace(out)
325 327
 
328
+	// fetch events with `--until`, so that the client detaches after a second
329
+	// instead of staying attached, waiting for more events to arrive.
326 330
 	out, _ = dockerCmd(
327 331
 		c,
328 332
 		"events",
329 333
 		"--since", since,
330
-		"--until", daemonUnixTime(c),
331
-		"--filter", fmt.Sprintf("label=%s", label))
334
+		"--until", strconv.FormatUint(uint64(daemonTime(c).Add(time.Second).Unix()), 10),
335
+		"--filter", "label="+label,
336
+	)
332 337
 
333 338
 	events := strings.Split(strings.TrimSpace(out), "\n")
334
-	c.Assert(len(events), checker.Equals, 3)
339
+	c.Assert(len(events), checker.GreaterThan, 0)
335 340
 
341
+	var found bool
336 342
 	for _, e := range events {
337
-		c.Assert(e, checker.Contains, container1)
343
+		if strings.Contains(e, container1) {
344
+			found = true
345
+		}
338 346
 		c.Assert(e, checker.Not(checker.Contains), container2)
339 347
 	}
348
+	c.Assert(found, checker.Equals, true)
340 349
 }
341 350
 
342 351
 func (s *DockerSuite) TestEventsFilterImageLabels(c *check.C) {