Ejemplo n.º 1
0
async def test_run_once(test_config: FaradayTestConfig, tmp_default_config, test_logger_handler,
                        test_logger_folder, executor_options):
    # Config
    workspace = test_config.workspace if "workspace" not in executor_options else executor_options["workspace"]
    configuration.set(Sections.SERVER, "api_port", str(test_config.client.port))
    configuration.set(Sections.SERVER, "host", test_config.client.host)
    configuration.set(Sections.SERVER, "workspace", workspace)
    configuration.set(Sections.TOKENS, "registration", test_config.registration_token)
    configuration.set(Sections.TOKENS, "agent", test_config.agent_token)
    path_to_basic_executor = (
            Path(__file__).parent.parent /
            'data' / 'basic_executor.py'
    )
    executor_names = ["ex1"] + ([] if "extra" not in executor_options else executor_options["extra"])
    configuration.set(Sections.AGENT, "executors", ",".join(executor_names))
    for executor_name in executor_names:
        executor_section = Sections.EXECUTOR_DATA.format(executor_name)
        params_section = Sections.EXECUTOR_PARAMS.format(executor_name)
        varenvs_section = Sections.EXECUTOR_VARENVS.format(executor_name)
        for section in [executor_section, params_section, varenvs_section]:
            if section not in configuration:
                configuration.add_section(section)

        configuration.set(executor_section, "cmd", "python {}".format(path_to_basic_executor))
        configuration.set(params_section, "out", "True")
        [configuration.set(params_section, param, "False") for param in [
            "count", "spare", "spaced_before", "spaced_middle", "err", "fails"]]
        if "varenvs" in executor_options:
            for varenv in executor_options["varenvs"]:
                configuration.set(varenvs_section, varenv, executor_options["varenvs"][varenv])

        max_size = str(64 * 1024) if "max_size" not in executor_options else executor_options["max_size"]
        configuration.set(executor_section, "max_size", max_size)

    tmp_default_config.save()

    async def ws_messages_checker(msg):
        msg_ = json.loads(msg)
        assert msg_ in executor_options["ws_responses"]
        executor_options["ws_responses"].remove(msg_)

    # Init and register it
    dispatcher = Dispatcher(test_config.client.session, tmp_default_config.config_file_path)
    await dispatcher.run_once(json.dumps(executor_options["data"]), ws_messages_checker)
    history = test_logger_handler.history
    assert len(executor_options["ws_responses"]) == 0
    for l in executor_options["logs"]:
        min_count = 1 if "min_count" not in l else l["min_count"]
        max_count = sys.maxsize if "max_count" not in l else l["max_count"]
        assert max_count >= \
            len(list(filter(lambda x: x.levelname == l["levelname"] and l["msg"] in x.message, history))) >= \
            min_count, l["msg"]
Ejemplo n.º 2
0
async def test_start_with_bad_config(test_config: FaradayTestConfig, tmp_default_config):
    # Config
    configuration.set(Sections.SERVER, "api_port", str(test_config.client.port))
    configuration.set(Sections.SERVER, "host", test_config.client.host)
    configuration.set(Sections.SERVER, "workspace", test_config.workspace)
    configuration.set(Sections.TOKENS, "registration", "NotOk" * 5)
    configuration.set(Sections.EXECUTOR_DATA.format("ex1"), "cmd", 'exit 1')
    tmp_default_config.save()

    # Init and register it
    dispatcher = Dispatcher(test_config.client.session, tmp_default_config.config_file_path)

    with pytest.raises(AssertionError):
        await dispatcher.register()
