@Override public String getEventDescription() { return "upgrading vm: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()) + " to service offering: " + this._uuidMgr.getUuid(ServiceOffering.class, getServiceOfferingId()); }
@Override @ActionEvent(eventType = EventTypes.EVENT_VM_UPGRADE, eventDescription = "Upgrading VM", async = true) public UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException { Long vmId = cmd.getId(); Long newServiceOfferingId = cmd.getServiceOfferingId(); VirtualMachine vm = (VirtualMachine) this._entityMgr.findById(VirtualMachine.class, vmId); if (vm == null) { throw new InvalidParameterValueException("Unable to find VM's UUID"); } CallContext.current().setEventDetails("Vm Id: " + vm.getUuid()); boolean result = upgradeVirtualMachine(vmId, newServiceOfferingId, cmd.getDetails()); if (result) { UserVmVO vmInstance = _vmDao.findById(vmId); if (vmInstance.getState().equals(State.Stopped)) { // Generate usage event for VM upgrade generateUsageEvent(vmInstance, vmInstance.isDisplayVm(), EventTypes.EVENT_VM_UPGRADE); } if (vmInstance.getState().equals(State.Running)) { // Generate usage event for Dynamic scaling of VM generateUsageEvent( vmInstance, vmInstance.isDisplayVm(), EventTypes.EVENT_VM_DYNAMIC_SCALE); } return vmInstance; } else { throw new CloudRuntimeException("Failed to scale the VM"); } }
@Override public long getEntityOwnerId() { UserVm userVm = _entityMgr.findById(UserVm.class, getId()); if (userVm != null) { return userVm.getAccountId(); } return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked }
@Override public void execute() { UserVm result; try { result = _userVmService.upgradeVirtualMachine(this); } catch (ResourceUnavailableException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); } catch (ConcurrentOperationException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } catch (ManagementServerException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } catch (VirtualMachineMigrationException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } if (result != null){ List<UserVmResponse> responseList = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result); UserVmResponse response = responseList.get(0); response.setResponseName(getCommandName()); setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm"); } } }
@Test public void testCreateFailure() { UserVmService userVmService = Mockito.mock(UserVmService.class); try { Mockito.when(userVmService.upgradeVirtualMachine(scaleVMCmd)).thenReturn(null); } catch (Exception e) { Assert.fail("Received exception when success expected " + e.getMessage()); } scaleVMCmd._userVmService = userVmService; try { scaleVMCmd.execute(); } catch (ServerApiException exception) { Assert.assertEquals("Failed to scale vm", exception.getDescription()); } } }
@Test public void testCreateSuccess() { UserVmService userVmService = Mockito.mock(UserVmService.class); UserVm userVm = Mockito.mock(UserVm.class); try { Mockito.when(userVmService.upgradeVirtualMachine(scaleVMCmd)).thenReturn(userVm); } catch (Exception e) { Assert.fail("Received exception when success expected " + e.getMessage()); } ResponseGenerator responseGenerator = Mockito.mock(ResponseGenerator.class); scaleVMCmd._responseGenerator = responseGenerator; UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class); //List<UserVmResponse> list = Mockito.mock(UserVmResponse.class); //list.add(userVmResponse); //LinkedList<UserVmResponse> mockedList = Mockito.mock(LinkedList.class); //Mockito.when(mockedList.get(0)).thenReturn(userVmResponse); List<UserVmResponse> list = new LinkedList<UserVmResponse>(); list.add(userVmResponse); Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn( list); scaleVMCmd._userVmService = userVmService; scaleVMCmd.execute(); }