Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow TanzuNet slug to be set in the Kilnfile stemcell criteria #413

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions internal/commands/find_stemcell_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/find_stemcell_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
})
})

Expand Down
24 changes: 21 additions & 3 deletions pkg/cargo/kilnfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
53 changes: 51 additions & 2 deletions pkg/cargo/kilnfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
}
Loading