aetherscale

[unmaintained] code for a cloud provider tutorial
Log | Files | Refs | README | LICENSE

commit f3c639bdb0a2ff1cbc6d5999940fcb86ccd60222
parent 4dffef2ff492c35c053651a044fbc29f9bb8216d
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sat, 30 Jan 2021 12:41:58 +0100

fix unit tests after switch to iterator interface

Diffstat:
Maetherscale/computing.py | 5++++-
Mtests/test_computing.py | 27+++++++++++++++------------
2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/aetherscale/computing.py b/aetherscale/computing.py @@ -282,7 +282,7 @@ class ComputingHandler: # force kill stop when a VM is deleted options['kill'] = True - self.stop_vm(options) + self._exhaust(self.stop_vm(options)) unit_name = systemd_unit_name_for_vm(vm_id) user_image = user_image_path(vm_id) @@ -407,6 +407,9 @@ class ComputingHandler: return associated_tap_device + def _exhaust(self, generator): + all(generator) + def get_process_for_vm(vm_id: str) -> Optional[psutil.Process]: for proc in psutil.process_iter(['name']): diff --git a/tests/test_computing.py b/tests/test_computing.py @@ -14,8 +14,8 @@ from aetherscale.services import ServiceManager @contextmanager def base_image(directory: Path) -> Iterator[Path]: random_name = str(uuid.uuid4()) + img_file = directory / f'{random_name}.qcow2' try: - img_file = directory / f'{random_name}.qcow2' subprocess.run([ 'qemu-img', 'create', '-f', 'qcow2', str(img_file), '1G']) yield img_file @@ -40,25 +40,26 @@ def test_vm_lifecycle(tmppath, mock_service_manager: ServiceManager): radvd=mock.MagicMock(), service_manager=mock_service_manager) with base_image(tmppath) as img: - result = handler.create_vm({'image': img.stem}) - vm_id = result['vm-id'] + results = list(handler.create_vm({'image': img.stem})) + vm_id = results[0]['vm-id'] service_name = computing.systemd_unit_name_for_vm(vm_id) - assert result['status'] == 'starting' + assert results[0]['status'] == 'allocating' + assert results[1]['status'] == 'starting' assert mock_service_manager.service_is_running(service_name) # TODO: Test graceful stop, needs mock of QemuMonitor - result = handler.stop_vm({'vm-id': vm_id, 'kill': True}) - assert result['status'] == 'killed' + results = list(handler.stop_vm({'vm-id': vm_id, 'kill': True})) + assert results[0]['status'] == 'killed' assert mock_service_manager.service_exists(service_name) assert not mock_service_manager.service_is_running(service_name) - result = handler.start_vm({'vm-id': vm_id}) - assert result['status'] == 'starting' + results = list(handler.start_vm({'vm-id': vm_id})) + assert results[0]['status'] == 'starting' assert mock_service_manager.service_exists(service_name) assert mock_service_manager.service_is_running(service_name) - result = handler.delete_vm({'vm-id': vm_id}) - assert result['status'] == 'deleted' + results = list(handler.delete_vm({'vm-id': vm_id})) + assert results[0]['status'] == 'deleted' assert not mock_service_manager.service_exists(service_name) assert not mock_service_manager.service_is_running(service_name) @@ -72,8 +73,10 @@ def test_run_missing_base_image(tmppath, mock_service_manager: ServiceManager): # specify invalid base image with pytest.raises(OSError): - handler.create_vm({'image': 'some-missing-image'}) + # make sure to exhaust the iterator + list(handler.create_vm({'image': 'some-missing-image'})) # do not specify a base image with pytest.raises(ValueError): - handler.create_vm({}) + # make sure to exhaust the iterator + list(handler.create_vm({}))