Beispiel #1
0
    def test_multiwalk(self):
        response_1 = readbytes('apiv1/multiwalk_response_1.hex')
        response_2 = readbytes('apiv1/multiwalk_response_2.hex')
        response_3 = readbytes('apiv1/multiwalk_response_3.hex')

        self.transport.send.side_effect = [
            response_1,
            response_2,
            response_3,
        ]
        result = snmp.multiwalk('127.0.0.1', 'private', [
            '1.3.6.1.2.1.2.2.1.1',
            '1.3.6.1.2.1.1.2.1',
        ])
        expected = [
            VarBind(OID('1.3.6.1.2.1.2.2.1.1.1'), 1),
            VarBind(OID('1.3.6.1.2.1.2.2.1.1.6'), 6),
        ]
        result = list(result)
        self.assertEqual(result, expected)

        expected_types = [
            (ObjectIdentifier, int),
            (ObjectIdentifier, int),
        ]
        returned_values = [(row.oid, row.value) for row in result]
        assert_of_types(returned_values, expected_types)
Beispiel #2
0
    def test_multi_walk(self):
        response_1 = readbytes('multiwalk_response_1.hex')
        response_2 = readbytes('multiwalk_response_2.hex')
        response_3 = readbytes('multiwalk_response_3.hex')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.1.1'), 1),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.1'),
                    b'lo'),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.1.78'),
                    78),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.78'),
                    b'eth0')
        ]

        with patch('puresnmp.send') as mck:
            mck.side_effect = [response_1, response_2, response_3]
            result = list(
                multiwalk('::1', 'public',
                          ['1.3.6.1.2.1.2.2.1.1', '1.3.6.1.2.1.2.2.1.2']))
        # TODO (advanced): should order matter in the following result?
        self.assertCountEqual(result, expected)
Beispiel #3
0
    def test_multi_walk(self):
        response_1 = readbytes('multiwalk_response_1.hex')
        response_2 = readbytes('multiwalk_response_2.hex')
        response_3 = readbytes('multiwalk_response_3.hex')

        num_call = 0

        def mocked_responses(*args, **kwargs):
            nonlocal num_call
            num_call += 1
            if num_call == 1:
                return response_1
            elif num_call == 2:
                return response_2
            elif num_call == 3:
                return response_3
            else:
                raise AssertionError('Expected no more than 3 calls!')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.1.1'), 1),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.1'),
                    b'lo'),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.1.78'),
                    78),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.78'),
                    b'eth0')
        ]

        with patch('puresnmp.send') as mck:
            mck.side_effect = mocked_responses
            result = list(
                multiwalk('::1', 'public',
                          ['1.3.6.1.2.1.2.2.1.1', '1.3.6.1.2.1.2.2.1.2']))
        # TODO (advanced): should order matter in the following result?
        self.assertCountEqual(result, expected)
Beispiel #4
0
    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