def test_create_snmp_section_plugin(): trees: List[SNMPTree] = [ SNMPTree( base='.1.2.3', oids=[OIDEnd(), '2.3'], ), ] detect = SNMPDetectSpec([ [('.1.2.3.4.5', 'Foo.*', True)], ]) plugin = section_plugins.create_snmp_section_plugin( name="norris", parsed_section_name="chuck", parse_function=_parse_dummy, fetch=trees, detect_spec=detect, supersedes=["foo", "bar"], ) assert isinstance(plugin, SNMPSectionPlugin) assert len(plugin) == 8 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.detect_spec == detect assert plugin.trees == trees assert plugin.supersedes == {SectionName("bar"), SectionName("foo")}
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")]
def test_get_simple_snmp_table_oid_end(backend): oid_info = SNMPTree( base=".1.3.6.1.2.1.2.2.1", oids=["1", "2", "3", OIDEnd()], ) table = snmp_table.get_snmp_table( section_name=SectionName("my_Section"), oid_info=oid_info, backend=backend, ) assert table == [ [u'1', u'lo', u'24', u'1'], [u'2', u'eth0', u'6', u'2'], ]
def test_create_snmp_section_plugin_single_tree(): single_tree = SNMPTree(base='.1.2.3', oids=[OIDEnd(), '2.3']) plugin = section_plugins.create_snmp_section_plugin( name="norris", parse_function=lambda string_table: string_table, # just one, no list: fetch=single_tree, detect_spec=SNMPDetectSpec([[('.1.2.3.4.5', 'Foo.*', True)]]), ) assert plugin.trees == [single_tree] # the plugin only specified a single tree (not a list), # so a wrapper should unpack the argument: assert plugin.parse_function([[['A', 'B']]]) == [['A', 'B']]
['2.C0FEFE', 'C0FEFE'], ['2.C0FEFE', 'C0FEFE'], ] @pytest.mark.parametrize("column", snmp_table.SPECIAL_COLUMNS) def test_value_encoding(column): assert snmp_table._value_encoding(column) == "string" @pytest.mark.parametrize("snmp_info, expected_values", [ ( SNMPTree( base='.1.3.6.1.4.1.13595.2.2.3.1', oids=[ OIDEnd(), OIDBytes("16"), ], ), DATA_2TUPLE, ), (TREE_2TUPLE, DATA_2TUPLE), (TREE_3TUPLE, DATA_3TUPLE), ([TREE_2TUPLE, TREE_3TUPLE], [DATA_2TUPLE, DATA_3TUPLE]), ]) def test_get_snmp_table(monkeypatch, snmp_info, expected_values): def get_all_snmp_tables(info): backend = SNMPTestBackend(SNMPConfig, logger) if not isinstance(info, list): return snmp_table.get_snmp_table(SectionName("unit_test"), info,
def test_oid_end(): oide = OIDEnd() assert oide == OIDEnd() assert repr(oide) == "OIDEnd()" assert oide == OID_END
('1.2', ['1', '2']), # base no leading dot ('.1.2', '12'), # oids not a list # TODO: this should fail once OIDEndCompat is not needed anymore # ('.1.2', ['1', 2]), # int in list ('.1.2', ['42.1', '42.2']), # 42 should be in base ]) 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=".1.2", oids=["3.4", "3"])], None, ), ( (".1.2", ["3.4", "3.5"]), [SNMPTree(base=".1.2.3", oids=["4", "5"])], None, ), ( (".1.2.3", list(range(4, 6))), [SNMPTree(base=".1.2.3", oids=["4", "5"])], None, ), ( (".1.2.3", [OID_END]), [SNMPTree(base=".1.2.3", oids=[OIDEnd()])], None, ), ( (".1.2.3", ["4", 5], ["1", "2", "3"]), [ SNMPTree(base=".1.2.3.4", oids=["1", "2", "3"]), SNMPTree(base=".1.2.3.5", oids=["1", "2", "3"]), ], ["4", 5], ), # not used in mainline code, but test it anyway: ( (".1.2.3", [OID_STRING]), # suppression: I know I should not pass an int. But I need to be able to. [SNMPTree(base=".1.2.3", oids=[OID_STRING])