@@ -76,15 +76,31 @@ func TestFindVM_NotCreated(t *testing.T) {
7676 require .ErrorIs (t , err , ErrVMNotCreated )
7777}
7878
79+ func TestFindVM_NotInitialized (t * testing.T ) {
80+ ctx := context .TODO ()
81+ machineScope , proxmoxClient , _ := setupReconcilerTest (t )
82+ vm := newRunningVM ()
83+ vm .Name = "bar"
84+ machineScope .ProxmoxMachine .Spec .VirtualMachineID = ptr .To (int64 (vm .VMID ))
85+ machineScope .ProxmoxMachine .Status .ProxmoxNode = ptr .To ("node2" )
86+
87+ proxmoxClient .EXPECT ().GetVM (ctx , "node2" , int64 (123 )).Return (vm , nil ).Once ()
88+
89+ _ , err := FindVM (ctx , machineScope )
90+ require .ErrorIs (t , err , ErrVMNotInitialized )
91+ }
92+
7993func TestUpdateVMLocation_MissingName (t * testing.T ) {
8094 ctx := context .TODO ()
8195 machineScope , proxmoxClient , _ := setupReconcilerTest (t )
8296 vm := newRunningVM ()
8397 vmr := newVMResource ()
8498 vmr .Name = ""
99+ vm .Name = ""
85100 machineScope .ProxmoxMachine .Spec .VirtualMachineID = ptr .To (int64 (vm .VMID ))
86101
87102 proxmoxClient .EXPECT ().FindVMResource (ctx , uint64 (123 )).Return (vmr , nil ).Once ()
103+ proxmoxClient .EXPECT ().GetVM (ctx , "node1" , int64 (123 )).Return (vm , nil ).Once ()
88104
89105 require .Error (t , updateVMLocation (ctx , machineScope ))
90106}
@@ -94,10 +110,13 @@ func TestUpdateVMLocation_NameMismatch(t *testing.T) {
94110 machineScope , proxmoxClient , _ := setupReconcilerTest (t )
95111 vm := newRunningVM ()
96112 vmr := newVMResource ()
97- vmr .Name = "foo"
113+ name := "foo"
114+ vmr .Name = name
115+ vm .Name = name
98116 machineScope .ProxmoxMachine .Spec .VirtualMachineID = ptr .To (int64 (vm .VMID ))
99117
100118 proxmoxClient .EXPECT ().FindVMResource (ctx , uint64 (123 )).Return (vmr , nil ).Once ()
119+ proxmoxClient .EXPECT ().GetVM (ctx , "node1" , int64 (123 )).Return (vm , nil ).Once ()
101120
102121 require .Error (t , updateVMLocation (ctx , machineScope ))
103122 require .True (t , machineScope .HasFailed (), "expected failureReason and failureMessage to be set" )
@@ -116,6 +135,7 @@ func TestUpdateVMLocation_UpdateNode(t *testing.T) {
116135 }, false )
117136
118137 proxmoxClient .EXPECT ().FindVMResource (ctx , uint64 (123 )).Return (vmr , nil ).Once ()
138+ proxmoxClient .EXPECT ().GetVM (ctx , "node1" , int64 (123 )).Return (vm , nil ).Once ()
119139
120140 require .NoError (t , updateVMLocation (ctx , machineScope ))
121141 require .Equal (t , vmr .Node , * machineScope .ProxmoxMachine .Status .ProxmoxNode )
@@ -137,11 +157,14 @@ func TestUpdateVMLocation_WithoutTaskNameMismatch(t *testing.T) {
137157 machineScope , proxmoxClient , _ := setupReconcilerTest (t )
138158 vm := newRunningVM ()
139159 vmr := newVMResource ()
140- vmr .Name = "foo"
160+ name := "foo"
161+ vmr .Name = name
162+ vm .Name = name
141163 machineScope .ProxmoxMachine .Spec .VirtualMachineID = ptr .To (int64 (vm .VMID ))
142164 machineScope .ProxmoxMachine .Status .TaskRef = nil
143165
144166 proxmoxClient .EXPECT ().FindVMResource (ctx , uint64 (123 )).Return (vmr , nil ).Once ()
167+ proxmoxClient .EXPECT ().GetVM (ctx , "node1" , int64 (123 )).Return (vm , nil ).Once ()
145168
146169 require .Error (t , updateVMLocation (ctx , machineScope ))
147170 require .True (t , machineScope .HasFailed (), "expected failureReason and failureMessage to be set" )
0 commit comments