Browse code

Improve the containers unit tests (add error checking)

Guillaume J. Charmes authored on 2013/03/29 23:44:58
Showing 1 changed files
... ...
@@ -91,17 +91,33 @@ func TestCommitRun(t *testing.T) {
91 91
 	defer runtime.Destroy(container2)
92 92
 
93 93
 	stdout, err := container2.StdoutPipe()
94
+	if err != nil {
95
+		t.Fatal(err)
96
+	}
94 97
 	stderr, err := container2.StderrPipe()
98
+	if err != nil {
99
+		t.Fatal(err)
100
+	}
95 101
 	if err := container2.Start(); err != nil {
96 102
 		t.Fatal(err)
97 103
 	}
98 104
 	container2.Wait()
99 105
 	output, err := ioutil.ReadAll(stdout)
106
+	if err != nil {
107
+		t.Fatal(err)
108
+	}
100 109
 	output2, err := ioutil.ReadAll(stderr)
101
-	stdout.Close()
102
-	stderr.Close()
110
+	if err != nil {
111
+		t.Fatal(err)
112
+	}
113
+	if err := stdout.Close(); err != nil {
114
+		t.Fatal(err)
115
+	}
116
+	if err := stderr.Close(); err != nil {
117
+		t.Fatal(err)
118
+	}
103 119
 	if string(output) != "hello\n" {
104
-		t.Fatalf("\nout: %s\nerr: %s\n", string(output), string(output2))
120
+		t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", string(output), string(output2))
105 121
 	}
106 122
 }
107 123
 
... ...
@@ -208,11 +224,9 @@ func TestExitCode(t *testing.T) {
208 208
 	defer nuke(runtime)
209 209
 
210 210
 	trueContainer, err := runtime.Create(&Config{
211
-
212 211
 		Image: GetTestImage(runtime).Id,
213 212
 		Cmd:   []string{"/bin/true", ""},
214
-	},
215
-	)
213
+	})
216 214
 	if err != nil {
217 215
 		t.Fatal(err)
218 216
 	}
... ...
@@ -220,12 +234,14 @@ func TestExitCode(t *testing.T) {
220 220
 	if err := trueContainer.Run(); err != nil {
221 221
 		t.Fatal(err)
222 222
 	}
223
+	if trueContainer.State.ExitCode != 0 {
224
+		t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
225
+	}
223 226
 
224 227
 	falseContainer, err := runtime.Create(&Config{
225 228
 		Image: GetTestImage(runtime).Id,
226 229
 		Cmd:   []string{"/bin/false", ""},
227
-	},
228
-	)
230
+	})
229 231
 	if err != nil {
230 232
 		t.Fatal(err)
231 233
 	}
... ...
@@ -233,13 +249,8 @@ func TestExitCode(t *testing.T) {
233 233
 	if err := falseContainer.Run(); err != nil {
234 234
 		t.Fatal(err)
235 235
 	}
236
-
237
-	if trueContainer.State.ExitCode != 0 {
238
-		t.Errorf("Unexpected exit code %v", trueContainer.State.ExitCode)
239
-	}
240
-
241 236
 	if falseContainer.State.ExitCode != 1 {
242
-		t.Errorf("Unexpected exit code %v", falseContainer.State.ExitCode)
237
+		t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
243 238
 	}
244 239
 }
245 240
 
... ...
@@ -295,32 +306,62 @@ func TestRestartStdin(t *testing.T) {
295 295
 	defer runtime.Destroy(container)
296 296
 
297 297
 	stdin, err := container.StdinPipe()
298
+	if err != nil {
299
+		t.Fatal(err)
300
+	}
298 301
 	stdout, err := container.StdoutPipe()
302
+	if err != nil {
303
+		t.Fatal(err)
304
+	}
299 305
 	if err := container.Start(); err != nil {
300 306
 		t.Fatal(err)
301 307
 	}
302
-	io.WriteString(stdin, "hello world")
303
-	stdin.Close()
308
+	if _, err := io.WriteString(stdin, "hello world"); err != nil {
309
+		t.Fatal(err)
310
+	}
311
+	if err := stdin.Close(); err != nil {
312
+		t.Fatal(err)
313
+	}
304 314
 	container.Wait()
305 315
 	output, err := ioutil.ReadAll(stdout)
306
-	stdout.Close()
316
+	if err != nil {
317
+		t.Fatal(err)
318
+	}
319
+	if err := stdout.Close(); err != nil {
320
+		t.Fatal(err)
321
+	}
307 322
 	if string(output) != "hello world" {
308
-		t.Fatal(string(output))
323
+		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
309 324
 	}
310 325
 
311 326
 	// Restart and try again
312 327
 	stdin, err = container.StdinPipe()
328
+	if err != nil {
329
+		t.Fatal(err)
330
+	}
313 331
 	stdout, err = container.StdoutPipe()
332
+	if err != nil {
333
+		t.Fatal(err)
334
+	}
314 335
 	if err := container.Start(); err != nil {
315 336
 		t.Fatal(err)
316 337
 	}
317
-	io.WriteString(stdin, "hello world #2")
318
-	stdin.Close()
338
+	if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
339
+		t.Fatal(err)
340
+	}
341
+	if err := stdin.Close(); err != nil {
342
+		t.Fatal(err)
343
+	}
319 344
 	container.Wait()
