Skip to content

Commit

Permalink
enhancement(#4843): Tested version with dynamic waits
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-akim committed Mar 19, 2024
1 parent badcf4b commit 080b7a2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 136 deletions.
23 changes: 15 additions & 8 deletions deployability/modules/testing/tests/helpers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
])
Expand Down Expand Up @@ -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 = [
Expand Down
19 changes: 19 additions & 0 deletions deployability/modules/testing/tests/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
38 changes: 0 additions & 38 deletions deployability/modules/testing/tests/test_agent/test_basic_info.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand Down
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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):
Expand Down
28 changes: 0 additions & 28 deletions deployability/modules/testing/tests/test_agent/test_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
28 changes: 0 additions & 28 deletions deployability/modules/testing/tests/test_agent/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 080b7a2

Please sign in to comment.