forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebase_test.go
249 lines (204 loc) · 9.04 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package pack_test
import (
"bytes"
"context"
"testing"
"github.com/buildpack/lifecycle/image/fakes"
"github.com/fatih/color"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/logging"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpack/pack"
"github.com/buildpack/pack/mocks"
h "github.com/buildpack/pack/testhelpers"
)
func TestRebase(t *testing.T) {
color.NoColor = true
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 (
mockController *gomock.Controller
mockImageFetcher *mocks.MockImageFetcher
client *pack.Client
cfg *config.Config
outBuf bytes.Buffer
errBuff bytes.Buffer
)
it.Before(func() {
mockController = gomock.NewController(t)
mockImageFetcher = mocks.NewMockImageFetcher(mockController)
cfg = &config.Config{}
client = pack.NewClient(
cfg,
logging.NewLogger(&outBuf, &errBuff, false, false),
mockImageFetcher,
)
})
it.After(func() {
mockController.Finish()
})
when("#Rebase", func() {
when("run image is provided by the user", func() {
when("the image has a label with a run image specified", func() {
it("uses the run image provided by the user", func() {
fakeNewBaseImage := fakes.NewImage(t, "some/run", "new-base-top-layer-sha", "new-base-digest")
fakeAppImage := fakes.NewImage(t, "some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata", "{}")
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/app", true, true).Return(fakeAppImage, nil)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/run", true, true).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RunImage: "some/run",
RepoName: "some/app",
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-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() {
fakeNewBaseImage := fakes.NewImage(t, "some/run", "new-base-top-layer-sha", "new-base-digest")
fakeAppImage := fakes.NewImage(t, "some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata", `{"stack":{"runImage":{"image":"some/run"}}}`)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/app", true, true).Return(fakeAppImage, nil)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/run", true, true).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RepoName: "some/app",
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-base-digest"`)
})
})
when("the image has a label with a run image mirrors specified", func() {
var fakeAppImage *fakes.Image
it.Before(func() {
fakeAppImage = fakes.NewImage(t, "example.com/some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata",
`{"stack":{"runImage":{"image":"some/run", "mirrors":["example.com/some/run"]}}}`)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/some/app", true, true).Return(fakeAppImage, nil)
})
when("there are no locally-configured mirrors", func() {
it("chooses a matching mirror from the app image label", func() {
fakeNewBaseImage := fakes.NewImage(t, "example.com/some/run", "new-base-top-layer-sha", "new-base-digest")
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/some/run", true, true).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RepoName: "example.com/some/app",
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "example.com/some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-base-digest"`)
})
})
when("there are locally-configured mirrors", func() {
it.Before(func() {
cfg.RunImages = []config.RunImage{
{
Image: "some/run",
Mirrors: []string{"example.com/some/local-run"},
},
}
})
it("chooses a matching local mirror first", func() {
fakeNewBaseImage := fakes.NewImage(t, "example.com/some/local-run", "new-base-top-layer-sha", "new-base-digest")
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/some/local-run", true, true).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RepoName: "example.com/some/app",
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "example.com/some/local-run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-base-digest"`)
})
})
})
when("the image does not have a label with a run image specified", func() {
it("returns an error", func() {
fakeAppImage := fakes.NewImage(t, "some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata", "{}")
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/app", true, true).Return(fakeAppImage, nil)
opts := pack.RebaseOptions{
RepoName: "some/app",
}
err := client.Rebase(context.TODO(), opts)
h.AssertError(t, err, "run image must be specified")
})
})
})
when("publish is false", func() {
when("skip pull is false", func() {
it("updates the local image", func() {
fakeNewBaseImage := fakes.NewImage(t, "some/run", "new-base-top-layer-sha", "new-base-digest")
fakeAppImage := fakes.NewImage(t, "some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata",
`{"stack":{"runImage":{"image":"some/run"}}}`)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/app", true, true).Return(fakeAppImage, nil)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/run", true, true).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RepoName: "some/app",
SkipPull: false,
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-base-digest"`)
})
})
when("skip pull is true", func() {
it("uses local image", func() {
fakeNewBaseImage := fakes.NewImage(t, "some/run", "new-base-top-layer-sha", "new-base-digest")
fakeAppImage := fakes.NewImage(t, "some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata",
`{"stack":{"runImage":{"image":"some/run"}}}`)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/app", true, false).Return(fakeAppImage, nil)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/run", true, false).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RepoName: "some/app",
SkipPull: true,
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-base-digest"`)
})
})
})
when("publish is true", func() {
when("skip pull is anything", func() {
it("uses remote image", func() {
fakeNewBaseImage := fakes.NewImage(t, "some/run", "new-base-top-layer-sha", "new-base-digest")
fakeAppImage := fakes.NewImage(t, "some/app", "", "")
fakeAppImage.SetLabel("io.buildpacks.lifecycle.metadata",
`{"stack":{"runImage":{"image":"some/run"}}}`)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/app", false, true).Return(fakeAppImage, nil)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/run", false, true).Return(fakeNewBaseImage, nil)
opts := pack.RebaseOptions{
RepoName: "some/app",
Publish: true,
}
err := client.Rebase(context.TODO(), opts)
h.AssertNil(t, err)
h.AssertEq(t, fakeAppImage.Base(), "some/run")
lbl, _ := fakeAppImage.Label("io.buildpacks.lifecycle.metadata")
h.AssertContains(t, lbl, `"runImage":{"topLayer":"new-base-top-layer-sha","sha":"new-base-digest"`)
})
})
})
})
})
}