Beispiel #1
0
    def test_multi_walk(self):
        expected = [
            VarBind('1.3.6.1.2.1.2.2.1.1.1', 1),
            VarBind('1.3.6.1.2.1.2.2.1.2.1', b'lo'),
            VarBind('1.3.6.1.2.1.2.2.1.1.78', 78),
            VarBind('1.3.6.1.2.1.2.2.1.2.78', b'eth0')
        ]

        with patch('puresnmp.api.pythonic.raw') as mck:
            mck.multiwalk.return_value = [
                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')
            ]
            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?
        six.assertCountEqual(self, result, expected)
Beispiel #2
0
    def test_walk(self):
        response_1 = readbytes('walk_response_1.hex')
        response_2 = readbytes('walk_response_2.hex')
        response_3 = readbytes('walk_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.5.1'),
                    10000000),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                    4294967295)
        ]

        with patch('puresnmp.send') as mck:
            mck.side_effect = mocked_responses
            result = list(walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'))
        self.assertEqual(result, expected)
    async 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'), Integer(1)
        ), VarBind(
            ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.1'),
            OctetString(b'lo')
        ), VarBind(
            ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.1.78'), Integer(78)
        ), VarBind(
            ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.78'),
            OctetString(b'eth0')
        )]

        with patch('puresnmp.aio.api.raw.Transport') as mck:
            mck().send = AsyncMock()
            mck().get_request_id.return_value = 0
            mck().send.side_effect = [response_1, response_2, response_3]
            result = []
            async for x in multiwalk('::1', 'public', [
                '1.3.6.1.2.1.2.2.1.1',
                '1.3.6.1.2.1.2.2.1.2'
            ]):
                result.append(x)
        # TODO (advanced): should order matter in the following result?
        assert len(result) == len(expected)
Beispiel #4
0
    async def test_multi_walk(self):
        expected = [
            VarBind('1.3.6.1.2.1.2.2.1.1.1', 1),
            VarBind('1.3.6.1.2.1.2.2.1.2.1', b'lo'),
            VarBind('1.3.6.1.2.1.2.2.1.1.78', 78),
            VarBind('1.3.6.1.2.1.2.2.1.2.78', b'eth0')
        ]

        with patch('puresnmp.aio.api.pythonic.raw',
                   new_callable=AsyncGenMock) as mck:
            mck.multiwalk.return_value = [
                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')
            ]
            result = []
            async for x in multiwalk(
                    '::1', 'public',
                ['1.3.6.1.2.1.2.2.1.1', '1.3.6.1.2.1.2.2.1.2']):
                result.append(x)
        # TODO (advanced): should order matter in the following result?
        assert len(result) == len(expected)
