forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebaser_test.go
130 lines (106 loc) · 4.77 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
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/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/lifecycle"
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
fakeWorkingImage *fakes.Image
fakeNewBaseImage *fakes.Image
additionalNames []string
md lifecycle.LayersMetadataCompat
)
it.Before(func() {
fakeWorkingImage = fakes.NewImage(
"some-repo/app-image",
"some-top-layer-sha",
local.IDIdentifier{
ImageID: "some-image-id",
},
)
h.AssertNil(t, fakeWorkingImage.SetLabel(lifecycle.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(lifecycle.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{}},
}
})
it.After(func() {
h.AssertNil(t, fakeWorkingImage.Cleanup())
h.AssertNil(t, fakeNewBaseImage.Cleanup())
})
when("#Rebase", func() {
when("app image and run image exist", func() {
it("updates the base image of the working image", func() {
h.AssertNil(t, rebaser.Rebase(fakeWorkingImage, fakeNewBaseImage, additionalNames))
h.AssertEq(t, fakeWorkingImage.Base(), "some-repo/new-base-image")
})
it("saves to all names", func() {
h.AssertNil(t, rebaser.Rebase(fakeWorkingImage, fakeNewBaseImage, additionalNames))
h.AssertContains(t, fakeWorkingImage.SavedNames(), "some-repo/app-image", "some-repo/app-image:foo", "some-repo/app-image:bar")
})
it("sets the top layer in the metadata", func() {
h.AssertNil(t, rebaser.Rebase(fakeWorkingImage, fakeNewBaseImage, additionalNames))
h.AssertNil(t, lifecycle.DecodeLabel(fakeWorkingImage, lifecycle.LayerMetadataLabel, &md))
h.AssertEq(t, md.RunImage.TopLayer, "new-top-layer-sha")
})
it("sets the run image reference in the metadata", func() {
h.AssertNil(t, rebaser.Rebase(fakeWorkingImage, fakeNewBaseImage, additionalNames))
h.AssertNil(t, lifecycle.DecodeLabel(fakeWorkingImage, lifecycle.LayerMetadataLabel, &md))
h.AssertEq(t, md.RunImage.Reference, "new-run-id")
})
it("preserves other existing metadata", func() {
h.AssertNil(t, fakeWorkingImage.SetLabel(
lifecycle.LayerMetadataLabel,
`{"app": [{"sha": "123456"}], "buildpacks":[{"key": "buildpack.id", "layers": {}}]}`,
))
h.AssertNil(t, rebaser.Rebase(fakeWorkingImage, fakeNewBaseImage, additionalNames))
h.AssertNil(t, lifecycle.DecodeLabel(fakeWorkingImage, lifecycle.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("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, fakeWorkingImage.SetLabel(lifecycle.StackIDLabel, "io.buildpacks.stacks.bionic"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(lifecycle.StackIDLabel, "io.buildpacks.stacks.cflinuxfs3"))
err := rebaser.Rebase(fakeWorkingImage, 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, fakeWorkingImage.SetLabel(lifecycle.StackIDLabel, "io.buildpacks.stacks.bionic"))
h.AssertNil(t, fakeNewBaseImage.SetLabel(lifecycle.StackIDLabel, ""))
err := rebaser.Rebase(fakeWorkingImage, 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 working image has no stack defined", func() {
h.AssertNil(t, fakeWorkingImage.SetLabel(lifecycle.StackIDLabel, ""))
h.AssertNil(t, fakeNewBaseImage.SetLabel(lifecycle.StackIDLabel, "io.buildpacks.stacks.cflinuxfs3"))
err := rebaser.Rebase(fakeWorkingImage, fakeNewBaseImage, additionalNames)
h.AssertError(t, err, "stack not defined on working image")
})
})
})
}