Browse code

Autonomous testing: Don't depend on /var/lib/docker/images/ubuntu being there. Instead, automatically downlaod the test image if not present.

Andrea Luzzardi authored on 2013/02/15 08:37:45
Showing 3 changed files
... ...
@@ -18,7 +18,7 @@ func TestStart(t *testing.T) {
18 18
 		"start_test",
19 19
 		"ls",
20 20
 		[]string{"-al"},
21
-		[]string{"/var/lib/docker/images/ubuntu"},
21
+		[]string{testLayerPath},
22 22
 		&Config{
23 23
 			Ram: 33554432,
24 24
 		},
... ...
@@ -54,7 +54,7 @@ func TestRun(t *testing.T) {
54 54
 		"run_test",
55 55
 		"ls",
56 56
 		[]string{"-al"},
57
-		[]string{"/var/lib/docker/images/ubuntu"},
57
+		[]string{testLayerPath},
58 58
 		&Config{
59 59
 			Ram: 33554432,
60 60
 		},
... ...
@@ -84,7 +84,7 @@ func TestOutput(t *testing.T) {
84 84
 		"output_test",
85 85
 		"echo",
86 86
 		[]string{"-n", "foobar"},
87
-		[]string{"/var/lib/docker/images/ubuntu"},
87
+		[]string{testLayerPath},
88 88
 		&Config{},
89 89
 	)
90 90
 	if err != nil {
... ...
@@ -109,7 +109,7 @@ func TestKill(t *testing.T) {
109 109
 		"stop_test",
110 110
 		"cat",
111 111
 		[]string{"/dev/zero"},
112
-		[]string{"/var/lib/docker/images/ubuntu"},
112
+		[]string{testLayerPath},
113 113
 		&Config{},
114 114
 	)
115 115
 	if err != nil {
... ...
@@ -152,7 +152,7 @@ func TestExitCode(t *testing.T) {
152 152
 		"exit_test_1",
153 153
 		"/bin/true",
154 154
 		[]string{""},
155
-		[]string{"/var/lib/docker/images/ubuntu"},
155
+		[]string{testLayerPath},
156 156
 		&Config{},
157 157
 	)
158 158
 	if err != nil {
... ...
@@ -167,7 +167,7 @@ func TestExitCode(t *testing.T) {
167 167
 		"exit_test_2",
168 168
 		"/bin/false",
169 169
 		[]string{""},
170
-		[]string{"/var/lib/docker/images/ubuntu"},
170
+		[]string{testLayerPath},
171 171
 		&Config{},
172 172
 	)
173 173
 	if err != nil {
... ...
@@ -196,7 +196,7 @@ func TestRestart(t *testing.T) {
196 196
 		"restart_test",
197 197
 		"echo",
198 198
 		[]string{"-n", "foobar"},
199
-		[]string{"/var/lib/docker/images/ubuntu"},
199
+		[]string{testLayerPath},
200 200
 		&Config{},
201 201
 	)
202 202
 	if err != nil {
... ...
@@ -230,7 +230,7 @@ func TestRestartStdin(t *testing.T) {
230 230
 		"restart_stdin_test",
231 231
 		"cat",
232 232
 		[]string{},
233
-		[]string{"/var/lib/docker/images/ubuntu"},
233
+		[]string{testLayerPath},
234 234
 		&Config{
235 235
 			OpenStdin: true,
236 236
 		},
... ...
@@ -281,7 +281,7 @@ func TestUser(t *testing.T) {
281 281
 		"user_default",
282 282
 		"id",
283 283
 		[]string{},
284
-		[]string{"/var/lib/docker/images/ubuntu"},
284
+		[]string{testLayerPath},
285 285
 		&Config{},
286 286
 	)
287 287
 	if err != nil {
... ...
@@ -301,7 +301,7 @@ func TestUser(t *testing.T) {
301 301
 		"user_root",
302 302
 		"id",
303 303
 		[]string{},
304
-		[]string{"/var/lib/docker/images/ubuntu"},
304
+		[]string{testLayerPath},
305 305
 		&Config{
306 306
 			User: "root",
307 307
 		},
... ...
@@ -323,7 +323,7 @@ func TestUser(t *testing.T) {
323 323
 		"user_uid0",
324 324
 		"id",
325 325
 		[]string{},
326
-		[]string{"/var/lib/docker/images/ubuntu"},
326
+		[]string{testLayerPath},
327 327
 		&Config{
328 328
 			User: "0",
329 329
 		},
... ...
@@ -345,7 +345,7 @@ func TestUser(t *testing.T) {
345 345
 		"user_uid1",
346 346
 		"id",
347 347
 		[]string{},
348
-		[]string{"/var/lib/docker/images/ubuntu"},
348
+		[]string{testLayerPath},
349 349
 		&Config{
350 350
 			User: "1",
351 351
 		},
... ...
@@ -367,7 +367,7 @@ func TestUser(t *testing.T) {
367 367
 		"user_daemon",
368 368
 		"id",
369 369
 		[]string{},
370
-		[]string{"/var/lib/docker/images/ubuntu"},
370
+		[]string{testLayerPath},
371 371
 		&Config{
372 372
 			User: "daemon",
373 373
 		},
... ...
@@ -395,7 +395,7 @@ func TestMultipleContainers(t *testing.T) {
395 395
 		"container1",
396 396
 		"cat",
397 397
 		[]string{"/dev/zero"},
398
-		[]string{"/var/lib/docker/images/ubuntu"},
398
+		[]string{testLayerPath},
399 399
 		&Config{},
400 400
 	)
401 401
 	if err != nil {
... ...
@@ -407,7 +407,7 @@ func TestMultipleContainers(t *testing.T) {
407 407
 		"container2",
408 408
 		"cat",
409 409
 		[]string{"/dev/zero"},
410
-		[]string{"/var/lib/docker/images/ubuntu"},
410
+		[]string{testLayerPath},
411 411
 		&Config{},
412 412
 	)
413 413
 	if err != nil {
... ...
@@ -450,7 +450,7 @@ func TestStdin(t *testing.T) {
450 450
 		"stdin_test",
451 451
 		"cat",
452 452
 		[]string{},
453
-		[]string{"/var/lib/docker/images/ubuntu"},
453
+		[]string{testLayerPath},
454 454
 		&Config{
455 455
 			OpenStdin: true,
456 456
 		},
... ...
@@ -485,7 +485,7 @@ func TestTty(t *testing.T) {
485 485
 		"tty_test",
486 486
 		"cat",
487 487
 		[]string{},
488
-		[]string{"/var/lib/docker/images/ubuntu"},
488
+		[]string{testLayerPath},
489 489
 		&Config{
490 490
 			OpenStdin: true,
491 491
 		},
... ...
@@ -521,7 +521,7 @@ func BenchmarkRunSequencial(b *testing.B) {
521 521
 			fmt.Sprintf("bench_%v", i),
522 522
 			"echo",
523 523
 			[]string{"-n", "foo"},
524
-			[]string{"/var/lib/docker/images/ubuntu"},
524
+			[]string{testLayerPath},
525 525
 			&Config{},
526 526
 		)
527 527
 		if err != nil {
... ...
@@ -557,7 +557,7 @@ func BenchmarkRunParallel(b *testing.B) {
557 557
 				fmt.Sprintf("bench_%v", i),
558 558
 				"echo",
559 559
 				[]string{"-n", "foo"},
560
-				[]string{"/var/lib/docker/images/ubuntu"},
560
+				[]string{testLayerPath},
561 561
 				&Config{},
562 562
 			)
563 563
 			if err != nil {
... ...
@@ -1,16 +1,42 @@
1 1
 package docker
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"io/ioutil"
6
+	"log"
5 7
 	"os"
8
+	"os/exec"
6 9
 	"testing"
7 10
 )
8 11
 
9
-// Hack to run sys init during unit testing
12
+const testLayerPath string = "/var/lib/docker/images/docker-ut"
13
+
10 14
 func init() {
15
+	// Hack to run sys init during unit testing
11 16
 	if SelfPath() == "/sbin/init" {
12 17
 		SysInit()
13 18
 	}
19
+
20
+	// Make sure the unit test image is there, download otherwise
21
+	_, err := os.Stat(testLayerPath)
22
+	// The image is already there
23
+	if err == nil {
24
+		return
25
+	}
26
+	// Otherwise, we'll have to fetch it
27
+	if !os.IsNotExist(err) {
28
+		panic(err)
29
+	}
30
+	log.Printf("Test image not found. Downloading and extracting for the first time.")
31
+	if err := os.MkdirAll(testLayerPath, 0755); err != nil {
32
+		panic(err)
33
+	}
34
+	cmd := exec.Command("sh", "-c", fmt.Sprintf("wget https://s3.amazonaws.com/docker.io/images/base -O - | tar -jx -C %v/", testLayerPath))
35
+	cmd.Stdout = os.Stdout
36
+	cmd.Stderr = os.Stdout
37
+	if err := cmd.Run(); err != nil {
38
+		panic(err)
39
+	}
14 40
 }
15 41
 
16 42
 func newTestDocker() (*Docker, error) {
... ...
@@ -39,7 +65,7 @@ func TestCreate(t *testing.T) {
39 39
 		"test_create",
40 40
 		"ls",
41 41
 		[]string{"-al"},
42
-		[]string{"/var/lib/docker/images/ubuntu"},
42
+		[]string{testLayerPath},
43 43
 		&Config{},
44 44
 	)
45 45
 	if err != nil {
... ...
@@ -87,7 +113,7 @@ func TestDestroy(t *testing.T) {
87 87
 		"test_destroy",
88 88
 		"ls",
89 89
 		[]string{"-al"},
90
-		[]string{"/var/lib/docker/images/ubuntu"},
90
+		[]string{testLayerPath},
91 91
 		&Config{},
92 92
 	)
93 93
 	if err != nil {
... ...
@@ -135,7 +161,7 @@ func TestGet(t *testing.T) {
135 135
 		"test1",
136 136
 		"ls",
137 137
 		[]string{"-al"},
138
-		[]string{"/var/lib/docker/images/ubuntu"},
138
+		[]string{testLayerPath},
139 139
 		&Config{},
140 140
 	)
141 141
 	if err != nil {
... ...
@@ -147,7 +173,7 @@ func TestGet(t *testing.T) {
147 147
 		"test2",
148 148
 		"ls",
149 149
 		[]string{"-al"},
150
-		[]string{"/var/lib/docker/images/ubuntu"},
150
+		[]string{testLayerPath},
151 151
 		&Config{},
152 152
 	)
153 153
 	if err != nil {
... ...
@@ -159,7 +185,7 @@ func TestGet(t *testing.T) {
159 159
 		"test3",
160 160
 		"ls",
161 161
 		[]string{"-al"},
162
-		[]string{"/var/lib/docker/images/ubuntu"},
162
+		[]string{testLayerPath},
163 163
 		&Config{},
164 164
 	)
165 165
 	if err != nil {
... ...
@@ -196,7 +222,7 @@ func TestRestore(t *testing.T) {
196 196
 		"restore_test",
197 197
 		"ls",
198 198
 		[]string{"-al"},
199
-		[]string{"/var/lib/docker/images/ubuntu"},
199
+		[]string{testLayerPath},
200 200
 		&Config{},
201 201
 	)
202 202
 	if err != nil {
... ...
@@ -22,7 +22,7 @@ func newTestFilesystem(t *testing.T, layers []string) (rootfs string, fs *Filesy
22 22
 }
23 23
 
24 24
 func TestFilesystem(t *testing.T) {
25
-	_, filesystem := newTestFilesystem(t, []string{"/var/lib/docker/images/ubuntu"})
25
+	_, filesystem := newTestFilesystem(t, []string{testLayerPath})
26 26
 	if err := filesystem.Umount(); err == nil {
27 27
 		t.Errorf("Umount succeeded even though the filesystem was not mounted")
28 28
 	}
... ...
@@ -76,7 +76,7 @@ func TestFilesystemMultiLayer(t *testing.T) {
76 76
 	}
77 77
 
78 78
 	// Create the layered filesystem and add our fake layer on top
79
-	rootfs, filesystem := newTestFilesystem(t, []string{"/var/lib/docker/images/ubuntu", fakeLayer})
79
+	rootfs, filesystem := newTestFilesystem(t, []string{testLayerPath, fakeLayer})
80 80
 
81 81
 	// Mount it
82 82
 	if err := filesystem.Mount(); err != nil {
... ...
@@ -102,7 +102,7 @@ func TestFilesystemMultiLayer(t *testing.T) {
102 102
 }
103 103
 
104 104
 func TestChanges(t *testing.T) {
105
-	rootfs, filesystem := newTestFilesystem(t, []string{"/var/lib/docker/images/ubuntu"})
105
+	rootfs, filesystem := newTestFilesystem(t, []string{testLayerPath})
106 106
 	// Mount it
107 107
 	if err := filesystem.Mount(); err != nil {
108 108
 		t.Fatal(err)