Skip to content

Commit

Permalink
Rename distributions -> distros in the platform spec
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Jul 31, 2023
1 parent a6b30d1 commit b9b2926
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
os = "linux"
arch = "amd64"
arch-variant = "some-variant"
[run-image.target.distribution]
[run-image.target.distro]
name = "ubuntu"
version = "some-cute-version"

14 changes: 7 additions & 7 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,14 @@ func testAnalyzer(platformAPI string) func(t *testing.T, when spec.G, it spec.S)

h.AssertEq(t, md.RunImage.Reference, "s0m3D1g3sT")
})
it("populates Target metadata from the run image", func() {
h.AssertNil(t, image.SetLabel("io.buildpacks.id", "id software"))
it("populates target metadata from the run image", func() {
h.AssertNil(t, image.SetLabel("io.buildpacks.base.id", "id software"))
h.AssertNil(t, image.SetOS("windows"))
h.AssertNil(t, image.SetOSVersion("95"))
h.AssertNil(t, image.SetArchitecture("Pentium"))
h.AssertNil(t, image.SetVariant("MMX"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distribution.name", "moobuntu"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distribution.version", "Helpful Holstein"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distro.name", "moobuntu"))
h.AssertNil(t, image.SetLabel("io.buildpacks.distro.version", "Helpful Holstein"))

md, err := analyzer.Analyze()
h.AssertNil(t, err)
Expand All @@ -685,9 +685,9 @@ func testAnalyzer(platformAPI string) func(t *testing.T, when spec.G, it spec.S)
h.AssertEq(t, md.RunImage.TargetMetadata.ArchVariant, "MMX")
h.AssertEq(t, md.RunImage.TargetMetadata.OS, "windows")
h.AssertEq(t, md.RunImage.TargetMetadata.ID, "id software")
h.AssertNotNil(t, md.RunImage.TargetMetadata.Distribution)
h.AssertEq(t, md.RunImage.TargetMetadata.Distribution.Name, "moobuntu")
h.AssertEq(t, md.RunImage.TargetMetadata.Distribution.Version, "Helpful Holstein")
h.AssertNotNil(t, md.RunImage.TargetMetadata.Distro)
h.AssertEq(t, md.RunImage.TargetMetadata.Distro.Name, "moobuntu")
h.AssertEq(t, md.RunImage.TargetMetadata.Distro.Version, "Helpful Holstein")
}
})
})
Expand Down
26 changes: 13 additions & 13 deletions detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("totally works if the constraints are met", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "snow cheetah"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "snow cheetah"},
},
}

Expand Down Expand Up @@ -862,9 +862,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("was born to be wildcard compliant", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "snow cheetah"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "snow cheetah"},
},
}

Expand Down Expand Up @@ -896,9 +896,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("totally works if the constraints are met", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "snow cheetah"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "snow cheetah"},
},
}

Expand Down Expand Up @@ -946,9 +946,9 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
it("errors if the buildpacks don't share that target arch/os", func() {
detector.AnalyzeMD.RunImage = &files.RunImage{
TargetMetadata: &files.TargetMetadata{
OS: "MacOS",
Arch: "ARM64",
Distribution: &files.OSDistribution{Name: "MacOS", Version: "some kind of big cat"},
OS: "MacOS",
Arch: "ARM64",
Distro: &files.OSDistro{Name: "MacOS", Version: "some kind of big cat"},
},
}

Expand All @@ -972,7 +972,7 @@ func testDetector(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, ok, true)
outs := val.(buildpack.DetectOutputs)
h.AssertEq(t, outs.Code, -1)
h.AssertStringContains(t, outs.Err.Error(), `unable to satisfy target os/arch constraints; run image: {"os":"MacOS","arch":"ARM64","distribution":{"name":"MacOS","version":"some kind of big cat"}}, buildpack: [{"os":"Win95","arch":"P6","arch-variant":"Pentium Pro","distros":[{"name":"Windows 95","version":"OSR1"},{"name":"Windows 95","version":"OSR2.5"}]},{"os":"Win98","arch":"Pentium M","distros":[{"name":"Windows 2000","version":"Server"}]}]`)
h.AssertStringContains(t, outs.Err.Error(), `unable to satisfy target os/arch constraints; run image: {"os":"MacOS","arch":"ARM64","distro":{"name":"MacOS","version":"some kind of big cat"}}, buildpack: [{"os":"Win95","arch":"P6","arch-variant":"Pentium Pro","distros":[{"name":"Windows 95","version":"OSR1"},{"name":"Windows 95","version":"OSR2.5"}]},{"os":"Win98","arch":"Pentium M","distros":[{"name":"Windows 2000","version":"Server"}]}]`)
return []buildpack.GroupElement{}, []files.BuildPlanEntry{}, nil
})

