Skip to content

Commit

Permalink
[chores] Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jun 11, 2021
1 parent 9168a27 commit cfa6650
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions netengine/backends/snmp/airos.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,9 @@ def to_dict(self):
return self._dict(
{
'type': 'DeviceMonitoring',
'general': {'uptime': self.uptime,},
'general': {'uptime': self.uptime},
'resources': {
'memory': {'total': self.RAM_total, 'free': self.RAM_free,},
'memory': {'total': self.RAM_total, 'free': self.RAM_free},
},
'interfaces': self.interfaces_to_dict,
}
Expand Down
2 changes: 1 addition & 1 deletion netengine/backends/snmp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _octet_to_mac(self, octet_mac):
returns a valid mac address for a given octetstring
"""
mac_address = binascii.b2a_hex(octet_mac.encode()).decode()
if mac_address is not '':
if mac_address != '':
mac_address = ':'.join(
[mac_address[slice(i, i + 2)] for i in range(0, 12, 2) if i != '']
)
Expand Down
13 changes: 7 additions & 6 deletions netengine/backends/snmp/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datetime import timedelta

import pytz
from netaddr import EUI, mac_unix_expanded

from netengine.backends.snmp import SNMP

Expand Down Expand Up @@ -480,7 +481,7 @@ def resources_to_dict(self):
'free': self.RAM_free,
'cached': self.RAM_cached,
},
'swap': {'total': self.SWAP_total, 'free': self.SWAP_free,},
'swap': {'total': self.SWAP_total, 'free': self.SWAP_free},
}
)
return result
Expand All @@ -505,14 +506,14 @@ def neighbors(self):
neighbor_states = self.next('1.3.6.1.2.1.4.35.1.7')[3]
result = []

for i in range(len(neighbors)):
interface_num = neighbors[i][0][0].getOid()[10]
for index, neighbor in enumerate(neighbors):
mac = EUI(int(neighbor[0][1].prettyPrint(), 16), dialect=mac_unix_expanded)
interface_num = neighbor[0][0].getOid()[10]
interface = self.get(f'1.3.6.1.2.1.31.1.1.1.1.{interface_num}')[3][0][1]
state = states_map[str(neighbor_states[i][0][1])]
mac = self._octet_to_mac(str(neighbors[i][0][1]))
state = states_map[str(neighbor_states[index][0][1])]
result.append(
self._dict(
{'mac': str(mac), 'state': str(state), 'interface': str(interface),}
{'mac': str(mac), 'state': str(state), 'interface': str(interface)}
)
)
return result
Expand Down
5 changes: 4 additions & 1 deletion tests/static/test-openwrt-snmp-oid.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@
"1.3.6.1.2.1.25.2.3.1.6.8": "88",
"1.3.6.1.2.1.25.2.3.1.6.10": "0",
"1.3.6.1.2.1.25.2.3.1.5.10": "0",
"1.3.6.1.2.1.25.1.2.0": "\u0007\u00e5\u0006\t14\ufffd+\ufffd\ufffd"
"1.3.6.1.2.1.25.1.2.0": {
"type": "bytes",
"value": "\\x07\\xe5\\x06\\x0b\\x06\\x00\r\\x00+\\x00\\x00"
}
}
3 changes: 3 additions & 0 deletions tests/test_snmp/test_openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ def test_CPU_count(self):
def test_neighbors(self):
self.assertIsInstance(self.device.neighbors, list)

def test_local_time(self):
self.assertIsInstance(self.device.local_time, int)

def test_to_dict(self):
with self.nextcmd_patcher as p:
SpyMock._update_patch(p, _mock_return_value=[0, 0, 0, []])
Expand Down
8 changes: 7 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codecs
import json
import os
from unittest import mock
Expand Down Expand Up @@ -32,7 +33,12 @@ def _load_mock_json(file):
def _get_mocked_getcmd(data, input):
oid = input[2]
result = data[oid]
if type(result) == list:
if type(result) == dict:
_type = result['type']
_value = result['value']
if _type == 'bytes':
result = codecs.escape_decode(_value)[0]
elif type(result) == list:
result = '\n'.join(result[0:])
return [0, 0, 0, [[0, result]]]

Expand Down

0 comments on commit cfa6650

Please sign in to comment.