Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create daemon handler fixture for integration test #1826

Merged
merged 14 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions deps/wazuh_testing/wazuh_testing/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def get_service():

WAZUH_SOCKETS = {
'wazuh-agentd': [],
'wazuh-apid': [],
'wazuh-agentlessd': [],
'wazuh-csyslogd': [],
'wazuh-analysisd': [
ANALYSISD_ANALISIS_SOCKET_PATH,
ANALYSISD_QUEUE_SOCKET_PATH
Expand All @@ -130,6 +133,7 @@ def get_service():
'wazuh-logcollector': [LOGCOLLECTOR_SOCKET_PATH],
'wazuh-monitord': [MONITORD_SOCKET_PATH],
'wazuh-remoted': [REMOTED_SOCKET_PATH],
'wazuh-maild': [],
'wazuh-syscheckd': [SYSCHECKD_SOCKET_PATH],
'wazuh-db': [WAZUH_DB_SOCKET_PATH],
'wazuh-modulesd': [
Expand All @@ -146,3 +150,27 @@ def get_service():
MODULESD_KREQUEST_SOCKET_PATH,
AUTHD_SOCKET_PATH
]

# Wazuh daemons
LOGCOLLECTOR_DAEMON = 'wazuh-logcollector'
AGENTLESS_DAEMON = 'wazuh-agentlessd'
CSYSLOG_DAEMON = 'wazuh-csyslogd'
REMOTE_DAEMON = 'wazuh-remoted'
ANALYSISD_DAEMON = 'wazuh-analysisd'
API_DAEMON = 'wazuh-apid'
MAIL_DAEMON = 'wazuh-maild'
SYSCHECK_DAEMON = 'wazuh-syscheckd'
EXEC_DAEMON = 'wazuh-execd'
MODULES_DAEMON = 'wazuh-modulesd'
CLUSTER_DAEMON = 'wazuh-clusterd'
INTEGRATOR_DAEMON = 'wazuh-integratord'
MONITOR_DAEMON = 'wazuh-monitord'
DB_DAEMON = 'wazuh-db'
AGENT_DAEMON = 'wazuh-agentd'


ALL_MANAGER_DAEMONS = [LOGCOLLECTOR_DAEMON, AGENTLESS_DAEMON, CSYSLOG_DAEMON, REMOTE_DAEMON, ANALYSISD_DAEMON,
API_DAEMON, MAIL_DAEMON, SYSCHECK_DAEMON, EXEC_DAEMON, MODULES_DAEMON, CLUSTER_DAEMON, INTEGRATOR_DAEMON, MONITOR_DAEMON,
DB_DAEMON ]
ALL_AGENT_DAEMONS = [AGENT_DAEMON, EXEC_DAEMON, LOGCOLLECTOR_DAEMON, SYSCHECK_DAEMON, MODULES_DAEMON]
API_DAEMONS_REQUIREMENTS = [API_DAEMON, MODULES_DAEMON, ANALYSISD_DAEMON, EXEC_DAEMON, DB_DAEMON, REMOTE_DAEMON]
73 changes: 72 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from py.xml import html

import wazuh_testing.tools.configuration as conf
from wazuh_testing import global_parameters
from wazuh_testing import global_parameters, logger
from wazuh_testing.logcollector import create_file_structure, delete_file_structure
from wazuh_testing.tools import LOG_FILE_PATH, WAZUH_CONF, get_service, ALERT_FILE_PATH
from wazuh_testing.tools.file import truncate_file
Expand Down Expand Up @@ -602,3 +602,74 @@ def create_file_structure_function(get_files_list):
yield

delete_file_structure(get_files_list)


@pytest.fixture(scope='module')
def daemons_handler(get_configuration, request):
"""Handler of Wazuh daemons.

It uses `daemons_handler_configuration` of each module in order to configure the behavior of the fixture.
The `daemons_handler_configuration` should be a dictionary with the following keys:
daemons (list, optional): List with every daemon to be used by the module. In case of empty a ValueError
will be raised
wazuh_control (boolean): Configure to restart all wazuh services. Default `False`.
ignore_errors (boolean): Configure if errors in daemon handling should be ignored. This option is available
in order to use this fixture along with invalid configuration. Default `False`

Args:
get_configuration (fixture): Gets the current configuration of the test.
request (fixture): Provide information on the executing test function.
"""
daemons = []
ignore_errors = False
wazuh_control = False
Copy link
Contributor

@jmv74211 jmv74211 Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this for all_daemons or something similar. wazuh_control is a bit confusing


try:
daemons_handler_configuration = getattr(request.module, 'daemons_handler_configuration')
if 'daemons' in daemons_handler_configuration and not wazuh_control:
daemons = daemons_handler_configuration['daemons']
if not daemons or (type(daemons) == list and len(daemons) == 0):
logger.error('Daemons Handler: Daemons list is not set')
raise ValueError

if 'wazuh_control' in daemons_handler_configuration:
logger.debug(f"Daemons Handler: Wazuh control set to {daemons_handler_configuration['wazuh_control']}")
wazuh_control = daemons_handler_configuration['wazuh_control']

if 'ignore_errors' in daemons_handler_configuration:
logger.debug(f"Daemons Handler: Ignore error set to {daemons_handler_configuration['ignore_errors']}")
ignore_errors = daemons_handler_configuration['ignore_errors']

except AttributeError as daemon_configuration_not_set:
logger.error('Daemons Handler: Error - daemons_handler_configuration is not set')
raise daemon_configuration_not_set

try:
if wazuh_control:
logger.debug('Daemons Handler: Debug - Restarting wazuh using wazuh-control')
# Restart daemon instead of starting due to legacy used fixture in the test suite.
control_service('restart')
else:
for daemon in daemons:
logger.debug(f"Daemons Handler: Restarting {daemon}")
# Restart daemon instead of starting due to legacy used fixture in the test suite.
control_service('restart', daemon=daemon)
jmv74211 marked this conversation as resolved.
Show resolved Hide resolved

except ValueError as value_error:
logger.error(f"Daemons Handler: Error - {str(value_error)}")
if not ignore_errors:
raise value_error
except subprocess.CalledProcessError as called_process_error:
logger.error(f"Daemons Handler: Error - {str(called_process_error)}")
if not ignore_errors:
raise called_process_error

yield

if wazuh_control:
logger.debug('Daemons Handler: Debug - Stopping wazuh using wazuh-control')
control_service('stop')
else:
for daemon in daemons:
logger.debug(f"Daemons Handler: Stopping {daemon}")
control_service('stop', daemon=daemon)
jmv74211 marked this conversation as resolved.
Show resolved Hide resolved