-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
build_test.go
161 lines (141 loc) · 4.67 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
package image
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/docker/cli/e2e/internal/fixtures"
"github.com/docker/cli/internal/test/environment"
"github.com/docker/cli/internal/test/output"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/fs"
"gotest.tools/v3/icmd"
"gotest.tools/v3/skip"
)
func TestBuildFromContextDirectoryWithTag(t *testing.T) {
t.Setenv("DOCKER_BUILDKIT", "0")
dir := fs.NewDir(t, "test-build-context-dir",
fs.WithFile("run", "echo running", fs.WithMode(0o755)),
fs.WithDir("data", fs.WithFile("one", "1111")),
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
COPY run /usr/bin/run
RUN run
COPY data /data
`, fixtures.AlpineImage)))
defer dir.Remove()
result := icmd.RunCmd(
icmd.Command("docker", "build", "-t", "myimage", "."),
withWorkingDir(dir))
defer icmd.RunCommand("docker", "image", "rm", "myimage")
const buildkitDisabledWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0
environment-variable.
`
result.Assert(t, icmd.Expected{Err: buildkitDisabledWarning})
output.Assert(t, result.Stdout(), map[int]func(string) error{
0: output.Prefix("Sending build context to Docker daemon"),
1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:frozen"),
3: output.Suffix("Step 2/4 : COPY run /usr/bin/run"),
5: output.Suffix("Step 3/4 : RUN run"),
7: output.Suffix("running"),
// TODO(krissetto): ugly, remove when no longer testing against moby 24. see https://github.com/moby/moby/pull/46270
8: func(s string) error {
err := output.Contains("Removed intermediate container")(s) // moby >= v25
if err == nil {
return nil
}
return output.Contains("Removing intermediate container")(s) // moby < v25
},
10: output.Suffix("Step 4/4 : COPY data /data"),
12: output.Contains("Successfully built "),
13: output.Suffix("Successfully tagged myimage:latest"),
})
}
func TestTrustedBuild(t *testing.T) {
skip.If(t, environment.RemoteDaemon())
t.Setenv("DOCKER_BUILDKIT", "0")
dir := fixtures.SetupConfigFile(t)
defer dir.Remove()
image1 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build1", "latest")
image2 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build2", "latest")
buildDir := fs.NewDir(t, "test-trusted-build-context-dir",
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s as build-base
RUN echo ok > /foo
FROM %s
COPY --from=build-base foo bar
`, image1, image2)))
defer buildDir.Remove()
result := icmd.RunCmd(
icmd.Command("docker", "build", "-t", "myimage", "."),
withWorkingDir(buildDir),
fixtures.WithConfig(dir.Path()),
fixtures.WithTrust,
fixtures.WithNotary,
)
result.Assert(t, icmd.Expected{
Out: fmt.Sprintf("FROM %s@sha", image1[:len(image1)-7]),
Err: fmt.Sprintf("Tagging %s@sha", image1[:len(image1)-7]),
})
result.Assert(t, icmd.Expected{
Out: fmt.Sprintf("FROM %s@sha", image2[:len(image2)-7]),
})
}
func TestTrustedBuildUntrustedImage(t *testing.T) {
skip.If(t, environment.RemoteDaemon())
t.Setenv("DOCKER_BUILDKIT", "0")
dir := fixtures.SetupConfigFile(t)
defer dir.Remove()
buildDir := fs.NewDir(t, "test-trusted-build-context-dir",
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
RUN []
`, fixtures.AlpineImage)))
defer buildDir.Remove()
result := icmd.RunCmd(
icmd.Command("docker", "build", "-t", "myimage", "."),
withWorkingDir(buildDir),
fixtures.WithConfig(dir.Path()),
fixtures.WithTrust,
fixtures.WithNotary,
)
result.Assert(t, icmd.Expected{
ExitCode: 1,
Err: "does not have trust data for",
})
}
func TestBuildIidFileSquash(t *testing.T) {
environment.SkipIfNotExperimentalDaemon(t)
t.Setenv("DOCKER_BUILDKIT", "0")
dir := fs.NewDir(t, "test-iidfile-squash")
defer dir.Remove()
iidfile := filepath.Join(dir.Path(), "idsquash")
buildDir := fs.NewDir(t, "test-iidfile-squash-build",
fs.WithFile("Dockerfile", fmt.Sprintf(`
FROM %s
ENV FOO=FOO
ENV BAR=BAR
RUN touch /fiip
RUN touch /foop`, fixtures.AlpineImage)),
)
defer buildDir.Remove()
imageTag := "testbuildiidfilesquash"
result := icmd.RunCmd(
icmd.Command("docker", "build", "--iidfile", iidfile, "--squash", "-t", imageTag, "."),
withWorkingDir(buildDir),
)
result.Assert(t, icmd.Success)
id, err := os.ReadFile(iidfile)
assert.NilError(t, err)
result = icmd.RunCommand("docker", "image", "inspect", "-f", "{{.Id}}", imageTag)
result.Assert(t, icmd.Success)
assert.Check(t, is.Equal(string(id), strings.TrimSpace(result.Combined())))
}
func withWorkingDir(dir *fs.Dir) func(*icmd.Cmd) {
return func(cmd *icmd.Cmd) {
cmd.Dir = dir.Path()
}
}