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

[config/console] Support update console configuration related commands #1166

Merged
merged 1 commit into from
Oct 15, 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
88 changes: 87 additions & 1 deletion config/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,92 @@ def remove_console_setting(db, linenum):
ctx = click.get_current_context()
ctx.fail("Trying to delete console port setting, which is not present.")

#
# 'console remote_device' group ('config console remote_device ...')
#
@console.command('remote_device')
@clicommon.pass_db
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
@click.argument('devicename', metavar='<device_name>', required=False)
def upate_console_remote_device_name(db, linenum, devicename):
"""Update remote device name for a console line"""
config_db = db.cfgdb
ctx = click.get_current_context()

table = "CONSOLE_PORT"
dataKey = 'remote_device'

data = config_db.get_entry(table, linenum)
if data:
if dataKey in data and devicename == data[dataKey]:
# do nothing if the device name is same with existing configurtion
return
elif not devicename:
# remove configuration key from console setting if user not give a remote device name
data.pop(dataKey, None)
config_db.mod_entry(table, linenum, data)
elif isExistingSameDevice(config_db, devicename, table):
ctx.fail("Given device name {} has been used. Please enter a valid device name or remove the existing one !!".format(devicename))
else:
data[dataKey] = devicename
config_db.mod_entry(table, linenum, data)
else:
ctx.fail("Trying to update console port setting, which is not present.")

#
# 'console baud' group ('config console baud ...')
#
@console.command('baud')
@clicommon.pass_db
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
@click.argument('baud', metavar='<baud>', required=True, type=click.INT)
def update_console_baud(db, linenum, baud):
"""Update baud for a console line"""
config_db = db.cfgdb
ctx = click.get_current_context()

table = "CONSOLE_PORT"
dataKey = 'baud_rate'

data = config_db.get_entry(table, linenum)
if data:
baud = str(baud)
if dataKey in data and baud == data[dataKey]:
# do nothing if the baud is same with existing configurtion
return
else:
data[dataKey] = baud
config_db.mod_entry(table, linenum, data)
else:
ctx.fail("Trying to update console port setting, which is not present.")

#
# 'console flow_control' group ('config console flow_control ...')
#
@console.command('flow_control')
@clicommon.pass_db
@click.argument('mode', metavar='<mode>', required=True, type=click.Choice(["enable", "disable"]))
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
def update_console_flow_control(db, mode, linenum):
"""Update flow control setting for a console line"""
config_db = db.cfgdb
ctx = click.get_current_context()

table = "CONSOLE_PORT"
dataKey = 'flow_control'

innerMode = "1" if mode == "enable" else "0"

data = config_db.get_entry(table, linenum)
if data:
if dataKey in data and innerMode == data[dataKey]:
# do nothing if the flow control setting is same with existing configurtion
return
else:
data[dataKey] = innerMode
config_db.mod_entry(table, linenum, data)
else:
ctx.fail("Trying to update console port setting, which is not present.")

def isExistingSameDevice(config_db, deviceName, table):
"""Check if the given device name is conflict with existing device"""
Expand All @@ -73,4 +159,4 @@ def isExistingSameDevice(config_db, deviceName, table):
if "remote_device" in values and deviceName == values["remote_device"]:
return True

return False
return False
129 changes: 129 additions & 0 deletions tests/console_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def test_console_add_success(self):
print(sys.stderr, result.output)
assert result.exit_code == 0

# add a console setting with device name option
result = runner.invoke(config.config.commands["console"].commands["add"], ["2", '--baud', "9600", "--devicename", "switch1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_console_del_non_exists(self):
runner = CliRunner()
db = Db()
Expand All @@ -85,3 +91,126 @@ def test_console_del_success(self):
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

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

# trying to update a console line remote device configuration which is not exists
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["1", "switch1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code != 0
assert "Trying to update console port setting, which is not present." in result.output

def test_update_console_remote_device_name_conflict(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", 1, { "baud": "9600" })
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "baud": "9600", "remote_device" : "switch1" })

# trying to update a console line remote device configuration which is not exists
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["1", "switch1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code != 0
assert "Please enter a valid device name or remove the existing one" in result.output

def test_update_console_remote_device_name_existing_and_same(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch1" })

# trying to update a console line remote device configuration which is existing and same with user provided value
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["2", "switch1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_update_console_remote_device_name_reset(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch1" })

# trying to reset a console line remote device configuration which is not exists
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["2"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_update_console_remote_device_name_success(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" })

# trying to set a console line remote device configuration
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["1", "switch1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_update_console_baud_no_change(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" })

# trying to set a console line baud which is same with existing one
result = runner.invoke(config.config.commands["console"].commands["baud"], ["1", "9600"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

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

# trying to set a console line baud which is not exists
result = runner.invoke(config.config.commands["console"].commands["baud"], ["1", "9600"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code != 0
assert "Trying to update console port setting, which is not present." in result.output

def test_update_console_baud_success(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" })

# trying to set a console line baud
result = runner.invoke(config.config.commands["console"].commands["baud"], ["1", "115200"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

def test_update_console_flow_control_no_change(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600", "flow_control" : "0" })

# trying to set a console line flow control option which is same with existing one
result = runner.invoke(config.config.commands["console"].commands["flow_control"], ["disable", "1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0

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

# trying to set a console line flow control option which is not exists
result = runner.invoke(config.config.commands["console"].commands["flow_control"], ["enable", "1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code != 0
assert "Trying to update console port setting, which is not present." in result.output

def test_update_console_flow_control_success(self):
runner = CliRunner()
db = Db()
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600", "flow_control" : "0" })

# trying to set a console line flow control option
result = runner.invoke(config.config.commands["console"].commands["flow_control"], ["enable", "1"], obj=db)
print(result.exit_code)
print(sys.stderr, result.output)
assert result.exit_code == 0