2018-10-16 13:09:35 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2015-06-21 10:53:08 -04:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2017-06-20 07:21:07 -04:00
|
|
|
"fmt"
|
2015-06-21 10:53:08 -04:00
|
|
|
"reflect"
|
2017-10-19 18:29:17 -04:00
|
|
|
"strconv"
|
2015-06-21 10:53:08 -04:00
|
|
|
"testing"
|
2016-06-30 15:21:57 -04:00
|
|
|
|
2017-11-05 08:58:08 -05:00
|
|
|
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
|
2020-12-02 15:12:47 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 15:07:02 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2015-06-21 10:53:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2020-05-28 05:02:09 -04:00
|
|
|
"iso_checksum": "md5:0B0F137F17AC10944716020B018F8126",
|
2015-10-30 16:13:48 -04:00
|
|
|
"iso_url": "http://www.packer.io",
|
|
|
|
"shutdown_command": "yes",
|
|
|
|
"ssh_username": "foo",
|
2019-03-29 18:52:41 -04:00
|
|
|
"memory": 64,
|
2015-10-30 16:13:48 -04:00
|
|
|
"disk_size": 256,
|
2018-02-23 14:19:26 -05:00
|
|
|
"disk_block_size": 1,
|
2015-10-30 16:13:48 -04:00
|
|
|
"guest_additions_mode": "none",
|
2017-10-19 18:29:17 -04:00
|
|
|
"disk_additional_size": "50000,40000,30000",
|
2020-12-02 15:12:47 -05:00
|
|
|
common.BuildNameConfigKey: "foo",
|
2015-06-21 10:53:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
2020-12-01 16:42:11 -05:00
|
|
|
if _, ok := raw.(packersdk.Builder); !ok {
|
2015-06-21 10:53:08 -04:00
|
|
|
t.Error("Builder must implement builder.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_Defaults(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2016-06-30 15:21:57 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2015-06-21 10:53:08 -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.VMName != "packer-foo" {
|
|
|
|
t.Errorf("bad vm name: %s", b.config.VMName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-30 16:13:48 -04:00
|
|
|
if b.config.DiskSize != 40*1024 {
|
2015-06-21 10:53:08 -04:00
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
|
|
|
}
|
|
|
|
|
2015-10-18 12:11:38 -04:00
|
|
|
config["disk_size"] = 256
|
2015-06-21 10:53:08 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-18 12:11:38 -04:00
|
|
|
if b.config.DiskSize != 256 {
|
2015-06-21 10:53:08 -04:00
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 14:19:26 -05:00
|
|
|
func TestBuilderPrepare_DiskBlockSize(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
expected_default_block_size := uint(32)
|
|
|
|
expected_min_block_size := uint(0)
|
|
|
|
expected_max_block_size := uint(256)
|
|
|
|
|
|
|
|
// Test default with empty disk_block_size
|
|
|
|
delete(config, "disk_block_size")
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2018-02-23 14:19:26 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
if b.config.DiskBlockSize != expected_default_block_size {
|
2018-07-09 12:20:38 -04:00
|
|
|
t.Fatalf("bad default block size with empty config: %d. Expected %d", b.config.DiskBlockSize,
|
|
|
|
expected_default_block_size)
|
2018-02-23 14:19:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
test_sizes := []uint{0, 1, 32, 256, 512, 1 * 1024, 32 * 1024}
|
|
|
|
for _, test_size := range test_sizes {
|
|
|
|
config["disk_block_size"] = test_size
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2018-02-23 14:19:26 -05:00
|
|
|
if test_size > expected_max_block_size || test_size < expected_min_block_size {
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad, should have no warns: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
t.Fatalf("bad, should have error. disk_block_size=%d outside expected valid range [%d,%d]",
|
|
|
|
test_size, expected_min_block_size, expected_max_block_size)
|
2018-02-23 14:19:26 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad, should not have error: %s", err)
|
|
|
|
}
|
|
|
|
if test_size == 0 {
|
|
|
|
if b.config.DiskBlockSize != expected_default_block_size {
|
2018-07-09 12:20:38 -04:00
|
|
|
t.Fatalf("bad default block size with 0 value config: %d. Expected: %d",
|
|
|
|
b.config.DiskBlockSize, expected_default_block_size)
|
2018-02-23 14:19:26 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if b.config.DiskBlockSize != test_size {
|
2018-07-09 12:20:38 -04:00
|
|
|
t.Fatalf("bad block size with 0 value config: %d. Expected: %d", b.config.DiskBlockSize,
|
|
|
|
expected_default_block_size)
|
2018-02-23 14:19:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:13:12 -04:00
|
|
|
func TestBuilderPrepare_FixedVHDFormat(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
config["use_fixed_vhd_format"] = true
|
|
|
|
config["generation"] = 1
|
|
|
|
config["skip_compaction"] = true
|
|
|
|
config["differencing_disk"] = false
|
|
|
|
|
2018-07-09 12:20:38 -04:00
|
|
|
// use_fixed_vhd_format should work with generation = 1, skip_compaction
|
|
|
|
// = true, and differencing_disk = false
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2018-04-20 15:13:12 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//use_fixed_vhd_format should not work with differencing_disk = true
|
|
|
|
config["differencing_disk"] = true
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2018-04-20 15:13:12 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
config["differencing_disk"] = false
|
|
|
|
|
|
|
|
//use_fixed_vhd_format should not work with skip_compaction = false
|
|
|
|
config["skip_compaction"] = false
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2018-04-20 15:13:12 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
config["skip_compaction"] = true
|
|
|
|
|
|
|
|
//use_fixed_vhd_format should not work with generation = 2
|
|
|
|
config["generation"] = 2
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2018-04-20 15:13:12 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:21:07 -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)
|
2017-06-20 07:21:07 -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)
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:44:02 -05:00
|
|
|
floppiesPath := "../../../packer-plugin-sdk/test-fixtures/floppies"
|
2017-06-20 07:21:07 -04:00
|
|
|
config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppiesPath), fmt.Sprintf("%s/foo.ps1", floppiesPath)}
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-06-20 07:21:07 -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", floppiesPath), fmt.Sprintf("%s/foo.ps1", floppiesPath)}
|
|
|
|
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()
|
|
|
|
config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, _, errs := b.Prepare(config)
|
2017-06-20 07:21:07 -04:00
|
|
|
if errs == nil {
|
|
|
|
t.Fatalf("Nonexistent floppies should trigger multierror")
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
if len(errs.(*packersdk.MultiError).Errors) != 2 {
|
2017-06-20 07:21:07 -04:00
|
|
|
t.Fatalf("Multierror should work and report 2 errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 10:53:08 -04:00
|
|
|
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)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOChecksum(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["iso_checksum"] = ""
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
2020-05-28 05:02:09 -04:00
|
|
|
// Test invalid checksum
|
2015-06-21 10:53:08 -04:00
|
|
|
config["iso_checksum"] = "FOo"
|
2020-05-28 05:02:09 -04:00
|
|
|
_, warns, err = b.Prepare(config)
|
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["iso_checksum"] = "md5:0B0F137F17AC10944716020B018F8126"
|
2015-06-21 10:53:08 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2020-05-28 05:02:09 -04:00
|
|
|
config["iso_checksum"] = "0B0F137F17AC10944716020B018F8126"
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
2015-06-21 10:53:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
2020-05-28 05:02:09 -04:00
|
|
|
config["iso_checksum"] = "mD5:0B0F137F17AC10944716020B018F8126"
|
2015-06-21 10:53:08 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test unknown
|
2020-05-28 05:02:09 -04:00
|
|
|
config["iso_checksum"] = "fake:foo"
|
2015-06-21 10:53:08 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
t.Log("should error in prepare but go-getter doesn't let us validate yet. This will fail before dl.")
|
2015-06-21 10:53:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test none
|
2020-05-28 05:02:09 -04:00
|
|
|
config["iso_checksum"] = "none"
|
2015-06-21 10:53:08 -04:00
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) == 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOUrl(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "iso_url")
|
|
|
|
delete(config, "iso_urls")
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// Test both empty
|
2015-06-21 10:53:08 -04:00
|
|
|
config["iso_url"] = ""
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test iso_url set
|
|
|
|
config["iso_url"] = "http://www.packer.io"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{"http://www.packer.io"}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both set
|
|
|
|
config["iso_url"] = "http://www.packer.io"
|
|
|
|
config["iso_urls"] = []string{"http://www.packer.io"}
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test just iso_urls set
|
|
|
|
delete(config, "iso_url")
|
|
|
|
config["iso_urls"] = []string{
|
|
|
|
"http://www.packer.io",
|
|
|
|
"http://www.hashicorp.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2015-06-21 10:53:08 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []string{
|
|
|
|
"http://www.packer.io",
|
|
|
|
"http://www.hashicorp.com",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
}
|
2017-05-28 20:51:50 -04:00
|
|
|
|
2017-05-28 21:36:01 -04:00
|
|
|
func TestBuilderPrepare_SizeNotRequiredWhenUsingExistingHarddrive(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "iso_url")
|
|
|
|
delete(config, "iso_urls")
|
|
|
|
delete(config, "disk_size")
|
|
|
|
|
|
|
|
config["disk_size"] = 1
|
|
|
|
|
|
|
|
// Test just iso_urls set but with vhdx
|
|
|
|
delete(config, "iso_url")
|
|
|
|
config["iso_urls"] = []string{
|
|
|
|
"http://www.packer.io/hdd.vhdx",
|
|
|
|
"http://www.hashicorp.com/dvd.iso",
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 21:36:01 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
"http://www.packer.io/hdd.vhdx",
|
|
|
|
"http://www.hashicorp.com/dvd.iso",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test just iso_urls set but with vhd
|
|
|
|
delete(config, "iso_url")
|
|
|
|
config["iso_urls"] = []string{
|
|
|
|
"http://www.packer.io/hdd.vhd",
|
|
|
|
"http://www.hashicorp.com/dvd.iso",
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-05-28 21:36:01 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []string{
|
|
|
|
"http://www.packer.io/hdd.vhd",
|
|
|
|
"http://www.hashicorp.com/dvd.iso",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_SizeIsRequiredWhenNotUsingExistingHarddrive(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
delete(config, "iso_url")
|
|
|
|
delete(config, "iso_urls")
|
|
|
|
delete(config, "disk_size")
|
|
|
|
|
|
|
|
config["disk_size"] = 1
|
|
|
|
|
|
|
|
// Test just iso_urls set but with vhdx
|
|
|
|
delete(config, "iso_url")
|
|
|
|
config["iso_urls"] = []string{
|
|
|
|
"http://www.packer.io/os.iso",
|
|
|
|
"http://www.hashicorp.com/dvd.iso",
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 21:36:01 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
"http://www.packer.io/os.iso",
|
|
|
|
"http://www.hashicorp.com/dvd.iso",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:29:17 -04:00
|
|
|
func TestBuilderPrepare_MaximumOfSixtyFourAdditionalDisks(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
disks := make([]string, 65)
|
|
|
|
for i := range disks {
|
|
|
|
disks[i] = strconv.Itoa(i)
|
|
|
|
}
|
|
|
|
config["disk_additional_size"] = disks
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-10-19 18:29:17 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-28 20:51:50 -04:00
|
|
|
func TestBuilderPrepare_CommConfig(t *testing.T) {
|
|
|
|
// Test Winrm
|
|
|
|
{
|
|
|
|
config := testConfig()
|
|
|
|
config["communicator"] = "winrm"
|
|
|
|
config["winrm_username"] = "username"
|
|
|
|
config["winrm_password"] = "password"
|
|
|
|
config["winrm_host"] = "1.2.3.4"
|
|
|
|
|
|
|
|
var b Builder
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 20:51:50 -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.Comm.WinRMUser != "username" {
|
|
|
|
t.Errorf("bad winrm_username: %s", b.config.Comm.WinRMUser)
|
|
|
|
}
|
|
|
|
if b.config.Comm.WinRMPassword != "password" {
|
|
|
|
t.Errorf("bad winrm_password: %s", b.config.Comm.WinRMPassword)
|
|
|
|
}
|
|
|
|
if host := b.config.Comm.Host(); host != "1.2.3.4" {
|
|
|
|
t.Errorf("bad host: %s", host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test SSH
|
|
|
|
{
|
|
|
|
config := testConfig()
|
|
|
|
config["communicator"] = "ssh"
|
|
|
|
config["ssh_username"] = "username"
|
|
|
|
config["ssh_password"] = "password"
|
|
|
|
config["ssh_host"] = "1.2.3.4"
|
|
|
|
|
|
|
|
var b Builder
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 20:51:50 -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.Comm.SSHUsername != "username" {
|
|
|
|
t.Errorf("bad ssh_username: %s", b.config.Comm.SSHUsername)
|
|
|
|
}
|
|
|
|
if b.config.Comm.SSHPassword != "password" {
|
|
|
|
t.Errorf("bad ssh_password: %s", b.config.Comm.SSHPassword)
|
|
|
|
}
|
|
|
|
if host := b.config.Comm.Host(); host != "1.2.3.4" {
|
|
|
|
t.Errorf("bad host: %s", host)
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 18:29:17 -04:00
|
|
|
|
2017-05-28 20:51:50 -04:00
|
|
|
}
|
2017-11-05 08:58:08 -05:00
|
|
|
|
|
|
|
func TestUserVariablesInBootCommand(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2020-12-02 15:12:47 -05:00
|
|
|
config[common.UserVariablesConfigKey] = map[string]string{"test-variable": "test"}
|
2017-11-05 08:58:08 -05:00
|
|
|
config["boot_command"] = []string{"blah {{user `test-variable`}} blah"}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-11-05 08:58:08 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:50:37 -05:00
|
|
|
ui := packersdk.TestUi(t)
|
2020-11-20 13:21:29 -05:00
|
|
|
hook := &packersdk.MockHook{}
|
2017-11-05 08:58:08 -05:00
|
|
|
driver := &hypervcommon.DriverMock{}
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
2019-03-19 09:47:21 -04:00
|
|
|
state.Put("http_port", 0)
|
2020-05-19 05:49:48 -04:00
|
|
|
state.Put("http_ip", "0.0.0.0")
|
2017-11-05 08:58:08 -05:00
|
|
|
state.Put("ui", ui)
|
|
|
|
state.Put("vmName", "packer-foo")
|
|
|
|
|
|
|
|
step := &hypervcommon.StepTypeBootCommand{
|
2018-04-18 19:26:48 -04:00
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
2017-11-05 08:58:08 -05:00
|
|
|
SwitchName: b.config.SwitchName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
ret := step.Run(context.Background(), state)
|
2017-11-05 08:58:08 -05:00
|
|
|
if ret != multistep.ActionContinue {
|
2017-11-15 20:01:53 -05:00
|
|
|
t.Fatalf("should not have error: %#v", ret)
|
2017-11-05 08:58:08 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 05:59:00 -05:00
|
|
|
|
|
|
|
func TestBuilderPrepare_UseLegacyNetworkAdapter(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// should be allowed for default config
|
|
|
|
config["use_legacy_network_adapter"] = true
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2018-12-18 05:59:00 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not be allowed for gen 2
|
|
|
|
config["generation"] = 2
|
|
|
|
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2018-12-18 05:59:00 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|