server: fix build error with BackupManagerTest.tryRestoreVMTestRestoreSucceeded

```
[ERROR] Tests run: 10, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.025 s <<< FAILURE! - in org.apache.cloudstack.backup.BackupManagerTest
[ERROR] tryRestoreVMTestRestoreSucceeded(org.apache.cloudstack.backup.BackupManagerTest)  Time elapsed: 0.469 s  <<< ERROR!
com.cloud.utils.exception.CloudRuntimeException: Unable to change state of volume [Mock for VolumeVO, hashCode: 220689785] to [Ready].
```
This commit is contained in:
Wei Zhou 2024-02-05 13:24:04 +01:00
parent 54225ecd15
commit c795547152
1 changed files with 44 additions and 27 deletions

View File

@ -16,6 +16,7 @@
// under the License.
package org.apache.cloudstack.backup;
import com.cloud.event.ActionEventUtils;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeApiService;
@ -32,8 +33,11 @@ import org.apache.cloudstack.api.command.admin.backup.UpdateBackupOfferingCmd;
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
@ -45,6 +49,7 @@ import static org.junit.Assert.fail;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class BackupManagerTest {
@Spy
@InjectMocks
@ -75,15 +80,11 @@ public class BackupManagerTest {
when(backupOfferingDao.findById(123l)).thenReturn(null);
BackupOfferingVO offering = Mockito.spy(BackupOfferingVO.class);
when(offering.getId()).thenReturn(1234l);
when(offering.getName()).thenCallRealMethod();
when(offering.getDescription()).thenCallRealMethod();
when(offering.isUserDrivenBackupAllowed()).thenCallRealMethod();
BackupOfferingVO offeringUpdate = Mockito.spy(BackupOfferingVO.class);
when(offeringUpdate.getId()).thenReturn(1234l);
when(offeringUpdate.getName()).thenReturn("Old name");
when(offeringUpdate.getDescription()).thenReturn("Old description");
when(backupOfferingDao.findById(1234l)).thenReturn(offering);
when(backupOfferingDao.createForUpdate(1234l)).thenReturn(offeringUpdate);
@ -218,18 +219,25 @@ public class BackupManagerTest {
VMInstanceVO vm = Mockito.mock(VMInstanceVO.class);
BackupVO backup = Mockito.mock(BackupVO.class);
Mockito.when(volumeDao.findIncludingRemovedByInstanceAndType(1L, null)).thenReturn(Collections.singletonList(volumeVO));
Mockito.when(virtualMachineManager.stateTransitTo(Mockito.eq(vm), Mockito.eq(VirtualMachine.Event.RestoringRequested), Mockito.any())).thenReturn(true);
Mockito.when(volumeApiService.stateTransitTo(Mockito.eq(volumeVO), Mockito.eq(Volume.Event.RestoreRequested))).thenReturn(true);
try (MockedStatic<ActionEventUtils> utils = Mockito.mockStatic(ActionEventUtils.class)) {
Mockito.when(ActionEventUtils.onStartedActionEvent(Mockito.anyLong(), Mockito.anyLong(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(),
Mockito.eq(true), Mockito.eq(0))).thenReturn(1L);
Mockito.when(ActionEventUtils.onCompletedActionEvent(Mockito.anyLong(), Mockito.anyLong(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(),
Mockito.anyString(), Mockito.eq(0))).thenReturn(2L);
Mockito.when(volumeDao.findIncludingRemovedByInstanceAndType(1L, null)).thenReturn(Collections.singletonList(volumeVO));
Mockito.when(virtualMachineManager.stateTransitTo(Mockito.eq(vm), Mockito.eq(VirtualMachine.Event.RestoringRequested), Mockito.any())).thenReturn(true);
Mockito.when(volumeApiService.stateTransitTo(Mockito.eq(volumeVO), Mockito.eq(Volume.Event.RestoreRequested))).thenReturn(true);
Mockito.when(vm.getId()).thenReturn(1L);
Mockito.when(offering.getProvider()).thenReturn("veeam");
Mockito.doReturn(backupProvider).when(backupManager).getBackupProvider("veeam");
Mockito.when(backupProvider.restoreVMFromBackup(vm, backup)).thenReturn(true);
Mockito.when(vm.getId()).thenReturn(1L);
Mockito.when(offering.getProvider()).thenReturn("veeam");
Mockito.doReturn(backupProvider).when(backupManager).getBackupProvider("veeam");
Mockito.when(backupProvider.restoreVMFromBackup(vm, backup)).thenReturn(true);
backupManager.tryRestoreVM(backup, vm, offering, "Nothing to write here.");
backupManager.tryRestoreVM(backup, vm, offering, "Nothing to write here.");
}
}
@Test
@ -239,21 +247,30 @@ public class BackupManagerTest {
VMInstanceVO vm = Mockito.mock(VMInstanceVO.class);
BackupVO backup = Mockito.mock(BackupVO.class);
Mockito.when(volumeDao.findIncludingRemovedByInstanceAndType(1L, null)).thenReturn(Collections.singletonList(volumeVO));
Mockito.when(virtualMachineManager.stateTransitTo(Mockito.eq(vm), Mockito.eq(VirtualMachine.Event.RestoringRequested), Mockito.any())).thenReturn(true);
Mockito.when(volumeApiService.stateTransitTo(Mockito.eq(volumeVO), Mockito.eq(Volume.Event.RestoreRequested))).thenReturn(true);
Mockito.when(virtualMachineManager.stateTransitTo(Mockito.eq(vm), Mockito.eq(VirtualMachine.Event.RestoringFailed), Mockito.any())).thenReturn(true);
Mockito.when(volumeApiService.stateTransitTo(Mockito.eq(volumeVO), Mockito.eq(Volume.Event.RestoreFailed))).thenReturn(true);
try (MockedStatic<ActionEventUtils> utils = Mockito.mockStatic(ActionEventUtils.class)) {
Mockito.when(ActionEventUtils.onStartedActionEvent(Mockito.anyLong(), Mockito.anyLong(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(),
Mockito.eq(true), Mockito.eq(0))).thenReturn(1L);
Mockito.when(ActionEventUtils.onCompletedActionEvent(Mockito.anyLong(), Mockito.anyLong(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(),
Mockito.anyString(), Mockito.eq(0))).thenReturn(2L);
Mockito.when(vm.getId()).thenReturn(1L);
Mockito.when(offering.getProvider()).thenReturn("veeam");
Mockito.doReturn(backupProvider).when(backupManager).getBackupProvider("veeam");
Mockito.when(backupProvider.restoreVMFromBackup(vm, backup)).thenReturn(false);
try {
backupManager.tryRestoreVM(backup, vm, offering, "Checking message error.");
fail("An exception is needed.");
} catch (CloudRuntimeException e) {
assertEquals("Error restoring VM from backup [Checking message error.].", e.getMessage());
Mockito.when(volumeDao.findIncludingRemovedByInstanceAndType(1L, null)).thenReturn(Collections.singletonList(volumeVO));
Mockito.when(virtualMachineManager.stateTransitTo(Mockito.eq(vm), Mockito.eq(VirtualMachine.Event.RestoringRequested), Mockito.any())).thenReturn(true);
Mockito.when(volumeApiService.stateTransitTo(Mockito.eq(volumeVO), Mockito.eq(Volume.Event.RestoreRequested))).thenReturn(true);
Mockito.when(virtualMachineManager.stateTransitTo(Mockito.eq(vm), Mockito.eq(VirtualMachine.Event.RestoringFailed), Mockito.any())).thenReturn(true);
Mockito.when(volumeApiService.stateTransitTo(Mockito.eq(volumeVO), Mockito.eq(Volume.Event.RestoreFailed))).thenReturn(true);
Mockito.when(vm.getId()).thenReturn(1L);
Mockito.when(offering.getProvider()).thenReturn("veeam");
Mockito.doReturn(backupProvider).when(backupManager).getBackupProvider("veeam");
Mockito.when(backupProvider.restoreVMFromBackup(vm, backup)).thenReturn(false);
try {
backupManager.tryRestoreVM(backup, vm, offering, "Checking message error.");
fail("An exception is needed.");
} catch (CloudRuntimeException e) {
assertEquals("Error restoring VM from backup [Checking message error.].", e.getMessage());
}
}
}
}