forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phase_test.go
390 lines (330 loc) · 13.6 KB
/
phase_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package build_test
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"testing"
"time"
"github.com/buildpacks/imgutil/local"
"github.com/buildpacks/lifecycle/auth"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/pack/internal/archive"
"github.com/buildpacks/pack/internal/build"
"github.com/buildpacks/pack/internal/builder"
ilogging "github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/logging"
h "github.com/buildpacks/pack/testhelpers"
)
const phaseName = "phase"
var (
repoName string
dockerCli client.CommonAPIClient
)
func TestPhase(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
color.Disable(true)
defer color.Disable(false)
h.RequireDocker(t)
var err error
dockerCli, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
h.AssertNil(t, err)
info, err := dockerCli.Info(context.TODO())
h.AssertNil(t, err)
h.SkipIf(t, info.OSType == "windows", "These tests are not yet compatible with Windows-based containers")
repoName = "phase.test.lc-" + h.RandString(10)
wd, err := os.Getwd()
h.AssertNil(t, err)
h.CreateImageFromDir(t, dockerCli, repoName, filepath.Join(wd, "testdata", "fake-lifecycle"))
defer h.DockerRmi(dockerCli, repoName)
spec.Run(t, "phase", testPhase, spec.Report(report.Terminal{}), spec.Sequential())
}
func testPhase(t *testing.T, when spec.G, it spec.S) {
var (
lifecycle *build.Lifecycle
phaseFactory *build.DefaultPhaseFactory
outBuf, errBuf bytes.Buffer
docker client.CommonAPIClient
logger logging.Logger
)
it.Before(func() {
logger = ilogging.NewLogWithWriters(&outBuf, &outBuf)
var err error
docker, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
h.AssertNil(t, err)
lifecycle, err = CreateFakeLifecycle(docker, logger, filepath.Join("testdata", "fake-app"), repoName)
h.AssertNil(t, err)
phaseFactory = build.NewDefaultPhaseFactory(lifecycle)
})
it.After(func() {
h.AssertNil(t, lifecycle.Cleanup())
})
when("Phase", func() {
when("#Run", func() {
it("runs the subject phase on the builder image", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle)
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "running some-lifecycle-phase")
})
it("prefixes the output with the phase name", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle)
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] running some-lifecycle-phase")
})
it("attaches the same layers volume to each phase", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("write", "/layers/test.txt", "test-layers"))
writePhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, writePhase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] write test")
configProvider = build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("read", "/layers/test.txt"))
readPhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, readPhase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] file contents: test-layers")
})
it("attaches the same app volume to each phase", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("write", "/workspace/test.txt", "test-app"))
writePhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, writePhase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] write test")
configProvider = build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("read", "/workspace/test.txt"))
readPhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, readPhase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] file contents: test-app")
})
it("copies the app into the app volume before the first phase", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("read", "/workspace/fake-app-file"))
readPhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, readPhase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] file contents: fake-app-contents")
h.AssertContains(t, outBuf.String(), "[phase] file uid/gid: 111/222")
configProvider = build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("delete", "/workspace/fake-app-file"))
deletePhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, deletePhase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] delete test")
configProvider = build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("read", "/workspace/fake-app-file"))
readPhase2 := phaseFactory.New(configProvider)
err := readPhase2.Run(context.TODO())
readPhase2.Cleanup()
h.AssertNotNil(t, err)
h.AssertContains(t, outBuf.String(), "failed to read file")
})
when("app is a dir", func() {
it("preserves original mod times", func() {
assertAppModTimePreserved(t, lifecycle, phaseFactory, &outBuf, &errBuf)
})
})
when("app is a zip", func() {
it("preserves original mod times", func() {
var err error
lifecycle, err = CreateFakeLifecycle(docker, logger, filepath.Join("testdata", "fake-app.zip"), repoName)
h.AssertNil(t, err)
phaseFactory = build.NewDefaultPhaseFactory(lifecycle)
assertAppModTimePreserved(t, lifecycle, phaseFactory, &outBuf, &errBuf)
})
})
when("is posix", func() {
it.Before(func() {
h.SkipIf(t, runtime.GOOS == "windows", "Skipping on windows")
})
when("restricted directory is present", func() {
var (
err error
tmpFakeAppDir string
dirWithoutAccess string
)
it.Before(func() {
h.SkipIf(t, os.Getuid() == 0, "Skipping b/c current user is root")
tmpFakeAppDir, err = ioutil.TempDir("", "fake-app")
h.AssertNil(t, err)
dirWithoutAccess = filepath.Join(tmpFakeAppDir, "bad-dir")
err := os.MkdirAll(dirWithoutAccess, 0222)
h.AssertNil(t, err)
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpFakeAppDir))
})
it("returns an error", func() {
logger := ilogging.NewLogWithWriters(&outBuf, &outBuf)
lifecycle, err = CreateFakeLifecycle(docker, logger, tmpFakeAppDir, repoName)
h.AssertNil(t, err)
phaseFactory = build.NewDefaultPhaseFactory(lifecycle)
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("read", "/workspace/fake-app-file"))
readPhase := phaseFactory.New(configProvider)
h.AssertNil(t, err)
err = readPhase.Run(context.TODO())
defer readPhase.Cleanup()
h.AssertNotNil(t, err)
h.AssertContains(t,
err.Error(),
fmt.Sprintf("open %s: permission denied", dirWithoutAccess),
)
})
})
})
it("sets the proxy vars in the container", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("proxy"))
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "HTTP_PROXY=some-http-proxy")
h.AssertContains(t, outBuf.String(), "HTTPS_PROXY=some-https-proxy")
h.AssertContains(t, outBuf.String(), "NO_PROXY=some-no-proxy")
h.AssertContains(t, outBuf.String(), "http_proxy=some-http-proxy")
h.AssertContains(t, outBuf.String(), "https_proxy=some-https-proxy")
h.AssertContains(t, outBuf.String(), "no_proxy=some-no-proxy")
})
when("#WithArgs", func() {
it("runs the subject phase with args", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("some", "args"))
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), `received args [/cnb/lifecycle/phase some args]`)
})
})
when("#WithDaemonAccess", func() {
it("allows daemon access inside the container", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("daemon"), build.WithDaemonAccess())
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] daemon test")
})
})
when("#WithRoot", func() {
it("sets the containers user to root", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("user"), build.WithRoot())
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] current user is root")
})
})
when("#WithBinds", func() {
it.After(func() {
docker.VolumeRemove(context.TODO(), "some-volume", true)
})
it("mounts volumes inside container", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("binds"), build.WithBinds("some-volume:/mounted"))
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] binds test")
body, err := docker.VolumeList(context.TODO(), filters.NewArgs(filters.KeyValuePair{
Key: "name",
Value: "some-volume",
}))
h.AssertNil(t, err)
h.AssertEq(t, len(body.Volumes), 1)
})
})
when("#WithRegistryAccess", func() {
var registry *h.TestRegistryConfig
it.Before(func() {
registry = h.RunRegistry(t)
h.AssertNil(t, os.Setenv("DOCKER_CONFIG", registry.DockerConfigDir))
})
it.After(func() {
if registry != nil {
registry.StopRegistry(t)
}
h.AssertNil(t, os.Unsetenv("DOCKER_CONFIG"))
})
it("provides auth for registry in the container", func() { // TODO: fix this flake [https://github.com/buildpacks/pack/issues/533].
repoName := h.CreateImageOnRemote(t, dockerCli, registry, "packs/build:v3alpha2", "FROM busybox")
authConfig, err := auth.BuildEnvVar(authn.DefaultKeychain, repoName)
h.AssertNil(t, err)
configProvider := build.NewPhaseConfigProvider(
phaseName,
lifecycle,
build.WithArgs("registry", repoName),
build.WithRegistryAccess(authConfig),
build.WithNetwork("host"),
)
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "[phase] registry test")
})
})
when("#WithNetwork", func() {
it("specifies a network for the container", func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("network"), build.WithNetwork("none"))
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertNotContainsMatch(t, outBuf.String(), `interface: eth\d+`)
h.AssertContains(t, outBuf.String(), `error connecting to internet:`)
})
})
})
})
when("#Cleanup", func() {
it.Before(func() {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle)
phase := phaseFactory.New(configProvider)
assertRunSucceeds(t, phase, &outBuf, &errBuf)
h.AssertContains(t, outBuf.String(), "running some-lifecycle-phase")
h.AssertNil(t, lifecycle.Cleanup())
})
it("should delete the layers volume", func() {
body, err := docker.VolumeList(context.TODO(),
filters.NewArgs(filters.KeyValuePair{
Key: "name",
Value: lifecycle.LayersVolume,
}))
h.AssertNil(t, err)
h.AssertEq(t, len(body.Volumes), 0)
})
it("should delete the app volume", func() {
body, err := docker.VolumeList(context.TODO(),
filters.NewArgs(filters.KeyValuePair{
Key: "name",
Value: lifecycle.AppVolume,
}))
h.AssertNil(t, err)
h.AssertEq(t, len(body.Volumes), 0)
})
})
}
func assertAppModTimePreserved(t *testing.T, lifecycle *build.Lifecycle, phaseFactory *build.DefaultPhaseFactory, outBuf *bytes.Buffer, errBuf *bytes.Buffer) {
configProvider := build.NewPhaseConfigProvider(phaseName, lifecycle, build.WithArgs("read", "/workspace/fake-app-file"))
readPhase := phaseFactory.New(configProvider)
assertRunSucceeds(t, readPhase, outBuf, errBuf)
matches := regexp.MustCompile(regexp.QuoteMeta("[phase] file mod time (unix): ") + "(.*)").FindStringSubmatch(outBuf.String())
h.AssertEq(t, len(matches), 2)
h.AssertFalse(t, matches[1] == strconv.FormatInt(archive.NormalizedDateTime.Unix(), 10))
}
func assertRunSucceeds(t *testing.T, phase build.RunnerCleaner, outBuf *bytes.Buffer, errBuf *bytes.Buffer) {
t.Helper()
if err := phase.Run(context.TODO()); err != nil {
phase.Cleanup()
t.Fatalf("Failed to run phase: %s\nstdout:\n%s\nstderr:\n%s\n", err, outBuf.String(), errBuf.String())
}
phase.Cleanup()
}
func CreateFakeLifecycle(docker client.CommonAPIClient, logger logging.Logger, appDir string, repoName string) (*build.Lifecycle, error) {
subject := build.NewLifecycle(docker, logger)
builderImage, err := local.NewImage(repoName, docker, local.FromBaseImage(repoName))
if err != nil {
return nil, err
}
bldr, err := builder.FromImage(builderImage)
if err != nil {
return nil, err
}
subject.Setup(build.LifecycleOptions{
AppPath: appDir,
Builder: bldr,
HTTPProxy: "some-http-proxy",
HTTPSProxy: "some-https-proxy",
NoProxy: "some-no-proxy",
})
return subject, nil
}