File: step_create_vm_test.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (58 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download | duplicates (2)
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
package common

import (
	"context"
	"fmt"
	"testing"

	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
)

func TestStepCreateVM_impl(t *testing.T) {
	var _ multistep.Step = new(StepCreateVM)
}

func TestStepCreateVM(t *testing.T) {
	state := testState(t)
	step := new(StepCreateVM)

	step.VMName = "test-VM-Name"
	driver := state.Get("driver").(*DriverMock)

	// Test the run
	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
		t.Fatalf("Bad action: %v", action)
	}
	if _, ok := state.GetOk("error"); ok {
		t.Fatal("Should NOT have error")
	}

	// Test the driver
	if !driver.CheckVMName_Called {
		t.Fatal("Should have called CheckVMName")
	}
}

func TestStepCreateVM_CheckVMNameErr(t *testing.T) {
	state := testState(t)
	step := new(StepCreateVM)

	step.VMName = "test-VM-Name"
	driver := state.Get("driver").(*DriverMock)
	driver.CheckVMName_Err = fmt.Errorf("A virtual machine with the name is already" +
		" defined in Hyper-V. To avoid a name collision, please set your " +
		"vm_name to a unique value")

	// Test the run
	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
		t.Fatalf("Bad action: %v", action)
	}
	if _, ok := state.GetOk("error"); !ok {
		t.Fatal("Should have error")
	}

	// Test the driver
	if !driver.CheckVMName_Called {
		t.Fatal("Should have called CheckVMName")
	}
}