Merge pull request #2228 from mitchellh/b-fix-validate

command/fix: validate resulting template [GH-2075]
This commit is contained in:
Mitchell Hashimoto 2015-06-15 09:37:13 -07:00
commit 53cf507060
4 changed files with 97 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"strings"
"github.com/mitchellh/packer/fix"
"github.com/mitchellh/packer/template"
)
type FixCommand struct {
@ -16,7 +17,9 @@ type FixCommand struct {
}
func (c *FixCommand) Run(args []string) int {
var flagValidate bool
flags := c.Meta.FlagSet("fix", FlagSetNone)
flags.BoolVar(&flagValidate, "validate", true, "")
flags.Usage = func() { c.Ui.Say(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
@ -80,6 +83,28 @@ func (c *FixCommand) Run(args []string) int {
result = strings.Replace(result, `\u003c`, "<", -1)
result = strings.Replace(result, `\u003e`, ">", -1)
c.Ui.Say(result)
if flagValidate {
// Attemot to parse and validate the template
tpl, err := template.Parse(strings.NewReader(result))
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error! Fixed template fails to parse: %s\n\n"+
"This is usually caused by an error in the input template.\n"+
"Please fix the error and try again.",
err))
return 1
}
if err := tpl.Validate(); err != nil {
c.Ui.Error(fmt.Sprintf(
"Error! Fixed template failed to validate: %s\n\n"+
"This is usually caused by an error in the input template.\n"+
"Please fix the error and try again.",
err))
return 1
}
}
return 0
}
@ -102,6 +127,10 @@ Fixes that are run:
pp-vagrant-override Replaces old-style provider overrides for the Vagrant
post-processor to new-style as of Packer 0.5.0.
virtualbox-rename Updates "virtualbox" builders to "virtualbox-iso"
Options:
-validate=true If true (default), validates the fixed template.
`
return strings.TrimSpace(helpText)

58
command/fix_test.go Normal file
View File

@ -0,0 +1,58 @@
package command
import (
"path/filepath"
"testing"
)
func TestFix_noArgs(t *testing.T) {
c := &PushCommand{Meta: testMeta(t)}
code := c.Run(nil)
if code != 1 {
t.Fatalf("bad: %#v", code)
}
}
func TestFix_multiArgs(t *testing.T) {
c := &PushCommand{Meta: testMeta(t)}
code := c.Run([]string{"one", "two"})
if code != 1 {
t.Fatalf("bad: %#v", code)
}
}
func TestFix(t *testing.T) {
c := &FixCommand{
Meta: testMeta(t),
}
args := []string{filepath.Join(testFixture("fix"), "template.json")}
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
}
func TestFix_invalidTemplate(t *testing.T) {
c := &FixCommand{
Meta: testMeta(t),
}
args := []string{filepath.Join(testFixture("fix-invalid"), "template.json")}
if code := c.Run(args); code != 1 {
fatalCommand(t, c.Meta)
}
}
func TestFix_invalidTemplateDisableValidation(t *testing.T) {
c := &FixCommand{
Meta: testMeta(t),
}
args := []string{
"-validate=false",
filepath.Join(testFixture("fix-invalid"), "template.json"),
}
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
}

View File

@ -0,0 +1,3 @@
{
"hello": "world"
}

View File

@ -0,0 +1,7 @@
{
"builders": [{"type": "dummy"}],
"push": {
"name": "foo/bar"
}
}