Skip to content

Commit

Permalink
Added parsing step and tests
Browse files Browse the repository at this point in the history
Signed-off-by: David Gannon <[email protected]>
  • Loading branch information
dgannon991 committed Sep 1, 2024
1 parent b3d14ec commit 2d9f5a9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
48 changes: 44 additions & 4 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,30 @@ func (l Location) IsEmpty() bool {
}

type MixinDeclaration struct {
Name string
Config interface{}
Name string
Version *semver.Constraints
Config interface{}
}

func extractVersionFromName(name string) (string, *semver.Constraints, error) {
parts := strings.Split(name, "@")

// if there isn't a version in the name, just stop!
if len(parts) == 1 {
return name, nil, nil
}

// if we somehow got more parts than expected!
if len(parts) != 2 {
return "", nil, fmt.Errorf("expected name@version, got: %s", name)
}

version, err := semver.NewConstraint(parts[1])
if err != nil {
return "", nil, err
}

return parts[0], version, nil
}

// UnmarshalYAML allows mixin declarations to either be a normal list of strings
Expand All @@ -652,12 +674,25 @@ type MixinDeclaration struct {
// - az:
// extensions:
// - iot
//
// for each type, we can optionally support a version number in the name field
// mixins:
// - [email protected]
// or
// - [email protected]
// extensions:
// - iot
func (m *MixinDeclaration) UnmarshalYAML(unmarshal func(interface{}) error) error {
// First try to just read the mixin name
var mixinNameOnly string
err := unmarshal(&mixinNameOnly)
if err == nil {
m.Name = mixinNameOnly
name, version, err := extractVersionFromName(mixinNameOnly)
if err != nil {
return fmt.Errorf("invalid mixin name/version: %w", err)
}
m.Name = name
m.Version = version
m.Config = nil
return nil
}
Expand All @@ -676,7 +711,12 @@ func (m *MixinDeclaration) UnmarshalYAML(unmarshal func(interface{}) error) erro
}

for mixinName, config := range mixinWithConfig {
m.Name = mixinName
name, version, err := extractVersionFromName(mixinName)
if err != nil {
return fmt.Errorf("invalid mixin name/version: %w", err)
}
m.Name = name
m.Version = version
m.Config = config
break // There is only one mixin anyway but break for clarity
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/schema"
"get.porter.sh/porter/pkg/yaml"
"github.com/Masterminds/semver/v3"
"github.com/cnabio/cnab-go/bundle/definition"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -589,6 +590,43 @@ func TestMixinDeclaration_UnmarshalYAML_Invalid(t *testing.T) {
assert.Contains(t, err.Error(), "mixin declaration contained more than one mixin")
}

func TestMixinDeclaration_UnmarshalYAML_Versions(t *testing.T) {
cxt := portercontext.NewTestContext(t)
cxt.AddTestFile("testdata/mixin-with-versions.yaml", config.Name)
m, err := ReadManifest(cxt.Context, config.Name, config.NewTestConfig(t).Config)

execVersion, _ := semver.NewConstraint("1")
axVersion, _ := semver.NewConstraint("1.1.X")
terraformVersoin, _ := semver.NewConstraint(">=2")

require.NoError(t, err)
assert.Len(t, m.Mixins, 3, "expected 3 mixins")
assert.Equal(t, "exec", m.Mixins[0].Name)
assert.Equal(t, "az", m.Mixins[1].Name)
assert.Equal(t, "terraform", m.Mixins[2].Name)
assert.Equal(t, execVersion, m.Mixins[0].Version)
assert.Equal(t, axVersion, m.Mixins[1].Version)
assert.Equal(t, terraformVersoin, m.Mixins[2].Version)
}

func TestMixinDeclaration_UnmarshalYAML_Versions_Empty(t *testing.T) {
cxt := portercontext.NewTestContext(t)
cxt.AddTestFile("testdata/mixin-with-empty-version.yaml", config.Name)
_, err := ReadManifest(cxt.Context, config.Name, config.NewTestConfig(t).Config)

require.Error(t, err)
assert.Contains(t, err.Error(), "invalid mixin name/version: improper constraint:")
}

func TestMixinDeclaration_UnmarshalYAML_Versions_Invalid(t *testing.T) {
cxt := portercontext.NewTestContext(t)
cxt.AddTestFile("testdata/mixin-with-invalid-version.yaml", config.Name)
_, err := ReadManifest(cxt.Context, config.Name, config.NewTestConfig(t).Config)

require.Error(t, err)
assert.Contains(t, err.Error(), "invalid mixin name/version: expected name@version, got: az@this@that")
}

func TestCredentialsDefinition_UnmarshalYAML(t *testing.T) {
assertAllCredentialsRequired := func(t *testing.T, creds CredentialDefinitions) {
for _, cred := range creds {
Expand Down
6 changes: 6 additions & 0 deletions pkg/manifest/testdata/mixin-with-empty-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mixins:
- exec@
- az:
extensions:
- iot
- terraform
6 changes: 6 additions & 0 deletions pkg/manifest/testdata/mixin-with-invalid-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mixins:
- exec
- az@this@that:
extensions:
- iot
- terraform
6 changes: 6 additions & 0 deletions pkg/manifest/testdata/mixin-with-versions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mixins:
- exec@1
- [email protected]:
extensions:
- iot
- terraform@>=2

0 comments on commit 2d9f5a9

Please sign in to comment.