forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebaser_test.go
356 lines (301 loc) · 12.9 KB
/
rebaser_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
package lifecycle_test
import (
"math/rand"
"testing"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/discard"
"github.com/buildpacks/imgutil/fakes"
"github.com/buildpacks/imgutil/local"
"github.com/buildpacks/imgutil/remote"
"github.com/google/go-containerregistry/pkg/name"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/platform"
h "github.com/buildpacks/lifecycle/testhelpers"
)
func TestRebaser(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
spec.Run(t, "Rebaser", testRebaser, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testRebaser(t *testing.T, when spec.G, it spec.S) {
var (
rebaser *lifecycle.Rebaser
fakeAppImage *fakes.Image
fakeNewBaseImage *fakes.Image
additionalNames []string
md platform.LayersMetadataCompat
)
it.Before(func() {
fakeAppImage = fakes.NewImage(
"some-repo/app-image",
"some-top-layer-sha",
local.IDIdentifier{
ImageID: "some-image-id",
},
)
h.AssertNil(t, fakeAppImage.SetLabel(platform.StackIDLabel, "io.buildpacks.stacks.bionic"))
fakeNewBaseImage = fakes.NewImage(
"some-repo/new-base-image",
"new-top-layer-sha",
local.IDIdentifier{
ImageID: "new-run-id",
},
)
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.StackIDLabel, "io.buildpacks.stacks.bionic"))
additionalNames = []string{"some-repo/app-image:foo", "some-repo/app-image:bar"}
rebaser = &lifecycle.Rebaser{
Logger: &log.Logger{Handler: &discard.Handler{}},
PlatformAPI: api.Platform.Latest(),
}
})
it.After(func() {
h.AssertNil(t, fakeAppImage.Cleanup())
h.AssertNil(t, fakeNewBaseImage.Cleanup())
})
when("#Rebase", func() {
when("app image and run image exist", func() {
it("updates the base image of the app image", func() {
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
it("saves to all names", func() {
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertContains(t, fakeAppImage.SavedNames(), "some-repo/app-image", "some-repo/app-image:foo", "some-repo/app-image:bar")
})
it("adds all names to report", func() {
report, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertContains(t, report.Image.Tags, "some-repo/app-image", "some-repo/app-image:foo", "some-repo/app-image:bar")
})
it("sets the top layer in the metadata", func() {
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertNil(t, lifecycle.DecodeLabel(fakeAppImage, platform.LayerMetadataLabel, &md))
h.AssertEq(t, md.RunImage.TopLayer, "new-top-layer-sha")
})
it("sets the run image reference in the metadata", func() {
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertNil(t, lifecycle.DecodeLabel(fakeAppImage, platform.LayerMetadataLabel, &md))
h.AssertEq(t, md.RunImage.Reference, "new-run-id")
})
it("preserves other existing metadata", func() {
h.AssertNil(t, fakeAppImage.SetLabel(
platform.LayerMetadataLabel,
`{"app": [{"sha": "123456"}], "buildpacks":[{"key": "buildpack.id", "layers": {}}]}`,
))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertNil(t, lifecycle.DecodeLabel(fakeAppImage, platform.LayerMetadataLabel, &md))
h.AssertEq(t, len(md.Buildpacks), 1)
h.AssertEq(t, md.Buildpacks[0].ID, "buildpack.id")
h.AssertEq(t, md.App, []interface{}{map[string]interface{}{"sha": "123456"}})
})
when("image has io.buildpacks.stack.* labels", func() {
var tests = []struct {
label string
appImageValue string
runImageValue string
want string
}{
{"io.buildpacks.stack.distro.name", "ubuntu", "ubuntu", "ubuntu"},
{"io.buildpacks.stack.changed", "v1", "v2", "v2"},
{"io.buildpacks.stack.added", "", "new", "new"},
{"io.buildpacks.stack.removed", "old", "", ""},
{"io.custom.old", "abc", "", "abc"},
{"io.custom.stack", "", "def", ""},
}
it.Before(func() {
for _, l := range tests {
if l.runImageValue != "" {
h.AssertNil(t, fakeNewBaseImage.SetLabel(l.label, l.runImageValue))
}
if l.appImageValue != "" {
h.AssertNil(t, fakeAppImage.SetLabel(l.label, l.appImageValue))
}
}
})
it("syncs matching labels", func() {
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
for _, test := range tests {
test := test
t.Run(test.label, func(t *testing.T) {
actual, err := fakeAppImage.Label(test.label)
h.AssertNil(t, err)
h.AssertEq(t, test.want, actual)
})
}
})
})
when("image has a digest identifier", func() {
var fakeRemoteDigest = "sha256:c27a27006b74a056bed5d9edcebc394783880abe8691a8c87c78b7cffa6fa5ad"
it.Before(func() {
digestRef, err := name.NewDigest("some-repo/app-image@" + fakeRemoteDigest)
h.AssertNil(t, err)
fakeAppImage.SetIdentifier(remote.DigestIdentifier{
Digest: digestRef,
})
})
it("add the digest to the report", func() {
report, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, report.Image.Digest, fakeRemoteDigest)
})
})
when("checking the image manifest", func() {
var fakeRemoteManifestSize int64
when("platform API is < 0.6", func() {
it.Before(func() {
rebaser.PlatformAPI = api.MustParse("0.5")
})
when("image has a manifest", func() {
it.Before(func() {
fakeRemoteManifestSize = 12345
fakeAppImage.SetManifestSize(fakeRemoteManifestSize)
})
it("doesn't set the manifest size in the report.toml", func() {
report, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, report.Image.ManifestSize, int64(0))
})
})
})
when("platform API is >= 0.6", func() {
when("image has a manifest", func() {
it.Before(func() {
fakeRemoteManifestSize = 12345
fakeAppImage.SetManifestSize(fakeRemoteManifestSize)
})
it("add the manifest size to the report", func() {
report, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, report.Image.ManifestSize, fakeRemoteManifestSize)
})
})
when("image doesn't have a manifest", func() {
it.Before(func() {
fakeRemoteManifestSize = 0
fakeAppImage.SetManifestSize(fakeRemoteManifestSize)
})
it("doesn't set the manifest size in the report.toml", func() {
report, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, report.Image.ManifestSize, int64(0))
})
})
})
})
when("image has an ID identifier", func() {
it("add the imageID to the report", func() {
report, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, report.Image.ImageID, "some-image-id")
})
})
when("validating mixins", func() {
when("there are no mixin labels", func() {
it("allows rebase", func() {
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("there are invalid mixin labels", func() {
it("returns an error", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "thisisn'tvalid!"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertError(t, err, "get app image mixins: failed to unmarshal context of label 'io.buildpacks.stack.mixins': invalid character 'h' in literal true (expecting 'r')")
})
})
when("mixins are not present in either image", func() {
it("allows rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "null"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "null"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("mixins are not present on the app image", func() {
it("allows rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "null"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\"]"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("mixins match on the app and run images", func() {
it("allows rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\"]"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\"]"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("extra mixins are present on the run image", func() {
it("allows rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\"]"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\", \"mixin-3\"]"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("mixins on the app image have stage specifiers that do not match the run image", func() {
it("allows rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\"]"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"mixin-2\"]"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("mixins on the run image have stage specifiers that do not match the app image", func() {
it("allows rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\"]"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "[\"run:mixin-1\", \"run:mixin-2\"]"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some-repo/new-base-image")
})
})
when("mixins are missing on the run image", func() {
it("does not allow rebase", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.MixinsLabel, "[\"mixin-1\", \"run:mixin-2\"]"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.MixinsLabel, "[\"run:mixin-2\"]"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertError(t, err, "missing required mixin(s): mixin-1")
})
})
})
})
when("app image and run image are based on different stacks", func() {
it("returns an error and prevents the rebase from taking place when the stacks are different", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.StackIDLabel, "io.buildpacks.stacks.bionic"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.StackIDLabel, "io.buildpacks.stacks.cflinuxfs3"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertError(t, err, "incompatible stack: 'io.buildpacks.stacks.cflinuxfs3' is not compatible with 'io.buildpacks.stacks.bionic'")
})
it("returns an error and prevents the rebase from taking place when the new base image has no stack defined", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.StackIDLabel, "io.buildpacks.stacks.bionic"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.StackIDLabel, ""))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertError(t, err, "stack not defined on new base image")
})
it("returns an error and prevents the rebase from taking place when the app image has no stack defined", func() {
h.AssertNil(t, fakeAppImage.SetLabel(platform.StackIDLabel, ""))
h.AssertNil(t, fakeNewBaseImage.SetLabel(platform.StackIDLabel, "io.buildpacks.stacks.cflinuxfs3"))
_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, additionalNames)
h.AssertError(t, err, "stack not defined on app image")
})
})
})
}