Browse code

Add builder_test.go

Guillaume J. Charmes authored on 2013/04/26 03:20:56
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,88 @@
0
+package docker
1
+
2
+import (
3
+	"strings"
4
+	"testing"
5
+)
6
+
7
+const Dockerfile = `
8
+# VERSION		0.1
9
+# DOCKER-VERSION	0.1.6
10
+
11
+from	docker-ut
12
+run	sh -c 'echo root:testpass > /tmp/passwd'
13
+run	mkdir -p /var/run/sshd
14
+copy	https://raw.github.com/dotcloud/docker/master/CHANGELOG.md /tmp/CHANGELOG.md
15
+`
16
+
17
+func TestBuild(t *testing.T) {
18
+	runtime, err := newTestRuntime()
19
+	if err != nil {
20
+		t.Fatal(err)
21
+	}
22
+	defer nuke(runtime)
23
+
24
+	builder := NewBuilder(runtime)
25
+
26
+	img, err := builder.Build(strings.NewReader(Dockerfile), &nopWriter{})
27
+	if err != nil {
28
+		t.Fatal(err)
29
+	}
30
+
31
+	container, err := builder.Create(
32
+		&Config{
33
+			Image: img.Id,
34
+			Cmd:   []string{"cat", "/tmp/passwd"},
35
+		},
36
+	)
37
+	if err != nil {
38
+		t.Fatal(err)
39
+	}
40
+	defer runtime.Destroy(container)
41
+
42
+	output, err := container.Output()
43
+	if err != nil {
44
+		t.Fatal(err)
45
+	}
46
+	if string(output) != "root:testpass\n" {
47
+		t.Fatalf("Unexpected output. Read '%s', expected '%s'", output, "root:testpass\n")
48
+	}
49
+
50
+	container2, err := builder.Create(
51
+		&Config{
52
+			Image: img.Id,
53
+			Cmd:   []string{"ls", "-d", "/var/run/sshd"},
54
+		},
55
+	)
56
+	if err != nil {
57
+		t.Fatal(err)
58
+	}
59
+	defer runtime.Destroy(container2)
60
+
61
+	output, err = container2.Output()
62
+	if err != nil {
63
+		t.Fatal(err)
64
+	}
65
+	if string(output) != "/var/run/sshd\n" {
66
+		t.Fatal("/var/run/sshd has not been created")
67
+	}
68
+
69
+	container3, err := builder.Create(
70
+		&Config{
71
+			Image: img.Id,
72
+			Cmd:   []string{"cat", "/tmp/CHANGELOG.md"},
73
+		},
74
+	)
75
+	if err != nil {
76
+		t.Fatal(err)
77
+	}
78
+	defer runtime.Destroy(container3)
79
+
80
+	output, err = container3.Output()
81
+	if err != nil {
82
+		t.Fatal(err)
83
+	}
84
+	if len(output) == 0 {
85
+		t.Fatal("/tmp/CHANGELOG.md has not been copied")
86
+	}
87
+}