2016-04-24 23:22:13 -04:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2017-02-27 16:34:53 -05:00
|
|
|
"bytes"
|
2020-12-02 19:24:48 -05:00
|
|
|
"encoding/json"
|
2016-04-24 23:22:13 -04:00
|
|
|
"fmt"
|
2017-02-27 16:34:53 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-24 23:22:13 -04:00
|
|
|
"runtime"
|
2017-02-27 16:34:53 -05:00
|
|
|
"strings"
|
2018-04-18 17:53:59 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
builderT "github.com/hashicorp/packer-plugin-sdk/acctest"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/tmp"
|
2016-04-24 23:22:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const vmxTestTemplate string = `{"builders":[{%s}],"provisioners":[{%s}]}`
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
func createFloppyOutput(prefix string) (string, map[string]string, error) {
|
|
|
|
f, err := tmp.File(prefix)
|
2017-02-27 16:34:53 -05:00
|
|
|
if err != nil {
|
2020-12-02 19:24:48 -05:00
|
|
|
return "", map[string]string{}, fmt.Errorf("unable to create temp file")
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2016-04-24 23:22:13 -04:00
|
|
|
f.Close()
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
output := f.Name()
|
2016-04-24 23:22:13 -04:00
|
|
|
outputFile := strings.Replace(output, "\\", "\\\\", -1)
|
2020-12-02 19:24:48 -05:00
|
|
|
vmxData := map[string]string{
|
|
|
|
"floppy0.present": "TRUE",
|
|
|
|
"floppy0.fileType": "file",
|
|
|
|
"floppy0.clientDevice": "FALSE",
|
|
|
|
"floppy0.fileName": outputFile,
|
|
|
|
"floppy0.startConnected": "TRUE",
|
|
|
|
}
|
|
|
|
return output, vmxData, nil
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
func readFloppyOutput(path string) (string, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable to open file %s", path)
|
|
|
|
}
|
2016-04-24 23:22:13 -04:00
|
|
|
defer f.Close()
|
2017-02-27 16:34:53 -05:00
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Unable to read file: %s", err)
|
|
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return string(data[:bytes.IndexByte(data, 0)]), nil
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
// RenderConfig helps create dynamic packer template configs for parsing by
|
|
|
|
// builderT without having to write the config to a file.
|
|
|
|
func RenderConfig(builderConfig map[string]interface{}, provisionerConfig map[string]string) string {
|
|
|
|
// set up basic build template
|
|
|
|
t := map[string][]map[string]interface{}{
|
|
|
|
"builders": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "test",
|
|
|
|
"iso_url": "https://archive.org/download/ut-ttylinux-i686-12.6/ut-ttylinux-i686-12.6.iso",
|
|
|
|
"iso_checksum": "md5:43c1feeae55a44c6ef694b8eb18408a6",
|
|
|
|
"ssh_username": "root",
|
|
|
|
"ssh_password": "password",
|
|
|
|
"ssh_wait_timeout": "45s",
|
|
|
|
"boot_command": []string{"<enter><wait5><wait10>", "root<enter><wait>password<enter><wait>", "udhcpc<enter><wait>"},
|
|
|
|
"shutdown_command": "/sbin/shutdown -h; exit 0",
|
|
|
|
"ssh_key_exchange_algorithms": []string{"diffie-hellman-group1-sha1"},
|
|
|
|
},
|
2016-04-24 23:22:13 -04:00
|
|
|
},
|
2020-12-02 19:24:48 -05:00
|
|
|
"provisioners": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "shell",
|
|
|
|
"inline": []string{"echo hola mundo"},
|
|
|
|
},
|
2016-04-24 23:22:13 -04:00
|
|
|
},
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2020-12-02 19:24:48 -05:00
|
|
|
// apply special builder overrides
|
|
|
|
for k, v := range builderConfig {
|
|
|
|
t["builders"][0][k] = v
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
2020-12-02 19:24:48 -05:00
|
|
|
// Apply special provisioner overrides
|
|
|
|
for k, v := range provisionerConfig {
|
|
|
|
t["provisioners"][0][k] = v
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
j, _ := json.Marshal(t)
|
|
|
|
return string(j)
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateVmx_SerialFile(t *testing.T) {
|
2017-06-17 16:35:58 -04:00
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
tmpfile, err := tmp.File("SerialFileInput.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp file")
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
2020-12-02 19:24:48 -05:00
|
|
|
serialConfig := map[string]interface{}{
|
|
|
|
"serial": fmt.Sprintf("file:%s", filepath.ToSlash(tmpfile.Name())),
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
2016-04-24 23:22:13 -04:00
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
configString := RenderConfig(serialConfig, map[string]string{})
|
2016-04-24 23:22:13 -04:00
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: configString,
|
|
|
|
Check: func(a []packersdk.Artifact) error {
|
|
|
|
_, err := os.Stat(tmpfile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Teardown: func() error {
|
|
|
|
f, _ := os.Stat(tmpfile.Name())
|
|
|
|
if f != nil {
|
|
|
|
if err := os.Remove(tmpfile.Name()); err != nil {
|
|
|
|
return fmt.Errorf("Unable to remove file %s: %s", tmpfile.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateVmx_SerialPort(t *testing.T) {
|
2017-06-17 16:35:58 -04:00
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
|
|
|
|
}
|
|
|
|
|
2016-04-24 23:22:13 -04:00
|
|
|
var defaultSerial string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
defaultSerial = "COM1"
|
|
|
|
} else {
|
|
|
|
defaultSerial = "/dev/ttyS0"
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config := map[string]interface{}{
|
|
|
|
"serial": fmt.Sprintf("device:%s", filepath.ToSlash(defaultSerial)),
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
provision := map[string]string{
|
2020-12-02 19:24:48 -05:00
|
|
|
"inline": "dmesg | egrep -o '^serial8250: ttyS1 at' > /dev/fd0",
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// where to write output
|
2017-02-27 16:34:53 -05:00
|
|
|
output, vmxData, err := createFloppyOutput("SerialPortOutput.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating output: %s", err)
|
|
|
|
}
|
2016-04-24 23:22:13 -04:00
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config["vmx_data"] = vmxData
|
|
|
|
configString := RenderConfig(config, provision)
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: configString,
|
|
|
|
Check: func(a []packersdk.Artifact) error {
|
|
|
|
_, err := os.Stat(output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
|
|
|
|
}
|
|
|
|
// check the output
|
|
|
|
data, err := readFloppyOutput(output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data != "serial8250: ttyS1 at\n" {
|
|
|
|
return fmt.Errorf("Serial port not detected : %v", data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Teardown: func() error {
|
|
|
|
if _, err := os.Stat(output); err == nil {
|
|
|
|
os.Remove(output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateVmx_ParallelPort(t *testing.T) {
|
2017-06-17 16:35:58 -04:00
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
|
|
|
|
}
|
|
|
|
|
2016-04-24 23:22:13 -04:00
|
|
|
var defaultParallel string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
defaultParallel = "LPT1"
|
|
|
|
} else {
|
|
|
|
defaultParallel = "/dev/lp0"
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config := map[string]interface{}{
|
|
|
|
"parallel": fmt.Sprintf("device:%s,uni", filepath.ToSlash(defaultParallel)),
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
provision := map[string]string{
|
2020-12-02 19:24:48 -05:00
|
|
|
"inline": "cat /proc/modules | egrep -o '^parport ' > /dev/fd0",
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// where to write output
|
2017-02-27 16:34:53 -05:00
|
|
|
output, vmxData, err := createFloppyOutput("ParallelPortOutput.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating output: %s", err)
|
|
|
|
}
|
2016-04-24 23:22:13 -04:00
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config["vmx_data"] = vmxData
|
|
|
|
configString := RenderConfig(config, provision)
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: configString,
|
|
|
|
Check: func(a []packersdk.Artifact) error {
|
|
|
|
_, err := os.Stat(output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
|
|
|
|
}
|
|
|
|
// check the output
|
|
|
|
data, err := readFloppyOutput(output)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data != "parport \n" {
|
|
|
|
t.Errorf("Parallel port not detected : %v", data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Teardown: func() error {
|
|
|
|
if _, err := os.Stat(output); err == nil {
|
|
|
|
os.Remove(output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateVmx_Usb(t *testing.T) {
|
2017-06-17 16:35:58 -04:00
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config := map[string]interface{}{
|
|
|
|
"usb": "TRUE",
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
provision := map[string]string{
|
2020-12-02 19:24:48 -05:00
|
|
|
"inline": "dmesg | egrep -m1 -o 'USB hub found$' > /dev/fd0",
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
output, vmxData, err := createFloppyOutput("UsbOutput.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating output: %s", err)
|
|
|
|
}
|
2016-04-24 23:22:13 -04:00
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config["vmx_data"] = vmxData
|
|
|
|
configString := RenderConfig(config, provision)
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: configString,
|
|
|
|
Check: func(a []packersdk.Artifact) error {
|
|
|
|
_, err := os.Stat(output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
|
|
|
|
}
|
|
|
|
// check the output
|
|
|
|
data, err := readFloppyOutput(output)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data != "USB hub found\n" {
|
|
|
|
t.Errorf("USB support not detected : %v", data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Teardown: func() error {
|
|
|
|
if _, err := os.Stat(output); err == nil {
|
|
|
|
os.Remove(output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateVmx_Sound(t *testing.T) {
|
2017-06-17 16:35:58 -04:00
|
|
|
if os.Getenv("PACKER_ACC") == "" {
|
|
|
|
t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.")
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config := map[string]interface{}{
|
|
|
|
"sound": "TRUE",
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
2017-02-27 16:34:53 -05:00
|
|
|
provision := map[string]string{
|
2020-12-02 19:24:48 -05:00
|
|
|
"inline": "cat /proc/modules | egrep -o '^soundcore' > /dev/fd0",
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// where to write output
|
2017-02-27 16:34:53 -05:00
|
|
|
output, vmxData, err := createFloppyOutput("SoundOutput.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating output: %s", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if _, err := os.Stat(output); err == nil {
|
|
|
|
os.Remove(output)
|
|
|
|
}
|
|
|
|
}()
|
2016-04-24 23:22:13 -04:00
|
|
|
|
2020-12-02 19:24:48 -05:00
|
|
|
config["vmx_data"] = vmxData
|
|
|
|
configString := RenderConfig(config, provision)
|
|
|
|
builderT.Test(t, builderT.TestCase{
|
|
|
|
Builder: &Builder{},
|
|
|
|
Template: configString,
|
|
|
|
Check: func(a []packersdk.Artifact) error {
|
|
|
|
_, err := os.Stat(output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("VMware builder did not create a file for serial port: %s", err)
|
|
|
|
}
|
|
|
|
// check the output
|
|
|
|
data, err := readFloppyOutput(output)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data != "soundcore\n" {
|
|
|
|
t.Errorf("Soundcard not detected : %v", data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Teardown: func() error {
|
|
|
|
if _, err := os.Stat(output); err == nil {
|
|
|
|
os.Remove(output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2016-04-24 23:22:13 -04:00
|
|
|
}
|