AdministrationAgent implements InitializingBean { protected void migrateVM(VMInstanceVO vm, HostVO host) { long vmId = vm.getId(); long targetHostId = host.getId(); Long sourceHostId = vm.getHostId(); if (sourceHostId == targetHostId) { logger.debug(String.format("VM [id=%d] is not being migrated hence it was mapped to the host it is running.", vmId)); return; } try { logger.info(String.format("VM [id=%d] is migrating from host[Id=%d] to host[Id=%d].", vmId, sourceHostId, targetHostId)); userVmService.migrateVirtualMachine(vmId, host); logger.info(String.format("VM [id=%d] was migrated from host[Id=%d] to host[Id=%d].", vmId, sourceHostId, targetHostId)); } catch (Exception e) { logger.debug(String.format("Could not migrate VM [id=%d] to host [id=%d, hostName=%s], from host [id=%d]", vmId, targetHostId, host.getName(), sourceHostId), e); } }  void receiveClusterToBeManaged(Long clusterId); Long searchClusterToBeManaged(); List<ClusterResourcesUp> createAllClustersResourcesUp(); @Override void afterPropertiesSet();  }
@Test public void migrateVMTestTargetHostEqualsVmHost() throws Exception { VMInstanceVO vmInstanceVo = migrateVMSetupVmInstanceVo(1l); HostVO hostVo = migrateVMSetupHostVo(1l); Mockito.doReturn(vmInstanceVo).when(userVmService).migrateVirtualMachine(Mockito.anyLong(), Mockito.any(HostVO.class)); Mockito.doReturn(new HostVO("")).when(hostService).findHostById(Mockito.anyLong()); administrationAgent.migrateVM(vmInstanceVo, hostVo); Mockito.verify(userVmService, Mockito.times(0)).migrateVirtualMachine(Mockito.anyLong(), Mockito.any(HostVO.class)); }
@Test public void migrateVMTestWithException() throws Exception { VMInstanceVO vmInstanceVo = migrateVMSetupVmInstanceVo(0l); HostVO hostVo = migrateVMSetupHostVo(1l); Mockito.doThrow(Exception.class).when(userVmService).migrateVirtualMachine(Mockito.anyLong(), Mockito.any(HostVO.class)); administrationAgent.migrateVM(vmInstanceVo, hostVo); Mockito.verify(userVmService, Mockito.times(1)).migrateVirtualMachine(Mockito.anyLong(), Mockito.any(HostVO.class)); }
@Test public void migrateVMTestTargetHostDifferentFromVmHost() throws Exception { VMInstanceVO vmInstanceVo = migrateVMSetupVmInstanceVo(0l); HostVO hostVo = migrateVMSetupHostVo(1l); Mockito.doReturn(vmInstanceVo).when(userVmService).migrateVirtualMachine(Mockito.anyLong(), Mockito.any(HostVO.class)); administrationAgent.migrateVM(vmInstanceVo, hostVo); Mockito.verify(userVmService, Mockito.times(1)).migrateVirtualMachine(Mockito.anyLong(), Mockito.any(HostVO.class)); }