Skip to content

Commit 9356613

Browse files
authored
[devices] Clean up platform/os version info for SonicHosts (#1732)
- Deprecate get_asic_type and get_platform_info - Add facts and os_version properties to SonicHost - Clarify documentation Signed-off-by: Danny Allen <[email protected]>
1 parent 30b29ed commit 9356613

7 files changed

Lines changed: 90 additions & 60 deletions

File tree

tests/common/broadcom_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
def is_broadcom_device(dut):
2-
return dut.get_asic_type() == "broadcom"
2+
return dut.facts["asic_type"] == "broadcom"

tests/common/devices.py

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -102,58 +102,103 @@ def __init__(self, ansible_adhoc, hostname):
102102

103103
class SonicHost(AnsibleHostBase):
104104
"""
105-
@summary: Class for SONiC switch
105+
A remote host running SONiC.
106106
107-
For running ansible module on the SONiC switch
107+
This type of host contains information about the SONiC device (device info, services, etc.),
108+
and also provides the ability to run Ansible modules on the SONiC device.
108109
"""
110+
111+
# TODO: Because people are editing this variable in a bunch of places it should probably be
112+
# revised to "DEFAULT_CRITICAL_SERVICES", and we should make critical_services a property of
113+
# SonicHost
109114
CRITICAL_SERVICES = ["swss", "syncd", "database", "teamd", "bgp", "pmon", "lldp", "snmp"]
110115

111-
def __init__(self, ansible_adhoc, hostname, gather_facts=False):
116+
def __init__(self, ansible_adhoc, hostname):
112117
AnsibleHostBase.__init__(self, ansible_adhoc, hostname)
113-
if gather_facts:
114-
self.gather_facts()
118+
self._facts = self._gather_facts()
119+
self._os_version = self._get_os_version()
120+
121+
if self.facts["num_npu"] > 1:
122+
self._update_critical_services_for_multi_npu()
115123

116-
def _get_critical_services_for_multi_npu(self):
124+
@property
125+
def facts(self):
117126
"""
118-
Update the critical_services with the service names for multi-npu platforms
127+
Platform information for this SONiC device.
128+
129+
Returns:
130+
dict: A dictionary containing the device platform information.
131+
132+
For example:
133+
{
134+
"platform": "x86_64-arista_7050_qx32s",
135+
"hwsku": "Arista-7050-QX-32S",
136+
"asic_type": "broadcom",
137+
"num_npu": 1
138+
}
119139
"""
120-
m_service = []
121-
for service in self.CRITICAL_SERVICES:
122-
for npu in self.facts["num_npu"]:
123-
npu_service = service+npu
124-
m_service.insert(npu, npu_service)
125-
self.CRITICAL_SERVICES = m_service
126-
print self.CRITICAL_SERVICES
127140

128-
def _get_npu_info(self):
141+
return self._facts
142+
143+
@property
144+
def os_version(self):
145+
"""
146+
The OS version running on this SONiC device.
147+
148+
Returns:
149+
str: The SONiC OS version (e.g. "20181130.31")
150+
"""
151+
152+
return self._os_version
153+
154+
def _gather_facts(self):
155+
"""
156+
Gather facts about the platform for this SONiC device.
157+
"""
158+
159+
facts = dict()
160+
facts.update(self._get_platform_info())
161+
facts["num_npu"] = self._get_npu_count(facts["platform"])
162+
163+
logging.debug("Gathered SonicHost facts: %s" % json.dumps(facts))
164+
return facts
165+
166+
def _get_npu_count(self, platform):
129167
"""
130-
Check if the DUT is multi-npu platfrom and store the number of npus in the facts
168+
Gets the number of npus for this device.
131169
"""
132-
asic_conf_file_path = os.path.join('/usr/share/sonic/device', self.facts["platform"], 'asic.conf')
170+
171+
asic_conf_file_path = os.path.join("/usr/share/sonic/device", platform, "asic.conf")
133172
try:
134-
output = self.shell('cat %s' % asic_conf_file_path)["stdout_lines"]
135-
print output
173+
output = self.shell("cat {}".format(asic_conf_file_path))["stdout_lines"]
174+
logging.debug(output)
175+
136176
for line in output:
137-
num_npu=line.split("=",1)[1].strip()
138-
print "num_npu = {}".format(num_npu)
139-
self.facts["num_npu"] = int(num_npu)
177+
num_npu = line.split("=", 1)[1].strip()
178+
179+
logging.debug("num_npu = %s" % num_npu)
180+
return int(num_npu)
140181
except:
141-
self.facts["num_npu"] =1
182+
return 1
142183

143-
if self.facts["num_npu"] > 1:
144-
self._get_critical_services_for_multi_npu
184+
def _update_critical_services_for_multi_npu(self):
185+
"""
186+
Updates the critical services for this device with the services for multi-npu platforms.
187+
"""
145188

189+
m_service = []
190+
for service in self.CRITICAL_SERVICES:
191+
for npu in self.facts["num_npu"]:
192+
npu_service = service + npu
193+
m_service.insert(npu, npu_service)
194+
self.CRITICAL_SERVICES = m_service
195+
logging.debug(self.CRITICAL_SERVICES)
146196

147-
def get_platform_info(self):
197+
def _get_platform_info(self):
148198
"""
149-
@summary: Get the platform information of the SONiC switch.
150-
@return: Returns a dictionary containing preperties of the platform information, for example:
151-
{
152-
"platform": "",
153-
"hwsku": "",
154-
"asic_type": ""
155-
}
199+
Gets platform information about this SONiC device.
156200
"""
201+
157202
platform_info = self.command("show platform summary")["stdout_lines"]
158203
result = {}
159204
for line in platform_info:
@@ -165,15 +210,13 @@ def get_platform_info(self):
165210
result["asic_type"] = line.split(":")[1].strip()
166211
return result
167212

168-
def gather_facts(self):
213+
def _get_os_version(self):
169214
"""
170-
@summary: Gather facts of the SONiC switch and store the gathered facts in the dict type 'facts' attribute.
215+
Gets the SONiC OS version that is running on this device.
171216
"""
172-
self.facts = {}
173-
platform_info = self.get_platform_info()
174-
self.facts.update(platform_info)
175-
self._get_npu_info()
176-
logging.debug("SonicHost facts: %s" % json.dumps(self.facts))
217+
218+
output = self.command("sonic-cfggen -y /etc/sonic/sonic_version.yml -v build_version")
219+
return output["stdout_lines"][0].strip()
177220

178221
def get_service_props(self, service, props=["ActiveState", "SubState"]):
179222
"""
@@ -424,9 +467,6 @@ def get_image_info(self):
424467
ret['installed_list'] = images
425468
return ret
426469

427-
def get_asic_type(self):
428-
return self.facts["asic_type"]
429-
430470
def shutdown(self, ifname):
431471
"""
432472
Shutdown interface specified by ifname
@@ -557,16 +597,6 @@ def check_bgp_session_nsf(self, neighbor_ip):
557597
return True
558598
return False
559599

560-
def get_version(self):
561-
"""
562-
Gets the SONiC version this device is running.
563-
564-
Returns:
565-
str: the firmware version number (e.g. 20181130.31)
566-
"""
567-
output = dut.command("sonic-cfggen -y /etc/sonic/sonic_version.yml -v build_version")
568-
return output["stdout_lines"][0].strip()
569-
570600
class EosHost(AnsibleHostBase):
571601
"""
572602
@summary: Class for Eos switch

tests/common/mellanox_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,4 @@
552552
}
553553

554554
def is_mellanox_device(dut):
555-
return dut.get_asic_type() == "mellanox"
555+
return dut.facts["asic_type"] == "mellanox"

tests/common/system_utils/docker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def swap_syncd(dut):
130130
elif is_mellanox_device(dut):
131131
vendor_id = "mlnx"
132132
else:
133-
error_message = "\"{}\" is not currently supported".format(dut.get_asic_type())
133+
error_message = "\"{}\" is not currently supported".format(dut.facts["asic_type"])
134134
_LOGGER.error(error_message)
135135
raise ValueError(error_message)
136136

@@ -176,7 +176,7 @@ def restore_default_syncd(dut):
176176
elif is_mellanox_device(dut):
177177
vendor_id = "mlnx"
178178
else:
179-
error_message = "\"{}\" is not currently supported".format(dut.get_asic_type())
179+
error_message = "\"{}\" is not currently supported".format(dut.facts["asic_type"])
180180
_LOGGER.error(error_message)
181181
raise ValueError(error_message)
182182

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def duthost(ansible_adhoc, testbed, request):
214214
dut_index = getattr(request.module, "dut_index", 0)
215215
assert dut_index < len(testbed["duts"]), "DUT index '{0}' is out of bound '{1}'".format(dut_index, len(testbed["duts"]))
216216

217-
duthost = SonicHost(ansible_adhoc, testbed["duts"][dut_index], gather_facts=True)
217+
duthost = SonicHost(ansible_adhoc, testbed["duts"][dut_index])
218218
if stop_ssh_timeout is not None:
219219
disable_ssh_timout(duthost)
220220

tests/copp/test_copp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_policer(self, protocol, duthost, ptfhost, _copp_testbed):
6262

6363
if protocol == "ARP" \
6464
and is_broadcom_device(duthost) \
65-
and "201811" not in duthost.get_version():
65+
and "201811" not in duthost.os_version:
6666
pytest.xfail("ARP policy disabled on BRCM devices due to SAI bug")
6767

6868
if protocol in ["IP2ME", "SNMP", "SSH"] and _copp_testbed.topo == "t1-lag":

tests/platform_tests/test_sensors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def test_sensors(duthost, creds):
77
# Get platform name
8-
platform = duthost.get_platform_info()['platform']
8+
platform = duthost.facts['platform']
99

1010
# Prepare check list
1111
sensors_checks = creds['sensors_checks']

0 commit comments

Comments
 (0)