async def test_merge_config(
    test_config: FaradayTestConfig,  # noqa F811
    tmp_default_config,  # noqa F811
    test_logger_handler,  # noqa F811
    test_logger_folder,  # noqa F811
):
    configuration.set(Sections.SERVER, "api_port", str(test_config.client.port))
    configuration.set(Sections.SERVER, "websocket_port", str(test_config.client.port))
    if test_config.base_route:
        configuration.set(Sections.SERVER, "base_route", test_config.base_route)
    configuration.set(Sections.SERVER, "ssl", str(test_config.is_ssl))
    if test_config.is_ssl:
        configuration.set(Sections.SERVER, "ssl_cert", str(test_config.ssl_cert_path / "ok.crt"))
        configuration.set(Sections.SERVER, "host", "localhost")
    else:
        configuration.set(Sections.SERVER, "host", test_config.client.host)
    configuration.set(Sections.SERVER, "workspaces", test_config.workspaces_str())
    random_workspace_name = fuzzy_string(15)
    configuration.set(Sections.SERVER, "workspace", random_workspace_name)

    test_config.workspaces = [random_workspace_name] + test_config.workspaces
    configuration.set(Sections.TOKENS, "registration", test_config.registration_token)
    configuration.set(Sections.TOKENS, "agent", test_config.agent_token)
    path_to_basic_executor = Path(__file__).parent.parent / "data" / "basic_executor.py"
    configuration.set(Sections.AGENT, "executors", "ex1,ex2,ex3,ex4")

    for executor_name in ["ex1", "ex3", "ex4"]:
        executor_section = Sections.EXECUTOR_DATA.format(executor_name)
        params_section = Sections.EXECUTOR_PARAMS.format(executor_name)
        for section in [executor_section, params_section]:
            if section not in configuration:
                configuration.add_section(section)
        configuration.set(executor_section, "cmd", "python {}".format(path_to_basic_executor))

    configuration.set(Sections.EXECUTOR_PARAMS.format("ex1"), "param1", "True")
    configuration.set(Sections.EXECUTOR_PARAMS.format("ex1"), "param2", "False")
    configuration.set(Sections.EXECUTOR_PARAMS.format("ex3"), "param3", "False")
    configuration.set(Sections.EXECUTOR_PARAMS.format("ex3"), "param4", "False")
    tmp_default_config.save()
    dispatcher = Dispatcher(test_config.client.session, tmp_default_config.config_file_path)

    test_config.ws_data["run_data"] = {"agent_id": 1}
    test_config.ws_data["ws_responses"] = [{"error": "'action' key is mandatory in this websocket connection"}]
    test_config.executors = get_merge_executors()

    await dispatcher.register()
    await dispatcher.connect()

    assert len(test_config.ws_data["ws_responses"]) == 0
Ejemplo n.º 4
0
async def test_start_and_register(test_config: FaradayTestConfig, tmp_default_config):
    # Config
    configuration.set(Sections.SERVER, "api_port", str(test_config.client.port))
    configuration.set(Sections.SERVER, "host", test_config.client.host)
    configuration.set(Sections.SERVER, "workspace", test_config.workspace)
    configuration.set(Sections.TOKENS, "registration", test_config.registration_token)
    configuration.set(Sections.EXECUTOR_DATA.format("ex1"), "cmd", 'exit 1')
    tmp_default_config.save()

    # Init and register it
    dispatcher = Dispatcher(test_config.client.session, tmp_default_config.config_file_path)
    await dispatcher.register()

    # Control tokens
    assert dispatcher.agent_token == test_config.agent_token

    signer = TimestampSigner(test_config.app_config['SECRET_KEY'], salt="websocket_agent")
    agent_id = int(signer.unsign(dispatcher.websocket_token).decode('utf-8'))
    assert test_config.agent_id == agent_id
