2020-04-03 14:53:32 -04:00
|
|
|
package powershell_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-12-04 18:14:45 -05:00
|
|
|
"os/exec"
|
2020-04-03 14:53:32 -04:00
|
|
|
"path/filepath"
|
2020-12-04 18:14:45 -05:00
|
|
|
"runtime"
|
2020-04-03 14:53:32 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-12-03 16:56:41 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc"
|
2020-04-03 14:53:32 -04:00
|
|
|
)
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
const TestProvisionerType = "powershell"
|
2020-04-06 15:18:53 -04:00
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func powershellIsCompatible(builder string, vmOS string) bool {
|
|
|
|
return vmOS == "windows"
|
2020-04-07 15:03:58 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func fixtureDir() string {
|
|
|
|
_, file, _, _ := runtime.Caller(0)
|
|
|
|
return filepath.Join(filepath.Dir(file), "test-fixtures")
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func LoadProvisionerFragment(templateFragmentPath string) (string, error) {
|
|
|
|
dir := fixtureDir()
|
|
|
|
fragmentAbsPath := filepath.Join(dir, templateFragmentPath)
|
|
|
|
fragmentFile, err := os.Open(fragmentAbsPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable find %s", fragmentAbsPath)
|
|
|
|
}
|
|
|
|
defer fragmentFile.Close()
|
2020-04-03 14:53:32 -04:00
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
fragmentString, err := ioutil.ReadAll(fragmentFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable to read %s", fragmentAbsPath)
|
|
|
|
}
|
2020-04-03 14:53:32 -04:00
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
return string(fragmentString), nil
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func TestAccPowershellProvisioner_basic(t *testing.T) {
|
|
|
|
templateString, err := LoadProvisionerFragment("powershell-provisioner-cleanup.txt")
|
2020-04-03 14:53:32 -04:00
|
|
|
if err != nil {
|
2020-12-04 18:14:45 -05:00
|
|
|
t.Fatalf("Couldn't load test fixture; %s", err.Error())
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
testCase := &provisioneracc.ProvisionerTestCase{
|
|
|
|
IsCompatible: powershellIsCompatible,
|
|
|
|
Name: "powershell-provisioner-cleanup",
|
|
|
|
Template: templateString,
|
|
|
|
Type: TestProvisionerType,
|
|
|
|
Check: func(buildcommand *exec.Cmd, logfile string) error {
|
|
|
|
if buildcommand.ProcessState != nil {
|
|
|
|
if buildcommand.ProcessState.ExitCode() != 0 {
|
|
|
|
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2020-04-08 13:04:08 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
|
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(testCase, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func TestAccPowershellProvisioner_Inline(t *testing.T) {
|
|
|
|
templateString, err := LoadProvisionerFragment("powershell-inline-provisioner.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't load test fixture; %s", err.Error())
|
|
|
|
}
|
|
|
|
testCase := &provisioneracc.ProvisionerTestCase{
|
|
|
|
IsCompatible: powershellIsCompatible,
|
|
|
|
Name: "powershell-provisioner-inline",
|
|
|
|
Template: templateString,
|
|
|
|
Type: TestProvisionerType,
|
|
|
|
Check: func(buildcommand *exec.Cmd, logfile string) error {
|
|
|
|
if buildcommand.ProcessState != nil {
|
|
|
|
if buildcommand.ProcessState.ExitCode() != 0 {
|
|
|
|
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(testCase, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func TestAccPowershellProvisioner_Script(t *testing.T) {
|
|
|
|
templateString, err := LoadProvisionerFragment("powershell-script-provisioner.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't load test fixture; %s", err.Error())
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
testCase := &provisioneracc.ProvisionerTestCase{
|
|
|
|
IsCompatible: powershellIsCompatible,
|
|
|
|
Name: "powershell-provisioner-script",
|
|
|
|
Template: templateString,
|
|
|
|
Type: TestProvisionerType,
|
|
|
|
Check: func(buildcommand *exec.Cmd, logfile string) error {
|
|
|
|
if buildcommand.ProcessState != nil {
|
|
|
|
if buildcommand.ProcessState.ExitCode() != 0 {
|
|
|
|
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(testCase, t)
|
2020-04-03 14:53:32 -04:00
|
|
|
}
|