Commit 53cf5070 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

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

command/fix: validate resulting template [GH-2075]
parents 1691971e 1bcb52a0
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/mitchellh/packer/fix" "github.com/mitchellh/packer/fix"
"github.com/mitchellh/packer/template"
) )
type FixCommand struct { type FixCommand struct {
...@@ -16,7 +17,9 @@ type FixCommand struct { ...@@ -16,7 +17,9 @@ type FixCommand struct {
} }
func (c *FixCommand) Run(args []string) int { func (c *FixCommand) Run(args []string) int {
var flagValidate bool
flags := c.Meta.FlagSet("fix", FlagSetNone) flags := c.Meta.FlagSet("fix", FlagSetNone)
flags.BoolVar(&flagValidate, "validate", true, "")
flags.Usage = func() { c.Ui.Say(c.Help()) } flags.Usage = func() { c.Ui.Say(c.Help()) }
if err := flags.Parse(args); err != nil { if err := flags.Parse(args); err != nil {
return 1 return 1
...@@ -80,6 +83,28 @@ func (c *FixCommand) Run(args []string) int { ...@@ -80,6 +83,28 @@ func (c *FixCommand) Run(args []string) int {
result = strings.Replace(result, `\u003c`, "<", -1) result = strings.Replace(result, `\u003c`, "<", -1)
result = strings.Replace(result, `\u003e`, ">", -1) result = strings.Replace(result, `\u003e`, ">", -1)
c.Ui.Say(result) 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 return 0
} }
...@@ -102,6 +127,10 @@ Fixes that are run: ...@@ -102,6 +127,10 @@ Fixes that are run:
pp-vagrant-override Replaces old-style provider overrides for the Vagrant pp-vagrant-override Replaces old-style provider overrides for the Vagrant
post-processor to new-style as of Packer 0.5.0. post-processor to new-style as of Packer 0.5.0.
virtualbox-rename Updates "virtualbox" builders to "virtualbox-iso" virtualbox-rename Updates "virtualbox" builders to "virtualbox-iso"
Options:
-validate=true If true (default), validates the fixed template.
` `
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
......
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)
}
}
{
"builders": [{"type": "dummy"}],
"push": {
"name": "foo/bar"
}
}
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