diff --git a/internal/commands/find_stemcell_version.go b/internal/commands/find_stemcell_version.go index b64800f4a..6f7ce6619 100644 --- a/internal/commands/find_stemcell_version.go +++ b/internal/commands/find_stemcell_version.go @@ -16,7 +16,6 @@ import ( ) const ( - ErrStemcellOSInfoMustBeValid = "stemcell os information is missing or invalid" ErrStemcellMajorVersionMustBeValid = "stemcell major Version is missing or invalid" TanzuNetRemotePath = "network.pivotal.io" ) @@ -52,19 +51,9 @@ func (cmd FindStemcellVersion) Execute(args []string) error { return err } - productSlug := "" - - // Get Stemcell OS and major from Kilnfile - if kilnfile.Stemcell.OS == "ubuntu-xenial" { - productSlug = "stemcells-ubuntu-xenial" - } else if kilnfile.Stemcell.OS == "ubuntu-jammy" { - productSlug = "stemcells-ubuntu-jammy" - } else if kilnfile.Stemcell.OS == "windows2019" { - productSlug = "stemcells-windows-server" - } - - if productSlug == "" { - return fmt.Errorf(ErrStemcellOSInfoMustBeValid) + productSlug, err := kilnfile.Stemcell.ProductSlug() + if err != nil { + return err } if kilnfile.Stemcell.Version == "" { diff --git a/internal/commands/find_stemcell_version_test.go b/internal/commands/find_stemcell_version_test.go index 8aeffc2b6..e1bbe788b 100644 --- a/internal/commands/find_stemcell_version_test.go +++ b/internal/commands/find_stemcell_version_test.go @@ -112,7 +112,7 @@ release_sources: }) It("returns the stemcell os info missing error message", func() { Expect(executeErr).To(HaveOccurred()) - Expect(executeErr).To(MatchError(ContainSubstring(commands.ErrStemcellOSInfoMustBeValid))) + Expect(executeErr).To(MatchError(ContainSubstring("stemcell slug not set"))) }) }) diff --git a/pkg/cargo/kilnfile.go b/pkg/cargo/kilnfile.go index e7fa8da46..5c83edf31 100644 --- a/pkg/cargo/kilnfile.go +++ b/pkg/cargo/kilnfile.go @@ -194,7 +194,25 @@ func (lock BOSHReleaseTarballLock) ParseVersion() (*semver.Version, error) { } type Stemcell struct { - Alias string `yaml:"alias,omitempty"` - OS string `yaml:"os"` - Version string `yaml:"version"` + Alias string `yaml:"alias,omitempty"` + OS string `yaml:"os"` + Version string `yaml:"version"` + TanzuNetSlug string `yaml:"slug,omitempty"` +} + +func (stemcell Stemcell) ProductSlug() (string, error) { + if stemcell.TanzuNetSlug != "" { + return stemcell.TanzuNetSlug, nil + } + // fall back behavior for compatability + switch stemcell.OS { + case "ubuntu-xenial": + return "stemcells-ubuntu-xenial", nil + case "ubuntu-jammy": + return "stemcells-ubuntu-jammy", nil + case "windows2019": + return "stemcells-windows-server", nil + default: + return "", fmt.Errorf("stemcell slug not set for os %s", stemcell.OS) + } } diff --git a/pkg/cargo/kilnfile_test.go b/pkg/cargo/kilnfile_test.go index a66eca739..7ed926bdc 100644 --- a/pkg/cargo/kilnfile_test.go +++ b/pkg/cargo/kilnfile_test.go @@ -5,8 +5,8 @@ import ( . "github.com/onsi/gomega" "github.com/stretchr/testify/assert" - - "gopkg.in/yaml.v2" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestComponentLock_yaml_marshal_order(t *testing.T) { @@ -72,3 +72,52 @@ func TestKilnfileLock_UpdateBOSHReleaseTarballLockWithName(t *testing.T) { }) } } + +func TestStemcell_ProductSlug(t *testing.T) { + for _, tt := range []struct { + Name string + Stemcell Stemcell + ExpSlug, ExpErrSubstring string + }{ + { + Name: "when using known os ubuntu-xenial", + Stemcell: Stemcell{OS: "ubuntu-xenial"}, + ExpSlug: "stemcells-ubuntu-xenial", + }, + { + Name: "when using known os ubuntu-jammy", + Stemcell: Stemcell{OS: "ubuntu-jammy"}, + ExpSlug: "stemcells-ubuntu-jammy", + }, + { + Name: "when using known os windows2019", + Stemcell: Stemcell{OS: "windows2019"}, + ExpSlug: "stemcells-windows-server", + }, + { + Name: "when slug is not set", + Stemcell: Stemcell{OS: "orange"}, + ExpErrSubstring: "stemcell slug not set", + }, + { + Name: "when slug is set", + Stemcell: Stemcell{TanzuNetSlug: "naval-orange"}, + ExpSlug: "naval-orange", + }, + { + Name: "when slug is set and os is a known value windows2019", + Stemcell: Stemcell{OS: "windows2019", TanzuNetSlug: "naval-orange"}, + ExpSlug: "naval-orange", + }, + } { + t.Run(tt.Name, func(t *testing.T) { + productSlug, err := tt.Stemcell.ProductSlug() + if tt.ExpErrSubstring != "" { + require.ErrorContains(t, err, tt.ExpErrSubstring) + } else { + require.NoError(t, err) + assert.Equal(t, tt.ExpSlug, productSlug) + } + }) + } +}