def test_set_without_type(self): """ As we need typing information, we have to hand in an instance of supported types (a subclass of puresnmp.x690.Type). """ with patch('puresnmp.send'): with self.assertRaisesRegex(TypeError, 'Type'): set('::1', 'private', '1.2.3', 12)
def test_set_multiple_varbind(self): """ SET responses should only contain one varbind. """ data = readbytes('set_response_multiple.hex') with patch('puresnmp.send') as mck: mck.return_value = data with self.assertRaisesRegex(SnmpError, 'varbind'): set('::1', 'private', '1.3.6.1.2.1.1.4.0', OctetString(b'*****@*****.**'))
def test_set(self): self.transport.send.return_value = readbytes('apiv1/set_response.hex') result = snmp.set('127.0.0.1', 'private', '1.3.6.1.2.1.1.6.0', OctetString(b'Hello')) self.assertEqual(result, b'On the move') self.assertTrue(self.transport.send.called, 'method was not called') self.assertIsInstance(result, bytes)
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
def test_set(self): data = readbytes('set_response.hex') with patch('puresnmp.send') as mck: mck.return_value = data set('::1', 'private', '1.3.6.1.2.1.1.4.0', OctetString(b'*****@*****.**'))