tests/api: adjust for proper exceptions instead of AssertionError

This commit is contained in:
Marek Marczykowski-Górecki 2018-10-29 20:22:10 +01:00
parent 2b5fc6299e
commit db6094f397
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 64 additions and 60 deletions

View File

@ -396,7 +396,7 @@ class TC_00_VMs(AdminAPITestCase):
'keys.return_value': ['root', 'private', 'volatile', 'kernel'] 'keys.return_value': ['root', 'private', 'volatile', 'kernel']
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Info', b'test-vm1', self.call_mgmt_func(b'admin.vm.volume.Info', b'test-vm1',
b'no-such-volume') b'no-such-volume')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -424,7 +424,7 @@ class TC_00_VMs(AdminAPITestCase):
'keys.return_value': ['root', 'private', 'volatile', 'kernel'] 'keys.return_value': ['root', 'private', 'volatile', 'kernel']
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.ListSnapshots', b'test-vm1', self.call_mgmt_func(b'admin.vm.volume.ListSnapshots', b'test-vm1',
b'no-such-volume') b'no-such-volume')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -443,7 +443,7 @@ class TC_00_VMs(AdminAPITestCase):
{'rev2': '2018-02-22T22:22:22', 'rev1': '2018-01-11T11:11:11'}, {'rev2': '2018-02-22T22:22:22', 'rev1': '2018-01-11T11:11:11'},
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Snapshots', self.call_mgmt_func(b'admin.vm.volume.Snapshots',
b'test-vm1', b'no-such-volume') b'test-vm1', b'no-such-volume')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -456,7 +456,7 @@ class TC_00_VMs(AdminAPITestCase):
'keys.return_value': ['root', 'private', 'volatile', 'kernel'] 'keys.return_value': ['root', 'private', 'volatile', 'kernel']
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Snapshots', self.call_mgmt_func(b'admin.vm.volume.Snapshots',
b'test-vm1', b'private', b'no-such-rev') b'test-vm1', b'private', b'no-such-rev')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -495,7 +495,7 @@ class TC_00_VMs(AdminAPITestCase):
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
self.vm.storage = unittest.mock.Mock() self.vm.storage = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Revert', self.call_mgmt_func(b'admin.vm.volume.Revert',
b'test-vm1', b'private', b'no-such-rev') b'test-vm1', b'private', b'no-such-rev')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -527,7 +527,7 @@ class TC_00_VMs(AdminAPITestCase):
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
self.vm.storage = unittest.mock.Mock() self.vm.storage = unittest.mock.Mock()
self.vm.storage.resize.side_effect = self.dummy_coro self.vm.storage.resize.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Resize', self.call_mgmt_func(b'admin.vm.volume.Resize',
b'test-vm1', b'private', b'no-int-size') b'test-vm1', b'private', b'no-int-size')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -542,7 +542,7 @@ class TC_00_VMs(AdminAPITestCase):
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
self.vm.storage = unittest.mock.Mock() self.vm.storage = unittest.mock.Mock()
self.vm.storage.resize.side_effect = self.dummy_coro self.vm.storage.resize.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Resize', self.call_mgmt_func(b'admin.vm.volume.Resize',
b'test-vm1', b'private', b'-1') b'test-vm1', b'private', b'-1')
self.assertEqual(self.vm.volumes.mock_calls, self.assertEqual(self.vm.volumes.mock_calls,
@ -665,7 +665,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.add_pool = unittest.mock.Mock() self.app.add_pool = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0', self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'no-such-driver', b'name=test-pool\nparam1=some-value\n') b'no-such-driver', b'name=test-pool\nparam1=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()]) self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
@ -691,7 +691,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.add_pool = unittest.mock.Mock() self.app.add_pool = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0', self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'name=test-pool\nparam3=some-value\n') b'driver1', b'name=test-pool\nparam3=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()]) self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
@ -717,7 +717,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.add_pool = unittest.mock.Mock() self.app.add_pool = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0', self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'param1=value\nparam2=some-value\n') b'driver1', b'param1=value\nparam2=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()]) self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
@ -742,7 +742,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.add_pool = unittest.mock.Mock() self.app.add_pool = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0', self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'name=file\nparam1=value\nparam2=some-value\n') b'driver1', b'name=file\nparam1=value\nparam2=some-value\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()]) self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
@ -768,7 +768,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.add_pool = unittest.mock.Mock() self.app.add_pool = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Add', b'dom0', self.call_mgmt_func(b'admin.pool.Add', b'dom0',
b'driver1', b'name=test-pool\nparam 1=value\n_param2\n') b'driver1', b'name=test-pool\nparam 1=value\n_param2\n')
self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()]) self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
@ -796,7 +796,7 @@ class TC_00_VMs(AdminAPITestCase):
'test-pool': unittest.mock.Mock(), 'test-pool': unittest.mock.Mock(),
} }
self.app.remove_pool = unittest.mock.Mock() self.app.remove_pool = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Remove', b'dom0', self.call_mgmt_func(b'admin.pool.Remove', b'dom0',
b'no-such-pool') b'no-such-pool')
self.assertEqual(self.app.remove_pool.mock_calls, []) self.assertEqual(self.app.remove_pool.mock_calls, [])
@ -853,7 +853,7 @@ class TC_00_VMs(AdminAPITestCase):
'keys.return_value': range(1, 9), 'keys.return_value': range(1, 9),
} }
self.app.labels.configure_mock(**labels_config) self.app.labels.configure_mock(**labels_config)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0', b'cyan', self.call_mgmt_func(b'admin.label.Create', b'dom0', b'cyan',
b'abcd') b'abcd')
self.assertEqual(self.app.get_label.mock_calls, self.assertEqual(self.app.get_label.mock_calls,
@ -869,13 +869,13 @@ class TC_00_VMs(AdminAPITestCase):
'keys.return_value': range(1, 9), 'keys.return_value': range(1, 9),
} }
self.app.labels.configure_mock(**labels_config) self.app.labels.configure_mock(**labels_config)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0', b'01', self.call_mgmt_func(b'admin.label.Create', b'dom0', b'01',
b'0xff0000') b'0xff0000')
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0', b'../xxx', self.call_mgmt_func(b'admin.label.Create', b'dom0', b'../xxx',
b'0xff0000') b'0xff0000')
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Create', b'dom0', self.call_mgmt_func(b'admin.label.Create', b'dom0',
b'strange-name!@#$', b'strange-name!@#$',
b'0xff0000') b'0xff0000')
@ -917,7 +917,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.labels = unittest.mock.MagicMock(wraps=self.app.labels) self.app.labels = unittest.mock.MagicMock(wraps=self.app.labels)
self.app.get_label = unittest.mock.Mock(wraps=self.app.get_label, self.app.get_label = unittest.mock.Mock(wraps=self.app.get_label,
**{'return_value.index': 6}) **{'return_value.index': 6})
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Remove', b'dom0', self.call_mgmt_func(b'admin.label.Remove', b'dom0',
b'blue') b'blue')
self.assertEqual(self.app.labels.mock_calls, []) self.assertEqual(self.app.labels.mock_calls, [])
@ -927,7 +927,7 @@ class TC_00_VMs(AdminAPITestCase):
self.app.labels = unittest.mock.MagicMock(wraps=self.app.labels) self.app.labels = unittest.mock.MagicMock(wraps=self.app.labels)
self.app.get_label = unittest.mock.Mock(wraps=self.app.get_label, self.app.get_label = unittest.mock.Mock(wraps=self.app.get_label,
**{'return_value.index': 1}) **{'return_value.index': 1})
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.label.Remove', b'dom0', self.call_mgmt_func(b'admin.label.Remove', b'dom0',
b'red') b'red')
self.assertEqual(self.app.labels.mock_calls, []) self.assertEqual(self.app.labels.mock_calls, [])
@ -1167,7 +1167,7 @@ class TC_00_VMs(AdminAPITestCase):
@unittest.mock.patch('qubes.storage.Storage.create') @unittest.mock.patch('qubes.storage.Storage.create')
def test_331_vm_create_standalone_spurious_template(self, storage_mock): def test_331_vm_create_standalone_spurious_template(self, storage_mock):
storage_mock.side_effect = self.dummy_coro storage_mock.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.Create.StandaloneVM', self.call_mgmt_func(b'admin.vm.Create.StandaloneVM',
b'dom0', b'test-template', b'name=test-vm2 label=red') b'dom0', b'test-template', b'name=test-vm2 label=red')
@ -1223,7 +1223,7 @@ class TC_00_VMs(AdminAPITestCase):
@unittest.mock.patch('qubes.storage.Storage.create') @unittest.mock.patch('qubes.storage.Storage.create')
def test_335_vm_create_missing_name(self, storage_mock): def test_335_vm_create_missing_name(self, storage_mock):
storage_mock.side_effect = self.dummy_coro storage_mock.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.vm.Create.AppVM', self.call_mgmt_func(b'admin.vm.Create.AppVM',
b'dom0', b'test-template', b'label=red') b'dom0', b'test-template', b'label=red')
@ -1232,7 +1232,7 @@ class TC_00_VMs(AdminAPITestCase):
@unittest.mock.patch('qubes.storage.Storage.create') @unittest.mock.patch('qubes.storage.Storage.create')
def test_336_vm_create_spurious_pool(self, storage_mock): def test_336_vm_create_spurious_pool(self, storage_mock):
storage_mock.side_effect = self.dummy_coro storage_mock.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.vm.Create.AppVM', self.call_mgmt_func(b'admin.vm.Create.AppVM',
b'dom0', b'test-template', b'dom0', b'test-template',
b'name=test-vm2 label=red pool=default') b'name=test-vm2 label=red pool=default')
@ -1253,7 +1253,7 @@ class TC_00_VMs(AdminAPITestCase):
@unittest.mock.patch('qubes.storage.Storage.create') @unittest.mock.patch('qubes.storage.Storage.create')
def test_338_vm_create_name_twice(self, storage_mock): def test_338_vm_create_name_twice(self, storage_mock):
storage_mock.side_effect = self.dummy_coro storage_mock.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.vm.Create.AppVM', self.call_mgmt_func(b'admin.vm.Create.AppVM',
b'dom0', b'test-template', b'dom0', b'test-template',
b'name=test-vm2 name=test-vm3 label=red') b'name=test-vm2 name=test-vm3 label=red')
@ -1335,7 +1335,7 @@ class TC_00_VMs(AdminAPITestCase):
@unittest.mock.patch('qubes.storage.Storage.create') @unittest.mock.patch('qubes.storage.Storage.create')
def test_344_vm_create_in_pool_invalid_volume(self, storage_mock): def test_344_vm_create_in_pool_invalid_volume(self, storage_mock):
storage_mock.side_effect = self.dummy_coro storage_mock.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.CreateInPool.AppVM', self.call_mgmt_func(b'admin.vm.CreateInPool.AppVM',
b'dom0', b'test-template', b'name=test-vm2 label=red ' b'dom0', b'test-template', b'name=test-vm2 label=red '
b'pool:invalid=test') b'pool:invalid=test')
@ -1361,7 +1361,7 @@ class TC_00_VMs(AdminAPITestCase):
# setting custom pool for 'root' volume of AppVM should not be # setting custom pool for 'root' volume of AppVM should not be
# allowed - this volume belongs to the template # allowed - this volume belongs to the template
storage_mock.side_effect = self.dummy_coro storage_mock.side_effect = self.dummy_coro
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.vm.CreateInPool.AppVM', self.call_mgmt_func(b'admin.vm.CreateInPool.AppVM',
b'dom0', b'test-template', b'name=test-vm2 label=red ' b'dom0', b'test-template', b'name=test-vm2 label=red '
b'pool=test pool:root=test') b'pool=test pool:root=test')
@ -1735,9 +1735,9 @@ class TC_00_VMs(AdminAPITestCase):
def test_521_vm_volume_clone_invalid_volume(self): def test_521_vm_volume_clone_invalid_volume(self):
self.setup_for_clone() self.setup_for_clone()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.CloneFrom', self.call_mgmt_func(b'admin.vm.volume.CloneFrom',
b'test-vm1', b'private123', None) b'test-vm1', b'private123', b'')
self.assertNotIn('init_volume().import_volume', self.assertNotIn('init_volume().import_volume',
map(operator.itemgetter(0), self.pool.mock_calls)) map(operator.itemgetter(0), self.pool.mock_calls))
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
@ -1747,7 +1747,7 @@ class TC_00_VMs(AdminAPITestCase):
token = self.call_mgmt_func(b'admin.vm.volume.CloneFrom', token = self.call_mgmt_func(b'admin.vm.volume.CloneFrom',
b'test-vm1', b'private', b'') b'test-vm1', b'private', b'')
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.CloneTo', self.call_mgmt_func(b'admin.vm.volume.CloneTo',
b'test-vm1', b'private123', token.encode()) b'test-vm1', b'private123', token.encode())
self.assertNotIn('init_volume().import_volume', self.assertNotIn('init_volume().import_volume',
@ -1765,7 +1765,7 @@ class TC_00_VMs(AdminAPITestCase):
else: else:
return unittest.mock.DEFAULT return unittest.mock.DEFAULT
self.pool.get_volume.side_effect = get_volume self.pool.get_volume.side_effect = get_volume
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.CloneTo', self.call_mgmt_func(b'admin.vm.volume.CloneTo',
b'test-vm1', b'private', token.encode()) b'test-vm1', b'private', token.encode())
self.assertNotIn('init_volume().import_volume', self.assertNotIn('init_volume().import_volume',
@ -1775,7 +1775,7 @@ class TC_00_VMs(AdminAPITestCase):
def test_524_vm_volume_clone_invlid_token(self): def test_524_vm_volume_clone_invlid_token(self):
self.setup_for_clone() self.setup_for_clone()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.CloneTo', self.call_mgmt_func(b'admin.vm.volume.CloneTo',
b'test-vm1', b'private', b'no-such-token') b'test-vm1', b'private', b'no-such-token')
self.assertNotIn('init_volume().import_volume', self.assertNotIn('init_volume().import_volume',
@ -1823,7 +1823,7 @@ class TC_00_VMs(AdminAPITestCase):
self.assertTrue(self.app.save.called) self.assertTrue(self.app.save.called)
def test_561_tag_set_invalid(self): def test_561_tag_set_invalid(self):
with self.assertRaises(AssertionError): with self.assertRaises(ValueError):
self.call_mgmt_func(b'admin.vm.tag.Set', self.call_mgmt_func(b'admin.vm.tag.Set',
b'test-vm1', b'+.some-tag') b'test-vm1', b'+.some-tag')
self.assertNotIn('+.some-tag', self.vm.tags) self.assertNotIn('+.some-tag', self.vm.tags)
@ -1910,7 +1910,7 @@ class TC_00_VMs(AdminAPITestCase):
rules_txt = ( rules_txt = (
'proto=tcp dstports=1-1024\n' 'proto=tcp dstports=1-1024\n'
'action=drop\n') 'action=drop\n')
with self.assertRaises(AssertionError): with self.assertRaises(ValueError):
self.call_mgmt_func(b'admin.vm.firewall.Set', self.call_mgmt_func(b'admin.vm.firewall.Set',
b'test-vm1', b'', rules_txt.encode()) b'test-vm1', b'', rules_txt.encode())
self.assertEqual(self.vm.firewall.rules, self.assertEqual(self.vm.firewall.rules,
@ -2331,7 +2331,7 @@ class TC_00_VMs(AdminAPITestCase):
self.device_list_testclass) self.device_list_testclass)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM, with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False): 'is_halted', lambda _: False):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func( self.call_mgmt_func(
b'admin.vm.device.testclass.Set.persistent', b'admin.vm.device.testclass.Set.persistent',
b'test-vm1', b'test-vm1+1234', b'True') b'test-vm1', b'test-vm1+1234', b'True')
@ -2344,7 +2344,7 @@ class TC_00_VMs(AdminAPITestCase):
self.device_list_testclass) self.device_list_testclass)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM, with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False): 'is_halted', lambda _: False):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func( self.call_mgmt_func(
b'admin.vm.device.testclass.Set.persistent', b'admin.vm.device.testclass.Set.persistent',
b'test-vm1', b'test-vm1+1234', b'maybe') b'test-vm1', b'test-vm1+1234', b'maybe')
@ -2363,7 +2363,7 @@ class TC_00_VMs(AdminAPITestCase):
def test_661_pool_set_revisions_to_keep_negative(self): def test_661_pool_set_revisions_to_keep_negative(self):
self.app.pools['test-pool'] = unittest.mock.Mock() self.app.pools['test-pool'] = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.pool.Set.revisions_to_keep', self.call_mgmt_func(b'admin.pool.Set.revisions_to_keep',
b'dom0', b'test-pool', b'-2') b'dom0', b'test-pool', b'-2')
self.assertEqual(self.app.pools['test-pool'].mock_calls, []) self.assertEqual(self.app.pools['test-pool'].mock_calls, [])
@ -2371,7 +2371,7 @@ class TC_00_VMs(AdminAPITestCase):
def test_662_pool_set_revisions_to_keep_not_a_number(self): def test_662_pool_set_revisions_to_keep_not_a_number(self):
self.app.pools['test-pool'] = unittest.mock.Mock() self.app.pools['test-pool'] = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.pool.Set.revisions_to_keep', self.call_mgmt_func(b'admin.pool.Set.revisions_to_keep',
b'dom0', b'test-pool', b'abc') b'dom0', b'test-pool', b'abc')
self.assertEqual(self.app.pools['test-pool'].mock_calls, []) self.assertEqual(self.app.pools['test-pool'].mock_calls, [])
@ -2400,7 +2400,7 @@ class TC_00_VMs(AdminAPITestCase):
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
self.vm.storage = unittest.mock.Mock() self.vm.storage = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'admin.vm.volume.Set.revisions_to_keep', self.call_mgmt_func(b'admin.vm.volume.Set.revisions_to_keep',
b'test-vm1', b'private', b'-2') b'test-vm1', b'private', b'-2')
@ -2411,7 +2411,7 @@ class TC_00_VMs(AdminAPITestCase):
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
self.vm.storage = unittest.mock.Mock() self.vm.storage = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.vm.volume.Set.revisions_to_keep', self.call_mgmt_func(b'admin.vm.volume.Set.revisions_to_keep',
b'test-vm1', b'private', b'abc') b'test-vm1', b'private', b'abc')
@ -2438,7 +2438,7 @@ class TC_00_VMs(AdminAPITestCase):
} }
self.vm.volumes.configure_mock(**volumes_conf) self.vm.volumes.configure_mock(**volumes_conf)
self.vm.storage = unittest.mock.Mock() self.vm.storage = unittest.mock.Mock()
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(b'admin.vm.volume.Set.revisions_to_keep', self.call_mgmt_func(b'admin.vm.volume.Set.revisions_to_keep',
b'test-vm1', b'private', b'abc') b'test-vm1', b'private', b'abc')
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
@ -2488,14 +2488,14 @@ class TC_00_VMs(AdminAPITestCase):
for method in methods_with_no_payload: for method in methods_with_no_payload:
# should reject payload regardless of having argument or not # should reject payload regardless of having argument or not
with self.subTest(method.decode('ascii')): with self.subTest(method.decode('ascii')):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(method, b'test-vm1', b'', self.call_mgmt_func(method, b'test-vm1', b'',
b'unexpected-payload') b'unexpected-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+arg'): with self.subTest(method.decode('ascii') + '+arg'):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(method, b'test-vm1', b'some-arg', self.call_mgmt_func(method, b'test-vm1', b'some-arg',
b'unexpected-payload') b'unexpected-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
@ -2526,17 +2526,18 @@ class TC_00_VMs(AdminAPITestCase):
vm_mock.qid = self.vm.qid vm_mock.qid = self.vm.qid
vm_mock.__lt__ = (lambda x, y: x.qid < y.qid) vm_mock.__lt__ = (lambda x, y: x.qid < y.qid)
self.app.domains._dict[self.vm.qid] = vm_mock self.app.domains._dict[self.vm.qid] = vm_mock
exceptions = (qubes.api.PermissionDenied, qubes.api.ProtocolError)
for method in methods_with_no_argument: for method in methods_with_no_argument:
# should reject argument regardless of having payload or not # should reject argument regardless of having payload or not
with self.subTest(method.decode('ascii')): with self.subTest(method.decode('ascii')):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(method, b'test-vm1', b'some-arg', self.call_mgmt_func(method, b'test-vm1', b'some-arg',
b'') b'')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+payload'): with self.subTest(method.decode('ascii') + '+payload'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'test-vm1', b'unexpected-arg', self.call_mgmt_func(method, b'test-vm1', b'unexpected-arg',
b'some-payload') b'some-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
@ -2570,14 +2571,14 @@ class TC_00_VMs(AdminAPITestCase):
for method in methods_with_no_payload: for method in methods_with_no_payload:
# should reject payload regardless of having argument or not # should reject payload regardless of having argument or not
with self.subTest(method.decode('ascii')): with self.subTest(method.decode('ascii')):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(method, b'dom0', b'', self.call_mgmt_func(method, b'dom0', b'',
b'unexpected-payload') b'unexpected-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+arg'): with self.subTest(method.decode('ascii') + '+arg'):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.ProtocolError):
self.call_mgmt_func(method, b'dom0', b'some-arg', self.call_mgmt_func(method, b'dom0', b'some-arg',
b'unexpected-payload') b'unexpected-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
@ -2599,17 +2600,18 @@ class TC_00_VMs(AdminAPITestCase):
vm_mock.qid = self.vm.qid vm_mock.qid = self.vm.qid
vm_mock.__lt__ = (lambda x, y: x.qid < y.qid) vm_mock.__lt__ = (lambda x, y: x.qid < y.qid)
self.app.domains._dict[self.vm.qid] = vm_mock self.app.domains._dict[self.vm.qid] = vm_mock
exceptions = (qubes.api.PermissionDenied, qubes.api.ProtocolError)
for method in methods_with_no_argument: for method in methods_with_no_argument:
# should reject argument regardless of having payload or not # should reject argument regardless of having payload or not
with self.subTest(method.decode('ascii')): with self.subTest(method.decode('ascii')):
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(method, b'dom0', b'some-arg', self.call_mgmt_func(method, b'dom0', b'some-arg',
b'') b'')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+payload'): with self.subTest(method.decode('ascii') + '+payload'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'dom0', b'unexpected-arg', self.call_mgmt_func(method, b'dom0', b'unexpected-arg',
b'some-payload') b'some-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
@ -2652,31 +2654,32 @@ class TC_00_VMs(AdminAPITestCase):
vm_mock.qid = self.vm.qid vm_mock.qid = self.vm.qid
vm_mock.__lt__ = (lambda x, y: x.qid < y.qid) vm_mock.__lt__ = (lambda x, y: x.qid < y.qid)
self.app.domains._dict[self.vm.qid] = vm_mock self.app.domains._dict[self.vm.qid] = vm_mock
exceptions = (qubes.api.PermissionDenied, qubes.api.ProtocolError)
for method in methods_for_dom0_only: for method in methods_for_dom0_only:
# should reject call regardless of having payload or not # should reject call regardless of having payload or not
with self.subTest(method.decode('ascii')): with self.subTest(method.decode('ascii')):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'test-vm1', b'', self.call_mgmt_func(method, b'test-vm1', b'',
b'') b'')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+arg'): with self.subTest(method.decode('ascii') + '+arg'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'test-vm1', b'some-arg', self.call_mgmt_func(method, b'test-vm1', b'some-arg',
b'') b'')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+payload'): with self.subTest(method.decode('ascii') + '+payload'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'test-vm1', b'', self.call_mgmt_func(method, b'test-vm1', b'',
b'payload') b'payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+arg+payload'): with self.subTest(method.decode('ascii') + '+arg+payload'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'test-vm1', b'some-arg', self.call_mgmt_func(method, b'test-vm1', b'some-arg',
b'some-payload') b'some-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
@ -2737,32 +2740,33 @@ class TC_00_VMs(AdminAPITestCase):
vm_mock.qid = self.vm.qid vm_mock.qid = self.vm.qid
vm_mock.__lt__ = (lambda x, y: x.qid < y.qid) vm_mock.__lt__ = (lambda x, y: x.qid < y.qid)
self.app.domains._dict[self.vm.qid] = vm_mock self.app.domains._dict[self.vm.qid] = vm_mock
exceptions = (qubes.api.PermissionDenied, qubes.api.ProtocolError)
for method in methods_for_vm_only: for method in methods_for_vm_only:
# should reject payload regardless of having argument or not # should reject payload regardless of having argument or not
# should reject call regardless of having payload or not # should reject call regardless of having payload or not
with self.subTest(method.decode('ascii')): with self.subTest(method.decode('ascii')):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'dom0', b'', self.call_mgmt_func(method, b'dom0', b'',
b'') b'')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+arg'): with self.subTest(method.decode('ascii') + '+arg'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'dom0', b'some-arg', self.call_mgmt_func(method, b'dom0', b'some-arg',
b'') b'')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+payload'): with self.subTest(method.decode('ascii') + '+payload'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'dom0', b'', self.call_mgmt_func(method, b'dom0', b'',
b'payload') b'payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)
self.assertFalse(self.app.save.called) self.assertFalse(self.app.save.called)
with self.subTest(method.decode('ascii') + '+arg+payload'): with self.subTest(method.decode('ascii') + '+arg+payload'):
with self.assertRaises(AssertionError): with self.assertRaises(exceptions):
self.call_mgmt_func(method, b'dom0', b'some-arg', self.call_mgmt_func(method, b'dom0', b'some-arg',
b'some-payload') b'some-payload')
self.assertFalse(vm_mock.called) self.assertFalse(vm_mock.called)

