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

Generic Config Updater Syslog server test #4725

Merged
merged 7 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions tests/generic_config_updater/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ def check_image_version(duthost):
"""
skip_version(duthost, ["201811", "201911", "202012"])

@pytest.fixture(scope="module")
def cfg_facts(duthosts, rand_one_dut_hostname):
"""
Config facts for selected DUT
Args:
duthosts: list of DUTs.
rand_selected_dut: The fixture returns a randomly selected DuT.
"""
duthost = duthosts[rand_one_dut_hostname]
return duthost.config_facts(host=duthost.hostname, source="persistent")['ansible_facts']
80 changes: 63 additions & 17 deletions tests/generic_config_updater/gu_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@

logger = logging.getLogger(__name__)

CONTAINER_SERVICES_LIST = ["swss", "syncd", "radv", "lldp", "dhcp_relay", "teamd", "bgp", "pmon", "telemetry", "acms"]

def generate_tmpfile(duthost):
"""Generate temp file
"""
return duthost.shell('mktemp')['stdout']

def delete_tmpfile(duthost, tmpfile):
"""Delete temp file
"""
duthost.file(path=tmpfile, state='absent')

def apply_patch(duthost, json_data, dest_file):
"""Run apply-patch on target duthost

Args:
duthost: Device Under Test (DUT)
json_data: Source json patch to apply
dest_file: Destination file on duthost
"""
duthost.copy(content=json.dumps(json_data, indent=4), dest=dest_file)

cmds = 'config apply-patch {}'.format(dest_file)
Expand All @@ -23,20 +36,38 @@ def apply_patch(duthost, json_data, dest_file):
return output

def expect_op_success(duthost, output):
"""Expected success from apply-patch output
"""
pytest_assert(not output['rc'], "Command is not running successfully")
pytest_assert(
"Patch applied successfully" in output['stdout'],
"Please check if json file is validate"
)

def expect_op_success_and_reset_check(duthost, output, container_name, threshold, interval, delay):
'''Add contianer reset check after op success
'''
def expect_op_success_and_reset_check(duthost, output, service_name, timeout, interval, delay):
"""Add contianer reset check after op success

Args:
duthost: Device Under Test (DUT)
output: Command couput
service_name: Service to reset
timeout: Maximum time to wait
interval: Poll interval
delay: Delay time
"""
expect_op_success(duthost, output)
if start_limit_hit(duthost, container_name):
reset_start_limit_hit(duthost, container_name, threshold, interval, delay)
if start_limit_hit(duthost, service_name):
reset_start_limit_hit(duthost, service_name, timeout, interval, delay)

def expect_res_success(duthost, output, expected_content_list, unexpected_content_list):
"""Check output success with expected and unexpected content

Args:
duthost: Device Under Test (DUT)
output: Command output
expected_content_list: Expected content from output
unexpected_content_list: Unexpected content from output
"""
for expected_content in expected_content_list:
pytest_assert(
expected_content in output['stdout'],
Expand All @@ -50,19 +81,24 @@ def expect_res_success(duthost, output, expected_content_list, unexpected_conten
)

def expect_op_failure(output):
"""Expected failure from apply-patch output
"""
logger.info("return code {}".format(output['rc']))
pytest_assert(
output['rc'],
"The command should fail with non zero return code"
)

def start_limit_hit(duthost, container_name):
def start_limit_hit(duthost, service_name):
"""If start-limit-hit is hit, the service will not start anyway.

Args:
service_name: Service to reset
"""
service_status = duthost.shell("sudo systemctl status {}.service | grep 'Active'".format(container_name))
service_status = duthost.shell("sudo systemctl status {}.service | grep 'Active'".format(service_name))
pytest_assert(
not service_status['rc'],
"{} service status cannot be found".format(container_name)
"{} service status cannot be found".format(service_name)
)

for line in service_status["stdout_lines"]:
Expand All @@ -71,29 +107,39 @@ def start_limit_hit(duthost, container_name):

return False

def reset_start_limit_hit(duthost, container_name, threshold, interval, delay):
"""Reset container if hit start-limit-hit
def reset_start_limit_hit(duthost, service_name, timeout, interval, delay):
"""Reset service if hit start-limit-hit

Args:
duthost: Device Under Test (DUT)
service_name: Service to reset
timeout: Maximum time to wait
interval: Poll interval
delay: Delay time
"""
logger.info("Reset container '{}' due to start-limit-hit".format(container_name))
logger.info("Reset service '{}' due to start-limit-hit".format(service_name))

service_reset_failed = duthost.shell("sudo systemctl reset-failed {}.service".format(container_name))
service_reset_failed = duthost.shell("sudo systemctl reset-failed {}.service".format(service_name))
pytest_assert(
not service_reset_failed['rc'],
"{} systemctl reset-failed service fails"
)

