2020-03-13 12:10:51 -04:00
|
|
|
package shell_test
|
2020-03-13 11:17:40 -04:00
|
|
|
|
|
|
|
import (
|
2020-04-03 12:17:09 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-03-13 11:17:40 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-03-13 12:17:42 -04:00
|
|
|
|
2020-04-06 10:14:14 -04:00
|
|
|
"github.com/hashicorp/packer/helper/tests/acc"
|
|
|
|
"github.com/hashicorp/packer/provisioner/file"
|
|
|
|
"github.com/hashicorp/packer/provisioner/shell"
|
|
|
|
|
2020-04-03 12:17:09 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-20 13:21:29 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-04-03 12:17:09 -04:00
|
|
|
|
2020-03-13 12:17:42 -04:00
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/packer/command"
|
2020-03-13 12:52:33 -04:00
|
|
|
testshelper "github.com/hashicorp/packer/helper/tests"
|
2020-03-13 11:17:40 -04:00
|
|
|
)
|
|
|
|
|
2020-04-03 12:17:09 -04:00
|
|
|
func TestShellProvisioner(t *testing.T) {
|
2020-04-06 15:18:53 -04:00
|
|
|
acc.TestProvisionersPreCheck("shell", t)
|
2020-04-03 12:17:09 -04:00
|
|
|
acc.TestProvisionersAgainstBuilders(new(ShellProvisionerAccTest), t)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShellProvisionerAccTest struct{}
|
|
|
|
|
|
|
|
func (s *ShellProvisionerAccTest) GetName() string {
|
|
|
|
return "shell"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShellProvisionerAccTest) GetConfig() (string, error) {
|
|
|
|
filePath := filepath.Join("./test-fixtures", "shell-provisioner.txt")
|
|
|
|
config, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Expected to find %s", filePath)
|
|
|
|
}
|
|
|
|
defer config.Close()
|
|
|
|
|
|
|
|
file, err := ioutil.ReadAll(config)
|
2020-04-06 17:30:19 -04:00
|
|
|
return string(file), err
|
2020-04-03 12:17:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShellProvisionerAccTest) GetProvisionerStore() packer.MapOfProvisioner {
|
|
|
|
return packer.MapOfProvisioner{
|
2020-12-01 17:25:14 -05:00
|
|
|
"shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil },
|
|
|
|
"file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil },
|
2020-04-03 12:17:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ShellProvisionerAccTest) IsCompatible(builder string, vmOS string) bool {
|
|
|
|
return vmOS == "linux"
|
|
|
|
}
|
2020-03-13 12:50:05 -04:00
|
|
|
|
2020-04-03 12:17:09 -04:00
|
|
|
func (s *ShellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string) error {
|
|
|
|
UUID := os.Getenv("PACKER_RUN_UUID")
|
|
|
|
if UUID == "" {
|
|
|
|
UUID, _ = uuid.GenerateUUID()
|
|
|
|
os.Setenv("PACKER_RUN_UUID", UUID)
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
file := "provisioner.shell." + UUID + ".txt"
|
2020-03-13 11:35:31 -04:00
|
|
|
defer testshelper.CleanupFiles(file)
|
2020-03-13 11:17:40 -04:00
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
2020-11-20 13:21:29 -05:00
|
|
|
ui := c.Meta.Ui.(*packersdk.BasicUi)
|
2020-04-03 12:17:09 -04:00
|
|
|
out := ui.Writer.(*bytes.Buffer)
|
|
|
|
err := ui.ErrorWriter.(*bytes.Buffer)
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Bad exit code.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
|
|
|
out.String(),
|
|
|
|
err.String())
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|
|
|
|
|
2020-03-13 11:35:31 -04:00
|
|
|
if !testshelper.FileExists(file) {
|
2020-04-03 12:17:09 -04:00
|
|
|
return fmt.Errorf("Expected to find %s", file)
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|
2020-04-03 12:17:09 -04:00
|
|
|
return nil
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|