class ConsoleLogReader(object): def __init__(self, instance_paths): self.__stderr_io = FileIO(instance_paths.get_stderr_path()) self.__message_reader = MessageReader(self.__stderr_io.read, SingleLineParser()) def wait_for_start_message(self): syslog_ng_start_message = ["syslog-ng starting up;"] return self.__wait_for_messages_in_console_log(syslog_ng_start_message) def wait_for_stop_message(self): syslog_ng_stop_message = ["syslog-ng shutting down"] return self.__wait_for_messages_in_console_log(syslog_ng_stop_message) def wait_for_reload_message(self): syslog_ng_reload_messages = [ "New configuration initialized", "Configuration reload request received, reloading configuration", "Configuration reload finished", ] return self.__wait_for_messages_in_console_log(syslog_ng_reload_messages) def __wait_for_messages_in_console_log(self, expected_messages): if not self.__stderr_io.wait_for_creation(): raise Exception console_log_messages = self.__message_reader.pop_messages(counter=READ_ALL_MESSAGES) console_log_content = "".join(console_log_messages) result = [] for expected_message in expected_messages: result.append(expected_message in console_log_content) return all(result) def check_for_unexpected_messages(self, unexpected_messages): unexpected_patterns = ["Plugin module not found"] console_log_messages = self.__message_reader.peek_messages(counter=READ_ALL_MESSAGES) if unexpected_messages is not None: unexpected_patterns.append(unexpected_messages) for unexpected_pattern in unexpected_patterns: for console_log_message in console_log_messages: if unexpected_pattern in console_log_message: logger.error("Found unexpected message in console log: {}".format(console_log_message)) assert False def dump_stderr(self, last_n_lines=10): console_log_messages = self.__message_reader.peek_messages(counter=READ_ALL_MESSAGES) logger.error("".join(console_log_messages[-last_n_lines:])) @staticmethod def handle_valgrind_log(valgrind_log_path): with open(valgrind_log_path, "r") as valgrind_log: valgrind_content = valgrind_log.read() assert "Invalid read" not in valgrind_content assert "Invalid write" not in valgrind_content assert "blocks are definitely lost in loss record" not in valgrind_content assert "Uninitialised value was created by a heap allocation" not in valgrind_content
def test_file_io_rewrite_read(temp_file): fileio = FileIO(temp_file) content1 = "message 1\n" fileio.write(content1) assert fileio.read() == content1 content2 = "message 2\n" fileio.rewrite(content2) assert fileio.read(position=0) == content2
def __init__(self, instance_paths): self.__stderr_io = FileIO(instance_paths.get_stderr_path()) self.__message_reader = MessageReader(self.__stderr_io.read, SingleLineParser())
def __init__(self, logger_factory, instance_paths): self.__logger = logger_factory.create_logger("ConsoleLogReader") self.__stderr_io = FileIO(logger_factory, instance_paths.get_stderr_path()) self.__message_reader = MessageReader(logger_factory, self.__stderr_io.read, SingleLineParser(logger_factory))
class ConsoleLogReader(object): def __init__(self, logger_factory, instance_paths): self.__logger = logger_factory.create_logger("ConsoleLogReader") self.__stderr_io = FileIO(logger_factory, instance_paths.get_stderr_path()) self.__message_reader = MessageReader(logger_factory, self.__stderr_io.read, SingleLineParser(logger_factory)) def wait_for_start_message(self): syslog_ng_start_message = ["syslog-ng starting up;"] return self.__wait_for_messages_in_console_log(syslog_ng_start_message) def wait_for_stop_message(self): syslog_ng_stop_message = ["syslog-ng shutting down"] return self.__wait_for_messages_in_console_log(syslog_ng_stop_message) def wait_for_reload_message(self): syslog_ng_reload_messages = [ "New configuration initialized", "Configuration reload request received, reloading configuration", "Configuration reload finished", ] return self.__wait_for_messages_in_console_log( syslog_ng_reload_messages) def __wait_for_messages_in_console_log(self, expected_messages): if not self.__stderr_io.wait_for_creation(): raise Exception console_log_messages = self.__message_reader.pop_messages( counter=READ_ALL_MESSAGES) console_log_content = "".join(console_log_messages) result = [] for expected_message in expected_messages: result.append(expected_message in console_log_content) return all(result) def check_for_unexpected_messages(self, unexpected_messages): unexpected_patterns = ["Plugin module not found"] console_log_messages = self.__message_reader.peek_messages( counter=READ_ALL_MESSAGES) if unexpected_messages is not None: unexpected_patterns.append(unexpected_messages) for unexpected_pattern in unexpected_patterns: for console_log_message in console_log_messages: if unexpected_pattern in console_log_message: self.__logger.error( "Found unexpected message in console log: {}".format( console_log_message)) assert False def dump_stderr(self, last_n_lines=10): console_log_messages = self.__message_reader.peek_messages( counter=READ_ALL_MESSAGES) self.__logger.info("".join(console_log_messages[-last_n_lines:])) @staticmethod def handle_valgrind_log(valgrind_log_path): with open(valgrind_log_path, "r") as valgrind_log: valgrind_content = valgrind_log.read() assert "Invalid read" not in valgrind_content assert "Invalid write" not in valgrind_content
def test_file_io_multiple_write_read(temp_file, test_message): fileio = FileIO(temp_file) fileio.write(test_message) fileio.write(test_message) output = fileio.read() assert test_message + test_message == output