forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebase_test.go
281 lines (245 loc) · 11 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
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
package pack_test
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/fatih/color"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/logging"
"github.com/buildpack/lifecycle"
"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
factory pack.RebaseFactory
outBuf bytes.Buffer
errBuff bytes.Buffer
)
it.Before(func() {
mockController = gomock.NewController(t)
MockImageFetcher = mocks.NewMockImageFetcher(mockController)
factory = pack.RebaseFactory{
Logger: logging.NewLogger(&outBuf, &errBuff, false, false),
Fetcher: MockImageFetcher,
Config: &config.Config{},
}
})
it.After(func() {
mockController.Finish()
})
when("#RebaseConfigFromFlags", 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() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
mockImage.EXPECT().Label("io.buildpacks.lifecycle.metadata").
Return(`{"stack":{"runImage":{"image":"label/run/image"}}}`, nil).AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", true, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "my/run/image", true, true).Return(mockBaseImage, nil)
flags := pack.RebaseFlags{
RunImage: "my/run/image",
RepoName: "myorg/myrepo",
}
_, err := factory.RebaseConfigFromFlags(context.TODO(), flags)
h.AssertNil(t, err)
})
})
when("the image does not have a label with a run image specified", func() {
it("uses the run image provided by the user", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", true, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "my/run/image", true, true).Return(mockBaseImage, nil)
flags := pack.RebaseFlags{
RunImage: "my/run/image",
RepoName: "myorg/myrepo",
}
_, err := factory.RebaseConfigFromFlags(context.TODO(), flags)
h.AssertNil(t, err)
})
})
})
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() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", true, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "some/other/runimage", true, true).Return(mockBaseImage, nil)
mockImage.EXPECT().Label("io.buildpacks.lifecycle.metadata").
Return(`{"stack":{"runImage":{"image":"some/other/runimage"}}}`, nil).AnyTimes()
flags := pack.RebaseFlags{
RepoName: "myorg/myrepo",
}
rc, err := factory.RebaseConfigFromFlags(context.TODO(), flags)
h.AssertNil(t, err)
h.AssertSameInstance(t, rc.NewBaseImage, mockBaseImage)
})
})
when("the image has a label with a run image mirrors specified", func() {
it("chooses the best mirror", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/myorg/myrepo", true, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/local/run/image", true, true).Return(mockBaseImage, nil)
mockImage.EXPECT().Label("io.buildpacks.lifecycle.metadata").
Return(`{"stack":{"runImage":{"image":"some/other/runimage", "mirrors":["example.com/run/image"]}}}`, nil).AnyTimes()
factory.Config = &config.Config{
RunImages: []config.RunImage{
{Image: "some/other/runimage", Mirrors: []string{"example.com/local/run/image"}},
},
}
flags := pack.RebaseFlags{
RepoName: "example.com/myorg/myrepo",
}
rc, err := factory.RebaseConfigFromFlags(context.TODO(), flags)
h.AssertNil(t, err)
h.AssertSameInstance(t, rc.NewBaseImage, mockBaseImage)
})
})
when("there are locally configured mirrors", func() {
it("chooses the best mirror from local and label mirrors", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/myorg/myrepo", true, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "example.com/run/image", true, true).Return(mockBaseImage, nil)
mockImage.EXPECT().Label("io.buildpacks.lifecycle.metadata").
Return(`{"stack":{"runImage":{"image":"some/other/runimage", "mirrors":["example.com/run/image"]}}}`, nil).AnyTimes()
flags := pack.RebaseFlags{
RepoName: "example.com/myorg/myrepo",
}
rc, err := factory.RebaseConfigFromFlags(context.TODO(), flags)
h.AssertNil(t, err)
h.AssertSameInstance(t, rc.NewBaseImage, mockBaseImage)
})
})
when("the image does not have a label with a run image specified", func() {
it("returns an error", func() {
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", true, true).Return(mockImage, nil)
mockImage.EXPECT().Label("io.buildpacks.lifecycle.metadata").Return(`{"stack":{}}`, nil)
flags := pack.RebaseFlags{
RepoName: "myorg/myrepo",
}
_, err := factory.RebaseConfigFromFlags(context.TODO(), flags)
h.AssertError(t, err, "run image must be specified")
})
})
})
when("publish is false", func() {
when("no-pull is false", func() {
it("XXXX", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", true, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "default/run", true, true).Return(mockBaseImage, nil)
cfg, err := factory.RebaseConfigFromFlags(context.TODO(), pack.RebaseFlags{
RepoName: "myorg/myrepo",
RunImage: "default/run",
Publish: false,
NoPull: false,
})
h.AssertNil(t, err)
h.AssertSameInstance(t, cfg.Image, mockImage)
h.AssertSameInstance(t, cfg.NewBaseImage, mockBaseImage)
})
})
when("no-pull is true", func() {
it("XXXX", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", true, false).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "default/run", true, false).Return(mockBaseImage, nil)
cfg, err := factory.RebaseConfigFromFlags(context.TODO(), pack.RebaseFlags{
RepoName: "myorg/myrepo",
RunImage: "default/run",
Publish: false,
NoPull: true,
})
h.AssertNil(t, err)
h.AssertSameInstance(t, cfg.Image, mockImage)
h.AssertSameInstance(t, cfg.NewBaseImage, mockBaseImage)
})
})
})
when("publish is true", func() {
when("no-pull is anything", func() {
it("XXXX", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "myorg/myrepo", false, true).Return(mockImage, nil)
MockImageFetcher.EXPECT().Fetch(gomock.Any(), "default/run", false, true).Return(mockBaseImage, nil)
cfg, err := factory.RebaseConfigFromFlags(context.TODO(), pack.RebaseFlags{
RepoName: "myorg/myrepo",
RunImage: "default/run",
Publish: true,
NoPull: false,
})
h.AssertNil(t, err)
h.AssertSameInstance(t, cfg.Image, mockImage)
h.AssertSameInstance(t, cfg.NewBaseImage, mockBaseImage)
})
})
})
})
when("#Rebase", func() {
it("swaps the old base for the new base AND stores new sha for new runimage", func() {
mockBaseImage := mocks.NewMockImage(mockController)
mockBaseImage.EXPECT().Name().Return("some/base-image").AnyTimes()
mockBaseImage.EXPECT().TopLayer().Return("some-top-layer", nil)
mockBaseImage.EXPECT().Digest().Return("some-sha", nil)
mockImage := mocks.NewMockImage(mockController)
mockImage.EXPECT().Name().Return("some/name").AnyTimes()
mockImage.EXPECT().Label("io.buildpacks.lifecycle.metadata").
Return(`{"runimage":{"topLayer":"old-top-layer"}, "app":{"sha":"data"}}`, nil)
mockImage.EXPECT().Rebase("old-top-layer", mockBaseImage)
setLabel := mockImage.EXPECT().SetLabel("io.buildpacks.lifecycle.metadata", gomock.Any()).
Do(func(_, label string) {
var metadata lifecycle.AppImageMetadata
h.AssertNil(t, json.Unmarshal([]byte(label), &metadata))
h.AssertEq(t, metadata.RunImage.TopLayer, "some-top-layer")
h.AssertEq(t, metadata.RunImage.SHA, "some-sha")
h.AssertEq(t, metadata.App.SHA, "data")
})
mockImage.EXPECT().Save().After(setLabel).Return("some-digest", nil)
rebaseConfig := pack.RebaseConfig{
Image: mockImage,
NewBaseImage: mockBaseImage,
}
err := factory.Rebase(rebaseConfig)
h.AssertNil(t, err)
})
})
})
}