320 345
 	output, err = ioutil.ReadAll(stdout)
321
-	stdout.Close()
346
+	if err != nil {
347
+		t.Fatal(err)
348
+	}
349
+	if err := stdout.Close(); err != nil {
350
+		t.Fatal(err)
351
+	}
322 352
 	if string(output) != "hello world #2" {
323
-		t.Fatal(string(output))
353
+		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
324 354
 	}
325 355
 }
326 356
 
... ...
@@ -504,18 +545,31 @@ func TestStdin(t *testing.T) {
504 504
 	defer runtime.Destroy(container)
505 505
 
506 506
 	stdin, err := container.StdinPipe()
507
+	if err != nil {
508
+		t.Fatal(err)
509
+	}
507 510
 	stdout, err := container.StdoutPipe()
511
+	if err != nil {
512
+		t.Fatal(err)
513
+	}
514
+	if err := container.Start(); err != nil {
515
+		t.Fatal(err)
516
+	}
508 517
 	defer stdin.Close()
509 518
 	defer stdout.Close()
510
-	if err := container.Start(); err != nil {
519
+	if _, err := io.WriteString(stdin, "hello world"); err != nil {
520
+		t.Fatal(err)
521
+	}
522
+	if err := stdin.Close(); err != nil {
511 523
 		t.Fatal(err)
512 524
 	}
513
-	io.WriteString(stdin, "hello world")
514
-	stdin.Close()
515 525
 	container.Wait()
516 526
 	output, err := ioutil.ReadAll(stdout)
527
+	if err != nil {
528
+		t.Fatal(err)
529
+	}
517 530
 	if string(output) != "hello world" {
518
-		t.Fatal(string(output))
531
+		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
519 532
 	}
520 533
 }
521 534
 
... ...
@@ -538,18 +592,31 @@ func TestTty(t *testing.T) {
538 538
 	defer runtime.Destroy(container)
539 539
 
540 540
 	stdin, err := container.StdinPipe()
541
+	if err != nil {
542
+		t.Fatal(err)
543
+	}
541 544
 	stdout, err := container.StdoutPipe()
545
+	if err != nil {
546
+		t.Fatal(err)
547
+	}
548
+	if err := container.Start(); err != nil {
549
+		t.Fatal(err)
550
+	}
542 551
 	defer stdin.Close()
543 552
 	defer stdout.Close()
544
-	if err := container.Start(); err != nil {
553
+	if _, err := io.WriteString(stdin, "hello world"); err != nil {
554
+		t.Fatal(err)
555
+	}
556
+	if err := stdin.Close(); err != nil {
545 557
 		t.Fatal(err)
546 558
 	}
547
-	io.WriteString(stdin, "hello world")
548
-	stdin.Close()
549 559
 	container.Wait()
550 560
 	output, err := ioutil.ReadAll(stdout)
561
+	if err != nil {
562
+		t.Fatal(err)
563
+	}
551 564
 	if string(output) != "hello world" {
552
-		t.Fatal(string(output))
565
+		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
553 566
 	}
554 567
 }
555 568
 
... ...
@@ -568,6 +635,7 @@ func TestEnv(t *testing.T) {
568 568
 		t.Fatal(err)
569 569
 	}
570 570
 	defer runtime.Destroy(container)
571
+
571 572
 	stdout, err := container.StdoutPipe()
572 573
 	if err != nil {
573 574
 		t.Fatal(err)