From 080b7a266f5a0582a0ce78e31a6bdad4f170f287 Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 19 Mar 2024 14:41:30 +0100 Subject: [PATCH] enhancement(#4843): Tested version with dynamic waits --- .../modules/testing/tests/helpers/agent.py | 23 +++++++---- .../modules/testing/tests/helpers/utils.py | 19 ++++++++++ .../tests/test_agent/test_basic_info.py | 38 ------------------- .../testing/tests/test_agent/test_install.py | 4 +- .../tests/test_agent/test_registration.py | 38 +++---------------- .../testing/tests/test_agent/test_restart.py | 28 -------------- .../testing/tests/test_agent/test_stop.py | 28 -------------- 7 files changed, 42 insertions(+), 136 deletions(-) delete mode 100644 deployability/modules/testing/tests/test_agent/test_basic_info.py diff --git a/deployability/modules/testing/tests/helpers/agent.py b/deployability/modules/testing/tests/helpers/agent.py index 84e0b55506..3e4c635a61 100644 --- a/deployability/modules/testing/tests/helpers/agent.py +++ b/deployability/modules/testing/tests/helpers/agent.py @@ -24,7 +24,7 @@ def install_agent(inventory_path, agent_name, wazuh_version, wazuh_revision, liv distribution = HostInformation.get_linux_distribution(inventory_path) architecture = HostInformation.get_architecture(inventory_path) - if distribution == 'rpm' or distribution == 'opensuse-leap' or distribution == 'amzn' and 'x86_64' in architecture: + if distribution == 'rpm' and 'x86_64' in architecture: commands.extend([ f"curl -o wazuh-agent-{wazuh_version}-1.x86_64.rpm https://{s3_url}.wazuh.com/{release}/yum/wazuh-agent-{wazuh_version}-1.x86_64.rpm && sudo WAZUH_MANAGER='MANAGER_IP' WAZUH_AGENT_NAME='{agent_name}' rpm -ihv wazuh-agent-{wazuh_version}-1.x86_64.rpm" ]) @@ -104,16 +104,23 @@ def uninstall_agent(inventory_path, wazuh_version=None, wazuh_revision=None) -> commands = [] if 'linux' in os_type: distribution = HostInformation.get_linux_distribution(inventory_path) - if distribution == 'deb': + os_name = HostInformation.get_os_name_from_inventory(inventory_path) + if os_name == 'opensuse' or os_name == 'suse': commands.extend([ - "apt-get remove --purge wazuh-agent -y" + "zypper remove --no-confirm wazuh-agent", + "rm -r /var/ossec" + ]) + else: + if distribution == 'deb': + commands.extend([ + "apt-get remove --purge wazuh-agent -y" + ]) + elif distribution == 'rpm': + commands.extend([ + "yum remove wazuh-agent -y", + f"rm -rf {WAZUH_ROOT}" ]) - elif distribution == 'rpm': - commands.extend([ - "yum remove wazuh-agent -y", - f"rm -rf {WAZUH_ROOT}" - ]) system_commands = [ diff --git a/deployability/modules/testing/tests/helpers/utils.py b/deployability/modules/testing/tests/helpers/utils.py index 07e4d2962f..f73431dd12 100755 --- a/deployability/modules/testing/tests/helpers/utils.py +++ b/deployability/modules/testing/tests/helpers/utils.py @@ -323,3 +323,22 @@ def is_process_alive(process_name: str) -> bool: """ return process_name in (p.name() for p in psutil.process_iter()) + + +def dynamic_wait(expected_condition_func, cycles=10, waiting_time=10) -> None: + """ + Waits the process during assigned cycles for the assigned seconds + + Args: + expected_condition_func (function): The function that returns True when the expected condition is met + cycles(int): Number of cycles + waiting_Time(int): Number of seconds per cycle + + """ + for _ in range(cycles): + if expected_condition_func(): + break + else: + time.sleep(waiting_time) + else: + raise RuntimeError(f'Time out') \ No newline at end of file diff --git a/deployability/modules/testing/tests/test_agent/test_basic_info.py b/deployability/modules/testing/tests/test_agent/test_basic_info.py deleted file mode 100644 index 1e2b803a84..0000000000 --- a/deployability/modules/testing/tests/test_agent/test_basic_info.py +++ /dev/null @@ -1,38 +0,0 @@ -import platform -import pytest - - -@pytest.fixture -def wazuh_params(request): - - return { - 'wazuh_version': request.config.getoption('--wazuh_version') - } - -@pytest.fixture(scope='module') -def agent_uname(agent_info: dict) -> dict: - uname_list = agent_info.get('os').get('uname').split('|') - uname = {'system': uname_list[0], - 'node': uname_list[1], - 'release': uname_list[2], - 'version': uname_list[3], - 'machine': uname_list[4]} - - return uname - -def test_agent_version(wazuh_params: dict, agent_info: dict) -> None: - expected_version = f"Wazuh v{wazuh_params['wazuh_version']}" - actual_version = agent_info.get('version') - assert expected_version in actual_version, 'Unexpected agent version reported by server.' - -def test_agent_system(agent_uname: dict) -> None: - expected_system = platform.uname().system - assert expected_system in agent_uname.get('system'), 'Unexpected OS.' - -def test_agent_architecture(agent_uname: dict) -> None: - expected_machine = platform.uname().machine - assert expected_machine in agent_uname.get('machine'), 'Unexpected architecture.' - -def test_agent_os_version(agent_uname: dict) -> None: - expected_release = platform.uname().version - assert expected_release in agent_uname.get('version'), 'Unexpected OS version.' diff --git a/deployability/modules/testing/tests/test_agent/test_install.py b/deployability/modules/testing/tests/test_agent/test_install.py index 61f2caff58..46b8ca486f 100644 --- a/deployability/modules/testing/tests/test_agent/test_install.py +++ b/deployability/modules/testing/tests/test_agent/test_install.py @@ -74,7 +74,7 @@ def setup_test_environment(wazuh_params): wazuh_params['managers'] = {key: value for key, value in targets_dict.items() if key.startswith('wazuh-')} wazuh_params['agents'] = {key: value for key, value in targets_dict.items() if key.startswith('agent-')} -""" def test_installation(wazuh_params): +def test_installation(wazuh_params): # Disabling firewall for all managers for manager_name, manager_params in wazuh_params['managers'].items(): HostConfiguration.disable_firewall(manager_params) @@ -91,7 +91,7 @@ def setup_test_environment(wazuh_params): # Testing installation directory for agent in wazuh_params['agents'].values(): - assert HostInformation.dir_exists(agent, WAZUH_ROOT) """ + assert HostInformation.dir_exists(agent, WAZUH_ROOT) def test_status(wazuh_params): for agent in wazuh_params['agents'].values(): diff --git a/deployability/modules/testing/tests/test_agent/test_registration.py b/deployability/modules/testing/tests/test_agent/test_registration.py index 88c3727e63..efddb1b67a 100644 --- a/deployability/modules/testing/tests/test_agent/test_registration.py +++ b/deployability/modules/testing/tests/test_agent/test_registration.py @@ -1,38 +1,11 @@ import pytest +import time from ..helpers.manager import WazuhManager, WazuhAPI -from ..helpers.agent import WazuhAgent +from ..helpers.agent import WazuhAgent, WazuhAPI from ..helpers.generic import HostConfiguration, CheckFiles, HostInformation, GeneralComponentActions from ..helpers.constants import WAZUH_ROOT - - -def install_agent_callback(wazuh_params, agent_name, agent_params): - WazuhAgent.install_agent(agent_params, agent_name, wazuh_params['wazuh_version'], wazuh_params['wazuh_revision'], wazuh_params['live']) - - -def perform_action_and_scan_for_agent(agent_params, agent_name, wazuh_params): - result = CheckFiles.perform_action_and_scan(agent_params, lambda: install_agent_callback(wazuh_params, agent_name, agent_params)) - categories = ['/root', '/usr/bin', '/usr/sbin', '/boot'] - actions = ['added', 'modified', 'removed'] - - # Selecting filter - os_name = HostInformation.get_os_name_from_inventory(agent_params) - if 'debian' in os_name: - filter_data= {'/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, '/usr/bin': {'added': ['unattended-upgrade', 'gapplication', 'add-apt-repository', 'gpg-wks-server', 'pkexec', 'gpgsplit', 'watchgnupg', 'pinentry-curses', 'gpg-zip', 'gsettings', 'gpg-agent', 'gresource', 'gdbus', 'gpg-connect-agent', 'gpgconf', 'gpgparsemail', 'lspgpot', 'pkaction', 'pkttyagent', 'pkmon', 'dirmngr', 'kbxutil', 'migrate-pubring-from-classic-gpg', 'gpgcompose', 'pkcheck', 'gpgsm', 'gio', 'pkcon', 'gpgtar', 'dirmngr-client', 'gpg', 'filebeat', 'gawk', 'curl', 'update-mime-database', 'dh_installxmlcatalogs', 'appstreamcli','lspgpot'], 'removed': [], 'modified': []}, '/root': {'added': ['trustdb.gpg'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': ['update-catalog', 'applygnupgdefaults', 'addgnupghome', 'install-sgmlcatalog', 'update-xmlcatalog'], 'removed': [], 'modified': []}} - else: - filter_data = {'/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, '/usr/bin': {'added': ['filebeat'], 'removed': [], 'modified': []}, '/root': {'added': ['trustdb.gpg'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': [], 'removed': [], 'modified': []}} - - # Use of filters - for directory, changes in result.items(): - if directory in filter_data: - for change, files in changes.items(): - if change in filter_data[directory]: - result[directory][change] = [file for file in files if file.split('/')[-1] not in filter_data[directory][change]] - - # Testing the results - for category in categories: - for action in actions: - assert result[category][action] == [] +from ..helpers.utils import dynamic_wait @pytest.fixture def wazuh_params(request): @@ -91,10 +64,11 @@ def test_connection(wazuh_params): def test_isActive(wazuh_params): + wazuh_api = WazuhAPI(wazuh_params['master']) for agent_names, agent_params in wazuh_params['agents'].items(): assert GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent') - wazuh_api = WazuhAPI(wazuh_params['master']) - assert 'active' == WazuhAgent.get_agent_status(wazuh_api, agent_names) + expected_condition_func = lambda: 'active' == WazuhAgent.get_agent_status(wazuh_api, agent_names) + dynamic_wait(expected_condition_func, cycles=10, waiting_time=20) def test_clientKeys(wazuh_params): diff --git a/deployability/modules/testing/tests/test_agent/test_restart.py b/deployability/modules/testing/tests/test_agent/test_restart.py index bbd428e7e8..f049950ff1 100644 --- a/deployability/modules/testing/tests/test_agent/test_restart.py +++ b/deployability/modules/testing/tests/test_agent/test_restart.py @@ -6,34 +6,6 @@ from ..helpers.constants import WAZUH_ROOT -def install_agent_callback(wazuh_params, agent_name, agent_params): - WazuhAgent.install_agent(agent_params, agent_name, wazuh_params['wazuh_version'], wazuh_params['wazuh_revision'], wazuh_params['live']) - - -def perform_action_and_scan_for_agent(agent_params, agent_name, wazuh_params): - result = CheckFiles.perform_action_and_scan(agent_params, lambda: install_agent_callback(wazuh_params, agent_name, agent_params)) - categories = ['/root', '/usr/bin', '/usr/sbin', '/boot'] - actions = ['added', 'modified', 'removed'] - - # Selecting filter - os_name = HostInformation.get_os_name_from_inventory(agent_params) - if 'debian' in os_name: - filter_data= {'/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, '/usr/bin': {'added': ['unattended-upgrade', 'gapplication', 'add-apt-repository', 'gpg-wks-server', 'pkexec', 'gpgsplit', 'watchgnupg', 'pinentry-curses', 'gpg-zip', 'gsettings', 'gpg-agent', 'gresource', 'gdbus', 'gpg-connect-agent', 'gpgconf', 'gpgparsemail', 'lspgpot', 'pkaction', 'pkttyagent', 'pkmon', 'dirmngr', 'kbxutil', 'migrate-pubring-from-classic-gpg', 'gpgcompose', 'pkcheck', 'gpgsm', 'gio', 'pkcon', 'gpgtar', 'dirmngr-client', 'gpg', 'filebeat', 'gawk', 'curl', 'update-mime-database', 'dh_installxmlcatalogs', 'appstreamcli','lspgpot'], 'removed': [], 'modified': []}, '/root': {'added': ['trustdb.gpg'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': ['update-catalog', 'applygnupgdefaults', 'addgnupghome', 'install-sgmlcatalog', 'update-xmlcatalog'], 'removed': [], 'modified': []}} - else: - filter_data = {'/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, '/usr/bin': {'added': ['filebeat'], 'removed': [], 'modified': []}, '/root': {'added': ['trustdb.gpg'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': [], 'removed': [], 'modified': []}} - - # Use of filters - for directory, changes in result.items(): - if directory in filter_data: - for change, files in changes.items(): - if change in filter_data[directory]: - result[directory][change] = [file for file in files if file.split('/')[-1] not in filter_data[directory][change]] - - # Testing the results - for category in categories: - for action in actions: - assert result[category][action] == [] - @pytest.fixture def wazuh_params(request): wazuh_version = request.config.getoption('--wazuh_version') diff --git a/deployability/modules/testing/tests/test_agent/test_stop.py b/deployability/modules/testing/tests/test_agent/test_stop.py index 9fbff6e856..e0a8ef5c52 100644 --- a/deployability/modules/testing/tests/test_agent/test_stop.py +++ b/deployability/modules/testing/tests/test_agent/test_stop.py @@ -6,34 +6,6 @@ from ..helpers.constants import WAZUH_ROOT -def install_agent_callback(wazuh_params, agent_name, agent_params): - WazuhAgent.install_agent(agent_params, agent_name, wazuh_params['wazuh_version'], wazuh_params['wazuh_revision'], wazuh_params['live']) - - -def perform_action_and_scan_for_agent(agent_params, agent_name, wazuh_params): - result = CheckFiles.perform_action_and_scan(agent_params, lambda: install_agent_callback(wazuh_params, agent_name, agent_params)) - categories = ['/root', '/usr/bin', '/usr/sbin', '/boot'] - actions = ['added', 'modified', 'removed'] - - # Selecting filter - os_name = HostInformation.get_os_name_from_inventory(agent_params) - if 'debian' in os_name: - filter_data= {'/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, '/usr/bin': {'added': ['unattended-upgrade', 'gapplication', 'add-apt-repository', 'gpg-wks-server', 'pkexec', 'gpgsplit', 'watchgnupg', 'pinentry-curses', 'gpg-zip', 'gsettings', 'gpg-agent', 'gresource', 'gdbus', 'gpg-connect-agent', 'gpgconf', 'gpgparsemail', 'lspgpot', 'pkaction', 'pkttyagent', 'pkmon', 'dirmngr', 'kbxutil', 'migrate-pubring-from-classic-gpg', 'gpgcompose', 'pkcheck', 'gpgsm', 'gio', 'pkcon', 'gpgtar', 'dirmngr-client', 'gpg', 'filebeat', 'gawk', 'curl', 'update-mime-database', 'dh_installxmlcatalogs', 'appstreamcli','lspgpot'], 'removed': [], 'modified': []}, '/root': {'added': ['trustdb.gpg'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': ['update-catalog', 'applygnupgdefaults', 'addgnupghome', 'install-sgmlcatalog', 'update-xmlcatalog'], 'removed': [], 'modified': []}} - else: - filter_data = {'/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, '/usr/bin': {'added': ['filebeat'], 'removed': [], 'modified': []}, '/root': {'added': ['trustdb.gpg'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': [], 'removed': [], 'modified': []}} - - # Use of filters - for directory, changes in result.items(): - if directory in filter_data: - for change, files in changes.items(): - if change in filter_data[directory]: - result[directory][change] = [file for file in files if file.split('/')[-1] not in filter_data[directory][change]] - - # Testing the results - for category in categories: - for action in actions: - assert result[category][action] == [] - @pytest.fixture def wazuh_params(request): wazuh_version = request.config.getoption('--wazuh_version')