Browse code

Simplify unit tests code with mkRuntime()

Solomon Hykes authored on 2013/07/12 09:59:25
Showing 7 changed files
... ...
@@ -41,10 +41,8 @@ func TestGetBoolParam(t *testing.T) {
41 41
 }
42 42
 
43 43
 func TestGetVersion(t *testing.T) {
44
-	runtime, err := newTestRuntime()
45
-	if err != nil {
46
-		t.Fatal(err)
47
-	}
44
+	var err error
45
+	runtime := mkRuntime(t)
48 46
 	defer nuke(runtime)
49 47
 
50 48
 	srv := &Server{runtime: runtime}
... ...
@@ -65,10 +63,7 @@ func TestGetVersion(t *testing.T) {
65 65
 }
66 66
 
67 67
 func TestGetInfo(t *testing.T) {
68
-	runtime, err := newTestRuntime()
69
-	if err != nil {
70
-		t.Fatal(err)
71
-	}
68
+	runtime := mkRuntime(t)
72 69
 	defer nuke(runtime)
73 70
 
74 71
 	srv := &Server{runtime: runtime}
... ...
@@ -95,10 +90,7 @@ func TestGetInfo(t *testing.T) {
95 95
 }
96 96
 
97 97
 func TestGetImagesJSON(t *testing.T) {
98
-	runtime, err := newTestRuntime()
99
-	if err != nil {
100
-		t.Fatal(err)
101
-	}
98
+	runtime := mkRuntime(t)
102 99
 	defer nuke(runtime)
103 100
 
104 101
 	srv := &Server{runtime: runtime}
... ...
@@ -220,10 +212,7 @@ func TestGetImagesJSON(t *testing.T) {
220 220
 }
221 221
 
222 222
 func TestGetImagesViz(t *testing.T) {
223
-	runtime, err := newTestRuntime()
224
-	if err != nil {
225
-		t.Fatal(err)
226
-	}
223
+	runtime := mkRuntime(t)
227 224
 	defer nuke(runtime)
228 225
 
229 226
 	srv := &Server{runtime: runtime}
... ...
@@ -248,10 +237,7 @@ func TestGetImagesViz(t *testing.T) {
248 248
 }
249 249
 
250 250
 func TestGetImagesHistory(t *testing.T) {
251
-	runtime, err := newTestRuntime()
252
-	if err != nil {
253
-		t.Fatal(err)
254
-	}
251
+	runtime := mkRuntime(t)
255 252
 	defer nuke(runtime)
256 253
 
257 254
 	srv := &Server{runtime: runtime}
... ...
@@ -272,10 +258,7 @@ func TestGetImagesHistory(t *testing.T) {
272 272
 }
273 273
 
274 274
 func TestGetImagesByName(t *testing.T) {
275
-	runtime, err := newTestRuntime()
276
-	if err != nil {
277
-		t.Fatal(err)
278
-	}
275
+	runtime := mkRuntime(t)
279 276
 	defer nuke(runtime)
280 277
 
281 278
 	srv := &Server{runtime: runtime}
... ...
@@ -295,10 +278,7 @@ func TestGetImagesByName(t *testing.T) {
295 295
 }
296 296
 
297 297
 func TestGetContainersJSON(t *testing.T) {
298
-	runtime, err := newTestRuntime()
299
-	if err != nil {
300
-		t.Fatal(err)
301
-	}
298
+	runtime := mkRuntime(t)
302 299
 	defer nuke(runtime)
303 300
 
304 301
 	srv := &Server{runtime: runtime}
... ...
@@ -334,10 +314,7 @@ func TestGetContainersJSON(t *testing.T) {
334 334
 }
335 335
 
336 336
 func TestGetContainersExport(t *testing.T) {
337
-	runtime, err := newTestRuntime()
338
-	if err != nil {
339
-		t.Fatal(err)
340
-	}
337
+	runtime := mkRuntime(t)
341 338
 	defer nuke(runtime)
342 339
 
343 340
 	srv := &Server{runtime: runtime}
... ...
@@ -389,10 +366,7 @@ func TestGetContainersExport(t *testing.T) {
389 389
 }
390 390
 
391 391
 func TestGetContainersChanges(t *testing.T) {
392
-	runtime, err := newTestRuntime()
393
-	if err != nil {
394
-		t.Fatal(err)
395
-	}
392
+	runtime := mkRuntime(t)
396 393
 	defer nuke(runtime)
397 394
 
398 395
 	srv := &Server{runtime: runtime}
... ...
@@ -437,10 +411,7 @@ func TestGetContainersChanges(t *testing.T) {
437 437
 }
438 438
 
439 439
 func TestGetContainersByName(t *testing.T) {
440
-	runtime, err := newTestRuntime()
441
-	if err != nil {
442
-		t.Fatal(err)
443
-	}
440
+	runtime := mkRuntime(t)
444 441
 	defer nuke(runtime)
445 442
 
446 443
 	srv := &Server{runtime: runtime}
... ...
@@ -473,10 +444,7 @@ func TestGetContainersByName(t *testing.T) {
473 473
 }
474 474
 
475 475
 func TestPostCommit(t *testing.T) {
476
-	runtime, err := newTestRuntime()
477
-	if err != nil {
478
-		t.Fatal(err)
479
-	}
476
+	runtime := mkRuntime(t)
480 477
 	defer nuke(runtime)
481 478
 
482 479
 	srv := &Server{runtime: runtime}
... ...
@@ -522,10 +490,7 @@ func TestPostCommit(t *testing.T) {
522 522
 }
523 523
 
524 524
 func TestPostContainersCreate(t *testing.T) {
525
-	runtime, err := newTestRuntime()
526
-	if err != nil {
527
-		t.Fatal(err)
528
-	}
525
+	runtime := mkRuntime(t)
529 526
 	defer nuke(runtime)
530 527
 
531 528
 	srv := &Server{runtime: runtime}
... ...
@@ -576,10 +541,7 @@ func TestPostContainersCreate(t *testing.T) {
576 576
 }
577 577
 
578 578
 func TestPostContainersKill(t *testing.T) {
579
-	runtime, err := newTestRuntime()
580
-	if err != nil {
581
-		t.Fatal(err)
582
-	}
579
+	runtime := mkRuntime(t)
583 580
 	defer nuke(runtime)
584 581
 
585 582
 	srv := &Server{runtime: runtime}
... ...
@@ -621,10 +583,7 @@ func TestPostContainersKill(t *testing.T) {
621 621
 }
622 622
 
623 623
 func TestPostContainersRestart(t *testing.T) {
624
-	runtime, err := newTestRuntime()
625
-	if err != nil {
626
-		t.Fatal(err)
627
-	}
624
+	runtime := mkRuntime(t)
628 625
 	defer nuke(runtime)
629 626
 
630 627
 	srv := &Server{runtime: runtime}
... ...
@@ -678,10 +637,7 @@ func TestPostContainersRestart(t *testing.T) {
678 678
 }
679 679
 
680 680
 func TestPostContainersStart(t *testing.T) {
681
-	runtime, err := newTestRuntime()
682
-	if err != nil {
683
-		t.Fatal(err)
684
-	}
681
+	runtime := mkRuntime(t)
685 682
 	defer nuke(runtime)
686 683
 
687 684
 	srv := &Server{runtime: runtime}
... ...
@@ -731,10 +687,7 @@ func TestPostContainersStart(t *testing.T) {
731 731
 }
732 732
 
733 733
 func TestPostContainersStop(t *testing.T) {
734
-	runtime, err := newTestRuntime()
735
-	if err != nil {
736
-		t.Fatal(err)
737
-	}
734
+	runtime := mkRuntime(t)
738 735
 	defer nuke(runtime)
739 736
 
740 737
 	srv := &Server{runtime: runtime}
... ...
@@ -781,10 +734,7 @@ func TestPostContainersStop(t *testing.T) {
781 781
 }
782 782
 
783 783
 func TestPostContainersWait(t *testing.T) {
784
-	runtime, err := newTestRuntime()
785
-	if err != nil {
786
-		t.Fatal(err)
787
-	}
784
+	runtime := mkRuntime(t)
788 785
 	defer nuke(runtime)
789 786
 
790 787
 	srv := &Server{runtime: runtime}
... ...
@@ -826,10 +776,7 @@ func TestPostContainersWait(t *testing.T) {
826 826
 }
827 827
 
828 828
 func TestPostContainersAttach(t *testing.T) {
829
-	runtime, err := newTestRuntime()
830
-	if err != nil {
831
-		t.Fatal(err)
832
-	}
829
+	runtime := mkRuntime(t)
833 830
 	defer nuke(runtime)
834 831
 
835 832
 	srv := &Server{runtime: runtime}
... ...
@@ -915,10 +862,7 @@ func TestPostContainersAttach(t *testing.T) {
915 915
 // FIXME: Test deleting container with volume
916 916
 // FIXME: Test deleting volume in use by other container
917 917
 func TestDeleteContainers(t *testing.T) {
918
-	runtime, err := newTestRuntime()
919
-	if err != nil {
920
-		t.Fatal(err)
921
-	}
918
+	runtime := mkRuntime(t)
922 919
 	defer nuke(runtime)
923 920
 
924 921
 	srv := &Server{runtime: runtime}
... ...
@@ -958,10 +902,7 @@ func TestDeleteContainers(t *testing.T) {
958 958
 }
959 959
 
960 960
 func TestOptionsRoute(t *testing.T) {
961
-	runtime, err := newTestRuntime()
962
-	if err != nil {
963
-		t.Fatal(err)
964
-	}
961
+	runtime := mkRuntime(t)
965 962
 	defer nuke(runtime)
966 963
 
967 964
 	srv := &Server{runtime: runtime, enableCors: true}
... ...
@@ -984,10 +925,7 @@ func TestOptionsRoute(t *testing.T) {
984 984
 }
985 985
 
986 986
 func TestGetEnabledCors(t *testing.T) {
987
-	runtime, err := newTestRuntime()
988
-	if err != nil {
989
-		t.Fatal(err)
990
-	}
987
+	runtime := mkRuntime(t)
991 988
 	defer nuke(runtime)
992 989
 
993 990
 	srv := &Server{runtime: runtime, enableCors: true}
... ...
@@ -1025,10 +963,7 @@ func TestGetEnabledCors(t *testing.T) {
1025 1025
 }
1026 1026
 
1027 1027
 func TestDeleteImages(t *testing.T) {
1028
-	runtime, err := newTestRuntime()
1029
-	if err != nil {
1030
-		t.Fatal(err)
1031
-	}
1028
+	runtime := mkRuntime(t)
1032 1029
 	defer nuke(runtime)
1033 1030
 
1034 1031
 	srv := &Server{runtime: runtime}
... ...
@@ -96,10 +96,7 @@ CMD Hello world
96 96
 
97 97
 func TestBuild(t *testing.T) {
98 98
 	for _, ctx := range testContexts {
99
-		runtime, err := newTestRuntime()
100
-		if err != nil {
101
-			t.Fatal(err)
102
-		}
99
+		runtime := mkRuntime(t)
103 100
 		defer nuke(runtime)
104 101
 
105 102
 		srv := &Server{
... ...
@@ -1046,10 +1046,7 @@ func TestEnv(t *testing.T) {
1046 1046
 }
1047 1047
 
1048 1048
 func TestEntrypoint(t *testing.T) {
1049
-	runtime, err := newTestRuntime()
1050
-	if err != nil {
1051
-		t.Fatal(err)
1052
-	}
1049
+	runtime := mkRuntime(t)
1053 1050
 	defer nuke(runtime)
1054 1051
 	container, err := NewBuilder(runtime).Create(
1055 1052
 		&Config{
... ...
@@ -1125,10 +1122,7 @@ func TestLXCConfig(t *testing.T) {
1125 1125
 }
1126 1126
 
1127 1127
 func BenchmarkRunSequencial(b *testing.B) {
1128
-	runtime, err := newTestRuntime()
1129
-	if err != nil {
1130
-		b.Fatal(err)
1131
-	}
1128
+	runtime := mkRuntime(b)
1132 1129
 	defer nuke(runtime)
1133 1130
 	for i := 0; i < b.N; i++ {
1134 1131
 		container, err := NewBuilder(runtime).Create(&Config{
... ...
@@ -1154,10 +1148,7 @@ func BenchmarkRunSequencial(b *testing.B) {
1154 1154
 }
1155 1155
 
1156 1156
 func BenchmarkRunParallel(b *testing.B) {
1157
-	runtime, err := newTestRuntime()
1158
-	if err != nil {
1159
-		b.Fatal(err)
1160
-	}
1157
+	runtime := mkRuntime(b)
1161 1158
 	defer nuke(runtime)
1162 1159
 
1163 1160
 	var tasks []chan error
... ...
@@ -114,26 +114,6 @@ func init() {
114 114
 
115 115
 // FIXME: test that ImagePull(json=true) send correct json output
116 116
 
117
-func newTestRuntime() (*Runtime, error) {
118
-	root, err := ioutil.TempDir("", "docker-test")
119
-	if err != nil {
120
-		return nil, err
121
-	}
122
-	if err := os.Remove(root); err != nil {
123
-		return nil, err
124
-	}
125
-	if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
126
-		return nil, err
127
-	}
128
-
129
-	runtime, err := NewRuntimeFromDirectory(root, false)
130
-	if err != nil {
131
-		return nil, err
132
-	}
133
-	runtime.UpdateCapabilities(true)
134
-	return runtime, nil
135
-}
136
-
137 117
 func GetTestImage(runtime *Runtime) *Image {
138 118
 	imgs, err := runtime.graph.All()
139 119
 	if err != nil {
... ...
@@ -148,10 +128,7 @@ func GetTestImage(runtime *Runtime) *Image {
148 148
 }
149 149
 
150 150
 func TestRuntimeCreate(t *testing.T) {
151
-	runtime, err := newTestRuntime()
152
-	if err != nil {
153
-		t.Fatal(err)
154
-	}
151
+	runtime := mkRuntime(t)
155 152
 	defer nuke(runtime)
156 153
 
157 154
 	// Make sure we start we 0 containers
... ...
@@ -223,10 +200,7 @@ func TestRuntimeCreate(t *testing.T) {
223 223
 }
224 224
 
225 225
 func TestDestroy(t *testing.T) {
226
-	runtime, err := newTestRuntime()
227
-	if err != nil {
228
-		t.Fatal(err)
229
-	}
226
+	runtime := mkRuntime(t)
230 227
 	defer nuke(runtime)
231 228
 	container, err := NewBuilder(runtime).Create(&Config{
232 229
 		Image: GetTestImage(runtime).ID,
... ...
@@ -270,10 +244,7 @@ func TestDestroy(t *testing.T) {
270 270
 }
271 271
 
272 272
 func TestGet(t *testing.T) {
273
-	runtime, err := newTestRuntime()
274
-	if err != nil {
275
-		t.Fatal(err)
276
-	}
273
+	runtime := mkRuntime(t)
277 274
 	defer nuke(runtime)
278 275
 
279 276
 	builder := NewBuilder(runtime)
... ...
@@ -323,11 +294,8 @@ func TestGet(t *testing.T) {
323 323
 }
324 324
 
325 325
 func startEchoServerContainer(t *testing.T, proto string) (*Runtime, *Container, string) {
326
-	runtime, err := newTestRuntime()
327
-	if err != nil {
328
-		t.Fatal(err)
329
-	}
330
-
326
+	var err error
327
+	runtime := mkRuntime(t)
331 328
 	port := 5554
332 329
 	var container *Container
333 330
 	var strPort string
... ...
@@ -5,10 +5,7 @@ import (
5 5
 )
6 6
 
7 7
 func TestContainerTagImageDelete(t *testing.T) {
8
-	runtime, err := newTestRuntime()
9
-	if err != nil {
10
-		t.Fatal(err)
11
-	}
8
+	runtime := mkRuntime(t)
12 9
 	defer nuke(runtime)
13 10
 
14 11
 	srv := &Server{runtime: runtime}
... ...
@@ -62,10 +59,7 @@ func TestContainerTagImageDelete(t *testing.T) {
62 62
 }
63 63
 
64 64
 func TestCreateRm(t *testing.T) {
65
-	runtime, err := newTestRuntime()
66
-	if err != nil {
67
-		t.Fatal(err)
68
-	}
65
+	runtime := mkRuntime(t)
69 66
 	defer nuke(runtime)
70 67
 
71 68
 	srv := &Server{runtime: runtime}
... ...
@@ -95,10 +89,7 @@ func TestCreateRm(t *testing.T) {
95 95
 }
96 96
 
97 97
 func TestCreateStartRestartStopStartKillRm(t *testing.T) {
98
-	runtime, err := newTestRuntime()
99
-	if err != nil {
100
-		t.Fatal(err)
101
-	}
98
+	runtime := mkRuntime(t)
102 99
 	defer nuke(runtime)
103 100
 
104 101
 	srv := &Server{runtime: runtime}
... ...
@@ -154,11 +145,9 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
154 154
 }
155 155
 
156 156
 func TestRunWithTooLowMemoryLimit(t *testing.T) {
157
-	runtime, err := newTestRuntime()
157
+	var err error
158
+	runtime := mkRuntime(t)
158 159
 	srv := &Server{runtime: runtime}
159
-	if err != nil {
160
-		t.Fatal(err)
161
-	}
162 160
 	defer nuke(runtime)
163 161
 	// Try to create a container with a memory limit of 1 byte less than the minimum allowed limit.
164 162
 	_, err = srv.ContainerCreate(
... ...
@@ -5,10 +5,7 @@ import (
5 5
 )
6 6
 
7 7
 func TestLookupImage(t *testing.T) {
8
-	runtime, err := newTestRuntime()
9
-	if err != nil {
10
-		t.Fatal(err)
11
-	}
8
+	runtime := mkRuntime(t)
12 9
 	defer nuke(runtime)
13 10
 
14 11
 	if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil {
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"path"
8 8
 	"strings"
9 9
 	"testing"
10
+	"github.com/dotcloud/docker/utils"
10 11
 )
11 12
 
12 13
 // This file contains utility functions for docker's unit test suite.
... ...
@@ -15,14 +16,40 @@ import (
15 15
 
16 16
 // Create a temporary runtime suitable for unit testing.
17 17
 // Call t.Fatal() at the first error.
18
-func mkRuntime(t *testing.T) *Runtime {
18
+func mkRuntime(f Fataler) *Runtime {
19 19
 	runtime, err := newTestRuntime()
20 20
 	if err != nil {
21
-		t.Fatal(err)
21
+		f.Fatal(err)
22 22
 	}
23 23
 	return runtime
24 24
 }
25 25
 
26
+// A common interface to access the Fatal method of
27
+// both testing.B and testing.T.
28
+type Fataler interface {
29
+	Fatal(args ...interface{})
30
+}
31
+
32
+func newTestRuntime() (*Runtime, error) {
33
+	root, err := ioutil.TempDir("", "docker-test")
34
+	if err != nil {
35
+		return nil, err
36
+	}
37
+	if err := os.Remove(root); err != nil {
38
+		return nil, err
39
+	}
40
+	if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
41
+		return nil, err
42
+	}
43
+
44
+	runtime, err := NewRuntimeFromDirectory(root, false)
45
+	if err != nil {
46
+		return nil, err
47
+	}
48
+	runtime.UpdateCapabilities(true)
49
+	return runtime, nil
50
+}
51
+
26 52
 // Write `content` to the file at path `dst`, creating it if necessary,
27 53
 // as well as any missing directories.
28 54
 // The file is truncated if it already exists.