Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use device loopback IP address to send SNMP query from neighboring ceos or vsonic #8802

Merged
merged 13 commits into from
Jul 10, 2023
29 changes: 26 additions & 3 deletions tests/macsec/test_interop_protocol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import logging
import re
import ipaddress

from tests.common.utilities import wait_until
from tests.common.devices.eos import EosHost
Expand Down Expand Up @@ -132,11 +133,33 @@ def test_snmp(self, duthost, ctrl_links, upstream_links, creds, wait_mka_establi
commands=['show snmp community | include name'])
community = re.search(r'Community name: (\S+)',
result['stdout'][0]).groups()[0]
up_link = upstream_links[ctrl_port]
nbr_ip = up_link["local_ipv4_addr"]
else: # vsonic neighbour
community = creds['snmp_rocommunity']
# Use LoopbackIP to query from vsonic neighbor
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the code comment, could you be more specific about ipv4 or ipv6? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

result = nbr["host"].config_facts(host=nbr["host"].hostname,
source="running")[
"ansible_facts"].get(
"LOOPBACK_INTERFACE",
{}).get('Loopback0', {})
for ip in result:
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for

You may use for-else to capture the situation that no ip satisfy the condition. In the else block, you can explicitly fail the test. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if isinstance(ipaddress.ip_address(ip.split('/')[0]),
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to split, you can directly parse ipaddress.ip_network #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

ipaddress.IPv4Address):
nbr_ip = ip.split('/')[0]
nbr["host"].command("sudo iptables -I INPUT 1 -p udp \
--dport 161 -d {} -j ACCEPT".
format(nbr_ip))
break

up_link = upstream_links[ctrl_port]
sysDescr = ".1.3.6.1.2.1.1.1.0"
command = "docker exec snmp snmpwalk -v 2c -c {} {} {}".format(
community, up_link["local_ipv4_addr"], sysDescr)
assert not duthost.command(command)["failed"]
community, nbr_ip, sysDescr)
result = duthost.command(command)

if not isinstance(nbr["host"], EosHost):
nbr["host"].command("sudo iptables -D INPUT -p udp \
--dport 161 -d {} -j \
ACCEPT".format(nbr_ip))

assert not result["failed"]