Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions lib/rift/Controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down
17 changes: 16 additions & 1 deletion lib/rift/VM.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 28 additions & 1 deletion tests/VM.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down