Skip to content

Commit

Permalink
Validate upgrade action on build
Browse files Browse the repository at this point in the history
Validate the upgrade action during `porter build`, otherwise it will
only the validated when `porter upgrade` is executed. All other actions
`install`, `uninstall` and custom actions are validated on `porter build`
already.

Signed-off-by: Kim Christensen <kimworking@gmail.com>
  • Loading branch information
kichristensen committed Mar 23, 2024
1 parent f0a68b3 commit f99db1d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ func (m *Manifest) Validate(ctx context.Context, cfg *config.Config) error {
result = multierror.Append(result, fmt.Errorf(invalidStepErrorFormat, "uninstall", err))
}

if m.Upgrade != nil {
err = m.Upgrade.Validate(m)
if err != nil {
result = multierror.Append(result, fmt.Errorf(invalidStepErrorFormat, "upgrade", err))
}
}

for actionName, steps := range m.CustomActions {
err := steps.Validate(m)
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,45 @@ func TestAction_Validate_RequireSingleMixinData(t *testing.T) {
assert.EqualError(t, err, "more than one mixin specified")
}

func TestAction_Validate_RequireSingleMixinData_Actions(t *testing.T) {
testcases := []struct {
name string
getStep func(*Manifest) *Steps
}{
{"install", func(m *Manifest) *Steps { return &m.Install }},
{"uninstall", func(m *Manifest) *Steps { return &m.Uninstall }},
{"upgrade", func(m *Manifest) *Steps { return &m.Upgrade }},
{"custom", func(m *Manifest) *Steps { status := m.CustomActions["status"]; return &status }},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
c := config.NewTestConfig(t)

c.TestContext.AddTestFile("testdata/simple.porter.yaml", config.Name)

m, err := LoadManifestFrom(ctx, c.Config, config.Name)
require.NoError(t, err, "could not load manifest")
step := tc.getStep(m)

if len(*step) == 0 {
*step = make(Steps, 1)
(*step)[0] = &Step{
Data: make(map[string]interface{}),
}
(*step)[0].Data["exec"] = ""
}

// Sabotage!
(*step)[0].Data["rando-mixin"] = ""

err = m.Validate(ctx, c.Config)
assert.ErrorContains(t, err, "more than one mixin specified")
})
}
}

func TestManifest_Empty_Steps(t *testing.T) {
c := config.NewTestConfig(t)

Expand Down

0 comments on commit f99db1d

Please sign in to comment.