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 13 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
34 changes: 31 additions & 3 deletions deps/wazuh_testing/wazuh_testing/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def get_service():
CLUSTER_SOCKET_PATH = os.path.join(WAZUH_PATH, 'queue', 'cluster')


ANALYSISD_ANALISIS_SOCKET_PATH= os.path.join(QUEUE_SOCKETS_PATH, 'analysis')
ANALYSISD_QUEUE_SOCKET_PATH= os.path.join(QUEUE_SOCKETS_PATH, 'queue')
ANALYSISD_ANALISIS_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'analysis')
ANALYSISD_QUEUE_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'queue')
AUTHD_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'auth')
EXECD_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'com')
LOGCOLLECTOR_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'logcollector')
Expand All @@ -117,10 +117,13 @@ def get_service():
MODULESD_CONTROL_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'control')
MODULESD_KREQUEST_SOCKET_PATH = os.path.join(QUEUE_SOCKETS_PATH, 'krequest')
MODULESD_C_INTERNAL_SOCKET_PATH = os.path.join(CLUSTER_SOCKET_PATH, 'c-internal.sock')
ACTIVE_RESPONSE_SOCKET_PATH = os.path.join(QUEUE_ALERTS_PATH,'ar')
ACTIVE_RESPONSE_SOCKET_PATH = os.path.join(QUEUE_ALERTS_PATH, 'ar')

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
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)
jmv74211 marked this conversation as resolved.
Show resolved Hide resolved

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)
jmv74211 marked this conversation as resolved.
Show resolved Hide resolved