Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sonic_platform_base/sonic_eeprom/eeprom_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,21 @@ def write_cache(self, e):
if self.cache_name:
F = None
try:
# Ensure the directory exists
directory = os.path.dirname(self.cache_name)

if not os.path.exists(directory):
os.makedirs(directory)

# Set directory permissions to 755
os.chmod(directory, 0o755)

F = open(self.cache_name, "wb")
F.seek(self.s)
F.write(e)

# Set file permissions to 755
os.chmod(self.cache_name, 0o755)
except IOError as e:
raise IOError("Failed to write cache : %s" % (str(e)))
finally:
Expand Down
36 changes: 35 additions & 1 deletion tests/eeprom_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import subprocess
from unittest import mock
from unittest.mock import patch, MagicMock
from unittest.mock import patch, MagicMock, mock_open
from sonic_platform_base.sonic_eeprom import eeprom_base, eeprom_tlvinfo
EEPROM_SYMLINK = "vpd_info"
EEPROM_HEX_FILE = "syseeprom.hex"
Expand Down Expand Up @@ -200,6 +200,40 @@ def test_set_eeprom_invalid_field(self, mock_print):
mock_print.assert_called_with("Error: invalid field '0x20'")
assert e.value.code == 1

@mock.patch("builtins.open", new_callable=mock.mock_open)
@mock.patch("os.makedirs")
@mock.patch("os.path.exists", return_value=False)
@mock.patch("os.chmod")
def test_write_cache(self, mock_chmod, mock_exists, mock_makedirs, mock_open):
# Create an instance of TlvInfoDecoder with a mock cache_name
eeprom_decoder = eeprom_tlvinfo.TlvInfoDecoder("/dev/null", 0, "", True)
eeprom_decoder.cache_name = "/tmp/mock_cache"

# Mock EEPROM data to write
mock_eeprom_data = b"mock_eeprom_data"

# Call the write_cache method
eeprom_decoder.write_cache(mock_eeprom_data)

# Verify that os.makedirs was called with the correct directory
mock_makedirs.assert_called_once_with("/tmp")

# Verify that os.chmod was called to set directory permissions
mock_chmod.assert_any_call("/tmp", 0o755)

# Verify that the file was opened in write-binary mode
mock_open.assert_called_once_with("/tmp/mock_cache", "wb")

# Verify that the file's seek method was called with the correct offset
mock_open().seek.assert_called_once_with(eeprom_decoder.s)

# Verify that the file's write method was called with the EEPROM data
mock_open().write.assert_called_once_with(mock_eeprom_data)

# Verify that os.chmod was called to set file permissions
mock_chmod.assert_any_call("/tmp/mock_cache", 0o755)


def teardown(self):
print("TEAR DOWN")

Loading