From 128acc8886b8f7dc299191c11d4712b7c0a9f48a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 20 Feb 2023 17:48:25 +0000 Subject: [PATCH 01/56] fix(#3969): pytest env errors --- .../wazuh_testing/tools/__init__.py | 37 +++++---- .../wazuh_testing/tools/utils.py | 9 ++- tests/integration/conftest.py | 78 ++++++++++++------- .../test_agentd_parametrized_reconnections.py | 21 +++-- .../test_agentd/test_agentd_reconnection.py | 16 +--- tests/integration/test_analysisd/__init__.py | 0 .../test_analysisd/test_limit_eps/__init__.py | 0 tests/integration/test_gcloud/__init__.py | 0 .../test_configuration/__init__.py | 0 .../test_functionality/__init__.py | 0 .../test_functionality/test_interval.py | 6 +- .../test_functionality/test_max_messages.py | 5 +- .../test_functionality/test_rules.py | 5 +- tests/integration/test_github/__init__.py | 0 .../test_configuration/__init__.py | 0 .../integration/test_integratord/conftest.py | 13 ++++ .../test_integratord_change_inode_alert.py | 4 +- .../test_integratord_read_json_alerts.py | 7 +- ...test_integratord_read_json_file_deleted.py | 4 +- tests/integration/test_wpk/test_wpk_agent.py | 43 +++++----- .../integration/test_wpk/test_wpk_manager.py | 8 +- .../test_wpk/test_wpk_manager_task_states.py | 4 +- tests/performance/test_api/conftest.py | 3 +- tests/performance/test_cluster/conftest.py | 2 +- .../test_cluster_logs/conftest.py | 2 +- 25 files changed, 156 insertions(+), 111 deletions(-) create mode 100644 tests/integration/test_analysisd/__init__.py create mode 100644 tests/integration/test_analysisd/test_limit_eps/__init__.py create mode 100644 tests/integration/test_gcloud/__init__.py create mode 100644 tests/integration/test_gcloud/test_configuration/__init__.py create mode 100644 tests/integration/test_gcloud/test_functionality/__init__.py create mode 100644 tests/integration/test_github/__init__.py create mode 100644 tests/integration/test_github/test_configuration/__init__.py diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 60fa272e99..b40e5d746d 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -90,28 +90,33 @@ def get_version(): + try: + if platform.system() in ['Windows', 'win32']: + with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f: + version = f.read() + return version[:version.rfind('\n')] - if platform.system() in ['Windows', 'win32']: - with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f: - version = f.read() - return version[:version.rfind('\n')] - - else: # Linux, sunos5, darwin, aix... - return subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v" - ], stderr=subprocess.PIPE).decode('utf-8').rstrip() + else: # Linux, sunos5, darwin, aix... + return subprocess.check_output([ + f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v" + ], stderr=subprocess.PIPE).decode('utf-8').rstrip() + except Exception: + return 'N/A' def get_service(): if platform.system() in ['Windows', 'win32']: - return 'wazuh-agent' - + service = 'wazuh-agent' else: # Linux, sunos5, darwin, aix... - service = subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t" - ], stderr=subprocess.PIPE).decode('utf-8').strip() - - return 'wazuh-manager' if service == 'server' else 'wazuh-agent' + try: + output = subprocess.check_output([ + f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t" + ], stderr=subprocess.PIPE).decode('utf-8').strip() + service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' + except Exception: + service = '' + + return service _data_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'data') diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index 346bfddb87..e5f22d73ee 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,9 +131,12 @@ def get_random_string(string_length, digits=True): def get_version(): - f = open('../../version.json') - data = json.load(f) - version = data['version'] + try: + f = open('../../version.json') + data = json.load(f) + version = data['version'] + except Exception: + version = 'N/A' return version diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 99d1749ee4..c74442ea2d 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -58,36 +58,39 @@ def get_report_files(): def pytest_collection_modifyitems(session, config, items): selected_tests = [] deselected_tests = [] - - for item in items: - supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) - plat = sys.platform - - selected = True - if supported_platforms and plat not in supported_platforms: - selected = False - - host_type = 'agent' if 'agent' in get_service() else 'server' - supported_types = HOST_TYPES.intersection(mark.name for mark in item.iter_markers()) - if supported_types and host_type not in supported_types: - selected = False - # Consider only first mark - levels = [mark.kwargs['level'] for mark in item.iter_markers(name="tier")] - if levels and len(levels) > 0: - tiers = item.config.getoption("--tier") - if tiers is not None and levels[0] not in tiers: + print(session) + print(config) + if not global_parameters.allow_platform_deselected_tests: + for item in items: + supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) + plat = sys.platform + + selected = True + if supported_platforms and plat not in supported_platforms: selected = False - elif item.config.getoption("--tier-minimum") > levels[0]: - selected = False - elif item.config.getoption("--tier-maximum") < levels[0]: + + host_type = 'agent' if 'agent' in get_service() else 'server' + + supported_types = HOST_TYPES.intersection(mark.name for mark in item.iter_markers()) + if supported_types and host_type not in supported_types: selected = False - if selected: - selected_tests.append(item) - else: - deselected_tests.append(item) + # Consider only first mark + levels = [mark.kwargs['level'] for mark in item.iter_markers(name="tier")] + if levels and len(levels) > 0: + tiers = item.config.getoption("--tier") + if tiers is not None and levels[0] not in tiers: + selected = False + elif item.config.getoption("--tier-minimum") > levels[0]: + selected = False + elif item.config.getoption("--tier-maximum") < levels[0]: + selected = False + if selected: + selected_tests.append(item) + else: + deselected_tests.append(item) - config.hook.pytest_deselected(items=deselected_tests) - items[:] = selected_tests + config.hook.pytest_deselected(items=deselected_tests) + items[:] = selected_tests @pytest.fixture(scope='module') @@ -226,6 +229,7 @@ def pytest_addoption(parser): parser.addoption( "--fim-database-memory", action="store_true", + default=False, help="run tests activating database memory in the syscheck configuration" ) parser.addoption( @@ -309,6 +313,12 @@ def pytest_addoption(parser): help="pass api key required for integratord tests." ) + parser.addoption( + "--allow-platform-deselected-tests", + action="store_true", + default=False, + help="Avoid tests deselection based on current environment" + ) def pytest_configure(config): # Register an additional marker @@ -378,6 +388,10 @@ def pytest_configure(config): if global_parameters.wpk_package_path: global_parameters.wpk_package_path = global_parameters.wpk_package_path + # Set collect test mode + global_parameters.allow_platform_deselected_tests = config.getoption("--allow-platform-deselected-tests") + + def pytest_html_results_table_header(cells): cells.insert(4, html.th('Tier', class_='sortable tier', col='tier')) @@ -1240,3 +1254,13 @@ def truncate_event_logs(): for log_file in log_files: truncate_file(log_file) + + +def pytest_deselected(items): + if not items: + return + config = items[0].session.config + reporter = config.pluginmanager.getplugin("terminalreporter") + reporter.ensure_newline() + for item in items: + reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) \ No newline at end of file diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 1a0799fc42..8e0784cf3a 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -72,8 +72,12 @@ # Marks pytestmark = [pytest.mark.linux, pytest.mark.win32, pytest.mark.tier(level=0), pytest.mark.agent] + test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') configurations_path = os.path.join(test_data_path, 'wazuh_conf.yaml') +global timeout +timeout = '5' + params = [ # Different parameters on UDP @@ -109,6 +113,7 @@ def teardown(): remoted_server.stop() +@pytest.fixture(scope="module", autouse=True) def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" if platform.system() == 'win32' or platform.system() == 'Windows': @@ -126,9 +131,6 @@ def set_debug_mode(): local_file_write.write('\n' + debug_line) -set_debug_mode() - - # fixtures @pytest.fixture(scope="module", params=configurations, ids=case_ids) def get_configuration(request): @@ -253,17 +255,15 @@ def wait_unable_to_connect(line): return line return None - -def change_timeout(new_value): +@pytest.fixture(scope="module") +def change_timeout(): """Set agent.recv_timeout for agentd in local internal options file. The above option sets the maximum number of seconds to wait for server response from the TCP client socket. - - Args: - new_value (int): Number of seconds (between 1 and 600). """ - new_timeout = 'agent.recv_timeout=' + new_value + global timeout + new_timeout = 'agent.recv_timeout=' + timeout if platform.system() == 'win32' or platform.system() == 'Windows': local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') else: @@ -277,9 +277,6 @@ def change_timeout(new_value): local_file_write.write('\n' + new_timeout) -change_timeout('5') - - def parse_time_from_log_line(log_line): """Create a datetime object from a date in a string. diff --git a/tests/integration/test_agentd/test_agentd_reconnection.py b/tests/integration/test_agentd/test_agentd_reconnection.py index f1baa75029..91955f9a0c 100644 --- a/tests/integration/test_agentd/test_agentd_reconnection.py +++ b/tests/integration/test_agentd/test_agentd_reconnection.py @@ -86,20 +86,15 @@ {'PROTOCOL': 'udp'} ] -config_ids = ['tcp', 'udp'] - -configurations = load_wazuh_configurations(configurations_path, __name__, params=params, metadata=metadata) - log_monitor_paths = [] - receiver_sockets_params = [] - monitored_sockets_params = [] -receiver_sockets, monitored_sockets, log_monitors = None, None, None # Set in the fixtures +config_ids = ['tcp', 'udp'] +configurations = load_wazuh_configurations(configurations_path, __name__, params=params, metadata=metadata) +receiver_sockets, monitored_sockets, log_monitors = None, None, None # Set in the fixtures authd_server = AuthdSimulator(params[0]['SERVER_ADDRESS'], key_path=SERVER_KEY_PATH, cert_path=SERVER_CERT_PATH) - remoted_server = None @@ -108,7 +103,7 @@ def teardown(): if remoted_server is not None: remoted_server.stop() - +@pytest.fixture(scope="module", autouse=True) def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" if platform.system() == 'win32' or platform.system() == 'Windows': @@ -127,9 +122,6 @@ def set_debug_mode(): local_file_write.write('\n' + debug_line) -set_debug_mode() - - # fixtures @pytest.fixture(scope="module", params=configurations, ids=config_ids) def get_configuration(request): diff --git a/tests/integration/test_analysisd/__init__.py b/tests/integration/test_analysisd/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_analysisd/test_limit_eps/__init__.py b/tests/integration/test_analysisd/test_limit_eps/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/__init__.py b/tests/integration/test_gcloud/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/test_configuration/__init__.py b/tests/integration/test_gcloud/test_configuration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/test_functionality/__init__.py b/tests/integration/test_gcloud/test_functionality/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/test_functionality/test_interval.py b/tests/integration/test_gcloud/test_functionality/test_interval.py index 72d725d43c..6dc2d6ec78 100644 --- a/tests/integration/test_gcloud/test_functionality/test_interval.py +++ b/tests/integration/test_gcloud/test_functionality/test_interval.py @@ -96,7 +96,11 @@ # Preparing -truncate_file(LOG_FILE_PATH) +@pytest.fixture(scope='module', autouse=True) +def truncate_logs(): + """Truncate the 'ossec.log' file.""" + truncate_file(LOG_FILE_PATH) + # fixtures diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 5b9b539d33..4402ced11b 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -97,7 +97,10 @@ # Preparing -truncate_file(LOG_FILE_PATH) +@pytest.fixture(scope='module', autouse=True) +def truncate_logs(): + """Truncate the 'ossec.log' file.""" + truncate_file(LOG_FILE_PATH) # fixtures diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index a6441fa587..8abe88c06e 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -93,7 +93,10 @@ # Preparing -truncate_file(LOG_FILE_PATH) +@pytest.fixture(scope='module', params=configurations) +def get_configuration(request): + """Get configurations from the module.""" + return request.param # fixtures diff --git a/tests/integration/test_github/__init__.py b/tests/integration/test_github/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_github/test_configuration/__init__.py b/tests/integration/test_github/test_configuration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_integratord/conftest.py b/tests/integration/test_integratord/conftest.py index 1243d020fa..b35bab7eb8 100644 --- a/tests/integration/test_integratord/conftest.py +++ b/tests/integration/test_integratord/conftest.py @@ -20,3 +20,16 @@ def wait_for_start_module(request): check_integratord_event(file_monitor=file_monitor, timeout=20, callback=generate_monitoring_callback(integrator.CB_INTEGRATORD_THREAD_READY), error_message=integrator.ERR_MSG_VIRUST_TOTAL_ENABLED_NOT_FOUND) + + +@pytest.fixture(scope='module') +def get_integration_api_key(): + return global_parameters.integration_api_key + + +@pytest.fixture(scope='module') +def replace_configuration_api_key(configuration, get_integration_api_key): + """ + Replace the API key in the configuration file with the one provided by the environment variable. + """ + return configuration.replace('API_KEY', get_integration_api_key) diff --git a/tests/integration/test_integratord/test_integratord_change_inode_alert.py b/tests/integration/test_integratord/test_integratord_change_inode_alert.py index 4c35546427..517a09f032 100644 --- a/tests/integration/test_integratord/test_integratord_change_inode_alert.py +++ b/tests/integration/test_integratord/test_integratord_change_inode_alert.py @@ -69,7 +69,6 @@ # Configurations configuration_parameters, configuration_metadata, case_ids = get_test_cases_data(cases_path) -configuration_parameters[0]['API_KEY'] = global_parameters.integration_api_key configurations = load_configuration_template(configurations_path, configuration_parameters, configuration_metadata) local_internal_options = {'integrator.debug': '2'} @@ -82,7 +81,8 @@ @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(configurations, configuration_metadata), ids=case_ids) -def test_integratord_change_json_inode(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_change_json_inode(configuration, metadata, replace_configuration_api_key, + set_wazuh_configuration, truncate_monitored_files, configure_local_internal_options_module, restart_wazuh_daemon_function, wait_for_start_module): ''' diff --git a/tests/integration/test_integratord/test_integratord_read_json_alerts.py b/tests/integration/test_integratord/test_integratord_read_json_alerts.py index 6a3a3e3dff..0522daa4bb 100644 --- a/tests/integration/test_integratord/test_integratord_read_json_alerts.py +++ b/tests/integration/test_integratord/test_integratord_read_json_alerts.py @@ -70,11 +70,9 @@ # Configurations t1_configuration_parameters, t1_configuration_metadata, t1_case_ids = get_test_cases_data(t1_cases_path) -t1_configuration_parameters[0]['API_KEY'] = global_parameters.integration_api_key t1_configurations = load_configuration_template(configurations_path, t1_configuration_parameters, t1_configuration_metadata) t2_configuration_parameters, t2_configuration_metadata, t2_case_ids = get_test_cases_data(t2_cases_path) -t2_configuration_parameters[0]['API_KEY'] = global_parameters.integration_api_key t2_configurations = load_configuration_template(configurations_path, t2_configuration_parameters, t2_configuration_metadata) local_internal_options = {'integrator.debug': '2'} @@ -84,7 +82,8 @@ @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t1_configurations, t1_configuration_metadata), ids=t1_case_ids) -def test_integratord_read_valid_alerts(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_valid_alerts(configuration, metadata, replace_configuration_api_key, + set_wazuh_configuration, truncate_monitored_files, configure_local_internal_options_module, restart_wazuh_daemon_function, wait_for_start_module): ''' @@ -145,7 +144,7 @@ def test_integratord_read_valid_alerts(configuration, metadata, set_wazuh_config @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t2_configurations, t2_configuration_metadata), ids=t2_case_ids) -def test_integratord_read_invalid_alerts(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_invalid_alerts(configuration, metadata, replace_configuration_api_key, set_wazuh_configuration, truncate_monitored_files, configure_local_internal_options_module, restart_wazuh_daemon_function, wait_for_start_module): ''' diff --git a/tests/integration/test_integratord/test_integratord_read_json_file_deleted.py b/tests/integration/test_integratord/test_integratord_read_json_file_deleted.py index d9dfc68c56..57ca01be8a 100644 --- a/tests/integration/test_integratord/test_integratord_read_json_file_deleted.py +++ b/tests/integration/test_integratord/test_integratord_read_json_file_deleted.py @@ -70,7 +70,6 @@ # Configurations configuration_parameters, configuration_metadata, case_ids = get_test_cases_data(cases_path) -configuration_parameters[0]['API_KEY'] = global_parameters.integration_api_key configurations = load_configuration_template(configurations_path, configuration_parameters, configuration_metadata) local_internal_options = {'integrator.debug': '2'} @@ -80,7 +79,8 @@ @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(configurations, configuration_metadata), ids=case_ids) -def test_integratord_read_json_file_deleted(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_json_file_deleted(configuration, metadata, replace_configuration_api_key, + set_wazuh_configuration, truncate_monitored_files, configure_local_internal_options_module, restart_wazuh_daemon_function, wait_for_start_module): ''' diff --git a/tests/integration/test_wpk/test_wpk_agent.py b/tests/integration/test_wpk/test_wpk_agent.py index 97aa68fc18..e9c52fa795 100644 --- a/tests/integration/test_wpk/test_wpk_agent.py +++ b/tests/integration/test_wpk/test_wpk_agent.py @@ -93,26 +93,29 @@ mark_skip_agentLinux = pytest.mark.skipif(get_service() == 'wazuh-agent' and sys_platform == 'Linux', reason="It will be blocked by wazuh/wazuh#9763") -if not global_parameters.wpk_version: - raise Exception("The WPK package version must be defined by parameter. See README.md") -if global_parameters.wpk_package_path is None: - raise ValueError("The WPK package path must be defined by parameter. See README.md") - -version_to_upgrade = global_parameters.wpk_version[0] -package_path = global_parameters.wpk_package_path[0] - -_agent_version = get_version() - -error_msg = '' -ver_split = _agent_version.replace("v", "").split(".") -if int(ver_split[0]) >= 4 and int(ver_split[1]) >= 1: - error_msg = 'Could not chmod' \ - if sys_platform != "Windows" else \ - 'Error executing command' -else: - error_msg = 'err Could not chmod' \ - if sys_platform != "Windows" else \ - 'err Cannot execute installer' + +try: + version_to_upgrade = global_parameters.wpk_version[0] + package_path = global_parameters.wpk_package_path[0] + _agent_version = get_version() + + error_msg = '' + ver_split = _agent_version.replace("v", "").split(".") + if int(ver_split[0]) >= 4 and int(ver_split[1]) >= 1: + error_msg = 'Could not chmod' \ + if sys_platform != "Windows" else \ + 'Error executing command' + else: + error_msg = 'err Could not chmod' \ + if sys_platform != "Windows" else \ + 'err Cannot execute installer' +except Exception as e: + print("Error: {}".format(e)) + print("Using default values") + version_to_upgrade = 'v4.4.0' + error_msg = '' + _agent_version = 'v4.3.0' + package_path = "https://packages.wazuh.com/wpk/linux/x86_64/wazuh_agent_v4.4.0_linux_x86_64.wpk""" time_to_sleep_until_backup = 10 diff --git a/tests/integration/test_wpk/test_wpk_manager.py b/tests/integration/test_wpk/test_wpk_manager.py index c36369e4d7..bfcac57ba0 100644 --- a/tests/integration/test_wpk/test_wpk_manager.py +++ b/tests/integration/test_wpk/test_wpk_manager.py @@ -72,7 +72,7 @@ UPGRADE_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'upgrade') TASK_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'task') SERVER_ADDRESS = 'localhost' -WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] +WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] if global_parameters.wpk_package_path else 'v4.4.0' WPK_REPOSITORY_3x = 'packages.wazuh.com/wpk/' CRYPTO = "aes" CHUNK_SIZE = 16384 @@ -88,12 +88,8 @@ max_upgrade_result_status_retries = 30 -if global_parameters.wpk_version is None: - raise ValueError("The WPK package version must be defined by parameter. See README.md") -if global_parameters.wpk_package_path is None: - raise ValueError("The WPK package path must be defined by parameter. See README.md") -version_to_upgrade = global_parameters.wpk_version[0] +version_to_upgrade = global_parameters.wpk_version[0] if global_parameters.wpk_version else 'v4.4.0' MANAGER_VERSION = get_version() diff --git a/tests/integration/test_wpk/test_wpk_manager_task_states.py b/tests/integration/test_wpk/test_wpk_manager_task_states.py index 28f2fdcf6d..a84e7a87de 100644 --- a/tests/integration/test_wpk/test_wpk_manager_task_states.py +++ b/tests/integration/test_wpk/test_wpk_manager_task_states.py @@ -67,7 +67,9 @@ UPGRADE_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'upgrade') TASK_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'task') SERVER_ADDRESS = 'localhost' -WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] + +WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] if global_parameters.wpk_package_path else 'v4.4.0' + CRYPTO = "aes" CHUNK_SIZE = 16384 TASK_TIMEOUT = '15m' diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index d1324a68f2..44b58bc006 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -173,4 +173,5 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) + if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) \ No newline at end of file diff --git a/tests/performance/test_cluster/conftest.py b/tests/performance/test_cluster/conftest.py index b550384c87..8847f1cad8 100644 --- a/tests/performance/test_cluster/conftest.py +++ b/tests/performance/test_cluster/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--artifacts_path", + "--performance-artifacts_path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." diff --git a/tests/reliability/test_cluster/test_cluster_logs/conftest.py b/tests/reliability/test_cluster/test_cluster_logs/conftest.py index faab49f847..3b56d9f860 100644 --- a/tests/reliability/test_cluster/test_cluster_logs/conftest.py +++ b/tests/reliability/test_cluster/test_cluster_logs/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--artifacts_path", + "--reliability-artifacts-path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." From 1b462d46200c0d4f91eb3cd55489d58458411309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 20 Feb 2023 18:11:33 +0000 Subject: [PATCH 02/56] fix(#3969): renamed parameter and minnor fixes --- deps/wazuh_testing/wazuh_testing/tools/utils.py | 9 +++------ tests/integration/conftest.py | 10 ++++------ .../test_gcloud/test_functionality/test_rules.py | 9 ++++----- tests/performance/test_api/conftest.py | 2 +- tests/performance/test_cluster/conftest.py | 2 +- .../test_cluster/test_cluster_logs/conftest.py | 2 +- 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index e5f22d73ee..346bfddb87 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,12 +131,9 @@ def get_random_string(string_length, digits=True): def get_version(): - try: - f = open('../../version.json') - data = json.load(f) - version = data['version'] - except Exception: - version = 'N/A' + f = open('../../version.json') + data = json.load(f) + version = data['version'] return version diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index c74442ea2d..0454b5fe7f 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -58,9 +58,7 @@ def get_report_files(): def pytest_collection_modifyitems(session, config, items): selected_tests = [] deselected_tests = [] - print(session) - print(config) - if not global_parameters.allow_platform_deselected_tests: + if not global_parameters.avoid_platform_based_deselection: for item in items: supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) plat = sys.platform @@ -314,7 +312,7 @@ def pytest_addoption(parser): ) parser.addoption( - "--allow-platform-deselected-tests", + "--avoid-platform-based-deselection", action="store_true", default=False, help="Avoid tests deselection based on current environment" @@ -389,7 +387,7 @@ def pytest_configure(config): global_parameters.wpk_package_path = global_parameters.wpk_package_path # Set collect test mode - global_parameters.allow_platform_deselected_tests = config.getoption("--allow-platform-deselected-tests") + global_parameters.avoid_platform_based_deselection = config.getoption("--avoid_platform_based_deselection") @@ -1263,4 +1261,4 @@ def pytest_deselected(items): reporter = config.pluginmanager.getplugin("terminalreporter") reporter.ensure_newline() for item in items: - reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) \ No newline at end of file + reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index 8abe88c06e..b4333e6e93 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -92,11 +92,10 @@ # Preparing - -@pytest.fixture(scope='module', params=configurations) -def get_configuration(request): - """Get configurations from the module.""" - return request.param +@pytest.fixture(scope='module', autouse=True) +def truncate_logs(): + """Truncate the 'ossec.log' file.""" + truncate_file(LOG_FILE_PATH) # fixtures diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index 44b58bc006..f745964c04 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -174,4 +174,4 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) \ No newline at end of file + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) diff --git a/tests/performance/test_cluster/conftest.py b/tests/performance/test_cluster/conftest.py index 8847f1cad8..b550384c87 100644 --- a/tests/performance/test_cluster/conftest.py +++ b/tests/performance/test_cluster/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--performance-artifacts_path", + "--artifacts_path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." diff --git a/tests/reliability/test_cluster/test_cluster_logs/conftest.py b/tests/reliability/test_cluster/test_cluster_logs/conftest.py index 3b56d9f860..faab49f847 100644 --- a/tests/reliability/test_cluster/test_cluster_logs/conftest.py +++ b/tests/reliability/test_cluster/test_cluster_logs/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--reliability-artifacts-path", + "--artifacts_path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." From 6112ee11884928d492c15010800e37cf58579555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 20 Feb 2023 18:22:36 +0000 Subject: [PATCH 03/56] fix(#3969): bad variable name --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 2 +- tests/integration/conftest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index b40e5d746d..51db9cfd8c 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -114,7 +114,7 @@ def get_service(): ], stderr=subprocess.PIPE).decode('utf-8').strip() service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' except Exception: - service = '' + service = 'N/A' return service diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 0454b5fe7f..32a2f812d3 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -387,7 +387,7 @@ def pytest_configure(config): global_parameters.wpk_package_path = global_parameters.wpk_package_path # Set collect test mode - global_parameters.avoid_platform_based_deselection = config.getoption("--avoid_platform_based_deselection") + global_parameters.avoid_platform_based_deselection = config.getoption("--avoid-platform-based-deselection") From c0ed0bd9456c52c22904a6f6d0514e59633b65bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:11:53 +0000 Subject: [PATCH 04/56] style(#3969): fit pep8 --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 10 ++++------ tests/integration/conftest.py | 2 +- .../test_agentd_parametrized_reconnections.py | 1 + .../test_agentd/test_agentd_reconnection.py | 1 + .../test_gcloud/test_functionality/test_interval.py | 4 ++-- .../test_functionality/test_max_messages.py | 2 +- .../test_gcloud/test_functionality/test_rules.py | 2 +- .../test_integratord_read_json_alerts.py | 3 ++- tests/integration/test_wpk/test_wpk_agent.py | 2 +- tests/integration/test_wpk/test_wpk_manager.py | 2 -- .../test_wpk/test_wpk_manager_task_states.py | 7 +++---- tests/performance/test_api/conftest.py | 3 ++- 12 files changed, 19 insertions(+), 20 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 51db9cfd8c..248413c943 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -97,9 +97,8 @@ def get_version(): return version[:version.rfind('\n')] else: # Linux, sunos5, darwin, aix... - return subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v" - ], stderr=subprocess.PIPE).decode('utf-8').rstrip() + return subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v"], + stderr=subprocess.PIPE).decode('utf-8').rstrip() except Exception: return 'N/A' @@ -109,9 +108,8 @@ def get_service(): service = 'wazuh-agent' else: # Linux, sunos5, darwin, aix... try: - output = subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t" - ], stderr=subprocess.PIPE).decode('utf-8').strip() + output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], + stderr=subprocess.PIPE).decode('utf-8').strip() service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' except Exception: service = 'N/A' diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 32a2f812d3..4a2b67dc17 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -318,6 +318,7 @@ def pytest_addoption(parser): help="Avoid tests deselection based on current environment" ) + def pytest_configure(config): # Register an additional marker config.addinivalue_line( @@ -390,7 +391,6 @@ def pytest_configure(config): global_parameters.avoid_platform_based_deselection = config.getoption("--avoid-platform-based-deselection") - def pytest_html_results_table_header(cells): cells.insert(4, html.th('Tier', class_='sortable tier', col='tier')) cells.insert(3, html.th('Markers')) diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 8e0784cf3a..c9bc47d1fb 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -255,6 +255,7 @@ def wait_unable_to_connect(line): return line return None + @pytest.fixture(scope="module") def change_timeout(): """Set agent.recv_timeout for agentd in local internal options file. diff --git a/tests/integration/test_agentd/test_agentd_reconnection.py b/tests/integration/test_agentd/test_agentd_reconnection.py index 91955f9a0c..f16a23fb05 100644 --- a/tests/integration/test_agentd/test_agentd_reconnection.py +++ b/tests/integration/test_agentd/test_agentd_reconnection.py @@ -103,6 +103,7 @@ def teardown(): if remoted_server is not None: remoted_server.stop() + @pytest.fixture(scope="module", autouse=True) def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" diff --git a/tests/integration/test_gcloud/test_functionality/test_interval.py b/tests/integration/test_gcloud/test_functionality/test_interval.py index 6dc2d6ec78..64e4e2b923 100644 --- a/tests/integration/test_gcloud/test_functionality/test_interval.py +++ b/tests/integration/test_gcloud/test_functionality/test_interval.py @@ -94,8 +94,8 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing +# Preparing @pytest.fixture(scope='module', autouse=True) def truncate_logs(): """Truncate the 'ossec.log' file.""" @@ -181,4 +181,4 @@ def test_interval(get_configuration, configure_environment, reset_ossec_log, dae '"Starting fetching of logs" event').result() end_time = time.time() diff_time = int(end_time - start_time) - assert time_interval - diff_time <= 10 \ No newline at end of file + assert time_interval - diff_time <= 10 diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 4402ced11b..1bd3221e18 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -95,8 +95,8 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing +# Preparing @pytest.fixture(scope='module', autouse=True) def truncate_logs(): """Truncate the 'ossec.log' file.""" diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index b4333e6e93..52a9e32b40 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -97,8 +97,8 @@ def truncate_logs(): """Truncate the 'ossec.log' file.""" truncate_file(LOG_FILE_PATH) -# fixtures +# fixtures @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" diff --git a/tests/integration/test_integratord/test_integratord_read_json_alerts.py b/tests/integration/test_integratord/test_integratord_read_json_alerts.py index 0522daa4bb..e667e493c1 100644 --- a/tests/integration/test_integratord/test_integratord_read_json_alerts.py +++ b/tests/integration/test_integratord/test_integratord_read_json_alerts.py @@ -144,7 +144,8 @@ def test_integratord_read_valid_alerts(configuration, metadata, replace_configur @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t2_configurations, t2_configuration_metadata), ids=t2_case_ids) -def test_integratord_read_invalid_alerts(configuration, metadata, replace_configuration_api_key, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_invalid_alerts(configuration, metadata, replace_configuration_api_key, + set_wazuh_configuration, truncate_monitored_files, configure_local_internal_options_module, restart_wazuh_daemon_function, wait_for_start_module): ''' diff --git a/tests/integration/test_wpk/test_wpk_agent.py b/tests/integration/test_wpk/test_wpk_agent.py index e9c52fa795..b64e870c5a 100644 --- a/tests/integration/test_wpk/test_wpk_agent.py +++ b/tests/integration/test_wpk/test_wpk_agent.py @@ -508,7 +508,7 @@ def test_wpk_agent(get_configuration, prepare_agent_version, download_wpk, remoted_simulator.change_default_listener = True event = wazuh_log_monitor.start(timeout=timeout_ack_response, error_message='ACK event not received', - callback=callback_detect_upgrade_ack_event).result() + callback=callback_detect_upgrade_ack_event).result() result = event['parameters'] if result is not None: diff --git a/tests/integration/test_wpk/test_wpk_manager.py b/tests/integration/test_wpk/test_wpk_manager.py index bfcac57ba0..e311841d65 100644 --- a/tests/integration/test_wpk/test_wpk_manager.py +++ b/tests/integration/test_wpk/test_wpk_manager.py @@ -87,8 +87,6 @@ time_until_ask_upgrade_result = 30 max_upgrade_result_status_retries = 30 - - version_to_upgrade = global_parameters.wpk_version[0] if global_parameters.wpk_version else 'v4.4.0' MANAGER_VERSION = get_version() diff --git a/tests/integration/test_wpk/test_wpk_manager_task_states.py b/tests/integration/test_wpk/test_wpk_manager_task_states.py index a84e7a87de..f6874ce6cb 100644 --- a/tests/integration/test_wpk/test_wpk_manager_task_states.py +++ b/tests/integration/test_wpk/test_wpk_manager_task_states.py @@ -424,10 +424,9 @@ def test_wpk_manager_task_states(get_configuration, configure_environment, # Chech that result of first attempt is Success assert new_expected_response[agents_id.index(agent_id)] == \ - response['data'][0]['message'], \ - f'New upgrade response did not match expected! ' \ - f'Expected {new_expected_response} obtained ' \ - f'{response["data"][0]["message"]}' + response['data'][0]['message'], f'New upgrade response did not match expected! ' \ + f'Expected {new_expected_response} obtained ' \ + f'{response["data"][0]["message"]}' for injector in injectors: injector.stop_receive() diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index f745964c04..f3b11884b2 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -110,7 +110,8 @@ def pytest_html_results_table_row(report, cells): cells[2] = HTMLStyle.colored_td(report.user_properties[0][1]) cells[3] = HTMLStyle.colored_td(str(report.user_properties[2][1])) cells.append(HTMLStyle.colored_td(str(report.user_properties[3][1]))) - cells.append(HTMLStyle.colored_td(u'\u2713' if len(report.user_properties) > 4 and report.user_properties[4][1] else '')) + cells.append(HTMLStyle.colored_td(u'\u2713' if len(report.user_properties) > 4 and report.user_properties[4][1] + else '')) cells.append(HTMLStyle.colored_td(f'{report.duration:.3f} s')) except AttributeError: From 6ff260af82ba06b8103cb5ca7bffde3634fbfb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:14:18 +0000 Subject: [PATCH 05/56] docs(#3969): include 3969 changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a687e9718f..79afbd5fbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Release report: TBD ### Added +- Fix pytest test collection errors ([#3969](https://github.com/wazuh/wazuh-qa/pull/3972)) \- (Framework + Tests) - Fix test_agent_groups system test ([#3955](https://github.com/wazuh/wazuh-qa/pull/3964)) \- (Tests) - Add new group_hash case and update the `without condition` case output in `wazuh_db/sync_agent_groups_get` ([#3959](https://github.com/wazuh/wazuh-qa/pull/3959)) \- (Tests) - Add markers for each system test environment ([#3961](https://github.com/wazuh/wazuh-qa/pull/3961)) \- (Framework + Tests) From 9e25b393026d77cb76686d2969f96a1c01f9c473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:15:02 +0000 Subject: [PATCH 06/56] style(#3969): fit pep8 tools module --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 248413c943..493c2680db 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -109,7 +109,7 @@ def get_service(): else: # Linux, sunos5, darwin, aix... try: output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], - stderr=subprocess.PIPE).decode('utf-8').strip() + stderr=subprocess.PIPE).decode('utf-8').strip() service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' except Exception: service = 'N/A' From a6952aabc4d0c0d69a94008fa42c519a9f5cab95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:26:03 +0000 Subject: [PATCH 07/56] feat(): include tests collection action --- .../workflows/pytest_tests_collection.yaml | 63 +++++++++++++++++++ .../wazuh_testing/tools/utils.py | 9 ++- f | 1 + 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/pytest_tests_collection.yaml create mode 100644 f diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml new file mode 100644 index 0000000000..e6f33fe096 --- /dev/null +++ b/.github/workflows/pytest_tests_collection.yaml @@ -0,0 +1,63 @@ +name: Tests collection + +on: + pull_request: + paths: + - tests/** + +jobs: + setup: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v14.6 + + - id: set-matrix + run: | + files_changed="${{ steps.changed-files.outputs.all_changed_files }}" + DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | uniq | tr -d ' ') + output='matrix=[' + dir_list='' + + for dir in $DIRECTORIES; do + dir_list="$dir_list\"$dir\"," + done + dir_list=${dir_list[@]::-1} + output="$output""$dir_list"' ]' + echo "$output" >> $GITHUB_OUTPUT + - name: Show matrix + run: echo ${{ steps.set-matrix.outputs.matrix }} + + run-pytest: + name: Tests Collection + needs: setup + strategy: + matrix: + value: ${{ fromJson(needs.matrix_prep.outputs.matrix) }} + + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Install dependencies + run: python3 setup.py install + working-directory: deps/wazuh_testing + + - name: Run pytest + run: | + pytest --collect-only tests/${{ matrix.value }} diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index 346bfddb87..e5f22d73ee 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,9 +131,12 @@ def get_random_string(string_length, digits=True): def get_version(): - f = open('../../version.json') - data = json.load(f) - version = data['version'] + try: + f = open('../../version.json') + data = json.load(f) + version = data['version'] + except Exception: + version = 'N/A' return version diff --git a/f b/f new file mode 100644 index 0000000000..37603f9fd5 --- /dev/null +++ b/f @@ -0,0 +1 @@ +matrix=integration performance From eb4b777ffd34d92546fb04d5b7e77e0f3dc40686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 22 May 2023 09:53:51 +0100 Subject: [PATCH 08/56] fix(#3969): fix gh tests collection --- .github/workflows/pytest_tests_collection.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index e6f33fe096..2886cea274 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -30,7 +30,7 @@ jobs: done dir_list=${dir_list[@]::-1} output="$output""$dir_list"' ]' - echo "$output" >> $GITHUB_OUTPUT + echo "$output" >> $GITHUB_ENV - name: Show matrix run: echo ${{ steps.set-matrix.outputs.matrix }} @@ -39,7 +39,7 @@ jobs: needs: setup strategy: matrix: - value: ${{ fromJson(needs.matrix_prep.outputs.matrix) }} + value: ${{ needs.setup.outputs.matrix }} runs-on: ubuntu-latest steps: From b6c8cc9aa8997b19849f719ca69020294dbb8c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 22 May 2023 10:21:49 +0100 Subject: [PATCH 09/56] refac(#3969): tests collection action --- .../workflows/pytest_tests_collection.yaml | 66 +++++++------------ 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index 2886cea274..6c3670bcc8 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -6,58 +6,40 @@ on: - tests/** jobs: - setup: + test-collection: runs-on: ubuntu-latest - outputs: - matrix: ${{ steps.set-matrix.outputs.matrix }} steps: + - name: Checkout code uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Install dependencies + run: python3 setup.py install + working-directory: deps/wazuh_testing + - name: Get changed files id: changed-files - uses: tj-actions/changed-files@v14.6 + uses: tj-actions/changed-files@v35.9.2 - - id: set-matrix + - name: Get tests modules changed + id: set-changed-modules run: | files_changed="${{ steps.changed-files.outputs.all_changed_files }}" DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | uniq | tr -d ' ') - output='matrix=[' - dir_list='' + DIRECTORIES=$(echo $DIRECTORIES | tr ' ' ',') + echo "matrix=$DIRECTORIES" >> $GITHUB_OUTPUT - for dir in $DIRECTORIES; do - dir_list="$dir_list\"$dir\"," + - name: Collect tests + run: | + DIRECTORIES=$(echo ${{ steps.set-changed-modules.outputs.matrix }} | tr ',' ' ') + for directory in $DIRECTORIES; do + pytest --collect-only tests/${directory} done - dir_list=${dir_list[@]::-1} - output="$output""$dir_list"' ]' - echo "$output" >> $GITHUB_ENV - - name: Show matrix - run: echo ${{ steps.set-matrix.outputs.matrix }} - - run-pytest: - name: Tests Collection - needs: setup - strategy: - matrix: - value: ${{ needs.setup.outputs.matrix }} - - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: 3.8 - - - name: Install dependencies - run: pip install -r requirements.txt - - - name: Install dependencies - run: python3 setup.py install - working-directory: deps/wazuh_testing - - - name: Run pytest - run: | - pytest --collect-only tests/${{ matrix.value }} From 0c2c3ba3c2dcf948c4d006bf520047afcf72fdd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 22 May 2023 15:08:59 +0100 Subject: [PATCH 10/56] fix(#3969): get service --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 4fb87b5a24..e7a71d942a 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -111,7 +111,7 @@ def get_service(): try: output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], stderr=subprocess.PIPE).decode('utf-8').strip() - service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' + service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' except Exception: service = 'N/A' From 5cc93ce79bb232efcc7f46dd414111431443cb73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:08:19 +0100 Subject: [PATCH 11/56] feat: check if agent is installed in Windows hosts --- .../wazuh_testing/tools/__init__.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index e7a71d942a..aa5f209321 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -105,15 +105,18 @@ def get_version(): def get_service(): - if platform.system() in ['Windows', 'win32']: - service = 'wazuh-agent' - else: # Linux, sunos5, darwin, aix... - try: - output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], - stderr=subprocess.PIPE).decode('utf-8').strip() - service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' - except Exception: - service = 'N/A' + try: + if platform.system() in ['Windows', 'win32']: + if os.path.exists(WAZUH_PATH): + service = 'wazuh-agent' + else: + service = 'N/A' + else: # Linux, sunos5, darwin, aix... + output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], + stderr=subprocess.PIPE).decode('utf-8').strip() + service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' + except Exception: + service = 'N/A' return service From 804b25b9cd998cfe721d6237e41c029ad939740f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:08:53 +0100 Subject: [PATCH 12/56] refactor: remove try/catch logic --- deps/wazuh_testing/wazuh_testing/tools/utils.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index e5f22d73ee..884d2b602c 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,12 +131,10 @@ def get_random_string(string_length, digits=True): def get_version(): - try: - f = open('../../version.json') - data = json.load(f) - version = data['version'] - except Exception: - version = 'N/A' + f = open('../../version.json') + data = json.load(f) + version = data['version'] + return version From 2a98192cad7e2291e8d6a33039d993bdc6edf4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:09:13 +0100 Subject: [PATCH 13/56] refactor: remove timeout global variable --- .../test_agentd/test_agentd_parametrized_reconnections.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 6b28efa1e0..1fa16fe17f 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -75,7 +75,6 @@ test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') configurations_path = os.path.join(test_data_path, 'wazuh_conf.yaml') -global timeout timeout = '5' @@ -266,7 +265,6 @@ def change_timeout(): The above option sets the maximum number of seconds to wait for server response from the TCP client socket. """ - global timeout new_timeout = 'agent.recv_timeout=' + timeout if platform.system() == 'win32' or platform.system() == 'Windows': local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') From 064e06493269de12187e9e40d31d0d226df7ed28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:09:42 +0100 Subject: [PATCH 14/56] refactor: remove truncate log fixture --- .../test_gcloud/test_functionality/test_interval.py | 11 ----------- .../test_functionality/test_max_messages.py | 11 ----------- .../test_gcloud/test_functionality/test_rules.py | 6 +----- 3 files changed, 1 insertion(+), 27 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_interval.py b/tests/integration/test_gcloud/test_functionality/test_interval.py index ee8480c34b..8bf8d5f63f 100644 --- a/tests/integration/test_gcloud/test_functionality/test_interval.py +++ b/tests/integration/test_gcloud/test_functionality/test_interval.py @@ -94,23 +94,12 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing -@pytest.fixture(scope='module', autouse=True) -def truncate_logs(): - """Truncate the 'ossec.log' file.""" - truncate_file(LOG_FILE_PATH) - - -# fixtures - @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" return request.param -# tests - @pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have support for Google Cloud integration.") def test_interval(get_configuration, configure_environment, reset_ossec_log, daemons_handler, wait_for_gcp_start): ''' diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 6c58f31c76..a0eaeb6ce2 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -101,23 +101,12 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing -@pytest.fixture(scope='module', autouse=True) -def truncate_logs(): - """Truncate the 'ossec.log' file.""" - truncate_file(LOG_FILE_PATH) - -# fixtures - - @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" return request.param -# tests - @pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have support for Google Cloud integration.") @pytest.mark.parametrize('publish_messages', [ ['- DEBUG - GCP message' for _ in range(30)], diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index 52a9e32b40..988a6613a3 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -98,18 +98,14 @@ def truncate_logs(): truncate_file(LOG_FILE_PATH) -# fixtures @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" return request.param -# tests - @pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have support for Google Cloud integration.") -def test_rules(get_configuration, configure_environment, - daemons_handler, wait_for_gcp_start): +def test_rules(get_configuration, configure_environment, reset_ossec_log, daemons_handler, wait_for_gcp_start): ''' description: Check if the 'gcp-pubsub' module gets messages matching the GCP rules. It also checks if the triggered alerts contain the proper rule ID. For this purpose, the test will From f024c10a0eddcbd2c5d5acbbfd7fc29bf6bc0d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:10:11 +0100 Subject: [PATCH 15/56] docs: include fixture docstring --- tests/integration/test_integratord/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/test_integratord/conftest.py b/tests/integration/test_integratord/conftest.py index b35bab7eb8..67e38ba46e 100644 --- a/tests/integration/test_integratord/conftest.py +++ b/tests/integration/test_integratord/conftest.py @@ -24,6 +24,7 @@ def wait_for_start_module(request): @pytest.fixture(scope='module') def get_integration_api_key(): + """Get the API key from the environment variable.""" return global_parameters.integration_api_key From 79ef471b3b9d9f9359c1c66faaa228bc5a12663e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:10:31 +0100 Subject: [PATCH 16/56] style: remove extra whitespaces --- tests/integration/test_wpk/test_wpk_manager_task_states.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/test_wpk/test_wpk_manager_task_states.py b/tests/integration/test_wpk/test_wpk_manager_task_states.py index f6874ce6cb..7dcfca4a91 100644 --- a/tests/integration/test_wpk/test_wpk_manager_task_states.py +++ b/tests/integration/test_wpk/test_wpk_manager_task_states.py @@ -67,9 +67,7 @@ UPGRADE_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'upgrade') TASK_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'task') SERVER_ADDRESS = 'localhost' - WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] if global_parameters.wpk_package_path else 'v4.4.0' - CRYPTO = "aes" CHUNK_SIZE = 16384 TASK_TIMEOUT = '15m' From d725a517c81f0beb50e646e56eea4ec26acc7a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:11:04 +0100 Subject: [PATCH 17/56] refactor: remove unnecessary if conditional --- tests/performance/test_api/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index f3b11884b2..5d45d91cb3 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -174,5 +174,4 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: - if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) From 297b91b8598a58fc4517fd5dc2ded053f15d8b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:17:26 +0100 Subject: [PATCH 18/56] refactor: remove extra file --- f | 1 - 1 file changed, 1 deletion(-) delete mode 100644 f diff --git a/f b/f deleted file mode 100644 index 37603f9fd5..0000000000 --- a/f +++ /dev/null @@ -1 +0,0 @@ -matrix=integration performance From 1184898c95b4df0fe3fde2da262cd6bfba9870f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:19:58 +0100 Subject: [PATCH 19/56] refactor: remove truncate log fixture --- .../test_gcloud/test_functionality/test_rules.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index edfe824fc7..f9e7e58a3b 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -91,13 +91,6 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing -@pytest.fixture(scope='module', autouse=True) -def truncate_logs(): - """Truncate the 'ossec.log' file.""" - truncate_file(LOG_FILE_PATH) - - @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" From 3a080940faf3d147f0c1ef8e48b3051e5899f742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:21:59 +0100 Subject: [PATCH 20/56] feat: restore wait_for_start_module fixture --- tests/integration/test_integratord/conftest.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/integration/test_integratord/conftest.py b/tests/integration/test_integratord/conftest.py index 9169f597f0..64db4af575 100644 --- a/tests/integration/test_integratord/conftest.py +++ b/tests/integration/test_integratord/conftest.py @@ -17,10 +17,6 @@ def wait_for_start_module(request): # Wait for integratord thread to start file_monitor = FileMonitor(LOG_FILE_PATH) - check_integratord_event(file_monitor=file_monitor, timeout=20, - callback=generate_monitoring_callback(integrator.CB_INTEGRATORD_THREAD_READY), - error_message=integrator.ERR_MSG_VIRUST_TOTAL_ENABLED_NOT_FOUND) - evm.check_integratord_thread_ready(file_monitor=file_monitor) # Wait for analysisd to start successfully (to detect changes in the alerts.json file) From 1682d9045bdd7d45ced485b1dd72b6805eb16b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:28:51 +0100 Subject: [PATCH 21/56] style: fix pep8 --- .github/workflows/pytest_tests_collection.yaml | 5 +++-- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 6 +++--- deps/wazuh_testing/wazuh_testing/tools/utils.py | 5 +++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index 6c3670bcc8..219e6c0604 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -33,7 +33,8 @@ jobs: id: set-changed-modules run: | files_changed="${{ steps.changed-files.outputs.all_changed_files }}" - DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | uniq | tr -d ' ') + DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | \ + uniq | tr -d ' ') DIRECTORIES=$(echo $DIRECTORIES | tr ' ' ',') echo "matrix=$DIRECTORIES" >> $GITHUB_OUTPUT @@ -41,5 +42,5 @@ jobs: run: | DIRECTORIES=$(echo ${{ steps.set-changed-modules.outputs.matrix }} | tr ',' ' ') for directory in $DIRECTORIES; do - pytest --collect-only tests/${directory} + pytest --collect-only tests/${directory} done diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 83545f0225..6d42379944 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -112,9 +112,9 @@ def get_service(): else: service = 'N/A' else: # Linux, sunos5, darwin, aix... - output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], - stderr=subprocess.PIPE).decode('utf-8').strip() - service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' + output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], + stderr=subprocess.PIPE).decode('utf-8').strip() + service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' except Exception: service = 'N/A' diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index 884d2b602c..3b17d51fd3 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -163,12 +163,13 @@ def get_host_name(): def validate_interval_format(interval): """Validate that the interval passed has the format in which the last digit is a letter from those passed and the other characters are between 0-9""" - if interval=='': + if interval == '': return False - if interval[-1] not in ['s','m', 'h','d','w','y'] or not isinstance(int(interval[0:-1]), numbers.Number): + if interval[-1] not in ['s', 'm', 'h', 'd', 'w', 'y'] or not isinstance(int(interval[0:-1]), numbers.Number): return False return True + def format_ipv6_long(ipv6_address): """Return the long form of the address representation in uppercase. From 79ce775791f45d8498f181e1cae7b0130a81d956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 15:00:27 +0100 Subject: [PATCH 22/56] fix: tests collection integratord fim --- ...basic_usage_registry_duplicated_entries.py | 2 -- .../test_integratord/test_alerts_reading.py | 22 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py b/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py index bec0a8c9d6..b9ec66a79b 100644 --- a/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py +++ b/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py @@ -4,7 +4,6 @@ from wazuh_testing import global_parameters import wazuh_testing.fim as fim from wazuh_testing.tools.configuration import load_wazuh_configurations -from wazuh_testing.tools.utils import get_version # Helper functions @@ -62,7 +61,6 @@ def get_configuration(request): # Test -@pytest.mark.skipif(get_version() != 'v4.2.3', reason="This test fails by wazuh/wazuh#6797, It was fixed on v4.2.3") @pytest.mark.parametrize('key, subkey1, subkey2, arch', [(key, sub_key_1, sub_key_2, fim.KEY_WOW64_32KEY)]) def test_registry_duplicated_entry(key, subkey1, subkey2, arch, get_configuration, configure_environment, file_monitoring, configure_local_internal_options_module, daemons_handler_module, diff --git a/tests/integration/test_integratord/test_alerts_reading.py b/tests/integration/test_integratord/test_alerts_reading.py index c3507f9ca9..2997e1bffb 100644 --- a/tests/integration/test_integratord/test_alerts_reading.py +++ b/tests/integration/test_integratord/test_alerts_reading.py @@ -55,7 +55,8 @@ from wazuh_testing.modules.integratord import event_monitor as evm -def replace_webhook_url(ids, configurations): +@pytest.fixture(scope='function') +def replace_webhook_url(configuration): '''Replace the Webhook URL in each test case configuration parameters. Args: @@ -65,10 +66,8 @@ def replace_webhook_url(ids, configurations): Returns: configurations (list): List of configurations. ''' - for i in range(0, len(ids)): - configurations[i]['WEBHOOK_URL'] = global_parameters.slack_webhook_url - - return configurations + configuration['WEBHOOK_URL'] = global_parameters.slack_webhook_url + return configuration # Marks @@ -90,11 +89,11 @@ def replace_webhook_url(ids, configurations): t2_config_params, t2_metadata, t2_cases_ids = get_test_cases_data(t2_cases_path) t3_config_params, t3_metadata, t3_cases_ids = get_test_cases_data(t3_cases_path) -t1_config_params = replace_webhook_url(t1_cases_ids, t1_config_params) -t2_config_params = replace_webhook_url(t2_cases_ids, t2_config_params) -t3_config_params = replace_webhook_url(t3_cases_ids, t3_config_params) - # Load tests configurations +# print(len(t1_config_params)) +# print(len(t1_metadata)) +# print(load_configuration_template(configurations_template, t1_config_params, t1_metadata)) + t1_config = load_configuration_template(configurations_template, t1_config_params, t1_metadata) t2_config = load_configuration_template(configurations_template, t2_config_params, t2_metadata) t3_config = load_configuration_template(configurations_template, t3_config_params, t3_metadata) @@ -108,8 +107,9 @@ def replace_webhook_url(ids, configurations): # Tests @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t1_config, t1_metadata), ids=t1_cases_ids) -def test_integratord_change_json_inode(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, - configure_local_internal_options_module, daemons_handler_function, +def test_integratord_change_json_inode(configuration, metadata, replace_webhook_url, set_wazuh_configuration, + truncate_monitored_files, configure_local_internal_options_module, + daemons_handler_function, wait_for_start_module): ''' description: Check that wazuh-integratord detects a change in the inode of the alerts.json and continues reading From 20decee7aa42c67b54aa1ba68768b54d5500ac53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 17:12:54 +0100 Subject: [PATCH 23/56] feat: skip integratord if no webhooks is provided --- .../integration/test_integratord/conftest.py | 26 +++++++++++++++- .../cases_integratord_change_inode_alert.yaml | 2 +- ..._integratord_read_invalid_json_alerts.yaml | 4 +-- ...es_integratord_read_valid_json_alerts.yaml | 2 +- .../test_integratord/test_alerts_reading.py | 31 +++++-------------- tests/performance/test_api/conftest.py | 3 +- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/tests/integration/test_integratord/conftest.py b/tests/integration/test_integratord/conftest.py index 64db4af575..a12ba42d02 100644 --- a/tests/integration/test_integratord/conftest.py +++ b/tests/integration/test_integratord/conftest.py @@ -5,7 +5,7 @@ ''' import pytest -from wazuh_testing import T_5 +from wazuh_testing import T_5, WAZUH_CONF_PATH, global_parameters from wazuh_testing.tools import LOG_FILE_PATH from wazuh_testing.tools.monitoring import FileMonitor from wazuh_testing.modules import analysisd @@ -13,6 +13,9 @@ from wazuh_testing.modules.integratord import event_monitor as evm +webhook_placeholder = 'WEBHOOK_URL' + + @pytest.fixture(scope='function') def wait_for_start_module(request): # Wait for integratord thread to start @@ -23,3 +26,24 @@ def wait_for_start_module(request): check_analysisd_event(file_monitor=file_monitor, timeout=T_5, callback=analysisd.CB_ANALYSISD_STARTUP_COMPLETED, error_message=analysisd.ERR_MSG_STARTUP_COMPLETED_NOT_FOUND) + + +@pytest.fixture(scope='package') +def validate_slack_hook(): + """Validate the slack hook is defined.""" + + if not hasattr(global_parameters, 'slack_webhook_url'): + pytest.skip('Slack webhook URL not defined.') + + +@pytest.fixture(scope='function') +def replace_placeholder_slack_hook(): + """Replace the placeholder slack hook with the real one.""" + + with open(WAZUH_CONF_PATH, 'r') as f: + configuration = f.read() + + configuration = configuration.replace(webhook_placeholder, global_parameters.slack_webhook_url) + + with open(WAZUH_CONF_PATH, 'w') as f: + f.write(configuration) diff --git a/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml b/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml index c3b3691da9..5c7f710144 100644 --- a/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml +++ b/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml @@ -1,7 +1,7 @@ - name: cannot_read_alerts_file_inode_changed description: The alerts.json file inode has changed and it cannot read alerts from it until it reloads. configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000","rule":{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", diff --git a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml index 65c2c501f3..a26eb2945b 100644 --- a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml +++ b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml @@ -1,7 +1,7 @@ - name: read_invalid_json_alert description: Read a invalid alert from alerts.json - removed rule key name - Integration fails configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000",:{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", @@ -25,7 +25,7 @@ - name: read_overlong_json_alert description: Read a an alert that is over 64kb alert from alerts.json - Integration fails configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000","rule":{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", diff --git a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml index 8ee984321a..a136cc68b8 100644 --- a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml +++ b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml @@ -1,7 +1,7 @@ - name: read_valid_json_alert description: Read a valid alert from alerts.json configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000","rule":{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", diff --git a/tests/integration/test_integratord/test_alerts_reading.py b/tests/integration/test_integratord/test_alerts_reading.py index 2997e1bffb..3f2e8f646d 100644 --- a/tests/integration/test_integratord/test_alerts_reading.py +++ b/tests/integration/test_integratord/test_alerts_reading.py @@ -55,21 +55,6 @@ from wazuh_testing.modules.integratord import event_monitor as evm -@pytest.fixture(scope='function') -def replace_webhook_url(configuration): - '''Replace the Webhook URL in each test case configuration parameters. - - Args: - ids (list): List of ids of test cases. - configurations (list): List of test's configuration parameters. - - Returns: - configurations (list): List of configurations. - ''' - configuration['WEBHOOK_URL'] = global_parameters.slack_webhook_url - return configuration - - # Marks pytestmark = [pytest.mark.server] @@ -90,10 +75,6 @@ def replace_webhook_url(configuration): t3_config_params, t3_metadata, t3_cases_ids = get_test_cases_data(t3_cases_path) # Load tests configurations -# print(len(t1_config_params)) -# print(len(t1_metadata)) -# print(load_configuration_template(configurations_template, t1_config_params, t1_metadata)) - t1_config = load_configuration_template(configurations_template, t1_config_params, t1_metadata) t2_config = load_configuration_template(configurations_template, t2_config_params, t2_metadata) t3_config = load_configuration_template(configurations_template, t3_config_params, t3_metadata) @@ -107,8 +88,9 @@ def replace_webhook_url(configuration): # Tests @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t1_config, t1_metadata), ids=t1_cases_ids) -def test_integratord_change_json_inode(configuration, metadata, replace_webhook_url, set_wazuh_configuration, - truncate_monitored_files, configure_local_internal_options_module, +def test_integratord_change_json_inode(validate_slack_hook, configuration, metadata, set_wazuh_configuration, + replace_placeholder_slack_hook, truncate_monitored_files, + configure_local_internal_options_module, daemons_handler_function, wait_for_start_module): ''' @@ -209,7 +191,9 @@ def test_integratord_change_json_inode(configuration, metadata, replace_webhook_ @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t2_config, t2_metadata), ids=t2_cases_ids) -def test_integratord_read_valid_alerts(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_valid_alerts(validate_slack_hook, configuration, metadata, set_wazuh_configuration, + replace_placeholder_slack_hook, + truncate_monitored_files, configure_local_internal_options_module, daemons_handler_function, wait_for_start_module): ''' @@ -278,7 +262,8 @@ def test_integratord_read_valid_alerts(configuration, metadata, set_wazuh_config @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t3_config, t3_metadata), ids=t3_cases_ids) -def test_integratord_read_invalid_alerts(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_invalid_alerts(validate_slack_hook, configuration, metadata, set_wazuh_configuration, + replace_placeholder_slack_hook, truncate_monitored_files, configure_local_internal_options_module, daemons_handler_function, wait_for_start_module): ''' diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index 5d45d91cb3..f3b11884b2 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -174,4 +174,5 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) + if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) From 7bea6955ccfae2dc54dd9d53aad3b7a07708e805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Wed, 28 Jun 2023 10:49:09 +0200 Subject: [PATCH 24/56] fix: internal option fixtures --- .../test_agentd_parametrized_reconnections.py | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 1fa16fe17f..25fd8ce93d 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -61,7 +61,7 @@ import pytest from time import sleep -from wazuh_testing.tools import WAZUH_PATH, LOG_FILE_PATH +from wazuh_testing.tools import WAZUH_PATH, LOG_FILE_PATH, WAZUH_LOCAL_INTERNAL_OPTIONS from wazuh_testing.tools.authd_sim import AuthdSimulator from wazuh_testing.tools.configuration import load_wazuh_configurations from wazuh_testing.tools.file import truncate_file @@ -115,22 +115,27 @@ def teardown(): remoted_server.stop() -@pytest.fixture(scope="module", autouse=True) +@pytest.fixture(scope="module") def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" + + new_timeout = 'agent.recv_timeout=' + timeout + if platform.system() == 'win32' or platform.system() == 'Windows': - local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') debug_line = 'windows.debug=2\n' else: - local_int_conf_path = os.path.join(WAZUH_PATH, 'etc', 'local_internal_options.conf') debug_line = 'agent.debug=2\n' - with open(local_int_conf_path) as local_file_read: - lines = local_file_read.readlines() - for line in lines: - if line == debug_line: - return - with open(local_int_conf_path, 'a') as local_file_write: - local_file_write.write('\n' + debug_line) + + with open(WAZUH_LOCAL_INTERNAL_OPTIONS, 'r') as local_file_read: + backup_configuration = local_file_read.read() + + with open(WAZUH_LOCAL_INTERNAL_OPTIONS, 'w+') as local_file_read: + local_file_read.write(debug_line + new_timeout) + + yield + + with open(WAZUH_LOCAL_INTERNAL_OPTIONS, 'w+') as local_file_read: + local_file_read.write(backup_configuration) # fixtures @@ -258,27 +263,6 @@ def wait_unable_to_connect(line): return None -@pytest.fixture(scope="module") -def change_timeout(): - """Set agent.recv_timeout for agentd in local internal options file. - - The above option sets the maximum number of seconds to wait - for server response from the TCP client socket. - """ - new_timeout = 'agent.recv_timeout=' + timeout - if platform.system() == 'win32' or platform.system() == 'Windows': - local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') - else: - local_int_conf_path = os.path.join(WAZUH_PATH, 'etc', 'local_internal_options.conf') - with open(local_int_conf_path, 'r') as local_file_read: - lines = local_file_read.readlines() - for line in lines: - if line == new_timeout: - return - with open(local_int_conf_path, 'a') as local_file_write: - local_file_write.write('\n' + new_timeout) - - def parse_time_from_log_line(log_line): """Create a datetime object from a date in a string. @@ -306,7 +290,7 @@ def parse_time_from_log_line(log_line): """ -def test_agentd_parametrized_reconnections(configure_authd_server, start_authd, stop_agent, set_keys, +def test_agentd_parametrized_reconnections(set_debug_mode, configure_authd_server, start_authd, stop_agent, set_keys, configure_environment, get_configuration, teardown): ''' description: Check how the agent behaves when there are delays between connection From 22ff92e62412cca92138dab40d2f5a21b7bf0267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 10:40:57 +0100 Subject: [PATCH 25/56] fix: removed deselection debugging fixture Co-authored-by: mauromalara --- tests/integration/conftest.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index ccf9eb5559..4116b9cbe1 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -1300,13 +1300,3 @@ def truncate_event_logs(): for log_file in log_files: truncate_file(log_file) - - -def pytest_deselected(items): - if not items: - return - config = items[0].session.config - reporter = config.pluginmanager.getplugin("terminalreporter") - reporter.ensure_newline() - for item in items: - reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) From 1f5ab150e6380e0191c1cd5b12703e4c5281fb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 10:47:40 +0100 Subject: [PATCH 26/56] fix: rename collection action task Co-authored-by: mauromalara --- .github/workflows/pytest_tests_collection.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index 219e6c0604..a6a00dcf9f 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -21,7 +21,7 @@ jobs: - name: Install dependencies run: pip install -r requirements.txt - - name: Install dependencies + - name: Install the QA framework run: python3 setup.py install working-directory: deps/wazuh_testing From bb079ab9b6ba14acf592b62cbdfbefc7241222a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 11:15:31 +0100 Subject: [PATCH 27/56] fix: include comment for modifyitems hook Co-authored-by: mauromalara --- tests/integration/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 4116b9cbe1..9afd8e0a2b 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -57,6 +57,7 @@ def get_report_files(): def pytest_collection_modifyitems(session, config, items): selected_tests = [] deselected_tests = [] + # Ignore platform deselection if avoid-platform-based-deselection is used if not global_parameters.avoid_platform_based_deselection: for item in items: supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) From 9594d0fdc7c6d618d5091c033666b4d50b0306d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 11:32:27 +0100 Subject: [PATCH 28/56] docs: include docstring to get_version/get_service Co-authored-by: mauromalara --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 6d42379944..678da556e5 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -91,6 +91,11 @@ def get_version(): + """Get Wazuh version + + Returns: + str: Wazuh version. If it is not possible to get it, it returns 'N/A' + """ try: if platform.system() in ['Windows', 'win32']: with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f: @@ -105,6 +110,11 @@ def get_version(): def get_service(): + """Get Wazuh installed component + + Returns: + str: Wazuh installed component, wazuh-manager or wazuh-agent. If it is not possible to get it, it returns 'N/A' + """ try: if platform.system() in ['Windows', 'win32']: if os.path.exists(WAZUH_PATH): From 2b7e16399b6a15e0872e70e925fea971503fda75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 11:45:43 +0100 Subject: [PATCH 29/56] fix: test collection github action --- .github/workflows/pytest_tests_collection.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index a6a00dcf9f..abbd24957e 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -2,8 +2,10 @@ name: Tests collection on: pull_request: - paths: - - tests/** + types: + - opened + - ready_for_review + - synchronize jobs: test-collection: From fb9b65feedd0eba5234569516061cd485ebd024b Mon Sep 17 00:00:00 2001 From: Julia Date: Thu, 7 Sep 2023 14:13:16 +0200 Subject: [PATCH 30/56] fix(#3969): pytest env errors --- .../wazuh_testing/tools/__init__.py | 37 +++++---- .../wazuh_testing/tools/utils.py | 9 ++- tests/integration/conftest.py | 78 ++++++++++++------- .../test_agentd_parametrized_reconnections.py | 21 +++-- .../test_agentd/test_agentd_reconnection.py | 15 +--- tests/integration/test_analysisd/__init__.py | 0 .../test_analysisd/test_limit_eps/__init__.py | 0 tests/integration/test_gcloud/__init__.py | 0 .../test_configuration/__init__.py | 0 .../test_functionality/__init__.py | 0 .../test_functionality/test_interval.py | 6 +- .../test_functionality/test_max_messages.py | 5 +- .../test_functionality/test_rules.py | 5 +- tests/integration/test_github/__init__.py | 0 .../test_configuration/__init__.py | 0 tests/integration/test_wpk/test_wpk_agent.py | 43 +++++----- .../integration/test_wpk/test_wpk_manager.py | 8 +- .../test_wpk/test_wpk_manager_task_states.py | 4 +- tests/performance/test_api/conftest.py | 3 +- tests/performance/test_cluster/conftest.py | 2 +- .../test_cluster_logs/conftest.py | 2 +- 21 files changed, 136 insertions(+), 102 deletions(-) create mode 100644 tests/integration/test_analysisd/__init__.py create mode 100644 tests/integration/test_analysisd/test_limit_eps/__init__.py create mode 100644 tests/integration/test_gcloud/__init__.py create mode 100644 tests/integration/test_gcloud/test_configuration/__init__.py create mode 100644 tests/integration/test_gcloud/test_functionality/__init__.py create mode 100644 tests/integration/test_github/__init__.py create mode 100644 tests/integration/test_github/test_configuration/__init__.py diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 52f543128d..4af28e3471 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -91,28 +91,33 @@ def get_version(): + try: + if platform.system() in ['Windows', 'win32']: + with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f: + version = f.read() + return version[:version.rfind('\n')] - if platform.system() in ['Windows', 'win32']: - with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f: - version = f.read() - return version[:version.rfind('\n')] - - else: # Linux, sunos5, darwin, aix... - return subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v" - ], stderr=subprocess.PIPE).decode('utf-8').rstrip() + else: # Linux, sunos5, darwin, aix... + return subprocess.check_output([ + f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v" + ], stderr=subprocess.PIPE).decode('utf-8').rstrip() + except Exception: + return 'N/A' def get_service(): if platform.system() in ['Windows', 'win32']: - return 'wazuh-agent' - + service = 'wazuh-agent' else: # Linux, sunos5, darwin, aix... - service = subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t" - ], stderr=subprocess.PIPE).decode('utf-8').strip() - - return 'wazuh-manager' if service == 'server' else 'wazuh-agent' + try: + output = subprocess.check_output([ + f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t" + ], stderr=subprocess.PIPE).decode('utf-8').strip() + service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' + except Exception: + service = '' + + return service _data_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'data') diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index 346bfddb87..e5f22d73ee 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,9 +131,12 @@ def get_random_string(string_length, digits=True): def get_version(): - f = open('../../version.json') - data = json.load(f) - version = data['version'] + try: + f = open('../../version.json') + data = json.load(f) + version = data['version'] + except Exception: + version = 'N/A' return version diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 258b236b52..0a394789fb 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -57,36 +57,39 @@ def get_report_files(): def pytest_collection_modifyitems(session, config, items): selected_tests = [] deselected_tests = [] - - for item in items: - supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) - plat = sys.platform - - selected = True - if supported_platforms and plat not in supported_platforms: - selected = False - - host_type = 'agent' if 'agent' in get_service() else 'server' - supported_types = HOST_TYPES.intersection(mark.name for mark in item.iter_markers()) - if supported_types and host_type not in supported_types: - selected = False - # Consider only first mark - levels = [mark.kwargs['level'] for mark in item.iter_markers(name="tier")] - if levels and len(levels) > 0: - tiers = item.config.getoption("--tier") - if tiers is not None and levels[0] not in tiers: + print(session) + print(config) + if not global_parameters.allow_platform_deselected_tests: + for item in items: + supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) + plat = sys.platform + + selected = True + if supported_platforms and plat not in supported_platforms: selected = False - elif item.config.getoption("--tier-minimum") > levels[0]: - selected = False - elif item.config.getoption("--tier-maximum") < levels[0]: + + host_type = 'agent' if 'agent' in get_service() else 'server' + + supported_types = HOST_TYPES.intersection(mark.name for mark in item.iter_markers()) + if supported_types and host_type not in supported_types: selected = False - if selected: - selected_tests.append(item) - else: - deselected_tests.append(item) + # Consider only first mark + levels = [mark.kwargs['level'] for mark in item.iter_markers(name="tier")] + if levels and len(levels) > 0: + tiers = item.config.getoption("--tier") + if tiers is not None and levels[0] not in tiers: + selected = False + elif item.config.getoption("--tier-minimum") > levels[0]: + selected = False + elif item.config.getoption("--tier-maximum") < levels[0]: + selected = False + if selected: + selected_tests.append(item) + else: + deselected_tests.append(item) - config.hook.pytest_deselected(items=deselected_tests) - items[:] = selected_tests + config.hook.pytest_deselected(items=deselected_tests) + items[:] = selected_tests @pytest.fixture(scope='module') @@ -253,6 +256,7 @@ def pytest_addoption(parser): parser.addoption( "--fim-database-memory", action="store_true", + default=False, help="run tests activating database memory in the syscheck configuration" ) parser.addoption( @@ -336,6 +340,12 @@ def pytest_addoption(parser): help="pass webhook url required for integratord tests." ) + parser.addoption( + "--allow-platform-deselected-tests", + action="store_true", + default=False, + help="Avoid tests deselection based on current environment" + ) def pytest_configure(config): # Register an additional marker @@ -405,6 +415,10 @@ def pytest_configure(config): if global_parameters.wpk_package_path: global_parameters.wpk_package_path = global_parameters.wpk_package_path + # Set collect test mode + global_parameters.allow_platform_deselected_tests = config.getoption("--allow-platform-deselected-tests") + + def pytest_html_results_table_header(cells): cells.insert(4, html.th('Tier', class_='sortable tier', col='tier')) @@ -1288,3 +1302,13 @@ def truncate_event_logs(): for log_file in log_files: truncate_file(log_file) + + +def pytest_deselected(items): + if not items: + return + config = items[0].session.config + reporter = config.pluginmanager.getplugin("terminalreporter") + reporter.ensure_newline() + for item in items: + reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) \ No newline at end of file diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 71a2e53e72..12640d9ada 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -72,8 +72,12 @@ # Marks pytestmark = [pytest.mark.linux, pytest.mark.win32, pytest.mark.tier(level=0), pytest.mark.agent] + test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') configurations_path = os.path.join(test_data_path, 'wazuh_conf.yaml') +global timeout +timeout = '5' + params = [ # Different parameters on UDP @@ -112,6 +116,7 @@ def teardown(): remoted_server.stop() +@pytest.fixture(scope="module", autouse=True) def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" if platform.system() == 'win32' or platform.system() == 'Windows': @@ -129,9 +134,6 @@ def set_debug_mode(): local_file_write.write('\n' + debug_line) -set_debug_mode() - - # fixtures @pytest.fixture(scope="module", params=configurations, ids=case_ids) def get_configuration(request): @@ -256,17 +258,15 @@ def wait_unable_to_connect(line): return line return None - -def change_timeout(new_value): +@pytest.fixture(scope="module") +def change_timeout(): """Set agent.recv_timeout for agentd in local internal options file. The above option sets the maximum number of seconds to wait for server response from the TCP client socket. - - Args: - new_value (int): Number of seconds (between 1 and 600). """ - new_timeout = 'agent.recv_timeout=' + new_value + global timeout + new_timeout = 'agent.recv_timeout=' + timeout if platform.system() == 'win32' or platform.system() == 'Windows': local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') else: @@ -280,9 +280,6 @@ def change_timeout(new_value): local_file_write.write('\n' + new_timeout) -change_timeout('5') - - def parse_time_from_log_line(log_line): """Create a datetime object from a date in a string. diff --git a/tests/integration/test_agentd/test_agentd_reconnection.py b/tests/integration/test_agentd/test_agentd_reconnection.py index 5a36b1c4a0..3a1f655dd6 100644 --- a/tests/integration/test_agentd/test_agentd_reconnection.py +++ b/tests/integration/test_agentd/test_agentd_reconnection.py @@ -86,18 +86,14 @@ {'PROTOCOL': 'udp'} ] -config_ids = ['tcp', 'udp'] - -configurations = load_wazuh_configurations(configurations_path, __name__, params=params, metadata=metadata) - log_monitor_paths = [] - receiver_sockets_params = [] - monitored_sockets_params = [] -receiver_sockets, monitored_sockets, log_monitors = None, None, None # Set in the fixtures +config_ids = ['tcp', 'udp'] +configurations = load_wazuh_configurations(configurations_path, __name__, params=params, metadata=metadata) +receiver_sockets, monitored_sockets, log_monitors = None, None, None # Set in the fixtures authd_server = AuthdSimulator(params[0]['SERVER_ADDRESS'], key_path=SERVER_KEY_PATH, cert_path=SERVER_CERT_PATH) global remoted_server @@ -111,7 +107,7 @@ def teardown(): if remoted_server is not None: remoted_server.stop() - +@pytest.fixture(scope="module", autouse=True) def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" if platform.system() == 'win32' or platform.system() == 'Windows': @@ -130,9 +126,6 @@ def set_debug_mode(): local_file_write.write('\n' + debug_line) -set_debug_mode() - - # fixtures @pytest.fixture(scope="module", params=configurations, ids=config_ids) def get_configuration(request): diff --git a/tests/integration/test_analysisd/__init__.py b/tests/integration/test_analysisd/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_analysisd/test_limit_eps/__init__.py b/tests/integration/test_analysisd/test_limit_eps/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/__init__.py b/tests/integration/test_gcloud/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/test_configuration/__init__.py b/tests/integration/test_gcloud/test_configuration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/test_functionality/__init__.py b/tests/integration/test_gcloud/test_functionality/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_gcloud/test_functionality/test_interval.py b/tests/integration/test_gcloud/test_functionality/test_interval.py index 657a4dc4b0..3d5365e953 100644 --- a/tests/integration/test_gcloud/test_functionality/test_interval.py +++ b/tests/integration/test_gcloud/test_functionality/test_interval.py @@ -95,7 +95,11 @@ # Preparing -truncate_file(LOG_FILE_PATH) +@pytest.fixture(scope='module', autouse=True) +def truncate_logs(): + """Truncate the 'ossec.log' file.""" + truncate_file(LOG_FILE_PATH) + # fixtures diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index b8040a73cc..cbfd4336e1 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -102,7 +102,10 @@ # Preparing -truncate_file(LOG_FILE_PATH) +@pytest.fixture(scope='module', autouse=True) +def truncate_logs(): + """Truncate the 'ossec.log' file.""" + truncate_file(LOG_FILE_PATH) # fixtures diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index e28dcf3d32..79affa7176 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -93,7 +93,10 @@ # Preparing -truncate_file(LOG_FILE_PATH) +@pytest.fixture(scope='module', params=configurations) +def get_configuration(request): + """Get configurations from the module.""" + return request.param # fixtures diff --git a/tests/integration/test_github/__init__.py b/tests/integration/test_github/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_github/test_configuration/__init__.py b/tests/integration/test_github/test_configuration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integration/test_wpk/test_wpk_agent.py b/tests/integration/test_wpk/test_wpk_agent.py index 97aa68fc18..e9c52fa795 100644 --- a/tests/integration/test_wpk/test_wpk_agent.py +++ b/tests/integration/test_wpk/test_wpk_agent.py @@ -93,26 +93,29 @@ mark_skip_agentLinux = pytest.mark.skipif(get_service() == 'wazuh-agent' and sys_platform == 'Linux', reason="It will be blocked by wazuh/wazuh#9763") -if not global_parameters.wpk_version: - raise Exception("The WPK package version must be defined by parameter. See README.md") -if global_parameters.wpk_package_path is None: - raise ValueError("The WPK package path must be defined by parameter. See README.md") - -version_to_upgrade = global_parameters.wpk_version[0] -package_path = global_parameters.wpk_package_path[0] - -_agent_version = get_version() - -error_msg = '' -ver_split = _agent_version.replace("v", "").split(".") -if int(ver_split[0]) >= 4 and int(ver_split[1]) >= 1: - error_msg = 'Could not chmod' \ - if sys_platform != "Windows" else \ - 'Error executing command' -else: - error_msg = 'err Could not chmod' \ - if sys_platform != "Windows" else \ - 'err Cannot execute installer' + +try: + version_to_upgrade = global_parameters.wpk_version[0] + package_path = global_parameters.wpk_package_path[0] + _agent_version = get_version() + + error_msg = '' + ver_split = _agent_version.replace("v", "").split(".") + if int(ver_split[0]) >= 4 and int(ver_split[1]) >= 1: + error_msg = 'Could not chmod' \ + if sys_platform != "Windows" else \ + 'Error executing command' + else: + error_msg = 'err Could not chmod' \ + if sys_platform != "Windows" else \ + 'err Cannot execute installer' +except Exception as e: + print("Error: {}".format(e)) + print("Using default values") + version_to_upgrade = 'v4.4.0' + error_msg = '' + _agent_version = 'v4.3.0' + package_path = "https://packages.wazuh.com/wpk/linux/x86_64/wazuh_agent_v4.4.0_linux_x86_64.wpk""" time_to_sleep_until_backup = 10 diff --git a/tests/integration/test_wpk/test_wpk_manager.py b/tests/integration/test_wpk/test_wpk_manager.py index c36369e4d7..bfcac57ba0 100644 --- a/tests/integration/test_wpk/test_wpk_manager.py +++ b/tests/integration/test_wpk/test_wpk_manager.py @@ -72,7 +72,7 @@ UPGRADE_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'upgrade') TASK_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'task') SERVER_ADDRESS = 'localhost' -WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] +WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] if global_parameters.wpk_package_path else 'v4.4.0' WPK_REPOSITORY_3x = 'packages.wazuh.com/wpk/' CRYPTO = "aes" CHUNK_SIZE = 16384 @@ -88,12 +88,8 @@ max_upgrade_result_status_retries = 30 -if global_parameters.wpk_version is None: - raise ValueError("The WPK package version must be defined by parameter. See README.md") -if global_parameters.wpk_package_path is None: - raise ValueError("The WPK package path must be defined by parameter. See README.md") -version_to_upgrade = global_parameters.wpk_version[0] +version_to_upgrade = global_parameters.wpk_version[0] if global_parameters.wpk_version else 'v4.4.0' MANAGER_VERSION = get_version() diff --git a/tests/integration/test_wpk/test_wpk_manager_task_states.py b/tests/integration/test_wpk/test_wpk_manager_task_states.py index 28f2fdcf6d..a84e7a87de 100644 --- a/tests/integration/test_wpk/test_wpk_manager_task_states.py +++ b/tests/integration/test_wpk/test_wpk_manager_task_states.py @@ -67,7 +67,9 @@ UPGRADE_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'upgrade') TASK_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'task') SERVER_ADDRESS = 'localhost' -WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] + +WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] if global_parameters.wpk_package_path else 'v4.4.0' + CRYPTO = "aes" CHUNK_SIZE = 16384 TASK_TIMEOUT = '15m' diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index d1324a68f2..44b58bc006 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -173,4 +173,5 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) + if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) \ No newline at end of file diff --git a/tests/performance/test_cluster/conftest.py b/tests/performance/test_cluster/conftest.py index b550384c87..8847f1cad8 100644 --- a/tests/performance/test_cluster/conftest.py +++ b/tests/performance/test_cluster/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--artifacts_path", + "--performance-artifacts_path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." diff --git a/tests/reliability/test_cluster/test_cluster_logs/conftest.py b/tests/reliability/test_cluster/test_cluster_logs/conftest.py index faab49f847..3b56d9f860 100644 --- a/tests/reliability/test_cluster/test_cluster_logs/conftest.py +++ b/tests/reliability/test_cluster/test_cluster_logs/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--artifacts_path", + "--reliability-artifacts-path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." From 84cd7186f5b3be0812e0c70d2fd9a5b677f44ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 20 Feb 2023 18:11:33 +0000 Subject: [PATCH 31/56] fix(#3969): renamed parameter and minnor fixes --- deps/wazuh_testing/wazuh_testing/tools/utils.py | 9 +++------ tests/integration/conftest.py | 10 ++++------ .../test_gcloud/test_functionality/test_rules.py | 9 ++++----- tests/performance/test_api/conftest.py | 2 +- tests/performance/test_cluster/conftest.py | 2 +- .../test_cluster/test_cluster_logs/conftest.py | 2 +- 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index e5f22d73ee..346bfddb87 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,12 +131,9 @@ def get_random_string(string_length, digits=True): def get_version(): - try: - f = open('../../version.json') - data = json.load(f) - version = data['version'] - except Exception: - version = 'N/A' + f = open('../../version.json') + data = json.load(f) + version = data['version'] return version diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 0a394789fb..064832ff1b 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -57,9 +57,7 @@ def get_report_files(): def pytest_collection_modifyitems(session, config, items): selected_tests = [] deselected_tests = [] - print(session) - print(config) - if not global_parameters.allow_platform_deselected_tests: + if not global_parameters.avoid_platform_based_deselection: for item in items: supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) plat = sys.platform @@ -341,7 +339,7 @@ def pytest_addoption(parser): ) parser.addoption( - "--allow-platform-deselected-tests", + "--avoid-platform-based-deselection", action="store_true", default=False, help="Avoid tests deselection based on current environment" @@ -416,7 +414,7 @@ def pytest_configure(config): global_parameters.wpk_package_path = global_parameters.wpk_package_path # Set collect test mode - global_parameters.allow_platform_deselected_tests = config.getoption("--allow-platform-deselected-tests") + global_parameters.avoid_platform_based_deselection = config.getoption("--avoid_platform_based_deselection") @@ -1311,4 +1309,4 @@ def pytest_deselected(items): reporter = config.pluginmanager.getplugin("terminalreporter") reporter.ensure_newline() for item in items: - reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) \ No newline at end of file + reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index 79affa7176..081463ed77 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -92,11 +92,10 @@ # Preparing - -@pytest.fixture(scope='module', params=configurations) -def get_configuration(request): - """Get configurations from the module.""" - return request.param +@pytest.fixture(scope='module', autouse=True) +def truncate_logs(): + """Truncate the 'ossec.log' file.""" + truncate_file(LOG_FILE_PATH) # fixtures diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index 44b58bc006..f745964c04 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -174,4 +174,4 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) \ No newline at end of file + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) diff --git a/tests/performance/test_cluster/conftest.py b/tests/performance/test_cluster/conftest.py index 8847f1cad8..b550384c87 100644 --- a/tests/performance/test_cluster/conftest.py +++ b/tests/performance/test_cluster/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--performance-artifacts_path", + "--artifacts_path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." diff --git a/tests/reliability/test_cluster/test_cluster_logs/conftest.py b/tests/reliability/test_cluster/test_cluster_logs/conftest.py index 3b56d9f860..faab49f847 100644 --- a/tests/reliability/test_cluster/test_cluster_logs/conftest.py +++ b/tests/reliability/test_cluster/test_cluster_logs/conftest.py @@ -11,7 +11,7 @@ def pytest_addoption(parser): # Get command line options parser.addoption( - "--reliability-artifacts-path", + "--artifacts_path", action="store", type=str, help="Path where information of all cluster nodes can be found (logs, stats CSVs, etc)." From b9a63c496422bef6146dc6332935f7b96b951f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 20 Feb 2023 18:22:36 +0000 Subject: [PATCH 32/56] fix(#3969): bad variable name --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 2 +- tests/integration/conftest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 4af28e3471..6e9b7156bc 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -115,7 +115,7 @@ def get_service(): ], stderr=subprocess.PIPE).decode('utf-8').strip() service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' except Exception: - service = '' + service = 'N/A' return service diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 064832ff1b..6740fe9e4c 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -414,7 +414,7 @@ def pytest_configure(config): global_parameters.wpk_package_path = global_parameters.wpk_package_path # Set collect test mode - global_parameters.avoid_platform_based_deselection = config.getoption("--avoid_platform_based_deselection") + global_parameters.avoid_platform_based_deselection = config.getoption("--avoid-platform-based-deselection") From 49fbed76ceff6aafcbeaa849cec832c21053e42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:11:53 +0000 Subject: [PATCH 33/56] style(#3969): fit pep8 --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 10 ++++------ tests/integration/conftest.py | 2 +- .../test_agentd_parametrized_reconnections.py | 1 + .../test_agentd/test_agentd_reconnection.py | 1 + .../test_gcloud/test_functionality/test_interval.py | 2 +- .../test_functionality/test_max_messages.py | 2 +- .../test_gcloud/test_functionality/test_rules.py | 1 - tests/integration/test_wpk/test_wpk_agent.py | 2 +- tests/integration/test_wpk/test_wpk_manager.py | 2 -- .../test_wpk/test_wpk_manager_task_states.py | 7 +++---- tests/performance/test_api/conftest.py | 3 ++- 11 files changed, 15 insertions(+), 18 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 6e9b7156bc..f73e06f54a 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -98,9 +98,8 @@ def get_version(): return version[:version.rfind('\n')] else: # Linux, sunos5, darwin, aix... - return subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v" - ], stderr=subprocess.PIPE).decode('utf-8').rstrip() + return subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v"], + stderr=subprocess.PIPE).decode('utf-8').rstrip() except Exception: return 'N/A' @@ -110,9 +109,8 @@ def get_service(): service = 'wazuh-agent' else: # Linux, sunos5, darwin, aix... try: - output = subprocess.check_output([ - f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t" - ], stderr=subprocess.PIPE).decode('utf-8').strip() + output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], + stderr=subprocess.PIPE).decode('utf-8').strip() service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' except Exception: service = 'N/A' diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 6740fe9e4c..ccf9eb5559 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -345,6 +345,7 @@ def pytest_addoption(parser): help="Avoid tests deselection based on current environment" ) + def pytest_configure(config): # Register an additional marker config.addinivalue_line( @@ -417,7 +418,6 @@ def pytest_configure(config): global_parameters.avoid_platform_based_deselection = config.getoption("--avoid-platform-based-deselection") - def pytest_html_results_table_header(cells): cells.insert(4, html.th('Tier', class_='sortable tier', col='tier')) cells.insert(3, html.th('Markers')) diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 12640d9ada..6b28efa1e0 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -258,6 +258,7 @@ def wait_unable_to_connect(line): return line return None + @pytest.fixture(scope="module") def change_timeout(): """Set agent.recv_timeout for agentd in local internal options file. diff --git a/tests/integration/test_agentd/test_agentd_reconnection.py b/tests/integration/test_agentd/test_agentd_reconnection.py index 3a1f655dd6..a52a8fdb7a 100644 --- a/tests/integration/test_agentd/test_agentd_reconnection.py +++ b/tests/integration/test_agentd/test_agentd_reconnection.py @@ -107,6 +107,7 @@ def teardown(): if remoted_server is not None: remoted_server.stop() + @pytest.fixture(scope="module", autouse=True) def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" diff --git a/tests/integration/test_gcloud/test_functionality/test_interval.py b/tests/integration/test_gcloud/test_functionality/test_interval.py index 3d5365e953..555c3cd142 100644 --- a/tests/integration/test_gcloud/test_functionality/test_interval.py +++ b/tests/integration/test_gcloud/test_functionality/test_interval.py @@ -93,8 +93,8 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing +# Preparing @pytest.fixture(scope='module', autouse=True) def truncate_logs(): """Truncate the 'ossec.log' file.""" diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index cbfd4336e1..1770cd7869 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -100,8 +100,8 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing +# Preparing @pytest.fixture(scope='module', autouse=True) def truncate_logs(): """Truncate the 'ossec.log' file.""" diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index 081463ed77..0157b289c9 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -99,7 +99,6 @@ def truncate_logs(): # fixtures - @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" diff --git a/tests/integration/test_wpk/test_wpk_agent.py b/tests/integration/test_wpk/test_wpk_agent.py index e9c52fa795..b64e870c5a 100644 --- a/tests/integration/test_wpk/test_wpk_agent.py +++ b/tests/integration/test_wpk/test_wpk_agent.py @@ -508,7 +508,7 @@ def test_wpk_agent(get_configuration, prepare_agent_version, download_wpk, remoted_simulator.change_default_listener = True event = wazuh_log_monitor.start(timeout=timeout_ack_response, error_message='ACK event not received', - callback=callback_detect_upgrade_ack_event).result() + callback=callback_detect_upgrade_ack_event).result() result = event['parameters'] if result is not None: diff --git a/tests/integration/test_wpk/test_wpk_manager.py b/tests/integration/test_wpk/test_wpk_manager.py index bfcac57ba0..e311841d65 100644 --- a/tests/integration/test_wpk/test_wpk_manager.py +++ b/tests/integration/test_wpk/test_wpk_manager.py @@ -87,8 +87,6 @@ time_until_ask_upgrade_result = 30 max_upgrade_result_status_retries = 30 - - version_to_upgrade = global_parameters.wpk_version[0] if global_parameters.wpk_version else 'v4.4.0' MANAGER_VERSION = get_version() diff --git a/tests/integration/test_wpk/test_wpk_manager_task_states.py b/tests/integration/test_wpk/test_wpk_manager_task_states.py index a84e7a87de..f6874ce6cb 100644 --- a/tests/integration/test_wpk/test_wpk_manager_task_states.py +++ b/tests/integration/test_wpk/test_wpk_manager_task_states.py @@ -424,10 +424,9 @@ def test_wpk_manager_task_states(get_configuration, configure_environment, # Chech that result of first attempt is Success assert new_expected_response[agents_id.index(agent_id)] == \ - response['data'][0]['message'], \ - f'New upgrade response did not match expected! ' \ - f'Expected {new_expected_response} obtained ' \ - f'{response["data"][0]["message"]}' + response['data'][0]['message'], f'New upgrade response did not match expected! ' \ + f'Expected {new_expected_response} obtained ' \ + f'{response["data"][0]["message"]}' for injector in injectors: injector.stop_receive() diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index f745964c04..f3b11884b2 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -110,7 +110,8 @@ def pytest_html_results_table_row(report, cells): cells[2] = HTMLStyle.colored_td(report.user_properties[0][1]) cells[3] = HTMLStyle.colored_td(str(report.user_properties[2][1])) cells.append(HTMLStyle.colored_td(str(report.user_properties[3][1]))) - cells.append(HTMLStyle.colored_td(u'\u2713' if len(report.user_properties) > 4 and report.user_properties[4][1] else '')) + cells.append(HTMLStyle.colored_td(u'\u2713' if len(report.user_properties) > 4 and report.user_properties[4][1] + else '')) cells.append(HTMLStyle.colored_td(f'{report.duration:.3f} s')) except AttributeError: From 032e9241f43890786394087e5c6976c09ef65a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:14:18 +0000 Subject: [PATCH 34/56] docs(#3969): include 3969 changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a227b50f58..f81d0a0d6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file. Wazuh commit: TBD \ Release report: TBD +### Fixed + +- Fix pytest test collection errors ([#3969](https://github.com/wazuh/wazuh-qa/pull/3969)) \- (Framework + Tests) + ## [4.5.2] - TBD Wazuh commit: TBD \ From 29933f691d8abe2a6cfaf7e02d499c373b07cf19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:15:02 +0000 Subject: [PATCH 35/56] style(#3969): fit pep8 tools module --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index f73e06f54a..de346e9e0d 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -110,7 +110,7 @@ def get_service(): else: # Linux, sunos5, darwin, aix... try: output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], - stderr=subprocess.PIPE).decode('utf-8').strip() + stderr=subprocess.PIPE).decode('utf-8').strip() service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' except Exception: service = 'N/A' From dad023bf9f95794d9f1375a3645d28dc3c5d55d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Tue, 21 Feb 2023 10:26:03 +0000 Subject: [PATCH 36/56] feat(): include tests collection action --- .../workflows/pytest_tests_collection.yaml | 63 +++++++++++++++++++ .../wazuh_testing/tools/utils.py | 9 ++- f | 1 + 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/pytest_tests_collection.yaml create mode 100644 f diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml new file mode 100644 index 0000000000..e6f33fe096 --- /dev/null +++ b/.github/workflows/pytest_tests_collection.yaml @@ -0,0 +1,63 @@ +name: Tests collection + +on: + pull_request: + paths: + - tests/** + +jobs: + setup: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v14.6 + + - id: set-matrix + run: | + files_changed="${{ steps.changed-files.outputs.all_changed_files }}" + DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | uniq | tr -d ' ') + output='matrix=[' + dir_list='' + + for dir in $DIRECTORIES; do + dir_list="$dir_list\"$dir\"," + done + dir_list=${dir_list[@]::-1} + output="$output""$dir_list"' ]' + echo "$output" >> $GITHUB_OUTPUT + - name: Show matrix + run: echo ${{ steps.set-matrix.outputs.matrix }} + + run-pytest: + name: Tests Collection + needs: setup + strategy: + matrix: + value: ${{ fromJson(needs.matrix_prep.outputs.matrix) }} + + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Install dependencies + run: python3 setup.py install + working-directory: deps/wazuh_testing + + - name: Run pytest + run: | + pytest --collect-only tests/${{ matrix.value }} diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index 346bfddb87..e5f22d73ee 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,9 +131,12 @@ def get_random_string(string_length, digits=True): def get_version(): - f = open('../../version.json') - data = json.load(f) - version = data['version'] + try: + f = open('../../version.json') + data = json.load(f) + version = data['version'] + except Exception: + version = 'N/A' return version diff --git a/f b/f new file mode 100644 index 0000000000..37603f9fd5 --- /dev/null +++ b/f @@ -0,0 +1 @@ +matrix=integration performance From 07dda703c9f68b2ee53d63e5ae95aa008f403dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 22 May 2023 09:53:51 +0100 Subject: [PATCH 37/56] fix(#3969): fix gh tests collection --- .github/workflows/pytest_tests_collection.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index e6f33fe096..2886cea274 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -30,7 +30,7 @@ jobs: done dir_list=${dir_list[@]::-1} output="$output""$dir_list"' ]' - echo "$output" >> $GITHUB_OUTPUT + echo "$output" >> $GITHUB_ENV - name: Show matrix run: echo ${{ steps.set-matrix.outputs.matrix }} @@ -39,7 +39,7 @@ jobs: needs: setup strategy: matrix: - value: ${{ fromJson(needs.matrix_prep.outputs.matrix) }} + value: ${{ needs.setup.outputs.matrix }} runs-on: ubuntu-latest steps: From 125591509f58ed36499c2917de2d5d4af013094a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 22 May 2023 10:21:49 +0100 Subject: [PATCH 38/56] refac(#3969): tests collection action --- .../workflows/pytest_tests_collection.yaml | 66 +++++++------------ 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index 2886cea274..6c3670bcc8 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -6,58 +6,40 @@ on: - tests/** jobs: - setup: + test-collection: runs-on: ubuntu-latest - outputs: - matrix: ${{ steps.set-matrix.outputs.matrix }} steps: + - name: Checkout code uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Install dependencies + run: python3 setup.py install + working-directory: deps/wazuh_testing + - name: Get changed files id: changed-files - uses: tj-actions/changed-files@v14.6 + uses: tj-actions/changed-files@v35.9.2 - - id: set-matrix + - name: Get tests modules changed + id: set-changed-modules run: | files_changed="${{ steps.changed-files.outputs.all_changed_files }}" DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | uniq | tr -d ' ') - output='matrix=[' - dir_list='' + DIRECTORIES=$(echo $DIRECTORIES | tr ' ' ',') + echo "matrix=$DIRECTORIES" >> $GITHUB_OUTPUT - for dir in $DIRECTORIES; do - dir_list="$dir_list\"$dir\"," + - name: Collect tests + run: | + DIRECTORIES=$(echo ${{ steps.set-changed-modules.outputs.matrix }} | tr ',' ' ') + for directory in $DIRECTORIES; do + pytest --collect-only tests/${directory} done - dir_list=${dir_list[@]::-1} - output="$output""$dir_list"' ]' - echo "$output" >> $GITHUB_ENV - - name: Show matrix - run: echo ${{ steps.set-matrix.outputs.matrix }} - - run-pytest: - name: Tests Collection - needs: setup - strategy: - matrix: - value: ${{ needs.setup.outputs.matrix }} - - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: 3.8 - - - name: Install dependencies - run: pip install -r requirements.txt - - - name: Install dependencies - run: python3 setup.py install - working-directory: deps/wazuh_testing - - - name: Run pytest - run: | - pytest --collect-only tests/${{ matrix.value }} From 3517a33ce9f2393f36560a899e517b784dbbed29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 22 May 2023 15:08:59 +0100 Subject: [PATCH 39/56] fix(#3969): get service --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index de346e9e0d..70a16f8b56 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -111,7 +111,7 @@ def get_service(): try: output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], stderr=subprocess.PIPE).decode('utf-8').strip() - service = 'wazuh-manager' if service == 'server' else 'wazuh-agent' + service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' except Exception: service = 'N/A' From 5c1447c45e33f0781a9d19d5057195c6c2a55dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:08:19 +0100 Subject: [PATCH 40/56] feat: check if agent is installed in Windows hosts --- .../wazuh_testing/tools/__init__.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 70a16f8b56..83545f0225 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -105,15 +105,18 @@ def get_version(): def get_service(): - if platform.system() in ['Windows', 'win32']: - service = 'wazuh-agent' - else: # Linux, sunos5, darwin, aix... - try: - output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], - stderr=subprocess.PIPE).decode('utf-8').strip() - service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' - except Exception: - service = 'N/A' + try: + if platform.system() in ['Windows', 'win32']: + if os.path.exists(WAZUH_PATH): + service = 'wazuh-agent' + else: + service = 'N/A' + else: # Linux, sunos5, darwin, aix... + output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], + stderr=subprocess.PIPE).decode('utf-8').strip() + service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' + except Exception: + service = 'N/A' return service From 7ec2e0d2ba15c9aebb6f33ad77c221c1fb78b3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:08:53 +0100 Subject: [PATCH 41/56] refactor: remove try/catch logic --- deps/wazuh_testing/wazuh_testing/tools/utils.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index e5f22d73ee..884d2b602c 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -131,12 +131,10 @@ def get_random_string(string_length, digits=True): def get_version(): - try: - f = open('../../version.json') - data = json.load(f) - version = data['version'] - except Exception: - version = 'N/A' + f = open('../../version.json') + data = json.load(f) + version = data['version'] + return version From b9e2df8dc5f87bd2738b8059d084b222d4fde2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:09:13 +0100 Subject: [PATCH 42/56] refactor: remove timeout global variable --- .../test_agentd/test_agentd_parametrized_reconnections.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 6b28efa1e0..1fa16fe17f 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -75,7 +75,6 @@ test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') configurations_path = os.path.join(test_data_path, 'wazuh_conf.yaml') -global timeout timeout = '5' @@ -266,7 +265,6 @@ def change_timeout(): The above option sets the maximum number of seconds to wait for server response from the TCP client socket. """ - global timeout new_timeout = 'agent.recv_timeout=' + timeout if platform.system() == 'win32' or platform.system() == 'Windows': local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') From a1784673c0fe975442572c2cce04ba5d584b913f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:09:42 +0100 Subject: [PATCH 43/56] refactor: remove truncate log fixture --- .../test_gcloud/test_functionality/test_interval.py | 8 +------- .../test_functionality/test_max_messages.py | 11 ----------- .../test_gcloud/test_functionality/test_rules.py | 1 - 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_interval.py b/tests/integration/test_gcloud/test_functionality/test_interval.py index 555c3cd142..5e341705f2 100644 --- a/tests/integration/test_gcloud/test_functionality/test_interval.py +++ b/tests/integration/test_gcloud/test_functionality/test_interval.py @@ -93,13 +93,9 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) - # Preparing -@pytest.fixture(scope='module', autouse=True) -def truncate_logs(): - """Truncate the 'ossec.log' file.""" - truncate_file(LOG_FILE_PATH) +truncate_file(LOG_FILE_PATH) # fixtures @@ -110,8 +106,6 @@ def get_configuration(request): return request.param -# tests - @pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have support for Google Cloud integration.") def test_interval(get_configuration, configure_environment, reset_ossec_log, daemons_handler_module, wait_for_gcp_start): diff --git a/tests/integration/test_gcloud/test_functionality/test_max_messages.py b/tests/integration/test_gcloud/test_functionality/test_max_messages.py index 1770cd7869..292852e40f 100644 --- a/tests/integration/test_gcloud/test_functionality/test_max_messages.py +++ b/tests/integration/test_gcloud/test_functionality/test_max_messages.py @@ -101,23 +101,12 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing -@pytest.fixture(scope='module', autouse=True) -def truncate_logs(): - """Truncate the 'ossec.log' file.""" - truncate_file(LOG_FILE_PATH) - -# fixtures - - @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" return request.param -# tests - @pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have support for Google Cloud integration.") @pytest.mark.parametrize('publish_messages', [ ['- DEBUG - GCP message' for _ in range(30)], diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index 0157b289c9..153edea40b 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -98,7 +98,6 @@ def truncate_logs(): truncate_file(LOG_FILE_PATH) -# fixtures @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" From b17254f93338cc54974f728d4392423b0bf4ba7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:10:31 +0100 Subject: [PATCH 44/56] style: remove extra whitespaces --- tests/integration/test_wpk/test_wpk_manager_task_states.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/test_wpk/test_wpk_manager_task_states.py b/tests/integration/test_wpk/test_wpk_manager_task_states.py index f6874ce6cb..7dcfca4a91 100644 --- a/tests/integration/test_wpk/test_wpk_manager_task_states.py +++ b/tests/integration/test_wpk/test_wpk_manager_task_states.py @@ -67,9 +67,7 @@ UPGRADE_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'upgrade') TASK_SOCKET = os.path.join(WAZUH_PATH, 'queue', 'tasks', 'task') SERVER_ADDRESS = 'localhost' - WPK_REPOSITORY_4x = global_parameters.wpk_package_path[0] if global_parameters.wpk_package_path else 'v4.4.0' - CRYPTO = "aes" CHUNK_SIZE = 16384 TASK_TIMEOUT = '15m' From 3e4f69eb85af5b174a798d28f0f7bc02e4a8efbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:11:04 +0100 Subject: [PATCH 45/56] refactor: remove unnecessary if conditional --- tests/performance/test_api/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index f3b11884b2..5d45d91cb3 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -174,5 +174,4 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: - if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) From 6221722b623a428ca50a55ec95fe9c2a57512692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:17:26 +0100 Subject: [PATCH 46/56] refactor: remove extra file --- f | 1 - 1 file changed, 1 deletion(-) delete mode 100644 f diff --git a/f b/f deleted file mode 100644 index 37603f9fd5..0000000000 --- a/f +++ /dev/null @@ -1 +0,0 @@ -matrix=integration performance From d9d5f2301872a00d61aee74aa6431c33ec994687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:19:58 +0100 Subject: [PATCH 47/56] refactor: remove truncate log fixture --- .../test_gcloud/test_functionality/test_rules.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/integration/test_gcloud/test_functionality/test_rules.py b/tests/integration/test_gcloud/test_functionality/test_rules.py index 153edea40b..9cf0afcc82 100644 --- a/tests/integration/test_gcloud/test_functionality/test_rules.py +++ b/tests/integration/test_gcloud/test_functionality/test_rules.py @@ -91,13 +91,6 @@ configurations = load_wazuh_configurations(configurations_path, __name__, params=p, metadata=m) -# Preparing -@pytest.fixture(scope='module', autouse=True) -def truncate_logs(): - """Truncate the 'ossec.log' file.""" - truncate_file(LOG_FILE_PATH) - - @pytest.fixture(scope='module', params=configurations) def get_configuration(request): """Get configurations from the module.""" From 65ac11bea49b1d6c00303d4cc57cbeb545cd07ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 10:28:51 +0100 Subject: [PATCH 48/56] style: fix pep8 --- .github/workflows/pytest_tests_collection.yaml | 5 +++-- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 6 +++--- deps/wazuh_testing/wazuh_testing/tools/utils.py | 5 +++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index 6c3670bcc8..219e6c0604 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -33,7 +33,8 @@ jobs: id: set-changed-modules run: | files_changed="${{ steps.changed-files.outputs.all_changed_files }}" - DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | uniq | tr -d ' ') + DIRECTORIES=$(echo $files_changed | tr ' ' '\n' | grep ^tests | grep -v pytest.ini | cut -d/ -f2 | \ + uniq | tr -d ' ') DIRECTORIES=$(echo $DIRECTORIES | tr ' ' ',') echo "matrix=$DIRECTORIES" >> $GITHUB_OUTPUT @@ -41,5 +42,5 @@ jobs: run: | DIRECTORIES=$(echo ${{ steps.set-changed-modules.outputs.matrix }} | tr ',' ' ') for directory in $DIRECTORIES; do - pytest --collect-only tests/${directory} + pytest --collect-only tests/${directory} done diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 83545f0225..6d42379944 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -112,9 +112,9 @@ def get_service(): else: service = 'N/A' else: # Linux, sunos5, darwin, aix... - output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], - stderr=subprocess.PIPE).decode('utf-8').strip() - service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' + output = subprocess.check_output([f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"], + stderr=subprocess.PIPE).decode('utf-8').strip() + service = 'wazuh-manager' if output == 'server' else 'wazuh-agent' except Exception: service = 'N/A' diff --git a/deps/wazuh_testing/wazuh_testing/tools/utils.py b/deps/wazuh_testing/wazuh_testing/tools/utils.py index 884d2b602c..3b17d51fd3 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/utils.py +++ b/deps/wazuh_testing/wazuh_testing/tools/utils.py @@ -163,12 +163,13 @@ def get_host_name(): def validate_interval_format(interval): """Validate that the interval passed has the format in which the last digit is a letter from those passed and the other characters are between 0-9""" - if interval=='': + if interval == '': return False - if interval[-1] not in ['s','m', 'h','d','w','y'] or not isinstance(int(interval[0:-1]), numbers.Number): + if interval[-1] not in ['s', 'm', 'h', 'd', 'w', 'y'] or not isinstance(int(interval[0:-1]), numbers.Number): return False return True + def format_ipv6_long(ipv6_address): """Return the long form of the address representation in uppercase. From da2f76b541f292e20801271db5a69e907048b7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 15:00:27 +0100 Subject: [PATCH 49/56] fix: tests collection integratord fim --- ...basic_usage_registry_duplicated_entries.py | 2 -- .../test_integratord/test_alerts_reading.py | 22 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py b/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py index bec0a8c9d6..b9ec66a79b 100644 --- a/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py +++ b/tests/integration/test_fim/test_registry/test_registry_basic_usage/test_basic_usage_registry_duplicated_entries.py @@ -4,7 +4,6 @@ from wazuh_testing import global_parameters import wazuh_testing.fim as fim from wazuh_testing.tools.configuration import load_wazuh_configurations -from wazuh_testing.tools.utils import get_version # Helper functions @@ -62,7 +61,6 @@ def get_configuration(request): # Test -@pytest.mark.skipif(get_version() != 'v4.2.3', reason="This test fails by wazuh/wazuh#6797, It was fixed on v4.2.3") @pytest.mark.parametrize('key, subkey1, subkey2, arch', [(key, sub_key_1, sub_key_2, fim.KEY_WOW64_32KEY)]) def test_registry_duplicated_entry(key, subkey1, subkey2, arch, get_configuration, configure_environment, file_monitoring, configure_local_internal_options_module, daemons_handler_module, diff --git a/tests/integration/test_integratord/test_alerts_reading.py b/tests/integration/test_integratord/test_alerts_reading.py index c3507f9ca9..2997e1bffb 100644 --- a/tests/integration/test_integratord/test_alerts_reading.py +++ b/tests/integration/test_integratord/test_alerts_reading.py @@ -55,7 +55,8 @@ from wazuh_testing.modules.integratord import event_monitor as evm -def replace_webhook_url(ids, configurations): +@pytest.fixture(scope='function') +def replace_webhook_url(configuration): '''Replace the Webhook URL in each test case configuration parameters. Args: @@ -65,10 +66,8 @@ def replace_webhook_url(ids, configurations): Returns: configurations (list): List of configurations. ''' - for i in range(0, len(ids)): - configurations[i]['WEBHOOK_URL'] = global_parameters.slack_webhook_url - - return configurations + configuration['WEBHOOK_URL'] = global_parameters.slack_webhook_url + return configuration # Marks @@ -90,11 +89,11 @@ def replace_webhook_url(ids, configurations): t2_config_params, t2_metadata, t2_cases_ids = get_test_cases_data(t2_cases_path) t3_config_params, t3_metadata, t3_cases_ids = get_test_cases_data(t3_cases_path) -t1_config_params = replace_webhook_url(t1_cases_ids, t1_config_params) -t2_config_params = replace_webhook_url(t2_cases_ids, t2_config_params) -t3_config_params = replace_webhook_url(t3_cases_ids, t3_config_params) - # Load tests configurations +# print(len(t1_config_params)) +# print(len(t1_metadata)) +# print(load_configuration_template(configurations_template, t1_config_params, t1_metadata)) + t1_config = load_configuration_template(configurations_template, t1_config_params, t1_metadata) t2_config = load_configuration_template(configurations_template, t2_config_params, t2_metadata) t3_config = load_configuration_template(configurations_template, t3_config_params, t3_metadata) @@ -108,8 +107,9 @@ def replace_webhook_url(ids, configurations): # Tests @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t1_config, t1_metadata), ids=t1_cases_ids) -def test_integratord_change_json_inode(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, - configure_local_internal_options_module, daemons_handler_function, +def test_integratord_change_json_inode(configuration, metadata, replace_webhook_url, set_wazuh_configuration, + truncate_monitored_files, configure_local_internal_options_module, + daemons_handler_function, wait_for_start_module): ''' description: Check that wazuh-integratord detects a change in the inode of the alerts.json and continues reading From 8c7a75e92bb45341bc728c050779987ab2fe8bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Mon, 26 Jun 2023 17:12:54 +0100 Subject: [PATCH 50/56] feat: skip integratord if no webhooks is provided --- .../integration/test_integratord/conftest.py | 26 +++++++++++++++- .../cases_integratord_change_inode_alert.yaml | 2 +- ..._integratord_read_invalid_json_alerts.yaml | 4 +-- ...es_integratord_read_valid_json_alerts.yaml | 2 +- .../test_integratord/test_alerts_reading.py | 31 +++++-------------- tests/performance/test_api/conftest.py | 3 +- 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/tests/integration/test_integratord/conftest.py b/tests/integration/test_integratord/conftest.py index 64db4af575..a12ba42d02 100644 --- a/tests/integration/test_integratord/conftest.py +++ b/tests/integration/test_integratord/conftest.py @@ -5,7 +5,7 @@ ''' import pytest -from wazuh_testing import T_5 +from wazuh_testing import T_5, WAZUH_CONF_PATH, global_parameters from wazuh_testing.tools import LOG_FILE_PATH from wazuh_testing.tools.monitoring import FileMonitor from wazuh_testing.modules import analysisd @@ -13,6 +13,9 @@ from wazuh_testing.modules.integratord import event_monitor as evm +webhook_placeholder = 'WEBHOOK_URL' + + @pytest.fixture(scope='function') def wait_for_start_module(request): # Wait for integratord thread to start @@ -23,3 +26,24 @@ def wait_for_start_module(request): check_analysisd_event(file_monitor=file_monitor, timeout=T_5, callback=analysisd.CB_ANALYSISD_STARTUP_COMPLETED, error_message=analysisd.ERR_MSG_STARTUP_COMPLETED_NOT_FOUND) + + +@pytest.fixture(scope='package') +def validate_slack_hook(): + """Validate the slack hook is defined.""" + + if not hasattr(global_parameters, 'slack_webhook_url'): + pytest.skip('Slack webhook URL not defined.') + + +@pytest.fixture(scope='function') +def replace_placeholder_slack_hook(): + """Replace the placeholder slack hook with the real one.""" + + with open(WAZUH_CONF_PATH, 'r') as f: + configuration = f.read() + + configuration = configuration.replace(webhook_placeholder, global_parameters.slack_webhook_url) + + with open(WAZUH_CONF_PATH, 'w') as f: + f.write(configuration) diff --git a/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml b/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml index c3b3691da9..5c7f710144 100644 --- a/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml +++ b/tests/integration/test_integratord/data/test_cases/cases_integratord_change_inode_alert.yaml @@ -1,7 +1,7 @@ - name: cannot_read_alerts_file_inode_changed description: The alerts.json file inode has changed and it cannot read alerts from it until it reloads. configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000","rule":{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", diff --git a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml index 65c2c501f3..a26eb2945b 100644 --- a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml +++ b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_invalid_json_alerts.yaml @@ -1,7 +1,7 @@ - name: read_invalid_json_alert description: Read a invalid alert from alerts.json - removed rule key name - Integration fails configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000",:{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", @@ -25,7 +25,7 @@ - name: read_overlong_json_alert description: Read a an alert that is over 64kb alert from alerts.json - Integration fails configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000","rule":{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", diff --git a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml index 8ee984321a..a136cc68b8 100644 --- a/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml +++ b/tests/integration/test_integratord/data/test_cases/cases_integratord_read_valid_json_alerts.yaml @@ -1,7 +1,7 @@ - name: read_valid_json_alert description: Read a valid alert from alerts.json configuration_parameters: - WEBHOOK_URL: Insert using --slack-webhook-url parameter + WEBHOOK_URL: WEBHOOK_URL metadata: alert_sample: '{"timestamp":"2022-05-11T12:29:19.905+0000","rule":{"level":10,"description": "sshd: brute force trying to get access to the system. Non existent user.","id":"5712", diff --git a/tests/integration/test_integratord/test_alerts_reading.py b/tests/integration/test_integratord/test_alerts_reading.py index 2997e1bffb..3f2e8f646d 100644 --- a/tests/integration/test_integratord/test_alerts_reading.py +++ b/tests/integration/test_integratord/test_alerts_reading.py @@ -55,21 +55,6 @@ from wazuh_testing.modules.integratord import event_monitor as evm -@pytest.fixture(scope='function') -def replace_webhook_url(configuration): - '''Replace the Webhook URL in each test case configuration parameters. - - Args: - ids (list): List of ids of test cases. - configurations (list): List of test's configuration parameters. - - Returns: - configurations (list): List of configurations. - ''' - configuration['WEBHOOK_URL'] = global_parameters.slack_webhook_url - return configuration - - # Marks pytestmark = [pytest.mark.server] @@ -90,10 +75,6 @@ def replace_webhook_url(configuration): t3_config_params, t3_metadata, t3_cases_ids = get_test_cases_data(t3_cases_path) # Load tests configurations -# print(len(t1_config_params)) -# print(len(t1_metadata)) -# print(load_configuration_template(configurations_template, t1_config_params, t1_metadata)) - t1_config = load_configuration_template(configurations_template, t1_config_params, t1_metadata) t2_config = load_configuration_template(configurations_template, t2_config_params, t2_metadata) t3_config = load_configuration_template(configurations_template, t3_config_params, t3_metadata) @@ -107,8 +88,9 @@ def replace_webhook_url(configuration): # Tests @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t1_config, t1_metadata), ids=t1_cases_ids) -def test_integratord_change_json_inode(configuration, metadata, replace_webhook_url, set_wazuh_configuration, - truncate_monitored_files, configure_local_internal_options_module, +def test_integratord_change_json_inode(validate_slack_hook, configuration, metadata, set_wazuh_configuration, + replace_placeholder_slack_hook, truncate_monitored_files, + configure_local_internal_options_module, daemons_handler_function, wait_for_start_module): ''' @@ -209,7 +191,9 @@ def test_integratord_change_json_inode(configuration, metadata, replace_webhook_ @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t2_config, t2_metadata), ids=t2_cases_ids) -def test_integratord_read_valid_alerts(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_valid_alerts(validate_slack_hook, configuration, metadata, set_wazuh_configuration, + replace_placeholder_slack_hook, + truncate_monitored_files, configure_local_internal_options_module, daemons_handler_function, wait_for_start_module): ''' @@ -278,7 +262,8 @@ def test_integratord_read_valid_alerts(configuration, metadata, set_wazuh_config @pytest.mark.tier(level=1) @pytest.mark.parametrize('configuration, metadata', zip(t3_config, t3_metadata), ids=t3_cases_ids) -def test_integratord_read_invalid_alerts(configuration, metadata, set_wazuh_configuration, truncate_monitored_files, +def test_integratord_read_invalid_alerts(validate_slack_hook, configuration, metadata, set_wazuh_configuration, + replace_placeholder_slack_hook, truncate_monitored_files, configure_local_internal_options_module, daemons_handler_function, wait_for_start_module): ''' diff --git a/tests/performance/test_api/conftest.py b/tests/performance/test_api/conftest.py index 5d45d91cb3..f3b11884b2 100755 --- a/tests/performance/test_api/conftest.py +++ b/tests/performance/test_api/conftest.py @@ -174,4 +174,5 @@ def pytest_collection_modifyitems(session, config, items): # Add each test_case metadata as user_properties for its item for item in items: - item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) + if hasattr(item, 'callspec') and 'test_case' in item.callspec.params: + item.user_properties.extend([(key, value) for key, value in item.callspec.params['test_case'].items()]) From d911eb3af361c03eba4d9bb3ab16aa82bd04dd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Wed, 28 Jun 2023 10:49:09 +0200 Subject: [PATCH 51/56] fix: internal option fixtures --- .../test_agentd_parametrized_reconnections.py | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py index 1fa16fe17f..25fd8ce93d 100644 --- a/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py +++ b/tests/integration/test_agentd/test_agentd_parametrized_reconnections.py @@ -61,7 +61,7 @@ import pytest from time import sleep -from wazuh_testing.tools import WAZUH_PATH, LOG_FILE_PATH +from wazuh_testing.tools import WAZUH_PATH, LOG_FILE_PATH, WAZUH_LOCAL_INTERNAL_OPTIONS from wazuh_testing.tools.authd_sim import AuthdSimulator from wazuh_testing.tools.configuration import load_wazuh_configurations from wazuh_testing.tools.file import truncate_file @@ -115,22 +115,27 @@ def teardown(): remoted_server.stop() -@pytest.fixture(scope="module", autouse=True) +@pytest.fixture(scope="module") def set_debug_mode(): """Set debug2 for agentd in local internal options file.""" + + new_timeout = 'agent.recv_timeout=' + timeout + if platform.system() == 'win32' or platform.system() == 'Windows': - local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') debug_line = 'windows.debug=2\n' else: - local_int_conf_path = os.path.join(WAZUH_PATH, 'etc', 'local_internal_options.conf') debug_line = 'agent.debug=2\n' - with open(local_int_conf_path) as local_file_read: - lines = local_file_read.readlines() - for line in lines: - if line == debug_line: - return - with open(local_int_conf_path, 'a') as local_file_write: - local_file_write.write('\n' + debug_line) + + with open(WAZUH_LOCAL_INTERNAL_OPTIONS, 'r') as local_file_read: + backup_configuration = local_file_read.read() + + with open(WAZUH_LOCAL_INTERNAL_OPTIONS, 'w+') as local_file_read: + local_file_read.write(debug_line + new_timeout) + + yield + + with open(WAZUH_LOCAL_INTERNAL_OPTIONS, 'w+') as local_file_read: + local_file_read.write(backup_configuration) # fixtures @@ -258,27 +263,6 @@ def wait_unable_to_connect(line): return None -@pytest.fixture(scope="module") -def change_timeout(): - """Set agent.recv_timeout for agentd in local internal options file. - - The above option sets the maximum number of seconds to wait - for server response from the TCP client socket. - """ - new_timeout = 'agent.recv_timeout=' + timeout - if platform.system() == 'win32' or platform.system() == 'Windows': - local_int_conf_path = os.path.join(WAZUH_PATH, 'local_internal_options.conf') - else: - local_int_conf_path = os.path.join(WAZUH_PATH, 'etc', 'local_internal_options.conf') - with open(local_int_conf_path, 'r') as local_file_read: - lines = local_file_read.readlines() - for line in lines: - if line == new_timeout: - return - with open(local_int_conf_path, 'a') as local_file_write: - local_file_write.write('\n' + new_timeout) - - def parse_time_from_log_line(log_line): """Create a datetime object from a date in a string. @@ -306,7 +290,7 @@ def parse_time_from_log_line(log_line): """ -def test_agentd_parametrized_reconnections(configure_authd_server, start_authd, stop_agent, set_keys, +def test_agentd_parametrized_reconnections(set_debug_mode, configure_authd_server, start_authd, stop_agent, set_keys, configure_environment, get_configuration, teardown): ''' description: Check how the agent behaves when there are delays between connection From 9a526203a87bc3affd8c78211f8612fc3f5e1ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 10:40:57 +0100 Subject: [PATCH 52/56] fix: removed deselection debugging fixture Co-authored-by: mauromalara --- tests/integration/conftest.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index ccf9eb5559..4116b9cbe1 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -1300,13 +1300,3 @@ def truncate_event_logs(): for log_file in log_files: truncate_file(log_file) - - -def pytest_deselected(items): - if not items: - return - config = items[0].session.config - reporter = config.pluginmanager.getplugin("terminalreporter") - reporter.ensure_newline() - for item in items: - reporter.line(f"deselected: {item.nodeid}", yellow=True, bold=True) From 8fcfe5bf19843c0e1c08a931ba16a09072c47423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 10:47:40 +0100 Subject: [PATCH 53/56] fix: rename collection action task Co-authored-by: mauromalara --- .github/workflows/pytest_tests_collection.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index 219e6c0604..a6a00dcf9f 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -21,7 +21,7 @@ jobs: - name: Install dependencies run: pip install -r requirements.txt - - name: Install dependencies + - name: Install the QA framework run: python3 setup.py install working-directory: deps/wazuh_testing From 2b9e4ffecb409ac856ad1ae2852ff0be7efa17ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 11:15:31 +0100 Subject: [PATCH 54/56] fix: include comment for modifyitems hook Co-authored-by: mauromalara --- tests/integration/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 4116b9cbe1..9afd8e0a2b 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -57,6 +57,7 @@ def get_report_files(): def pytest_collection_modifyitems(session, config, items): selected_tests = [] deselected_tests = [] + # Ignore platform deselection if avoid-platform-based-deselection is used if not global_parameters.avoid_platform_based_deselection: for item in items: supported_platforms = PLATFORMS.intersection(mark.name for mark in item.iter_markers()) From 9637846a4310a6af87ce5505bbbfe1ee18213e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 11:32:27 +0100 Subject: [PATCH 55/56] docs: include docstring to get_version/get_service Co-authored-by: mauromalara --- deps/wazuh_testing/wazuh_testing/tools/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/deps/wazuh_testing/wazuh_testing/tools/__init__.py b/deps/wazuh_testing/wazuh_testing/tools/__init__.py index 6d42379944..678da556e5 100644 --- a/deps/wazuh_testing/wazuh_testing/tools/__init__.py +++ b/deps/wazuh_testing/wazuh_testing/tools/__init__.py @@ -91,6 +91,11 @@ def get_version(): + """Get Wazuh version + + Returns: + str: Wazuh version. If it is not possible to get it, it returns 'N/A' + """ try: if platform.system() in ['Windows', 'win32']: with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f: @@ -105,6 +110,11 @@ def get_version(): def get_service(): + """Get Wazuh installed component + + Returns: + str: Wazuh installed component, wazuh-manager or wazuh-agent. If it is not possible to get it, it returns 'N/A' + """ try: if platform.system() in ['Windows', 'win32']: if os.path.exists(WAZUH_PATH): From fa4af3904a8fa73edb01e2eb138345dd9f523125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rebollo=20P=C3=A9rez?= Date: Fri, 28 Jul 2023 11:45:43 +0100 Subject: [PATCH 56/56] fix: test collection github action --- .github/workflows/pytest_tests_collection.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pytest_tests_collection.yaml b/.github/workflows/pytest_tests_collection.yaml index a6a00dcf9f..abbd24957e 100644 --- a/.github/workflows/pytest_tests_collection.yaml +++ b/.github/workflows/pytest_tests_collection.yaml @@ -2,8 +2,10 @@ name: Tests collection on: pull_request: - paths: - - tests/** + types: + - opened + - ready_for_review + - synchronize jobs: test-collection: