def test_cluster_ignores_nodes_parameters(monkeypatch: MonkeyPatch) -> None: node = HostName("node") cluster = HostName("cluster") service_id = CheckPluginName("smart_temp"), "auto-clustered" ts = Scenario() ts.add_host("node") ts.add_cluster("cluster", nodes=["node"]) ts.set_ruleset( "clustered_services", [([], ["node"], ["Temperature SMART auto-clustered$"])], ) ts.set_autochecks("node", [AutocheckEntry(*service_id, {}, {})]) ts.apply(monkeypatch) # a rule for the node: monkeypatch.setattr( config, "_get_configured_parameters", lambda host, plugin, item: (TimespecificParameters((TimespecificParameterSet.from_parameters( {"levels_for_node": (1, 2)}), )) if host == node else TimespecificParameters()), ) clustered_service = check_table.get_check_table(cluster)[service_id] assert clustered_service.parameters.entries == ( TimespecificParameterSet.from_parameters({"levels": (35, 40)}), )
def test_first_tuple_wins(self): tuple_1: List[Tuple[str, LegacyCheckParameters]] = [("tp3", (1, 1))] tuple_2: List[Tuple[str, LegacyCheckParameters]] = [("tp3", (2, 2))] assert (TimespecificParameters(( TimespecificParameterSet(_default(), _tp_values()), TimespecificParameterSet(_default(), _tp_values() + tuple_1), TimespecificParameterSet(_default(), tuple_2 + _tp_values()), )).evaluate(lambda x: True) == (1, 1))
def test_first_key_wins(self): assert (TimespecificParameters(( TimespecificParameterSet( {"key": "I am only default, but the most specific rule!"}, ()), TimespecificParameterSet( {"key": "default"}, [( "active_tp", { "key": "I am a specificly time-matching value, but from a more general rule!" }, )], ), )).evaluate(lambda x: True) == { "key": "I am only default, but the most specific rule!" })
def test_from_parameters_ts_dict(self): tsp = TimespecificParameterSet.from_parameters( { "tp_default_value": _default(), "tp_values": _tp_values(), } ) assert tsp.default == _default() assert tsp.timeperiod_values == tuple(_tp_values())
def test_evaluate_tp_filtering(self): assert TimespecificParameterSet(_default(), _tp_values()).evaluate( lambda x: x == "tp2" ) == {"default": 42, "value": "from tp2"}
def test_evaluate_first_period_wins(self): assert TimespecificParameterSet(_default(), _tp_values()).evaluate(lambda x: True) == { "default": 42, "value": "from tp1", }
def test_evaluate_no_period_active(self): assert ( TimespecificParameterSet(_default(), _tp_values()).evaluate(lambda x: False) == _default() )
def test_evaluate_constant_reevaluates_to_itself(self): assert TimespecificParameterSet(_default(), ()).evaluate(lambda x: True) == _default()
def test_from_parameters_constant_dict(self): tsp = TimespecificParameterSet.from_parameters(_default()) assert tsp.default == _default() assert not tsp.timeperiod_values
def test_from_paramters_legacy_tuple(self): tsp = TimespecificParameterSet.from_parameters((1, 2)) assert tsp.default == (1, 2) assert not tsp.timeperiod_values
def test_check_table__get_static_check_entries( monkeypatch: MonkeyPatch, check_group_parameters: LegacyCheckParameters) -> None: hostname = HostName("hostname") static_parameters_default = {"levels": (1, 2, 3, 4)} static_checks: Dict[str, List] = { "ps": [(("ps", "item", static_parameters_default), [], [hostname], {})], } ts = Scenario() ts.add_host(hostname) ts.set_option("static_checks", static_checks) ts.set_ruleset( "checkgroup_parameters", { "ps": [(check_group_parameters, [hostname], [], {})], }, ) config_cache = ts.apply(monkeypatch) monkeypatch.setattr( agent_based_register, "get_check_plugin", lambda cpn: CheckPlugin( CheckPluginName("ps"), [], "Process item", lambda: [], None, None, "merged", lambda: [], {}, RuleSetName("ps"), None, None, ), ) host_config = config_cache.get_host_config(hostname) static_check_parameters = [ service.parameters for service in check_table._get_static_check_entries( config_cache, host_config) ] entries = config._get_checkgroup_parameters( config_cache, hostname, "ps", "item", "Process item", ) assert len(entries) == 1 assert entries[0] == check_group_parameters assert len(static_check_parameters) == 1 static_check_parameter = static_check_parameters[0] assert static_check_parameter == TimespecificParameters(( TimespecificParameterSet(static_parameters_default, ()), TimespecificParameterSet({}, ()), ))
@pytest.mark.parametrize( "hostname_str,expected_result", [ ("empty-host", {}), # Skip the autochecks automatically for ping hosts ("ping-host", {}), ( "no-autochecks", { (CheckPluginName("smart_temp"), "/dev/sda"): ConfiguredService( check_plugin_name=CheckPluginName("smart_temp"), item="/dev/sda", description="Temperature SMART /dev/sda", parameters=TimespecificParameters(( TimespecificParameterSet({}, ()), TimespecificParameterSet({"levels": (35, 40)}, ()), )), discovered_parameters=None, service_labels={}, ), }, ), # Static checks overwrite the autocheck definitions ( "autocheck-overwrite", { (CheckPluginName("smart_temp"), "/dev/sda"): ConfiguredService( check_plugin_name=CheckPluginName("smart_temp"), item="/dev/sda",
def make_timespecific_params_list( entries: Iterable[LegacyCheckParameters], ) -> TimespecificParameters: return TimespecificParameters([TimespecificParameterSet.from_parameters(e) for e in entries])