Browse code

Refactor "init" version parsing, and add unit-test

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2017/03/29 20:44:14
Showing 2 changed files
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/docker/docker/api/types"
12 12
 	"github.com/docker/docker/dockerversion"
13 13
 	"github.com/docker/docker/pkg/sysinfo"
14
+	"github.com/pkg/errors"
14 15
 )
15 16
 
16 17
 // FillPlatformInfo fills the platform related info.
... ...
@@ -54,32 +55,37 @@ func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo)
54 54
 		v.RuncCommit.ID = "N/A"
55 55
 	}
56 56
 
57
-	v.InitCommit.Expected = dockerversion.InitCommitID
58 57
 	if rv, err := exec.Command(DefaultInitBinary, "--version").Output(); err == nil {
59
-		// examples of how Tini outputs version info:
60
-		//   "tini version 0.13.0 - git.949e6fa"
61
-		//   "tini version 0.13.2"
62
-		parts := strings.Split(strings.TrimSpace(string(rv)), " - ")
58
+		ver, err := parseInitVersion(string(rv))
63 59
 
64
-		v.InitCommit.ID = ""
65
-		if v.InitCommit.ID == "" && len(parts) >= 2 {
66
-			gitParts := strings.Split(parts[1], ".")
67
-			if len(gitParts) == 2 && gitParts[0] == "git" {
68
-				v.InitCommit.ID = gitParts[1]
69
-				v.InitCommit.Expected = dockerversion.InitCommitID[0:len(v.InitCommit.ID)]
70
-			}
71
-		}
72
-		if v.InitCommit.ID == "" && strings.HasPrefix(parts[0], "tini version") {
73
-			vs := strings.TrimPrefix(parts[0], "tini version ")
74
-			v.InitCommit.ID = "v" + vs
75
-		}
76
-
77
-		if v.InitCommit.ID == "" {
78
-			logrus.Warnf("failed to retrieve %s version: unknown output format: %s", DefaultInitBinary, string(rv))
79
-			v.InitCommit.ID = "N/A"
60
+		if err != nil {
61
+			logrus.Warnf("failed to retrieve %s version: %s", DefaultInitBinary, err)
80 62
 		}
63
+		v.InitCommit = ver
81 64
 	} else {
82
-		logrus.Warnf("failed to retrieve %s version", DefaultInitBinary)
65
+		logrus.Warnf("failed to retrieve %s version: %s", DefaultInitBinary, err)
83 66
 		v.InitCommit.ID = "N/A"
84 67
 	}
85 68
 }
69
+
70
+// parseInitVersion parses a Tini version string, and extracts the version.
71
+func parseInitVersion(v string) (types.Commit, error) {
72
+	version := types.Commit{ID: "", Expected: dockerversion.InitCommitID}
73
+	parts := strings.Split(strings.TrimSpace(v), " - ")
74
+
75
+	if len(parts) >= 2 {
76
+		gitParts := strings.Split(parts[1], ".")
77
+		if len(gitParts) == 2 && gitParts[0] == "git" {
78
+			version.ID = gitParts[1]
79
+			version.Expected = dockerversion.InitCommitID[0:len(version.ID)]
80
+		}
81
+	}
82
+	if version.ID == "" && strings.HasPrefix(parts[0], "tini version ") {
83
+		version.ID = "v" + strings.TrimPrefix(parts[0], "tini version ")
84
+	}
85
+	if version.ID == "" {
86
+		version.ID = "N/A"
87
+		return version, errors.Errorf("unknown output format: %s", v)
88
+	}
89
+	return version, nil
90
+}
86 91
new file mode 100644
... ...
@@ -0,0 +1,52 @@
0
+// +build !windows
1
+
2
+package daemon
3
+
4
+import (
5
+	"testing"
6
+
7
+	"github.com/docker/docker/api/types"
8
+	"github.com/docker/docker/dockerversion"
9
+	"github.com/stretchr/testify/assert"
10
+)
11
+
12
+func TestParseInitVersion(t *testing.T) {
13
+	tests := []struct {
14
+		version string
15
+		result  types.Commit
16
+		invalid bool
17
+	}{
18
+		{
19
+			version: "tini version 0.13.0 - git.949e6fa",
20
+			result:  types.Commit{ID: "949e6fa", Expected: dockerversion.InitCommitID[0:7]},
21
+		}, {
22
+			version: "tini version 0.13.0\n",
23
+			result:  types.Commit{ID: "v0.13.0", Expected: dockerversion.InitCommitID},
24
+		}, {
25
+			version: "tini version 0.13.2",
26
+			result:  types.Commit{ID: "v0.13.2", Expected: dockerversion.InitCommitID},
27
+		}, {
28
+			version: "tini version0.13.2",
29
+			result:  types.Commit{ID: "N/A", Expected: dockerversion.InitCommitID},
30
+			invalid: true,
31
+		}, {
32
+			version: "",
33
+			result:  types.Commit{ID: "N/A", Expected: dockerversion.InitCommitID},
34
+			invalid: true,
35
+		}, {
36
+			version: "hello world",
37
+			result:  types.Commit{ID: "N/A", Expected: dockerversion.InitCommitID},
38
+			invalid: true,
39
+		},
40
+	}
41
+
42
+	for _, test := range tests {
43
+		ver, err := parseInitVersion(string(test.version))
44
+		if test.invalid {
45
+			assert.Error(t, err)
46
+		} else {
47
+			assert.NoError(t, err)
48
+		}
49
+		assert.Equal(t, test.result, ver)
50
+	}
51
+}