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