def test_get_call_args(self): data = readbytes('dummy.hex') # any dump would do packet = Sequence(Integer(Version.V2C), OctetString('public'), GetNextRequest(0, ObjectIdentifier(1, 2, 3))) with patch('puresnmp.send') as mck, \ patch('puresnmp.get_request_id') as mck2: mck2.return_value = 0 mck.return_value = data getnext('::1', 'public', '1.2.3') mck.assert_called_with('::1', 161, bytes(packet), timeout=2)
def test_getnext(self): data = readbytes('getnext_response.hex') expected = VarBind('1.3.6.1.6.3.1.1.6.1.0', 354522558) with patch('puresnmp.send') as mck: mck.return_value = data result = getnext('::1', 'private', '1.3.6.1.5') self.assertEqual(result, expected)
def test_getnext(self): response = readbytes('apiv1/getnext_response.hex') self.transport.send.return_value = response result = snmp.getnext('192.168.1.1', 'private', '1.3.6.1.2.1.1.6.0', port=161, timeout=1) expected = VarBind(OID('1.3.6.1.2.1.1.7.0'), 72) self.assertEqual(result, expected) self.assertTrue(self.transport.send.called, 'method was not called') self.assertIsInstance(result.oid, ObjectIdentifier) self.assertIsInstance(result.value, int)
def __process_methods(method, common_parameters, datatype_config): response = None if method == "get": oid = datatype_config["oid"] response = puresnmp.get(**common_parameters, oid=oid) elif method == "multiget": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) response = puresnmp.multiget(**common_parameters, oids=oids) elif method == "getnext": oid = datatype_config["oid"] master_response = puresnmp.getnext(**common_parameters, oid=oid) response = {master_response.oid: master_response.value} elif method == "multigetnext": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) master_response = puresnmp.multigetnext(**common_parameters, oids=oids) response = { binded_var.oid: binded_var.value for binded_var in master_response } elif method == "walk": oid = datatype_config["oid"] response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.walk(**common_parameters, oid=oid)) } elif method == "multiwalk": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.multiwalk(**common_parameters, oids=oids)) } elif method == "set": oid = datatype_config["oid"] value = datatype_config["value"] response = puresnmp.set(**common_parameters, oid=oid, value=value) elif method == "multiset": mappings = datatype_config["mappings"] response = puresnmp.multiset(**common_parameters, mappings=mappings) elif method == "bulkget": scalar_oids = datatype_config.get("scalarOid", []) scalar_oids = scalar_oids if isinstance( scalar_oids, list) else list(scalar_oids) repeating_oids = datatype_config.get("repeatingOid", []) repeating_oids = repeating_oids if isinstance( repeating_oids, list) else list(repeating_oids) max_list_size = datatype_config.get("maxListSize", 1) response = puresnmp.bulkget(**common_parameters, scalar_oids=scalar_oids, repeating_oids=repeating_oids, max_list_size=max_list_size)._asdict() elif method == "bulkwalk": oids = datatype_config["oid"] oids = oids if isinstance(oids, list) else list(oids) bulk_size = datatype_config.get("bulkSize", 10) response = { binded_var.oid: binded_var.value for binded_var in list( puresnmp.bulkwalk( **common_parameters, bulk_size=bulk_size, oids=oids)) } elif method == "table": oid = datatype_config["oid"] del common_parameters["timeout"] num_base_nodes = datatype_config.get("numBaseNodes", 0) response = puresnmp.table(**common_parameters, oid=oid, num_base_nodes=num_base_nodes) elif method == "bulktable": oid = datatype_config["oid"] num_base_nodes = datatype_config.get("numBaseNodes", 0) bulk_size = datatype_config.get("bulkSize", 10) response = puresnmp.bulktable(**common_parameters, oid=oid, num_base_nodes=num_base_nodes, bulk_size=bulk_size) else: log.error("Method \"%s\" - Not found", str(method)) return response