@@ -27,6 +27,7 @@ import (
2727 "time"
2828
2929 "github.com/google/go-cmp/cmp"
30+ "github.com/google/go-cmp/cmp/cmpopts"
3031 v1 "github.com/google/go-containerregistry/pkg/v1"
3132 "github.com/google/go-containerregistry/pkg/v1/empty"
3233 "github.com/google/go-containerregistry/pkg/v1/match"
@@ -324,20 +325,47 @@ func TestMutateCreatedAt(t *testing.T) {
324325}
325326
326327func TestMutateTime (t * testing.T ) {
327- source := sourceImage (t )
328- want := time.Time {}
329- result , err := mutate .Time (source , want )
330- if err != nil {
331- t .Fatalf ("failed to mutate a config: %v" , err )
332- }
328+ for _ , tc := range []struct {
329+ name string
330+ source v1.Image
331+ }{
332+ {
333+ name : "image with matching history and layers" ,
334+ source : sourceImage (t ),
335+ },
336+ {
337+ name : "image with empty_layer history entries" ,
338+ source : sourceImagePath (t , "testdata/source_image_with_empty_layer_history.tar" ),
339+ },
340+ } {
341+ t .Run (tc .name , func (t * testing.T ) {
342+ want := time.Time {}
343+ result , err := mutate .Time (tc .source , want )
344+ if err != nil {
345+ t .Fatalf ("failed to mutate a config: %v" , err )
346+ }
333347
334- if configDigestsAreEqual (t , source , result ) {
335- t .Fatal ("mutating the created time MUST mutate the config digest" )
336- }
348+ if configDigestsAreEqual (t , tc . source , result ) {
349+ t .Fatal ("mutating the created time MUST mutate the config digest" )
350+ }
337351
338- got := getConfigFile (t , result ).Created .Time
339- if got != want {
340- t .Fatalf ("mutating the created time MUST mutate the time from %v to %v" , got , want )
352+ mutatedOriginalConfig := getConfigFile (t , tc .source ).DeepCopy ()
353+ gotConfig := getConfigFile (t , result )
354+
355+ // manually change the fields we expect to be changed by mutate.Time
356+ mutatedOriginalConfig .Author = ""
357+ mutatedOriginalConfig .Created = v1.Time {Time : want }
358+ for i := range mutatedOriginalConfig .History {
359+ mutatedOriginalConfig .History [i ].Created = v1.Time {Time : want }
360+ mutatedOriginalConfig .History [i ].Author = ""
361+ }
362+
363+ if diff := cmp .Diff (mutatedOriginalConfig , gotConfig ,
364+ cmpopts .IgnoreFields (v1.RootFS {}, "DiffIDs" ),
365+ ); diff != "" {
366+ t .Errorf ("configFile() mismatch (-want +got):\n %s" , diff )
367+ }
368+ })
341369 }
342370}
343371
@@ -636,9 +664,13 @@ func assertMTime(t *testing.T, layer v1.Layer, expectedTime time.Time) {
636664}
637665
638666func sourceImage (t * testing.T ) v1.Image {
667+ return sourceImagePath (t , "testdata/source_image.tar" )
668+ }
669+
670+ func sourceImagePath (t * testing.T , tarPath string ) v1.Image {
639671 t .Helper ()
640672
641- image , err := tarball .ImageFromPath ("testdata/source_image.tar" , nil )
673+ image , err := tarball .ImageFromPath (tarPath , nil )
642674 if err != nil {
643675 t .Fatalf ("Error loading image: %v" , err )
644676 }
0 commit comments