Commit 2f7e95cc authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template: Validate

parent 2e4dd639
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
//-------------------------------------------------------------------
......
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)
}
}
}
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"override": {
"bar": {}
}
}]
}
{
"builders": [{
"type": "foo"
}],
"provisioners": [{
"type": "bar",
"override": {
"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