add tests making sure post-processor has backwards compatability

This commit is contained in:
Megan Marsh 2018-03-01 08:48:21 -08:00
parent f799003b66
commit 854d6fb141
3 changed files with 64 additions and 4 deletions

View File

@ -87,7 +87,6 @@ func Validate(config *Config) error {
"{{.Vars}}",
"{{.Script}}",
}
config.ExecuteCommand = []string{`chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`}
}
}

View File

@ -1,6 +1,8 @@
package shell_local
import (
"runtime"
sl "github.com/hashicorp/packer/common/shell-local"
"github.com/hashicorp/packer/packer"
)
@ -19,6 +21,18 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
if err != nil {
return err
}
if len(p.config.ExecuteCommand) == 0 && runtime.GOOS != "windows" {
// Backwards compatibility from before post-processor merge with
// provisioner. Don't need to default separately for windows becuase the
// post-processor never worked for windows before the merge with the
// provisioner code, so the provisioner defaults are fine.
p.config.ExecuteCommand = []string{"sh", "-c", `chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`}
} else if len(p.config.ExecuteCommand) == 1 {
// Backwards compatibility -- before merge, post-processor didn't have
// configurable call to shell program, meaning users may not have
// defined this in their call
p.config.ExecuteCommand = append([]string{"sh", "-c"}, p.config.ExecuteCommand...)
}
return sl.Validate(&p.config)
}

View File

@ -3,6 +3,8 @@ package shell_local
import (
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
"github.com/hashicorp/packer/packer"
@ -45,8 +47,11 @@ func TestPostProcessorPrepare_InlineShebang(t *testing.T) {
if err != nil {
t.Fatalf("should not have error: %s", err)
}
if p.config.InlineShebang != "/bin/sh -e" {
expected := ""
if runtime.GOOS != "windows" {
expected = "/bin/sh -e"
}
if p.config.InlineShebang != expected {
t.Fatalf("bad value: %s", p.config.InlineShebang)
}
@ -101,6 +106,48 @@ func TestPostProcessorPrepare_Script(t *testing.T) {
}
}
func TestPostProcessorPrepare_ExecuteCommand(t *testing.T) {
// Check that passing a string will work (Backwards Compatibility)
p := new(PostProcessor)
raws := testConfig()
raws["execute_command"] = "foo bar"
err := p.Configure(raws)
expected := []string{"sh", "-c", "foo bar"}
if err != nil {
t.Fatalf("should handle backwards compatibility: %s", err)
}
if strings.Compare(strings.Join(p.config.ExecuteCommand, " "), strings.Join(expected, " ")) != 0 {
t.Fatalf("Did not get expected execute_command: expected: %#v; received %#v", expected, p.config.ExecuteCommand)
}
// Check that passing a list will work
p = new(PostProcessor)
raws = testConfig()
raws["execute_command"] = []string{"foo", "bar"}
err = p.Configure(raws)
if err != nil {
t.Fatalf("should handle backwards compatibility: %s", err)
}
expected = []string{"foo", "bar"}
if strings.Compare(strings.Join(p.config.ExecuteCommand, " "), strings.Join(expected, " ")) != 0 {
t.Fatalf("Did not get expected execute_command: expected: %#v; received %#v", expected, p.config.ExecuteCommand)
}
// Check that default is as expected
raws = testConfig()
delete(raws, "execute_command")
p = new(PostProcessor)
p.Configure(raws)
if runtime.GOOS != "windows" {
expected = []string{"sh", "-c", `chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`}
} else {
expected = []string{"cmd", "/C", "{{.Vars}}", "{{.Script}}"}
}
if strings.Compare(strings.Join(p.config.ExecuteCommand, " "), strings.Join(expected, " ")) != 0 {
t.Fatalf("Did not get expected default: expected: %#v; received %#v", expected, p.config.ExecuteCommand)
}
}
func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) {
var p PostProcessor
raws := testConfig()
@ -112,7 +159,7 @@ func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) {
delete(raws, "scripts")
err := p.Configure(raws)
if err == nil {
t.Fatalf("should error when no scripts/inline commands are provided: %#v", raws)
t.Fatalf("should error when no scripts/inline commands are provided")
}
// Test with both