Skip to content

Commit

Permalink
[config/console][consutil] Support enable/disable console switch (#1275)
Browse files Browse the repository at this point in the history
* [config/console][consutil] Support enable/disable console switch
* Changed the key to aligned with feature table style

Signed-off-by: Jing Kan jika@microsoft.com
  • Loading branch information
Blueve authored Dec 9, 2020
1 parent 2f263c4 commit e22980f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
32 changes: 32 additions & 0 deletions config/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ def console():
"""Console-related configuration tasks"""
pass

#
# 'console enable' group ('config console enable')
#
@console.command('enable')
@clicommon.pass_db
def enable_console_switch(db):
"""Enable console switch"""
config_db = db.cfgdb

table = "CONSOLE_SWITCH"
dataKey1 = 'console_mgmt'
dataKey2 = 'enabled'

data = { dataKey2 : "yes" }
config_db.mod_entry(table, dataKey1, data)

#
# 'console disable' group ('config console disable')
#
@console.command('disable')
@clicommon.pass_db
def disable_console_switch(db):
"""Disable console switch"""
config_db = db.cfgdb

table = "CONSOLE_SWITCH"
dataKey1 = 'console_mgmt'
dataKey2 = 'enabled'

data = { dataKey2 : "no" }
config_db.mod_entry(table, dataKey1, data)

#
# 'console add' group ('config console add ...')
#
Expand Down
4 changes: 4 additions & 0 deletions consutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
ERR_BUSY = 5

CONSOLE_PORT_TABLE = "CONSOLE_PORT"
CONSOLE_SWITCH_TABLE = "CONSOLE_SWITCH"

LINE_KEY = "LINE"
CUR_STATE_KEY = "CUR_STATE"

# CONFIG_DB Keys
BAUD_KEY = "baud_rate"
DEVICE_KEY = "remote_device"
FLOW_KEY = "flow_control"
FEATURE_KEY = "console_mgmt"
FEATURE_ENABLED_KEY = "enabled"

# STATE_DB Keys
STATE_KEY = "state"
Expand Down
9 changes: 8 additions & 1 deletion consutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
raise ImportError("%s - required module not found" % str(e))

@click.group()
def consutil():
@clicommon.pass_db
def consutil(db):
"""consutil - Command-line utility for interacting with switches via console device"""
config_db = db.cfgdb
data = config_db.get_entry(CONSOLE_SWITCH_TABLE, FEATURE_KEY)
if FEATURE_ENABLED_KEY not in data or data[FEATURE_ENABLED_KEY] == "no":
click.echo("Console switch feature is disabled")
sys.exit(ERR_DISABLE)

SysInfoProvider.init_device_prefix()

# 'show' subcommand
Expand Down
60 changes: 60 additions & 0 deletions tests/console_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class TestConfigConsoleCommands(object):
def setup_class(cls):
print("SETUP")

def test_enable_console_switch(self):
runner = CliRunner()
db = Db()

result = runner.invoke(config.config.commands["console"].commands["enable"])
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_disable_console_switch(self):
runner = CliRunner()
db = Db()

result = runner.invoke(config.config.commands["console"].commands["disable"])
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_console_add_exists(self):
runner = CliRunner()
db = Db()
Expand Down Expand Up @@ -465,6 +483,48 @@ def test_sys_info_provider_get_active_console_process_info_nonexists(self):
proc = SysInfoProvider.get_active_console_process_info("2")
assert proc is None

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

@mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None))
@mock.patch('consutil.main.show', mock.MagicMock(return_value=None))
def test_consutil_feature_disabled_null_config(self):
runner = CliRunner()
db = Db()

result = runner.invoke(consutil.consutil, ['show'], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 1
assert result.output == "Console switch feature is disabled\n"

@mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None))
@mock.patch('consutil.main.show', mock.MagicMock(return_value=None))
def test_consutil_feature_disabled_config(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_SWITCH", "console_mgmt", { "enabled" : "no" })

result = runner.invoke(consutil.consutil, ['show'], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 1
assert result.output == "Console switch feature is disabled\n"

@mock.patch('consutil.lib.SysInfoProvider.init_device_prefix', mock.MagicMock(return_value=None))
@mock.patch('consutil.main.show', mock.MagicMock(return_value=None))
def test_consutil_feature_enabled(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_SWITCH", "console_mgmt", { "enabled" : "yes" })

result = runner.invoke(consutil.consutil, ['show'], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

class TestConsutilShow(object):
@classmethod
def setup_class(cls):
Expand Down

0 comments on commit e22980f

Please sign in to comment.