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_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"])]
def _create_snmp_trees_from_tuple(snmp_info_element): # type: (tuple) -> Tuple[List[SNMPTree], Optional[LayoutRecoverSuboids]] """Create a SNMPTrees from (part of) a legacy definition Legacy definition *elements* can be 2-tuple or 3-tuple. We are quite generous here: we will make sure that * base will not end with '.' * subtrees are strings, starting but not ending with '.' * oids are not the empty string. """ assert isinstance(snmp_info_element, tuple) assert len(snmp_info_element) in (2, 3) base = snmp_info_element[0].rstrip('.') # "Triple"-case: recursively return a list if len(snmp_info_element) == 3: tmp_base, suboids, oids = snmp_info_element base_list = [("%s.%s" % (tmp_base, str(i).strip('.'))) for i in suboids] return sum((_create_snmp_trees_from_tuple((base, oids))[0] for base in base_list), []), suboids # this fixes 7 weird cases: oids = [ "%d" % oid if isinstance(oid, int) and oid > 0 else oid for oid in snmp_info_element[1] ] if '' in oids: # this fixes 19 cases base, tail = str(base).rsplit('.', 1) oids = [ o if isinstance(o, int) else type(o)( ("%s.%s" % (tail, o)).strip('.')) for o in oids ] else: # this fixes 21 cases common, oids = _extract_conmmon_part(oids) if common: base = "%s.%s" % (base, common) return [SNMPTree(base=base, oids=oids)], None
def test_execute_with_canned_responses(monkeypatch): # Beginning of setup. tree = SNMPTree( base='.1.3.6.1.4.1.13595.2.2.3.1', oids=[ OIDEnd(), snmp_utils.OIDBytes("16"), ], ) name = "acme_agent_sessions" # Must be in config.registered_snmp_sections # Replace I/O with canned responses. monkeypatch.setattr(SNMPDataSource, "get_check_plugin_names", lambda *args, **kwargs: {name}) monkeypatch.setattr(snmp, "get_snmp_table_cached", lambda *args: tree) hostname = "testhost" ipaddress = "127.0.0.1" Scenario().add_host(hostname).apply(monkeypatch) source = SNMPDataSource(hostname, ipaddress) # End of setup. assert source._execute() == {name: [tree]}
['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() if not isinstance(info, list): return snmp_table.get_snmp_table(SNMPConfig, "unit-test",
def test_snmptree_valid(base, oids): with pytest.raises((ValueError, TypeError)): SNMPTree(base=base, oids=oids)
["3.1", "2"], ["3.3", "4"], ]], ), ]) def test_create_layout_recover_function(suboids_list, input_data, expected_output): layout_recover_func = _create_layout_recover_function(suboids_list) assert layout_recover_func(input_data) == expected_output @pytest.mark.parametrize( "element, expected_tree, expected_suboids", [ ( (".1.2.3", ["4", "5"]), [SNMPTree(base=".1.2.3", oids=["4", "5"])], None, ), ( (".1.2.3", ["4", ""]), [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", range(4, 6)), [SNMPTree(base=".1.2.3", oids=["4", "5"])],