Skip to content

Commit

Permalink
[config/console] Support add/del console port setting commands (sonic…
Browse files Browse the repository at this point in the history
…-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
Blueve authored Sep 30, 2020
1 parent f692e1e commit adb8941
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
76 changes: 76 additions & 0 deletions config/console.py
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
2 changes: 2 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


import aaa
import console
import feature
import kube
import mlnx
Expand Down Expand Up @@ -876,6 +877,7 @@ def config(ctx):
# Add groups from other modules
config.add_command(aaa.aaa)
config.add_command(aaa.tacacs)
config.add_command(console.console)
config.add_command(feature.feature)
config.add_command(kube.kubernetes)
config.add_command(nat.nat)
Expand Down
87 changes: 87 additions & 0 deletions tests/console_test.py
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

0 comments on commit adb8941

Please sign in to comment.