Select Git revision
CompendiumData.tsx
test_parse_router.py 2.38 KiB
import json
import os
from unittest.mock import patch, MagicMock
from jsonschema import validate
import pytest
from resource_management.hardware.router \
import load_line_cards, LINE_CARDS_LIST_SCHEMA
# another possible approach: try to mock the netconf responses
# directly and use the code from:
# https://github.com/GIC-de/Juniper-PyEZ-Unit-Testing
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
def _load_json_data(filename):
with open(os.path.join(DATA_DIR, filename)) as f:
return json.loads(f.read())
class _namespace(object):
pass
HOSTNAMES = [
'vmx',
'mx1.lab.office.geant.net',
'mx2.lab.office.geant.net',
'mx3.lab.office.geant.net',
'mx4.lab.office.geant.net',
'mx5.lab.office.geant.net',
]
def pytest_generate_tests(metafunc):
metafunc.parametrize("router_name", HOSTNAMES)
@pytest.fixture
def mocked_pyez_vmx(router_name):
def _table_data_to_iter(json_data_filename):
# simple simulation of how pyez renders
# a table as an iterable
#
# datafile should be something formatted as
# if (or actually captured) from a pyez table
# object's to_json() method
for name, data in _load_json_data(json_data_filename).items():
o = _namespace()
o.name = o.key = name
for k, v in data.items():
setattr(o, k, v)
yield o
m = 'resource_management.hardware.router'
with patch(f'{m}.PhyPortTable') as PhyPortTable:
with patch(f'{m}.FpcHwTable') as FpcHwTable:
with patch(f'{m}.Device') as Device:
phy_port_table = MagicMock()
phy_port_table.__iter__.return_value \
= _table_data_to_iter(f'{router_name}-PhyPortTable.json')
PhyPortTable.return_value = phy_port_table
fpc_hw_table = MagicMock()
fpc_hw_table.__iter__.return_value \
= _table_data_to_iter(f'{router_name}-FpcHwTable.json')
FpcHwTable.return_value = fpc_hw_table
yield # stay in this block until caller context exits
def test_load_line_cards(mocked_pyez_vmx):
fpcs = load_line_cards(
hostname='bogus', username='bogus', key_filename='no file')
fpcs = list(fpcs)
validate(fpcs, LINE_CARDS_LIST_SCHEMA)
assert len(fpcs) > 0 # because we know the test data is non-empty