Skip to content

Commit c82ae54

Browse files
authored
Implementing set_optoe_write_timeout API (#422)
Signed-off-by: Mihir Patel <[email protected]>
1 parent 0f72932 commit c82ae54

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

sonic_platform_base/sonic_xcvr/sfp_optoe_base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ def set_optoe_write_max(self, write_max):
230230
except (OSError, IOError):
231231
pass
232232

233+
def set_optoe_write_timeout(self, write_timeout):
234+
sys_path = self.get_eeprom_path()
235+
sys_path = sys_path.replace("eeprom", "write_timeout")
236+
try:
237+
with open(sys_path, mode='w') as f:
238+
f.write(str(write_timeout))
239+
except (OSError, IOError):
240+
pass
241+
233242
def read_eeprom(self, offset, num_bytes):
234243
try:
235244
with open(self.get_eeprom_path(), mode='rb', buffering=0) as f:

tests/sonic_xcvr/test_sfp_optoe_base.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest.mock import mock_open
12
from mock import MagicMock
23
from mock import patch
34
import pytest
@@ -79,3 +80,36 @@ def test_get_vdm_unfreeze_status(self, mock_response1, mock_response2, expected)
7980
result = self.sfp_optoe_api.get_vdm_unfreeze_status()
8081
assert result == expected
8182

83+
@patch("builtins.open", new_callable=mock_open)
84+
@patch.object(SfpOptoeBase, 'get_eeprom_path')
85+
def test_set_optoe_write_timeout_success(self, mock_get_eeprom_path, mock_open):
86+
mock_get_eeprom_path.return_value = "/sys/bus/i2c/devices/1-0050/eeprom"
87+
expected_path = "/sys/bus/i2c/devices/1-0050/write_timeout"
88+
expected_timeout = 1
89+
90+
self.sfp_optoe_api.set_optoe_write_timeout(expected_timeout)
91+
92+
mock_open.assert_called_once_with(expected_path, mode='w')
93+
mock_open().write.assert_called_once_with(str(expected_timeout))
94+
95+
@patch("builtins.open", new_callable=mock_open)
96+
@patch.object(SfpOptoeBase, 'get_eeprom_path')
97+
def test_set_optoe_write_timeout_ioerror(self, mock_get_eeprom_path, mock_open):
98+
mock_get_eeprom_path.return_value = "/sys/bus/i2c/devices/1-0050/eeprom"
99+
expected_timeout = 1
100+
mock_open.side_effect = IOError
101+
102+
self.sfp_optoe_api.set_optoe_write_timeout(expected_timeout)
103+
104+
mock_open.assert_called()
105+
106+
@patch("builtins.open", new_callable=mock_open)
107+
@patch.object(SfpOptoeBase, 'get_eeprom_path')
108+
def test_set_optoe_write_timeout_oserror(self, mock_get_eeprom_path, mock_open):
109+
mock_get_eeprom_path.return_value = "/sys/bus/i2c/devices/1-0050/eeprom"
110+
expected_timeout = 1
111+
mock_open.side_effect = OSError
112+
113+
self.sfp_optoe_api.set_optoe_write_timeout(expected_timeout)
114+
115+
mock_open.assert_called()

0 commit comments

Comments
 (0)