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