template: validate only/except

This commit is contained in:
Mitchell Hashimoto 2015-05-21 15:39:32 -06:00
parent 2f7e95cc46
commit 637fabc1c7
6 changed files with 92 additions and 0 deletions

View File

@ -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
//-------------------------------------------------------------------

View File

@ -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 {

View File

@ -0,0 +1,10 @@
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"except": ["bar"]
}]
}

View File

@ -0,0 +1,10 @@
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"only": ["bar"]
}]
}

View File

@ -0,0 +1,10 @@
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"except": ["foo"]
}]
}

View File

@ -0,0 +1,10 @@
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"only": ["foo"]
}]
}