class SyslogNgCtlExecutor(object):
    def __init__(self, instance_paths):
        self.__instance_paths = instance_paths
        self.__working_dir = instance_paths.get_working_dir()
        self.__syslog_ng_control_tool_path = instance_paths.get_syslog_ng_ctl_bin()
        self.__syslog_ng_control_socket_path = instance_paths.get_control_socket_path()
        self.__command_executor = CommandExecutor()

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_ctl_command(command),
            stdout_path=self.__construct_std_file_path(command_short_name, "stdout"),
            stderr_path=self.__construct_std_file_path(command_short_name, "stderr"),
        )

    def __construct_std_file_path(self, command_short_name, std_type):
        instance_name = self.__instance_paths.get_instance_name()
        return Path(self.__working_dir, "syslog_ng_ctl_{}_{}_{}".format(instance_name, command_short_name, std_type))

    def __construct_ctl_command(self, command):
        ctl_command = [self.__syslog_ng_control_tool_path]
        ctl_command += command
        ctl_command.append("--control={}".format(self.__syslog_ng_control_socket_path))
        return ctl_command

    @staticmethod
    def construct_ctl_stats_command(reset):
        stats_command = ["stats"]
        if reset:
            stats_command.append("--reset")
        return stats_command
Esempio n. 2
0
class SyslogNgCtlExecutor(object):
    def __init__(self, instance_paths):
        self.__instance_paths = instance_paths
        self.__working_dir = instance_paths.get_working_dir()
        self.__syslog_ng_control_tool_path = instance_paths.get_syslog_ng_ctl_bin()
        self.__syslog_ng_control_socket_path = instance_paths.get_control_socket_path()
        self.__command_executor = CommandExecutor()

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_ctl_command(command),
            stdout_path=self.__construct_std_file_path(command_short_name, "stdout"),
            stderr_path=self.__construct_std_file_path(command_short_name, "stderr"),
        )

    def __construct_std_file_path(self, command_short_name, std_type):
        instance_name = self.__instance_paths.get_instance_name()
        return Path(self.__working_dir, "syslog_ng_ctl_{}_{}_{}".format(instance_name, command_short_name, std_type))

    def __construct_ctl_command(self, command):
        ctl_command = [self.__syslog_ng_control_tool_path]
        ctl_command += command
        ctl_command.append("--control={}".format(self.__syslog_ng_control_socket_path))
        return ctl_command

    @staticmethod
    def construct_ctl_stats_command(reset):
        stats_command = ["stats"]
        if reset:
            stats_command.append("--reset")
        return stats_command
class SyslogNgCtlExecutor(object):
    def __init__(self, instance_paths):
        self.__instance_paths = instance_paths
        self.__syslog_ng_control_tool_path = instance_paths.get_syslog_ng_ctl_bin(
        )
        self.__syslog_ng_control_socket_path = instance_paths.get_control_socket_path(
        )
        self.__command_executor = CommandExecutor()

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_ctl_command(command),
            stdout_path=self.__construct_std_file_path(command_short_name,
                                                       "stdout"),
            stderr_path=self.__construct_std_file_path(command_short_name,
                                                       "stderr"),
        )

    def __construct_std_file_path(self, command_short_name, std_type):
        instance_name = self.__instance_paths.get_instance_name()
        return Path(
            tc_parameters.WORKING_DIR,
            "syslog_ng_ctl_{}_{}_{}".format(instance_name, command_short_name,
                                            std_type))

    def __construct_ctl_command(self, command):
        ctl_command = [self.__syslog_ng_control_tool_path]
        ctl_command += command
        ctl_command.append("--control={}".format(
            self.__syslog_ng_control_socket_path))
        return ctl_command

    @staticmethod
    def construct_ctl_stats_command(reset):
        stats_command = ["stats"]
        if reset:
            stats_command.append("--reset")
        return stats_command

    @staticmethod
    def construct_ctl_credentials_command(credential, secret):
        return ["credentials", "add", credential, secret]

    @staticmethod
    def construct_ctl_query_command(pattern, query_type):
        query_command = ["query"]

        if query_type == QueryTypes.QUERY_GET:
            query_command += ["get"]
        elif query_type == QueryTypes.QUERY_SUM:
            query_command += ["get --sum"]
        elif query_type == QueryTypes.QUERY_LIST:
            query_command += ["list"]
        elif query_type == QueryTypes.QUERY_GET_RESET:
            query_command += ["get --reset"]

        query_command += [pattern]

        return query_command
