Browse code

integration-cli: TestDaemonEvents*: don't rely on CLI output format

Running these tests with a different version of the CLI caused
some failures because the tests relied on the CLI's output format.

Although these tests should be rewritten to use the API directly,
in the meantime this makes them slightly more reliable.

Signed-off-by: Tibor Vass <tibor@docker.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/10/08 04:21:54
Showing 1 changed files
... ...
@@ -396,41 +396,30 @@ func (s *DockerDaemonSuite) TestDaemonEvents(c *testing.T) {
396 396
 	defer os.Remove(configFilePath)
397 397
 
398 398
 	daemonConfig := `{"labels":["foo=bar"]}`
399
-	fmt.Fprintf(configFile, "%s", daemonConfig)
400
-	configFile.Close()
401
-	s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath))
402
-
403
-	// Get daemon ID
404
-	out, err := s.d.Cmd("info")
399
+	_, err = configFile.Write([]byte(daemonConfig))
400
+	assert.NilError(c, configFile.Close())
405 401
 	assert.NilError(c, err)
406
-	daemonID := ""
407
-	daemonName := ""
408
-	for _, line := range strings.Split(out, "\n") {
409
-		if strings.HasPrefix(line, "ID: ") {
410
-			daemonID = strings.TrimPrefix(line, "ID: ")
411
-		} else if strings.HasPrefix(line, "Name: ") {
412
-			daemonName = strings.TrimPrefix(line, "Name: ")
413
-		}
414
-	}
415
-	assert.Assert(c, daemonID != "")
402
+	s.d.Start(c, "--config-file="+configFilePath)
403
+
404
+	info := s.d.Info(c)
416 405
 
417 406
 	configFile, err = os.Create(configFilePath)
418 407
 	assert.NilError(c, err)
419 408
 	daemonConfig = `{"max-concurrent-downloads":1,"labels":["bar=foo"], "shutdown-timeout": 10}`
420
-	fmt.Fprintf(configFile, "%s", daemonConfig)
421
-	configFile.Close()
409
+	_, err = fmt.Fprintf(configFile, "%s", daemonConfig)
410
+	assert.NilError(c, configFile.Close())
411
+	assert.NilError(c, err)
422 412
 
423 413
 	assert.NilError(c, s.d.Signal(unix.SIGHUP))
424
-
425 414
 	time.Sleep(3 * time.Second)
426 415
 
427
-	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c))
416
+	out, err := s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c))
428 417
 	assert.NilError(c, err)
429 418
 
430 419
 	// only check for values known (daemon ID/name) or explicitly set above,
431 420
 	// otherwise just check for names being present.
432 421
 	expectedSubstrings := []string{
433
-		" daemon reload " + daemonID + " ",
422
+		" daemon reload " + info.ID + " ",
434 423
 		"(allow-nondistributable-artifacts=[",
435 424
 		" cluster-advertise=, ",
436 425
 		" cluster-store=, ",
... ...
@@ -444,7 +433,7 @@ func (s *DockerDaemonSuite) TestDaemonEvents(c *testing.T) {
444 444
 		" live-restore=",
445 445
 		" max-concurrent-downloads=1, ",
446 446
 		" max-concurrent-uploads=5, ",
447
-		" name=" + daemonName,
447
+		" name=" + info.Name,
448 448
 		" registry-mirrors=[",
449 449
 		" runtimes=",
450 450
 		" shutdown-timeout=10)",
... ...
@@ -464,44 +453,33 @@ func (s *DockerDaemonSuite) TestDaemonEventsWithFilters(c *testing.T) {
464 464
 	defer os.Remove(configFilePath)
465 465
 
466 466
 	daemonConfig := `{"labels":["foo=bar"]}`
467
-	fmt.Fprintf(configFile, "%s", daemonConfig)
468
-	configFile.Close()
469
-	s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath))
470
-
471
-	// Get daemon ID
472
-	out, err := s.d.Cmd("info")
467
+	_, err = configFile.Write([]byte(daemonConfig))
468
+	assert.NilError(c, configFile.Close())
473 469
 	assert.NilError(c, err)
474
-	daemonID := ""
475
-	daemonName := ""
476
-	for _, line := range strings.Split(out, "\n") {
477
-		if strings.HasPrefix(line, "ID: ") {
478
-			daemonID = strings.TrimPrefix(line, "ID: ")
479
-		} else if strings.HasPrefix(line, "Name: ") {
480
-			daemonName = strings.TrimPrefix(line, "Name: ")
481
-		}
482
-	}
483
-	assert.Assert(c, daemonID != "")
484
-	assert.NilError(c, s.d.Signal(unix.SIGHUP))
470
+	s.d.Start(c, "--config-file="+configFilePath)
471
+
472
+	info := s.d.Info(c)
485 473
 
474
+	assert.NilError(c, s.d.Signal(unix.SIGHUP))
486 475
 	time.Sleep(3 * time.Second)
487 476
 
488
-	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", daemonID))
477
+	out, err := s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", info.ID))
489 478
 	assert.NilError(c, err)
490
-	assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", daemonID)))
479
+	assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID)))
491 480
 
492
-	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", daemonName))
481
+	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", info.ID))
493 482
 	assert.NilError(c, err)
494
-	assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", daemonID)))
483
+	assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID)))
495 484
 
496 485
 	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "daemon=foo")
497 486
 	assert.NilError(c, err)
498
-	assert.Assert(c, !strings.Contains(out, fmt.Sprintf("daemon reload %s", daemonID)))
487
+	assert.Assert(c, !strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID)))
499 488
 
500 489
 	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=daemon")
501 490
 	assert.NilError(c, err)
502
-	assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", daemonID)))
491
+	assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID)))
503 492
 
504 493
 	out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=container")
505 494
 	assert.NilError(c, err)
506
-	assert.Assert(c, !strings.Contains(out, fmt.Sprintf("daemon reload %s", daemonID)))
495
+	assert.Assert(c, !strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID)))
507 496
 }