forked from panoptes/POCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
345 lines (268 loc) · 11.2 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
# This is in the root POCS directory so that pytest will recognize the
# options added below without having to also specify pocs/test, or a
# one of the tests in that directory, on the command line; i.e. pytest
# doesn't load pocs/tests/conftest.py until after it has searched for
# tests.
# In addition, there are fixtures defined here that are available to
# all tests, not just those in pocs/tests.
import copy
import os
import pytest
import subprocess
import time
from pocs import hardware
from pocs.utils.database import PanDB
from pocs.utils.logger import get_root_logger
from pocs.utils.messaging import PanMessaging
# Global variable set to a bool by can_connect_to_mongo().
_can_connect_to_mongo = None
_all_databases = ['mongo', 'file', 'memory']
def pytest_addoption(parser):
hw_names = ",".join(hardware.get_all_names()) + ' (or all for all hardware)'
db_names = ",".join(_all_databases) + ' (or all for all databases)'
group = parser.getgroup("PANOPTES pytest options")
group.addoption(
"--with-hardware",
nargs='+',
default=[],
help=("A comma separated list of hardware to test. List items can include: " + hw_names))
group.addoption(
"--without-hardware",
nargs='+',
default=[],
help=("A comma separated list of hardware to NOT test. " + "List items can include: " +
hw_names))
group.addoption(
"--solve",
action="store_true",
default=False,
help="If tests that require solving should be run")
group.addoption(
"--test-databases",
nargs="+",
default=['file'],
help=("Test databases in the list. List items can include: " + db_names +
". Note that travis-ci will test all of them by default."))
def pytest_collection_modifyitems(config, items):
"""Modify tests to skip or not based on cli options.
Certain tests should only be run when the appropriate hardware is attached.
Other tests fail if real hardware is attached (e.g. they expect there is no
hardware). The names of the types of hardware are in hardware.py, but
include 'mount' and 'camera'. For a test that requires a mount, for
example, the test should be marked as follows:
`@pytest.mark.with_mount`
And the same applies for the names of other types of hardware.
For a test that requires that there be no cameras attached, mark the test
as follows:
`@pytest.mark.without_camera`
"""
# without_hardware is a list of hardware names whose tests we don't want to run.
without_hardware = hardware.get_simulator_names(
simulator=config.getoption('--without-hardware'))
# with_hardware is a list of hardware names for which we have that hardware attached.
with_hardware = hardware.get_simulator_names(simulator=config.getoption('--with-hardware'))
for name in without_hardware:
# User does not want to run tests that interact with hardware called name,
# whether it is marked as with_name or without_name.
if name in with_hardware:
print('Warning: {!r} in both --with-hardware and --without-hardware'.format(name))
with_hardware.remove(name)
skip = pytest.mark.skip(reason="--without-hardware={} specified".format(name))
with_keyword = 'with_' + name
without_keyword = 'without_' + name
for item in items:
if with_keyword in item.keywords or without_keyword in item.keywords:
item.add_marker(skip)
for name in hardware.get_all_names(without=with_hardware):
# We don't have hardware called name, so find all tests that need that
# hardware and mark it to be skipped.
skip = pytest.mark.skip(reason="Test needs --with-hardware={} option to run".format(name))
keyword = 'with_' + name
for item in items:
if keyword in item.keywords:
item.add_marker(skip)
def pytest_runtest_logstart(nodeid, location):
"""Signal the start of running a single test item.
This hook will be called before pytest_runtest_setup(),
pytest_runtest_call() and pytest_runtest_teardown() hooks.
Args:
nodeid (str) – full id of the item
location – a triple of (filename, linenum, testname)
"""
try:
logger = get_root_logger()
logger.critical('')
logger.critical('##########' * 8)
logger.critical(' START TEST {}', nodeid)
logger.critical('')
except Exception:
pass
def pytest_runtest_logfinish(nodeid, location):
"""Signal the complete finish of running a single test item.
This hook will be called after pytest_runtest_setup(),
pytest_runtest_call() and pytest_runtest_teardown() hooks.
Args:
nodeid (str) – full id of the item
location – a triple of (filename, linenum, testname)
"""
try:
logger = get_root_logger()
logger.critical('')
logger.critical(' END TEST {}', nodeid)
logger.critical('')
logger.critical('##########' * 8)
except Exception:
pass
def pytest_runtest_logreport(report):
"""Adds the failure info that pytest prints to stdout into the log."""
if report.skipped or report.outcome != 'failed':
return
try:
logger = get_root_logger()
logger.critical('')
logger.critical(' TEST {} FAILED during {}\n\n{}\n', report.nodeid, report.when,
report.longreprtext)
cnt = 15
if report.capstdout:
logger.critical('{}Captured stdout during {}{}\n{}\n', '= ' * cnt, report.when,
' =' * cnt, report.capstdout)
if report.capstderr:
logger.critical('{}Captured stderr during {}{}\n{}\n', '* ' * cnt, report.when,
' *' * cnt, report.capstderr)
except Exception:
pass
@pytest.fixture
def temp_file():
temp_file = 'temp'
with open(temp_file, 'w') as f:
f.write('')
yield temp_file
os.unlink(temp_file)
@pytest.fixture(scope="session")
def db_name():
return 'panoptes_testing'
class FakeLogger:
def __init__(self):
self.messages = []
pass
def _add(self, name, *args):
msg = [name]
assert len(args) == 1
assert isinstance(args[0], tuple)
msg.append(args[0])
self.messages.append(msg)
def debug(self, *args):
self._add('debug', args)
def info(self, *args):
self._add('info', args)
def warning(self, *args):
self._add('warning', args)
def error(self, *args):
self._add('error', args)
def critical(self, *args):
self._add('critical', args)
@pytest.fixture(scope='function')
def fake_logger():
return FakeLogger()
def can_connect_to_mongo(db_name):
global _can_connect_to_mongo
if _can_connect_to_mongo is None:
logger = get_root_logger()
try:
PanDB(db_type='mongo', db_name=db_name, logger=logger, connect=True)
_can_connect_to_mongo = True
except Exception:
_can_connect_to_mongo = False
logger.info('can_connect_to_mongo = {}', _can_connect_to_mongo)
return _can_connect_to_mongo
@pytest.fixture(scope='function', params=_all_databases)
def db_type(request, db_name):
db_list = request.config.option.test_databases
if request.param not in db_list and 'all' not in db_list:
pytest.skip("Skipping {} DB, set --test-all-databases=True".format(request.param))
# If testing mongo, make sure we can connect, otherwise skip.
if request.param == 'mongo' and not can_connect_to_mongo(db_name):
pytest.skip("Can't connect to {} DB, skipping".format(request.param))
PanDB.permanently_erase_database(request.param, db_name, really='Yes', dangerous='Totally')
return request.param
@pytest.fixture(scope='function')
def db(db_type, db_name):
return PanDB(
db_type=db_type, db_name=db_name, logger=get_root_logger(), connect=True)
@pytest.fixture(scope='function')
def memory_db(db_name):
PanDB.permanently_erase_database('memory', db_name, really='Yes', dangerous='Totally')
return PanDB(db_type='memory', db_name=db_name)
# -----------------------------------------------------------------------
# Messaging support fixtures. It is important that tests NOT use the same
# ports that the real pocs_shell et al use; when they use the same ports,
# then tests may cause errors in the real system (e.g. by sending a
# shutdown command).
@pytest.fixture(scope='module')
def messaging_ports():
# Some code (e.g. POCS._setup_messaging) assumes that sub and pub ports
# are sequential so these need to match that assumption for now.
return dict(msg_ports=(43001, 43002), cmd_ports=(44001, 44002))
@pytest.fixture(scope='function')
def message_forwarder(messaging_ports):
cmd = os.path.join(os.getenv('POCS'), 'scripts', 'run_messaging_hub.py')
args = [cmd]
# Note that the other programs using these port pairs consider
# them to be pub and sub, in that order, but the forwarder sees things
# in reverse: it subscribes to the port that others publish to,
# and it publishes to the port that others subscribe to.
for _, (sub, pub) in messaging_ports.items():
args.append('--pair')
args.append(str(sub))
args.append(str(pub))
get_root_logger().info('message_forwarder fixture starting: {}', args)
proc = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# It takes a while for the forwarder to start, so allow for that.
# TODO(jamessynge): Come up with a way to speed up these fixtures.
time.sleep(3)
yield messaging_ports
proc.terminate()
@pytest.fixture(scope='function')
def msg_publisher(message_forwarder):
port = message_forwarder['msg_ports'][0]
publisher = PanMessaging.create_publisher(port)
yield publisher
publisher.close()
@pytest.fixture(scope='function')
def msg_subscriber(message_forwarder):
port = message_forwarder['msg_ports'][1]
subscriber = PanMessaging.create_subscriber(port)
yield subscriber
subscriber.close()
@pytest.fixture(scope='function')
def cmd_publisher(message_forwarder):
port = message_forwarder['cmd_ports'][0]
publisher = PanMessaging.create_publisher(port)
yield publisher
publisher.close()
@pytest.fixture(scope='function')
def cmd_subscriber(message_forwarder):
port = message_forwarder['cmd_ports'][1]
subscriber = PanMessaging.create_subscriber(port)
yield subscriber
subscriber.close()
@pytest.fixture(scope='function')
def save_environ():
old_env = copy.deepcopy(os.environ)
yield
os.environ = old_env
@pytest.fixture(scope='session')
def data_dir():
return os.path.join(os.getenv('POCS'), 'pocs', 'tests', 'data')
@pytest.fixture(scope='session')
def unsolved_fits_file(data_dir):
return os.path.join(data_dir, 'unsolved.fits')
@pytest.fixture(scope='session')
def solved_fits_file(data_dir):
return os.path.join(data_dir, 'solved.fits.fz')
@pytest.fixture(scope='session')
def tiny_fits_file(data_dir):
return os.path.join(data_dir, 'tiny.fits')
@pytest.fixture(scope='session')
def noheader_fits_file(data_dir):
return os.path.join(data_dir, 'noheader.fits')