Skip to content

Commit

Permalink
q-dev: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 14, 2024
1 parent 421935e commit c7a17e4
Showing 1 changed file with 138 additions and 10 deletions.
148 changes: 138 additions & 10 deletions qubesadmin/tests/tools/qvm_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,20 @@ def test_031_assign_required(self):
['testclass', 'assign', '--required', 'test-vm2', 'test-vm1:dev1'], app=self.app)
self.assertAllCalled()

def test_032_assign_options(self):
def test_032_assign_ask_and_options(self):
""" Test `read-only` assign option """
self.app.domains['test-vm2'].is_running = lambda: True
self.app.expected_calls[(
'test-vm2', 'admin.vm.device.testclass.Assign',
'test-vm1+dev1:dead:beef:babe:u0123456',
b"device_id='dead:beef:babe:u0123456' port_id='dev1' "
b"devclass='testclass' backend_domain='test-vm1' "
b"mode='auto-attach' frontend_domain='test-vm2' _read-only='yes'"
b"mode='ask-to-attach' frontend_domain='test-vm2' _read-only='yes'"
)] = b'0\0'
with qubesadmin.tests.tools.StdoutBuffer() as buf:
qubesadmin.tools.qvm_device.main(
['testclass', 'assign', '--ro', 'test-vm2', 'test-vm1:dev1'],
['testclass', 'assign', '--ro', '--ask', 'test-vm2',
'test-vm1:dev1'],
app=self.app)
self.assertIn('Assigned.', buf.getvalue())
self.assertIn('now restart domain', buf.getvalue())
Expand Down Expand Up @@ -337,25 +338,153 @@ def test_035_assign_invalid_backend(self):
self.assertIn('no such backend vm!', stderr.getvalue())
self.assertAllCalled()

def test_040_unassign(self):
def test_036_assign_port(self):
""" Test assign action """
self.app.domains['test-vm2'].is_running = lambda: True
self.app.expected_calls[(
'test-vm2', 'admin.vm.device.testclass.Assign',
'test-vm1+dev1:*',
b"device_id='*' port_id='dev1' "
b"devclass='testclass' backend_domain='test-vm1' "
b"mode='auto-attach' frontend_domain='test-vm2'"
)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'assign', 'test-vm2', 'test-vm1:dev1', '--port'],
app=self.app)
self.assertAllCalled()

def test_037_assign_port_asterisk(self):
""" Test assign action """
self.app.domains['test-vm2'].is_running = lambda: True
self.app.expected_calls[(
'test-vm2', 'admin.vm.device.testclass.Assign',
'test-vm1+dev1:*',
b"device_id='*' port_id='dev1' "
b"devclass='testclass' backend_domain='test-vm1' "
b"mode='auto-attach' frontend_domain='test-vm2'"
)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'assign', 'test-vm2', 'test-vm1:dev1:*'],
app=self.app)
self.assertAllCalled()

def test_038_assign_device_from_port(self):
""" Test assign action """
self.app.domains['test-vm2'].is_running = lambda: True
self.app.expected_calls[(
'test-vm2', 'admin.vm.device.testclass.Assign',
'test-vm1+*:dead:beef:babe:u0123456',
b"device_id='dead:beef:babe:u0123456' port_id='*' "
b"devclass='testclass' backend_domain='test-vm1' "
b"mode='auto-attach' frontend_domain='test-vm2'"
)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'assign', 'test-vm2', 'test-vm1:dev1', '--device'],
app=self.app)
self.assertAllCalled()

def test_039_assign_explicit_device(self):
""" Test assign action """
self.app.domains['test-vm2'].is_running = lambda: True
self.app.expected_calls[(
'test-vm2', 'admin.vm.device.testclass.Assign',
'test-vm1+dev1:cafe:cafe::0123456u654321',
b"device_id='cafe:cafe::0123456u654321' port_id='dev1' "
b"devclass='testclass' backend_domain='test-vm1' "
b"mode='auto-attach' frontend_domain='test-vm2'"
)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'assign', 'test-vm2',
'test-vm1:dev1:cafe:cafe::0123456u654321'], app=self.app)
self.assertAllCalled()

def test_040_assign_explicit_device_device_id(self):
""" Test assign action """
self.app.domains['test-vm2'].is_running = lambda: True
self.app.expected_calls[(
'test-vm2', 'admin.vm.device.testclass.Assign',
'test-vm1+*:cafe:cafe::0123456u654321',
b"device_id='cafe:cafe::0123456u654321' port_id='*' "
b"devclass='testclass' backend_domain='test-vm1' "
b"mode='auto-attach' frontend_domain='test-vm2'"
)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'assign', 'test-vm2',
'test-vm1:dev1:cafe:cafe::0123456u654321', '--device'],
app=self.app)
self.assertAllCalled()

def test_050_unassign(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+dev1:dead:beef:babe:u0123456', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2', 'test-vm1:dev1'], app=self.app)
['testclass', 'unassign', 'test-vm2', 'test-vm1:dev1'],
app=self.app)
self.assertAllCalled()

def test_041_unassign_unknown(self):
def test_051_unassign_unknown(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+dev7:*', None)] = b'0\0'
'test-vm1+dev7:0000:0000::?******', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2', 'test-vm1:dev7'],
app=self.app)
self.assertAllCalled()

def test_052_unassign_port(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+dev1:*', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2', 'test-vm1:dev1', '--port'],
app=self.app)
self.assertAllCalled()

def test_053_unassign_device_from_port(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+*:dead:beef:babe:u0123456', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2', 'test-vm1:dev1', '--device'],
app=self.app)
self.assertAllCalled()

def test_054_unassign_explicit_device(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+dev1:dead:beef:babe:u0123456', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2', 'test-vm1:dev7', '--port'], app=self.app)
['testclass', 'unassign', 'test-vm2',
'test-vm1:dev1:dead:beef:babe:u0123456'], app=self.app)
self.assertAllCalled()

def test_042_unassign_all(self):
def test_055_unassign_explicit_device_port(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+dev1:*', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2',
'test-vm1:dev1:dead:beef:babe:u0123456', '--port'], app=self.app)
self.assertAllCalled()

def test_056_unassign_explicit_device_id(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Unassign',
'test-vm1+*:cafe:cafe::0123456u654321', None)] = b'0\0'
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2',
'test-vm1:dev1:cafe:cafe::0123456u654321', '--device'], app=self.app)
self.assertAllCalled()

def test_057_unassign_all(self):
""" Test unassign action """
self.app.expected_calls[
('test-vm2', 'admin.vm.device.testclass.Assigned', None, None)] = \
Expand All @@ -370,4 +499,3 @@ def test_042_unassign_all(self):
qubesadmin.tools.qvm_device.main(
['testclass', 'unassign', 'test-vm2'], app=self.app)
self.assertAllCalled()

0 comments on commit c7a17e4

Please sign in to comment.