Skip to content

Commit

Permalink
Merge pull request #1826 from wazuh/1825_daemon_handler_fixture
Browse files Browse the repository at this point in the history
Create daemon handler fixture for integration test
  • Loading branch information
jmv74211 authored Sep 7, 2021
2 parents 28a0013 + b02648a commit f78f310
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
29 changes: 29 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 @@ -147,6 +151,31 @@ def get_service():
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]


DISABLE_MONITORD_ROTATE_LOG_OPTION = {'monitord.rotate_log': '0'}
REMOTED_LOCAL_INTERNAL_OPTIONS = {'remoted.debug': '2'}.update(DISABLE_MONITORD_ROTATE_LOG_OPTION)
ANALYSISD_LOCAL_INTERNAL_OPTIONS = {'analysisd.debug': '2'}.update(DISABLE_MONITORD_ROTATE_LOG_OPTION)
Expand Down
71 changes: 71 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,77 @@ def create_file_structure_function(get_files_list):
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
all_daemons (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
all_daemons = False

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

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

if 'ignore_errors' in daemons_handler_configuration:
logger.debug(f"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_configuration is not set')
raise daemon_configuration_not_set

try:
if all_daemons:
logger.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"Restarting {daemon}")
# Restart daemon instead of starting due to legacy used fixture in the test suite.
control_service('restart', daemon=daemon)

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

yield

if all_daemons:
logger.debug('Stopping wazuh using wazuh-control')
control_service('stop')
else:
for daemon in daemons:
logger.debug(f"Stopping {daemon}")
control_service('stop', daemon=daemon)


@pytest.fixture(scope='function')
def file_monitoring(request):
"""Fixture to handle the monitoring of a specified file.
Expand Down

0 comments on commit f78f310

Please sign in to comment.