Skip to content

Commit 4190a5f

Browse files
Merge pull request #113 from network-unit-testing-system/111-support-regex-for-interfaces-in-vrf
add regex
2 parents c1f2a59 + 6de37bc commit 4190a5f

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

docs/source/testbundles/alltestbundles.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ Network Instances
408408
- host: <host name, required>
409409
network_instance: <VRF name, required>
410410
interfaces:
411-
- <interface name>
411+
- <interface name, regex allowed>
412412
route_distinguisher: "<number>:<number>"
413413
414414
Required fields for specific tests in this bundle:
@@ -429,6 +429,7 @@ Required fields for specific tests in this bundle:
429429
interfaces:
430430
- GigabitEthernet2
431431
- GigabitEthernet3
432+
- GigabitEthernet\d{1}
432433
- Loopback0
433434
route_distinguisher: "1:1"
434435

nuts/base_tests/napalm_network_instances.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Query network instances of a device."""
22

3+
import copy
4+
import re
35
from typing import Dict, List, Callable, Any
46

57
import pytest
@@ -50,7 +52,21 @@ def test_network_instance_exists(self, single_result, network_instance):
5052
def test_network_instance_contains_interfaces(
5153
self, single_result: NutsResult, network_instance: str, interfaces: List[str]
5254
) -> None:
53-
assert single_result.result[network_instance]["interfaces"] == interfaces
55+
result = copy.deepcopy(single_result.result[network_instance]["interfaces"])
56+
patterns = len(interfaces)
57+
matches = 0
58+
for interface in interfaces:
59+
pattern = re.compile(interface)
60+
for i in result:
61+
if pattern.match(i):
62+
single_result.result[network_instance]["interfaces"].remove(i)
63+
if len(result) != len(single_result.result[network_instance]["interfaces"]):
64+
result = copy.deepcopy(
65+
single_result.result[network_instance]["interfaces"]
66+
)
67+
matches += 1
68+
assert patterns == matches
69+
assert result == []
5470

5571
@pytest.mark.nuts("network_instance,route_distinguisher")
5672
def test_route_distinguisher(

tests/base_tests/test_napalm_network_instances.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import copy
2+
import re
13
import pytest
24
from nornir.core.task import AggregatedResult
35
from nornir_napalm.plugins import tasks
@@ -137,6 +139,34 @@
137139
},
138140
)
139141

142+
r2_default_range = SelfTestData(
143+
name="r2_default",
144+
nornir_raw_result=nornir_raw_result_r2,
145+
test_data={
146+
"host": "R2",
147+
"network_instance": "default",
148+
"interfaces": [
149+
"GigabitEthernet[2-4]",
150+
"Loopback0",
151+
],
152+
"route_distinguisher": "",
153+
},
154+
)
155+
156+
r2_default_digit = SelfTestData(
157+
name="r2_default",
158+
nornir_raw_result=nornir_raw_result_r2,
159+
test_data={
160+
"host": "R2",
161+
"network_instance": "default",
162+
"interfaces": [
163+
"GigabitEthernet\\d{1}",
164+
"Loopback0",
165+
],
166+
"route_distinguisher": "",
167+
},
168+
)
169+
140170

141171
r2_mgmt = SelfTestData(
142172
name="r2_mgmt",
@@ -171,6 +201,8 @@ def general_result(timeouted_multiresult):
171201
r1_space,
172202
r1_ship,
173203
r2_default,
204+
r2_default_range,
205+
r2_default_digit,
174206
r2_mgmt,
175207
],
176208
ids=lambda data: data.name,
@@ -213,7 +245,20 @@ def test_contains_interfaces_at_network_instance(transformed_result, testdata):
213245

214246
host_result = transformed_result[host]
215247
host_result.validate()
216-
assert host_result.result[network_instance]["interfaces"] == expected
248+
patterns = len(expected)
249+
matches = 0
250+
result = copy.deepcopy(host_result.result[network_instance]["interfaces"])
251+
for interface in expected:
252+
pattern = re.compile(interface)
253+
for i in result:
254+
if pattern.match(i):
255+
host_result.result[network_instance]["interfaces"].remove(i)
256+
if len(result) != len(host_result.result[network_instance]["interfaces"]):
257+
result = copy.deepcopy(host_result.result[network_instance]["interfaces"])
258+
matches += 1
259+
assert patterns == matches
260+
assert result == []
261+
# assert host_result.result[network_instance]["interfaces"] == expected
217262

218263

219264
def test_contains_route_distinguisher_at_network_instance(transformed_result, testdata):

0 commit comments

Comments
 (0)