Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test refactoring #126

Merged
merged 38 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4a81561
integration test refactoring
sharifelgamal Apr 20, 2018
4469daf
config file cleanup
sharifelgamal Apr 23, 2018
cd0101e
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into t…
sharifelgamal Apr 24, 2018
094d030
more test refactoring
sharifelgamal Apr 24, 2018
d0fd846
remove debug file
sharifelgamal Apr 24, 2018
4efd0e7
moving around more files
sharifelgamal Apr 24, 2018
9d91c1f
fixing up integration tests
sharifelgamal Apr 30, 2018
90d9c54
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into t…
sharifelgamal Apr 30, 2018
e1c03d8
integration tests work
sharifelgamal May 2, 2018
74f089b
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into t…
sharifelgamal May 2, 2018
b531fb3
some housekeeping
sharifelgamal May 2, 2018
0833122
fixing tests
sharifelgamal May 2, 2018
c914273
addressing comments
sharifelgamal May 2, 2018
37d4ab7
debugging
sharifelgamal May 2, 2018
229614d
debugging
sharifelgamal May 3, 2018
126db2d
actual debugging
sharifelgamal May 3, 2018
c9e005c
skip integration tests for travis
sharifelgamal May 3, 2018
fe41419
install container-diff before integration tests
sharifelgamal May 3, 2018
e0e254f
syntax
sharifelgamal May 3, 2018
386f4d6
make test failures less noisy
sharifelgamal May 3, 2018
e13ee62
fixing tests
sharifelgamal May 3, 2018
86ec5a7
hopefully fixing CI?
sharifelgamal May 8, 2018
cd126e5
fixes
sharifelgamal May 8, 2018
e6517b5
more fixes
sharifelgamal May 8, 2018
502d203
let's actually fix CI
sharifelgamal May 8, 2018
6d0892f
more testing
sharifelgamal May 8, 2018
bbdbd12
testing
sharifelgamal May 8, 2018
c9750c8
proper auth
sharifelgamal May 9, 2018
929256d
typos
sharifelgamal May 9, 2018
089bc58
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into t…
sharifelgamal May 14, 2018
55298cc
Merge branch 'master' of github.com:GoogleCloudPlatform/kaniko into t…
sharifelgamal May 14, 2018
f9fec87
adding support for args in integration tests
sharifelgamal May 14, 2018
bd85e0a
formatting
sharifelgamal May 14, 2018
59b3232
formatting
sharifelgamal May 14, 2018
64c157e
adding support for testing bucket context
sharifelgamal May 14, 2018
416ee82
adding bucket test dockerfile
sharifelgamal May 14, 2018
0fc8385
addressing comments
sharifelgamal May 15, 2018
1050dd4
syntax
sharifelgamal May 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ if [ -f "$KOKORO_GFILE_DIR"/common.sh ]; then
mkdir -p /usr/local/go/src/github.com/GoogleContainerTools/
cp -r github/kaniko /usr/local/go/src/github.com/GoogleContainerTools/
pushd /usr/local/go/src/github.com/GoogleContainerTools/kaniko
echo "Installing container-diff..."
mv $KOKORO_GFILE_DIR/container-diff-linux-amd64 $KOKORO_GFILE_DIR/container-diff
chmod +x $KOKORO_GFILE_DIR/container-diff
export PATH=$PATH:$KOKORO_GFILE_DIR
cp $KOKORO_ROOT/src/keystore/72508_gcr_application_creds $HOME/.config/gcloud/application_default_credentials.json
fi

echo "Running integration tests..."
make out/executor
go run integration_tests/integration_test_yaml.go | gcloud container builds submit --config /dev/fd/0 .
pushd integration
go test
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions integration/dockerfiles/Dockerfile_test_copy_bucket
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM alpine:3.7
COPY context/foo foo
COPY context/foo /foodir/
COPY context/bar/b* bar/
COPY context/fo? /foo2
COPY context/bar/doesnotexist* context/foo hello
COPY ./context/empty /empty
COPY ./ dir/
COPY . newdir
COPY context/bar /baz/
COPY ["context/foo", "/tmp/foo" ]
COPY context/b* /baz/
COPY context/foo context/bar/ba? /test/
COPY context/arr[[]0].txt /mydir/
COPY context/bar/bat .

ENV contextenv ./context
COPY ${contextenv}/foo /tmp/foo2
COPY $contextenv/foo /tmp/foo3
COPY $contextenv/* /tmp/${contextenv}/
194 changes: 194 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
Copyright 2018 Google LLC

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.
*/

package integration

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/GoogleContainerTools/kaniko/testutil"
)

