Exemple #1
0
def provision_agent_using_config_file(context, port, agent_tags, status):
    agent_name = f"{agent_name_prefix}{random_string(10)}"
    interface = configs.get('orb_agent_interface', 'mock')
    orb_url = configs.get('orb_url')
    base_orb_address = configs.get('orb_address')
    context.agent_file_name = create_agent_config_file(
        context.token,
        agent_name,
        interface,
        agent_tags,
        orb_url,
        base_orb_address,
        port,
        existing_agent_groups=context.agent_groups,
        context=context)
    context.container_id = run_agent_config_file(agent_name)
    if context.container_id not in context.containers_id.keys():
        context.containers_id[context.container_id] = str(port)
    context.agent, is_agent_created = check_agent_exists_on_backend(
        context.token, agent_name, timeout=10)
    logs = get_orb_agent_logs(context.container_id)
    assert_that(is_agent_created, equal_to(True),
                f"Agent {agent_name} not found. Logs: {logs}")
    assert_that(context.agent, is_not(None),
                f"Agent {agent_name} not correctly created. Logs: {logs}")
    agent_id = context.agent['id']
    existing_agents = get_agent(context.token, agent_id)
    assert_that(len(existing_agents), greater_than(0),
                f"Agent not created. Logs: {logs}")
    wait_until_expected_agent_status(context.token, agent_id, status)
def policy_stopped_and_removed(container_id,
                               stop_policy_info,
                               remove_policy_info,
                               start_considering_time,
                               event=None):
    """

    :param (str) container_id: agent container id
    :param (str) stop_policy_info: log info that confirms that the policy was stopped
    :param (str) remove_policy_info: log info that confirms that the policy was removed
    :param (str) start_considering_time: timestamp after which logs must be validated
    :param (obj) event: threading.event
    :return: (bool) if the expected message is found return True, if not, False
    """
    found = {'stop': False, 'remove': False}
    logs = get_orb_agent_logs(container_id)
    for log_line in logs:
        log_line = safe_load_json(log_line)
        if found['stop'] is False:
            found['stop'] = is_expected_log_info_in_log_line(
                log_line, stop_policy_info, start_considering_time)

        if found['remove'] is False:
            found['remove'] = is_expected_log_info_in_log_line(
                log_line, remove_policy_info, start_considering_time)
        if found['stop'] is True and found['remove'] is True:
            event.set()
            return event.is_set()
    return event.is_set()
Exemple #3
0
def check_if_agents_exist(context, orb_tags, status):
    context.agent_name = generate_random_string_with_predefined_prefix(
        agent_name_prefix)
    context.orb_tags = create_tags_set(orb_tags)
    context.agent = create_agent(context.token, context.agent_name,
                                 context.orb_tags)
    context.agent_key = context.agent["key"]
    token = context.token
    run_local_agent_container(context, "available")
    agent_id = context.agent['id']
    existing_agents = get_agent(token, agent_id)
    assert_that(len(existing_agents), greater_than(0), "Agent not created")
    timeout = 30
    agent_status = wait_until_expected_agent_status(token,
                                                    agent_id,
                                                    status,
                                                    timeout=timeout)
    context.agent = get_agent(token, agent_id)
    logs = get_orb_agent_logs(context.container_id)
    assert_that(
        agent_status, is_(equal_to(status)),
        f"Agent did not get '{status}' after {str(timeout)} seconds, but was '{agent_status}'. \n"
        f"Agent: {json.dumps(context.agent, indent=4)}. \n Logs: {logs}")
    local_orb_path = configs.get("local_orb_path")
    agent_schema_path = local_orb_path + "/python-test/features/steps/schemas/agent_schema.json"
    is_schema_valid = validate_json(context.agent, agent_schema_path)
    assert_that(
        is_schema_valid, equal_to(True),
        f"Invalid agent json. \n Agent = {context.agent}."
        f"Agent logs: {get_orb_agent_logs(context.container_id)}."
        f"\nLogs: {logs}")
def check_agent_log_for_policies(
        expected_message,
        container_id,
        list_agent_policies_id,
        considered_timestamp=datetime.now().timestamp(),
        event=None):
    """
    Checks agent container logs for expected message for each applied policy over a period of time

    :param (str) expected_message: message that we expect to find in the logs
    :param (str) container_id: agent container id
    :param (list) list_agent_policies_id: list with all policy id applied to the agent
    :param (float) considered_timestamp: timestamp from which the log will be considered.
                                                                Default: timestamp at which behave execution is started
    :param (obj) event: threading.event
    """
    logs = get_orb_agent_logs(container_id)
    policies_have_expected_message = \
        check_logs_contain_message_for_policies(logs, expected_message, list_agent_policies_id,
                                                considered_timestamp)
    if len(policies_have_expected_message) == len(list_agent_policies_id):
        event.set()
        return policies_have_expected_message

    return policies_have_expected_message
Exemple #5
0
def list_policies_applied_to_an_agent(context, amount_of_policies):
    context.agent, context.list_agent_policies_id = get_policies_applied_to_an_agent(
        context.token, context.agent['id'], amount_of_policies, timeout=180)
    context.agent = get_agent(context.token, context.agent['id'])
    logs = get_orb_agent_logs(context.container_id)
    assert_that(
        len(context.list_agent_policies_id), equal_to(int(amount_of_policies)),
        f"Amount of policies applied to this agent failed with {len(context.list_agent_policies_id)} policies."
        f"\n Agent: {json.dumps(context.agent, indent=4)}. \n Logs: {logs}")
