Skip to content

Commit fb93dae

Browse files
committed
Add unit test
1 parent 75123c7 commit fb93dae

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tests/eeprom_base_test.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33
import subprocess
44
from unittest import mock
5-
from unittest.mock import patch, MagicMock
5+
from unittest.mock import patch, MagicMock, mock_open
66
from sonic_platform_base.sonic_eeprom import eeprom_base, eeprom_tlvinfo
77
EEPROM_SYMLINK = "vpd_info"
88
EEPROM_HEX_FILE = "syseeprom.hex"
@@ -200,6 +200,40 @@ def test_set_eeprom_invalid_field(self, mock_print):
200200
mock_print.assert_called_with("Error: invalid field '0x20'")
201201
assert e.value.code == 1
202202

203+
@mock.patch("builtins.open", new_callable=mock.mock_open)
204+
@mock.patch("os.makedirs")
205+
@mock.patch("os.path.exists", return_value=False)
206+
@mock.patch("os.chmod")
207+
def test_write_cache(self, mock_chmod, mock_exists, mock_makedirs, mock_open):
208+
# Create an instance of TlvInfoDecoder with a mock cache_name
209+
eeprom_decoder = eeprom_tlvinfo.TlvInfoDecoder("/dev/null", 0, "", True)
210+
eeprom_decoder.cache_name = "/tmp/mock_cache"
211+
212+
# Mock EEPROM data to write
213+
mock_eeprom_data = b"mock_eeprom_data"
214+
215+
# Call the write_cache method
216+
eeprom_decoder.write_cache(mock_eeprom_data)
217+
218+
# Verify that os.makedirs was called with the correct directory
219+
mock_makedirs.assert_called_once_with("/tmp")
220+
221+
# Verify that os.chmod was called to set directory permissions
222+
mock_chmod.assert_any_call("/tmp", 0o755)
223+
224+
# Verify that the file was opened in write-binary mode
225+
mock_open.assert_called_once_with("/tmp/mock_cache", "wb")
226+
227+
# Verify that the file's seek method was called with the correct offset
228+
mock_open().seek.assert_called_once_with(eeprom_decoder.s)
229+
230+
# Verify that the file's write method was called with the EEPROM data
231+
mock_open().write.assert_called_once_with(mock_eeprom_data)
232+
233+
# Verify that os.chmod was called to set file permissions
234+
mock_chmod.assert_any_call("/tmp/mock_cache", 0o755)
235+
236+
203237
def teardown(self):
204238
print("TEAR DOWN")
205239

0 commit comments

Comments
 (0)