Browse code

Fix missing Init Binary in docker info output

- Moved DefaultInitBinary from daemon/daemon.go to
daemon/config/config.go since it's a daemon config and is referred in
config package files.
- Added condition in GetInitPath to check for any explicitly configured
DefaultInitBinary. If not, the default value of DefaultInitBinary is
returned.
- Changed all references of DefaultInitBinary to refer to the variable
from new location.
- Added TestCommonUnixGetInitPath to test for the various values of
GetInitPath.

Fixes #32314

Signed-off-by: Sunny Gogoi <indiasuny000@gmail.com>

Sunny Gogoi authored on 2017/04/10 18:25:15
Showing 6 changed files
... ...
@@ -41,6 +41,8 @@ const (
41 41
 	DefaultNetworkMtu = 1500
42 42
 	// DisableNetworkBridge is the default value of the option to disable network bridge
43 43
 	DisableNetworkBridge = "none"
44
+	// DefaultInitBinary is the name of the default init binary
45
+	DefaultInitBinary = "docker-init"
44 46
 )
45 47
 
46 48
 // flatOptions contains configuration keys
... ...
@@ -66,5 +66,8 @@ func (conf *Config) GetInitPath() string {
66 66
 	if conf.InitPath != "" {
67 67
 		return conf.InitPath
68 68
 	}
69
-	return conf.DefaultInitBinary
69
+	if conf.DefaultInitBinary != "" {
70
+		return conf.DefaultInitBinary
71
+	}
72
+	return DefaultInitBinary
70 73
 }
... ...
@@ -41,3 +41,44 @@ func TestCommonUnixValidateConfigurationErrors(t *testing.T) {
41 41
 		}
42 42
 	}
43 43
 }
44
+
45
+func TestCommonUnixGetInitPath(t *testing.T) {
46
+	testCases := []struct {
47
+		config           *Config
48
+		expectedInitPath string
49
+	}{
50
+		{
51
+			config: &Config{
52
+				InitPath: "some-init-path",
53
+			},
54
+			expectedInitPath: "some-init-path",
55
+		},
56
+		{
57
+			config: &Config{
58
+				CommonUnixConfig: CommonUnixConfig{
59
+					DefaultInitBinary: "foo-init-bin",
60
+				},
61
+			},
62
+			expectedInitPath: "foo-init-bin",
63
+		},
64
+		{
65
+			config: &Config{
66
+				InitPath: "init-path-A",
67
+				CommonUnixConfig: CommonUnixConfig{
68
+					DefaultInitBinary: "init-path-B",
69
+				},
70
+			},
71
+			expectedInitPath: "init-path-A",
72
+		},
73
+		{
74
+			config:           &Config{},
75
+			expectedInitPath: "docker-init",
76
+		},
77
+	}
78
+	for _, tc := range testCases {
79
+		initPath := tc.config.GetInitPath()
80
+		if initPath != tc.expectedInitPath {
81
+			t.Fatalf("expected initPath to be %v, got %v", tc.expectedInitPath, initPath)
82
+		}
83
+	}
84
+}
... ...
@@ -65,9 +65,6 @@ var (
65 65
 	// containerd if none is specified
66 66
 	DefaultRuntimeBinary = "docker-runc"
67 67
 
68
-	// DefaultInitBinary is the name of the default init binary
69
-	DefaultInitBinary = "docker-init"
70
-
71 68
 	errSystemNotSupported = errors.New("The Docker daemon is not supported on this platform.")
72 69
 )
73 70
 
... ...
@@ -9,6 +9,7 @@ import (
9 9
 
10 10
 	"github.com/Sirupsen/logrus"
11 11
 	"github.com/docker/docker/api/types"
12
+	daemonconfig "github.com/docker/docker/daemon/config"
12 13
 	"github.com/docker/docker/dockerversion"
13 14
 	"github.com/docker/docker/pkg/sysinfo"
14 15
 	"github.com/pkg/errors"
... ...
@@ -55,15 +56,15 @@ func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo)
55 55
 		v.RuncCommit.ID = "N/A"
56 56
 	}
57 57
 
58
-	if rv, err := exec.Command(DefaultInitBinary, "--version").Output(); err == nil {
58
+	if rv, err := exec.Command(daemonconfig.DefaultInitBinary, "--version").Output(); err == nil {
59 59
 		ver, err := parseInitVersion(string(rv))
60 60
 
61 61
 		if err != nil {
62
-			logrus.Warnf("failed to retrieve %s version: %s", DefaultInitBinary, err)
62
+			logrus.Warnf("failed to retrieve %s version: %s", daemonconfig.DefaultInitBinary, err)
63 63
 		}
64 64
 		v.InitCommit = ver
65 65
 	} else {
66
-		logrus.Warnf("failed to retrieve %s version: %s", DefaultInitBinary, err)
66
+		logrus.Warnf("failed to retrieve %s version: %s", daemonconfig.DefaultInitBinary, err)
67 67
 		v.InitCommit.ID = "N/A"
68 68
 	}
69 69
 }
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	containertypes "github.com/docker/docker/api/types/container"
16 16
 	"github.com/docker/docker/container"
17 17
 	"github.com/docker/docker/daemon/caps"
18
+	daemonconfig "github.com/docker/docker/daemon/config"
18 19
 	"github.com/docker/docker/oci"
19 20
 	"github.com/docker/docker/pkg/idtools"
20 21
 	"github.com/docker/docker/pkg/mount"
... ...
@@ -624,7 +625,7 @@ func (daemon *Daemon) populateCommonSpec(s *specs.Spec, c *container.Container)
624 624
 			s.Process.Args = append([]string{"/dev/init", "--", c.Path}, c.Args...)
625 625
 			var path string
626 626
 			if daemon.configStore.InitPath == "" && c.HostConfig.InitPath == "" {
627
-				path, err = exec.LookPath(DefaultInitBinary)
627
+				path, err = exec.LookPath(daemonconfig.DefaultInitBinary)
628 628
 				if err != nil {
629 629
 					return err
630 630
 				}