2020-04-03 14:53:32 -04:00
|
|
|
package powershell_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/packer/command"
|
2020-12-03 16:56:41 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc"
|
2020-11-20 13:21:29 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-04-07 15:03:58 -04:00
|
|
|
"github.com/hashicorp/packer/provisioner/powershell"
|
|
|
|
windowsshellprovisioner "github.com/hashicorp/packer/provisioner/windows-shell"
|
2020-04-03 14:53:32 -04:00
|
|
|
)
|
|
|
|
|
2020-04-06 15:18:53 -04:00
|
|
|
const TestProvisionerName = "powershell"
|
|
|
|
|
2020-04-07 15:03:58 -04:00
|
|
|
func TestAccPowershellProvisioner_basic(t *testing.T) {
|
2020-12-03 16:56:41 -05:00
|
|
|
provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t)
|
2020-04-07 15:03:58 -04:00
|
|
|
|
|
|
|
testProvisioner := PowershellProvisionerAccTest{"powershell-provisioner-cleanup.txt"}
|
2020-12-03 16:56:41 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t)
|
2020-04-07 15:03:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccPowershellProvisioner_Inline(t *testing.T) {
|
2020-12-03 16:56:41 -05:00
|
|
|
provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
|
|
|
|
testProvisioner := PowershellProvisionerAccTest{"powershell-inline-provisioner.txt"}
|
2020-12-03 16:56:41 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 15:03:58 -04:00
|
|
|
func TestAccPowershellProvisioner_Script(t *testing.T) {
|
2020-12-03 16:56:41 -05:00
|
|
|
provisioneracc.TestProvisionersPreCheck(TestProvisionerName, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
|
|
|
|
testProvisioner := PowershellProvisionerAccTest{"powershell-script-provisioner.txt"}
|
2020-12-03 16:56:41 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(&testProvisioner, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type PowershellProvisionerAccTest struct {
|
|
|
|
ConfigName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PowershellProvisionerAccTest) GetName() string {
|
2020-04-06 15:18:53 -04:00
|
|
|
return TestProvisionerName
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PowershellProvisionerAccTest) GetConfig() (string, error) {
|
|
|
|
filePath := filepath.Join("./test-fixtures", s.ConfigName)
|
|
|
|
config, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
2020-04-08 13:04:08 -04:00
|
|
|
return "", fmt.Errorf("os.Open:%v", err)
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
defer config.Close()
|
|
|
|
|
|
|
|
file, err := ioutil.ReadAll(config)
|
2020-04-08 13:04:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("ioutil.ReadAll:%v", err)
|
|
|
|
}
|
2020-04-03 14:53:32 -04:00
|
|
|
return string(file), nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:12:47 -05:00
|
|
|
func (s *PowershellProvisionerAccTest) GetProvisionerStore() packersdk.MapOfProvisioner {
|
|
|
|
return packersdk.MapOfProvisioner{
|
2020-12-01 17:25:14 -05:00
|
|
|
TestProvisionerName: func() (packersdk.Provisioner, error) { return &powershell.Provisioner{}, nil },
|
|
|
|
"windows-shell": func() (packersdk.Provisioner, error) { return &windowsshellprovisioner.Provisioner{}, nil },
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PowershellProvisionerAccTest) IsCompatible(builder string, vmOS string) bool {
|
|
|
|
return vmOS == "windows"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PowershellProvisionerAccTest) RunTest(c *command.BuildCommand, args []string) error {
|
|
|
|
UUID := os.Getenv("PACKER_RUN_UUID")
|
|
|
|
if UUID == "" {
|
|
|
|
UUID, _ = uuid.GenerateUUID()
|
|
|
|
os.Setenv("PACKER_RUN_UUID", UUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
2020-11-20 13:21:29 -05:00
|
|
|
ui := c.Meta.Ui.(*packersdk.BasicUi)
|
2020-04-03 14:53:32 -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())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|