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][show] Remove root need from show line command #1218

Merged
merged 1 commit into from
Nov 5, 2020
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 @@ -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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will raise another PR to remove this because we need a udev rule update in sonic-buildimage before doing that.

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