-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconftest.py
368 lines (300 loc) · 10.6 KB
/
conftest.py
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import argparse
import datetime
import pytest
from _pytest.doctest import DoctestItem
from _pytest.mark import Mark, MarkDecorator
from cache import patch_cache_set
from aws.client import BotocoreClient
from gcp.client import GCPClient
from gsuite.client import GsuiteClient
import custom_config
botocore_client = None
gcp_client = None
gsuite_client = None
custom_config_global = None
def pytest_addoption(parser):
frost_parser = parser.getgroup("Frost", "Frost's custom arguments")
frost_parser.addoption(
"--aws-profiles",
nargs="*",
help="Set default AWS profiles to use. Defaults to the current AWS profile i.e. [None].",
)
frost_parser.addoption(
"--aws-regions",
type=str,
help="Set AWS regions to use as a comma separate list. Defaults to all available AWS regions",
)
frost_parser.addoption(
"--gcp-project-id", type=str, help="Set GCP project to test.",
)
frost_parser.addoption(
"--gcp-folder-id",
type=str,
help="Set GCP folder to test. Will test all projects under this folder.",
)
# While only used for Heroku at the moment, GitHub tests are soon to be
# added, which will also need an "organization" option. Current plan is to
# reuse this one.
frost_parser.addoption(
"--organization",
type=str,
help="Set organization to test. Used for Heroku tests.",
)
frost_parser.addoption(
"--debug-calls", action="store_true", help="Log API calls. Requires -s"
)
frost_parser.addoption(
"--debug-cache",
action="store_true",
help="Log whether API calls hit the cache. Requires -s",
)
frost_parser.addoption(
"--offline",
action="store_true",
default=False,
help="Instruct service clients to return empty lists and not make HTTP requests.",
)
frost_parser.addoption(
"--config", type=argparse.FileType("r"), help="Path to the config file."
)
def pytest_configure(config):
global botocore_client
global gcp_client
global gsuite_client
global custom_config_global
# run with -p 'no:cacheprovider'
cache = config.cache if hasattr(config, "cache") else None
if cache:
# monkeypatch cache.set to serialize datetime.datetime's
patch_cache_set(config)
profiles = config.getoption("--aws-profiles")
aws_regions = (
config.getoption("--aws-regions").split(",")
if config.getoption("--aws-regions")
else []
)
project_id = config.getoption("--gcp-project-id")
folder_id = config.getoption("--gcp-folder-id")
if project_id is not None and folder_id is not None:
raise Exception(
"--gcp-project-id and --gcp-folder-id are mutually exclusive arguments"
)
organization = config.getoption("--organization")
botocore_client = BotocoreClient(
profiles=profiles,
regions=aws_regions,
cache=cache,
debug_calls=config.getoption("--debug-calls"),
debug_cache=config.getoption("--debug-cache"),
offline=config.getoption("--offline"),
)
gcp_client = GCPClient(
project_id=project_id,
folder_id=folder_id,
cache=cache,
debug_calls=config.getoption("--debug-calls"),
debug_cache=config.getoption("--debug-cache"),
offline=config.getoption("--offline"),
)
custom_config_global = custom_config.CustomConfig(config.getoption("--config"))
config.custom_config = custom_config_global
try:
if any(x for x in config.args if "gsuite" in x):
gsuite_client = GsuiteClient(
domain=config.custom_config.gsuite.domain,
offline=config.getoption("--offline"),
)
else:
gsuite_client = GsuiteClient(domain="", offline=True)
except AttributeError as e:
gsuite_client = GsuiteClient(domain="", offline=True)
# register custom marker for rationale (used in report)
config.addinivalue_line(
"markers",
"rationale(reason): (optional) rationale behind the test. (null if not set)",
)
@pytest.fixture
def aws_config(pytestconfig):
return pytestconfig.custom_config.aws
@pytest.fixture
def gcp_config(pytestconfig):
return pytestconfig.custom_config.gcp
def pytest_runtest_setup(item):
"""
Add custom markers to pytest tests.
"""
if not isinstance(item, DoctestItem):
item.config.custom_config.add_markers(item)
# Reporting
def get_node_markers(node):
return [m for m in node.iter_markers()]
# METADATA_KEYS are modified by services to specify which metadata is
# relevant for the JSON output. It's unlikely that duplicate keys are
# intended, so failfast
# adapted from
# https://stackoverflow.com/questions/41281346/how-to-raise-error-if-user-tries-to-enter-duplicate-entries-in-a-set-in-python/41281734#41281734
class DuplicateKeyError(Exception):
pass
class SingleSet(set):
"""Set only allowing values to be added once
When addition of a duplicate value is detected, the `DuplicateKeyError`
exception will be raised, all non duplicate values are added to the set.
Raises:
DuplicateKeyError - when adding a value already in the set
>>> ss = SingleSet({1, 2, 3, 4})
>>> ss.add(3)
Traceback (most recent call last):
...
conftest.DuplicateKeyError: Value 3 already present
>>> ss.update({4, 5, 6, 3})
Traceback (most recent call last):
...
conftest.DuplicateKeyError: Value(s) {3, 4} already present
>>> ss
SingleSet({1, 2, 3, 4, 5, 6})
>>>
**NB:**
- duplicate values on initialization are not detected
>>> ss = SingleSet({1, 2, 3, 4, 3, 2, 1})
>>> ss
SingleSet({1, 2, 3, 4})
"""
def add(self, value):
if value in self:
raise DuplicateKeyError("Value {!r} already present".format(value))
super().add(value)
def update(self, values):
error_values = set()
for value in values:
if value in self:
error_values.add(value)
if error_values:
# we want the non-duplicate values added
super().update(values - error_values)
raise DuplicateKeyError(
"Value(s) {!r} already present".format(error_values)
)
super().update(values)
METADATA_KEYS: SingleSet = SingleSet(
[
"DBInstanceArn",
"DBInstanceIdentifier",
"GroupId",
"ImageId",
"InstanceId",
"LaunchTime",
"OwnerId",
"TagList",
"Tags",
"UserName",
"VolumeId",
"VpcId",
"__pytest_meta",
"displayName",
"id",
"kind",
"members",
"name",
"project",
"projectId",
"role",
"uniqueId",
]
)
def serialize_datetimes(obj):
"""Serializes datetimes to ISO format strings.
Used on report test_metadata since pytest-json doesn't let us pass
options to the serializer.
>>> from datetime import datetime
>>> serialize_datetimes({datetime(2000, 1, 1): -1})
{'2000-01-01T00:00:00': -1}
>>> serialize_datetimes({'foo': datetime(2000, 1, 1)})
{'foo': '2000-01-01T00:00:00'}
"""
if isinstance(obj, datetime.datetime):
return obj.isoformat()
elif isinstance(obj, list):
return [serialize_datetimes(item) for item in obj]
elif isinstance(obj, dict):
new_obj = {}
for k, v in obj.items():
new_obj[serialize_datetimes(k)] = serialize_datetimes(v)
return new_obj
return obj
def extract_metadata(resource):
return {
metadata_key: resource[metadata_key]
for metadata_key in METADATA_KEYS
if metadata_key in resource
}
def get_metadata_from_funcargs(funcargs):
metadata = {}
for k in funcargs:
if isinstance(funcargs[k], dict):
metadata = {**metadata, **extract_metadata(funcargs[k])}
return metadata
def serialize_marker(marker):
if isinstance(marker, (MarkDecorator, Mark)):
args = ["...skipped..."] if marker.name == "parametrize" else marker.args
kwargs = ["...skipped..."] if marker.name == "parametrize" else marker.kwargs
return {"name": marker.name, "args": args, "kwargs": kwargs}
else:
raise NotImplementedError("Unexpected Marker type %s" % repr(marker))
def get_outcome_and_reason(report, markers, call):
xfail = "xfail" in markers
xpass = report.passed and xfail
if (
call.excinfo
and not isinstance(call.excinfo, AssertionError)
and isinstance(call.excinfo, Exception)
):
return "errored", call.excinfo
elif xpass:
return "xpassed", markers["xfail"]["kwargs"].get("reason", None)
elif xfail:
return "xfailed", markers["xfail"]["kwargs"].get("reason", None)
else:
return report.outcome, None # passed, failed, skipped
def clean_docstring(docstr):
"""
Transforms a docstring into a properly formatted single line string.
>>> clean_docstring("\\nfoo\\n bar\\n")
'foo bar'
>>> clean_docstring("foo bar")
'foo bar'
"""
return " ".join(
[word for word in docstr.replace("\n", " ").strip().split(" ") if word != ""]
)
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
# only add this during call instead of during any stage
if report.when == "call" and not isinstance(item, DoctestItem):
metadata = get_metadata_from_funcargs(item.funcargs)
markers = {n.name: serialize_marker(n) for n in get_node_markers(item)}
severity = markers.get("severity", None) and markers.get("severity")["args"][0]
regression = (
markers.get("regression", None) and markers.get("regression")["args"][0]
)
outcome, reason = get_outcome_and_reason(report, markers, call)
rationale = markers.get("rationale", None) and clean_docstring(
markers.get("rationale")["args"][0]
)
description = item._obj.__doc__ and clean_docstring(item._obj.__doc__)
# add json metadata
report.test_metadata = serialize_datetimes(
dict(
description=description,
markers=markers,
metadata=metadata,
outcome=outcome, # 'passed', 'failed', 'skipped', 'xfailed', 'xskipped', or 'errored'
parametrized_name=item.name,
rationale=rationale,
reason=reason,
severity=severity,
regression=regression,
unparametrized_name=item.originalname,
)
)