Skip to content

Commit

Permalink
test: add integration test for local .tar.gz
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanGoasdoue committed Mar 8, 2020
1 parent 5364c56 commit f3d6b23
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
20 changes: 20 additions & 0 deletions integration/dockerfiles-to-tar-gz/Dockerfile_echo_hey
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions integration/dockerfiles-to-tar-gz/Dockerfile_echo_home
Original file line number Diff line number Diff line change
@@ -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
90 changes: 88 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f3d6b23

Please sign in to comment.