예제 #1
0
    def __init__(self,
                 redis_bin_base,
                 hostname=gethostname(),
                 port=None,
                 logs_dir=None,
                 password=None):
        self.redis_bin_base = redis_bin_base
        self.host = hostname
        self.port = port
        self.logs_dir = logs_dir

        self._password = str(password) if password else secrets.token_urlsafe(
            32).lstrip('-')
        self._log_file = None
        self._pid_file = None
        self._metadata_file = None
        self._password_file = None
        self._redis_server_bin = os.path.join(redis_bin_base, 'redis-server')

        if logs_dir:
            # Run Redis server from logs directory, so that the command line has the logs path
            redis_bin_dir = os.path.join(logs_dir, 'bin')
            redis_server_bin = os.path.join(
                redis_bin_dir, os.path.basename(self._redis_server_bin))
            try:
                silent_mkdir(redis_bin_dir)
                os.symlink(self._redis_server_bin, redis_server_bin)
            except FileExistsError:
                # Link was created previously
                pass
            self._redis_server_bin = redis_server_bin
예제 #2
0
def RunIntegrationTests(test_output_dir=None,
                        flow_tests_configs=None,
                        flow_tests_file=None,
                        xunit_file_name=None,
                        uid=None,
                        coverage=True,
                        public_runs=False):
    assert flow_tests_configs or flow_tests_file, 'Must provide at least flow_tests_configs or flow_tests_file'
    if not test_output_dir and uid:
        test_output_dir = os.path.join(uid.logs_dir, 'flow_test_logs')

    # if test_output_dir:
    #    self.send_flame_html(test_logs=get_link(get_firex_viewer_url(test_output_dir), 'Test Logs'))

    cmd = ['flow_tests']
    if test_output_dir:
        silent_mkdir(test_output_dir)
        cmd += ['--logs', test_output_dir]
    if flow_tests_configs:
        cmd += ['--config', flow_tests_configs]
    if flow_tests_file:
        cmd += ['--tests', flow_tests_file]
    if xunit_file_name:
        cmd += ['--xunit_file_name', xunit_file_name]
    if coverage:
        cmd += ['--coverage']
    if public_runs:
        cmd += ['--public_runs']
    start = datetime.datetime.now()
    try:
        completed = firex_subprocess.run(cmd,
                                         capture_output=True,
                                         timeout=6 * 60,
                                         check=True,
                                         text=True)
    except (firex_subprocess.CommandFailed, subprocess.TimeoutExpired) as e:
        # TimeoutExpired doesn't respect text=True, so we need to decode the output
        stdout = e.stdout
        stderr = e.stderr
        if stdout:
            if not isinstance(stdout, str):
                stdout = stdout.decode()
            logger.error('Stdout:\n' + stdout)
        if stderr:
            if not isinstance(stderr, str):
                stderr = stderr.decode()
            logger.error('Stderr:\n' + stderr)
        raise
    else:
        done = datetime.datetime.now()
        if completed.stdout:
            logger.info('Stdout:\n' + completed.stdout)
        if completed.stderr:
            logger.info('Stderr:\n' + completed.stderr)

    return (done - start).total_seconds()
예제 #3
0
    def write_report_file(data, report_file):
        # Create the json_reporter dir if it doesn't exist
        silent_mkdir(os.path.dirname(report_file))

        with open(report_file, 'w') as f:
            json.dump(convert_to_serializable(data),
                      fp=f,
                      skipkeys=True,
                      sort_keys=True,
                      indent=4)
예제 #4
0
    def __call__(self, parser, namespace, values, option_string=None):

        if not os.path.isabs(values):
            values = os.path.join(os.getcwd(), values)

        assert not os.path.isdir(values), f'{values} is a directory, you must provide a filename or path'

        if os.path.islink(values) or os.path.isfile(values):
            logger.print(f'--json_file {values} exists; removing it')
            os.remove(values)

        dirpath = os.path.dirname(values)
        if not os.path.exists(dirpath):
            logger.print(f'The directory for --json_file {values} does not exist...creating {dirpath}')
            silent_mkdir(dirpath)

        setattr(namespace, self.dest, values)
예제 #5
0
    def start(self, args, uid=None, **kwargs)->{}:
        # assemble startup cmd
        if args.recording:
            dest = args.recording
        else:
            dest = FileRegistry().get_file(DEFAULT_RECORDER_DEST_REGISTRY_KEY, uid.logs_dir)
        if not os.path.isdir(os.path.dirname(dest)):
            silent_mkdir(os.path.dirname(dest))

        cmd = "firex_recorder"
        cmd += ' --destination ' + dest
        cmd += ' --broker ' + BrokerFactory.get_broker_url()
        if self.default_timeout:
            cmd += ' --timeout ' + str(self.default_timeout)
        cmd += " &"

        # start the recording service
        logger.debug("Starting Recorder...")
        recorder_stdout = FileRegistry().get_file(RECORDER_LOG_REGISTRY_KEY, uid.logs_dir)
        with open(recorder_stdout, 'wb') as out:
            subprocess.check_call(cmd, shell=True, stdout=out, stderr=subprocess.STDOUT)