const (
executorImage = "executor-image"
dockerImage = "gcr.io/cloud-builders/docker"
ubuntuImage = "ubuntu"
testRepo = "gcr.io/kaniko-test/"
dockerPrefix = "docker-"
kanikoPrefix = "kaniko-"
daemonPrefix = "daemon://"
kanikoTestBucket = "kaniko-test-bucket"
dockerfilesPath = "dockerfiles"
onbuildBaseImage = testRepo + "onbuild-base:latest"
buildContextPath = "/workspace"
emptyContainerDiff = `[
{
"Image1": "%s:latest",
"Image2": "%s:latest",
"DiffType": "File",
"Diff": {
"Adds": null,
"Dels": null,
"Mods": null
}
}
]`
)

func TestMain(m *testing.M) {
buildKaniko := exec.Command("docker", "build", "-t", executorImage, "-f", "../deploy/Dockerfile", "..")
err := buildKaniko.Run()
if err != nil {
fmt.Print(err)
fmt.Print("Building kaniko failed.")
os.Exit(1)
}

// Make sure container-diff is on user's PATH
err = exec.Command("container-diff").Run()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: exec.LookPath would probably be a bit easier here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if err != nil {
fmt.Print("Make sure you have container-diff installed and on your PATH")
os.Exit(1)
}

os.Exit(m.Run())
}

func TestRun(t *testing.T) {
dockerfiles, err := filepath.Glob(path.Join(dockerfilesPath, "Dockerfile*"))
if err != nil {
t.Error(err)
t.FailNow()
}

// Map for test Dockerfile to expected ARGs
argsMap := map[string][]string{
"Dockerfile_test_run": {"file=/file"},
"Dockerfile_test_workdir": {"workdir=/arg/workdir"},
"Dockerfile_test_add": {"file=context/foo"},
"Dockerfile_test_onbuild": {"file=/tmp/onbuild"},
"Dockerfile_test_scratch": {
"hello=hello-value",
"file=context/foo",
"file3=context/b*",
},
}

bucketContextTests := []string{"Dockerfile_test_copy_bucket"}

_, ex, _, _ := runtime.Caller(0)
cwd := filepath.Dir(ex)

for _, dockerfile := range dockerfiles {
t.Run("test_"+dockerfile, func(t *testing.T) {
dockerfile = dockerfile[len("dockerfile/")+1:]
fmt.Printf("%s\n", dockerfile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: t.Log here instead of fmt.Print

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


var buildArgs []string
buildArgFlag := "--build-arg"
for _, arg := range argsMap[dockerfile] {
buildArgs = append(buildArgs, buildArgFlag)
buildArgs = append(buildArgs, arg)
}
// build docker image
dockerImage := strings.ToLower(testRepo + dockerPrefix + dockerfile)
dockerCmd := exec.Command("docker",
append([]string{"build",
"-t", dockerImage,
"-f", path.Join(dockerfilesPath, dockerfile),
"."},
buildArgs...)...,
)
RunCommand(dockerCmd, t)

contextFlag := "-c"
contextPath := buildContextPath
for _, d := range bucketContextTests {
if d == dockerfile {
contextFlag = "-b"
contextPath = kanikoTestBucket
break
}
}

// build kaniko image
kanikoImage := strings.ToLower(testRepo + kanikoPrefix + dockerfile)
kanikoCmd := exec.Command("docker",
append([]string{"run",
"-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud",
"-v", cwd + ":/workspace",
executorImage,
"-f", path.Join(buildContextPath, dockerfilesPath, dockerfile),
"-d", kanikoImage,
contextFlag, contextPath},
buildArgs...)...,
)

RunCommand(kanikoCmd, t)

// container-diff
daemonDockerImage := daemonPrefix + dockerImage
//daemonKanikoImage := daemonPrefix + kanikoImage
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


containerdiffCmd := exec.Command("container-diff", "diff",
daemonDockerImage, kanikoImage,
"-q", "--type=file", "--json")
diff := RunCommand(containerdiffCmd, t)
t.Logf("diff = %s", string(diff))

expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage)

// Let's compare the json objects themselves instead of strings to avoid
// issues with spaces and indents
var diffInt interface{}
var expectedInt interface{}

err = json.Unmarshal(diff, &diffInt)
if err != nil {
t.Error(err)
t.Fail()
}

err = json.Unmarshal([]byte(expected), &expectedInt)
if err != nil {
t.Error(err)
t.Fail()
}

testutil.CheckErrorAndDeepEqual(t, false, nil, expectedInt, diffInt)
})
}
}

func RunCommand(cmd *exec.Cmd, t *testing.T) []byte {
output, err := cmd.CombinedOutput()
if err != nil {
t.Log(cmd.Args)
t.Log(string(output))
t.Error(err)
t.FailNow()
}

return output
}
12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_add.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_bucket_buildcontext.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_copy.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_extract_fs.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_mv_add.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_onbuild.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_registry.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_run.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_run_2.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_scratch.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_volume.json

This file was deleted.

12 changes: 0 additions & 12 deletions integration_tests/dockerfiles/config_test_workdir.json

This file was deleted.

Loading