def test_soft_shutdown_fail(self, mock_vm_info, mock_control_vm, mock_sleep): mock_vm_info.side_effect = [{constants.VM_ACPI: 'off'}, {constants.VM_ACPI: 'on'}] mock_control_vm.side_effect = [ vbox_exception.VBoxException('Expected fail.')] self.assertFalse(vmutils.soft_shutdown(self._instance)) self.assertFalse(vmutils.soft_shutdown(self._instance, 1, 1)) mock_control_vm.assert_called_once_with(self._instance, constants.ACPI_POWER_BUTTON) mock_sleep.assert_called_once_with(1)
def test_soft_shutdown_fail(self, mock_vm_info, mock_control_vm, mock_sleep): mock_vm_info.side_effect = [{ constants.VM_ACPI: 'off' }, { constants.VM_ACPI: 'on' }] mock_control_vm.side_effect = [ vbox_exception.VBoxException('Expected fail.') ] self.assertFalse(vmutils.soft_shutdown(self._instance)) self.assertFalse(vmutils.soft_shutdown(self._instance, 1, 1)) mock_control_vm.assert_called_once_with(self._instance, constants.ACPI_POWER_BUTTON) mock_sleep.assert_called_once_with(1)
def test_soft_shutdown(self, mock_control_vm, mock_vm_info, mock_wait): mock_vm_info.return_value = {constants.VM_ACPI: 'on'} mock_wait.return_value = True self.assertTrue(vmutils.soft_shutdown(self._instance)) mock_control_vm.assert_called_once_with(self._instance, constants.ACPI_POWER_BUTTON)
def power_off(self, instance, timeout=0, retry_interval=0): """Power off has the same effect on a virtual machine as pulling the power cable on a real computer. .. note:: If timeout is greater than 0 it will try to power off virtual machine softly. The state of the VM is not saved beforehand, and data may be lost. """ LOG.debug("Power off instance %(instance)s", {"instance": instance.name}) try: if timeout and vmutils.soft_shutdown(instance, timeout, retry_interval): LOG.info(i18n._LI("Soft shutdown succeeded."), instance=instance) return except (vbox_exc.VBoxException, exception.InstanceInvalidState) as exc: LOG.debug("Soft shutdown failed: %(instance)s: %(error)s", { "error": exc, "instance": instance.name }) self._vbox_manage.control_vm(instance, constants.STATE_POWER_OFF)
def power_off(self, instance, timeout=0, retry_interval=0): """Power off has the same effect on a virtual machine as pulling the power cable on a real computer. .. note:: If timeout is greater than 0 it will try to power off virtual machine softly. The state of the VM is not saved beforehand, and data may be lost. """ LOG.debug("Power off instance %(instance)s", {"instance": instance.name}) try: if timeout and vmutils.soft_shutdown(instance, timeout, retry_interval): LOG.info( i18n._LI("Soft shutdown succeeded."), instance=instance) return except (vbox_exc.VBoxException, exception.InstanceInvalidState) as exc: LOG.debug("Soft shutdown failed: %(instance)s: %(error)s", { "error": exc, "instance": instance.name }) self._vbox_manage.control_vm(instance, constants.STATE_POWER_OFF)
def test_soft_shutdown_wait_timeout(self, mock_control_vm, mock_vm_info, mock_wait): mock_vm_info.return_value = {constants.VM_ACPI: 'on'} mock_wait.return_value = False self.assertFalse(vmutils.soft_shutdown(self._instance, self._FAKE_TIMEOUT, 1.5)) mock_wait.assert_has_calls([ mock.call(self._instance, constants.STATE_POWER_OFF, 1.5), mock.call(self._instance, constants.STATE_POWER_OFF, self._FAKE_TIMEOUT - 1.5)])
def test_soft_shutdown_wait_timeout(self, mock_control_vm, mock_vm_info, mock_wait): mock_vm_info.return_value = {constants.VM_ACPI: 'on'} mock_wait.return_value = False self.assertFalse( vmutils.soft_shutdown(self._instance, self._FAKE_TIMEOUT, 1.5)) mock_wait.assert_has_calls([ mock.call(self._instance, constants.STATE_POWER_OFF, 1.5), mock.call(self._instance, constants.STATE_POWER_OFF, self._FAKE_TIMEOUT - 1.5) ])
def test_soft_shutdown_invalid_state(self, mock_control_vm, mock_vm_info, mock_power_state): mock_vm_info.return_value = {constants.VM_ACPI: 'on'} mock_power_state.side_effect = [constants.STATE_POWER_OFF, mock.sentinel.power_state] mock_control_vm.side_effect = exception.InstanceInvalidState( attr=None, instance_uuid=self._instance.uuid, state='invalid-state', method='shutdown') self.assertTrue(vmutils.soft_shutdown( self._instance, self._FAKE_TIMEOUT, 1.5)) self.assertRaises(exception.InstanceInvalidState, vmutils.soft_shutdown, self._instance, self._FAKE_TIMEOUT, 1.5)
def test_soft_shutdown_invalid_state(self, mock_control_vm, mock_vm_info, mock_power_state): mock_vm_info.return_value = {constants.VM_ACPI: 'on'} mock_power_state.side_effect = [ constants.STATE_POWER_OFF, mock.sentinel.power_state ] mock_control_vm.side_effect = exception.InstanceInvalidState( attr=None, instance_uuid=self._instance.uuid, state='invalid-state', method='shutdown') self.assertTrue( vmutils.soft_shutdown(self._instance, self._FAKE_TIMEOUT, 1.5)) self.assertRaises(exception.InstanceInvalidState, vmutils.soft_shutdown, self._instance, self._FAKE_TIMEOUT, 1.5)
def reboot(self, instance, context=None, network_info=None, reboot_type=None, block_device_info=None, bad_volumes_callback=None): """Reboot the specified instance. After this is called successfully, the instance's state goes back to power_state.RUNNING. The virtualization platform should ensure that the reboot action has completed successfully even in cases in which the underlying domain/vm is paused or halted/stopped. """ # TODO(alexandrucoman): Process the information from the unused # arguments. if reboot_type == constants.REBOOT_SOFT: if vmutils.soft_shutdown(instance): self.power_on(instance) return self._vbox_manage.control_vm(instance, constants.STATE_RESET)