new test generator
This commit is contained in:
parent
b6111be746
commit
f64503c1c6
|
@ -0,0 +1,52 @@
|
|||
import random
|
||||
from ipaddress import IPv6Network
|
||||
|
||||
|
||||
def parse_rule(rule):
|
||||
parts = rule.split()
|
||||
prefix, next_hop = parts
|
||||
return IPv6Network(prefix), int(next_hop)
|
||||
|
||||
|
||||
def generate_random_test_cases(rules, n):
|
||||
with open('../data/test-data', 'w') as f:
|
||||
for rule in rules:
|
||||
rule_network = rule[0]
|
||||
rule_next_hop = rule[1]
|
||||
|
||||
for _ in range(n):
|
||||
ipaddress = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)
|
||||
prefix_length = random.randint(rule_network.prefixlen, rule_network.max_prefixlen)
|
||||
test_network = IPv6Network(ipaddress).supernet(new_prefix=prefix_length)
|
||||
|
||||
closest_rule = rule
|
||||
for oth_rule in rules:
|
||||
|
||||
if oth_rule == rule:
|
||||
continue
|
||||
oth_network = oth_rule[0]
|
||||
oth_next_hop = oth_rule[1]
|
||||
if test_network.subnet_of(oth_network) and oth_network.subnet_of(closest_rule[0]):
|
||||
print(f"{test_network} is closer to {oth_network} than {closest_rule[0]}")
|
||||
closest_rule = oth_rule
|
||||
|
||||
f.write(f"{test_network} {closest_rule[1]}\n")
|
||||
print(f"Generated test case: {test_network} {closest_rule[1]}")
|
||||
print("Tests for rule", rule_network, "with next hop", rule_next_hop, "generated")
|
||||
|
||||
def main():
|
||||
rules = []
|
||||
|
||||
with open('../data/routing-data', 'r') as f:
|
||||
for line in f:
|
||||
rules.append(line.strip())
|
||||
|
||||
parsed_rules = [parse_rule(rule) for rule in rules]
|
||||
|
||||
num_tests = 10
|
||||
generate_random_test_cases(parsed_rules, num_tests)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -14,8 +14,8 @@ def generate_random_test_cases(rules, n):
|
|||
rule_network = rule[0]
|
||||
rule_next_hop = rule[1]
|
||||
|
||||
network = rule_network
|
||||
for _ in range(n):
|
||||
network = rule_network
|
||||
next_hop = rule_next_hop
|
||||
address = rule_network.network_address + random.randint(0, rule_network.num_addresses - 1)
|
||||
|
||||
|
|
Loading…
Reference in New Issue