|
@@ -21,7 +21,7 @@ from ipam.views import AggregatePrefixesView, PrefixPrefixesView
|
|
|
from netbox.choices import CSVDelimiterChoices, ImportFormatChoices
|
|
from netbox.choices import CSVDelimiterChoices, ImportFormatChoices
|
|
|
from tenancy.models import Tenant
|
|
from tenancy.models import Tenant
|
|
|
from users.models import Group, ObjectPermission, Owner
|
|
from users.models import Group, ObjectPermission, Owner
|
|
|
-from utilities.testing import ViewTestCases, create_tags, post_data
|
|
|
|
|
|
|
+from utilities.testing import TestCase, ViewTestCases, create_tags, post_data
|
|
|
|
|
|
|
|
|
|
|
|
|
class ASNRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
|
class ASNRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
|
@@ -1907,6 +1907,142 @@ description: Test Site-Specific VLAN
|
|
|
self.assertEqual(prefix.vlan, vlan_site)
|
|
self.assertEqual(prefix.vlan, vlan_site)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class ChildPrefixTabTestCase(TestCase):
|
|
|
|
|
+ """
|
|
|
|
|
+ Pin the rows, order and pagination of the aggregate and prefix child prefix tabs.
|
|
|
|
|
+ """
|
|
|
|
|
+
|
|
|
|
|
+ @classmethod
|
|
|
|
|
+ def setUpTestData(cls):
|
|
|
|
|
+ rir = RIR.objects.create(name='RIR 1', slug='rir-1')
|
|
|
|
|
+ cls.aggregate = Aggregate.objects.create(prefix=IPNetwork('10.0.0.0/16'), rir=rir)
|
|
|
|
|
+ cls.parent = Prefix.objects.create(prefix=IPNetwork('10.1.0.0/16'), status=PrefixStatusChoices.STATUS_CONTAINER)
|
|
|
|
|
+ vrfs = (VRF(name='VRF A'), VRF(name='VRF B'))
|
|
|
|
|
+ VRF.objects.bulk_create(vrfs)
|
|
|
|
|
+
|
|
|
|
|
+ # Created out of display order. The descriptions identify duplicates of the same prefix.
|
|
|
|
|
+ for n in (0, 1):
|
|
|
|
|
+ Prefix.objects.bulk_create((
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.4.0/22'), description='d'),
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.0.0/24'), vrf=vrfs[0], description='b1'),
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.0.0/24'), description='a'),
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.0.0/24'), vrf=vrfs[0], description='b2'),
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.0.128/25'), description='c'),
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.1.0/24'), vrf=vrfs[1], description='e'),
|
|
|
|
|
+ Prefix(prefix=IPNetwork(f'10.{n}.255.0/24'), description='f'),
|
|
|
|
|
+ ))
|
|
|
|
|
+
|
|
|
|
|
+ def setUp(self):
|
|
|
|
|
+ super().setUp()
|
|
|
|
|
+ self.add_permissions('ipam.view_aggregate', 'ipam.view_prefix')
|
|
|
|
|
+
|
|
|
|
|
+ def tabs(self):
|
|
|
|
|
+ return (
|
|
|
|
|
+ (reverse('ipam:aggregate_prefixes', kwargs={'pk': self.aggregate.pk}), 0),
|
|
|
|
|
+ (reverse('ipam:prefix_prefixes', kwargs={'pk': self.parent.pk}), 1),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ def get_rows(self, url, **params):
|
|
|
|
|
+ response = self.client.get(url, params)
|
|
|
|
|
+ self.assertHttpStatus(response, 200)
|
|
|
|
|
+ rows = []
|
|
|
|
|
+ for row in response.context['table'].page.object_list:
|
|
|
|
|
+ prefix = row.record
|
|
|
|
|
+ vrf = prefix.vrf.name if prefix.vrf else None
|
|
|
|
|
+ rows.append((str(prefix.prefix), vrf, prefix.description if prefix.pk else 'available'))
|
|
|
|
|
+ return rows
|
|
|
|
|
+
|
|
|
|
|
+ @staticmethod
|
|
|
|
|
+ def expected_rows(n, show_available=True, show_assigned=True):
|
|
|
|
|
+ assigned = {
|
|
|
|
|
+ 'a': (f'10.{n}.0.0/24', None, 'a'),
|
|
|
|
|
+ 'b1': (f'10.{n}.0.0/24', 'VRF A', 'b1'),
|
|
|
|
|
+ 'b2': (f'10.{n}.0.0/24', 'VRF A', 'b2'),
|
|
|
|
|
+ 'c': (f'10.{n}.0.128/25', None, 'c'),
|
|
|
|
|
+ 'e': (f'10.{n}.1.0/24', 'VRF B', 'e'),
|
|
|
|
|
+ 'd': (f'10.{n}.4.0/22', None, 'd'),
|
|
|
|
|
+ 'f': (f'10.{n}.255.0/24', None, 'f'),
|
|
|
|
|
+ }
|
|
|
|
|
+ available_after_e = [f'10.{n}.2.0/23']
|
|
|
|
|
+ available_after_d = [
|
|
|
|
|
+ f'10.{n}.8.0/21', f'10.{n}.16.0/20', f'10.{n}.32.0/19', f'10.{n}.64.0/18', f'10.{n}.128.0/18',
|
|
|
|
|
+ f'10.{n}.192.0/19', f'10.{n}.224.0/20', f'10.{n}.240.0/21', f'10.{n}.248.0/22', f'10.{n}.252.0/23',
|
|
|
|
|
+ f'10.{n}.254.0/24',
|
|
|
|
|
+ ]
|
|
|
|
|
+ rows = []
|
|
|
|
|
+ for key in ('a', 'b1', 'b2', 'c', 'e', 'd', 'f'):
|
|
|
|
|
+ if show_assigned:
|
|
|
|
|
+ rows.append(assigned[key])
|
|
|
|
|
+ if show_available and key == 'e':
|
|
|
|
|
+ rows += [(p, None, 'available') for p in available_after_e]
|
|
|
|
|
+ if show_available and key == 'd':
|
|
|
|
|
+ rows += [(p, None, 'available') for p in available_after_d]
|
|
|
|
|
+ return rows
|
|
|
|
|
+
|
|
|
|
|
+ def test_show_all(self):
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url), self.expected_rows(n))
|
|
|
|
|
+
|
|
|
|
|
+ def test_show_assigned(self):
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ self.get_rows(url, show_available='false'),
|
|
|
|
|
+ self.expected_rows(n, show_available=False),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ def test_show_available(self):
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ self.get_rows(url, show_assigned='false'),
|
|
|
|
|
+ self.expected_rows(n, show_assigned=False),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ def test_show_neither(self):
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url, show_available='false', show_assigned='false'), [])
|
|
|
|
|
+
|
|
|
|
|
+ def test_pagination(self):
|
|
|
|
|
+ # EnhancedPaginator folds up to five orphans into the last page
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ expected = self.expected_rows(n)
|
|
|
|
|
+ with self.subTest(url=url, page=2):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url, per_page=5, page=2), expected[5:10])
|
|
|
|
|
+ with self.subTest(url=url, page='last'):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url, per_page=5, page=3), expected[10:])
|
|
|
|
|
+ expected = self.expected_rows(n, show_available=False)
|
|
|
|
|
+ with self.subTest(url=url, page='assigned last'):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url, show_available='false', per_page=1, page=2), expected[1:])
|
|
|
|
|
+
|
|
|
|
|
+ def test_sorted_by_column(self):
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ rows = {row[2]: row for row in self.expected_rows(n, show_available=False)}
|
|
|
|
|
+ # Equal prefixes keep their unsorted order (a, b1, b2)
|
|
|
|
|
+ expected = [rows[key] for key in ('f', 'd', 'e', 'c', 'a', 'b1', 'b2')]
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url, show_available='false', sort='-prefix'), expected)
|
|
|
|
|
+
|
|
|
|
|
+ def test_sort_cleared(self):
|
|
|
|
|
+ for url, n in self.tabs():
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ self.assertEqual(self.get_rows(url, sort=''), self.expected_rows(n))
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ self.get_rows(url, sort='', show_available='false'),
|
|
|
|
|
+ self.expected_rows(n, show_available=False),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ def test_first_available_prefix(self):
|
|
|
|
|
+ # A global container prefix considers only global children, so 10.1.1.0/24 (VRF B) counts as free
|
|
|
|
|
+ expected = (IPNetwork('10.0.2.0/23'), IPNetwork('10.1.1.0/24'))
|
|
|
|
|
+ for (url, n), first_available in zip(self.tabs(), expected):
|
|
|
|
|
+ with self.subTest(url=url):
|
|
|
|
|
+ response = self.client.get(url)
|
|
|
|
|
+ self.assertEqual(response.context['first_available_prefix'], first_available)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
class IPRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
|
class IPRangeTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
|
|
model = IPRange
|
|
model = IPRange
|
|
|
|
|
|