Ejemplo n.º 5
0
async def test_connect(test_config: FaradayTestConfig, tmp_default_config, test_logger_handler,
                       test_logger_folder):
    configuration.set(Sections.SERVER, "api_port", str(test_config.client.port))
    configuration.set(Sections.SERVER, "host", test_config.client.host)
    configuration.set(Sections.SERVER, "workspace", test_config.workspace)
    configuration.set(Sections.TOKENS, "registration", test_config.registration_token)
    configuration.set(Sections.TOKENS, "agent", test_config.agent_token)
    path_to_basic_executor = (
            Path(__file__).parent.parent /
            'data' / 'basic_executor.py'
    )
    configuration.set(Sections.AGENT, "executors", "ex1,ex2,ex3")

    for executor_name in ["ex1","ex2","ex3"]:
        executor_section = Sections.EXECUTOR_DATA.format(executor_name)
        params_section = Sections.EXECUTOR_PARAMS.format(executor_name)
        for section in [executor_section, params_section]:
            if section not in configuration:
                configuration.add_section(section)
        configuration.set(executor_section, "cmd", "python {}".format(path_to_basic_executor))

    configuration.set(Sections.EXECUTOR_PARAMS.format("ex1"), "param1", "True")
    configuration.set(Sections.EXECUTOR_PARAMS.format("ex1"), "param2", "False")
    configuration.set(Sections.EXECUTOR_PARAMS.format("ex2"), "param3", "False")
    configuration.set(Sections.EXECUTOR_PARAMS.format("ex2"), "param4", "False")
    tmp_default_config.save()
    dispatcher = Dispatcher(test_config.client.session, tmp_default_config.config_file_path)

    ws_responses = [{
                    'action': 'JOIN_AGENT',
                    'workspace': test_config.workspace,
                    'token': None,
                    'executors': [
                        {
                            "executor_name": "ex1",
                            "args": {
                                "param1": True,
                                "param2": False
                            }
                        },
                        {
                            "executor_name": "ex2",
                            "args": {
                                "param3": False,
                                "param4": False
                            }
                        },
                        {
                            "executor_name": "ex3",
                            "args": {}
                        }
                    ]
                }]

    async def ws_messages_checker(msg):
        msg_ = json.loads(msg)
        assert msg_ in ws_responses
        ws_responses.remove(msg_)

    await dispatcher.connect(ws_messages_checker)

    assert len(ws_responses) == 0
async def test_start_and_register(
    register_options,
    test_config: FaradayTestConfig,  # noqa F811
    tmp_default_config,  # noqa F811
    test_logger_handler,  # noqa F811
):
    os.environ["DISPATCHER_TEST"] = "True"
    if "use_ssl" in register_options:
        if (register_options["use_ssl"] and not test_config.is_ssl) or (
            not register_options["use_ssl"] and test_config.is_ssl
        ):
            pytest.skip(
                f"This test should be skipped: server_ssl:{test_config.is_ssl}"
                f" and config_use_ssl:f {register_options['use_ssl']}"
            )

    client = test_config.client

    if test_config.base_route:
        configuration[Sections.SERVER]["base_route"] = test_config.base_route

    # Config
    configuration[Sections.SERVER]["ssl"] = str(test_config.is_ssl)
    if test_config.is_ssl:
        configuration[Sections.SERVER]["ssl_cert"] = str(test_config.ssl_cert_path / "ok.crt")
        configuration[Sections.SERVER]["host"] = "localhost"
    else:
        configuration[Sections.SERVER]["host"] = client.host

    configuration[Sections.SERVER]["api_port"] = str(client.port)
    configuration[Sections.SERVER]["workspaces"] = test_config.workspaces
    if "ex1" not in configuration[Sections.AGENT][Sections.EXECUTORS]:
        configuration[Sections.AGENT][Sections.EXECUTORS]["ex1"] = {
            "max_size": "65536",
            "params": {},
            "varenvs": {},
        }
    configuration[Sections.AGENT][Sections.EXECUTORS]["ex1"]["cmd"] = "exit 1"

    for section in register_options["replace_data"]:
        for option in register_options["replace_data"][section]:
            if section not in configuration:
                configuration[section] = {}
            configuration[section][option] = register_options["replace_data"][section][option]

    tmp_default_config.save()

    # Init and register it
    dispatcher = Dispatcher(client.session, tmp_default_config.config_file_path)

    if "expected_exception" not in register_options:
        await dispatcher.register(test_config.registration_token)
        # Control tokens
        assert dispatcher.agent_token == test_config.agent_token

        signer = TimestampSigner(test_config.app_config["SECRET_KEY"], salt="websocket_agent")
        agent_id = int(signer.unsign(dispatcher.websocket_token).decode("utf-8"))
        assert test_config.agent_id == agent_id
    else:
        if "bad_registration_token" in register_options:
            if register_options["bad_registration_token"] is None:
                token = None
            elif register_options["bad_registration_token"] == "incorrect":
                token = f"{((int(test_config.registration_token) + 1) % 1000000):06}"
            elif register_options["bad_registration_token"] == "bad format":
                token = "qewqwe"
            else:  # == "bad"
                token = test_config.registration_token[0:3]
        else:
            token = test_config.registration_token
        with pytest.raises(register_options["expected_exception"]):
            await dispatcher.register(token)

    history = test_logger_handler.history

    logs_ok, failed_logs = await check_logs(history, register_options["logs"])

    if "optional_logs" in register_options and not logs_ok:
        logs_ok, new_failed_logs = await check_logs(history, register_options["optional_logs"])
        failed_logs = {"logs": failed_logs, "optional_logs": new_failed_logs}

    assert logs_ok, failed_logs
