add tests making sure post-processor has backwards compatability
This commit is contained in:
parent
f799003b66
commit
854d6fb141
|
@ -87,7 +87,6 @@ func Validate(config *Config) error {
|
||||||
"{{.Vars}}",
|
"{{.Vars}}",
|
||||||
"{{.Script}}",
|
"{{.Script}}",
|
||||||
}
|
}
|
||||||
config.ExecuteCommand = []string{`chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package shell_local
|
package shell_local
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
|
|
||||||
sl "github.com/hashicorp/packer/common/shell-local"
|
sl "github.com/hashicorp/packer/common/shell-local"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
)
|
)
|
||||||
|
@ -19,6 +21,18 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
return sl.Validate(&p.config)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package shell_local
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
|
@ -45,8 +47,11 @@ func TestPostProcessorPrepare_InlineShebang(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("should not have error: %s", err)
|
t.Fatalf("should not have error: %s", err)
|
||||||
}
|
}
|
||||||
|
expected := ""
|
||||||
if p.config.InlineShebang != "/bin/sh -e" {
|
if runtime.GOOS != "windows" {
|
||||||
|
expected = "/bin/sh -e"
|
||||||
|
}
|
||||||
|
if p.config.InlineShebang != expected {
|
||||||
t.Fatalf("bad value: %s", p.config.InlineShebang)
|
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) {
|
func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) {
|
||||||
var p PostProcessor
|
var p PostProcessor
|
||||||
raws := testConfig()
|
raws := testConfig()
|
||||||
|
@ -112,7 +159,7 @@ func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) {
|
||||||
delete(raws, "scripts")
|
delete(raws, "scripts")
|
||||||
err := p.Configure(raws)
|
err := p.Configure(raws)
|
||||||
if err == nil {
|
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
|
// Test with both
|
||||||
|
|
Loading…
Reference in New Issue