@@ -646,6 +646,7 @@ func Test_stageBuilder_build(t *testing.T) {
646646 type testcase struct {
647647 description string
648648 opts * config.KanikoOptions
649+ args map [string ]string
649650 layerCache * fakeLayerCache
650651 expectedCacheKeys []string
651652 pushedCacheKeys []string
@@ -738,14 +739,14 @@ func Test_stageBuilder_build(t *testing.T) {
738739 },
739740 },
740741 func () testcase {
741- dir , filenames := tempDirAndFile (t )
742- filename := filenames [0 ]
743- filepath := filepath .Join (dir , filename )
742+ dir , files := tempDirAndFile (t )
743+ file := files [0 ]
744+ filePath := filepath .Join (dir , file )
744745
745- tarContent := generateTar (t , dir , filename )
746+ tarContent := generateTar (t , dir , file )
746747
747748 ch := NewCompositeCache ("" , "" )
748- ch .AddPath (filepath , "" )
749+ ch .AddPath (filePath , "" )
749750
750751 hash , err := ch .Hash ()
751752 if err != nil {
@@ -772,11 +773,11 @@ func Test_stageBuilder_build(t *testing.T) {
772773 commands : getCommands (dir , []instructions.Command {
773774 & instructions.CopyCommand {
774775 SourcesAndDest : []string {
775- filename , "foo.txt" ,
776+ file , "foo.txt" ,
776777 },
777778 },
778779 }),
779- fileName : filename ,
780+ fileName : file ,
780781 }
781782 }(),
782783 func () testcase {
@@ -859,10 +860,10 @@ func Test_stageBuilder_build(t *testing.T) {
859860 }
860861
861862 dockerFile := fmt .Sprintf (`
862- FROM ubuntu:16.04
863- RUN foobar
864- COPY %s bar.txt
865- ` , filename )
863+ FROM ubuntu:16.04
864+ RUN foobar
865+ COPY %s bar.txt
866+ ` , filename )
866867 f , _ := ioutil .TempFile ("" , "" )
867868 ioutil .WriteFile (f .Name (), []byte (dockerFile ), 0755 )
868869 opts := & config.KanikoOptions {
@@ -930,10 +931,10 @@ COPY %s bar.txt
930931 }
931932
932933 dockerFile := fmt .Sprintf (`
933- FROM ubuntu:16.04
934- COPY %s foo.txt
935- COPY %s bar.txt
936- ` , filename , filename )
934+ FROM ubuntu:16.04
935+ COPY %s foo.txt
936+ COPY %s bar.txt
937+ ` , filename , filename )
937938 f , _ := ioutil .TempFile ("" , "" )
938939 ioutil .WriteFile (f .Name (), []byte (dockerFile ), 0755 )
939940 opts := & config.KanikoOptions {
@@ -965,6 +966,71 @@ COPY %s bar.txt
965966 commands : getCommands (dir , cmds ),
966967 }
967968 }(),
969+ func () testcase {
970+ dir , _ := tempDirAndFile (t )
971+ ch := NewCompositeCache ("" )
972+ ch .AddKey ("RUN foobar" )
973+ hash , err := ch .Hash ()
974+ if err != nil {
975+ t .Errorf ("couldn't create hash %v" , err )
976+ }
977+
978+ command := MockDockerCommand {
979+ command : "RUN foobar" ,
980+ contextFiles : []string {},
981+ cacheCommand : MockCachedDockerCommand {
982+ contextFiles : []string {},
983+ },
984+ }
985+
986+ return testcase {
987+ description : "cached run command with no build arg value used uses cached layer and does not push anything" ,
988+ config : & v1.ConfigFile {Config : v1.Config {WorkingDir : dir }},
989+ opts : & config.KanikoOptions {Cache : true },
990+ args : map [string ]string {
991+ "test" : "value" ,
992+ },
993+ expectedCacheKeys : []string {hash },
994+ commands : []commands.DockerCommand {command },
995+ // layer key needs to be read.
996+ layerCache : & fakeLayerCache {
997+ img : & fakeImage {ImageLayers : []v1.Layer {fakeLayer {}}},
998+ keySequence : []string {hash },
999+ },
1000+ rootDir : dir ,
1001+ }
1002+ }(),
1003+ func () testcase {
1004+ dir , _ := tempDirAndFile (t )
1005+
1006+ ch := NewCompositeCache ("" )
1007+ ch .AddKey ("RUN anotherValue" )
1008+ hash , err := ch .Hash ()
1009+ if err != nil {
1010+ t .Errorf ("couldn't create hash %v" , err )
1011+ }
1012+
1013+ command := MockDockerCommand {
1014+ command : "RUN $arg" ,
1015+ contextFiles : []string {},
1016+ cacheCommand : MockCachedDockerCommand {
1017+ contextFiles : []string {},
1018+ },
1019+ }
1020+
1021+ return testcase {
1022+ description : "cached run command with another build arg pushes layer" ,
1023+ config : & v1.ConfigFile {Config : v1.Config {WorkingDir : dir }},
1024+ opts : & config.KanikoOptions {Cache : true },
1025+ args : map [string ]string {
1026+ "arg" : "anotherValue" ,
1027+ },
1028+ expectedCacheKeys : []string {hash },
1029+ pushedCacheKeys : []string {hash },
1030+ commands : []commands.DockerCommand {command },
1031+ rootDir : dir ,
1032+ }
1033+ }(),
9681034 }
9691035 for _ , tc := range testCases {
9701036 t .Run (tc .description , func (t * testing.T ) {
@@ -1002,7 +1068,7 @@ COPY %s bar.txt
10021068 }
10031069 keys := []string {}
10041070 sb := & stageBuilder {
1005- args : & dockerfile.BuildArgs {} , //required or code will panic
1071+ args : dockerfile .NewBuildArgs ([] string {}) , //required or code will panic
10061072 image : tc .image ,
10071073 opts : tc .opts ,
10081074 cf : cf ,
@@ -1014,6 +1080,10 @@ COPY %s bar.txt
10141080 },
10151081 }
10161082 sb .cmds = tc .commands
1083+ for key , value := range tc .args {
1084+ sb .args .AddArg (key , & value )
1085+ }
1086+
10171087 tmp := commands .RootDir
10181088 if tc .rootDir != "" {
10191089 commands .RootDir = tc .rootDir
0 commit comments