forked from aerleon/aerleon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: added tests and test case generating function for iputils
- Loading branch information
1 parent
b372939
commit cbbd3a7
Showing
7 changed files
with
102 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool:pytest] | ||
markers = | ||
unit: Marks a unit test | ||
testpath = tests |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
|
||
import pathlib | ||
|
||
from capirca.utils import iputils | ||
from capirca.lib import nacaddr | ||
|
||
|
||
file_directory = pathlib.Path(__file__).parent.absolute() | ||
exclude_address_testcases = [] | ||
with open(str(file_directory)+"/address_exclude_test_cases.txt", 'r') as f: | ||
for line in f: | ||
ipstr, exstrs, restrs = line.strip().split(' ') | ||
ip = nacaddr.IP(ipstr) | ||
exclude_ips = list(map(nacaddr.IP, exstrs.split(','))) | ||
expected_results = [] | ||
for i in restrs.split(';'): | ||
result_strings = i.split(',') | ||
ip_map = map(nacaddr.IP, result_strings) | ||
ip_list = list(ip_map) | ||
expected_results.append(ip_list) | ||
for ex, res in zip(exclude_ips, expected_results): | ||
exclude_address_testcases.append((ip, ex, res)) | ||
|
||
class TestIPUtils: | ||
@pytest.mark.unit | ||
@pytest.mark.parametrize("ip,exclude,expected", exclude_address_testcases) | ||
def test_exclude_address(self, ip, exclude, expected): | ||
result = iputils.exclude_address(ip, exclude) | ||
|
||
assert list(result) == expected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import random | ||
import ipaddress | ||
import itertools as it | ||
|
||
|
||
def write_excludes_testcase(ipstr, excludelist='', max_prefix_range=8, max_random_subnets=30): | ||
""" | ||
Writes a testcase to the tests/utils/address_exclude_test_cases.txt file. | ||
Note that the number of prefixes to consider grows exponentially, so unless | ||
you *do* want to consider a large pool to randomly select from, keep it at the default | ||
Args: | ||
ipstr: the ip network as a string (v4 or v6) to base the test on. | ||
excludelist: optional comma-separated string of ip networks to exclude | ||
max_prefix_range: the largest number of prefixes to consider. | ||
max_random_subnets: the number of subnets to do exclusion tests for, if randomly generating | ||
Returns: | ||
None | ||
""" | ||
ip = ipaddress.ip_network(ipstr) | ||
if len(excludelist) == 0: # empty excludelist, making a random one | ||
prefixrange = min(max_prefix_range, ip.max_prefixlen - ip.prefixlen) | ||
excludelist = it.chain.from_iterable(ip.subnets(i) for i in range(1, prefixrange+1)) | ||
total_ips = 2**prefixrange | ||
ip_positions = set( | ||
random.choices( | ||
range(total_ips), | ||
k=min( | ||
max_random_subnets, | ||
total_ips | ||
) | ||
) | ||
) | ||
compress_map = (1 if i in ip_positions else 0 for i in range(total_ips)) | ||
excludelist = list(it.compress(excludelist, compress_map)) | ||
else: | ||
excludelist = list(map(ipaddress.ip_network, excludelist.split(','))) | ||
|
||
result_list = [] | ||
for address in excludelist: | ||
result_list.append(ip.address_exclude(address)) | ||
|
||
ipst = str(ip) | ||
exst = ",".join(map(str, excludelist)) | ||
rest = ";".join(",".join(map(str, sorted(result))) for result in result_list) | ||
with open('tests/utils/address_exclude_test_cases.txt', 'a') as f: | ||
f.write("%s %s %s\n" % (ipst, exst, rest)) | ||
|
||
|
||
|