def test__transform_replaced_wato_rulesets_and_params( ruleset_name, param_value, new_ruleset_name, transformed_param_value, ): all_rulesets = RulesetCollection() # checkmk: all_rulesets are loaded via # all_rulesets = cmk.gui.watolib.rulesets.AllRulesets() all_rulesets.set_rulesets({ ruleset_name: _instantiate_ruleset(ruleset_name, param_value), new_ruleset_name: Ruleset(new_ruleset_name, {}), }) uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) uc._transform_replaced_wato_rulesets(all_rulesets) uc._transform_wato_rulesets_params(all_rulesets) assert not all_rulesets.exists(ruleset_name) rules = all_rulesets.get(new_ruleset_name).get_rules() assert len(rules) == 1 rule = rules[0] assert len(rule) == 3 assert rule[2].value == transformed_param_value
def test__migrate_pre_2_0_audit_log_not_existing(tmp_path, old_path, new_path): uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) assert not new_path.exists() assert not old_path.exists() uc._migrate_pre_2_0_audit_log() assert not new_path.exists() assert not old_path.exists()
def test__migrate_pre_2_0_audit_log(tmp_path, old_audit_log, new_path): uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) assert not new_path.exists() assert old_audit_log.exists() uc._migrate_pre_2_0_audit_log() assert new_path.exists() assert not old_audit_log.exists() # Now try to parse the migrated log with the new logic store = AuditLogStore(new_path) assert store.read() == [ AuditLogStore.Entry( time=1604991356, object_ref=None, user_id='cmkadmin', action='liveproxyd-activate', text='Activating changes of Livestatus Proxy configuration', diff_text=None), AuditLogStore.Entry( time=1604991356, object_ref=None, user_id='cmkadmin', action='liveproxyd-activate', text='Activating changes of Livestatus Proxy configuration', diff_text=None), AuditLogStore.Entry(time=1604992040, object_ref=ObjectRef(ObjectRefType.Host, "heute2"), user_id='cmkadmin', action='create-host', text='Created new host heute2.', diff_text=None), AuditLogStore.Entry(time=1604992159, object_ref=ObjectRef(ObjectRefType.Host, "heute2"), user_id='cmkadmin', action='delete-host', text='Deleted host heute2', diff_text=None), AuditLogStore.Entry(time=1604992163, object_ref=ObjectRef(ObjectRefType.Host, "heute1"), user_id='cmkadmin', action='create-host', text='Created new host heute1.', diff_text=None), AuditLogStore.Entry(time=1604992166, object_ref=ObjectRef(ObjectRefType.Host, "heute12"), user_id='cmkadmin', action='create-host', text='Created new host heute12.', diff_text=None), ]
def test__transform_wato_rulesets_params( ruleset_name, param_value, transformed_param_value, ): ruleset = _instantiate_ruleset(ruleset_name, param_value) rulesets = RulesetCollection() rulesets.set_rulesets({ruleset_name: ruleset}) uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) uc._transform_wato_rulesets_params(rulesets) assert len(ruleset.get_rules()[0]) == 3 assert ruleset.get_rules()[0][2].value == transformed_param_value
def test__migrate_pre_2_0_audit_log_not_migrate_already_migrated(tmp_path, old_audit_log, new_path): uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) assert not new_path.exists() assert old_audit_log.exists() new_path.parent.mkdir(exist_ok=True, parents=True) with new_path.open("w") as f: f.write("abc\n") assert new_path.exists() uc._migrate_pre_2_0_audit_log() assert new_path.open().read() == "abc\n" assert old_audit_log.exists()
def test_update_global_config(mocker: MockerFixture) -> None: mocker.patch.object( update_config, "REMOVED_GLOBALS_MAP", [ ("global_a", "new_global_a", { True: 1, False: 0 }), ("global_b", "new_global_b", {}), ("missing", "new_missing", {}), ], ) mocker.patch.object( update_config, "filter_unknown_settings", lambda global_config: {k: v for k, v in global_config.items() if k != "unknown"}, ) mocker.patch.object( update_config.UpdateConfig, "_transform_global_config_value", lambda _self, config_var, config_val: { "new_global_a": config_val, "new_global_b": 15, "global_c": ["x", "y", "z"], "unchanged": config_val, }[config_var], ) assert update_config.UpdateConfig( cmk.utils.log.logger, argparse.Namespace())._update_global_config({ "global_a": True, "global_b": 14, "global_c": None, "unchanged": "please leave me alone", "unknown": "How did this get here?", }) == { "global_c": ["x", "y", "z"], "unchanged": "please leave me alone", "new_global_a": 1, "new_global_b": 15, }
def test__transform_discovery_disabled_services( ruleset_spec, expected_ruleset, ): ruleset = Ruleset("ignored_services", {}) ruleset.from_config(Folder(''), ruleset_spec) assert ruleset.get_rules() rulesets = RulesetCollection() rulesets.set_rulesets({"ignored_services": ruleset}) uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) uc._transform_discovery_disabled_services(rulesets) folder_rules = ruleset.get_folder_rules(Folder('')) assert [r.to_config() for r in folder_rules] == expected_ruleset
def test__rename_discovered_host_label_files_fix_wrong_name(monkeypatch): Scenario().add_host("abc.d").apply(monkeypatch) uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) host_name = "abc.d" old_path = (cmk.utils.paths.discovered_host_labels_dir / host_name).with_suffix(".mk") new_path = cmk.utils.paths.discovered_host_labels_dir / (host_name + ".mk") old_path.parent.mkdir(exist_ok=True, parents=True) with old_path.open("w") as f: f.write("{}\n") assert old_path.exists() assert not new_path.exists() uc._rename_discovered_host_label_files() assert not old_path.exists() assert new_path.exists()
def test_update_config_init() -> None: update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace())
def fixture_uc() -> update_config.UpdateConfig: return update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace())
def uc(): return update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace())
def test__transform_contact_groups(monkeypatch, contact_groups, expected_contact_groups): uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) uc._transform_contact_groups(contact_groups) assert contact_groups == expected_contact_groups