Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
def test_buffer_and_parse(test_message, temp_file, input_message_counter, requested_message_counter, expected_result):
    input_content = test_message * input_message_counter
    __writeable_file, readable_file = prepare_input_file(input_content, temp_file)
    single_line_parser = SingleLineParser()
    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader._MessageReader__buffer_and_parse(requested_message_counter) is expected_result
    assert single_line_parser.msg_list == input_content.splitlines(True)
Ejemplo n.º 3
0
def test_peek_messages(temp_file):
    test_message = "test message 2\ntest message 3\n"
    __writeable_file, readable_file = prepare_input_file(test_message, temp_file)
    single_line_parser = SingleLineParser()
    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader.peek_messages(counter=2) == test_message.splitlines(True)
    assert message_reader._MessageReader__parser.msg_list == test_message.splitlines(True)
Ejemplo n.º 4
0
def test_peek_messages(tc_unittest):
    test_message = "test message 2\ntest message 3\n"
    __writeable_file, readable_file = tc_unittest.prepare_input_file(test_message)
    single_line_parser = SingleLineParser(tc_unittest.get_fake_logger_factory())
    message_reader = MessageReader(tc_unittest.get_fake_logger_factory(), readable_file.read, single_line_parser)

    assert message_reader.peek_messages(counter=2) == test_message.splitlines(True)
    assert message_reader._MessageReader__parser.msg_list == test_message.splitlines(True)
Ejemplo n.º 5
0
def test_buffer_and_parse(tc_unittest, input_message_counter, requested_message_counter, expected_result):
    input_content = tc_unittest.get_utf8_test_messages(input_message_counter)
    __writeable_file, readable_file = tc_unittest.prepare_input_file(input_content)
    single_line_parser = SingleLineParser(tc_unittest.get_fake_logger_factory())
    message_reader = MessageReader(tc_unittest.get_fake_logger_factory(), readable_file.read, single_line_parser)

    assert message_reader._MessageReader__buffer_and_parse(requested_message_counter) is expected_result
    assert single_line_parser.msg_list == input_content.splitlines(True)
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
    def wait_file_content(self, content):
        with self.path.open() as f:
            message_reader = MessageReader(f.readline, SingleLineParser())

            while True:
                msg = message_reader.pop_messages(1)[0]
                if content in msg:
                    return True

            return False
Ejemplo n.º 8
0
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=0)
        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=0)
        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=0)
        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
Ejemplo n.º 9
0
def test_multiple_buffer_and_parse(tc_unittest):
    input_content = tc_unittest.get_utf8_test_messages(2)
    writeable_file, readable_file = tc_unittest.prepare_input_file(input_content)
    single_line_parser = SingleLineParser(tc_unittest.get_fake_logger_factory())
    message_reader = MessageReader(tc_unittest.get_fake_logger_factory(), readable_file.read, single_line_parser)

    assert message_reader._MessageReader__buffer_and_parse(0) is False
    writeable_file.write(input_content)
    writeable_file.flush()
    assert message_reader._MessageReader__buffer_and_parse(0) is False
    assert single_line_parser.msg_list == input_content.splitlines(True) * 2
Ejemplo n.º 10
0
def test_peek_messages(temp_file):
    test_message = "test message 2\ntest message 3\n"
    __writeable_file, readable_file = prepare_input_file(
        test_message, temp_file)
    single_line_parser = SingleLineParser()
    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader.peek_messages(
        counter=2) == test_message.splitlines(True)
    assert message_reader._MessageReader__parser.msg_list == test_message.splitlines(
        True)
Ejemplo n.º 11
0
def test_multiple_buffer_and_parse(test_message, temp_file):
    input_content = test_message * 2
    writeable_file, readable_file = prepare_input_file(input_content, temp_file)
    single_line_parser = SingleLineParser()
    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader._MessageReader__buffer_and_parse(0) is False
    writeable_file.write(input_content)
    writeable_file.flush()
    assert message_reader._MessageReader__buffer_and_parse(0) is False
    assert single_line_parser.msg_list == input_content.splitlines(True) * 2
