2014-01-24 20:06:55 -05:00
|
|
|
package chefclient
|
|
|
|
|
|
|
|
import (
|
2014-11-26 18:13:09 -05:00
|
|
|
"bytes"
|
2014-01-24 20:06:55 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-11-26 18:13:09 -05:00
|
|
|
"strings"
|
2014-01-24 20:06:55 -05:00
|
|
|
"testing"
|
2014-02-24 11:53:56 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-01-24 20:06:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
2014-02-24 12:45:04 -05:00
|
|
|
return map[string]interface{}{
|
|
|
|
"server_url": "foo",
|
|
|
|
}
|
2014-01-24 20:06:55 -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-24 20:06:55 -05:00
|
|
|
t.Fatalf("must be a Provisioner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 09:30:18 -04:00
|
|
|
func TestProvisionerPrepare_chefEnvironment(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
config := testConfig()
|
|
|
|
config["chef_environment"] = "some-env"
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ChefEnvironment != "some-env" {
|
|
|
|
t.Fatalf("unexpected: %#v", p.config.ChefEnvironment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:06:55 -05:00
|
|
|
func TestProvisionerPrepare_configTemplate(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
// Test no config template
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "config_template")
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a file
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
config = testConfig()
|
|
|
|
config["config_template"] = tf.Name()
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a directory
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
config = testConfig()
|
|
|
|
config["config_template"] = td
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have err")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 11:53:56 -05:00
|
|
|
func TestProvisionerPrepare_commands(t *testing.T) {
|
|
|
|
commands := []string{
|
|
|
|
"execute_command",
|
|
|
|
"install_command",
|
2016-06-14 14:34:25 -04:00
|
|
|
"knife_command",
|
2014-02-24 11:53:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
// Test not set
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, command)
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test invalid template
|
|
|
|
config = testConfig()
|
|
|
|
config[command] = "{{if NOPE}}"
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good template
|
|
|
|
config = testConfig()
|
|
|
|
config[command] = "{{.Foo}}"
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-01-24 20:06:55 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-24 12:45:04 -05:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_serverUrl(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
// Test not set
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "server_url")
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config = testConfig()
|
|
|
|
config["server_url"] = "foo"
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 11:19:22 -04:00
|
|
|
func TestProvisionerPrepare_chefLicense(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
// Test not set
|
|
|
|
config := testConfig()
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ChefLicense != "accept-silent" {
|
|
|
|
t.Fatalf("unexpected: %#v", p.config.ChefLicense)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set
|
|
|
|
config = testConfig()
|
|
|
|
config["chef_license"] = "accept"
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ChefLicense != "accept" {
|
|
|
|
t.Fatalf("unexpected: %#v", p.config.ChefLicense)
|
|
|
|
}
|
2019-05-21 09:26:07 -04:00
|
|
|
|
|
|
|
// Test set skipInstall true
|
|
|
|
config = testConfig()
|
|
|
|
config["skip_install"] = true
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ChefLicense != "" {
|
|
|
|
t.Fatalf("unexpected: %#v", "empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test set installCommand true
|
|
|
|
config = testConfig()
|
|
|
|
config["install_command"] = "install chef"
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.ChefLicense != "" {
|
|
|
|
t.Fatalf("unexpected: %#v", "empty string")
|
|
|
|
}
|
2019-05-17 11:19:22 -04:00
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-21 18:06:24 -04:00
|
|
|
func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
// Test no config template
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "encrypted_data_bag_secret_path")
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a file
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
config = testConfig()
|
|
|
|
config["encrypted_data_bag_secret_path"] = tf.Name()
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a directory
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
config = testConfig()
|
|
|
|
config["encrypted_data_bag_secret_path"] = td
|
|
|
|
p = Provisioner{}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have err")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 18:13:09 -05:00
|
|
|
func TestProvisioner_createDir(t *testing.T) {
|
2015-08-20 16:27:51 -04:00
|
|
|
for _, sudo := range []bool{true, false} {
|
|
|
|
config := testConfig()
|
|
|
|
config["prevent_sudo"] = !sudo
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
p := &Provisioner{}
|
2020-11-20 13:21:29 -05:00
|
|
|
comm := &packersdk.MockCommunicator{}
|
|
|
|
ui := &packersdk.BasicUi{
|
2015-08-20 16:27:51 -04:00
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
if err := p.createDir(ui, comm, "/tmp/foo"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sudo && strings.HasPrefix(comm.StartCmd.Command, "sudo") {
|
|
|
|
t.Fatalf("createDir should not use sudo, got: \"%s\"", comm.StartCmd.Command)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
if sudo && !strings.HasPrefix(comm.StartCmd.Command, "sudo") {
|
|
|
|
t.Fatalf("createDir should use sudo, got: \"%s\"", comm.StartCmd.Command)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_removeDir(t *testing.T) {
|
2015-08-20 16:27:51 -04:00
|
|
|
for _, sudo := range []bool{true, false} {
|
|
|
|
config := testConfig()
|
|
|
|
config["prevent_sudo"] = !sudo
|
2016-02-08 20:34:06 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
p := &Provisioner{}
|
2020-11-20 13:21:29 -05:00
|
|
|
comm := &packersdk.MockCommunicator{}
|
|
|
|
ui := &packersdk.BasicUi{
|
2015-08-20 16:27:51 -04:00
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
if err := p.removeDir(ui, comm, "/tmp/foo"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
if !sudo && strings.HasPrefix(comm.StartCmd.Command, "sudo") {
|
|
|
|
t.Fatalf("removeDir should not use sudo, got: \"%s\"", comm.StartCmd.Command)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
|
2015-08-20 16:27:51 -04:00
|
|
|
if sudo && !strings.HasPrefix(comm.StartCmd.Command, "sudo") {
|
|
|
|
t.Fatalf("removeDir should use sudo, got: \"%s\"", comm.StartCmd.Command)
|
|
|
|
}
|
2014-11-26 18:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 16:08:25 -05:00
|
|
|
|
|
|
|
func TestProvisionerPrepare_policy(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
var policyTests = []struct {
|
|
|
|
name string
|
|
|
|
group string
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{"", "", true},
|
|
|
|
{"a", "b", true},
|
|
|
|
{"a", "", false},
|
|
|
|
{"", "a", false},
|
|
|
|
}
|
|
|
|
for _, tt := range policyTests {
|
|
|
|
config := testConfig()
|
|
|
|
config["policy_name"] = tt.name
|
|
|
|
config["policy_group"] = tt.group
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if (err == nil) != tt.success {
|
|
|
|
t.Fatalf("wasn't expecting %+v to fail: %s", tt, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|