Skip to content

Commit 4e27ad7

Browse files
authored
[decode-syseeprom] Refactor use of swsscommon; Add unit tests (sonic-net#1444)
- Refactor the way swsscommon is used in decode-syseeprom to align with more modern approach - Add unit tests for DB-related functionality of decode-syseeprom utility - Align whitespace in tests/mock_tables/state_db.json
1 parent 787d884 commit 4e27ad7

File tree

3 files changed

+364
-103
lines changed

3 files changed

+364
-103
lines changed

scripts/decode-syseeprom

+20-38
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import sys
1818
import sonic_platform
1919
from sonic_platform_base.sonic_eeprom.eeprom_tlvinfo import TlvInfoDecoder
2020
from sonic_py_common import device_info
21-
from swsscommon import swsscommon
21+
from swsscommon.swsscommon import SonicV2Connector
2222
from tabulate import tabulate
2323

2424

@@ -77,67 +77,49 @@ def print_eeprom_dict(tlv_dict):
7777
def read_eeprom_from_db():
7878
tlv_dict = {}
7979

80-
state_db = swsscommon.DBConnector('STATE_DB', 0)
81-
tbl = swsscommon.Table(state_db, EEPROM_INFO_TABLE)
80+
db = SonicV2Connector(host="127.0.0.1")
81+
db.connect(db.STATE_DB)
8282

83-
status, fvs = tbl.get('State')
84-
status = bool(status)
85-
data = dict(fvs)
86-
if not status or data.get('Initialized', '0') != '1':
83+
initialized = db.get(db.STATE_DB, '{}|{}'.format(EEPROM_INFO_TABLE, 'State'), 'Initialized')
84+
if initialized != '1':
8785
return None
8886

89-
status, fvs = tbl.get('TlvHeader')
90-
status = bool(status)
91-
if not status:
92-
return None
93-
94-
data = dict(fvs)
87+
tlv_header = db.get_all(db.STATE_DB, '{}|{}'.format(EEPROM_INFO_TABLE, 'TlvHeader'))
9588
tlv_dict['header'] = {}
96-
tlv_dict['header']['id'] = data.get('Id String', 'N/A')
97-
tlv_dict['header']['version'] = data.get('Version', 'N/A')
98-
tlv_dict['header']['length'] = data.get('Total Length', 'N/A')
89+
tlv_dict['header']['id'] = tlv_header.get('Id String', 'N/A')
90+
tlv_dict['header']['version'] = tlv_header.get('Version', 'N/A')
91+
tlv_dict['header']['length'] = tlv_header.get('Total Length', 'N/A')
9992

10093
tlv_dict['tlv_list'] = []
10194
for tlv_code in range(TlvInfoDecoder._TLV_CODE_PRODUCT_NAME, TlvInfoDecoder._TLV_CODE_SERVICE_TAG + 1):
10295
tlv_code_string = '0x{:02x}'.format(tlv_code)
103-
status, fvs = tbl.get(tlv_code_string)
104-
status = bool(status)
105-
if not status:
106-
continue
96+
tlv_data = db.get_all(db.STATE_DB, '{}|{}'.format(EEPROM_INFO_TABLE, tlv_code_string))
10797

108-
data = dict(fvs)
10998
tlv = {}
11099
tlv['code'] = tlv_code_string
111-
tlv['name'] = data.get('Name', 'N/A')
112-
tlv['length'] = data.get('Len', 'N/A')
113-
tlv['value'] = data.get('Value', 'N/A')
100+
tlv['name'] = tlv_data.get('Name', 'N/A')
101+
tlv['length'] = tlv_data.get('Len', 'N/A')
102+
tlv['value'] = tlv_data.get('Value', 'N/A')
114103
tlv_dict['tlv_list'].append(tlv)
115104

116-
status, fvs = tbl.get('Checksum')
117-
tlv_dict['checksum_valid'] = dict(fvs).get('Valid', '0') == '1'
105+
checksum_valid = db.get(db.STATE_DB, '{}|{}'.format(EEPROM_INFO_TABLE, 'Checksum'), 'Valid')
106+
tlv_dict['checksum_valid'] = (checksum_valid == '1')
118107

119108
return tlv_dict
120109

121110

122111
def get_tlv_value_from_db(tlv_code):
123-
state_db = swsscommon.DBConnector('STATE_DB', 0)
124-
tbl = swsscommon.Table(state_db, EEPROM_INFO_TABLE)
112+
db = SonicV2Connector(host="127.0.0.1")
113+
db.connect(db.STATE_DB)
125114

126-
status, fvs = tbl.get('State')
127-
status = bool(status)
128-
data = dict(fvs)
129-
if not status or data.get('Initialized', '0') != '1':
115+
initialized = db.get(db.STATE_DB, '{}|{}'.format(EEPROM_INFO_TABLE, 'State'), 'Initialized')
116+
if initialized != '1':
130117
print('Failed to read system EEPROM info from DB')
131118
return None
132119

133120
tlv_code_string = '0x{:02x}'.format(tlv_code)
134-
status, fvs = tbl.get(tlv_code_string)
135-
status = bool(status)
136-
if not status:
137-
print('Failed to read system EEPROM info from DB')
138-
return None
139121

140-
return dict(fvs).get('Value')
122+
return db.get(db.STATE_DB, '{}|{}'.format(EEPROM_INFO_TABLE, tlv_code_string), 'Value')
141123

142124

143125
def print_mgmt_mac(use_db=False):

tests/decode_syseeprom_test.py

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import importlib
2+
import os
3+
import sys
4+
from unittest import mock
5+
6+
import pytest
7+
from click.testing import CliRunner
8+
9+
from .mock_tables import dbconnector
10+
11+
test_path = os.path.dirname(os.path.abspath(__file__))
12+
modules_path = os.path.dirname(test_path)
13+
scripts_path = os.path.join(modules_path, 'scripts')
14+
sys.path.insert(0, modules_path)
15+
16+
sys.modules['sonic_platform'] = mock.MagicMock()
17+
18+
19+
decode_syseeprom_path = os.path.join(scripts_path, 'decode-syseeprom')
20+
loader = importlib.machinery.SourceFileLoader('decode-syseeprom', decode_syseeprom_path)
21+
spec = importlib.util.spec_from_loader(loader.name, loader)
22+
decode_syseeprom = importlib.util.module_from_spec(spec)
23+
loader.exec_module(decode_syseeprom)
24+
25+
# Replace swsscommon objects with mocked objects
26+
decode_syseeprom.SonicV2Connector = dbconnector.SonicV2Connector
27+
28+
SAMPLE_TLV_DICT = {
29+
'header': {
30+
'id': 'TlvInfo',
31+
'version': '1',
32+
'length': '170'
33+
},
34+
'tlv_list': [
35+
{
36+
'code': '0x21',
37+
'name': 'Product Name',
38+
'length': '8',
39+
'value': 'S6100-ON'
40+
},
41+
{
42+
'code': '0x22',
43+
'name': 'Part Number',
44+
'length': '6',
45+
'value': '0F6N2R'
46+
},
47+
{
48+
'code': '0x23',
49+
'name': 'Serial Number',
50+
'length': '20',
51+
'value': 'TH0F6N2RCET0007600NG'
52+
},
53+
{
54+
'code': '0x24',
55+
'name': 'Base MAC Address',
56+
'length': '6',
57+
'value': '0C:29:EF:CF:AC:A0'
58+
},
59+
{
60+
'code': '0x25',
61+
'name': 'Manufacture Date',
62+
'length': '19',
63+
'value': '07/07/2020 15:05:34'
64+
},
65+
{
66+
'code': '0x26',
67+
'name': 'Device Version',
68+
'length': '1',
69+
'value': '1'
70+
},
71+
{
72+
'code': '0x27',
73+
'name': 'Label Revision',
74+
'length': '3',
75+
'value': 'A08'
76+
},
77+
{
78+
'code': '0x28',
79+
'name': 'Platform Name',
80+
'length': '26',
81+
'value': 'x86_64-dell_s6100_c2538-r0'
82+
},
83+
{
84+
'code': '0x29',
85+
'name': 'ONIE Version',
86+
'length': '8',
87+
'value': '3.15.1.0'
88+
},
89+
{
90+
'code': '0x2a',
91+
'name': 'MAC Addresses',
92+
'length': '2',
93+
'value': '384'
94+
},
95+
{
96+
'code': '0x2b',
97+
'name': 'Manufacturer',
98+
'length': '5',
99+
'value': 'CET00'
100+
},
101+
{
102+
'code': '0x2c',
103+
'name': 'Manufacture Country',
104+
'length': '2',
105+
'value': 'TH'
106+
},
107+
{
108+
'code': '0x2d',
109+
'name': 'Vendor Name',
110+
'length': '4',
111+
'value': 'DELL'
112+
},
113+
{
114+
'code': '0x2e',
115+
'name': 'Diag Version',
116+
'length': '8',
117+
'value': '3.25.4.1'
118+
},
119+
{
120+
'code': '0x2f',
121+
'name': 'Service Tag',
122+
'length': '7',
123+
'value': 'F3CD9Z2'
124+
}
125+
],
126+
'checksum_valid': True
127+
}
128+
129+
class TestDecodeSyseeprom(object):
130+
def test_print_eeprom_dict(self, capsys):
131+
132+
expected_output = '''\
133+
TlvInfo Header:
134+
Id String: TlvInfo
135+
Version: 1
136+
Total Length: 170
137+
TLV Name Code Len Value
138+
------------------- ------ ----- --------------------------
139+
Product Name 0X21 8 S6100-ON
140+
Part Number 0X22 6 0F6N2R
141+
Serial Number 0X23 20 TH0F6N2RCET0007600NG
142+
Base MAC Address 0X24 6 0C:29:EF:CF:AC:A0
143+
Manufacture Date 0X25 19 07/07/2020 15:05:34
144+
Device Version 0X26 1 1
145+
Label Revision 0X27 3 A08
146+
Platform Name 0X28 26 x86_64-dell_s6100_c2538-r0
147+
ONIE Version 0X29 8 3.15.1.0
148+
MAC Addresses 0X2A 2 384
149+
Manufacturer 0X2B 5 CET00
150+
Manufacture Country 0X2C 2 TH
151+
Vendor Name 0X2D 4 DELL
152+
Diag Version 0X2E 8 3.25.4.1
153+
Service Tag 0X2F 7 F3CD9Z2
154+
155+
(checksum valid)
156+
'''
157+
158+
decode_syseeprom.print_eeprom_dict(SAMPLE_TLV_DICT)
159+
captured = capsys.readouterr()
160+
assert captured.out == expected_output
161+
162+
def test_read_eeprom_from_db(self):
163+
tlv_dict = decode_syseeprom.read_eeprom_from_db()
164+
assert tlv_dict == SAMPLE_TLV_DICT
165+
166+
def test_get_tlv_value_from_db(self):
167+
value = decode_syseeprom.get_tlv_value_from_db(0x28)
168+
assert value == 'x86_64-dell_s6100_c2538-r0'
169+
170+
def test_print_mgmt_mac_db(self, capsys):
171+
decode_syseeprom.print_mgmt_mac(True)
172+
captured = capsys.readouterr()
173+
assert captured.out == '0C:29:EF:CF:AC:A0\n'
174+
175+
def test_print_serial(self, capsys):
176+
decode_syseeprom.print_serial(True)
177+
captured = capsys.readouterr()
178+
assert captured.out == 'TH0F6N2RCET0007600NG\n'
179+
180+
def test_print_model(self, capsys):
181+
decode_syseeprom.print_model(True)
182+
captured = capsys.readouterr()
183+
assert captured.out == 'S6100-ON\n'

0 commit comments

Comments
 (0)