Пример #1
0
def _validate_detect_spec(detect_spec):
    # type: (SNMPDetectSpec) -> None
    if not (isinstance(detect_spec, list) and
            all(isinstance(element, list) for element in detect_spec)):
        raise TypeError("value of 'detect' keyword must be a list of lists of 3-tuples")

    for atom in itertools.chain(*detect_spec):
        if not isinstance(atom, tuple) or not len(atom) == 3:
            raise TypeError("value of 'detect' keyword must be a list of lists of 3-tuples")
        oid_string, expression, expected_match = atom

        if not isinstance(oid_string, str):
            raise TypeError("value of 'detect' keywords first element must be a string: %r" %
                            (oid_string,))
        if not str(oid_string).startswith('.'):
            raise ValueError("OID in value of 'detect' keyword must start with '.': %r" %
                             (oid_string,))
        OIDSpec.validate(oid_string.rstrip('.*'))

        if expression is not None:
            try:
                _ = regex(expression)
            except MKGeneralException as exc:
                raise ValueError("invalid regex in value of 'detect' keyword: %s" % exc)

        if not isinstance(expected_match, bool):
            TypeError("value of 'detect' keywords third element must be a boolean: %r" %
                      (expected_match,))
Пример #2
0
    def _sanitize_oids(oids):
        # type: (List[Union[str, OIDSpec, OIDEnd]]) -> List[Union[OIDSpec, CompatibleOIDEnd]]
        if not isinstance(oids, list):
            raise TypeError("oids must be a list")

        # Remove the "int" once CompatibleOIDEnd is not needed anymore.
        # We must handle int, for legacy code. Typing should prevent us from
        # adding new cases.
        typed_oids = [
            oid if isinstance(oid, (OIDSpec, OIDEnd, int)) else OIDSpec(oid)
            for oid in oids
        ]

        # remaining validations only regard true OIDSpec objects
        oid_specs = [o for o in typed_oids if isinstance(o, OIDSpec)]
        if len(oid_specs) < 2:
            return typed_oids  # type: ignore[return-value] # allow for legacy code

        for oid in oid_specs:
            if str(oid).startswith('.'):
                raise ValueError("column %r must not start with '.'" % (oid, ))

        # make sure the base is as long as possible
        heads_counter = collections.Counter(
            str(oid).split('.', 1)[0] for oid in oid_specs)
        head, count = max(heads_counter.items(), key=lambda x: x[1])
        if count == len(oid_specs) and all(str(o) != head for o in oid_specs):
            raise ValueError("base can be extended by '.%s'" % head)

        return typed_oids  # type: ignore[return-value] # allow for legacy code
Пример #3
0
def test_snmptree(base, oids):
    tree = SNMPTree(base=base, oids=oids)

    assert tree.base == OIDSpec(base)
    assert isinstance(tree.oids, list)
    for oid in tree.oids:
        assert isinstance(oid, (OIDSpec, OIDEnd))
Пример #4
0
def test_oidspec():
    oid_base = OIDSpec(".1.2.3")
    oid_column = OIDBytes("4.5")

    assert str(oid_base) == ".1.2.3"
    assert str(oid_column) == "4.5"

    assert repr(oid_base) == "OIDSpec('.1.2.3')"
    assert repr(oid_column) == "OIDBytes('4.5')"

    oid_sum = oid_base + oid_column
    assert isinstance(oid_sum, OIDBytes)
    assert str(oid_sum) == ".1.2.3.4.5"
Пример #5
0
 def _sanitize_base(base):
     # type: (str) -> OIDSpec
     oid_base = OIDSpec(base)
     if not str(oid_base).startswith('.'):
         raise ValueError("%r must start with '.'" % (oid_base, ))
     return oid_base
Пример #6
0
def test_oidspec_invalid_adding_type(value):
    oid = OIDSpec(".1.2.3")
    with pytest.raises(TypeError):
        _ = oid + value
Пример #7
0
def test_oidspec_invalid_value(value):
    with pytest.raises(ValueError):
        _ = OIDSpec(value)
Пример #8
0
def test_oidspec_invalid_type(value):
    with pytest.raises(TypeError):
        _ = OIDSpec(value)
Пример #9
0
@pytest.mark.parametrize("value", ["", "foo", "1."])
def test_oidspec_invalid_value(value):
    with pytest.raises(ValueError):
        _ = OIDSpec(value)


@pytest.mark.parametrize("value", ["foo", 1])
def test_oidspec_invalid_adding_type(value):
    oid = OIDSpec(".1.2.3")
    with pytest.raises(TypeError):
        _ = oid + value


@pytest.mark.parametrize("left, right", [
    (OIDBytes("4.5"), OIDBytes("4.5")),
    (OIDSpec(".1.2.3"), OIDSpec(".1.2.3")),
])
def test_oidspec_invalid_adding_value(left, right):
    with pytest.raises(ValueError):
        _ = left + right


def test_oidspec():
    oid_base = OIDSpec(".1.2.3")
    oid_column = OIDBytes("4.5")

    assert str(oid_base) == ".1.2.3"
    assert str(oid_column) == "4.5"

    assert repr(oid_base) == "OIDSpec('.1.2.3')"
    assert repr(oid_column) == "OIDBytes('4.5')"