Skip to content

Commit

Permalink
Remove separate packages for platform interface implementation (#771)
Browse files Browse the repository at this point in the history
* Remove separate packages for platform interface implementation

Signed-off-by: Natalie Arellano <narellano@vmware.com>

* Update builder_test.go

Signed-off-by: Natalie Arellano <narellano@vmware.com>

* Update builder_test.go

Signed-off-by: Natalie Arellano <narellano@vmware.com>

* Add tests

Signed-off-by: Natalie Arellano <narellano@vmware.com>
  • Loading branch information
natalieparellano authored Dec 7, 2021
1 parent 071939d commit 42be2d1
Show file tree
Hide file tree
Showing 30 changed files with 261 additions and 413 deletions.
6 changes: 3 additions & 3 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type Platform interface {
API() string
API() *api.Version
}

type Analyzer struct {
Expand Down Expand Up @@ -49,7 +49,7 @@ func (a *Analyzer) Analyze() (platform.AnalyzedMetadata, error) {
appMeta = platform.LayersMetadata{}
}

if api.MustParse(a.Platform.API()).AtLeast("0.8") {
if a.Platform.API().AtLeast("0.8") {
if appMeta.BOM != nil && appMeta.BOM.SHA != "" {
if err := a.SBOMRestorer.RestoreFromPrevious(a.PreviousImage, appMeta.BOM.SHA); err != nil {
return platform.AnalyzedMetadata{}, errors.Wrap(err, "retrieving launch sBOM layer")
Expand Down Expand Up @@ -87,7 +87,7 @@ func (a *Analyzer) Analyze() (platform.AnalyzedMetadata, error) {
}

func (a *Analyzer) restoresLayerMetadata() bool {
return api.MustParse(a.Platform.API()).LessThan("0.7")
return a.Platform.API().LessThan("0.7")
}

func (a *Analyzer) getImageIdentifier(image imgutil.Image) (*platform.ImageIdentifier, error) {
Expand Down
4 changes: 2 additions & 2 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func testAnalyzerBuilder(platformAPI string) func(t *testing.T, when spec.G, it

sbomRestorer = ltestmock.NewMockSBOMRestorer(mockCtrl)

p, err := platform.NewPlatform(platformAPI)
p := platform.NewPlatform(platformAPI)
h.AssertNil(t, err)
analyzer = &lifecycle.Analyzer{
PreviousImage: image,
Expand Down Expand Up @@ -112,7 +112,7 @@ func testAnalyzerBuilder(platformAPI string) func(t *testing.T, when spec.G, it
)

expectRestoresLayerMetadataIfSupported := func() {
if api.MustParse(analyzer.Platform.API()).LessThan("0.7") {
if analyzer.Platform.API().LessThan("0.7") {
useShaFiles := true
layerSHAStore := layer.NewSHAStore(useShaFiles)
metadataRestorer.EXPECT().Restore(analyzer.Buildpacks, expectedAppMetadata, expectedCacheMetadata, layerSHAStore)
Expand Down
7 changes: 3 additions & 4 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type Builder struct {
LayersDir string
PlatformDir string
Platform Platform
PlatformAPI *api.Version // TODO: derive from platform
Group buildpack.Group
Plan platform.BuildPlan
Out, Err io.Writer
Expand Down Expand Up @@ -88,7 +87,7 @@ func (b *Builder) Build() (*platform.BuildMetadata, error) {
}

b.Logger.Debug("Updating buildpack processes")
updateDefaultProcesses(br.Processes, api.MustParse(bp.API), b.PlatformAPI)
updateDefaultProcesses(br.Processes, api.MustParse(bp.API), b.Platform.API())

bom = append(bom, br.BOM...)
bomFiles = append(bomFiles, br.BOMFiles...)
Expand All @@ -106,14 +105,14 @@ func (b *Builder) Build() (*platform.BuildMetadata, error) {
b.Logger.Debugf("Finished running build for buildpack %s", bp)
}

if b.PlatformAPI.LessThan("0.4") {
if b.Platform.API().LessThan("0.4") {
config.Logger.Debug("Updating BOM entries")
for i := range bom {
bom[i].ConvertMetadataToVersion()
}
}

if b.PlatformAPI.AtLeast("0.8") {
if b.Platform.API().AtLeast("0.8") {
b.Logger.Debug("Copying sBOM files")
err = b.copyBOMFiles(config.LayersDir, bomFiles)
if err != nil {
Expand Down
11 changes: 3 additions & 8 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestBuilder(t *testing.T) {
//go:generate mockgen -package testmock -destination testmock/env.go github.com/buildpacks/lifecycle BuildEnv
//go:generate mockgen -package testmock -destination testmock/buildpack_store.go github.com/buildpacks/lifecycle BuildpackStore
//go:generate mockgen -package testmock -destination testmock/buildpack.go github.com/buildpacks/lifecycle Buildpack
//go:generate mockgen -package testmock -destination testmock/platform.go github.com/buildpacks/lifecycle Platform

func testBuilder(t *testing.T, when spec.G, it spec.S) {
var (
Expand All @@ -47,7 +46,6 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
platformDir string
appDir string
layersDir string
platformInt *testmock.MockPlatform
config buildpack.BuildConfig
logHandler = memory.New()
)
Expand All @@ -67,14 +65,11 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {
appDir = filepath.Join(layersDir, "app")
h.Mkdir(t, layersDir, appDir, filepath.Join(platformDir, "env"))

platformInt = testmock.NewMockPlatform(mockCtrl)

builder = &lifecycle.Builder{
AppDir: appDir,
LayersDir: layersDir,
PlatformDir: platformDir,
Platform: platformInt,
PlatformAPI: api.Platform.Latest(),
Platform: platform.NewPlatform(api.Platform.Latest().String()),
Group: buildpack.Group{
Group: []buildpack.GroupBuildpack{
{ID: "A", Version: "v1", API: api.Buildpack.Latest().String(), Homepage: "Buildpack A Homepage"},
Expand Down Expand Up @@ -831,7 +826,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {

when("platform api < 0.4", func() {
it.Before(func() {
builder.PlatformAPI = api.MustParse("0.3")
builder.Platform = platform.NewPlatform("0.3")
})

when("build metadata", func() {
Expand Down Expand Up @@ -878,7 +873,7 @@ func testBuilder(t *testing.T, when spec.G, it spec.S) {

when("platform api < 0.6", func() {
it.Before(func() {
builder.PlatformAPI = api.MustParse("0.5")
builder.Platform = platform.NewPlatform("0.5")
})

when("there is a web process", func() {
Expand Down
12 changes: 4 additions & 8 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/buildpacks/lifecycle/env"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/common"
)

func main() {
Expand All @@ -28,10 +27,7 @@ func runLaunch() error {
cmd.Exit(err)
}

platform, err := platform.NewPlatform(platformAPI)
if err != nil {
cmd.Exit(err)
}
p := platform.NewPlatform(platformAPI)

var md launch.Metadata
if _, err := toml.DecodeFile(launch.GetMetadataFilePath(cmd.EnvOrDefault(cmd.EnvLayersDir, cmd.DefaultLayersDir)), &md); err != nil {
Expand All @@ -41,13 +37,13 @@ func runLaunch() error {
return err
}

defaultProcessType := defaultProcessType(api.MustParse(platform.API()), md)
defaultProcessType := defaultProcessType(p.API(), md)

launcher := &launch.Launcher{
DefaultProcessType: defaultProcessType,
LayersDir: cmd.EnvOrDefault(cmd.EnvLayersDir, cmd.DefaultLayersDir),
AppDir: cmd.EnvOrDefault(cmd.EnvAppDir, cmd.DefaultAppDir),
PlatformAPI: api.MustParse(platform.API()),
PlatformAPI: p.API(),
Processes: md.Processes,
Buildpacks: md.Buildpacks,
Env: env.NewLaunchEnv(os.Environ(), launch.ProcessDir, launch.LifecycleDir),
Expand All @@ -58,7 +54,7 @@ func runLaunch() error {
}

if err := launcher.Launch(os.Args[0], os.Args[1:]); err != nil {
return cmd.FailErrCode(err, platform.CodeFor(common.LaunchError), "launch")
return cmd.FailErrCode(err, p.CodeFor(platform.LaunchError), "launch")
}
return nil
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/lifecycle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import (
"github.com/pkg/errors"

"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/auth"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/image"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/internal/layer"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/common"
"github.com/buildpacks/lifecycle/priv"
)

Expand Down Expand Up @@ -115,11 +113,11 @@ func (a *analyzeCmd) Args(nargs int, args []string) error {
}

if a.analyzedPath == cmd.PlaceholderAnalyzedPath {
a.analyzedPath = cmd.DefaultAnalyzedPath(a.platform.API(), a.layersDir)
a.analyzedPath = cmd.DefaultAnalyzedPath(a.platform.API().String(), a.layersDir)
}

if a.platform06.groupPath == cmd.PlaceholderGroupPath {
a.platform06.groupPath = cmd.DefaultGroupPath(a.platform.API(), a.layersDir)
a.platform06.groupPath = cmd.DefaultGroupPath(a.platform.API().String(), a.layersDir)
}

if a.supportsRunImage() {
Expand Down Expand Up @@ -238,7 +236,7 @@ func (aa analyzeArgs) analyze() (platform.AnalyzedMetadata, error) {
SBOMRestorer: layer.NewSBOMRestorer(aa.layersDir, cmd.DefaultLogger),
}).Analyze()
if err != nil {
return platform.AnalyzedMetadata{}, cmd.FailErrCode(err, aa.platform.CodeFor(common.AnalyzeError), "analyzer")
return platform.AnalyzedMetadata{}, cmd.FailErrCode(err, aa.platform.CodeFor(platform.AnalyzeError), "analyzer")
}

return analyzedMD, nil
Expand All @@ -265,7 +263,7 @@ func (aa analyzeArgs) localOrRemote(fromImage string) (imgutil.Image, error) {
}

func (a *analyzeCmd) platformAPIVersionGreaterThan06() bool {
return api.MustParse(a.platform.API()).AtLeast("0.7")
return a.platform.API().AtLeast("0.7")
}

func (a *analyzeCmd) restoresLayerMetadata() bool {
Expand Down
13 changes: 5 additions & 8 deletions cmd/lifecycle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/common"
"github.com/buildpacks/lifecycle/priv"
)

Expand Down Expand Up @@ -48,11 +46,11 @@ func (b *buildCmd) Args(nargs int, args []string) error {
}

if b.groupPath == cmd.PlaceholderGroupPath {
b.groupPath = cmd.DefaultGroupPath(b.platform.API(), b.layersDir)
b.groupPath = cmd.DefaultGroupPath(b.platform.API().String(), b.layersDir)
}

if b.planPath == cmd.PlaceholderPlanPath {
b.planPath = cmd.DefaultPlanPath(b.platform.API(), b.layersDir)
b.planPath = cmd.DefaultPlanPath(b.platform.API().String(), b.layersDir)
}

return nil
Expand Down Expand Up @@ -80,15 +78,14 @@ func (b *buildCmd) Exec() error {
func (ba buildArgs) build(group buildpack.Group, plan platform.BuildPlan) error {
buildpackStore, err := buildpack.NewBuildpackStore(ba.buildpacksDir)
if err != nil {
return cmd.FailErrCode(err, ba.platform.CodeFor(common.BuildError), "build")
return cmd.FailErrCode(err, ba.platform.CodeFor(platform.BuildError), "build")
}

builder := &lifecycle.Builder{
AppDir: ba.appDir,
LayersDir: ba.layersDir,
PlatformDir: ba.platformDir,
Platform: ba.platform,
PlatformAPI: api.MustParse(ba.platform.API()),
Group: group,
Plan: plan,
Out: cmd.Stdout,
Expand All @@ -101,10 +98,10 @@ func (ba buildArgs) build(group buildpack.Group, plan platform.BuildPlan) error
if err != nil {
if err, ok := err.(*buildpack.Error); ok {
if err.Type == buildpack.ErrTypeBuildpack {
return cmd.FailErrCode(err.Cause(), ba.platform.CodeFor(common.FailedBuildWithErrors), "build")
return cmd.FailErrCode(err.Cause(), ba.platform.CodeFor(platform.FailedBuildWithErrors), "build")
}
}
return cmd.FailErrCode(err, ba.platform.CodeFor(common.BuildError), "build")
return cmd.FailErrCode(err, ba.platform.CodeFor(platform.BuildError), "build")
}

if err := encoding.WriteTOML(launch.GetMetadataFilePath(ba.layersDir), md); err != nil {
Expand Down
11 changes: 5 additions & 6 deletions cmd/lifecycle/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/pkg/errors"

"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/auth"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cmd"
Expand Down Expand Up @@ -95,15 +94,15 @@ func (c *createCmd) Args(nargs int, args []string) error {
}

if c.projectMetadataPath == cmd.PlaceholderProjectMetadataPath {
c.projectMetadataPath = cmd.DefaultProjectMetadataPath(c.platform.API(), c.layersDir)
c.projectMetadataPath = cmd.DefaultProjectMetadataPath(c.platform.API().String(), c.layersDir)
}

if c.reportPath == cmd.PlaceholderReportPath {
c.reportPath = cmd.DefaultReportPath(c.platform.API(), c.layersDir)
c.reportPath = cmd.DefaultReportPath(c.platform.API().String(), c.layersDir)
}

if c.orderPath == cmd.PlaceholderOrderPath {
c.orderPath = cmd.DefaultOrderPath(c.platform.API(), c.layersDir)
c.orderPath = cmd.DefaultOrderPath(c.platform.API().String(), c.layersDir)
}

var err error
Expand Down Expand Up @@ -166,7 +165,7 @@ func (c *createCmd) Exec() error {
group buildpack.Group
plan platform.BuildPlan
)
if api.MustParse(c.platform.API()).AtLeast("0.7") {
if c.platform.API().AtLeast("0.7") {
cmd.DefaultLogger.Phase("ANALYZING")
analyzedMD, err = analyzeArgs{
additionalTags: c.additionalTags,
Expand Down Expand Up @@ -288,7 +287,7 @@ func (c *createCmd) registryImages() []string {
}

func (c *createCmd) platformAPIVersionGreaterThan06() bool {
return api.MustParse(c.platform.API()).AtLeast("0.7")
return c.platform.API().AtLeast("0.7")
}

func (c *createCmd) ReadableRegistryImages() []string {
Expand Down
15 changes: 7 additions & 8 deletions cmd/lifecycle/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/platform"
"github.com/buildpacks/lifecycle/platform/common"
"github.com/buildpacks/lifecycle/priv"
)

Expand Down Expand Up @@ -49,15 +48,15 @@ func (d *detectCmd) Args(nargs int, args []string) error {
}

if d.groupPath == cmd.PlaceholderGroupPath {
d.groupPath = cmd.DefaultGroupPath(d.platform.API(), d.layersDir)
d.groupPath = cmd.DefaultGroupPath(d.platform.API().String(), d.layersDir)
}

if d.planPath == cmd.PlaceholderPlanPath {
d.planPath = cmd.DefaultPlanPath(d.platform.API(), d.layersDir)
d.planPath = cmd.DefaultPlanPath(d.platform.API().String(), d.layersDir)
}

if d.orderPath == cmd.PlaceholderOrderPath {
d.orderPath = cmd.DefaultOrderPath(d.platform.API(), d.layersDir)
d.orderPath = cmd.DefaultOrderPath(d.platform.API().String(), d.layersDir)
}

return nil
Expand Down Expand Up @@ -108,15 +107,15 @@ func (da detectArgs) detect() (buildpack.Group, platform.BuildPlan, error) {
case buildpack.ErrTypeFailedDetection:
cmd.DefaultLogger.Error("No buildpack groups passed detection.")
cmd.DefaultLogger.Error("Please check that you are running against the correct path.")
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(common.FailedDetect), "detect")
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(platform.FailedDetect), "detect")
case buildpack.ErrTypeBuildpack:
cmd.DefaultLogger.Error("No buildpack groups passed detection.")
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(common.FailedDetectWithErrors), "detect")
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(platform.FailedDetectWithErrors), "detect")
default:
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(common.DetectError), "detect")
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(platform.DetectError), "detect")
}
default:
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(common.DetectError), "detect")
return buildpack.Group{}, platform.BuildPlan{}, cmd.FailErrCode(err, da.platform.CodeFor(platform.DetectError), "detect")
}
}

Expand Down
Loading

0 comments on commit 42be2d1

Please sign in to comment.