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
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/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) {
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg := testConfig(t)
|
2020-12-17 17:41:36 -05:00
|
|
|
floppies_path := "../../test-fixtures/floppies"
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
|
|
|
|
var c Config
|
|
|
|
_, err := c.Prepare(cfg)
|
2016-07-26 15:42:04 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewConfig_InvalidFloppies(t *testing.T) {
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg := testConfig(t)
|
|
|
|
cfg["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
|
|
|
|
var c Config
|
|
|
|
_, errs := c.Prepare(cfg)
|
2016-07-26 15:42:04 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
if len(errs.(*packersdk.MultiError).Errors) != 2 {
|
2016-07-26 15:42:04 -04:00
|
|
|
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
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg := testConfig(t)
|
|
|
|
delete(cfg, "source_path")
|
|
|
|
var c Config
|
|
|
|
warns, err := c.Prepare(cfg)
|
2016-10-10 22:16:25 -04:00
|
|
|
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
|
|
|
|
|
|
|
// 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())
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg = testConfig(t)
|
|
|
|
cfg["source_path"] = tf.Name()
|
|
|
|
warns, err = c.Prepare(cfg)
|
2016-10-10 22:16:25 -04:00
|
|
|
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) {
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg := testConfig(t)
|
2013-12-31 18:28:33 -05:00
|
|
|
tf := getTempFile(t)
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
// Expect this to fail
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg["source_path"] = tf.Name()
|
|
|
|
cfg["shutdown_timeout"] = "NaN"
|
|
|
|
var c Config
|
|
|
|
warns, err := c.Prepare(cfg)
|
2016-10-10 22:16:25 -04:00
|
|
|
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
|
2019-12-17 05:25:56 -05:00
|
|
|
cfg["shutdown_timeout"] = "10s"
|
|
|
|
warns, err = c.Prepare(cfg)
|
2016-10-10 22:16:25 -04:00
|
|
|
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
|
|
|
}
|
2020-05-07 19:08:52 -04:00
|
|
|
|
|
|
|
// TestChecksumFileNameMixedCaseBug reproduces Github issue #9049:
|
|
|
|
// https://github.com/hashicorp/packer/issues/9049
|
|
|
|
func TestChecksumFileNameMixedCaseBug(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
Name string
|
|
|
|
ChecksumPath string
|
|
|
|
}{
|
2020-05-28 05:02:09 -04:00
|
|
|
{"Lowercase", "file:/tmp/random/file.md5"},
|
|
|
|
{"MiXeDcAsE", "file:/tmp/RaNdOm/FiLe.Md5"},
|
2020-05-07 19:08:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
|
|
|
|
cfg := testConfig(t)
|
|
|
|
cfg["source_path"] = "bug.ovf"
|
|
|
|
cfg["checksum"] = tc.ChecksumPath
|
|
|
|
cfg["type"] = "virtualbox-ovf"
|
|
|
|
cfg["guest_additions_mode"] = "disable"
|
|
|
|
cfg["headless"] = false
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
warns, err := c.Prepare(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("config failed to Prepare, %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(warns) != 0 {
|
|
|
|
t.Errorf("Encountered warnings during config preparation: %s", warns)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Checksum != tc.ChecksumPath {
|
|
|
|
t.Errorf("%s test failed, Checksum and ChecksumPath are expected to be equal, expected: %s, got: %s", tc.Name, tc.ChecksumPath, c.Checksum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|