Skip to content

Commit

Permalink
Run integration tests in Travis CI
Browse files Browse the repository at this point in the history
- Change integration tests to use docker hub instead of GCR due to bug
in library that requires authentication with gcr.io even for public
images. See GoogleContainerTools#966 for bug tracking this.
- Make uploading to GCS bucket configurable through a flag --uploadToGCS
- Utilize a locally deployed docker registry in travis CI to remove
dependency of needing to authenticate with GCP. This requires host
networking such that we can access the registry on localhost:5000
- Use the commit that's being tested for `TestGitBuildcontext`
- Remove duplicate GitBuildContext case that's now covered by default in
travis CI.
  • Loading branch information
samos123 committed Jan 19, 2020
1 parent 64bfc3e commit 9ac0de5
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 91 deletions.
13 changes: 12 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
language: go
os: linux

dist: bionic
env:
- IMAGE_REPO=localhost:5000
go:
- "1.13.3"
go_import_path: github.com/GoogleContainerTools/kaniko
before_install:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- sudo apt-get update
- sudo apt-get -y -o Dpkg::Options::="--force-confnew" install docker-ce
- curl -LO https://storage.googleapis.com/container-diff/latest/container-diff-linux-amd64 && chmod +x container-diff-linux-amd64 && sudo mv container-diff-linux-amd64 /usr/local/bin/container-diff
- docker run -d -p 5000:5000 --restart always --name registry registry:2
- ./integration/replace-gcr-with-local-registry.sh integration/dockerfiles

script:
- make test
- ./integration-test.sh --uploadToGCS=false
- make images
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_onbuild_base
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM marketplace.gcr.io/google/ubuntu1804@sha256:4649ae6b381090fba6db38137eb05e03f44bf43c40149f734241c9f96aa0e001
FROM ubuntu:18.04
ENV dir /tmp/dir/
ONBUILD RUN echo "onbuild" > /tmp/onbuild
ONBUILD RUN mkdir $dir
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_add
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
# First, try adding some regular files
ADD context/foo foo
ADD context/foo /foodir/
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_cache
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# If the image is built twice, /date should be the same in both images
# if the cache is implemented correctly

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
RUN date > /date
COPY context/foo /foo
RUN echo hey
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_cache_install
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# /date should be the same regardless of when this image is built
# if the cache is implemented correctly

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
WORKDIR /foo
RUN apt-get update && apt-get install -y make
COPY context/bar /context
Expand Down
3 changes: 1 addition & 2 deletions integration/dockerfiles/Dockerfile_test_cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM gcr.io/distroless/base@sha256:628939ac8bf3f49571d05c6c76b8688cb4a851af6c7088e599388259875bde20 AS first
FROM debian:10.2 AS first
CMD ["mycmd"]

FROM first
ENTRYPOINT ["myentrypoint"] # This should clear out CMD in the config metadata
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
ENV hey hey
ENV PATH /usr/local
ENV testmultipleeq="this=is a=test string=with a=lot of=equals"
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_expose
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
EXPOSE 80
EXPOSE 81/udp
ENV protocol tcp
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_extract_fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_label
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
LABEL foo=bar
LABEL "baz"="bat"
ENV label1 "mylabel"
Expand Down
9 changes: 5 additions & 4 deletions integration/dockerfiles/Dockerfile_test_meta_arg
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
ARG REGISTRY=gcr.io
ARG REPO=google-appengine
ARG REGISTRY=docker.io
ARG IMAGE=debian
ARG TAG=9.11
ARG WORD=hello
ARG W0RD2=hey

FROM ${REGISTRY}/${REPO}/debian9 as stage1
FROM ${REGISTRY}/${IMAGE}:${TAG} as stage1

# Should evaluate WORD and create /tmp/hello
ARG WORD
RUN touch /${WORD}

FROM ${REGISTRY}/${REPO}/debian9
FROM ${REGISTRY}/${IMAGE}:${TAG}

COPY --from=stage1 /hello /tmp

Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_metadata
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/distroless/base@sha256:628939ac8bf3f49571d05c6c76b8688cb4a851af6c7088e599388259875bde20
FROM debian:10.2
CMD ["command", "one"]
CMD ["command", "two"]
CMD echo "hello"
Expand Down
4 changes: 2 additions & 2 deletions integration/dockerfiles/Dockerfile_test_multistage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:f0159d14385afcb58a9b2fa8955c0cb64bd3abc365e8589f8c2dd38150fbfdbe as base
FROM debian:9.11 as base
COPY . .

FROM scratch as second
Expand All @@ -20,4 +20,4 @@ FROM fedora@sha256:c4cc32b09c6ae3f1353e7e33a8dda93dc41676b923d6d89afa996b421cc5a
FROM fourth
ARG file
COPY --from=second /foo ${file}
COPY --from=gcr.io/google-appengine/debian9@sha256:00109fa40230a081f5ecffe0e814725042ff62a03e2d1eae0563f1f82eaeae9b /etc/os-release /new
COPY --from=debian:9.11 /etc/os-release /new
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_run
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
RUN echo "hey" > /etc/foo
RUN echo "baz" > /etc/baz
RUN cp /etc/baz /etc/bar
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_run_2
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
# Test to make sure the executor builds an image correctly
# when no files are changed

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
RUN echo "hey"
MAINTAINER kaniko
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_user
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
USER testuser:testgroup

