def test_returns_false_if_missing_required_command_line_options(self): c = burton.Config( command_line_defaults = { "foo" : None, }, command_line_mapping = { "--foo" : [ "foo", "str" ], }, required_command_line_options = [ "foo" ], ) captured_log = testfixtures.LogCapture() self.assertFalse(c.parse_command_line_options("script_name", [ ])) captured_log.check( (burton.logger_name, "ERROR", "Missing required option foo"), ) captured_log.uninstall() captured_log = testfixtures.LogCapture() self.assertFalse(c.parse_command_line_options( "script_name", [ "some_path", ] )) captured_log.check( (burton.logger_name, "ERROR", "Missing required option foo"), ) captured_log.uninstall()
def test_invalid_version(self): data = {"version": "2", "workflow": {}} with testfixtures.LogCapture() as capture: with self.assertRaises(InvalidVersionException): self.wfreader._extract_version(data) capture.check( ( "force_bdss.io.workflow_reader", "ERROR", "Invalid input file format: " " version 2 is not in the " "list of supported versions ['1', '1.1']", ) ) data = {"workflow": {}} with testfixtures.LogCapture() as capture: with self.assertRaises(InvalidFileException): self.wfreader._extract_version(data) capture.check( ( "force_bdss.io.workflow_reader", "ERROR", "Invalid input file format: " "no version specified", ) ) data = {} with testfixtures.LogCapture(): with self.assertRaises(InvalidFileException): self.wfreader._extract_version(data)
def test_metrics_capture_for_batch_uploads(self): app = self._make_test_app() collection = "/1.5/42/storage/xxx_col1" with testfixtures.LogCapture() as logs: bso = {"id": "1", "payload": "x"} res = app.post_json(collection + "?batch=true", [bso]) batch = res.json["batch"] for r in logs.records: if "syncstorage.storage.sql.append_items_to_batch" in r.__dict__: break else: assert False, "timer metrics were not emitted" with testfixtures.LogCapture() as logs: endpoint = collection + "?batch={0}&commit=true".format(batch) app.post_json(endpoint, []) # DB timing metrics should have been generated in a log message. for r in logs.records: if "syncstorage.storage.sql.apply_batch" in r.__dict__: break else: assert False, "timer metrics were not emitted"
def test_file_opening_exception_handling(self): with testfixtures.LogCapture() as capture: with self.assertRaises(IOError): self.reader.read( 'this_file_should_not_exist.itp' ) capture.check( ('force_gromacs.io.gromacs_molecule_reader', 'ERROR', 'unable to open "this_file_should_not_exist.itp"') ) mock_open = mock.mock_open(read_data=" ") with mock.patch( FILE_READER_OPEN_PATH, mock_open, create=True),\ testfixtures.LogCapture() as capture: with self.assertRaises(IOError): self.reader.read( 'this_file_is_empty.itp' ) capture.check( ('force_gromacs.io.gromacs_molecule_reader', 'ERROR', 'unable to load data from "this_file_is_empty.itp"') )
def test__initialize_listeners(self): # Test normal operation self.operation._initialize_listeners() self.assertEqual(1, len(self.operation.listeners)) self.assertTrue(self.operation.listeners[0].initialize_called) # Test for error on creation of listener, we expect # an Exception and log message factory = self.registry.notification_listener_factories[0] factory.raises_on_create_listener = True with testfixtures.LogCapture() as capture: with self.assertRaises(Exception): self.operation._initialize_listeners() capture.check(( "force_bdss.app.base_operation", "ERROR", "Failed to create listener with id " "'force.bdss.enthought.plugin.test.v0" ".factory.probe_notification_listener' in plugin " "'force.bdss.enthought.plugin.test.v0'. " "This may indicate a programming error in the " "plugin.", )) # Test for error on initialization of listener, we # only expect a log message factory.raises_on_create_listener = False factory.raises_on_initialize_listener = True with testfixtures.LogCapture() as capture: self.operation._initialize_listeners() capture.check(( "force_bdss.app.base_operation", "ERROR", "Failed to initialize listener with id " "'force.bdss.enthought.plugin.test.v0" ".factory.probe_notification_listener' in plugin " "'force.bdss.enthought.plugin.test.v0'. " "The listener will be dropped.", )) # Test setting of stop and pause threading events on # a UIEventNotificationMixin listener factory.raises_on_initialize_listener = False with mock.patch('force_bdss.app.optimize_operation.OptimizeOperation' '._set_threading_events') as mock_set_thread: self.operation._initialize_listeners() self.assertEqual(0, mock_set_thread.call_count) factory.listener_class = ProbeUIEventNotificationListener self.operation._initialize_listeners() self.assertEqual(0, mock_set_thread.call_count)
def test_run_error_raise_sys_exit(self): # Sys exit on non-existent file with testfixtures.LogCapture(): app = BDSSApplication(False, fixtures.get("test_nonexistent.json")) with self.assertRaises(SystemExit): app.run() # Sys exit on empty workflow file with testfixtures.LogCapture(): app = BDSSApplication(False, fixtures.get("test_empty.json")) with self.assertRaises(SystemExit): app.run()
def test_shell_cmd_not_found(): with pytest.raises(SystemExit) as excinfo: with testfixtures.LogCapture() as log_capture: cli.main(args=['create']) log_capture.check( ('gpucrate', 'ERROR', 'required command not found: doesnotexist'), ) assert excinfo.value.code == 2
def test_update_translation_file_ignores_whitespace_entries( self, read_func, write_func ): def _open_translation_file(conf, language, vcs_class): translation_file = burton.translation.Base( "English", "en", "en-us", "Test Company", "Test Product", "*****@*****.**" ) translation_file.add_translation( "String1", "Translation for String1" ) return translation_file, "test filename" read_func.side_effect = _open_translation_file captured_log = testfixtures.LogCapture() burton.update_translation_file( mock.Mock(), [ " ", "String1" ], [ " ", "String1" ], "English", burton.vcs.NoOp() ) captured_log.check() captured_log.uninstall()
def test_metrics_capture(self): app = TestApp(self.config.make_wsgi_app()) # Monkey-patch the app to make legitimate hawk-signed requests. user_id = 42 auth_policy = self.config.registry.getUtility(IAuthenticationPolicy) req = Request.blank("http://localhost/") auth_token, auth_secret = auth_policy.encode_hawk_id(req, user_id) def new_do_request(req, *args, **kwds): hawkauthlib.sign_request(req, auth_token, auth_secret) return orig_do_request(req, *args, **kwds) orig_do_request = app.do_request app.do_request = new_do_request # Make a request that hits the database, capturing its logs. with testfixtures.LogCapture() as logs: app.get("/1.5/42/info/collections") # DB usage metrics should have been generated in a log message. for r in logs.records: if "syncstorage.storage.sql.db.execute" in r.__dict__: break else: assert False, "metrics were not collected"
def test_logging_of_invalid_bsos(self): app = self._make_test_app() # Make a request with an invalid bso, capturing its logs with testfixtures.LogCapture() as logs: app.post_json("/1.5/42/storage/bookmarks", [ { "id": "valid1", "payload": "thisisok" }, { "id": "invalid", "payload": "TOOBIG" * 1024 * 128 }, { "id": "valid2", "payload": "thisisoktoo" }, ]) # An error log should have been generated for r in logs.records: if r.name == "syncstorage.views.validators": expect_message = "Invalid BSO 42/bookmarks/invalid" \ " (payload too large):" \ " BSO({\"id\": \"invalid\", \"payload_size\": 786432})" self.assertEqual(r.getMessage(), expect_message) break else: assert False, "log was not generated"
def test_bad_slots(self): self.mock_factory.create_data_source = mock.MagicMock( return_value=BadDataSource(self.mock_factory)) model = DummyDataSourceModel(self.mock_factory) with testfixtures.LogCapture(): with self.assertRaisesRegex(Exception, "bad slots"): model.verify()
def test_toolkit_null(self): ETSConfig.toolkit = 'null' with testfixtures.LogCapture(): with warnings.catch_warnings(): warnings.simplefilter("ignore") BDSSApplication(False, "foo/bar") self.assertEqual(ETSConfig.toolkit, 'null')
def test_error_for_non_data_source(self): data_values = [DataValue(name="foo")] self.layer.data_sources[0].input_slot_info = [ InputSlotInfo(name="foo") ] self.layer.data_sources[0].output_slot_info = [ OutputSlotInfo(name="one") ] def probe_run(self, *args, **kwargs): return ["hello"] factory = self.registry.data_source_factories[0] factory.run_function = probe_run with testfixtures.LogCapture(): with self.assertRaisesRegex( RuntimeError, "The result list returned by DataSource " "test_data_source" " contains an entry that is not a DataValue." " An entry of type <.* 'str'> was instead found" " in position 0." r" Fix the DataSource.run\(\) method to" " return the appropriate entity.", ): self.layer.execute_layer(data_values)
def test_valid_run_can_dump_info(self): for valid in self.valid_props: r = SpecJBBRun(**valid) with testfixtures.LogCapture() as l: r.dump() # need to have some actual logging output self.assertTrue(l.actual())
def test_create_new_config_file(self): lines = [] def _write(line): lines.append(line) write_fp = mock.Mock() write_fp.write = mock.Mock(side_effect = _write) c = burton.Config() c._open_for_writing = mock.Mock(return_value = write_fp) captured_log = testfixtures.LogCapture() c.create_new_config_file("some_path") captured_log.check( ( burton.logger_name, "ERROR", "An empty config file has been created at some_path" ), ( burton.logger_name, "ERROR", "Please fill out the config file and run your command again" ), ) captured_log.uninstall() self.assertEquals( "".join(lines), c._get_default_config_file().read() )
def test__finalize_listeners(self): # Test normal operation self.operation._initialize_listeners() listener = self.operation.listeners[0] self.operation._finalize_listeners() self.assertEqual(0, len(self.operation.listeners)) self.assertTrue(listener.finalize_called) # Now initialise a set of listeners that will raise an # exception when finalized to test error handling factory = self.registry.notification_listener_factories[0] factory.raises_on_finalize_listener = True self.operation._initialize_listeners() listener = self.operation.listeners[0] with testfixtures.LogCapture() as capture: self.operation._finalize_listeners() capture.check(( "force_bdss.app.base_operation", "ERROR", "Exception while finalizing listener " "'force.bdss.enthought.plugin.test.v0" ".factory.probe_notification_listener' in plugin " "'force.bdss.enthought.plugin.test.v0'.", )) self.assertEqual(0, len(self.operation.listeners)) self.assertTrue(listener.finalize_called)
def test_normal_salary(self): with testfixtures.LogCapture() as logs: pay_developer(180) first_record = logs.records[0] self.assertEqual(first_record.msg, f"We get 180 hours of work") self.assertEqual(first_record.levelname, "DEBUG")
def test_non_valid_file(self): # Provide a workflow that is invalid self.operation.workflow_file = ProbeWorkflowFile( path=fixtures.get("test_null.json")) self.operation.workflow_file.read() with testfixtures.LogCapture() as capture: with self.assertRaises(RuntimeError): self.operation.run() capture.check( ("force_bdss.app.base_operation", "ERROR", "Unable to execute workflow due to verification errors:"), ("force_bdss.app.base_operation", "ERROR", "The MCO has no defined parameters"), ("force_bdss.app.base_operation", "ERROR", "The MCO has no defined KPIs"), ("force_bdss.app.base_operation", "ERROR", "The number of input slots (1 values) returned by " "'test_data_source' does " 'not match the number of user-defined names specified ' '(0 values). This is ' 'either a plugin error or a file error.'), ('force_bdss.app.base_operation', 'ERROR', "The number of output slots (1 values) returned by " "'test_data_source' does " 'not match the number of user-defined names specified ' '(0 values). This is ' 'either a plugin error or a file error.'))
def test_data_source_run_error(self): data_values = [DataValue(name="foo")] self.layer.data_sources[0].input_slot_info = [ InputSlotInfo(name="foo") ] factory = self.registry.data_source_factories[0] factory.raises_on_data_source_run = True with testfixtures.LogCapture() as capture: with self.assertRaises(Exception): self.layer.execute_layer(data_values) capture.check( ( "force_bdss.core.execution_layer", "INFO", "Evaluating for Data Source test_data_source", ), ("force_bdss.core.execution_layer", "INFO", "Passed values:"), ( "force_bdss.core.execution_layer", "INFO", "0: foo = None (AVERAGE)", ), ( "force_bdss.core.execution_layer", "ERROR", "Evaluation could not be performed. " "Run method raised exception.", ), )
def test_workflow(self): with testfixtures.LogCapture(): with warnings.catch_warnings(): warnings.simplefilter("ignore") app = BDSSApplication(False, fixtures.get("test_empty.json")) app._load_workflow() self.assertIsInstance(app.workflow_file.workflow, Workflow) with testfixtures.LogCapture(): with warnings.catch_warnings(): warnings.simplefilter("ignore") app = BDSSApplication(True, fixtures.get("test_empty.json")) app._load_workflow() self.assertIsInstance(app.workflow_file.workflow, Workflow)
def test_deliver_listeners(self): # Test normal operation self.operation._initialize_listeners() listener = self.operation.listeners[0] # Deliver a start event self.operation._deliver_start_event() self.assertTrue(listener.deliver_called) self.assertIsInstance(listener.deliver_call_args[0][0], MCOStartEvent) # Deliver a finish event self.operation._deliver_finish_event() self.assertIsInstance(listener.deliver_call_args[0][0], MCOFinishEvent) # Now initialise a set of listeners that will raise an # exception when delivered to test error handling factory = self.registry.notification_listener_factories[0] factory.raises_on_deliver_listener = True self.operation._initialize_listeners() listener = self.operation.listeners[0] with testfixtures.LogCapture() as capture: self.operation._deliver_start_event() self.assertTrue(listener.deliver_called) capture.check(( "force_bdss.app.base_operation", "ERROR", "Exception while delivering to listener " "'force.bdss.enthought.plugin.test.v0" ".factory.probe_notification_listener' in plugin " "'force.bdss.enthought.plugin.test.v0'. " "The listener will be dropped and computation " "will continue.", ))
def test_initialization(self): with testfixtures.LogCapture(): with warnings.catch_warnings(): warnings.simplefilter("ignore") app = BDSSApplication(False, "foo/bar") self.assertIsInstance(app.operation, OptimizeOperation) self.assertEqual(app.workflow_file.path, "foo/bar") self.assertEqual(ETSConfig.toolkit, 'null')
def test_get_last_archived_date_bad_date(self, patched_get): with testfixtures.LogCapture() as log: status = scraper.SyncStatus(None, None) with self.assertRaises(scraper.NonRecoverableScraperException): patched_get.return_value = dict( maxrawfilemtimearchived='monkey') status.get_last_archived_mtime() self.assertIn('ERROR', [x.levelname for x in log.records])
def test_create_no_volume_root(): with pytest.raises(SystemExit): with testfixtures.LogCapture() as log_capture: with testfixtures.TempDirectory() as d: d.write('config', 'volume_root:') config.load(path=os.path.join(d.path, 'config')) cli.main(args=['create']) log_capture.check(('gpucrate', 'ERROR', exception.NoVolumeRoot.message), )
def test_initialization_errors_invalid_data_source_class(self): class Broken(DummyDataSourceFactory): def get_data_source_class(self): return None with testfixtures.LogCapture(): with self.assertRaises(TraitError): Broken(self.plugin)
def test_initialization_errors_invalid_idetifier(self): class Broken(DummyDataSourceFactory): def get_identifier(self): return None with testfixtures.LogCapture(): with self.assertRaises(ValueError): Broken(self.plugin)
def test_download_files_fails_and_dies(self): with testfixtures.LogCapture() as log: with self.assertRaises(scraper.RecoverableScraperException): scraper.download_files( '/usr/bin/timeout', '/bin/false', 'localhost/', [ scraper.RemoteFile('2016/10/26/DNE1', 0), scraper.RemoteFile('2016/10/26/DNE2', 0) ], '/tmp') self.assertIn('ERROR', [x.levelname for x in log.records])
def test_without_ipython(): with testfixtures.LogCapture() as log: utils.shell() log.check( ('gpucrate', 'ERROR', 'Unable to load IPython:\n\nNo module named IPython\n'), ('gpucrate', 'ERROR', 'Please check that IPython is installed and working.'), )
def test_get_rating_logging(self): """Does get_rating log an error when there's no rating? """ with testfixtures.LogCapture('stars') as log: self.creditset.get_rating(None) self.assertEqual(len(log.records), 1) self.assertEqual(log.records[0].levelname, 'WARNING') self.assertTrue('No valid rating' in log.records[0].msg)
def test_basic_log(args): with testfixtures.LogCapture() as logs: example_func(args) logs.check(( LOGGER_NAME, 'DEBUG', "Calling example_func(%r)" % (args, ), ))