File: hyperv_test.go

package info (click to toggle)
packer 1.3.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,324 kB
  • sloc: python: 619; sh: 557; makefile: 111
file content (131 lines) | stat: -rw-r--r-- 5,061 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package hyperv

import (
	"strings"
	"testing"
)

func Test_getCreateVMScript(t *testing.T) {
	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(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}

	expected := `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhd
Hyper-V\New-VHD -Path $vhdPath -ParentPath C://harddrivepath -Differencing -BlockSizeBytes 10
Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch -Version 5.0`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	// 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.
	opts.Generation = uint(2)
	scriptString, err = getCreateVMScript(&opts)
	if err == nil {
		t.Fatalf("Should have Error: %s", err.Error())
	}

	// Check VHDX conditional set
	opts.FixedVHD = false
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}

	expected = `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhdx
Hyper-V\New-VHD -Path $vhdPath -ParentPath C://harddrivepath -Differencing -BlockSizeBytes 10
Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch -Generation 2 -Version 5.0`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	// Check generation 1 no fixed VHD
	opts.FixedVHD = false
	opts.Generation = uint(1)
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}

	expected = `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhdx
Hyper-V\New-VHD -Path $vhdPath -ParentPath C://harddrivepath -Differencing -BlockSizeBytes 10
Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch -Version 5.0`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	// Check that we use generation one template even if generation is unset
	opts.Generation = uint(0)
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}
	// same "expected" as above
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	opts.Version = ""
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}
	expected = `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhdx
Hyper-V\New-VHD -Path $vhdPath -ParentPath C://harddrivepath -Differencing -BlockSizeBytes 10
Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	opts.DiffDisks = false
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}
	expected = `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhdx
Copy-Item -Path C://harddrivepath -Destination $vhdPath
Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	opts.HardDrivePath = ""
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}
	expected = `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhdx
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`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}

	opts.FixedVHD = true
	scriptString, err = getCreateVMScript(&opts)
	if err != nil {
		t.Fatalf("Error: %s", err.Error())
	}
	expected = `$vhdPath = Join-Path -Path C://mypath -ChildPath myvm.vhd
Hyper-V\New-VHD -Path $vhdPath -Fixed -SizeBytes 8192
Hyper-V\New-VM -Name myvm -Path C://mypath -MemoryStartupBytes 1024 -VHDPath $vhdPath -SwitchName hyperv-vmx-switch`
	if ok := strings.Compare(scriptString, expected); ok != 0 {
		t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, scriptString)
	}
}