diff --git a/cmd/lifecycle/exporter.go b/cmd/lifecycle/exporter.go index 6bfb228ff..1850d97ba 100644 --- a/cmd/lifecycle/exporter.go +++ b/cmd/lifecycle/exporter.go @@ -298,6 +298,11 @@ func (e *exportCmd) initDaemonAppImage(analyzedMD files.Analyzed, logger log.Log return nil, "", cmd.FailErr(err, " image") } + // Identifier() returns an empty ID without an error when the run image is missing from the daemon + if !appImage.Found() { + return nil, "", cmd.FailErr(fmt.Errorf("run image %q not found in daemon", e.RunImageRef), "access run image") + } + runImageID, err := appImage.Identifier() if err != nil { return nil, "", cmd.FailErr(err, "get run image ID") diff --git a/cmd/lifecycle/exporter_test.go b/cmd/lifecycle/exporter_test.go new file mode 100644 index 000000000..202248654 --- /dev/null +++ b/cmd/lifecycle/exporter_test.go @@ -0,0 +1,56 @@ +package main + +import ( + "context" + "testing" + + "github.com/moby/moby/client" + "github.com/sclevine/spec" + "github.com/sclevine/spec/report" + + "github.com/buildpacks/lifecycle/platform" + "github.com/buildpacks/lifecycle/platform/files" + h "github.com/buildpacks/lifecycle/testhelpers" +) + +func TestExporter(t *testing.T) { + spec.Run(t, "exporter", testExporter, spec.Report(report.Terminal{})) +} + +// notFoundError is recognized as "not found" by the daemon image lookup. +type notFoundError struct{} + +func (notFoundError) Error() string { return "no such image" } +func (notFoundError) NotFound() {} + +// emptyDaemon is a docker client whose daemon has no images. +type emptyDaemon struct { + client.APIClient +} + +func (emptyDaemon) ServerVersion(context.Context, client.ServerVersionOptions) (client.ServerVersionResult, error) { + return client.ServerVersionResult{APIVersion: "1.48", Os: "linux", Arch: "amd64"}, nil +} + +func (emptyDaemon) ImageInspect(context.Context, string, ...client.ImageInspectOption) (client.ImageInspectResult, error) { + return client.ImageInspectResult{}, notFoundError{} +} + +func testExporter(t *testing.T, when spec.G, it spec.S) { + when("#initDaemonAppImage", func() { + when("the run image is not in the daemon", func() { + it("fails with an error naming the run image", func() { + e := &exportCmd{ + Platform: platform.NewPlatformFor("0.11"), + docker: emptyDaemon{}, + } + e.RunImageRef = "some-run-image" + e.OutputImageRef = "some-app-image" + + _, _, err := e.initDaemonAppImage(files.Analyzed{}, nil) + + h.AssertError(t, err, `run image "some-run-image" not found in daemon`) + }) + }) + }) +}