Skip to content

Commit 1ab6552

Browse files
authored
test(dockerfile): add multi-stage build (fanal#187)
* test(dockerfile): add multi-stage build * test(dockerfile): add multi-stage Dockerfile
1 parent 60c5a04 commit 1ab6552

File tree

3 files changed

+177
-8
lines changed

3 files changed

+177
-8
lines changed

config/parser/dockerfile/dockerfile_test.go

Lines changed: 165 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dockerfile_test
22

33
import (
4-
"encoding/json"
54
"io/ioutil"
65
"testing"
76

@@ -11,17 +10,177 @@ import (
1110
"github.com/aquasecurity/fanal/config/parser/dockerfile"
1211
)
1312

14-
func Test_dockerParser_Parse(t *testing.T) {
13+
func TestParser_Parse(t *testing.T) {
1514
tests := []struct {
1615
name string
1716
inputFile string
18-
want string
17+
want interface{}
1918
wantErr string
2019
}{
2120
{
2221
name: "happy path",
23-
inputFile: "testdata/Dockerfile.deployment",
24-
want: `{"command":{"foo":[{"Cmd":"from","EndLine":1,"Flags":[],"JSON":false,"Original":"FROM foo","Stage":0,"StartLine":1,"SubCmd":"","Value":["foo"]},{"Cmd":"copy","EndLine":2,"Flags":[],"JSON":false,"Original":"COPY . /","Stage":0,"StartLine":2,"SubCmd":"","Value":[".","/"]},{"Cmd":"run","EndLine":3,"Flags":[],"JSON":false,"Original":"RUN echo hello","Stage":0,"StartLine":3,"SubCmd":"","Value":["echo hello"]}]}}`,
22+
inputFile: "testdata/Dockerfile",
23+
want: map[string]interface{}{
24+
"command": map[string]interface{}{
25+
"foo": []interface{}{
26+
map[string]interface{}{
27+
"Cmd": "from",
28+
"Flags": []interface{}{},
29+
"JSON": false,
30+
"Original": "FROM foo",
31+
"Stage": float64(0),
32+
"StartLine": float64(1),
33+
"EndLine": float64(1),
34+
"SubCmd": "",
35+
"Value": []interface{}{"foo"},
36+
},
37+
map[string]interface{}{
38+
"Cmd": "copy",
39+
"Flags": []interface{}{},
40+
"JSON": false,
41+
"Original": "COPY . /",
42+
"Stage": float64(0),
43+
"StartLine": float64(2),
44+
"EndLine": float64(2),
45+
"SubCmd": "",
46+
"Value": []interface{}{".", "/"},
47+
},
48+
map[string]interface{}{
49+
"Cmd": "run",
50+
"Flags": []interface{}{},
51+
"JSON": false,
52+
"Original": "RUN echo hello",
53+
"Stage": float64(0),
54+
"StartLine": float64(3),
55+
"EndLine": float64(3),
56+
"SubCmd": "",
57+
"Value": []interface{}{"echo hello"},
58+
},
59+
},
60+
},
61+
},
62+
},
63+
{
64+
name: "multi stage",
65+
inputFile: "testdata/Dockerfile.multi",
66+
want: map[string]interface{}{
67+
"command": map[string]interface{}{
68+
"golang:1.16 AS builder": []interface{}{
69+
map[string]interface{}{
70+
"Cmd": "from",
71+
"EndLine": float64(1),
72+
"Flags": []interface{}{},
73+
"JSON": false,
74+
"Original": "FROM golang:1.16 AS builder",
75+
"Stage": float64(0),
76+
"StartLine": float64(1),
77+
"SubCmd": "",
78+
"Value": []interface{}{"golang:1.16", "AS", "builder"},
79+
},
80+
map[string]interface{}{
81+
"Cmd": "workdir",
82+
"EndLine": float64(2),
83+
"Flags": []interface{}{},
84+
"JSON": false,
85+
"Original": "WORKDIR /go/src/github.com/alexellis/href-counter/",
86+
"Stage": float64(0),
87+
"StartLine": float64(2),
88+
"SubCmd": "",
89+
"Value": []interface{}{"/go/src/github.com/alexellis/href-counter/"},
90+
},
91+
map[string]interface{}{
92+
"Cmd": "run",
93+
"EndLine": float64(3),
94+
"Flags": []interface{}{},
95+
"JSON": false,
96+
"Original": "RUN go get -d -v golang.org/x/net/html",
97+
"Stage": float64(0),
98+
"StartLine": float64(3),
99+
"SubCmd": "",
100+
"Value": []interface{}{"go get -d -v golang.org/x/net/html"},
101+
},
102+
map[string]interface{}{
103+
"Cmd": "copy",
104+
"EndLine": float64(4),
105+
"Flags": []interface{}{},
106+
"JSON": false,
107+
"Original": "COPY app.go .",
108+
"Stage": float64(0),
109+
"StartLine": float64(4),
110+
"SubCmd": "",
111+
"Value": []interface{}{"app.go", "."},
112+
},
113+
map[string]interface{}{
114+
"Cmd": "run",
115+
"EndLine": float64(5),
116+
"Flags": []interface{}{},
117+
"JSON": false,
118+
"Original": "RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .",
119+
"Stage": float64(0),
120+
"StartLine": float64(5),
121+
"SubCmd": "",
122+
"Value": []interface{}{"CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app ."},
123+
},
124+
},
125+
"alpine:latest": []interface{}{
126+
map[string]interface{}{
127+
"Cmd": "from",
128+
"Flags": []interface{}{},
129+
"JSON": false,
130+
"Original": "FROM alpine:latest",
131+
"Stage": float64(1),
132+
"StartLine": float64(7),
133+
"EndLine": float64(7),
134+
"SubCmd": "",
135+
"Value": []interface{}{"alpine:latest"},
136+
},
137+
map[string]interface{}{
138+
"Cmd": "run",
139+
"Flags": []interface{}{},
140+
"JSON": false,
141+
"Original": "RUN apk --no-cache add ca-certificates && apk add --no-cache bash",
142+
"Stage": float64(1),
143+
"StartLine": float64(8),
144+
"EndLine": float64(9),
145+
"SubCmd": "",
146+
"Value": []interface{}{"apk --no-cache add ca-certificates && apk add --no-cache bash"},
147+
},
148+
map[string]interface{}{
149+
"Cmd": "workdir",
150+
"Flags": []interface{}{},
151+
"JSON": false,
152+
"Original": "WORKDIR /root/",
153+
"Stage": float64(1),
154+
"StartLine": float64(10),
155+
"EndLine": float64(10),
156+
"SubCmd": "",
157+
"Value": []interface{}{"/root/"},
158+
},
159+
map[string]interface{}{
160+
"Cmd": "copy",
161+
"Flags": []interface{}{"--from=builder"},
162+
"JSON": false,
163+
"Original": "COPY --from=builder /go/src/github.com/alexellis/href-counter/app .",
164+
"Stage": float64(1),
165+
"StartLine": float64(11),
166+
"EndLine": float64(11),
167+
"SubCmd": "",
168+
"Value": []interface{}{"/go/src/github.com/alexellis/href-counter/app", "."},
169+
},
170+
map[string]interface{}{
171+
"Cmd": "cmd",
172+
"Flags": []interface{}{},
173+
"JSON": true,
174+
"Original": `CMD ["./app"]`,
175+
"Stage": float64(1),
176+
"StartLine": float64(12),
177+
"EndLine": float64(12),
178+
"SubCmd": "",
179+
"Value": []interface{}{"./app"},
180+
},
181+
},
182+
},
183+
},
25184
},
26185
}
27186
for _, tt := range tests {
@@ -38,9 +197,7 @@ func Test_dockerParser_Parse(t *testing.T) {
38197
return
39198
}
40199
assert.NoError(t, err)
41-
gotJson, err := json.Marshal(got)
42-
assert.NoError(t, err)
43-
assert.Equal(t, tt.want, string(gotJson))
200+
assert.Equal(t, tt.want, got)
44201
})
45202
}
46203
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM golang:1.16 AS builder
2+
WORKDIR /go/src/github.com/alexellis/href-counter/
3+
RUN go get -d -v golang.org/x/net/html
4+
COPY app.go .
5+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
6+
7+
FROM alpine:latest
8+
RUN apk --no-cache add ca-certificates \
9+
&& apk add --no-cache bash
10+
WORKDIR /root/
11+
COPY --from=builder /go/src/github.com/alexellis/href-counter/app .
12+
CMD ["./app"]

0 commit comments

Comments
 (0)