Skip to content

Commit

Permalink
q-dev: update device tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Oct 15, 2024
1 parent be25600 commit 4539f63
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
4 changes: 4 additions & 0 deletions qubes/tests/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def dev_testclass_list_attached(self, event, persistent=False):
def dev_testclass_list(self, event):
yield self.device

@qubes.events.handler('device-get:testclass')
def dev_testclass_get(self, event, **kwargs):
yield self.device

def is_halted(self):
return not self.running

Expand Down
39 changes: 21 additions & 18 deletions qubes/tests/devices_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,10 @@ def test_069_on_qdb_change_removed_attached(self):
('device-removed:block', frozenset({('port', exp_dev.port)}))],
1)

@unittest.mock.patch('subprocess.Popen')
def test_070_on_qdb_change_two_fronts_failed(self, mock_confirm):
# with `new_callable=Mock` we override async function with synchronous Mock
@unittest.mock.patch(
'qubes.ext.utils.confirm_device_attachment', new_callable=Mock)
def test_070_on_qdb_change_two_fronts_failed(self, _mock_confirm):
back, front = self.added_assign_setup()

exp_dev = qubes.ext.block.BlockDevice(back, 'sda')
Expand All @@ -954,18 +956,20 @@ def test_070_on_qdb_change_two_fronts_failed(self, mock_confirm):
back.devices['block']._assigned.append(assign)
back.devices['block']._exposed.append(exp_dev)

proc = Mock()
proc.communicate = Mock()
proc.communicate.return_value = (b'nonsense', b'')
mock_confirm.return_value = proc
self.ext.attach_and_notify = Mock()
with mock.patch('asyncio.ensure_future'):

with mock.patch('qubes.ext.utils.asyncio.ensure_future') as future:
future.return_value = Mock()
future.return_value.result = Mock()
future.return_value.result.return_value = "nonsense"
self.ext.on_qdb_change(back, None, None)
proc.communicate.assert_called_once()

self.ext.attach_and_notify.assert_not_called()

@unittest.mock.patch('subprocess.Popen')
def test_071_on_qdb_change_two_fronts(self, mock_confirm):
# with `new_callable=Mock` we override async function with synchronous Mock
@unittest.mock.patch(
'qubes.ext.utils.confirm_device_attachment', new_callable=Mock)
def test_071_on_qdb_change_two_fronts(self, _mock_confirm):
back, front = self.added_assign_setup()

exp_dev = qubes.ext.block.BlockDevice(back, 'sda')
Expand All @@ -975,16 +979,15 @@ def test_071_on_qdb_change_two_fronts(self, mock_confirm):
back.devices['block']._assigned.append(assign)
back.devices['block']._exposed.append(exp_dev)

proc = Mock()
proc.communicate = Mock()
proc.communicate.return_value = (b'front-vm', b'')
mock_confirm.return_value = proc
self.ext.attach_and_notify = Mock()
with mock.patch('asyncio.ensure_future'):

with mock.patch('asyncio.ensure_future') as future:
future.return_value = Mock()
future.return_value.result = Mock()
future.return_value.result.return_value = "front-vm"
self.ext.on_qdb_change(back, None, None)
proc.communicate.assert_called_once()
self.ext.attach_and_notify.assert_called_once_with(
front, assign)

self.ext.attach_and_notify.assert_called_once_with(front, assign)
# don't ask again
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].mode.value,
'auto-attach')
Expand Down

0 comments on commit 4539f63

Please sign in to comment.