Ejemplo n.º 12
0
def test_buffer_and_parse(test_message, temp_file, input_message_counter,
                          requested_message_counter, expected_result):
    input_content = test_message * input_message_counter
    __writeable_file, readable_file = prepare_input_file(
        input_content, temp_file)
    single_line_parser = SingleLineParser()
    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader._MessageReader__buffer_and_parse(
        requested_message_counter) is expected_result
    assert single_line_parser.msg_list == input_content.splitlines(True)
Ejemplo n.º 13
0
 def dd_read_logs(self, path, counter):
     if path not in self.__native_driver_io_collection.keys():
         native_driver_io = self.__native_driver_io_ref(self.__logger_factory, path)
         native_driver_io.wait_for_creation()
         message_reader = MessageReader(
             self.__logger_factory, native_driver_io.read, SingleLineParser(self.__logger_factory)
         )
         self.__native_driver_io_collection.update({path: message_reader})
     message_reader = self.__native_driver_io_collection[path]
     messages = message_reader.pop_messages(counter)
     self.__logger.print_io_content(path, messages, "Content has been read from")
     return messages
Ejemplo n.º 14
0
def test_pop_messages(temp_file, input_message, requested_message_counter, popped_message, remaining_message):
    __writeable_file, readable_file = prepare_input_file(input_message, temp_file)
    single_line_parser = SingleLineParser()

    message_reader = MessageReader(readable_file.read, single_line_parser)

    if requested_message_counter > input_message.count("\n"):
        with pytest.raises(AssertionError):
            message_reader.pop_messages(requested_message_counter, timeout=0.1)
    else:
        assert message_reader.pop_messages(requested_message_counter) == popped_message
        assert message_reader._MessageReader__parser.msg_list == remaining_message
Ejemplo n.º 15
0
def test_multiple_buffer_and_parse(test_message, temp_file):
    input_content = test_message * 2
    writeable_file, readable_file = prepare_input_file(input_content,
                                                       temp_file)
    single_line_parser = SingleLineParser()
    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader._MessageReader__buffer_and_parse(0) is False
    writeable_file.write(input_content)
    writeable_file.flush()
    assert message_reader._MessageReader__buffer_and_parse(0) is False
    assert single_line_parser.msg_list == input_content.splitlines(True) * 2
Ejemplo n.º 16
0
def test_pop_messages(tc_unittest, input_message, requested_message_counter, popped_message, remaining_message):
    __writeable_file, readable_file = tc_unittest.prepare_input_file(input_message)
    single_line_parser = SingleLineParser(tc_unittest.get_fake_logger_factory())

    message_reader = MessageReader(tc_unittest.get_fake_logger_factory(), readable_file.read, single_line_parser)

    if requested_message_counter > input_message.count("\n"):
        with pytest.raises(AssertionError):
            message_reader.pop_messages(requested_message_counter, timeout=0.1)
    else:
        assert message_reader.pop_messages(requested_message_counter) == popped_message
        assert message_reader._MessageReader__parser.msg_list == remaining_message
Ejemplo n.º 17
0
def test_read_all_messages():
    def read_generator():
        yield ""  # eof
        yield "second\n"
        yield "third\n"
        yield ""  # eof

    single_line_parser = SingleLineParser()
    generator = read_generator()
    message_reader = MessageReader(lambda: next(generator), single_line_parser)

    assert len(message_reader.pop_messages(counter=READ_ALL_MESSAGES)) == 0
    assert len(message_reader.pop_messages(counter=READ_ALL_MESSAGES)) == 2
Ejemplo n.º 18
0
def test_read_all_messages():
    def read_generator():
        yield ""  # eof
        yield "second\n"
        yield "third\n"
        yield ""  # eof

    single_line_parser = SingleLineParser()
    generator = read_generator()
    message_reader = MessageReader(lambda: next(generator), single_line_parser)

    assert len(message_reader.pop_messages(counter=READ_ALL_MESSAGES)) == 0
    assert len(message_reader.pop_messages(counter=READ_ALL_MESSAGES)) == 2
