2017-03-12 07:31:31 -04:00
|
|
|
package vmcx
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2017-11-05 08:55:56 -05:00
|
|
|
"fmt"
|
2017-03-12 07:31:31 -04:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-22 20:21:10 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2017-11-05 08:55:56 -05:00
|
|
|
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-21 12:29:26 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-12 07:31:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"iso_checksum": "foo",
|
|
|
|
"iso_checksum_type": "md5",
|
|
|
|
"iso_url": "http://www.packer.io",
|
|
|
|
"shutdown_command": "yes",
|
|
|
|
"ssh_username": "foo",
|
2018-10-17 06:23:01 -04:00
|
|
|
"switch_name": "switch", // to avoid using builder.detectSwitchName which can lock down in travis-ci
|
2019-03-29 18:52:41 -04:00
|
|
|
"memory": 64,
|
2017-03-12 07:31:31 -04:00
|
|
|
"guest_additions_mode": "none",
|
2018-07-19 13:53:42 -04:00
|
|
|
"clone_from_vmcx_path": "generated",
|
2017-03-12 07:31:31 -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()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-21 12:29:26 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-21 12:29:26 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-03-12 07:31:31 -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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
2017-03-12 07:31:31 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-21 12:29:26 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
2017-03-12 07:31:31 -04:00
|
|
|
if err != nil {
|
2017-05-21 12:29:26 -04:00
|
|
|
t.Fatalf("err: %s", err)
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
2017-05-21 12:29:26 -04:00
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-03-12 07:31:31 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 22:12:35 -04:00
|
|
|
func TestBuilderPrepare_CloneFromExistingMachineOrImportFromExportedMachineSettingsRequired(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2018-07-19 13:53:42 -04:00
|
|
|
delete(config, "clone_from_vmcx_path")
|
2017-05-28 22:12:35 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 22:12:35 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ExportedMachinePathDoesNotExist(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-28 22:12:35 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete the folder immediately
|
|
|
|
os.RemoveAll(td)
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-28 22:12:35 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 22:12:35 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ExportedMachinePathExists(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-28 22:12:35 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Only delete afterwards
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-28 22:12:35 -04:00
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 22:12:35 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
func disabled_TestBuilderPrepare_CloneFromVmSettingUsedSoNoCloneFromVmcxPathRequired(t *testing.T) {
|
2017-05-28 22:12:35 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2018-07-19 13:53:42 -04:00
|
|
|
delete(config, "clone_from_vmcx_path")
|
2017-05-28 22:12:35 -04:00
|
|
|
|
|
|
|
config["clone_from_vm_name"] = "test_machine_name_that_does_not_exist"
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-05-28 22:12:35 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
} else {
|
|
|
|
errorMessage := err.Error()
|
2018-07-09 12:20:38 -04:00
|
|
|
if errorMessage != "1 error(s) occurred:\n\n* Virtual machine 'test_machine_name_that_does_not_exist' "+
|
|
|
|
"to clone from does not exist." {
|
2017-05-28 22:12:35 -04:00
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
func TestBuilderPrepare_ISOChecksum(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-21 12:29:26 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-21 12:29:26 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// Test bad
|
|
|
|
config["iso_checksum"] = ""
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-03-12 07:31:31 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["iso_checksum"] = "FOo"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-03-12 07:31:31 -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()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-21 12:29:26 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-21 12:29:26 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// Test bad
|
|
|
|
config["iso_checksum_type"] = ""
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-03-12 07:31:31 -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)
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["iso_checksum_type"] = "mD5"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-03-12 07:31:31 -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.ISOChecksumType != "md5" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test none
|
|
|
|
config["iso_checksum_type"] = "none"
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err = b.Prepare(config)
|
2017-03-12 07:31:31 -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.ISOChecksumType != "none" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOUrl(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2017-05-21 12:29:26 -04:00
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-21 12:29:26 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-21 12:29:26 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
delete(config, "iso_url")
|
|
|
|
delete(config, "iso_urls")
|
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// Test both empty (should be allowed, as we cloning a vm so we probably don't need an ISO file)
|
2017-03-12 07:31:31 -04:00
|
|
|
config["iso_url"] = ""
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warns, err := b.Prepare(config)
|
2017-03-12 07:31:31 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
2017-05-21 12:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have an error")
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2017-03-12 07:31:31 -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)
|
2017-03-12 07:31:31 -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)
|
2017-03-12 07:31:31 -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
|
|
|
|
|
|
|
func TestBuilderPrepare_FloppyFiles(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-28 20:51:50 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-28 20:51:50 -04:00
|
|
|
|
|
|
|
delete(config, "floppy_files")
|
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("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)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-28 20:51:50 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-28 20:51:50 -04:00
|
|
|
|
|
|
|
config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"}
|
|
|
|
b = Builder{}
|
2019-12-17 00:23:05 -05:00
|
|
|
_, _, errs := b.Prepare(config)
|
2017-05-28 20:51:50 -04:00
|
|
|
if errs == nil {
|
|
|
|
t.Fatalf("Nonexistent floppies should trigger multierror")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs.(*packer.MultiError).Errors) != 2 {
|
|
|
|
t.Fatalf("Multierror should work and report 2 errors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_CommConfig(t *testing.T) {
|
|
|
|
// Test Winrm
|
|
|
|
{
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-28 20:51:50 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-28 20:51:50 -04:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-05-28 20:51:50 -04:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-05-28 20:51:50 -04:00
|
|
|
|
|
|
|
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-11-05 08:55:56 -05:00
|
|
|
|
|
|
|
func TestUserVariablesInBootCommand(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
//Create vmcx folder
|
2017-11-05 08:55:56 -05:00
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
2018-07-19 13:53:42 -04:00
|
|
|
config["clone_from_vmcx_path"] = td
|
2017-11-05 08:55:56 -05:00
|
|
|
|
|
|
|
config[packer.UserVariablesConfigKey] = map[string]string{"test-variable": "test"}
|
|
|
|
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:55:56 -05:00
|
|
|
if len(warns) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warns)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := packer.TestUi(t)
|
|
|
|
hook := &packer.MockHook{}
|
|
|
|
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)
|
2017-11-05 08:55:56 -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:55:56 -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:55:56 -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:55:56 -05:00
|
|
|
}
|
|
|
|
}
|