Python unittest.mock.sentinel() Examples
The following are 30
code examples of unittest.mock.sentinel().
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: test_selector_events.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_force_close(self): tr = self.create_transport() tr._buffer.extend(b'1') self.loop.add_reader(7, mock.sentinel) self.loop.add_writer(7, mock.sentinel) tr._force_close(None) self.assertTrue(tr.is_closing()) self.assertEqual(tr._buffer, list_to_buffer()) self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) # second close should not remove reader tr._force_close(None) self.assertFalse(self.loop.readers) self.assertEqual(1, self.loop.remove_reader_count[7])
Example #2
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def _test_set_path_admin_acls(self, mock_execute_system32_process, ret_val=None): mock_path = mock.sentinel.path expected_logging = ["Assigning admin ACLs on path: %s" % mock_path] expected_call = [ "icacls.exe", mock_path, "/inheritance:r", "/grant:r", "*S-1-5-18:(OI)(CI)F", "*S-1-5-32-544:(OI)(CI)F"] mock_execute_system32_process.return_value = ( mock.sentinel.out, mock.sentinel.err, ret_val) with self.snatcher: if ret_val: self.assertRaises( exception.CloudbaseInitException, self._winutils.set_path_admin_acls, mock_path) else: self._winutils.set_path_admin_acls(mock_path) self.assertEqual(self.snatcher.output, expected_logging) mock_execute_system32_process.assert_called_once_with(expected_call)
Example #3
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def _test_enum_users(self, resume_handle=False, exc=None): userenum_mock = self._win32net_mock.NetUserEnum if exc is not None: userenum_mock.side_effect = [exc] with self.assertRaises(exception.CloudbaseInitException): self._winutils.enum_users() return else: userenum_mock.side_effect = ( [([{"name": "fake name"}], mock.sentinel, False)] * 3 + [([{"name": "fake name"}], mock.sentinel, resume_handle)]) self._winutils.enum_users() result = self._winutils.enum_users() if resume_handle: self.assertEqual(result, ["fake name"] * 3)
Example #4
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def _test_take_path_ownership(self, mock_execute_system32_process, ret_val=None, username=None): mock_path = mock.sentinel.path expected_logging = ["Taking ownership of path: %s" % mock_path] expected_call = ["takeown.exe", "/F", mock_path] mock_execute_system32_process.return_value = ( mock.sentinel.out, mock.sentinel.err, ret_val) if username: self.assertRaises( NotImplementedError, self._winutils.take_path_ownership, mock_path, username) return with self.snatcher: if ret_val: self.assertRaises( exception.CloudbaseInitException, self._winutils.take_path_ownership, mock_path, username) else: self._winutils.take_path_ownership(mock_path, username) self.assertEqual(self.snatcher.output, expected_logging) mock_execute_system32_process.assert_called_once_with(expected_call)
Example #5
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def test_execute_process_as_user_fail(self, mock_list2cmdline, mock_proc_info, mock_startup_info): advapi32 = self._windll_mock.advapi32 advapi32.CreateProcessAsUserW.side_effect = [False, True] kernel32 = self._ctypes_mock.windll.kernel32 kernel32.GetExitCodeProcess.return_value = False mock_proc_info.hProcess = True token = mock.sentinel.token args = mock.sentinel.args new_console = mock.sentinel.new_console with self.assert_raises_windows_message("CreateProcessAsUserW " "failed: %r", 100): self._winutils.execute_process_as_user(token, args, False, new_console) with self.assert_raises_windows_message("GetExitCodeProcess " "failed: %r", 100): self._winutils.execute_process_as_user(token, args, True, new_console)
Example #6
Source File: test_ephemeraldisk.py From cloudbase-init with Apache License 2.0 | 6 votes |
def _test_get_ephemeral_disk_volume_by_label(self, label, ephemeral_disk_volume_label): expected_result = None mock_osutils = mock.MagicMock() if ephemeral_disk_volume_label: labels = [None, str(mock.sentinel.label)] * 2 labels += [label] + [None, str(mock.sentinel.label)] mock_osutils.get_logical_drives.return_value = range(len(labels)) mock_osutils.get_volume_label.side_effect = labels if label.upper() == ephemeral_disk_volume_label.upper(): expected_result = labels.index(label) with testutils.ConfPatcher('ephemeral_disk_volume_label', ephemeral_disk_volume_label): result = self._disk._get_ephemeral_disk_volume_by_label( mock_osutils) self.assertEqual(result, expected_result) if ephemeral_disk_volume_label: mock_osutils.get_logical_drives.assert_called_once_with() if expected_result is not None: self.assertEqual(mock_osutils.get_volume_label.call_count, expected_result + 1) else: self.assertEqual(mock_osutils.get_volume_label.call_count, len(labels))
Example #7
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def test__get_forward_table_insufficient_buffer_no_memory(self): self._kernel32.HeapAlloc.side_effect = (mock.sentinel.table_mem, None) self._iphlpapi.GetIpForwardTable.return_value = ( self._winutils.ERROR_INSUFFICIENT_BUFFER) with self.assertRaises(exception.CloudbaseInitException): with self._winutils._get_forward_table(): pass table = self._ctypes_mock.cast.return_value self._iphlpapi.GetIpForwardTable.assert_called_once_with( table, self._ctypes_mock.byref.return_value, 0) heap_calls = [ mock.call(self._kernel32.GetProcessHeap.return_value, 0, table), mock.call(self._kernel32.GetProcessHeap.return_value, 0, table) ] self.assertEqual(heap_calls, self._kernel32.HeapFree.mock_calls)
Example #8
Source File: test_ephemeraldisk.py From cloudbase-init with Apache License 2.0 | 6 votes |
def _test_set_ephemeral_disk_data_loss_warning(self, exception_raised): mock_service = mock.MagicMock() disk_warning_path = str(mock.sentinel.disk_warning_path) expected_logging = [ "Setting ephemeral disk data loss warning: %s" % disk_warning_path ] if exception_raised: eclass = metadata_services_base.NotExistingMetadataException mock_service.get_ephemeral_disk_data_loss_warning.side_effect = \ eclass expected_logging += [ "Metadata service does not provide an ephemeral " "disk data loss warning content" ] else: mock_service.get_ephemeral_disk_data_loss_warning.return_value = \ str(mock.sentinel.data_loss_warning) with testutils.LogSnatcher(MODULE_PATH) as snatcher: with mock.patch(MODULE_PATH + '.open', mock.mock_open(), create=True) as mock_open: self._disk._set_ephemeral_disk_data_loss_warning( mock_service, disk_warning_path) self.assertEqual(snatcher.output, expected_logging) mock_open.assert_called_once_with(disk_warning_path, 'wb')
Example #9
Source File: test_selector_events.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_force_close(self): tr = self.create_transport() tr._buffer.extend(b'1') self.loop.add_reader(7, mock.sentinel) self.loop.add_writer(7, mock.sentinel) tr._force_close(None) self.assertTrue(tr.is_closing()) self.assertEqual(tr._buffer, list_to_buffer()) self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) # second close should not remove reader tr._force_close(None) self.assertFalse(self.loop.readers) self.assertEqual(1, self.loop.remove_reader_count[7])
Example #10
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def test_set_service_credentials(self, mock_get_service): self._win32service_mock.SERVICE_CHANGE_CONFIG = mock.sentinel.change self._win32service_mock.SERVICE_NO_CHANGE = mock.sentinel.no_change mock_change_service = self._win32service_mock.ChangeServiceConfig mock_context_manager = mock.MagicMock() mock_context_manager.__enter__.return_value = mock.sentinel.hs mock_get_service.return_value = mock_context_manager self._winutils.set_service_credentials( mock.sentinel.service, mock.sentinel.user, mock.sentinel.password) mock_get_service.assert_called_with(mock.sentinel.service, mock.sentinel.change) mock_change_service.assert_called_with( mock.sentinel.hs, mock.sentinel.no_change, mock.sentinel.no_change, mock.sentinel.no_change, None, None, False, None, mock.sentinel.user, mock.sentinel.password, None)
Example #11
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def test_create_service(self, mock_get_service_control_manager): mock_hs = mock.MagicMock() mock_service_name = "fake name" mock_start_mode = "Automatic" mock_display_name = mock.sentinel.mock_display_name mock_path = mock.sentinel.path mock_get_service_control_manager.return_value = mock_hs with self.snatcher: self._winutils.create_service(mock_service_name, mock_display_name, mock_path, mock_start_mode) self.assertEqual(["Creating service fake name"], self.snatcher.output) mock_get_service_control_manager.assert_called_once_with( scm_access=self._win32service_mock.SC_MANAGER_CREATE_SERVICE) self._win32service_mock.CreateService.assert_called_once_with( mock_hs.__enter__(), mock_service_name, mock_display_name, self._win32service_mock.SERVICE_ALL_ACCESS, self._win32service_mock.SERVICE_WIN32_OWN_PROCESS, self._win32service_mock.SERVICE_AUTO_START, self._win32service_mock.SERVICE_ERROR_NORMAL, mock_path, None, False, None, None, None)
Example #12
Source File: test_selector_events.py From annotated-py-projects with MIT License | 6 votes |
def test_force_close(self): tr = self.create_transport() tr._buffer.extend(b'1') self.loop.add_reader(7, mock.sentinel) self.loop.add_writer(7, mock.sentinel) tr._force_close(None) self.assertTrue(tr._closing) self.assertEqual(tr._buffer, list_to_buffer()) self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) # second close should not remove reader tr._force_close(None) self.assertFalse(self.loop.readers) self.assertEqual(1, self.loop.remove_reader_count[7])
Example #13
Source File: test_selector_events.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_force_close(self): tr = self.create_transport() tr._buffer.extend(b'1') self.loop._add_reader(7, mock.sentinel) self.loop._add_writer(7, mock.sentinel) tr._force_close(None) self.assertTrue(tr.is_closing()) self.assertEqual(tr._buffer, list_to_buffer()) self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) # second close should not remove reader tr._force_close(None) self.assertFalse(self.loop.readers) self.assertEqual(1, self.loop.remove_reader_count[7])
Example #14
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 6 votes |
def test_get_service_handle(self): open_scm = self._win32service_mock.OpenSCManager open_scm.return_value = mock.sentinel.hscm open_service = self._win32service_mock.OpenService open_service.return_value = mock.sentinel.hs close_service = self._win32service_mock.CloseServiceHandle args = ("fake_name", mock.sentinel.service_access, mock.sentinel.scm_access) with self._winutils._get_service_handle(*args) as hs: self.assertIs(hs, mock.sentinel.hs) open_scm.assert_called_with(None, None, mock.sentinel.scm_access) open_service.assert_called_with(mock.sentinel.hscm, "fake_name", mock.sentinel.service_access) close_service.assert_has_calls([mock.call(mock.sentinel.hs), mock.call(mock.sentinel.hscm)])
Example #15
Source File: test_selector_events.py From android_universal with MIT License | 6 votes |
def test_force_close(self): tr = self.create_transport() tr._buffer.extend(b'1') self.loop._add_reader(7, mock.sentinel) self.loop._add_writer(7, mock.sentinel) tr._force_close(None) self.assertTrue(tr.is_closing()) self.assertEqual(tr._buffer, list_to_buffer()) self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) # second close should not remove reader tr._force_close(None) self.assertFalse(self.loop.readers) self.assertEqual(1, self.loop.remove_reader_count[7])
Example #16
Source File: test_lb_public_ip.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_acquire_service_pub_ip_info_usr_specified_ip(self): cls = d_lb_public_ip.FloatingIpServicePubIPDriver m_driver = mock.Mock(spec=cls) m_driver._drv_pub_ip = public_ip.FipPubIpDriver() os_net = self.useFixture(k_fix.MockNetworkClient()).client fip = munch.Munch({'floating_ip_address': '1.2.3.4', 'port_id': None, 'id': 'a2a62ea7-e3bf-40df-8c09-aa0c29876a6b'}) os_net.ips.return_value = (ip for ip in [fip]) project_id = mock.sentinel.project_id spec_type = 'LoadBalancer' spec_lb_ip = '1.2.3.4' expected_resp = (obj_lbaas .LBaaSPubIp(ip_id=fip.id, ip_addr=fip.floating_ip_address, alloc_method='user')) result = cls.acquire_service_pub_ip_info(m_driver, spec_type, spec_lb_ip, project_id) self.assertEqual(result, expected_resp)
Example #17
Source File: test_lb_public_ip.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_acquire_service_pub_ip_info_alloc_from_pool(self, m_cfg): cls = d_lb_public_ip.FloatingIpServicePubIPDriver m_driver = mock.Mock(spec=cls) m_driver._drv_pub_ip = public_ip.FipPubIpDriver() os_net = self.useFixture(k_fix.MockNetworkClient()).client m_cfg.neutron_defaults.external_svc_subnet = (mock.sentinel .external_svc_subnet) os_net.get_subnet.return_value = munch.Munch( {'network_id': 'ec29d641-fec4-4f67-928a-124a76b3a8e6'}) fip = munch.Munch({'floating_ip_address': '1.2.3.5', 'id': 'ec29d641-fec4-4f67-928a-124a76b3a888'}) os_net.create_ip.return_value = fip project_id = mock.sentinel.project_id spec_type = 'LoadBalancer' spec_lb_ip = None expected_resp = obj_lbaas.LBaaSPubIp(ip_id=fip.id, ip_addr=fip.floating_ip_address, alloc_method='pool') result = cls.acquire_service_pub_ip_info(m_driver, spec_type, spec_lb_ip, project_id) self.assertEqual(result, expected_resp)
Example #18
Source File: test_utils.py From dwave-cloud-client with Apache License 2.0 | 6 votes |
def test_func_called_only_until_succeeds(self): """Wrapped function is called no more times then it takes to succeed.""" err = ValueError val = mock.sentinel attrs = dict(__name__='f') # f succeeds on 3rd try f = mock.Mock(side_effect=[err, err, val.a, val.b], **attrs) ret = retried(3)(f)() self.assertEqual(ret, val.a) self.assertEqual(f.call_count, 3) # fail with only on retry f = mock.Mock(side_effect=[err, err, val.a, val.b], **attrs) with self.assertRaises(err): ret = retried(1)(f)() # no errors, return without retries f = mock.Mock(side_effect=[val.a, val.b, val.c], **attrs) ret = retried(3)(f)() self.assertEqual(ret, val.a) self.assertEqual(f.call_count, 1)
Example #19
Source File: test_lb_public_ip.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_acquire_service_pub_ip_info_user_specified_non_exist_fip(self): cls = d_lb_public_ip.FloatingIpServicePubIPDriver m_driver = mock.Mock(spec=cls) m_driver._drv_pub_ip = public_ip.FipPubIpDriver() os_net = self.useFixture(k_fix.MockNetworkClient()).client fip = munch.Munch({'floating_ip_address': '1.2.3.5', 'port_id': None}) os_net.ips.return_value = (ip for ip in [fip]) project_id = mock.sentinel.project_id spec_type = 'LoadBalancer' spec_lb_ip = '1.2.3.4' result = cls.acquire_service_pub_ip_info(m_driver, spec_type, spec_lb_ip, project_id) self.assertIsNone(result)
Example #20
Source File: test_lb_public_ip.py From kuryr-kubernetes with Apache License 2.0 | 6 votes |
def test_acquire_service_pub_ip_info_user_specified_occupied_fip(self): cls = d_lb_public_ip.FloatingIpServicePubIPDriver m_driver = mock.Mock(spec=cls) m_driver._drv_pub_ip = public_ip.FipPubIpDriver() os_net = self.useFixture(k_fix.MockNetworkClient()).client fip = munch.Munch({'floating_ip_address': '1.2.3.4', 'port_id': 'ec29d641-fec4-4f67-928a-124a76b3a8e6'}) os_net.ips.return_value = (ip for ip in [fip]) project_id = mock.sentinel.project_id spec_type = 'LoadBalancer' spec_lb_ip = '1.2.3.4' result = cls.acquire_service_pub_ip_info(m_driver, spec_type, spec_lb_ip, project_id) self.assertIsNone(result)
Example #21
Source File: test_selector_events.py From android_universal with MIT License | 5 votes |
def test__add_reader(self): tr = self.create_transport() tr._buffer.extend(b'1') tr._add_reader(7, mock.sentinel) self.assertTrue(self.loop.readers) tr._force_close(None) self.assertTrue(tr.is_closing()) self.assertFalse(self.loop.readers) # can not add readers after closing tr._add_reader(7, mock.sentinel) self.assertFalse(self.loop.readers)
Example #22
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_get_file_version(self): mock_path = mock.sentinel.fake_path mock_info = mock.MagicMock() self._win32api_mock.GetFileVersionInfo.return_value = mock_info res = self._winutils.get_file_version(mock_path) self._win32api_mock.GetFileVersionInfo.assert_called_once_with( mock_path, '\\') self.assertIsNotNone(res)
Example #23
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_create_network_team(self, mock_get_network_team_manager): mock_team_manager = mock_get_network_team_manager.return_value self._winutils.create_network_team( mock.sentinel.team_name, mock.sentinel.mode, mock.sentinel.lb_algo, mock.sentinel.members, mock.sentinel.mac, mock.sentinel.primary_name, mock.sentinel.vlan_id, mock.sentinel.lacp_timer) mock_team_manager.create_team.assert_called_once_with( mock.sentinel.team_name, mock.sentinel.mode, mock.sentinel.lb_algo, mock.sentinel.members, mock.sentinel.mac, mock.sentinel.primary_name, mock.sentinel.vlan_id, mock.sentinel.lacp_timer)
Example #24
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_add_network_team_nic(self, mock_get_network_team_manager): mock_team_manager = mock_get_network_team_manager.return_value self._winutils.add_network_team_nic( mock.sentinel.team_name, mock.sentinel.nic_name, mock.sentinel.vlan_id) mock_team_manager.add_team_nic.assert_called_once_with( mock.sentinel.team_name, mock.sentinel.nic_name, mock.sentinel.vlan_id)
Example #25
Source File: test_ephemeraldisk.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_get_ephemeral_disk_volume_by_label_no_label(self): self._test_get_ephemeral_disk_volume_by_label( label=str(mock.sentinel.label), ephemeral_disk_volume_label=str(mock.sentinel.disk_volume_label))
Example #26
Source File: test_ephemeraldisk.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_get_ephemeral_disk_volume_by_label(self): self._test_get_ephemeral_disk_volume_by_label( label=str(mock.sentinel.same_label), ephemeral_disk_volume_label=str(mock.sentinel.same_label))
Example #27
Source File: test_ephemeraldisk.py From cloudbase-init with Apache License 2.0 | 5 votes |
def _test_execute(self, mock_get_osutils, mock_get_volume_path, mock_set_volume_path, mock_join, not_existing_exception, disk_volume_path, disk_warning_path): mock_service = mock.MagicMock() shared_data = mock.sentinel.shared_data eclass = metadata_services_base.NotExistingMetadataException expected_result = base.PLUGIN_EXECUTION_DONE, False expected_logging = [] if not_existing_exception: mock_service.get_ephemeral_disk_data_loss_warning.side_effect = \ eclass() else: mock_osutils = mock.MagicMock() mock_get_osutils.return_value = mock_osutils mock_get_volume_path.return_value = disk_volume_path if not disk_volume_path: expected_logging += [ "Ephemeral disk volume not found" ] else: mock_join.return_value = disk_warning_path with testutils.ConfPatcher('ephemeral_disk_data_loss_warning_path', disk_warning_path): with testutils.LogSnatcher(MODULE_PATH) as snatcher: result = self._disk.execute(mock_service, shared_data) self.assertEqual(result, expected_result) self.assertEqual(snatcher.output, expected_logging) (mock_service.get_ephemeral_disk_data_loss_warning. assert_called_once_with()) if not not_existing_exception: mock_get_osutils.assert_called_once_with() mock_get_volume_path.assert_called_once_with(mock_osutils) if disk_volume_path and disk_warning_path: mock_join.assert_called_once_with( disk_volume_path, disk_warning_path) mock_set_volume_path.assert_called_once_with( mock_service, disk_warning_path)
Example #28
Source File: test_ephemeraldisk.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_execute(self): self._test_execute( not_existing_exception=None, disk_volume_path=str(mock.sentinel.disk_volume_path), disk_warning_path=str(mock.sentinel.path))
Example #29
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_execute_process_as_user(self): self._test_execute_process_as_user(token=mock.sentinel.token, args=mock.sentinel.args, wait=False, new_console=False)
Example #30
Source File: test_windows.py From cloudbase-init with Apache License 2.0 | 5 votes |
def test_execute_system32_process(self, mock_execute_process, mock_get_system_dir): mock_get_system_dir.return_value = 'base_dir' mock_execute_process.return_value = mock.sentinel.execute_process args = ['command', 'argument'] result = self._winutils.execute_system32_process(args) mock_execute_process.assert_called_once_with( [os.path.join('base_dir', args[0])] + args[1:], decode_output=False, shell=True) self.assertEqual(mock.sentinel.execute_process, result)