Esempio n. 4
0
def test_execute_command(tmpdir, command, expected_stdout, expected_stderr, expected_exit_code):
    stdout_file = tmpdir.join("stdout.log")
    stderr_file = tmpdir.join("stderr.log")
    command_executor = CommandExecutor()
    result = command_executor.run(command, stdout_file, stderr_file)
    assert result["stdout"] == expected_stdout
    assert result["stderr"] == expected_stderr
    assert result["exit_code"] == expected_exit_code
def test_execute_command(tc_unittest, command, expected_stdout,
                         expected_stderr, expected_exit_code):
    stdout_path = tc_unittest.get_temp_file()
    stderr_path = tc_unittest.get_temp_file()
    command_executor = CommandExecutor(tc_unittest.get_fake_logger_factory())
    result = command_executor.run(command, stdout_path, stderr_path)
    assert result["stdout"] == expected_stdout
    assert result["stderr"] == expected_stderr
    assert result["exit_code"] == expected_exit_code
Esempio n. 6
0
def test_execute_command(tmpdir, command, expected_stdout, expected_stderr,
                         expected_exit_code):
    stdout_file = tmpdir.join("stdout.log")
    stderr_file = tmpdir.join("stderr.log")
    command_executor = CommandExecutor()
    result = command_executor.run(command, stdout_file, stderr_file)
    assert result["stdout"] == expected_stdout
    assert result["stderr"] == expected_stderr
    assert result["exit_code"] == expected_exit_code