Exemple #6
0
def multiple_dataset_for_policy(context, amount_of_datasets, time_to_wait):
    datasets_ok, context.agent = check_datasets_for_policy(
        context.token,
        context.agent['id'],
        context.list_agent_policies_id,
        amount_of_datasets,
        timeout=time_to_wait)
    logs = get_orb_agent_logs(context.container_id)
    diff = datasets_ok ^ set(context.list_agent_policies_id)
    assert_that(
        datasets_ok, equal_to(set(context.list_agent_policies_id)),
        f"Amount of datasets linked with policy {diff} failed. Agent: {context.agent}. \nAgent logs: {logs}"
    )
def check_logs_for_group(context, text_to_match, time_to_wait):
    groups_matching, context.groups_matching_id = return_matching_groups(
        context.token, context.agent_groups, context.agent)
    text_found, groups_to_which_subscribed = check_subscription(
        groups_matching,
        text_to_match,
        context.container_id,
        timeout=time_to_wait)
    container_logs = get_orb_agent_logs(context.container_id)
    assert_that(
        text_found, is_(True),
        f"Message {text_to_match} was not found in the agent logs for group(s)"
        f"{set(groups_matching).difference(groups_to_which_subscribed)}!.\n\n"
        f"Logs = {container_logs}. \n\n"
        f"Agent: {json.dumps(context.agent, indent=4)} \n\n")
Exemple #8
0
def check_agent_status(context, status):
    timeout = 30
    token = context.token
    agent_id = context.agent['id']
    agent_status = wait_until_expected_agent_status(token,
                                                    agent_id,
                                                    status,
                                                    timeout=timeout)
    context.agent = get_agent(context.token, context.agent['id'])
    logs = get_orb_agent_logs(context.container_id)
    assert_that(
        agent_status, is_(equal_to(status)),
        f"Agent did not get '{status}' after {str(timeout)} seconds, but was '{agent_status}'."
        f"Agent: {json.dumps(context.agent, indent=4)}."
        f"Agent logs: {logs}.")
Exemple #9
0
def reset_agent_remotely(context):
    context.considered_timestamp_reset = datetime.now().timestamp()
    headers_request = {
        'Content-type': 'application/json',
        'Accept': '*/*',
        'Authorization': f'Bearer {context.token}'
    }
    response = requests.post(
        f"{orb_url}/api/v1/agents/{context.agent['id']}/rpc/reset",
        headers=headers_request)
    logs = get_orb_agent_logs(context.container_id)
    assert_that(
        response.status_code, equal_to(200),
        f"Request to restart agent failed with status= {str(response.status_code)}. \n Agent: {context.agent}\n"
        f" Logs: {logs}")
Exemple #10
0
def list_policies_applied_to_an_agent_and_referred_status(
        context, amount_of_policies, amount_of_policies_with_status,
        policies_status):
    list_policies_applied_to_an_agent(context, amount_of_policies)
    list_of_policies_status = list()
    for policy_id in context.list_agent_policies_id:
        list_of_policies_status.append(
            context.agent['last_hb_data']['policy_state'][policy_id]["state"])
    if amount_of_policies_with_status == "all":
        amount_of_policies_with_status = int(amount_of_policies)
    amount_of_policies_applied_with_status = list_of_policies_status.count(
        policies_status)
    logs = get_orb_agent_logs(context.container_id)
    assert_that(
        amount_of_policies_applied_with_status,
        equal_to(int(amount_of_policies_with_status)),
        f"{amount_of_policies_with_status} policies was supposed to have status {policies_status}. \n"
        f"Agent: {context.agent}. \n Logs: {logs}")
Exemple #11
0
def list_groups_matching_an_agent(context, amount_of_groups):
    groups_matching, context.groups_matching_id = return_matching_groups(
        context.token, context.agent_groups, context.agent)
    context.list_groups_id = get_groups_to_which_agent_is_matching(
        context.token,
        context.agent['id'],
        context.groups_matching_id,
        timeout=180)
    context.agent = get_agent(context.token, context.agent['id'])
    logs = get_orb_agent_logs(context.container_id)
    assert_that(
        len(context.list_groups_id), equal_to(int(amount_of_groups)),
        f"Amount of groups matching the agent failed with {context.list_groups_id} groups. \n"
        f"Agent: {json.dumps(context.agent, indent=4)} \n\n"
        f"Agent Logs: {logs}.")
    assert_that(
        sorted(context.list_groups_id),
        equal_to(sorted(context.groups_matching_id)),
        "Groups matching the agent is not the same as the created by test process"
    )
def check_subscription(agent_groups_names,
                       expected_message,
                       container_id,
                       event=None):
    """

    :param (list) agent_groups_names: groups to which the agent must be subscribed
    :param (str) expected_message: message that we expect to find in the logs
    :param (str) container_id: agent container id
    :param (obj) event: threading.event
    :return: (bool) True if agent is subscribed to all matching groups, (list) names of the groups to which agent is subscribed
    """
    groups_to_which_subscribed = set()
    for name in agent_groups_names:
        logs = get_orb_agent_logs(container_id)
        text_found, log_line = check_logs_contain_message_and_name(
            logs, expected_message, name, "group_name")
        if text_found is True:
            groups_to_which_subscribed.add(log_line["group_name"])
            if set(groups_to_which_subscribed) == set(agent_groups_names):
                event.set()
                return event.is_set(), groups_to_which_subscribed

    return event.is_set(), groups_to_which_subscribed