Skip to content

Commit 76d0ada

Browse files
committed
Fix pre-commit check error and address review comments
1 parent 001e482 commit 76d0ada

File tree

2 files changed

+141
-51
lines changed

2 files changed

+141
-51
lines changed

config/chassis_modules.py

+39-20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import click
44
import time
55
import re
6-
from swsscommon.swsscommon import SonicV2Connector
6+
import subprocess
77
import utilities_common.cli as clicommon
88

9+
TIMEOUT_SECS = 10
10+
11+
912
#
1013
# 'chassis_modules' group ('config chassis_modules ...')
1114
#
@@ -19,6 +22,7 @@ def modules():
1922
"""Configure chassis modules"""
2023
pass
2124

25+
2226
def get_config_module_state(db, chassis_module_name):
2327
config_db = db.cfgdb
2428
fvs = config_db.get_entry('CHASSIS_MODULE', chassis_module_name)
@@ -27,22 +31,22 @@ def get_config_module_state(db, chassis_module_name):
2731
else:
2832
return fvs['admin_status']
2933

30-
TIMEOUT_SECS = 10
3134

3235
#
33-
# Name: get_config_module_state_timeout
36+
# Name: check_config_module_state_with_timeout
3437
# return: True: timeout, False: not timeout
3538
#
36-
def get_config_module_state_timeout(ctx, db, chassis_module_name, state):
39+
def check_config_module_state_with_timeout(ctx, db, chassis_module_name, state):
3740
counter = 0
38-
while get_config_module_state(db, chassis_module_name) != state:
41+
while get_config_module_state(db, chassis_module_name) != state:
3942
time.sleep(1)
4043
counter += 1
4144
if counter >= TIMEOUT_SECS:
4245
ctx.fail("get_config_module_state {} timeout".format(chassis_module_name))
4346
return True
4447
return False
4548

49+
4650
def get_asic_list_from_db(chassisdb, chassis_module_name):
4751
asic_list = []
4852
asics_keys_list = chassisdb.keys("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE*")
@@ -53,11 +57,12 @@ def get_asic_list_from_db(chassisdb, chassis_module_name):
5357
asic_list.append(asic_id)
5458
return asic_list
5559

60+
5661
#
5762
# Syntax: fabric_module_set_admin_status <chassis_module_name> <'up'/'down'>
5863
#
59-
def fabric_module_set_admin_status(chassis_module_name, state):
60-
chassisdb = SonicV2Connector(host="127.0.0.1")
64+
def fabric_module_set_admin_status(db, chassis_module_name, state):
65+
chassisdb = db.db
6166
chassisdb.connect("CHASSIS_STATE_DB")
6267
asic_list = get_asic_list_from_db(chassisdb, chassis_module_name)
6368

@@ -66,16 +71,30 @@ def fabric_module_set_admin_status(chassis_module_name, state):
6671

6772
if state == "down":
6873
for asic in asic_list:
69-
click.echo("Stop swss@{} and syncd@{} ...".format(asic, asic))
74+
click.echo("Stop swss@{} and syncd@{} services".format(asic, asic))
7075
clicommon.run_command('sudo systemctl stop swss@{}.service'.format(asic))
71-
# wait for service is down
72-
time.sleep(2)
73-
chassisdb.delete("CHASSIS_STATE_DB","CHASSIS_FABRIC_ASIC_TABLE|asic" + str(asic))
74-
click.echo("Start swss@{} and syncd@{} ...".format(asic, asic))
76+
77+
is_active = subprocess.call(["systemctl", "is-active", "--quiet", "swss@{}.service".format(asic)])
78+
79+
if is_active == 0: # zero active, non-zero, inactive
80+
click.echo("stop swss@{} and syncd@{} services failed".format(asic, asic))
81+
return
82+
83+
click.echo("Delete related CAHSSIS_FABRIC_ASIC_TABLE entries")
84+
85+
for asic in asic_list:
86+
chassisdb.delete("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic" + str(asic))
87+
88+
# Start the services in case of the users just execute issue command "systemctl stop swss@/syncd@"
89+
# without bring down the hardware
90+
for asic in asic_list:
91+
click.echo("Start swss@{} and syncd@{} services".format(asic, asic))
92+
# To address systemd service restart limit by resetting the count
93+
clicommon.run_command('sudo systemctl reset-failed swss@{}.service'.format(asic))
7594
clicommon.run_command('sudo systemctl start swss@{}.service'.format(asic))
76-
else:
95+
elif state == "up":
7796
for asic in asic_list:
78-
click.echo("Start swss@{} and syncd@{} ...".format(asic, asic))
97+
click.echo("Start swss@{} and syncd@{} services".format(asic, asic))
7998
clicommon.run_command('sudo systemctl start swss@{}.service'.format(asic))
8099

81100
#
@@ -94,7 +113,7 @@ def shutdown_chassis_module(db, chassis_module_name):
94113
not chassis_module_name.startswith("FABRIC-CARD"):
95114
ctx.fail("'module_name' has to begin with 'SUPERVISOR', 'LINE-CARD' or 'FABRIC-CARD'")
96115

