Skip to content

Commit

Permalink
Add a missing else condition and corresponding test case
Browse files Browse the repository at this point in the history
  • Loading branch information
vvolam committed Nov 25, 2024
1 parent e96fcbf commit f540e68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/sonic-py-common/sonic_py_common/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def is_dpu():
# Retrieve platform.json data
platform_data = get_platform_json_data()
if platform_data:
return "DPU" in platform_data
return 'DPU' in platform_data

return False

Expand Down Expand Up @@ -956,6 +956,8 @@ def get_dpu_info():
return platform_data["DPUS"]
elif 'DPU' in platform_data:
return platform_data['DPU']
else:
return {}


def get_num_dpus():
Expand Down
8 changes: 8 additions & 0 deletions src/sonic-py-common/tests/device_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def test_is_dpu(self, mock_get_platform, mock_is_smartswitch, mock_get_platform_
mock_get_platform_json_data.return_value={"DPU": {}}
assert device_info.is_dpu() == True

# Test case where platform is a smart switch and DPU data is present in platform.json
mock_get_platform_json_data.return_value={"DPUS": {}}
assert device_info.is_dpu() == False

@mock.patch("sonic_py_common.device_info.get_platform_json_data")
@mock.patch("sonic_py_common.device_info.get_platform")
def test_get_dpu_info(self, mock_get_platform, mock_get_platform_json_data):
Expand All @@ -273,6 +277,10 @@ def test_get_dpu_info(self, mock_get_platform, mock_get_platform_json_data):
mock_get_platform_json_data.return_value = {"DPU": {"dpu0": {}}}
assert device_info.get_dpu_info() == {"dpu0": {}}

# Test case where platform.json data does not contain "DPU" or "DPUS"
mock_get_platform_json_data.return_value = {"chassis": {}}
assert device_info.get_dpu_info() == {}

@mock.patch("sonic_py_common.device_info.get_platform_json_data")
@mock.patch("sonic_py_common.device_info.is_dpu")
@mock.patch("sonic_py_common.device_info.get_platform")
Expand Down

0 comments on commit f540e68

Please sign in to comment.