Skip to content

Commit

Permalink
refac: Refactor the test_general_settings_enabled test module #2460
Browse files Browse the repository at this point in the history
  • Loading branch information
jmv74211 committed Feb 7, 2022
1 parent 1018163 commit 38dd975
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


VULN_DETECTOR_FAST_TIMEOUT = 5
VULN_DETECTOR_MEDIUM_TIMEOUT = 10
VULN_DETECTOR_GLOBAL_TIMEOUT = 20
VULN_DETECTOR_EXTENDED_GLOBAL_TIMEOUT = 60
VULN_DETECTOR_SCAN_TIMEOUT = 40
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@ def callback_detect_vulnerability_scan_sleeping(line):
return match.group(1) if match is not None else ""


def callback_detect_vulnerability_detector_disabled(line):
msg = rf"{vd.VULNERABILITY_DETECTOR_PREFIX}DEBUG: Module disabled. Exiting..."
match = re.match(msg, line)

return match is not None


def callback_detect_vulnerability_detector_enabled(line):
msg = r'(.*)wazuh-modulesd:vulnerability-detector(.*)'
match1 = re.match(msg, line)
msg = r'(.*)DEBUG: Module disabled. Exiting...(.*)'
match2 = re.match(msg, line)

return match1 is not None and match2 is None


def check_vuln_detector_event(wazuh_log_monitor=None, callback='', error_message=None, update_position=True,
timeout=vd.VULN_DETECTOR_EXTENDED_GLOBAL_TIMEOUT,
prefix=vd.VULNERABILITY_DETECTOR_PREFIX, accum_results=1):
Expand All @@ -72,6 +56,11 @@ def check_vuln_detector_event(wazuh_log_monitor=None, callback='', error_message
callback=make_vuln_callback(callback, prefix), error_message=error_message)


def check_vulnerability_detector_disabled():
"""Check if the vulnerability detector module is disabled"""
check_vuln_detector_event(callback='DEBUG: Module disabled. Exiting...', timeout=vd.VULN_DETECTOR_MEDIUM_TIMEOUT)


