2015-03-10 00:11:57 -04:00
|
|
|
package ansible
|
|
|
|
|
|
|
|
import (
|
2016-07-06 20:02:10 -04:00
|
|
|
"bytes"
|
2015-03-10 00:11:57 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-03-01 13:35:48 -05:00
|
|
|
"path"
|
2017-03-21 23:26:41 -04:00
|
|
|
"strings"
|
2015-03-10 00:11:57 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-03-10 00:11:57 -04:00
|
|
|
)
|
|
|
|
|
2016-03-01 13:35:48 -05:00
|
|
|
// Be sure to remove the Ansible stub file in each test with:
|
|
|
|
// defer os.Remove(config["command"].(string))
|
2018-08-03 13:45:57 -04:00
|
|
|
func testConfig(t *testing.T) map[interface{}]interface{} {
|
|
|
|
m := make((map[interface{}]interface{}))
|
2016-03-01 13:35:48 -05:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
ansible_stub := path.Join(wd, "packer-ansible-stub.sh")
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(ansible_stub, []byte("#!/usr/bin/env bash\necho ansible 1.6.0"), 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
m["command"] = ansible_stub
|
|
|
|
|
2015-03-10 00:11:57 -04:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-03-01 13:35:48 -05:00
|
|
|
config := testConfig(t)
|
|
|
|
defer os.Remove(config["command"].(string))
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
hostkey_file, err := ioutil.TempFile("", "hostkey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostkey_file.Name())
|
|
|
|
|
|
|
|
publickey_file, err := ioutil.TempFile("", "publickey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(publickey_file.Name())
|
|
|
|
|
|
|
|
playbook_file, err := ioutil.TempFile("", "playbook")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
2015-03-10 01:25:45 -04:00
|
|
|
config["ssh_host_key_file"] = hostkey_file.Name()
|
2015-03-10 00:11:57 -04:00
|
|
|
config["ssh_authorized_key_file"] = publickey_file.Name()
|
|
|
|
config["playbook_file"] = playbook_file.Name()
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-01-03 02:33:32 -05:00
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
|
|
|
err = os.Unsetenv("USER")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-03-10 00:11:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_PlaybookFile(t *testing.T) {
|
|
|
|
var p Provisioner
|
2016-03-01 13:35:48 -05:00
|
|
|
config := testConfig(t)
|
|
|
|
defer os.Remove(config["command"].(string))
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
hostkey_file, err := ioutil.TempFile("", "hostkey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostkey_file.Name())
|
|
|
|
|
|
|
|
publickey_file, err := ioutil.TempFile("", "publickey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(publickey_file.Name())
|
|
|
|
|
2015-03-10 01:25:45 -04:00
|
|
|
config["ssh_host_key_file"] = hostkey_file.Name()
|
2015-03-10 00:11:57 -04:00
|
|
|
config["ssh_authorized_key_file"] = publickey_file.Name()
|
|
|
|
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
playbook_file, err := ioutil.TempFile("", "playbook")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
|
|
|
config["playbook_file"] = playbook_file.Name()
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_HostKeyFile(t *testing.T) {
|
|
|
|
var p Provisioner
|
2016-03-01 13:35:48 -05:00
|
|
|
config := testConfig(t)
|
|
|
|
defer os.Remove(config["command"].(string))
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
publickey_file, err := ioutil.TempFile("", "publickey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(publickey_file.Name())
|
|
|
|
|
|
|
|
playbook_file, err := ioutil.TempFile("", "playbook")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
|
|
|
filename := make([]byte, 10)
|
|
|
|
n, err := io.ReadFull(rand.Reader, filename)
|
|
|
|
if n != len(filename) || err != nil {
|
|
|
|
t.Fatal("could not create random file name")
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:25:45 -04:00
|
|
|
config["ssh_host_key_file"] = fmt.Sprintf("%x", filename)
|
2015-03-10 00:11:57 -04:00
|
|
|
config["ssh_authorized_key_file"] = publickey_file.Name()
|
|
|
|
config["playbook_file"] = playbook_file.Name()
|
|
|
|
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
2015-03-10 01:25:45 -04:00
|
|
|
t.Fatal("should error if ssh_host_key_file does not exist")
|
2015-03-10 00:11:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hostkey_file, err := ioutil.TempFile("", "hostkey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostkey_file.Name())
|
|
|
|
|
2015-03-10 01:25:45 -04:00
|
|
|
config["ssh_host_key_file"] = hostkey_file.Name()
|
2015-03-10 00:11:57 -04:00
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_AuthorizedKeyFile(t *testing.T) {
|
|
|
|
var p Provisioner
|
2016-03-01 13:35:48 -05:00
|
|
|
config := testConfig(t)
|
|
|
|
defer os.Remove(config["command"].(string))
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
hostkey_file, err := ioutil.TempFile("", "hostkey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostkey_file.Name())
|
|
|
|
|
|
|
|
playbook_file, err := ioutil.TempFile("", "playbook")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
2016-02-06 21:22:38 -05:00
|
|
|
filename := make([]byte, 10)
|
|
|
|
n, err := io.ReadFull(rand.Reader, filename)
|
|
|
|
if n != len(filename) || err != nil {
|
|
|
|
t.Fatal("could not create random file name")
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:25:45 -04:00
|
|
|
config["ssh_host_key_file"] = hostkey_file.Name()
|
2015-03-10 00:11:57 -04:00
|
|
|
config["playbook_file"] = playbook_file.Name()
|
2016-02-06 21:22:38 -05:00
|
|
|
config["ssh_authorized_key_file"] = fmt.Sprintf("%x", filename)
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
2016-02-06 21:22:38 -05:00
|
|
|
t.Errorf("should error if ssh_authorized_key_file does not exist")
|
2015-03-10 00:11:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
publickey_file, err := ioutil.TempFile("", "publickey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(publickey_file.Name())
|
|
|
|
|
|
|
|
config["ssh_authorized_key_file"] = publickey_file.Name()
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
2016-02-06 21:22:38 -05:00
|
|
|
t.Errorf("err: %s", err)
|
2015-03-10 00:11:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_LocalPort(t *testing.T) {
|
|
|
|
var p Provisioner
|
2016-03-01 13:35:48 -05:00
|
|
|
config := testConfig(t)
|
|
|
|
defer os.Remove(config["command"].(string))
|
2015-03-10 00:11:57 -04:00
|
|
|
|
|
|
|
hostkey_file, err := ioutil.TempFile("", "hostkey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostkey_file.Name())
|
|
|
|
|
|
|
|
publickey_file, err := ioutil.TempFile("", "publickey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(publickey_file.Name())
|
|
|
|
|
|
|
|
playbook_file, err := ioutil.TempFile("", "playbook")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
2015-03-10 01:25:45 -04:00
|
|
|
config["ssh_host_key_file"] = hostkey_file.Name()
|
2015-03-10 00:11:57 -04:00
|
|
|
config["ssh_authorized_key_file"] = publickey_file.Name()
|
|
|
|
config["playbook_file"] = playbook_file.Name()
|
|
|
|
|
|
|
|
config["local_port"] = "65537"
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["local_port"] = "22222"
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 20:02:10 -04:00
|
|
|
|
2017-04-04 23:49:24 -04:00
|
|
|
func TestProvisionerPrepare_InventoryDirectory(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig(t)
|
|
|
|
defer os.Remove(config["command"].(string))
|
|
|
|
|
|
|
|
hostkey_file, err := ioutil.TempFile("", "hostkey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostkey_file.Name())
|
|
|
|
|
|
|
|
publickey_file, err := ioutil.TempFile("", "publickey")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(publickey_file.Name())
|
|
|
|
|
|
|
|
playbook_file, err := ioutil.TempFile("", "playbook")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(playbook_file.Name())
|
|
|
|
|
|
|
|
config["ssh_host_key_file"] = hostkey_file.Name()
|
|
|
|
config["ssh_authorized_key_file"] = publickey_file.Name()
|
|
|
|
config["playbook_file"] = playbook_file.Name()
|
|
|
|
|
|
|
|
config["inventory_directory"] = "doesnotexist"
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("should error if inventory_directory does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
inventoryDirectory, err := ioutil.TempDir("", "some_inventory_dir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(inventoryDirectory)
|
|
|
|
|
|
|
|
config["inventory_directory"] = inventoryDirectory
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:02:10 -04:00
|
|
|
func TestAnsibleGetVersion(t *testing.T) {
|
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 and it requires Ansible to be installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var p Provisioner
|
|
|
|
p.config.Command = "ansible-playbook"
|
2016-07-07 15:01:23 -04:00
|
|
|
err := p.getVersion()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-07-06 20:02:10 -04:00
|
|
|
}
|
|
|
|
|
2017-03-21 23:26:41 -04:00
|
|
|
func TestAnsibleGetVersionError(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
p.config.Command = "./test-fixtures/exit1"
|
|
|
|
err := p.getVersion()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should return error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "./test-fixtures/exit1 --version") {
|
|
|
|
t.Fatal("Error message should include command name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:02:10 -04:00
|
|
|
func TestAnsibleLongMessages(t *testing.T) {
|
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 and it requires Ansible to be installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var p Provisioner
|
|
|
|
p.config.Command = "ansible-playbook"
|
|
|
|
p.config.PlaybookFile = "./test-fixtures/long-debug-message.yml"
|
2016-07-07 15:01:23 -04:00
|
|
|
err := p.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-07-06 20:02:10 -04:00
|
|
|
|
|
|
|
comm := &packer.MockCommunicator{}
|
|
|
|
ui := &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
|
2016-07-07 15:01:23 -04:00
|
|
|
err = p.Provision(ui, comm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-07-06 20:02:10 -04:00
|
|
|
}
|