Skip to content

Commit fe9900d

Browse files
keboliulguohan
authored andcommitted
[pytest]porting back changes from master to 201811 (#1145)
* Porting back pytest change from master to 201811 update device info to add more facts add log analyzer add check daemon status test add check interface status test add Mellanox check sfp presence test update reboot, config reload and sequential restart test update sfp test update check sysfs test update platform fixture * fix review comments Rebase to add some new master PR: #1130 [platform] Fix the reboot SONiC stuck issue #1120 [platform] Disable log analyzer for the reload and restart cases #1125 [pytest] Fix pytest conftest.py issue * update loganalyzer ignore log * [tests/platform/mellanox] check PSU state against sysfs on Mellanox devices (#1082) * [psu test case] check psu state against vendor specific info. for mellanox, check sysfs * [test_platform_info.py]handle "NOT PRESENT" in test_show_platform_psustatus * [psu testcase] address comments Conflicts: tests/platform/mellanox/check_sysfs.py * [check_sysfs] rewords. * [check_sysfs.py] rewording Conflicts: tests/platform/mellanox/check_sysfs.py * reduce redundant code and rename function * remove redundant code
1 parent 5a29d69 commit fe9900d

24 files changed

Lines changed: 1605 additions & 116 deletions

tests/common/devices.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"""
1010
import json
1111
import logging
12-
from multiprocessing import Process, Queue
12+
import os
13+
from multiprocessing.pool import ThreadPool
1314

1415
from errors import RunAnsibleModuleFail
1516
from errors import UnsupportedAnsibleModule
1617

18+
1719
class AnsibleHostBase(object):
1820
"""
1921
@summary: The base class for various objects.
@@ -44,13 +46,11 @@ def _run(self, *module_args, **complex_args):
4446
module_async = complex_args.pop('module_async', False)
4547

4648
if module_async:
47-
q = Queue()
48-
def run_module(queue, module_args, complex_args):
49-
res = self.module(*module_args, **complex_args)
50-
q.put(res[self.hostname])
51-
p = Process(target=run_module, args=(q, module_args, complex_args))
52-
p.start()
53-
return p, q
49+
def run_module(module_args, complex_args):
50+
return self.module(*module_args, **complex_args)[self.hostname]
51+
pool = ThreadPool()
52+
result = pool.apply_async(run_module, (module_args, complex_args))
53+
return pool, result
5454

5555
res = self.module(*module_args, **complex_args)[self.hostname]
5656
if res.is_failed and not module_ignore_errors:
@@ -149,7 +149,8 @@ def is_service_fully_started(self, service):
149149
return True
150150
else:
151151
return False
152-
except:
152+
except Exception as e:
153+
logging.error("Failed to get service status, exception: %s" % repr(e))
153154
return False
154155

155156
def critical_services_fully_started(self):
@@ -163,7 +164,6 @@ def critical_services_fully_started(self):
163164
logging.debug("Status of critical services: %s" % str(result))
164165
return all(result.values())
165166

166-
167167
def get_crm_resources(self):
168168
"""
169169
@summary: Run the "crm show resources all" command and parse its output
@@ -185,11 +185,32 @@ def get_crm_resources(self):
185185
fields = line.split()
186186
if len(fields) == 5:
187187
result["acl_resources"].append({"stage": fields[0], "bind_point": fields[1],
188-
"resource_name": fields[2], "used_count": int(fields[3]), "available_count": int(fields[4])})
188+
"resource_name": fields[2], "used_count": int(fields[3]),
189+
"available_count": int(fields[4])})
189190
if current_table == 3: # content of the third table, table resources
190191
fields = line.split()
191192
if len(fields) == 4:
192193
result["table_resources"].append({"table_id": fields[0], "resource_name": fields[1],
193-
"used_count": int(fields[2]), "available_count": int(fields[3])})
194+
"used_count": int(fields[2]), "available_count": int(fields[3])})
194195

195196
return result
197+
198+
def get_pmon_daemon_list(self):
199+
"""
200+
@summary: in 201811 use different way to get the pmon daemon list since
201+
config file (/usr/share/sonic/device/{platform}/{hwsku}/pmon_daemon_control.json) is not available.
202+
Check the availability of two plugins led_control.py and sfputil.py, they are for ledd and xcvrd.
203+
If one of them does not exist, then the related daemon is not expected to be running on this platform.
204+
"""
205+
daemon_list = []
206+
207+
led_plugin_path = os.path.join('/usr/share/sonic/device', self.facts["platform"], 'plugins/led_control.py')
208+
sfp_plugin_path = os.path.join('/usr/share/sonic/device', self.facts["platform"], 'plugins/sfputil.py')
209+
210+
if os.path.isfile(led_plugin_path):
211+
daemon_list.append('ledd')
212+
if os.path.isfile(sfp_plugin_path):
213+
daemon_list.append('xcvrd')
214+
215+
logging.info("Pmon daemon list for this platform is %s" % str(daemon_list))
216+
return daemon_list

tests/common/mellanox_data.py

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
"psus": {
1818
"number": 2,
1919
"hot_swappable": True
20+
},
21+
"cpu_pack": {
22+
"number": 1
23+
},
24+
"cpu_cores": {
25+
"number": 2
26+
},
27+
"ports": {
28+
"number": 32
2029
}
2130
},
2231
"ACS-MSN2740": {
@@ -32,6 +41,15 @@
3241
"psus": {
3342
"number": 2,
3443
"hot_swappable": True
44+
},
45+
"cpu_pack": {
46+
"number": 0
47+
},
48+
"cpu_cores": {
49+
"number": 4
50+
},
51+
"ports": {
52+
"number": 32
3553
}
3654
},
3755
"ACS-MSN2410": {
@@ -47,6 +65,15 @@
4765
"psus": {
4866
"number": 2,
4967
"hot_swappable": True
68+
},
69+
"cpu_pack": {
70+
"number": 1
71+
},
72+
"cpu_cores": {
73+
"number": 2
74+
},
75+
"ports": {
76+
"number": 56
5077
}
5178
},
5279
"ACS-MSN2010": {
@@ -62,6 +89,15 @@
6289
"psus": {
6390
"number": 2,
6491
"hot_swappable": False
92+
},
93+
"cpu_pack": {
94+
"number": 0
95+
},
96+
"cpu_cores": {
97+
"number": 4
98+
},
99+
"ports": {
100+
"number": 22
65101
}
66102
},
67103
"ACS-MSN2100": {
@@ -77,6 +113,15 @@
77113
"psus": {
78114
"number": 2,
79115
"hot_swappable": False
116+
},
117+
"cpu_pack": {
118+
"number": 0
119+
},
120+
"cpu_cores": {
121+
"number": 4
122+
},
123+
"ports": {
124+
"number": 16
80125
}
81126
},
82127
"ACS-MSN3800": {
@@ -92,6 +137,15 @@
92137
"psus": {
93138
"number": 2,
94139
"hot_swappable": True
140+
},
141+
"cpu_pack": {
142+
"number": 1
143+
},
144+
"cpu_cores": {
145+
"number": 4
146+
},
147+
"ports": {
148+
"number": 64
95149
}
96150
},
97151
"ACS-MSN3700": {
@@ -107,6 +161,15 @@
107161
"psus": {
108162
"number": 2,
109163
"hot_swappable": True
164+
},
165+
"cpu_pack": {
166+
"number": 1
167+
},
168+
"cpu_cores": {
169+
"number": 4
170+
},
171+
"ports": {
172+
"number": 32
110173
}
111174
},
112175
"ACS-MSN3700C": {
@@ -122,21 +185,15 @@
122185
"psus": {
123186
"number": 2,
124187
"hot_swappable": True
125-
}
126-
},
127-
"ACS-MSN3510": {
128-
"reboot": {
129-
"cold_reboot": True,
130-
"fast_reboot": True,
131-
"warm_reboot": False
132188
},
133-
"fans": {
134-
"number": 6,
135-
"hot_swappable": True
189+
"cpu_pack": {
190+
"number": 1
136191
},
137-
"psus": {
138-
"number": 2,
139-
"hot_swappable": True
192+
"cpu_cores": {
193+
"number": 2
194+
},
195+
"ports": {
196+
"number": 32
140197
}
141198
}
142199
}

tests/conftest.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
import ipaddr as ipaddress
88

99
from ansible_host import AnsibleHost
10+
from loganalyzer import LogAnalyzer
1011

1112
pytest_plugins = ('ptf_fixtures', 'ansible_fixtures')
1213

13-
# Add the tests folder to sys.path, for importing the lib package
14-
_current_file_dir = os.path.dirname(os.path.realpath(__file__))
15-
if _current_file_dir not in sys.path:
16-
sys.path.append(current_file_dir)
17-
1814

1915
class TestbedInfo(object):
2016
"""
@@ -35,7 +31,6 @@ def __init__(self, testbed_file):
3531
name = ''
3632
for key in line:
3733
if ('uniq-name' in key or 'conf-name' in key) and '#' in line[key]:
38-
### skip comment line
3934
continue
4035
elif 'uniq-name' in key or 'conf-name' in key:
4136
name = line[key]
@@ -52,6 +47,8 @@ def __init__(self, testbed_file):
5247
def pytest_addoption(parser):
5348
parser.addoption("--testbed", action="store", default=None, help="testbed name")
5449
parser.addoption("--testbed_file", action="store", default=None, help="testbed file name")
50+
parser.addoption("--disable_loganalyzer", action="store_true", default=False,
51+
help="disable loganalyzer analysis for 'loganalyzer' fixture")
5552

5653

5754
@pytest.fixture(scope="session")
@@ -78,11 +75,12 @@ def testbed_devices(ansible_adhoc, testbed):
7875
@param testbed: Fixture for parsing testbed configuration file.
7976
@return: Return the created device objects in a dictionary
8077
"""
81-
from common.devices import SonicHost, Localhost
78+
from common.devices import SonicHost, Localhost, PTFHost
79+
80+
devices = {
81+
"localhost": Localhost(ansible_adhoc),
82+
"dut": SonicHost(ansible_adhoc, testbed["dut"], gather_facts=True)}
8283

83-
devices = {}
84-
devices["localhost"] = Localhost(ansible_adhoc)
85-
devices["dut"] = SonicHost(ansible_adhoc, testbed["dut"], gather_facts=True)
8684
if "ptf" in testbed:
8785
devices["ptf"] = PTFHost(ansible_adhoc, testbed["ptf"])
8886

@@ -121,3 +119,21 @@ def eos():
121119
with open('eos/eos.yml') as stream:
122120
eos = yaml.safe_load(stream)
123121
return eos
122+
123+
124+
@pytest.fixture(autouse=True)
125+
def loganalyzer(duthost, request):
126+
loganalyzer = LogAnalyzer(ansible_host=duthost, marker_prefix=request.node.name)
127+
# Add start marker into DUT syslog
128+
marker = loganalyzer.init()
129+
yield loganalyzer
130+
if not request.config.getoption("--disable_loganalyzer") and "disable_loganalyzer" not in request.keywords:
131+
# Read existed common regular expressions located with legacy loganalyzer module
132+
loganalyzer.load_common_config()
133+
# Parse syslog and process result. Raise "LogAnalyzerError" exception if: total match or expected missing
134+
# match is not equal to zero
135+
loganalyzer.analyze(marker)
136+
else:
137+
# Add end marker into DUT syslog
138+
loganalyzer._add_end_marker(marker)
139+

0 commit comments

Comments
 (0)