Update tests and alernate ordering

This commit is contained in:
Pieter Lambrecht 2022-06-16 17:14:20 +02:00
parent 08d38574c0
commit c433b1c95c
3 changed files with 58 additions and 37 deletions

View File

@ -735,10 +735,13 @@ inventory-items:
self.assertEqual(po1.feed_leg, PowerOutletFeedLegChoices.FEED_LEG_A)
self.assertEqual(device_type.interfacetemplates.count(), 3)
iface1 = InterfaceTemplate.objects.first()
iface1 = InterfaceTemplate.objects.last()
self.assertEqual(iface1.name, 'Interface 1')
self.assertEqual(iface1.type, InterfaceTypeChoices.TYPE_1GE_FIXED)
self.assertTrue(iface1.mgmt_only)
iface2 = InterfaceTemplate.objects.first()
self.assertEqual(iface2.name, 'Interface 2')
self.assertEqual(iface2.type, InterfaceTypeChoices.TYPE_1GE_FIXED)
self.assertEqual(device_type.rearporttemplates.count(), 3)
rp1 = RearPortTemplate.objects.first()
@ -1041,10 +1044,13 @@ front-ports:
self.assertEqual(po1.feed_leg, PowerOutletFeedLegChoices.FEED_LEG_A)
self.assertEqual(module_type.interfacetemplates.count(), 3)
iface1 = InterfaceTemplate.objects.first()
iface1 = InterfaceTemplate.objects.last()
self.assertEqual(iface1.name, 'Interface 1')
self.assertEqual(iface1.type, InterfaceTypeChoices.TYPE_1GE_FIXED)
self.assertTrue(iface1.mgmt_only)
iface2 = InterfaceTemplate.objects.first()
self.assertEqual(iface2.name, 'Interface 2')
self.assertEqual(iface2.type, InterfaceTypeChoices.TYPE_1GE_FIXED)
self.assertEqual(module_type.rearporttemplates.count(), 3)
rp1 = RearPortTemplate.objects.first()

View File

