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/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..6f0b9b8a6 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -386,6 +386,40 @@ 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("an error was expected, got %s", 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("an error was expected, got %s", string(out)) + } +} + func TestLayers(t *testing.T) { offset := map[string]int{ "Dockerfile_test_add": 12, diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 9a34f2974..71657ac94 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("invalid response status %d", resp.StatusCode) + } + if err := CreateFile(dest, resp.Body, 0600, uint32(uid), uint32(gid)); err != nil { return err }