networktocode / netutils

Python library that is a collection of functions and objects for common network automation tasks.
https://netutils.readthedocs.io/
Other
217 stars 51 forks source link

Create IP randomization function for test suites #27

Open dgjustice opened 3 years ago

dgjustice commented 3 years ago

Environment

Proposed Functionality

This functionality is largely supported in Faker, but I am proposing a stand-alone utility with extra nerd knobs.

generate_random_ip(*args) and/or generate_random_network(*args)

args:

returns:

Use Case

Random IP/networks for unit testing

@itdependsnetworks @jeffkala

dgjustice commented 3 years ago

I was also thinking about supporting a whitelist or blacklist. e.g. pass it a list of blocks to exclude due to business logic restrictions etc.

jeffkala commented 3 years ago

I like the thought of a whitelist/blacklist, sounds like it would be very useful.

itdependsnetworks commented 3 years ago

Another use case I have considered in the past is maintaining octet size. This comes into play when replacing IP addresses that change the whitespace of the output. As networking is still heavy parsing, this has bit me before. Imagine

IP          Name        description
-------------------------------------------------------
10.1.1.1    Hello       description should start on char 25
10.1.1.2    World       description should start on char 25

turns into

IP          Name        description
-------------------------------------------------------
100.30.182.1    Hello         description should start on char 25
1.1.1.2    World       description should start on char 25
dgjustice commented 3 years ago

For that use case, would it be better to address the issue in the template that generates test cases?

template = """IP             Name        description
-------------------------------------------------------
{ip1}    Hello         description should start on char 25
{ip2}    World         description should start on char 25
"""

ip1 = "100.30.182.1"
ip2 = "1.1.1.2"

print(template.format(ip1=ip1, ip2=ip2))

template = """IP             Name        description
-------------------------------------------------------
{ip1:<14} Hello         description should start on char 25
{ip2:<14} World         description should start on char 25
"""

print(template.format(ip1=ip1, ip2=ip2))
IP             Name        description
-------------------------------------------------------
100.30.182.1    Hello         description should start on char 25
1.1.1.2    World         description should start on char 25

IP             Name        description
-------------------------------------------------------
100.30.182.1   Hello         description should start on char 25
1.1.1.2        World         description should start on char 25

Would this address your concern?

itdependsnetworks commented 3 years ago

The use case is anonymizing and generating the data. Imagine taking in dozens of outputs for an ntc-templates PR and you obviously cannot use your companies IPs, so you want to anonymize. If you anonymize without keeping the octet structure the same, you will end up with bad data.

dgjustice commented 3 years ago

I've poked around with this quite a bit this week, and I hope to have a draft submitted tomorrow. I think randomization and obfuscation should be dealt with in different functions. I am happy to add the latter based on the work you shared from the ansible module.