2013-08-01 02:01:03 -04:00
|
|
|
package saltmasterless
|
2013-07-27 03:11:04 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-10 13:20:02 -04:00
|
|
|
"io/ioutil"
|
2013-08-01 02:11:08 -04:00
|
|
|
"os"
|
2015-08-23 07:56:16 -04:00
|
|
|
"strings"
|
2013-07-27 03:11:04 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2013-08-01 02:11:08 -04:00
|
|
|
"local_state_tree": os.TempDir(),
|
2013-07-27 03:11:04 -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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_Defaults(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.TempConfigDir != DefaultTempConfigDir {
|
|
|
|
t.Errorf("unexpected temp config dir: %s", p.config.TempConfigDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-01 02:11:08 -04:00
|
|
|
|
2013-08-10 13:20:02 -04:00
|
|
|
func TestProvisionerPrepare_MinionConfig(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["minion_config"] = "/i/dont/exist/i/think"
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "minion")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
config["minion_config"] = tf.Name()
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 07:56:16 -04:00
|
|
|
func TestProvisionerPrepare_MinionConfig_RemoteStateTree(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["minion_config"] = "/i/dont/exist/i/think"
|
|
|
|
config["remote_state_tree"] = "/i/dont/exist/remote_state_tree"
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("minion_config and remote_state_tree should cause error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_MinionConfig_RemotePillarRoots(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["minion_config"] = "/i/dont/exist/i/think"
|
|
|
|
config["remote_pillar_roots"] = "/i/dont/exist/remote_pillar_roots"
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("minion_config and remote_pillar_roots should cause error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 02:11:08 -04:00
|
|
|
func TestProvisionerPrepare_LocalStateTree(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["local_state_tree"] = "/i/dont/exist/i/think"
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["local_state_tree"] = os.TempDir()
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 19:37:06 -04:00
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
func TestProvisionerPrepare_LocalPillarRoots(t *testing.T) {
|
2013-08-27 19:37:06 -04:00
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
config["local_pillar_roots"] = "/i/dont/exist/i/think"
|
2013-08-27 19:37:06 -04:00
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:48:24 -04:00
|
|
|
config["local_pillar_roots"] = os.TempDir()
|
2013-08-27 19:37:06 -04:00
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-13 09:32:09 -04:00
|
|
|
|
|
|
|
func TestProvisionerSudo(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-06-22 15:37:52 -04:00
|
|
|
withSudo := p.sudo("echo hello")
|
|
|
|
if withSudo != "sudo echo hello" {
|
|
|
|
t.Fatalf("sudo command not generated correctly")
|
|
|
|
}
|
2015-05-13 09:32:09 -04:00
|
|
|
|
2015-06-22 15:37:52 -04:00
|
|
|
config["disable_sudo"] = true
|
2015-05-13 09:32:09 -04:00
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-06-22 15:37:52 -04:00
|
|
|
withoutSudo := p.sudo("echo hello")
|
|
|
|
if withoutSudo != "echo hello" {
|
|
|
|
t.Fatalf("sudo-less command not generated correctly")
|
|
|
|
}
|
2015-05-13 09:32:09 -04:00
|
|
|
}
|
2015-08-23 07:56:16 -04:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_RemoteStateTree(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["remote_state_tree"] = "/remote_state_tree"
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.CmdArgs, "--file-root=/remote_state_tree") {
|
|
|
|
t.Fatal("--file-root should be set in CmdArgs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_RemotePillarRoots(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["remote_pillar_roots"] = "/remote_pillar_roots"
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.CmdArgs, "--pillar-root=/remote_pillar_roots") {
|
|
|
|
t.Fatal("--pillar-root should be set in CmdArgs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_RemoteStateTree_Default(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// no minion_config, no remote_state_tree
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.CmdArgs, "--file-root=/srv/salt") {
|
|
|
|
t.Fatal("--file-root should be set in CmdArgs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_RemotePillarRoots_Default(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// no minion_config, no remote_pillar_roots
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.CmdArgs, "--pillar-root=/srv/pillar") {
|
|
|
|
t.Fatal("--pillar-root should be set in CmdArgs")
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 08:40:05 -04:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_NoExitOnFailure(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(p.config.CmdArgs, "--retcode-passthrough") {
|
|
|
|
t.Fatal("--retcode-passthrough should be set in CmdArgs")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["no_exit_on_failure"] = true
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(p.config.CmdArgs, "--retcode-passthrough") {
|
|
|
|
t.Fatal("--retcode-passthrough should not be set in CmdArgs")
|
|
|
|
}
|
|
|
|
}
|