From 3b04710fb2d766d8214f98af07e48e817ca65275 Mon Sep 17 00:00:00 2001 From: Sumukha Tumkur Vani Date: Thu, 18 Feb 2021 16:09:10 -0800 Subject: [PATCH] Refactor neighbor_advertiser script and use mock for testing (#1427) Refactored code in neighbor_advertiser script and used mock module to unittesting --- scripts/neighbor_advertiser | 34 +++++++++++++++++++------------ tests/neighbor_advertiser_test.py | 14 +++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index 7d38dd435a1c..dc38cf26c34d 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -226,6 +226,23 @@ def get_vlan_addr_prefix(vlan_intf_name, ip_ver): return vlan_addr, vlan_prefix +def get_link_local_addr(vlan_interface): + try: + out = subprocess.check_output(['ip', '-6', 'addr', 'show', vlan_interface]) + out = out.decode('UTF-8') + for line in out.splitlines(): + keys = line.split() + if keys[0] == 'inet6': + ip = IPNetwork(keys[1]) + if str(ip.ip).startswith("fe80"): + # Link local ipv6 address + return str(ip.ip) + except Exception: + log.log_error('failed to get %s addresses from o.s.' % vlan_interface) + + return None + + def get_vlan_addresses(vlan_interface): vlan_id = get_vlan_interface_vlan_id(vlan_interface) vxlan_id = get_vlan_interface_vxlan_id(vlan_interface) @@ -235,19 +252,10 @@ def get_vlan_addresses(vlan_interface): ipv6_addr, ipv6_prefix = get_vlan_addr_prefix(vlan_interface, 6) if len(ipv6_addr): - try: - out = subprocess.check_output(['ip', '-6', 'addr', 'show', vlan_interface]) - out = out.decode('UTF-8') - for line in out.splitlines(): - keys = line.split() - if keys[0] == 'inet6': - ip = IPNetwork(keys[1]) - if str(ip.ip).startswith("fe80") and str(ip.ip) not in ipv6_addr: - # Link local ipv6 address - ipv6_addr.append(str(ip.ip)) - ipv6_prefix.append('128') - except Exception: - log.log_error('failed to get %s addresses from o.s.' % vlan_interface) + link_local_addr = get_link_local_addr(vlan_interface) + if link_local_addr and link_local_addr not in ipv6_addr: + ipv6_addr.append(link_local_addr) + ipv6_prefix.append('128') metadata = config_db.get_table('DEVICE_METADATA') mac_addr = metadata['localhost']['mac'] diff --git a/tests/neighbor_advertiser_test.py b/tests/neighbor_advertiser_test.py index 3ba7b8afd75f..c6dee598af48 100644 --- a/tests/neighbor_advertiser_test.py +++ b/tests/neighbor_advertiser_test.py @@ -1,6 +1,7 @@ import sys import os import pytest +from unittest import mock import subprocess from swsscommon.swsscommon import ConfigDBConnector @@ -20,16 +21,7 @@ def set_up(self): neighbor_advertiser.connect_app_db() def test_neighbor_advertiser_slice(self, set_up): - cmd = "sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0" - subprocess.check_output(cmd.split()) - cmd = "sudo ip link add Vlan1000 type dummy" - subprocess.check_output(cmd.split()) - cmd = "sudo ip -6 address add dev Vlan1000 scope link fe80::1e34:daff:fe1e:2800/64" - subprocess.check_output(cmd.split()) - cmd = "sudo ip link add Vlan2000 type dummy" - subprocess.check_output(cmd.split()) - cmd = "sudo ip -6 address add dev Vlan2000 scope link fe80::1e43:dfaf:fe2e:1800/64" - subprocess.check_output(cmd.split()) + neighbor_advertiser.get_link_local_addr = mock.MagicMock(return_value='fe80::1e34:daff:fe1e:2800') output = neighbor_advertiser.construct_neighbor_advertiser_slice() expected_output = dict( { @@ -53,7 +45,7 @@ def test_neighbor_advertiser_slice(self, set_up): ], 'ipv6AddrMappings': [ {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fc02:1011::1', 'ipPrefixLen': '64'}, - {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fe80::1e43:dfaf:fe2e:1800', 'ipPrefixLen': '128'} + {'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fe80::1e34:daff:fe1e:2800', 'ipPrefixLen': '128'} ], 'vxlanId': '2000', 'vlanId': '2000',