Skip to content

Commit 9d7c287

Browse files
sumukhatvanand-kumar-subramanian
authored andcommitted
Refactor neighbor_advertiser script and use mock for testing (sonic-net#1427)
Refactored code in neighbor_advertiser script and used mock module to unittesting
1 parent 0926b11 commit 9d7c287

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

scripts/neighbor_advertiser

+21-13
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@ def get_vlan_addr_prefix(vlan_intf_name, ip_ver):
226226
return vlan_addr, vlan_prefix
227227

228228

229+
def get_link_local_addr(vlan_interface):
230+
try:
231+
out = subprocess.check_output(['ip', '-6', 'addr', 'show', vlan_interface])
232+
out = out.decode('UTF-8')
233+
for line in out.splitlines():
234+
keys = line.split()
235+
if keys[0] == 'inet6':
236+
ip = IPNetwork(keys[1])
237+
if str(ip.ip).startswith("fe80"):
238+
# Link local ipv6 address
239+
return str(ip.ip)
240+
except Exception:
241+
log.log_error('failed to get %s addresses from o.s.' % vlan_interface)
242+
243+
return None
244+
245+
229246
def get_vlan_addresses(vlan_interface):
230247
vlan_id = get_vlan_interface_vlan_id(vlan_interface)
231248
vxlan_id = get_vlan_interface_vxlan_id(vlan_interface)
@@ -235,19 +252,10 @@ def get_vlan_addresses(vlan_interface):
235252
ipv6_addr, ipv6_prefix = get_vlan_addr_prefix(vlan_interface, 6)
236253

237254
if len(ipv6_addr):
238-
try:
239-
out = subprocess.check_output(['ip', '-6', 'addr', 'show', vlan_interface])
240-
out = out.decode('UTF-8')
241-
for line in out.splitlines():
242-
keys = line.split()
243-
if keys[0] == 'inet6':
244-
ip = IPNetwork(keys[1])
245-
if str(ip.ip).startswith("fe80") and str(ip.ip) not in ipv6_addr:
246-
# Link local ipv6 address
247-
ipv6_addr.append(str(ip.ip))
248-
ipv6_prefix.append('128')
249-
except Exception:
250-
log.log_error('failed to get %s addresses from o.s.' % vlan_interface)
255+
link_local_addr = get_link_local_addr(vlan_interface)
256+
if link_local_addr and link_local_addr not in ipv6_addr:
257+
ipv6_addr.append(link_local_addr)
258+
ipv6_prefix.append('128')
251259

252260
metadata = config_db.get_table('DEVICE_METADATA')
253261
mac_addr = metadata['localhost']['mac']

tests/neighbor_advertiser_test.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
import pytest
4+
from unittest import mock
45
import subprocess
56
from swsscommon.swsscommon import ConfigDBConnector
67

@@ -20,16 +21,7 @@ def set_up(self):
2021
neighbor_advertiser.connect_app_db()
2122

2223
def test_neighbor_advertiser_slice(self, set_up):
23-
cmd = "sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0"
24-
subprocess.check_output(cmd.split())
25-
cmd = "sudo ip link add Vlan1000 type dummy"
26-
subprocess.check_output(cmd.split())
27-
cmd = "sudo ip -6 address add dev Vlan1000 scope link fe80::1e34:daff:fe1e:2800/64"
28-
subprocess.check_output(cmd.split())
29-
cmd = "sudo ip link add Vlan2000 type dummy"
30-
subprocess.check_output(cmd.split())
31-
cmd = "sudo ip -6 address add dev Vlan2000 scope link fe80::1e43:dfaf:fe2e:1800/64"
32-
subprocess.check_output(cmd.split())
24+
neighbor_advertiser.get_link_local_addr = mock.MagicMock(return_value='fe80::1e34:daff:fe1e:2800')
3325
output = neighbor_advertiser.construct_neighbor_advertiser_slice()
3426
expected_output = dict(
3527
{
@@ -53,7 +45,7 @@ def test_neighbor_advertiser_slice(self, set_up):
5345
],
5446
'ipv6AddrMappings': [
5547
{'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fc02:1011::1', 'ipPrefixLen': '64'},
56-
{'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fe80::1e43:dfaf:fe2e:1800', 'ipPrefixLen': '128'}
48+
{'macAddr': '1d:34:db:16:a6:00', 'ipAddr': 'fe80::1e34:daff:fe1e:2800', 'ipPrefixLen': '128'}
5749
],
5850
'vxlanId': '2000',
5951
'vlanId': '2000',

0 commit comments

Comments
 (0)