service_start = duthost.shell("sudo systemctl start {}.service".format(container_name))
service_start = duthost.shell("sudo systemctl start {}.service".format(service_name))
pytest_assert(
not service_start['rc'],
"{} systemctl start service fails"
)

reset_container = wait_until(threshold,
if not service_name in CONTAINER_SERVICES_LIST:
return

reset_service = wait_until(timeout,
interval,
delay,
duthost.is_service_fully_started,
container_name)
service_name)
pytest_assert(
reset_container,
"Failed to reset container '{}' due to start-limit-hit".format(container_name)
reset_service,
"Failed to reset service '{}' due to start-limit-hit".format(service_name)
)
45 changes: 20 additions & 25 deletions tests/generic_config_updater/test_dhcp_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@

logger = logging.getLogger(__name__)

DHCP_RELAY_THRESHOLD=120
DHCP_RELAY_INTERVAL=10

@pytest.fixture(scope="module")
def cfg_facts(duthosts, rand_one_dut_hostname):
duthost = duthosts[rand_one_dut_hostname]
return duthost.config_facts(host=duthost.hostname, source="persistent")['ansible_facts']
DHCP_RELAY_TIMEOUT = 120
DHCP_RELAY_INTERVAL = 10

@pytest.fixture(scope="module")
def vlan_intfs_dict(utils_vlan_intfs_dict_orig):
''' Add two new vlan for test
""" Add two new vlan for test

If added vlan_id is 108 and 109, it will add a dict as below
{108: {'ip': u'192.168.8.1/24', 'orig': False}, 109: {'ip': u'192.168.9.1/24', 'orig': False}}
'''
"""
logger.info("vlan_intrfs_dict ORIG {}".format(utils_vlan_intfs_dict_orig))
vlan_intfs_dict = utils_vlan_intfs_dict_add(utils_vlan_intfs_dict_orig, 2)
logger.info("vlan_intrfs_dict FINAL {}".format(vlan_intfs_dict))
Expand Down Expand Up @@ -56,7 +51,7 @@ def ensure_dhcp_relay_running(duthost):
)

def create_test_vlans(duthost, cfg_facts, vlan_intfs_dict, first_avai_vlan_port):
'''Generate two vlan config for testing
"""Generate two vlan config for testing