async def test_run_once(
    test_config: FaradayTestConfig,  # noqa F811
    tmp_default_config,  # noqa F811
    test_logger_handler,  # noqa F811
    test_logger_folder,  # noqa F811
    executor_options,
):
    # Config
    if "workspaces" in executor_options:
        test_config.workspaces = executor_options["workspaces"]
    workspaces = test_config.workspaces

    if test_config.base_route:
        configuration[Sections.SERVER]["base_route"] = test_config.base_route

    configuration[Sections.SERVER]["api_port"] = str(test_config.client.port)
    configuration[Sections.SERVER]["websocket_port"] = str(test_config.client.port)
    configuration[Sections.SERVER]["workspaces"] = workspaces
    if Sections.TOKENS not in configuration:
        configuration[Sections.TOKENS] = {}
    configuration[Sections.TOKENS]["agent"] = test_config.agent_token
    configuration[Sections.SERVER]["ssl"] = str(test_config.is_ssl)
    if test_config.is_ssl:
        configuration[Sections.SERVER]["ssl_cert"] = str(test_config.ssl_cert_path / "ok.crt")
        configuration[Sections.SERVER]["host"] = "localhost"
    else:
        configuration[Sections.SERVER]["host"] = test_config.client.host
    path_to_basic_executor = Path(__file__).parent.parent / "data" / "basic_executor.py"
    executor_names = ["ex1"] + ([] if "extra" not in executor_options else executor_options["extra"])

    test_config.executors = []
    for ex in executor_names:
        from faraday_agent_parameters_types.data_types import DATA_TYPE

        false_params = {
            "count": DATA_TYPE["integer"],
            "spare": DATA_TYPE["boolean"],
            "spaced_before": DATA_TYPE["boolean"],
            "spaced_middle": DATA_TYPE["boolean"],
            "err": DATA_TYPE["boolean"],
            "fails": DATA_TYPE["boolean"],
        }
        configuration[Sections.AGENT][Sections.EXECUTORS][ex] = {
            "cmd": f"python {path_to_basic_executor}",
            "params": {},
            "varenvs": {},
        }
        for param in false_params:
            configuration[Sections.AGENT][Sections.EXECUTORS][ex][Sections.EXECUTOR_PARAMS][param] = {
                "mandatory": False,
                "type": false_params[param].type.class_name,
                "base": false_params[param].type.base,
            }

        configuration[Sections.AGENT][Sections.EXECUTORS][ex][Sections.EXECUTOR_PARAMS]["out"] = {
            "mandatory": True,
            "type": DATA_TYPE["string"].type.class_name,
            "base": DATA_TYPE["string"].type.base,
        }

        if "varenvs" in executor_options:
            for varenv in executor_options["varenvs"]:
                configuration[Sections.AGENT][Sections.EXECUTORS][ex][Sections.EXECUTOR_VARENVS][
                    varenv
                ] = executor_options["varenvs"][varenv]

        max_size = str(64 * 1024) if "max_size" not in executor_options else executor_options["max_size"]
        configuration[Sections.AGENT][Sections.EXECUTORS][ex]["max_size"] = max_size
        executor_metadata = {
            "executor_name": ex,
            "args": {
                param: value
                for param, value in configuration[Sections.AGENT][Sections.EXECUTORS][ex][
                    Sections.EXECUTOR_PARAMS
                ].items()
            },
        }
        executor_metadata["args"]["out"] = {"mandatory": True, "type": "string", "base": "string"}
        test_config.executors.append(executor_metadata)

    tmp_default_config.save()

    # Init and register it
    dispatcher = Dispatcher(test_config.client.session, tmp_default_config.config_file_path)
    selected_workspace = random.choice(workspaces)
    print(selected_workspace)

    ws_responses = deepcopy(executor_options["ws_responses"])
    run_data = deepcopy(executor_options["data"])
    if "workspace" in run_data:
        run_data["workspace"] = run_data["workspace"].format(selected_workspace)
    test_config.ws_data = {"run_data": run_data, "ws_responses": ws_responses}

    await dispatcher.register(test_config.registration_token)
    await dispatcher.connect()

    history = test_logger_handler.history
    assert len(test_config.ws_data["ws_responses"]) == 0
    for log in executor_options["logs"]:
        min_count = 1 if "min_count" not in log else log["min_count"]
        max_count = sys.maxsize if "max_count" not in log else log["max_count"]
        assert (
            max_count
            >= len(
                list(
                    filter(
                        lambda x: x.levelname == log["levelname"] and log["msg"] in x.message,
                        history,
                    )
                )
            )
            >= min_count
        ), log["msg"]
