|
2 | 2 | import pytest |
3 | 3 | import subprocess |
4 | 4 | from unittest import mock |
5 | | -from unittest.mock import patch, MagicMock |
| 5 | +from unittest.mock import patch, MagicMock, mock_open |
6 | 6 | from sonic_platform_base.sonic_eeprom import eeprom_base, eeprom_tlvinfo |
7 | 7 | EEPROM_SYMLINK = "vpd_info" |
8 | 8 | EEPROM_HEX_FILE = "syseeprom.hex" |
@@ -200,6 +200,40 @@ def test_set_eeprom_invalid_field(self, mock_print): |
200 | 200 | mock_print.assert_called_with("Error: invalid field '0x20'") |
201 | 201 | assert e.value.code == 1 |
202 | 202 |
|
| 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 | + |
203 | 237 | def teardown(self): |
204 | 238 | print("TEAR DOWN") |
205 | 239 |
|
0 commit comments