Browse code

Optimize slow bottleneck tests of TestDaemonStartWithDaemonCommand

* Remvoe integration test of TestDaemonStartWithDaemonCommand
* Rewrite as unit test

Related issue #19425

Signed-off-by: Wen Cheng Ma <wenchma@cn.ibm.com>

Wen Cheng Ma authored on 2016/02/19 12:02:45
Showing 2 changed files
... ...
@@ -34,6 +34,53 @@ func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
34 34
 	}
35 35
 }
36 36
 
37
+func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) {
38
+	c := &daemon.Config{}
39
+	common := &cli.CommonFlags{
40
+		Debug:    true,
41
+		LogLevel: "info",
42
+	}
43
+
44
+	f, err := ioutil.TempFile("", "docker-config-")
45
+	if err != nil {
46
+		t.Fatal(err)
47
+	}
48
+
49
+	configFile := f.Name()
50
+	f.Write([]byte(`{"log-opts": {"max-size": "1k"}}`))
51
+	f.Close()
52
+
53
+	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
54
+	flags.String([]string{daemonConfigFileFlag}, "", "")
55
+	flags.BoolVar(&c.EnableSelinuxSupport, []string{"-selinux-enabled"}, true, "")
56
+	flags.StringVar(&c.LogConfig.Type, []string{"-log-driver"}, "json-file", "")
57
+	flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
58
+	flags.Set(daemonConfigFileFlag, configFile)
59
+
60
+	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
61
+	if err != nil {
62
+		t.Fatal(err)
63
+	}
64
+	if loadedConfig == nil {
65
+		t.Fatalf("expected configuration %v, got nil", c)
66
+	}
67
+	if !loadedConfig.Debug {
68
+		t.Fatalf("expected debug mode, got false")
69
+	}
70
+	if loadedConfig.LogLevel != "info" {
71
+		t.Fatalf("expected info log level, got %v", loadedConfig.LogLevel)
72
+	}
73
+	if !loadedConfig.EnableSelinuxSupport {
74
+		t.Fatalf("expected enabled selinux support, got disabled")
75
+	}
76
+	if loadedConfig.LogConfig.Type != "json-file" {
77
+		t.Fatalf("expected LogConfig type json-file, got %v", loadedConfig.LogConfig.Type)
78
+	}
79
+	if maxSize := loadedConfig.LogConfig.Config["max-size"]; maxSize != "1k" {
80
+		t.Fatalf("expected log max-size `1k`, got %s", maxSize)
81
+	}
82
+}
83
+
37 84
 func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
38 85
 	c := &daemon.Config{}
39 86
 	common := &cli.CommonFlags{
... ...
@@ -432,69 +432,6 @@ func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
432 432
 	c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
433 433
 }
434 434
 
435
-func (s *DockerSuite) TestDaemonStartWithDaemonCommand(c *check.C) {
436
-
437
-	type kind int
438
-
439
-	const (
440
-		common kind = iota
441
-		daemon
442
-	)
443
-
444
-	var flags = []map[kind][]string{
445
-		{common: {"-l", "info"}, daemon: {"--selinux-enabled"}},
446
-		{common: {"-D"}, daemon: {"--selinux-enabled", "-r"}},
447
-		{common: {"-D"}, daemon: {"--restart"}},
448
-		{common: {"--debug"}, daemon: {"--log-driver=json-file", "--log-opt=max-size=1k"}},
449
-	}
450
-
451
-	var invalidGlobalFlags = [][]string{
452
-		//Invalid because you cannot pass daemon flags as global flags.
453
-		{"--selinux-enabled", "-l", "info"},
454
-		{"-D", "-r"},
455
-		{"--config", "/tmp"},
456
-	}
457
-
458
-	// `docker daemon -l info --selinux-enabled`
459
-	// should NOT error out
460
-	for _, f := range flags {
461
-		d := NewDaemon(c)
462
-		args := append(f[common], f[daemon]...)
463
-		if err := d.Start(args...); err != nil {
464
-			c.Fatalf("Daemon should have started successfully with %v: %v", args, err)
465
-		}
466
-		d.Stop()
467
-	}
468
-
469
-	// `docker -l info daemon --selinux-enabled`
470
-	// should error out
471
-	for _, f := range flags {
472
-		d := NewDaemon(c)
473
-		d.GlobalFlags = f[common]
474
-		if err := d.Start(f[daemon]...); err == nil {
475
-			d.Stop()
476
-			c.Fatalf("Daemon should have failed to start with docker %v daemon %v", d.GlobalFlags, f[daemon])
477
-		}
478
-	}
479
-
480
-	for _, f := range invalidGlobalFlags {
481
-		cmd := exec.Command(dockerBinary, append(f, "daemon")...)
482
-		errch := make(chan error)
483
-		var err error
484
-		go func() {
485
-			errch <- cmd.Run()
486
-		}()
487
-		select {
488
-		case <-time.After(time.Second):
489
-			cmd.Process.Kill()
490
-		case err = <-errch:
491
-		}
492
-		if err == nil {
493
-			c.Fatalf("Daemon should have failed to start with docker %v daemon", f)
494
-		}
495
-	}
496
-}
497
-
498 435
 func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
499 436
 	if err := s.d.Start("--log-level=debug"); err != nil {
500 437
 		c.Fatal(err)