2014-01-07 11:32:57 -05:00
|
|
|
package puppetserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2017-08-07 13:20:01 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-01-07 11:32:57 -05:00
|
|
|
)
|
|
|
|
|
2018-04-25 14:56:16 -04:00
|
|
|
func testConfig() (config map[string]interface{}, tf *os.File) {
|
2014-01-07 11:32:57 -05:00
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-04-25 14:56:16 -04:00
|
|
|
config = map[string]interface{}{
|
2014-01-07 11:32:57 -05:00
|
|
|
"puppet_server": tf.Name(),
|
|
|
|
}
|
2018-04-25 14:56:16 -04:00
|
|
|
|
|
|
|
return config, tf
|
2014-01-07 11:32:57 -05: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 {
|
2014-01-07 11:32:57 -05:00
|
|
|
t.Fatalf("must be a Provisioner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-27 00:11:27 -05:00
|
|
|
func TestProvisionerPrepare_puppetBinDir(t *testing.T) {
|
2018-04-25 14:56:16 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-12-27 00:11:27 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 11:32:57 -05:00
|
|
|
func TestProvisionerPrepare_clientPrivateKeyPath(t *testing.T) {
|
2018-04-25 14:56:16 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2014-01-07 11:32:57 -05:00
|
|
|
|
|
|
|
delete(config, "client_private_key_path")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with bad paths
|
|
|
|
config["client_private_key_path"] = "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["client_private_key_path"] = td
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_clientCertPath(t *testing.T) {
|
2018-04-25 14:56:16 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2014-01-07 11:32:57 -05:00
|
|
|
|
|
|
|
delete(config, "client_cert_path")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with bad paths
|
|
|
|
config["client_cert_path"] = "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["client_cert_path"] = td
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-12-14 23:47:35 -05:00
|
|
|
|
2016-12-27 23:35:13 -05:00
|
|
|
func TestProvisionerPrepare_executeCommand(t *testing.T) {
|
2018-04-25 14:56:16 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-12-27 23:35:13 -05:00
|
|
|
|
|
|
|
delete(config, "execute_command")
|
|
|
|
p := new(Provisioner)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:47:35 -05:00
|
|
|
func TestProvisionerPrepare_facterFacts(t *testing.T) {
|
2018-04-25 14:56:16 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-12-14 23:47:35 -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)
|
|
|
|
|
|
|
|
facts := make(map[string]string)
|
|
|
|
facts["fact_name"] = "fact_value"
|
|
|
|
config["facter"] = facts
|
|
|
|
|
|
|
|
p = new(Provisioner)
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the default facts are present
|
|
|
|
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)
|
|
|
|
}
|
2016-12-14 23:47:35 -05:00
|
|
|
if p.config.Facter == nil {
|
|
|
|
t.Fatalf("err: Default facts are not set in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
|
|
|
|
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!")
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 23:35:13 -05:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_stagingDir(t *testing.T) {
|
2018-04-25 14:56:16 -04:00
|
|
|
config, tempfile := testConfig()
|
|
|
|
defer os.Remove(tempfile.Name())
|
|
|
|
defer tempfile.Close()
|
2016-12-27 23:35:13 -05:00
|
|
|
|
|
|
|
delete(config, "staging_dir")
|
|
|
|
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-server" {
|
|
|
|
t.Fatalf("err: Default staging_dir is not set in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure default staging directory can be overridden
|
|
|
|
config["staging_dir"] = "/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_dir is not set correctly in the Puppet provisioner!")
|
|
|
|
}
|
|
|
|
}
|