示例#1
0
def test_parse_sections_superseded(monkeypatch):

    host_key, mhs = _get_host_section_for_parse_sections_test()

    monkeypatch.setattr(
        agent_based_register._config,
        "get_section_plugin",
        MOCK_SECTIONS.get,
    )

    assert mhs.determine_applicable_sections(
        {ParsedSectionName("parsed"), ParsedSectionName("parsed_four")},
        host_key.source_type,
    ) == [
        SECTION_FOUR,
    ]
    assert mhs.get_parsed_section(host_key, ParsedSectionName("parsed")) is None
示例#2
0
def _section(name: str, parsed_section_name: str,
             supersedes: Set[str]) -> SectionPlugin:
    section = trivial_section_factory(SectionName(name))
    return section._replace(
        parsed_section_name=ParsedSectionName(parsed_section_name),
        supersedes={SectionName(n)
                    for n in supersedes},
    )
示例#3
0
def create_agent_section_plugin(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: Optional[AgentParseFunction] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    module: Optional[str] = None,
    validate_creation_kwargs: bool = True,
) -> AgentSectionPlugin:
    """Return an AgentSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    section_name = SectionName(name)

    if validate_creation_kwargs:
        if parse_function is not None:
            _validate_parse_function(
                parse_function,
                expected_annotation=(AgentStringTable, "AgentStringTable"),
            )

        if host_label_function is not None:
            validate_function_arguments(
                type_label="host_label",
                function=host_label_function,
                has_item=False,
                # TODO:
                # The following is a special case for the ps plugin. This should be done
                # in a more general sense when CMK-5158 is addressed. Make sure to grep for
                # "CMK-5158" in the code base.
                default_params={} if name in ("ps", "ps_lnx") else None,
                sections=[ParsedSectionName("__always_just_one_section__")],
            )

    return AgentSectionPlugin(
        section_name,
        ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        _create_agent_parse_function(parse_function),
        _create_host_label_function(host_label_function),
        _create_supersedes(section_name, supersedes),
        module,
    )
示例#4
0
    def test_straight_forward_case(self):
        resolver, parser = self.make_provider(section_plugins=[
            _section("section_one", "parsed_section_name", set()),
        ])

        resolved = resolver.resolve(
            parser,  # type: ignore[arg-type]
            ParsedSectionName("parsed_section_name"),
        )
        assert resolved
        parsed, section = resolved
        assert parsed and parsed.data == 1
        assert section and section.name == SectionName("section_one")
        assert resolver.resolve(
            parser,  # type: ignore[arg-type]
            ParsedSectionName("no_such_section"),
        ) is None
示例#5
0
def trivial_section_factory(section_name: SectionName) -> AgentSectionPlugin:
    return AgentSectionPlugin(
        name=section_name,
        parsed_section_name=ParsedSectionName(str(section_name)),
        parse_function=parse_to_string_table,
        host_label_function=_noop_host_label_function,
        supersedes=set(),
        module=None,
    )
示例#6
0
    def test_superseder_is_present(self):
        resolver, parser = self.make_provider(section_plugins=[
            _section("section_one", "parsed_section_one", set()),
            _section("section_two", "parsed_section_two", {"section_one"}),
        ])

        assert resolver.resolve(
            parser,  # type: ignore[arg-type]
            ParsedSectionName("parsed_section_one"),
        ) is None
示例#7
0
def test_create_snmp_section_plugin():

    trees: List[SNMPTree] = [
        section_types.SNMPTree(
            base='.1.2.3',
            oids=[OIDEnd(), '2.3'],
        ),
    ]

    detect = [
        [('.1.2.3.4.5', 'Foo.*', True)],
    ]

    with pytest.raises(NotImplementedError):
        plugin = section_plugins.create_snmp_section_plugin(
            name="norris",
            parsed_section_name="chuck",
            parse_function=_parse_dummy,
            trees=trees,
            detect_spec=detect,
            supersedes=None,
            forbidden_names=[],
        )

    with pytest.raises(NotImplementedError):
        plugin = section_plugins.create_snmp_section_plugin(
            name="norris",
            parsed_section_name=None,
            parse_function=_parse_dummy,
            trees=trees,
            detect_spec=detect,
            supersedes=["Foo", "Bar"],
            forbidden_names=[],
        )

    plugin = section_plugins.create_snmp_section_plugin(
        name="norris",
        parsed_section_name=None,  # "chuck",
        parse_function=_parse_dummy,
        trees=trees,
        detect_spec=detect,
        supersedes=None,  # ["Foo", "Bar"],
        forbidden_names=[],
    )

    assert isinstance(plugin, section_types.SNMPSectionPlugin)
    assert len(plugin) == 8
    assert plugin.name == SectionName("norris")
    assert plugin.parsed_section_name == ParsedSectionName(
        "norris")  # "chuck")
    assert plugin.parse_function is _parse_dummy
    assert plugin.host_label_function is section_plugins._noop_host_label_function
    assert plugin.detect_spec == detect
    assert plugin.trees == trees
    assert plugin.supersedes == []  # [SectionName("Bar"), SectionName("Foo")]
示例#8
0
def _test_section(
    *,
    section_name: str,
    parsed_section_name: str,
    parse_function: Callable,
    supersedes: Iterable[str],
) -> section_plugins.AgentSectionPlugin:
    return section_plugins.trivial_section_factory(SectionName(section_name))._replace(
        parsed_section_name=ParsedSectionName(parsed_section_name),
        parse_function=parse_function,
        supersedes={SectionName(n) for n in supersedes},
    )
示例#9
0
def trivial_section_factory(section_name: SectionName) -> AgentSectionPlugin:
    return AgentSectionPlugin(
        name=section_name,
        parsed_section_name=ParsedSectionName(str(section_name)),
        parse_function=lambda string_table: string_table,
        host_label_function=_noop_host_label_function,
        host_label_default_parameters=None,
        host_label_ruleset_name=None,
        host_label_ruleset_type="merged",  # doesn't matter, use default.
        supersedes=set(),
        module=None,
    )
示例#10
0
def test_get_parsed_section(patch_register, node_sections, expected_result):

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey("node1", "127.0.0.1", SourceType.HOST): SectionsParser(host_sections=node_sections)
    })

    content = parsed_sections_broker.get_parsed_section(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        ParsedSectionName("parsed"),
    )

    assert expected_result == content
示例#11
0
def test_create_check_plugin():
    plugin = check_plugins.create_check_plugin(**MINIMAL_CREATION_KWARGS)

    assert plugin.name == CheckPluginName(MINIMAL_CREATION_KWARGS["name"])
    assert plugin.sections == [ParsedSectionName(MINIMAL_CREATION_KWARGS["name"])]
    assert plugin.service_name == MINIMAL_CREATION_KWARGS["service_name"]
    assert plugin.discovery_function.__name__ == MINIMAL_CREATION_KWARGS[
        "discovery_function"].__name__
    assert plugin.discovery_default_parameters == {}
    assert plugin.discovery_ruleset_name is None
    assert plugin.check_function.__name__ == MINIMAL_CREATION_KWARGS["check_function"].__name__
    assert plugin.check_default_parameters == {}
    assert plugin.check_ruleset_name is None
示例#12
0
def create_snmp_section_plugin(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: SNMPParseFunction,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    detect_spec: SNMPDetectSpec,
    trees: List[SNMPTree],
    module: Optional[str] = None,
    rule_dependent_detect_spec: Optional[SNMPRuleDependentDetectSpec] = None,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.

    Args:
        rule_dependent_detect_spec: Not an official part of the API. Used for dynamically computing
        SNMP detection conditions based on the configured discovery rules for the current host. This
        is needed by for example needed by some interface checks.
    """
    section_name = SectionName(name)

    if any(isinstance(oid, OIDBytes) for tree in trees for oid in tree.oids):
        expected_annotation: Tuple[Type, str] = (SNMPStringByteTable,
                                                 "SNMPStringByteTable")
    else:
        expected_annotation = (SNMPStringTable, "SNMPStringTable")

    _validate_parse_function(parse_function,
                             expected_annotation=expected_annotation)
    _validate_detect_spec(detect_spec)
    _validate_snmp_trees(trees)

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        parse_function,
        _create_host_label_function(
            host_label_function,
            default_params=None,
        ),
        _create_supersedes(section_name, supersedes),
        detect_spec,
        trees,
        module,
        rule_dependent_detect_spec,
    )
