forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase_test.go
232 lines (200 loc) · 8.12 KB
/
rebase_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
package pack
import (
"bytes"
"context"
"testing"
"github.com/buildpacks/pack/config"
"github.com/buildpacks/imgutil/fakes"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
ifakes "github.com/buildpacks/pack/internal/fakes"
"github.com/buildpacks/pack/internal/logging"
h "github.com/buildpacks/pack/testhelpers"
)
func TestRebase(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "rebase_factory", testRebase, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testRebase(t *testing.T, when spec.G, it spec.S) {
when("#Rebase", func() {
var (
fakeImageFetcher *ifakes.FakeImageFetcher
subject *Client
fakeAppImage *fakes.Image
fakeRunImage *fakes.Image
fakeRunImageMirror *fakes.Image
out bytes.Buffer
)
it.Before(func() {
fakeImageFetcher = ifakes.NewFakeImageFetcher()
fakeAppImage = fakes.NewImage("some/app", "", &fakeIdentifier{name: "app-image"})
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata",
`{"stack":{"runImage":{"image":"some/run", "mirrors":["example.com/some/run"]}}}`))
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.stack.id", "io.buildpacks.stacks.bionic"))
fakeImageFetcher.LocalImages["some/app"] = fakeAppImage
fakeRunImage = fakes.NewImage("some/run", "run-image-top-layer-sha", &fakeIdentifier{name: "run-image-digest"})
h.AssertNil(t, fakeRunImage.SetLabel("io.buildpacks.stack.id", "io.buildpacks.stacks.bionic"))
fakeImageFetcher.LocalImages["some/run"] = fakeRunImage
fakeRunImageMirror = fakes.NewImage("example.com/some/run", "mirror-top-layer-sha", &fakeIdentifier{name: "mirror-digest"})
h.AssertNil(t, fakeRunImageMirror.SetLabel("io.buildpacks.stack.id", "io.buildpacks.stacks.bionic"))
fakeImageFetcher.LocalImages["example.com/some/run"] = fakeRunImageMirror
fakeLogger := logging.NewLogWithWriters(&out, &out)
subject = &Client{
logger: fakeLogger,
imageFetcher: fakeImageFetcher,
}
})
it.After(func() {
fakeAppImage.Cleanup()
fakeRunImage.Cleanup()
fakeRunImageMirror.Cleanup()
})
when("#Rebase", func() {
when("run image is provided by the user", func() {
when("the image has a label with a run image specified", func() {
var fakeCustomRunImage *fakes.Image
it.Before(func() {
fakeCustomRunImage = fakes.NewImage("custom/run", "custom-base-top-layer-sha", &fakeIdentifier{name: "custom-base-digest"})
h.AssertNil(t, fakeCustomRunImage.SetLabel("io.buildpacks.stack.id", "io.buildpacks.stacks.bionic"))
fakeImageFetcher.LocalImages["custom/run"] = fakeCustomRunImage
})
it.After(func() {
fakeCustomRunImage.Cleanup()
})
it("uses the run image provided by the user", func() {
h.AssertNil(t, subject.Rebase(context.TODO(),
RebaseOptions{
RunImage: "custom/run",
RepoName: "some/app",
}))
h.AssertEq(t, fakeAppImage.Base(), "custom/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"custom-base-top-layer-sha","reference":"custom-base-digest"`)
})
})
})
when("run image is NOT provided by the user", func() {
when("the image has a label with a run image specified", func() {
it("uses the run image provided in the App image label", func() {
h.AssertNil(t, subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "some/app",
}))
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"run-image-top-layer-sha","reference":"run-image-digest"`)
})
})
when("the image has a label with a run image mirrors specified", func() {
when("there are no user provided mirrors", func() {
it.Before(func() {
fakeImageFetcher.LocalImages["example.com/some/app"] = fakeAppImage
})
it("chooses a matching mirror from the app image label", func() {
h.AssertNil(t, subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "example.com/some/app",
}))
h.AssertEq(t, fakeAppImage.Base(), "example.com/some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"mirror-top-layer-sha","reference":"mirror-digest"`)
})
})
when("there are user provided mirrors", func() {
var (
fakeLocalMirror *fakes.Image
)
it.Before(func() {
fakeImageFetcher.LocalImages["example.com/some/app"] = fakeAppImage
fakeLocalMirror = fakes.NewImage("example.com/some/local-run", "local-mirror-top-layer-sha", &fakeIdentifier{name: "local-mirror-digest"})
h.AssertNil(t, fakeLocalMirror.SetLabel("io.buildpacks.stack.id", "io.buildpacks.stacks.bionic"))
fakeImageFetcher.LocalImages["example.com/some/local-run"] = fakeLocalMirror
})
it.After(func() {
fakeLocalMirror.Cleanup()
})
it("chooses a matching local mirror first", func() {
h.AssertNil(t, subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "example.com/some/app",
AdditionalMirrors: map[string][]string{
"some/run": {"example.com/some/local-run"},
},
}))
h.AssertEq(t, fakeAppImage.Base(), "example.com/some/local-run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"local-mirror-top-layer-sha","reference":"local-mirror-digest"`)
})
})
})
when("the image does not have a label with a run image specified", func() {
it("returns an error", func() {
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata", "{}"))
err := subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "some/app",
})
h.AssertError(t, err, "run image must be specified")
})
})
})
when("publish", func() {
var (
fakeRemoteRunImage *fakes.Image
)
it.Before(func() {
fakeRemoteRunImage = fakes.NewImage("some/run", "remote-top-layer-sha", &fakeIdentifier{name: "remote-digest"})
h.AssertNil(t, fakeRemoteRunImage.SetLabel("io.buildpacks.stack.id", "io.buildpacks.stacks.bionic"))
fakeImageFetcher.RemoteImages["some/run"] = fakeRemoteRunImage
})
it.After(func() {
fakeRemoteRunImage.Cleanup()
})
when("is false", func() {
when("pull policy is always", func() {
it("updates the local image", func() {
h.AssertNil(t, subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "some/app",
PullPolicy: config.PullAlways,
}))
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"remote-top-layer-sha","reference":"remote-digest"`)
})
})
when("pull policy is never", func() {
it("uses local image", func() {
h.AssertNil(t, subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "some/app",
PullPolicy: config.PullNever,
}))
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"run-image-top-layer-sha","reference":"run-image-digest"`)
})
})
})
when("is true", func() {
it.Before(func() {
fakeImageFetcher.RemoteImages["some/app"] = fakeAppImage
})
when("skip pull is anything", func() {
it("uses remote image", func() {
h.AssertNil(t, subject.Rebase(context.TODO(), RebaseOptions{
RepoName: "some/app",
Publish: true,
}))
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"remote-top-layer-sha","reference":"remote-digest"`)
})
})
})
})
})
})
}
type fakeIdentifier struct {
name string
}
func (f *fakeIdentifier) String() string {
return f.name
}