async def test_run_once(
    test_config: FaradayTestConfig,  # noqa F811
    tmp_default_config,  # noqa F811
    test_logger_handler,  # noqa F811
    test_logger_folder,  # noqa F811
    executor_options,
):
    # Config
    if "workspaces" in executor_options:
        test_config.workspaces = executor_options["workspaces"].split(",")
    workspaces = test_config.workspaces
    workspaces_str = test_config.workspaces_str()

    if test_config.base_route:
        configuration.set(Sections.SERVER, "base_route",
                          test_config.base_route)

    configuration.set(Sections.SERVER, "api_port",
                      str(test_config.client.port))
    configuration.set(Sections.SERVER, "websocket_port",
                      str(test_config.client.port))
    configuration.set(Sections.SERVER, "workspaces", workspaces_str)
    if Sections.TOKENS not in configuration:
        configuration.add_section(Sections.TOKENS)
    configuration.set(Sections.TOKENS, "agent", test_config.agent_token)
    configuration.set(Sections.SERVER, "ssl", str(test_config.is_ssl))
    if test_config.is_ssl:
        configuration.set(Sections.SERVER, "ssl_cert",
                          str(test_config.ssl_cert_path / "ok.crt"))
        configuration.set(Sections.SERVER, "host", "localhost")
    else:
        configuration.set(Sections.SERVER, "host", test_config.client.host)
    path_to_basic_executor = Path(
        __file__).parent.parent / "data" / "basic_executor.py"
    executor_names = ["ex1"] + ([] if "extra" not in executor_options else
                                executor_options["extra"])
    configuration.set(Sections.AGENT, "executors", ",".join(executor_names))
    test_config.executors = []
    for executor_name in executor_names:
        executor_section = Sections.EXECUTOR_DATA.format(executor_name)
        params_section = Sections.EXECUTOR_PARAMS.format(executor_name)
        varenvs_section = Sections.EXECUTOR_VARENVS.format(executor_name)
        for section in [executor_section, params_section, varenvs_section]:
            if section not in configuration:
                configuration.add_section(section)

        configuration.set(executor_section, "cmd",
                          "python {}".format(path_to_basic_executor))
        false_params = [
            "count",
            "spare",
            "spaced_before",
            "spaced_middle",
            "err",
            "fails",
        ]
        [
            configuration.set(params_section, param, "False")
            for param in false_params
        ]
        configuration.set(params_section, "out", "True")
        if "varenvs" in executor_options:
            for varenv in executor_options["varenvs"]:
                configuration.set(varenvs_section, varenv,
                                  executor_options["varenvs"][varenv])

        max_size = str(
            64 *
            1024) if "max_size" not in executor_options else executor_options[
                "max_size"]
        configuration.set(executor_section, "max_size", max_size)
        executor_metadata = {
            "executor_name": executor_name,
            "args": {param: False
                     for param in false_params},
        }
        executor_metadata["args"]["out"] = True
        test_config.executors.append(executor_metadata)

    tmp_default_config.save()

    # Init and register it
    dispatcher = Dispatcher(test_config.client.session,
                            tmp_default_config.config_file_path)
    selected_workspace = random.choice(workspaces)
    print(selected_workspace)

    ws_responses = deepcopy(executor_options["ws_responses"])
    run_data = deepcopy(executor_options["data"])
    if "workspace" in run_data:
        run_data["workspace"] = run_data["workspace"].format(
            selected_workspace)
    test_config.ws_data = {"run_data": run_data, "ws_responses": ws_responses}

    await dispatcher.register(test_config.registration_token)
    await dispatcher.connect()

    history = test_logger_handler.history
    assert len(test_config.ws_data["ws_responses"]) == 0
    for log in executor_options["logs"]:
        min_count = 1 if "min_count" not in log else log["min_count"]
        max_count = sys.maxsize if "max_count" not in log else log["max_count"]
        assert (max_count >= len(
            list(
                filter(
                    lambda x: x.levelname == log["levelname"] and log["msg"] in
                    x.message,
                    history,
                ))) >= min_count), log["msg"]