Esempio n. 7
0
class SyslogNgExecutor(object):
    def __init__(self, logger_factory, instance_paths):
        self.__instance_paths = instance_paths
        self.__process_executor = ProcessExecutor(logger_factory)
        self.__command_executor = CommandExecutor(logger_factory)

    def run_process(self):
        return self.__process_executor.start(
            command=self.__construct_syslog_ng_process(),
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_process_with_valgrind(self):
        valgrind_command_args = [
            "valgrind",
            "--show-leak-kinds=all",
            "--track-origins=yes",
            "--tool=memcheck",
            "--leak-check=full",
            "--keep-stacktraces=alloc-and-free",
            "--read-var-info=yes",
            "--error-limit=no",
            "--num-callers=40",
            "--verbose",
            "--log-file={}".format(
                self.__instance_paths.get_valgrind_log_path()),
        ]
        full_command_args = valgrind_command_args + self.__construct_syslog_ng_process(
        )
        return self.__process_executor.start(
            command=full_command_args,
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_syslog_ng_command(command),
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(
                postfix=command_short_name),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(
                postfix=command_short_name),
        )

    def get_backtrace_from_core(self, core_file):
        gdb_command_args = [
            "gdb",
            "-ex",
            "bt full",
            "--batch",
            self.__instance_paths.get_syslog_ng_bin(),
            "--core",
            core_file,
        ]
        core_postfix = "gdb_core_{}".format(get_unique_id())
        return self.__command_executor.run(
            command=gdb_command_args,
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(
                postfix=core_postfix),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(
                postfix=core_postfix),
        )

    def __construct_syslog_ng_process(
        self,
        stderr=True,
        debug=True,
        trace=True,
        verbose=True,
        startup_debug=True,
        no_caps=True,
        config_path=None,
        persist_path=None,
        pid_path=None,
        control_socket_path=None,
    ):
        syslog_ng_process_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_process_args += ["--foreground", "--enable-core"]
        if stderr:
            syslog_ng_process_args += ["--stderr"]
        if debug:
            syslog_ng_process_args += ["--debug"]
        if trace:
            syslog_ng_process_args += ["--trace"]
        if verbose:
            syslog_ng_process_args += ["--verbose"]
        if startup_debug:
            syslog_ng_process_args += ["--startup-debug"]
        if no_caps:
            syslog_ng_process_args += ["--no-caps"]
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        syslog_ng_process_args += ["--cfgfile={}".format(config_path)]
        if persist_path is None:
            persist_path = self.__instance_paths.get_persist_path()
        syslog_ng_process_args += ["--persist-file={}".format(persist_path)]
        if pid_path is None:
            pid_path = self.__instance_paths.get_pid_path()
        syslog_ng_process_args += ["--pidfile={}".format(pid_path)]
        if control_socket_path is None:
            control_socket_path = self.__instance_paths.get_control_socket_path(
            )
        syslog_ng_process_args += ["--control={}".format(control_socket_path)]
        return syslog_ng_process_args

    def __construct_syslog_ng_command(self, command):
        syslog_ng_command_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_command_args += command
        return syslog_ng_command_args
Esempio n. 8
0
class SyslogNgExecutor(object):
    def __init__(self, instance_paths):
        self.__instance_paths = instance_paths
        self.__process_executor = ProcessExecutor()
        self.__command_executor = CommandExecutor()

    def run_process(self, stderr, debug, trace, verbose, startup_debug, no_caps, config_path, persist_path, pid_path, control_socket_path):
        return self.__process_executor.start(
            command=self.__construct_syslog_ng_process(stderr, debug, trace, verbose, startup_debug, no_caps, config_path, persist_path, pid_path, control_socket_path),
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_process_with_external_tool(self, external_tool):
        self.__instance_paths.register_external_tool_output_path(external_tool)
        if external_tool == "valgrind":
            return self.run_process_with_valgrind()
        elif external_tool == "strace":
            return self.run_process_with_strace()
        else:
            raise Exception("Unknown external tool was selected: {}".format(external_tool))

    def run_process_with_valgrind(self):
        valgrind_command_args = [
            "valgrind",
            "--show-leak-kinds=all",
            "--track-origins=yes",
            "--tool=memcheck",
            "--leak-check=full",
            "--keep-stacktraces=alloc-and-free",
            "--read-var-info=yes",
            "--error-limit=no",
            "--num-callers=40",
            "--verbose",
            "--log-file={}".format(self.__instance_paths.get_external_tool_output_path("valgrind")),
        ]
        full_command_args = valgrind_command_args + self.__construct_syslog_ng_process()
        return self.__process_executor.start(
            command=full_command_args,
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_process_with_strace(self):
        strace_command_args = [
            "strace",
            "-s",
            "4096",
            "-tt",
            "-T",
            "-ff",
            "-o",
            self.__instance_paths.get_external_tool_output_path("strace"),
        ]
        full_command_args = strace_command_args + self.__construct_syslog_ng_process()
        return self.__process_executor.start(
            command=full_command_args,
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_syslog_ng_command(command),
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(postfix=command_short_name),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(postfix=command_short_name),
        )

    def get_backtrace_from_core(self, core_file):
        gdb_command_args = [
            "gdb",
            "-ex",
            "bt full",
            "--batch",
            self.__instance_paths.get_syslog_ng_bin(),
            "--core",
            core_file,
        ]
        core_postfix = "gdb_core_{}".format(get_unique_id())
        return self.__command_executor.run(
            command=gdb_command_args,
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(postfix=core_postfix),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(postfix=core_postfix),
        )

    def __construct_syslog_ng_process(
        self,
        stderr,
        debug,
        trace,
        verbose,
        startup_debug,
        no_caps,
        config_path,
        persist_path,
        pid_path,
        control_socket_path,
    ):
        syslog_ng_process_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_process_args += ["--foreground", "--enable-core"]
        if stderr:
            syslog_ng_process_args += ["--stderr"]
        if debug:
            syslog_ng_process_args += ["--debug"]
        if trace:
            syslog_ng_process_args += ["--trace"]
        if verbose:
            syslog_ng_process_args += ["--verbose"]
        if startup_debug:
            syslog_ng_process_args += ["--startup-debug"]
        if no_caps:
            syslog_ng_process_args += ["--no-caps"]
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        syslog_ng_process_args += ["--cfgfile={}".format(config_path)]
        if persist_path is None:
            persist_path = self.__instance_paths.get_persist_path()
        syslog_ng_process_args += ["--persist-file={}".format(persist_path)]
        if pid_path is None:
            pid_path = self.__instance_paths.get_pid_path()
        syslog_ng_process_args += ["--pidfile={}".format(pid_path)]
        if control_socket_path is None:
            control_socket_path = self.__instance_paths.get_control_socket_path()
        syslog_ng_process_args += ["--control={}".format(control_socket_path)]
        return syslog_ng_process_args

    def __construct_syslog_ng_command(self, command):
        syslog_ng_command_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_command_args += command
        return syslog_ng_command_args
class SyslogNgExecutor(object):
    def __init__(self, logger_factory, instance_paths):
        self.__instance_paths = instance_paths
        self.__process_executor = ProcessExecutor(logger_factory)
        self.__command_executor = CommandExecutor(logger_factory)

    def run_process(self):
        return self.__process_executor.start(
            command=self.__construct_syslog_ng_process(),
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_syslog_ng_command(command),
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(postfix=command_short_name),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(postfix=command_short_name),
        )

    def __construct_syslog_ng_process(
        self,
        stderr=True,
        debug=True,
        trace=True,
        verbose=True,
        startup_debug=True,
        no_caps=True,
        config_path=None,
        persist_path=None,
        pid_path=None,
        control_socket_path=None,
    ):
        syslog_ng_process_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_process_args += ["--foreground", "--enable-core"]
        if stderr:
            syslog_ng_process_args += ["--stderr"]
        if debug:
            syslog_ng_process_args += ["--debug"]
        if trace:
            syslog_ng_process_args += ["--trace"]
        if verbose:
            syslog_ng_process_args += ["--verbose"]
        if startup_debug:
            syslog_ng_process_args += ["--startup-debug"]
        if no_caps:
            syslog_ng_process_args += ["--no-caps"]
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        syslog_ng_process_args += ["--cfgfile={}".format(config_path)]
        if persist_path is None:
            persist_path = self.__instance_paths.get_persist_path()
        syslog_ng_process_args += ["--persist-file={}".format(persist_path)]
        if pid_path is None:
            pid_path = self.__instance_paths.get_pid_path()
        syslog_ng_process_args += ["--pidfile={}".format(pid_path)]
        if control_socket_path is None:
            control_socket_path = self.__instance_paths.get_control_socket_path()
        syslog_ng_process_args += ["--control={}".format(control_socket_path)]
        return syslog_ng_process_args

    def __construct_syslog_ng_command(self, command):
        syslog_ng_command_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_command_args += command
        return syslog_ng_command_args
Esempio n. 10
0
class SyslogNgExecutor(object):
    def __init__(self, instance_paths):
        self.__instance_paths = instance_paths
        self.__process_executor = ProcessExecutor()
        self.__command_executor = CommandExecutor()

    def run_process(self):
        return self.__process_executor.start(
            command=self.__construct_syslog_ng_process(),
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_process_with_valgrind(self):
        valgrind_command_args = [
            "valgrind",
            "--show-leak-kinds=all",
            "--track-origins=yes",
            "--tool=memcheck",
            "--leak-check=full",
            "--keep-stacktraces=alloc-and-free",
            "--read-var-info=yes",
            "--error-limit=no",
            "--num-callers=40",
            "--verbose",
            "--log-file={}".format(self.__instance_paths.get_valgrind_log_path()),
        ]
        full_command_args = valgrind_command_args + self.__construct_syslog_ng_process()
        return self.__process_executor.start(
            command=full_command_args,
            stdout_path=self.__instance_paths.get_stdout_path(),
            stderr_path=self.__instance_paths.get_stderr_path(),
        )

    def run_command(self, command_short_name, command):
        return self.__command_executor.run(
            command=self.__construct_syslog_ng_command(command),
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(postfix=command_short_name),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(postfix=command_short_name),
        )

    def get_backtrace_from_core(self, core_file):
        gdb_command_args = [
            "gdb",
            "-ex",
            "bt full",
            "--batch",
            self.__instance_paths.get_syslog_ng_bin(),
            "--core",
            core_file,
        ]
        core_postfix = "gdb_core_{}".format(get_unique_id())
        return self.__command_executor.run(
            command=gdb_command_args,
            stdout_path=self.__instance_paths.get_stdout_path_with_postfix(postfix=core_postfix),
            stderr_path=self.__instance_paths.get_stderr_path_with_postfix(postfix=core_postfix),
        )

    def __construct_syslog_ng_process(
        self,
        stderr=True,
        debug=True,
        trace=True,
        verbose=True,
        startup_debug=True,
        no_caps=True,
        config_path=None,
        persist_path=None,
        pid_path=None,
        control_socket_path=None,
    ):
        syslog_ng_process_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_process_args += ["--foreground", "--enable-core"]
        if stderr:
            syslog_ng_process_args += ["--stderr"]
        if debug:
            syslog_ng_process_args += ["--debug"]
        if trace:
            syslog_ng_process_args += ["--trace"]
        if verbose:
            syslog_ng_process_args += ["--verbose"]
        if startup_debug:
            syslog_ng_process_args += ["--startup-debug"]
        if no_caps:
            syslog_ng_process_args += ["--no-caps"]
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        syslog_ng_process_args += ["--cfgfile={}".format(config_path)]
        if persist_path is None:
            persist_path = self.__instance_paths.get_persist_path()
        syslog_ng_process_args += ["--persist-file={}".format(persist_path)]
        if pid_path is None:
            pid_path = self.__instance_paths.get_pid_path()
        syslog_ng_process_args += ["--pidfile={}".format(pid_path)]
        if control_socket_path is None:
            control_socket_path = self.__instance_paths.get_control_socket_path()
        syslog_ng_process_args += ["--control={}".format(control_socket_path)]
        return syslog_ng_process_args

    def __construct_syslog_ng_command(self, command):
        syslog_ng_command_args = [self.__instance_paths.get_syslog_ng_bin()]
        syslog_ng_command_args += command
        return syslog_ng_command_args