Skip to content

Commit

Permalink
add more tests for stageBuilder_build
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Mar 5, 2020
1 parent 248b201 commit 4bf24ed
Showing 1 changed file with 86 additions and 16 deletions.
102 changes: 86 additions & 16 deletions pkg/executor/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ func Test_stageBuilder_build(t *testing.T) {
type testcase struct {
description string
opts *config.KanikoOptions
args map[string]string
layerCache *fakeLayerCache
expectedCacheKeys []string
pushedCacheKeys []string
Expand Down Expand Up @@ -738,14 +739,14 @@ func Test_stageBuilder_build(t *testing.T) {
},
},
func() testcase {
dir, filenames := tempDirAndFile(t)
filename := filenames[0]
filepath := filepath.Join(dir, filename)
dir, files := tempDirAndFile(t)
file := files[0]
filePath := filepath.Join(dir, file)

tarContent := generateTar(t, dir, filename)
tarContent := generateTar(t, dir, file)

ch := NewCompositeCache("", "")
ch.AddPath(filepath, "")
ch.AddPath(filePath, "")

hash, err := ch.Hash()
if err != nil {
Expand All @@ -772,11 +773,11 @@ func Test_stageBuilder_build(t *testing.T) {
commands: getCommands(dir, []instructions.Command{
&instructions.CopyCommand{
SourcesAndDest: []string{
filename, "foo.txt",
file, "foo.txt",
},
},
}),
fileName: filename,
fileName: file,
}
}(),
func() testcase {
Expand Down Expand Up @@ -859,10 +860,10 @@ func Test_stageBuilder_build(t *testing.T) {
}

dockerFile := fmt.Sprintf(`
FROM ubuntu:16.04
RUN foobar
COPY %s bar.txt
`, filename)
FROM ubuntu:16.04
RUN foobar
COPY %s bar.txt
`, filename)
f, _ := ioutil.TempFile("", "")
ioutil.WriteFile(f.Name(), []byte(dockerFile), 0755)
opts := &config.KanikoOptions{
Expand Down Expand Up @@ -930,10 +931,10 @@ COPY %s bar.txt
}

dockerFile := fmt.Sprintf(`
FROM ubuntu:16.04
COPY %s foo.txt
COPY %s bar.txt
`, filename, filename)
FROM ubuntu:16.04
COPY %s foo.txt
COPY %s bar.txt
`, filename, filename)
f, _ := ioutil.TempFile("", "")
ioutil.WriteFile(f.Name(), []byte(dockerFile), 0755)
opts := &config.KanikoOptions{
Expand Down Expand Up @@ -965,6 +966,71 @@ COPY %s bar.txt
commands: getCommands(dir, cmds),
}
}(),
func() testcase {
dir, _ := tempDirAndFile(t)
ch := NewCompositeCache("")
ch.AddKey("RUN foobar")
hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
}

command := MockDockerCommand{
command: "RUN foobar",
contextFiles: []string{},
cacheCommand: MockCachedDockerCommand{
contextFiles: []string{},
},
}

return testcase{
description: "cached run command with no build arg value used uses cached layer and does not push anything",
config: &v1.ConfigFile{Config: v1.Config{WorkingDir: dir}},
opts: &config.KanikoOptions{Cache: true},
args: map[string]string{
"test": "value",
},
expectedCacheKeys: []string{hash},
commands: []commands.DockerCommand{command},
// layer key needs to be read.
layerCache: &fakeLayerCache{
img: &fakeImage{ImageLayers: []v1.Layer{fakeLayer{}}},
keySequence: []string{hash},
},
rootDir: dir,
}
}(),
func() testcase {
dir, _ := tempDirAndFile(t)

ch := NewCompositeCache("")
ch.AddKey("RUN anotherValue")
hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
}

command := MockDockerCommand{
command: "RUN $arg",
contextFiles: []string{},
cacheCommand: MockCachedDockerCommand{
contextFiles: []string{},
},
}

return testcase{
description: "cached run command with another build arg pushes layer",
config: &v1.ConfigFile{Config: v1.Config{WorkingDir: dir}},
opts: &config.KanikoOptions{Cache: true},
args: map[string]string{
"arg": "anotherValue",
},
expectedCacheKeys: []string{hash},
pushedCacheKeys: []string{hash},
commands: []commands.DockerCommand{command},
rootDir: dir,
}
}(),
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
Expand Down Expand Up @@ -1002,7 +1068,7 @@ COPY %s bar.txt
}
keys := []string{}
sb := &stageBuilder{
args: &dockerfile.BuildArgs{}, //required or code will panic
args: dockerfile.NewBuildArgs([]string{}), //required or code will panic
image: tc.image,
opts: tc.opts,
cf: cf,
Expand All @@ -1014,6 +1080,10 @@ COPY %s bar.txt
},
}
sb.cmds = tc.commands
for key, value := range tc.args {
sb.args.AddArg(key, &value)
}

tmp := commands.RootDir
if tc.rootDir != "" {
commands.RootDir = tc.rootDir
Expand Down

0 comments on commit 4bf24ed

Please sign in to comment.