diff --git a/command/command_test.go b/command/command_test.go index 126897810..4ba4b8787 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -20,6 +20,13 @@ func fatalCommand(t *testing.T, m Meta) { err.String()) } +func outputCommand(t *testing.T, m Meta) (string, string) { + ui := m.Ui.(*packer.BasicUi) + out := ui.Writer.(*bytes.Buffer) + err := ui.ErrorWriter.(*bytes.Buffer) + return out.String(), err.String() +} + func testFixture(n string) string { return filepath.Join(fixturesDir, n) } diff --git a/command/meta.go b/command/meta.go index 1c0864f92..d22efcaba 100644 --- a/command/meta.go +++ b/command/meta.go @@ -43,7 +43,6 @@ func (m *Meta) Core(tpl *template.Template) (*packer.Core, error) { config := *m.CoreConfig config.Template = tpl config.Variables = m.flagVars - config.Version = m.Version // Init the core core, err := packer.NewCore(&config) diff --git a/command/push_test.go b/command/push_test.go index f1b7fd306..9d7b79be7 100644 --- a/command/push_test.go +++ b/command/push_test.go @@ -122,10 +122,8 @@ func TestPush_noName(t *testing.T) { func TestPush_cliName(t *testing.T) { var actual []string - var actualOpts *uploadOpts uploadFn := func(r io.Reader, opts *uploadOpts) (<-chan struct{}, <-chan error, error) { actual = testArchive(t, r) - actualOpts = opts doneCh := make(chan struct{}) close(doneCh) diff --git a/command/test-fixtures/validate/template.json b/command/test-fixtures/validate/template.json new file mode 100644 index 000000000..75b3f6251 --- /dev/null +++ b/command/test-fixtures/validate/template.json @@ -0,0 +1,10 @@ +{ + "builders":[ + { + "type":"file", + "target":"chocolate.txt", + "content":"chocolate" + } + ], + "min_packer_version":"101.0.0" +} diff --git a/command/validate_test.go b/command/validate_test.go new file mode 100644 index 000000000..b15e5f155 --- /dev/null +++ b/command/validate_test.go @@ -0,0 +1,44 @@ +package command + +import ( + "path/filepath" + "testing" +) + +func TestValidateCommandOKVersion(t *testing.T) { + c := &ValidateCommand{ + Meta: testMetaFile(t), + } + args := []string{ + filepath.Join(testFixture("validate"), "template.json"), + } + + // This should pass with a valid configuration version + c.CoreConfig.Version = "102.0.0" + if code := c.Run(args); code != 0 { + fatalCommand(t, c.Meta) + } +} + +func TestValidateCommandBadVersion(t *testing.T) { + c := &ValidateCommand{ + Meta: testMetaFile(t), + } + args := []string{ + filepath.Join(testFixture("validate"), "template.json"), + } + + // This should fail with an invalid configuration version + c.CoreConfig.Version = "100.0.0" + if code := c.Run(args); code != 1 { + t.Errorf("Expected exit code 1") + } + + stdout, stderr := outputCommand(t, c.Meta) + expected := `Error initializing core: This template requires Packer version 101.0.0 or higher; using 100.0.0 +` + if stderr != expected { + t.Fatalf("Expected:\n%s\nFound:\n%s\n", expected, stderr) + } + t.Log(stdout) +} diff --git a/packer/core.go b/packer/core.go index f9bf87b9d..496fce6bd 100644 --- a/packer/core.go +++ b/packer/core.go @@ -246,8 +246,7 @@ func (c *Core) validate() error { if versionActual.LessThan(versionMin) { return fmt.Errorf( - "This template requires a minimum Packer version of %s,\n"+ - "but version %s is running.", + "This template requires Packer version %s or higher; using %s", versionMin, versionActual) }