-
Notifications
You must be signed in to change notification settings - Fork 12
/
build_test.go
316 lines (274 loc) · 9.11 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
309
310
311
312
313
314
315
316
package gomodvendor_test
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/sbom"
gomodvendor "github.com/paketo-buildpacks/go-mod-vendor"
"github.com/paketo-buildpacks/go-mod-vendor/fakes"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testBuild(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
layersDir string
workingDir string
logs *bytes.Buffer
buildProcess *fakes.BuildProcess
sbomGenerator *fakes.SBOMGenerator
clock chronos.Clock
build packit.BuildFunc
)
it.Before(func() {
var err error
layersDir, err = os.MkdirTemp("", "layers-dir")
Expect(err).NotTo(HaveOccurred())
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(workingDir, "go.mod"), nil, os.ModePerm))
logs = bytes.NewBuffer(nil)
buildProcess = &fakes.BuildProcess{}
buildProcess.ShouldRunCall.Returns.Ok = true
err = os.MkdirAll(filepath.Join(layersDir, "mod-cache"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(filepath.Join(layersDir, "mod-cache"), "cache"), nil, os.ModePerm))
now := time.Now()
clock = chronos.NewClock(func() time.Time {
return now
})
sbomGenerator = &fakes.SBOMGenerator{}
sbomGenerator.GenerateCall.Returns.SBOM = sbom.SBOM{}
build = gomodvendor.Build(
buildProcess,
scribe.NewEmitter(logs),
clock,
sbomGenerator,
)
})
it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("builds", func() {
result, err := build(packit.BuildContext{
Layers: packit.Layers{Path: layersDir},
WorkingDir: workingDir,
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json"},
},
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Layers).To(HaveLen(1))
layer := result.Layers[0]
Expect(layer.Name).To(Equal("mod-cache"))
Expect(layer.Path).To(Equal(filepath.Join(layersDir, "mod-cache")))
Expect(result.Build.SBOM.Formats()).To(HaveLen(2))
cdx := result.Build.SBOM.Formats()[0]
spdx := result.Build.SBOM.Formats()[1]
Expect(cdx.Extension).To(Equal("cdx.json"))
content, err := io.ReadAll(cdx.Content)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(MatchJSON(`{
"bomFormat": "CycloneDX",
"components": [],
"metadata": {
"tools": [
{
"name": "syft",
"vendor": "anchore",
"version": "[not provided]"
}
]
},
"specVersion": "1.3",
"version": 1
}`))
Expect(spdx.Extension).To(Equal("spdx.json"))
content, err = io.ReadAll(spdx.Content)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(MatchJSON(`{
"SPDXID": "SPDXRef-DOCUMENT",
"creationInfo": {
"created": "0001-01-01T00:00:00Z",
"creators": [
"Organization: Anchore, Inc",
"Tool: syft-"
],
"licenseListVersion": "3.16"
},
"dataLicense": "CC0-1.0",
"documentNamespace": "https://paketo.io/packit/unknown-source-type/unknown-88cfa225-65e0-5755-895f-c1c8f10fde76",
"name": "unknown",
"relationships": [
{
"relatedSpdxElement": "SPDXRef-DOCUMENT",
"relationshipType": "DESCRIBES",
"spdxElementId": "SPDXRef-DOCUMENT"
}
],
"spdxVersion": "SPDX-2.2"
}`))
Expect(buildProcess.ShouldRunCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(buildProcess.ExecuteCall.Receives.Path).To(Equal(filepath.Join(layersDir, "mod-cache")))
Expect(buildProcess.ExecuteCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(sbomGenerator.GenerateCall.Receives.Dir).To(Equal(filepath.Join(workingDir, "go.mod")))
Expect(logs.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(logs.String()).NotTo(ContainSubstring("Skipping build process: module graph is empty"))
})
context("when the mod cache layer does not exist", func() {
it.Before(func() {
err := os.RemoveAll(filepath.Join(layersDir, "mod-cache"))
Expect(err).NotTo(HaveOccurred())
})
it("does not include the module cache layer in the build result", func() {
result, err := build(packit.BuildContext{
Layers: packit.Layers{Path: layersDir},
WorkingDir: workingDir,
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Layers).To(BeEmpty())
})
})
context("when the mod cache layer is empty", func() {
it.Before(func() {
err := os.RemoveAll(filepath.Join(layersDir, "mod-cache", "cache"))
Expect(err).NotTo(HaveOccurred())
})
it("does not include the module cache layer in the build result", func() {
result, err := build(packit.BuildContext{
Layers: packit.Layers{Path: layersDir},
WorkingDir: workingDir,
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Layers).To(BeEmpty())
})
})
context("failure cases", func() {
context("build process fails to check if it should run", func() {
it.Before(func() {
buildProcess.ShouldRunCall.Returns.Err = errors.New("build process failed to check")
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
Layers: packit.Layers{Path: layersDir},
WorkingDir: workingDir,
})
Expect(err).To(MatchError(ContainSubstring("build process failed to check")))
})
})
context("modCacheLayer cannot be retrieved", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(layersDir, "mod-cache.toml"), nil, 0000)).To(Succeed())
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
Layers: packit.Layers{Path: layersDir},
WorkingDir: workingDir,
})
Expect(err).To(MatchError(ContainSubstring("failed to parse layer content metadata")))
})
})
context("build process fails to execute", func() {
it.Before(func() {
buildProcess.ExecuteCall.Stub = nil
buildProcess.ExecuteCall.Returns.Error = errors.New("build process failed to execute")
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
Layers: packit.Layers{Path: layersDir},
WorkingDir: workingDir,
})
Expect(err).To(MatchError(ContainSubstring("build process failed to execute")))
})
})
context("when checking for go.mod fails", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{{Name: "node_modules"}},
},
Stack: "some-stack",
})
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when go.mod is missing", func() {
it.Before(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, "go.mod"))).To(Succeed())
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{{Name: "node_modules"}},
},
Stack: "some-stack",
})
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("failed to generate SBOM: '%s' does not exist", filepath.Join(workingDir, "go.mod")))))
})
})
context("when the BOM cannot be generated", func() {
it.Before(func() {
sbomGenerator.GenerateCall.Returns.Error = errors.New("failed to generate SBOM")
})
it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{{Name: "node_modules"}},
},
Stack: "some-stack",
})
Expect(err).To(MatchError("failed to generate SBOM"))
})
})
context("when the BOM cannot be formatted", func() {
it("returns an error", func() {
_, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"random-format"},
},
})
Expect(err).To(MatchError("unsupported SBOM format: 'random-format'"))
})
})
})
}