Exemplo n.º 1
0
def test_should_test_content_pack(mocker, tmp_path, pack_metadata_content,
                                  pack_name, expected):
    """
    Given:
        - Case A: CortexXDR content pack
        - Case B: NonSupported content pack
        - Case C: Deprecated CortexXDR content pack

    When:
        - Checking if pack should be tested

    Then:
        - Case A: Verify pack should be tested
        - Case B: Verify pack should not be tested
        - Case C: Verify pack should not be tested
    """
    # Creating temp dirs
    pack = tmp_path / PACKS_DIR / pack_name
    pack.mkdir(parents=True)
    pack_metadata_file = pack / PACKS_PACK_META_FILE_NAME
    pack_metadata_file.write_text(json.dumps(pack_metadata_content))

    # Mocking os.path.join to return the temp path created instead the path in content
    mocker.patch.object(os.path, 'join', return_value=str(pack_metadata_file))
    assert should_test_content_pack(pack_name) == expected
Exemplo n.º 2
0
def run():
    new_conf_json_objects = []
    existing_test_playbooks = load_test_data_from_conf_json()

    for pack_name in os.listdir(PACKS_DIR):
        pack_path = os.path.join(PACKS_DIR, pack_name)
        if not should_test_content_pack(pack_name):
            continue

        pack_integrations = []
        pack_test_playbooks = []

        integration_dir_path = os.path.join(pack_path, INTEGRATIONS_DIR)
        test_playbook_dir_path = os.path.join(pack_path, TEST_PLAYBOOKS_DIR)
        if not os.path.isdir(test_playbook_dir_path) or not os.listdir(
                test_playbook_dir_path):
            continue

        print(f'Going over {pack_name}')
        if os.path.exists(integration_dir_path):
            for file_or_dir in os.listdir(integration_dir_path):
                if os.path.isdir(
                        os.path.join(integration_dir_path, file_or_dir)):
                    inner_dir_path = os.path.join(integration_dir_path,
                                                  file_or_dir)
                    for integration_file in os.listdir(inner_dir_path):
                        is_yml_file = integration_file.endswith('.yml')
                        file_path = os.path.join(inner_dir_path,
                                                 integration_file)
                        if is_yml_file:
                            pack_integrations.append(
                                get_integration_data(file_path))
                else:
                    is_yml_file = file_or_dir.endswith('.yml')
                    file_path = os.path.join(integration_dir_path, file_or_dir)
                    if is_yml_file:
                        pack_integrations.append(
                            get_integration_data(file_path))

        for file_path in os.listdir(test_playbook_dir_path):
            is_yml_file = file_path.endswith('.yml')
            file_path = os.path.join(test_playbook_dir_path, file_path)
            if is_yml_file and find_type(file_path) == FileType.TEST_PLAYBOOK:
                test_playbook_id, fromversion = get_playbook_data(file_path)
                if test_playbook_id not in existing_test_playbooks:
                    pack_test_playbooks.append((test_playbook_id, fromversion))

        if pack_test_playbooks:
            new_conf_json_objects.extend(
                calc_conf_json_object(pack_integrations, pack_test_playbooks))

    add_to_conf_json(new_conf_json_objects)
    print(f'Added {len(new_conf_json_objects)} tests to the conf.json')
    print(
        f'Added the following objects to the conf.json:\n{json.dumps(new_conf_json_objects, indent=4)}'
    )
def test_should_test_content_pack(pack_name, expected):
    """
    Given:
        - Case A: CortexXDR content pack
        - Case B: NonSupported content pack

    When:
        - Checking if pack should be tested

    Then:
        - Case A: Verify pack should be tested
        - Case B: Verify pack should not be tested
    """
    assert should_test_content_pack(pack_name) == expected
def generate_pack_tests_configuration(pack_name, existing_test_playbooks):
    install_logging('Update Tests step.log', include_process_name=True)
    pack_integrations = []
    pack_test_playbooks = []

    pack_path = os.path.join(PACKS_DIR, pack_name)
    if not should_test_content_pack(pack_name):
        return pack_integrations, pack_test_playbooks, pack_name

    integration_dir_path = os.path.join(pack_path, INTEGRATIONS_DIR)
    test_playbook_dir_path = os.path.join(pack_path, TEST_PLAYBOOKS_DIR)
    if not os.path.isdir(test_playbook_dir_path) or not os.listdir(test_playbook_dir_path):
        return pack_integrations, pack_test_playbooks, pack_name

    logging.info(f'Going over {pack_name}')
    if os.path.exists(integration_dir_path):
        for file_or_dir in os.listdir(integration_dir_path):
            if os.path.isdir(os.path.join(integration_dir_path, file_or_dir)):
                inner_dir_path = os.path.join(integration_dir_path, file_or_dir)
                for integration_file in os.listdir(inner_dir_path):
                    is_yml_file = integration_file.endswith('.yml')
                    file_path = os.path.join(inner_dir_path, integration_file)
                    if is_yml_file:
                        pack_integrations.append(get_integration_data(file_path))
            else:
                is_yml_file = file_or_dir.endswith('.yml')
                file_path = os.path.join(integration_dir_path, file_or_dir)
                if is_yml_file:
                    pack_integrations.append(get_integration_data(file_path))

    for file_path in os.listdir(test_playbook_dir_path):
        is_yml_file = file_path.endswith('.yml')
        file_path = os.path.join(test_playbook_dir_path, file_path)
        if is_yml_file and find_type(file_path) == FileType.TEST_PLAYBOOK:
            test_playbook_id, fromversion = get_playbook_data(file_path)
            if test_playbook_id not in existing_test_playbooks:
                pack_test_playbooks.append((test_playbook_id, fromversion))
    return pack_integrations, pack_test_playbooks, pack_name