From ca23ae441e1abeab7f27a724e1ff503ca5dd7d4a Mon Sep 17 00:00:00 2001 From: Yoan Blanc Date: Sat, 20 Jun 2020 09:49:11 +0200 Subject: [PATCH 1/4] add should fail on 40x Signed-off-by: Yoan Blanc --- integration/dockerfiles/Dockerfile_test_add_404 | 4 ++++ pkg/util/fs_util.go | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 integration/dockerfiles/Dockerfile_test_add_404 diff --git a/integration/dockerfiles/Dockerfile_test_add_404 b/integration/dockerfiles/Dockerfile_test_add_404 new file mode 100644 index 000000000..21b31ae1f --- /dev/null +++ b/integration/dockerfiles/Dockerfile_test_add_404 @@ -0,0 +1,4 @@ +FROM debian:9.11 + +# Testing that any HTTP failure is handled properly +ADD https://httpstat.us/404 . diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 9a34f2974..38c91357e 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -545,6 +545,11 @@ func DownloadFileToDest(rawurl, dest string, uid, gid int64) error { return err } defer resp.Body.Close() + + if resp.StatusCode >= 400 { + return fmt.Errorf("%s failed with %d", rawurl, resp.StatusCode) + } + if err := CreateFile(dest, resp.Body, 0600, uint32(uid), uint32(gid)); err != nil { return err } From 8cc772ae9c1ea1e7c1e5659af861a78312af467e Mon Sep 17 00:00:00 2001 From: Yoan Blanc Date: Sat, 20 Jun 2020 10:15:50 +0200 Subject: [PATCH 2/4] fixup! add should fail on 40x Signed-off-by: Yoan Blanc --- integration/images.go | 1 + integration/integration_test.go | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/integration/images.go b/integration/images.go index ea5d1f464..3c3f0ccd7 100644 --- a/integration/images.go +++ b/integration/images.go @@ -167,6 +167,7 @@ type DockerFileBuilder struct { func NewDockerFileBuilder() *DockerFileBuilder { d := DockerFileBuilder{filesBuilt: map[string]struct{}{}} d.DockerfilesToIgnore = map[string]struct{}{ + "Dockerfile_test_add_404": {}, // TODO: remove test_user_run from this when https://github.com/GoogleContainerTools/container-diff/issues/237 is fixed "Dockerfile_test_user_run": {}, } diff --git a/integration/integration_test.go b/integration/integration_test.go index 117ba76d7..8eb356923 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -386,6 +386,45 @@ func TestBuildWithLabels(t *testing.T) { checkContainerDiffOutput(t, diff, expected) } +func TestBuildWithHTTPError(t *testing.T) { + repo := getGitRepo() + dockerfile := fmt.Sprintf("%s/%s/Dockerfile_test_add_404", integrationPath, dockerfilesPath) + + // Build with docker + dockerImage := GetDockerImage(config.imageRepo, "Dockerfile_test_add_404") + 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 %q: %s %s", dockerImage, dockerCmd.Args, err, string(out)) + } + + // Build with kaniko + kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_add_404") + dockerRunFlags := []string{"run", "--net=host"} + 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 %q: %v %s", dockerImage, kanikoCmd.Args, err, string(out)) + } + + 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, From 999365f5f9aa56e65f07d99080d7dde570aff30a Mon Sep 17 00:00:00 2001 From: Yoan Blanc Date: Sat, 20 Jun 2020 10:42:59 +0200 Subject: [PATCH 3/4] fixup! fixup! add should fail on 40x Signed-off-by: Yoan Blanc --- integration/integration_test.go | 17 ++++++----------- pkg/util/fs_util.go | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 8eb356923..4406fe42c 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -397,9 +397,9 @@ func TestBuildWithHTTPError(t *testing.T) { "-t", dockerImage, "-f", dockerfile, repo})...) - out, err := RunCommandWithoutTest(dockerCmd) - if err != nil { - t.Errorf("Failed to build image %s with docker command %q: %s %s", dockerImage, dockerCmd.Args, err, string(out)) + _, err := RunCommandWithoutTest(dockerCmd) + if err == nil { + t.Fatalf("an error was expected") } // Build with kaniko @@ -414,15 +414,10 @@ func TestBuildWithHTTPError(t *testing.T) { kanikoCmd := exec.Command("docker", dockerRunFlags...) - out, err = RunCommandWithoutTest(kanikoCmd) - if err != nil { - t.Errorf("Failed to build image %s with kaniko command %q: %v %s", dockerImage, kanikoCmd.Args, err, string(out)) + _, err = RunCommandWithoutTest(kanikoCmd) + if err == nil { + t.Fatalf("an error was expected") } - - 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) { diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 38c91357e..71657ac94 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -547,7 +547,7 @@ func DownloadFileToDest(rawurl, dest string, uid, gid int64) error { defer resp.Body.Close() if resp.StatusCode >= 400 { - return fmt.Errorf("%s failed with %d", rawurl, resp.StatusCode) + return fmt.Errorf("invalid response status %d", resp.StatusCode) } if err := CreateFile(dest, resp.Body, 0600, uint32(uid), uint32(gid)); err != nil { From 5412ac65da6968f233d216cda39e4428f6163f43 Mon Sep 17 00:00:00 2001 From: Yoan Blanc Date: Sat, 20 Jun 2020 10:46:41 +0200 Subject: [PATCH 4/4] fixup! fixup! fixup! add should fail on 40x Signed-off-by: Yoan Blanc --- integration/integration_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 4406fe42c..6f0b9b8a6 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -397,9 +397,9 @@ func TestBuildWithHTTPError(t *testing.T) { "-t", dockerImage, "-f", dockerfile, repo})...) - _, err := RunCommandWithoutTest(dockerCmd) + out, err := RunCommandWithoutTest(dockerCmd) if err == nil { - t.Fatalf("an error was expected") + t.Errorf("an error was expected, got %s", string(out)) } // Build with kaniko @@ -414,9 +414,9 @@ func TestBuildWithHTTPError(t *testing.T) { kanikoCmd := exec.Command("docker", dockerRunFlags...) - _, err = RunCommandWithoutTest(kanikoCmd) + out, err = RunCommandWithoutTest(kanikoCmd) if err == nil { - t.Fatalf("an error was expected") + t.Errorf("an error was expected, got %s", string(out)) } }