Skip to content
118 changes: 76 additions & 42 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,28 +272,8 @@ func getBranchCommitAndURL() (branch, commit, url string) {
return
}

func DockerGitRepo(url string, commit string, branch string) string {
ref := ""
if commit != "" {
ref = "#" + commit
} else if branch != "" {
ref = "#" + branch
}
return fmt.Sprintf("https://%s.git%s", url, ref)
}

func KanikoGitRepo(url string, commit string, branch string) string {
ref := ""
if commit != "" {
ref = "#" + commit
} else if branch != "" {
ref = "#refs/heads/" + branch
}
return fmt.Sprintf("git://%s.git%s", url, ref)
}

func testGitBuildcontextHelper(t *testing.T, url string, commit string, branch string, imageName string) {
t.Log("testGitBuildcontextHelper repo", url)
func testGitBuildcontextHelper(t *testing.T, dockerRef, kanikoRef, imageName string, kanikoExtraFlags ...string) {
t.Log("testGitBuildcontextHelper docker", dockerRef, "kaniko", kanikoRef)
dockerfile := fmt.Sprintf("%s/%s/Dockerfile_test_run_2", integrationPath, dockerfilesPath)

// Build with docker
Expand All @@ -304,7 +284,7 @@ func testGitBuildcontextHelper(t *testing.T, url string, commit string, branch s
"--push",
"-t", dockerImage,
"-f", dockerfile,
DockerGitRepo(url, commit, branch),
dockerRef,
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -319,7 +299,8 @@ func testGitBuildcontextHelper(t *testing.T, url string, commit string, branch s
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", KanikoGitRepo(url, commit, branch))
"-c", kanikoRef)
dockerRunFlags = append(dockerRunFlags, kanikoExtraFlags...)

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

Expand All @@ -338,7 +319,11 @@ func testGitBuildcontextHelper(t *testing.T, url string, commit string, branch s
func TestGitBuildcontext(t *testing.T) {
t.Parallel()
branch, _, url := getBranchCommitAndURL()
testGitBuildcontextHelper(t, url, "", branch, "Dockerfile_test_git_branch")
testGitBuildcontextHelper(t,
fmt.Sprintf("https://%s.git#%s", url, branch),
fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch),
"Dockerfile_test_git_branch",
)
}

// TestGitBuildcontextNoRef builds without any commit / branch reference
Expand All @@ -349,7 +334,11 @@ func TestGitBuildcontextNoRef(t *testing.T) {
t.Skip("Docker's behavior is to assume a 'master' branch, which the Kaniko repo doesn't have")
t.Parallel()
_, _, url := getBranchCommitAndURL()
testGitBuildcontextHelper(t, url, "", "", "Dockerfile_test_git_noref")
testGitBuildcontextHelper(t,
fmt.Sprintf("https://%s.git", url),
fmt.Sprintf("git://%s.git", url),
"Dockerfile_test_git_noref",
)
}

// TestGitBuildcontextExplicitCommit uses an explicit commit hash instead of named reference
Expand All @@ -359,7 +348,11 @@ func TestGitBuildcontextNoRef(t *testing.T) {
func TestGitBuildcontextExplicitCommit(t *testing.T) {
t.Parallel()
_, commit, url := getBranchCommitAndURL()
testGitBuildcontextHelper(t, url, commit, "", "Dockerfile_test_git_commit")
testGitBuildcontextHelper(t,
fmt.Sprintf("https://%s.git#%s", url, commit),
fmt.Sprintf("git://%s.git#%s", url, commit),
"Dockerfile_test_git_commit",
)
}

func TestGitBuildcontextSubPath(t *testing.T) {
Expand All @@ -375,7 +368,7 @@ func TestGitBuildcontextSubPath(t *testing.T) {
"--push",
"-t", dockerImage,
"-f", filepath.Join(integrationPath, dockerfilesPath, dockerfile),
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -392,7 +385,7 @@ func TestGitBuildcontextSubPath(t *testing.T) {
ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", KanikoGitRepo(url, "", branch),
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch),
"--context-sub-path", filepath.Join(integrationPath, dockerfilesPath),
)

Expand All @@ -406,6 +399,46 @@ func TestGitBuildcontextSubPath(t *testing.T) {
containerDiff(t, dockerImage, kanikoImage, "--semantic", "--extra-ignore-file-content", "--extra-ignore-layer-length-mismatch")
}

// TestGitBuildcontextPlainBranch exercises a bare branch name with no
// refs/heads/ prefix (else branch in UnpackTarFromBuildContext around line 88).
func TestGitBuildcontextPlainBranch(t *testing.T) {
t.Parallel()
branch, _, url := getBranchCommitAndURL()
testGitBuildcontextHelper(t,
fmt.Sprintf("https://%s.git#%s", url, branch),
fmt.Sprintf("git://%s.git#%s", url, branch),
"Dockerfile_test_git_plain_branch",
)
}

// TestGitBuildcontextTag exercises a refs/tags/<tag> ref, which goes through
// the post-clone fetch flow (lines 84-87 and 108-124) instead of the direct
// clone-by-ref path used for refs/heads/.
func TestGitBuildcontextTag(t *testing.T) {
t.Parallel()
_, _, url := getBranchCommitAndURL()
tag := "refs/tags/v1.27.5"
testGitBuildcontextHelper(t,
fmt.Sprintf("https://%s.git#%s", url, tag),
fmt.Sprintf("git://%s.git#%s", url, tag),
"Dockerfile_test_git_tag",
)
}

// TestGitBuildcontextBranchFlag exercises the --git=branch=<name> flag, which
// triggers getGitReferenceName (lines 94-100 and 152-179). Docker has no
// equivalent flag, so the docker side puts the branch in the URL fragment.
func TestGitBuildcontextBranchFlag(t *testing.T) {
t.Parallel()
branch, _, url := getBranchCommitAndURL()
testGitBuildcontextHelper(t,
fmt.Sprintf("https://%s.git#%s", url, branch),
fmt.Sprintf("git://%s.git", url),
"Dockerfile_test_git_branch_flag",
"--git=branch="+branch,
)
}

func TestBuildViaRegistryMirrors(t *testing.T) {
t.Parallel()
branch, _, url := getBranchCommitAndURL()
Expand All @@ -419,7 +452,7 @@ func TestBuildViaRegistryMirrors(t *testing.T) {
"--push",
"-t", dockerImage,
"-f", dockerfile,
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -436,7 +469,7 @@ func TestBuildViaRegistryMirrors(t *testing.T) {
"-d", kanikoImage,
"--registry-mirror", "doesnotexist.example.com",
"--registry-mirror", "us-mirror.gcr.io",
"-c", KanikoGitRepo(url, "", branch))
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch))

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

Expand All @@ -461,7 +494,7 @@ func TestBuildViaRegistryMap(t *testing.T) {
"-t", dockerImage,
"-f", dockerfile,
"--push",
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -478,7 +511,7 @@ func TestBuildViaRegistryMap(t *testing.T) {
"-d", kanikoImage,
"--registry-map", "index.docker.io=doesnotexist.example.com",
"--registry-map", "index.docker.io=us-mirror.gcr.io",
"-c", KanikoGitRepo(url, "", branch))
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch))

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

Expand All @@ -505,7 +538,7 @@ func TestBuildSkipFallback(t *testing.T) {
"-d", kanikoImage,
"--registry-mirror", "doesnotexist.example.com",
"--skip-default-registry-fallback",
"-c", KanikoGitRepo(url, "", branch))
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch))

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

Expand All @@ -529,7 +562,7 @@ func TestKanikoDir(t *testing.T) {
"-t", dockerImage,
"-f", dockerfile,
"--push",
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -544,7 +577,7 @@ func TestKanikoDir(t *testing.T) {
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", KanikoGitRepo(url, "", branch))
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch))

kanikoCmd := exec.Command("docker", dockerRunFlags...)
kanikoCmd.Env = append(kanikoCmd.Env, "KANIKO_DIR=/not-kaniko")
Expand Down Expand Up @@ -573,7 +606,7 @@ func TestBuildWithLabels(t *testing.T) {
"-t", dockerImage,
"-f", dockerfile,
"--label", testLabel,
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -589,7 +622,7 @@ func TestBuildWithLabels(t *testing.T) {
"-f", dockerfile,
"-d", kanikoImage,
"--label", testLabel,
"-c", KanikoGitRepo(url, "", branch),
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch),
)

kanikoCmd := exec.Command("docker", dockerRunFlags...)
Expand All @@ -614,7 +647,7 @@ func TestBuildWithHTTPError(t *testing.T) {
"build",
"-t", dockerImage,
"-f", dockerfile,
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
}...)
out, err := RunCommandWithoutTest(dockerCmd)
if err == nil {
Expand All @@ -629,7 +662,7 @@ func TestBuildWithHTTPError(t *testing.T) {
dockerRunFlags = append(dockerRunFlags, ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", KanikoGitRepo(url, "", branch),
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch),
)

kanikoCmd := exec.Command("docker", dockerRunFlags...)
Expand Down Expand Up @@ -880,6 +913,7 @@ func TestWarmerTwice(t *testing.T) {
// Start a sleeping warmer container
dockerRunFlags := []string{"run", "--net=host"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addCoverageFlags(dockerRunFlags)
for _, envVariable := range WarmerEnv {
dockerRunFlags = append(dockerRunFlags, "-e", envVariable)
}
Expand Down Expand Up @@ -1059,7 +1093,7 @@ func TestBuildWithAnnotations(t *testing.T) {
"-t", dockerImage,
"-f", dockerfile,
"--annotation", fmt.Sprintf("%s=%s", annotationKey, annotationValue),
DockerGitRepo(url, "", branch),
fmt.Sprintf("https://%s.git#%s", url, branch),
)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -1075,7 +1109,7 @@ func TestBuildWithAnnotations(t *testing.T) {
"-f", dockerfile,
"-d", kanikoImage,
"--annotation", fmt.Sprintf("%s=%s", annotationKey, annotationValue),
"-c", KanikoGitRepo(url, "", branch),
"-c", fmt.Sprintf("git://%s.git#refs/heads/%s", url, branch),
)
kanikoCmd := exec.Command("docker", dockerRunFlags...)
out, err = RunCommandWithoutTest(kanikoCmd)
Expand Down
1 change: 1 addition & 0 deletions integration/integration_with_stdin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func TestBuildWithStdin(t *testing.T) {

dockerRunFlags := []string{"run", "--interactive", "--net=host", "-v", cwd + ":/workspace"}
dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount)
dockerRunFlags = addCoverageFlags(dockerRunFlags)
dockerRunFlags = append(dockerRunFlags,
ExecutorImage,
"-f", dockerfile,
Expand Down
Loading
Loading