@@ -24,8 +24,10 @@ import (
24
24
"math"
25
25
"os"
26
26
"os/exec"
27
+ "path/filepath"
27
28
"strings"
28
29
"testing"
30
+ "time"
29
31
30
32
"github.com/google/go-containerregistry/pkg/name"
31
33
"github.com/google/go-containerregistry/pkg/v1/daemon"
@@ -148,6 +150,7 @@ func TestMain(m *testing.M) {
148
150
fmt .Printf ("error building onbuild base: %v" , err )
149
151
os .Exit (1 )
150
152
}
153
+
151
154
pushOnbuildBase := exec .Command ("docker" , "push" , config .onbuildBaseImage )
152
155
if err := pushOnbuildBase .Run (); err != nil {
153
156
fmt .Printf ("error pushing onbuild base %s: %v" , config .onbuildBaseImage , err )
@@ -165,7 +168,6 @@ func TestMain(m *testing.M) {
165
168
fmt .Printf ("error pushing hardlink base %s: %v" , config .hardlinkBaseImage , err )
166
169
os .Exit (1 )
167
170
}
168
-
169
171
dockerfiles , err := FindDockerFiles (dockerfilesPath )
170
172
if err != nil {
171
173
fmt .Printf ("Coudn't create map of dockerfiles: %s" , err )
@@ -177,6 +179,12 @@ func TestMain(m *testing.M) {
177
179
func TestRun (t * testing.T ) {
178
180
for dockerfile , built := range imageBuilder .FilesBuilt {
179
181
t .Run ("test_" + dockerfile , func (t * testing.T ) {
182
+ if _ , ok := imageBuilder .DockerfilesToIgnore [dockerfile ]; ok {
183
+ t .SkipNow ()
184
+ }
185
+ if _ , ok := imageBuilder .TestCacheDockerfiles [dockerfile ]; ok {
186
+ t .SkipNow ()
187
+ }
180
188
if ! built {
181
189
err := imageBuilder .BuildImage (config .imageRepo , config .gcsBucket , dockerfilesPath , dockerfile )
182
190
if err != nil {
@@ -195,25 +203,8 @@ func TestRun(t *testing.T) {
195
203
t .Logf ("diff = %s" , string (diff ))
196
204
197
205
expected := fmt .Sprintf (emptyContainerDiff , dockerImage , kanikoImage , dockerImage , kanikoImage )
206
+ checkContainerDiffOutput (t , diff , expected )
198
207
199
- // Let's compare the json objects themselves instead of strings to avoid
200
- // issues with spaces and indents
201
- var diffInt interface {}
202
- var expectedInt interface {}
203
-
204
- err := json .Unmarshal (diff , & diffInt )
205
- if err != nil {
206
- t .Error (err )
207
- t .Fail ()
208
- }
209
-
210
- err = json .Unmarshal ([]byte (expected ), & expectedInt )
211
- if err != nil {
212
- t .Error (err )
213
- t .Fail ()
214
- }
215
-
216
- testutil .CheckErrorAndDeepEqual (t , false , nil , expectedInt , diffInt )
217
208
})
218
209
}
219
210
}
@@ -228,6 +219,9 @@ func TestLayers(t *testing.T) {
228
219
}
229
220
for dockerfile , built := range imageBuilder .FilesBuilt {
230
221
t .Run ("test_layer_" + dockerfile , func (t * testing.T ) {
222
+ if _ , ok := imageBuilder .DockerfilesToIgnore [dockerfile ]; ok {
223
+ t .SkipNow ()
224
+ }
231
225
if ! built {
232
226
err := imageBuilder .BuildImage (config .imageRepo , config .gcsBucket , dockerfilesPath , dockerfile )
233
227
if err != nil {
@@ -244,6 +238,58 @@ func TestLayers(t *testing.T) {
244
238
}
245
239
}
246
240
241
+ // Build each image with kaniko twice, and then make sure they're exactly the same
242
+ func TestCache (t * testing.T ) {
243
+ for dockerfile := range imageBuilder .TestCacheDockerfiles {
244
+ t .Run ("test_cache_" + dockerfile , func (t * testing.T ) {
245
+ cache := filepath .Join (config .imageRepo , "cache" , fmt .Sprintf ("%v" , time .Now ().UnixNano ()))
246
+ // Build the initial image which will cache layers
247
+ if err := imageBuilder .buildCachedImages (config .imageRepo , cache , dockerfilesPath , 0 ); err != nil {
248
+ t .Fatalf ("error building cached image for the first time: %v" , err )
249
+ }
250
+ // Build the second image which should pull from the cache
251
+ if err := imageBuilder .buildCachedImages (config .imageRepo , cache , dockerfilesPath , 1 ); err != nil {
252
+ t .Fatalf ("error building cached image for the first time: %v" , err )
253
+ }
254
+ // Make sure both images are the same
255
+ kanikoVersion0 := GetVersionedKanikoImage (config .imageRepo , dockerfile , 0 )
256
+ kanikoVersion1 := GetVersionedKanikoImage (config .imageRepo , dockerfile , 1 )
257
+
258
+ // container-diff
259
+ containerdiffCmd := exec .Command ("container-diff" , "diff" ,
260
+ kanikoVersion0 , kanikoVersion1 ,
261
+ "-q" , "--type=file" , "--type=metadata" , "--json" )
262
+
263
+ diff := RunCommand (containerdiffCmd , t )
264
+ t .Logf ("diff = %s" , diff )
265
+
266
+ expected := fmt .Sprintf (emptyContainerDiff , kanikoVersion0 , kanikoVersion1 , kanikoVersion0 , kanikoVersion1 )
267
+ checkContainerDiffOutput (t , diff , expected )
268
+ })
269
+ }
270
+ }
271
+
272
+ func checkContainerDiffOutput (t * testing.T , diff []byte , expected string ) {
273
+ // Let's compare the json objects themselves instead of strings to avoid
274
+ // issues with spaces and indents
275
+ t .Helper ()
276
+
277
+ var diffInt interface {}
278
+ var expectedInt interface {}
279
+
280
+ err := json .Unmarshal (diff , & diffInt )
281
+ if err != nil {
282
+ t .Error (err )
283
+ }
284
+
285
+ err = json .Unmarshal ([]byte (expected ), & expectedInt )
286
+ if err != nil {
287
+ t .Error (err )
288
+ }
289
+
290
+ testutil .CheckErrorAndDeepEqual (t , false , nil , expectedInt , diffInt )
291
+ }
292
+
247
293
func checkLayers (t * testing.T , image1 , image2 string , offset int ) {
248
294
t .Helper ()
249
295
img1 , err := getImageDetails (image1 )
0 commit comments