2013-12-21 20:38:06 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
2016-07-26 15:42:04 -04:00
|
|
|
"fmt"
|
2013-12-22 20:02:22 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-12-23 17:21:28 -05:00
|
|
|
"testing"
|
2016-07-26 15:42:04 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-21 20:38:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig(t *testing.T) map[string]interface{} {
|
2013-12-22 20:02:22 -05:00
|
|
|
return map[string]interface{}{
|
2013-12-23 17:21:28 -05:00
|
|
|
"ssh_username": "foo",
|
2013-12-22 20:02:22 -05:00
|
|
|
"shutdown_command": "foo",
|
2016-07-26 15:42:04 -04:00
|
|
|
"source_path": "config_test.go",
|
2013-12-22 20:02:22 -05:00
|
|
|
}
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|
2013-12-22 20:02:22 -05:00
|
|
|
|
2013-12-31 18:28:33 -05:00
|
|
|
func getTempFile(t *testing.T) *os.File {
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
tf.Close()
|
|
|
|
|
|
|
|
// don't forget to cleanup the file downstream:
|
|
|
|
// defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
return tf
|
|
|
|
}
|
|
|
|
|
2016-07-26 15:42:04 -04:00
|
|
|
func TestNewConfig_FloppyFiles(t *testing.T) {
|
|
|
|
c := testConfig(t)
|
|
|
|
floppies_path := "../../../common/test-fixtures/floppies"
|
|
|
|
c["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
|
|
|
|
_, _, err := NewConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewConfig_InvalidFloppies(t *testing.T) {
|
|
|
|
c := testConfig(t)
|
2017-03-28 20:45:01 -04:00
|
|
|
c["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
|
2016-07-26 15:42:04 -04:00
|
|
|
_, _, errs := NewConfig(c)
|
|
|
|
if errs == nil {
|
2017-03-29 15:38:33 -04:00
|
|
|
t.Fatalf("Nonexistent floppies should trigger multierror")
|
2016-07-26 15:42:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.(*packer.MultiError).Errors) != 2 {
|
|
|
|
t.Fatalf("Multierror should work and report 2 errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-22 20:02:22 -05:00
|
|
|
func TestNewConfig_sourcePath(t *testing.T) {
|
2016-10-10 22:16:25 -04:00
|
|
|
// Okay, because it gets caught during download
|
2013-12-22 20:02:22 -05:00
|
|
|
c := testConfig(t)
|
|
|
|
delete(c, "source_path")
|
2016-10-10 22:16:25 -04:00
|
|
|
_, warns, err := NewConfig(c)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should error with empty `source_path`")
|
|
|
|
}
|
2013-12-22 20:02:22 -05:00
|
|
|
|
2017-11-13 14:44:59 -05:00
|
|
|
// Want this to fail on validation
|
2013-12-22 20:02:22 -05:00
|
|
|
c = testConfig(t)
|
|
|
|
c["source_path"] = "/i/dont/exist"
|
2016-10-10 22:16:25 -04:00
|
|
|
_, warns, err = NewConfig(c)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2017-11-13 14:44:59 -05:00
|
|
|
if err == nil {
|
2016-10-10 22:16:25 -04:00
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
c = testConfig(t)
|
|
|
|
c["source_path"] = "ftp://i/dont/exist"
|
|
|
|
_, warns, err = NewConfig(c)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
2017-11-13 15:42:20 -05:00
|
|
|
t.Fatal("should error")
|
2016-10-10 22:16:25 -04:00
|
|
|
}
|
2013-12-22 20:02:22 -05:00
|
|
|
|
|
|
|
// Good
|
2013-12-31 18:28:33 -05:00
|
|
|
tf := getTempFile(t)
|
2013-12-22 20:02:22 -05:00
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
c = testConfig(t)
|
|
|
|
c["source_path"] = tf.Name()
|
2016-10-10 22:16:25 -04:00
|
|
|
_, warns, err = NewConfig(c)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2013-12-22 20:02:22 -05:00
|
|
|
}
|
2013-12-31 18:28:33 -05:00
|
|
|
|
|
|
|
func TestNewConfig_shutdown_timeout(t *testing.T) {
|
|
|
|
c := testConfig(t)
|
|
|
|
tf := getTempFile(t)
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
// Expect this to fail
|
|
|
|
c["source_path"] = tf.Name()
|
|
|
|
c["shutdown_timeout"] = "NaN"
|
2016-10-10 22:16:25 -04:00
|
|
|
_, warns, err := NewConfig(c)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
2013-12-31 18:28:33 -05:00
|
|
|
|
|
|
|
// Passes when given a valid time duration
|
|
|
|
c["shutdown_timeout"] = "10s"
|
2016-10-10 22:16:25 -04:00
|
|
|
_, warns, err = NewConfig(c)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2013-12-31 18:28:33 -05:00
|
|
|
}
|