Skip to content

Commit

Permalink
q-dev: update tests for qvm-device and app
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed May 26, 2024
1 parent 6468b20 commit a8650a7
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 166 deletions.
8 changes: 5 additions & 3 deletions qubesadmin/device_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import string
import sys
from enum import Enum
from typing import Optional, Dict, Any, List, Type, Union
from typing import Optional, Dict, Any, List, Union

import qubesadmin
import qubesadmin.exc


class ProtocolError(AssertionError):
Expand Down Expand Up @@ -74,7 +74,8 @@ def qbool(value):


class Device:
ALLOWED_CHARS_KEY = string.digits + string.ascii_letters + string.punctuation + ' '
ALLOWED_CHARS_KEY = (
string.digits + string.ascii_letters + string.punctuation + ' ')
ALLOWED_CHARS_PARAM = ALLOWED_CHARS_KEY + string.punctuation + ' '

def __init__(self, backend_domain, ident, devclass=None):
Expand Down Expand Up @@ -609,6 +610,7 @@ def deserialize(

try:
device = cls._deserialize(rest, device)
# pylint: disable=broad-exception-caught
except Exception as exc:
print(exc, file=sys.stderr)

Expand Down
7 changes: 6 additions & 1 deletion qubesadmin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class is implemented by an extension.
import itertools
from typing import Optional, Iterable

import qubesadmin
import qubesadmin.exc
from qubesadmin.device_protocol import (Device, DeviceInfo, UnknownDevice,
DeviceAssignment)
Expand Down Expand Up @@ -100,6 +99,9 @@ def unassign(self, assignment: DeviceAssignment) -> None:
self._remove(assignment, 'unassign')

def _add(self, assignment: DeviceAssignment, action: str) -> None:
"""
Helper for attaching/assigning device.
"""
if not assignment.frontend_domain:
assignment.frontend_domain = self._vm
elif assignment.frontend_domain != self._vm:
Expand All @@ -120,6 +122,9 @@ def _add(self, assignment: DeviceAssignment, action: str) -> None:
)

def _remove(self, assignment: DeviceAssignment, action: str) -> None:
"""
Helper for detaching/unassigning device.
"""
if (assignment.frontend_domain
and assignment.frontend_domain != self._vm):
raise qubesadmin.exc.QubesValueError(
Expand Down
46 changes: 35 additions & 11 deletions qubesadmin/tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,13 +734,25 @@ def test_043_clone_devices(self):
b'0\0pci\n'

self.app.expected_calls[
('test-vm', 'admin.vm.device.pci.List', None, None)] = \
b'0\0test-vm2+dev1 ro=True\n' \
b'test-vm3+dev2 required=True\n'
('test-vm', 'admin.vm.device.pci.Assigned', None, None)] = \
(b"0\0test-vm2+dev1 ident='dev1' devclass='pci' "
b"backend_domain='test-vm2' attach_automatically='yes' "
b"_ro='yes'\n"
b"test-vm3+dev2 ident='dev2' devclass='pci' "
b"backend_domain='test-vm3' attach_automatically='yes' "
b"required='yes'\n")

self.app.expected_calls[
('new-name', 'admin.vm.device.pci.Attach', 'test-vm3+dev2',
b'required=True')] = b'0\0'
('new-name', 'admin.vm.device.pci.Assign', 'test-vm2+dev1',
b"required='no' attach_automatically='yes' ident='dev1' "
b"devclass='pci' backend_domain='test-vm2' "
b"frontend_domain='new-name' _ro='yes'")] = b'0\0'

self.app.expected_calls[
('new-name', 'admin.vm.device.pci.Assign', 'test-vm3+dev2',
b"required='yes' attach_automatically='yes' ident='dev2' "
b"devclass='pci' backend_domain='test-vm3' "
b"frontend_domain='new-name'")] = b'0\0'

new_vm = self.app.clone_vm('test-vm', 'new-name')
self.assertEqual(new_vm.name, 'new-name')
Expand Down Expand Up @@ -770,20 +782,32 @@ def test_044_clone_devices_fail(self):
b'0\0pci\n'

self.app.expected_calls[
('test-vm', 'admin.vm.device.pci.List', None, None)] = \
b'0\0test-vm2+dev1 ro=True\n' \
b'test-vm3+dev2 required=True\n'
('test-vm', 'admin.vm.device.pci.Assigned', None, None)] = \
(b"0\0test-vm2+dev1 ident='dev1' devclass='pci' "
b"backend_domain='test-vm2' attach_automatically='yes' "
b"_ro='yes'\n"
b"test-vm3+dev2 ident='dev2' devclass='pci' "
b"backend_domain='test-vm3' attach_automatically='yes' "
b"required='yes'\n")

self.app.expected_calls[
('new-name', 'admin.vm.device.pci.Attach', 'test-vm3+dev2',
b'required=True')] = \
('new-name', 'admin.vm.device.pci.Assign', 'test-vm2+dev1',
b"required='no' attach_automatically='yes' ident='dev1' "
b"devclass='pci' backend_domain='test-vm2' "
b"frontend_domain='new-name' _ro='yes'")] = b'0\0'

self.app.expected_calls[
('new-name', 'admin.vm.device.pci.Assign', 'test-vm3+dev2',
b"required='yes' attach_automatically='yes' ident='dev2' "
b"devclass='pci' backend_domain='test-vm3' "
b"frontend_domain='new-name'")] = \
b'2\0QubesException\0\0something happened\0'

self.app.expected_calls[
('new-name', 'admin.vm.Remove', None, None)] = b'0\0'

with self.assertRaises(qubesadmin.exc.QubesException):
new_vm = self.app.clone_vm('test-vm', 'new-name')
self.app.clone_vm('test-vm', 'new-name')

self.assertAllCalled()

Expand Down
Loading

0 comments on commit a8650a7

Please sign in to comment.