@@ -1771,6 +1771,7 @@ public void testPersistDomainForKvmForStoppedVmSuccess() throws AgentUnavailable
17711771 doReturn (vmTO ).when (virtualMachineManagerImpl ).prepVmSpecForUnmanageCmd (vmInstanceVoMockId , 1L );
17721772 UnmanageInstanceAnswer successAnswer = new UnmanageInstanceAnswer (null , true , "success" );
17731773 when (agentManagerMock .send (anyLong (), any (UnmanageInstanceCommand .class ))).thenReturn (successAnswer );
1774+ when (virtualMachineManagerImpl .findClusterAndHostIdForVm (vmInstanceVoMockId )).thenReturn (new Pair <>(1L , 1L ));
17741775 virtualMachineManagerImpl .persistDomainForKVM (vmInstanceMock );
17751776 ArgumentCaptor <Long > hostIdCaptor = ArgumentCaptor .forClass (Long .class );
17761777 ArgumentCaptor <UnmanageInstanceCommand > commandCaptor = ArgumentCaptor .forClass (UnmanageInstanceCommand .class );
@@ -1783,6 +1784,41 @@ public void testPersistDomainForKvmForStoppedVmSuccess() throws AgentUnavailable
17831784 verify (virtualMachineManagerImpl ).prepVmSpecForUnmanageCmd (vmInstanceVoMockId , 1L );
17841785 }
17851786
1787+
1788+ @ Test
1789+ public void testPersistDomainForKvmForStoppedVmHostRemoved () throws AgentUnavailableException , OperationTimedoutException {
1790+ when (vmInstanceMock .getState ()).thenReturn (VirtualMachine .State .Stopped );
1791+ when (vmInstanceMock .getLastHostId ()).thenReturn (1L );
1792+ when (vmInstanceMock .getDataCenterId ()).thenReturn (zoneMockId );
1793+ VirtualMachineTO vmTO = new VirtualMachineTO () {};
1794+ vmTO .setName (vmName );
1795+ doReturn (vmTO ).when (virtualMachineManagerImpl ).prepVmSpecForUnmanageCmd (vmInstanceVoMockId , 1L );
1796+ UnmanageInstanceAnswer successAnswer = new UnmanageInstanceAnswer (null , true , "success" );
1797+ when (agentManagerMock .send (anyLong (), any (UnmanageInstanceCommand .class ))).thenReturn (successAnswer );
1798+ when (virtualMachineManagerImpl .findClusterAndHostIdForVm (vmInstanceVoMockId )).thenReturn (new Pair <>(clusterMockId , null ));
1799+ when (hostDaoMock .listByDataCenterIdAndHypervisorType (zoneMockId , HypervisorType .KVM )).thenReturn (List .of (hostMock ));
1800+ virtualMachineManagerImpl .persistDomainForKVM (vmInstanceMock );
1801+ ArgumentCaptor <Long > hostIdCaptor = ArgumentCaptor .forClass (Long .class );
1802+ ArgumentCaptor <UnmanageInstanceCommand > commandCaptor = ArgumentCaptor .forClass (UnmanageInstanceCommand .class );
1803+ verify (agentManagerMock ).send (hostIdCaptor .capture (), commandCaptor .capture ());
1804+ assertEquals (1L , hostIdCaptor .getValue ().longValue ());
1805+ UnmanageInstanceCommand sentCommand = commandCaptor .getValue ();
1806+ assertNotNull (sentCommand .getVm ());
1807+ assertEquals (vmTO , sentCommand .getVm ());
1808+ assertEquals (vmName , sentCommand .getInstanceName ());
1809+ verify (virtualMachineManagerImpl ).prepVmSpecForUnmanageCmd (vmInstanceVoMockId , 1L );
1810+ }
1811+
1812+ @ Test
1813+ public void testPersistDomainForKvmForStoppedVmNoHost () throws AgentUnavailableException , OperationTimedoutException {
1814+ when (vmInstanceMock .getState ()).thenReturn (VirtualMachine .State .Stopped );
1815+ when (vmInstanceMock .getLastHostId ()).thenReturn (1L );
1816+ VirtualMachineTO vmTO = new VirtualMachineTO () {};
1817+ vmTO .setName (vmName );
1818+ CloudRuntimeException exception = assertThrows (CloudRuntimeException .class , () -> virtualMachineManagerImpl .persistDomainForKVM (vmInstanceMock ));
1819+ assertEquals ("No available host to persist domainXML for Instance: " + vmName , exception .getMessage ());
1820+ }
1821+
17861822 @ Test
17871823 public void testPersistDomainForKvmForRunningVmAgentFailure () throws AgentUnavailableException , OperationTimedoutException {
17881824 when (vmInstanceMock .getState ()).thenReturn (VirtualMachine .State .Running );
0 commit comments