@ -48,15 +48,28 @@ def naturalize_interface(value, max_length, integer_places=5, model_instance=Non
digit_separators = [':', '/', '-']
subinterface_separators = ['.']
interface_remainder_len = 10
interface_type_sort_length = 4
interface_type_weight_list = {
r'^([fgstx]e|et|lt)-': '05', # Group juniper interfaces (https://www.juniper.net/documentation/us/en/software/junos/interfaces-fundamentals/topics/topic-map/router-interfaces-overview.html). Other Juniper interfaces will come after these and sorted alphabetically
r'^ethernet': '10', # Group Arista Interfaces Ethernet1, Ethernet2, Ethernet49/1, Ethernet50/1
r'[' + '\\'.join(digit_separators) + ']+': '15', # Group Anything with a digit_separator or subinterface_separators
r'^\d+\.?\d*$': '15', # Group Only digits together with digit_separator, including optional subinterface
r'eth': '20', # Group Anything with eth in the name
r'^([fgstx]e|et|lt)-': '5', # Group Juniper interfaces (https://www.juniper.net/documentation/us/en/software/junos/interfaces-fundamentals/topics/topic-map/router-interfaces-overview.html). Other Juniper interfaces will come after these and sorted alphabetically
r'^ethernet\d+(\/[1-4])?$': '10', # Group Arista Interfaces Ethernet1, Ethernet2, Ethernet49/1, Ethernet50/1
r'^(fa|gi|ten|hun)[a-z]*\d+$': '16', # Group Cisco Interfaces with only numbers after with digit_separators
r'^(fa|gi|ten|hun)[a-z]*\d+?[' + '\\'.join(subinterface_separators) + r']\d*': '16', # Group Cisco Interfaces with only numbers after with digit_separators
r'^(fa|gi|ten|hun)[a-z]*\d+[' + '\\'.join(digit_separators) + ']+': '15', # Group Cisco Interfaces (Ethernet, FastEthernet, GigeEthernet, TenEthernet, HundredEthernet) with digit_separators
r'^[e]?\d+[a-z]*$': '20', # Group Netapp Interfaces
r'^[^a-z]*$': '25', # Group Only digits together
r'[' + '\\'.join(digit_separators) + ']+': '30', # Group Anything with a digit_separator
r'eth': '35', # Group Anything with eth in the name
}
# interface_type_weight_list = {
# r'^([fgstx]e|et|lt)-': '05', # Group juniper interfaces (https://www.juniper.net/documentation/us/en/software/junos/interfaces-fundamentals/topics/topic-map/router-interfaces-overview.html). Other Juniper interfaces will come after these and sorted alphabetically
# r'^ethernet\d+(\/[1-4])?$': '10', # Group Arista Interfaces Ethernet1, Ethernet2, Ethernet49/1, Ethernet50/1
# r'[' + '\\'.join(digit_separators) + ']+[' + '\\'.join(subinterface_separators) + ']*': '15', # Group Anything with a digit_separator and an optional subinterface_separator
# r'^\d+' + '\\'.join(subinterface_separators) + '?\d*$': '15', # Group Only digits together with digit_separator, including optional subinterface
# r'eth': '20', # Group Anything with eth in the name
# }
if integer_places < 5:
integer_places = 5
@ -65,21 +78,24 @@ def naturalize_interface(value, max_length, integer_places=5, model_instance=Non
if parts:
# If the last part of the interface name is non-digit, then this will be added to the naturalized name for sorting purposes. Maxlength is 10 (space padded)
interface_remainder = ''.ljust(interface_remainder_len, ' ')
if re.match(r'[^\d]+', parts[-1]):
interface_remainder = naturalize(parts[-1], interface_remainder_len).ljust(interface_remainder_len, ' ')
parts.pop()
interface_remainder = ''.ljust(interface_remainder_len, '.')
if len(parts) > 1:
if re.match(r'[^\d]+', parts[-1]):
interface_remainder = naturalize(parts[-1], interface_remainder_len).ljust(interface_remainder_len, '.')
parts.pop()
if re.match(r'[^\d]+', parts[0]):
interface_type_weight = parts[0][:interface_type_sort_length].upper()
parts.pop(0)
interface_type_weight = value[:2].upper() # the two character of the interface name will determin the sort order
if getattr(model_instance, 'mgmt_only', False):
interface_type_weight = "ZZ" # mgmt interfaces are put at the end of the table
interface_type_weight = ''.ljust(interface_type_sort_length, 'Z') # mgmt interfaces are put at the end of the table
else:
for regmatch, weigth in interface_type_weight_list.items(): # unless it matches a specific pattern, then the interfaces will be grouped
if re.search(regmatch, value, re.IGNORECASE): # first match is applied
interface_type_weight = weigth
break
output = interface_type_weight.rjust(2, '9')
output = interface_type_weight.ljust(interface_type_sort_length, '0')
for part in parts:
if part.isdigit():

View File

@ -32,34 +32,33 @@ class NaturalizationTestCase(TestCase):
data = (
# IOS/JunOS-style
('Gi', '9999999999999999Gi..................'),
('Gi1', '9999999999999999Gi000001............'),
('Gi1.0', '9999999999999999Gi000001......000000'),
('Gi1.1', '9999999999999999Gi000001......000001'),
('Gi1:0', '9999999999999999Gi000001000000......'),
('Gi1:0.0', '9999999999999999Gi000001000000000000'),
('Gi1:0.1', '9999999999999999Gi000001000000000001'),
('Gi1:1', '9999999999999999Gi000001000001......'),
('Gi1:1.0', '9999999999999999Gi000001000001000000'),
('Gi1:1.1', '9999999999999999Gi000001000001000001'),
('Gi1/2', '0001999999999999Gi000002............'),
('Gi1/2/3', '0001000299999999Gi000003............'),
('Gi1/2/3/4', '0001000200039999Gi000004............'),
('Gi1/2/3/4/5', '0001000200030004Gi000005............'),
('Gi1/2/3/4/5:6', '0001000200030004Gi000005000006......'),
('Gi1/2/3/4/5:6.7', '0001000200030004Gi000005000006000007'),
('Gi', 'GI........................................................................................Gi........'),
('Gi1', '1500002.............................................................................................'),
('Gi1.0', '1500002000001.......................................................................................'),
('Gi1.1', '1500002000002.......................................................................................'),
('Gi1:0', '1500002900001.......................................................................................'),
('Gi1:0.0', '1500002900001000001.................................................................................'),
('Gi1:0.1', '1500002900001000002.................................................................................'),
('Gi1:1', '1500002900002.......................................................................................'),
('Gi1:1.0', '1500002900002000001.................................................................................'),
('Gi1:1.1', '1500002900002000002.................................................................................'),
('Gi1/2', '1500002900003.......................................................................................'),
('Gi1/2/3', '1500002900003900004.................................................................................'),
('Gi1/2/3/4', '1500002900003900004900005...........................................................................'),
('Gi1/2/3/4/5', '1500002900003900004900005900006.....................................................................'),
('Gi1/2/3/4/5:6', '1500002900003900004900005900006900007...............................................................'),
('Gi1/2/3/4/5:6.7', '1500002900003900004900005900006900007000008.........................................................'),
# Generic
('Interface 1', '9999999999999999Interface 000001............'),
('Interface 1 (other)', '9999999999999999Interface 000001............ (other)'),
('Interface 99', '9999999999999999Interface 000099............'),
('PCIe1-p1', '9999999999999999PCIe000001............-p00000001'),
('PCIe1-p99', '9999999999999999PCIe000001............-p00000099'),
('PCIe1-p1', '200000200002........................................................................................'),
('PCIe1-p99', '200000200100........................................................................................'),
('Interface 1', 'IN00002.............................................................................................'),
('Interface 1 (other)', 'IN00002................................................................................... (other)..'),
('Interface 99', 'IN00100.............................................................................................'),
)
for origin, naturalized in data:
self.assertEqual(naturalize_interface(origin, max_length=100), naturalized)
def test_naturalize_interface_max_length(self):
self.assertEqual(naturalize_interface('Gi1/2/3', max_length=20), '0001000299999999Gi00')
self.assertEqual(naturalize_interface('Gi1/2/3', max_length=20), '1500002900003900004.')