-
Notifications
You must be signed in to change notification settings - Fork 290
/
pack.go
328 lines (271 loc) · 7.77 KB
/
pack.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
//go:build acceptance
// +build acceptance
package invoke
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"testing"
"github.com/Masterminds/semver"
acceptanceOS "github.com/buildpacks/pack/acceptance/os"
h "github.com/buildpacks/pack/testhelpers"
)
type PackInvoker struct {
testObject *testing.T
assert h.AssertionManager
path string
home string
dockerConfigDir string
fixtureManager PackFixtureManager
verbose bool
}
type packPathsProvider interface {
Path() string
FixturePaths() []string
}
func NewPackInvoker(
testObject *testing.T,
assert h.AssertionManager,
packAssets packPathsProvider,
dockerConfigDir string,
) *PackInvoker {
testObject.Helper()
home, err := os.MkdirTemp("", "buildpack.pack.home.")
if err != nil {
testObject.Fatalf("couldn't create home folder for pack: %s", err)
}
return &PackInvoker{
testObject: testObject,
assert: assert,
path: packAssets.Path(),
home: home,
dockerConfigDir: dockerConfigDir,
verbose: true,
fixtureManager: PackFixtureManager{
testObject: testObject,
assert: assert,
locations: packAssets.FixturePaths(),
},
}
}
func (i *PackInvoker) Cleanup() {
if i == nil {
return
}
i.testObject.Helper()
err := os.RemoveAll(i.home)
i.assert.Nil(err)
}
func (i *PackInvoker) cmd(name string, args ...string) *exec.Cmd {
i.testObject.Helper()
cmdArgs := append([]string{name}, args...)
cmdArgs = append(cmdArgs, "--no-color")
if i.verbose {
cmdArgs = append(cmdArgs, "--verbose")
}
cmd := i.baseCmd(cmdArgs...)
cmd.Env = append(os.Environ(), "DOCKER_CONFIG="+i.dockerConfigDir)
if i.home != "" {
cmd.Env = append(cmd.Env, "PACK_HOME="+i.home)
}
return cmd
}
func (i *PackInvoker) baseCmd(parts ...string) *exec.Cmd {
return exec.Command(i.path, parts...)
}
func (i *PackInvoker) Run(name string, args ...string) (string, error) {
i.testObject.Helper()
output, err := i.cmd(name, args...).CombinedOutput()
return string(output), err
}
func (i *PackInvoker) SetVerbose(verbose bool) {
i.verbose = verbose
}
func (i *PackInvoker) RunSuccessfully(name string, args ...string) string {
i.testObject.Helper()
output, err := i.Run(name, args...)
i.assert.NilWithMessage(err, output)
return output
}
func (i *PackInvoker) JustRunSuccessfully(name string, args ...string) {
i.testObject.Helper()
_ = i.RunSuccessfully(name, args...)
}
func (i *PackInvoker) StartWithWriter(combinedOutput *bytes.Buffer, name string, args ...string) *InterruptCmd {
cmd := i.cmd(name, args...)
cmd.Stderr = combinedOutput
cmd.Stdout = combinedOutput
err := cmd.Start()
i.assert.Nil(err)
return &InterruptCmd{
testObject: i.testObject,
assert: i.assert,
cmd: cmd,
combinedOutput: combinedOutput,
}
}
func (i *PackInvoker) Home() string {
return i.home
}
type InterruptCmd struct {
testObject *testing.T
assert h.AssertionManager
cmd *exec.Cmd
combinedOutput *bytes.Buffer
outputMux sync.Mutex
}
func (c *InterruptCmd) TerminateAtStep(pattern string) {
c.testObject.Helper()
for {
c.outputMux.Lock()
if strings.Contains(c.combinedOutput.String(), pattern) {
err := c.cmd.Process.Signal(acceptanceOS.InterruptSignal)
c.assert.Nil(err)
h.AssertNil(c.testObject, err)
return
}
c.outputMux.Unlock()
}
}
func (c *InterruptCmd) Wait() error {
return c.cmd.Wait()
}
func (i *PackInvoker) Version() string {
i.testObject.Helper()
return strings.TrimSpace(i.RunSuccessfully("version"))
}
func (i *PackInvoker) SanitizedVersion() string {
i.testObject.Helper()
// Sanitizing any git commit sha and build number from the version output
re := regexp.MustCompile(`\d+\.\d+\.\d+`)
return re.FindString(strings.TrimSpace(i.RunSuccessfully("version")))
}
func (i *PackInvoker) EnableExperimental() {
i.testObject.Helper()
i.JustRunSuccessfully("config", "experimental", "true")
}
// Supports returns whether or not the executor's pack binary supports a
// given command string. The command string can take one of four forms:
// - "<command>" (e.g. "create-builder")
// - "<flag>" (e.g. "--verbose")
// - "<command> <flag>" (e.g. "build --network")
// - "<command>... <flag>" (e.g. "config trusted-builder --network")
//
// Any other form may return false.
func (i *PackInvoker) Supports(command string) bool {
i.testObject.Helper()
parts := strings.Split(command, " ")
var cmdParts = []string{"help"}
var search string
if len(parts) > 1 {
last := len(parts) - 1
cmdParts = append(cmdParts, parts[:last]...)
search = parts[last]
} else {
cmdParts = append(cmdParts, command)
search = command
}
re := regexp.MustCompile(fmt.Sprint(`\b%s\b`, search))
output, err := i.baseCmd(cmdParts...).CombinedOutput()
i.assert.Nil(err)
// FIXME: this doesn't appear to be working as expected,
// as tests against "build --creation-time" and "build --cache" are returning unsupported
// even on the latest version of pack.
return re.MatchString(string(output)) && !strings.Contains(string(output), "Unknown help topic")
}
type Feature int
const (
CreationTime = iota
Cache
BuildImageExtensions
RunImageExtensions
StackValidation
ForceRebase
BuildpackFlatten
MetaBuildpackFolder
PlatformRetries
FlattenBuilderCreationV2
FixesRunImageMetadata
ManifestCommands
PlatformOption
MultiPlatformBuildersAndBuildPackages
)
var featureTests = map[Feature]func(i *PackInvoker) bool{
CreationTime: func(i *PackInvoker) bool {
return i.Supports("build --creation-time")
},
Cache: func(i *PackInvoker) bool {
return i.Supports("build --cache")
},
BuildImageExtensions: func(i *PackInvoker) bool {
return i.laterThan("v0.27.0")
},
RunImageExtensions: func(i *PackInvoker) bool {
return i.laterThan("v0.29.0")
},
StackValidation: func(i *PackInvoker) bool {
return !i.atLeast("v0.30.0")
},
ForceRebase: func(i *PackInvoker) bool {
return i.atLeast("v0.30.0")
},
BuildpackFlatten: func(i *PackInvoker) bool {
return i.atLeast("v0.30.0")
},
MetaBuildpackFolder: func(i *PackInvoker) bool {
return i.atLeast("v0.30.0")
},
PlatformRetries: func(i *PackInvoker) bool {
return i.atLeast("v0.32.1")
},
FlattenBuilderCreationV2: func(i *PackInvoker) bool {
return i.atLeast("v0.33.1")
},
FixesRunImageMetadata: func(i *PackInvoker) bool {
return i.atLeast("v0.34.0")
},
ManifestCommands: func(i *PackInvoker) bool {
return i.atLeast("v0.34.0")
},
PlatformOption: func(i *PackInvoker) bool {
return i.atLeast("v0.34.0")
},
MultiPlatformBuildersAndBuildPackages: func(i *PackInvoker) bool {
return i.atLeast("v0.34.0")
},
}
func (i *PackInvoker) SupportsFeature(f Feature) bool {
return featureTests[f](i)
}
func (i *PackInvoker) semanticVersion() *semver.Version {
version := i.Version()
semanticVersion, err := semver.NewVersion(strings.TrimPrefix(strings.Split(version, " ")[0], "v"))
i.assert.Nil(err)
return semanticVersion
}
// laterThan returns true if pack version is older than the provided version
func (i *PackInvoker) laterThan(version string) bool {
providedVersion := semver.MustParse(version)
ver := i.semanticVersion()
return ver.Compare(providedVersion) > 0 || ver.Equal(semver.MustParse("0.0.0"))
}
// atLeast returns true if pack version is the same or older than the provided version
func (i *PackInvoker) atLeast(version string) bool {
minimalVersion := semver.MustParse(version)
ver := i.semanticVersion()
return ver.Equal(minimalVersion) || ver.GreaterThan(minimalVersion) || ver.Equal(semver.MustParse("0.0.0"))
}
func (i *PackInvoker) ConfigFileContents() string {
i.testObject.Helper()
contents, err := os.ReadFile(filepath.Join(i.home, "config.toml"))
i.assert.Nil(err)
return string(contents)
}
func (i *PackInvoker) FixtureManager() PackFixtureManager {
return i.fixtureManager
}