Skip to content

Commit

Permalink
refactor(#2947): update fixture for setting local internal options
Browse files Browse the repository at this point in the history
  • Loading branch information
jmv74211 committed Sep 8, 2022
1 parent 50eeb07 commit 5ac7de7
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 128 deletions.
119 changes: 69 additions & 50 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ def restart_wazuh_daemon_after_finishing(daemon=None):
control_service("restart", daemon=daemon)


@pytest.fixture(scope='function')
def restart_analysisd_function():
"""Restart wazuh-analysisd daemon before starting a test, and stop it after finishing"""
control_service('restart', daemon='wazuh-analysisd')
yield
control_service('stop', daemon='wazuh-analysisd')


@pytest.fixture(scope='module')
def reset_ossec_log(get_configuration, request):
# Reset ossec.log and start a new monitor
Expand Down Expand Up @@ -545,6 +553,67 @@ def connect_to_sockets_configuration(request, get_configuration):
close_sockets(receiver_sockets)


@pytest.fixture(scope='module')
def configure_local_internal_options_module(request):
"""Fixture to configure the local internal options file.
It uses the test variable local_internal_options. This should be
a dictionary wich keys and values corresponds to the internal option configuration, For example:
local_internal_options = {'monitord.rotate_log': '0', 'syscheck.debug': '0' }
"""
try:
local_internal_options = request.param
except AttributeError:
try:
local_internal_options = getattr(request.module, 'local_internal_options')
except AttributeError:
logger.debug('local_internal_options is not set')
raise AttributeError('Error when using the fixture "configure_local_internal_options_module", no ' \
'parameter has been passed explicitly, nor is the variable local_internal_options ' \
'found in the module.') from AttributeError

backup_local_internal_options = conf.get_local_internal_options_dict()

logger.debug(f"Set local_internal_option to {str(local_internal_options)}")
conf.set_local_internal_options_dict(local_internal_options)

yield local_internal_options

logger.debug(f"Restore local_internal_option to {str(backup_local_internal_options)}")
conf.set_local_internal_options_dict(backup_local_internal_options)


@pytest.fixture(scope='function')
def configure_local_internal_options_function(request):
"""Fixture to configure the local internal options file.
It uses the test variable local_internal_options. This should be
a dictionary wich keys and values corresponds to the internal option configuration, For example:
local_internal_options = {'monitord.rotate_log': '0', 'syscheck.debug': '0' }
"""
try:
local_internal_options = request.param
except AttributeError:
try:
local_internal_options = getattr(request.module, 'local_internal_options')
except AttributeError:
logger.debug('local_internal_options is not set')
raise AttributeError('Error when using the fixture "configure_local_internal_options_module", no ' \
'parameter has been passed explicitly, nor is the variable local_internal_options ' \
'found in the module.') from AttributeError

backup_local_internal_options = conf.get_local_internal_options_dict()

logger.debug(f"Set local_internal_option to {str(local_internal_options)}")
conf.set_local_internal_options_dict(local_internal_options)

yield

logger.debug(f"Restore local_internal_option to {str(backup_local_internal_options)}")
conf.set_local_internal_options_dict(backup_local_internal_options)


# DEPRECATED
@pytest.fixture(scope='module')
def configure_local_internal_options(get_local_internal_options):
"""Configure Wazuh local internal options.
Expand Down Expand Up @@ -911,31 +980,6 @@ def file_monitoring(request):
logger.debug(f"Trucanted {file_to_monitor}")


@pytest.fixture(scope='module')
def configure_local_internal_options_module(request):
"""Fixture to configure the local internal options file.
It uses the test variable local_internal_options. This should be
a dictionary wich keys and values corresponds to the internal option configuration, For example:
local_internal_options = {'monitord.rotate_log': '0', 'syscheck.debug': '0' }
"""
try:
local_internal_options = getattr(request.module, 'local_internal_options')
except AttributeError as local_internal_configuration_not_set:
logger.debug('local_internal_options is not set')
raise local_internal_configuration_not_set

backup_local_internal_options = conf.get_local_internal_options_dict()

logger.debug(f"Set local_internal_option to {str(local_internal_options)}")
conf.set_local_internal_options_dict(local_internal_options)

yield

logger.debug(f"Restore local_internal_option to {str(backup_local_internal_options)}")
conf.set_local_internal_options_dict(backup_local_internal_options)


@pytest.fixture(scope='function')
def set_wazuh_configuration(configuration):
"""Set wazuh configuration
Expand All @@ -961,31 +1005,6 @@ def set_wazuh_configuration(configuration):
conf.write_wazuh_conf(backup_config)


@pytest.fixture(scope='function')
def configure_local_internal_options_function(request):
"""Fixture to configure the local internal options file.
It uses the test variable local_internal_options. This should be
a dictionary wich keys and values corresponds to the internal option configuration, For example:
local_internal_options = {'monitord.rotate_log': '0', 'syscheck.debug': '0' }
"""
try:
local_internal_options = getattr(request.module, 'local_internal_options')
except AttributeError as local_internal_configuration_not_set:
logger.debug('local_internal_options is not set')
raise local_internal_configuration_not_set

backup_local_internal_options = conf.get_local_internal_options_dict()

logger.debug(f"Set local_internal_option to {str(local_internal_options)}")
conf.set_local_internal_options_dict(local_internal_options)

yield

logger.debug(f"Restore local_internal_option to {str(backup_local_internal_options)}")
conf.set_local_internal_options_dict(backup_local_internal_options)


@pytest.fixture(scope='function')
def truncate_monitored_files():
"""Truncate all the log files and json alerts files before and after the test execution"""
Expand Down
27 changes: 0 additions & 27 deletions tests/integration/test_analysisd/test_eps/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,6 @@
from wazuh_testing.tools.run_simulator import simulate_agent,syslog_simulator


@pytest.fixture(scope='function')
def restart_analysisd_function():
"""Restart wazuh-analysisd daemon before starting a test, and stop it after finishing"""
control_service('restart', daemon='wazuh-analysisd')
yield
control_service('stop', daemon='wazuh-analysisd')


@pytest.fixture(scope='module')
def configure_local_internal_options_eps(request):
"""Fixture to configure the local internal options file."""
# Define local internal options for EPS tests
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{request.param[0]}"}

# Backup the old local internal options
backup_local_internal_options = configuration.get_wazuh_local_internal_options()

# Set the new local internal options configuration
configuration.set_wazuh_local_internal_options(configuration.create_local_internal_options(local_internal_options))

yield

# Backup the old local internal options cofiguration
configuration.set_wazuh_local_internal_options(backup_local_internal_options)


@pytest.fixture(scope='function')
def set_wazuh_configuration_analysisd(configuration, set_wazuh_configuration, configure_local_internal_options_eps):
"""Set wazuh configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

# Global variables
TIMEFRAME_DEFAULT_VALUE = 10
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{ANALYSISD_STATE_INTERNAL_DEFAULT}"}

# Reference paths
TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
Expand All @@ -29,9 +31,9 @@

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(t1_configurations, t1_configuration_metadata), ids=t1_case_ids)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_without_timeframe(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration_analysisd,
truncate_monitored_files, restart_wazuh_daemon_function):
def test_without_timeframe(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration,
configure_local_internal_options_module, truncate_monitored_files,
restart_wazuh_daemon_function):
'''
description: Check that limits EPS is started when `maximum` is set to a value greater than 0 lower and than 100000,
and `timeframe` is not present. In this case, 'timeframe' will be set with a default value.
Expand Down
13 changes: 7 additions & 6 deletions tests/integration/test_analysisd/test_eps/test_disabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
CONFIGURATIONS_PATH = os.path.join(TEST_DATA_PATH, 'configuration_template')
TEST_CASES_PATH = os.path.join(TEST_DATA_PATH, 'test_cases')
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{ANALYSISD_STATE_INTERNAL_DEFAULT}"}

# Configuration and cases data
t1_configurations_path = os.path.join(CONFIGURATIONS_PATH, 'configuration_disabled.yaml')
Expand All @@ -34,9 +36,8 @@

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(t1_configurations, t1_configuration_metadata), ids=t1_case_ids)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_disabled(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration_analysisd,
truncate_monitored_files, restart_wazuh_daemon_function):
def test_disabled(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration,
configure_local_internal_options_module, truncate_monitored_files, restart_wazuh_daemon_function):
'''
description: Check that limits EPS is not started when `maximum` is set to a value equal to 0, or with an empty
value, and `timeframe` is set to a value greater than 0 and lower than 3600.
Expand Down Expand Up @@ -90,9 +91,9 @@ def test_disabled(configuration, metadata, load_wazuh_basic_configuration, set_w

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(t2_configurations, t2_configuration_metadata), ids=t2_case_ids)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_without_maximum(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration_analysisd,
truncate_monitored_files, restart_wazuh_daemon_function):
def test_without_maximum(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration,
configure_local_internal_options_module, truncate_monitored_files,
restart_wazuh_daemon_function):
'''
description: Check that limits EPS is not started when `maximum` value is not present in the configuration
file.
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/test_analysisd/test_eps/test_enabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
CONFIGURATIONS_PATH = os.path.join(TEST_DATA_PATH, 'configuration_template')
TEST_CASES_PATH = os.path.join(TEST_DATA_PATH, 'test_cases')
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{ANALYSISD_STATE_INTERNAL_DEFAULT}"}

# Configuration and cases data
configurations_path = os.path.join(CONFIGURATIONS_PATH, 'configuration_enabled.yaml')
Expand All @@ -26,9 +28,8 @@

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(t1_configurations, t1_configuration_metadata), ids=t1_case_ids)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_enabled(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration_analysisd,
truncate_monitored_files, restart_wazuh_daemon_function):
def test_enabled(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration,
configure_local_internal_options_module, truncate_monitored_files, restart_wazuh_daemon_function):
'''
description: Check that limits EPS is started when `maximum` is set to a value greater than 0 lower and than 100000,
and `timeframe` is set to a value greater than 0 and lower than 3600.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
CONFIGURATIONS_PATH = os.path.join(TEST_DATA_PATH, 'configuration_template')
TEST_CASES_PATH = os.path.join(TEST_DATA_PATH, 'test_cases')
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{ANALYSISD_STATE_INTERNAL_DEFAULT}"}

# Configuration and cases data
t1_configurations_path = os.path.join(CONFIGURATIONS_PATH, 'configuration_without_timeframe_maximum.yaml')
Expand All @@ -27,10 +29,9 @@

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(t1_configurations, t1_configuration_metadata), ids=t1_case_ids)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_without_timeframe_maximum(configuration, metadata, load_wazuh_basic_configuration,
set_wazuh_configuration_analysisd, truncate_monitored_files,
restart_wazuh_daemon_after_finishing):
set_wazuh_configuration, configure_local_internal_options_module,
truncate_monitored_files, restart_wazuh_daemon_after_finishing):
'''
description: Check that wazuh manager is not started when `maximum` and `timeframe` are not present in the
configuration file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
TEST_DATA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
CONFIGURATIONS_PATH = os.path.join(TEST_DATA_PATH, 'configuration_template')
TEST_CASES_PATH = os.path.join(TEST_DATA_PATH, 'test_cases')
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{ANALYSISD_STATE_INTERNAL_DEFAULT}"}

# Configuration and cases data
configurations_path = os.path.join(CONFIGURATIONS_PATH, 'configuration_invalid_values.yaml')
Expand All @@ -28,9 +30,9 @@

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(t1_configurations, t1_configuration_metadata), ids=t1_case_ids)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_invalid_values(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration_analysisd,
truncate_monitored_files, restart_wazuh_daemon_after_finishing):
def test_invalid_values(configuration, metadata, load_wazuh_basic_configuration, set_wazuh_configuration,
configure_local_internal_options_module, truncate_monitored_files,
restart_wazuh_daemon_after_finishing):
'''
description: Check that wazuh manager is not started when an invalid value is set to `maximum` and/or `timeframe`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# Simulate agent configuration
configurations_simulate_agent_path = os.path.join(TEST_DATA_PATH,
'configuration_simulate_agent.yaml')
local_internal_options = {'wazuh_modules.debug': '2', 'monitord.rotate_log': '0',
'analysisd.state_interval': f"{ANALYSISD_STATE_INTERNAL_DEFAULT}"}

# Get simulate agent configurations (t1)
params_disabled_eps = get_simulate_agent_configuration(configurations_simulate_agent_path)
Expand All @@ -22,8 +24,7 @@


@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
def test_disabled(load_wazuh_basic_configuration, configure_local_internal_options_eps,
def test_disabled(load_wazuh_basic_configuration, configure_local_internal_options_module,
truncate_monitored_files, restart_wazuh_daemon_function):
'''
description: Check that limits EPS is disabled when it is not configured.
Expand Down Expand Up @@ -59,9 +60,8 @@ def test_disabled(load_wazuh_basic_configuration, configure_local_internal_optio


@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configure_local_internal_options_eps', [ANALYSISD_STATE_INTERNAL_DEFAULT], indirect=True)
@pytest.mark.parametrize('simulate_agent_function', [params_disabled_eps], indirect=True)
def test_without_eps_setting(load_wazuh_basic_configuration, configure_local_internal_options_eps,
def test_without_eps_setting(load_wazuh_basic_configuration, configure_local_internal_options_module,
truncate_monitored_files, restart_wazuh_daemon_function, simulate_agent_function):
'''
description: Check that limits EPS is disabled when it is not configured and the received events are similar or
Expand Down
Loading

0 comments on commit 5ac7de7

Please sign in to comment.