Ejemplo n.º 19
0
def test_popping_in_sequence(tc_unittest):
    input_content = tc_unittest.get_utf8_test_messages(counter=10)
    __writeable_file, readable_file = tc_unittest.prepare_input_file(input_content)
    single_line_parser = SingleLineParser(tc_unittest.get_fake_logger_factory())

    message_reader = MessageReader(tc_unittest.get_fake_logger_factory(), readable_file.read, single_line_parser)

    input_content_list = input_content.splitlines(True)
    assert message_reader.pop_messages(counter=2) == input_content_list[0:2]
    assert message_reader.pop_messages(counter=2) == input_content_list[2:4]
    assert message_reader.pop_messages(counter=2) == input_content_list[4:6]
    assert message_reader.pop_messages(counter=2) == input_content_list[6:8]
    assert message_reader.pop_messages(counter=2) == input_content_list[8:10]
Ejemplo n.º 20
0
def test_popping_in_sequence(test_message, temp_file):
    input_content = test_message * 10
    __writeable_file, readable_file = prepare_input_file(input_content, temp_file)
    single_line_parser = SingleLineParser()

    message_reader = MessageReader(readable_file.read, single_line_parser)

    input_content_list = input_content.splitlines(True)
    assert message_reader.pop_messages(counter=2) == input_content_list[0:2]
    assert message_reader.pop_messages(counter=2) == input_content_list[2:4]
    assert message_reader.pop_messages(counter=2) == input_content_list[4:6]
    assert message_reader.pop_messages(counter=2) == input_content_list[6:8]
    assert message_reader.pop_messages(counter=2) == input_content_list[8:10]
Ejemplo n.º 21
0
def test_popping_in_sequence(test_message, temp_file):
    input_content = test_message * 10
    __writeable_file, readable_file = prepare_input_file(
        input_content, temp_file)
    single_line_parser = SingleLineParser()

    message_reader = MessageReader(readable_file.read, single_line_parser)

    input_content_list = input_content.splitlines(True)
    assert message_reader.pop_messages(counter=2) == input_content_list[0:2]
    assert message_reader.pop_messages(counter=2) == input_content_list[2:4]
    assert message_reader.pop_messages(counter=2) == input_content_list[4:6]
    assert message_reader.pop_messages(counter=2) == input_content_list[6:8]
    assert message_reader.pop_messages(counter=2) == input_content_list[8:10]
Ejemplo n.º 22
0
def test_writing_popping_in_sequence(temp_file):
    test_message = "test message 1\n"
    writeable_file, readable_file = prepare_input_file(test_message, temp_file)
    single_line_parser = SingleLineParser()

    message_reader = MessageReader(readable_file.readline, single_line_parser)

    assert message_reader.pop_messages(
        counter=1) == test_message.splitlines(True)

    test_message = "test message 2\ntest message 3\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(
        counter=2) == test_message.splitlines(True)

    test_message = "test message 4\ntest message 5\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=1) == ["test message 4\n"]
    assert message_reader.pop_messages(counter=1) == ["test message 5\n"]

    test_message = "test message 6\ntest message 7\ntest message 8\ntest message 9\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    os.fsync(writeable_file.fileno())
    assert message_reader.pop_messages(
        counter=READ_ALL_AVAILABLE_MESSAGES) == test_message.splitlines(True)

    test_message = "test message 10\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(
        counter=1) == test_message.splitlines(True)
