From 97dec1252ec7f981134aebba45bc6ed704a24034 Mon Sep 17 00:00:00 2001 From: Blueve <672454911@qq.com> Date: Thu, 5 Nov 2020 16:02:42 +0800 Subject: [PATCH] [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 --- consutil/lib.py | 2 +- consutil/main.py | 11 ++++++++--- tests/console_test.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/consutil/lib.py b/consutil/lib.py index 2afdac5f4825..4391b7b5efdf 100644 --- a/consutil/lib.py +++ b/consutil/lib.py @@ -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" diff --git a/consutil/main.py b/consutil/main.py index 04753e00e08a..3b526d9e29fd 100644 --- a/consutil/main.py +++ b/consutil/main.py @@ -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 @@ -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: @@ -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: diff --git a/tests/console_test.py b/tests/console_test.py index 30093ce37f93..518cf66d2b9e 100644 --- a/tests/console_test.py +++ b/tests/console_test.py @@ -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 @@ -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