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
22 changes: 22 additions & 0 deletions scripts/decode-syseeprom
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def print_model(use_db=False):
print(model)


def print_vendor(use_db=False):
vendor = None
if use_db:
vendor = get_tlv_value_from_db(TlvInfoDecoder._TLV_CODE_VENDOR_NAME)
else:
eeprom = instantiate_eeprom_object()
if not eeprom:
print('Failed to read system EEPROM info')
return

try:
vendor = eeprom.vendorstr()
except TypeError:
vendor = eeprom.vendorstr(eeprom.read_eeprom())

print(vendor)

#-------------------------------------------------------------------------------
#
# sets global variable "optcfg"
Expand All @@ -220,6 +237,9 @@ def get_cmdline_opts():
help='print the device product name')
optcfg.add_option('-m', dest='mgmtmac', action='store_true', default=False,
help='print the base mac address for management interfaces')
optcfg.add_option('-v', dest='vendorstr', action='store_true', default=False,
help='print the vendor string for the platform')

return optcfg.parse_args()


Expand Down Expand Up @@ -253,6 +273,8 @@ def main():
print_serial(use_db)
elif opts.modelstr:
print_model(use_db)
elif opts.vendorstr:
print_vendor(use_db)
else:
if use_db:
tlv_dict = read_eeprom_from_db()
Expand Down
32 changes: 32 additions & 0 deletions tests/decode_syseeprom_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ def test_print_model(self, capsys):
captured = capsys.readouterr()
assert captured.out == 'S6100-ON\n'

def test_print_vendor(self, capsys):
decode_syseeprom.print_vendor(True)
captured = capsys.readouterr()
assert captured.out == 'DELL\n'

@mock.patch('os.geteuid', lambda: 0)
@mock.patch('sonic_py_common.device_info.get_platform', lambda: 'arista')
@mock.patch.object(sys, 'argv', ["decode-syseeprom"])
Expand All @@ -212,3 +217,30 @@ def test_support_platforms_not_db_based(self, mockDbBased, mockNotDbBased):
def test_support_platforms_no_eeprom(self, mockDbBased, mockNotDbBased):
ret = decode_syseeprom.main()
assert ret == errno.ENODEV

@mock.patch('decode_syseeprom.instantiate_eeprom_object')
def test_print_vendor_eeprom_success(self, mock_instantiate, capsys):
mock_eeprom = mock.MagicMock()
mock_eeprom.vendorstr.return_value = 'MOCK_VENDOR'
mock_instantiate.return_value = mock_eeprom
decode_syseeprom.print_vendor(use_db=False)
captured = capsys.readouterr()
assert captured.out == 'MOCK_VENDOR\n'

@mock.patch('decode_syseeprom.instantiate_eeprom_object')
def test_print_vendor_eeprom_none(self, mock_instantiate, capsys):
mock_instantiate.return_value = None
decode_syseeprom.print_vendor(use_db=False)
captured = capsys.readouterr()
assert 'Failed to read system EEPROM info' in captured.out

@mock.patch('decode_syseeprom.instantiate_eeprom_object')
def test_print_vendor_typeerror_fallback(self, mock_instantiate, capsys):
mock_eeprom = mock.MagicMock()
# Simulate vendorstr() raising TypeError when called with no args
mock_eeprom.vendorstr.side_effect = [TypeError(), 'FALLBACK_VENDOR']
mock_eeprom.read_eeprom.return_value = 'dummy'
mock_instantiate.return_value = mock_eeprom
decode_syseeprom.print_vendor(use_db=False)
captured = capsys.readouterr()
assert captured.out == 'FALLBACK_VENDOR\n'
Loading