def init_server_context(mocker, tmp_path, mockable=False):
    playbook_type = 'mocked_playbook' if mockable else 'unmocked_playbook'
    playbook_id_type = 'mocked_playbook' if mockable else 'unmocked_playbook'
    integrations_type = 'mocked_integration' if mockable else 'unmocked_integration'
    mock_func = '_execute_unmockable_tests' if mockable else '_execute_mockable_tests'
    unmockable_integration = {
        integrations_type: 'reason'
    } if not mockable else {}

    filtered_tests = [playbook_type]
    tests = [
        generate_test_configuration(playbook_id=playbook_id_type,
                                    integrations=[integrations_type])
    ]
    integrations_configurations = [
        generate_integration_configuration(integrations_type)
    ]
    secret_test_conf = generate_secret_conf_json(integrations_configurations)
    content_conf_json = generate_content_conf_json(
        tests=tests, unmockable_integrations=unmockable_integration)
    build_context = get_mocked_build_context(
        mocker,
        tmp_path,
        secret_conf_json=secret_test_conf,
        content_conf_json=content_conf_json,
        filtered_tests_content=filtered_tests)
    mocked_demisto_client = DemistoClientMock(integrations=[integrations_type])
    server_context = generate_mocked_server_context(build_context,
                                                    mocked_demisto_client,
                                                    mocker)
    mocker.patch.object(server_context, mock_func, return_value=None)

    return build_context, server_context
def test_replacing_placeholders(mocker, tmp_path):
    """
    Given:
        - Integration with placeholders, different servers
    When:
        - Calling _set_integration_params during creating integrations configurations
    Then:
        - Ensure that replacing placeholders happens not in place,
        and next integration with same build_context, will able to replace '%%SERVER_HOST%%' placeholder.
    """
    # Setting up the build context
    filtered_tests = ['playbook_integration', 'playbook_second_integration']
    # Setting up the content conf.json
    tests = [
        generate_test_configuration(
            playbook_id='playbook_integration',
            integrations=['integration_with_placeholders']),
        generate_test_configuration(
            playbook_id='playbook_second_integration',
            integrations=['integration_with_placeholders'])
    ]
    content_conf_json = generate_content_conf_json(
        tests=tests,
        unmockable_integrations={'FirstIntegration': 'reason'},
        skipped_tests={})
    # Setting up the content-test-conf conf.json
    integration_names = ['integration_with_placeholders']
    integrations_configurations = [
        generate_integration_configuration(
            name=integration_name, params={'url': '%%SERVER_HOST%%/server'})
        for integration_name in integration_names
    ]
    secret_test_conf = generate_secret_conf_json(integrations_configurations)

    # Setting up the build_context instance
    build_context = get_mocked_build_context(
        mocker,
        tmp_path,
        content_conf_json=content_conf_json,
        secret_conf_json=secret_test_conf,
        filtered_tests_content=filtered_tests)

    integration = Integration(build_context, 'integration_with_placeholders',
                              ['instance'])
    integration._set_integration_params(server_url='1.1.1.1',
                                        playbook_id='playbook_integration',
                                        is_mockable=False)
    integration = Integration(build_context, 'integration_with_placeholders',
                              ['instance'])
    integration._set_integration_params(server_url='1.2.3.4',
                                        playbook_id='playbook_integration',
                                        is_mockable=False)
    assert '%%SERVER_HOST%%' in build_context.secret_conf.integrations[
        0].params.get('url')
