forked from sassoftware/python-sasctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
411 lines (327 loc) · 12.6 KB
/
conftest.py
File metadata and controls
411 lines (327 loc) · 12.6 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import builtins
import os
import re
import warnings
from unittest import mock
from urllib.parse import urlsplit
import betamax
from betamax_serializers import pretty_json
from betamax.cassette.cassette import Placeholder
import pytest
from sasctl import Session
from .matcher import RedactedPathMatcher
def redact(interaction, cassette):
"""Remove sensitive or environment-specific information from cassettes
before they are saved.
Parameters
----------
interaction
cassette
Returns
-------
"""
# Server name in Origin header may differ from hostname that was sent the
# request.
for origin in interaction.data['response']['headers'].get('Origin', []):
host = urlsplit(origin).netloc
if (
host != ''
and Placeholder(placeholder='hostname.com', replace=host)
not in cassette.placeholders
):
cassette.placeholders.append(
Placeholder(placeholder='hostname.com', replace=host)
)
def add_placeholder(pattern, string, placeholder, group):
if isinstance(string, bytes):
pattern = pattern.encode('utf-8')
match = re.search(pattern, string)
if match:
old_text = match.group(group)
cassette.placeholders.append(
Placeholder(placeholder=placeholder, replace=old_text)
)
if 'string' in interaction.data['request']['body']:
add_placeholder(
r"(?<=&password=)([^&]*)\b",
interaction.data['request']['body']['string'],
'*****',
1,
)
if 'string' in interaction.data['response']['body']:
add_placeholder(
'(?<=access_token":")[^"]*',
interaction.data['response']['body']['string'],
'[redacted]',
0,
)
for index, header in enumerate(
interaction.data['request']['headers'].get('Authorization', [])
):
# Betamax tries to replace Placeholders on all headers. Mixed str/bytes headers will cause Betamax to break.
if isinstance(header, bytes):
header = header.decode('utf-8')
interaction.data['request']['headers']['Authorization'][index] = header
add_placeholder(r'(?<=Basic ).*', header, '[redacted]', 0) # swat
add_placeholder(r'(?<=Bearer ).*', header, '[redacted]', 0) # sasctl
betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
betamax.Betamax.register_request_matcher(RedactedPathMatcher)
from .matcher import PartialBodyMatcher
betamax.Betamax.register_request_matcher(PartialBodyMatcher)
# Replay cassettes only by default
# Can be overridden as necessary to update cassettes
# See https://betamax.readthedocs.io/en/latest/record_modes.html for details.
# NOTE: We've added a custom "live" record mode that bypasses all cassettes
# and allows test suite to be run against a live server.
os.environ.setdefault('SASCTL_RECORD_MODE', 'once')
if os.environ['SASCTL_RECORD_MODE'] not in (
'once',
'new_episodes',
'all',
'none',
'live',
):
os.environ['SASCTL_RECORD_MODE'] = 'once'
# Set a flag to indicate whether bypassing Betamax altogether.
if os.environ['SASCTL_RECORD_MODE'].lower() == 'live':
SKIP_REPLAY = True
# Setting this back to a valid Betamax value to avoid downstream errors.
os.environ['SASCTL_RECORD_MODE'] = 'once'
else:
SKIP_REPLAY = False
with betamax.Betamax.configure() as config:
config.cassette_library_dir = "tests/cassettes"
config.default_cassette_options['record_mode'] = os.environ['SASCTL_RECORD_MODE']
config.default_cassette_options['match_requests_on'] = [
'method',
'redacted_path',
# 'partial_body',
'query',
]
config.before_record(callback=redact)
config.before_playback(callback=redact)
# Use the SASCTL_TEST_SERVERS variable to specify one or more servers that will
# be used for recording test cases
os.environ.setdefault('SASCTL_TEST_SERVERS', 'sasctl.example.com')
hostnames = [host.strip() for host in os.environ['SASCTL_TEST_SERVERS'].split(',')]
# Set dummy credentials if none were provided.
# Credentials don't matter if rerunning Betamax cassettes, but new recordings
# will fail.
os.environ.setdefault('SASCTL_SERVER_NAME', hostnames[0])
os.environ.setdefault('SASCTL_USER_NAME', 'dummyuser')
os.environ.setdefault('SASCTL_PASSWORD', 'dummypass')
os.environ.setdefault('SSLREQCERT', 'no')
with betamax.Betamax.configure() as config:
for hostname in hostnames:
config.define_cassette_placeholder('hostname.com', hostname)
config.define_cassette_placeholder('hostname.com', os.environ['SASCTL_SERVER_NAME'])
config.define_cassette_placeholder('USERNAME', os.environ['SASCTL_USER_NAME'])
config.define_cassette_placeholder('*****', os.environ['SASCTL_PASSWORD'])
@pytest.fixture(scope='session', params=hostnames)
def credentials(request):
auth = {
'hostname': request.param,
'username': os.environ['SASCTL_USER_NAME'],
'password': os.environ['SASCTL_PASSWORD'],
'verify_ssl': False,
}
if 'SASCTL_AUTHINFO' in os.environ:
auth['authinfo'] = os.path.expanduser(os.environ['SASCTL_AUTHINFO'])
return auth
@pytest.fixture(scope='function')
def session(request, credentials):
import warnings
from betamax.fixtures.pytest import _casette_name
from sasctl import current_session
if SKIP_REPLAY:
yield Session(**credentials)
current_session(None)
return
# Ignore FutureWarnings from betamax to avoid cluttering test results
with warnings.catch_warnings():
warnings.simplefilter('ignore')
cassette_name = _casette_name(request, parametrized=False)
# Need to instantiate a Session before starting Betamax recording,
# but sasctl.Session makes requests (which should be recorded) during
# __init__(). Mock __init__ to prevent from running and then manually
# execute requests.Session.__init__() so Betamax can use the session.
with mock.patch('sasctl.core.Session.__init__', return_value=None):
recorded_session = Session()
super(Session, recorded_session).__init__()
with betamax.Betamax(recorded_session).use_cassette(
cassette_name, serialize_with='prettyjson'
) as recorder:
recorder.start()
# Manually run the sasctl.Session constructor. Mock out calls to
# underlying requests.Session.__init__ to prevent hooks placed by
# Betamax from being reset.
with mock.patch('sasctl.core.requests.Session.__init__'):
recorded_session.__init__(**credentials)
current_session(recorded_session)
yield recorded_session
recorder.stop()
current_session(None)
@pytest.fixture
def missing_packages():
"""Creates a context manager that prevents the specified packages from being imported.
Use the simulate packages missing during testing.
Examples
--------
with missing_packages('os'):
with pytest.raises(ImportError):
import os
"""
from unittest import mock
from contextlib import contextmanager
@contextmanager
def mocked_importer(packages):
builtin_import = __import__
# Accept single string or iterable of strings
if isinstance(packages, str):
packages = [packages]
# Method that fails to load specified packages but otherwise behaves normally
def _import(name, *args, **kwargs):
if any(name == package for package in packages):
raise ImportError()
return builtin_import(name, *args, **kwargs)
try:
with mock.patch(builtins.__name__ + '.__import__', side_effect=_import):
yield
finally:
pass
return mocked_importer
@pytest.fixture
def cas_session(request, credentials):
import requests
from betamax.fixtures.pytest import _casette_name
from unittest import mock
swat = pytest.importorskip('swat')
from swat.exceptions import SWATError
if SKIP_REPLAY:
with swat.CAS(
'https://{}/cas-shared-default-http/'.format(credentials['hostname']),
username=credentials['username'],
password=credentials['password'],
) as s:
yield s
return
# Ignore FutureWarnings from betamax to avoid cluttering test results
with warnings.catch_warnings():
warnings.simplefilter('ignore')
cassette_name = _casette_name(request, parametrized=False) + '_swat'
# Must have an existing Session for Betamax to record
recorded_session = requests.Session()
with betamax.Betamax(recorded_session).use_cassette(
cassette_name, serialize_with='prettyjson'
) as recorder:
recorder.start()
# CAS connection tries to create its own Session instance.
# Inject the session being recorded into the CAS connection
with mock.patch('swat.cas.rest.connection.requests.Session') as mocked:
mocked.return_value = recorded_session
s = None
try:
s = swat.CAS(
'https://{}/cas-shared-default-http/'.format(
credentials['hostname']
),
username=credentials['username'],
password=credentials['password'],
)
# Strip out the session id from requests & responses.
recorder.config.define_cassette_placeholder('[session id]', s._session)
yield s
finally:
try:
if hasattr(s, 'close'):
s.close()
except SWATError:
# session was closed during testing
pass
recorder.stop()
@pytest.fixture
def iris_astore(cas_session):
pd = pytest.importorskip('pandas')
datasets = pytest.importorskip('sklearn.datasets')
ASTORE_NAME = 'astore'
cas_session.loadactionset('decisionTree')
raw = datasets.load_iris()
iris = pd.DataFrame(raw.data, columns=raw.feature_names)
iris = iris.join(pd.DataFrame(raw.target))
iris.columns = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
tbl = cas_session.upload(iris).casTable
_ = tbl.decisiontree.gbtreetrain(
target='Species',
inputs=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'],
nominal=['Species'],
ntree=10,
savestate=ASTORE_NAME,
)
return cas_session.CASTable(ASTORE_NAME)
def pytest_configure(config):
config.addinivalue_line(
"markers",
"incremental: tests should be executed in order and xfail if previous test fails.",
)
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" % previousfailed.name)
@pytest.fixture
def airline_dataset():
"""Sentiment analysis dataset."""
pd = pytest.importorskip('pandas')
df = pd.read_csv('examples/data/airline_tweets.csv')
df = df[
[
'airline_sentiment',
'airline',
'name',
'tweet_location',
'tweet_id',
'tweet_created',
'retweet_count',
'text',
]
]
return df
@pytest.fixture
def boston_dataset():
"""Regression dataset."""
pytest.importorskip('sklearn')
pd = pytest.importorskip('pandas')
from sklearn import datasets
raw = datasets.load_boston()
df = pd.DataFrame(raw.data, columns=raw.feature_names)
df['Price'] = raw.target
return df
@pytest.fixture
def cancer_dataset():
"""Binary classification dataset."""
pytest.importorskip('sklearn')
pd = pytest.importorskip('pandas')
from sklearn import datasets
raw = datasets.load_breast_cancer()
df = pd.DataFrame(raw.data, columns=raw.feature_names)
df['Type'] = raw.target
df.Type = df.Type.astype('category')
df.Type.cat.categories = raw.target_names
return df
@pytest.fixture
def iris_dataset():
"""Multi-class classification dataset."""
pd = pytest.importorskip('pandas')
df = pd.read_csv('examples/data/iris.csv')
df.Species = df.Species.astype('category')
return df