View File

@ -93,7 +93,7 @@ class TC_00_API_Misc(qubes.tests.QubesTestCase):
'/features-request/feature1': b'test spaces', '/features-request/feature1': b'test spaces',
} }
self.configure_qdb(qdb_entries) self.configure_qdb(qdb_entries)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'qubes.FeaturesRequest') self.call_mgmt_func(b'qubes.FeaturesRequest')
self.assertEqual(self.app.mock_calls, []) self.assertEqual(self.app.mock_calls, [])
self.assertEqual(self.src.mock_calls, [ self.assertEqual(self.src.mock_calls, [
@ -171,7 +171,7 @@ class TC_00_API_Misc(qubes.tests.QubesTestCase):
'/qubes-tools/default-user': b'user', '/qubes-tools/default-user': b'user',
} }
self.configure_qdb(qdb_entries) self.configure_qdb(qdb_entries)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'qubes.NotifyTools') self.call_mgmt_func(b'qubes.NotifyTools')
self.assertEqual(self.app.mock_calls, []) self.assertEqual(self.app.mock_calls, [])
self.assertEqual(self.src.mock_calls, [ self.assertEqual(self.src.mock_calls, [
@ -187,7 +187,7 @@ class TC_00_API_Misc(qubes.tests.QubesTestCase):
'/qubes-tools/default-user': b'user', '/qubes-tools/default-user': b'user',
} }
self.configure_qdb(qdb_entries) self.configure_qdb(qdb_entries)
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'qubes.NotifyTools') self.call_mgmt_func(b'qubes.NotifyTools')
self.assertEqual(self.app.mock_calls, []) self.assertEqual(self.app.mock_calls, [])
self.assertEqual(self.src.mock_calls, [ self.assertEqual(self.src.mock_calls, [
@ -219,14 +219,14 @@ class TC_00_API_Misc(qubes.tests.QubesTestCase):
def test_022_notify_updates_invalid(self): def test_022_notify_updates_invalid(self):
del self.src.template del self.src.template
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'') self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'')
self.assertEqual(self.src.mock_calls, []) self.assertEqual(self.src.mock_calls, [])
self.assertEqual(self.app.mock_calls, []) self.assertEqual(self.app.mock_calls, [])
def test_023_notify_updates_invalid2(self): def test_023_notify_updates_invalid2(self):
del self.src.template del self.src.template
with self.assertRaises(AssertionError): with self.assertRaises(qubes.api.PermissionDenied):
self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'no updates') self.call_mgmt_func(b'qubes.NotifyUpdates', payload=b'no updates')
self.assertEqual(self.src.mock_calls, []) self.assertEqual(self.src.mock_calls, [])
self.assertEqual(self.app.mock_calls, []) self.assertEqual(self.app.mock_calls, [])