-
Notifications
You must be signed in to change notification settings - Fork 2
/
conda_runner_test.go
365 lines (319 loc) · 12.2 KB
/
conda_runner_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
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
package condaenvupdate_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
condaenvupdate "github.com/initializ-buildpacks/conda-env-update"
"github.com/initializ-buildpacks/conda-env-update/fakes"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)
func testCondaRunner(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
condaLayerPath string
condaCachePath string
executable *fakes.Executable
executions []pexec.Execution
summer *fakes.Summer
runner condaenvupdate.CondaRunner
buffer *bytes.Buffer
logger scribe.Emitter
)
it.Before(func() {
workingDir = t.TempDir()
layersDir := t.TempDir()
condaLayerPath = filepath.Join(layersDir, "a-conda-layer")
condaCachePath = filepath.Join(layersDir, "a-conda-cache-path")
executable = &fakes.Executable{}
executions = []pexec.Execution{}
executable.ExecuteCall.Stub = func(ex pexec.Execution) error {
executions = append(executions, ex)
Expect(os.MkdirAll(filepath.Join(condaLayerPath, "conda-meta"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(condaLayerPath, "conda-meta", "history"), []byte("some content"), os.ModePerm)).To(Succeed())
fmt.Fprintln(ex.Stdout, "stdout output")
fmt.Fprintln(ex.Stderr, "stderr output")
return nil
}
summer = &fakes.Summer{}
buffer = bytes.NewBuffer(nil)
logger = scribe.NewEmitter(buffer)
runner = condaenvupdate.NewCondaRunner(executable, summer, logger)
})
context("ShouldRun", func() {
it("returns true, with no sha, and no error when no lockfile is present", func() {
run, sha, err := runner.ShouldRun(workingDir, map[string]interface{}{})
Expect(run).To(BeTrue())
Expect(sha).To(Equal(""))
Expect(err).NotTo(HaveOccurred())
})
context("when there is an error checking if a lockfile is present", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns false, with no sha, and an error", func() {
run, sha, err := runner.ShouldRun(workingDir, map[string]interface{}{})
Expect(run).To(BeFalse())
Expect(sha).To(Equal(""))
Expect(err).To(HaveOccurred())
})
})
context("when a lockfile is present", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "package-list.txt"), nil, os.ModePerm)).To(Succeed())
})
context("and the lockfile sha is unchanged", func() {
it("return false, with the existing sha, and no error", func() {
summer.SumCall.Returns.String = "a-sha"
Expect(os.WriteFile(filepath.Join(workingDir, "package-list.txt"), nil, os.ModePerm)).To(Succeed())
metadata := map[string]interface{}{
"lockfile-sha": "a-sha",
}
run, sha, err := runner.ShouldRun(workingDir, metadata)
Expect(run).To(BeFalse())
Expect(sha).To(Equal("a-sha"))
Expect(err).NotTo(HaveOccurred())
})
context("and there is and error summing the lock file", func() {
it.Before(func() {
summer.SumCall.Returns.Error = errors.New("summing lockfile failed")
})
it("returns false, with no sha, and an error", func() {
run, sha, err := runner.ShouldRun(workingDir, map[string]interface{}{})
Expect(run).To(BeFalse())
Expect(sha).To(Equal(""))
Expect(err).To(MatchError("summing lockfile failed"))
})
})
})
it("returns true, with a new sha, and no error when the lockfile has changed", func() {
summer.SumCall.Returns.String = "a-new-sha"
metadata := map[string]interface{}{
"lockfile-sha": "a-sha",
}
run, sha, err := runner.ShouldRun(workingDir, metadata)
Expect(run).To(BeTrue())
Expect(sha).To(Equal("a-new-sha"))
Expect(err).NotTo(HaveOccurred())
})
})
})
context("Execute", func() {
context("when a vendor dir is present", func() {
var vendorPath string
it.Before(func() {
vendorPath = filepath.Join(workingDir, "vendor")
Expect(os.Mkdir(vendorPath, os.ModePerm))
})
it("runs conda create with additional vendor args and WITHOUT cache layer in env", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).NotTo(HaveOccurred())
args := []string{
"create",
"--file", filepath.Join(workingDir, "package-list.txt"),
"--prefix", condaLayerPath,
"--yes",
"--quiet",
"--channel", vendorPath,
"--override-channels",
"--offline",
}
Expect(executions[0].Args).To(Equal(args))
Expect(executions[0].Env).NotTo(ContainElement(fmt.Sprintf("CONDA_PKGS_DIRS=%s", condaCachePath)))
Expect(executable.ExecuteCall.CallCount).To(Equal(1))
Expect(buffer.String()).To(ContainLines(
fmt.Sprintf(" Running 'conda %s'", strings.Join(args, " ")),
" stdout output",
" stderr output",
))
historyFilepath := filepath.Join(condaLayerPath, "conda-meta", "history")
Expect(historyFilepath).NotTo(BeAnExistingFile())
})
context("failure cases", func() {
context("when there is an error running the conda command", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(ex pexec.Execution) error {
fmt.Fprintln(ex.Stdout, "conda error stdout")
fmt.Fprintln(ex.Stderr, "conda error stderr")
return errors.New("some conda failure")
}
})
it("returns an error with stdout/stderr output", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).To(MatchError("failed to run conda command: some conda failure"))
args := []string{
"create",
"--file", filepath.Join(workingDir, "package-list.txt"),
"--prefix", condaLayerPath,
"--yes",
"--quiet",
"--channel", vendorPath,
"--override-channels",
"--offline",
}
Expect(buffer.String()).To(ContainLines(
fmt.Sprintf(" Running 'conda %s'", strings.Join(args, " ")),
" conda error stdout",
" conda error stderr",
))
})
})
context("when the removing the history file fails with error", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(_ pexec.Execution) error {
Expect(os.MkdirAll(filepath.Join(condaLayerPath, "conda-meta"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(condaLayerPath, "conda-meta", "history"), []byte("some content"), os.ModePerm)).To(Succeed())
Expect(os.Chmod(filepath.Join(condaLayerPath, "conda-meta"), 0)).To(Succeed())
return nil
}
})
it("returns the error", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).To(MatchError(ContainSubstring("%s: permission denied", filepath.Join(condaLayerPath, "conda-meta"))))
// Required for the automatic t.TempDir cleanup
Expect(os.Chmod(filepath.Join(condaLayerPath, "conda-meta"), os.ModePerm)).To(Succeed())
})
})
})
})
context("when a lockfile exists", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, condaenvupdate.LockfileName), nil, os.ModePerm)).To(Succeed())
})
it("runs conda create with the cache layer available in the environment", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(executions[0].Args).To(Equal([]string{
"create",
"--file", filepath.Join(workingDir, "package-list.txt"),
"--prefix", condaLayerPath,
"--yes",
"--quiet",
}))
Expect(executions[0].Env).To(ContainElement(fmt.Sprintf("CONDA_PKGS_DIRS=%s", condaCachePath)))
Expect(executable.ExecuteCall.CallCount).To(Equal(2))
Expect(executions[1].Args).To(Equal([]string{
"clean",
"--packages",
"--tarballs",
}))
historyFilepath := filepath.Join(condaLayerPath, "conda-meta", "history")
Expect(historyFilepath).NotTo(BeAnExistingFile())
})
})
context("when no vendor dir or lockfile exists", func() {
it("runs conda env update with the cache layer available in the environment", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(executions[0].Args).To(Equal([]string{
"env",
"update",
"--prefix", condaLayerPath,
"--file", filepath.Join(workingDir, "environment.yml"),
}))
Expect(executions[0].Env).To(ContainElement(fmt.Sprintf("CONDA_PKGS_DIRS=%s", condaCachePath)))
Expect(executable.ExecuteCall.CallCount).To(Equal(2))
Expect(executions[1].Args).To(Equal([]string{
"clean",
"--packages",
"--tarballs",
}))
historyFilepath := filepath.Join(condaLayerPath, "conda-meta", "history")
Expect(historyFilepath).NotTo(BeAnExistingFile())
})
context("failure cases", func() {
context("there is an error checking for vendor directory", 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 := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the conda env command fails to run", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(ex pexec.Execution) error {
fmt.Fprintln(ex.Stdout, "conda error stdout")
fmt.Fprintln(ex.Stderr, "conda error stderr")
return errors.New("some conda failure")
}
})
it("returns an error and logs the stdout and stderr output from the command", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).To(MatchError("failed to run conda command: some conda failure"))
Expect(buffer.String()).To(ContainLines(
fmt.Sprintf(
" Running 'CONDA_PKGS_DIRS=%s conda env update --prefix %s --file %s'",
condaCachePath,
condaLayerPath,
filepath.Join(workingDir, condaenvupdate.EnvironmentFileName),
),
" conda error stdout",
" conda error stderr",
))
})
})
context("when the conda clean command fails to run", func() {
it.Before(func() {
executable.ExecuteCall.Stub = func(ex pexec.Execution) error {
for _, arg := range ex.Args {
if arg == "clean" {
fmt.Fprintln(ex.Stdout, "conda error stdout")
fmt.Fprintln(ex.Stderr, "conda error stderr")
return errors.New("some conda clean failure")
}
}
return nil
}
})
it("returns an error", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).To(MatchError("failed to run conda command: some conda clean failure"))
Expect(buffer.String()).To(ContainLines(
" Running 'conda clean --packages --tarballs'",
" conda error stdout",
" conda error stderr",
))
})
})
context("when the removing the history file fails with error", func() {
it.Before(func() {
// Use the clean operation to create the error environment as it is the last thing that runs before
// the history file is removed
executable.ExecuteCall.Stub = func(ex pexec.Execution) error {
for _, arg := range ex.Args {
if arg == "clean" {
Expect(os.MkdirAll(filepath.Join(condaLayerPath, "conda-meta"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(condaLayerPath, "conda-meta", "history"), []byte("some content"), os.ModePerm)).To(Succeed())
Expect(os.Chmod(filepath.Join(condaLayerPath, "conda-meta"), 0)).To(Succeed())
}
}
return nil
}
})
it("returns the error", func() {
err := runner.Execute(condaLayerPath, condaCachePath, workingDir)
Expect(err).To(MatchError(ContainSubstring("%s: permission denied", filepath.Join(condaLayerPath, "conda-meta"))))
// Required for the automatic t.TempDir cleanup
Expect(os.Chmod(filepath.Join(condaLayerPath, "conda-meta"), os.ModePerm)).To(Succeed())
})
})
})
})
})
}