예제 #3
0
def test_second_playback_enforcement(mocker, tmp_path):
    """
    Given:
        - A mockable test
    When:
        - The mockable test fails on the second playback
    Then:
        - Ensure that it exists in the failed_playbooks set
        - Ensure that it does not exists in the succeeded_playbooks list
    """
    class RunIncidentTestMock:
        call_count = 0
        count_response_mapping = {
            1: PB_Status.FAILED,  # The first playback run
            2: PB_Status.COMPLETED,  # The record run
            3: PB_Status.FAILED  # The second playback run
        }

        @staticmethod
        def run_incident_test(*_):
            # First playback run
            RunIncidentTestMock.call_count += 1
            return RunIncidentTestMock.count_response_mapping[
                RunIncidentTestMock.call_count]

    filtered_tests = ['mocked_playbook']
    tests = [
        generate_test_configuration(playbook_id='mocked_playbook',
                                    integrations=['mocked_integration'])
    ]
    integrations_configurations = [
        generate_integration_configuration('mocked_integration')
    ]
    secret_test_conf = generate_secret_conf_json(integrations_configurations)
    content_conf_json = generate_content_conf_json(tests=tests)
    build_context = get_mocked_build_context(
        mocker,
        tmp_path,
        secret_conf_json=secret_test_conf,
        content_conf_json=content_conf_json,
        filtered_tests_content=filtered_tests)
    mocked_demisto_client = DemistoClientMock(
        integrations=['mocked_integration'])
    server_context = generate_mocked_server_context(build_context,
                                                    mocked_demisto_client,
                                                    mocker)
    mocker.patch(
        'demisto_sdk.commands.test_content.TestContentClasses.TestContext._run_incident_test',
        RunIncidentTestMock.run_incident_test)
    server_context.execute_tests()
    assert 'mocked_playbook (Second Playback)' in build_context.tests_data_keeper.failed_playbooks
    assert 'mocked_playbook' not in build_context.tests_data_keeper.succeeded_playbooks
def test_execute_tests(mocker, tmp_path):
    """
    Given:
        - A ServerContext instance that should execute three tests:
            1) A test with no integrations
            2) A test with mockable integration
            3) A test with unmockable integration

    When:
        - Running execute_tests.

    Then:
        - Ensure the server context has executed all the tests
        - Ensure the mockable and unmockable tests queues were emptied
        - Ensure no test has failed during that test
    """
    # Setting up the build context
    filtered_tests = [
        'playbook_without_integrations', 'playbook_with_mockable_integration',
        'playbook_with_unmockable_integration', 'skipped_playbook'
    ]
    # Setting up the content conf.json
    tests = [
        generate_test_configuration(
            playbook_id='playbook_without_integrations'),
        generate_test_configuration(
            playbook_id='playbook_with_mockable_integration',
            integrations=['mockable_integration']),
        generate_test_configuration(
            playbook_id='playbook_with_unmockable_integration',
            integrations=['unmockable_integration']),
        generate_test_configuration(playbook_id='skipped_playbook')
    ]
    content_conf_json = generate_content_conf_json(
        tests=tests,
        unmockable_integrations={'unmockable_integration': 'reason'},
        skipped_tests={'skipped_playbook': 'reason'})
    # Setting up the content-test-conf conf.json
    integration_names = ['mockable_integration', 'unmockable_integration']
    integrations_configurations = [
        generate_integration_configuration(integration_name)
        for integration_name in integration_names
    ]
    secret_test_conf = generate_secret_conf_json(integrations_configurations)

    # Setting up the build_context instance
    build_context = get_mocked_build_context(
        mocker,
        tmp_path,
        content_conf_json=content_conf_json,
        secret_conf_json=secret_test_conf,
        filtered_tests_content=filtered_tests)
    # Setting up the client
    mocked_demisto_client = DemistoClientMock(integrations=integration_names)
    server_context = generate_mocked_server_context(build_context,
                                                    mocked_demisto_client,
                                                    mocker)
    server_context.execute_tests()

    # Validating all tests were executed
    for test in set(filtered_tests) - {'skipped_playbook'}:
        assert test in server_context.executed_tests

    # Validating all queues were emptied
    assert build_context.mockable_tests_to_run.all_tasks_done
    assert build_context.unmockable_tests_to_run.all_tasks_done

    # Validating no failed playbooks
    assert not build_context.tests_data_keeper.failed_playbooks

    # Validating skipped test
    assert 'skipped_playbook' not in server_context.executed_tests
    assert 'skipped_playbook' in build_context.tests_data_keeper.skipped_tests