示例#13
0
def create_agent_section_plugin(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: Optional[AgentParseFunction] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    host_label_default_parameters: Optional[ParametersTypeAlias] = None,
    host_label_ruleset_name: Optional[str] = None,
    host_label_ruleset_type: RuleSetType = RuleSetType.MERGED,
    supersedes: Optional[List[str]] = None,
    module: Optional[str] = None,
    validate_creation_kwargs: bool = True,
) -> AgentSectionPlugin:
    """Return an AgentSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    section_name = SectionName(name)

    if validate_creation_kwargs:
        if parse_function is not None:
            _validate_parse_function(
                parse_function,
                expected_annotation=_create_parse_annotation(),
            )

        if host_label_function is not None:
            _validate_host_label_kwargs(
                host_label_function=host_label_function,
                host_label_default_parameters=host_label_default_parameters,
                host_label_ruleset_name=host_label_ruleset_name,
                host_label_ruleset_type=host_label_ruleset_type,
            )

    return AgentSectionPlugin(
        name=section_name,
        parsed_section_name=ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        parse_function=_create_agent_parse_function(parse_function),
        host_label_function=_create_host_label_function(host_label_function),
        host_label_default_parameters=host_label_default_parameters,
        host_label_ruleset_name=(None if host_label_ruleset_name is None else
                                 RuleSetName(host_label_ruleset_name)),
        host_label_ruleset_type=("merged" if
                                 host_label_ruleset_type is RuleSetType.MERGED
                                 else "all"),
        supersedes=_create_supersedes(section_name, supersedes),
        module=module,
    )
def test_get_parsed_section(patch_register, node_section_content, expected_result):

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey("node1", "127.0.0.1", SourceType.HOST):
            AgentHostSections(sections=node_section_content)
    })

    content = parsed_sections_broker.get_parsed_section(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        ParsedSectionName("parsed"),
    )

    assert expected_result == content,\
           "Section content: Expected '%s' but got '%s'" % (expected_result, content)
示例#15
0
    def test_superseder_with_same_name(self):
        resolver, parser = self.make_provider(section_plugins=[
            _section("section_one", "parsed_section", set()),
            _section("section_two", "parsed_section", {"section_one"}),
        ])

        resolved = resolver.resolve(
            parser,  # type: ignore[arg-type]
            ParsedSectionName("parsed_section"),
        )
        assert resolved
        parsed, section = resolved
        assert parsed and parsed.data == 2
        assert section and section.name == SectionName("section_two")
示例#16
0
def test_get_parsed_section(patch_register, node_section_content, expected_result):

    multi_host_sections = MultiHostSections()
    multi_host_sections.setdefault(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        AgentHostSections(sections=node_section_content),
    )

    content = multi_host_sections.get_parsed_section(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        ParsedSectionName("parsed"),
    )

    assert expected_result == content,\
           "Section content: Expected '%s' but got '%s'" % (expected_result, content)
def fixture_check_plugin(monkeypatch):
    return CheckPlugin(
        CheckPluginName("unit_test_check_plugin"),
        [ParsedSectionName("norris")],
        "Unit Test",
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
        None,  # type: ignore[arg-type]  # irrelevant for test
    )
示例#18
0
def test_create_agent_section_plugin():
    plugin = section_plugins.create_agent_section_plugin(
        name="norris",
        parsed_section_name="chuck",
        parse_function=_parse_dummy,
        supersedes=["foo", "bar"],
    )

    assert isinstance(plugin, AgentSectionPlugin)
    assert len(plugin) == 6
    assert plugin.name == SectionName("norris")
    assert plugin.parsed_section_name == ParsedSectionName("chuck")
    assert plugin.parse_function is _parse_dummy
    assert plugin.host_label_function is section_plugins._noop_host_label_function
    assert plugin.supersedes == {SectionName("bar"), SectionName("foo")}
示例#19
0
def test_rank_section_by_supersedes_with_supersedings():

    available = [
        _create_creator("ding", "dong", ["moo"]),  # filter this out!
        _create_creator("foo1", "desired",
                        ["ding", "moo", "foo3"]),  # one of three is relevant
        _create_creator("foo2", "desired",
                        ["zfoo4", "afoo5"]),  # only two, but all relevant
        _create_creator("foo3", "desired", ["ding"]),
        _create_creator("zfoo4", "desired", []),
        _create_creator("afoo5", "desired", []),
    ]
    assert utils.rank_sections_by_supersedes(
        available,
        {ParsedSectionName("desired")},
    ) == [available[i][1] for i in (2, 1, 5, 3, 4)]
def test_create_inventory_plugin_minimal():
    plugin = inventory_plugins.create_inventory_plugin(
        name="norris",
        inventory_function=dummy_generator,
    )

    assert isinstance(plugin, InventoryPlugin)
    assert plugin.name == InventoryPluginName("norris")
    assert plugin.sections == [ParsedSectionName("norris")]
    assert plugin.inventory_function.__name__ == "dummy_generator"
    assert plugin.inventory_default_parameters == {}
    assert plugin.inventory_ruleset_name is None
    assert plugin.module is None

    with pytest.raises(TypeError):
        _ = list(plugin.inventory_function(None))
示例#21
0
def test_get_section_cluster_kwargs(required_sections: Sequence[str],
                                    expected_result: Dict[str, Any]) -> None:

    node1_sections = HostSections[AgentRawDataSection](
        sections={
            SectionName("one"): NODE_1,
            SectionName("two"): NODE_1,
            SectionName("three"): NODE_1,
        })

    node2_sections = HostSections[AgentRawDataSection](
        sections={
            SectionName("two"): NODE_2,
            SectionName("three"): NODE_2,
        })

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST):
        (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ], ),
            SectionsParser(host_sections=node1_sections,
                           host_name=HostName("node1")),
        ),
        HostKey(HostName("node2"), HostAddress("127.0.0.1"), SourceType.HOST):
        (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ], ),
            SectionsParser(host_sections=node2_sections,
                           host_name=HostName("node2")),
        ),
    })

    kwargs = get_section_cluster_kwargs(
        parsed_sections_broker,
        [
            HostKey(HostName("node1"), HostAddress("127.0.0.1"),
                    SourceType.HOST),
            HostKey(HostName("node2"), HostAddress("127.0.0.1"),
                    SourceType.HOST),
        ],
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs
示例#22
0
def test_get_parsed_section(node_sections, expected_result):

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey("node1", "127.0.0.1", SourceType.HOST): (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ], ),
            SectionsParser(host_sections=node_sections),
        ),
    })

    content = parsed_sections_broker.get_parsed_section(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        ParsedSectionName("parsed"),
    )

    assert expected_result == content
示例#23
0
def test_create_host_label_function(disco_func, labels_expected):
    host_label_function = section_plugins_legacy._create_host_label_function(
        disco_func, ["some_extra_section"])

    assert host_label_function is not None
    section_plugins.validate_function_arguments(
        "host_label",
        host_label_function,
        has_item=False,
        has_params=False,
        sections=[ParsedSectionName("__only_one_seciton__")],
    )

    # check that we can pass an un-unpackable argument now!
    actual_labels = list(host_label_function({"parse": "result"}))

    assert actual_labels == labels_expected
示例#24
0
def test_create_check_plugin_from_legacy_wo_params(monkeypatch):
    monkeypatch.setattr(config, '_check_contexts', {"norris": {}})

    plugin = check_plugins_legacy.create_check_plugin_from_legacy(
        "norris",
        MINIMAL_CHECK_INFO,
        [],
    )

    assert plugin.name == CheckPluginName("norris")
    assert plugin.sections == [ParsedSectionName("norris")]
    assert plugin.service_name == MINIMAL_CHECK_INFO["service_description"]
    assert plugin.discovery_function.__name__ == 'discovery_migration_wrapper'
    assert plugin.discovery_default_parameters == {}
    assert plugin.discovery_ruleset_name is None
    assert plugin.check_function.__name__ == 'check_migration_wrapper'
    assert plugin.check_default_parameters == {}
    assert plugin.check_ruleset_name is None
    assert plugin.cluster_check_function.__name__ == "cluster_legacy_mode_from_hell"
def test_get_parsed_section(node_sections: HostSections[AgentRawDataSection],
                            expected_result: Mapping) -> None:

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST):
        (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ], ),
            SectionsParser(host_sections=node_sections),
        ),
    })

    content = parsed_sections_broker.get_parsed_section(
        HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST),
        ParsedSectionName("parsed"),
    )

    assert expected_result == content
def test_create_snmp_section_plugin_from_legacy():

    plugin = section_plugins_legacy.create_snmp_section_plugin_from_legacy(
        "norris",
        {
            'parse_function': old_school_parse_function,
            'inventory_function': old_school_discover_function,
        },
        old_school_scan_function,
        (".1.2.3.4.5", ["2", 3]),
    )

    assert plugin.name == SectionName("norris")
    assert plugin.parsed_section_name == ParsedSectionName("norris")
    assert plugin.parse_function.__name__ == "old_school_parse_function"
    assert plugin.host_label_function.__name__ == "host_label_function"
    assert plugin.supersedes == []
    assert plugin.detect_spec == [[(".1.2.3.4.5", "norris.*", True)]]
    assert plugin.trees == [SNMPTree(base=".1.2.3.4.5", oids=["2", "3"])]
示例#27
0
def create_snmp_section_plugin(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: SNMPParseFunction,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    detect_spec: SNMPDetectSpec,
    trees: List[SNMPTree],
    module: Optional[str] = None,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    # TODO (mo): Well, implement it, and remove pragma below!
    if supersedes is not None:
        raise NotImplementedError("supersedes is not yet available")
    if parsed_section_name is not None:
        raise NotImplementedError("parsed_section_name is not yet available")

    section_name = SectionName(name)

    if any(isinstance(oid, OIDBytes) for tree in trees for oid in tree.oids):
        expected_annotation: Tuple[Type, str] = (SNMPStringByteTable, "SNMPStringByteTable")
    else:
        expected_annotation = (SNMPStringTable, "SNMPStringTable")

    _validate_parse_function(parse_function, expected_annotation=expected_annotation)
    _validate_detect_spec(detect_spec)
    _validate_snmp_trees(trees)

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(parsed_section_name if parsed_section_name else str(section_name)),
        parse_function,
        _create_host_label_function(host_label_function),
        _create_supersedes(supersedes),
        detect_spec,
        trees,
        module,
    )
示例#28
0
def test_create_check_plugin_from_legacy_wo_params():

    plugin = check_plugins_legacy.create_check_plugin_from_legacy(
        "norris",
        MINIMAL_CHECK_INFO,
        [],
        {},  # factory_settings
        lambda _x: {},  # get_check_context
    )

    assert plugin.name == CheckPluginName("norris")
    assert plugin.sections == [ParsedSectionName("norris")]
    assert plugin.service_name == MINIMAL_CHECK_INFO["service_description"]
    assert plugin.discovery_function.__name__ == "discovery_migration_wrapper"
    assert plugin.discovery_default_parameters is None
    assert plugin.discovery_ruleset_name is None
    assert plugin.check_function.__name__ == "check_migration_wrapper"
    assert plugin.check_default_parameters == {}
    assert plugin.check_ruleset_name is None
    assert plugin.cluster_check_function is None
def test_get_section_kwargs(patch_register, required_sections, expected_result):

    node_section_content = {
        SectionName("one"): NODE_1,
        SectionName("two"): NODE_1,
        SectionName("three"): NODE_1
    }

    host_key = HostKey("node1", "127.0.0.1", SourceType.HOST)

    parsed_sections_broker = ParsedSectionsBroker(
        {host_key: AgentHostSections(sections=node_section_content)})

    kwargs = parsed_sections_broker.get_section_kwargs(
        host_key,
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs,\
           "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
示例#30
0
def test_get_section_kwargs(patch_register, required_sections, expected_result):

    node_sections = AgentHostSections(sections={
        SectionName("one"): NODE_1,
        SectionName("two"): NODE_1,
        SectionName("three"): NODE_1
    })

    host_key = HostKey("node1", "127.0.0.1", SourceType.HOST)

    parsed_sections_broker = ParsedSectionsBroker({
        host_key: SectionsParser(host_sections=node_sections),
    })

    kwargs = parsed_sections_broker.get_section_kwargs(
        host_key,
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs