netutils icon indicating copy to clipboard operation
netutils copied to clipboard

Create IP randomization function for test suites

Open dgjustice opened this issue 4 years ago • 6 comments

Environment

  • netutils version:

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:

  • address family
  • class
  • mask length

returns:

  • str
  • ipaddress.ip_address/ipaddress.ip_network
  • packed bytes?
  • optional?

Use Case

Random IP/networks for unit testing

@itdependsnetworks @jeffkala

dgjustice avatar Jun 18 '21 13:06 dgjustice

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.

dgjustice avatar Jun 18 '21 13:06 dgjustice

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

jeffkala avatar Jun 18 '21 13:06 jeffkala

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

itdependsnetworks avatar Jun 18 '21 13:06 itdependsnetworks

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?

dgjustice avatar Jun 18 '21 15:06 dgjustice

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.

itdependsnetworks avatar Jun 18 '21 16:06 itdependsnetworks

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.

dgjustice avatar Jun 24 '21 23:06 dgjustice