2020-04-16 05:58:54 -04:00
|
|
|
package shell_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-12-04 18:14:45 -05:00
|
|
|
"os/exec"
|
2020-04-16 05:58:54 -04:00
|
|
|
"path/filepath"
|
2020-12-04 18:14:45 -05:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2020-04-16 05:58:54 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/acctest/provisioneracc"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/acctest/testutils"
|
2020-04-16 05:58:54 -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-16 05:58:54 -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-16 05:58:54 -04:00
|
|
|
if err != nil {
|
2020-12-04 18:14:45 -05:00
|
|
|
return "", fmt.Errorf("Unable find %s", fragmentAbsPath)
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
defer fragmentFile.Close()
|
2020-04-16 05:58:54 -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-16 05:58:54 -04:00
|
|
|
}
|
2020-12-04 18:14:45 -05:00
|
|
|
|
|
|
|
return string(fragmentString), nil
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func IsCompatible(builder string, vmOS string) bool {
|
2020-04-16 05:58:54 -04:00
|
|
|
return vmOS == "linux"
|
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
func TestAccShellProvisioner_basic(t *testing.T) {
|
|
|
|
templateString, err := loadFile("shell-local-provisioner.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't load test fixture; %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
testCase := &provisioneracc.ProvisionerTestCase{
|
|
|
|
IsCompatible: IsCompatible,
|
|
|
|
Name: "shell-local-provisioner-basic",
|
|
|
|
Teardown: func() error {
|
|
|
|
testutils.CleanupFiles("test-fixtures/file.txt")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Template: templateString,
|
|
|
|
Type: "shell-local",
|
|
|
|
Check: func(buildcommand *exec.Cmd, logfile string) error {
|
|
|
|
if buildcommand.ProcessState != nil {
|
|
|
|
if buildcommand.ProcessState.ExitCode() != 0 {
|
2020-12-08 12:59:04 -05:00
|
|
|
return fmt.Errorf("Bad exit code. Logfile: %s\n", logfile)
|
2020-12-04 18:14:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filecontents, err := loadFile("file.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.Contains(filecontents, "hello") {
|
|
|
|
return fmt.Errorf("file contents were wrong: %s", filecontents)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:14:45 -05:00
|
|
|
provisioneracc.TestProvisionersAgainstBuilders(testCase, t)
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|