tests: extend mock objects in QubesVM tests

- allow TestQubesDB to be populated with initial data
- support list() method
- allow to register pre-created VM instance (useful for AdminVM, which
        don't accept setting qid)
This commit is contained in:
Marek Marczykowski-Górecki 2019-11-18 23:43:19 +01:00
parent e092b40350
commit 9bf0cce11e
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -39,6 +39,7 @@ import shutil
import qubes
import qubes.exc
import qubes.config
import qubes.devices
import qubes.vm
import qubes.vm.qubesvm
@ -79,8 +80,10 @@ class TestDeviceCollection(object):
return self._list
class TestQubesDB(object):
def __init__(self):
def __init__(self, data=None):
self.data = {}
if data:
self.data = data
def write(self, path, value):
self.data[path] = value
@ -92,6 +95,12 @@ class TestQubesDB(object):
else:
self.data.pop(path, None)
def list(self, prefix):
return [key for key in self.data if key.startswith(prefix)]
def close(self):
pass
class TestVM(object):
# pylint: disable=too-few-public-methods
app = TestApp()
@ -269,10 +278,11 @@ class QubesVMTestsMixin(object):
pass
super(QubesVMTestsMixin, self).tearDown()
def get_vm(self, name='test', cls=qubes.vm.qubesvm.QubesVM, **kwargs):
vm = cls(self.app, None,
qid=kwargs.pop('qid', 1), name=qubes.tests.VMPREFIX + name,
**kwargs)
def get_vm(self, name='test', cls=qubes.vm.qubesvm.QubesVM, vm=None, **kwargs):
if not vm:
vm = cls(self.app, None,
qid=kwargs.pop('qid', 1), name=qubes.tests.VMPREFIX + name,
**kwargs)
self.app.domains[vm.qid] = vm
self.app.domains[vm.uuid] = vm
self.app.domains[vm.name] = vm