2013-05-24 01:38:40 -04:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
2013-06-11 16:42:15 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-04-26 19:04:29 -04:00
|
|
|
"regexp"
|
2016-09-29 17:13:04 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-10-12 02:35:24 -04:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-12-01 17:25:14 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-24 01:38:40 -04:00
|
|
|
)
|
|
|
|
|
2013-06-11 16:42:15 -04:00
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"inline": []interface{}{"foo", "bar"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:38:40 -04:00
|
|
|
func TestProvisioner_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Provisioner{}
|
2020-12-01 17:25:14 -05:00
|
|
|
if _, ok := raw.(packersdk.Provisioner); !ok {
|
2013-05-24 01:38:40 -04:00
|
|
|
t.Fatalf("must be a Provisioner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:06:34 -04:00
|
|
|
func TestProvisionerPrepare_Defaults(t *testing.T) {
|
2013-06-11 16:42:15 -04:00
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
2013-05-27 18:06:34 -04:00
|
|
|
|
2013-06-11 16:42:15 -04:00
|
|
|
err := p.Prepare(config)
|
2013-06-06 20:07:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-27 18:06:34 -04:00
|
|
|
|
2017-10-12 02:35:24 -04:00
|
|
|
if p.config.ExpectDisconnect != false {
|
2017-08-24 17:46:52 -04:00
|
|
|
t.Errorf("expected ExpectDisconnect to default to false")
|
2016-10-19 21:30:19 -04:00
|
|
|
}
|
|
|
|
|
2015-06-18 04:38:23 -04:00
|
|
|
if p.config.RemotePath == "" {
|
2013-05-27 18:06:34 -04:00
|
|
|
t.Errorf("unexpected remote path: %s", p.config.RemotePath)
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 16:42:15 -04:00
|
|
|
|
2016-10-19 21:30:19 -04:00
|
|
|
func TestProvisionerPrepare_ExpectDisconnect(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
p := new(Provisioner)
|
|
|
|
config["expect_disconnect"] = false
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:35:24 -04:00
|
|
|
if p.config.ExpectDisconnect != false {
|
2016-10-19 21:30:19 -04:00
|
|
|
t.Errorf("expected ExpectDisconnect to be false")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["expect_disconnect"] = true
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:35:24 -04:00
|
|
|
if p.config.ExpectDisconnect != true {
|
2016-10-19 21:30:19 -04:00
|
|
|
t.Errorf("expected ExpectDisconnect to be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:52:20 -04:00
|
|
|
func TestProvisionerPrepare_InlineShebang(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "inline_shebang")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-11 17:24:02 -04:00
|
|
|
if p.config.InlineShebang != "/bin/sh -e" {
|
2013-07-07 20:52:20 -04:00
|
|
|
t.Fatalf("bad value: %s", p.config.InlineShebang)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["inline_shebang"] = "foo"
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.InlineShebang != "foo" {
|
|
|
|
t.Fatalf("bad value: %s", p.config.InlineShebang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
func TestProvisionerPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 13:56:46 -04:00
|
|
|
func TestProvisionerPrepare_Script(t *testing.T) {
|
2013-06-11 16:42:15 -04:00
|
|
|
config := testConfig()
|
|
|
|
delete(config, "inline")
|
|
|
|
|
2013-06-27 13:56:46 -04:00
|
|
|
config["script"] = "/this/should/not/exist"
|
2013-06-20 16:45:54 -04:00
|
|
|
p := new(Provisioner)
|
2013-06-11 16:42:15 -04:00
|
|
|
err := p.Prepare(config)
|
|
|
|
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())
|
|
|
|
|
2013-06-27 13:56:46 -04:00
|
|
|
config["script"] = tf.Name()
|
2013-06-20 16:45:54 -04:00
|
|
|
p = new(Provisioner)
|
2013-06-11 16:42:15 -04:00
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 13:56:46 -04:00
|
|
|
func TestProvisionerPrepare_ScriptAndInline(t *testing.T) {
|
2013-06-11 16:42:15 -04:00
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "inline")
|
2013-06-27 13:56:46 -04:00
|
|
|
delete(config, "script")
|
2013-06-11 16:42:15 -04:00
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with both
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
config["inline"] = []interface{}{"foo"}
|
2013-06-27 13:56:46 -04:00
|
|
|
config["script"] = tf.Name()
|
2013-06-11 16:42:15 -04:00
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 16:45:54 -04:00
|
|
|
|
2013-06-27 13:56:46 -04:00
|
|
|
func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) {
|
2013-06-20 16:45:54 -04:00
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with both
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
config["inline"] = []interface{}{"foo"}
|
|
|
|
config["scripts"] = []string{tf.Name()}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_Scripts(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "inline")
|
|
|
|
|
|
|
|
config["scripts"] = []string{}
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
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())
|
|
|
|
|
|
|
|
config["scripts"] = []string{tf.Name()}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 08:39:16 -04:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_EnvironmentVars(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with a bad case
|
|
|
|
config["environment_vars"] = []string{"badvar", "good=var"}
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a trickier case
|
|
|
|
config["environment_vars"] = []string{"=bad"}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good case
|
|
|
|
// Note: baz= is a real env variable, just empty
|
|
|
|
config["environment_vars"] = []string{"FOO=bar", "baz="}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2017-01-23 05:33:15 -05:00
|
|
|
|
|
|
|
// Test when the env variable value contains an equals sign
|
|
|
|
config["environment_vars"] = []string{"good=withequals=true"}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test when the env variable value starts with an equals sign
|
|
|
|
config["environment_vars"] = []string{"good==true"}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2013-06-27 08:39:16 -04:00
|
|
|
}
|
2014-10-14 17:20:36 -04:00
|
|
|
|
2017-01-23 05:33:15 -05:00
|
|
|
func TestProvisioner_createFlattenedEnvVars(t *testing.T) {
|
|
|
|
var flattenedEnvVars string
|
2014-10-14 17:20:36 -04:00
|
|
|
config := testConfig()
|
|
|
|
|
2017-01-23 05:33:15 -05:00
|
|
|
userEnvVarTests := [][]string{
|
|
|
|
{}, // No user env var
|
|
|
|
{"FOO=bar"}, // Single user env var
|
|
|
|
{"FOO=bar's"}, // User env var with single quote in value
|
|
|
|
{"FOO=bar", "BAZ=qux"}, // Multiple user env vars
|
|
|
|
{"FOO=bar=baz"}, // User env var with value containing equals
|
|
|
|
{"FOO==bar"}, // User env var with value starting with equals
|
|
|
|
}
|
|
|
|
expected := []string{
|
|
|
|
`PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
|
|
|
|
`FOO='bar' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
|
|
|
|
`FOO='bar'"'"'s' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
|
|
|
|
`BAZ='qux' FOO='bar' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
|
|
|
|
`FOO='bar=baz' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
|
|
|
|
`FOO='=bar' PACKER_BUILDER_TYPE='iso' PACKER_BUILD_NAME='vmware' `,
|
|
|
|
}
|
|
|
|
|
2014-10-14 17:20:36 -04:00
|
|
|
p := new(Provisioner)
|
2020-05-19 05:49:48 -04:00
|
|
|
p.generatedData = generatedData()
|
2014-10-14 17:20:36 -04:00
|
|
|
p.Prepare(config)
|
|
|
|
|
2017-01-23 05:33:15 -05:00
|
|
|
// Defaults provided by Packer
|
|
|
|
p.config.PackerBuildName = "vmware"
|
|
|
|
p.config.PackerBuilderType = "iso"
|
2014-10-14 17:20:36 -04:00
|
|
|
|
2017-01-23 05:33:15 -05:00
|
|
|
for i, expectedValue := range expected {
|
|
|
|
p.config.Vars = userEnvVarTests[i]
|
|
|
|
flattenedEnvVars = p.createFlattenedEnvVars()
|
|
|
|
if flattenedEnvVars != expectedValue {
|
|
|
|
t.Fatalf("expected flattened env vars to be: %s, got %s.", expectedValue, flattenedEnvVars)
|
|
|
|
}
|
2014-10-14 17:20:36 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 19:04:29 -04:00
|
|
|
|
2019-10-31 14:41:48 -04:00
|
|
|
func TestProvisioner_createFlattenedEnvVars_withEnvVarFormat(t *testing.T) {
|
|
|
|
var flattenedEnvVars string
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
userEnvVarTests := [][]string{
|
|
|
|
{}, // No user env var
|
|
|
|
{"FOO=bar"}, // Single user env var
|
|
|
|
{"FOO=bar's"}, // User env var with single quote in value
|
|
|
|
{"FOO=bar", "BAZ=qux"}, // Multiple user env vars
|
|
|
|
{"FOO=bar=baz"}, // User env var with value containing equals
|
|
|
|
{"FOO==bar"}, // User env var with value starting with equals
|
|
|
|
}
|
|
|
|
expected := []string{
|
|
|
|
`PACKER_BUILDER_TYPE=iso PACKER_BUILD_NAME=vmware `,
|
|
|
|
`FOO=bar PACKER_BUILDER_TYPE=iso PACKER_BUILD_NAME=vmware `,
|
|
|
|
`FOO=bar'"'"'s PACKER_BUILDER_TYPE=iso PACKER_BUILD_NAME=vmware `,
|
|
|
|
`BAZ=qux FOO=bar PACKER_BUILDER_TYPE=iso PACKER_BUILD_NAME=vmware `,
|
|
|
|
`FOO=bar=baz PACKER_BUILDER_TYPE=iso PACKER_BUILD_NAME=vmware `,
|
|
|
|
`FOO==bar PACKER_BUILDER_TYPE=iso PACKER_BUILD_NAME=vmware `,
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
2020-05-19 05:49:48 -04:00
|
|
|
p.generatedData = generatedData()
|
2019-10-31 14:41:48 -04:00
|
|
|
p.config.EnvVarFormat = "%s=%s "
|
|
|
|
p.Prepare(config)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
p.config.PackerBuildName = "vmware"
|
|
|
|
p.config.PackerBuilderType = "iso"
|
|
|
|
|
|
|
|
for i, expectedValue := range expected {
|
|
|
|
p.config.Vars = userEnvVarTests[i]
|
|
|
|
flattenedEnvVars = p.createFlattenedEnvVars()
|
|
|
|
if flattenedEnvVars != expectedValue {
|
|
|
|
t.Fatalf("expected flattened env vars to be: %s, got %s.", expectedValue, flattenedEnvVars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 14:10:45 -04:00
|
|
|
func TestProvisioner_createEnvVarFileContent(t *testing.T) {
|
|
|
|
var flattenedEnvVars string
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
userEnvVarTests := [][]string{
|
|
|
|
{}, // No user env var
|
|
|
|
{"FOO=bar"}, // Single user env var
|
|
|
|
{"FOO=bar's"}, // User env var with single quote in value
|
|
|
|
{"FOO=bar", "BAZ=qux"}, // Multiple user env vars
|
|
|
|
{"FOO=bar=baz"}, // User env var with value containing equals
|
|
|
|
{"FOO==bar"}, // User env var with value starting with equals
|
|
|
|
}
|
|
|
|
expected := []string{
|
|
|
|
`export PACKER_BUILDER_TYPE='iso'
|
|
|
|
export PACKER_BUILD_NAME='vmware'
|
|
|
|
`,
|
|
|
|
`export FOO='bar'
|
|
|
|
export PACKER_BUILDER_TYPE='iso'
|
|
|
|
export PACKER_BUILD_NAME='vmware'
|
|
|
|
`,
|
|
|
|
`export FOO='bar'"'"'s'
|
|
|
|
export PACKER_BUILDER_TYPE='iso'
|
|
|
|
export PACKER_BUILD_NAME='vmware'
|
|
|
|
`,
|
|
|
|
`export BAZ='qux'
|
|
|
|
export FOO='bar'
|
|
|
|
export PACKER_BUILDER_TYPE='iso'
|
|
|
|
export PACKER_BUILD_NAME='vmware'
|
|
|
|
`,
|
|
|
|
`export FOO='bar=baz'
|
|
|
|
export PACKER_BUILDER_TYPE='iso'
|
|
|
|
export PACKER_BUILD_NAME='vmware'
|
|
|
|
`,
|
|
|
|
`export FOO='=bar'
|
|
|
|
export PACKER_BUILDER_TYPE='iso'
|
|
|
|
export PACKER_BUILD_NAME='vmware'
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
2020-05-19 05:49:48 -04:00
|
|
|
p.generatedData = generatedData()
|
2019-10-31 14:41:48 -04:00
|
|
|
p.config.UseEnvVarFile = true
|
2018-08-29 14:10:45 -04:00
|
|
|
p.Prepare(config)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
p.config.PackerBuildName = "vmware"
|
|
|
|
p.config.PackerBuilderType = "iso"
|
|
|
|
|
|
|
|
for i, expectedValue := range expected {
|
|
|
|
p.config.Vars = userEnvVarTests[i]
|
|
|
|
flattenedEnvVars = p.createEnvVarFileContent()
|
|
|
|
if flattenedEnvVars != expectedValue {
|
|
|
|
t.Fatalf("expected flattened env vars to be: %s, got %s.", expectedValue, flattenedEnvVars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:41:48 -04:00
|
|
|
func TestProvisioner_createEnvVarFileContent_withEnvVarFormat(t *testing.T) {
|
|
|
|
var flattenedEnvVars string
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
userEnvVarTests := [][]string{
|
|
|
|
{}, // No user env var
|
|
|
|
{"FOO=bar", "BAZ=qux"}, // Multiple user env vars
|
|
|
|
{"FOO=bar=baz"}, // User env var with value containing equals
|
|
|
|
{"FOO==bar"}, // User env var with value starting with equals
|
|
|
|
}
|
|
|
|
expected := []string{
|
|
|
|
`PACKER_BUILDER_TYPE=iso
|
|
|
|
PACKER_BUILD_NAME=vmware
|
|
|
|
`,
|
|
|
|
`BAZ=qux
|
|
|
|
FOO=bar
|
|
|
|
PACKER_BUILDER_TYPE=iso
|
|
|
|
PACKER_BUILD_NAME=vmware
|
|
|
|
`,
|
|
|
|
`FOO=bar=baz
|
|
|
|
PACKER_BUILDER_TYPE=iso
|
|
|
|
PACKER_BUILD_NAME=vmware
|
|
|
|
`,
|
|
|
|
`FOO==bar
|
|
|
|
PACKER_BUILDER_TYPE=iso
|
|
|
|
PACKER_BUILD_NAME=vmware
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
2020-05-19 05:49:48 -04:00
|
|
|
p.generatedData = generatedData()
|
2019-10-31 14:41:48 -04:00
|
|
|
p.config.UseEnvVarFile = true
|
|
|
|
//User provided env_var_format without export prefix
|
|
|
|
p.config.EnvVarFormat = "%s=%s\n"
|
|
|
|
p.Prepare(config)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
p.config.PackerBuildName = "vmware"
|
|
|
|
p.config.PackerBuilderType = "iso"
|
|
|
|
|
|
|
|
for i, expectedValue := range expected {
|
|
|
|
p.config.Vars = userEnvVarTests[i]
|
|
|
|
flattenedEnvVars = p.createEnvVarFileContent()
|
|
|
|
if flattenedEnvVars != expectedValue {
|
|
|
|
t.Fatalf("expected flattened env vars to be: %q, got %q.", expectedValue, flattenedEnvVars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:04:29 -04:00
|
|
|
func TestProvisioner_RemoteFolderSetSuccessfully(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
expectedRemoteFolder := "/example/path"
|
|
|
|
config["remote_folder"] = expectedRemoteFolder
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.RemotePath, expectedRemoteFolder) {
|
|
|
|
t.Fatalf("remote path does not contain remote_folder")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_RemoteFolderDefaultsToTmp(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RemoteFolder != "/tmp" {
|
|
|
|
t.Fatalf("remote_folder did not default to /tmp")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.RemotePath, "/tmp") {
|
|
|
|
t.Fatalf("remote path does not contain remote_folder")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_RemoteFileSetSuccessfully(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
expectedRemoteFile := "example.sh"
|
|
|
|
config["remote_file"] = expectedRemoteFile
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.RemotePath, expectedRemoteFile) {
|
|
|
|
t.Fatalf("remote path does not contain remote_file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_RemoteFileDefaultsToScriptnnnn(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-03-17 12:10:43 -04:00
|
|
|
remoteFileRegex := regexp.MustCompile("script_[0-9]{1,4}.sh")
|
2016-04-26 19:04:29 -04:00
|
|
|
|
|
|
|
if !remoteFileRegex.MatchString(p.config.RemoteFile) {
|
2020-03-17 12:02:18 -04:00
|
|
|
t.Fatalf("remote_file did not default to script_nnnn.sh: %q", p.config.RemoteFile)
|
2016-04-26 19:04:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !remoteFileRegex.MatchString(p.config.RemotePath) {
|
2020-03-17 12:02:18 -04:00
|
|
|
t.Fatalf("remote_path did not match script_nnnn.sh: %q", p.config.RemotePath)
|
2016-04-26 19:04:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_RemotePathSetViaRemotePathAndRemoteFile(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
expectedRemoteFile := "example.sh"
|
|
|
|
expectedRemoteFolder := "/example/path"
|
|
|
|
config["remote_file"] = expectedRemoteFile
|
|
|
|
config["remote_folder"] = expectedRemoteFolder
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-29 17:13:04 -04:00
|
|
|
if p.config.RemotePath != expectedRemoteFolder+"/"+expectedRemoteFile {
|
2020-03-18 06:51:54 -04:00
|
|
|
t.Fatalf("remote path does not contain remote_file: %q", p.config.RemotePath)
|
2016-04-26 19:04:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_RemotePathOverridesRemotePathAndRemoteFile(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
expectedRemoteFile := "example.sh"
|
|
|
|
expectedRemoteFolder := "/example/path"
|
|
|
|
expectedRemotePath := "/example/remote/path/script.sh"
|
|
|
|
config["remote_file"] = expectedRemoteFile
|
|
|
|
config["remote_folder"] = expectedRemoteFolder
|
|
|
|
config["remote_path"] = expectedRemotePath
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RemotePath != expectedRemotePath {
|
2020-03-18 06:51:54 -04:00
|
|
|
t.Fatalf("remote path does not contain remote_path: %q", p.config.RemotePath)
|
2016-04-26 19:04:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerRemotePathDefaultsSuccessfully(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-03-18 06:51:54 -04:00
|
|
|
remotePathRegex := regexp.MustCompile("/tmp/script_[0-9]{1,4}.sh")
|
2016-04-26 19:04:29 -04:00
|
|
|
|
|
|
|
if !remotePathRegex.MatchString(p.config.RemotePath) {
|
2020-03-18 06:51:54 -04:00
|
|
|
t.Fatalf("remote path does not match the expected default regex: %q", p.config.RemotePath)
|
2016-04-26 19:04:29 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:49:48 -04:00
|
|
|
|
|
|
|
func generatedData() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2020-11-11 18:04:28 -05:00
|
|
|
"PackerHTTPAddr": commonsteps.HttpAddrNotImplemented,
|
|
|
|
"PackerHTTPIP": commonsteps.HttpIPNotImplemented,
|
|
|
|
"PackerHTTPPort": commonsteps.HttpPortNotImplemented,
|
2020-05-19 05:49:48 -04:00
|
|
|
}
|
|
|
|
}
|