def test_tag_config_insert_tag_group_missing_multiple_tags_empty( cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="Only one tag may be empty"): tg = tags.TagGroup.from_config({ "id": "tgid3", "topic": "Topics", "title": "titlor", "tags": [ { "id": None, "title": "tagid2", "aux_tags": [] }, { "id": "", "title": "tagid3", "aux_tags": [] }, ], }) cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_insert_tag_group_missing_title(cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="Please specify"): tg = tags.TagGroup() tg.id = "abc" tg.title = "" cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_insert_tag_group_missing_tag_not_unique( cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="must be unique"): tg = tags.TagGroup.from_config({ "id": "tgid4", "topic": "Topics", "title": "titlor", "tags": [ { "id": "ding", "title": "tagid2", "aux_tags": [] }, { "id": "ding", "title": "tagid3", "aux_tags": [] }, ], }) cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_insert_tag_group_missing_tag_not_unique( cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="must be unique"): tg = tags.TagGroup(("tgid4", "Topics/titlor", [ ("ding", "tagid2", []), ("ding", "tagid3", []), ])) cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_insert_tag_group_missing_multiple_tags_empty( cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="Only one tag may be empty"): tg = tags.TagGroup(("tgid3", "Topics/titlor", [ (None, "tagid2", []), ("", "tagid3", []), ])) cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_insert_tag_group_missing_id(cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="Please specify"): tg = tags.TagGroup(group_id="", title="title", help=None, topic=None, tags=[]) cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_get_tag_or_aux_tag_duplicate(test_cfg: tags.TagConfig) -> None: tag_none_choice_1 = test_cfg.get_tag_or_aux_tag("none_choice", "none_val") assert isinstance(tag_none_choice_1, tags.GroupedTag) assert tag_none_choice_1.title == "None value" assert tag_none_choice_1.group.id == "none_choice" tag_none_choice_2 = test_cfg.get_tag_or_aux_tag("none_2", "none_val") assert isinstance(tag_none_choice_2, tags.GroupedTag) assert tag_none_choice_2.title == "None value 2" assert tag_none_choice_2.group.id == "none_2"
def test_tag_config_insert_tag_group_twice(cfg: tags.TagConfig) -> None: cfg.insert_tag_group(tags.TagGroup(("tgid2", "Topics/titlor", [("tgid2", "tagid2", [])]))) assert cfg.tag_groups[-1].id == "tgid2" cfg.insert_tag_group(tags.TagGroup(("tgidX", "Topics/titlor", [("tgid2", "tagid2", [])]))) cfg.validate_config() with pytest.raises(MKGeneralException, match="is used twice"): cfg.insert_tag_group(tags.TagGroup(("tgid2", "Topics/titlor", [("tgid3", "tagid3", [])]))) cfg.validate_config()
def test_tag_config_insert_tag_group_no_tag(cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="at least one tag"): tg = tags.TagGroup.from_config({ "id": "tgid7", "topic": "Topics", "title": "titlor", "tags": [] }) cfg.insert_tag_group(tg) cfg.validate_config()
def test_ruleset_matcher_get_host_ruleset_values_tags_duplicate_ids( monkeypatch: MonkeyPatch, rule_spec: RuleConditionsSpec, expected_result: Sequence[RuleValue], ) -> None: ts = Scenario() add_tag_config = TagConfig() add_tag_config.parse_config({ "aux_tags": [], "tag_groups": [ { "id": "grp1", "tags": [ { "aux_tags": [], "id": "v1", "title": "Value1", }, ], "title": "Group 1", }, { "id": "grp2", "tags": [ { "aux_tags": [], "id": "v1", "title": "Value1", }, ], "title": "Group 2", }, ], }) ts.tags += add_tag_config ts.add_host( "host", tags={ "grp1": "v1", }, ) config_cache = ts.apply(monkeypatch) matcher = config_cache.ruleset_matcher assert (list( matcher.get_host_ruleset_values( RulesetMatchObject( host_name=HostName("host"), service_description=None, ), ruleset=[rule_spec], is_binary=False, )) == expected_result)
def test_ruleset_matcher_get_host_ruleset_values_tags_duplicate_ids( monkeypatch: MonkeyPatch, rule_spec: RuleSpec, expected_result: Sequence[RuleValue], ) -> None: ts = Scenario() add_tag_config = TagConfig() add_tag_config.parse_config({ 'aux_tags': [], 'tag_groups': [ { 'id': 'grp1', 'tags': [ { 'aux_tags': [], 'id': 'v1', 'title': 'Value1', }, ], 'title': 'Group 1' }, { 'id': 'grp2', 'tags': [ { 'aux_tags': [], 'id': 'v1', 'title': 'Value1', }, ], 'title': 'Group 2' }, ], }) ts.tags += add_tag_config ts.add_host( "host", tags={ "grp1": "v1", }, ) config_cache = ts.apply(monkeypatch) matcher = config_cache.ruleset_matcher assert list( matcher.get_host_ruleset_values( RulesetMatchObject( host_name="host", service_description=None, ), ruleset=[rule_spec], is_binary=False, )) == expected_result
def test_tag_config_insert_tag_group_aux_tag_id_conflict(cfg: tags.TagConfig) -> None: cfg.aux_tag_list.append(tags.AuxTag(("bla", "BLAAAA"))) tg = tags.TagGroup( ( "tgid6", "Topics/titlor", [ ("bla", "tagid2", []), ], ) ) cfg.insert_tag_group(tg) cfg.validate_config() with pytest.raises(MKGeneralException, match="is used twice"): tg = tags.TagGroup( ( "bla", "Topics/titlor", [ ("tagid2", "tagid2", []), ], ) ) cfg.insert_tag_group(tg) cfg.validate_config()
def test_tag_config_update_tag_group(test_cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="Unknown tag group"): test_cfg.update_tag_group( tags.TagGroup.from_config({ "id": "tgid2", "topic": "Topics", "title": "titlor", "tags": [{ "id": "tgid2", "title": "tagid2", "aux_tags": [] }], })) test_cfg.validate_config() test_cfg.update_tag_group( tags.TagGroup.from_config({ "id": "networking", "title": "title", "tags": [{ "id": "tgid2", "title": "tagid2", "aux_tags": [] }], })) assert test_cfg.tag_groups[1].title == "title" test_cfg.validate_config()
def test_tag_config_insert_tag_group_twice(cfg: tags.TagConfig) -> None: cfg.insert_tag_group( tags.TagGroup.from_config({ "id": "tgid2", "topic": "Topics", "title": "titlor", "tags": [{ "id": "tgid2", "title": "tagid2", "aux_tags": [] }], })) assert cfg.tag_groups[-1].id == "tgid2" cfg.insert_tag_group( tags.TagGroup.from_config({ "id": "tgidX", "topic": "Topics", "title": "titlor", "tags": [{ "id": "tgid2", "title": "tagid2", "aux_tags": [] }], })) cfg.validate_config() with pytest.raises(MKGeneralException, match="is used twice"): cfg.insert_tag_group( tags.TagGroup.from_config({ "id": "tgid2", "topic": "Topics", "title": "titlor", "tags": [{ "id": "tgid3", "title": "tagid3", "aux_tags": [] }], })) cfg.validate_config()
def test_tag_config_get_tag_group_choices(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_tag_group_choices() == [ ('criticality', u'Blubberei / Criticality'), ('networking', u'Networking Segment'), ('none_choice', u'None choice'), ('none_2', 'None 2'), ]
def test_tag_group_get_tag_group_config(test_cfg: tags.TagConfig) -> None: tg = test_cfg.get_tag_group("criticality") assert tg is not None assert tg.get_tag_group_config("prod") == { "bla": "bla", "criticality": "prod" }
def test_tag_group_get_tag_group_config(test_cfg: tags.TagConfig) -> None: tg = test_cfg.get_tag_group("criticality") assert tg is not None assert tg.get_tag_group_config("prod") == { 'bla': 'bla', 'criticality': 'prod' }
def test_tag_config_get_tag_group_choices(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_tag_group_choices() == [ ("criticality", "Blubberei / Criticality"), ("networking", "Networking Segment"), ("none_choice", "None choice"), ("none_2", "None 2"), ]
def test_tag_config_get_topic_choices(test_cfg: tags.TagConfig) -> None: assert sorted(test_cfg.get_topic_choices()) == sorted( [ ("Blubberei", "Blubberei"), ("Bluna", "Bluna"), ("Tags", "Tags"), ] )
def test_tag_config_update_tag_group(test_cfg: tags.TagConfig) -> None: with pytest.raises(MKGeneralException, match="Unknown tag group"): test_cfg.update_tag_group( tags.TagGroup(("tgid2", "Topics/titlor", [("tgid2", "tagid2", [])])) ) test_cfg.validate_config() test_cfg.update_tag_group(tags.TagGroup(("networking", "title", [("tgid2", "tagid2", [])]))) assert test_cfg.tag_groups[1].title == "title" test_cfg.validate_config()
def test_tag_config_remove_tag_group(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_tag_group("xyz") is None test_cfg.remove_tag_group("xyz") # not existing -> fine assert test_cfg.get_tag_group("networking") is not None test_cfg.remove_tag_group("networking") assert test_cfg.get_tag_group("networking") is None
def test_tag_config_get_aux_tags_by_topic(test_cfg: tags.TagConfig) -> None: expected_groups = { "Bluna": ["bla"], } actual_groups = dict(test_cfg.get_aux_tags_by_topic()) assert sorted(actual_groups.keys()) == sorted(expected_groups.keys()) for topic, tag_group_ids in expected_groups.items(): tg_ids = [tg.id for tg in actual_groups[topic]] assert sorted(tg_ids) == sorted(tag_group_ids)
def test_iadd_tag_config(test_cfg: tags.TagConfig) -> None: cfg2 = tags.TagConfig() cfg2.insert_tag_group( tags.TagGroup.from_config({ "id": "tgid3", "title": "titlor", "topic": "Topics", "tags": [{ "id": "tgid3", "title": "tagid3", "aux_tags": [], }], })) cfg2.insert_tag_group( tags.TagGroup.from_config({ "id": "tgid2", "title": "BLAAA", "tags": [{ "id": "tgid2", "title": "tagid2", "aux_tags": [] }], })) cfg2.aux_tag_list.append( tags.AuxTag.from_config({ "id": "blub", "title": "BLUB" })) cfg2.aux_tag_list.append( tags.AuxTag.from_config({ "id": "bla", "title": "BLUB" })) test_cfg += cfg2 assert len(test_cfg.tag_groups) == 6 assert test_cfg.tag_groups[0].id == "criticality" assert test_cfg.tag_groups[1].id == "networking" assert test_cfg.tag_groups[2].id == "none_choice" assert test_cfg.tag_groups[3].id == "none_2" assert test_cfg.tag_groups[4].id == "tgid3" assert test_cfg.tag_groups[4].title == "titlor" aux_tags = test_cfg.get_aux_tags() assert len(aux_tags) == 2 assert aux_tags[0].id == "bla" assert aux_tags[0].title == "bläää" assert aux_tags[1].id == "blub"
def update_tag_config(tag_config: TagConfig): """Persist the tag config saving the information to the mk file and update the current environment Args: tag_config: The tag config object to persist """ TagConfigFile().save(tag_config.get_dict_format()) _update_tag_dependencies()
def test_tag_groups_by_topic(test_cfg: tags.TagConfig) -> None: expected_groups = { "Blubberei": ["criticality"], "Tags": ["networking", "none_choice", "none_2"], } actual_groups = dict(test_cfg.get_tag_groups_by_topic()) assert sorted(actual_groups.keys()) == sorted(expected_groups.keys()) for topic, tag_group_ids in expected_groups.items(): tg_ids = [tg.id for tg in actual_groups[topic] if tg.id is not None] assert sorted(tg_ids) == sorted(tag_group_ids)
def update_tag_config(tag_config: TagConfig): """Persist the tag config saving the information to the mk file and update the current environment Args: tag_config: The tag config object to persist """ if user: user.need_permission("wato.hosttags") TagConfigFile().save(tag_config.get_dict_format()) _update_tag_dependencies()
def test_tag_config_get_aux_tags_by_tag(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_aux_tags_by_tag() == { None: ["bla"], "none_val": ["bla"], # none_val from none_2 overwrites none_val from none_choice "none_val_2": [], "critical": [], "dmz": [], "lan": [], "offline": [], "prod": ["bla"], "test": [], "wan": [], }
def test_tag_config_get_aux_tags_by_tag(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_aux_tags_by_tag() == { None: ['bla'], 'none_val': ['bla'], # none_val from none_2 overwrites none_val from none_choice 'none_val_2': [], 'critical': [], 'dmz': [], 'lan': [], 'offline': [], 'prod': ['bla'], 'test': [], 'wan': [], }
def test_tag_config_get_tag_ids(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_tag_ids() == { None, 'none_val', 'bla', 'critical', 'dmz', 'lan', 'offline', 'prod', 'test', 'wan', 'none_val_2', }
def test_tag_config_get_tag_ids(test_cfg: tags.TagConfig) -> None: assert test_cfg.get_tag_ids() == { None, "none_val", "bla", "critical", "dmz", "lan", "offline", "prod", "test", "wan", "none_val_2", }