forked from ceph/teuthology
/
results.py
298 lines (255 loc) · 9.13 KB
/
results.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
import os
import sys
import time
import logging
import subprocess
from collections import OrderedDict
from textwrap import dedent
from textwrap import fill
import teuthology
from teuthology.config import config
from teuthology import misc
from .report import ResultsReporter
log = logging.getLogger(__name__)
UNFINISHED_STATUSES = ('queued', 'running', 'waiting')
def main(args):
log = logging.getLogger(__name__)
if args['--verbose']:
teuthology.log.setLevel(logging.DEBUG)
if not args['--dry-run']:
log_path = os.path.join(args['--archive-dir'], 'results.log')
teuthology.setup_log_file(log_path)
try:
if args['--seed']:
note_rerun_params(args['--subset'], args['--seed'])
else:
results(args['--archive-dir'], args['--name'], args['--email'],
int(args['--timeout']), args['--dry-run'])
except Exception:
log.exception('error generating memo/results')
raise
def note_rerun_params(subset, seed):
if subset:
log.info('subset: %r', subset)
if seed:
log.info('seed: %r', seed)
def results(archive_dir, name, email, timeout, dry_run):
starttime = time.time()
if timeout:
log.info('Waiting up to %d seconds for tests to finish...', timeout)
reporter = ResultsReporter()
while timeout > 0:
if time.time() - starttime > timeout:
log.warn('test(s) did not finish before timeout of %d seconds',
timeout)
break
jobs = reporter.get_jobs(name, fields=['job_id', 'status'])
unfinished_jobs = [job for job in jobs if job['status'] in
UNFINISHED_STATUSES]
if not unfinished_jobs:
log.info('Tests finished! gathering results...')
break
time.sleep(60)
(subject, body) = build_email_body(name)
try:
if email and dry_run:
print "From: %s" % (config.results_sending_email or 'teuthology')
print "To: %s" % email
print "Subject: %s" % subject
print body
elif email:
email_results(
subject=subject,
from_=(config.results_sending_email or 'teuthology'),
to=email,
body=body,
)
finally:
generate_coverage(archive_dir, name)
def generate_coverage(archive_dir, name):
coverage_config_keys = ('coverage_output_dir', 'coverage_html_dir',
'coverage_tools_dir')
for key in coverage_config_keys:
if key not in config.to_dict():
log.warn(
"'%s' not in teuthology config; skipping coverage report",
key)
return
log.info('starting coverage generation')
subprocess.Popen(
args=[
os.path.join(os.path.dirname(sys.argv[0]), 'teuthology-coverage'),
'-v',
'-o',
os.path.join(config.coverage_output_dir, name),
'--html-output',
os.path.join(config.coverage_html_dir, name),
'--cov-tools-dir',
config.coverage_tools_dir,
archive_dir,
],
)
def email_results(subject, from_, to, body):
log.info('Sending results to {to}: {body}'.format(to=to, body=body))
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_
msg['To'] = to
log.debug('sending email %s', msg.as_string())
smtp = smtplib.SMTP('localhost')
smtp.sendmail(msg['From'], [msg['To']], msg.as_string())
smtp.quit()
def build_email_body(name, _reporter=None):
stanzas = OrderedDict([
('fail', dict()),
('dead', dict()),
('running', dict()),
('waiting', dict()),
('queued', dict()),
('pass', dict()),
])
reporter = _reporter or ResultsReporter()
fields = ('job_id', 'status', 'description', 'duration', 'failure_reason',
'sentry_event', 'log_href')
jobs = reporter.get_jobs(name, fields=fields)
jobs.sort(key=lambda job: job['job_id'])
for job in jobs:
job_stanza = format_job(name, job)
stanzas[job['status']][job['job_id']] = job_stanza
sections = OrderedDict.fromkeys(stanzas.keys(), '')
subject_fragments = []
for status in sections.keys():
stanza = stanzas[status]
if stanza:
subject_fragments.append('%s %s' % (len(stanza), status))
sections[status] = email_templates['sect_templ'].format(
title=status.title(),
jobs=''.join(stanza.values()),
)
subject = ', '.join(subject_fragments) + ' '
if config.archive_server:
log_root = os.path.join(config.archive_server, name, '')
else:
log_root = None
body = email_templates['body_templ'].format(
name=name,
info_root=misc.get_results_url(name),
log_root=log_root,
fail_count=len(stanzas['fail']),
dead_count=len(stanzas['dead']),
running_count=len(stanzas['running']),
waiting_count=len(stanzas['waiting']),
queued_count=len(stanzas['queued']),
pass_count=len(stanzas['pass']),
fail_sect=sections['fail'],
dead_sect=sections['dead'],
running_sect=sections['running'],
waiting_sect=sections['waiting'],
queued_sect=sections['queued'],
pass_sect=sections['pass'],
)
subject += 'in {suite}'.format(suite=name)
return (subject.strip(), body.strip())
def format_job(run_name, job):
job_id = job['job_id']
status = job['status']
description = job['description']
duration = seconds_to_hms(int(job['duration'] or 0))
# Every job gets a link to e.g. pulpito's pages
info_url = misc.get_results_url(run_name, job_id)
if info_url:
info_line = email_templates['info_url_templ'].format(info=info_url)
else:
info_line = ''
if status in UNFINISHED_STATUSES:
format_args = dict(
job_id=job_id,
desc=description,
time=duration,
info_line=info_line,
)
return email_templates['running_templ'].format(**format_args)
if status == 'pass':
return email_templates['pass_templ'].format(
job_id=job_id,
desc=description,
time=duration,
info_line=info_line,
)
else:
log_dir_url = job['log_href'].rstrip('teuthology.yaml')
if log_dir_url:
log_line = email_templates['fail_log_templ'].format(
log=log_dir_url)
else:
log_line = ''
sentry_event = job.get('sentry_event')
if sentry_event:
sentry_line = email_templates['fail_sentry_templ'].format(
sentry_event=sentry_event)
else:
sentry_line = ''
if job['failure_reason']:
# 'fill' is from the textwrap module and it collapses a given
# string into multiple lines of a maximum width as specified.
# We want 75 characters here so that when we indent by 4 on the
# next line, we have 79-character exception paragraphs.
reason = fill(job['failure_reason'] or '', 75)
reason = \
'\n'.join((' ') + line for line in reason.splitlines())
reason_lines = email_templates['fail_reason_templ'].format(
reason=reason).rstrip()
else:
reason_lines = ''
format_args = dict(
job_id=job_id,
desc=description,
time=duration,
info_line=info_line,
log_line=log_line,
sentry_line=sentry_line,
reason_lines=reason_lines,
)
return email_templates['fail_templ'].format(**format_args)
def seconds_to_hms(seconds):
(minutes, seconds) = divmod(seconds, 60)
(hours, minutes) = divmod(minutes, 60)
return "%02d:%02d:%02d" % (hours, minutes, seconds)
email_templates = {
'body_templ': dedent("""\
Test Run: {name}
=================================================================
info: {info_root}
logs: {log_root}
failed: {fail_count}
dead: {dead_count}
running: {running_count}
waiting: {waiting_count}
queued: {queued_count}
passed: {pass_count}
{fail_sect}{dead_sect}{running_sect}{waiting_sect}{queued_sect}{pass_sect}
"""),
'sect_templ': dedent("""\
{title}
=================================================================
{jobs}
"""),
'fail_templ': dedent("""\
[{job_id}] {desc}
-----------------------------------------------------------------
time: {time}{info_line}{log_line}{sentry_line}{reason_lines}
"""),
'info_url_templ': "\ninfo: {info}",
'fail_log_templ': "\nlog: {log}",
'fail_sentry_templ': "\nsentry: {sentry_event}",
'fail_reason_templ': "\n\n{reason}\n",
'running_templ': dedent("""\
[{job_id}] {desc}{info_line}
"""),
'pass_templ': dedent("""\
[{job_id}] {desc}
time: {time}{info_line}
"""),
}