def testSetContentTypeFromFile(self): """Tests that content type is correctly determined for symlinks.""" if system_util.IS_WINDOWS: return unittest.skip('use_magicfile features not available on Windows') surprise_html = b'<html><body>And you thought I was just text!</body></html>' temp_dir_path = self.CreateTempDir() txt_file_path = self.CreateTempFile(tmpdir=temp_dir_path, contents=surprise_html, file_name='html_in_disguise.txt') link_name = 'link_to_realfile' # Notice no file extension was supplied. os.symlink(txt_file_path, temp_dir_path + os.path.sep + link_name) # Content-type of a symlink should be obtained from the link's target. dst_obj_metadata_mock = mock.MagicMock(contentType=None) src_url_stub = mock.MagicMock(object_name=temp_dir_path + os.path.sep + link_name, **{ 'IsFileUrl.return_value': True, 'IsStream.return_value': False, 'IsFifo.return_value': False }) # The file command should detect HTML in the real file. with SetBotoConfigForTest([('GSUtil', 'use_magicfile', 'True')]): _SetContentTypeFromFile(src_url_stub, dst_obj_metadata_mock) self.assertEqual('text/html; charset=us-ascii', dst_obj_metadata_mock.contentType) dst_obj_metadata_mock = mock.MagicMock(contentType=None) # The mimetypes module should guess based on the real file's extension. with SetBotoConfigForTest([('GSUtil', 'use_magicfile', 'False')]): _SetContentTypeFromFile(src_url_stub, dst_obj_metadata_mock) self.assertEqual('text/plain', dst_obj_metadata_mock.contentType)
def test_generate_elements_impl_over_generate_elements(self): """ Test that an error is thrown when an implementation that returns more vectors than data elements (IndexError). """ # Mock data element input data_iter = [ mock.Mock(spec=smqtk.representation.DataElement), mock.Mock(spec=smqtk.representation.DataElement), mock.Mock(spec=smqtk.representation.DataElement), ] for d in data_iter: d.content_type.return_value = 'image/png' # Mock generated descriptor elements that *don't* have vectors m_descr_elem = mock.MagicMock(spec=DescriptorElement) m_descr_elem.has_vector.return_value = False # Mock factory to return some descriptor element mock m_fact = mock.MagicMock(spec=DescriptorElementFactory) m_fact.new_descriptor.return_value = m_descr_elem # Mock generator instance to return self.inst._generate_arrays = self.inst._generate_too_many_arrays # TODO: Check index error message when fail with pytest.raises(IndexError): list( self.inst.generate_elements(data_iter, descr_factory=m_fact, overwrite=False)) # Incomplete iteration should have occurred, so post-yield # function should not be expected to have been called. self.inst._post_iterator_check.assert_not_called()
def test_generate_elements_impl_under_generate_elements(self): """ Test that an error is thrown when an implementation that returns less vectors than data elements. """ # Mock data element input data_iter = [ mock.Mock(spec=smqtk.representation.DataElement), mock.Mock(spec=smqtk.representation.DataElement), mock.Mock(spec=smqtk.representation.DataElement), ] for d in data_iter: d.content_type.return_value = 'image/png' # Mock generated descriptor elements that *don't* have vectors m_descr_elem = mock.MagicMock(spec=DescriptorElement) m_descr_elem.has_vector.return_value = False m_descr_elem.uuid.return_value = "test_uuid" # Mock factory to return some descriptor element mock m_fact = mock.MagicMock(spec=DescriptorElementFactory) m_fact.new_descriptor.return_value = m_descr_elem # Mock generator instance to return self.inst._generate_arrays = self.inst._generate_too_few_arrays with pytest.raises(IndexError): list( self.inst.generate_elements(data_iter, descr_factory=m_fact, overwrite=False)) # Under-yielding generator should have completed iteration, so the # post-yield method should have been called. self.inst._post_iterator_check.assert_called_once()
def make_mock_directive(content): """Create the main class which is translated and rendered as text. Args: content (list[str]): The lines that the user provides in a standard code-include block. Returns: :class:`code_include.extension`: The class that is later translated by Sphinx into HTML tags. """ name = "code-include" arguments = [] options = {} line_number = 11 content_offset = 10 block_text = ( u".. code-include:: :meth:`ways.asdf.base.plugin.DataPlugin.get_hierarchy`\n" ) state = mock.MagicMock() state_machine = mock.MagicMock() return extension.Directive( name, arguments, options, content, line_number, content_offset, block_text, state, state_machine, )
def test_detect_objects(): """ Test expected ``detect_objects`` behavior when ``_detect_objects`` yields expected values. """ # Test Inputs t_de1_uuid = "test uuid" t_de1 = mock.MagicMock(spec=DataElement) t_de1.uuid.return_value = t_de1_uuid # Expected outputs of _detect_objects t_det1 = (AxisAlignedBoundingBox([0, 0], [1, 1]), {'l1': 0, 'l2': 1}) t_det2 = (AxisAlignedBoundingBox([1, 1], [2, 2]), {'l1': 1, 'l3': 0}) # Mock instance of ObjectDetector mocking _detect_objects. m_inst = mock.MagicMock(spec=ObjectDetector) m_inst._detect_objects.return_value = (t_det1, t_det2) m_inst._gen_detection_uuid = \ mock.Mock(wraps=ObjectDetector._gen_detection_uuid) dets_list = list(ObjectDetector.detect_objects(m_inst, t_de1)) m_inst._detect_objects.assert_called_once_with(t_de1) assert m_inst._gen_detection_uuid.call_count == 2 m_inst._gen_detection_uuid.assert_any_call(t_de1_uuid, t_det1[0], t_det1[1].keys()) m_inst._gen_detection_uuid.assert_any_call(t_de1_uuid, t_det2[0], t_det2[1].keys()) assert len(dets_list) == 2 # Assert detections returned have the expected properties assert dets_list[0].get_detection()[0] == t_det1[0] assert dets_list[0].get_detection()[1].get_classification() == t_det1[1] assert dets_list[1].get_detection()[0] == t_det2[0] assert dets_list[1].get_detection()[1].get_classification() == t_det2[1]
def test_load_dataset_tempfile(self): """ Test DataElement temporary file based context loader. """ # Creating separate element from global so we can mock it up. e = DataFileElement(GH_IMAGE_FP, readonly=True) e.write_temp = mock.MagicMock(wraps=e.write_temp) e.clean_temp = mock.MagicMock(wraps=e.clean_temp) e.get_bytes = mock.MagicMock(wraps=e.get_bytes) # Using explicit patcher start/stop in order to avoid using ``patch`` # as a decorator because ``osgeo`` might not be defined when # decorating the method. patcher_gdal_open = mock.patch('smqtk.algorithms.image_io.gdal_io.gdal' '.Open', wraps=osgeo.gdal.Open) self.addCleanup(patcher_gdal_open.stop) m_gdal_open = patcher_gdal_open.start() with load_dataset_tempfile(e) as gdal_ds: # noinspection PyUnresolvedReferences e.write_temp.assert_called_once_with() # noinspection PyUnresolvedReferences e.get_bytes.assert_not_called() m_gdal_open.assert_called_once() assert gdal_ds.RasterCount == 3 assert gdal_ds.RasterXSize == 512 assert gdal_ds.RasterYSize == 600 # noinspection PyUnresolvedReferences e.clean_temp.assert_called_once_with() assert len(e._temp_filepath_stack) == 0
def test_run_cmd_parse_error(self, mock_run): mock_process = mock.MagicMock() mock_process.returncode = 0 mock_run.return_value = mock_process mock_parser = mock.MagicMock( side_effect=VSCtlCmdExecError('Expected Error')) vsctl = VSCtl(protocol='tcp', ip_addr='127.0.0.1', port=6640) vsctl.run('show', parser=mock_parser)
def testSetsContentTypesForCommonFileExtensionsCorrectly(self): extension_rules = copy_helper.COMMON_EXTENSION_RULES.items() for extension, expected_content_type in extension_rules: dst_obj_metadata_mock = mock.MagicMock(contentType=None) src_url_stub = mock.MagicMock(object_name='file.' + extension) src_url_stub.IsFileUrl.return_value = True src_url_stub.IsStream.return_value = False src_url_stub.IsFifo.return_value = False _SetContentTypeFromFile(src_url_stub, dst_obj_metadata_mock) self.assertEqual(expected_content_type, dst_obj_metadata_mock.contentType)
def test_mask_passwords(self): # Ensure that passwords are masked with notifications driver = _impl_log.LogDriver(None, None, None) logger = mock.MagicMock() logger.info = mock.MagicMock() message = {'password': '******', 'event_type': 'foo'} mask_str = jsonutils.dumps(strutils.mask_dict_password(message)) with mock.patch.object(logging, 'getLogger') as gl: gl.return_value = logger driver.notify(None, message, 'info', 0) logger.info.assert_called_once_with(mask_str)
def test_is_valid_element_has_matrix(): """ Test handling a DataElement instance that does have a ``matrix`` property """ test_de = mock.MagicMock(spec=MatrixDataElement) m_inst = mock.MagicMock(spec=ImageReader) # Matrix return value should be appropriate type: None | ndarray test_de.matrix = None assert ImageReader.is_valid_element(m_inst, test_de) test_de.matrix = np.eye(3) assert ImageReader.is_valid_element(m_inst, test_de)
def test_load_as_matrix_crop_zero_volume(): """ Test that a ValueError is raised when a crop bbox is passed with zero volume. """ m_reader = mock.MagicMock(spec=ImageReader) m_data = mock.MagicMock(spec_set=DataElement) crop_bb = AxisAlignedBoundingBox([0, 0], [0, 0]) with pytest.raises(ValueError, match=r"Volume of crop bounding box must be " r"greater than 0\."): ImageReader.load_as_matrix(m_reader, m_data, pixel_crop=crop_bb)
def test_load_dataset_vsimem(self): """ Test that VSIMEM loading context """ if LooseVersion(osgeo.__version__).version[0] < 2: pytest.skip("Skipping VSIMEM test because GDAL version < 2") # Creating separate element from global so we can mock it up. e = DataFileElement(GH_IMAGE_FP, readonly=True) e.write_temp = mock.MagicMock(wraps=e.write_temp) e.clean_temp = mock.MagicMock(wraps=e.clean_temp) e.get_bytes = mock.MagicMock(wraps=e.get_bytes) vsimem_path_re = re.compile(r'^/vsimem/\w+$') # Using explicit patcher start/stop in order to avoid using ``patch`` # as a *decorator* because ``osgeo`` might not be defined when # decorating the method. patcher_gdal_open = mock.patch( 'smqtk.algorithms.image_io.gdal_io.gdal.Open', wraps=osgeo.gdal.Open, ) self.addCleanup(patcher_gdal_open.stop) patcher_gdal_unlink = mock.patch( 'smqtk.algorithms.image_io.gdal_io.gdal.Unlink', wraps=osgeo.gdal.Unlink, ) self.addCleanup(patcher_gdal_unlink.stop) m_gdal_open = patcher_gdal_open.start() m_gdal_unlink = patcher_gdal_unlink.start() with load_dataset_vsimem(e) as gdal_ds: # noinspection PyUnresolvedReferences e.write_temp.assert_not_called() # noinspection PyUnresolvedReferences e.get_bytes.assert_called_once_with() m_gdal_open.assert_called_once() ds_path = gdal_ds.GetDescription() assert vsimem_path_re.match(ds_path) assert gdal_ds.RasterCount == 3 assert gdal_ds.RasterXSize == 512 assert gdal_ds.RasterYSize == 600 m_gdal_unlink.assert_called_once_with(ds_path) # noinspection PyUnresolvedReferences e.clean_temp.assert_not_called() assert len(e._temp_filepath_stack) == 0
def testExceptionCatchingDecorator(self): """Tests the exception catching decorator CaptureAndLogException.""" # A wrapped function with an exception should not stop the process. mock_exc_fn = mock.MagicMock(__name__=str('mock_exc_fn'), side_effect=Exception()) wrapped_fn = metrics.CaptureAndLogException(mock_exc_fn) wrapped_fn() debug_messages = self.log_handler.messages['debug'] self.assertIn('Exception captured in mock_exc_fn during metrics collection', debug_messages[0]) self.log_handler.reset() self.assertEqual(1, mock_exc_fn.call_count) mock_err_fn = mock.MagicMock(__name__=str('mock_err_fn'), side_effect=TypeError()) wrapped_fn = metrics.CaptureAndLogException(mock_err_fn) wrapped_fn() self.assertEqual(1, mock_err_fn.call_count) debug_messages = self.log_handler.messages['debug'] self.assertIn('Exception captured in mock_err_fn during metrics collection', debug_messages[0]) self.log_handler.reset() # Test that exceptions in the unprotected metrics functions are caught. with mock.patch.object(MetricsCollector, 'GetCollector', return_value='not a collector'): # These calls should all fail, but the exceptions shouldn't propagate up. metrics.Shutdown() metrics.LogCommandParams() metrics.LogRetryableError() metrics.LogFatalError() metrics.LogPerformanceSummaryParams() metrics.CheckAndMaybePromptForAnalyticsEnabling('invalid argument') debug_messages = self.log_handler.messages['debug'] message_index = 0 for func_name in ('Shutdown', 'LogCommandParams', 'LogRetryableError', 'LogFatalError', 'LogPerformanceSummaryParams', 'CheckAndMaybePromptForAnalyticsEnabling'): self.assertIn( 'Exception captured in %s during metrics collection' % func_name, debug_messages[message_index]) message_index += 1 self.log_handler.reset()
def _make_fake_release_data(): """Make the required arguments needed for rez-release to work.""" options = mock.MagicMock() options.cmd = "release" options.debug = False options.message = "Fake release message" options.no_message = False options.process = "local" options.variants = None options.vcs = None parser = mock.MagicMock() parser.prog = "rez release" return options, parser
def test_systems(self, libvirt_mock): conn_mock = libvirt_mock.return_value domain = mock.MagicMock() domain.UUIDString.return_value = self.uuid conn_mock.listDefinedDomains.return_value = [domain] systems = self.test_driver.systems self.assertEqual([self.uuid], systems)
def setUp(self): super(ZmqBaseTestCase, self).setUp() self.messaging_conf.transport_driver = 'zmq' zmq_options.register_opts(self.conf, mock.MagicMock()) # Set config values self.internal_ipc_dir = self.useFixture(fixtures.TempDir()).path kwargs = { 'rpc_zmq_bind_address': '127.0.0.1', 'rpc_zmq_host': '127.0.0.1', 'rpc_zmq_ipc_dir': self.internal_ipc_dir, 'use_pub_sub': False, 'use_router_proxy': False, 'rpc_zmq_matchmaker': 'dummy' } self.config(group='oslo_messaging_zmq', **kwargs) self.config(rpc_response_timeout=5) # Get driver transport = oslo_messaging.get_transport(self.conf) self.driver = transport._driver self.listener = TestServerListener(self.driver) self.addCleanup( StopRpc(self, [('listener', 'stop'), ('driver', 'cleanup')]))
def _get_test_results(command_text, paths=None, arguments=None): """Get the conditions for a test (but don't actually run unittest. Args: command_text (str): Usually "shell", "yaml2py", "move_imports", etc. This string will find and modify Rez packages based on some registered plugin. paths (list[str], optional): The locations on-disk that will be used to any Rez-environment-related work. Some plugins need these paths for resolving a context, for example. Default is None. Returns: The output of :func:`rez_batch_process.core.worker.run`. """ if not arguments: arguments = mock.MagicMock() finder_ = registry.get_package_finder(command_text) valid_packages, invalid_packages, skips = finder_(paths=paths) command = registry.get_command(command_text) final_packages, unfixed, invalids = worker.run( functools.partial(command.run, arguments=arguments), valid_packages, keep_temporary_files=True, ) invalids.extend(invalid_packages) return (final_packages, unfixed, invalids, skips)
def test_load_as_matrix_success(): """ Test successfully passing ``load_as_matrix`` and invoking implementation defined ``_load_as_matrix`` method (no ``matrix`` property on data elem). """ m_elem = mock.MagicMock(spec_set=DataElement) crop_bb = AxisAlignedBoundingBox([0, 0], [1, 1]) m_reader = mock.MagicMock(spec=ImageReader) m_reader._get_matrix_property = \ mock.MagicMock(wraps=ImageReader._get_matrix_property) m_reader._load_as_matrix = mock.MagicMock() ImageReader.load_as_matrix(m_reader, m_elem, pixel_crop=crop_bb) m_reader._load_as_matrix.assert_called_once_with(m_elem, pixel_crop=crop_bb)
def test_run_cmd_exec_error(self, mock_run): mock_process = mock.MagicMock() mock_process.returncode = 1 mock_run.return_value = mock_process vsctl = VSCtl(protocol='tcp', ip_addr='127.0.0.1', port=6640) vsctl.run('show')
def _test_unhandled(paths=None): """Get the conditions for a test (but don't actually run unittest. Args: paths (list[str], optional): The locations on-disk that will be used to any Rez-environment-related work. Some plugins need these paths for resolving a context, for example. Default is None. Returns: The output of :func:`rez_batch_process.core.worker.run`. """ arguments = mock.MagicMock() arguments.command = "echo 'foo'" arguments.pull_request_name = "ticket-name" arguments.exit_on_error = True finder_ = registry.get_package_finder("shell") valid_packages, invalid_packages, skips = finder_(paths=paths) command = registry.get_command("shell") _, unfixed, invalids = worker.run( functools.partial(command.run, arguments=arguments), valid_packages) invalids.extend(invalid_packages) return (unfixed, invalids, skips)
def test_send_receive_topic(self, mock_msg): """Call() with method.""" mock_msg.return_value = msg = mock.MagicMock() msg.received = received = mock.MagicMock() received.failure = False received.reply = True msg.condition = condition = mock.MagicMock() condition.wait.return_value = True target = messaging.Target(topic='testtopic') self.driver.listen(target) result = self.driver.send( target, {}, {'method': 'hello-world', 'tx_id': 1}, wait_for_reply=True) self.assertEqual(result, True)
def test_publish(self): msg_mock = mock.MagicMock() msg_data = 'msg-data' msg_mock.to_dict.return_value = msg_data routing_key = 'routing-key' task_uuid = 'task-uuid' kwargs = dict(a='a', b='b') self.proxy(reset_master_mock=True).publish( msg_mock, routing_key, correlation_id=task_uuid, **kwargs) master_mock_calls = [ mock.call.Queue(name=self._queue_name(routing_key), exchange=self.exchange_inst_mock, routing_key=routing_key, durable=False, auto_delete=True), mock.call.producer.publish(body=msg_data, routing_key=routing_key, exchange=self.exchange_inst_mock, correlation_id=task_uuid, declare=[self.queue_inst_mock], type=msg_mock.TYPE, **kwargs) ] self.master_mock.assert_has_calls(master_mock_calls)
def test_load_as_matrix_crop_not_integer(): """ Test that a ValueError is raised when the pixel crop bbox provided does not report an integer type as its dtype. """ m_reader = mock.MagicMock(spec=ImageReader) m_data = mock.MagicMock(spec_set=DataElement) # Create bbox with floats. crop_bb = AxisAlignedBoundingBox([0.0, 0.0], [1.0, 1.0]) with pytest.raises(ValueError, match=r"Crop bounding box must be composed of integer " r"coordinates\. Given bounding box with dtype " r".+\."): ImageReader.load_as_matrix(m_reader, m_data, pixel_crop=crop_bb)
def test_get_all_drives(self): test_driver = StaticDriver(self.CONFIG, mock.MagicMock()) drives = test_driver.get_all_drives() self.assertEqual({('da69abcc-dae0-4913-9a7b-d344043097c0', '1', '32ADF365C6C1B7BD'), ('da69abcc-dae0-4913-9a7b-d344043097c0', '1', '58CFF987G8J2V9KL')}, set(drives))
def test_should_load_plugin(self): self.router.used_drivers = set(["zoo", "blah"]) ext = mock.MagicMock() ext.name = "foo" self.assertFalse(self.router._should_load_plugin(ext)) ext.name = "zoo" self.assertTrue(self.router._should_load_plugin(ext))
def test_generate_elements_all_preexisting_overwrite(self): """ Test that descriptors are computed even though the generated elements (mocked) report as having a vector. """ # Mock data element input data_iter = [ mock.Mock(spec=smqtk.representation.DataElement), mock.Mock(spec=smqtk.representation.DataElement), mock.Mock(spec=smqtk.representation.DataElement), ] for d in data_iter: d.content_type.return_value = 'image/png' # Mock element type m_de_type = mock.MagicMock(name="DescrElemType") # Mock factory fact = smqtk.representation.DescriptorElementFactory(m_de_type, {}) # Mock element instance m_de_inst = m_de_type.from_config() # from factory # !!! Mock that elements all *have* a vector set m_de_inst.has_vector.return_value = True # Default factor is the in-memory descriptor element. list( self.inst.generate_elements(data_iter, descr_factory=fact, overwrite=True)) # expect no has-vec checks because its after overwrite short-circuit. assert m_de_inst.has_vector.call_count == 0 assert m_de_inst.set_vector.call_count == 3 # Complete iteration should cause post-yield method to be called. self.inst._post_iterator_check.assert_called_once()
def setUp(self): super(TestProxy, self).setUp() self.topic = 'test-topic' self.broker_url = 'test-url' self.exchange_name = 'test-exchange' self.timeout = 5 self.de_period = proxy.DRAIN_EVENTS_PERIOD # patch classes self.conn_mock, self.conn_inst_mock = self.patchClass( proxy.kombu, 'Connection') self.exchange_mock, self.exchange_inst_mock = self.patchClass( proxy.kombu, 'Exchange') self.queue_mock, self.queue_inst_mock = self.patchClass( proxy.kombu, 'Queue') self.producer_mock, self.producer_inst_mock = self.patchClass( proxy.kombu, 'Producer') # connection mocking self.conn_inst_mock.drain_events.side_effect = [ socket.timeout, socket.timeout, KeyboardInterrupt ] # connections mocking self.connections_mock = self.patch( "taskflow.engines.worker_based.proxy.kombu.connections", attach_as='connections') self.connections_mock.__getitem__().acquire().__enter__.return_value =\ self.conn_inst_mock # producers mocking self.producers_mock = self.patch( "taskflow.engines.worker_based.proxy.kombu.producers", attach_as='producers') self.producers_mock.__getitem__().acquire().__enter__.return_value =\ self.producer_inst_mock # consumer mocking self.conn_inst_mock.Consumer.return_value.__enter__ = mock.MagicMock() self.conn_inst_mock.Consumer.return_value.__exit__ = mock.MagicMock() # other mocking self.on_wait_mock = mock.MagicMock(name='on_wait') self.main_mock.attach_mock(self.on_wait_mock, 'on_wait') # reset main mock self.resetMainMock()
def mock_notifier_exchange(name): def side_effect(target, ctxt, message, version, retry): target.exchange = name return transport._driver.send_notification(target, ctxt, message, version, retry=retry) transport._send_notification = mock.MagicMock( side_effect=side_effect)
def test_normal(self): """Release a package and make sure it is valid.""" def _make_fake_git_repository_at_directory(root): repository = git.Repo.init(root) repository.index.add(".") repository.index.commit("initial commit") source_path = tempfile.mkdtemp(suffix="_rez_package_source_path") self.delete_item_later(source_path) with open(os.path.join(source_path, "package.py"), "w") as handler: handler.write( textwrap.dedent("""\ name = "some_package" version = "1.0.0" build_command = "echo 'foo'" """)) _make_fake_git_repository_at_directory(source_path) package = packages_.get_developer_package(source_path) options = mock.MagicMock() options.cmd = "release" options.debug = False options.message = "Fake release message" options.no_message = False options.process = "local" options.variants = None options.vcs = None parser = mock.MagicMock() parser.prog = "rez release" release_path = tempfile.mkdtemp(suffix="_rez_package_release_path") self.delete_item_later(release_path) with wurlitzer.pipes(): creator.release(finder.get_package_root(package), options, parser, release_path) release_package = packages_.get_developer_package( os.path.join(release_path, "some_package", "1.0.0")) self.assertIsNotNone(release_package)
def mock_validate_connection(self, subdomain, status_code, session_mock): """ Validate cinnection using a session with a mocked status_code. Args: subdomain (str): The subdomain which is being tested status_code (int): the status code which is always returned by the mocked session session_mock : Provided by the mock.patch decorator """ mock_response = requests.Response() mock_response.status_code = status_code session_mock.return_value = mock.MagicMock(get=mock.MagicMock( return_value=mock_response)) session = requests.Session() config = configargparse.Namespace() config.subdomain = subdomain validate_connection(config, session)