This function should generate two VLAN detail shown below
+-----------+------------------+-----------+----------------+-------------+-----------------------+
Expand All @@ -66,7 +61,7 @@ def create_test_vlans(duthost, cfg_facts, vlan_intfs_dict, first_avai_vlan_port)
+-----------+------------------+-----------+----------------+-------------+-----------------------+
| 109 | 192.168.9.1/24 | Ethernet4 | tagged | disabled | |
+-----------+------------------+-----------+----------------+-------------+-----------------------+
'''
"""

logger.info("CREATE TEST VLANS START")
vlan_ports_list = [{
Expand All @@ -83,7 +78,7 @@ def clean_setup():
pass

def default_setup(duthost, vlan_intfs_list):
'''Generate 4 dhcp server for each vlan
"""Generate 4 dhcp server for each vlan

This VLAN detail shows below
+-----------+------------------+-----------+----------------+-------------+-----------------------+
Expand All @@ -99,7 +94,7 @@ def default_setup(duthost, vlan_intfs_list):
| | | | | | 192.0.109.3 |
| | | | | | 192.0.109.4 |
+-----------+------------------+-----------+----------------+-------------+-----------------------+
'''
"""
cmds = []
expected_content_dict = {}
logger.info("default_setup is initiated")
Expand Down Expand Up @@ -151,8 +146,8 @@ def setup_vlan(duthosts, rand_one_dut_hostname, vlan_intfs_dict, first_avai_vlan
tearDown(duthost, vlan_intfs_dict, first_avai_vlan_port)

def tearDown(duthost, vlan_intfs_dict, first_avai_vlan_port):
'''Clean up VLAN CONFIG for this test
'''
"""Clean up VLAN CONFIG for this test
"""
logger.info("VLAN test ending ...")
config_reload(duthost)

Expand All @@ -161,14 +156,14 @@ def vlan_intfs_list(vlan_intfs_dict):
return [ key for key, value in vlan_intfs_dict.items() if not value['orig'] ]

def ensure_dhcp_server_up(duthost):
'''Wait till dhcp-relay server is setup
"""Wait till dhcp-relay server is setup

Sample output
admin@vlab-01:~$ docker exec dhcp_relay supervisorctl status | grep ^dhcp-relay
dhcp-relay:isc-dhcpv4-relay-Vlan100 RUNNING pid 72, uptime 0:00:09
dhcp-relay:isc-dhcpv4-relay-Vlan1000 RUNNING pid 73, uptime 0:00:09

'''
"""
def _dhcp_server_up():
cmds = 'docker exec dhcp_relay supervisorctl status | grep ^dhcp-relay'
output = duthost.shell(cmds)
Expand All @@ -180,18 +175,18 @@ def _dhcp_server_up():
return 'RUNNING' in output['stdout']

pytest_assert(
wait_until(DHCP_RELAY_THRESHOLD, DHCP_RELAY_INTERVAL, 0, _dhcp_server_up),
wait_until(DHCP_RELAY_TIMEOUT, DHCP_RELAY_INTERVAL, 0, _dhcp_server_up),
"The dhcp relay server is not running"
)

def dhcp_severs_by_vlanid(duthost, vlanid):
'''Get pid and then only output the related dhcp server info for that pid
"""Get pid and then only output the related dhcp server info for that pid

Sample output
admin@vlab-01:~$ docker exec dhcp_relay ps -fp 73
UID PID PPID C STIME TTY TIME CMD
root 73 1 0 06:39 pts/0 00:00:00 /usr/sbin/dhcrelay -d -m discard -a %h:%p %P --name-alias-map-file /tmp/port-name-alias-map.txt -id Vlan1000 -iu Vlan100 -iu PortChannel0001 -iu PortChannel0002 -iu PortChannel0003 -iu PortChannel0004 192.0.0.1 192.0.0.2 192.0.0.3 192.0.0.4
'''
"""
cmds = "docker exec dhcp_relay supervisorctl status | grep ^dhcp-relay \
| grep 'Vlan{} ' | awk '{{print $4}}'".format(vlanid)
output = duthost.shell(cmds)
Expand Down Expand Up @@ -229,7 +224,7 @@ def test_dhcp_relay_tc1_apply_empty(duthost, setup_vlan, init_dhcp_server_config
logger.info("tmpfile {}".format(tmpfile))

output = apply_patch(duthost, json_data=dhcp_apply_empty_json, dest_file=tmpfile)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_THRESHOLD, DHCP_RELAY_INTERVAL, 0)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_TIMEOUT, DHCP_RELAY_INTERVAL, 0)
pytest_assert(
duthost.is_service_fully_started('dhcp_relay'),
"dhcp_relay service is not running"
Expand Down Expand Up @@ -329,7 +324,7 @@ def test_dhcp_relay_tc5_rm(duthost, setup_vlan, init_dhcp_server_config, vlan_in
logger.info("tmpfile {}".format(tmpfile))

output = apply_patch(duthost, json_data=dhcp_rm_json, dest_file=tmpfile)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_THRESHOLD, DHCP_RELAY_INTERVAL, 0)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_TIMEOUT, DHCP_RELAY_INTERVAL, 0)
pytest_assert(
duthost.is_service_fully_started('dhcp_relay'),
"dhcp_relay service is not running"
Expand Down Expand Up @@ -372,7 +367,7 @@ def test_dhcp_relay_tc6_add(duthost, setup_vlan, init_dhcp_server_config, vlan_i
logger.info("tmpfile {}".format(tmpfile))

output = apply_patch(duthost, json_data=dhcp_add_json, dest_file=tmpfile)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_THRESHOLD, DHCP_RELAY_INTERVAL, 0)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_TIMEOUT, DHCP_RELAY_INTERVAL, 0)
pytest_assert(
duthost.is_service_fully_started('dhcp_relay'),
"dhcp_relay service is not running"
Expand Down Expand Up @@ -419,7 +414,7 @@ def test_dhcp_relay_tc7_add_rm(duthost, setup_vlan, init_dhcp_server_config, vla
logger.info("tmpfile {}".format(tmpfile))

output = apply_patch(duthost, json_data=dhcp_add_rm_json, dest_file=tmpfile)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_THRESHOLD, DHCP_RELAY_INTERVAL, 0)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_TIMEOUT, DHCP_RELAY_INTERVAL, 0)
pytest_assert(
duthost.is_service_fully_started('dhcp_relay'),
"dhcp_relay service is not running"
Expand Down Expand Up @@ -464,7 +459,7 @@ def test_dhcp_relay_tc7_replace(duthost, setup_vlan, init_dhcp_server_config, vl
logger.info("tmpfile {}".format(tmpfile))

output = apply_patch(duthost, json_data=dhcp_replace_json, dest_file=tmpfile)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_THRESHOLD, DHCP_RELAY_INTERVAL, 0)
expect_op_success_and_reset_check(duthost, output, 'dhcp_relay', DHCP_RELAY_TIMEOUT, DHCP_RELAY_INTERVAL, 0)
pytest_assert(
duthost.is_service_fully_started('dhcp_relay'),
"dhcp_relay service is not running"
Expand Down
Loading