-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_test.go
308 lines (245 loc) · 8.62 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
package condaenvupdate_test
import (
"bytes"
"errors"
"os"
"path/filepath"
"testing"
condaenvupdate "github.com/initializ-buildpacks/conda-env-update"
"github.com/initializ-buildpacks/conda-env-update/fakes"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/sbom"
"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
cnbDir string
buffer *bytes.Buffer
runner *fakes.Runner
sbomGenerator *fakes.SBOMGenerator
build packit.BuildFunc
buildContext packit.BuildContext
)
it.Before(func() {
var err error
layersDir, err = os.MkdirTemp("", "layers")
Expect(err).NotTo(HaveOccurred())
cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
runner = &fakes.Runner{}
sbomGenerator = &fakes.SBOMGenerator{}
runner.ShouldRunCall.Returns.Bool = true
runner.ShouldRunCall.Returns.String = "some-sha"
sbomGenerator.GenerateCall.Returns.SBOM = sbom.SBOM{}
buffer = bytes.NewBuffer(nil)
logger := scribe.NewEmitter(buffer)
build = condaenvupdate.Build(runner, sbomGenerator, logger, chronos.DefaultClock)
buildContext = packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
SBOMFormats: []string{sbom.CycloneDXFormat, sbom.SPDXFormat},
},
WorkingDir: workingDir,
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{
Name: condaenvupdate.CondaEnvPlanEntry,
},
},
},
Platform: packit.Platform{Path: "some-platform-path"},
Layers: packit.Layers{Path: layersDir},
Stack: "some-stack",
}
})
it.After(func() {
Expect(os.RemoveAll(layersDir)).To(Succeed())
Expect(os.RemoveAll(cnbDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("returns a result that builds correctly", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
layers := result.Layers
Expect(layers).To(HaveLen(1))
condaEnvLayer := layers[0]
Expect(condaEnvLayer.Name).To(Equal("conda-env"))
Expect(condaEnvLayer.Path).To(Equal(filepath.Join(layersDir, "conda-env")))
Expect(condaEnvLayer.Build).To(BeFalse())
Expect(condaEnvLayer.Launch).To(BeFalse())
Expect(condaEnvLayer.Cache).To(BeFalse())
Expect(condaEnvLayer.BuildEnv).To(BeEmpty())
Expect(condaEnvLayer.LaunchEnv).To(BeEmpty())
Expect(condaEnvLayer.ProcessLaunchEnv).To(BeEmpty())
Expect(condaEnvLayer.SharedEnv).To(BeEmpty())
Expect(condaEnvLayer.Metadata).To(HaveLen(1))
Expect(condaEnvLayer.Metadata["lockfile-sha"]).To(Equal("some-sha"))
Expect(condaEnvLayer.SBOM.Formats()).To(HaveLen(2))
var actualExtensions []string
for _, format := range condaEnvLayer.SBOM.Formats() {
actualExtensions = append(actualExtensions, format.Extension)
}
Expect(actualExtensions).To(ConsistOf("cdx.json", "spdx.json"))
Expect(runner.ExecuteCall.Receives.CondaEnvPath).To(Equal(filepath.Join(layersDir, "conda-env")))
Expect(runner.ExecuteCall.Receives.CondaCachePath).To(Equal(filepath.Join(layersDir, "conda-env-cache")))
Expect(runner.ExecuteCall.Receives.WorkingDir).To(Equal(workingDir))
Expect(sbomGenerator.GenerateCall.Receives.Dir).To(Equal(workingDir))
})
context("when the runner executes outputting a non-empty cache dir", func() {
it.Before(func() {
runner.ExecuteCall.Stub = func(_, c, _ string) error {
Expect(os.Mkdir(c, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(c, "some-file"), []byte{}, os.ModePerm)).To(Succeed())
return nil
}
})
it.After(func() {
Expect(os.RemoveAll(filepath.Join(layersDir, "conda-env-cache"))).To(Succeed())
})
it("cache layer is exported", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
layers := result.Layers
Expect(layers).To(HaveLen(2))
condaEnvLayer := layers[0]
Expect(condaEnvLayer.Name).To(Equal("conda-env"))
cacheLayer := layers[1]
Expect(cacheLayer.Name).To(Equal("conda-env-cache"))
Expect(cacheLayer.Path).To(Equal(filepath.Join(layersDir, "conda-env-cache")))
Expect(cacheLayer.Build).To(BeFalse())
Expect(cacheLayer.Launch).To(BeFalse())
Expect(cacheLayer.Cache).To(BeTrue())
})
})
context("when a build plan entry requires conda-environment at launch", func() {
it.Before(func() {
buildContext.Plan.Entries[0].Metadata = map[string]interface{}{
"launch": true,
}
})
it("assigns the flag to the conda env layer", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
layers := result.Layers
Expect(layers).To(HaveLen(1))
condaEnvLayer := layers[0]
Expect(condaEnvLayer.Name).To(Equal("conda-env"))
Expect(condaEnvLayer.Build).To(BeFalse())
Expect(condaEnvLayer.Launch).To(BeTrue())
Expect(condaEnvLayer.Cache).To(BeFalse())
})
})
context("when a build plan entry requires conda-environment at build", func() {
it.Before(func() {
buildContext.Plan.Entries[0].Metadata = map[string]interface{}{
"build": true,
}
})
it("assigns build and cache to the conda env layer", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
layers := result.Layers
Expect(layers).To(HaveLen(1))
condaEnvLayer := layers[0]
Expect(condaEnvLayer.Name).To(Equal("conda-env"))
Expect(condaEnvLayer.Build).To(BeTrue())
Expect(condaEnvLayer.Launch).To(BeFalse())
Expect(condaEnvLayer.Cache).To(BeTrue())
})
})
context("cached packages should be reused", func() {
it.Before(func() {
runner.ShouldRunCall.Returns.Bool = false
runner.ShouldRunCall.Returns.String = "cached-sha"
})
it("reuses cached conda env layer instead of running build process", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())
layers := result.Layers
Expect(layers).To(HaveLen(1))
condaEnvLayer := layers[0]
Expect(condaEnvLayer.Name).To(Equal("conda-env"))
Expect(runner.ExecuteCall.CallCount).To(BeZero())
})
})
context("failure cases", func() {
context("conda layer cannot be fetched", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(layersDir, "conda-env.toml"), nil, 0000)).To(Succeed())
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("conda cache layer cannot be fetched", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(layersDir, "conda-env-cache.toml"), nil, 0000)).To(Succeed())
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("runner ShouldRun fails", func() {
it.Before(func() {
runner.ShouldRunCall.Returns.Error = errors.New("some-shouldrun-error")
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError("some-shouldrun-error"))
})
})
context("layer cannot be reset", func() {
it.Before(func() {
runner.ShouldRunCall.Returns.Bool = true
Expect(os.Chmod(layersDir, 0500)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(layersDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("error could not create directory")))
})
})
context("install process fails to execute", func() {
it.Before(func() {
runner.ShouldRunCall.Returns.Bool = true
runner.ExecuteCall.Returns.Error = errors.New("some execution error")
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("some execution error")))
})
})
context("when generating the SBOM returns an error", func() {
it.Before(func() {
buildContext.BuildpackInfo.SBOMFormats = []string{"random-format"}
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(`unsupported SBOM format: 'random-format'`))
})
})
context("when formatting the SBOM returns an error", func() {
it.Before(func() {
sbomGenerator.GenerateCall.Returns.Error = errors.New("failed to generate SBOM")
})
it("returns an error", func() {
_, err := build(buildContext)
Expect(err).To(MatchError(ContainSubstring("failed to generate SBOM")))
})
})
})
}