def test_syslog_shortcut_simple(self): """Make sure that ``coloredlogs.install(syslog=True)`` works.""" system_log_file = self.find_system_log() expected_message = random_string(50) with cleanup_handlers(): # See test_system_logging() for the importance of this log level. coloredlogs.install(syslog=True) logging.error("%s", expected_message) # See the comments in test_system_logging() on why this is retried. retry(lambda: check_contents(system_log_file, expected_message, True))
def test_syslog_shortcut_simple(self): """Make sure that ``coloredlogs.install(syslog=True)`` works.""" system_log_file = self.find_system_log() expected_message = random_string(50) with cleanup_handlers(): # See test_system_logging() for the importance of this log level. coloredlogs.install(syslog=True) logging.error("%s", expected_message) # See the comments in test_system_logging() on why this is retried. retry(lambda: check_contents(system_log_file, expected_message, True))
def test_partial_read(self): """Test that partial reading works as expected.""" # This test method uses retry logic because `partial=True' makes these # tests prone to race conditions (this is the whole reason why # `partial=False' by default :-). initial_part = random_string() later_part = random_string() with CaptureOutput() as capturer: sys.stderr.write("%s\n" % initial_part) retry(lambda: initial_part in capturer.get_lines(partial=True)) sys.stderr.write("%s\n" % later_part) retry(lambda: later_part in capturer.get_lines(partial=True))
def test_syslog_shortcut_enhanced(self): """Make sure that ``coloredlogs.install(syslog='warning')`` works.""" system_log_file = self.find_system_log() the_expected_message = random_string(50) not_an_expected_message = random_string(50) with cleanup_handlers(): # See test_system_logging() for the importance of these log levels. coloredlogs.install(syslog='error') logging.warning("%s", not_an_expected_message) logging.error("%s", the_expected_message) # See the comments in test_system_logging() on why this is retried. retry(lambda: check_contents(system_log_file, the_expected_message, True)) retry(lambda: check_contents(system_log_file, not_an_expected_message, False))
def test_syslog_shortcut_enhanced(self): """Make sure that ``coloredlogs.install(syslog='warning')`` works.""" system_log_file = self.find_system_log() the_expected_message = random_string(50) not_an_expected_message = random_string(50) with cleanup_handlers(): # See test_system_logging() for the importance of these log levels. coloredlogs.install(syslog='error') logging.warning("%s", not_an_expected_message) logging.error("%s", the_expected_message) # See the comments in test_system_logging() on why this is retried. retry(lambda: check_contents(system_log_file, the_expected_message, True)) retry(lambda: check_contents(system_log_file, not_an_expected_message, False))
def test_system_logging(self): """Make sure the :class:`coloredlogs.syslog.SystemLogging` context manager works.""" system_log_file = self.find_system_log() expected_message = random_string(50) with SystemLogging(programname='coloredlogs-test-suite') as syslog: if not syslog: return self.skipTest("couldn't connect to syslog daemon") # When I tried out the system logging support on macOS 10.13.1 on # 2018-01-05 I found that while WARNING and ERROR messages show up # in the system log DEBUG and INFO messages don't. This explains # the importance of the level of the log message below. logging.error("%s", expected_message) # Retry the following assertion (for up to 60 seconds) to give the # logging daemon time to write our log message to disk. This # appears to be needed on MacOS workers on Travis CI, see: # https://travis-ci.org/xolox/python-coloredlogs/jobs/325245853 retry(lambda: check_contents(system_log_file, expected_message, True))
def test_system_logging(self): """Make sure the :class:`coloredlogs.syslog.SystemLogging` context manager works.""" system_log_file = self.find_system_log() expected_message = random_string(50) with SystemLogging(programname='coloredlogs-test-suite') as syslog: if not syslog: return self.skipTest("couldn't connect to syslog daemon") # When I tried out the system logging support on macOS 10.13.1 on # 2018-01-05 I found that while WARNING and ERROR messages show up # in the system log DEBUG and INFO messages don't. This explains # the importance of the level of the log message below. logging.error("%s", expected_message) # Retry the following assertion (for up to 60 seconds) to give the # logging daemon time to write our log message to disk. This # appears to be needed on MacOS workers on Travis CI, see: # https://travis-ci.org/xolox/python-coloredlogs/jobs/325245853 retry(lambda: check_contents(system_log_file, expected_message, True))
def test_retry_return(self): """Test :func:`~humanfriendly.testing.retry()` based on return values.""" # Define a helper function that will return False on the first call and # return a number on the second call. def success_helper(): if not hasattr(success_helper, 'was_called'): # On the first call we return False. setattr(success_helper, 'was_called', True) return False else: # On the second call we return a number. return 42 assert retry(success_helper) == 42 self.assertRaises(CallableTimedOut, retry, lambda: False, timeout=1)
def test_retry_raise(self): """Test :func:`~humanfriendly.testing.retry()` based on assertion errors.""" # Define a helper function that will raise an assertion error on the # first call and return a string on the second call. def success_helper(): if not hasattr(success_helper, 'was_called'): setattr(success_helper, 'was_called', True) assert False else: return 'yes' assert retry(success_helper) == 'yes' # Define a helper function that always raises an assertion error. def failure_helper(): assert False self.assertRaises(AssertionError, retry, failure_helper, timeout=1)