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

[consutil] Fix consule CLI and enhance unittest #3360

Merged
merged 2 commits into from
Jun 9, 2024
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
2 changes: 1 addition & 1 deletion consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def init_device_prefix():
@staticmethod
def list_console_ttys():
"""Lists all console tty devices"""
cmd = ["ls", SysInfoProvider.DEVICE_PREFIX + "*"]
cmd = ["bash", "-c", "ls " + SysInfoProvider.DEVICE_PREFIX + "*"]
output, _ = SysInfoProvider.run_command(cmd, abort=False)
ttys = output.split('\n')
ttys = list([dev for dev in ttys if re.match(SysInfoProvider.DEVICE_PREFIX + r"\d+", dev) != None])
Expand Down
Empty file added tests/console_mock/dev/ttyACM1
Empty file.
Empty file added tests/console_mock/dev/ttyUSB0
Empty file.
19 changes: 11 additions & 8 deletions tests/console_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

from click.testing import CliRunner
from utilities_common.db import Db
from consutil.lib import *
from consutil.lib import ConsolePortProvider, ConsolePortInfo, ConsoleSession, SysInfoProvider, DbUtils, \
InvalidConfigurationError, LineBusyError, LineNotFoundError, ConnectionFailedError
lizhijianrd marked this conversation as resolved.
Show resolved Hide resolved
from sonic_py_common import device_info
from jsonpatch import JsonPatchConflict

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
CONSOLE_MOCK_DIR = SCRIPT_DIR + "/console_mock"


class TestConfigConsoleCommands(object):
@classmethod
def setup_class(cls):
Expand Down Expand Up @@ -543,17 +548,15 @@ def test_sys_info_provider_init_device_prefix_plugin(self):
with mock.patch("builtins.open", mock.mock_open(read_data="C0-")):
SysInfoProvider.init_device_prefix()
assert SysInfoProvider.DEVICE_PREFIX == "/dev/C0-"
SysInfoProvider.DEVICE_PREFIX = "/dev/ttyUSB"

@mock.patch('consutil.lib.SysInfoProvider.run_command', mock.MagicMock(return_value=("/dev/ttyUSB0\n/dev/ttyACM1", "")))
def test_sys_info_provider_list_console_ttys(self):
SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB"
SysInfoProvider.DEVICE_PREFIX = CONSOLE_MOCK_DIR + "/dev/ttyUSB"
ttys = SysInfoProvider.list_console_ttys()
print(SysInfoProvider.DEVICE_PREFIX)
assert len(ttys) == 1

@mock.patch('consutil.lib.SysInfoProvider.run_command', mock.MagicMock(return_value=("", "ls: cannot access '/dev/ttyUSB*': No such file or directory")))
def test_sys_info_provider_list_console_ttys_device_not_exists(self):
SysInfoProvider.DEVICE_PREFIX = CONSOLE_MOCK_DIR + "/dev_not_exist/ttyUSB"
ttys = SysInfoProvider.list_console_ttys()
assert len(ttys) == 0

Expand All @@ -563,7 +566,7 @@ def test_sys_info_provider_list_console_ttys_device_not_exists(self):
"""
@mock.patch('consutil.lib.SysInfoProvider.run_command', mock.MagicMock(return_value=all_active_processes_output))
def test_sys_info_provider_list_active_console_processes(self):
SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB"
SysInfoProvider.DEVICE_PREFIX = "/dev/ttyUSB"
procs = SysInfoProvider.list_active_console_processes()
assert len(procs) == 1
assert "0" in procs
Expand All @@ -572,15 +575,15 @@ def test_sys_info_provider_list_active_console_processes(self):
active_process_output = "13751 Wed Mar 6 08:31:35 2019 /usr/bin/sudo picocom -b 9600 -f n /dev/ttyUSB1"
@mock.patch('consutil.lib.SysInfoProvider.run_command', mock.MagicMock(return_value=active_process_output))
def test_sys_info_provider_get_active_console_process_info_exists(self):
SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB"
SysInfoProvider.DEVICE_PREFIX = "/dev/ttyUSB"
proc = SysInfoProvider.get_active_console_process_info("13751")
assert proc is not None
assert proc == ("1", "13751", "Wed Mar 6 08:31:35 2019")

active_process_empty_output = ""
@mock.patch('consutil.lib.SysInfoProvider.run_command', mock.MagicMock(return_value=active_process_empty_output))
def test_sys_info_provider_get_active_console_process_info_nonexists(self):
SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB"
SysInfoProvider.DEVICE_PREFIX = "/dev/ttyUSB"
proc = SysInfoProvider.get_active_console_process_info("2")
assert proc is None

Expand Down
Loading