Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Dec 11, 2020
1 parent cd9f19e commit e678705
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
5 changes: 4 additions & 1 deletion integration/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func populateVolumeCache() error {
}

// buildCachedImages builds the images for testing caching via kaniko where version is the nth time this image has been built
func (d *DockerFileBuilder) buildCachedImages(config *integrationTestConfig, cacheRepo, dockerfilesPath string, version int) error {
func (d *DockerFileBuilder) buildCachedImages(config *integrationTestConfig, cacheRepo, dockerfilesPath string, version int, args []string) error {
imageRepo, serviceAccount := config.imageRepo, config.serviceAccount
_, ex, _, _ := runtime.Caller(0)
cwd := filepath.Dir(ex)
Expand All @@ -334,6 +334,9 @@ func (d *DockerFileBuilder) buildCachedImages(config *integrationTestConfig, cac
cacheFlag,
"--cache-repo", cacheRepo,
"--cache-dir", cacheDir)
for _, v := range args {
dockerRunFlags = append(dockerRunFlags, v)
}
kanikoCmd := exec.Command("docker", dockerRunFlags...)

_, err := RunCommandWithoutTest(kanikoCmd)
Expand Down
8 changes: 6 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,21 @@ func buildImage(t *testing.T, dockerfile string, imageBuilder *DockerFileBuilder
func TestCache(t *testing.T) {
populateVolumeCache()
for dockerfile := range imageBuilder.TestCacheDockerfiles {
args := []string{}
if dockerfile == "Dockerfile_test_cache_copy" {
args = append(args, "--cache-copy-layers=true")
}
t.Run("test_cache_"+dockerfile, func(t *testing.T) {
dockerfile := dockerfile
t.Parallel()

cache := filepath.Join(config.imageRepo, "cache", fmt.Sprintf("%v", time.Now().UnixNano()))
// Build the initial image which will cache layers
if err := imageBuilder.buildCachedImages(config, cache, dockerfilesPath, 0); err != nil {
if err := imageBuilder.buildCachedImages(config, cache, dockerfilesPath, 0, args); err != nil {
t.Fatalf("error building cached image for the first time: %v", err)
}
// Build the second image which should pull from the cache
if err := imageBuilder.buildCachedImages(config, cache, dockerfilesPath, 1); err != nil {
if err := imageBuilder.buildCachedImages(config, cache, dockerfilesPath, 1, args); err != nil {
t.Fatalf("error building cached image for the first time: %v", err)
}
// Make sure both images are the same
Expand Down
96 changes: 71 additions & 25 deletions pkg/executor/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,17 +802,50 @@ func Test_stageBuilder_build(t *testing.T) {

tarContent := generateTar(t, dir, filename)

ch := NewCompositeCache(fmt.Sprintf("COPY %s foo.txt", filename))
ch := NewCompositeCache("", fmt.Sprintf("COPY %s foo.txt", filename))
ch.AddPath(filepath, util.FileContext{})

hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
}
copyCommandCacheKey := hash
dockerFile := fmt.Sprintf(`
FROM ubuntu:16.04
COPY %s foo.txt
`, filename)
f, _ := ioutil.TempFile("", "")
ioutil.WriteFile(f.Name(), []byte(dockerFile), 0755)
opts := &config.KanikoOptions{
DockerfilePath: f.Name(),
Cache: true,
CacheCopyLayers: true,
}

testStages, metaArgs, err := dockerfile.ParseStages(opts)
if err != nil {
t.Errorf("Failed to parse test dockerfile to stages: %s", err)
}

kanikoStages, err := dockerfile.MakeKanikoStages(opts, testStages, metaArgs)
if err != nil {
t.Errorf("Failed to parse stages to Kaniko Stages: %s", err)
}
_ = ResolveCrossStageInstructions(kanikoStages)
stage := kanikoStages[0]

cmds := stage.Commands

return testcase{
description: "copy command cache enabled and key in cache",
opts: &config.KanikoOptions{Cache: true, CacheCopyLayers: true},
opts: opts,
image: fakeImage{
ImageLayers: []v1.Layer{
fakeLayer{
TarContent: tarContent,
},
},
},
layerCache: &fakeLayerCache{
retrieve: true,
img: fakeImage{
Expand All @@ -827,14 +860,8 @@ func Test_stageBuilder_build(t *testing.T) {
expectedCacheKeys: []string{copyCommandCacheKey},
// CachingCopyCommand is not pushed to the cache
pushedCacheKeys: []string{},
commands: getCommands(util.FileContext{Root: dir}, []instructions.Command{
&instructions.CopyCommand{
SourcesAndDest: []string{
filename, "foo.txt",
},
},
}),
fileName: filename,
commands: getCommands(util.FileContext{Root: dir}, cmds, true),
fileName: filename,
}
}(),
func() testcase {
Expand All @@ -846,16 +873,41 @@ func Test_stageBuilder_build(t *testing.T) {
t.Errorf("could not create temp dir %v", err)
}
filePath := filepath.Join(dir, filename)
ch := NewCompositeCache("", "")
ch := NewCompositeCache("", fmt.Sprintf("COPY %s foo.txt", filename))
ch.AddPath(filePath, util.FileContext{})

hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
}
dockerFile := fmt.Sprintf(`
FROM ubuntu:16.04
COPY %s foo.txt
`, filename)
f, _ := ioutil.TempFile("", "")
ioutil.WriteFile(f.Name(), []byte(dockerFile), 0755)
opts := &config.KanikoOptions{
DockerfilePath: f.Name(),
Cache: true,
CacheCopyLayers: true,
}

testStages, metaArgs, err := dockerfile.ParseStages(opts)
if err != nil {
t.Errorf("Failed to parse test dockerfile to stages: %s", err)
}

kanikoStages, err := dockerfile.MakeKanikoStages(opts, testStages, metaArgs)
if err != nil {
t.Errorf("Failed to parse stages to Kaniko Stages: %s", err)
}
_ = ResolveCrossStageInstructions(kanikoStages)
stage := kanikoStages[0]

cmds := stage.Commands
return testcase{
description: "copy command cache enabled and key is not in cache",
opts: &config.KanikoOptions{Cache: true},
opts: opts,
config: &v1.ConfigFile{Config: v1.Config{WorkingDir: destDir}},
layerCache: &fakeLayerCache{},
image: fakeImage{
Expand All @@ -868,14 +920,8 @@ func Test_stageBuilder_build(t *testing.T) {
rootDir: dir,
expectedCacheKeys: []string{hash},
pushedCacheKeys: []string{hash},
commands: getCommands(util.FileContext{Root: dir}, []instructions.Command{
&instructions.CopyCommand{
SourcesAndDest: []string{
filename, "foo.txt",
},
},
}),
fileName: filename,
commands: getCommands(util.FileContext{Root: dir}, cmds, true),
fileName: filename,
}
}(),
func() testcase {
Expand Down Expand Up @@ -952,7 +998,7 @@ COPY %s bar.txt
// hash1 is the read cachekey for the first layer
expectedCacheKeys: []string{hash1, hash2},
pushedCacheKeys: []string{hash2},
commands: getCommands(util.FileContext{Root: dir}, cmds),
commands: getCommands(util.FileContext{Root: dir}, cmds, true),
}
}(),
func() testcase {
Expand Down Expand Up @@ -1028,7 +1074,7 @@ RUN foobar
image: image,
expectedCacheKeys: []string{runHash},
pushedCacheKeys: []string{},
commands: getCommands(util.FileContext{Root: dir}, cmds),
commands: getCommands(util.FileContext{Root: dir}, cmds, false),
}
}(),
func() testcase {
Expand Down Expand Up @@ -1202,7 +1248,7 @@ RUN foobar
if err != nil {
t.Errorf("Expected error to be nil but was %v", err)
}

fmt.Println(lc.receivedKeys)
assertCacheKeys(t, tc.expectedCacheKeys, lc.receivedKeys, "receive")
assertCacheKeys(t, tc.pushedCacheKeys, keys, "push")

Expand Down Expand Up @@ -1235,14 +1281,14 @@ func assertCacheKeys(t *testing.T, expectedCacheKeys, actualCacheKeys []string,
}
}

func getCommands(fileContext util.FileContext, cmds []instructions.Command) []commands.DockerCommand {
func getCommands(fileContext util.FileContext, cmds []instructions.Command, cacheCopy bool) []commands.DockerCommand {
outCommands := make([]commands.DockerCommand, 0)
for _, c := range cmds {
cmd, err := commands.GetCommand(
c,
fileContext,
false,
false,
cacheCopy,
)
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
}
}
timing.DefaultRun.Stop(t)
logrus.Infof("Pushed images to %d destinations", len(destRefs))
return writeImageOutputs(image, destRefs)
}

Expand Down

0 comments on commit e678705

Please sign in to comment.