From 907412f49cbd9e8bee60aefbae0027972a838215 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Tue, 22 Mar 2022 13:15:21 +0000 Subject: [PATCH] Fix bug in get_l2xc_interfaces(), and add tests for it --- unittest/test_interface.yaml | 8 ++++++++ validator/interface.py | 5 +++-- validator/lcp.py | 2 +- validator/test_interface.py | 22 ++++++++++++++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/unittest/test_interface.yaml b/unittest/test_interface.yaml index 2b7150f..72542b7 100644 --- a/unittest/test_interface.yaml +++ b/unittest/test_interface.yaml @@ -63,3 +63,11 @@ interfaces: l2xc: GigabitEthernet3/0/1 GigabitEthernet3/0/1: l2xc: GigabitEthernet3/0/0 + + GigabitEthernet3/0/2: + sub-interfaces: + 100: + description: "This interface connects one-way to Gi3/0/2.200. Strange, but valid." + l2xc: GigabitEthernet3/0/2.200 + 200: + description: "This interface does not connect back to Gi3/0/2.100. Strange, but valid." diff --git a/validator/interface.py b/validator/interface.py index 216454d..e33eec1 100644 --- a/validator/interface.py +++ b/validator/interface.py @@ -120,7 +120,8 @@ def get_l2xc_interfaces(yaml): if 'l2xc' in iface: ret.append(ifname) if 'sub-interfaces' in iface: - for sub_ifname, sub_iface in iface['sub-interfaces'].items(): + for subid, sub_iface in iface['sub-interfaces'].items(): + sub_ifname = "%s.%d" % (ifname, subid) if 'l2xc' in sub_iface: ret.append(sub_ifname) @@ -141,7 +142,7 @@ def get_l2xc_target_interfaces(yaml): if 'l2xc' in iface: ret.append(iface['l2xc']) if 'sub-interfaces' in iface: - for sub_ifname, sub_iface in iface['sub-interfaces'].items(): + for subid, sub_iface in iface['sub-interfaces'].items(): if 'l2xc' in sub_iface: ret.append(sub_iface['l2xc']) diff --git a/validator/lcp.py b/validator/lcp.py index aa1e170..350762c 100644 --- a/validator/lcp.py +++ b/validator/lcp.py @@ -22,7 +22,7 @@ def get_lcps(yaml): if 'lcp' in iface: ret.append(iface['lcp']) if 'sub-interfaces' in iface: - for sub_ifname, sub_iface in iface['sub-interfaces'].items(): + for subid, sub_iface in iface['sub-interfaces'].items(): if 'lcp' in sub_iface: ret.append(sub_iface['lcp']) diff --git a/validator/test_interface.py b/validator/test_interface.py index a2dedc1..fb9a94a 100644 --- a/validator/test_interface.py +++ b/validator/test_interface.py @@ -9,12 +9,12 @@ class TestInterfaceMethods(unittest.TestCase): def test_enumerators(self): ifs = interface.get_interfaces(self.cfg) - self.assertEqual(len(ifs), 16) + self.assertEqual(len(ifs), 19) self.assertIn("GigabitEthernet1/0/1", ifs) self.assertIn("GigabitEthernet1/0/1.200", ifs) ifs = interface.get_sub_interfaces(self.cfg) - self.assertEqual(len(ifs), 11) + self.assertEqual(len(ifs), 13) self.assertNotIn("GigabitEthernet1/0/1", ifs) self.assertIn("GigabitEthernet1/0/1.200", ifs) self.assertIn("GigabitEthernet1/0/1.201", ifs) @@ -28,6 +28,24 @@ class TestInterfaceMethods(unittest.TestCase): self.assertIn("GigabitEthernet1/0/1.201", ifs) self.assertIn("GigabitEthernet1/0/1.203", ifs) + ifs = interface.get_l2xc_interfaces(self.cfg) + self.assertEqual(len(ifs), 3) + self.assertIn("GigabitEthernet3/0/0", ifs) + self.assertIn("GigabitEthernet3/0/1", ifs) + self.assertIn("GigabitEthernet3/0/2.100", ifs) + self.assertNotIn("GigabitEthernet3/0/2.200", ifs) + + target_ifs = interface.get_l2xc_target_interfaces(self.cfg) + self.assertEqual(len(target_ifs), 3) + self.assertIn("GigabitEthernet3/0/0", target_ifs) + self.assertIn("GigabitEthernet3/0/1", target_ifs) + self.assertNotIn("GigabitEthernet3/0/2.100", target_ifs) + self.assertIn("GigabitEthernet3/0/2.200", target_ifs) + + ## Since l2xc cannot connect to itself, and the target must exist, + ## it follows that the same number of l2xc target interfaces must exist. + self.assertEqual(len(target_ifs), len(ifs)) + def test_mtu(self): self.assertEqual(interface.get_mtu(self.cfg, "GigabitEthernet1/0/1"), 9216) self.assertEqual(interface.get_mtu(self.cfg, "GigabitEthernet1/0/1.200"), 9000)