def test_duplicate_vrf(self): vrf = VRF.objects.create(name="Test", rd="1:1", enforce_unique=False) Prefix.objects.create(vrf=vrf, prefix=netaddr.IPNetwork("192.0.2.0/24")) duplicate_prefix = Prefix(vrf=vrf, prefix=netaddr.IPNetwork("192.0.2.0/24")) self.assertIsNone(duplicate_prefix.clean())
def test_get_first_available_prefix(self): prefixes = Prefix.objects.bulk_create(( Prefix(prefix=netaddr.IPNetwork("10.0.0.0/16")), # Parent prefix Prefix(prefix=netaddr.IPNetwork("10.0.0.0/24")), Prefix(prefix=netaddr.IPNetwork("10.0.1.0/24")), Prefix(prefix=netaddr.IPNetwork("10.0.2.0/24")), )) self.assertEqual(prefixes[0].get_first_available_prefix(), netaddr.IPNetwork("10.0.3.0/24")) Prefix.objects.create(prefix=netaddr.IPNetwork("10.0.3.0/24")) self.assertEqual(prefixes[0].get_first_available_prefix(), netaddr.IPNetwork("10.0.4.0/22"))
def test_duplicate_vrf_unique(self): vrf = VRF.objects.create(name="Test", rd="1:1", enforce_unique=True) Prefix.objects.create(vrf=vrf, prefix=netaddr.IPNetwork("192.0.2.0/24")) duplicate_prefix = Prefix(vrf=vrf, prefix=netaddr.IPNetwork("192.0.2.0/24")) self.assertRaises(ValidationError, duplicate_prefix.clean)
def test_get_available_prefixes(self): prefixes = Prefix.objects.bulk_create(( Prefix(prefix=netaddr.IPNetwork("10.0.0.0/16")), # Parent prefix Prefix(prefix=netaddr.IPNetwork("10.0.0.0/20")), Prefix(prefix=netaddr.IPNetwork("10.0.32.0/20")), Prefix(prefix=netaddr.IPNetwork("10.0.128.0/18")), )) missing_prefixes = netaddr.IPSet([ netaddr.IPNetwork("10.0.16.0/20"), netaddr.IPNetwork("10.0.48.0/20"), netaddr.IPNetwork("10.0.64.0/18"), netaddr.IPNetwork("10.0.192.0/18"), ]) available_prefixes = prefixes[0].get_available_prefixes() self.assertEqual(available_prefixes, missing_prefixes)
def test_prefix_initial(self): """Ensure initial kwargs for prefix is passed through.""" with mock.patch( "nautobot.utilities.forms.forms.forms.ModelForm.__init__" ) as mock_init: prefix_none = Prefix() PrefixFieldMixin(initial=self.initial, instance=prefix_none) mock_init.assert_called_with(initial=self.initial, instance=prefix_none)
def test_get_utilization(self): # Container Prefix prefix = Prefix.objects.create(prefix=netaddr.IPNetwork("10.0.0.0/24"), status=Prefix.STATUS_CONTAINER) Prefix.objects.bulk_create(( Prefix(prefix=netaddr.IPNetwork("10.0.0.0/26")), Prefix(prefix=netaddr.IPNetwork("10.0.0.128/26")), )) self.assertEqual(prefix.get_utilization(), (128, 256)) # IPv4 Non-container Prefix /24 prefix.status = self.statuses.get(slug="active") prefix.save() IPAddress.objects.bulk_create( # Create 32 IPAddresses within the Prefix [ IPAddress(address=netaddr.IPNetwork("10.0.0.{}/24".format(i))) for i in range(1, 33) ]) # Create IPAddress objects for network and broadcast addresses IPAddress.objects.bulk_create( (IPAddress(address=netaddr.IPNetwork("10.0.0.0/32")), IPAddress(address=netaddr.IPNetwork("10.0.0.255/32")))) self.assertEqual(prefix.get_utilization(), (32, 254)) # Change prefix to a pool, network and broadcast address will count toward numerator and denominator in utilization prefix.is_pool = True prefix.save() self.assertEqual(prefix.get_utilization(), (34, 256)) # IPv4 Non-container Prefix /31, network and broadcast addresses count toward utilization prefix = Prefix.objects.create(prefix="10.0.1.0/31") IPAddress.objects.bulk_create( (IPAddress(address=netaddr.IPNetwork("10.0.1.0/32")), IPAddress(address=netaddr.IPNetwork("10.0.1.1/32")))) self.assertEqual(prefix.get_utilization(), (2, 2)) # IPv6 Non-container Prefix, network and broadcast addresses count toward utilization prefix = Prefix.objects.create(prefix="aaaa::/124") IPAddress.objects.bulk_create( (IPAddress(address=netaddr.IPNetwork("aaaa::0/128")), IPAddress(address=netaddr.IPNetwork("aaaa::f/128")))) self.assertEqual(prefix.get_utilization(), (2, 16))
def test_get_utilization(self): # Container Prefix prefix = Prefix.objects.create(prefix=netaddr.IPNetwork("10.0.0.0/24"), status=Prefix.STATUS_CONTAINER) Prefix.objects.bulk_create( ( Prefix(prefix=netaddr.IPNetwork("10.0.0.0/26")), Prefix(prefix=netaddr.IPNetwork("10.0.0.128/26")), ) ) self.assertEqual(prefix.get_utilization(), (128, 256)) # Non-container Prefix prefix.status = self.statuses.get(slug="active") prefix.save() IPAddress.objects.bulk_create( # Create 32 IPAddresses within the Prefix [IPAddress(address=netaddr.IPNetwork("10.0.0.{}/24".format(i))) for i in range(1, 33)] ) self.assertEqual(prefix.get_utilization(), (32, 254))
def test_child_relationship_flat(self): """ Test that 10.0.0.0/24 is NOT shown under 10.0.0.0/16, so the table is a flat list """ status = Status.objects.get(slug="active") Prefix(prefix=netaddr.IPNetwork("10.0.0.0/16"), status=status).validated_save() Prefix(prefix=netaddr.IPNetwork("10.0.0.0/24"), status=status).validated_save() # Navigate to Prefix list view self.browser.visit(self.live_server_url) self.browser.links.find_by_partial_text("IPAM").click() self.browser.links.find_by_partial_text("Prefixes").click() self.assertEqual( len(self.browser.find_by_tag("tr")[1].find_by_text("10.0.0.0/16")), 1) # 10.0.0.0/16 is first self.assertEqual( len(self.browser.find_by_tag("tr")[2].find_by_text("10.0.0.0/24")), 1) # 10.0.0.0/24 is second self.assertEqual( len(self.browser.find_by_tag("tr")[2].find_by_tag("i")), 0) # 10.0.0.0/24 is *not* indented via an <i> tag
def test_get_utilization(self): rir = RIR.objects.create(name="RIR 1", slug="rir-1") aggregate = Aggregate(prefix=netaddr.IPNetwork("10.0.0.0/8"), rir=rir) aggregate.save() # 25% utilization Prefix.objects.bulk_create(( Prefix(prefix=netaddr.IPNetwork("10.0.0.0/12")), Prefix(prefix=netaddr.IPNetwork("10.16.0.0/12")), Prefix(prefix=netaddr.IPNetwork("10.32.0.0/12")), Prefix(prefix=netaddr.IPNetwork("10.48.0.0/12")), )) self.assertEqual(aggregate.get_utilization(), (4194304, 16777216)) # 50% utilization Prefix.objects.bulk_create( (Prefix(prefix=netaddr.IPNetwork("10.64.0.0/10")), )) self.assertEqual(aggregate.get_utilization(), (8388608, 16777216)) # 100% utilization Prefix.objects.bulk_create( (Prefix(prefix=netaddr.IPNetwork("10.128.0.0/9")), )) self.assertEqual(aggregate.get_utilization(), (16777216, 16777216))
def test_duplicate_global_unique(self): Prefix.objects.create(prefix=netaddr.IPNetwork("192.0.2.0/24")) duplicate_prefix = Prefix(prefix=netaddr.IPNetwork("192.0.2.0/24")) self.assertRaises(ValidationError, duplicate_prefix.clean)
def test_duplicate_global(self): Prefix.objects.create(prefix=netaddr.IPNetwork("192.0.2.0/24")) duplicate_prefix = Prefix(prefix=netaddr.IPNetwork("192.0.2.0/24")) self.assertIsNone(duplicate_prefix.clean())