github.com/rothwerx/packer@v0.9.0/builder/virtualbox/ovf/config_test.go (about)

     1  package ovf
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func testConfig(t *testing.T) map[string]interface{} {
    10  	return map[string]interface{}{
    11  		"ssh_username":     "foo",
    12  		"shutdown_command": "foo",
    13  	}
    14  }
    15  
    16  func getTempFile(t *testing.T) *os.File {
    17  	tf, err := ioutil.TempFile("", "packer")
    18  	if err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	tf.Close()
    22  
    23  	// don't forget to cleanup the file downstream:
    24  	// defer os.Remove(tf.Name())
    25  
    26  	return tf
    27  }
    28  
    29  func testConfigErr(t *testing.T, warns []string, err error) {
    30  	if len(warns) > 0 {
    31  		t.Fatalf("bad: %#v", warns)
    32  	}
    33  	if err == nil {
    34  		t.Fatal("should error")
    35  	}
    36  }
    37  
    38  func testConfigOk(t *testing.T, warns []string, err error) {
    39  	if len(warns) > 0 {
    40  		t.Fatalf("bad: %#v", warns)
    41  	}
    42  	if err != nil {
    43  		t.Fatalf("bad: %s", err)
    44  	}
    45  }
    46  
    47  func TestNewConfig_sourcePath(t *testing.T) {
    48  	// Bad
    49  	c := testConfig(t)
    50  	delete(c, "source_path")
    51  	_, warns, errs := NewConfig(c)
    52  	testConfigErr(t, warns, errs)
    53  
    54  	// Bad
    55  	c = testConfig(t)
    56  	c["source_path"] = "/i/dont/exist"
    57  	_, warns, errs = NewConfig(c)
    58  	testConfigErr(t, warns, errs)
    59  
    60  	// Good
    61  	tf := getTempFile(t)
    62  	defer os.Remove(tf.Name())
    63  
    64  	c = testConfig(t)
    65  	c["source_path"] = tf.Name()
    66  	_, warns, errs = NewConfig(c)
    67  	testConfigOk(t, warns, errs)
    68  }
    69  
    70  func TestNewConfig_shutdown_timeout(t *testing.T) {
    71  	c := testConfig(t)
    72  	tf := getTempFile(t)
    73  	defer os.Remove(tf.Name())
    74  
    75  	// Expect this to fail
    76  	c["source_path"] = tf.Name()
    77  	c["shutdown_timeout"] = "NaN"
    78  	_, warns, errs := NewConfig(c)
    79  	testConfigErr(t, warns, errs)
    80  
    81  	// Passes when given a valid time duration
    82  	c["shutdown_timeout"] = "10s"
    83  	_, warns, errs = NewConfig(c)
    84  	testConfigOk(t, warns, errs)
    85  }