2013-09-08 01:27:25 -04:00
|
|
|
package puppetmasterless
|
2013-08-01 20:05:23 -04:00
|
|
|
|
|
|
|
import (
|
2013-09-08 02:31:28 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
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"
|
2013-08-01 20:05:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
2013-09-08 02:31:28 -04:00
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-08-01 20:05:23 -04:00
|
|
|
return map[string]interface{}{
|
2013-09-08 02:31:28 -04:00
|
|
|
"manifest_file": tf.Name(),
|
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
|
|
|
|
2016-10-15 05:39:50 -04:00
|
|
|
func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
2015-11-03 14:36:04 -05:00
|
|
|
config := testConfig()
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
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) {
|
2015-11-03 14:59:55 -05:00
|
|
|
config := testConfig()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Provision(ui, comm)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.Provision(ui, comm)
|
|
|
|
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
|
|
|
}
|