Skip to content

Commit e6aacd3

Browse files
authored
Update TRANSCEIVER_INFO table after CDB FW upgrade (#2837)
* Update TRANSCEIVER_INFO table after CDB FW upgrade Signed-off-by: Mihir Patel <[email protected]> * Added testcases to improve code coverage --------- Signed-off-by: Mihir Patel <[email protected]>
1 parent 33d665c commit e6aacd3

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

sfputil/main.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,18 @@ def reset(port_name):
12011201

12021202
i += 1
12031203

1204+
def update_firmware_info_to_state_db(port_name):
1205+
physical_port = logical_port_to_physical_port_index(port_name)
1206+
1207+
namespaces = multi_asic.get_front_end_namespaces()
1208+
for namespace in namespaces:
1209+
state_db = SonicV2Connector(use_unix_socket_path=False, namespace=namespace)
1210+
if state_db is not None:
1211+
state_db.connect(state_db.STATE_DB)
1212+
active_firmware, inactive_firmware = platform_chassis.get_sfp(physical_port).get_transceiver_info_firmware_versions()
1213+
state_db.set(state_db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(port_name), "active_firmware", active_firmware)
1214+
state_db.set(state_db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(port_name), "inactive_firmware", inactive_firmware)
1215+
12041216
# 'firmware' subgroup
12051217
@cli.group()
12061218
def firmware():
@@ -1271,7 +1283,7 @@ def is_fw_switch_done(port_name):
12711283

12721284
if fw_info['status'] == True:
12731285
(ImageA, ImageARunning, ImageACommitted, ImageAInvalid,
1274-
ImageB, ImageBRunning, ImageBCommitted, ImageBInvalid) = fw_info['result']
1286+
ImageB, ImageBRunning, ImageBCommitted, ImageBInvalid, _, _) = fw_info['result']
12751287

12761288
if (ImageARunning == 1) and (ImageAInvalid == 1): # ImageA is running, but also invalid.
12771289
click.echo("FW info error : ImageA shows running, but also shows invalid!")
@@ -1382,6 +1394,7 @@ def download_firmware(port_name, filepath):
13821394
sfp.set_optoe_write_max(1)
13831395

13841396
status = api.cdb_firmware_download_complete()
1397+
update_firmware_info_to_state_db(port_name)
13851398
click.echo('CDB: firmware download complete')
13861399
return status
13871400

@@ -1409,6 +1422,7 @@ def run(port_name, mode):
14091422
click.echo('Failed to run firmware in mode={}! CDB status: {}'.format(mode, status))
14101423
sys.exit(EXIT_FAIL)
14111424

1425+
update_firmware_info_to_state_db(port_name)
14121426
click.echo("Firmware run in mode={} success".format(mode))
14131427

14141428
# 'commit' subcommand
@@ -1430,6 +1444,7 @@ def commit(port_name):
14301444
click.echo('Failed to commit firmware! CDB status: {}'.format(status))
14311445
sys.exit(EXIT_FAIL)
14321446

1447+
update_firmware_info_to_state_db(port_name)
14331448
click.echo("Firmware commit successful")
14341449

14351450
# 'upgrade' subcommand

tests/sfputil_test.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,12 @@ def test_run_firmwre(self, mock_chassis):
710710
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
711711
@pytest.mark.parametrize("mock_response, expected", [
712712
({'status': False, 'result': None} , -1),
713-
({'status': True, 'result': ("1.0.1", 1, 1, 0, "1.0.2", 0, 0, 0)} , -1),
714-
({'status': True, 'result': ("1.0.1", 0, 0, 0, "1.0.2", 1, 1, 0)} , -1),
715-
({'status': True, 'result': ("1.0.1", 1, 0, 0, "1.0.2", 0, 1, 0)} , 1),
716-
({'status': True, 'result': ("1.0.1", 0, 1, 0, "1.0.2", 1, 0, 0)} , 1),
717-
({'status': True, 'result': ("1.0.1", 1, 0, 1, "1.0.2", 0, 1, 0)} , -1),
718-
({'status': True, 'result': ("1.0.1", 0, 1, 0, "1.0.2", 1, 0, 1)} , -1),
713+
({'status': True, 'result': ("1.0.1", 1, 1, 0, "1.0.2", 0, 0, 0, "1.0.1", "1.0.2")} , -1),
714+
({'status': True, 'result': ("1.0.1", 0, 0, 0, "1.0.2", 1, 1, 0, "1.0.2", "1.0.1")} , -1),
715+
({'status': True, 'result': ("1.0.1", 1, 0, 0, "1.0.2", 0, 1, 0, "1.0.1", "1.0.2")} , 1),
716+
({'status': True, 'result': ("1.0.1", 0, 1, 0, "1.0.2", 1, 0, 0, "1.0.2", "1.0.1")} , 1),
717+
({'status': True, 'result': ("1.0.1", 1, 0, 1, "1.0.2", 0, 1, 0, "1.0.1", "1.0.2")} , -1),
718+
({'status': True, 'result': ("1.0.1", 0, 1, 0, "1.0.2", 1, 0, 1, "1.0.2", "1.0.1")} , -1),
719719
720720
# "is_fw_switch_done" function will waiting until timeout under below condition, so that this test will spend around 1min.
721721
({'status': False, 'result': 0} , -1),
@@ -790,3 +790,32 @@ def test_firmware_download_RJ45(self):
790790
result = runner.invoke(sfputil.cli.commands['firmware'].commands['download'], ["Ethernet0", "a.b"])
791791
assert result.output == 'This functionality is not applicable for RJ45 port Ethernet0.\n'
792792
assert result.exit_code == EXIT_FAIL
793+
794+
@patch('sfputil.main.is_sfp_present', MagicMock(return_value=True))
795+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=False))
796+
@patch('sfputil.main.run_firmware', MagicMock(return_value=1))
797+
@patch('sfputil.main.update_firmware_info_to_state_db', MagicMock())
798+
def test_firmware_run_cli(self):
799+
runner = CliRunner()
800+
result = runner.invoke(sfputil.cli.commands['firmware'].commands['run'], ["Ethernet0"])
801+
assert result.exit_code == 0
802+
803+
@patch('sfputil.main.is_sfp_present', MagicMock(return_value=True))
804+
@patch('sfputil.main.is_port_type_rj45', MagicMock(return_value=False))
805+
@patch('sfputil.main.commit_firmware', MagicMock(return_value=1))
806+
@patch('sfputil.main.update_firmware_info_to_state_db', MagicMock())
807+
def test_firmware_commit_cli(self):
808+
runner = CliRunner()
809+
result = runner.invoke(sfputil.cli.commands['firmware'].commands['commit'], ["Ethernet0"])
810+
assert result.exit_code == 0
811+
812+
@patch('sfputil.main.logical_port_to_physical_port_index', MagicMock(return_value=1))
813+
@patch('sonic_py_common.multi_asic.get_front_end_namespaces', MagicMock(return_value=['']))
814+
@patch('sfputil.main.SonicV2Connector', MagicMock())
815+
@patch('sfputil.main.platform_chassis')
816+
def test_update_firmware_info_to_state_db(self, mock_chassis):
817+
mock_sfp = MagicMock()
818+
mock_chassis.get_sfp = MagicMock(return_value=mock_sfp)
819+
mock_sfp.get_transceiver_info_firmware_versions.return_value = ['a.b.c', 'd.e.f']
820+
821+
sfputil.update_firmware_info_to_state_db("Ethernet0")

0 commit comments

Comments
 (0)