def write_config(self, config_path): if self.__raw_config: rendered_config = self.__raw_config else: rendered_config = ConfigRenderer(self.__syslog_ng_config).get_rendered_config() logger.info("Generated syslog-ng config\n{}\n".format(rendered_config)) f = File(config_path) with f.open('w+') as config_file: config_file.write(rendered_config)
def write(self, content, rate=None): loggen_input_file_path = Path(tc_parameters.WORKING_DIR, "loggen_input_{}.txt".format(get_unique_id())) loggen_input_file = File(loggen_input_file_path) loggen_input_file.open(mode="w") loggen_input_file.write(content) loggen_input_file.close() Loggen().start(self.__ip, self.__port, read_file=str(loggen_input_file_path), dont_parse=True, permanent=True, rate=rate, **self.__transport.value)
def get_log(self): f = File(self.snmptrapd_log) f.open("r") log = f.read() f.close() return log
def test_pp_tls_passthrough(config, syslog_ng, port_allocator, loggen, testcase_parameters): server_key_path = copy_shared_file(testcase_parameters, "server.key") server_cert_path = copy_shared_file(testcase_parameters, "server.crt") network_source = config.create_network_source( ip="localhost", port=port_allocator(), transport='"proxied-tls-passthrough"', flags="no-parse", tls={ "key-file": server_key_path, "cert-file": server_cert_path, "peer-verify": '"optional-untrusted"', }, ) file_destination = config.create_file_destination(file_name="output.log", template=TEMPLATE) config.create_logpath(statements=[network_source, file_destination]) syslog_ng.start(config) loggen_input_file_path = Path( tc_parameters.WORKING_DIR, "loggen_input_{}.txt".format(get_unique_id())) loggen_input_file = File(loggen_input_file_path) loggen_input_file.open(mode="w") loggen_input_file.write(INPUT_MESSAGES) loggen_input_file.close() loggen.start( network_source.options["ip"], network_source.options["port"], number=NUMBER_OF_MESSAGES, use_ssl=True, proxied_tls_passthrough=True, read_file=str(loggen_input_file_path), dont_parse=True, proxy_src_ip="1.1.1.1", proxy_dst_ip="2.2.2.2", proxy_src_port="3333", proxy_dst_port="4444", ) wait_until_true( lambda: loggen.get_sent_message_count() == NUMBER_OF_MESSAGES) assert file_destination.read_log() == EXPECTED_MESSAGES
def get_traps(self, counter): trap_list = [] f = File(self.snmptrapd_log) f.open("r") while True: trap_line = f.wait_for_lines([self.TRAP_LOG_PREFIX])[0] res = re.match('({})(.*)'.format(self.TRAP_LOG_PREFIX), trap_line) if (res): trap_list.extend(res.group(2).rstrip().split("\t")) if len(trap_list) == counter: break f.close() return sorted(trap_list)
def __init__(self, file_path): self.__readable_file = File(file_path) self.__writeable_file = File(file_path)
class FileIO(): def __init__(self, file_path): self.__readable_file = File(file_path) self.__writeable_file = File(file_path) def read_number_of_lines(self, counter): if not self.__readable_file.is_opened(): if not self.__readable_file.wait_for_creation(): raise Exception("{} was not created in time.".format( self.__readable_file.path)) self.__readable_file.open("r") return self.__readable_file.wait_for_number_of_lines(counter) def read_until_lines(self, lines): if not self.__readable_file.is_opened(): if not self.__readable_file.wait_for_creation(): raise Exception("{} was not created in time.".format( self.__readable_file.path)) self.__readable_file.open("r") return self.__readable_file.wait_for_lines(lines) def write(self, content): if not self.__writeable_file.is_opened(): self.__writeable_file.open("a+") self.__writeable_file.write(content)
def __read_all_from_stderr_file(self): stderr_file_from_the_beginning = File(self.__stderr_path) stderr_file_from_the_beginning.open("r") stderr = stderr_file_from_the_beginning.read() stderr_file_from_the_beginning.close() return stderr
def __init__(self, instance_paths): self.__stderr_path = instance_paths.get_stderr_path() self.__stderr_file = File(self.__stderr_path)
class ConsoleLogReader(object): def __init__(self, instance_paths): self.__stderr_path = instance_paths.get_stderr_path() self.__stderr_file = File(self.__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_file.is_opened(): self.__stderr_file.wait_for_creation() self.__stderr_file.open("r") return self.__stderr_file.wait_for_lines(expected_messages, timeout=5) def check_for_unexpected_messages(self, unexpected_messages=None): unexpected_patterns = ["Plugin module not found", "assertion"] if unexpected_messages is not None: unexpected_patterns.extend(unexpected_messages) stderr = self.__read_all_from_stderr_file() for unexpected_pattern in unexpected_patterns: for console_log_message in stderr.split("\n"): 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): stderr = self.__read_all_from_stderr_file() logger.error("\n".join(stderr.split("\n")[-last_n_lines:])) def __read_all_from_stderr_file(self): stderr_file_from_the_beginning = File(self.__stderr_path) stderr_file_from_the_beginning.open("r") stderr = stderr_file_from_the_beginning.read() stderr_file_from_the_beginning.close() return stderr @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 prepare_std_outputs(stdout_path, stderr_path): stdout = File(stdout_path) stderr = File(stderr_path) return stdout, stderr