use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cosmic by MissionCriticalCloud.
the class VirtualMachineManagerImplTest method initializeMockConfigForMigratingVmWithVolumes.
private void initializeMockConfigForMigratingVmWithVolumes() throws OperationTimedoutException, ResourceUnavailableException {
// Mock the source and destination hosts.
when(_srcHostMock.getId()).thenReturn(5L);
when(_destHostMock.getId()).thenReturn(6L);
when(_hostDao.findById(5L)).thenReturn(_srcHostMock);
when(_hostDao.findById(6L)).thenReturn(_destHostMock);
// Mock the vm being migrated.
when(_vmMock.getId()).thenReturn(1L);
when(_vmMock.getHypervisorType()).thenReturn(HypervisorType.XenServer);
when(_vmMock.getState()).thenReturn(State.Running).thenReturn(State.Running).thenReturn(State.Migrating).thenReturn(State.Migrating);
when(_vmMock.getHostId()).thenReturn(5L);
when(_vmInstance.getId()).thenReturn(1L);
when(_vmInstance.getServiceOfferingId()).thenReturn(2L);
when(_vmInstance.getInstanceName()).thenReturn("myVm");
when(_vmInstance.getHostId()).thenReturn(5L);
when(_vmInstance.getType()).thenReturn(VirtualMachine.Type.User);
when(_vmInstance.getState()).thenReturn(State.Running).thenReturn(State.Running).thenReturn(State.Migrating).thenReturn(State.Migrating);
// Mock the work item.
when(_workDao.persist(any(ItWorkVO.class))).thenReturn(_work);
when(_workDao.update("1", _work)).thenReturn(true);
when(_work.getId()).thenReturn("1");
doNothing().when(_work).setStep(ItWorkVO.Step.Done);
// Mock the vm guru and the user vm object that gets returned.
_vmMgr._vmGurus = new HashMap<>();
// UserVmManagerImpl userVmManager = mock(UserVmManagerImpl.class);
// _vmMgr.registerGuru(VirtualMachine.Type.User, userVmManager);
// Mock the iteration over all the volumes of an instance.
final Iterator<VolumeVO> volumeIterator = mock(Iterator.class);
when(_volsDao.findUsableVolumesForInstance(anyLong())).thenReturn(_rootVols);
when(_rootVols.iterator()).thenReturn(volumeIterator);
when(volumeIterator.hasNext()).thenReturn(true, false);
when(volumeIterator.next()).thenReturn(_volumeMock);
// Mock the disk offering and pool objects for a volume.
when(_volumeMock.getDiskOfferingId()).thenReturn(5L);
when(_volumeMock.getPoolId()).thenReturn(200L);
when(_volumeMock.getId()).thenReturn(5L);
when(_diskOfferingDao.findById(anyLong())).thenReturn(_diskOfferingMock);
when(_storagePoolDao.findById(200L)).thenReturn(_srcStoragePoolMock);
when(_storagePoolDao.findById(201L)).thenReturn(_destStoragePoolMock);
// Mock the volume to pool mapping.
when(_volumeToPoolMock.get(5L)).thenReturn(201L);
when(_destStoragePoolMock.getId()).thenReturn(201L);
when(_srcStoragePoolMock.getId()).thenReturn(200L);
when(_destStoragePoolMock.isLocal()).thenReturn(false);
when(_diskOfferingMock.getUseLocalStorage()).thenReturn(false);
when(_poolHostDao.findByPoolHost(anyLong(), anyLong())).thenReturn(mock(StoragePoolHostVO.class));
// Mock hypervisor guru.
final HypervisorGuru guruMock = mock(HypervisorGuru.class);
when(_hvGuruMgr.getGuru(HypervisorType.XenServer)).thenReturn(guruMock);
when(_srcHostMock.getClusterId()).thenReturn(3L);
when(_destHostMock.getClusterId()).thenReturn(3L);
// Mock the commands and answers to the agent.
final PrepareForMigrationAnswer prepAnswerMock = mock(PrepareForMigrationAnswer.class);
when(prepAnswerMock.getResult()).thenReturn(true);
when(_agentMgr.send(anyLong(), isA(PrepareForMigrationCommand.class))).thenReturn(prepAnswerMock);
final MigrateWithStorageAnswer migAnswerMock = mock(MigrateWithStorageAnswer.class);
when(migAnswerMock.getResult()).thenReturn(true);
when(_agentMgr.send(anyLong(), isA(MigrateWithStorageCommand.class))).thenReturn(migAnswerMock);
final MigrateWithStorageReceiveAnswer migRecAnswerMock = mock(MigrateWithStorageReceiveAnswer.class);
when(migRecAnswerMock.getResult()).thenReturn(true);
when(_agentMgr.send(anyLong(), isA(MigrateWithStorageReceiveCommand.class))).thenReturn(migRecAnswerMock);
final MigrateWithStorageSendAnswer migSendAnswerMock = mock(MigrateWithStorageSendAnswer.class);
when(migSendAnswerMock.getResult()).thenReturn(true);
when(_agentMgr.send(anyLong(), isA(MigrateWithStorageSendCommand.class))).thenReturn(migSendAnswerMock);
final MigrateWithStorageCompleteAnswer migCompleteAnswerMock = mock(MigrateWithStorageCompleteAnswer.class);
when(migCompleteAnswerMock.getResult()).thenReturn(true);
when(_agentMgr.send(anyLong(), isA(MigrateWithStorageCompleteCommand.class))).thenReturn(migCompleteAnswerMock);
final CheckVirtualMachineAnswer checkVmAnswerMock = mock(CheckVirtualMachineAnswer.class);
when(checkVmAnswerMock.getResult()).thenReturn(true);
when(checkVmAnswerMock.getState()).thenReturn(PowerState.PowerOn);
when(_agentMgr.send(anyLong(), isA(CheckVirtualMachineCommand.class))).thenReturn(checkVmAnswerMock);
// Mock the state transitions of vm.
final Pair<Long, Long> opaqueMock = new Pair<>(_vmMock.getHostId(), _destHostMock.getId());
when(_vmSnapshotMgr.hasActiveVMSnapshotTasks(anyLong())).thenReturn(false);
when(_vmInstanceDao.updateState(State.Running, Event.MigrationRequested, State.Migrating, _vmMock, opaqueMock)).thenReturn(true);
when(_vmInstanceDao.updateState(State.Migrating, Event.OperationSucceeded, State.Running, _vmMock, opaqueMock)).thenReturn(true);
}
use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cosmic by MissionCriticalCloud.
the class CitrixCheckVirtualMachineCommandWrapper method execute.
@Override
public Answer execute(final CheckVirtualMachineCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final String vmName = command.getVmName();
final PowerState powerState = citrixResourceBase.getVmState(conn, vmName);
final Integer vncPort = null;
if (powerState == PowerState.PowerOn) {
s_logger.debug("3. The VM " + vmName + " is in Running state");
}
return new CheckVirtualMachineAnswer(command, powerState, vncPort);
}
use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cloudstack by apache.
the class VirtualMachineManagerImpl method checkVmOnHost.
protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) throws AgentUnavailableException, OperationTimedoutException {
final Answer answer = _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName()));
if (answer == null || !answer.getResult()) {
return false;
}
if (answer instanceof CheckVirtualMachineAnswer) {
final CheckVirtualMachineAnswer vmAnswer = (CheckVirtualMachineAnswer) answer;
if (vmAnswer.getState() == PowerState.PowerOff) {
return false;
}
}
UserVmVO userVm = _userVmDao.findById(vm.getId());
if (userVm != null) {
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vm.getId());
RestoreVMSnapshotCommand command = _vmSnapshotMgr.createRestoreCommand(userVm, vmSnapshots);
if (command != null) {
RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) _agentMgr.send(hostId, command);
if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) {
s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer == null ? "null answer" : restoreVMSnapshotAnswer.getDetails());
}
}
}
return true;
}
use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cloudstack by apache.
the class CheckOnAgentInvestigator method isVmAlive.
@Override
public boolean isVmAlive(VirtualMachine vm, Host host) throws UnknownVM {
CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName());
try {
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(vm.getHostId(), cmd);
if (!answer.getResult()) {
s_logger.debug("Unable to get vm state on " + vm.toString());
throw new UnknownVM();
}
s_logger.debug("Agent responded with state " + answer.getState().toString());
return answer.getState() == PowerState.PowerOn;
} catch (AgentUnavailableException e) {
s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage());
throw new UnknownVM();
} catch (OperationTimedoutException e) {
s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage());
throw new UnknownVM();
}
}
use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cloudstack by apache.
the class SimulatorInvestigator method isVmAlive.
@Override
public boolean isVmAlive(VirtualMachine vm, Host host) throws UnknownVM {
if (haManager.isHAEligible(host)) {
return haManager.isVMAliveOnHost(host);
}
CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName());
try {
Answer answer = _agentMgr.send(vm.getHostId(), cmd);
if (!answer.getResult()) {
s_logger.debug("Unable to get vm state on " + vm.toString());
throw new UnknownVM();
}
CheckVirtualMachineAnswer cvmAnswer = (CheckVirtualMachineAnswer) answer;
s_logger.debug("Agent responded with state " + cvmAnswer.getState().toString());
return cvmAnswer.getState() == PowerState.PowerOn;
} catch (AgentUnavailableException e) {
s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage());
throw new UnknownVM();
} catch (OperationTimedoutException e) {
s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage());
throw new UnknownVM();
}
}
Aggregations