Test for ansible-local playbook_files with mocked Communicator.

This commit is contained in:
localghost 2017-07-03 23:44:10 +02:00 committed by Zbigniew Kostrzewa
parent 41d0adfbdd
commit f682045345
3 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package ansiblelocal
import (
"github.com/hashicorp/packer/packer"
"io"
"os"
)
type communicatorMock struct {
startCommand []string
uploadDestination []string
}
func (c *communicatorMock) Start(cmd *packer.RemoteCmd) error {
c.startCommand = append(c.startCommand, cmd.Command)
cmd.SetExited(0)
return nil
}
func (c *communicatorMock) Upload(dst string, _ io.Reader, _ *os.FileInfo) error {
c.uploadDestination = append(c.uploadDestination, dst)
return nil
}
func (c *communicatorMock) UploadDir(dst, src string, exclude []string) error {
return nil
}
func (c *communicatorMock) Download(src string, dst io.Writer) error {
return nil
}
func (c *communicatorMock) DownloadDir(src, dst string, exclude []string) error {
return nil
}
func (c *communicatorMock) verify() {
}

View File

@ -7,6 +7,7 @@ import (
"strings"
"testing"
"fmt"
"github.com/hashicorp/packer/packer"
)
@ -15,6 +16,37 @@ func testConfig() map[string]interface{} {
return m
}
func createTempFile() string {
file, err := ioutil.TempFile("", "")
if err != nil {
panic(fmt.Sprintf("err: %s", err))
}
return file.Name()
}
func createTempFiles(numFiles int) []string {
files := make([]string, 0, numFiles)
defer func() {
// Cleanup the files if not all were created.
if len(files) < numFiles {
for _, file := range files {
os.Remove(file)
}
}
}()
for i := 0; i < numFiles; i++ {
files = append(files, createTempFile())
}
return files
}
func removeFiles(files ...string) {
for _, file := range files {
os.Remove(file)
}
}
func TestProvisioner_Impl(t *testing.T) {
var raw interface{}
raw = &Provisioner{}
@ -117,6 +149,58 @@ func TestProvisionerPrepare_PlaybookFiles(t *testing.T) {
}
}
func assertPlaybooksExecuted(comm *communicatorMock, playbooks []string) {
cmdIndex := 0
for _, playbook := range playbooks {
for ; cmdIndex < len(comm.startCommand); cmdIndex++ {
cmd := comm.startCommand[cmdIndex]
if strings.Contains(cmd, "ansible-playbook") && strings.Contains(cmd, playbook) {
break
}
}
if cmdIndex == len(comm.startCommand) {
panic(fmt.Sprintf("Playbook %s was not executed", playbook))
}
}
}
func assertPlaybooksUploaded(comm *communicatorMock, playbooks []string) {
uploadIndex := 0
for _, playbook := range playbooks {
for ; uploadIndex < len(comm.uploadDestination); uploadIndex++ {
dest := comm.uploadDestination[uploadIndex]
if strings.HasSuffix(dest, playbook) {
break
}
}
if uploadIndex == len(comm.uploadDestination) {
panic(fmt.Sprintf("Playbook %s was not uploaded", playbook))
}
}
}
func TestProvisionerProvision_PlaybookFiles(t *testing.T) {
var p Provisioner
config := testConfig()
playbooks := createTempFiles(3)
defer removeFiles(playbooks...)
config["playbook_files"] = playbooks
err := p.Prepare(config)
if err != nil {
t.Fatalf("err: %s", err)
}
comm := &communicatorMock{}
if err := p.Provision(&uiStub{}, comm); err != nil {
t.Fatalf("err: %s", err)
}
assertPlaybooksUploaded(comm, playbooks)
assertPlaybooksExecuted(comm, playbooks)
}
func TestProvisionerPrepare_InventoryFile(t *testing.T) {
var p Provisioner
config := testConfig()

View File

@ -0,0 +1,15 @@
package ansiblelocal
type uiStub struct{}
func (su *uiStub) Ask(string) (string, error) {
return "", nil
}
func (su *uiStub) Error(string) {}
func (su *uiStub) Machine(string, ...string) {}
func (su *uiStub) Message(string) {}
func (su *uiStub) Say(msg string) {}