Skip to content

Commit 97dec12

Browse files
authored
[consutil][show] Remove root need from show line command (#1218)
* 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
1 parent 99de167 commit 97dec12

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

consutil/lib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# STATE_DB Keys
3535
STATE_KEY = "state"
3636
PID_KEY = "pid"
37-
START_TIME_KEY = "state_time"
37+
START_TIME_KEY = "start_time"
3838

3939
BUSY_FLAG = "busy"
4040
IDLE_FLAG = "idle"

consutil/main.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
@click.group()
2020
def consutil():
2121
"""consutil - Command-line utility for interacting with switches via console device"""
22-
if os.geteuid() != 0:
23-
click.echo("Root privileges are required for this operation")
24-
sys.exit(ERR_CMD)
2522
SysInfoProvider.init_device_prefix()
2623

2724
# 'show' subcommand
@@ -55,6 +52,10 @@ def show(db, brief):
5552
@click.option('--devicename', '-d', is_flag=True, help="clear by name - if flag is set, interpret linenum as device name instead")
5653
def clear(db, target, devicename):
5754
"""Clear preexisting connection to line"""
55+
if os.geteuid() != 0:
56+
click.echo("Root privileges are required for this operation")
57+
sys.exit(ERR_CMD)
58+
5859
# identify the target line
5960
port_provider = ConsolePortProvider(db, configured_only=False)
6061
try:
@@ -73,6 +74,10 @@ def clear(db, target, devicename):
7374
@click.option('--devicename', '-d', is_flag=True, help="connect by name - if flag is set, interpret linenum as device name instead")
7475
def connect(db, target, devicename):
7576
"""Connect to switch via console device - TARGET is line number or device name of switch"""
77+
if os.geteuid() != 0:
78+
click.echo("Root privileges are required for this operation")
79+
sys.exit(ERR_CMD)
80+
7681
# identify the target line
7782
port_provider = ConsolePortProvider(db, configured_only=False)
7883
try:

tests/console_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
import config.main as config
10+
import consutil.main as consutil
1011
import tests.mock_tables.dbconnector
1112

1213
from click.testing import CliRunner
@@ -463,3 +464,34 @@ def test_sys_info_provider_get_active_console_process_info_nonexists(self):
463464
SysInfoProvider.DEVICE_PREFIX == "/dev/ttyUSB"
464465
proc = SysInfoProvider.get_active_console_process_info("2")
465466
assert proc is None
467+
468+
class TestConsutilShow(object):
469+
@classmethod
470+
def setup_class(cls):
471+
print("SETUP")
472+
473+
expect_show_output = ''+ \
474+
""" Line Baud PID Start Time Device
475+
------ ------ ----- ------------------------ --------
476+
1 9600 - - switch1
477+
*2 9600 223 Wed Mar 6 08:31:35 2019 switch2
478+
3 9600 - -
479+
"""
480+
@mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None))
481+
def test_show(self):
482+
runner = CliRunner()
483+
db = Db()
484+
db.cfgdb.set_entry("CONSOLE_PORT", 1, { "remote_device" : "switch1", "baud_rate" : "9600" })
485+
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch2", "baud_rate" : "9600" })
486+
db.cfgdb.set_entry("CONSOLE_PORT", 3, { "baud_rate" : "9600" })
487+
488+
db.db.set(db.db.STATE_DB, "CONSOLE_PORT|2", "state", "busy")
489+
db.db.set(db.db.STATE_DB, "CONSOLE_PORT|2", "pid", "223")
490+
db.db.set(db.db.STATE_DB, "CONSOLE_PORT|2", "start_time", "Wed Mar 6 08:31:35 2019")
491+
492+
# use '--brief' option to avoid access system
493+
result = runner.invoke(consutil.consutil.commands["show"], ['--brief'], obj=db)
494+
print(result.exit_code)
495+
print(sys.stderr, result.output)
496+
assert result.exit_code == 0
497+
assert result.output == TestConsutilShow.expect_show_output

0 commit comments

Comments
 (0)