Skip to content

Commit

Permalink
Merge pull request #75 from mssonicbld/sonicbld/202305-merge
Browse files Browse the repository at this point in the history
[code sync] Merge code from sonic-net/sonic-utilities.msft:202305 to 202305
  • Loading branch information
mssonicbld authored Jun 12, 2024
2 parents 56ccb65 + caff77f commit 4e4d054
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
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
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

0 comments on commit 4e4d054

Please sign in to comment.