diff --git a/docs/source/topics/usage.rst b/docs/source/topics/usage.rst index d351cd2..6c97c54 100755 --- a/docs/source/topics/usage.rst +++ b/docs/source/topics/usage.rst @@ -30,24 +30,24 @@ Further example will be found inside dedicated docs for every backend Running tests ************* -Install nose:: +Install test reqirements:: - pip install nose + pip install -r reqirements.txt + pip install -r requirements-test.txt Clone repo:: git clone git://github.com/openwisp/netengine - cd netengine/ + ./runtests.py -Edit settings json file according to your network:: +To run tests on real devices, first copy the settings file:: cp test-settings.example.json test-settings.json - vim test-settings.json -Run tests with:: +Then change the credentials accordingly, now run tests with:: - nosetests + DISABLE_MOCKS=1 TEST_SETTINGS_FILE='test-settings.json' ./runtests.py See test coverage with:: @@ -62,3 +62,6 @@ Run specific tests by specifying the relative path:: nosetests tests.snmp # snmp openwrt specific tests nosetests tests.snmp.openwrt + + # run without mocks with a custom test file + DISABLE_MOCKS=1 TEST_SETTINGS_FILE='test-settings.json' nosetests tests.snmp diff --git a/netengine/backends/base.py b/netengine/backends/base.py index 51a54c8..da893c0 100644 --- a/netengine/backends/base.py +++ b/netengine/backends/base.py @@ -1,7 +1,7 @@ import json from netengine.shortcuts import OrderedDict -from netaddr import EUI, NotRegisteredError, AddrFormatError +from netaddr import EUI, NotRegisteredError __all__ = [ @@ -135,7 +135,9 @@ def get_interfaces(self): def get_manufacturer(self, mac_address): """ returns the manufacturer of the network interface """ + if not mac_address: + return '' try: return EUI(mac_address).oui.registration().org - except (NotRegisteredError, AddrFormatError): - return None + except NotRegisteredError: + return '' diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..a6c88f5 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,2 @@ +nose +coverage diff --git a/requirements.txt b/requirements.txt index 59d04a7..4d6cfba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ netaddr pysnmp ipaddress -argparse \ No newline at end of file +argparse +mock diff --git a/runtests.py b/runtests.py new file mode 100755 index 0000000..c253020 --- /dev/null +++ b/runtests.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +try: + import nose +except: + message = """nose package not installed, install test requirements with: + pip install -r requirements-test.txt + """ + raise ImportError(message) + +if __name__ == "__main__": + file_path = os.path.abspath(__file__) + tests_path = os.path.join(os.path.abspath(os.path.dirname(file_path)), "tests") + result = nose.run( + argv=[ + os.path.abspath(__file__), + "--with-cov", + "--cover-package=netengine", + tests_path + ] + ) diff --git a/test-settings.example.json b/test-settings.example.json index 873eb75..9c7d366 100644 --- a/test-settings.example.json +++ b/test-settings.example.json @@ -1,17 +1,17 @@ { "base-snmp": { - "host": "yourhost", + "host": "0.0.0.0", "community": "public", "port": 161 }, "airos-snmp": { - "host": "your_airos_device", + "host": "0.0.0.0", "community": "public", "port": 161 }, "openwrt-snmp": { - "host": "your_openwrt_device", + "host": "0.0.0.0", "community": "public", "port": 161 } -} \ No newline at end of file +} diff --git a/tests/base.py b/tests/base.py index ff42c19..225e62c 100644 --- a/tests/base.py +++ b/tests/base.py @@ -2,7 +2,7 @@ from netengine import get_version, __version__ from netengine.backends import BaseBackend -from netengine.exceptions import NetEngineError +from netaddr import AddrFormatError __all__ = ['TestBaseBackend'] @@ -89,4 +89,5 @@ def test_base_backend(self): def test_get_manufacturer_unicode(self): device = BaseBackend() - self.assertIsNone(device.get_manufacturer(u"wrong MAC")) + with self.assertRaises(AddrFormatError): + device.get_manufacturer(u"wrong MAC") diff --git a/tests/settings.py b/tests/settings.py index eb963c7..65c5c40 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,3 +1,6 @@ import json +import os -settings = json.loads(open('./test-settings.json').read()) \ No newline at end of file +settings_file = os.getenv('TEST_SETTINGS_FILE', './test-settings.example.json') +settings = json.loads(open(settings_file).read()) +settings['disable_mocks'] = os.getenv('DISABLE_MOCKS', '0') is '1' diff --git a/tests/snmp/airos.py b/tests/snmp/airos.py index eb3719a..43aa1fe 100644 --- a/tests/snmp/airos.py +++ b/tests/snmp/airos.py @@ -1,27 +1,49 @@ import unittest from netengine.backends.snmp import AirOS from netengine.exceptions import NetEngineError +from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.smi.error import NoSuchObjectError from ..settings import settings +from ..utils import MockOutputMixin, SpyMock __all__ = ['TestSNMPAirOS'] -class TestSNMPAirOS(unittest.TestCase): +class TestSNMPAirOS(unittest.TestCase, MockOutputMixin): def setUp(self): self.host = settings['airos-snmp']['host'] self.community = settings['airos-snmp']['community'] self.port = settings['airos-snmp'].get('port', 161) - self.device = AirOS(self.host, self.community, port=self.port) + + # mock calls being made to devices + self.oid_mock_data = self._load_mock_json('/static/test-airos-snmp.json') + self.nextcmd_patcher = SpyMock._patch( + target=cmdgen.CommandGenerator, + attribute='nextCmd', + wrap_obj=self.device._command, + return_value=[0, 0, 0, [[[0, 1]]] * 5] + ) + self.getcmd_patcher = SpyMock._patch( + target=cmdgen.CommandGenerator, + attribute='getCmd', + wrap_obj=self.device._command, + side_effect=lambda *args: self._get_mocked_getcmd( + data=self.oid_mock_data, input=args + ), + ) + self.getcmd_patcher.start() def test_get_value_error(self): - with self.assertRaises(NetEngineError): + self.getcmd_patcher.stop() + with self.assertRaises(NoSuchObjectError): self.device.get_value('.') def test_validate_negative_result(self): + self.getcmd_patcher.stop() wrong = AirOS('10.40.0.254', 'wrong', 'wrong') self.assertRaises(NetEngineError, wrong.validate) @@ -31,10 +53,8 @@ def test_validate_positive_result(self): def test_get(self): with self.assertRaises(AttributeError): self.device.get({}) - with self.assertRaises(AttributeError): self.device.get(object) - self.device.get('1,3,6,1,2,1,1,5,0') self.device.get(u'1,3,6,1,2,1,1,5,0') self.device.get((1,3,6,1,2,1,1,5,0)) @@ -51,67 +71,101 @@ def test_properties(self): device.uptime_tuple def test_name(self): - self.assertTrue(type(self.device.name) == str) + self.assertIsInstance(self.device.name, str) def test_os(self): - self.assertTrue(type(self.device.os) == tuple) + self.assertIsInstance(self.device.os, tuple) def test_get_interfaces(self): - self.assertTrue(type(self.device.get_interfaces()) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.get_interfaces(), list) def test_get_interfaces_mtu(self): - self.assertTrue(type(self.device.interfaces_mtu) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_mtu, list) def test_interfaces_state(self): - self.assertTrue(type(self.device.interfaces_state) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_state, list) def test_interfaces_speed(self): - self.assertTrue(type(self.device.interfaces_speed) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_speed, list) def test_interfaces_bytes(self): - self.assertTrue(type(self.device.interfaces_bytes) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_bytes, list) def test_interfaces_MAC(self): - self.assertTrue(type(self.device.interfaces_MAC) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_MAC, list) def test_interfaces_type(self): - self.assertTrue(type(self.device.interfaces_type) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_type, list) def test_interfaces_to_dict(self): - self.assertTrue(type(self.device.interfaces_to_dict) == list) - + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_to_dict, list) + def test_wireless_dbm(self): - self.assertTrue(type(self.device.wireless_dbm) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.wireless_dbm, list) def test_interfaces_number(self): - self.assertTrue(type(self.device.interfaces_number) == int) + self.assertIsInstance(self.device.interfaces_number, int) def test_wireless_to_dict(self): - self.assertTrue(type(self.device.wireless_links) == list) + with self.nextcmd_patcher as np: + SpyMock._update_patch( + np, + _mock_side_effect=lambda *args: self._get_mocked_wireless_links( + data=args + ), + ) + self.assertIsInstance(self.device.wireless_links, list) def test_RAM_free(self): - self.assertTrue(type(self.device.RAM_free) == int) + self.assertIsInstance(self.device.RAM_free, int) def test_RAM_total(self): - self.assertTrue(type(self.device.RAM_total) == int) + self.assertIsInstance(self.device.RAM_total, int) def test_to_dict(self): - self.assertTrue(isinstance(self.device.to_dict(), dict)) + with self.nextcmd_patcher as np: + SpyMock._update_patch( + np, + _mock_side_effect=lambda *args: self._get_mocked_wireless_links( + data=args + ), + ) + self.assertTrue(isinstance(self.device.to_dict(), dict)) def test_manufacturer_to_dict(self): - self.assertIsNotNone(self.device.to_dict()['manufacturer']) + with self.nextcmd_patcher as np: + SpyMock._update_patch( + np, + _mock_side_effect=lambda *args: self._get_mocked_wireless_links( + data=args + ), + ) + self.assertIsNotNone(self.device.to_dict()['manufacturer']) def test_manufacturer(self): - self.assertIsNotNone(self.device.manufacturer) + with self.nextcmd_patcher: + self.assertIsNotNone(self.device.manufacturer) def test_model(self): - self.assertTrue(type(self.device.model) == str) + self.assertIsInstance(self.device.model, str) def test_firmware(self): - self.assertTrue(type(self.device.firmware) == str) + self.assertIsInstance(self.device.firmware, str) def test_uptime(self): - self.assertTrue(type(self.device.uptime) == int) + self.assertIsInstance(self.device.uptime, int) def test_uptime_tuple(self): - self.assertTrue(type(self.device.uptime_tuple) == tuple) \ No newline at end of file + self.assertIsInstance(self.device.uptime_tuple, tuple) + + def tearDown(self): + self.getcmd_patcher.stop() diff --git a/tests/snmp/openwrt.py b/tests/snmp/openwrt.py index eabb5d2..8a9a19c 100644 --- a/tests/snmp/openwrt.py +++ b/tests/snmp/openwrt.py @@ -1,71 +1,107 @@ import unittest from netengine.backends.snmp import OpenWRT +from pysnmp.entity.rfc3413.oneliner import cmdgen from ..settings import settings - +from ..utils import MockOutputMixin, SpyMock __all__ = ['TestSNMPOpenWRT'] -class TestSNMPOpenWRT(unittest.TestCase): - +class TestSNMPOpenWRT(unittest.TestCase, MockOutputMixin): def setUp(self): self.host = settings['openwrt-snmp']['host'] self.community = settings['openwrt-snmp']['community'] self.port = settings['openwrt-snmp'].get('port', 161) - self.device = OpenWRT(self.host, self.community, self.port) + # mock calls being made to devices + self.oid_mock_data = self._load_mock_json('/static/test-openwrt-snmp-oid.json') + self.nextcmd_patcher = SpyMock._patch( + target=cmdgen.CommandGenerator, + attribute='nextCmd', + wrap_obj=self.device._command, + return_value=[0, 0, 0, [[[0, 1]]] * 5] + ) + self.getcmd_patcher = SpyMock._patch( + target=cmdgen.CommandGenerator, + attribute='getCmd', + wrap_obj=self.device._command, + side_effect=lambda *args: self._get_mocked_getcmd( + data=self.oid_mock_data, input=args + ), + ) + self.getcmd_patcher.start() + def test_os(self): - self.assertTrue(type(self.device.os) == tuple) + self.assertIsInstance(self.device.os, tuple) def test_manufacturer(self): - self.assertIsNotNone(self.device.manufacturer) + with self.nextcmd_patcher: + self.assertIsNotNone(self.device.manufacturer) def test_name(self): - self.assertTrue(type(self.device.name) == str) + self.assertIsInstance(self.device.name, str) def test_uptime(self): - self.assertTrue(type(self.device.uptime) == int) + self.assertIsInstance(self.device.uptime, int) def test_uptime_tuple(self): - self.assertTrue(type(self.device.uptime_tuple) == tuple) + self.assertIsInstance(self.device.uptime_tuple, tuple) def test_get_interfaces(self): - self.assertTrue(type(self.device.get_interfaces()) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.get_interfaces(), list) def test_interfaces_speed(self): - self.assertTrue(type(self.device.interfaces_speed) == list) + self.assertIsInstance(self.device.interfaces_speed, list) def test_interfaces_bytes(self): - self.assertTrue(type(self.device.interfaces_bytes) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_bytes, list) def test_interfaces_MAC(self): - self.assertTrue(type(self.device.interfaces_MAC) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_MAC, list) def test_interfaces_type(self): - self.assertTrue(type(self.device.interfaces_type) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_type, list) def test_interfaces_mtu(self): - self.assertTrue(type(self.device.interfaces_mtu) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_mtu, list) def test_interfaces_state(self): - self.assertTrue(type(self.device.interfaces_state) == list) + with self.nextcmd_patcher: + self.assertIsInstance(self.device.interfaces_state, list) def test_interfaces_to_dict(self): - self.assertTrue(type(self.device.interfaces_to_dict) == list) + with self.nextcmd_patcher as p: + p.return_value = (0, 0, 0, []) + self.assertIsInstance(self.device.interfaces_to_dict, list) def test_interface_addr_and_mask(self): - self.assertTrue(type(self.device.interface_addr_and_mask) == dict) + with self.nextcmd_patcher as p: + p.return_value = (0, 0, 0, []) + self.assertIsInstance(self.device.interface_addr_and_mask, dict) def test_RAM_total(self): - self.assertTrue(type(self.device.RAM_total) == int) + self.assertIsInstance(self.device.RAM_total, int) def test_to_dict(self): - device_dict = self.device.to_dict() - - self.assertTrue(isinstance(device_dict, dict)) - self.assertEqual(len(device_dict['interfaces']), len(self.device.get_interfaces())) + with self.nextcmd_patcher as p: + SpyMock._update_patch(p, _mock_return_value=[0, 0, 0, [[[0, 1]]] * 5]) + device_dict = self.device.to_dict() + self.assertTrue(isinstance(device_dict, dict)) + self.assertEqual( + len(device_dict['interfaces']), len(self.device.get_interfaces()) + ) def test_manufacturer_to_dict(self): - self.assertIsNotNone(self.device.to_dict()['manufacturer']) + with self.nextcmd_patcher as p: + SpyMock._update_patch(p, _mock_return_value=[0, 0, 0, [[[0, 1]]] * 5]) + self.assertIsNotNone(self.device.to_dict()['manufacturer']) + + def tearDown(self): + self.getcmd_patcher.stop() diff --git a/tests/static/test-airos-snmp.json b/tests/static/test-airos-snmp.json new file mode 100644 index 0000000..e470631 --- /dev/null +++ b/tests/static/test-airos-snmp.json @@ -0,0 +1,50 @@ +{ + "1.3.6.1.2.1.1.5.0": "DeviceName", + "1.3.6.1.2.1.1.3.0": "37352", + "1.3.6.1.2.1.1.1.0": "Linux DeviceName 2.6.32.71 #1 Wed May 23 18:10:52 EEST 2018 mips GNU/Linux", + "1.3.6.1.2.1.2.1.0": "5", + "1.3.6.1.4.1.10002.1.1.1.1.2.0": "65504", + "1.3.6.1.4.1.10002.1.1.1.1.1.0": "126272", + "1.2.840.10036.3.1.2.1.4.5": "XM.ar7240.v5.5.12536.120406.1455", + "1.2.840.10036.3.1.2.1.3.5": "NanoStation Loco M2", + "1.3.6.1.2.1.2.2.1.2.1": "\\xd4\\xa0*", + "1.3.6.1.2.1.2.2.1.2.2": "\\xd4\\xa0*", + "1.3.6.1.2.1.2.2.1.2.3": "\\xd4\\xa0*", + "1.3.6.1.2.1.2.2.1.2.4": "\\xd4\\xa0*", + "1.3.6.1.2.1.2.2.1.2.5": "\\xd4\\xa0*", + "1.3.6.1.2.1.2.2.1.6.1": "\b\u0000''�\u0010\u0014", + "1.3.6.1.2.1.2.2.1.6.2": "\b\u0000''�\u0010\u0000", + "1.3.6.1.2.1.2.2.1.6.3": "\b\u0000''�\u0010\u0015", + "1.3.6.1.2.1.2.2.1.6.4": "\b\u0000''�\u0010\u0015", + "1.3.6.1.2.1.2.2.1.6.5": "\b\u0000''�\u0010\u0015", + "1.3.6.1.2.1.2.2.1.3.1": "24", + "1.3.6.1.2.1.2.2.1.3.2": "6", + "1.3.6.1.2.1.2.2.1.3.3": "6", + "1.3.6.1.2.1.2.2.1.3.4": "6", + "1.3.6.1.2.1.2.2.1.3.5": "6", + "1.3.6.1.2.1.2.2.1.16.1": "3214378817", + "1.3.6.1.2.1.2.2.1.16.2": "2438196185", + "1.3.6.1.2.1.2.2.1.16.3": "3214378817", + "1.3.6.1.2.1.2.2.1.16.4": "2438196185", + "1.3.6.1.2.1.2.2.1.16.5": "3214378817", + "1.3.6.1.2.1.2.2.1.10.1": "3214378817", + "1.3.6.1.2.1.2.2.1.10.2": "2438196185", + "1.3.6.1.2.1.2.2.1.10.3": "3214378817", + "1.3.6.1.2.1.2.2.1.10.4": "2438196185", + "1.3.6.1.2.1.2.2.1.10.5": "3214378817", + "1.3.6.1.2.1.2.2.1.8.1": "1", + "1.3.6.1.2.1.2.2.1.8.2": "1", + "1.3.6.1.2.1.2.2.1.8.3": "2", + "1.3.6.1.2.1.2.2.1.8.4": "2", + "1.3.6.1.2.1.2.2.1.8.5": "2", + "1.3.6.1.2.1.2.2.1.4.1": "65536", + "1.3.6.1.2.1.2.2.1.4.2": "1500", + "1.3.6.1.2.1.2.2.1.4.3": "1500", + "1.3.6.1.2.1.2.2.1.4.4": "1500", + "1.3.6.1.2.1.2.2.1.4.5": "1500", + "1.3.6.1.2.1.2.2.1.5.1": "10000000", + "1.3.6.1.2.1.2.2.1.5.2": "0", + "1.3.6.1.2.1.2.2.1.5.3": "0", + "1.3.6.1.2.1.2.2.1.5.4": "0", + "1.3.6.1.2.1.2.2.1.5.5": "0" +} diff --git a/tests/static/test-openwrt-snmp-oid.json b/tests/static/test-openwrt-snmp-oid.json new file mode 100644 index 0000000..2db1140 --- /dev/null +++ b/tests/static/test-openwrt-snmp-oid.json @@ -0,0 +1,49 @@ +{ + "1.3.6.1.2.1.25.2.3.1.5.1": "115080", + "1.3.6.1.2.1.2.2.1.2.1": "lo", + "1.3.6.1.2.1.2.2.1.2.2": "Device 8086:100e", + "1.3.6.1.2.1.2.2.1.2.3": "Device 8086:100e", + "1.3.6.1.2.1.2.2.1.2.4": "Device 8086:100e", + "1.3.6.1.2.1.2.2.1.2.5": "br-lan", + "1.3.6.1.2.1.2.2.1.16.1": "719914", + "1.3.6.1.2.1.2.2.1.10.1": "719914", + "1.3.6.1.2.1.2.2.1.16.2": "806244", + "1.3.6.1.2.1.2.2.1.10.2": "758983", + "1.3.6.1.2.1.2.2.1.16.3": "3326302", + "1.3.6.1.2.1.2.2.1.10.3": "9723560", + "1.3.6.1.2.1.2.2.1.10.5": "647519", + "1.3.6.1.2.1.2.2.1.16.4": "0", + "1.3.6.1.2.1.2.2.1.10.4": "0", + "1.3.6.1.2.1.2.2.1.16.5": "805932", + "1.3.6.1.2.1.2.2.1.8.1": "1", + "1.3.6.1.2.1.2.2.1.8.2": "1", + "1.3.6.1.2.1.2.2.1.8.3": "1", + "1.3.6.1.2.1.2.2.1.8.4": "2", + "1.3.6.1.2.1.2.2.1.8.5": "1", + "1.3.6.1.2.1.2.2.1.4.1": "65536", + "1.3.6.1.2.1.2.2.1.4.2": "1500", + "1.3.6.1.2.1.2.2.1.4.3": "1500", + "1.3.6.1.2.1.2.2.1.4.4": "1500", + "1.3.6.1.2.1.2.2.1.4.5": "1500", + "1.3.6.1.2.1.2.2.1.5.1": "10000000", + "1.3.6.1.2.1.2.2.1.5.2": "1000000000", + "1.3.6.1.2.1.2.2.1.5.3": "1000000000", + "1.3.6.1.2.1.2.2.1.5.4": "1000000000", + "1.3.6.1.2.1.2.2.1.5.5": "0", + "1.3.6.1.2.1.2.2.1.2.6": "", + "1.3.6.1.2.1.2.2.1.2.7": "", + "1.3.6.1.2.1.2.2.1.2.8": "", + "1.3.6.1.2.1.2.2.1.3.1": "24", + "1.3.6.1.2.1.2.2.1.3.2": "6", + "1.3.6.1.2.1.2.2.1.3.3": "6", + "1.3.6.1.2.1.2.2.1.3.4": "6", + "1.3.6.1.2.1.2.2.1.3.5": "6", + "1.3.6.1.2.1.1.3.0": "1033939", + "1.3.6.1.2.1.1.5.0": "HeartOfGold", + "1.3.6.1.2.1.1.1.0": "Linux 08-00-27-0A-F7-6A 4.14.221 #0 SMP Mon Feb 15 15:22:37 2021 x86_64", + "1.3.6.1.2.1.2.2.1.6.1": "\b\u0000''�\u0010\u0014", + "1.3.6.1.2.1.2.2.1.6.2": "\b\u0000''�\u0010\u0000", + "1.3.6.1.2.1.2.2.1.6.3": "\b\u0000''�\u0010\u0014", + "1.3.6.1.2.1.2.2.1.6.4": "\b\u0000''�\u0010\u0000", + "1.3.6.1.2.1.2.2.1.6.5": "\b\u0000''�\u0010\u0015" +} diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..ee167f3 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,47 @@ +import json +import mock +import os + +from .settings import settings + +class SpyMock: + @staticmethod + def _patch(*args, **kwargs): + if not settings['disable_mocks']: + return mock.patch.object(*args, **kwargs) + wraps = getattr(kwargs['wrap_obj'], kwargs['attribute']) + return mock.patch.object(kwargs['target'], kwargs['attribute'], wraps=wraps) + + @staticmethod + def _update_patch(mock_obj, *args, **kwargs): + if settings['disable_mocks']: + return + mock_obj.__dict__.update(*args, **kwargs) + + +class MockOutputMixin(object): + @staticmethod + def _load_mock_json(file): + base_dir = os.path.dirname(os.path.abspath(__file__)) + with open(base_dir + file) as f: + data = json.load(f) + return data + + @staticmethod + def _get_mocked_getcmd(data, input): + oid = input[2] + result = data[oid].encode('ascii', 'ignore') + if type(result) == list: + result = "\n".join(result[0:]) + return [0, 0, 0, [[0, result]]] + + @staticmethod + def _get_mocked_wireless_links(data): + oid = data[2] + return_data = { + '1.3.6.1.4.1.14988.1.1.1.2.1': [0, 0, 0, [[[0, 0], 0]] * 28], + '1.3.6.1.4.1.14988.1.1.1.2.1.3': [0, 0, 0, [0, 0]], + '1.3.6.1.4.1.14988.1.1.1.2.1.3.0': [None, 0, 0, []], + '1.3.6.1.2.1.1.9.1.1': [0, 0, 0, [[[0, 1]]] * 5] + } + return return_data[oid]