From 854d6fb141ae89ccf88c8f5c75ecf0c4de588454 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 1 Mar 2018 08:48:21 -0800 Subject: [PATCH] add tests making sure post-processor has backwards compatability --- common/shell-local/config.go | 1 - post-processor/shell-local/post-processor.go | 14 +++++ .../shell-local/post-processor_test.go | 53 +++++++++++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/common/shell-local/config.go b/common/shell-local/config.go index 76c82c793..2d31b7f01 100644 --- a/common/shell-local/config.go +++ b/common/shell-local/config.go @@ -87,7 +87,6 @@ func Validate(config *Config) error { "{{.Vars}}", "{{.Script}}", } - config.ExecuteCommand = []string{`chmod +x "{{.Script}}"; {{.Vars}} "{{.Script}}"`} } } diff --git a/post-processor/shell-local/post-processor.go b/post-processor/shell-local/post-processor.go index 91bc5acc9..557fe7eba 100644 --- a/post-processor/shell-local/post-processor.go +++ b/post-processor/shell-local/post-processor.go @@ -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) } diff --git a/post-processor/shell-local/post-processor_test.go b/post-processor/shell-local/post-processor_test.go index caf4f5a42..afec79f81 100644 --- a/post-processor/shell-local/post-processor_test.go +++ b/post-processor/shell-local/post-processor_test.go @@ -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