forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_rebase_test.go
212 lines (183 loc) · 7.41 KB
/
old_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
package pack_test
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/fatih/color"
"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 TestRebaseFactory(t *testing.T) {
color.NoColor = true
spec.Run(t, "rebase_factory", testRebaseFactory, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testRebaseFactory(t *testing.T, when spec.G, it spec.S) {
when("#RebaseFactory", func() {
var (
mockController *gomock.Controller
mockFetcher *mocks.MockFetcher
factory pack.RebaseFactory
outBuf bytes.Buffer
errBuff bytes.Buffer
)
it.Before(func() {
mockController = gomock.NewController(t)
mockFetcher = mocks.NewMockFetcher(mockController)
factory = pack.RebaseFactory{
Logger: logging.NewLogger(&outBuf, &errBuff, false, false),
Fetcher: mockFetcher,
}
})
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)
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "myorg/myrepo", gomock.Any()).Return(mockImage, nil)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "my/run/image", gomock.Any()).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)
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "myorg/myrepo", gomock.Any()).Return(mockImage, nil)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "my/run/image", gomock.Any()).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)
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "myorg/myrepo", gomock.Any()).Return(mockImage, nil)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "some/other/runimage", gomock.Any()).Return(mockBaseImage, nil)
mockImage.EXPECT().Label("io.buildpacks.run-image").Return("some/other/runimage", nil)
flags := pack.RebaseFlags{
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("returns an error", func() {
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "myorg/myrepo", gomock.Any()).Return(mockImage, nil)
mockImage.EXPECT().Label("io.buildpacks.run-image").Return("", 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)
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "default/run", gomock.Any()).Return(mockBaseImage, nil)
mockFetcher.EXPECT().FetchUpdatedLocalImage(gomock.Any(), "myorg/myrepo", gomock.Any()).Return(mockImage, 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)
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchLocalImage("default/run").Return(mockBaseImage, nil)
mockFetcher.EXPECT().FetchLocalImage("myorg/myrepo").Return(mockImage, 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)
mockImage := mocks.NewMockImage(mockController)
mockFetcher.EXPECT().FetchRemoteImage("default/run").Return(mockBaseImage, nil)
mockFetcher.EXPECT().FetchRemoteImage("myorg/myrepo").Return(mockImage, 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().TopLayer().Return("some-top-layer", nil)
mockBaseImage.EXPECT().Digest().Return("some-sha", nil)
mockImage := mocks.NewMockImage(mockController)
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)
})
})
})
}