diff --git a/lib/rift/Controller.py b/lib/rift/Controller.py index 02684272..4d22b2b4 100644 --- a/lib/rift/Controller.py +++ b/lib/rift/Controller.py @@ -372,17 +372,6 @@ def action_annex(args, config, staff, modules): ) message(f"Annex backup is available here: {output_file}") -def _vm_start(vm): - if vm.running(): - message('VM is already running') - return False - - message('Launching VM ...') - vm.spawn() - vm.ready() - vm.prepare() - return True - class BasicTest(Test): """ @@ -477,7 +466,7 @@ def test_one_pkg(config, args, pkg, vm, arch, repos): and return results. """ message(f"Preparing {arch} test environment") - _vm_start(vm) + vm.start() if repos.working is None: disablestr = '--disablerepo=working' else: @@ -728,7 +717,7 @@ def action_vm(args, config): ret = vm.copy(args.source, args.dest) elif args.vm_cmd == 'start': vm.tmpmode = args.tmpimg - if _vm_start(vm): + if vm.start(): message("VM started. Use: rift vm connect") ret = 0 elif args.vm_cmd == 'stop': diff --git a/lib/rift/VM.py b/lib/rift/VM.py index 107fb0a3..e312fed6 100644 --- a/lib/rift/VM.py +++ b/lib/rift/VM.py @@ -61,7 +61,7 @@ from rift.Config import _DEFAULT_VIRTIOFSD from rift.Repository import ProjectArchRepositories from rift.TempDir import TempDir -from rift.utils import download_file, setup_dl_opener +from rift.utils import download_file, setup_dl_opener, message from rift.run import run_command __all__ = ['VM'] @@ -648,6 +648,21 @@ def unlink(self): self._tmpimg.close() self._tmpimg = None + def start(self): + """ + Start VM if not already running. Return True if VM is actually started, + False if already running. + """ + if self.running(): + message('VM is already running') + return False + + message('Launching VM ...') + self.spawn() + self.ready() + self.prepare() + return True + def restart(self): """ Restart a running VM. diff --git a/tests/VM.py b/tests/VM.py index 2179436f..9850a501 100644 --- a/tests/VM.py +++ b/tests/VM.py @@ -4,7 +4,7 @@ import os import shutil import atexit -from unittest.mock import patch +from unittest.mock import patch, Mock import platform from TestUtils import RiftTestCase, RiftProjectTestCase, make_temp_dir @@ -335,6 +335,33 @@ def test_gen_qemu_args(self): 'virtio-net-device,netdev=hostnet0'] self.assertEqual(args, expected_args_aarch64) + @patch('rift.VM.message') + def test_start(self, mock_message): + """Test VM start not running""" + vm = VM(self.config, platform.machine()) + vm.running = Mock(return_value=False) + vm.spawn = Mock() + vm.ready = Mock() + vm.prepare = Mock() + self.assertTrue(vm.start()) + mock_message.assert_called_once_with("Launching VM ...") + vm.spawn.assert_called_once() + vm.ready.assert_called_once() + vm.prepare.assert_called_once() + + @patch('rift.VM.message') + def test_start_running(self, mock_message): + """Test VM start already running""" + vm = VM(self.config, platform.machine()) + vm.running = Mock(return_value=True) + vm.spawn = Mock() + vm.ready = Mock() + vm.prepare = Mock() + self.assertFalse(vm.start()) + mock_message.assert_called_once_with("VM is already running") + vm.spawn.assert_not_called() + vm.ready.assert_not_called() + vm.prepare.assert_not_called() class VMBuildTest(RiftProjectTestCase): """