2013-09-08 01:27:25 -04:00
|
|
|
package puppetmasterless
|
2013-08-01 20:05:23 -04:00
|
|
|
|
|
|
|
import (
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2017-10-03 14:39:33 -04:00
|
|
|
"fmt"
|
2013-09-08 02:31:28 -04:00
|
|
|
"io/ioutil"
|
2017-10-03 14:39:33 -04:00
|
|
|
"log"
|
2013-09-08 02:31:28 -04:00
|
|
|
"os"
|
2018-04-28 00:17:27 -04:00
|
|
|
"path/filepath"
|
2015-11-03 14:59:55 -05:00
|
|
|
"strings"
|
2013-08-01 20:05:23 -04:00
|
|
|
"testing"
|
2015-11-03 14:36:04 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-10-03 14:39:33 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2013-08-01 20:05:23 -04:00
|
|
|
)
|
|
|
|
|
2018-04-25 14:49:13 -04:00
|
|
|
func testConfig() (config map[string]interface{}, tf *os.File) {
|
2013-09-08 02:31:28 -04:00
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-04-25 14:49:13 -04:00
|
|
|
config = map[string]interface{}{
|
2013-09-08 02:31:28 -04:00
|
|
|
"manifest_file": tf.Name(),
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
2018-04-25 14:49:13 -04:00
|
|
|
|
|
|
|
return config, tf
|
2013-08-01 20:05:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Provisioner{}
|
|
|
|
if _, ok := raw.(packer.Provisioner); !ok {
|
|
|
|
t.Fatalf("must be a Provisioner")
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 02:31:28 -04:00
|
|
|
|
2017-10-03 14:39:33 -04:00
|
|
|
func TestGuestOSConfig_empty_unix(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
|
|
|
|
2017-10-03 14:39:33 -04:00
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
// Execute Puppet
|
|
|
|
p.config.ctx.Data = &ExecuteTemplate{
|
|
|
|
ManifestFile: "/r/m/f",
|
|
|
|
PuppetBinDir: p.config.PuppetBinDir,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
WorkingDir: p.config.WorkingDir,
|
|
|
|
}
|
|
|
|
log.Println(p.config.ExecuteCommand)
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "cd /tmp/packer-puppet-masterless && " +
|
2018-04-27 20:40:13 -04:00
|
|
|
"sudo -E puppet apply --detailed-exitcodes /r/m/f"
|
2017-10-03 14:39:33 -04:00
|
|
|
assert.Equal(t, expected, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGuestOSConfig_full_unix(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
|
|
|
|
2017-10-03 14:39:33 -04:00
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
facterVars := []string{
|
|
|
|
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "lhs", "rhs"),
|
|
|
|
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "foo", "bar"),
|
|
|
|
}
|
|
|
|
modulePaths := []string{"/m/p", "/a/b"}
|
|
|
|
// Execute Puppet
|
|
|
|
p.config.ctx.Data = &ExecuteTemplate{
|
|
|
|
FacterVars: strings.Join(facterVars, p.guestOSTypeConfig.facterVarsJoiner),
|
|
|
|
HieraConfigPath: "/h/c/p",
|
|
|
|
ManifestDir: "/r/m/d",
|
|
|
|
ManifestFile: "/r/m/f",
|
|
|
|
ModulePath: strings.Join(modulePaths, p.guestOSTypeConfig.modulePathJoiner),
|
|
|
|
PuppetBinDir: p.config.PuppetBinDir,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
WorkingDir: p.config.WorkingDir,
|
|
|
|
ExtraArguments: strings.Join(p.config.ExtraArguments, " "),
|
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "cd /tmp/packer-puppet-masterless && FACTER_lhs='rhs' FACTER_foo='bar' " +
|
|
|
|
"sudo -E puppet apply " +
|
2018-04-27 20:40:13 -04:00
|
|
|
"--detailed-exitcodes --modulepath='/m/p:/a/b' --hiera_config='/h/c/p' " +
|
|
|
|
"--manifestdir='/r/m/d' /r/m/f"
|
2017-10-03 14:39:33 -04:00
|
|
|
assert.Equal(t, expected, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGuestOSConfig_empty_windows(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
|
|
|
|
2017-10-03 14:39:33 -04:00
|
|
|
config["guest_os_type"] = "windows"
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
// Execute Puppet
|
|
|
|
p.config.ctx.Data = &ExecuteTemplate{
|
|
|
|
ManifestFile: "/r/m/f",
|
|
|
|
PuppetBinDir: p.config.PuppetBinDir,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
WorkingDir: p.config.WorkingDir,
|
|
|
|
}
|
|
|
|
log.Println(p.config.ExecuteCommand)
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-04-28 00:17:27 -04:00
|
|
|
expected := "cd " + filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-masterless && puppet apply --detailed-exitcodes /r/m/f"
|
2017-10-03 14:39:33 -04:00
|
|
|
assert.Equal(t, expected, command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGuestOSConfig_full_windows(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
|
|
|
|
2017-10-03 14:39:33 -04:00
|
|
|
config["guest_os_type"] = "windows"
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
facterVars := []string{
|
|
|
|
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "lhs", "rhs"),
|
|
|
|
fmt.Sprintf(p.guestOSTypeConfig.facterVarsFmt, "foo", "bar"),
|
|
|
|
}
|
|
|
|
modulePaths := []string{"/m/p", "/a/b"}
|
|
|
|
// Execute Puppet
|
|
|
|
p.config.ctx.Data = &ExecuteTemplate{
|
|
|
|
FacterVars: strings.Join(facterVars, p.guestOSTypeConfig.facterVarsJoiner),
|
|
|
|
HieraConfigPath: "/h/c/p",
|
|
|
|
ManifestDir: "/r/m/d",
|
|
|
|
ManifestFile: "/r/m/f",
|
|
|
|
ModulePath: strings.Join(modulePaths, p.guestOSTypeConfig.modulePathJoiner),
|
|
|
|
PuppetBinDir: p.config.PuppetBinDir,
|
|
|
|
Sudo: !p.config.PreventSudo,
|
|
|
|
WorkingDir: p.config.WorkingDir,
|
|
|
|
ExtraArguments: strings.Join(p.config.ExtraArguments, " "),
|
|
|
|
}
|
|
|
|
command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-04-28 00:17:27 -04:00
|
|
|
expected := "cd " + filepath.ToSlash(os.Getenv("SYSTEMROOT")) + "/Temp/packer-puppet-masterless && " +
|
2017-10-03 14:39:33 -04:00
|
|
|
"SET \"FACTER_lhs=rhs\" & SET \"FACTER_foo=bar\" && " +
|
2018-04-27 20:40:13 -04:00
|
|
|
"puppet apply --detailed-exitcodes --modulepath='/m/p;/a/b' --hiera_config='/h/c/p' " +
|
|
|
|
"--manifestdir='/r/m/d' /r/m/f"
|
2017-10-03 14:39:33 -04:00
|
|
|
assert.Equal(t, expected, command)
|
|
|
|
}
|
|
|
|
|
2016-10-15 05:39:50 -04:00
|
|
|
func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-10-15 05:39:50 -04:00
|
|
|
|
|
|
|
delete(config, "puppet_bin_dir")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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["puppet_bin_dir"] = tf.Name()
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 16:24:17 -04:00
|
|
|
func TestProvisionerPrepare_hieraConfigPath(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-09-09 16:24:17 -04:00
|
|
|
|
|
|
|
delete(config, "hiera_config_path")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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["hiera_config_path"] = tf.Name()
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:31:28 -04:00
|
|
|
func TestProvisionerPrepare_manifestFile(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-09-08 02:31:28 -04:00
|
|
|
|
|
|
|
delete(config, "manifest_file")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should be an 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["manifest_file"] = tf.Name()
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 02:52:41 -05:00
|
|
|
func TestProvisionerPrepare_manifestDir(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-11-25 02:52:41 -05:00
|
|
|
|
|
|
|
delete(config, "manifestdir")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
2013-12-11 14:21:51 -05:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
2013-11-25 02:52:41 -05:00
|
|
|
if err != nil {
|
2013-12-11 14:21:51 -05:00
|
|
|
t.Fatalf("error: %s", err)
|
2013-11-25 02:52:41 -05:00
|
|
|
}
|
2013-12-11 14:21:51 -05:00
|
|
|
defer os.RemoveAll(td)
|
2013-11-25 02:52:41 -05:00
|
|
|
|
2013-12-11 14:21:51 -05:00
|
|
|
config["manifest_dir"] = td
|
2013-11-25 02:52:41 -05:00
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:31:28 -04:00
|
|
|
func TestProvisionerPrepare_modulePaths(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2013-09-08 02:31:28 -04:00
|
|
|
|
|
|
|
delete(config, "module_paths")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with bad paths
|
|
|
|
config["module_paths"] = []string{"i-should-not-exist"}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should be an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
config["module_paths"] = []string{td}
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 22:52:09 -05:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_facterFacts(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2015-02-24 22:52:09 -05:00
|
|
|
|
|
|
|
delete(config, "facter")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with malformed fact
|
|
|
|
config["facter"] = "fact=stringified"
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should be an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2015-02-24 22:56:37 -05:00
|
|
|
facts := make(map[string]string)
|
2015-02-24 22:52:09 -05:00
|
|
|
facts["fact_name"] = "fact_value"
|
2015-02-24 22:56:37 -05:00
|
|
|
config["facter"] = facts
|
2015-02-24 22:52:09 -05:00
|
|
|
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-02-24 22:56:37 -05:00
|
|
|
// Make sure the default facts are present
|
2015-02-24 22:52:09 -05:00
|
|
|
delete(config, "facter")
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
2017-08-07 13:20:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-02-24 22:52:09 -05:00
|
|
|
if p.config.Facter == nil {
|
|
|
|
t.Fatalf("err: Default facts are not set in the Puppet provisioner!")
|
|
|
|
}
|
2016-12-27 00:11:27 -05:00
|
|
|
|
|
|
|
if _, ok := p.config.Facter["packer_build_name"]; !ok {
|
|
|
|
t.Fatalf("err: packer_build_name fact not set in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := p.config.Facter["packer_builder_type"]; !ok {
|
|
|
|
t.Fatalf("err: packer_builder_type fact not set in the Puppet provisioner!")
|
|
|
|
}
|
2015-02-24 22:52:09 -05:00
|
|
|
}
|
2015-11-03 14:36:04 -05:00
|
|
|
|
2015-11-03 17:55:03 -05:00
|
|
|
func TestProvisionerPrepare_extraArguments(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2015-11-03 14:36:04 -05:00
|
|
|
|
2015-11-03 18:18:24 -05:00
|
|
|
// Test with missing parameter
|
2015-11-03 17:55:03 -05:00
|
|
|
delete(config, "extra_arguments")
|
2015-11-03 14:36:04 -05:00
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-11-03 18:18:24 -05:00
|
|
|
// Test with malformed value
|
2015-11-03 17:55:03 -05:00
|
|
|
config["extra_arguments"] = "{{}}"
|
2015-11-03 14:36:04 -05:00
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should be an error")
|
|
|
|
}
|
|
|
|
|
2015-11-03 18:18:24 -05:00
|
|
|
// Test with valid values
|
2015-11-03 17:55:03 -05:00
|
|
|
config["extra_arguments"] = []string{
|
2015-11-03 14:36:04 -05:00
|
|
|
"arg",
|
|
|
|
}
|
|
|
|
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 14:59:55 -05:00
|
|
|
|
2016-12-28 22:14:17 -05:00
|
|
|
func TestProvisionerPrepare_stagingDir(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-12-28 22:14:17 -05:00
|
|
|
|
|
|
|
delete(config, "staging_directory")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the default staging directory is correct
|
|
|
|
if p.config.StagingDir != "/tmp/packer-puppet-masterless" {
|
|
|
|
t.Fatalf("err: Default staging_directory is not set in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure default staging directory can be overridden
|
|
|
|
config["staging_directory"] = "/tmp/override"
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.StagingDir != "/tmp/override" {
|
|
|
|
t.Fatalf("err: Overridden staging_directory is not set correctly in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_workingDir(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-12-28 22:14:17 -05:00
|
|
|
|
|
|
|
delete(config, "working_directory")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure default working dir and staging dir are the same
|
|
|
|
if p.config.WorkingDir != p.config.StagingDir {
|
|
|
|
t.Fatalf("err: Default working_directory is not set to the same value as default staging_directory in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the default working directory is correct
|
|
|
|
if p.config.WorkingDir != "/tmp/packer-puppet-masterless" {
|
|
|
|
t.Fatalf("err: Default working_directory is not set in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure default working directory can be overridden
|
|
|
|
config["working_directory"] = "/tmp/override"
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.WorkingDir != "/tmp/override" {
|
|
|
|
t.Fatalf("err: Overridden working_directory is not set correctly in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:55:03 -05:00
|
|
|
func TestProvisionerProvision_extraArguments(t *testing.T) {
|
2018-04-25 14:49:13 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
|
|
|
|
2015-11-03 14:59:55 -05:00
|
|
|
ui := &packer.MachineReadableUi{
|
|
|
|
Writer: ioutil.Discard,
|
|
|
|
}
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
|
2015-11-03 17:55:03 -05:00
|
|
|
extraArguments := []string{
|
2015-11-03 14:59:55 -05:00
|
|
|
"--some-arg=yup",
|
|
|
|
"--some-other-arg",
|
|
|
|
}
|
2015-11-03 17:55:03 -05:00
|
|
|
config["extra_arguments"] = extraArguments
|
2015-11-03 14:59:55 -05:00
|
|
|
|
2015-11-03 18:18:24 -05:00
|
|
|
// Test with valid values
|
2015-11-03 14:59:55 -05:00
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:59:44 -05:00
|
|
|
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-11-03 14:59:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:55:03 -05:00
|
|
|
expectedArgs := strings.Join(extraArguments, " ")
|
2015-11-03 14:59:55 -05:00
|
|
|
|
|
|
|
if !strings.Contains(comm.StartCmd.Command, expectedArgs) {
|
|
|
|
t.Fatalf("Command %q doesn't contain the expected arguments %q", comm.StartCmd.Command, expectedArgs)
|
|
|
|
}
|
2015-11-03 18:18:24 -05:00
|
|
|
|
|
|
|
// Test with missing parameter
|
|
|
|
delete(config, "extra_arguments")
|
|
|
|
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-12-12 13:59:44 -05:00
|
|
|
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-11-03 18:18:24 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the expected `extra_arguments` position for an empty value
|
|
|
|
splitCommand := strings.Split(comm.StartCmd.Command, " ")
|
|
|
|
if "" == splitCommand[len(splitCommand)-2] {
|
|
|
|
t.Fatalf("Command %q contains an extra-space which may cause arg parsing issues", comm.StartCmd.Command)
|
|
|
|
}
|
2015-11-03 14:59:55 -05:00
|
|
|
}
|