This repository was archived by the owner on Jun 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathdockerfile_test.go
More file actions
277 lines (260 loc) · 6.87 KB
/
Copy pathdockerfile_test.go
File metadata and controls
277 lines (260 loc) · 6.87 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package dockerfile
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)
func TestDockerfile_GetExposedPort(t *testing.T) {
wantedPath := "./Dockerfile"
testCases := map[string]struct {
dockerfilePath string
dockerfile []byte
wantedPorts []Port
wantedErr error
}{
"no exposed ports": {
dockerfilePath: wantedPath,
dockerfile: []byte(`
FROM nginx
ARG arg=80`),
wantedPorts: []Port{},
wantedErr: ErrNoExpose{Dockerfile: wantedPath},
},
"one exposed port": {
dockerfilePath: wantedPath,
dockerfile: []byte(`
FROM nginx
EXPOSE 8080
`),
wantedPorts: []Port{
{
Port: 8080,
RawString: "8080",
},
},
},
"two exposed ports": {
dockerfilePath: wantedPath,
dockerfile: []byte(`
FROM nginx
EXPOSE 8080
EXPOSE 80`),
wantedPorts: []Port{
{
Port: 8080,
RawString: "8080",
},
{
Port: 80,
RawString: "80",
},
},
},
"two exposed ports one line": {
dockerfilePath: wantedPath,
dockerfile: []byte(`
FROM nginx
EXPOSE 80/tcp 3000`),
wantedPorts: []Port{
{
Port: 80,
Protocol: "tcp",
RawString: "80/tcp",
},
{
Port: 3000,
RawString: "3000",
},
},
},
"bad expose token single port": {
dockerfilePath: wantedPath,
dockerfile: []byte(`
FROM nginx
EXPOSE $arg
`),
wantedPorts: nil,
wantedErr: ErrInvalidPort{Match: "EXPOSE $arg"},
},
"bad expose token multiple ports": {
dockerfilePath: wantedPath,
dockerfile: []byte(`
FROM nginx
EXPOSE 80
EXPOSE $arg
EXPOSE 8080/tcp 5000`),
wantedPorts: nil,
wantedErr: ErrInvalidPort{Match: "EXPOSE $arg"},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
fs := afero.Afero{Fs: afero.NewMemMapFs()}
err := fs.WriteFile("./Dockerfile", tc.dockerfile, 0644)
if err != nil {
t.FailNow()
}
// Ensure the dockerfile is parse-able by Docker.
dat, err := fs.ReadFile("./Dockerfile")
require.NoError(t, err)
ast, err := parser.Parse(bytes.NewReader(dat))
require.NoError(t, err)
stages, _, _ := instructions.Parse(ast.AST)
ports, err := New(fs, "./Dockerfile").GetExposedPorts()
if tc.wantedErr != nil {
require.EqualError(t, err, tc.wantedErr.Error())
require.Nil(t, ports)
} else {
require.NoError(t, err)
require.ElementsMatch(t, tc.wantedPorts, ports, "expected ports do not match")
// Compare our parsing against Docker's.
var portsFromDocker []string
for _, cmd := range stages[0].Commands {
switch v := cmd.(type) {
case *instructions.ExposeCommand:
portsFromDocker = append(portsFromDocker, v.Ports...)
}
}
require.ElementsMatch(t, portsFromDocker, stringifyPorts(ports), "ports from Docker do not match")
}
})
}
}
func TestDockerfile_GetHealthCheck(t *testing.T) {
testCases := map[string]struct {
dockerfilePath string
dockerfile []byte
wantedConfig *HealthCheck
wantedErr error
}{
"correctly parses healthcheck with default values": {
dockerfile: []byte(`
FROM nginx
HEALTHCHECK CMD curl -f http://localhost/ || exit 1
`),
wantedErr: nil,
wantedConfig: &HealthCheck{
Interval: 10 * time.Second,
Timeout: 5 * time.Second,
StartPeriod: 0,
Retries: 2,
Cmd: []string{cmdShell, "curl -f http://localhost/ || exit 1"},
},
},
"correctly parses healthcheck with user's values": {
dockerfile: []byte(`
FROM nginx
HEALTHCHECK --interval=5m --timeout=3s --start-period=2s --retries=3 \
CMD curl -f http://localhost/ || exit 1`),
wantedErr: nil,
wantedConfig: &HealthCheck{
Interval: 300 * time.Second,
Timeout: 3 * time.Second,
StartPeriod: 2 * time.Second,
Retries: 3,
Cmd: []string{cmdShell, "curl -f http://localhost/ || exit 1"},
},
},
"correctly parses healthcheck with NONE": {
dockerfile: []byte(`
FROM nginx
HEALTHCHECK NONE
`),
wantedErr: nil,
wantedConfig: nil,
},
"correctly parses no healthchecks": {
dockerfile: []byte(`FROM nginx`),
wantedErr: nil,
wantedConfig: nil,
},
"correctly parses HEALTHCHECK instruction with awkward spacing": {
dockerfile: []byte(`
FROM nginx
HEALTHCHECK CMD a b
`),
wantedErr: nil,
wantedConfig: &HealthCheck{
Interval: 10 * time.Second,
Timeout: 5 * time.Second,
Retries: 2,
Cmd: []string{cmdShell, "a b"},
},
},
"correctly parses HEALTHCHECK instruction with exec array format": {
dockerfile: []byte(`
FROM nginx
EXPOSE 80
HEALTHCHECK CMD ["a", "b"]
`),
wantedErr: nil,
wantedConfig: &HealthCheck{
Interval: 10 * time.Second,
Timeout: 5 * time.Second,
Retries: 2,
Cmd: []string{"CMD", "a", "b"},
},
},
"healthcheck contains an invalid flag": {
dockerfile: []byte(`HEALTHCHECK --interval=5m --randomFlag=4s CMD curl -f http://localhost/ || exit 1`),
wantedErr: fmt.Errorf("parse instructions: Unknown flag: randomFlag"),
},
"healthcheck does not contain CMD": {
dockerfile: []byte(`HEALTHCHECK --interval=5m curl -f http://localhost/ || exit 1`),
wantedErr: fmt.Errorf("parse instructions: Unknown type \"CURL\" in HEALTHCHECK (try CMD)"),
},
"healthcheck does not contain command": {
dockerfile: []byte(`HEALTHCHECK --interval=5m CMD`),
wantedErr: fmt.Errorf("parse instructions: Missing command after HEALTHCHECK CMD"),
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
fs := afero.Afero{Fs: afero.NewMemMapFs()}
err := fs.WriteFile("./Dockerfile", tc.dockerfile, 0644)
if err != nil {
t.FailNow()
}
// Ensure the dockerfile is parse-able by Docker.
dat, err := fs.ReadFile("./Dockerfile")
require.NoError(t, err)
ast, err := parser.Parse(bytes.NewReader(dat))
require.NoError(t, err)
stages, _, _ := instructions.Parse(ast.AST)
hc, err := New(fs, "./Dockerfile").GetHealthCheck()
if tc.wantedErr != nil {
require.EqualError(t, err, tc.wantedErr.Error())
} else {
require.NoError(t, err)
require.Equal(t, tc.wantedConfig, hc, "healthcheck configs do not match")
for _, cmd := range stages[0].Commands {
switch v := cmd.(type) {
case *instructions.HealthCheckCommand:
if tc.wantedConfig == nil {
require.Equal(t, []string{"NONE"}, v.Health.Test, "expected NONE from Docker healthcheck")
} else {
require.Equal(t, tc.wantedConfig.Cmd, v.Health.Test, "Docker CMD instructions do not match")
}
}
}
}
})
}
}
func stringifyPorts(ports []Port) []string {
var arr []string
for _, p := range ports {
if p.err != nil {
continue
}
arr = append(arr, p.String())
}
return arr
}