Skip to content

Commit

Permalink
Adds a check for an empty result for show intf
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanderaa authored and itdependsnetworks committed Feb 13, 2022
1 parent 6bf2d57 commit 30ec62f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions .bandit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ skips: []
# No need to check for security issues in the test scripts!
exclude_dirs:
- "./tests/"
- "./test/"
- "./docs/"
4 changes: 4 additions & 0 deletions napalm_panos/panos.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ def _extract_interface_list(self):

interface_set = set()

# Interfaces on an empty VM may have a empty list if no interfaces are defined.
if interfaces is None:
return []

for entry in interfaces.values():
for entry_contents in entry.values():
if isinstance(entry_contents, dict):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<response status="success"><result>
</result></response>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
117 changes: 116 additions & 1 deletion test/unit/test_getters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,126 @@
"""Tests for getters."""
import functools
import json
from itertools import zip_longest

from napalm.base.test import conftest
from napalm.base.test import helpers
from napalm.base.test import models
from napalm.base.test.getters import BaseTestGetters


import pytest


def list_dicts_diff(prv, nxt):
"""Compare two lists of dicts."""
result = []
for prv_element, nxt_element in zip_longest(prv, nxt, fillvalue={}):
intermediate_result = dict_diff(prv_element, nxt_element)
if intermediate_result:
result.append(intermediate_result)
return result


def dict_diff(prv, nxt):
"""Return a dict of keys that differ with another config object."""
keys = set(list(prv.keys()) + list(nxt.keys()))
result = {}

for k in keys:
if isinstance(prv.get(k), dict):
if isinstance(nxt.get(k), dict):
"If both are dicts we do a recursive call."
diff = dict_diff(prv.get(k), nxt.get(k))
if diff:
result[k] = diff
else:
"If only one is a dict they are clearly different"
result[k] = {"result": prv.get(k), "expected": nxt.get(k)}
else:
"Ellipsis is a wildcard." ""
if prv.get(k) != nxt.get(k) and nxt.get(k) != "...":
result[k] = {"result": prv.get(k), "expected": nxt.get(k)}
return result


def wrap_test_cases(func):
"""Wrap test cases."""
func.__dict__["build_test_cases"] = True

@functools.wraps(func)
def mock_wrapper(cls, test_case):
for patched_attr in cls.device.patched_attrs:
attr = getattr(cls.device, patched_attr)
attr.current_test = func.__name__
attr.current_test_case = test_case

try:
# This is an ugly, ugly, ugly hack because some python objects don't load
# as expected. For example, dicts where integers are strings
result = json.loads(json.dumps(func(cls, test_case)))
except IOError:
if test_case == "no_test_case_found":
pytest.fail("No test case for '{}' found".format(func.__name__))
else:
raise
except NotImplementedError:
pytest.skip("Method not implemented")
return

# This is an ugly, ugly, ugly hack because some python objects don't load
# as expected. For example, dicts where integers are strings

try:
expected_result = attr.expected_result
except IOError as e:
raise Exception("{}. Actual result was: {}".format(e, json.dumps(result)))
if isinstance(result, list):
diff = list_dicts_diff(result, expected_result)
else:
diff = dict_diff(result, expected_result)
if diff:
print("Resulting JSON object was: {}".format(json.dumps(result)))
raise AssertionError("Expected result varies on some keys {}".format(json.dumps(diff)))

for patched_attr in cls.device.patched_attrs:
attr = getattr(cls.device, patched_attr)
attr.current_test = "" # Empty them to avoid side effects
attr.current_test_case = "" # Empty them to avoid side effects

return result

@functools.wraps(func)
def real_wrapper(cls, test_case):
try:
return func(cls, test_case)
except NotImplementedError:
pytest.skip("Method not implemented")
return

if conftest.NAPALM_TEST_MOCK:
return mock_wrapper
else:
return real_wrapper


@pytest.mark.usefixtures("set_device_parameters")
class TestGetter(BaseTestGetters):
"""Test get_* methods."""

@wrap_test_cases
def test_get_interfaces(self, test_case):
"""Test get_interfaces."""
get_interfaces = self.device.get_interfaces()
if test_case in {
"empty_interfaces",
}:
assert len(get_interfaces) == 0
else:
assert len(get_interfaces) > 0

# for interface, interface_data in get_interfaces.items():
# assert helpers.test_model(InterfaceDict, interface_data)
for interface, interface_data in get_interfaces.items():
assert helpers.test_model(models.interface, interface_data)

return get_interfaces

0 comments on commit 30ec62f

Please sign in to comment.