Test settings for clone from vm and import vmxc from path

This commit is contained in:
Taliesin Sisson 2017-05-29 03:12:35 +01:00 committed by Vijaya Bhaskar Reddy Kondreddi
parent 8232759397
commit 628116f4c4
2 changed files with 84 additions and 6 deletions

View File

@ -16,7 +16,6 @@ import (
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"path/filepath"
) )
const ( const (
@ -126,11 +125,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx) isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...) warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...) errs = packer.MultiErrorAppend(errs, isoErrs...)
extension := strings.ToLower(filepath.Ext(b.config.ISOConfig.ISOUrls[0]))
if extension == "vhd" || extension == "vhdx" {
b.config.ISOConfig.TargetExtension = extension
}
} }
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)

View File

@ -80,6 +80,90 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) {
} }
} }
func TestBuilderPrepare_CloneFromExistingMachineOrImportFromExportedMachineSettingsRequired(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "clone_from_vmxc_path")
warns, err := b.Prepare(config)
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()
//Create vmxc folder
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
//Delete the folder immediately
os.RemoveAll(td)
config["clone_from_vmxc_path"] = td
warns, err := b.Prepare(config)
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()
//Create vmxc folder
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
//Only delete afterwards
defer os.RemoveAll(td)
config["clone_from_vmxc_path"] = td
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}
}
func TestBuilderPrepare_CloneFromVmSettingUsedSoNoCloneFromVmxcPathRequired(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "clone_from_vmxc_path")
config["clone_from_vm_name"] = "test_machine_name_that_does_not_exist"
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should have error")
} else {
errorMessage := err.Error()
if errorMessage != "1 error(s) occurred:\n\n* Virtual machine 'test_machine_name_that_does_not_exist' to clone from does not exist." {
t.Fatalf("should not have error: %s", err)
}
}
}
func TestBuilderPrepare_ISOChecksum(t *testing.T) { func TestBuilderPrepare_ISOChecksum(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()