github.com/mitchellh/packer@v1.3.2/builder/vmware/vmx/builder_test.go (about)

     1  package vmx
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  func TestBuilderPrepare_FloppyFiles(t *testing.T) {
    14  	var b Builder
    15  
    16  	tf, err := ioutil.TempFile("", "packer")
    17  	if err != nil {
    18  		t.Fatalf("err: %s", err)
    19  	}
    20  	tf.Close()
    21  	defer os.Remove(tf.Name())
    22  
    23  	config := testConfig(t)
    24  	config["source_path"] = tf.Name()
    25  
    26  	delete(config, "floppy_files")
    27  	warns, err := b.Prepare(config)
    28  	if len(warns) > 0 {
    29  		t.Fatalf("bad: %#v", warns)
    30  	}
    31  	if err != nil {
    32  		t.Fatalf("bad err: %s", err)
    33  	}
    34  
    35  	if len(b.config.FloppyFiles) != 0 {
    36  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
    37  	}
    38  
    39  	floppies_path := "../../../common/test-fixtures/floppies"
    40  	config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
    41  	b = Builder{}
    42  	warns, err = b.Prepare(config)
    43  	if len(warns) > 0 {
    44  		t.Fatalf("bad: %#v", warns)
    45  	}
    46  	if err != nil {
    47  		t.Fatalf("should not have error: %s", err)
    48  	}
    49  
    50  	expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
    51  	if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
    52  		t.Fatalf("bad: %#v", b.config.FloppyFiles)
    53  	}
    54  }
    55  
    56  func TestBuilderPrepare_InvalidFloppies(t *testing.T) {
    57  	var b Builder
    58  	config := testConfig(t)
    59  	config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
    60  	b = Builder{}
    61  	_, errs := b.Prepare(config)
    62  	if errs == nil {
    63  		t.Fatalf("Nonexistent floppies should trigger multierror")
    64  	}
    65  
    66  	if len(errs.(*packer.MultiError).Errors) != 2 {
    67  		t.Fatalf("Multierror should work and report 2 errors")
    68  	}
    69  }