def test_scan_function_translation(snmp_scan_functions):
    for name, scan_func in snmp_scan_functions.items():
        assert scan_func is not None

        # make sure we can convert the scan function
        with known_exceptions(name):
            _ = create_detect_spec(name, scan_func)
Exemplo n.º 2
0
def create_snmp_section_plugin_from_legacy(check_plugin_name, check_info_dict,
                                           snmp_scan_function, snmp_info):
    # type: (str, Dict[str, Any], Callable, Any) -> SNMPSectionPlugin
    trees, recover_layout_function = _create_snmp_trees(snmp_info)

    parse_function = _create_snmp_parse_function(
        check_info_dict.get('parse_function'),
        recover_layout_function,
    )

    host_label_function = _create_host_label_function(
        check_info_dict.get('inventory_function'),
        check_info_dict.get('extra_sections', []),
    )

    detect_spec = create_detect_spec(
        check_plugin_name,
        snmp_scan_function,
    )

    return create_snmp_section_plugin(
        name=get_section_name(check_plugin_name),
        parse_function=parse_function,
        host_label_function=host_label_function,
        forbidden_names=[],
        trees=trees,
        detect_spec=detect_spec,
    )
def test_scan_function_translation(snmp_scan_functions):
    for name, scan_func in snmp_scan_functions.items():
        assert scan_func is not None

        # make sure we can convert the scan function
        if name not in KNOWN_FAILURES:
            _ = create_detect_spec(name, scan_func, [])
Exemplo n.º 4
0
def test_snmp_can_functions(name, oids_data, expected_result):
    def oid_function(oid, default=None):
        return oids_data.get(oid, default)

    scan_function = SNMP_SCAN_FUNCTIONS[name]

    assert bool(scan_function(oid_function)) is expected_result

    converted_detect_spec = create_detect_spec(name, scan_function)
    actual_result = snmp_scan._evaluate_snmp_detection(oid_function,
                                                       converted_detect_spec)
    assert actual_result is expected_result
Exemplo n.º 5
0
def test_snmp_can_functions(name, oids_data, expected_result):
    def oid_function(oid, default=None, _name=None):
        # type: (snmp_utils.OID, Optional[snmp_utils.DecodedString], Optional[snmp_utils.CheckPluginName]) -> Optional[snmp_utils.DecodedString]
        return oids_data.get(oid, default)

    scan_function = SNMP_SCAN_FUNCTIONS[name]

    assert bool(scan_function(oid_function)) is expected_result

    converted_detect_spec = create_detect_spec(name, scan_function, [])
    actual_result = snmp_scan._evaluate_snmp_detection(oid_function,
                                                       converted_detect_spec)
    assert actual_result is expected_result
Exemplo n.º 6
0
def test_scan_function_translation(snmp_scan_functions):
    for name, scan_func in snmp_scan_functions.items():
        if name in (
                # these are already migrated manually:
                "ucd_mem",
                "hr_mem",
        ):
            continue

        assert scan_func is not None

        # make sure we can convert the scan function
        if ('section', name) not in KNOWN_AUTO_MIGRATION_FAILURES:
            _ = create_detect_spec(name, scan_func, [])
Exemplo n.º 7
0
def test_evaluate_snmp_detection(config_snmp_scan_functions, name, oids_data,
                                 expected_result):
    def oid_function(oid, _default=None, _name=None):
        return oids_data.get(oid)

    scan_function = config_snmp_scan_functions[name]
    assert bool(scan_function(oid_function)) is expected_result

    converted_detect_spec = create_detect_spec(name, scan_function, [])
    actual_result = evaluate_snmp_detection(
        detect_spec=converted_detect_spec,
        oid_value_getter=oids_data.get,
    )
    assert actual_result is expected_result
Exemplo n.º 8
0
def test_evaluate_snmp_detection(monkeypatch, config_snmp_scan_functions, name, oids_data,
                                 expected_result):
    def oid_function(oid, _default=None, _name=None):
        return oids_data.get(oid)

    monkeypatch.setattr(snmp_modes, "get_single_oid", lambda oid, *a, **kw: oids_data.get(oid))

    scan_function = config_snmp_scan_functions[name]
    assert bool(scan_function(oid_function)) is expected_result

    converted_detect_spec = create_detect_spec(name, scan_function, [])
    actual_result = snmp_scan._evaluate_snmp_detection(
        converted_detect_spec,
        name,
        None,  # type: ignore # not used
        backend=None,  # type: ignore  # monkeypatched
    )
    assert actual_result is expected_result
Exemplo n.º 9
0
def create_snmp_section_plugin_from_legacy(
    check_plugin_name: str,
    check_info_dict: Dict[str, Any],
    snmp_scan_function: Callable,
    snmp_info: Any,
    scan_function_fallback_files: Optional[List[str]] = None
) -> SNMPSectionPlugin:
    if check_info_dict.get("node_info"):
        # We refuse to tranform these. There's no way we get the data layout recovery right.
        # This would add 19 plugins to list of failures, but some are on the list anyway.
        raise NotImplementedError("cannot auto-migrate cluster aware plugins")

    trees, recover_layout_function = _create_snmp_trees(snmp_info)

    parse_function = _create_snmp_parse_function(
        check_info_dict.get('parse_function'),
        recover_layout_function,
    )

    host_label_function = _create_host_label_function(
        check_info_dict.get('inventory_function'),
        check_info_dict.get('extra_sections', []),
    )

    detect_spec = create_detect_spec(
        check_plugin_name,
        snmp_scan_function,
        scan_function_fallback_files or [],
    )

    return create_snmp_section_plugin(
        name=get_section_name(check_plugin_name),
        parse_function=parse_function,
        host_label_function=host_label_function,
        forbidden_names=[],
        trees=trees,
        detect_spec=detect_spec,
    )
Exemplo n.º 10
0
def test_explicit_conversion(check_manager, check_name, func_name):
    scan_func = check_manager.get_check(check_name).context[func_name]
    created = create_detect_spec("unit-test", scan_func, [])
    explicit = _explicit_conversions(scan_func.__name__)
    assert created == explicit