Skip to content

Commit

Permalink
Implementing set_optoe_write_timeout API (#422)
Browse files Browse the repository at this point in the history
Signed-off-by: Mihir Patel <[email protected]>
  • Loading branch information
mihirpat1 authored Dec 18, 2023
1 parent 0f72932 commit c82ae54
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sonic_platform_base/sonic_xcvr/sfp_optoe_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ def set_optoe_write_max(self, write_max):
except (OSError, IOError):
pass

def set_optoe_write_timeout(self, write_timeout):
sys_path = self.get_eeprom_path()
sys_path = sys_path.replace("eeprom", "write_timeout")
try:
with open(sys_path, mode='w') as f:
f.write(str(write_timeout))
except (OSError, IOError):
pass

def read_eeprom(self, offset, num_bytes):
try:
with open(self.get_eeprom_path(), mode='rb', buffering=0) as f:
Expand Down
34 changes: 34 additions & 0 deletions tests/sonic_xcvr/test_sfp_optoe_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest.mock import mock_open
from mock import MagicMock
from mock import patch
import pytest
Expand Down Expand Up @@ -79,3 +80,36 @@ def test_get_vdm_unfreeze_status(self, mock_response1, mock_response2, expected)
result = self.sfp_optoe_api.get_vdm_unfreeze_status()
assert result == expected

@patch("builtins.open", new_callable=mock_open)
@patch.object(SfpOptoeBase, 'get_eeprom_path')
def test_set_optoe_write_timeout_success(self, mock_get_eeprom_path, mock_open):
mock_get_eeprom_path.return_value = "/sys/bus/i2c/devices/1-0050/eeprom"
expected_path = "/sys/bus/i2c/devices/1-0050/write_timeout"
expected_timeout = 1

self.sfp_optoe_api.set_optoe_write_timeout(expected_timeout)

mock_open.assert_called_once_with(expected_path, mode='w')
mock_open().write.assert_called_once_with(str(expected_timeout))

@patch("builtins.open", new_callable=mock_open)
@patch.object(SfpOptoeBase, 'get_eeprom_path')
def test_set_optoe_write_timeout_ioerror(self, mock_get_eeprom_path, mock_open):
mock_get_eeprom_path.return_value = "/sys/bus/i2c/devices/1-0050/eeprom"
expected_timeout = 1
mock_open.side_effect = IOError

self.sfp_optoe_api.set_optoe_write_timeout(expected_timeout)

mock_open.assert_called()

@patch("builtins.open", new_callable=mock_open)
@patch.object(SfpOptoeBase, 'get_eeprom_path')
def test_set_optoe_write_timeout_oserror(self, mock_get_eeprom_path, mock_open):
mock_get_eeprom_path.return_value = "/sys/bus/i2c/devices/1-0050/eeprom"
expected_timeout = 1
mock_open.side_effect = OSError

self.sfp_optoe_api.set_optoe_write_timeout(expected_timeout)

mock_open.assert_called()

0 comments on commit c82ae54

Please sign in to comment.