Browse code

use of checkers on Integration test

Part of #16756

Use c.Assert instead of condition judgement in
integration-cli/docker_cli_start_test.go

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>

Zhang Wei authored on 2015/10/11 16:19:28
Showing 1 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"strings"
6 6
 	"time"
7 7
 
8
+	"github.com/docker/docker/pkg/integration/checker"
8 9
 	"github.com/go-check/check"
9 10
 )
10 11
 
... ...
@@ -15,9 +16,9 @@ func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
15 15
 	dockerCmd(c, "wait", "test")
16 16
 
17 17
 	// Expect this to fail because the above container is stopped, this is what we want
18
-	if _, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil {
19
-		c.Fatal("Expected error but got none")
20
-	}
18
+	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox")
19
+	// err shouldn't be nil because container test2 try to link to stopped container
20
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
21 21
 
22 22
 	ch := make(chan error)
23 23
 	go func() {
... ...
@@ -47,12 +48,10 @@ func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
47 47
 	dockerCmd(c, "wait", out)
48 48
 
49 49
 	startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
50
-	if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
51
-		c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
52
-	}
53
-	if exitCode != 1 {
54
-		c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
55
-	}
50
+	// start command should fail
51
+	c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
52
+	// start -a did not respond with proper exit code
53
+	c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
56 54
 
57 55
 }
58 56
 
... ...
@@ -65,9 +64,8 @@ func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
65 65
 	dockerCmd(c, "wait", name)
66 66
 
67 67
 	startOut, _ := dockerCmd(c, "start", "-a", name)
68
-	if expected := "test\n"; startOut != expected {
69
-		c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
70
-	}
68
+	// start -a produced unexpected output
69
+	c.Assert(startOut, checker.Equals, "test\n")
71 70
 }
72 71
 
73 72
 func (s *DockerSuite) TestStartRecordError(c *check.C) {
... ...
@@ -75,32 +73,26 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
75 75
 	// when container runs successfully, we should not have state.Error
76 76
 	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
77 77
 	stateErr, err := inspectField("test", "State.Error")
78
-	c.Assert(err, check.IsNil)
79
-	if stateErr != "" {
80
-		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
81
-	}
78
+	c.Assert(err, checker.IsNil, check.Commentf("stateErr: %s", stateErr))
79
+	// Expected to not have state error
80
+	c.Assert(stateErr, checker.Equals, "")
82 81
 
83 82
 	// Expect this to fail and records error because of ports conflict
84 83
 	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
85
-	if err == nil {
86
-		c.Fatalf("Expected error but got none, output %q", out)
87
-	}
84
+	// err shouldn't be nil because docker run will fail
85
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
88 86
 
89 87
 	stateErr, err = inspectField("test2", "State.Error")
90
-	c.Assert(err, check.IsNil)
91
-	expected := "port is already allocated"
92
-	if stateErr == "" || !strings.Contains(stateErr, expected) {
93
-		c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
94
-	}
88
+	c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
89
+	c.Assert(stateErr, checker.Contains, "port is already allocated")
95 90
 
96 91
 	// Expect the conflict to be resolved when we stop the initial container
97 92
 	dockerCmd(c, "stop", "test")
98 93
 	dockerCmd(c, "start", "test2")
99 94
 	stateErr, err = inspectField("test2", "State.Error")
100
-	c.Assert(err, check.IsNil)
101
-	if stateErr != "" {
102
-		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
103
-	}
95
+	c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
96
+	// Expected to not have state error but got one
97
+	c.Assert(stateErr, checker.Equals, "")
104 98
 }
105 99
 
106 100
 func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
... ...
@@ -111,9 +103,11 @@ func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
111 111
 
112 112
 	dockerCmd(c, "pause", "testing")
113 113
 
114
-	if out, _, err := dockerCmdWithError("start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
115
-		c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
116
-	}
114
+	out, _, err := dockerCmdWithError("start", "testing")
115
+	// an error should have been shown that you cannot start paused container
116
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
117
+	// an error should have been shown that you cannot start paused container
118
+	c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
117 119
 }
118 120
 
119 121
 func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
... ...
@@ -129,25 +123,24 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
129 129
 	dockerCmd(c, "stop", "parent")
130 130
 
131 131
 	out, err := inspectField("parent", "State.Running")
132
-	c.Assert(err, check.IsNil)
133
-	if out != "false" {
134
-		c.Fatal("Container should be stopped")
135
-	}
132
+	c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
133
+	// Container should be stopped
134
+	c.Assert(out, checker.Equals, "false")
136 135
 
137 136
 	// start all the three containers, container `child_first` start first which should be failed
138 137
 	// container 'parent' start second and then start container 'child_second'
139 138
 	out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
140
-	if !strings.Contains(out, "Cannot start container child_first") || err == nil {
141
-		c.Fatal("Expected error but got none")
142
-	}
139
+	// err shouldn't be nil because start will fail
140
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
141
+	// output does not correspond to what was expected
142
+	c.Assert(out, checker.Contains, "Cannot start container child_first")
143 143
 
144 144
 	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
145 145
 		out, err := inspectField(container, "State.Running")
146
-		c.Assert(err, check.IsNil)
147
-		if out != expected {
148
-			c.Fatal("Container running state wrong")
149
-		}
150
-
146
+		// Container running state wrong
147
+		c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
148
+		// Container running state wrong
149
+		c.Assert(out, checker.Equals, expected)
151 150
 	}
152 151
 }
153 152
 
... ...
@@ -166,19 +159,18 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
166 166
 	// test start and attach multiple containers at once, expected error
167 167
 	for _, option := range []string{"-a", "-i", "-ai"} {
168 168
 		out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
169
-		if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
170
-			c.Fatal("Expected error but got none")
171
-		}
169
+		// err shouldn't be nil because start will fail
170
+		c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
171
+		// output does not correspond to what was expected
172
+		c.Assert(out, checker.Contains, "You cannot start and attach multiple containers at once.")
172 173
 	}
173 174
 
174 175
 	// confirm the state of all the containers be stopped
175 176
 	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
176 177
 		out, err := inspectField(container, "State.Running")
177
-		if err != nil {
178
-			c.Fatal(out, err)
179
-		}
180
-		if out != expected {
181
-			c.Fatal("Container running state wrong")
182
-		}
178
+		// Container running state wrong
179
+		c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
180
+		// Container running state wrong
181
+		c.Assert(out, checker.Equals, expected)
183 182
 	}
184 183
 }