|
21 | 21 |
|
22 | 22 | import java.util.Collections; |
23 | 23 | import java.util.List; |
| 24 | +import java.util.Objects; |
24 | 25 | import java.util.Optional; |
25 | 26 |
|
26 | | -import org.apache.cloudstack.backup.dao.BackupDao; |
27 | | -import org.apache.cloudstack.backup.dao.BackupRepositoryDao; |
28 | | -import org.apache.cloudstack.backup.dao.BackupOfferingDao; |
29 | 27 | import org.junit.Assert; |
30 | 28 | import org.junit.Test; |
31 | 29 | import org.junit.runner.RunWith; |
|
39 | 37 | import com.cloud.agent.AgentManager; |
40 | 38 | import com.cloud.exception.AgentUnavailableException; |
41 | 39 | import com.cloud.exception.OperationTimedoutException; |
| 40 | +import com.cloud.host.Host; |
42 | 41 | import com.cloud.host.HostVO; |
43 | 42 | import com.cloud.host.Status; |
44 | 43 | import com.cloud.host.dao.HostDao; |
|
51 | 50 | import com.cloud.vm.VMInstanceVO; |
52 | 51 | import com.cloud.vm.dao.VMInstanceDao; |
53 | 52 |
|
| 53 | +import org.apache.cloudstack.backup.dao.BackupDao; |
| 54 | +import org.apache.cloudstack.backup.dao.BackupRepositoryDao; |
| 55 | +import org.apache.cloudstack.backup.dao.BackupOfferingDao; |
| 56 | +import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
| 57 | +import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
| 58 | + |
54 | 59 | @RunWith(MockitoJUnitRunner.class) |
55 | 60 | public class NASBackupProviderTest { |
56 | 61 | @Spy |
@@ -84,6 +89,9 @@ public class NASBackupProviderTest { |
84 | 89 | @Mock |
85 | 90 | private ResourceManager resourceManager; |
86 | 91 |
|
| 92 | + @Mock |
| 93 | + private PrimaryDataStoreDao storagePoolDao; |
| 94 | + |
87 | 95 | @Test |
88 | 96 | public void testDeleteBackup() throws OperationTimedoutException, AgentUnavailableException { |
89 | 97 | Long hostId = 1L; |
@@ -227,4 +235,118 @@ public void takeBackupSuccessfully() throws AgentUnavailableException, Operation |
227 | 235 | Mockito.verify(backupDao).update(Mockito.anyLong(), Mockito.any(BackupVO.class)); |
228 | 236 | Mockito.verify(agentManager).send(anyLong(), Mockito.any(TakeBackupCommand.class)); |
229 | 237 | } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void testGetVMHypervisorHost() { |
| 241 | + Long hostId = 1L; |
| 242 | + Long vmId = 1L; |
| 243 | + Long zoneId = 1L; |
| 244 | + |
| 245 | + VMInstanceVO vm = mock(VMInstanceVO.class); |
| 246 | + Mockito.when(vm.getLastHostId()).thenReturn(hostId); |
| 247 | + |
| 248 | + HostVO host = mock(HostVO.class); |
| 249 | + Mockito.when(host.getId()).thenReturn(hostId); |
| 250 | + Mockito.when(host.getStatus()).thenReturn(Status.Up); |
| 251 | + Mockito.when(hostDao.findById(hostId)).thenReturn(host); |
| 252 | + |
| 253 | + Host result = nasBackupProvider.getVMHypervisorHost(vm); |
| 254 | + |
| 255 | + Assert.assertNotNull(result); |
| 256 | + Assert.assertTrue(Objects.equals(hostId, result.getId())); |
| 257 | + Mockito.verify(hostDao).findById(hostId); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + public void testGetVMHypervisorHostWithHostDown() { |
| 262 | + Long hostId = 1L; |
| 263 | + Long clusterId = 2L; |
| 264 | + Long vmId = 1L; |
| 265 | + Long zoneId = 1L; |
| 266 | + |
| 267 | + VMInstanceVO vm = mock(VMInstanceVO.class); |
| 268 | + Mockito.when(vm.getLastHostId()).thenReturn(hostId); |
| 269 | + |
| 270 | + HostVO downHost = mock(HostVO.class); |
| 271 | + Mockito.when(downHost.getStatus()).thenReturn(Status.Down); |
| 272 | + Mockito.when(downHost.getClusterId()).thenReturn(clusterId); |
| 273 | + Mockito.when(hostDao.findById(hostId)).thenReturn(downHost); |
| 274 | + |
| 275 | + HostVO upHostInCluster = mock(HostVO.class); |
| 276 | + Mockito.when(upHostInCluster.getId()).thenReturn(3L); |
| 277 | + Mockito.when(upHostInCluster.getStatus()).thenReturn(Status.Up); |
| 278 | + Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(List.of(upHostInCluster)); |
| 279 | + |
| 280 | + Host result = nasBackupProvider.getVMHypervisorHost(vm); |
| 281 | + |
| 282 | + Assert.assertNotNull(result); |
| 283 | + Assert.assertTrue(Objects.equals(Long.valueOf(3L), result.getId())); |
| 284 | + Mockito.verify(hostDao).findById(hostId); |
| 285 | + Mockito.verify(hostDao).findHypervisorHostInCluster(clusterId); |
| 286 | + } |
| 287 | + |
| 288 | + @Test |
| 289 | + public void testGetVMHypervisorHostWithUpHostViaRootVolumeCluster() { |
| 290 | + Long vmId = 1L; |
| 291 | + Long zoneId = 1L; |
| 292 | + Long clusterId = 2L; |
| 293 | + Long poolId = 3L; |
| 294 | + |
| 295 | + VMInstanceVO vm = mock(VMInstanceVO.class); |
| 296 | + Mockito.when(vm.getLastHostId()).thenReturn(null); |
| 297 | + Mockito.when(vm.getId()).thenReturn(vmId); |
| 298 | + |
| 299 | + VolumeVO rootVolume = mock(VolumeVO.class); |
| 300 | + Mockito.when(rootVolume.getPoolId()).thenReturn(poolId); |
| 301 | + Mockito.when(volumeDao.getInstanceRootVolume(vmId)).thenReturn(rootVolume); |
| 302 | + |
| 303 | + StoragePoolVO storagePool = mock(StoragePoolVO.class); |
| 304 | + Mockito.when(storagePool.getClusterId()).thenReturn(clusterId); |
| 305 | + Mockito.when(storagePoolDao.findById(poolId)).thenReturn(storagePool); |
| 306 | + |
| 307 | + HostVO upHostInCluster = mock(HostVO.class); |
| 308 | + Mockito.when(upHostInCluster.getId()).thenReturn(4L); |
| 309 | + Mockito.when(upHostInCluster.getStatus()).thenReturn(Status.Up); |
| 310 | + Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(List.of(upHostInCluster)); |
| 311 | + |
| 312 | + Host result = nasBackupProvider.getVMHypervisorHost(vm); |
| 313 | + |
| 314 | + Assert.assertNotNull(result); |
| 315 | + Assert.assertTrue(Objects.equals(Long.valueOf(4L), result.getId())); |
| 316 | + Mockito.verify(volumeDao).getInstanceRootVolume(vmId); |
| 317 | + Mockito.verify(storagePoolDao).findById(poolId); |
| 318 | + Mockito.verify(hostDao).findHypervisorHostInCluster(clusterId); |
| 319 | + } |
| 320 | + |
| 321 | + @Test |
| 322 | + public void testGetVMHypervisorHostFallbackToZoneWideKVMHost() { |
| 323 | + Long hostId = 1L; |
| 324 | + Long clusterId = 2L; |
| 325 | + Long vmId = 1L; |
| 326 | + Long zoneId = 1L; |
| 327 | + |
| 328 | + VMInstanceVO vm = mock(VMInstanceVO.class); |
| 329 | + Mockito.when(vm.getLastHostId()).thenReturn(hostId); |
| 330 | + Mockito.when(vm.getDataCenterId()).thenReturn(zoneId); |
| 331 | + |
| 332 | + HostVO downHost = mock(HostVO.class); |
| 333 | + Mockito.when(downHost.getStatus()).thenReturn(Status.Down); |
| 334 | + Mockito.when(downHost.getClusterId()).thenReturn(clusterId); |
| 335 | + Mockito.when(hostDao.findById(hostId)).thenReturn(downHost); |
| 336 | + |
| 337 | + Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(Collections.emptyList()); |
| 338 | + |
| 339 | + HostVO fallbackHost = mock(HostVO.class); |
| 340 | + Mockito.when(fallbackHost.getId()).thenReturn(5L); |
| 341 | + Mockito.when(resourceManager.findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, zoneId)) |
| 342 | + .thenReturn(fallbackHost); |
| 343 | + |
| 344 | + Host result = nasBackupProvider.getVMHypervisorHost(vm); |
| 345 | + |
| 346 | + Assert.assertNotNull(result); |
| 347 | + Assert.assertTrue(Objects.equals(Long.valueOf(5L), result.getId())); |
| 348 | + Mockito.verify(hostDao).findById(hostId); |
| 349 | + Mockito.verify(hostDao).findHypervisorHostInCluster(clusterId); |
| 350 | + Mockito.verify(resourceManager).findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, zoneId); |
| 351 | + } |
230 | 352 | } |
0 commit comments