-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse shuttle.yaml and plan.yaml strictly (#64)
Currently we silently ignore unknown fields in shuttle.yaml and plan.yaml. This can lead to odd behaviour and hard to find bugs from typos. This change adds strict parsing of shuttle.yaml and plan.yaml failing commands if any unexpected fields are found.
- Loading branch information
Showing
8 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShuttleConfig_getConf(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
input string | ||
err error | ||
config ShuttleConfig | ||
}{ | ||
{ | ||
name: "empty path", | ||
input: "", | ||
}, | ||
{ | ||
name: "unknown field", | ||
input: "testdata/unknown_field", | ||
err: errors.New("exit code 2 - Failed to parse shuttle configuration: yaml: unmarshal errors:\n line 1: field nothing not found in type config.ShuttleConfig\n\nMake sure your 'shuttle.yaml' is valid."), | ||
}, | ||
{ | ||
name: "unknown file", | ||
input: "testdata/unknown_file", | ||
err: errors.New("exit code 2 - Failed to load shuttle configuration: open testdata/unknown_file/shuttle.yaml: no such file or directory\n\nMake sure you are in a project using shuttle and that a 'shuttle.yaml' file is available."), | ||
}, | ||
{ | ||
name: "valid", | ||
input: "testdata/valid", | ||
err: nil, | ||
config: ShuttleConfig{ | ||
Plan: ".", | ||
PlanRaw: ".", | ||
Variables: map[string]interface{}{ | ||
"squad": "nasa", | ||
}, | ||
Scripts: map[string]ShuttlePlanScript{ | ||
"shout": { | ||
Description: "Shout hello", | ||
Actions: []ShuttleAction{ | ||
{ | ||
Shell: `echo "HELLO WORLD"`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
c := &ShuttleConfig{} | ||
var err error | ||
c, err = c.getConf(tc.input) | ||
|
||
if tc.err != nil { | ||
assert.EqualError(t, err, tc.err.Error(), "error not as expected") | ||
} else { | ||
assert.NoError(t, err, "unexpected error") | ||
} | ||
assert.Equal(t, tc.config, *c, "config not as expected") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShuttlePlanConfiguration_Load(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
input string | ||
err error | ||
config ShuttlePlanConfiguration | ||
}{ | ||
{ | ||
name: "empty path", | ||
input: "", | ||
}, | ||
{ | ||
name: "unknown field", | ||
input: "testdata/unknown_field", | ||
err: errors.New("exit code 1 - Failed to load plan configuration: yaml: unmarshal errors:\n line 1: field unknown not found in type config.ShuttlePlanConfiguration\n\nThis is likely an issue with the referenced plan. Please, contact the plan maintainers."), | ||
}, | ||
{ | ||
name: "unknown file", | ||
input: "testdata/unknown_file", | ||
err: errors.New("exit code 2 - Failed to open plan configuration: open testdata/unknown_file/plan.yaml: no such file or directory\n\nMake sure you are in a project using shuttle and that a 'shuttle.yaml' file is available."), | ||
}, | ||
{ | ||
name: "valid", | ||
input: "testdata/valid", | ||
err: nil, | ||
config: ShuttlePlanConfiguration{ | ||
Scripts: map[string]ShuttlePlanScript{ | ||
"hello": { | ||
Description: "Say hello", | ||
Actions: []ShuttleAction{ | ||
{ | ||
Shell: `echo "Hello world"`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
c := &ShuttlePlanConfiguration{} | ||
var err error | ||
c, err = c.Load(tc.input) | ||
|
||
if tc.err != nil { | ||
assert.EqualError(t, err, tc.err.Error(), "error not as expected") | ||
} else { | ||
assert.NoError(t, err, "unexpected error") | ||
} | ||
assert.Equal(t, tc.config, *c, "config not as expected") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
unknown: field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nothing: important |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
scripts: | ||
hello: | ||
description: Say hello | ||
actions: | ||
- shell: echo "Hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plan: . | ||
vars: | ||
squad: nasa | ||
scripts: | ||
shout: | ||
description: Shout hello | ||
actions: | ||
- shell: echo "HELLO WORLD" |