Browse code

Merge pull request #16977 from mountkin/refactor-test

refactor integration test to use checkers

Vincent Demeester authored on 2015/10/17 07:22:03
Showing 2 changed files
... ...
@@ -22,9 +22,8 @@ func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
22 22
 	endGroup.Add(3)
23 23
 	startGroup.Add(3)
24 24
 
25
-	if err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done"); err != nil {
26
-		c.Fatal(err)
27
-	}
25
+	err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
26
+	c.Assert(err, check.IsNil)
28 27
 
29 28
 	startDone := make(chan struct{})
30 29
 	endDone := make(chan struct{})
... ...
@@ -84,7 +83,6 @@ func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
84 84
 	case <-time.After(attachWait):
85 85
 		c.Fatalf("Attaches did not finish properly")
86 86
 	}
87
-
88 87
 }
89 88
 
90 89
 func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
... ...
@@ -94,13 +92,6 @@ func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
94 94
 	id := strings.TrimSpace(out)
95 95
 	c.Assert(waitRun(id), check.IsNil)
96 96
 
97
-	defer func() {
98
-		cmd := exec.Command(dockerBinary, "kill", id)
99
-		if out, _, err := runCommandWithOutput(cmd); err != nil {
100
-			c.Fatalf("failed to kill container: %v (%v)", out, err)
101
-		}
102
-	}()
103
-
104 97
 	done := make(chan error)
105 98
 	go func() {
106 99
 		defer close(done)
... ...
@@ -141,37 +132,21 @@ func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
141 141
 	}
142 142
 	defer stdin.Close()
143 143
 	stdout, err := cmd.StdoutPipe()
144
-	if err != nil {
145
-		c.Fatal(err)
146
-	}
144
+	c.Assert(err, check.IsNil)
147 145
 	defer stdout.Close()
148
-	if err := cmd.Start(); err != nil {
149
-		c.Fatal(err)
150
-	}
146
+	c.Assert(cmd.Start(), check.IsNil)
151 147
 	defer cmd.Process.Kill()
152 148
 
153
-	if _, err := stdin.Write([]byte("hello\n")); err != nil {
154
-		c.Fatal(err)
155
-	}
149
+	_, err = stdin.Write([]byte("hello\n"))
150
+	c.Assert(err, check.IsNil)
156 151
 	out, err = bufio.NewReader(stdout).ReadString('\n')
157
-	if err != nil {
158
-		c.Fatal(err)
159
-	}
160
-	if strings.TrimSpace(out) != "hello" {
161
-		c.Fatalf("expected 'hello', got %q", out)
162
-	}
152
+	c.Assert(err, check.IsNil)
153
+	c.Assert(strings.TrimSpace(out), check.Equals, "hello")
163 154
 
164
-	if err := stdin.Close(); err != nil {
165
-		c.Fatal(err)
166
-	}
155
+	c.Assert(stdin.Close(), check.IsNil)
167 156
 
168 157
 	// Expect container to still be running after stdin is closed
169 158
 	running, err := inspectField(id, "State.Running")
170
-	if err != nil {
171
-		c.Fatal(err)
172
-	}
173
-	if running != "true" {
174
-		c.Fatal("expected container to still be running")
175
-	}
176
-
159
+	c.Assert(err, check.IsNil)
160
+	c.Assert(running, check.Equals, "true")
177 161
 }