Beispiel #5
0
    def test_walk(self):
        expected = [
            VarBind('1.3.6.1.2.1.2.2.1.5.1', 10000000),
            VarBind('1.3.6.1.2.1.2.2.1.5.13', 4294967295)
        ]

        with patch('puresnmp.api.pythonic.raw') as mck:
            mck.walk.return_value = [
                VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                        Gauge(10000000)),
                VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                        Integer(4294967295))
            ]
            result = list(walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'))
        self.assertEqual(result, expected)
 async def test_get_oid(self):
     expected = ('1.3.6.1.4.1.8072.3.2.10')
     with patch('puresnmp.aio.api.pythonic.raw', new_callable=AsyncMock) as mck:
         mck.get.return_value = ObjectIdentifier.from_string(
             '1.3.6.1.4.1.8072.3.2.10')
         result = await get('::1', 'private', '1.2.3')
     assert result == expected
Beispiel #7
0
 def test_get_oid(self):
     expected = ('1.3.6.1.4.1.8072.3.2.10')
     with patch('puresnmp.api.pythonic.raw') as mck:
         mck.get.return_value = ObjectIdentifier.from_string(
             '1.3.6.1.4.1.8072.3.2.10')
         result = get('::1', 'private', '1.2.3')
     self.assertEqual(result, expected)
Beispiel #8
0
    def test_walk(self):
        response_1 = readbytes('walk_response_1.hex')
        response_2 = readbytes('walk_response_2.hex')
        response_3 = readbytes('walk_response_3.hex')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                    Gauge(10000000)),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                    Gauge(4294967295))
        ]

        with patch('puresnmp.api.raw.send') as mck:
            mck.side_effect = [response_1, response_2, response_3]
            result = list(walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'))
        self.assertEqual(result, expected)
Beispiel #9
0
 async def test_get_oid(self):
     data = readbytes('get_sysoid_01.hex')
     expected = ObjectIdentifier.from_string('1.3.6.1.4.1.8072.3.2.10')
     with patch('puresnmp.aio.api.raw.send', new_callable=AsyncMock) as mck:
         mck.return_value = data
         result = await get('::1', 'private', '1.2.3')
     assert result == expected
Beispiel #10
0
 def test_get_oid(self):
     data = readbytes('get_sysoid_01.hex')
     expected = ObjectIdentifier.from_string('1.3.6.1.4.1.8072.3.2.10')
     with patch('puresnmp.api.raw.send') as mck:
         mck.return_value = data
         result = get('::1', 'private', '1.2.3')
     self.assertEqual(result, expected)
 async def test_set_string_absolute(self):
     expected = (b'foo')
     with patch('puresnmp.aio.api.pythonic.raw', new_callable=AsyncMock) as mck:
         mck.multiset.return_value = {
             ObjectIdentifier.from_string('1.2.3'): OctetString(b'foo')
         }
         result = await set('::1', 'private', '.1.2.3', OctetString(b'foo'))
     assert result == expected
Beispiel #12
0
 def test_set_string_absolute(self):
     expected = (b'foo')
     with patch('puresnmp.api.pythonic.raw') as mck:
         mck.multiset.return_value = {
             ObjectIdentifier.from_string('1.2.3'): OctetString(b'foo')
         }
         result = set('::1', 'private', '.1.2.3', OctetString(b'foo'))
     self.assertEqual(result, expected)
Beispiel #13
0
    async def test_walk(self):
        response_1 = readbytes('walk_response_1.hex')
        response_2 = readbytes('walk_response_2.hex')
        response_3 = readbytes('walk_response_3.hex')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                    Gauge(10000000)),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                    Gauge(4294967295))
        ]

        with patch('puresnmp.aio.api.raw.send', new_callable=AsyncMock) as mck:
            mck.side_effect = [response_1, response_2, response_3]
            result = []
            async for x in walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'):
                result.append(x)
        assert result == expected
 async def test_get_oid(self):
     data = readbytes('get_sysoid_01.hex')
     expected = ObjectIdentifier.from_string('1.3.6.1.4.1.8072.3.2.10')
     with patch('puresnmp.aio.api.raw.Transport') as mck:
         mck().send = AsyncMock()
         mck().send.return_value = data
         mck().get_request_id.return_value = 0
         result = await get('::1', 'private', '1.2.3')
     assert result == expected
Beispiel #15
0
    async def test_walk(self):
        expected = [
            VarBind('1.3.6.1.2.1.2.2.1.5.1', 10000000),
            VarBind('1.3.6.1.2.1.2.2.1.5.13', 4294967295)
        ]

        with patch('puresnmp.aio.api.pythonic.raw',
                   new_callable=AsyncGenMock) as mck:
            mck.walk.return_value = [
                VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                        Gauge(10000000)),
                VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                        Integer(4294967295))
            ]
            result = []
            async for x in walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'):
                result.append(x)
        assert result == expected
Beispiel #16
0
    async def test_walk(self):
        response_1 = readbytes('walk_response_1.hex')
        response_2 = readbytes('walk_response_2.hex')
        response_3 = readbytes('walk_response_3.hex')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                    Gauge(10000000)),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                    Gauge(4294967295))
        ]

        with patch('puresnmp.aio.api.raw.Transport') as mck:
            mck().send = AsyncMock()
            mck().send.side_effect = [response_1, response_2, response_3]
            mck().get_request_id.return_value = 0
            result = []
            async for x in walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'):
                result.append(x)
        assert result == expected
Beispiel #17
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 #18
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'),
                    Integer(1)),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.1'),
                    OctetString(b'lo')),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.1.78'),
                    Integer(78)),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.2.78'),
                    OctetString(b'eth0'))
        ]

        with patch('puresnmp.api.raw.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']))
        self.assertEqual(result, expected)
