forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[config/console] Support add/del console port setting commands (sonic…
…-net#1136) * [config/console] Support add/del console port setting commands * Add new console.py file to support config console commands * Add UTs for console.py
- Loading branch information
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python | ||
|
||
import click | ||
|
||
import utilities_common.cli as clicommon | ||
|
||
# | ||
# 'console' group ('config console ...') | ||
# | ||
@click.group('console') | ||
def console(): | ||
"""Console-related configuration tasks""" | ||
pass | ||
|
||
# | ||
# 'console add' group ('config console add ...') | ||
# | ||
@console.command('add') | ||
@clicommon.pass_db | ||
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535)) | ||
@click.option('--baud', '-b', metavar='<baud>', required=True, type=click.INT) | ||
@click.option('--flowcontrol', '-f', metavar='<flow_control>', required=False, is_flag=True) | ||
@click.option('--devicename', '-d', metavar='<device_name>', required=False) | ||
def add_console_setting(db, linenum, baud, flowcontrol, devicename): | ||
"""Add Console-realted configuration tasks""" | ||
config_db = db.cfgdb | ||
|
||
table = "CONSOLE_PORT" | ||
dataKey1 = 'baud_rate' | ||
dataKey2 = 'flow_control' | ||
dataKey3 = 'remote_device' | ||
|
||
ctx = click.get_current_context() | ||
data = config_db.get_entry(table, linenum) | ||
if data: | ||
ctx.fail("Trying to add console port setting, which is already exists.") | ||
else: | ||
console_entry = { dataKey1: baud } | ||
console_entry[dataKey2] = "1" if flowcontrol else "0" | ||
|
||
if devicename: | ||
if 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)) | ||
console_entry[dataKey3] = devicename | ||
|
||
config_db.set_entry(table, linenum, console_entry) | ||
|
||
|
||
# | ||
# 'console del' group ('config console del ...') | ||
# | ||
@console.command('del') | ||
@clicommon.pass_db | ||
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535)) | ||
def remove_console_setting(db, linenum): | ||
"""Remove Console-related configuration tasks""" | ||
config_db = db.cfgdb | ||
|
||
table = "CONSOLE_PORT" | ||
|
||
data = config_db.get_entry(table, linenum) | ||
if data: | ||
config_db.mod_entry(table, linenum, None) | ||
else: | ||
ctx = click.get_current_context() | ||
ctx.fail("Trying to delete console port setting, which is not present.") | ||
|
||
|
||
def isExistingSameDevice(config_db, deviceName, table): | ||
"""Check if the given device name is conflict with existing device""" | ||
settings = config_db.get_table(table) | ||
for key,values in settings.items(): | ||
if "remote_device" in values and deviceName == values["remote_device"]: | ||
return True | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
import sys | ||
import pytest | ||
|
||
import config.main as config | ||
import tests.mock_tables.dbconnector | ||
|
||
from click.testing import CliRunner | ||
from utilities_common.db import Db | ||
|
||
class TestConfigConsoleCommands(object): | ||
@classmethod | ||
def setup_class(cls): | ||
print("SETUP") | ||
|
||
def test_console_add_exists(self): | ||
runner = CliRunner() | ||
db = Db() | ||
db.cfgdb.set_entry("CONSOLE_PORT", 1, { "baud_rate" : "9600" }) | ||
|
||
# add a console setting which the port exists | ||
result = runner.invoke(config.config.commands["console"].commands["add"], ["1", '--baud', "9600"], obj=db) | ||
print(result.exit_code) | ||
print(sys.stderr, result.output) | ||
assert result.exit_code != 0 | ||
assert "Trying to add console port setting, which is already exists." in result.output | ||
|
||
def test_console_add_no_baud(self): | ||
runner = CliRunner() | ||
db = Db() | ||
|
||
# add a console setting without baud | ||
result = runner.invoke(config.config.commands["console"].commands["add"], ["1"], obj=db) | ||
print(result.exit_code) | ||
print(sys.stderr, result.output) | ||
assert result.exit_code != 0 | ||
assert "Missing option '--baud'" in result.output | ||
|
||
def test_console_add_name_conflict(self): | ||
runner = CliRunner() | ||
db = Db() | ||
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch1" }) | ||
|
||
# add a console setting which the device name has been used by other port | ||
result = runner.invoke(config.config.commands["console"].commands["add"], ["1", '--baud', "9600", "--devicename", "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_console_add_success(self): | ||
runner = CliRunner() | ||
db = Db() | ||
|
||
# add a console setting without flow control option | ||
result = runner.invoke(config.config.commands["console"].commands["add"], ["0", '--baud', "9600"], obj=db) | ||
print(result.exit_code) | ||
print(sys.stderr, result.output) | ||
assert result.exit_code == 0 | ||
|
||
# add a console setting with flow control option | ||
result = runner.invoke(config.config.commands["console"].commands["add"], ["1", '--baud', "9600", "--flowcontrol"], 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() | ||
|
||
# remote a console port setting which is not exists | ||
result = runner.invoke(config.config.commands["console"].commands["del"], ["0"], obj=db) | ||
print(result.exit_code) | ||
print(sys.stderr, result.output) | ||
assert result.exit_code != 0 | ||
assert "Trying to delete console port setting, which is not present." in result.output | ||
|
||
def test_console_del_success(self): | ||
runner = CliRunner() | ||
db = Db() | ||
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" }) | ||
|
||
# add a console setting which the port exists | ||
result = runner.invoke(config.config.commands["console"].commands["del"], ["1"], obj=db) | ||
print(result.exit_code) | ||
print(sys.stderr, result.output) | ||
assert result.exit_code == 0 |