Ejemplo n.º 23
0
def test_writing_popping_in_sequence(tc_unittest):
    test_message = "test message 1\n"
    writeable_file, readable_file = tc_unittest.prepare_input_file(test_message)
    single_line_parser = SingleLineParser(tc_unittest.get_fake_logger_factory())

    message_reader = MessageReader(tc_unittest.get_fake_logger_factory(), readable_file.read, single_line_parser)

    assert message_reader.pop_messages(counter=1) == test_message.splitlines(True)

    test_message = "test message 2\ntest message 3\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=2) == test_message.splitlines(True)

    test_message = "test message 4\ntest message 5\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=1) == ["test message 4\n"]
    assert message_reader.pop_messages(counter=1) == ["test message 5\n"]

    test_message = "test message 6\ntest message 7\ntest message 8\ntest message 9\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=0) == test_message.splitlines(True)

    test_message = "test message 10\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=1) == test_message.splitlines(True)
Ejemplo n.º 24
0
 def dd_read_logs(self, path, counter):
     if not self.__reader:
         io = self.__IOClass(path)
         io.wait_for_creation()
         message_reader = MessageReader(io.read, SingleLineParser())
         self.__reader = message_reader
     messages = self.__reader.pop_messages(counter)
     read_description = "Content has been read from\nresource: {}\ncontent: {}\n".format(
         path, messages)
     logger.info(read_description)
     return messages
Ejemplo n.º 25
0
 def dd_read_logs(self, path, counter):
     if not self.__reader:
         io = self.__IOClass(self.__logger_factory, path)
         io.wait_for_creation()
         message_reader = MessageReader(
             self.__logger_factory, io.read, SingleLineParser(self.__logger_factory)
         )
         self.__reader = message_reader
     messages = self.__reader.pop_messages(counter)
     self.__logger.print_io_content(path, messages, "Content has been read from")
     return messages
Ejemplo n.º 26
0
def test_writing_popping_in_sequence(temp_file):
    test_message = "test message 1\n"
    writeable_file, readable_file = prepare_input_file(test_message, temp_file)
    single_line_parser = SingleLineParser()

    message_reader = MessageReader(readable_file.read, single_line_parser)

    assert message_reader.pop_messages(counter=1) == test_message.splitlines(True)

    test_message = "test message 2\ntest message 3\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=2) == test_message.splitlines(True)

    test_message = "test message 4\ntest message 5\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=1) == ["test message 4\n"]
    assert message_reader.pop_messages(counter=1) == ["test message 5\n"]

    test_message = "test message 6\ntest message 7\ntest message 8\ntest message 9\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=READ_ALL_MESSAGES) == test_message.splitlines(True)

    test_message = "test message 10\n"
    writeable_file.write(test_message)
    writeable_file.flush()
    assert message_reader.pop_messages(counter=1) == test_message.splitlines(True)
Ejemplo n.º 27
0
 def __init__(self, instance_paths):
     self.__stderr_io = FileIO(instance_paths.get_stderr_path())
     self.__message_reader = MessageReader(self.__stderr_io.read, SingleLineParser())
Ejemplo n.º 28
0
 def __init__(self, instance_paths):
     self.__stderr_io = FileIO(instance_paths.get_stderr_path())
     self.__message_reader = MessageReader(self.__stderr_io.readline,
                                           SingleLineParser())
     self.__console_log_path = instance_paths.get_stderr_path()
Ejemplo n.º 29
0
class ConsoleLogReader(object):
    def __init__(self, instance_paths):
        self.__stderr_io = FileIO(instance_paths.get_stderr_path())
        self.__message_reader = MessageReader(self.__stderr_io.readline,
                                              SingleLineParser())
        self.__console_log_path = instance_paths.get_stderr_path()

    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_message_in_console_log(self, expected_message):
        return self.wait_for_messages_in_console_log(self, [expected_message])

    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_AVAILABLE_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", "assertion"]
        with self.__console_log_path.open() as f:
            console_log_messages = f.readlines()
        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))
                    raise Exception("Unexpected error log in console",
                                    console_log_message)

    def dump_stderr(self, last_n_lines=10):
        console_log_messages = self.__message_reader.peek_messages(
            counter=READ_ALL_AVAILABLE_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