... ...
@@ -57,21 +57,15 @@ type Daemon struct {
57 57
 // The daemon will not automatically start.
58 58
 func NewDaemon(c *check.C) *Daemon {
59 59
 	dest := os.Getenv("DEST")
60
-	if dest == "" {
61
-		c.Fatal("Please set the DEST environment variable")
62
-	}
60
+	c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
63 61
 
64 62
 	id := fmt.Sprintf("d%d", time.Now().UnixNano()%100000000)
65 63
 	dir := filepath.Join(dest, id)
66 64
 	daemonFolder, err := filepath.Abs(dir)
67
-	if err != nil {
68
-		c.Fatalf("Could not make %q an absolute path: %v", dir, err)
69
-	}
65
+	c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
70 66
 	daemonRoot := filepath.Join(daemonFolder, "root")
71 67
 
72
-	if err := os.MkdirAll(daemonRoot, 0755); err != nil {
73
-		c.Fatalf("Could not create daemon root %q: %v", dir, err)
74
-	}
68
+	c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
75 69
 
76 70
 	userlandProxy := true
77 71
 	if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
... ...
@@ -96,9 +90,7 @@ func NewDaemon(c *check.C) *Daemon {
96 96
 // You can specify additional daemon flags.
97 97
 func (d *Daemon) Start(arg ...string) error {
98 98
 	dockerBinary, err := exec.LookPath(dockerBinary)
99
-	if err != nil {
100
-		d.c.Fatalf("[%s] could not find docker binary in $PATH: %v", d.id, err)
101
-	}
99
+	d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not find docker binary in $PATH", d.id))
102 100
 
103 101
 	args := append(d.GlobalFlags,
104 102
 		d.Command,
... ...
@@ -136,9 +128,7 @@ func (d *Daemon) Start(arg ...string) error {
136 136
 	d.cmd = exec.Command(dockerBinary, args...)
137 137
 
138 138
 	d.logFile, err = os.OpenFile(filepath.Join(d.folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
139
-	if err != nil {
140
-		d.c.Fatalf("[%s] Could not create %s/docker.log: %v", d.id, d.folder, err)
141
-	}
139
+	d.c.Assert(err, check.IsNil, check.Commentf("[%s] Could not create %s/docker.log", d.id, d.folder))
142 140
 
143 141
 	d.cmd.Stdout = d.logFile
144 142
 	d.cmd.Stderr = d.logFile
... ...
@@ -187,9 +177,7 @@ func (d *Daemon) Start(arg ...string) error {
187 187
 			defer client.Close()
188 188
 
189 189
 			req, err := http.NewRequest("GET", "/_ping", nil)
190
-			if err != nil {
191
-				d.c.Fatalf("[%s] could not create new request: %v", d.id, err)
192
-			}
190
+			d.c.Assert(err, check.IsNil, check.Commentf("[%s] could not create new request", d.id))
193 191
 
194 192
 			resp, err := client.Do(req)
195 193
 			if err != nil {
... ...
@@ -754,13 +742,7 @@ func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...strin
754 754
 }
755 755
 
756 756
 func findContainerIP(c *check.C, id string, vargs ...string) string {
757
-	args := append(vargs, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id)
758
-	cmd := exec.Command(dockerBinary, args...)
759
-	out, _, err := runCommandWithOutput(cmd)
760
-	if err != nil {
761
-		c.Fatal(err, out)
762
-	}
763
-
757
+	out, _ := dockerCmd(c, "inspect", "--format='{{ .NetworkSettings.IPAddress }}'", id)
764 758
 	return strings.Trim(out, " \r\n'")
765 759
 }
766 760
 
... ...
@@ -1312,30 +1294,23 @@ func newFakeGit(name string, files map[string]string, enforceLocalServer bool) (
1312 1312
 // Write `content` to the file at path `dst`, creating it if necessary,
1313 1313
 // as well as any missing directories.
1314 1314
 // The file is truncated if it already exists.
1315
-// Call c.Fatal() at the first error.
1315
+// Fail the test when error occures.
1316 1316
 func writeFile(dst, content string, c *check.C) {
1317 1317
 	// Create subdirectories if necessary
1318
-	if err := os.MkdirAll(path.Dir(dst), 0700); err != nil {
1319
-		c.Fatal(err)
1320
-	}
1318
+	c.Assert(os.MkdirAll(path.Dir(dst), 0700), check.IsNil)
1321 1319
 	f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
1322
-	if err != nil {
1323
-		c.Fatal(err)
1324
-	}
1320
+	c.Assert(err, check.IsNil)
1325 1321
 	defer f.Close()
1326 1322
 	// Write content (truncate if it exists)
1327
-	if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
1328
-		c.Fatal(err)
1329
-	}
1323
+	_, err = io.Copy(f, strings.NewReader(content))
1324
+	c.Assert(err, check.IsNil)
1330 1325
 }
1331 1326
 
1332 1327
 // Return the contents of file at path `src`.
1333
-// Call c.Fatal() at the first error (including if the file doesn't exist)
1328
+// Fail the test when error occures.
1334 1329
 func readFile(src string, c *check.C) (content string) {
1335 1330
 	data, err := ioutil.ReadFile(src)
1336
-	if err != nil {
1337
-		c.Fatal(err)
1338
-	}
1331
+	c.Assert(err, check.IsNil)
1339 1332
 
1340 1333
 	return string(data)
1341 1334
 }
... ...
@@ -1387,30 +1362,25 @@ func daemonTime(c *check.C) time.Time {
1387 1387
 	}
1388 1388
 
1389 1389
 	status, body, err := sockRequest("GET", "/info", nil)
1390
-	c.Assert(status, check.Equals, http.StatusOK)
1391 1390
 	c.Assert(err, check.IsNil)
1391
+	c.Assert(status, check.Equals, http.StatusOK)
1392 1392
 
1393 1393
 	type infoJSON struct {
1394 1394
 		SystemTime string
1395 1395
 	}
1396 1396
 	var info infoJSON
1397
-	if err = json.Unmarshal(body, &info); err != nil {
1398
-		c.Fatalf("unable to unmarshal /info response: %v", err)
1399
-	}
1397
+	err = json.Unmarshal(body, &info)
1398
+	c.Assert(err, check.IsNil, check.Commentf("unable to unmarshal GET /info response"))
1400 1399
 
1401 1400
 	dt, err := time.Parse(time.RFC3339Nano, info.SystemTime)
1402
-	if err != nil {
1403
-		c.Fatal(err)
1404
-	}
1401
+	c.Assert(err, check.IsNil, check.Commentf("invalid time format in GET /info response"))
1405 1402
 	return dt
1406 1403
 }
1407 1404
 
1408 1405
 func setupRegistry(c *check.C) *testRegistryV2 {
1409 1406
 	testRequires(c, RegistryHosting)
1410 1407
 	reg, err := newTestRegistryV2(c)
1411
-	if err != nil {
1412
-		c.Fatal(err)
1413
-	}
1408
+	c.Assert(err, check.IsNil)
1414 1409
 
1415 1410
 	// Wait for registry to be ready to serve requests.
1416 1411
 	for i := 0; i != 5; i++ {
... ...
@@ -1420,18 +1390,14 @@ func setupRegistry(c *check.C) *testRegistryV2 {
1420 1420
 		time.Sleep(100 * time.Millisecond)
1421 1421
 	}
1422 1422
 
1423
-	if err != nil {
1424
-		c.Fatal("Timeout waiting for test registry to become available")
1425
-	}
1423
+	c.Assert(err, check.IsNil, check.Commentf("Timeout waiting for test registry to become available"))
1426 1424
 	return reg
1427 1425
 }
1428 1426
 
1429 1427
 func setupNotary(c *check.C) *testNotary {
1430 1428
 	testRequires(c, NotaryHosting)
1431 1429
 	ts, err := newTestNotary(c)
1432
-	if err != nil {
1433
-		c.Fatal(err)
1434
-	}
1430
+	c.Assert(err, check.IsNil)
1435 1431
 
1436 1432
 	return ts
1437 1433
 }