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')
Пример #2
0
def test_is_runnable_on_this_instance(mocker):
    """
    Given:
        - A test configuration configured to run only on instances that uses docker as container engine
    When:
        - The method _is_runnable_on_current_server_instance is invoked from the TestContext class
    Then:
        - Ensure that it returns False when the test is running on REHL instance that uses podman
        - Ensure that it returns True when the test is running on a regular Linux instance that uses docker
    """
    test_playbook_configuration = TestConfiguration(
        generate_test_configuration(
            playbook_id='playbook_runnable_only_on_docker',
            runnable_on_docker_only=True),
        default_test_timeout=30)
    test_context_builder = partial(TestContext,
                                   build_context=mocker.MagicMock(),
                                   playbook=TestPlaybook(
                                       mocker.MagicMock(),
                                       test_playbook_configuration),
                                   client=mocker.MagicMock())

    test_context = test_context_builder(server_context=mocker.MagicMock(
        is_instance_using_docker=False))
    assert not test_context._is_runnable_on_current_server_instance()
    test_context = test_context_builder(server_context=mocker.MagicMock(
        is_instance_using_docker=True))
    assert test_context._is_runnable_on_current_server_instance()
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_docker_thresholds_for_pwsh_integrations(mocker):
    """
    Given:
        - A test context with a playbook that uses a powershell integration
    When:
        - Running 'get_threshold_values' method
    Then:
        - Ensure that the memory threshold is the default powershell memory threshold value
        - Ensure that the pis threshold is the default powershell pid threshold value
    """
    test_playbook_configuration = TestConfiguration(
        generate_test_configuration(
            playbook_id='playbook_runnable_only_on_docker',
            integrations=['integration']),
        default_test_timeout=30)
    playbook_instance = TestPlaybook(mocker.MagicMock(),
                                     test_playbook_configuration)
    playbook_instance.integrations[
        0].integration_type = Docker.POWERSHELL_INTEGRATION_TYPE
    test_context = TestContext(build_context=mocker.MagicMock(),
                               playbook=playbook_instance,
                               client=mocker.MagicMock(),
                               server_context=mocker.MagicMock())
    memory_threshold, pid_threshold = test_context.get_threshold_values()
    assert memory_threshold == Docker.DEFAULT_PWSH_CONTAINER_MEMORY_USAGE
    assert pid_threshold == Docker.DEFAULT_PWSH_CONTAINER_PIDS_USAGE
 def create_playbook_instance(mocker):
     test_playbook_configuration = TestConfiguration(
         generate_test_configuration(playbook_id='playbook_with_context',
                                     integrations=['integration']),
         default_test_timeout=30)
     pb_instance = TestPlaybook(mocker.MagicMock(),
                                test_playbook_configuration)
     pb_instance.build_context.logging_module = mocker.MagicMock()
     return pb_instance
Пример #6
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