Skip to content

Commit a36af82

Browse files
LukaszMrugalacarlescufi
authored andcommitted
scripts: tests: Twister test expansion - quarantine
Here we achieve 100% coverage for the quarantine module, thanks to unit tests for QuarantineElement and QuarantineData dataclasses. Implemented PR suggestions of gchwier. Signed-off-by: Lukasz Mrugala <[email protected]>
1 parent 913920a commit a36af82

File tree

1 file changed

+275
-0
lines changed

1 file changed

+275
-0
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2023 Intel Corporation
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
"""
6+
Tests for quarantine.py classes' methods
7+
"""
8+
9+
import mock
10+
import os
11+
import pytest
12+
import textwrap
13+
14+
from twisterlib.quarantine import QuarantineException, \
15+
QuarantineElement, \
16+
QuarantineData
17+
18+
19+
TESTDATA_1 = [
20+
(
21+
['dummy scenario', 'another scenario'],
22+
['dummy platform', 'another platform'],
23+
['dummy architecture', 'another architecture'],
24+
['dummy simulation', 'another simulation'],
25+
None,
26+
[]
27+
),
28+
(
29+
['all'],
30+
['dummy platform', 'another platform'],
31+
['dummy architecture', 'another architecture'],
32+
['dummy simulation', 'another simulation'],
33+
None,
34+
['scenarios']
35+
),
36+
(
37+
['dummy scenario', 'another scenario'],
38+
['dummy platform', 'all'],
39+
['all', 'another architecture'],
40+
['dummy simulation', 'another simulation'],
41+
None,
42+
['platforms', 'architectures']
43+
),
44+
(
45+
['all', 'another scenario'],
46+
[],
47+
[],
48+
['all', 'all'],
49+
QuarantineException,
50+
['scenarios', 'platforms', 'architectures', 'simulations']
51+
),
52+
]
53+
54+
55+
@pytest.mark.parametrize(
56+
'scenarios, platforms, architectures, ' \
57+
'simulations, expected_exception, empty_filter_attrs',
58+
TESTDATA_1,
59+
ids=[
60+
'no empties',
61+
'all scenarios',
62+
'all platforms and architectures',
63+
'exception'
64+
]
65+
)
66+
def test_quarantineelement_post_init(
67+
scenarios,
68+
platforms,
69+
architectures,
70+
simulations,
71+
expected_exception,
72+
empty_filter_attrs
73+
):
74+
if expected_exception:
75+
with pytest.raises(expected_exception):
76+
quarantine_element = QuarantineElement(
77+
scenarios=scenarios,
78+
platforms=platforms,
79+
architectures=architectures,
80+
simulations=simulations
81+
)
82+
else:
83+
quarantine_element = QuarantineElement(
84+
scenarios=scenarios,
85+
platforms=platforms,
86+
architectures=architectures,
87+
simulations=simulations
88+
)
89+
90+
for attr in ['scenarios', 'platforms', 'architectures', 'simulations']:
91+
if attr in empty_filter_attrs:
92+
assert getattr(quarantine_element, attr) == []
93+
else:
94+
assert getattr(quarantine_element, attr) != []
95+
96+
97+
def test_quarantinedata_post_init():
98+
quarantine_element_dict = {
99+
'scenarios': ['all'],
100+
'platforms': ['dummy platform'],
101+
'architectures': [],
102+
'simulations': ['dummy simulation', 'another simulation']
103+
}
104+
105+
quarantine_element = QuarantineElement(
106+
platforms=['dummy platform'],
107+
architectures=[]
108+
)
109+
quarantine_element.scenarios = []
110+
quarantine_element.simulations = ['dummy simulation', 'another simulation']
111+
112+
quarantine_data_qlist = [quarantine_element, quarantine_element_dict]
113+
114+
quarantine_data = QuarantineData(quarantine_data_qlist)
115+
116+
assert quarantine_data.qlist[0] == quarantine_data.qlist[1]
117+
118+
119+
TESTDATA_2 = [
120+
(
121+
'',
122+
QuarantineData()
123+
),
124+
(
125+
textwrap.dedent("""
126+
[
127+
{
128+
\"scenarios\": [\"all\"],
129+
\"platforms\": [\"dummy platform\"],
130+
\"architectures\": [],
131+
\"simulations\": [\"dummy simulation\", \"another simulation\"]
132+
}
133+
]
134+
"""),
135+
QuarantineData(
136+
[
137+
QuarantineElement(
138+
scenarios=[],
139+
platforms=['dummy platform'],
140+
architectures=[],
141+
simulations=['dummy simulation', 'another simulation']
142+
)
143+
]
144+
)
145+
),
146+
(
147+
textwrap.dedent("""
148+
[
149+
{
150+
\"I\": [\"am\"],
151+
\"not\": \"a\",
152+
\"valid\": [],
153+
\"JSON\": [\"for\", \"this\"]
154+
}
155+
]
156+
"""),
157+
QuarantineException
158+
)
159+
]
160+
161+
162+
@pytest.mark.parametrize(
163+
'file_contents, expected',
164+
TESTDATA_2,
165+
ids=['empty', 'valid', 'not valid']
166+
)
167+
def test_quarantinedata_load_data_from_yaml(file_contents, expected):
168+
with mock.patch('builtins.open', mock.mock_open(read_data=file_contents)):
169+
if isinstance(expected, type) and issubclass(expected, Exception):
170+
with pytest.raises(expected):
171+
res = QuarantineData.load_data_from_yaml(
172+
os.path.join('dummy', 'path')
173+
)
174+
else:
175+
res = QuarantineData.load_data_from_yaml(
176+
os.path.join('dummy', 'path')
177+
)
178+
179+
assert res == expected
180+
181+
182+
TESTDATA_3 = [
183+
(
184+
'good scenario',
185+
'good platform',
186+
'good arch',
187+
'good sim',
188+
None
189+
),
190+
(
191+
'good scenario',
192+
'very bad dummy platform',
193+
'good arch',
194+
'good sim',
195+
0
196+
),
197+
(
198+
'bad scenario 1',
199+
'good platform',
200+
'good arch',
201+
'bad sim',
202+
1
203+
),
204+
(
205+
'bad scenario 1',
206+
'good platform',
207+
'good arch',
208+
'sim for scenario 1',
209+
None
210+
),
211+
(
212+
'good scenario',
213+
'good platform',
214+
'unsupported arch 1',
215+
'good sim',
216+
2
217+
)
218+
]
219+
220+
221+
@pytest.mark.parametrize(
222+
'scenario, platform, architecture, simulation, expected_idx',
223+
TESTDATA_3,
224+
ids=[
225+
'not quarantined',
226+
'quarantined platform',
227+
'quarantined scenario with sim',
228+
'not quarantined with bad scenario',
229+
'quarantined arch'
230+
]
231+
)
232+
def test_quarantinedata_get_matched_quarantine(
233+
scenario,
234+
platform,
235+
architecture,
236+
simulation,
237+
expected_idx
238+
):
239+
qlist = [
240+
QuarantineElement(
241+
scenarios=['all'],
242+
platforms=['very bad dummy platform'],
243+
architectures=['all'],
244+
simulations=['all']
245+
),
246+
QuarantineElement(
247+
scenarios=['bad scenario 1', 'bad scenario 2'],
248+
platforms=['all'],
249+
architectures=['all'],
250+
simulations=['bad sim']
251+
),
252+
QuarantineElement(
253+
scenarios=['all'],
254+
platforms=['all'],
255+
architectures=['unsupported arch 1'],
256+
simulations=['all']
257+
),
258+
]
259+
260+
quarantine_data = QuarantineData(qlist)
261+
262+
if expected_idx is None:
263+
assert quarantine_data.get_matched_quarantine(
264+
scenario=scenario,
265+
platform=platform,
266+
architecture=architecture,
267+
simulation=simulation
268+
) is None
269+
else:
270+
assert quarantine_data.get_matched_quarantine(
271+
scenario=scenario,
272+
platform=platform,
273+
architecture=architecture,
274+
simulation=simulation
275+
) == qlist[expected_idx]

0 commit comments

Comments
 (0)