-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 11 commits
4a81561
4469daf
cd0101e
094d030
d0fd846
4efd0e7
9d91c1f
90d9c54
e1c03d8
74f089b
b531fb3
0833122
c914273
37d4ab7
229614d
126db2d
c9e005c
fe41419
e0e254f
386f4d6
e13ee62
86ec5a7
cd126e5
e6517b5
502d203
6d0892f
bbdbd12
c9750c8
929256d
089bc58
55298cc
f9fec87
bd85e0a
59b3232
64c157e
416ee82
0fc8385
1050dd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package integration | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"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://" | ||
containerDiffOutputFile = "container-diff.json" | ||
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", "..") | ||
output, err := buildKaniko.CombinedOutput() | ||
if err != nil { | ||
fmt.Printf("output=%s\n", output) | ||
fmt.Printf("err=%s\n", err) | ||
os.Exit(1) | ||
} | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
dockerfiles, err := ioutil.ReadDir(dockerfilesPath) | ||
if err != nil { | ||
fmt.Printf("err=%s", err) | ||
t.FailNow() | ||
} | ||
|
||
_, ex, _, _ := runtime.Caller(0) | ||
cwd := filepath.Dir(ex) | ||
|
||
// Grab the latest container-diff binary | ||
getContainerDiff := exec.Command("gsutil", "cp", "gs://container-diff/latest/container-diff-linux-amd64", ".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just require that container-diff is on the path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
err = getContainerDiff.Run() | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
|
||
containerDiffPerms := exec.Command("chmod", "+x", "container-diff-linux-amd64") | ||
err = containerDiffPerms.Run() | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
|
||
|
||
for _, dockerfile := range dockerfiles { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filepath.Glob might be easier than ls'ing everything and filtering here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep. done. |
||
if strings.HasSuffix(dockerfile.Name(), ".yaml") { | ||
continue | ||
} | ||
t.Run("test_"+dockerfile.Name(), func(t *testing.T) { | ||
// Parallelization is broken | ||
// t.Parallel() | ||
|
||
// build docker image | ||
dockerImage := strings.ToLower(testRepo + dockerPrefix + dockerfile.Name()) | ||
dockerCmd := exec.Command("docker", "build", "-t", dockerImage, "-f", path.Join(dockerfilesPath, dockerfile.Name()), ".") | ||
err = dockerCmd.Run() | ||
if err != nil { | ||
t.Error(err) | ||
t.Fail() | ||
} | ||
|
||
// build kaniko image | ||
kanikoImage := strings.ToLower(testRepo + kanikoPrefix + dockerfile.Name()) | ||
kanikoCmd := exec.Command("docker", "run", | ||
"-v", os.Getenv("HOME")+"/.config/gcloud:/root/.config/gcloud", | ||
"-v", cwd + ":/workspace", | ||
executorImage, | ||
"-f", path.Join(buildContextPath, dockerfilesPath, dockerfile.Name()), | ||
"-d", kanikoImage, | ||
"-c", buildContextPath, | ||
) | ||
|
||
err = kanikoCmd.Run() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a small helper that takes the cmd and t and does this could be useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
if err != nil { | ||
t.Error(err) | ||
t.Fail() | ||
} | ||
|
||
// container-diff | ||
daemonDockerImage := daemonPrefix + dockerImage | ||
//daemonKanikoImage := daemonPrefix + kanikoImage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
containerdiffCmd := exec.Command("./container-diff-linux-amd64", "diff", daemonDockerImage, kanikoImage, "-q", "--type=file", "--json") | ||
diff, err := containerdiffCmd.CombinedOutput() | ||
if err != nil { | ||
t.Error(err) | ||
t.Fail() | ||
} | ||
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) | ||
}) | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit weird, could you use os.Args[0] instead?
https://golang.org/pkg/os/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.Args gives me where the test file is actually being run, which is in a tmp folder somewhere. I actually want to access the directory where the go file actually lives, which is what runtime.Caller gives.