diff --git a/integration/dockerfiles-to-tar-gz/Dockerfile_echo_hey b/integration/dockerfiles-to-tar-gz/Dockerfile_echo_hey new file mode 100644 index 0000000000..0087dd6ea9 --- /dev/null +++ b/integration/dockerfiles-to-tar-gz/Dockerfile_echo_hey @@ -0,0 +1,20 @@ +# Copyright 2018 Google, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Test to make sure the executor builds an image correctly +# when no files are changed + +FROM debian:9.11 +RUN echo "hey" +MAINTAINER kaniko diff --git a/integration/dockerfiles-to-tar-gz/Dockerfile_echo_home b/integration/dockerfiles-to-tar-gz/Dockerfile_echo_home new file mode 100644 index 0000000000..0af2c1a1d9 --- /dev/null +++ b/integration/dockerfiles-to-tar-gz/Dockerfile_echo_home @@ -0,0 +1,21 @@ +# Copyright 2018 Google, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Test to make sure the executor builds an image correctly +# when no files are changed + +FROM debian:9.11 +USER www-data +RUN echo $HOME +MAINTAINER kaniko diff --git a/integration/integration_test.go b/integration/integration_test.go index f70522b35b..7ecca7c10d 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -17,27 +17,30 @@ limitations under the License. package integration import ( + "compress/gzip" "encoding/json" "flag" "fmt" + "io/ioutil" "log" "math" "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" + "sync" "testing" "time" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/daemon" - "github.com/pkg/errors" - "github.com/GoogleContainerTools/kaniko/pkg/timing" "github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/GoogleContainerTools/kaniko/testutil" + "github.com/pkg/errors" ) var config *integrationTestConfig @@ -309,6 +312,89 @@ func TestBuildViaRegistryMirror(t *testing.T) { checkContainerDiffOutput(t, diff, expected) } +func TestBuildWithLocalTars(t *testing.T) { + sourceDir := "dockerfiles-to-tar-gz" + + fi, err := ioutil.ReadDir(sourceDir) + if err != nil { + t.Errorf("Failed to TestBuildWithLocalTars: can't list files from %s: %v", sourceDir, err) + } + + var wg sync.WaitGroup + + for _, f := range fi { + wg.Add(1) + dockerfile := filepath.Join(sourceDir, f.Name()) + tarPath := fmt.Sprintf("%s.tar.gz", dockerfile) + + // Create Tar Gz File with dockerfile inside + go func(wg *sync.WaitGroup) { + defer wg.Done() + tarFile, err := os.Create(tarPath) + if err != nil { + t.Errorf("Failed to TestBuildWithLocalTars: can't create %s: %v", tarPath, err) + } + defer tarFile.Close() + + gw := gzip.NewWriter(tarFile) + defer gw.Close() + + tw := util.NewTar(gw) + defer tw.Close() + + if err := tw.AddFileToTar(dockerfile); err != nil { + t.Errorf("Failed to TestBuildWithLocalTars: can't add %s to %s: %v", f.Name(), tarPath, err) + } + }(&wg) + // Waiting for the Tar Gz file creation to be done before moving on + wg.Wait() + + // Build with kaniko + kanikoImage := GetKanikoImage(config.imageRepo, f.Name()) + + _, ex, _, _ := runtime.Caller(0) + cwd := filepath.Dir(ex) + + dockerRunFlags := []string{"run", "--net=host", "-v", cwd + ":/workspace"} + dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = append(dockerRunFlags, ExecutorImage, + "-f", dockerfile, + "-d", kanikoImage, + "-c", fmt.Sprintf("file://%s", tarPath)) + + kanikoCmd := exec.Command("docker", dockerRunFlags...) + + out, err := RunCommandWithoutTest(kanikoCmd) + if err != nil { + t.Errorf("Failed to TestBuildWithLocalTars: can't build image %s with docker command %q: %v %s", kanikoImage, kanikoCmd.Args, err, string(out)) + } + + // Build with docker + dockerImage := GetDockerImage(config.imageRepo, f.Name()) + dockerArgs := []string{ + "build", + "-t", dockerImage, + "-f", dockerfile, + ".", + } + + dockerCmd := exec.Command("docker", dockerArgs...) + out, err = RunCommandWithoutTest(dockerCmd) + if err != nil { + t.Errorf("Failed to TestBuildWithLocalTars: can't build image %s with docker command %q: %v %s", dockerImage, dockerCmd.Args, err, string(out)) + } + + if err := os.Remove(tarPath); err != nil { + t.Errorf("Failed to TestBuildWithLocalTars: can't remove %s: %v", tarPath, err) + } + + diff := containerDiff(t, daemonPrefix+dockerImage, kanikoImage, "--no-cache") + + expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage) + checkContainerDiffOutput(t, diff, expected) + } +} + func TestLayers(t *testing.T) { offset := map[string]int{ "Dockerfile_test_add": 12,