template: Validate
This commit is contained in:
parent
2e4dd63912
commit
2f7e95cc46
|
@ -1,8 +1,11 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
)
|
||||
|
||||
// Template represents the parsed template that is used to configure
|
||||
|
@ -68,6 +71,38 @@ type OnlyExcept struct {
|
|||
Except []string
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Functions
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
// Validate does some basic validation of the template on top of the
|
||||
// validation that occurs while parsing. If possible, we try to defer
|
||||
// validation to here. The validation errors that occur during parsing
|
||||
// are the minimal necessary to make sure parsing builds a reasonable
|
||||
// Template structure.
|
||||
func (t *Template) Validate() error {
|
||||
var err error
|
||||
|
||||
// At least one builder must be defined
|
||||
if len(t.Builders) == 0 {
|
||||
err = multierror.Append(err, errors.New(
|
||||
"at least one builder must be defined"))
|
||||
}
|
||||
|
||||
// Verify that the provisioner overrides target builders that exist
|
||||
for i, p := range t.Provisioners {
|
||||
for name, _ := range p.Override {
|
||||
if _, ok := t.Builders[name]; !ok {
|
||||
err = multierror.Append(err, fmt.Errorf(
|
||||
"provisioner %d: override '%s' doesn't exist",
|
||||
i+1, name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// GoStringer
|
||||
//-------------------------------------------------------------------
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const FixturesDir = "./test-fixtures"
|
||||
|
@ -10,3 +12,43 @@ const FixturesDir = "./test-fixtures"
|
|||
func fixtureDir(n string) string {
|
||||
return filepath.Join(FixturesDir, n)
|
||||
}
|
||||
|
||||
func TestTemplateValidate(t *testing.T) {
|
||||
cases := []struct {
|
||||
File string
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
"validate-no-builders.json",
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
"validate-bad-override.json",
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
"validate-good-override.json",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
f, err := os.Open(fixtureDir(tc.File))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
tpl, err := Parse(f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s\n\n%s", tc.File, err)
|
||||
}
|
||||
|
||||
err = tpl.Validate()
|
||||
if (err != nil) != tc.Err {
|
||||
t.Fatalf("err: %s\n\n%s", tc.File, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"builders": [{
|
||||
"type": "foo"
|
||||
}],
|
||||
|
||||
"provisioners": [{
|
||||
"type": "bar",
|
||||
"override": {
|
||||
"bar": {}
|
||||
}
|
||||
}]
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"builders": [{
|
||||
"type": "foo"
|
||||
}],
|
||||
|
||||
"provisioners": [{
|
||||
"type": "bar",
|
||||
"override": {
|
||||
"foo": {}
|
||||
}
|
||||
}]
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{}
|
Loading…
Reference in New Issue