Skip to content
Snippets Groups Projects
Commit 1ed30709 authored by Erik Reid's avatar Erik Reid
Browse files

fixed mocking with test data

parent 0b4cd3df
No related branches found
No related tags found
No related merge requests found
import json
import os
from unittest.mock import patch, MagicMock
from jsonschema import validate, ValidationError
from resource_management.hardware.router import load_line_cards, LINE_CARDS_LIST_SCHEMA
# This is not yet working ... try to find the right mocks that will
# allow us to run all lines of code in router.py
#
# another possible approach: try to mock the netconf responses and use
# the code from https://github.com/GIC-de/Juniper-PyEZ-Unit-Testing
@patch('resource_management.hardware.router.PhyPortTable')
@patch('resource_management.hardware.router.FpcHwTable')
@patch('resource_management.hardware.router.Device')
def test_load_line_cards(Device, FpcHwTable, PhyPortTable):
PhyPortTable.return_value = MagicMock()
PhyPortTable.return_value.return_value = 'aaaa'
fpcs = load_line_cards(hostname='bogus', username='bogus', key_filename='no file')
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
@pytest.fixture
def mocked_pyez_vmx():
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('ports.json')
PhyPortTable.return_value = phy_port_table
fpc_hw_table = MagicMock()
fpc_hw_table.__iter__.return_value \
= _table_data_to_iter('line_cards.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) == 1
assert len(fpcs) > 0 # because we know the test data is non-empty
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment