Exemple #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)
Exemple #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')
Exemple #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)
Exemple #4
0
    def test_listeners_with_multiple_threads(self, tmp_path):
        """
        Given:
            - a ParallelLoggingManager
        When:
            - writing logs to the log file with 5 different threads
        Then:
            - assert logs does not appear in the file before each thread has called the 'execute_logs' method
            - assert logs are written to the file grouped together for each thread
        """
        pytest.skip(
            "For unknown reason this test cannot pass in the current infra")
        log_file_path = f'{tmp_path}/log_file.log'
        logging_manager = ParallelLoggingManager(log_file_path)
        successful_threads_results = set()

        def thread_log_function():
            thread_name = currentThread().getName()
            logging_manager.info('test1')
            logging_manager.info('test2')
            logging_manager.info('test3')
            log_content_lines = TestParallelLoggingManager.get_log_content(
                logging_manager.log_file_name)
            thread_logs = [
                line for line in log_content_lines if thread_name in line
            ]
            assert not thread_logs
            logging_manager.execute_logs()
            log_content_lines = TestParallelLoggingManager.get_log_content(
                logging_manager.log_file_name)
            first_thread_log_line = next(line for line in log_content_lines
                                         if thread_name in line)
            first_thread_log_line_index = log_content_lines.index(
                first_thread_log_line)
            self.assert_log_line(
                log_content_lines[first_thread_log_line_index], thread_name,
                'INFO', 'test1')
            self.assert_log_line(
                log_content_lines[first_thread_log_line_index + 1],
                thread_name, 'INFO', 'test2')
            self.assert_log_line(
                log_content_lines[first_thread_log_line_index + 2],
                thread_name, 'INFO', 'test3')
            successful_threads_results.add(thread_name)

        threads = []
        for i in range(5):
            threads.append(Thread(target=thread_log_function))
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()
            assert thread.getName() in successful_threads_results
Exemple #5
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)
Exemple #6
0
 def test_queue_listener_sanity(self, tmp_path):
     """
     Given:
         - a ParallelLoggingManager
     When:
         - writing logs to the log file
     Then:
         - assert logs are not written to the file until execute_logs method is called
         - assert all logs appear in file after execute_logs method is called
     """
     pytest.skip(
         "For unknown reason this test cannot pass in the current infra")
     log_file_path = f'{tmp_path}/log_file.log'
     logging_manager = ParallelLoggingManager(log_file_path)
     logging_manager.debug('debug1')
     logging_manager.info('info1')
     logging_manager.warning('warning1')
     logging_manager.error('error1')
     logging_manager.critical('critical1')
     logging_manager.success('success1')
     # Generating exception
     try:
         _ = 1 / 0
     except Exception:
         logging_manager.exception('exception1')
     log_file_lines = self.get_log_content(log_file_path)
     assert not log_file_lines
     logging_manager.execute_logs()
     log_file_lines = self.get_log_content(log_file_path)
     for expected_in_log, log_line in zip(
             self.get_expected_content('1').values(), log_file_lines):
         thread_name, level, content = expected_in_log
         self.assert_log_line(log_line, thread_name, level, content)
Exemple #7
0
 def test_real_time_logger_sanity(self, tmp_path):
     """
     Given:
         - a ParallelLoggingManager
     When:
         - writing logs to the log file using the flag 'real_time=True'
     Then:
         - assert logs are written to the file immediately
     """
     pytest.skip(
         "For unknown reason this test cannot pass in the current infra")
     log_file_path = f'{tmp_path}/log_file.log'
     logging_manager = ParallelLoggingManager(log_file_path)
     expected_logs = self.get_expected_content('1')
     logging_manager.debug('debug1', real_time=True)
     self.assert_latest_log_line(log_file_path, *expected_logs['debug'])
     logging_manager.info('info1', real_time=True)
     self.assert_latest_log_line(log_file_path, *expected_logs['info'])
     logging_manager.warning('warning1', real_time=True)
     self.assert_latest_log_line(log_file_path, *expected_logs['warning'])
     logging_manager.error('error1', real_time=True)
     self.assert_latest_log_line(log_file_path, *expected_logs['error'])
     logging_manager.critical('critical1', real_time=True)
     self.assert_latest_log_line(log_file_path, *expected_logs['critical'])
     logging_manager.success('success1', real_time=True)
     self.assert_latest_log_line(log_file_path, *expected_logs['success'])