Skip to content

Commit

Permalink
[consutil][show] Remove root need from show line command (sonic-net#1218
Browse files Browse the repository at this point in the history
)

* Simply remove the check
* Add unit test for consutil show command
* Fix a known typo (found by adding unittest)

Signed-off-by: Jing Kan jika@microsoft.com
  • Loading branch information
Blueve committed Nov 5, 2020
1 parent 99de167 commit 97dec12
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# STATE_DB Keys
STATE_KEY = "state"
PID_KEY = "pid"
START_TIME_KEY = "state_time"
START_TIME_KEY = "start_time"

BUSY_FLAG = "busy"
IDLE_FLAG = "idle"
Expand Down
11 changes: 8 additions & 3 deletions consutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
@click.group()
def consutil():
"""consutil - Command-line utility for interacting with switches via console device"""
if os.geteuid() != 0:
click.echo("Root privileges are required for this operation")
sys.exit(ERR_CMD)
SysInfoProvider.init_device_prefix()

# 'show' subcommand
Expand Down Expand Up @@ -55,6 +52,10 @@ def show(db, brief):
@click.option('--devicename', '-d', is_flag=True, help="clear by name - if flag is set, interpret linenum as device name instead")
def clear(db, target, devicename):
"""Clear preexisting connection to line"""
if os.geteuid() != 0:
click.echo("Root privileges are required for this operation")
sys.exit(ERR_CMD)

# identify the target line
port_provider = ConsolePortProvider(db, configured_only=False)
try:
Expand All @@ -73,6 +74,10 @@ def clear(db, target, devicename):
@click.option('--devicename', '-d', is_flag=True, help="connect by name - if flag is set, interpret linenum as device name instead")
def connect(db, target, devicename):
"""Connect to switch via console device - TARGET is line number or device name of switch"""
if os.geteuid() != 0:
click.echo("Root privileges are required for this operation")
sys.exit(ERR_CMD)

# identify the target line
port_provider = ConsolePortProvider(db, configured_only=False)
try:
Expand Down
32 changes: 32 additions & 0 deletions tests/console_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

import config.main as config
import consutil.main as consutil
import tests.mock_tables.dbconnector

from click.testing import CliRunner
Expand Down Expand Up @@ -463,3 +464,34 @@ def test_sys_info_provider_get_active_console_process_info_nonexists(self):
SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB"
proc = SysInfoProvider.get_active_console_process_info("2")
assert proc is None

class TestConsutilShow(object):
@classmethod
def setup_class(cls):
print("SETUP")

expect_show_output = ''+ \
""" Line Baud PID Start Time Device
------ ------ ----- ------------------------ --------
1 9600 - - switch1
*2 9600 223 Wed Mar 6 08:31:35 2019 switch2
3 9600 - -
"""
@mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None))
def test_show(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", 1, { "remote_device" : "switch1", "baud_rate" : "9600" })
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch2", "baud_rate" : "9600" })
db.cfgdb.set_entry("CONSOLE_PORT", 3, { "baud_rate" : "9600" })

db.db.set(db.db.STATE_DB, "CONSOLE_PORT|2", "state", "busy")
db.db.set(db.db.STATE_DB, "CONSOLE_PORT|2", "pid", "223")
db.db.set(db.db.STATE_DB, "CONSOLE_PORT|2", "start_time", "Wed Mar 6 08:31:35 2019")

# use '--brief' option to avoid access system
result = runner.invoke(consutil.consutil.commands["show"], ['--brief'], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0
assert result.output == TestConsutilShow.expect_show_output

0 comments on commit 97dec12

Please sign in to comment.