-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinstaller_test.go
237 lines (190 loc) · 6.55 KB
/
installer_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
package cpython_test
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/paketo-buildpacks/cpython"
"github.com/paketo-buildpacks/cpython/fakes"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
)
func testCPythonInstaller(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
sourcePath string
layerPath string
workingDir string
entry packit.BuildpackPlanEntry
dependency postal.Dependency
configureProcess *fakes.Executable
makeProcess *fakes.Executable
pythonInstaller cpython.PythonInstaller
)
it.Before(func() {
var err error
sourcePath, err = os.MkdirTemp("", "source")
Expect(err).NotTo(HaveOccurred())
layerPath, err = os.MkdirTemp("", "layer")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(layerPath, "bin"), 0755)).To(Succeed())
workingDir, err = os.MkdirTemp("", "workingdir")
Expect(err).NotTo(HaveOccurred())
configureProcess = &fakes.Executable{}
makeProcess = &fakes.Executable{}
pythonInstaller = cpython.NewCPythonInstaller(configureProcess, makeProcess, scribe.NewEmitter(bytes.NewBuffer(nil)))
})
it.After(func() {
Expect(os.RemoveAll(sourcePath)).To(Succeed())
Expect(os.RemoveAll(layerPath)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("Execute", func() {
var (
makeInvocationArgs [][]string
)
it.Before(func() {
makeInvocationArgs = [][]string{}
makeProcess.ExecuteCall.Stub = func(e pexec.Execution) error {
makeInvocationArgs = append(makeInvocationArgs, e.Args)
return nil
}
})
it("runs installation", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).NotTo(HaveOccurred())
Expect(configureProcess.ExecuteCall.CallCount).To(Equal(1))
Expect(configureProcess.ExecuteCall.Receives.Execution).To(MatchFields(IgnoreExtras, Fields{
"Args": Equal([]string{
"--with-ensurepip",
fmt.Sprintf("--prefix=%s", layerPath),
}),
}))
Expect(makeProcess.ExecuteCall.CallCount).To(Equal(2))
Expect(makeInvocationArgs[0]).To(Equal([]string{
"-j",
fmt.Sprintf("%d", runtime.NumCPU()),
`LDFLAGS="-Wl,--strip-all"`,
}))
Expect(makeInvocationArgs[1]).To(Equal([]string{
"altinstall",
}))
})
context("when configure flags are provided", func() {
it.Before(func() {
entry.Metadata = make(map[string]interface{})
entry.Metadata["configure-flags"] = "--foo --bar=baz"
})
it("uses the provided flags instead of the default", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).NotTo(HaveOccurred())
Expect(configureProcess.ExecuteCall.CallCount).To(Equal(1))
Expect(configureProcess.ExecuteCall.Receives.Execution).To(MatchFields(IgnoreExtras, Fields{
"Args": Equal([]string{
"--foo",
"--bar=baz",
fmt.Sprintf("--prefix=%s", layerPath),
}),
}))
})
})
context("failure cases", func() {
context("when changing to the source directory fails", func() {
it.Before(func() {
Expect(os.Chmod(sourcePath, 0000)).To(Succeed())
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError(And(
ContainSubstring(sourcePath),
ContainSubstring("permission denied"),
)))
})
})
context("when invoking the configureProcess fails with error", func() {
it.Before(func() {
configureProcess.ExecuteCall.Returns.Error = errors.New("some configure error")
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError("some configure error"))
})
})
context("when invoking the make process (first time) fails with error", func() {
it.Before(func() {
makeProcess.ExecuteCall.Stub = nil
makeProcess.ExecuteCall.Returns.Error = errors.New("some make error")
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError("some make error"))
})
})
context("when invoking the make process (second time) fails with error", func() {
var (
makeInvocationError error
)
it.Before(func() {
makeInvocationError = errors.New("some make error (second invocation)")
makeInvocationCount := 0
makeProcess.ExecuteCall.Stub = func(_ pexec.Execution) error {
makeInvocationCount++
if makeInvocationCount == 1 {
return nil
}
return makeInvocationError
}
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError("some make error (second invocation)"))
Expect(makeProcess.ExecuteCall.CallCount).To(Equal(2))
})
})
context("when changing to the layer bin directory fails", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join(layerPath, "bin"), 0000)).To(Succeed())
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError(And(
ContainSubstring(filepath.Join(layerPath, "bin")),
ContainSubstring("permission denied"),
)))
})
})
context("when creating symlinks fails", func() {
it.Before(func() {
// 0500 permissions because we need Read (4) + Execute (1)
// but not write (2) Execute permissions are required to cd
// into the directory
Expect(os.Chmod(filepath.Join(layerPath, "bin"), 0500)).To(Succeed())
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError(ContainSubstring("symlink")))
})
})
context("when changing back to the working directory fails", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it("fails with error", func() {
err := pythonInstaller.Install(sourcePath, workingDir, entry, dependency, layerPath)
Expect(err).Should(MatchError(And(
ContainSubstring(workingDir),
ContainSubstring("permission denied"),
)))
})
})
})
})
}