From b54bc9d2cac8cdd96a716398647b21f8b472d5fe Mon Sep 17 00:00:00 2001 From: Natalie Arellano Date: Thu, 24 Jun 2021 13:40:32 -0400 Subject: [PATCH] Revert "Merge pull request #617 from buildpacks/jab/validate-stack" This reverts commit 7c5f36b793af4d1118405465a1174b3ce7128df7, reversing changes made to 162090f6255b289c36fcfac8a884a8e8c945c82e. --- acceptance/analyzer_test.go | 199 +----------------- .../analyzer/analyze-image/Dockerfile | 41 ---- .../analyzer/analyze-image/Dockerfile.windows | 33 --- .../testdata/analyzer/run-image/Dockerfile | 4 - .../analyzer/run-image/Dockerfile.windows | 5 - cmd/flags.go | 1 - cmd/lifecycle/analyzer.go | 55 ----- cmd/lifecycle/creator.go | 15 +- platform/files.go | 8 +- stack_validation.go | 51 ----- stack_validation_test.go | 82 -------- utils.go | 26 --- utils_test.go | 35 --- 13 files changed, 8 insertions(+), 547 deletions(-) delete mode 100644 acceptance/testdata/analyzer/run-image/Dockerfile delete mode 100644 acceptance/testdata/analyzer/run-image/Dockerfile.windows delete mode 100644 stack_validation.go delete mode 100644 stack_validation_test.go diff --git a/acceptance/analyzer_test.go b/acceptance/analyzer_test.go index 5ac073f16..be6880286 100644 --- a/acceptance/analyzer_test.go +++ b/acceptance/analyzer_test.go @@ -73,36 +73,6 @@ func TestAnalyzer(t *testing.T) { h.AssertNil(t, os.RemoveAll(filepath.Join(targetDockerConfig, "config.json"))) h.RecursiveCopy(t, authRegistry.DockerDirectory, targetDockerConfig) - // build run-images into test registry - runImageContext := filepath.Join("testdata", "analyzer", "run-image") - buildAuthRegistryImage( - t, - "company/stack:bionic", - runImageContext, - "-f", filepath.Join(runImageContext, dockerfileName), - "--build-arg", "stackid=io.buildpacks.stacks.bionic", - ) - buildAuthRegistryImage( - t, - "company/stack:centos", - runImageContext, - "-f", filepath.Join(runImageContext, dockerfileName), - "--build-arg", "stackid=io.company.centos", - ) - - // build run-image into daemon - h.DockerBuild( - t, - "localcompany/stack:bionic", - runImageContext, - h.WithArgs( - "-f", filepath.Join(runImageContext, dockerfileName), - "--build-arg", "stackid=io.buildpacks.stacks.bionic", - ), - ) - - defer h.DockerImageRemove(t, "localcompany/stack:bionic") - // Setup test container h.MakeAndCopyLifecycle(t, daemonOS, analyzerBinaryDir) @@ -111,7 +81,6 @@ func TestAnalyzer(t *testing.T) { analyzeDockerContext, h.WithFlags( "-f", filepath.Join(analyzeDockerContext, dockerfileName), - "--build-arg", "registry="+noAuthRegistry.Host+":"+noAuthRegistry.Port, ), ) defer h.DockerImageRemove(t, analyzeImage) @@ -284,10 +253,7 @@ func testAnalyzerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe copyDir, ctrPath("/some-dir/some-analyzed.toml"), analyzeImage, - h.WithFlags( - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - ), + h.WithFlags("--env", "CNB_PLATFORM_API="+platformAPI), h.WithArgs(execArgs...), ) @@ -311,7 +277,6 @@ func testAnalyzerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe h.WithFlags(append( dockerSocketMount, "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/local-bionic-stack.toml", // /cnb/local-bionic-stack.toml has `io.buildpacks.stacks.bionic` and points to run image `localcompany/stack:bionic` with same stack id )...), h.WithArgs(execArgs...), ) @@ -351,7 +316,6 @@ func testAnalyzerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe h.WithFlags(append( dockerSocketMount, "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/local-bionic-stack.toml", // /cnb/local-bionic-stack.toml has `io.buildpacks.stacks.bionic` and points to run image `localcompany/stack:bionic` with same stack id )...), h.WithArgs( ctrPath(analyzerPath), @@ -660,7 +624,6 @@ func testAnalyzerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe ctrPath("/layers/analyzed.toml"), analyzeImage, h.WithFlags( - "--network", registryNetwork, "--env", "CNB_PLATFORM_API="+platformAPI, ), h.WithArgs(ctrPath(analyzerPath), "some-image"), @@ -1102,166 +1065,6 @@ func testAnalyzerFunc(platformAPI string) func(t *testing.T, when spec.G, it spe assertAnalyzedMetadata(t, filepath.Join(copyDir, "some-other-layers", "analyzed.toml")) // analyzed.toml is written at the provided -layers directory: /some-other-layers }) }) - - when("validating stack", func() { - it.Before(func() { - h.SkipIf(t, api.MustParse(platformAPI).Compare(api.MustParse("0.7")) < 0, "Platform API < 0.7 does not validate stack") - }) - - when("stack metadata is present", func() { - when("stacks match", func() { - it("passes validation", func() { - execArgs := []string{ctrPath(analyzerPath), "some-image"} - h.DockerRun(t, - analyzeImage, // /cnb/stack.toml has `io.buildpacks.stacks.bionic` and points to run image `company/stack:bionic` with same stack id - h.WithFlags( - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - ), - h.WithArgs(execArgs...), - ) - }) - }) - - when("CNB_RUN_IMAGE is present", func() { - it("uses CNB_RUN_IMAGE for validation", func() { - execArgs := []string{ctrPath(analyzerPath), "some-image"} - - h.DockerRun(t, - analyzeImage, - h.WithFlags( - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/mismatch-stack.toml", // /cnb/mismatch-stack.toml points to run image `company/stack:centos` - "--env", "CNB_RUN_IMAGE="+noAuthRegistry.RepoName("company/stack:bionic"), - ), - h.WithArgs(execArgs...), - ) - }) - }) - - when("stack metadata file is invalid", func() { - it("fails validation", func() { - cmd := exec.Command( - "docker", "run", "--rm", - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/bad-stack.toml", - analyzeImage, - ctrPath(analyzerPath), - "some-image", - ) // #nosec G204 - output, err := cmd.CombinedOutput() - - h.AssertNotNil(t, err) - expected := "get stack metadata" - h.AssertStringContains(t, string(output), expected) - }) - }) - - when("run image inaccessible", func() { - it("fails validation", func() { - cmd := exec.Command( - "docker", "run", "--rm", - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_RUN_IMAGE=fake.example.com/company/example:20", - analyzeImage, - ctrPath(analyzerPath), - "some-image", - ) // #nosec G204 - output, err := cmd.CombinedOutput() - - h.AssertNotNil(t, err) - expected := "failed to resolve run image" - h.AssertStringContains(t, string(output), expected) - }) - }) - - when("run image has mirrors", func() { - it("uses expected mirror for run-image", func() { - execArgs := []string{ctrPath(analyzerPath), noAuthRegistry.RepoName("apprepo/myapp")} // image located on same registry as mirror - - h.DockerRunAndCopy(t, - containerName, - copyDir, - ctrPath("/layers/analyzed.toml"), - analyzeImage, - h.WithFlags( - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/run-mirror-stack.toml", // /cnb/run-mirror-stack.toml points to run image on gcr.io and mirror on test registry - ), - h.WithArgs(execArgs...), - ) - }) - }) - - when("daemon case", func() { - when("stacks match", func() { - it("passes validation", func() { - execArgs := []string{ctrPath(analyzerPath), "-daemon", "some-image"} - - h.DockerRunAndCopy(t, - containerName, - copyDir, - ctrPath("/layers/analyzed.toml"), - analyzeImage, - h.WithFlags(append( - dockerSocketMount, - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/local-bionic-stack.toml", // /cnb/local-bionic-stack.toml has `io.buildpacks.stacks.bionic` and points to run image `localcompany/stack:bionic` with same stack id - )...), - h.WithArgs(execArgs...), - ) - }) - }) - }) - }) - - when("stack metadata is not present", func() { - when("CNB_RUN_IMAGE and CNB_STACK_ID are set", func() { - it("passes validation", func() { - execArgs := []string{ctrPath(analyzerPath), "some-image"} - - h.DockerRunAndCopy(t, - containerName, - copyDir, - ctrPath("/layers/analyzed.toml"), - analyzeImage, - h.WithFlags( - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/file-does-not-exist.toml", - "--env", "CNB_RUN_IMAGE="+noAuthRegistry.RepoName("company/stack:bionic"), - "--env", "CNB_STACK_ID=io.buildpacks.stacks.bionic", - ), - h.WithArgs(execArgs...), - ) - }) - }) - - when("run image and stack id are not provided as arguments or in the environment", func() { - it("fails validation", func() { - cmd := exec.Command( - "docker", "run", "--rm", - "--network", registryNetwork, - "--env", "CNB_PLATFORM_API="+platformAPI, - "--env", "CNB_STACK_PATH=/cnb/file-does-not-exist.toml", - analyzeImage, - ctrPath(analyzerPath), - "some-image", - ) // #nosec G204 - output, err := cmd.CombinedOutput() - - h.AssertNotNil(t, err) - expected := "a run image must be specified when there is no stack metadata available" - h.AssertStringContains(t, string(output), expected) - }) - }) - }) - }) } } diff --git a/acceptance/testdata/analyzer/analyze-image/Dockerfile b/acceptance/testdata/analyzer/analyze-image/Dockerfile index 2f5d3b7ed..0ebe49555 100644 --- a/acceptance/testdata/analyzer/analyze-image/Dockerfile +++ b/acceptance/testdata/analyzer/analyze-image/Dockerfile @@ -19,44 +19,3 @@ RUN chown -R $CNB_USER_ID:$CNB_GROUP_ID /layers # ensure docker config directory is root owned and NOT world readable RUN chown -R root /docker-config; chmod -R 700 /docker-config - -ARG registry - -# write some stack.toml files to use in tests -RUN echo "\ -[run-image]\n\ - image = \"${registry}/company/stack:bionic\"\n\ - mirrors = []\n\ -[build-image]\n\ - stack-id = \"io.buildpacks.stacks.bionic\"\n\ - mixins = []\n\ -" > /cnb/stack.toml - -RUN echo "\ -[run-image]\n\ - image = \"${registry}/company/stack:centos\"\n\ - mirrors = []\n\ -[build-image]\n\ - stack-id = \"io.buildpacks.stacks.bionic\"\n\ - mixins = []\n\ -" > /cnb/mismatch-stack.toml - -RUN echo "\ -[run-image]\n\ - image = \"gcr.io/paketobuildpacks/invalidimg:20\"\n\ - mirrors = [\"${registry}/company/stack:bionic\"]\n\ -[build-image]\n\ - stack-id = \"io.buildpacks.stacks.bionic\"\n\ - mixins = []\n\ -" > /cnb/run-mirror-stack.toml - -RUN echo "\ -[run-image]\n\ - image = \"localcompany/stack:bionic\"\n\ - mirrors = []\n\ -[build-image]\n\ - stack-id = \"io.buildpacks.stacks.bionic\"\n\ - mixins = []\n\ -" > /cnb/local-bionic-stack.toml - -RUN echo "[run-images" > /cnb/bad-stack.toml diff --git a/acceptance/testdata/analyzer/analyze-image/Dockerfile.windows b/acceptance/testdata/analyzer/analyze-image/Dockerfile.windows index 9b749b518..44f9d8339 100644 --- a/acceptance/testdata/analyzer/analyze-image/Dockerfile.windows +++ b/acceptance/testdata/analyzer/analyze-image/Dockerfile.windows @@ -10,36 +10,3 @@ ENV CNB_USER_ID=1 ENV CNB_GROUP_ID=1 ENV CNB_PLATFORM_API=${cnb_platform_api} - -ARG registry - -# write some stack.toml files to use in tests -RUN echo [run-image] > /cnb/stack.toml &\ - echo image = "%registry%/company/stack:bionic" >> /cnb/stack.toml &\ - echo mirrors = [] >> /cnb/stack.toml &\ - echo [build-image] >> /cnb/stack.toml &\ - echo stack-id = "io.buildpacks.stacks.bionic" >> /cnb/stack.toml &\ - echo mixins = [] >> /cnb/stack.toml - -RUN echo [run-image] > /cnb/mismatch-stack.toml &\ - echo image = "%registry%/company/stack:centos" >> /cnb/mismatch-stack.toml &\ - echo mirrors = [] >> /cnb/mismatch-stack.toml &\ - echo [build-image] >> /cnb/mismatch-stack.toml &\ - echo stack-id = "io.buildpacks.stacks.bionic" >> /cnb/mismatch-stack.toml &\ - echo mixins = [] >> /cnb/mismatch-stack.toml - -RUN echo [run-image] > /cnb/run-mirror-stack.toml &\ - echo image = "gcr.io/paketobuildpacks/invalidimg:20" >> /cnb/run-mirror-stack.toml &\ - echo mirrors = ["%registry%/company/stack:bionic"] >> /cnb/run-mirror-stack.toml &\ - echo [build-image] >> /cnb/run-mirror-stack.toml &\ - echo stack-id = "io.buildpacks.stacks.bionic" >> /cnb/run-mirror-stack.toml &\ - echo mixins = [] >> /cnb/run-mirror-stack.toml - -RUN echo [run-image] > /cnb/local-bionic-stack.toml &\ - echo image = "localcompany/stack:bionic" >> /cnb/local-bionic-stack.toml &\ - echo mirrors = [] >> /cnb/local-bionic-stack.toml &\ - echo [build-image] >> /cnb/local-bionic-stack.toml &\ - echo stack-id = "io.buildpacks.stacks.bionic" >> /cnb/local-bionic-stack.toml &\ - echo mixins = [] >> /cnb/local-bionic-stack.toml - -RUN echo [run-images > /cnb/bad-stack.toml diff --git a/acceptance/testdata/analyzer/run-image/Dockerfile b/acceptance/testdata/analyzer/run-image/Dockerfile deleted file mode 100644 index 074bd54ed..000000000 --- a/acceptance/testdata/analyzer/run-image/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM scratch - -ARG stackid -LABEL io.buildpacks.stack.id=${stackid} diff --git a/acceptance/testdata/analyzer/run-image/Dockerfile.windows b/acceptance/testdata/analyzer/run-image/Dockerfile.windows deleted file mode 100644 index a12552e59..000000000 --- a/acceptance/testdata/analyzer/run-image/Dockerfile.windows +++ /dev/null @@ -1,5 +0,0 @@ -FROM mcr.microsoft.com/windows/nanoserver:1809 -USER ContainerAdministrator - -ARG stackid -LABEL io.buildpacks.stack.id=${stackid} diff --git a/cmd/flags.go b/cmd/flags.go index 7af0925b2..3b55b5e94 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -62,7 +62,6 @@ const ( EnvSkipLayers = "CNB_ANALYZE_SKIP_LAYERS" // defaults to false EnvSkipRestore = "CNB_SKIP_RESTORE" // defaults to false EnvStackPath = "CNB_STACK_PATH" - EnvStackID = "CNB_STACK_ID" EnvUID = "CNB_USER_ID" EnvUseDaemon = "CNB_USE_DAEMON" // defaults to false ) diff --git a/cmd/lifecycle/analyzer.go b/cmd/lifecycle/analyzer.go index d965646d9..52934dda4 100644 --- a/cmd/lifecycle/analyzer.go +++ b/cmd/lifecycle/analyzer.go @@ -2,9 +2,7 @@ package main import ( "fmt" - "os" - "github.com/BurntSushi/toml" "github.com/buildpacks/imgutil" "github.com/buildpacks/imgutil/local" "github.com/buildpacks/imgutil/remote" @@ -173,29 +171,7 @@ func (a *analyzeCmd) Exec() error { return nil } -func (aa analyzeArgs) validateStack() error { - if !aa.supportsStackValidation() { - return nil - } - - var stackMD platform.StackMetadata - if _, err := toml.DecodeFile(aa.stackPath, &stackMD); err != nil && !os.IsNotExist(err) { - return cmd.FailErr(err, "get stack metadata") - } - - runImage, err := aa.getRunImage(stackMD) - if err != nil { - return cmd.FailErr(err, "resolve run image") - } - - return lifecycle.ValidateStack(stackMD, runImage) -} - func (aa analyzeArgs) analyze() (platform.AnalyzedMetadata, error) { - if err := aa.validateStack(); err != nil { - return platform.AnalyzedMetadata{}, cmd.FailErr(err, "validate stack") - } - var ( img imgutil.Image err error @@ -231,33 +207,6 @@ func (aa analyzeArgs) analyze() (platform.AnalyzedMetadata, error) { return analyzedMD, nil } -func (aa *analyzeArgs) getRunImage(stackMD platform.StackMetadata) (imgutil.Image, error) { - if aa.runImageRef == "" { - runImageRef, err := lifecycle.ResolveRunImage(stackMD, aa.imageName) - if err != nil { - return nil, err - } - aa.runImageRef = runImageRef - } - - var runImage imgutil.Image - var err error - if aa.useDaemon { - runImage, err = local.NewImage( - aa.runImageRef, - aa.docker, - local.FromBaseImage(aa.runImageRef), - ) - } else { - runImage, err = remote.NewImage( - aa.runImageRef, - aa.keychain, - remote.FromBaseImage(aa.runImageRef), - ) - } - return runImage, err -} - func (a *analyzeCmd) registryImages() []string { var registryImages []string if a.platform06.cacheImageTag != "" { @@ -273,10 +222,6 @@ func (a *analyzeCmd) restoresLayerMetadata() bool { return !a.platformAPIVersionGreaterThan06() } -func (aa *analyzeArgs) supportsStackValidation() bool { - return api.MustParse(aa.platform.API()).Compare(api.MustParse("0.7")) >= 0 -} - func (a *analyzeCmd) platformAPIVersionGreaterThan06() bool { return api.MustParse(a.platform.API()).Compare(api.MustParse("0.7")) >= 0 } diff --git a/cmd/lifecycle/creator.go b/cmd/lifecycle/creator.go index c227d9f53..eb7da48e9 100644 --- a/cmd/lifecycle/creator.go +++ b/cmd/lifecycle/creator.go @@ -154,15 +154,12 @@ func (c *createCmd) Exec() error { if api.MustParse(c.platform.API()).Compare(api.MustParse("0.7")) >= 0 { cmd.DefaultLogger.Phase("ANALYZING") analyzedMD, err = analyzeArgs{ - imageName: c.imageName, - keychain: c.keychain, - layersDir: c.layersDir, - platform: c.platform, - previousImage: c.previousImage, - runImageRef: c.runImageRef, - stackPath: c.stackPath, - useDaemon: c.useDaemon, - docker: c.docker, + imageName: c.previousImage, + keychain: c.keychain, + layersDir: c.layersDir, + platform: c.platform, + useDaemon: c.useDaemon, + docker: c.docker, }.analyze() if err != nil { return err diff --git a/platform/files.go b/platform/files.go index f628933e9..75b3bcdf7 100644 --- a/platform/files.go +++ b/platform/files.go @@ -205,8 +205,7 @@ type ImageReport struct { // stack.toml type StackMetadata struct { - RunImage StackRunImageMetadata `json:"runImage" toml:"run-image"` - BuildImage StackBuildImageMetadata `json:"buildImage" toml:"build-image"` + RunImage StackRunImageMetadata `json:"runImage" toml:"run-image"` } type StackRunImageMetadata struct { @@ -214,11 +213,6 @@ type StackRunImageMetadata struct { Mirrors []string `toml:"mirrors" json:"mirrors,omitempty"` } -type StackBuildImageMetadata struct { - StackID string `toml:"stack-id" json:"stack-id"` - Mixins []string `toml:"mixins" json:"mixins,omitempty"` -} - func (sm *StackMetadata) BestRunImageMirror(registry string) (string, error) { if sm.RunImage.Image == "" { return "", errors.New("missing run-image metadata") diff --git a/stack_validation.go b/stack_validation.go deleted file mode 100644 index 58fb89fc3..000000000 --- a/stack_validation.go +++ /dev/null @@ -1,51 +0,0 @@ -package lifecycle - -import ( - "fmt" - "os" - - "github.com/buildpacks/imgutil" - "github.com/pkg/errors" - - "github.com/buildpacks/lifecycle/cmd" - "github.com/buildpacks/lifecycle/platform" -) - -func ValidateStack(stackMD platform.StackMetadata, runImage imgutil.Image) error { - buildStackID, err := getBuildStack(stackMD) - if err != nil { - return err - } - - runStackID, err := getRunStack(runImage) - if err != nil { - return err - } - - if buildStackID != runStackID { - return errors.New(fmt.Sprintf("incompatible stack: '%s' is not compatible with '%s'", runStackID, buildStackID)) - } - return nil -} - -func getRunStack(runImage imgutil.Image) (string, error) { - runStackID, err := runImage.Label(platform.StackIDLabel) - if err != nil { - return "", errors.Wrap(err, "get run image label") - } - if runStackID == "" { - return "", errors.New("get run image label: io.buildpacks.stack.id") - } - return runStackID, nil -} - -func getBuildStack(stackMD platform.StackMetadata) (string, error) { - var buildStackID string - if buildStackID = os.Getenv(cmd.EnvStackID); buildStackID != "" { - return buildStackID, nil - } - if buildStackID = stackMD.BuildImage.StackID; buildStackID != "" { - return buildStackID, nil - } - return "", errors.New("CNB_STACK_ID is required when there is no stack metadata available") -} diff --git a/stack_validation_test.go b/stack_validation_test.go deleted file mode 100644 index 77bbdca35..000000000 --- a/stack_validation_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package lifecycle_test - -import ( - "os" - "testing" - - "github.com/buildpacks/imgutil/fakes" - "github.com/sclevine/spec" - "github.com/sclevine/spec/report" - - "github.com/buildpacks/lifecycle" - "github.com/buildpacks/lifecycle/platform" - h "github.com/buildpacks/lifecycle/testhelpers" -) - -func TestStackValidation(t *testing.T) { - spec.Run(t, "StackValidation", testStackValidation, spec.Report(report.Terminal{})) -} - -func testStackValidation(t *testing.T, when spec.G, it spec.S) { - when("ValidateStack", func() { - when("build and run stack ids match", func() { - it("should not err", func() { - md := platform.StackMetadata{BuildImage: platform.StackBuildImageMetadata{StackID: "my-stack"}} - runImage := fakes.NewImage("runimg", "", nil) - h.AssertNil(t, runImage.SetLabel(platform.StackIDLabel, "my-stack")) - err := lifecycle.ValidateStack(md, runImage) - h.AssertNil(t, err) - }) - }) - - when("build and run stack ids do not match", func() { - it("should fail", func() { - md := platform.StackMetadata{BuildImage: platform.StackBuildImageMetadata{StackID: "my-stack"}} - runImage := fakes.NewImage("runimg", "", nil) - h.AssertNil(t, runImage.SetLabel(platform.StackIDLabel, "my-other-stack")) - err := lifecycle.ValidateStack(md, runImage) - h.AssertNotNil(t, err) - h.AssertError(t, err, "incompatible stack: 'my-other-stack' is not compatible with 'my-stack'") - }) - }) - - when("run image is missing io.buildpacks.stack.id label", func() { - it("should fail", func() { - md := platform.StackMetadata{BuildImage: platform.StackBuildImageMetadata{StackID: "my-stack"}} - runImage := fakes.NewImage("runimg", "", nil) - err := lifecycle.ValidateStack(md, runImage) - h.AssertNotNil(t, err) - h.AssertError(t, err, "get run image label: io.buildpacks.stack.id") - }) - }) - - when("CNB_STACK_ID is present", func() { - it.Before(func() { - os.Setenv("CNB_STACK_ID", "my-stack") - }) - - it.After(func() { - h.AssertNil(t, os.Unsetenv("CNB_STACK_ID")) - }) - - it("prefers that value", func() { - md := platform.StackMetadata{BuildImage: platform.StackBuildImageMetadata{StackID: "my-other-stack"}} - runImage := fakes.NewImage("runimg", "", nil) - h.AssertNil(t, runImage.SetLabel(platform.StackIDLabel, "my-stack")) - err := lifecycle.ValidateStack(md, runImage) - h.AssertNil(t, err) - }) - }) - - when("no build stack is present", func() { - it("should fail", func() { - md := platform.StackMetadata{BuildImage: platform.StackBuildImageMetadata{StackID: ""}} - runImage := fakes.NewImage("runimg", "", nil) - h.AssertNil(t, runImage.SetLabel(platform.StackIDLabel, "my-stack")) - err := lifecycle.ValidateStack(md, runImage) - h.AssertNotNil(t, err) - h.AssertError(t, err, "CNB_STACK_ID is required when there is no stack metadata available") - }) - }) - }) -} diff --git a/utils.go b/utils.go index 4e8954340..0f18ffaf2 100644 --- a/utils.go +++ b/utils.go @@ -8,11 +8,9 @@ import ( "github.com/BurntSushi/toml" "github.com/buildpacks/imgutil" - "github.com/google/go-containerregistry/pkg/name" "github.com/pkg/errors" "github.com/buildpacks/lifecycle/buildpack" - "github.com/buildpacks/lifecycle/platform" ) func WriteTOML(path string, data interface{}) error { @@ -66,30 +64,6 @@ func DecodeLabel(image imgutil.Image, label string, v interface{}) error { return nil } -func ResolveRunImage(stackMD platform.StackMetadata, destinationImageRef string) (string, error) { - runImageRef := stackMD.RunImage.Image - - if runImageRef == "" { - return "", errors.New("a run image must be specified when there is no stack metadata available") - } - - if destinationImageRef != "" && len(stackMD.RunImage.Mirrors) > 0 { - ref, err := name.ParseReference(destinationImageRef, name.WeakValidation) - if err != nil { - return "", err - } - - registry := ref.Context().RegistryStr() - - runImageRef, err = stackMD.BestRunImageMirror(registry) - if err != nil { - return "", err - } - } - - return runImageRef, nil -} - func removeStagePrefixes(mixins []string) []string { var result []string for _, m := range mixins { diff --git a/utils_test.go b/utils_test.go index cd1668700..55ac108b6 100644 --- a/utils_test.go +++ b/utils_test.go @@ -12,7 +12,6 @@ import ( "github.com/buildpacks/lifecycle" "github.com/buildpacks/lifecycle/buildpack" - "github.com/buildpacks/lifecycle/platform" h "github.com/buildpacks/lifecycle/testhelpers" ) @@ -146,38 +145,4 @@ func testUtils(t *testing.T, when spec.G, it spec.S) { } }) }) - - when("ResolveRunImage", func() { - when("there are no mirrors", func() { - it("should return run-image", func() { - md := platform.StackMetadata{RunImage: platform.StackRunImageMetadata{Image: "company/run:focal"}} - dstImage := "someregistry/whatever" - res, err := lifecycle.ResolveRunImage(md, dstImage) - h.AssertNil(t, err) - h.AssertEq(t, res, md.RunImage.Image) - }) - }) - - when("there are mirrors", func() { - it("should return a run-image from the mirror matching the destination image", func() { - md := platform.StackMetadata{RunImage: platform.StackRunImageMetadata{ - Image: "company/run:focal", - Mirrors: []string{"some.registry/_/run:focal"}, - }} - dstImage := "some.registry/app/web" - res, err := lifecycle.ResolveRunImage(md, dstImage) - h.AssertNil(t, err) - h.AssertEq(t, res, "some.registry/_/run:focal") - }) - }) - - when("there is no run image defined", func() { - it("should fail", func() { - md := platform.StackMetadata{} - dstImage := "someregistry/whatever" - _, err := lifecycle.ResolveRunImage(md, dstImage) - h.AssertError(t, err, "a run image must be specified when there is no stack metadata available") - }) - }) - }) }