2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_user_run
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
RUN useradd testuser
RUN groupadd testgroup
USER testuser:testgroup
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_volume
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
RUN mkdir /foo
RUN echo "hello" > /foo/hey
VOLUME /foo/bar /tmp /qux/quux
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_volume_2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
FROM debian:9.11
VOLUME /foo1
RUN echo "hello" > /foo1/hello
WORKDIR /foo1/bar
Expand Down
2 changes: 1 addition & 1 deletion integration/dockerfiles/Dockerfile_test_workdir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM gcr.io/google-appengine/debian9@sha256:6b3aa04751aa2ac3b0c7be4ee71148b66d693ad212ce6d3244bd2a2a147f314a
FROM debian:9.11
COPY context/foo foo
WORKDIR /test
# Test that this will be appended on to the previous command, to create /test/workdir
Expand Down
10 changes: 5 additions & 5 deletions integration/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ func (d *DockerFileBuilder) BuildImage(config *gcpConfig, dockerfilesPath, docke
additionalFlags = append(buildArgs, additionalKanikoFlagsMap[dockerfile]...)
kanikoImage := GetKanikoImage(imageRepo, dockerfile)
fmt.Printf("Going to build image with kaniko: %s, flags: %s \n", kanikoImage, additionalFlags)
dockerRunFlags := []string{
"run", "-e", benchmarkEnv,
dockerRunFlags := []string{"run", "--net=host",
"-e", benchmarkEnv,
"-v", cwd + ":/workspace",
"-v", benchmarkDir + ":/kaniko/benchmarks",
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func populateVolumeCache() error {
_, ex, _, _ := runtime.Caller(0)
cwd := filepath.Dir(ex)
warmerCmd := exec.Command("docker",
append([]string{"run",
append([]string{"run", "--net=host",
"-d",
"-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud",
"-v", cwd + ":/workspace",
Expand Down Expand Up @@ -284,7 +284,7 @@ func (d *DockerFileBuilder) buildCachedImages(config *gcpConfig, cacheRepo, dock
}
kanikoImage := GetVersionedKanikoImage(imageRepo, dockerfile, version)

dockerRunFlags := []string{"run",
dockerRunFlags := []string{"run", "--net=host",
"-v", cwd + ":/workspace",
"-e", benchmarkEnv}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount)
Expand Down Expand Up @@ -315,7 +315,7 @@ func (d *DockerFileBuilder) buildRelativePathsImage(imageRepo, dockerfile, servi
buildContextPath := "./relative-subdirectory"
kanikoImage := GetKanikoImage(imageRepo, dockerfile)

dockerRunFlags := []string{"run", "-v", cwd + ":/workspace"}
dockerRunFlags := []string{"run", "--net=host", "-v", cwd + ":/workspace"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
Expand Down
100 changes: 39 additions & 61 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,28 @@ func TestMain(m *testing.M) {
}
config = initGCPConfig()

contextFile, err := CreateIntegrationTarball()
if err != nil {
fmt.Println("Failed to create tarball of integration files for build context", err)
os.Exit(1)
}
if config.uploadToGCS {
contextFile, err := CreateIntegrationTarball()
if err != nil {
fmt.Println("Failed to create tarball of integration files for build context", err)
os.Exit(1)
}

fileInBucket, err := UploadFileToBucket(config.gcsBucket, contextFile, contextFile)
if err != nil {
fmt.Println("Failed to upload build context", err)
os.Exit(1)
}
fileInBucket, err := UploadFileToBucket(config.gcsBucket, contextFile, contextFile)
if err != nil {
fmt.Println("Failed to upload build context", err)
os.Exit(1)
}

err = os.Remove(contextFile)
if err != nil {
fmt.Printf("Failed to remove tarball at %s: %s\n", contextFile, err)
os.Exit(1)
}
err = os.Remove(contextFile)
if err != nil {
fmt.Printf("Failed to remove tarball at %s: %s\n", contextFile, err)
os.Exit(1)
}

RunOnInterrupt(func() { DeleteFromBucket(fileInBucket) })
defer DeleteFromBucket(fileInBucket)
RunOnInterrupt(func() { DeleteFromBucket(fileInBucket) })
defer DeleteFromBucket(fileInBucket)
}

setupCommands := []struct {
name string
Expand Down Expand Up @@ -197,52 +199,26 @@ func TestRun(t *testing.T) {
}
}

func TestGitBuildcontext(t *testing.T) {
repo := "github.com/GoogleContainerTools/kaniko"
dockerfile := "integration/dockerfiles/Dockerfile_test_run_2"

// Build with docker
dockerImage := GetDockerImage(config.imageRepo, "Dockerfile_test_git")
dockerCmd := exec.Command("docker",
append([]string{"build",
"-t", dockerImage,
"-f", dockerfile,
repo})...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
t.Errorf("Failed to build image %s with docker command \"%s\": %s %s", dockerImage, dockerCmd.Args, err, string(out))
}

// Build with kaniko
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
dockerRunFlags := []string{"run"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", fmt.Sprintf("git://%s", repo))

kanikoCmd := exec.Command("docker", dockerRunFlags...)

out, err = RunCommandWithoutTest(kanikoCmd)
if err != nil {
t.Errorf("Failed to build image %s with kaniko command \"%s\": %v %s", dockerImage, kanikoCmd.Args, err, string(out))
func getGitRepo() string {
var branch, repoSlug string
if _, ok := os.LookupEnv("TRAVIS"); ok {
if os.Getenv("TRAVIS_PULL_REQUEST") != "false" {
branch = os.Getenv("TRAVIS_PULL_REQUEST_BRANCH")
repoSlug = os.Getenv("TRAVIS_PULL_REQUEST_SLUG")
log.Printf("Travis CI Pull request source repo: %s branch: %s\n", repoSlug, branch)
} else {
branch = os.Getenv("TRAVIS_BRANCH")
repoSlug = os.Getenv("TRAVIS_REPO_SLUG")
log.Printf("Travis CI pepo: %s branch: %s\n", repoSlug, branch)
}
return "github.com/" + repoSlug + "#refs/heads/" + branch
} else {
return "github.com/GoogleContainerTools/kaniko"
}

// container-diff
daemonDockerImage := daemonPrefix + dockerImage
containerdiffCmd := exec.Command("container-diff", "diff", "--no-cache",
daemonDockerImage, kanikoImage,
"-q", "--type=file", "--type=metadata", "--json")
diff := RunCommand(containerdiffCmd, t)
t.Logf("diff = %s", string(diff))

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
checkContainerDiffOutput(t, diff, expected)
}

func TestGitBuildContextWithBranch(t *testing.T) {
repo := "github.com/GoogleContainerTools/kaniko#refs/tags/v0.10.0"
func TestGitBuildcontext(t *testing.T) {
repo := getGitRepo()
dockerfile := "integration/dockerfiles/Dockerfile_test_run_2"

// Build with docker
Expand All @@ -259,7 +235,7 @@ func TestGitBuildContextWithBranch(t *testing.T) {

// Build with kaniko
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
dockerRunFlags := []string{"run"}
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
Expand Down Expand Up @@ -567,6 +543,7 @@ type gcpConfig struct {
hardlinkBaseImage string
serviceAccount string
dockerMajorVersion int
uploadToGCS bool
}

type imageDetails struct {
Expand All @@ -584,6 +561,7 @@ func initGCPConfig() *gcpConfig {
flag.StringVar(&c.gcsBucket, "bucket", "gs://kaniko-test-bucket", "The gcs bucket argument to uploaded the tar-ed contents of the `integration` dir to.")
flag.StringVar(&c.imageRepo, "repo", "gcr.io/kaniko-test", "The (docker) image repo to build and push images to during the test. `gcloud` must be authenticated with this repo or serviceAccount must be set.")
flag.StringVar(&c.serviceAccount, "serviceAccount", "", "The path to the service account push images to GCR and upload/download files to GCS.")
flag.BoolVar(&c.uploadToGCS, "uploadToGCS", true, "Upload the tar-ed contents of `integration` dir to GCS bucket. Default is true. Set this to false to prevent uploading.")
flag.Parse()

if len(c.serviceAccount) > 0 {
Expand Down
29 changes: 29 additions & 0 deletions integration/replace-gcr-with-local-registry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# This script is needed due to the following bug:
# https://github.com/GoogleContainerTools/kaniko/issues/966


if [ "$#" -ne 1 ]; then
echo "Please specify path to dockerfiles as first argument."
echo "Usage: `basename $0` integration/dockerfiles"
exit 2
fi

dir_with_docker_files=$1

for dockerfile in $dir_with_docker_files/*; do
cat $dockerfile | grep '^FROM' | grep "gcr" | while read -r line; do
gcr_repo=$(echo "$line" | awk '{ print $2 }')
local_repo=$(echo "$gcr_repo" | sed -e "s/^.*gcr.io\(\/.*\)$/localhost:5000\1/")
remove_digest=$(echo "$local_repo" | cut -f1 -d"@")
echo "Running docker pull $gcr_repo"
docker pull "$gcr_repo"
echo "Running docker tag $gcr_repo $remove_digest"
docker tag "$gcr_repo" "$remove_digest"
echo "Running docker push $remove_digest"
docker push "$remove_digest"
echo "Updating dockerfile $dockerfile to use local repo $local_repo"
sed -i -e "s/^\(FROM \).*gcr.io\(.*\)$/\1localhost:5000\2/" $dockerfile
done
done

0 comments on commit 9ac0de5

Please sign in to comment.