From e8d478dfff91147f02587c9652d0ab41049f8d69 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Thu, 14 Jan 2021 16:42:02 +0100
Subject: [PATCH] mock nextCmd rather than walk

---
 test/test_snmp_handling.py | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/test/test_snmp_handling.py b/test/test_snmp_handling.py
index 022fea52..6fe5c44e 100644
--- a/test/test_snmp_handling.py
+++ b/test/test_snmp_handling.py
@@ -15,15 +15,34 @@ TEST_DATA_DIRNAME = os.path.realpath(os.path.join(
     "data"))
 
 
-@pytest.fixture
-def snmp_walk_responses():
+def interfaces_walk_responses():
     test_data_filename = os.path.join(
         TEST_DATA_DIRNAME, "snmp-walk-interfaces.json")
     with open(test_data_filename) as f:
         return json.loads(f.read())
 
 
-def test_snmp_interfaces(mocker, data_config, snmp_walk_responses):
+def peering_walk_responses():
+    test_data_filename = os.path.join(
+        TEST_DATA_DIRNAME, "snmp-peer-info.json")
+    with open(test_data_filename) as f:
+        for peering in json.loads(f.read()):
+            yield {'oid': peering['oid'], 'value': 6}
+
+
+def _gen_mocked_nextCmd(mocked_walk_data):
+    engineErrorIndication = 0
+    pduErrorIndication = 0
+    errorIndex = 0
+    varBinds = [[x['oid'], x['value']] for x in mocked_walk_data]
+
+    def _mocked_nextCmd(*args, **kwargs):
+        yield engineErrorIndication, pduErrorIndication, errorIndex, varBinds
+
+    return _mocked_nextCmd
+
+
+def test_snmp_interfaces(mocker, data_config):
 
     expected_result_schema = {
         "$schema": "http://json-schema.org/draft-07/schema#",
@@ -39,12 +58,9 @@ def test_snmp_interfaces(mocker, data_config, snmp_walk_responses):
         }
     }
 
-    def _mocked_walk(agent_hostname, community, ignored_oid):
-        return snmp_walk_responses
-
-    mocker.patch(
-        'inventory_provider.snmp.walk',
-        _mocked_walk)
+    _mocked_nextCmd = _gen_mocked_nextCmd(interfaces_walk_responses())
+    mocker.patch('inventory_provider.snmp.nextCmd', _mocked_nextCmd)
+    mocker.patch('inventory_provider.snmp.UdpTransportTarget', lambda x: None)
 
     interfaces = list(snmp.get_router_snmp_indexes('ignored', 'ignored'))
 
-- 
GitLab