107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
import os
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from config import RuntimeSettings, load_runtime_settings, validate_runtime_settings
|
|
|
|
|
|
class RuntimeSettingsEnforcementTests(unittest.TestCase):
|
|
@patch("config.load_config")
|
|
def test_enforcement_uses_explicit_targets_when_present(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"agent": {"node_name": "n1"},
|
|
"enforcement": {
|
|
"targets": [
|
|
{"family": "ip", "table": "custom_v4"},
|
|
{"family": "inet", "table": "custom_inet"},
|
|
],
|
|
"sets": ["0.0.0.0/0-cscli", "crowdsec-blacklists"],
|
|
},
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertEqual(
|
|
(("ip", "custom_v4"), ("inet", "custom_inet")),
|
|
settings.enforcement_targets,
|
|
)
|
|
self.assertEqual(
|
|
("0.0.0.0/0-cscli", "crowdsec-blacklists"),
|
|
settings.enforcement_sets,
|
|
)
|
|
|
|
@patch("config.load_config")
|
|
def test_enforcement_falls_back_to_table_fields(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"enforcement": {
|
|
"table": "crowdsec_main",
|
|
"table6": "crowdsec6_main",
|
|
"families": ["ip", "ip6"],
|
|
}
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertEqual(
|
|
(("ip", "crowdsec_main"), ("ip6", "crowdsec6_main")),
|
|
settings.enforcement_targets,
|
|
)
|
|
self.assertEqual((), settings.enforcement_sets)
|
|
|
|
@patch("config.load_config")
|
|
def test_check_jitter_pct_loaded_and_clamped(self, load_config_mock):
|
|
load_config_mock.return_value = {
|
|
"agent": {
|
|
"check_jitter_pct": 1.5,
|
|
}
|
|
}
|
|
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
settings = load_runtime_settings()
|
|
|
|
self.assertEqual(0.90, settings.check_jitter_pct)
|
|
|
|
def test_validate_runtime_settings_reports_errors(self):
|
|
settings = RuntimeSettings(
|
|
node_name="n1",
|
|
log_path="/tmp/agent.log",
|
|
state_file="/tmp/state.json",
|
|
check_interval=0,
|
|
check_jitter_pct=0.10,
|
|
heal_verify_delay=-1,
|
|
heal_min_interval=-2,
|
|
enforcement_targets=(),
|
|
enforcement_sets=(),
|
|
config_path="config.yaml",
|
|
)
|
|
|
|
issues = validate_runtime_settings(settings)
|
|
errors = [issue for issue in issues if issue.level == "ERROR"]
|
|
|
|
self.assertGreaterEqual(len(errors), 4)
|
|
|
|
def test_validate_runtime_settings_reports_warn_for_relative_state_file(self):
|
|
settings = RuntimeSettings(
|
|
node_name="n1",
|
|
log_path="/tmp/agent.log",
|
|
state_file="relative-state.json",
|
|
check_interval=600,
|
|
check_jitter_pct=0.10,
|
|
heal_verify_delay=5,
|
|
heal_min_interval=900,
|
|
enforcement_targets=(("ip", "crowdsec"),),
|
|
enforcement_sets=(),
|
|
config_path="config.yaml",
|
|
)
|
|
|
|
issues = validate_runtime_settings(settings)
|
|
warns = [issue.message for issue in issues if issue.level == "WARN"]
|
|
|
|
self.assertTrue(any("state_file is not absolute" in msg for msg in warns))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|