Python unittest.mock.called() Examples

The following are 30 code examples of unittest.mock.called(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module unittest.mock , or try the search function .
Example #1
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_520_vm_volume_clone(self):
        self.setup_for_clone()
        token = self.call_mgmt_func(b'admin.vm.volume.CloneFrom',
                b'test-vm1', b'private', b'')
        # token
        self.assertEqual(len(token), 32)
        self.assertFalse(self.app.save.called)
        value = self.call_mgmt_func(b'admin.vm.volume.CloneTo',
                b'test-vm2', b'private', token.encode())
        self.assertIsNone(value)
        self.vm2.volumes['private'].import_volume.assert_called_once_with(
            self.vm.volumes['private']
        )
        self.vm2.volumes['private'].import_volume.assert_called_once_with(
            self.vm2.volumes['private']
        )
        self.app.save.assert_called_once_with() 
Example #2
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_340_vm_create_in_pool_app(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        self.call_mgmt_func(b'admin.vm.CreateInPool.AppVM',
            b'dom0', b'test-template', b'name=test-vm2 label=red '
                                 b'pool=test')

        self.assertIn('test-vm2', self.app.domains)
        vm = self.app.domains['test-vm2']
        self.assertEqual(vm.label, self.app.get_label('red'))
        self.assertEqual(vm.template, self.app.domains['test-template'])
        # setting pool= affect only volumes actually created for this VM,
        # not used from a template or so
        self.assertEqual(vm.volume_config['root']['pool'],
            self.template.volumes['root'].pool)
        self.assertEqual(vm.volume_config['private']['pool'], 'test')
        self.assertEqual(vm.volume_config['volatile']['pool'], 'test')
        self.assertEqual(vm.volume_config['kernel']['pool'], 'linux-kernel')
        self.assertEqual(storage_mock.mock_calls,
            [unittest.mock.call(self.app.domains['test-vm2']).create()])
        self.assertTrue(os.path.exists(os.path.join(
            self.test_base_dir, 'appvms', 'test-vm2')))

        self.assertTrue(self.app.save.called) 
Example #3
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_200_label_create(self):
        self.app.get_label = unittest.mock.Mock()
        self.app.get_label.side_effect=KeyError
        self.app.labels = unittest.mock.MagicMock()
        labels_config = {
            'keys.return_value': range(1, 9),
        }
        self.app.labels.configure_mock(**labels_config)
        value = self.call_mgmt_func(b'admin.label.Create', b'dom0', b'cyan',
            b'0x00ffff')
        self.assertIsNone(value)
        self.assertEqual(self.app.get_label.mock_calls,
            [unittest.mock.call('cyan')])
        self.assertEqual(self.app.labels.mock_calls,
            [unittest.mock.call.keys(),
            unittest.mock.call.__getattr__('__setitem__')(9,
                qubes.Label(9, '0x00ffff', 'cyan'))])
        self.assertTrue(self.app.save.called) 
Example #4
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_160_pool_add_existing_pool(self, mock_parameters, mock_drivers):
        self.app.pools = {
            'file': unittest.mock.Mock(),
            'lvm': unittest.mock.Mock()
        }

        mock_drivers.return_value = ['driver1', 'driver2']
        mock_parameters.side_effect = \
            lambda driver: {
                'driver1': ['param1', 'param2'],
                'driver2': ['param3', 'param4']
            }[driver]

        add_pool_mock, self.app.add_pool = self.coroutine_mock()

        with self.assertRaises(qubes.api.PermissionDenied):
            self.call_mgmt_func(b'admin.pool.Add', b'dom0',
                b'driver1', b'name=file\nparam1=value\nparam2=some-value\n')
        self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
        self.assertEqual(mock_parameters.mock_calls, [])
        self.assertEqual(add_pool_mock.mock_calls, [])
        self.assertFalse(self.app.save.called) 
Example #5
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_160_pool_add_invalid_param(self, mock_parameters, mock_drivers):
        self.app.pools = {
            'file': unittest.mock.Mock(),
            'lvm': unittest.mock.Mock()
        }

        mock_drivers.return_value = ['driver1', 'driver2']
        mock_parameters.side_effect = \
            lambda driver: {
                'driver1': ['param1', 'param2'],
                'driver2': ['param3', 'param4']
            }[driver]

        add_pool_mock, self.app.add_pool = self.coroutine_mock()

        with self.assertRaises(qubes.exc.QubesException):
            self.call_mgmt_func(b'admin.pool.Add', b'dom0',
                b'driver1', b'name=test-pool\nparam3=some-value\n')
        self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
        self.assertEqual(mock_parameters.mock_calls,
            [unittest.mock.call('driver1')])
        self.assertEqual(add_pool_mock.mock_calls, [])
        self.assertFalse(self.app.save.called) 
Example #6
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_160_pool_add_invalid_driver(self, mock_parameters, mock_drivers):
        self.app.pools = {
            'file': unittest.mock.Mock(),
            'lvm': unittest.mock.Mock()
        }

        mock_drivers.return_value = ['driver1', 'driver2']
        mock_parameters.side_effect = \
            lambda driver: {
                'driver1': ['param1', 'param2'],
                'driver2': ['param3', 'param4']
            }[driver]

        add_pool_mock, self.app.add_pool = self.coroutine_mock()

        with self.assertRaises(qubes.api.PermissionDenied):
            self.call_mgmt_func(b'admin.pool.Add', b'dom0',
                b'no-such-driver', b'name=test-pool\nparam1=some-value\n')
        self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
        self.assertEqual(mock_parameters.mock_calls, [])
        self.assertEqual(add_pool_mock.mock_calls, [])
        self.assertFalse(self.app.save.called) 
Example #7
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_160_pool_add(self, mock_parameters, mock_drivers):
        self.app.pools = {
            'file': unittest.mock.Mock(),
            'lvm': unittest.mock.Mock()
        }

        mock_drivers.return_value = ['driver1', 'driver2']
        mock_parameters.side_effect = \
            lambda driver: {
                'driver1': ['param1', 'param2'],
                'driver2': ['param3', 'param4']
            }[driver]

        add_pool_mock, self.app.add_pool = self.coroutine_mock()

        value = self.call_mgmt_func(b'admin.pool.Add', b'dom0', b'driver1',
            b'name=test-pool\nparam1=some-value\n')
        self.assertIsNone(value)
        self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
        self.assertEqual(mock_parameters.mock_calls,
            [unittest.mock.call('driver1')])
        self.assertEqual(add_pool_mock.mock_calls,
            [unittest.mock.call(name='test-pool', driver='driver1',
                param1='some-value')])
        self.assertTrue(self.app.save.called) 
Example #8
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_153_pool_usage(self):
        self.app.pools = {
            'pool1': unittest.mock.Mock(config={
                'param1': 'value1', 'param2': 'value2'},
                usage_details={
                    'data_usage': 102400,
                    'data_size': 204800,
                    'metadata_size': 1024,
                    'metadata_usage': 50})
        }
        self.app.pools['pool1'].included_in.return_value = None
        value = self.call_mgmt_func(b'admin.pool.UsageDetails', b'dom0', b'pool1')

        self.assertEqual(value,
                         'data_size=204800\ndata_usage=102400\nmetadata_size=1024\nmetadata_usage=50\n')
        self.assertFalse(self.app.save.called) 
Example #9
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_152_pool_info_included_in(self):
        self.app.pools = {
            'pool1': unittest.mock.MagicMock(config={
                'param1': 'value1',
                'param2': 'value2'},
                usage=102400,
                size=204800)
        }
        self.app.pools['pool1'].included_in.return_value = \
            self.app.pools['pool1']
        self.app.pools['pool1'].__str__.return_value = 'pool1'
        value = self.call_mgmt_func(b'admin.pool.Info', b'dom0', b'pool1')

        self.assertEqual(value,
            'param1=value1\nparam2=value2\nsize=204800\nusage=102400'
            '\nincluded_in=pool1\n')
        self.assertFalse(self.app.save.called) 
Example #10
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_160_pool_add_invalid_config_format(self, mock_parameters,
            mock_drivers):
        self.app.pools = {
            'file': unittest.mock.Mock(),
            'lvm': unittest.mock.Mock()
        }

        mock_drivers.return_value = ['driver1', 'driver2']
        mock_parameters.side_effect = \
            lambda driver: {
                'driver1': ['param1', 'param2'],
                'driver2': ['param3', 'param4']
            }[driver]

        add_pool_mock, self.app.add_pool = self.coroutine_mock()

        with self.assertRaises(qubes.api.PermissionDenied):
            self.call_mgmt_func(b'admin.pool.Add', b'dom0',
                b'driver1', b'name=test-pool\nparam 1=value\n_param2\n')
        self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
        self.assertEqual(mock_parameters.mock_calls, [])
        self.assertEqual(add_pool_mock.mock_calls, [])
        self.assertFalse(self.app.save.called) 
Example #11
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_200_label_create_invalid_name(self):
        self.app.get_label = unittest.mock.Mock()
        self.app.get_label.side_effect=KeyError
        self.app.labels = unittest.mock.MagicMock()
        labels_config = {
            'keys.return_value': range(1, 9),
        }
        self.app.labels.configure_mock(**labels_config)
        with self.assertRaises(qubes.api.PermissionDenied):
            self.call_mgmt_func(b'admin.label.Create', b'dom0', b'01',
                b'0xff0000')
        with self.assertRaises(qubes.api.PermissionDenied):
            self.call_mgmt_func(b'admin.label.Create', b'dom0', b'../xxx',
                b'0xff0000')
        with self.assertRaises(qubes.api.PermissionDenied):
            self.call_mgmt_func(b'admin.label.Create', b'dom0',
                b'strange-name!@#$',
                b'0xff0000')

        self.assertEqual(self.app.get_label.mock_calls, [])
        self.assertEqual(self.app.labels.mock_calls, [])
        self.assertFalse(self.app.save.called) 
Example #12
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_140_pool_listdrivers(self, mock_parameters, mock_drivers):
        self.app.pools = ['file', 'lvm']

        mock_drivers.return_value = ['driver1', 'driver2']
        mock_parameters.side_effect = \
            lambda driver: {
                'driver1': ['param1', 'param2'],
                'driver2': ['param3', 'param4']
            }[driver]

        value = self.call_mgmt_func(b'admin.pool.ListDrivers', b'dom0')
        self.assertEqual(value,
            'driver1 param1 param2\ndriver2 param3 param4\n')
        self.assertEqual(mock_drivers.mock_calls, [unittest.mock.call()])
        self.assertEqual(mock_parameters.mock_calls,
            [unittest.mock.call('driver1'), unittest.mock.call('driver2')])
        self.assertFalse(self.app.save.called) 
Example #13
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_338_vm_create_name_twice(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        with self.assertRaises(qubes.api.ProtocolError):
            self.call_mgmt_func(b'admin.vm.Create.AppVM',
                b'dom0', b'test-template',
                b'name=test-vm2 name=test-vm3 label=red')

        self.assertNotIn('test-vm2', self.app.domains)
        self.assertNotIn('test-vm3', self.app.domains)
        self.assertFalse(self.app.save.called) 
Example #14
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_336_vm_create_spurious_pool(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        with self.assertRaises(qubes.api.ProtocolError):
            self.call_mgmt_func(b'admin.vm.Create.AppVM',
                b'dom0', b'test-template',
                b'name=test-vm2 label=red pool=default')

        self.assertNotIn('test-vm2', self.app.domains)
        self.assertFalse(self.app.save.called) 
Example #15
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_474_vm_device_list_specific(self):
        self.vm.add_handler('device-list-attached:testclass',
            self.device_list_attached_testclass)
        assignment = qubes.devices.DeviceAssignment(self.vm, '4321',
            persistent=True)
        self.loop.run_until_complete(
            self.vm.devices['testclass'].attach(assignment))
        value = self.call_mgmt_func(b'admin.vm.device.testclass.List',
            b'test-vm1', b'test-vm1+1234')
        self.assertEqual(value,
            'test-vm1+1234 attach_opt=value persistent=no\n')
        self.assertFalse(self.app.save.called) 
Example #16
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_491_vm_device_detach_not_attached(self):
        mock_detach = unittest.mock.Mock()
        mock_detach.return_value = None
        del mock_detach._is_coroutine
        self.vm.add_handler('device-detach:testclass', mock_detach)
        with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
                'is_halted', lambda _: False):
            with self.assertRaises(qubes.devices.DeviceNotAttached):
                self.call_mgmt_func(b'admin.vm.device.testclass.Detach',
                    b'test-vm1', b'test-vm1+1234')
        self.assertFalse(mock_detach.called)
        self.assertFalse(self.app.save.called) 
Example #17
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_482_vm_device_attach_not_running(self):
        self.vm.add_handler('device-list:testclass', self.device_list_testclass)
        mock_attach = unittest.mock.Mock()
        del mock_attach._is_coroutine
        self.vm.add_handler('device-attach:testclass', mock_attach)
        with self.assertRaises(qubes.exc.QubesVMNotRunningError):
            self.call_mgmt_func(b'admin.vm.device.testclass.Attach',
                b'test-vm1', b'test-vm1+1234')
        self.assertFalse(mock_attach.called)
        self.assertEqual(len(self.vm.devices['testclass'].persistent()), 0)
        self.assertFalse(self.app.save.called) 
Example #18
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_337_vm_create_duplicate_name(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        with self.assertRaises(qubes.exc.QubesException):
            self.call_mgmt_func(b'admin.vm.Create.AppVM',
                b'dom0', b'test-template',
                b'name=test-vm1 label=red')

        self.assertFalse(self.app.save.called) 
Example #19
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_316_feature_checkwithnetvm_netvm(self):
        self.netvm = self.app.add_new_vm('AppVM', label='red',
            name='test-netvm1',
            template='test-template',
            provides_network=True)
        self.vm.netvm = self.netvm
        self.netvm.features['test-feature'] = 'some-value'
        value = self.call_mgmt_func(b'admin.vm.feature.CheckWithNetvm',
            b'test-vm1', b'test-feature')
        self.assertEqual(value, 'some-value')
        self.assertFalse(self.app.save.called) 
Example #20
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_335_vm_create_missing_name(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        with self.assertRaises(qubes.api.ProtocolError):
            self.call_mgmt_func(b'admin.vm.Create.AppVM',
                b'dom0', b'test-template', b'label=red')

        self.assertFalse(self.app.save.called) 
Example #21
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_333_vm_create_app_default_template(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        self.call_mgmt_func(b'admin.vm.Create.AppVM',
            b'dom0', b'', b'name=test-vm2 label=red')

        self.assertEqual(storage_mock.mock_calls,
            [unittest.mock.call(self.app.domains['test-vm2']).create()])

        self.assertIn('test-vm2', self.app.domains)
        self.assertEqual(self.app.domains['test-vm2'].template,
            self.app.default_template)
        self.assertTrue(self.app.save.called) 
Example #22
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_332_vm_create_app(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        self.call_mgmt_func(b'admin.vm.Create.AppVM',
            b'dom0', b'test-template', b'name=test-vm2 label=red')

        self.assertIn('test-vm2', self.app.domains)
        vm = self.app.domains['test-vm2']
        self.assertEqual(vm.label, self.app.get_label('red'))
        self.assertEqual(vm.template, self.app.domains['test-template'])
        self.assertEqual(storage_mock.mock_calls,
            [unittest.mock.call(self.app.domains['test-vm2']).create()])
        self.assertTrue(os.path.exists(os.path.join(
            self.test_base_dir, 'appvms', 'test-vm2')))

        self.assertTrue(self.app.save.called) 
Example #23
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_331_vm_create_standalone_spurious_template(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        with self.assertRaises(qubes.exc.QubesValueError):
            self.call_mgmt_func(b'admin.vm.Create.StandaloneVM',
                b'dom0', b'test-template', b'name=test-vm2 label=red')

        self.assertNotIn('test-vm2', self.app.domains)
        self.assertEqual(storage_mock.mock_calls, [])
        self.assertFalse(os.path.exists(os.path.join(
            self.test_base_dir, 'appvms', 'test-vm2')))

        self.assertNotIn('test-vm2', self.app.domains)
        self.assertFalse(self.app.save.called) 
Example #24
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_330_vm_create_standalone(self, storage_mock):
        storage_mock.side_effect = self.dummy_coro
        self.call_mgmt_func(b'admin.vm.Create.StandaloneVM',
            b'dom0', b'', b'name=test-vm2 label=red')

        self.assertIn('test-vm2', self.app.domains)
        vm = self.app.domains['test-vm2']
        self.assertIsInstance(vm, qubes.vm.standalonevm.StandaloneVM)
        self.assertEqual(vm.label, self.app.get_label('red'))
        self.assertEqual(storage_mock.mock_calls,
            [unittest.mock.call(self.app.domains['test-vm2']).create()])
        self.assertTrue(os.path.exists(os.path.join(
            self.test_base_dir, 'appvms', 'test-vm2')))

        self.assertTrue(self.app.save.called) 
Example #25
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_320_feature_set_invalid(self):
        with self.assertRaises(UnicodeDecodeError):
            self.call_mgmt_func(b'admin.vm.feature.Set',
                b'test-vm1', b'test-feature', b'\x02\x03\xffsome-value')
        self.assertNotIn('test-feature', self.vm.features)
        self.assertFalse(self.app.save.called) 
Example #26
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_320_feature_set(self):
        value = self.call_mgmt_func(b'admin.vm.feature.Set',
            b'test-vm1', b'test-feature', b'some-value')
        self.assertIsNone(value)
        self.assertEqual(self.vm.features['test-feature'], 'some-value')
        self.assertTrue(self.app.save.called) 
Example #27
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_319_feature_checkwithtpladminvm(self):
        self.app.domains['dom0'].features['test-feature'] = 'some-value'
        value = self.call_mgmt_func(
            b'admin.vm.feature.CheckWithTemplateAndAdminVM',
            b'test-vm1', b'test-feature')
        self.assertEqual(value, 'some-value')

        self.template.features['test-feature'] = 'some-value2'
        value = self.call_mgmt_func(
            b'admin.vm.feature.CheckWithTemplateAndAdminVM',
            b'test-vm1', b'test-feature')
        self.assertEqual(value, 'some-value2')

        self.assertFalse(self.app.save.called) 
Example #28
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_318_feature_checkwithadminvm(self):
        self.app.domains['dom0'].features['test-feature'] = 'some-value'
        value = self.call_mgmt_func(b'admin.vm.feature.CheckWithAdminVM',
            b'test-vm1', b'test-feature')
        self.assertEqual(value, 'some-value')
        self.assertFalse(self.app.save.called) 
Example #29
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_317_feature_checkwithnetvm_none(self):
        with self.assertRaises(qubes.exc.QubesFeatureNotFoundError):
            self.call_mgmt_func(b'admin.vm.feature.CheckWithNetvm',
                b'test-vm1', b'test-feature')
        self.assertFalse(self.app.save.called) 
Example #30
Source File: api_admin.py    From qubes-core-admin with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_290_feature_get(self):
        self.vm.features['test-feature'] = 'some-value'
        value = self.call_mgmt_func(b'admin.vm.feature.Get', b'test-vm1',
            b'test-feature')
        self.assertEqual(value, 'some-value')
        self.assertFalse(self.app.save.called)