async def test_start_and_register(
    register_options,
    test_config: FaradayTestConfig,  # noqa F811
    tmp_default_config,  # noqa F811
    test_logger_handler,  # noqa F811
):
    os.environ["DISPATCHER_TEST"] = "True"
    if "use_ssl" in register_options:
        if (register_options["use_ssl"] and not test_config.is_ssl) or (
            not register_options["use_ssl"] and test_config.is_ssl
        ):
            pytest.skip(
                f"This test should be skipped: server_ssl:{test_config.is_ssl}"
                f" and config_use_ssl:f {register_options['use_ssl']}"
            )

    client = test_config.client

    if test_config.base_route:
        configuration.set(Sections.SERVER, "base_route", test_config.base_route)

    # Config
    configuration.set(Sections.SERVER, "ssl", str(test_config.is_ssl))
    if test_config.is_ssl:
        configuration.set(Sections.SERVER, "ssl_cert", str(test_config.ssl_cert_path / "ok.crt"))
        configuration.set(Sections.SERVER, "host", "localhost")
    else:
        configuration.set(Sections.SERVER, "host", client.host)

    configuration.set(Sections.SERVER, "api_port", str(client.port))
    configuration.set(Sections.SERVER, "workspaces", test_config.workspaces_str())
    configuration.set(Sections.TOKENS, "registration", test_config.registration_token)
    configuration.set(Sections.EXECUTOR_DATA.format("ex1"), "cmd", "exit 1")

    for section in register_options["replace_data"]:
        for option in register_options["replace_data"][section]:
            if section not in configuration:
                configuration.add_section(section)
            configuration.set(section, option, register_options["replace_data"][section][option])

    tmp_default_config.save()

    # Init and register it
    dispatcher = Dispatcher(client.session, tmp_default_config.config_file_path)

    if "expected_exception" not in register_options:
        await dispatcher.register()
        # Control tokens
        assert dispatcher.agent_token == test_config.agent_token

        signer = TimestampSigner(test_config.app_config["SECRET_KEY"], salt="websocket_agent")
        agent_id = int(signer.unsign(dispatcher.websocket_token).decode("utf-8"))
        assert test_config.agent_id == agent_id
    else:
        with pytest.raises(register_options["expected_exception"]):
            await dispatcher.register()

    history = test_logger_handler.history

    logs_ok, failed_logs = await check_logs(history, register_options["logs"])

    if "optional_logs" in register_options and not logs_ok:
        logs_ok, new_failed_logs = await check_logs(history, register_options["optional_logs"])
        failed_logs = {"logs": failed_logs, "optional_logs": new_failed_logs}

    assert logs_ok, failed_logs