Expand Down
12 changes: 5 additions & 7 deletions platform/files/analyzed.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package files

import (
"fmt"
"os"

"github.com/BurntSushi/toml"

"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/internal/encoding"
"github.com/buildpacks/lifecycle/log"
)

Expand Down Expand Up @@ -157,17 +157,15 @@ type TargetMetadata struct {
Arch string `json:"arch" toml:"arch"`
ArchVariant string `json:"arch-variant,omitempty" toml:"arch-variant,omitempty"`

Distribution *OSDistribution `json:"distribution,omitempty" toml:"distribution,omitempty"`
Distro *OSDistro `json:"distro,omitempty" toml:"distro,omitempty"`
}

func (t *TargetMetadata) String() string {
if t.Distribution != nil {
return fmt.Sprintf("OS: %s, Arch: %s, ArchVariant: %s, Distribution: (Name: %s, Version: %s)", t.OS, t.Arch, t.ArchVariant, t.Distribution.Name, t.Distribution.Version)
}
return fmt.Sprintf("OS: %s, Arch: %s, ArchVariant: %s", t.OS, t.Arch, t.ArchVariant)
return encoding.ToJSONMaybe(*t)
}

type OSDistribution struct {
// OSDistro is the OS distribution that a base image provides.
type OSDistro struct {
Name string `json:"name" toml:"name"`
Version string `json:"version" toml:"version"`
}
9 changes: 6 additions & 3 deletions platform/run_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import (
)

const (
TargetLabel = "io.buildpacks.id"
OSDistributionNameLabel = "io.buildpacks.distribution.name"
OSDistributionVersionLabel = "io.buildpacks.distribution.version"
// TargetLabel is the label containing the target ID.
TargetLabel = "io.buildpacks.base.id"
// OSDistroNameLabel is the label containing the OS distribution name.
OSDistroNameLabel = "io.buildpacks.distro.name"
// OSDistroVersionLabel is the label containing the OS distribution version.
OSDistroVersionLabel = "io.buildpacks.distro.version"
)

func BestRunImageMirrorFor(targetRegistry string, runImageMD files.RunImageForExport, checkReadAccess CheckReadAccess) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions platform/run_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func testRunImage(t *testing.T, when spec.G, it spec.S) {

when(".EnvVarsFor", func() {
it("returns the right thing", func() {
tm := files.TargetMetadata{Arch: "pentium", ArchVariant: "mmx", ID: "my-id", OS: "linux", Distribution: &files.OSDistribution{Name: "nix", Version: "22.11"}}
tm := files.TargetMetadata{Arch: "pentium", ArchVariant: "mmx", ID: "my-id", OS: "linux", Distro: &files.OSDistro{Name: "nix", Version: "22.11"}}
observed := platform.EnvVarsFor(tm)
h.AssertContains(t, observed, "CNB_TARGET_ARCH="+tm.Arch)
h.AssertContains(t, observed, "CNB_TARGET_ARCH_VARIANT="+tm.ArchVariant)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_NAME="+tm.Distribution.Name)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_VERSION="+tm.Distribution.Version)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_NAME="+tm.Distro.Name)
h.AssertContains(t, observed, "CNB_TARGET_DISTRO_VERSION="+tm.Distro.Version)
h.AssertContains(t, observed, "CNB_TARGET_ID="+tm.ID)
h.AssertContains(t, observed, "CNB_TARGET_OS="+tm.OS)
h.AssertEq(t, len(observed), 6)
Expand Down
26 changes: 13 additions & 13 deletions platform/target_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func EnvVarsFor(tm files.TargetMetadata) []string {
ret := []string{"CNB_TARGET_OS=" + tm.OS, "CNB_TARGET_ARCH=" + tm.Arch}
ret = append(ret, "CNB_TARGET_ARCH_VARIANT="+tm.ArchVariant)
var distName, distVersion string
if tm.Distribution != nil {
distName = tm.Distribution.Name
distVersion = tm.Distribution.Version
if tm.Distro != nil {
distName = tm.Distro.Name
distVersion = tm.Distro.Version
}
ret = append(ret, "CNB_TARGET_DISTRO_NAME="+distName)
ret = append(ret, "CNB_TARGET_DISTRO_VERSION="+distVersion)
Expand Down Expand Up @@ -46,10 +46,10 @@ func GetTargetMetadata(fromImage imgutil.Image) (*files.TargetMetadata, error) {
if err != nil {
return &tm, err
}
distName, distNameExists := labels[OSDistributionNameLabel]
distVersion, distVersionExists := labels[OSDistributionVersionLabel]
distName, distNameExists := labels[OSDistroNameLabel]
distVersion, distVersionExists := labels[OSDistroVersionLabel]
if distNameExists || distVersionExists {
tm.Distribution = &files.OSDistribution{Name: distName, Version: distVersion}
tm.Distro = &files.OSDistro{Name: distName, Version: distVersion}
}
if id, exists := labels[TargetLabel]; exists {
tm.ID = id
Expand All @@ -70,7 +70,7 @@ func GetTargetOSFromFileSystem(d fsutil.Detector, tm *files.TargetMetadata, logg
info := d.GetInfo(contents)
if info.Version != "" || info.Name != "" {
tm.OS = "linux"
tm.Distribution = &files.OSDistribution{Name: info.Name, Version: info.Version}
tm.Distro = &files.OSDistro{Name: info.Name, Version: info.Version}
}
}
}
Expand All @@ -86,12 +86,12 @@ func TargetSatisfiedForBuild(base files.TargetMetadata, module buildpack.TargetM
if !matches(base.ArchVariant, module.ArchVariant) {
return false
}
if base.Distribution == nil || len(module.Distros) == 0 {
if base.Distro == nil || len(module.Distros) == 0 {
return true
}
foundMatchingDist := false
for _, modDist := range module.Distros {
if matches(base.Distribution.Name, modDist.Name) && matches(base.Distribution.Version, modDist.Version) {
if matches(base.Distro.Name, modDist.Name) && matches(base.Distro.Version, modDist.Version) {
foundMatchingDist = true
break
}
Expand All @@ -114,14 +114,14 @@ func TargetSatisfiedForRebase(t files.TargetMetadata, appTargetMetadata files.Ta
if !matches(t.ArchVariant, appTargetMetadata.ArchVariant) {
return false
}
if t.Distribution != nil {
if appTargetMetadata.Distribution == nil {
if t.Distro != nil {
if appTargetMetadata.Distro == nil {
return false
}
if t.Distribution.Name != "" && t.Distribution.Name != appTargetMetadata.Distribution.Name {
if t.Distro.Name != "" && t.Distro.Name != appTargetMetadata.Distro.Name {
return false
}
if t.Distribution.Version != "" && t.Distribution.Version != appTargetMetadata.Distribution.Version {
if t.Distro.Version != "" && t.Distro.Version != appTargetMetadata.Distro.Version {
return false
}
}
Expand Down
30 changes: 15 additions & 15 deletions platform/target_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) {
})

when("has distro information", func() {
baseTarget.Distribution = &files.OSDistribution{Name: "A", Version: "1"}
baseTarget.Distro = &files.OSDistro{Name: "A", Version: "1"}

when("buildpack data", func() {
when("has distro information", func() {
Expand Down Expand Up @@ -111,9 +111,9 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) {
it("matches", func() {
h.AssertEq(t, platform.TargetSatisfiedForRebase(baseTarget, files.TargetMetadata{OS: baseTarget.OS, Arch: baseTarget.Arch, ArchVariant: "MMX"}), true)
h.AssertEq(t, platform.TargetSatisfiedForRebase(baseTarget, files.TargetMetadata{
OS: baseTarget.OS,
Arch: baseTarget.Arch,
Distribution: &files.OSDistribution{Name: "a", Version: "2"},
OS: baseTarget.OS,
Arch: baseTarget.Arch,
Distro: &files.OSDistro{Name: "a", Version: "2"},
}), true)
})
})
Expand All @@ -139,20 +139,20 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) {
})

when("has distro information", func() {
baseTarget.Distribution = &files.OSDistribution{Name: "A", Version: "1"}
baseTarget.Distro = &files.OSDistro{Name: "A", Version: "1"}

when("new image data", func() {
when("has distro information", func() {
it("must match", func() {
h.AssertEq(t, platform.TargetSatisfiedForRebase(baseTarget, files.TargetMetadata{
OS: baseTarget.OS,
Arch: baseTarget.Arch,
Distribution: &files.OSDistribution{Name: "A", Version: "1"},
OS: baseTarget.OS,
Arch: baseTarget.Arch,
Distro: &files.OSDistro{Name: "A", Version: "1"},
}), true)
h.AssertEq(t, platform.TargetSatisfiedForRebase(baseTarget, files.TargetMetadata{
OS: baseTarget.OS,
Arch: baseTarget.Arch,
Distribution: &files.OSDistribution{Name: "B", Version: "2"},
OS: baseTarget.OS,
Arch: baseTarget.Arch,
Distro: &files.OSDistro{Name: "B", Version: "2"},
}), false)
})
})
Expand All @@ -176,8 +176,8 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) {
t: t,
HasFile: true}
platform.GetTargetOSFromFileSystem(&d, &tm, logr)
h.AssertEq(t, "opensesame", tm.Distribution.Name)
h.AssertEq(t, "3.14", tm.Distribution.Version)
h.AssertEq(t, "opensesame", tm.Distro.Name)
h.AssertEq(t, "3.14", tm.Distro.Version)
})

it("doesn't populate if there's no file", func() {
Expand All @@ -187,7 +187,7 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) {
t: t,
HasFile: false}
platform.GetTargetOSFromFileSystem(&d, &tm, logr)
h.AssertNil(t, tm.Distribution)
h.AssertNil(t, tm.Distro)
})

it("doesn't populate if there's an error reading the file", func() {
Expand All @@ -199,7 +199,7 @@ func testTargetData(t *testing.T, when spec.G, it spec.S) {
ReadFileErr: fmt.Errorf("I'm sorry Dave, I don't even remember exactly what HAL says"),
}
platform.GetTargetOSFromFileSystem(&d, &tm, logr)
h.AssertNil(t, tm.Distribution)
h.AssertNil(t, tm.Distro)
})
})
}
Expand Down
16 changes: 8 additions & 8 deletions rebaser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,20 +793,20 @@ func testRebaser(t *testing.T, when spec.G, it spec.S) {
h.AssertError(t, err, `unable to satisfy target os/arch constraints; new run image: {"os":"linux","arch":"amd64","arch-variant":"variant2"}, old run image: {"os":"linux","arch":"amd64","arch-variant":"variant1"}`)
})

it("errors and prevents the rebase from taking place when the io.buildpacks.distribution.name are different", func() {
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.distribution.name", "distro1"))
h.AssertNil(t, fakeNewBaseImage.SetLabel("io.buildpacks.distribution.name", "distro2"))
it("errors and prevents the rebase from taking place when the io.buildpacks.distro.name are different", func() {
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.distro.name", "distro1"))
h.AssertNil(t, fakeNewBaseImage.SetLabel("io.buildpacks.distro.name", "distro2"))

_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, fakeAppImage.Name(), additionalNames)
h.AssertError(t, err, `unable to satisfy target os/arch constraints; new run image: {"os":"linux","arch":"amd64","distribution":{"name":"distro2","version":""}}, old run image: {"os":"linux","arch":"amd64","distribution":{"name":"distro1","version":""}}`)
h.AssertError(t, err, `unable to satisfy target os/arch constraints; new run image: {"os":"linux","arch":"amd64","distro":{"name":"distro2","version":""}}, old run image: {"os":"linux","arch":"amd64","distro":{"name":"distro1","version":""}}`)
})

it("errors and prevents the rebase from taking place when the io.buildpacks.distribution.version are different", func() {
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.distribution.version", "version1"))
h.AssertNil(t, fakeNewBaseImage.SetLabel("io.buildpacks.distribution.version", "version2"))
it("errors and prevents the rebase from taking place when the io.buildpacks.distro.version are different", func() {
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.distro.version", "version1"))
h.AssertNil(t, fakeNewBaseImage.SetLabel("io.buildpacks.distro.version", "version2"))

_, err := rebaser.Rebase(fakeAppImage, fakeNewBaseImage, fakeAppImage.Name(), additionalNames)
h.AssertError(t, err, `unable to satisfy target os/arch constraints; new run image: {"os":"linux","arch":"amd64","distribution":{"name":"","version":"version2"}}, old run image: {"os":"linux","arch":"amd64","distribution":{"name":"","version":"version1"}}`)
h.AssertError(t, err, `unable to satisfy target os/arch constraints; new run image: {"os":"linux","arch":"amd64","distro":{"name":"","version":"version2"}}, old run image: {"os":"linux","arch":"amd64","distro":{"name":"","version":"version1"}}`)
})
})
})
Expand Down

0 comments on commit b9b2926

Please sign in to comment.