97-
#To avoid duplicate operation
116+
# To avoid duplicate operation
98117
if get_config_module_state(db, chassis_module_name) == 'down':
99118
click.echo("Module {} is already in down state".format(chassis_module_name))
100119
return
@@ -103,8 +122,8 @@ def shutdown_chassis_module(db, chassis_module_name):
103122
fvs = {'admin_status': 'down'}
104123
config_db.set_entry('CHASSIS_MODULE', chassis_module_name, fvs)
105124
if chassis_module_name.startswith("FABRIC-CARD"):
106-
if not get_config_module_state_timeout(ctx, db, chassis_module_name, 'down'):
107-
fabric_module_set_admin_status(chassis_module_name, 'down')
125+
if not check_config_module_state_with_timeout(ctx, db, chassis_module_name, 'down'):
126+
fabric_module_set_admin_status(db, chassis_module_name, 'down')
108127

109128
#
110129
# 'startup' subcommand ('config chassis_modules startup ...')
@@ -117,13 +136,13 @@ def startup_chassis_module(db, chassis_module_name):
117136
config_db = db.cfgdb
118137
ctx = click.get_current_context()
119138

120-
#To avoid duplicate operation
139+
# To avoid duplicate operation
121140
if get_config_module_state(db, chassis_module_name) == 'up':
122141
click.echo("Module {} is already set to up state".format(chassis_module_name))
123142
return
124143

125144
click.echo("Starting up chassis module {}".format(chassis_module_name))
126145
config_db.set_entry('CHASSIS_MODULE', chassis_module_name, None)
127146
if chassis_module_name.startswith("FABRIC-CARD"):
128-
if not get_config_module_state_timeout(ctx, db, chassis_module_name, 'up'):
129-
fabric_module_set_admin_status(chassis_module_name, 'up')
147+
if not check_config_module_state_with_timeout(ctx, db, chassis_module_name, 'up'):
148+
fabric_module_set_admin_status(db, chassis_module_name, 'up')

tests/chassis_modules_test.py

+102-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import tests.mock_tables.dbconnector
88
from utilities_common.db import Db
99
from .utils import get_result_and_return_code
10+
from unittest import mock
11+
sys.modules['clicommon'] = mock.Mock()
1012

1113
show_linecard0_shutdown_output="""\
1214
LINE-CARD0 line-card 1 Empty down LC1000101
@@ -16,11 +18,11 @@
1618
LINE-CARD0 line-card 1 Empty up LC1000101
1719
"""
1820

19-
show_fabriccard0_shutdown_output="""\
21+
show_fabriccard0_shutdown_output = """\
2022
FABRIC-CARD0 fabric-card 17 Online down FC1000101
2123
"""
2224

23-
show_fabriccard0_startup_output="""\
25+
show_fabriccard0_startup_output = """\
2426
FABRIC-CARD0 fabric-card 17 Online up FC1000101
2527
"""
2628

