Browse code

Dockerignore tests

Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>

Tomasz Kopczynski authored on 2016/03/06 05:35:06
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,177 @@
0
+package builder
1
+
2
+import (
3
+	"io/ioutil"
4
+	"log"
5
+	"os"
6
+	"path/filepath"
7
+	"testing"
8
+)
9
+
10
+func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) {
11
+	contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test")
12
+
13
+	if err != nil {
14
+		t.Fatalf("Error with creating temporary directory: %s", err)
15
+	}
16
+
17
+	defer os.RemoveAll(contextDir)
18
+
19
+	testFilename := filepath.Join(contextDir, "should_stay")
20
+	testContent := "test"
21
+	err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
22
+
23
+	if err != nil {
24
+		t.Fatalf("Error when creating should_stay file: %s", err)
25
+	}
26
+
27
+	dockerignoreFilename := filepath.Join(contextDir, ".dockerignore")
28
+	dockerignoreContent := "Dockerfile\n.dockerignore"
29
+	err = ioutil.WriteFile(dockerignoreFilename, []byte(dockerignoreContent), 0777)
30
+
31
+	if err != nil {
32
+		t.Fatalf("Error when creating .dockerignore file: %s", err)
33
+	}
34
+
35
+	dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
36
+	dockerfileContent := "FROM busybox"
37
+	err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
38
+
39
+	if err != nil {
40
+		t.Fatalf("Error when creating Dockerfile file: %s", err)
41
+	}
42
+
43
+	modifiableCtx := &tarSumContext{root: contextDir}
44
+	ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
45
+
46
+	err = ctx.Process([]string{DefaultDockerfileName})
47
+
48
+	if err != nil {
49
+		t.Fatalf("Error when executing Process: %s", err)
50
+	}
51
+
52
+	files, err := ioutil.ReadDir(contextDir)
53
+
54
+	if err != nil {
55
+		t.Fatalf("Could not read directory: %s", err)
56
+	}
57
+
58
+	if len(files) != 1 {
59
+		log.Fatal("Directory should contain exactly one file")
60
+	}
61
+
62
+	for _, file := range files {
63
+		if "should_stay" != file.Name() {
64
+			log.Fatalf("File %s should not be in the directory", file.Name())
65
+		}
66
+	}
67
+
68
+}
69
+
70
+func TestProcessNoDockerignore(t *testing.T) {
71
+	contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test")
72
+
73
+	if err != nil {
74
+		t.Fatalf("Error with creating temporary directory: %s", err)
75
+	}
76
+
77
+	defer os.RemoveAll(contextDir)
78
+
79
+	testFilename := filepath.Join(contextDir, "should_stay")
80
+	testContent := "test"
81
+	err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
82
+
83
+	if err != nil {
84
+		t.Fatalf("Error when creating should_stay file: %s", err)
85
+	}
86
+
87
+	dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
88
+	dockerfileContent := "FROM busybox"
89
+	err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
90
+
91
+	if err != nil {
92
+		t.Fatalf("Error when creating Dockerfile file: %s", err)
93
+	}
94
+
95
+	modifiableCtx := &tarSumContext{root: contextDir}
96
+	ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
97
+
98
+	ctx.Process([]string{DefaultDockerfileName})
99
+
100
+	files, err := ioutil.ReadDir(contextDir)
101
+
102
+	if err != nil {
103
+		t.Fatalf("Could not read directory: %s", err)
104
+	}
105
+
106
+	if len(files) != 2 {
107
+		log.Fatal("Directory should contain exactly two files")
108
+	}
109
+
110
+	for _, file := range files {
111
+		if "should_stay" != file.Name() && DefaultDockerfileName != file.Name() {
112
+			log.Fatalf("File %s should not be in the directory", file.Name())
113
+		}
114
+	}
115
+
116
+}
117
+
118
+func TestProcessShouldLeaveAllFiles(t *testing.T) {
119
+	contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test")
120
+
121
+	if err != nil {
122
+		t.Fatalf("Error with creating temporary directory: %s", err)
123
+	}
124
+
125
+	defer os.RemoveAll(contextDir)
126
+
127
+	testFilename := filepath.Join(contextDir, "should_stay")
128
+	testContent := "test"
129
+	err = ioutil.WriteFile(testFilename, []byte(testContent), 0777)
130
+
131
+	if err != nil {
132
+		t.Fatalf("Error when creating should_stay file: %s", err)
133
+	}
134
+
135
+	dockerignoreFilename := filepath.Join(contextDir, ".dockerignore")
136
+	dockerignoreContent := "input1\ninput2"
137
+	err = ioutil.WriteFile(dockerignoreFilename, []byte(dockerignoreContent), 0777)
138
+
139
+	if err != nil {
140
+		t.Fatalf("Error when creating .dockerignore file: %s", err)
141
+	}
142
+
143
+	dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName)
144
+	dockerfileContent := "FROM busybox"
145
+	err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777)
146
+
147
+	if err != nil {
148
+		t.Fatalf("Error when creating Dockerfile file: %s", err)
149
+	}
150
+
151
+	modifiableCtx := &tarSumContext{root: contextDir}
152
+	ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx}
153
+
154
+	err = ctx.Process([]string{DefaultDockerfileName})
155
+
156
+	if err != nil {
157
+		t.Fatalf("Error when executing Process: %s", err)
158
+	}
159
+
160
+	files, err := ioutil.ReadDir(contextDir)
161
+
162
+	if err != nil {
163
+		t.Fatalf("Could not read directory: %s", err)
164
+	}
165
+
166
+	if len(files) != 3 {
167
+		log.Fatal("Directory should contain exactly three files")
168
+	}
169
+
170
+	for _, file := range files {
171
+		if "should_stay" != file.Name() && DefaultDockerfileName != file.Name() && ".dockerignore" != file.Name() {
172
+			log.Fatalf("File %s should not be in the directory", file.Name())
173
+		}
174
+	}
175
+
176
+}