def check_provider_vulnerabilities_number(expected_number):
"""Check if the number of vulnerabilities inserted in VULNERABILITIES table of CVE DB is the expected.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- sections:
- section: vulnerability-detector
elements:
- enabled:
value: ENABLED

- section: sca
elements:
- enabled:
value: 'no'

- section: rootcheck
elements:
- disabled:
value: 'yes'

- section: syscheck
elements:
- disabled:
value: 'yes'

- section: wodle
attributes:
- name: 'syscollector'
elements:
- disabled:
value: 'yes'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

- name: 'disabled'
description: 'Set enabled to no value'
configuration_parameters:
ENABLED: 'no'
metadata:
enabled: 'no'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

- name: 'enabled'
description: 'Set enabled to yes value'
configuration_parameters:
ENABLED: 'yes'
metadata:
enabled: 'yes'
Original file line number Diff line number Diff line change
Expand Up @@ -59,85 +59,109 @@
import os
import pytest

from wazuh_testing.tools import LOG_FILE_PATH
from wazuh_testing.tools.configuration import load_wazuh_configurations, check_apply_test
from wazuh_testing.tools.monitoring import FileMonitor
from wazuh_testing.modules.vulnerability_detector import VULN_DETECTOR_EXTENDED_GLOBAL_TIMEOUT
from wazuh_testing.modules.vulnerability_detector.event_monitor import callback_detect_vulnerability_detector_enabled, \
callback_detect_vulnerability_detector_disabled
from wazuh_testing.tools.configuration import load_configuration_template, get_test_cases_data
from wazuh_testing.modules.vulnerability_detector import event_monitor as evm


# Marks
pytestmark = [pytest.mark.server, pytest.mark.tier(level=0)]
pytestmark = [pytest.mark.server]

# variables
test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
configurations_path = os.path.join(test_data_path, 'wazuh_enabled.yaml')

wazuh_log_monitor = FileMonitor(LOG_FILE_PATH)
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')

parameters = [{'ENABLED': 'yes', 'TAG': 'enabled'}, {'ENABLED': 'no', 'TAG': 'disabled'}]
metadata = [{'enabled': 'yes', 'id': "config_enabled"}, {'enabled': 'no', 'id': "config_disabled"}]
configurations_path = os.path.join(CONFIGURATIONS_PATH, 'enabled.yaml')
enabled_test_cases_path = os.path.join(TEST_CASES_PATH, 'test_enabled.yaml')
disabled_test_cases_path = os.path.join(TEST_CASES_PATH, 'test_disabled.yaml')

# Configuration data
configurations = load_wazuh_configurations(configurations_path, __name__, params=parameters, metadata=metadata)
# Enabled test configuration
enabled_configuration_parameters, enabled_configuration_metadata, enabled_test_case_ids = \
get_test_cases_data(enabled_test_cases_path)
enabled_configurations = load_configuration_template(configurations_path, enabled_configuration_parameters,
enabled_configuration_metadata)
# Disabled test configuration
disabled_configuration_parameters, disabled_configuration_metadata, disabled_test_case_ids = \
get_test_cases_data(disabled_test_cases_path)
disabled_configurations = load_configuration_template(configurations_path, disabled_configuration_parameters,
disabled_configuration_metadata)


# fixtures
@pytest.fixture(scope='module', params=configurations, ids=[f"{x['id']}" for x in metadata])
def get_configuration(request):
"""Get configurations from the module."""
return request.param


@pytest.mark.parametrize('tags_to_apply, custom_callback, custom_error_message', [
({'enabled'}, callback_detect_vulnerability_detector_enabled, 'Vulnerability detector is disabled'),
({'disabled'}, callback_detect_vulnerability_detector_disabled, 'Vulnerability detector is enabled')],
ids = ['expecting_vuldet_enabled', 'expecting_vuldet_disabled']
)
def test_enabled(tags_to_apply, custom_callback, custom_error_message, get_configuration, configure_environment,
restart_modulesd):
@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(enabled_configurations, enabled_configuration_metadata),
ids=enabled_test_case_ids)
def test_enabled(configuration, metadata, set_wazuh_configuration, truncate_log_files, restart_modulesd_function):
'''
description: Check if the `enabled ` option is working correctly. To do this,
description: Check if the `enabled ` option is working correctly when setting `yes`. To do this,
it checks the `ossec.log` file for the message indicating that the
Vulnerability Detector is enabled or disabled.
wazuh_min_version: 4.2.0
parameters:
- tags_to_apply:
type: string
brief: Tags used for use cases.
- custom_callback:
type: string
brief: Custom callback for the use case.
- custom_error_message:
type: string
brief: The message shows the vulnerability detector state.
- get_configuration:
- configuration:
type: dict
brief: Wazuh configuration data. Needed for set_wazuh_configuration fixture.
- metadata:
type: dict
brief: Wazuh configuration metadata.
- set_wazuh_configuration:
type: fixture
brief: Set the wazuh configuration according to the configuration data.
- truncate_log_files:
type: fixture
brief: Get configurations from the module.
- configure_environment:
brief: Truncate the log files at the end of the testing case.
- restart_modulesd_function:
type: fixture
brief: Configure a custom environment for testing.
- restart_modulesd:
type: callable
brief: Restart the `wazuh-modulesd` daemon.
brief: Restart the wazuh-modulesd daemon.
assertions:
- Verify that when the `enabled` option is set to `yes`, the Vulnerability Detector module is running.
- Verify that when the `enabled` option is set to `no`, the Vulnerability Detector module is stopped.
input_description:
- Two use cases are found in the test module and include parameters for `enabled` option (`yes` and `no`).
- The `test_enabled.yaml` file provides the module configuration for this test.
expected_output:
- r'(.*)wazuh-modulesd:vulnerability-detector(.*)'
- r'DEBUG: Module disabled. Exiting...'
- 'Vulnerability detector is disabled'
- 'Vulnerability detector is enabled'
- r'NOT (.*)wazuh-modulesd:vulnerability-detector(.*) Module disabled. Exiting...'
'''
check_apply_test(tags_to_apply, get_configuration['tags'])
with pytest.raises(TimeoutError):
evm.check_vulnerability_detector_disabled()

wazuh_log_monitor.start(timeout=VULN_DETECTOR_EXTENDED_GLOBAL_TIMEOUT, callback=custom_callback,
error_message=custom_error_message)

@pytest.mark.tier(level=0)
@pytest.mark.parametrize('configuration, metadata', zip(disabled_configurations, disabled_configuration_metadata),
ids=disabled_test_case_ids)
def test_disabled(configuration, metadata, set_wazuh_configuration, truncate_log_files, restart_modulesd_function):
'''
description: Check if the `enabled` option is working correctly when setting `no`. To do this,
it checks the `ossec.log` file for the message indicating that the
Vulnerability Detector is enabled or disabled.
wazuh_min_version: 4.2.0
parameters:
- configuration:
type: dict
brief: Wazuh configuration data. Needed for set_wazuh_configuration fixture.
- metadata:
type: dict
brief: Wazuh configuration metadata.
- set_wazuh_configuration:
type: fixture
brief: Set the wazuh configuration according to the configuration data.
- truncate_log_files:
type: fixture
brief: Truncate the log files at the end of the testing case.
- restart_modulesd_function:
type: fixture
brief: Restart the wazuh-modulesd daemon.
assertions:
- Verify that when the `enabled` option is set to `no`, the Vulnerability Detector module does not start.
input_description:
- The `test_disabled.yaml` file provides the module configuration for this test.
expected_output:
- r'(.*)wazuh-modulesd:vulnerability-detector(.*) Module disabled. Exiting...'
'''
evm.check_vulnerability_detector_disabled()

0 comments on commit 38dd975

Please sign in to comment.