-
Notifications
You must be signed in to change notification settings - Fork 15
/
log.py
269 lines (225 loc) · 9.64 KB
/
log.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
#
# Copyright (c) 2018 Two Sigma Open Source, LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
''':mod:`~marbles.core.marbles` can log information about each
assertion called.
If configured, the :data:`marbles.core.log.logger` will log a json
object for each assertion and its success or failure, as well as any
other attributes of interest specified by the test author.
The captured information includes the assertion's args and kwargs,
msg, note, local variables (for failed assertions, and also for
passing assertions if verbose logging is turned on), and the result of
the assertion.
Configuration is handled via the environment variables
:envvar:`MARBLES_LOGFILE`, :envvar:`MARBLES_TEST_CASE_ATTRS`,
:envvar:`MARBLES_TEST_CASE_ATTRS_VERBOSE`,
:envvar:`MARBLES_LOG_VERBOSE`, or via the
:meth:`AssertionLogger.configure` method. Environment variables
override those set with the :meth:`~AssertionLogger.configure` method,
so if a :mod:`marbles` program configures these programmatically, they
can always be overridden without changing the program.
Note that :class:`AssertionLogger` should not be instantiated
directly; instead, test authors should import and configure the
:data:`marbles.core.log.logger` as needed.
'''
import datetime
import inspect
import json
import os
from . import _stack
from . import __version__
# XXX(leif): I don't think it's worth the gymnastics it would take to
# test this function. We do test whether mixins are
# identified but catching all the cases here would be a
# lot.
def _class_defining_method(meth): # pragma: no cover
'''Gets the name of the class that defines meth.
Adapted from
http://stackoverflow.com/questions/3589311/get-defining-class-of-unbound-method-object-in-python-3/25959545#25959545.
'''
if inspect.ismethod(meth):
for cls in inspect.getmro(meth.__self__.__class__):
if cls.__dict__.get(meth.__name__) is meth:
return '{}.{}'.format(cls.__module__, cls.__name__)
meth = meth.__func__
if inspect.isfunction(meth):
module = meth.__qualname__.split('.<locals>', 1)[0]
cls = getattr(inspect.getmodule(meth), module.rsplit('.', 1)[0])
if isinstance(cls, type):
return '{}.{}'.format(cls.__module__, cls.__name__)
class AssertionLogger(object):
'''The :class:`AssertionLogger` logs json about each assertion.
.. warning::
If you are uploading these logs somewhere like Elasticsearch,
be sure that the information in your locals and arguments is
protected by securing the destination system.
This module exposes a single :class:`AssertionLogger`,
:data:`marbles.core.log.logger`, that is used during a marbles test
run. It can be configured with :meth:`configure` before running
the tests or via environment variables.
Example:
.. code-block:: py
import marbles.core
from marbles.core import log
if __name__ == '__main__':
log.logger.configure(logfile='/path/to/marbles.log',
attrs=['filename', 'date'])
marbles.core.main()
'''
def __init__(self):
self._logfile = None
self._logfilename = None
self._verbose = False
self._attrs = None
self._verbose_attrs = None
@staticmethod
def _open_if_needed(filename):
if isinstance(filename, (str, bytes)):
return open(filename, 'w')
else:
# Assume is already file-like
return filename
def configure(self, **kwargs):
'''Configure what assertion logging is done.
Settings configured with this method are overridden by
environment variables.
Parameters
----------
logfile : str or bytes or file object
If a string or bytes object, we write to that filename.
If an open file object, we just write to it. If None,
disable logging. If we open the file, we open it in
``'w'`` mode, so any contents will be overwritten.
attrs : list of str
Capture these attributes on the TestCase being run when
logging an assertion. For example, if you are testing
multiple resources, make sure the resource name is a
member of your TestCase, and configure marbles logging
with that name. These are only captured on failure.
verbose_attrs : list of str
Similar to attrs, but these attrs are captured even on
success.
verbose : bool or list of str
Fields (within the set {msg, note, locals}) to capture
even when the test is successful. By default, those three
fields are only captured on failure.
'''
if 'logfile' in kwargs:
# Note that kwargs['logfile'] might be an open file
# object, not a string. We deal with this in
# _open_if_needed, but refactoring it so that in that case
# it gets set on another attribute would be tricky to
# handle the lazy opening semantics that let us override
# it with MARBLES_LOGFILE, so instead we choose to let
# self._logfilename do double-duty: sometimes it's a name,
# sometimes it's sneakily a file object.
self._logfilename = kwargs['logfile']
if 'attrs' in kwargs:
self._attrs = kwargs['attrs']
if 'verbose_attrs' in kwargs:
self._verbose_attrs = kwargs['verbose_attrs']
if 'verbose' in kwargs:
self._verbose = kwargs['verbose']
@property
def log_enabled(self):
return self.logfile is not None
@property
def logfile(self):
if self._logfile:
return self._logfile
if self.logfilename:
self._logfile = self._open_if_needed(self.logfilename)
return self._logfile
@property
def logfilename(self):
return os.environ.get('MARBLES_LOGFILE', self._logfilename)
@property
def attrs(self):
try:
return os.environ['MARBLES_TEST_CASE_ATTRS'].split(',')
except KeyError:
return self._attrs or ()
@property
def verbose_attrs(self):
try:
return os.environ['MARBLES_TEST_CASE_ATTRS_VERBOSE'].split(',')
except KeyError:
return self._verbose_attrs or ()
@property
def verbose(self):
verbose = os.environ.get('MARBLES_LOG_VERBOSE', self._verbose)
verbose_attrs = ('msg', 'note', 'locals')
if isinstance(verbose, str):
if verbose.lower() == 'false':
return ()
elif verbose.lower() == 'true':
return verbose_attrs
else:
return verbose.split(',')
elif verbose is True:
return verbose_attrs
else:
return verbose or ()
def _log_assertion(self, case, assertion, args, kwargs, msg, note,
*exc_info):
if not self.log_enabled:
return
now = datetime.datetime.now()
locals_, module, filename, lineno = _stack.get_stack_info()
passed = exc_info[0] is None
doc = {
'case': str(case),
'test_case': case.__class__.__name__,
'test_method': case._testMethodName,
'module': module,
'file': filename,
'line': lineno,
'assertion': assertion.__name__,
'args': [str(a) for a in args],
'kwargs': [{'key': k, 'value': str(v)} for k, v in kwargs.items()],
'assertion_class': _class_defining_method(assertion),
'marbles_version': __version__,
'@timestamp': now.strftime('%Y-%m-%dT%H:%M:%S.%f')
}
verbose_elements = {
'msg': msg,
'note': note.format(**locals_) if note else None,
'locals': [{'key': k, 'value': str(v)} for k, v in locals_.items()
if (k not in ('msg', 'note', 'self')
and not k.startswith('_'))]
}
if not passed:
doc.update(verbose_elements)
elif self.verbose:
doc.update({k: v for k, v in verbose_elements.items()
if k in self.verbose})
doc.update({attr: str(getattr(case, attr, None))
for attr in self.verbose_attrs})
if not passed:
doc.update({attr: str(getattr(case, attr, None))
for attr in self.attrs})
if passed:
doc['result'] = 'pass'
else:
doc['result'] = 'fail'
json.dump(doc, self.logfile)
self.logfile.write('\n')
logger = AssertionLogger()