forked from twz123/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_test.go
308 lines (251 loc) · 7.16 KB
/
build_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"archive/tar"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestBuildShCmdJSONEntrypoint(t *testing.T) {
name := "testbuildshcmdjsonentrypoint"
runBuild(t, name, withDockerfile(`
FROM busybox
ENTRYPOINT ["echo"]
CMD echo test
`))
}
func TestBuildEnvironmentReplacementUser(t *testing.T) {
name := "testbuildenvironmentreplacement"
runBuild(t, name, withDockerfile(`
FROM scratch
ENV user foo
USER ${user}
`))
}
func TestBuildEnvironmentReplacementVolume(t *testing.T) {
name := "testbuildenvironmentreplacement"
volumePath := "/quux"
if runtime.GOOS == "windows" {
volumePath = "c:/quux"
}
runBuild(t, name, withDockerfile(`
FROM busybox
ENV volume `+volumePath+`
VOLUME ${volume}
`))
}
func TestBuildEnvironmentReplacementExpose(t *testing.T) {
name := "testbuildenvironmentreplacement"
runBuild(t, name, withDockerfile(`
FROM scratch
ENV port 80
EXPOSE ${port}
ENV ports " 99 100 "
EXPOSE ${ports}
`))
}
func TestBuildEnvironmentReplacementWorkdir(t *testing.T) {
name := "testbuildenvironmentreplacement"
runBuild(t, name, withDockerfile(`
FROM busybox
ENV MYWORKDIR /work
RUN mkdir ${MYWORKDIR}
WORKDIR ${MYWORKDIR}
`))
}
func TestBuildFromScratch(t *testing.T) {
name := "testbuildfromscratch"
runBuild(t, name, withDockerfile(`
FROM scratch
COPY . .
`))
}
func TestBuildDockerfileNotInContext(t *testing.T) {
name := "testbuilddockerfilenotincontext"
run(t, "build", "-t", name, "-f", "testdata/Dockerfile.test-build-dockerfile-not-in-context", "types")
}
func TestBuildDockerfileNotInContextRoot(t *testing.T) {
name := "testbuilddockerfilenotincontextroot"
run(t, "build", "-t", name, "-f", "testdata/Dockerfile.test-build-dockerfile-not-in-context", ".")
}
// Make sure the client exits with the correct exit code.
// https://github.com/genuinetools/img/issues/101
func TestBuildDockerfileFailing(t *testing.T) {
name := "testbuilddockerfilefailing"
args := []string{"build", "-t", name, "-f", "testdata/Dockerfile.test-build-failing", "."}
out, err := doRun(args, nil)
if err == nil {
t.Logf("img %v should have failed but did not: %s", args, out)
t.FailNow()
}
}
// Using apt requires subuid, subgid, setgroups, and networking to be enabled.
// https://github.com/genuinetools/img/issues/96
func TestBuildAPT(t *testing.T) {
name := "testbuildapt"
runBuild(t, name, withDockerfile(`
FROM debian:9-slim
RUN apt update
`))
}
func TestBuildLabels(t *testing.T) {
name := "testbuildlabels"
args := []string{"build", "-t", name, "--label", "cli-label-1=cli1", "--label", "cli-label-2=cli2", "-"}
_, err := doRun(args, withDockerfile(`
FROM scratch as builder
LABEL stage "builder"
FROM scratch
LABEL stage "final"
`))
if err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
}
func TestBuildMultipleTags(t *testing.T) {
names := []string{"testbuildmultipletags", "testbuildmultipletags:v1", "testbuildmultipletagsv1"}
args := []string{"build"}
for _, name := range names {
args = append(args, "-t", name)
}
args = append(args, "-")
_, err := doRun(args, withDockerfile(`
FROM scratch
`))
if err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
}
func TestBuildMultiplePlatforms(t *testing.T) {
args := []string{"build", "--platform", "amd64", "--platform", "linux/arm64,linux/arm/v7", "-t", "testbuildplatforms", "-"}
_, err := doRun(args, withDockerfile(`
FROM alpine
`))
if err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
}
func TestBuildContextFirstInCommand(t *testing.T) {
args := []string{"build", "-", "-t", "testbuildargsfirst"}
_, err := doRun(args, withDockerfile(`
FROM busybox
`))
if err != nil {
t.Logf("img %v failed unexpectedly: %v", args, err)
t.FailNow()
}
}
func TestBuildOutputLocal(t *testing.T) {
tmpd, err := ioutil.TempDir("", "img-buildoutputlocal")
if err != nil {
t.Fatalf("creating temporary directory for build output failed: %v", err)
}
defer os.RemoveAll(tmpd)
rootfs := filepath.Join(tmpd, "rootfs")
args := []string{"build", "-", "-o", fmt.Sprintf("type=local,dest=%s", rootfs)}
_, err = doRun(args, withDockerfile(`
FROM busybox
RUN touch /imgout
`))
if err != nil {
t.Fatalf("img %v failed unexpectedly: %v", args, err)
}
// Make sure the image actually is unpacked in the directory.
file := filepath.Join(rootfs, "imgout")
if _, err := os.Stat(file); os.IsNotExist(err) {
t.Fatalf("expected file at %q to exist but it did not", file)
}
}
func testBuildOutputArchive(otype string, t *testing.T) {
tmpd, err := ioutil.TempDir("", "img-buildoutput"+otype)
if err != nil {
t.Fatalf("creating temporary directory for build output failed: %v", err)
}
defer os.RemoveAll(tmpd)
archive := filepath.Join(tmpd, "output.tar")
args := []string{"build", "-", "-o", fmt.Sprintf("type=%s,dest=%s", otype, archive)}
_, err = doRun(args, withDockerfile(`
FROM busybox
`))
if err != nil {
t.Fatalf("img %v failed unexpectedly: %v", args, err)
}
// Make sure the output is a valid tar archive.
f, err := os.Open(archive)
if err != nil {
t.Fatalf("could not open output archive at %q: %s", archive, err)
}
defer f.Close()
tr := tar.NewReader(f)
if _, err = tr.Next(); err != nil {
t.Fatalf("could not read first item in %s archive: %s", otype, err)
}
}
func TestBuildOutputTar(t *testing.T) {
testBuildOutputArchive("tar", t)
}
func TestBuildOutputDocker(t *testing.T) {
testBuildOutputArchive("docker", t)
}
func TestBuildOutputOCI(t *testing.T) {
testBuildOutputArchive("oci", t)
}
func TestBuildOutputTarStdout(t *testing.T) {
args := []string{"build", "-", "-o", "type=tar"}
// modified doRun() function to capture stdout seperately
doRunStdout := func(args []string, stdin io.Reader) ([]byte, error) {
prog := "./testimg" + exeSuffix
newargs := []string{args[0], "--state", testStateDir}
newargs = append(newargs, args[1:]...)
cmd := exec.Command(prog, newargs...)
if stdin != nil {
cmd.Stdin = stdin
}
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("Error running %s: %v", strings.Join(newargs, " "), err)
}
return out, nil
}
out, err := doRunStdout(args, withDockerfile(`
FROM busybox
`))
if err != nil {
t.Fatalf("img %v failed unexpectedly: %v", args, err)
}
// try to read tar entry from stdout
tr := tar.NewReader(bytes.NewReader(out))
if _, err = tr.Next(); err != nil {
t.Logf("could not read tar archive from stdout: %s", err)
t.Logf("first 256 bytes: %s", out[:256])
t.FailNow()
}
}
func TestBuildOutputImage(t *testing.T) {
name := "testbuildoutputimage"
args := []string{"build", "-", "-o", fmt.Sprintf("type=image,name=%s", name)}
_, err := doRun(args, withDockerfile(`
FROM busybox
`))
if err != nil {
t.Fatalf("img %v failed unexpectedly: %v", args, err)
}
}
func TestBuildOutputImageFailing(t *testing.T) {
name := "testbuildoutputimagefailing"
args := []string{"build", "-", "-o", fmt.Sprintf("type=image,dest=%s", name)}
out, err := doRun(args, withDockerfile(`
FROM busybox
`))
if err == nil {
t.Fatalf("img %v should have failed but did not: %s", args, out)
}
}