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
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-03-13 11:17:40 -04:00
|
|
|
"os"
|
2020-12-04 18:14:45 -05:00
|
|
|
"os/exec"
|
2020-03-13 11:17:40 -04:00
|
|
|
"path/filepath"
|
2020-12-04 18:14:45 -05:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
2020-03-13 11:17:40 -04:00
|
|
|
"testing"
|
2020-03-13 12:17:42 -04:00
|
|
|
|
2020-12-03 16:56:41 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/provisioneracc"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/acctest/testutils"
|
2020-03-13 11:17:40 -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 12:17:09 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func loadFile(templateFragmentPath string) (string, error) {
|
|
|
|
dir := fixtureDir()
|
|
|
|
fragmentAbsPath := filepath.Join(dir, templateFragmentPath)
|
|
|
|
fragmentFile, err := os.Open(fragmentAbsPath)
|
2020-04-03 12:17:09 -04:00
|
|
|
if err != nil {
|
2020-12-04 18:14:45 -05:00
|
|
|
return "", fmt.Errorf("Unable find %s", fragmentAbsPath)
|
2020-04-03 12:17:09 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
defer fragmentFile.Close()
|
2020-04-03 12:17:09 -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 12:17:09 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
|
|
|
|
return string(fragmentString), nil
|
2020-04-03 12:17:09 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func IsCompatible(builder string, vmOS string) bool {
|
2020-04-03 12:17:09 -04:00
|
|
|
return vmOS == "linux"
|
|
|
|
}
|
2020-03-13 12:50:05 -04:00
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func TestAccShellProvisioner_basic(t *testing.T) {
|
|
|
|
templateString, err := loadFile("shell-provisioner.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't load test fixture; %s", err.Error())
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
testCase := &provisioneracc.ProvisionerTestCase{
|
|
|
|
IsCompatible: IsCompatible,
|
|
|
|
Name: "shell-provisioner-basic",
|
|
|
|
Teardown: func() error {
|
|
|
|
testutils.CleanupFiles("test-fixtures/provisioner.shell.txt")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Template: templateString,
|
|
|
|
Type: "shell",
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filecontents, err := loadFile("provisioner.shell.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
re := regexp.MustCompile(`build ID is .* and build UUID is [[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}`)
|
|
|
|
if !re.MatchString(filecontents) {
|
|
|
|
return fmt.Errorf("Bad file contents \"%s\"", filecontents)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(testCase, t)
|
2020-03-13 11:17:40 -04:00
|
|
|
}
|