Skip to content

Commit

Permalink
Add test for console availability validation (#4488)
Browse files Browse the repository at this point in the history
Description of PR
Summary: This PR adds a console availability test for virtual testbed.

Approach
What is the motivation for this PR?
Testing other changes to SONiC will not accidentally cause the console to fail, so console access would be available when network is in unreachable state.

How did you do it?
Simulating console access on SONiC switch for each neighbor device.

How did you verify/test it?
Run test on virtual testbed with command:

./run_tests.sh -n vms-kvm-t0 -d vlab-01 -c console/test_console_availability.py -f vtestbed.csv -i veos_vtb -u

And get output:

console/test_console_availability.py::test_console_availability[1] PASSED                               [ 25%]
console/test_console_availability.py::test_console_availability[2] PASSED                               [ 50%]
console/test_console_availability.py::test_console_availability[3] PASSED                               [ 75%]
console/test_console_availability.py::test_console_availability[4] PASSED                               [100%]
  • Loading branch information
xjasonlyu authored Oct 25, 2021
1 parent 786e116 commit d972c21
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Binary file added tests/console/socat
Binary file not shown.
69 changes: 69 additions & 0 deletions tests/console/test_console_availability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import getpass
import pexpect
import pytest

from tests.common.helpers.assertions import pytest_assert

pytestmark = [
pytest.mark.topology("any"),
pytest.mark.device_type("vs")
]

@pytest.mark.parametrize("target_line", ["1", "2", "3", "4"])
def test_console_availability(duthost, creds, target_line):
"""
Test console are well functional.
Verify console access is available after connecting from DUT
"""
dutip = duthost.host.options['inventory_manager'].get_host(duthost.hostname).vars['ansible_host']
dutuser, dutpass = creds['sonicadmin_user'], creds['sonicadmin_password']
hostip, hostuser = "172.17.0.1", getpass.getuser()

res = duthost.shell("which socat", module_ignore_errors=True)
if res["rc"] != 0:
# install socat to DUT host
duthost.copy(src="./console/socat", dest="/usr/local/bin/socat", mode=0755)

out = pexpect.run("ssh {}@{} -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null 'which socat'".format(
hostuser, hostip))
if not out:
# install socat to KVM host
pexpect.run("scp -q {} {}@{}:{}".format("./console/socat", hostuser, hostip, "/usr/local/bin/socat"))

pytest_assert(duthost.shell("socat -V", module_ignore_errors=True)["rc"] == 0,
"Invalid socat installation on DUT host")
pytest_assert(int(pexpect.run("ssh {}@{} -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "
"'socat -V > /dev/null 2>&1; echo $?'".format(hostuser, hostip))) == 0,
"Invalid socat installation on KVM host")

out = pexpect.run("ssh {0}@{1} -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "
"'sudo killall -q socat;"
"sudo lsof -i:{3} > /dev/null &&"
"sudo socat TCP-LISTEN:{2},fork,reuseaddr TCP:127.0.0.1:{3} & echo $?'".format(
hostuser, hostip, 2000 + int(target_line), 7000 + int(target_line) - 1))
pytest_assert(int(out.strip()) == 0, "Failed to start socat on KVM host")

try:
client = pexpect.spawn(
"ssh {2}@{3} -q -t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "
"'sudo killall -q socat;"
"sudo killall -q picocom;"
"sudo socat PTY,link=/dev/ttyUSB{0} TCP:10.250.0.1:{1},forever &"
"while [ ! -e /dev/ttyUSB{0} ]; do sleep 1; done;"
"sudo config console del {0} > /dev/null 2>&1;"
"sudo config console add {0} --baud 9600 --devicename device{0};"
"sudo connect line {0}'".format(
target_line, 2000 + int(target_line), dutuser, dutip))
client.expect('[Pp]assword:')
client.sendline(dutpass)

i = client.expect(['Successful connection', 'Cannot connect'], timeout=10)
pytest_assert(i == 0,
"Failed to connect line {}".format(target_line))
client.expect(['login:', '[:>~$]'], timeout=10)
except pexpect.exceptions.EOF:
pytest.fail("EOF reached")
except pexpect.exceptions.TIMEOUT:
pytest.fail("Timeout reached")
except Exception as e:
pytest.fail("Cannot connect to DUT host via SSH: {}".format(e))

0 comments on commit d972c21

Please sign in to comment.