Beispiel #19
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 #20
0
 def test_multiget(self):
     data = readbytes('multiget_response.hex')
     expected = [
         ObjectIdentifier.from_string('1.3.6.1.4.1.8072.3.2.10'),
         OctetString(b"Linux 7fbf2f0c363d 4.4.0-28-generic "
                     b"#47-Ubuntu SMP Fri Jun 24 10:09:13 "
                     b"UTC 2016 x86_64")
     ]
     with patch('puresnmp.api.raw.send') as mck:
         mck.return_value = data
         result = multiget('::1', 'private', [
             '1.3.6.1.2.1.1.2.0',
             '1.3.6.1.2.1.1.1.0',
         ])
     self.assertEqual(result, expected)
 async def test_multiget(self):
     expected = ['1.3.6.1.4.1.8072.3.2.10',
                 b"Linux 7fbf2f0c363d 4.4.0-28-generic #47-Ubuntu SMP Fri "
                 b"Jun 24 10:09:13 UTC 2016 x86_64"]
     with patch('puresnmp.aio.api.pythonic.raw', new_callable=AsyncMock) as mck:
         mck.multiget.return_value = [
             ObjectIdentifier.from_string('1.3.6.1.4.1.8072.3.2.10'),
             OctetString(b"Linux 7fbf2f0c363d 4.4.0-28-generic "
                         b"#47-Ubuntu SMP Fri Jun 24 10:09:13 "
                         b"UTC 2016 x86_64")
         ]
         result = await multiget('::1', 'private', [
             '1.3.6.1.2.1.1.2.0',
             '1.3.6.1.2.1.1.1.0',
         ])
     assert result == expected
 async def test_multiget(self):
     data = readbytes('multiget_response.hex')
     expected = [
         ObjectIdentifier.from_string('1.3.6.1.4.1.8072.3.2.10'),
         OctetString(b"Linux 7fbf2f0c363d 4.4.0-28-generic "
                     b"#47-Ubuntu SMP Fri Jun 24 10:09:13 "
                     b"UTC 2016 x86_64")
     ]
     with patch('puresnmp.aio.api.raw.Transport') as mck:
         mck().send = AsyncMock()
         mck().get_request_id.return_value = 0
         mck().send.return_value = data
         result = await multiget('::1', 'private', [
             '1.3.6.1.2.1.1.2.0',
             '1.3.6.1.2.1.1.1.0',
         ])
     assert result == expected
Beispiel #23
0
def tablify(varbinds, num_base_nodes=0, base_oid=''):
    # type: ( Iterable[Tuple[Any, Any]], int, str ) -> List[Dict[str, Any]]
    """
    Converts a list of varbinds into a table-like structure. *num_base_nodes*
    can be used for table which row-ids consist of multiple OID tree nodes. By
    default, the last node is considered the row ID, and the second-last is the
    column ID. Example:

    By default, for the table-cell at OID ``1.2.3.4.5``, ``4`` is the column
    index and ``5`` is the row index.

    Using ``num_base_nodes=2`` will only use the first two nodes (``1.2``) as
    table-identifier, so ``3`` becomes the column index, and ``4.5`` becomes
    the row index.

    The output should *not* be considered ordered in any way. If you need it
    sorted, you must sort it after retrieving the table from this function!

    Each element of the output is a dictionary where each key is the column
    index. By default the index ``0`` will be added, representing the row ID.

    Example::

        >>> data = [
        >>>     (ObjectIdentifier.from_string('1.2.1.1'), 'row 1 col 1'),
        >>>     (ObjectIdentifier.from_string('1.2.1.2'), 'row 2 col 1'),
        >>>     (ObjectIdentifier.from_string('1.2.2.1'), 'row 1 col 2'),
        >>>     (ObjectIdentifier.from_string('1.2.2.2'), 'row 2 col 2'),
        >>> ]
        >>> tablify(data)
        [
            {'0': '1', '1': 'row 1 col 1', '2': 'row 1 col 2'},
            {'0': '2', '1': 'row 2 col 1', '2': 'row 2 col 2'},
        ]


    Example with longer row ids (using the *first* two as table identifiers)::

        >>> data = [
        >>>     (ObjectIdentifier.from_string('1.2.1.5.10'), 'row 5.10 col 1'),
        >>>     (ObjectIdentifier.from_string('1.2.1.6.10'), 'row 6.10 col 1'),
        >>>     (ObjectIdentifier.from_string('1.2.2.5.10'), 'row 5.10 col 2'),
        >>>     (ObjectIdentifier.from_string('1.2.2.6.10'), 'row 6.10 col 2'),
        >>> ]
        >>> tablify(data, num_base_nodes=2)
        [
            {'0': '5.10', '1': 'row 5.10 col 1', '2': 'row 5.10 col 2'},
            {'0': '6.10', '1': 'row 6.10 col 1', '2': 'row 6.10 col 2'},
        ]
    """
    if isinstance(base_oid, str) and base_oid:
        # This import needs to be delayed to avoid circular imports
        from puresnmp.x690.types import ObjectIdentifier
        base_oid_parsed = ObjectIdentifier.from_string(base_oid)
        # Each table has a sub-index for the table "entry" so the number of
        # base-nodes needs to be incremented by 1
        num_base_nodes = len(base_oid_parsed)  #  type: ignore

    rows = {}  # type: Dict[str, Dict[str, Type[PyType]]]
    for oid, value in varbinds:
        if num_base_nodes:
            tail = oid.identifiers[num_base_nodes:]
            col_id, row_id = tail[0], tail[1:]
            row_id = '.'.join([unicode(node) for node in row_id])
        else:
            col_id = unicode(oid.identifiers[-2])
            row_id = unicode(oid.identifiers[-1])
        tmp = {
            '0': row_id,
        }
        row = rows.setdefault(row_id, tmp)
        row[unicode(col_id)] = value
    return list(rows.values())