github.phpd.cn/hashicorp/packer@v1.3.2/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/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 "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 TestNewConfig_FloppyFiles(t *testing.T) { 34 c := testConfig(t) 35 floppies_path := "../../../common/test-fixtures/floppies" 36 c["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} 37 _, _, err := NewConfig(c) 38 if err != nil { 39 t.Fatalf("should not have error: %s", err) 40 } 41 } 42 43 func TestNewConfig_InvalidFloppies(t *testing.T) { 44 c := testConfig(t) 45 c["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"} 46 _, _, errs := NewConfig(c) 47 if errs == nil { 48 t.Fatalf("Nonexistent floppies should trigger multierror") 49 } 50 51 if len(errs.(*packer.MultiError).Errors) != 2 { 52 t.Fatalf("Multierror should work and report 2 errors") 53 } 54 } 55 56 func TestNewConfig_sourcePath(t *testing.T) { 57 // Okay, because it gets caught during download 58 c := testConfig(t) 59 delete(c, "source_path") 60 _, warns, err := NewConfig(c) 61 if len(warns) > 0 { 62 t.Fatalf("bad: %#v", warns) 63 } 64 if err == nil { 65 t.Fatalf("should error with empty `source_path`") 66 } 67 68 // Want this to fail on validation 69 c = testConfig(t) 70 c["source_path"] = "/i/dont/exist" 71 _, warns, err = NewConfig(c) 72 if len(warns) > 0 { 73 t.Fatalf("bad: %#v", warns) 74 } 75 if err == nil { 76 t.Fatalf("Nonexistent file should throw a validation error!") 77 } 78 79 // Bad 80 c = testConfig(t) 81 c["source_path"] = "ftp://i/dont/exist" 82 _, warns, err = NewConfig(c) 83 if len(warns) > 0 { 84 t.Fatalf("bad: %#v", warns) 85 } 86 if err == nil { 87 t.Fatalf("should error") 88 } 89 90 // Good 91 tf := getTempFile(t) 92 defer os.Remove(tf.Name()) 93 94 c = testConfig(t) 95 c["source_path"] = tf.Name() 96 _, warns, err = NewConfig(c) 97 if len(warns) > 0 { 98 t.Fatalf("bad: %#v", warns) 99 } 100 if err != nil { 101 t.Fatalf("bad: %s", err) 102 } 103 } 104 105 func TestNewConfig_shutdown_timeout(t *testing.T) { 106 c := testConfig(t) 107 tf := getTempFile(t) 108 defer os.Remove(tf.Name()) 109 110 // Expect this to fail 111 c["source_path"] = tf.Name() 112 c["shutdown_timeout"] = "NaN" 113 _, warns, err := NewConfig(c) 114 if len(warns) > 0 { 115 t.Fatalf("bad: %#v", warns) 116 } 117 if err == nil { 118 t.Fatal("should error") 119 } 120 121 // Passes when given a valid time duration 122 c["shutdown_timeout"] = "10s" 123 _, warns, err = NewConfig(c) 124 if len(warns) > 0 { 125 t.Fatalf("bad: %#v", warns) 126 } 127 if err != nil { 128 t.Fatalf("bad: %s", err) 129 } 130 }