@@ -122,6 +124,11 @@
122124
Linecard4|Asic2|PortChannel0001 2 22 Linecard4|Asic2|Ethernet29, Linecard4|Asic2|Ethernet30
123125
"""
124126

127+
128+
def mock_run_command_side_effect(*args, **kwargs):
129+
return '', 0
130+
131+
125132
class TestChassisModules(object):
126133
@classmethod
127134
def setup_class(cls):
@@ -196,21 +203,45 @@ def test_config_shutdown_module(self):
196203
#db.get_data("CHASSIS_MODULE", "LINE-CARD0")
197204

198205
def test_config_shutdown_module_fabric(self):
199-
runner = CliRunner()
200-
db = Db()
201-
result = runner.invoke(config.config.commands["chassis"].commands["modules"].commands["shutdown"], ["FABRIC-CARD0"], obj=db)
202-
print(result.exit_code)
203-
print(result.output)
204-
assert result.exit_code != 0
205-
206-
result = runner.invoke(show.cli.commands["chassis"].commands["modules"].commands["status"], ["FABRIC-CARD0"], obj=db)
207-
print(result.exit_code)
208-
print(result.output)
209-
result_lines = result.output.strip('\n').split('\n')
210-
assert result.exit_code == 0
211-
header_lines = 2
212-
result_out = " ".join((result_lines[header_lines]).split())
213-
assert result_out.strip('\n') == show_fabriccard0_shutdown_output.strip('\n')
206+
with mock.patch("utilities_common.cli.run_command",
207+
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
208+
runner = CliRunner()
209+
db = Db()
210+
211+
chassisdb = db.db
212+
chassisdb.connect("CHASSIS_STATE_DB")
213+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic6", "asic_id_in_module", "0")
214+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic6", "asic_pci_address", "nokia-bdb:4:0")
215+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic6", "name", "FABRIC-CARD0")
216+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic7", "asic_id_in_module", "1")
217+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic7", "asic_pci_address", "nokia-bdb:4:1")
218+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic7", "name", "FABRIC-CARD0")
219+
chassisdb.close("CHASSIS_STATE_DB")
220+
221+
result = runner.invoke(config.config.commands["chassis"].commands["modules"].commands["shutdown"],
222+
["FABRIC-CARD0"], obj=db)
223+
print(result.exit_code)
224+
print(result.output)
225+
assert result.exit_code == 0
226+
227+
result = runner.invoke(show.cli.commands["chassis"].commands["modules"].commands["status"],
228+
["FABRIC-CARD0"], obj=db)
229+
print(result.exit_code)
230+
print(result.output)
231+
result_lines = result.output.strip('\n').split('\n')
232+
assert result.exit_code == 0
233+
header_lines = 2
234+
result_out = " ".join((result_lines[header_lines]).split())
235+
assert result_out.strip('\n') == show_fabriccard0_shutdown_output.strip('\n')
236+
237+
fvs = {'admin_status': 'down'}
238+
db.cfgdb.set_entry('CHASSIS_MODULE', "FABRIC-CARD0", fvs)
239+
result = runner.invoke(config.config.commands["chassis"].commands["modules"].commands["shutdown"],
240+
["FABRIC-CARD0"], obj=db)
241+
print(result.exit_code)
242+
print(result.output)
243+
assert result.exit_code == 0
244+
assert mock_run_command.call_count == 6
214245

215246
def test_config_startup_module(self):
216247
runner = CliRunner()
@@ -229,20 +260,60 @@ def test_config_startup_module(self):
229260
assert result_out.strip('\n') == show_linecard0_startup_output.strip('\n')
230261

231262
def test_config_startup_module_fabric(self):
232-
runner = CliRunner()
233-
db = Db()
234-
result = runner.invoke(config.config.commands["chassis"].commands["modules"].commands["startup"], ["FABRIC-CARD0"], obj=db)
235-
print(result.exit_code)
236-
print(result.output)
237-
assert result.exit_code == 0
238-
239-
result = runner.invoke(show.cli.commands["chassis"].commands["modules"].commands["status"], ["FABRIC-CARD0"], obj=db)
240-
print(result.exit_code)
241-
print(result.output)
242-
result_lines = result.output.strip('\n').split('\n')
243-
assert result.exit_code == 0
244-
result_out = " ".join((result_lines[header_lines]).split())
245-
assert result_out.strip('\n') == show_fabriccard0_startup_output.strip('\n')
263+
with mock.patch("utilities_common.cli.run_command",
264+
mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
265+
runner = CliRunner()
266+
db = Db()
267+
268+
chassisdb = db.db
269+
chassisdb.connect("CHASSIS_STATE_DB")
270+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic6", "asic_id_in_module", "0")
271+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic6", "asic_pci_address", "nokia-bdb:4:0")
272+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic6", "name", "FABRIC-CARD0")
273+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic7", "asic_id_in_module", "1")
274+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic7", "asic_pci_address", "nokia-bdb:4:1")
275+
chassisdb.set("CHASSIS_STATE_DB", "CHASSIS_FABRIC_ASIC_TABLE|asic7", "name", "FABRIC-CARD0")
276+
chassisdb.close("CHASSIS_STATE_DB")
277+
278+
# FC is down and doing startup
279+
fvs = {'admin_status': 'down'}
280+
db.cfgdb.set_entry('CHASSIS_MODULE', "FABRIC-CARD0", fvs)
281+
282+
result = runner.invoke(config.config.commands["chassis"].commands["modules"].commands["startup"],
283+
["FABRIC-CARD0"], obj=db)
284+
print(result.exit_code)
285+
print(result.output)
286+
assert result.exit_code == 0
287+
288+
result = runner.invoke(show.cli.commands["chassis"].commands["modules"].commands["status"],
289+
["FABRIC-CARD0"], obj=db)
290+
print(result.exit_code)
291+
print(result.output)
292+
result_lines = result.output.strip('\n').split('\n')
293+
assert result.exit_code == 0
294+
result_out = " ".join((result_lines[header_lines]).split())
295+
assert result_out.strip('\n') == show_fabriccard0_startup_output.strip('\n')
296+
assert mock_run_command.call_count == 2
297+
298+
# FC is up and doing startup
299+
fvs = {'admin_status': 'up'}
300+
db.cfgdb.set_entry('CHASSIS_MODULE', "FABRIC-CARD0", fvs)
301+
302+
result = runner.invoke(config.config.commands["chassis"].commands["modules"].commands["startup"],
303+
["FABRIC-CARD0"], obj=db)
304+
print(result.exit_code)
305+
print(result.output)
306+
assert result.exit_code == 0
307+
308+
result = runner.invoke(show.cli.commands["chassis"].commands["modules"].commands["status"],
309+
["FABRIC-CARD0"], obj=db)
310+
print(result.exit_code)
311+
print(result.output)
312+
result_lines = result.output.strip('\n').split('\n')
313+
assert result.exit_code == 0
314+
result_out = " ".join((result_lines[header_lines]).split())
315+
assert result_out.strip('\n') == show_fabriccard0_startup_output.strip('\n')
316+
assert mock_run_command.call_count == 2
246317

247318
def test_config_incorrect_module(self):
248319
runner = CliRunner()

0 commit comments

Comments
 (0)