-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathparse_test.go
More file actions
475 lines (397 loc) · 13.9 KB
/
parse_test.go
File metadata and controls
475 lines (397 loc) · 13.9 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package parse
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestParseDockerfile tests the main ParseDockerfile function with various Dockerfile examples.
// It validates that the parser correctly identifies violations based on the content of the Dockerfile.
// Each test case includes:
// - A complete Dockerfile as a string
// - Expected rule codes that should be present in the result
func TestParseDockerfile(t *testing.T) {
tests := []struct {
name string
dockerfileContent string
expectedRules []string // Rule codes that should be present
}{
{
name: "FROM with mixed casing - uppercase FROM with lowercase as",
dockerfileContent: `FROM debian:latest as builder
RUN echo "hello"`,
expectedRules: []string{"FromAsCasing"},
},
{
name: "FROM with mixed casing - lowercase from with uppercase AS",
dockerfileContent: `from debian:latest AS builder
RUN echo "hello"`,
expectedRules: []string{"ConsistentInstructionCasing", "FromAsCasing"},
},
{
name: "FROM with consistent uppercase casing",
dockerfileContent: `FROM debian:latest AS builder
RUN echo "hello"`,
expectedRules: []string{},
},
{
name: "FROM with consistent lowercase casing",
dockerfileContent: `from debian:latest as builder
run echo "hello"`,
expectedRules: []string{},
},
{
name: "WORKDIR with relative path",
dockerfileContent: `FROM alpine:latest
WORKDIR usr/share/nginx/html`,
expectedRules: []string{"WorkdirRelativePath"},
},
{
name: "WORKDIR with absolute path",
dockerfileContent: `FROM alpine:latest
WORKDIR /usr/share/nginx/html`,
expectedRules: []string{},
},
{
name: "WORKDIR without argument",
dockerfileContent: `FROM alpine:latest
WORKDIR`,
expectedRules: []string{"InvalidInstruction"},
},
{
name: "FROM without argument",
dockerfileContent: `FROM
RUN echo "hello"`,
expectedRules: []string{"InvalidInstruction"},
},
{
name: "Multiple violations - FROM casing and WORKDIR relative path",
dockerfileContent: `FROM debian:latest as builder
WORKDIR app
RUN echo "hello"`,
expectedRules: []string{"FromAsCasing", "WorkdirRelativePath"},
},
{
name: "Complex Dockerfile with multiple stages - all valid",
dockerfileContent: `FROM node:18-alpine AS builder
WORKDIR /app
RUN npm install
FROM nginx:alpine AS runtime
WORKDIR /usr/share/nginx/html
RUN echo "done"`,
expectedRules: []string{},
},
{
name: "Complex Dockerfile with multiple violations",
dockerfileContent: `FROM node:18-alpine as builder
WORKDIR app
RUN npm install
from nginx:alpine AS runtime
WORKDIR usr/share/nginx/html`,
expectedRules: []string{"ConsistentInstructionCasing", "FromAsCasing", "WorkdirRelativePath", "FromAsCasing", "WorkdirRelativePath"},
},
{
name: "Simple valid Dockerfile",
dockerfileContent: `FROM alpine:latest
RUN echo "Hello World"`,
expectedRules: []string{},
},
{
name: "Dockerfile with only FROM",
dockerfileContent: `FROM ubuntu:22.04`,
expectedRules: []string{},
},
{
name: "EXPOSE with IP address and port mapping",
dockerfileContent: `FROM alpine
EXPOSE 127.0.0.1:80:80`,
expectedRules: []string{"ExposeInvalidFormat"},
},
{
name: "EXPOSE with host-port mapping",
dockerfileContent: `FROM alpine
EXPOSE 80:80`,
expectedRules: []string{"ExposeInvalidFormat"},
},
{
name: "EXPOSE with valid port",
dockerfileContent: `FROM alpine
EXPOSE 80`,
expectedRules: []string{},
},
{
name: "EXPOSE with valid port and protocol",
dockerfileContent: `FROM alpine
EXPOSE 80/tcp`,
expectedRules: []string{},
},
{
name: "EXPOSE without argument",
dockerfileContent: `FROM alpine
EXPOSE`,
expectedRules: []string{"InvalidInstruction"},
},
{
name: "EXPOSE with multiple ports - some invalid",
dockerfileContent: `FROM alpine
EXPOSE 80 8080:8080 443`,
expectedRules: []string{"ExposeInvalidFormat"},
},
{
name: "WORKDIR with relative path using dot",
dockerfileContent: `FROM alpine:latest
WORKDIR ./build`,
expectedRules: []string{"WorkdirRelativePath"},
},
{
name: "WORKDIR with relative path using double dot",
dockerfileContent: `FROM alpine:latest
WORKDIR ../parent`,
expectedRules: []string{"WorkdirRelativePath"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ParseDockerfile(tt.dockerfileContent)
require.NoError(t, err, "ParseDockerfile should not return an error")
require.NotNil(t, result, "ParseDockerfile should return a non-nil result")
// Check the number of rules
require.Equal(t, len(result.Rules), len(tt.expectedRules), "Number of rules does not match expected")
// Collect actual rule codes
actualRuleCodes := make(map[string]bool)
for _, rule := range result.Rules {
actualRuleCodes[rule.Code] = true
}
// Check that all expected rules are present
for _, expectedCode := range tt.expectedRules {
require.True(t, actualRuleCodes[expectedCode],
"Expected rule %q to be present in results, but it was not found. Actual rules: %v",
expectedCode, getRuleCodes(result.Rules))
}
})
}
}
func TestParseDockerfile_InvalidSyntax(t *testing.T) {
t.Run("completely invalid Dockerfile syntax", func(t *testing.T) {
dockerfileContent := `this is not a valid dockerfile
it has no instructions
just random text`
result, err := ParseDockerfile(dockerfileContent)
// The parser might not return an error for unknown instructions
// but should still return a result
require.NoError(t, err)
require.NotNil(t, result)
})
}
func TestParseDockerfile_EmptyDockerfile(t *testing.T) {
t.Run("empty Dockerfile", func(t *testing.T) {
result, err := ParseDockerfile("")
// Empty Dockerfiles return an error from the parser
require.Error(t, err)
require.Nil(t, result)
require.Contains(t, err.Error(), "no instructions")
})
}
func TestParseDockerfile_RuleDetails(t *testing.T) {
t.Run("verify rule details for FromAsCasing", func(t *testing.T) {
dockerfileContent := `FROM debian:latest as builder`
result, err := ParseDockerfile(dockerfileContent)
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Rules, 1, "Expected exactly 1 rule")
rule := result.Rules[0]
require.Equal(t, "FromAsCasing", rule.Code)
require.NotEmpty(t, rule.Description, "Rule should have a description")
require.NotEmpty(t, rule.Url, "Rule should have a documentation URL")
require.Equal(t, 1, rule.StartLine, "Rule should start at line 1")
require.Equal(t, 1, rule.EndLine, "Rule should end at line 1")
})
t.Run("verify rule details for WorkdirRelativePath", func(t *testing.T) {
dockerfileContent := `FROM alpine:latest
WORKDIR app`
result, err := ParseDockerfile(dockerfileContent)
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Rules, 1, "Expected exactly 1 rule")
rule := result.Rules[0]
require.Equal(t, "WorkdirRelativePath", rule.Code)
require.NotEmpty(t, rule.Description, "Rule should have a description")
require.NotEmpty(t, rule.Url, "Rule should have a documentation URL")
require.Equal(t, 2, rule.StartLine, "Rule should start at line 2")
require.Equal(t, 2, rule.EndLine, "Rule should end at line 2")
})
t.Run("verify rule details for InvalidInstruction", func(t *testing.T) {
dockerfileContent := `FROM alpine:latest
WORKDIR`
result, err := ParseDockerfile(dockerfileContent)
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Rules, 1, "Expected exactly 1 rule")
rule := result.Rules[0]
require.Equal(t, "InvalidInstruction", rule.Code)
require.NotEmpty(t, rule.Description, "Rule should have a description")
require.Equal(t, 2, rule.StartLine, "Rule should start at line 2")
require.Equal(t, 2, rule.EndLine, "Rule should end at line 2")
})
}
func TestParseDockerfile_MultipleViolationsSameLine(t *testing.T) {
t.Run("single line can only have one violation per instruction", func(t *testing.T) {
// Each instruction is on its own line, so we test multiple instructions
dockerfileContent := `FROM debian:latest as builder
WORKDIR app`
result, err := ParseDockerfile(dockerfileContent)
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Rules, 2, "Expected 2 rules (one per violation)")
// Verify we have both violations
ruleCodes := getRuleCodes(result.Rules)
require.Contains(t, ruleCodes, "FromAsCasing")
require.Contains(t, ruleCodes, "WorkdirRelativePath")
})
}
func TestParseDockerfile_CompleteValidDockerfile(t *testing.T) {
t.Run("comprehensive valid Dockerfile with multiple instructions", func(t *testing.T) {
dockerfileContent := `# A comment
# FROM instruction valid examples
ARG NODE_VERSION=18
FROM debian:latest
FROM alpine:3.18
FROM debian:latest AS builder
FROM debian:latest AS deb-builder
FROM node:${NODE_VERSION}-alpine AS node-builder
# RUN instruction valid examples
RUN npm ci --only=production
RUN npm run build
# CMD instruction valid examples
CMD ["nginx", "-g", "daemon off;"]
# LABEL instruction valid examples
LABEL maintainer="[email protected]"
LABEL version="1.0.0"
LABEL org.opencontainers.image.source="https://github.com/example/repo"
# EXPOSE instruction valid examples
EXPOSE 80
EXPOSE 443
# ENV instruction valid examples
ENV NODE_ENV=production
ENV APP_PORT=3000
# ADD instruction valid examples
ADD file1.txt file2.txt /usr/src/things/
ADD https://example.com/archive.zip /usr/src/things/
ADD [email protected]:user/repo.git /usr/src/things/
# COPY instruction valid examples
COPY package*.json ./
COPY . .
COPY --from=builder /app/dist .
# ENTRYPOINT instruction valid examples
ENTRYPOINT ["executable", "param1", "param2"]
# VOLUME instruction valid examples
VOLUME ["/var/log/nginx"]
# USER instruction valid examples
USER nginx
# WORKDIR instruction valid examples
WORKDIR /app
WORKDIR /usr/share/nginx/html
# ARG instruction valid examples
ARG NODE_VERSION=18
ARG APP_ENV=production
# ONBUILD instruction valid examples
ONBUILD RUN echo "building"
ONBUILD ADD . /app
ONBUILD COPY requirements.txt /app/
# STOPSIGNAL instruction valid examples
STOPSIGNAL SIGTERM
# HEALTHCHECK instruction valid examples
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 CMD wget --quiet --tries=1 --spider http://localhost/ || exit 1
# SHELL instruction valid examples
SHELL ["cmd", "/S", "/C"]
`
result, err := ParseDockerfile(dockerfileContent)
require.NoError(t, err, "ParseDockerfile should not return an error for valid Dockerfile")
require.NotNil(t, result, "ParseDockerfile should return a non-nil result")
require.Empty(t, result.Rules, "Expected no rules for valid Dockerfile, but got: %v", getRuleCodes(result.Rules))
})
}
func TestParseDockerfile_CompleteInvalidDockerfile(t *testing.T) {
t.Run("comprehensive invalid Dockerfile with violations for each instruction", func(t *testing.T) {
dockerfileContent := `# Invalid Dockerfile with one violation per instruction type
# ARG instruction invalid example
ARG 1INVALID=value
# FROM instruction invalid example
FROM debian:latest as builder
# RUN instruction invalid example
RUN --network=invalid echo "test"
# CMD instruction invalid example
CMD ['single', 'quotes']
# LABEL instruction invalid example
LABEL version 1.0
# EXPOSE instruction invalid example
EXPOSE 80:8080
# ENV instruction invalid example
ENV =invalid
# ADD instruction invalid example
ADD --from=invalid file.txt /dest/
# COPY instruction invalid example
COPY --invalid file.txt /dest/
# ENTRYPOINT instruction invalid example
ENTRYPOINT ['single', 'quotes']
# VOLUME instruction invalid example
VOLUME ['/data']
# USER instruction invalid example
USER user:group:extra
# WORKDIR instruction invalid example
WORKDIR relative/path
# ONBUILD instruction invalid example
ONBUILD
# STOPSIGNAL instruction invalid example
STOPSIGNAL
# HEALTHCHECK instruction invalid example
HEALTHCHECK
# SHELL instruction invalid example
SHELL ['/bin/sh', '-c']
`
result, err := ParseDockerfile(dockerfileContent)
require.NoError(t, err, "ParseDockerfile should not return an error even with invalid instructions")
require.NotNil(t, result, "ParseDockerfile should return a non-nil result")
require.NotEmpty(t, result.Rules, "Expected rules for invalid Dockerfile")
// Collect actual rule codes
actualRuleCodes := getRuleCodes(result.Rules)
// Expected violations - one per instruction type
expectedRules := []string{
"ArgInvalidFormat", // ARG 1INVALID=value
"FromAsCasing", // FROM debian:latest as builder
"RunInvalidNetworkFlag", // RUN --network=invalid
"CmdInvalidExecForm", // CMD ['single', 'quotes']
"LabelInvalidFormat", // LABEL version 1.0
"ExposeInvalidFormat", // EXPOSE 80:8080
"EnvInvalidFormat", // ENV =invalid
"AddInvalidFlag", // ADD --from=invalid
"CopyInvalidFlag", // COPY --invalid
"EntrypointInvalidExecForm", // ENTRYPOINT ['single', 'quotes']
"VolumeInvalidJsonForm", // VOLUME ['/data']
"UserInvalidFormat", // USER user:group:extra
"WorkdirRelativePath", // WORKDIR relative/path
"InvalidInstruction", // ONBUILD, STOPSIGNAL, HEALTHCHECK (3 instances)
"ShellInvalidJsonForm", // SHELL ['/bin/sh', '-c']
}
// Create a map of actual rule codes for easy lookup
actualRuleMap := make(map[string]bool)
for _, code := range actualRuleCodes {
actualRuleMap[code] = true
}
// Verify each expected rule is present
var missingRules []string
for _, expectedCode := range expectedRules {
if !actualRuleMap[expectedCode] {
missingRules = append(missingRules, expectedCode)
}
}
require.Empty(t, missingRules, "Missing expected rule codes: %v. Got rules: %v", missingRules, actualRuleCodes)
t.Logf("Found %d rules as expected: %v", len(result.Rules), actualRuleCodes)
})
}
// Helper function to extract rule codes from a slice of Rules
func getRuleCodes(rules []Rule) []string {
codes := make([]string, 0, len(rules))
for _, rule := range rules {
codes = append(codes, rule.Code)
}
return codes
}