Browse code

Hack: use helper functions in tests for less copy-pasting

Solomon Hykes authored on 2013/07/13 09:56:55
Showing 3 changed files
... ...
@@ -39,16 +39,11 @@ func TestIDFormat(t *testing.T) {
39 39
 func TestMultipleAttachRestart(t *testing.T) {
40 40
 	runtime := mkRuntime(t)
41 41
 	defer nuke(runtime)
42
-	container, err := NewBuilder(runtime).Create(
43
-		&Config{
44
-			Image: GetTestImage(runtime).ID,
45
-			Cmd: []string{"/bin/sh", "-c",
46
-				"i=1; while [ $i -le 5 ]; do i=`expr $i + 1`;  echo hello; done"},
47
-		},
42
+	container, hostConfig, _ := mkContainer(
43
+		runtime,
44
+		[]string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`;  echo hello; done"},
45
+		t,
48 46
 	)
49
-	if err != nil {
50
-		t.Fatal(err)
51
-	}
52 47
 	defer runtime.Destroy(container)
53 48
 
54 49
 	// Simulate 3 client attaching to the container and stop/restart
... ...
@@ -65,7 +60,6 @@ func TestMultipleAttachRestart(t *testing.T) {
65 65
 	if err != nil {
66 66
 		t.Fatal(err)
67 67
 	}
68
-	hostConfig := &HostConfig{}
69 68
 	if err := container.Start(hostConfig); err != nil {
70 69
 		t.Fatal(err)
71 70
 	}
... ...
@@ -140,19 +134,8 @@ func TestMultipleAttachRestart(t *testing.T) {
140 140
 func TestDiff(t *testing.T) {
141 141
 	runtime := mkRuntime(t)
142 142
 	defer nuke(runtime)
143
-
144
-	builder := NewBuilder(runtime)
145
-
146 143
 	// Create a container and remove a file
147
-	container1, err := builder.Create(
148
-		&Config{
149
-			Image: GetTestImage(runtime).ID,
150
-			Cmd:   []string{"/bin/rm", "/etc/passwd"},
151
-		},
152
-	)
153
-	if err != nil {
154
-		t.Fatal(err)
155
-	}
144
+	container1, _, _ := mkContainer(runtime, []string{"_", "/bin/rm", "/etc/passwd"}, t)
156 145
 	defer runtime.Destroy(container1)
157 146
 
158 147
 	if err := container1.Run(); err != nil {
... ...
@@ -185,15 +168,7 @@ func TestDiff(t *testing.T) {
185 185
 	}
186 186
 
187 187
 	// Create a new container from the commited image
188
-	container2, err := builder.Create(
189
-		&Config{
190
-			Image: img.ID,
191
-			Cmd:   []string{"cat", "/etc/passwd"},
192
-		},
193
-	)
194
-	if err != nil {
195
-		t.Fatal(err)
196
-	}
188
+	container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/etc/passwd"}, t)
197 189
 	defer runtime.Destroy(container2)
198 190
 
199 191
 	if err := container2.Run(); err != nil {
... ...
@@ -212,15 +187,7 @@ func TestDiff(t *testing.T) {
212 212
 	}
213 213
 
214 214
 	// Create a new containere
215
-	container3, err := builder.Create(
216
-		&Config{
217
-			Image: GetTestImage(runtime).ID,
218
-			Cmd:   []string{"rm", "/bin/httpd"},
219
-		},
220
-	)
221
-	if err != nil {
222
-		t.Fatal(err)
223
-	}
215
+	container3, _, _ := mkContainer(runtime, []string{"_", "rm", "/bin/httpd"}, t)
224 216
 	defer runtime.Destroy(container3)
225 217
 
226 218
 	if err := container3.Run(); err != nil {
... ...
@@ -246,17 +213,7 @@ func TestDiff(t *testing.T) {
246 246
 func TestCommitAutoRun(t *testing.T) {
247 247
 	runtime := mkRuntime(t)
248 248
 	defer nuke(runtime)
249
-
250
-	builder := NewBuilder(runtime)
251
-	container1, err := builder.Create(
252
-		&Config{
253
-			Image: GetTestImage(runtime).ID,
254
-			Cmd:   []string{"/bin/sh", "-c", "echo hello > /world"},
255
-		},
256
-	)
257
-	if err != nil {
258
-		t.Fatal(err)
259
-	}
249
+	container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
260 250
 	defer runtime.Destroy(container1)
261 251
 
262 252
 	if container1.State.Running {
... ...
@@ -279,14 +236,7 @@ func TestCommitAutoRun(t *testing.T) {
279 279
 	}
280 280
 
281 281
 	// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
282
-	container2, err := builder.Create(
283
-		&Config{
284
-			Image: img.ID,
285
-		},
286
-	)
287
-	if err != nil {
288
-		t.Fatal(err)
289
-	}
282
+	container2, hostConfig, _ := mkContainer(runtime, []string{img.ID}, t)
290 283
 	defer runtime.Destroy(container2)
291 284
 	stdout, err := container2.StdoutPipe()
292 285
 	if err != nil {
... ...
@@ -296,7 +246,6 @@ func TestCommitAutoRun(t *testing.T) {
296 296
 	if err != nil {
297 297
 		t.Fatal(err)
298 298
 	}
299
-	hostConfig := &HostConfig{}
300 299
 	if err := container2.Start(hostConfig); err != nil {
301 300
 		t.Fatal(err)
302 301
 	}
... ...
@@ -324,17 +273,7 @@ func TestCommitRun(t *testing.T) {
324 324
 	runtime := mkRuntime(t)
325 325
 	defer nuke(runtime)
326 326
 
327
-	builder := NewBuilder(runtime)
328
-
329
-	container1, err := builder.Create(
330
-		&Config{
331
-			Image: GetTestImage(runtime).ID,
332
-			Cmd:   []string{"/bin/sh", "-c", "echo hello > /world"},
333
-		},
334
-	)
335
-	if err != nil {
336
-		t.Fatal(err)
337
-	}
327
+	container1, hostConfig, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
338 328
 	defer runtime.Destroy(container1)
339 329
 
340 330
 	if container1.State.Running {
... ...
@@ -357,16 +296,7 @@ func TestCommitRun(t *testing.T) {
357 357
 	}
358 358
 
359 359
 	// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
360
-
361
-	container2, err := builder.Create(
362
-		&Config{
363
-			Image: img.ID,
364
-			Cmd:   []string{"cat", "/world"},
365
-		},
366
-	)
367
-	if err != nil {
368
-		t.Fatal(err)
369
-	}
360
+	container2, hostConfig, _ := mkContainer(runtime, []string{img.ID, "cat", "/world"}, t)
370 361
 	defer runtime.Destroy(container2)
371 362
 	stdout, err := container2.StdoutPipe()
372 363
 	if err != nil {
... ...
@@ -376,7 +306,6 @@ func TestCommitRun(t *testing.T) {
376 376
 	if err != nil {
377 377
 		t.Fatal(err)
378 378
 	}
379
-	hostConfig := &HostConfig{}
380 379
 	if err := container2.Start(hostConfig); err != nil {
381 380
 		t.Fatal(err)
382 381
 	}
... ...
@@ -403,18 +332,7 @@ func TestCommitRun(t *testing.T) {
403 403
 func TestStart(t *testing.T) {
404 404
 	runtime := mkRuntime(t)
405 405
 	defer nuke(runtime)
406
-	container, err := NewBuilder(runtime).Create(
407
-		&Config{
408
-			Image:     GetTestImage(runtime).ID,
409
-			Memory:    33554432,
410
-			CpuShares: 1000,
411
-			Cmd:       []string{"/bin/cat"},
412
-			OpenStdin: true,
413
-		},
414
-	)
415
-	if err != nil {
416
-		t.Fatal(err)
417
-	}
406
+	container, hostConfig, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
418 407
 	defer runtime.Destroy(container)
419 408
 
420 409
 	cStdin, err := container.StdinPipe()
... ...
@@ -422,7 +340,6 @@ func TestStart(t *testing.T) {
422 422
 		t.Fatal(err)
423 423
 	}
424 424
 
425
-	hostConfig := &HostConfig{}
426 425
 	if err := container.Start(hostConfig); err != nil {
427 426
 		t.Fatal(err)
428 427
 	}
... ...
@@ -445,15 +362,7 @@ func TestStart(t *testing.T) {
445 445
 func TestRun(t *testing.T) {
446 446
 	runtime := mkRuntime(t)
447 447
 	defer nuke(runtime)
448
-	container, err := NewBuilder(runtime).Create(
449
-		&Config{
450
-			Image: GetTestImage(runtime).ID,
451
-			Cmd:   []string{"ls", "-al"},
452
-		},
453
-	)
454
-	if err != nil {
455
-		t.Fatal(err)
456
-	}
448
+	container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
457 449
 	defer runtime.Destroy(container)
458 450
 
459 451
 	if container.State.Running {
... ...
@@ -5,7 +5,6 @@ import (
5 5
 	"fmt"
6 6
 	"github.com/dotcloud/docker/utils"
7 7
 	"io"
8
-	"io/ioutil"
9 8
 	"log"
10 9
 	"net"
11 10
 	"os"
... ...
@@ -247,36 +246,13 @@ func TestGet(t *testing.T) {
247 247
 	runtime := mkRuntime(t)
248 248
 	defer nuke(runtime)
249 249
 
250
-	builder := NewBuilder(runtime)
251
-
252
-	container1, err := builder.Create(&Config{
253
-		Image: GetTestImage(runtime).ID,
254
-		Cmd:   []string{"ls", "-al"},
255
-	},
256
-	)
257
-	if err != nil {
258
-		t.Fatal(err)
259
-	}
250
+	container1, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
260 251
 	defer runtime.Destroy(container1)
261 252
 
262
-	container2, err := builder.Create(&Config{
263
-		Image: GetTestImage(runtime).ID,
264
-		Cmd:   []string{"ls", "-al"},
265
-	},
266
-	)
267
-	if err != nil {
268
-		t.Fatal(err)
269
-	}
253
+	container2, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
270 254
 	defer runtime.Destroy(container2)
271 255
 
272
-	container3, err := builder.Create(&Config{
273
-		Image: GetTestImage(runtime).ID,
274
-		Cmd:   []string{"ls", "-al"},
275
-	},
276
-	)
277
-	if err != nil {
278
-		t.Fatal(err)
279
-	}
256
+	container3, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
280 257
 	defer runtime.Destroy(container3)
281 258
 
282 259
 	if runtime.Get(container1.ID) != container1 {
... ...
@@ -431,46 +407,14 @@ func TestAllocateUDPPortLocalhost(t *testing.T) {
431 431
 }
432 432
 
433 433
 func TestRestore(t *testing.T) {
434
-
435
-	root, err := ioutil.TempDir("", "docker-test")
436
-	if err != nil {
437
-		t.Fatal(err)
438
-	}
439
-	if err := os.Remove(root); err != nil {
440
-		t.Fatal(err)
441
-	}
442
-	if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
443
-		t.Fatal(err)
444
-	}
445
-
446
-	runtime1, err := NewRuntimeFromDirectory(root, false)
447
-	if err != nil {
448
-		t.Fatal(err)
449
-	}
450
-
451
-	builder := NewBuilder(runtime1)
452
-
434
+	runtime1 := mkRuntime(t)
435
+	defer nuke(runtime1)
453 436
 	// Create a container with one instance of docker
454
-	container1, err := builder.Create(&Config{
455
-		Image: GetTestImage(runtime1).ID,
456
-		Cmd:   []string{"ls", "-al"},
457
-	},
458
-	)
459
-	if err != nil {
460
-		t.Fatal(err)
461
-	}
437
+	container1, _, _ := mkContainer(runtime1, []string{"_", "ls", "-al"}, t)
462 438
 	defer runtime1.Destroy(container1)
463 439
 
464 440
 	// Create a second container meant to be killed
465
-	container2, err := builder.Create(&Config{
466
-		Image:     GetTestImage(runtime1).ID,
467
-		Cmd:       []string{"/bin/cat"},
468
-		OpenStdin: true,
469
-	},
470
-	)
471
-	if err != nil {
472
-		t.Fatal(err)
473
-	}
441
+	container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
474 442
 	defer runtime1.Destroy(container2)
475 443
 
476 444
 	// Start the container non blocking
... ...
@@ -505,7 +449,7 @@ func TestRestore(t *testing.T) {
505 505
 
506 506
 	// Here are are simulating a docker restart - that is, reloading all containers
507 507
 	// from scratch
508
-	runtime2, err := NewRuntimeFromDirectory(root, false)
508
+	runtime2, err := NewRuntimeFromDirectory(runtime1.root, false)
509 509
 	if err != nil {
510 510
 		t.Fatal(err)
511 511
 	}
... ...
@@ -84,20 +84,28 @@ func readFile(src string, t *testing.T) (content string) {
84 84
 }
85 85
 
86 86
 // Create a test container from the given runtime `r` and run arguments `args`.
87
-// The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
87
+// If the image name is "_", (eg. []string{"-i", "-t", "_", "bash"}, it is
88
+// dynamically replaced by the current test image.
88 89
 // The caller is responsible for destroying the container.
89 90
 // Call t.Fatal() at the first error.
90
-func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig) {
91
+func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig, error) {
91 92
 	config, hostConfig, _, err := ParseRun(args, nil)
93
+	defer func() {
94
+		if err != nil && t != nil {
95
+			t.Fatal(err)
96
+		}
97
+	}()
92 98
 	if err != nil {
93
-		t.Fatal(err)
99
+		return nil, nil, err
100
+	}
101
+	if config.Image == "_" {
102
+		config.Image = GetTestImage(r).ID
94 103
 	}
95
-	config.Image = GetTestImage(r).ID
96 104
 	c, err := NewBuilder(r).Create(config)
97 105
 	if err != nil {
98
-		t.Fatal(err)
106
+		return nil, nil, err
99 107
 	}
100
-	return c, hostConfig
108
+	return c, hostConfig, nil
101 109
 }
102 110
 
103 111
 // Create a test container, start it, wait for it to complete, destroy it,
... ...
@@ -110,7 +118,10 @@ func runContainer(r *Runtime, args []string, t *testing.T) (output string, err e
110 110
 			t.Fatal(err)
111 111
 		}
112 112
 	}()
113
-	container, hostConfig := mkContainer(r, args, t)
113
+	container, hostConfig, err := mkContainer(r, args, t)
114
+	if err != nil {
115
+		return "", err
116
+	}
114 117
 	defer r.Destroy(container)
115 118
 	stdout, err := container.StdoutPipe()
116 119
 	if err != nil {