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

[PR:14926] Configuring Community SNMP Credentials for SNMP TCs #15359

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion tests/snmp/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,95 @@
import pytest
from tests.common.utilities import wait_until
import shutil
import yaml

from tests.generic_config_updater.gu_utils import create_checkpoint, rollback

SETUP_ENV_CP = "test_setup_checkpoint"


@pytest.fixture(scope="module", autouse=True)
def setup_check_snmp_ready(duthosts):
def setup_check_snmp_ready(duthosts, localhost):
for duthost in duthosts:
assert wait_until(300, 20, 0, duthost.is_service_fully_started,
"snmp"), "SNMP service is not running"

# creating checkpoint before any configuration changes
create_checkpoint(duthost, SETUP_ENV_CP)

snmp_config_path = "/etc/sonic/snmp.yml"

# copy snmp.yml to ucs
output = duthost.shell("sudo find /etc/sonic -name 'snmp.yml'")
filename = output["stdout"].split("\n")

if snmp_config_path in filename:
ret = duthost.fetch(src=snmp_config_path, dest=".")
ret_bin = ret.get("dest", None)
shutil.copyfile(ret_bin, "snmp/snmp.yml")
else:
assert False, f'{snmp_config_path} does not exist'

# configure snmp for every host
full_snmp_comm_list = ['snmp_rocommunity', 'snmp_rocommunities', 'snmp_rwcommunity', 'snmp_rwcommunities']
with open('./snmp/snmp.yml', 'r') as yaml_file:
yaml_snmp_info = yaml.load(yaml_file, Loader=yaml.FullLoader)

# get redis output for SNMP_COMMUNITY & SNMP_LOCATION
snmp_comm_redis_keys = check_redis_output(duthost, 'SNMP_COMMUNITY')
snmp_comm_redis_vals = list(map(extract_redis_keys, snmp_comm_redis_keys))
snmp_location_redis_keys = check_redis_output(duthost, 'SNMP|LOCATION')
snmp_location_redis_vals = list(map(extract_redis_keys, snmp_location_redis_keys))

for comm_type in full_snmp_comm_list:
if comm_type in yaml_snmp_info.keys():
if comm_type.startswith('snmp_rocommunities'):
for community in yaml_snmp_info[comm_type]:
if community not in snmp_comm_redis_vals:
duthost.shell(f"sudo config snmp community add {community} 'ro'") # set snmp cli

elif comm_type.startswith('snmp_rocommunity'):
community = yaml_snmp_info[comm_type]
if community not in snmp_comm_redis_vals:
duthost.shell(f"sudo config snmp community add {community} 'ro'") # set snmp cli

elif comm_type.startswith('snmp_rwcommunities'):
for community in yaml_snmp_info[comm_type]:
if community not in snmp_comm_redis_vals:
duthost.shell(f"sudo config snmp community add {community} 'rw'") # set snmp cli

elif comm_type.startswith('snmp_rwcommunity'):
community = yaml_snmp_info[comm_type]
if community not in snmp_comm_redis_vals:
duthost.shell(f"sudo config snmp community add {community} 'rw'") # set snmp cli

yaml_snmp_location = yaml_snmp_info.get('snmp_location')
if yaml_snmp_location:
if 'LOCATION' not in snmp_location_redis_vals:
duthost.shell(f'sudo config snmp location add {yaml_snmp_location}') # set snmp cli

yield

# rollback configuration
rollback(duthost, SETUP_ENV_CP)

# remove snmp files downloaded
local_command = "find ./snmp/ -type f -name 'snmp.yml' -exec rm -f {} +"
localhost.shell(local_command)


def extract_redis_keys(item):
return item.split('|')[1]


def check_redis_output(duthost, key):
snmp_redis_keys = duthost.shell(f"redis-cli -n 4 keys '{key}*'")
if snmp_redis_keys["stdout"] == "":
return []
else:
snmp_redis_keys = snmp_redis_keys["stdout"].split("\n")
return snmp_redis_keys


@pytest.fixture(scope="module", autouse=True)
def enable_queue_counterpoll_type(duthosts):
Expand Down
Loading