2015-09-02 06:29:26 -04:00
|
|
|
package shell_local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-03-01 11:48:21 -05:00
|
|
|
"runtime"
|
2015-09-02 06:29:26 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-05-21 18:26:57 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-09-02 06:29:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
|
2020-12-01 17:48:55 -05:00
|
|
|
var _ packersdk.PostProcessor = new(PostProcessor)
|
2015-09-02 06:29:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"inline": []interface{}{"foo", "bar"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &PostProcessor{}
|
2020-12-01 17:48:55 -05:00
|
|
|
if _, ok := raw.(packersdk.PostProcessor); !ok {
|
2015-09-02 06:29:26 -04:00
|
|
|
t.Fatalf("must be a post processor")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_Defaults(t *testing.T) {
|
|
|
|
var p PostProcessor
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
2015-09-02 06:29:26 -04:00
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_InlineShebang(t *testing.T) {
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
2015-09-02 06:29:26 -04:00
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
delete(raws, "inline_shebang")
|
2015-09-02 06:29:26 -04:00
|
|
|
p := new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2018-03-01 11:48:21 -05:00
|
|
|
expected := ""
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
expected = "/bin/sh -e"
|
|
|
|
}
|
|
|
|
if p.config.InlineShebang != expected {
|
2015-09-02 06:29:26 -04:00
|
|
|
t.Fatalf("bad value: %s", p.config.InlineShebang)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["inline_shebang"] = "foo"
|
2015-09-02 06:29:26 -04:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.InlineShebang != "foo" {
|
|
|
|
t.Fatalf("bad value: %s", p.config.InlineShebang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var p PostProcessor
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
2015-09-02 06:29:26 -04:00
|
|
|
|
|
|
|
// Add a random key
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["i_should_not_be_valid"] = true
|
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_Script(t *testing.T) {
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
|
|
|
delete(raws, "inline")
|
2015-09-02 06:29:26 -04:00
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["script"] = "/this/should/not/exist"
|
2015-09-02 06:29:26 -04:00
|
|
|
p := new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["script"] = tf.Name()
|
2015-09-02 06:29:26 -04:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:48:21 -05:00
|
|
|
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)
|
|
|
|
}
|
2018-05-21 18:26:57 -04:00
|
|
|
assert.Equal(t, p.config.ExecuteCommand, expected,
|
|
|
|
"Did not get expected execute_command: expected: %#v; received %#v", expected, p.config.ExecuteCommand)
|
2018-03-01 11:48:21 -05:00
|
|
|
|
|
|
|
// 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"}
|
2018-05-21 18:26:57 -04:00
|
|
|
assert.Equal(t, p.config.ExecuteCommand, expected,
|
|
|
|
"Did not get expected execute_command: expected: %#v; received %#v", expected, p.config.ExecuteCommand)
|
2018-03-01 11:48:21 -05:00
|
|
|
|
|
|
|
// Check that default is as expected
|
|
|
|
raws = testConfig()
|
|
|
|
delete(raws, "execute_command")
|
|
|
|
p = new(PostProcessor)
|
|
|
|
p.Configure(raws)
|
|
|
|
if runtime.GOOS != "windows" {
|
2018-05-21 18:26:57 -04:00
|
|
|
expected = []string{"/bin/sh", "-c", "{{.Vars}} {{.Script}}"}
|
2018-03-01 11:48:21 -05:00
|
|
|
} else {
|
2018-04-04 18:52:39 -04:00
|
|
|
expected = []string{"cmd", "/V", "/C", "{{.Vars}}", "call", "{{.Script}}"}
|
2018-03-01 11:48:21 -05:00
|
|
|
}
|
2018-05-21 18:26:57 -04:00
|
|
|
assert.Equal(t, p.config.ExecuteCommand, expected,
|
|
|
|
"Did not get expected default: expected: %#v; received %#v", expected, p.config.ExecuteCommand)
|
2018-03-01 11:48:21 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 06:29:26 -04:00
|
|
|
func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) {
|
|
|
|
var p PostProcessor
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
|
|
|
|
|
|
|
// Error if no scripts/inline commands provided
|
|
|
|
delete(raws, "inline")
|
|
|
|
delete(raws, "script")
|
|
|
|
delete(raws, "command")
|
|
|
|
delete(raws, "scripts")
|
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
2018-03-01 11:48:21 -05:00
|
|
|
t.Fatalf("should error when no scripts/inline commands are provided")
|
2015-09-02 06:29:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with both
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["inline"] = []interface{}{"foo"}
|
|
|
|
raws["script"] = tf.Name()
|
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_ScriptAndScripts(t *testing.T) {
|
|
|
|
var p PostProcessor
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
2015-09-02 06:29:26 -04:00
|
|
|
|
|
|
|
// Test with both
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["inline"] = []interface{}{"foo"}
|
|
|
|
raws["scripts"] = []string{tf.Name()}
|
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_Scripts(t *testing.T) {
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
|
|
|
delete(raws, "inline")
|
2015-09-02 06:29:26 -04:00
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["scripts"] = []string{}
|
2015-09-02 06:29:26 -04:00
|
|
|
p := new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["scripts"] = []string{tf.Name()}
|
2015-09-02 06:29:26 -04:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessorPrepare_EnvironmentVars(t *testing.T) {
|
2018-02-28 17:35:42 -05:00
|
|
|
raws := testConfig()
|
2015-09-02 06:29:26 -04:00
|
|
|
|
|
|
|
// Test with a bad case
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["environment_vars"] = []string{"badvar", "good=var"}
|
2015-09-02 06:29:26 -04:00
|
|
|
p := new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err := p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a trickier case
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["environment_vars"] = []string{"=bad"}
|
2015-09-02 06:29:26 -04:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good case
|
|
|
|
// Note: baz= is a real env variable, just empty
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["environment_vars"] = []string{"FOO=bar", "baz="}
|
2015-09-02 06:29:26 -04:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2015-09-02 06:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2017-01-23 17:25:39 -05:00
|
|
|
|
|
|
|
// Test when the env variable value contains an equals sign
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["environment_vars"] = []string{"good=withequals=true"}
|
2017-01-23 17:25:39 -05:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2017-01-23 17:25:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test when the env variable value starts with an equals sign
|
2018-02-28 17:35:42 -05:00
|
|
|
raws["environment_vars"] = []string{"good==true"}
|
2017-01-23 17:25:39 -05:00
|
|
|
p = new(PostProcessor)
|
2018-02-28 17:35:42 -05:00
|
|
|
err = p.Configure(raws)
|
2017-01-23 17:25:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2015-09-02 06:29:26 -04:00
|
|
|
}
|