-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtestrunner.py
More file actions
308 lines (275 loc) · 12.1 KB
/
testrunner.py
File metadata and controls
308 lines (275 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright 2020-2021 Axis Communications AB.
#
# For a full list of individual contributors, please see the commit history.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""ETR test runner module."""
import json
import time
import os
import logging
from pprint import pprint
from typing import Union
from eiffellib.events import EiffelTestSuiteStartedEvent
from etos_test_runner.lib.iut_monitoring import IutMonitoring
from etos_test_runner.lib.executor import Executor
from etos_test_runner.lib.workspace import Workspace
from etos_test_runner.lib.log_area import LogArea
from etos_test_runner.lib.verdict import CustomVerdictMatcher
class TestRunner:
"""Test runner for ETOS."""
# pylint: disable=too-many-instance-attributes
logger = logging.getLogger("ETR")
def __init__(self, iut, etos):
"""Initialize.
:param iut: IUT to execute tests on.
:type iut: :obj:`etr.lib.iut.Iut`
:param etos: ETOS library
:type etos: :obj:`etos_lib.etos.ETOS`
"""
self.etos = etos
self.iut = iut
self.config = self.etos.config.get("test_config")
self.log_area = LogArea(self.etos)
self.iut_monitoring = IutMonitoring(self.iut, self.etos)
self.issuer = {"name": "ETOS Test Runner"}
self.etos.config.set("iut", self.iut)
self.plugins = self.etos.config.get("plugins")
verdict_rule_file = os.getenv("VERDICT_RULES_FILE")
if verdict_rule_file is not None:
with open(verdict_rule_file, "r", encoding="utf-8") as inp:
rules = json.load(inp)
else:
rules = []
self.verdict_matcher = CustomVerdictMatcher(rules)
def test_suite_started(self):
"""Publish a test suite started event.
:return: Reference to test suite started.
:rtype: :obj:`eiffel.events.base_event.BaseEvent`
"""
suite_name = self.config.get("name")
categories = ["Regression test_suite", "Sub suite"]
categories.append(self.iut.identity.name)
livelogs = self.config.get("log_area", {}).get("livelogs")
test_suite_started = EiffelTestSuiteStartedEvent()
data = {
"name": suite_name,
"categories": categories,
"types": ["FUNCTIONAL"],
"liveLogs": [{"name": "console", "uri": livelogs}],
}
# TODO: Remove CONTEXT link here.
links = {
"CONTEXT": self.etos.config.get("context"),
"CAUSE": self.etos.config.get("main_suite_id"),
}
test_suite_started.meta.event_id = self.config.get("sub_suite_id")
return self.etos.events.send(test_suite_started, links, data)
def environment(self, context):
"""Send out which environment we're executing within.
:param context: Context where this environment is used.
:type context: str
"""
# TODO: Get this from prepare
if os.getenv("HOSTNAME") is not None:
self.etos.events.send_environment_defined(
"ETR Hostname",
links={"CONTEXT": context},
host={"name": os.getenv("HOSTNAME"), "user": "etos"},
)
if os.getenv("EXECUTION_SPACE_URL") is not None:
self.etos.events.send_environment_defined(
"Execution Space URL",
links={"CONTEXT": context},
host={"name": os.getenv("EXECUTION_SPACE_URL"), "user": "etos"},
)
def run_tests(self, workspace: Workspace) -> tuple[bool, list[Union[int, None]]]:
"""Execute test recipes within a test executor.
:param workspace: Which workspace to execute test suite within.
:type workspace: :obj:`etr.lib.workspace.Workspace`
:return: Result of test execution.
:rtype: bool
"""
recipes = self.config.get("recipes")
result = True
test_framework_exit_codes = []
for num, test in enumerate(recipes):
self.logger.info("Executing test %s/%s", num + 1, len(recipes))
with Executor(test, self.iut, self.etos) as executor:
self.logger.info("Starting test '%s'", executor.test_name)
executor.execute(workspace)
if not executor.result:
result = executor.result
self.logger.info(
"Test finished. Result: %s. Test framework exit code: %d",
executor.result,
executor.returncode,
)
test_framework_exit_codes.append(executor.returncode)
return result, test_framework_exit_codes
def outcome(
self,
result: bool,
executed: bool,
description: str,
test_framework_exit_codes: list[Union[int, None]],
) -> dict:
"""Get outcome from test execution.
:param result: Result of execution.
:type result: bool
:param executed: Whether or not tests have successfully executed.
:type executed: bool
:param description: Optional description.
:type description: str
:return: Outcome of test execution.
:rtype: dict
"""
test_framework_output = {
"test_framework_exit_codes": test_framework_exit_codes,
}
custom_verdict = self.verdict_matcher.evaluate(test_framework_output)
if custom_verdict is not None:
conclusion = custom_verdict["conclusion"]
verdict = custom_verdict["verdict"]
description = custom_verdict["description"]
self.logger.info("Verdict matches testrunner verdict rule: %s", custom_verdict)
elif executed:
conclusion = "SUCCESSFUL"
verdict = "PASSED" if result else "FAILED"
self.logger.info(
"Tests executed successfully. Verdict set to '%s' due to result being '%s'",
verdict,
result,
)
else:
conclusion = "FAILED"
verdict = "INCONCLUSIVE"
self.logger.info(
"Tests did not execute successfully. Setting verdict to '%s'",
verdict,
)
suite_name = self.config.get("name")
if not description and not result:
self.logger.info("No description but result is a failure. At least some tests failed.")
description = f"At least some {suite_name} tests failed."
elif not description and result:
self.logger.info(
"No description and result is a success. All tests executed successfully."
)
description = f"All {suite_name} tests completed successfully."
else:
self.logger.info("Description was set. Probably due to an exception.")
return {
"verdict": verdict,
"description": description,
"conclusion": conclusion,
}
def _test_suite_triggered(self, name):
"""Call on_test_suite_triggered for all ETR plugins.
:param name: Name of test suite that triggered.
:type name: str
"""
for plugin in self.plugins:
plugin.on_test_suite_triggered(name)
def _test_suite_started(self, test_suite_started):
"""Call on_test_suite_started for all ETR plugins.
:param test_suite_started: The test suite started event
:type test_suite_started: :obj:`eiffellib.events.EiffelTestSuiteStartedEvent`
"""
for plugin in self.plugins:
plugin.on_test_suite_started(test_suite_started)
def _test_suite_finished(self, name, outcome):
"""Call on_test_suite_finished for all ETR plugins.
:param name: Name of test suite that finished.
:type name: str
:param outcome: Outcome of test suite execution.
:type outcome: dict
"""
for plugin in self.plugins:
plugin.on_test_suite_finished(name, outcome)
def execute(self): # pylint:disable=too-many-branches,disable=too-many-statements
"""Execute all tests in test suite.
:return: Result of execution. Linux exit code.
:rtype: int
"""
self._test_suite_triggered(self.config.get("name"))
self.logger.info("Send test suite started event.")
test_suite_started = self.test_suite_started()
self._test_suite_started(test_suite_started)
sub_suite_id = test_suite_started.meta.event_id
self.logger.info("Send test environment events.")
self.environment(sub_suite_id)
self.etos.config.set("sub_suite_id", sub_suite_id)
result = True
description = None
executed = False
test_framework_exit_codes = []
outcome = None
try:
with Workspace(self.log_area) as workspace:
try:
self.logger.info("Start IUT monitoring.")
self.iut_monitoring.start_monitoring()
self.logger.info("Starting test executor.")
result, test_framework_exit_codes = self.run_tests(workspace)
executed = True
self.logger.info("Stop IUT monitoring.")
self.iut_monitoring.stop_monitoring()
except Exception as exception: # pylint:disable=broad-except
result = False
executed = False
description = str(exception)
raise
finally:
if self.iut_monitoring.monitoring:
self.logger.info("Stop IUT monitoring.")
self.iut_monitoring.stop_monitoring()
self.logger.info("Figure out test outcome.")
outcome = self.outcome(result, executed, description, test_framework_exit_codes)
pprint(outcome)
self.logger.info("Call on_test_suite_finished plugin handlers.")
self._test_suite_finished(self.config.get("name"), outcome)
finally:
if outcome is None:
self.logger.info("Figure out test outcome.")
outcome = self.outcome(result, executed, description, test_framework_exit_codes)
pprint(outcome)
self.logger.info("Send test suite finished event.")
test_suite_finished = self.etos.events.send_test_suite_finished(
test_suite_started,
links={"CONTEXT": self.etos.config.get("context")},
outcome=outcome,
persistentLogs=self.log_area.persistent_logs,
)
self.logger.info(test_suite_finished.pretty)
timeout = time.time() + 600 # 10 minutes
self.logger.info("Waiting for eiffel publisher to deliver events (600s).")
previous = 0
# pylint:disable=protected-access
current = len(self.etos.publisher._deliveries)
while current:
current = len(self.etos.publisher._deliveries)
self.logger.info("Remaining events to send : %d", current)
self.logger.info("Events sent since last iteration: %d", previous - current)
if time.time() > timeout:
if current < previous:
self.logger.info(
"Timeout reached, but events are still being sent. Increase timeout by 10s."
)
timeout = time.time() + 10
else:
raise TimeoutError("Eiffel publisher did not deliver all eiffel events.")
previous = current
time.sleep(1)
self.logger.info("Tests finished executing.")
return 0 if result else outcome