Skip to content

Commit c382d89

Browse files
authored
[config/console] Support update console configuration related commands (#1166)
Signed-off-by: Jing Kan [email protected]
1 parent e51d44f commit c382d89

File tree

2 files changed

+216
-1
lines changed

2 files changed

+216
-1
lines changed

config/console.py

+87-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,92 @@ def remove_console_setting(db, linenum):
6565
ctx = click.get_current_context()
6666
ctx.fail("Trying to delete console port setting, which is not present.")
6767

68+
#
69+
# 'console remote_device' group ('config console remote_device ...')
70+
#
71+
@console.command('remote_device')
72+
@clicommon.pass_db
73+
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
74+
@click.argument('devicename', metavar='<device_name>', required=False)
75+
def upate_console_remote_device_name(db, linenum, devicename):
76+
"""Update remote device name for a console line"""
77+
config_db = db.cfgdb
78+
ctx = click.get_current_context()
79+
80+
table = "CONSOLE_PORT"
81+
dataKey = 'remote_device'
82+
83+
data = config_db.get_entry(table, linenum)
84+
if data:
85+
if dataKey in data and devicename == data[dataKey]:
86+
# do nothing if the device name is same with existing configurtion
87+
return
88+
elif not devicename:
89+
# remove configuration key from console setting if user not give a remote device name
90+
data.pop(dataKey, None)
91+
config_db.mod_entry(table, linenum, data)
92+
elif isExistingSameDevice(config_db, devicename, table):
93+
ctx.fail("Given device name {} has been used. Please enter a valid device name or remove the existing one !!".format(devicename))
94+
else:
95+
data[dataKey] = devicename
96+
config_db.mod_entry(table, linenum, data)
97+
else:
98+
ctx.fail("Trying to update console port setting, which is not present.")
99+
100+
#
101+
# 'console baud' group ('config console baud ...')
102+
#
103+
@console.command('baud')
104+
@clicommon.pass_db
105+
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
106+
@click.argument('baud', metavar='<baud>', required=True, type=click.INT)
107+
def update_console_baud(db, linenum, baud):
108+
"""Update baud for a console line"""
109+
config_db = db.cfgdb
110+
ctx = click.get_current_context()
111+
112+
table = "CONSOLE_PORT"
113+
dataKey = 'baud_rate'
114+
115+
data = config_db.get_entry(table, linenum)
116+
if data:
117+
baud = str(baud)
118+
if dataKey in data and baud == data[dataKey]:
119+
# do nothing if the baud is same with existing configurtion
120+
return
121+
else:
122+
data[dataKey] = baud
123+
config_db.mod_entry(table, linenum, data)
124+
else:
125+
ctx.fail("Trying to update console port setting, which is not present.")
126+
127+
#
128+
# 'console flow_control' group ('config console flow_control ...')
129+
#
130+
@console.command('flow_control')
131+
@clicommon.pass_db
132+
@click.argument('mode', metavar='<mode>', required=True, type=click.Choice(["enable", "disable"]))
133+
@click.argument('linenum', metavar='<line_number>', required=True, type=click.IntRange(0, 65535))
134+
def update_console_flow_control(db, mode, linenum):
135+
"""Update flow control setting for a console line"""
136+
config_db = db.cfgdb
137+
ctx = click.get_current_context()
138+
139+
table = "CONSOLE_PORT"
140+
dataKey = 'flow_control'
141+
142+
innerMode = "1" if mode == "enable" else "0"
143+
144+
data = config_db.get_entry(table, linenum)
145+
if data:
146+
if dataKey in data and innerMode == data[dataKey]:
147+
# do nothing if the flow control setting is same with existing configurtion
148+
return
149+
else:
150+
data[dataKey] = innerMode
151+
config_db.mod_entry(table, linenum, data)
152+
else:
153+
ctx.fail("Trying to update console port setting, which is not present.")
68154

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

76-
return False
162+
return False

tests/console_test.py

+129
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ def test_console_add_success(self):
6464
print(sys.stderr, result.output)
6565
assert result.exit_code == 0
6666

67+
# add a console setting with device name option
68+
result = runner.invoke(config.config.commands["console"].commands["add"], ["2", '--baud', "9600", "--devicename", "switch1"], obj=db)
69+
print(result.exit_code)
70+
print(sys.stderr, result.output)
71+
assert result.exit_code == 0
72+
6773
def test_console_del_non_exists(self):
6874
runner = CliRunner()
6975
db = Db()
@@ -85,3 +91,126 @@ def test_console_del_success(self):
8591
print(result.exit_code)
8692
print(sys.stderr, result.output)
8793
assert result.exit_code == 0
94+
95+
def test_update_console_remote_device_name_non_exists(self):
96+
runner = CliRunner()
97+
db = Db()
98+
99+
# trying to update a console line remote device configuration which is not exists
100+
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["1", "switch1"], obj=db)
101+
print(result.exit_code)
102+
print(sys.stderr, result.output)
103+
assert result.exit_code != 0
104+
assert "Trying to update console port setting, which is not present." in result.output
105+
106+
def test_update_console_remote_device_name_conflict(self):
107+
runner = CliRunner()
108+
db = Db()
109+
db.cfgdb.set_entry("CONSOLE_PORT", 1, { "baud": "9600" })
110+
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "baud": "9600", "remote_device" : "switch1" })
111+
112+
# trying to update a console line remote device configuration which is not exists
113+
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["1", "switch1"], obj=db)
114+
print(result.exit_code)
115+
print(sys.stderr, result.output)
116+
assert result.exit_code != 0
117+
assert "Please enter a valid device name or remove the existing one" in result.output
118+
119+
def test_update_console_remote_device_name_existing_and_same(self):
120+
runner = CliRunner()
121+
db = Db()
122+
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch1" })
123+
124+
# trying to update a console line remote device configuration which is existing and same with user provided value
125+
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["2", "switch1"], obj=db)
126+
print(result.exit_code)
127+
print(sys.stderr, result.output)
128+
assert result.exit_code == 0
129+
130+
def test_update_console_remote_device_name_reset(self):
131+
runner = CliRunner()
132+
db = Db()
133+
db.cfgdb.set_entry("CONSOLE_PORT", 2, { "remote_device" : "switch1" })
134+
135+
# trying to reset a console line remote device configuration which is not exists
136+
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["2"], obj=db)
137+
print(result.exit_code)
138+
print(sys.stderr, result.output)
139+
assert result.exit_code == 0
140+
141+
def test_update_console_remote_device_name_success(self):
142+
runner = CliRunner()
143+
db = Db()
144+
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" })
145+
146+
# trying to set a console line remote device configuration
147+
result = runner.invoke(config.config.commands["console"].commands["remote_device"], ["1", "switch1"], obj=db)
148+
print(result.exit_code)
149+
print(sys.stderr, result.output)
150+
assert result.exit_code == 0
151+
152+
def test_update_console_baud_no_change(self):
153+
runner = CliRunner()
154+
db = Db()
155+
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" })
156+
157+
# trying to set a console line baud which is same with existing one
158+
result = runner.invoke(config.config.commands["console"].commands["baud"], ["1", "9600"], obj=db)
159+
print(result.exit_code)
160+
print(sys.stderr, result.output)
161+
assert result.exit_code == 0
162+
163+
def test_update_console_baud_non_exists(self):
164+
runner = CliRunner()
165+
db = Db()
166+
167+
# trying to set a console line baud which is not exists
168+
result = runner.invoke(config.config.commands["console"].commands["baud"], ["1", "9600"], obj=db)
169+
print(result.exit_code)
170+
print(sys.stderr, result.output)
171+
assert result.exit_code != 0
172+
assert "Trying to update console port setting, which is not present." in result.output
173+
174+
def test_update_console_baud_success(self):
175+
runner = CliRunner()
176+
db = Db()
177+
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600" })
178+
179+
# trying to set a console line baud
180+
result = runner.invoke(config.config.commands["console"].commands["baud"], ["1", "115200"], obj=db)
181+
print(result.exit_code)
182+
print(sys.stderr, result.output)
183+
assert result.exit_code == 0
184+
185+
def test_update_console_flow_control_no_change(self):
186+
runner = CliRunner()
187+
db = Db()
188+
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600", "flow_control" : "0" })
189+
190+
# trying to set a console line flow control option which is same with existing one
191+
result = runner.invoke(config.config.commands["console"].commands["flow_control"], ["disable", "1"], obj=db)
192+
print(result.exit_code)
193+
print(sys.stderr, result.output)
194+
assert result.exit_code == 0
195+
196+
def test_update_console_flow_control_non_exists(self):
197+
runner = CliRunner()
198+
db = Db()
199+
200+
# trying to set a console line flow control option which is not exists
201+
result = runner.invoke(config.config.commands["console"].commands["flow_control"], ["enable", "1"], obj=db)
202+
print(result.exit_code)
203+
print(sys.stderr, result.output)
204+
assert result.exit_code != 0
205+
assert "Trying to update console port setting, which is not present." in result.output
206+
207+
def test_update_console_flow_control_success(self):
208+
runner = CliRunner()
209+
db = Db()
210+
db.cfgdb.set_entry("CONSOLE_PORT", "1", { "baud_rate" : "9600", "flow_control" : "0" })
211+
212+
# trying to set a console line flow control option
213+
result = runner.invoke(config.config.commands["console"].commands["flow_control"], ["enable", "1"], obj=db)
214+
print(result.exit_code)
215+
print(sys.stderr, result.output)
216+
assert result.exit_code == 0

0 commit comments

Comments
 (0)