Skip to content

Commit

Permalink
Refactor neighbor_advertiser script and use mock for testing (sonic-n…
Browse files Browse the repository at this point in the history
…et#1427)

Refactored code in neighbor_advertiser script and used mock module to unittesting
  • Loading branch information
sumukhatv authored Feb 19, 2021
1 parent 6b0ca96 commit 3b04710
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
34 changes: 21 additions & 13 deletions scripts/neighbor_advertiser
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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']
Expand Down
14 changes: 3 additions & 11 deletions tests/neighbor_advertiser_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import os
import pytest
from unittest import mock
import subprocess
from swsscommon.swsscommon import ConfigDBConnector

Expand All @@ -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(
{
Expand All @@ -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',
Expand Down

0 comments on commit 3b04710

Please sign in to comment.