def _validate_detect_spec(detect_spec: 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,))
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))
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')"
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"
def test_oidspec_invalid_adding_type(value): oid = OIDSpec(".1.2.3") with pytest.raises(TypeError): _ = oid + value
def test_oidspec_invalid_value(value): with pytest.raises(ValueError): _ = OIDSpec(value)
def test_oidspec_invalid_type(value): with pytest.raises(TypeError): _ = OIDSpec(value)
@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')"
]) def test_snmptree_valid(base, oids): with pytest.raises((ValueError, TypeError)): SNMPTree(base=base, oids=oids) @pytest.mark.parametrize("base, oids", [ ('.1.2', ['1', '2']), ('.1.2', ['1', OIDCached('2')]), ('.1.2', ['1', OIDBytes('2')]), ('.1.2', ['1', OIDEnd()]), ]) 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)) @pytest.mark.parametrize("tree", [ SNMPTree(base=".1.2.3", oids=["4.5.6", "7.8.9"]), SNMPTree(base=".1.2.3", oids=[OIDSpec("4.5.6"), OIDSpec("7.8.9")]), SNMPTree(base=".1.2.3", oids=[OIDCached("4.5.6"), OIDBytes("7.8.9")]), SNMPTree(base=".1.2.3", oids=[OIDSpec("4.5.6"), OIDEnd()]), SNMPTree(base=OIDSpec(".1.2.3"), oids=[OIDBytes("4.5.6"), OIDEnd()]), ]) def test_serialize_snmptree(tree): assert tree.from_json(json.loads(json.dumps(tree.to_json()))) == tree
def _validate_common_oid_properties(raw: str) -> None: _ = OIDSpec(raw) # TODO: move validation here