Commit 637fabc1 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template: validate only/except

parent 2f7e95cc
......@@ -91,6 +91,15 @@ func (t *Template) Validate() error {
// Verify that the provisioner overrides target builders that exist
for i, p := range t.Provisioners {
// Validate only/except
if verr := p.OnlyExcept.Validate(t); verr != nil {
for _, e := range multierror.Append(verr).Errors {
err = multierror.Append(err, fmt.Errorf(
"provisioner %d: %s", i+1, e))
}
}
// Validate overrides
for name, _ := range p.Override {
if _, ok := t.Builders[name]; !ok {
err = multierror.Append(err, fmt.Errorf(
......@@ -103,6 +112,29 @@ func (t *Template) Validate() error {
return err
}
// Validate validates that the OnlyExcept settings are correct for a thing.
func (o *OnlyExcept) Validate(t *Template) error {
if len(o.Only) > 0 && len(o.Except) > 0 {
return errors.New("only one of 'only' or 'except' may be specified")
}
var err error
for _, n := range o.Only {
if _, ok := t.Builders[n]; !ok {
err = multierror.Append(err, fmt.Errorf(
"'only' specified builder '%s' not found", n))
}
}
for _, n := range o.Except {
if _, ok := t.Builders[n]; !ok {
err = multierror.Append(err, fmt.Errorf(
"'except' specified builder '%s' not found", n))
}
}
return err
}
//-------------------------------------------------------------------
// GoStringer
//-------------------------------------------------------------------
......
......@@ -32,6 +32,26 @@ func TestTemplateValidate(t *testing.T) {
"validate-good-override.json",
false,
},
{
"validate-bad-prov-only.json",
true,
},
{
"validate-good-prov-only.json",
false,
},
{
"validate-bad-prov-except.json",
true,
},
{
"validate-good-prov-except.json",
false,
},
}
for _, tc := range cases {
......
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"except": ["bar"]
}]
}
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"only": ["bar"]
}]
}
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"except": ["foo"]
}]
}
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"only": ["foo"]
}]
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment