github.com/timsutton/packer@v1.3.2/builder/parallels/pvm/config_test.go (about)

     1  package pvm
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  func testConfig(t *testing.T) map[string]interface{} {
    13  	return map[string]interface{}{
    14  		"ssh_username":           "foo",
    15  		"shutdown_command":       "foo",
    16  		"parallels_tools_flavor": "lin",
    17  		"source_path":            "config_test.go",
    18  	}
    19  }
    20  
    21  func getTempFile(t *testing.T) *os.File {
    22  	tf, err := ioutil.TempFile("", "packer")
    23  	if err != nil {
    24  		t.Fatalf("err: %s", err)
    25  	}
    26  	tf.Close()
    27  
    28  	// don't forget to cleanup the file downstream:
    29  	// defer os.Remove(tf.Name())
    30  
    31  	return tf
    32  }
    33  
    34  func testConfigErr(t *testing.T, warns []string, err error) {
    35  	if len(warns) > 0 {
    36  		t.Fatalf("bad: %#v", warns)
    37  	}
    38  	if err == nil {
    39  		t.Fatal("should error")
    40  	}
    41  }
    42  
    43  func testConfigOk(t *testing.T, warns []string, err error) {
    44  	if len(warns) > 0 {
    45  		t.Fatalf("bad: %#v", warns)
    46  	}
    47  	if err != nil {
    48  		t.Fatalf("bad: %s", err)
    49  	}
    50  }
    51  
    52  func TestNewConfig_sourcePath(t *testing.T) {
    53  	// Bad
    54  	c := testConfig(t)
    55  	delete(c, "source_path")
    56  	_, warns, errs := NewConfig(c)
    57  	testConfigErr(t, warns, errs)
    58  
    59  	// Bad
    60  	c = testConfig(t)
    61  	c["source_path"] = "/i/dont/exist"
    62  	_, warns, errs = NewConfig(c)
    63  	testConfigErr(t, warns, errs)
    64  
    65  	// Good
    66  	tf := getTempFile(t)
    67  	defer os.Remove(tf.Name())
    68  
    69  	c = testConfig(t)
    70  	c["source_path"] = tf.Name()
    71  	_, warns, errs = NewConfig(c)
    72  	testConfigOk(t, warns, errs)
    73  }
    74  
    75  func TestNewConfig_FloppyFiles(t *testing.T) {
    76  	c := testConfig(t)
    77  	floppies_path := "../../../common/test-fixtures/floppies"
    78  	c["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
    79  	_, _, err := NewConfig(c)
    80  	if err != nil {
    81  		t.Fatalf("should not have error: %s", err)
    82  	}
    83  }
    84  
    85  func TestNewConfig_InvalidFloppies(t *testing.T) {
    86  	c := testConfig(t)
    87  	c["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
    88  	_, _, errs := NewConfig(c)
    89  	if errs == nil {
    90  		t.Fatalf("Nonexistent floppies should trigger multierror")
    91  	}
    92  
    93  	if len(errs.(*packer.MultiError).Errors) != 2 {
    94  		t.Fatalf("Multierror should work and report 2 errors")
    95  	}
    96  }
    97  
    98  func TestNewConfig_shutdown_timeout(t *testing.T) {
    99  	c := testConfig(t)
   100  	tf := getTempFile(t)
   101  	defer os.Remove(tf.Name())
   102  
   103  	// Expect this to fail
   104  	c["source_path"] = tf.Name()
   105  	c["shutdown_timeout"] = "NaN"
   106  	_, warns, errs := NewConfig(c)
   107  	testConfigErr(t, warns, errs)
   108  
   109  	// Passes when given a valid time duration
   110  	c["shutdown_timeout"] = "10s"
   111  	_, warns, errs = NewConfig(c)
   112  	testConfigOk(t, warns, errs)
   113  }