From cbea4551ecb0664f322a74bd74ca7a58c84417e6 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Wed, 25 Jan 2023 20:42:07 +0000 Subject: [PATCH 01/16] [show] add support for gRPC show commands for `active-active` Signed-off-by: vaibhav-dahiya --- show/muxcable.py | 152 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/show/muxcable.py b/show/muxcable.py index d9f0a94f15..b412b6566f 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -2003,3 +2003,155 @@ def tunnel_route(db, port, json_output): click.echo(tabulate(print_data, headers=headers)) sys.exit(STATUS_SUCCESSFUL) + + +def get_grpc_cached_version_mux_direction_per_port(db, port): + + + state_db = {} + mux_info_dict = {} + mux_info_full_dict = {} + trans_info_full_dict = {} + mux_info_dict["rc"] = False + + # Getting all front asic namespace and correspding config and state DB connector + + namespaces = multi_asic.get_front_end_namespaces() + for namespace in namespaces: + asic_id = multi_asic.get_asic_index_from_namespace(namespace) + state_db[asic_id] = swsscommon.SonicV2Connector(use_unix_socket_path=False, namespace=namespace) + state_db[asic_id].connect(state_db[asic_id].STATE_DB) + + if platform_sfputil is not None: + asic_index = platform_sfputil_helper.get_asic_id_for_logical_port(port) + + if asic_index is None: + # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base + # is fully mocked + import sonic_platform_base.sonic_sfp.sfputilhelper + asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port) + if asic_index is None: + click.echo("Got invalid asic index for port {}, cant retrieve mux cable table entries".format(port)) + return mux_info_dict + + + mux_info_full_dict[asic_index] = state_db[asic_index].get_all( + state_db[asic_index].STATE_DB, 'MUX_CABLE_INFO|{}'.format(port)) + trans_info_full_dict[asic_index] = state_db[asic_index].get_all( + state_db[asic_index].STATE_DB, 'TRANSCEIVER_STATUS|{}'.format(port)) + + res_dir = {} + res_dir = mux_info_full_dict[asic_index] + mux_info_dict["self_mux_direction"] = res_dir.get("self_mux_direction", None) + mux_info_dict["peer_mux_direction"] = res_dir.get("peer_mux_direction", None) + mux_info_dict["grpc_connection_status"] = res_dir.get("grpc_connection_status", None) + + trans_dir = {} + trans_dir = trans_info_full_dict[asic_index] + + status = trans_dir.get("status", "0") + presence = "True" if status == "1" else "False" + + mux_info_dict["presence"] = presence + + mux_info_dict["rc"] = True + + return mux_info_dict + + +@muxcable.group(cls=clicommon.AbbreviationGroup) +def grpc(): + """Shows the muxcable hardware information directly""" + pass + + +@grpc.command() +@click.argument('port', metavar='', required=False, default=None) +@clicommon.pass_db +def muxdirection(db, port): + """Shows the current direction of the FPGA facing port on Tx Side {active/standy}""" + + port = platform_sfputil_helper.get_interface_name(port, db) + + + if port is not None: + + if check_port_in_mux_cable_table(port) == False: + click.echo("Not Y-cable port") + return CONFIG_FAIL + + res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) + + body = [] + temp_list = [] + headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + port = platform_sfputil_helper.get_interface_alias(port, db) + temp_list.append(port) + temp_list.append(res_dict["self_mux_direction"]) + temp_list.append(res_dict["peer_mux_direction"]) + temp_list.append(res_dict["presence"]) + temp_list.append(res_dict["grpc_connection_status"]) + body.append(temp_list) + + rc = res_dict["rc"] + click.echo(tabulate(body, headers=headers)) + + return rc + + else: + + + logical_port_list = platform_sfputil_helper.get_logical_list() + + rc_exit = True + body = [] + + for port in natsorted(logical_port_list): + + if platform_sfputil is not None: + physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(port) + + if not isinstance(physical_port_list, list): + continue + if len(physical_port_list) != 1: + continue + + if not check_port_in_mux_cable_table(port): + continue + + physical_port = physical_port_list[0] + logical_port_list_for_physical_port = platform_sfputil_helper.get_physical_to_logical() + + logical_port_list_per_port = logical_port_list_for_physical_port.get(physical_port, None) + + """ This check is required for checking whether or not this logical port is the one which is + actually mapped to physical port and by convention it is always the first port. + TODO: this should be removed with more logic to check which logical port maps to actual physical port + being used""" + + if port != logical_port_list_per_port[0]: + continue + + temp_list = [] + + res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) + + port = platform_sfputil_helper.get_interface_alias(port, db) + temp_list.append(port) + temp_list.append(res_dict["self_mux_direction"]) + temp_list.append(res_dict["peer_mux_direction"]) + temp_list.append(res_dict["presence"]) + temp_list.append(res_dict["grpc_connection_status"]) + body.append(temp_list) + rc = res_dict["rc"] + if rc != True: + rc_exit = False + + headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + + click.echo(tabulate(body, headers=headers)) + + if rc_exit == False: + sys.exit(EXIT_FAIL) + + From cd8d59d414cf6c394f485b40b1d467a65f5b09b0 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 00:47:13 +0000 Subject: [PATCH 02/16] add commits Signed-off-by: vaibhav-dahiya --- show/muxcable.py | 136 +++++++++++++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 53 deletions(-) diff --git a/show/muxcable.py b/show/muxcable.py index b412b6566f..b4f242a84e 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -36,6 +36,21 @@ VENDOR_MODEL_REGEX = re.compile(r"CAC\w{3}321P2P\w{2}MS") +def get_asic_index_for_port(port): + asic_index = None + if platform_sfputil is not None: + asic_index = platform_sfputil_helper.get_asic_id_for_logical_port(port) + if asic_index is None: + # TODO this import is only for unit test purposes, and should be removed once sonic_platform_base + # is fully mocked + import sonic_platform_base.sonic_sfp.sfputilhelper + asic_index = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper().get_asic_id_for_logical_port(port) + if asic_index is None: + port_name = platform_sfputil_helper.get_interface_alias(port, db) + click.echo("Got invalid asic index for port {}, cant retreive mux status".format(port_name)) + return 0 + return asic_index + def db_connect(db_name, namespace=EMPTY_NAMESPACE): return swsscommon.DBConnector(db_name, REDIS_TIMEOUT_MSECS, True, namespace) @@ -1238,6 +1253,40 @@ def get_hwmode_mux_direction_port(db, port): return res_dict +def create_active_active_mux_direction_result(body, port, db): + + res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) + temp_list = [] + port = platform_sfputil_helper.get_interface_alias(port, db) + temp_list.append(port) + temp_list.append(res_dict["self_mux_direction"]) + temp_list.append(res_dict["peer_mux_direction"]) + temp_list.append(res_dict["presence"]) + temp_list.append(res_dict["grpc_connection_status"]) + body.append(temp_list) + + rc = res_dict["rc"] + + return rc + +def create_active_standby_mux_direction_result(body, port, db): + + res_dict = get_hwmode_mux_direction_port(db, port) + + temp_list = [] + port = platform_sfputil_helper.get_interface_alias(port, db) + temp_list.append(port) + temp_list.append(res_dict[1]) + temp_list.append(res_dict[2]) + body.append(temp_list) + + rc = res_dict[0] + + delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_HWMODE_DIR_CMD") + delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RSP") + delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RES") + + return rc @muxcable.group(cls=clicommon.AbbreviationGroup) def hwmode(): @@ -1256,31 +1305,33 @@ def muxdirection(db, port): delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_HWMODE_DIR_CMD") delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RSP") delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RES") + per_npu_configdb = {} - if port is not None: + namespaces = multi_asic.get_front_end_namespaces() + for namespace in namespaces: + asic_id = multi_asic.get_asic_index_from_namespace(namespace) + + per_npu_configdb[asic_id] = ConfigDBConnector(use_unix_socket_path=False, namespace=namespace) + per_npu_configdb[asic_id].connect() + if port is not None: + + asic_index = get_asic_index_for_port(port) + cable_type = get_optional_value_for_key_in_config_tbl(per_npu_configdb[asic_index], port, "cable_type", "MUX_CABLE") if check_port_in_mux_cable_table(port) == False: click.echo("Not Y-cable port") return CONFIG_FAIL - res_dict = get_hwmode_mux_direction_port(db, port) - body = [] - temp_list = [] - headers = ['Port', 'Direction', 'Presence'] - port = platform_sfputil_helper.get_interface_alias(port, db) - temp_list.append(port) - temp_list.append(res_dict[1]) - temp_list.append(res_dict[2]) - body.append(temp_list) - - rc = res_dict[0] + click.echo("cable type= {} ".format(cable_type)) + if cable_type == "active-active": + headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + rc = create_active_active_mux_direction_result(body, port, db) + else: + rc = create_active_standby_mux_direction_result(body, port, db) + headers = ['Port', 'Direction', 'Presence'] click.echo(tabulate(body, headers=headers)) - delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_HWMODE_DIR_CMD") - delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RSP") - delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RES") - return rc else: @@ -1290,7 +1341,7 @@ def muxdirection(db, port): rc_exit = True body = [] - for port in logical_port_list: + for port in natsorted(logical_port_list): if platform_sfputil is not None: physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(port) @@ -1316,26 +1367,24 @@ def muxdirection(db, port): if port != logical_port_list_per_port[0]: continue - temp_list = [] - - res_dict = get_hwmode_mux_direction_port(db, port) - port = platform_sfputil_helper.get_interface_alias(port, db) - temp_list.append(port) - temp_list.append(res_dict[1]) - temp_list.append(res_dict[2]) - body.append(temp_list) - rc = res_dict[0] + asic_index = get_asic_index_for_port(port) + cable_type = get_optional_value_for_key_in_config_tbl(per_npu_configdb[asic_index], port, "cable_type", "MUX_CABLE") + if cable_type == 'active-active': + rc = create_active_active_mux_direction_result(body, port, db) + active = True + else: + rc = create_active_standby_mux_direction_result(body, port, db) + headers = ['Port', 'Direction', 'Presence'] if rc != 0: rc_exit = False - headers = ['Port', 'Direction', 'Presence'] - + if active: + headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + else: + headers = ['Port', 'Direction', 'Presence'] click.echo(tabulate(body, headers=headers)) - delete_all_keys_in_db_table("APPL_DB", "XCVRD_SHOW_HWMODE_DIR_CMD") - delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RSP") - delete_all_keys_in_db_table("STATE_DB", "XCVRD_SHOW_HWMODE_DIR_RES") if rc_exit == False: sys.exit(EXIT_FAIL) @@ -2080,20 +2129,11 @@ def muxdirection(db, port): click.echo("Not Y-cable port") return CONFIG_FAIL - res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) body = [] - temp_list = [] - headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] - port = platform_sfputil_helper.get_interface_alias(port, db) - temp_list.append(port) - temp_list.append(res_dict["self_mux_direction"]) - temp_list.append(res_dict["peer_mux_direction"]) - temp_list.append(res_dict["presence"]) - temp_list.append(res_dict["grpc_connection_status"]) - body.append(temp_list) - rc = res_dict["rc"] + headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + rc = create_active_active_mux_direction_result(body, port, db) click.echo(tabulate(body, headers=headers)) return rc @@ -2132,18 +2172,8 @@ def muxdirection(db, port): if port != logical_port_list_per_port[0]: continue - temp_list = [] - - res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) + rc = create_active_active_mux_direction_result(body, port, db) - port = platform_sfputil_helper.get_interface_alias(port, db) - temp_list.append(port) - temp_list.append(res_dict["self_mux_direction"]) - temp_list.append(res_dict["peer_mux_direction"]) - temp_list.append(res_dict["presence"]) - temp_list.append(res_dict["grpc_connection_status"]) - body.append(temp_list) - rc = res_dict["rc"] if rc != True: rc_exit = False From 94973927168465cbbc49b3a7fe09e47891d2fcbf Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 02:08:22 +0000 Subject: [PATCH 03/16] fix Signed-off-by: vaibhav-dahiya --- show/muxcable.py | 1 - 1 file changed, 1 deletion(-) diff --git a/show/muxcable.py b/show/muxcable.py index b4f242a84e..84eec06bdc 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -1323,7 +1323,6 @@ def muxdirection(db, port): return CONFIG_FAIL body = [] - click.echo("cable type= {} ".format(cable_type)) if cable_type == "active-active": headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] rc = create_active_active_mux_direction_result(body, port, db) From 60a03dcaa3d1041f856098c4c11e6a74666fb20c Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 02:22:18 +0000 Subject: [PATCH 04/16] fix tests Signed-off-by: vaibhav-dahiya --- show/muxcable.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/show/muxcable.py b/show/muxcable.py index 84eec06bdc..5b24ca6a47 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -1339,6 +1339,7 @@ def muxdirection(db, port): rc_exit = True body = [] + active_active = False for port in natsorted(logical_port_list): @@ -1366,19 +1367,19 @@ def muxdirection(db, port): if port != logical_port_list_per_port[0]: continue - + asic_index = get_asic_index_for_port(port) cable_type = get_optional_value_for_key_in_config_tbl(per_npu_configdb[asic_index], port, "cable_type", "MUX_CABLE") if cable_type == 'active-active': rc = create_active_active_mux_direction_result(body, port, db) - active = True + active_active = True else: rc = create_active_standby_mux_direction_result(body, port, db) headers = ['Port', 'Direction', 'Presence'] if rc != 0: rc_exit = False - if active: + if active_active: headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] else: headers = ['Port', 'Direction', 'Presence'] From f832df71fb7b096e9e2aa1b681300d8d79606cdb Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 07:54:45 +0000 Subject: [PATCH 05/16] add a test Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 5a84a65484..2802347cf2 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -378,6 +378,13 @@ } """ +show_muxcable_grpc_muxdirection_active_expected_output = """\ +Port Direction PeerDirection Presence ConnectivityState +---------- ----------- --------------- ---------- ------------------- +Ethernet12 active active True READY +""" + + expected_muxcable_cableinfo_output = """\ Vendor Model -------- --------------- @@ -2358,6 +2365,32 @@ def test_config_muxcable_telemetry_enable(self): "enable"], obj=db) assert result.exit_code == 0 + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "standby"})) + @mock.patch('show.muxcable.get_grpc_cached_version_mux_direction_per_port', mock.MagicMock(return_value={"self_mux_direction": "active", + "peer_mux_direction": "active", + "presence": "True", + "rc": 0, + "grpc_connection_status": "READY"})) + @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet12"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_hwmode_muxdirection_port_standby(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], + ["Ethernet12"], obj=db) + assert result.exit_code == 0 + assert result.output == show_muxcable_grpc_muxdirection_active_expected_output + @classmethod def teardown_class(cls): os.environ['UTILITIES_UNIT_TESTING'] = "0" From 6d4a78401cad88932af2571f9928ef8b6b6eff7a Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 08:59:33 +0000 Subject: [PATCH 06/16] add coverage Signed-off-by: vaibhav-dahiya --- tests/mock_tables/state_db.json | 18 ++++++++++++++++++ tests/muxcable_test.py | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/tests/mock_tables/state_db.json b/tests/mock_tables/state_db.json index 12552997b9..74e9eec257 100644 --- a/tests/mock_tables/state_db.json +++ b/tests/mock_tables/state_db.json @@ -403,6 +403,24 @@ "version_self_next": "0.2MS", "version_self_active": "0.2MS", "version_self_inactive": "0.2MS", + "peer_mux_direction": "active", + "self_mux_direction": "active", + "grpc_connection_status": "READY", + "Value": "AABB" + }, + "MUX_CABLE_INFO|Ethernet4": { + "version_peer_next": "0.2MS", + "version_peer_active": "0.2MS", + "version_peer_inactive": "0.2MS", + "version_nic_next": "0.2MS", + "version_nic_active": "0.2MS", + "version_nic_inactive": "0.2MS", + "version_self_next": "0.2MS", + "version_self_active": "0.2MS", + "version_self_inactive": "0.2MS", + "peer_mux_direction": "active", + "self_mux_direction": "standby", + "grpc_connection_status": "READY", "Value": "AABB" }, "MUX_CABLE_INFO|Ethernet12": { diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 2802347cf2..df93112283 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -384,6 +384,11 @@ Ethernet12 active active True READY """ +show_muxcable_grpc_muxdirection_standby_expected_output = """\ +Port Direction PeerDirection Presence ConnectivityState +--------- ----------- --------------- ---------- ------------------- +Ethernet4 standby active True READY +""" expected_muxcable_cableinfo_output = """\ Vendor Model @@ -2391,6 +2396,27 @@ def test_show_muxcable_hwmode_muxdirection_port_standby(self): assert result.exit_code == 0 assert result.output == show_muxcable_grpc_muxdirection_active_expected_output + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "standby"})) + @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet4"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_hwmode_muxdirection_port_standby(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], + ["Ethernet4"], obj=db) + assert result.exit_code == 0 + assert result.output == show_muxcable_grpc_muxdirection_standby_expected_output + @classmethod def teardown_class(cls): os.environ['UTILITIES_UNIT_TESTING'] = "0" From 66ee18c431cc55da25619a8ceff0d0d8e344a972 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 09:09:47 +0000 Subject: [PATCH 07/16] add all Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index df93112283..0363f07259 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -390,6 +390,13 @@ Ethernet4 standby active True READY """ +show_muxcable_grpc_muxdirection_active_expected_all_output = """\ +Port Direction PeerDirection Presence ConnectivityState +--------- ----------- --------------- ---------- ------------------- +Ethernet0 active active False READY +Ethernet4 standby active True READY +""" + expected_muxcable_cableinfo_output = """\ Vendor Model -------- --------------- @@ -2417,6 +2424,26 @@ def test_show_muxcable_hwmode_muxdirection_port_standby(self): assert result.exit_code == 0 assert result.output == show_muxcable_grpc_muxdirection_standby_expected_output + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "standby"})) + @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet4"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_hwmode_muxdirection_port_standby(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], obj=db) + assert result.exit_code == 0 + assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output + @classmethod def teardown_class(cls): os.environ['UTILITIES_UNIT_TESTING'] = "0" From a25ea90be27bc9095aa55eba243b9ea57daca718 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 09:33:54 +0000 Subject: [PATCH 08/16] fix tests Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 0363f07259..d78e85f493 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -394,7 +394,6 @@ Port Direction PeerDirection Presence ConnectivityState --------- ----------- --------------- ---------- ------------------- Ethernet0 active active False READY -Ethernet4 standby active True READY """ expected_muxcable_cableinfo_output = """\ @@ -2394,7 +2393,7 @@ def test_config_muxcable_telemetry_enable(self): @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) @mock.patch('re.match', mock.MagicMock(return_value=(True))) - def test_show_muxcable_hwmode_muxdirection_port_standby(self): + def test_show_muxcable_grpc_muxdirection_port_standby_with_patch(self): runner = CliRunner() db = Db() @@ -2415,7 +2414,7 @@ def test_show_muxcable_hwmode_muxdirection_port_standby(self): @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) @mock.patch('re.match', mock.MagicMock(return_value=(True))) - def test_show_muxcable_hwmode_muxdirection_port_standby(self): + def test_show_muxcable_grpc_muxdirection_port_standby(self): runner = CliRunner() db = Db() @@ -2432,11 +2431,11 @@ def test_show_muxcable_hwmode_muxdirection_port_standby(self): @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) - @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[1])) @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) @mock.patch('re.match', mock.MagicMock(return_value=(True))) - def test_show_muxcable_hwmode_muxdirection_port_standby(self): + def test_show_muxcable_grpc_muxdirection_port_all(self): runner = CliRunner() db = Db() From b61c2793009d2ceeab2d33553217b08d478be74d Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Thu, 2 Feb 2023 09:50:06 +0000 Subject: [PATCH 09/16] fix n Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index d78e85f493..1177772a96 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -2429,9 +2429,9 @@ def test_show_muxcable_grpc_muxdirection_port_standby(self): @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet4"])) @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) - @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) - @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) - @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[1])) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet0", "Ethernet4"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet0", "Ethernet4"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) @mock.patch('re.match', mock.MagicMock(return_value=(True))) From 1f43a7b3f7f5c47eb8367f2dc8fdfbf2dbfcf2db Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 00:47:09 +0000 Subject: [PATCH 10/16] add some enhancements Signed-off-by: vaibhav-dahiya --- show/muxcable.py | 99 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 22 deletions(-) diff --git a/show/muxcable.py b/show/muxcable.py index 5b24ca6a47..fbaf441383 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -1253,6 +1253,33 @@ def get_hwmode_mux_direction_port(db, port): return res_dict + +def create_active_active_mux_direction_json_result(result, port, db): + + port = platform_sfputil_helper.get_interface_alias(port, db) + result["HWMODE"][port] = {} + res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) + result["HWMODE"][port]["Direction"] = res_dict["self_mux_direction"] + result["HWMODE"][port]["Presence"] = res_dict["presence"] + result["HWMODE"][port]["PeerDirection"] = res_dict["peer_mux_direction"] + result["HWMODE"][port]["ConnectivityState"] = res_dict["grpc_connection_status"] + + rc = res_dict["rc"] + + return rc + +def create_active_standby_mux_direction_json_result(result, port, db): + + res_dict = get_hwmode_mux_direction_port(db, port) + port = platform_sfputil_helper.get_interface_alias(port, db) + result["HWMODE"][port] = {} + result["HWMODE"][port]["Direction"] = res_dict[1] + result["HWMODE"][port]["Presence"] = res_dict[2] + + rc = res_dict[0] + + return rc + def create_active_active_mux_direction_result(body, port, db): res_dict = get_grpc_cached_version_mux_direction_per_port(db, port) @@ -1260,8 +1287,8 @@ def create_active_active_mux_direction_result(body, port, db): port = platform_sfputil_helper.get_interface_alias(port, db) temp_list.append(port) temp_list.append(res_dict["self_mux_direction"]) - temp_list.append(res_dict["peer_mux_direction"]) temp_list.append(res_dict["presence"]) + temp_list.append(res_dict["peer_mux_direction"]) temp_list.append(res_dict["grpc_connection_status"]) body.append(temp_list) @@ -1296,8 +1323,9 @@ def hwmode(): @hwmode.command() @click.argument('port', metavar='', required=False, default=None) +@click.option('--json', 'json_output', required=False, is_flag=True, type=click.BOOL, help="display the output in json format") @clicommon.pass_db -def muxdirection(db, port): +def muxdirection(db, port, json_output): """Shows the current direction of the muxcable {active/standy}""" port = platform_sfputil_helper.get_interface_name(port, db) @@ -1322,14 +1350,25 @@ def muxdirection(db, port): click.echo("Not Y-cable port") return CONFIG_FAIL - body = [] - if cable_type == "active-active": - headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] - rc = create_active_active_mux_direction_result(body, port, db) + if json_output: + result = {} + result ["HWMODE"] = {} + if cable_type == "active-active": + rc = create_active_active_mux_direction_json_result(result, port, db) + else: + rc = False + rc = create_active_standby_mux_direction_json_result(result, port, db) + click.echo("{}".format(json.dumps(result, indent=4))) + else: - rc = create_active_standby_mux_direction_result(body, port, db) - headers = ['Port', 'Direction', 'Presence'] - click.echo(tabulate(body, headers=headers)) + body = [] + if cable_type == "active-active": + headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] + rc = create_active_active_mux_direction_result(body, port, db) + else: + rc = create_active_standby_mux_direction_result(body, port, db) + headers = ['Port', 'Direction', 'Presence'] + click.echo(tabulate(body, headers=headers)) return rc @@ -1340,6 +1379,9 @@ def muxdirection(db, port): rc_exit = True body = [] active_active = False + if json_output: + result = {} + result ["HWMODE"] = {} for port in natsorted(logical_port_list): @@ -1370,20 +1412,33 @@ def muxdirection(db, port): asic_index = get_asic_index_for_port(port) cable_type = get_optional_value_for_key_in_config_tbl(per_npu_configdb[asic_index], port, "cable_type", "MUX_CABLE") - if cable_type == 'active-active': - rc = create_active_active_mux_direction_result(body, port, db) - active_active = True + if json_output: + if cable_type == "active-active": + rc = create_active_active_mux_direction_json_result(result, port, db) + active_active = True + else: + rc = create_active_standby_mux_direction_json_result(result, port, db) + else: - rc = create_active_standby_mux_direction_result(body, port, db) - headers = ['Port', 'Direction', 'Presence'] - if rc != 0: - rc_exit = False + if cable_type == 'active-active': + rc = create_active_active_mux_direction_result(body, port, db) + active_active = True + else: + rc = create_active_standby_mux_direction_result(body, port, db) + if rc != 0: + rc_exit = False + + - if active_active: - headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + if json_output: + click.echo("{}".format(json.dumps(result, indent=4))) else: - headers = ['Port', 'Direction', 'Presence'] - click.echo(tabulate(body, headers=headers)) + if active_active: + + headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] + else: + headers = ['Port', 'Direction', 'Presence'] + click.echo(tabulate(body, headers=headers)) if rc_exit == False: sys.exit(EXIT_FAIL) @@ -2132,7 +2187,7 @@ def muxdirection(db, port): body = [] - headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] rc = create_active_active_mux_direction_result(body, port, db) click.echo(tabulate(body, headers=headers)) @@ -2177,7 +2232,7 @@ def muxdirection(db, port): if rc != True: rc_exit = False - headers = ['Port', 'Direction', 'PeerDirection', 'Presence', 'ConnectivityState'] + headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] click.echo(tabulate(body, headers=headers)) From adb1f4b97e3cb211d43df49085bbfbe7b2a959d2 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 02:10:57 +0000 Subject: [PATCH 11/16] add all Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 1177772a96..d7b48c62f3 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -379,21 +379,21 @@ """ show_muxcable_grpc_muxdirection_active_expected_output = """\ -Port Direction PeerDirection Presence ConnectivityState ----------- ----------- --------------- ---------- ------------------- -Ethernet12 active active True READY +Port Direction Presence PeerDirection ConnectivityState +---------- ----------- ---------- --------------- ------------------- +Ethernet12 active True active READY """ show_muxcable_grpc_muxdirection_standby_expected_output = """\ -Port Direction PeerDirection Presence ConnectivityState ---------- ----------- --------------- ---------- ------------------- -Ethernet4 standby active True READY +Port Direction Presence PeerDirection ConnectivityState +--------- ----------- ---------- --------------- ------------------- +Ethernet4 standby True active READY """ show_muxcable_grpc_muxdirection_active_expected_all_output = """\ -Port Direction PeerDirection Presence ConnectivityState ---------- ----------- --------------- ---------- ------------------- -Ethernet0 active active False READY +Port Direction Presence PeerDirection ConnectivityState +--------- ----------- ---------- --------------- ------------------- +Ethernet0 active False active READY """ expected_muxcable_cableinfo_output = """\ From 49e415649a329b3ace259f7298494e1db731c5b1 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 05:35:30 +0000 Subject: [PATCH 12/16] add more tests Signed-off-by: vaibhav-dahiya --- show/muxcable.py | 32 +++++++++++++++----- tests/muxcable_test.py | 68 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/show/muxcable.py b/show/muxcable.py index fbaf441383..b640d32135 100644 --- a/show/muxcable.py +++ b/show/muxcable.py @@ -2171,8 +2171,9 @@ def grpc(): @grpc.command() @click.argument('port', metavar='', required=False, default=None) +@click.option('--json', 'json_output', required=False, is_flag=True, type=click.BOOL, help="display the output in json format") @clicommon.pass_db -def muxdirection(db, port): +def muxdirection(db, port, json_output): """Shows the current direction of the FPGA facing port on Tx Side {active/standy}""" port = platform_sfputil_helper.get_interface_name(port, db) @@ -2184,12 +2185,18 @@ def muxdirection(db, port): click.echo("Not Y-cable port") return CONFIG_FAIL + if json_output: + result = {} + result ["HWMODE"] = {} + rc = create_active_active_mux_direction_json_result(result, port, db) + click.echo("{}".format(json.dumps(result, indent=4))) - body = [] + else: + body = [] - headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] - rc = create_active_active_mux_direction_result(body, port, db) - click.echo(tabulate(body, headers=headers)) + headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] + rc = create_active_active_mux_direction_result(body, port, db) + click.echo(tabulate(body, headers=headers)) return rc @@ -2200,6 +2207,9 @@ def muxdirection(db, port): rc_exit = True body = [] + if json_output: + result = {} + result ["HWMODE"] = {} for port in natsorted(logical_port_list): @@ -2227,14 +2237,20 @@ def muxdirection(db, port): if port != logical_port_list_per_port[0]: continue - rc = create_active_active_mux_direction_result(body, port, db) + if json_output: + rc = create_active_active_mux_direction_json_result(result, port, db) + else: + rc = create_active_active_mux_direction_result(body, port, db) if rc != True: rc_exit = False - headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] + if json_output: + click.echo("{}".format(json.dumps(result, indent=4))) + else: + headers = ['Port', 'Direction', 'Presence', 'PeerDirection', 'ConnectivityState'] - click.echo(tabulate(body, headers=headers)) + click.echo(tabulate(body, headers=headers)) if rc_exit == False: sys.exit(EXIT_FAIL) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index d7b48c62f3..2e8a0380a3 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -396,6 +396,31 @@ Ethernet0 active False active READY """ +show_muxcable_grpc_muxdirection_active_expected_all_output_json = """\ +{ + "HWMODE": { + "Ethernet0": { + "Direction": "active", + "Presence": "False", + "PeerDirection": "active", + "ConnectivityState": "READY" + } + } +} +""" + +show_muxcable_grpc_muxdirection_standby_expected_output_json = """\ +{ + "HWMODE": { + "Ethernet4": { + "Direction": "active", + "Presence": "True", + "PeerDirection": "active", + "ConnectivityState": "READY" + } + } +""" + expected_muxcable_cableinfo_output = """\ Vendor Model -------- --------------- @@ -2423,6 +2448,27 @@ def test_show_muxcable_grpc_muxdirection_port_standby(self): assert result.exit_code == 0 assert result.output == show_muxcable_grpc_muxdirection_standby_expected_output + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "standby"})) + @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet4"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet4", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_grpc_muxdirection_port_standby_json(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], + ["Ethernet4", "--json"], obj=db) + assert result.exit_code == 0 + assert result.output == show_muxcable_grpc_muxdirection_standby_expected_output + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, 1: "standby"})) @@ -2441,7 +2487,27 @@ def test_show_muxcable_grpc_muxdirection_port_all(self): result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], obj=db) assert result.exit_code == 0 - assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output + assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output_json + + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "standby"})) + @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet4"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet0", "Ethernet4"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet0", "Ethernet4"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(2))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_grpc_muxdirection_port_all_json(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], ["--json"], obj=db) + assert result.exit_code == 0 + assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output_json @classmethod def teardown_class(cls): From 6553604ba2f913d68d7b16c2c9089fd78385b378 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 06:01:16 +0000 Subject: [PATCH 13/16] add Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 2e8a0380a3..bb1808fc56 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -2467,7 +2467,7 @@ def test_show_muxcable_grpc_muxdirection_port_standby_json(self): result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], ["Ethernet4", "--json"], obj=db) assert result.exit_code == 0 - assert result.output == show_muxcable_grpc_muxdirection_standby_expected_output + assert result.output == show_muxcable_grpc_muxdirection_standby_expected_output_json @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, @@ -2487,7 +2487,7 @@ def test_show_muxcable_grpc_muxdirection_port_all(self): result = runner.invoke(show.cli.commands["muxcable"].commands["grpc"].commands["muxdirection"], obj=db) assert result.exit_code == 0 - assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output_json + assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, From 3a36590b791ceb0f517080eaeb928c963bdfacc8 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 06:17:37 +0000 Subject: [PATCH 14/16] fix Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index bb1808fc56..8cd52be841 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -413,7 +413,7 @@ { "HWMODE": { "Ethernet4": { - "Direction": "active", + "Direction": "standby", "Presence": "True", "PeerDirection": "active", "ConnectivityState": "READY" From 3b595117386ad0929f5faac439f360e4b0210163 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 06:53:21 +0000 Subject: [PATCH 15/16] add fix tests Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 8cd52be841..54c25a00a8 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -419,6 +419,7 @@ "ConnectivityState": "READY" } } +} """ expected_muxcable_cableinfo_output = """\ From 550f1695f029cd63aebdb4cd7a2d5801b36aefc5 Mon Sep 17 00:00:00 2001 From: vaibhav-dahiya Date: Tue, 7 Feb 2023 19:57:53 +0000 Subject: [PATCH 16/16] add UT Signed-off-by: vaibhav-dahiya --- tests/muxcable_test.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/muxcable_test.py b/tests/muxcable_test.py index 54c25a00a8..a63cdf5714 100644 --- a/tests/muxcable_test.py +++ b/tests/muxcable_test.py @@ -439,6 +439,17 @@ Ethernet12 active True """ +show_muxcable_hwmode_muxdirection_active_expected_output_json = """\ +{ + "HWMODE": { + "Ethernet12": { + "Direction": "active", + "Presence": "True" + } + } +} +""" + show_muxcable_hwmode_muxdirection_active_expected_output_alias = """\ Port Direction Presence ------ ----------- ---------- @@ -2510,6 +2521,30 @@ def test_show_muxcable_grpc_muxdirection_port_all_json(self): assert result.exit_code == 0 assert result.output == show_muxcable_grpc_muxdirection_active_expected_all_output_json + @mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0, + 1: "active"})) + @mock.patch('show.muxcable.get_hwmode_mux_direction_port', mock.MagicMock(return_value={0: 0, + 1: "active", + 2: "True"})) + @mock.patch('show.muxcable.check_port_in_mux_cable_table', mock.MagicMock(return_value=True)) + @mock.patch('utilities_common.platform_sfputil_helper.get_logical_list', mock.MagicMock(return_value=["Ethernet0", "Ethernet12"])) + @mock.patch('utilities_common.platform_sfputil_helper.get_asic_id_for_logical_port', mock.MagicMock(return_value=0)) + @mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.get_physical_to_logical', mock.MagicMock(return_value={0: ["Ethernet12", "Ethernet0"]})) + @mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0])) + @mock.patch('sonic_y_cable.y_cable.check_read_side', mock.MagicMock(return_value=(1))) + @mock.patch('sonic_y_cable.y_cable.check_mux_direction', mock.MagicMock(return_value=(1))) + @mock.patch('re.match', mock.MagicMock(return_value=(True))) + def test_show_muxcable_hwmode_muxdirection_port_active(self): + runner = CliRunner() + db = Db() + + result = runner.invoke(show.cli.commands["muxcable"].commands["hwmode"].commands["muxdirection"], + ["Ethernet12", "--json"], obj=db) + assert result.exit_code == 0 + assert result.output == show_muxcable_hwmode_muxdirection_active_expected_output_json + @classmethod def teardown_class(cls): os.environ['UTILITIES_UNIT_TESTING'] = "0"