예제 #1
0
def execute_test_content(**kwargs):
    logging_manager = ParallelLoggingManager('Run_Tests.log', real_time_logs_only=not kwargs['nightly'])
    build_context = BuildContext(kwargs, logging_manager)
    threads_list = []
    for server_ip, port in build_context.instances_ips.items():
        tests_execution_instance = ServerContext(build_context, server_private_ip=server_ip, tunnel_port=port)
        threads_list.append(Thread(target=tests_execution_instance.execute_tests))

    for thread in threads_list:
        thread.start()

    for t in threads_list:
        t.join()

    if not build_context.unmockable_tests_to_run.empty() or not build_context.mockable_tests_to_run.empty():
        raise Exception('Not all tests have been executed')
    if build_context.tests_data_keeper.playbook_skipped_integration \
            and build_context.build_name != 'master' \
            and not build_context.is_nightly:
        skipped_integrations = '\n- '.join(build_context.tests_data_keeper.playbook_skipped_integration)
        comment = f'{SKIPPED_INTEGRATION_COMMENT}:\n- {skipped_integrations}'
        _add_pr_comment(comment, logging_manager)
    build_context.tests_data_keeper.print_test_summary(build_context.isAMI, logging_manager)
    build_context.tests_data_keeper.create_result_files()
    if build_context.tests_data_keeper.failed_playbooks:
        logging_manager.critical("Some tests have failed. Not destroying instances.", real_time=True)
        sys.exit(1)
예제 #2
0
def test_create_module(mocker, incident_configuration, expected):
    """
    Given:
        incident configuration with only incident type
        incident configuration with both incident type and classifier/ mapper
        incident configuration with only classifier/ mapper
        incident configuration without incident configuration
    When:
        running configuring instance for test playbook run

    Then:
        validate the module iד configured with the incident configuration.
        (the default incidentType is changed to new one, and mapper/ classifier are added)
    """
    class Dummyconf:
        unmockable_integrations = []

    test_build_params = {
        'api_key': '',
        'server': '',
        'conf': '',
        'secret': '',
        'nightly': '',
        'circleci': '',
        'slack': '',
        'server_type': 'XSOAR',
        'build_number': '',
        'branch_name': '',
        'is_ami': '',
        'mem_check': '',
        'server_version': ''
    }
    mocker.patch.object(BuildContext,
                        '_load_conf_files',
                        return_value=(Dummyconf(), ''))
    mocker.patch.object(BuildContext, '_load_env_results_json')
    mocker.patch.object(BuildContext, '_get_server_numeric_version')
    mocker.patch.object(BuildContext, '_get_instances_ips')
    mocker.patch.object(BuildContext, '_extract_filtered_tests')
    mocker.patch.object(BuildContext, '_get_unmockable_tests_from_conf')
    mocker.patch.object(BuildContext,
                        '_get_tests_to_run',
                        return_value=('', ''))
    mocker.patch.object(BuildContext, '_retrieve_slack_user_id')
    mocker.patch.object(BuildContext, '_get_all_integration_config')

    test_integration = Integration(
        BuildContext(test_build_params, ParallelLoggingManager('temp_log')),
        'example_integration', [])

    res_module = test_integration.create_module(
        instance_name='test',
        configuration=deepcopy(CONFIGURATION),
        incident_configuration=incident_configuration)
    assert res_module.get('configuration').get('configuration')[0].get(
        'value') == expected.get('incident_type')
    assert res_module.get('incomingMapperId') == expected.get('mapper')
    assert res_module.get('mappingId') == expected.get('classifier')
예제 #3
0
def get_mocked_build_context(
        mocker,
        tmp_file,
        content_conf_json: dict = None,
        secret_conf_json: dict = None,
        env_results_content: dict = None,
        filtered_tests_content: list = None,
        nightly: bool = False,
        server_version: str = 'Server Master'
) -> BuildContext:
    """
    Generates a BuildContext instance with mocked data.
    Args:
        mocker: The mocker instance used in the unittest
        tmp_file: the path in which the log should be written
        content_conf_json: The contents of conf.json to load in the BuildContext instance
        secret_conf_json: The contents of content-test-conf conf.json to load in the BuildContext instance
        env_results_content: The contents of env_results.json to load in the BuildContext instance
        filtered_tests_content: The contents of filtered_tests to load in the BuildContext instance
        nightly: Indicates whether this build is a nightly build
        server_version: The server version to run the instance on
    """
    logging_manager = ParallelLoggingManager(tmp_file / 'log_file.log')
    conf_path = tmp_file / 'conf_path'
    conf_path.write_text(json.dumps(content_conf_json or generate_content_conf_json()))

    secret_conf_path = tmp_file / 'secret_conf_path'
    secret_conf_path.write_text(json.dumps(secret_conf_json or generate_secret_conf_json()))

    env_results_path = tmp_file / 'env_results_path'
    env_results_path.write_text(json.dumps(env_results_content or generate_env_results_content()))
    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.ENV_RESULTS_PATH', str(env_results_path))

    filtered_tests_path = tmp_file / 'filtered_tests_path'
    filtered_tests_path.write_text('\n'.join(filtered_tests_content or []))
    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.FILTER_CONF', str(filtered_tests_path))

    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.BuildContext._retrieve_slack_user_id',
                 return_value='some_user_id')
    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.BuildContext._get_all_integration_config',
                 return_value=[])
    kwargs = {
        'api_key': 'api_key',
        'server': None,
        'conf': conf_path,
        'secret': secret_conf_path,
        'slack': 'slack_token',
        'nightly': nightly,
        'is_ami': True,
        'circleci': 'circle_token',
        'build_number': '11111',
        'branch_name': 'branch',
        'server_version': server_version,
        'mem_check': False,
        'server_type': 'XSOAR'
    }
    return BuildContext(kwargs, logging_manager)
예제 #4
0
def create_xsiam_build(mocker, tmp_file):
    logging_manager = ParallelLoggingManager(tmp_file / 'log_file.log')
    conf_path = tmp_file / 'conf_path'
    conf_path.write_text(json.dumps(generate_content_conf_json()))

    secret_conf_path = tmp_file / 'secret_conf_path'
    secret_conf_path.write_text(json.dumps(generate_secret_conf_json()))

    xsiam_servers_path = tmp_file / 'xsiam_servers_path.json'
    xsiam_servers_path.write_text(json.dumps(generate_xsiam_servers_data()))

    env_results_path = tmp_file / 'env_results_path'
    env_results_path.write_text(json.dumps(generate_env_results_content()))
    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.ENV_RESULTS_PATH', str(env_results_path))

    filtered_tests_path = tmp_file / 'filtered_tests_path'
    filtered_tests_path.write_text('[]')
    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.FILTER_CONF', str(filtered_tests_path))

    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.BuildContext._retrieve_slack_user_id',
                 return_value='some_user_id')
    mocker.patch('demisto_sdk.commands.test_content.TestContentClasses.BuildContext._get_all_integration_config',
                 return_value=[])
    kwargs = {
        'api_key': 'api_key',
        'server': None,
        'conf': conf_path,
        'secret': secret_conf_path,
        'slack': 'slack_token',
        'nightly': False,
        'is_ami': True,
        'circleci': 'circle_token',
        'build_number': '11111',
        'branch_name': 'branch',
        'server_version': 'XSIAM Master',
        'mem_check': False,
        'server_type': 'XSIAM',
        'xsiam_servers_path': xsiam_servers_path,
        'xsiam_machine': 'qa2-test-111111'
    }
    return BuildContext(kwargs, logging_manager)