From afc798c30af8cddb958d3b3a0a7f7428adeaed12 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 2 Dec 2020 16:24:48 -0800 Subject: [PATCH] Modify vmware_vmx acceptance tests to use builderT framework Modify builderT framework to enable use of shell and file provisioners --- Makefile | 2 +- builder/vmware/iso/step_create_vmx_test.go | 467 ++++++++----------- packer-plugin-sdk/builder/testing/testing.go | 6 + 3 files changed, 205 insertions(+), 270 deletions(-) diff --git a/Makefile b/Makefile index 3a50eaf44..0d0657bba 100644 --- a/Makefile +++ b/Makefile @@ -144,7 +144,7 @@ provisioners-acctest: install-build-deps generate ACC_TEST_BUILDERS=$(ACC_TEST_BUILDERS) ACC_TEST_PROVISIONERS=$(ACC_TEST_PROVISIONERS) go test ./provisioner/... -timeout=1h # testacc runs acceptance tests -testacc: install-build-deps generate ## Run acceptance tests +testacc: # install-build-deps generate ## Run acceptance tests @echo "WARN: Acceptance tests will take a long time to run and may cost money. Ctrl-C if you want to cancel." PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) $(TESTARGS) -timeout=120m diff --git a/builder/vmware/iso/step_create_vmx_test.go b/builder/vmware/iso/step_create_vmx_test.go index a741eddde..e45f41b3f 100644 --- a/builder/vmware/iso/step_create_vmx_test.go +++ b/builder/vmware/iso/step_create_vmx_test.go @@ -2,81 +2,40 @@ package iso import ( "bytes" - "context" + "encoding/json" "fmt" "io/ioutil" - "math" - "math/rand" "os" "path/filepath" "runtime" - "strconv" "strings" - "testing" - "github.com/hashicorp/packer/packer" + builderT "github.com/hashicorp/packer/packer-plugin-sdk/builder/testing" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" - "github.com/hashicorp/packer/packer-plugin-sdk/template" - "github.com/hashicorp/packer/provisioner/shell" + "github.com/hashicorp/packer/packer-plugin-sdk/tmp" ) -var vmxTestBuilderConfig = map[string]string{ - "type": `"vmware-iso"`, - "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": `["","rootpassword","udhcpc"]`, - "shutdown_command": `"/sbin/shutdown -h; exit 0"`, - "ssh_key_exchange_algorithms": `["diffie-hellman-group1-sha1"]`, -} - -var vmxTestProvisionerConfig = map[string]string{ - "type": `"shell"`, - "inline": `["echo hola mundo"]`, -} - const vmxTestTemplate string = `{"builders":[{%s}],"provisioners":[{%s}]}` -func tmpnam(prefix string) string { - var path string - var err error - - const length = 16 - - dir := os.TempDir() - max := int(math.Pow(2, float64(length))) - - // FIXME use ioutil.TempFile() or at least mimic implementation, this could loop forever - n, err := rand.Intn(max), nil - for path = filepath.Join(dir, prefix+strconv.Itoa(n)); err == nil; _, err = os.Stat(path) { - n = rand.Intn(max) - path = filepath.Join(dir, prefix+strconv.Itoa(n)) - } - return path -} - -func createFloppyOutput(prefix string) (string, string, error) { - output := tmpnam(prefix) - f, err := os.Create(output) +func createFloppyOutput(prefix string) (string, map[string]string, error) { + f, err := tmp.File(prefix) if err != nil { - return "", "", fmt.Errorf("Unable to create empty %s: %s", output, err) + return "", map[string]string{}, fmt.Errorf("unable to create temp file") } f.Close() - vmxData := []string{ - `"floppy0.present":"TRUE"`, - `"floppy0.fileType":"file"`, - `"floppy0.clientDevice":"FALSE"`, - `"floppy0.fileName":"%s"`, - `"floppy0.startConnected":"TRUE"`, - } - + output := f.Name() outputFile := strings.Replace(output, "\\", "\\\\", -1) - vmxString := fmt.Sprintf("{"+strings.Join(vmxData, ",")+"}", outputFile) - return output, vmxString, nil + vmxData := map[string]string{ + "floppy0.present": "TRUE", + "floppy0.fileType": "file", + "floppy0.clientDevice": "FALSE", + "floppy0.fileName": outputFile, + "floppy0.startConnected": "TRUE", + } + // vmxString := fmt.Sprintf("{"+strings.Join(vmxData, ",")+"}", outputFile) + return output, vmxData, nil } func readFloppyOutput(path string) (string, error) { @@ -95,114 +54,42 @@ func readFloppyOutput(path string) (string, error) { return string(data[:bytes.IndexByte(data, 0)]), nil } -func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisionerConfig map[string]string) error { - ui := packersdk.TestUi(t) - - // create builder config and update with user-supplied options - cfgBuilder := map[string]string{} - for k, v := range vmxTestBuilderConfig { - cfgBuilder[k] = v +// 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{"", "rootpassword", "udhcpc"}, + "shutdown_command": "/sbin/shutdown -h; exit 0", + "ssh_key_exchange_algorithms": []string{"diffie-hellman-group1-sha1"}, + }, + }, + "provisioners": { + map[string]interface{}{ + "type": "shell", + "inline": []string{"echo hola mundo"}, + }, + }, } + // apply special builder overrides for k, v := range builderConfig { - cfgBuilder[k] = v - } - - // convert our builder config into a single sprintfable string - builderLines := []string{} - for k, v := range cfgBuilder { - builderLines = append(builderLines, fmt.Sprintf(`"%s":%s`, k, v)) - } - - // create provisioner config and update with user-supplied options - cfgProvisioner := map[string]string{} - for k, v := range vmxTestProvisionerConfig { - cfgProvisioner[k] = v + t["builders"][0][k] = v } + // Apply special provisioner overrides for k, v := range provisionerConfig { - cfgProvisioner[k] = v + t["provisioners"][0][k] = v } - // convert our provisioner config into a single sprintfable string - provisionerLines := []string{} - for k, v := range cfgProvisioner { - provisionerLines = append(provisionerLines, fmt.Sprintf(`"%s":%s`, k, v)) - } - - // and now parse them into a template - configString := fmt.Sprintf(vmxTestTemplate, strings.Join(builderLines, `,`), strings.Join(provisionerLines, `,`)) - - tpl, err := template.Parse(strings.NewReader(configString)) - if err != nil { - t.Fatalf("Unable to parse test config: %s", err) - } - - // create our config to test the vmware-iso builder - components := packer.ComponentFinder{ - BuilderStore: packersdk.MapOfBuilder{ - "vmware-iso": func() (packersdk.Builder, error) { return &Builder{}, nil }, - }, - Hook: func(n string) (packersdk.Hook, error) { - return &packersdk.DispatchHook{}, nil - }, - ProvisionerStore: packersdk.MapOfProvisioner{ - "shell": func() (packersdk.Provisioner, error) { return &shell.Provisioner{}, nil }, - }, - PostProcessorStore: packersdk.MapOfPostProcessor{ - "something": func() (packersdk.PostProcessor, error) { return &packer.MockPostProcessor{}, nil }, - }, - } - config := packer.CoreConfig{ - Template: tpl, - Components: components, - } - - // create a core using our template - core := packer.NewCore(&config) - err = core.Initialize() - if err != nil { - t.Fatalf("Unable to create core: %s", err) - } - - // now we can prepare our build - b, err := core.Build("vmware-iso") - if err != nil { - t.Fatalf("Unable to create build: %s", err) - } - - warn, err := b.Prepare() - if err != nil { - t.Fatalf("error preparing build: %v", err) - } - if len(warn) > 0 { - for _, w := range warn { - t.Logf("Configuration warning: %s", w) - } - } - - // and then finally build it - artifacts, err := b.Run(context.Background(), ui) - if err != nil { - t.Fatalf("Failed to build artifact: %s", err) - } - - // check to see that we only got one artifact back - if len(artifacts) == 1 { - return artifacts[0].Destroy() - } - - // otherwise some number of errors happened - t.Logf("Unexpected number of artifacts returned: %d", len(artifacts)) - errors := make([]error, 0) - for _, artifact := range artifacts { - if err := artifact.Destroy(); err != nil { - errors = append(errors, err) - } - } - if len(errors) > 0 { - t.Errorf("%d Errors returned while trying to destroy artifacts", len(errors)) - return fmt.Errorf("Error while trying to destroy artifacts: %v", errors) - } - return nil + j, _ := json.Marshal(t) + return string(j) } func TestStepCreateVmx_SerialFile(t *testing.T) { @@ -210,27 +97,37 @@ func TestStepCreateVmx_SerialFile(t *testing.T) { t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.") } - tmpfile := tmpnam("SerialFileInput.") - - serialConfig := map[string]string{ - "serial": fmt.Sprintf(`"file:%s"`, filepath.ToSlash(tmpfile)), - } - - error := setupVMwareBuild(t, serialConfig, map[string]string{}) - if error != nil { - t.Errorf("Unable to read file: %s", error) - } - - f, err := os.Stat(tmpfile) + tmpfile, err := tmp.File("SerialFileInput.") if err != nil { - t.Errorf("VMware builder did not create a file for serial port: %s", err) + t.Fatalf("unable to create temp file") + } + serialConfig := map[string]interface{}{ + "serial": fmt.Sprintf("file:%s", filepath.ToSlash(tmpfile.Name())), } - if f != nil { - if err := os.Remove(tmpfile); err != nil { - t.Fatalf("Unable to remove file %s: %s", tmpfile, err) - } - } + configString := RenderConfig(serialConfig, map[string]string{}) + + builderT.Test(t, builderT.TestCase{ + // PreCheck: func() { testAccPreCheck(t) }, + 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 + }, + }) } func TestStepCreateVmx_SerialPort(t *testing.T) { @@ -245,11 +142,11 @@ func TestStepCreateVmx_SerialPort(t *testing.T) { defaultSerial = "/dev/ttyS0" } - config := map[string]string{ - "serial": fmt.Sprintf(`"device:%s"`, filepath.ToSlash(defaultSerial)), + config := map[string]interface{}{ + "serial": fmt.Sprintf("device:%s", filepath.ToSlash(defaultSerial)), } provision := map[string]string{ - "inline": `"dmesg | egrep -o '^serial8250: ttyS1 at' > /dev/fd0"`, + "inline": "dmesg | egrep -o '^serial8250: ttyS1 at' > /dev/fd0", } // where to write output @@ -257,29 +154,36 @@ func TestStepCreateVmx_SerialPort(t *testing.T) { if err != nil { t.Fatalf("Error creating output: %s", err) } - defer func() { - if _, err := os.Stat(output); err == nil { - os.Remove(output) - } - }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + // PreCheck: func() { testAccPreCheck(t) }, + 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) + } - // whee - err = setupVMwareBuild(t, config, provision) - if err != nil { - t.Errorf("%s", err) - } - - // check the output - data, err := readFloppyOutput(output) - if err != nil { - t.Errorf("%s", err) - } - - if data != "serial8250: ttyS1 at\n" { - t.Errorf("Serial port not detected : %v", data) - } + 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 + }, + }) } func TestStepCreateVmx_ParallelPort(t *testing.T) { @@ -294,11 +198,11 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) { defaultParallel = "/dev/lp0" } - config := map[string]string{ - "parallel": fmt.Sprintf(`"device:%s,uni"`, filepath.ToSlash(defaultParallel)), + config := map[string]interface{}{ + "parallel": fmt.Sprintf("device:%s,uni", filepath.ToSlash(defaultParallel)), } provision := map[string]string{ - "inline": `"cat /proc/modules | egrep -o '^parport ' > /dev/fd0"`, + "inline": "cat /proc/modules | egrep -o '^parport ' > /dev/fd0", } // where to write output @@ -306,29 +210,36 @@ func TestStepCreateVmx_ParallelPort(t *testing.T) { if err != nil { t.Fatalf("Error creating output: %s", err) } - defer func() { - if _, err := os.Stat(output); err == nil { - os.Remove(output) - } - }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + // PreCheck: func() { testAccPreCheck(t) }, + 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) + } - // whee - error := setupVMwareBuild(t, config, provision) - if error != nil { - t.Errorf("%s", error) - } - - // 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) - } + 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 + }, + }) } func TestStepCreateVmx_Usb(t *testing.T) { @@ -336,41 +247,47 @@ func TestStepCreateVmx_Usb(t *testing.T) { t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.") } - config := map[string]string{ - "usb": `"TRUE"`, + config := map[string]interface{}{ + "usb": "TRUE", } provision := map[string]string{ - "inline": `"dmesg | egrep -m1 -o 'USB hub found$' > /dev/fd0"`, + "inline": "dmesg | egrep -m1 -o 'USB hub found$' > /dev/fd0", } - // where to write output output, vmxData, err := createFloppyOutput("UsbOutput.") if err != nil { t.Fatalf("Error creating output: %s", err) } - defer func() { - if _, err := os.Stat(output); err == nil { - os.Remove(output) - } - }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + // PreCheck: func() { testAccPreCheck(t) }, + 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) + } - // whee - error := setupVMwareBuild(t, config, provision) - if error != nil { - t.Errorf("%s", error) - } - - // 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) - } + 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 + }, + }) } func TestStepCreateVmx_Sound(t *testing.T) { @@ -378,11 +295,11 @@ func TestStepCreateVmx_Sound(t *testing.T) { t.Skip("This test is only run with PACKER_ACC=1 due to the requirement of access to the VMware binaries.") } - config := map[string]string{ - "sound": `"TRUE"`, + config := map[string]interface{}{ + "sound": "TRUE", } provision := map[string]string{ - "inline": `"cat /proc/modules | egrep -o '^soundcore' > /dev/fd0"`, + "inline": "cat /proc/modules | egrep -o '^soundcore' > /dev/fd0", } // where to write output @@ -395,22 +312,34 @@ func TestStepCreateVmx_Sound(t *testing.T) { os.Remove(output) } }() + config["vmx_data"] = vmxData - t.Logf("Preparing to write output to %s", output) + configString := RenderConfig(config, provision) + builderT.Test(t, builderT.TestCase{ + // PreCheck: func() { testAccPreCheck(t) }, + 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) + } - // whee - error := setupVMwareBuild(t, config, provision) - if error != nil { - t.Errorf("Unable to read file: %s", error) - } - - // 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) - } + 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 + }, + }) } diff --git a/packer-plugin-sdk/builder/testing/testing.go b/packer-plugin-sdk/builder/testing/testing.go index 746147866..3fbe5304a 100644 --- a/packer-plugin-sdk/builder/testing/testing.go +++ b/packer-plugin-sdk/builder/testing/testing.go @@ -12,6 +12,8 @@ import ( "github.com/hashicorp/packer/packer" packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" "github.com/hashicorp/packer/packer-plugin-sdk/template" + "github.com/hashicorp/packer/provisioner/file" + shellprovisioner "github.com/hashicorp/packer/provisioner/shell" ) // TestEnvVar must be set to a non-empty value for acceptance tests to run. @@ -123,6 +125,10 @@ func Test(t TestT, c TestCase) { return nil, nil }, }, + ProvisionerStore: packersdk.MapOfProvisioner{ + "shell": func() (packersdk.Provisioner, error) { return &shellprovisioner.Provisioner{}, nil }, + "file": func() (packersdk.Provisioner, error) { return &file.Provisioner{}, nil }, + }, }, Template: tpl, })