2014-04-06 13:21:22 -04:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2016-07-26 15:42:04 -04:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-04-06 13:21:22 -04:00
|
|
|
"testing"
|
2016-07-26 15:42:04 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2014-09-02 05:28:04 -04:00
|
|
|
"iso_checksum": "foo",
|
|
|
|
"iso_checksum_type": "md5",
|
|
|
|
"iso_url": "http://www.google.com/",
|
|
|
|
"shutdown_command": "yes",
|
|
|
|
"ssh_username": "foo",
|
|
|
|
"parallels_tools_flavor": "lin",
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
packer.BuildNameConfigKey: "foo",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
|
|
|
if _, ok := raw.(packer.Builder); !ok {
|
|
|
|
t.Error("Builder must implement builder.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Defaults(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-02 08:00:25 -04:00
|
|
|
if b.config.GuestOSType != "other" {
|
|
|
|
t.Errorf("bad guest OS type: %s", b.config.GuestOSType)
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.VMName != "packer-foo" {
|
|
|
|
t.Errorf("bad vm name: %s", b.config.VMName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 15:42:04 -04:00
|
|
|
func TestBuilderPrepare_FloppyFiles(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "floppy_files")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2016-07-26 15:42:04 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.config.FloppyFiles) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", b.config.FloppyFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
floppies_path := "../../../common/test-fixtures/floppies"
|
|
|
|
config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2016-07-26 15:42:04 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)}
|
|
|
|
if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.FloppyFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_InvalidFloppies(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2018-03-13 04:15:56 -04:00
|
|
|
config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
|
2016-07-26 15:42:04 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, _, errs := b.Prepare(config)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.(*packer.MultiError).Errors) != 2 {
|
|
|
|
t.Fatalf("Multierror should work and report 2 errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
func TestBuilderPrepare_DiskSize(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "disk_size")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskSize != 40000 {
|
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["disk_size"] = 60000
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskSize != 60000 {
|
2014-12-15 13:20:13 -05:00
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
2014-04-06 13:21:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:32:51 -05:00
|
|
|
func TestBuilderPrepare_DiskType(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2017-03-07 21:22:23 -05:00
|
|
|
// Test a default disk_type
|
2017-03-03 18:32:51 -05:00
|
|
|
delete(config, "disk_type")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-03-03 18:32:51 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskType != "expand" {
|
|
|
|
t.Fatalf("bad: %s", b.config.DiskType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a bad
|
|
|
|
config["disk_type"] = "fake"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-03-03 18:32:51 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
2017-03-07 21:22:23 -05:00
|
|
|
// Test with plain disk with wrong setting for compaction
|
|
|
|
config["disk_type"] = "plain"
|
|
|
|
config["skip_compaction"] = false
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-03-07 21:22:23 -05:00
|
|
|
if len(warns) == 0 {
|
|
|
|
t.Fatalf("should have warning")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with plain disk with correct setting for compaction
|
2017-03-03 18:32:51 -05:00
|
|
|
config["disk_type"] = "plain"
|
2017-03-07 21:22:23 -05:00
|
|
|
config["skip_compaction"] = true
|
2017-03-03 18:32:51 -05:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-03-03 18:32:51 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2017-03-07 21:22:23 -05:00
|
|
|
|
2017-03-03 18:32:51 -05:00
|
|
|
}
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test a default boot_wait
|
|
|
|
delete(config, "hard_drive_interface")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.HardDriveInterface != "sata" {
|
|
|
|
t.Fatalf("bad: %s", b.config.HardDriveInterface)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a bad
|
|
|
|
config["hard_drive_interface"] = "fake"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good
|
|
|
|
config["hard_drive_interface"] = "scsi"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2014-04-06 13:21:22 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|