Skip to content

Commit

Permalink
use script options struct rather than passing all the variables aroun…
Browse files Browse the repository at this point in the history
…d an extra time
  • Loading branch information
SwampDragons committed Jan 8, 2019
1 parent a1b3b63 commit 9557f3e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 72 deletions.
45 changes: 20 additions & 25 deletions common/powershell/hyperv/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,15 @@ Hyper-V\Set-VMFloppyDiskDrive -VMName $vmName -Path $null
// Hyper-V\New-VHD -Path $vhdPath -SizeBytes 8192 -BlockSizeBytes 10
// Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch
//
func getCreateVMScript(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool, version string) (string, error) {
func getCreateVMScript(opts *scriptOptions) (string, error) {

if fixedVHD && generation == 2 {
if opts.FixedVHD && opts.Generation == 2 {
return "", fmt.Errorf("Generation 2 VMs don't support fixed disks.")
}

vhdx := vmName + ".vhdx"
if fixedVHD {
vhdx = vmName + ".vhd"
}

opts := scriptOptions{
Version: version,
VMName: vmName,
VHDX: vhdx,
Path: path,
HardDrivePath: harddrivePath,
MemoryStartupBytes: ram,
NewVHDSizeBytes: diskSize,
VHDBlockSizeBytes: diskBlockSize,
SwitchName: switchName,
Generation: generation,
DiffDisks: diffDisks,
FixedVHD: fixedVHD,
opts.VHDX = opts.VMName + ".vhdx"
if opts.FixedVHD {
opts.VHDX = opts.VMName + ".vhd"
}

var tpl = template.Must(template.New("createVM").Parse(`
Expand Down Expand Up @@ -304,9 +287,21 @@ func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram
diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool, version string) error {

script, err := getCreateVMScript(vmName, path, harddrivePath, ram,
diskSize, diskBlockSize, switchName, generation,
diffDisks, fixedVHD, version)
opts := scriptOptions{
Version: version,
VMName: vmName,
Path: path,
HardDrivePath: harddrivePath,
MemoryStartupBytes: ram,
NewVHDSizeBytes: diskSize,
VHDBlockSizeBytes: diskBlockSize,
SwitchName: switchName,
Generation: generation,
DiffDisks: diffDisks,
FixedVHD: fixedVHD,
}

script, err := getCreateVMScript(&opts)
if err != nil {
return err
}
Expand Down
78 changes: 31 additions & 47 deletions common/powershell/hyperv/hyperv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import (
)

func Test_getCreateVMScript(t *testing.T) {
vmName := "myvm"
path := "C://mypath"
harddrivepath := "C://harddrivepath"
ram := int64(1024)
disksize := int64(8192)
diskBlockSize := int64(10)
switchName := "hyperv-vmx-switch"
generation := uint(1)
diffdisks := true
fixedVHD := true
version := "5.0"
opts := scriptOptions{
Version: "5.0",
VMName: "myvm",
Path: "C://mypath",
HardDrivePath: "C://harddrivepath",
MemoryStartupBytes: int64(1024),
NewVHDSizeBytes: int64(8192),
VHDBlockSizeBytes: int64(10),
SwitchName: "hyperv-vmx-switch",
Generation: uint(1),
DiffDisks: true,
FixedVHD: true,
}

// Check Fixed VHD conditional set
scriptString, err := getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
scriptString, err := getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -35,19 +35,15 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh

// We should never get here thanks to good template validation, but it's
// good to fail rather than trying to run the ps script and erroring.
generation = uint(2)
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.Generation = uint(2)
scriptString, err = getCreateVMScript(&opts)
if err == nil {
t.Fatalf("Should have Error: %s", err.Error())
}

// Check VHDX conditional set
fixedVHD = false
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.FixedVHD = false
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -60,11 +56,9 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
}

// Check generation 1 no fixed VHD
fixedVHD = false
generation = uint(1)
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.FixedVHD = false
opts.Generation = uint(1)
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -77,10 +71,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
}

// Check that we use generation one template even if generation is unset
generation = uint(0)
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.Generation = uint(0)
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -89,10 +81,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
}

version = ""
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.Version = ""
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -103,10 +93,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
}

diffdisks = false
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.DiffDisks = false
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -117,10 +105,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
}

harddrivepath = ""
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.HardDrivePath = ""
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -131,10 +117,8 @@ Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vh
t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
}

fixedVHD = true
scriptString, err = getCreateVMScript(vmName, path, harddrivepath, ram,
disksize, diskBlockSize, switchName, generation, diffdisks, fixedVHD,
version)
opts.FixedVHD = true
scriptString, err = getCreateVMScript(&opts)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand Down

0 comments on commit 9557f3e

Please sign in to comment.