forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
view_build_test.py
451 lines (396 loc) · 18.6 KB
/
view_build_test.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
import cloudstorage as gcs
import view_build
import main_test
import gcs_async_test
import github.models
import testgrid_test
app = main_test.app
init_build = main_test.init_build
write = gcs_async_test.write
class ParseJunitTest(unittest.TestCase):
@staticmethod
def parse(xml):
parser = view_build.JUnitParser()
parser.parse_xml(xml, 'junit_filename.xml')
return parser.get_results()
def test_normal(self):
results = self.parse(main_test.JUNIT_SUITE)
stack = '/go/src/k8s.io/kubernetes/test.go:123\nError Goes Here'
self.assertEqual(results, {
'passed': ['Second'],
'skipped': ['First'],
'failed': [('Third', 96.49, stack, "junit_filename.xml", "")],
})
def test_testsuites(self):
results = self.parse("""
<testsuites>
<testsuite name="k8s.io/suite">
<properties>
<property name="go.version" value="go1.6"/>
</properties>
<testcase name="TestBad" time="0.1">
<failure>something bad</failure>
<system-out>out: first line</system-out>
<system-err>err: first line</system-err>
<system-out>out: second line</system-out>
</testcase>
</testsuite>
</testsuites>""")
self.assertEqual(results['failed'], [(
'k8s.io/suite TestBad', 0.1, 'something bad', "junit_filename.xml",
"out: first line\nout: second line\nerr: first line",
)])
def test_testsuites_no_time(self):
results = self.parse("""
<testsuites>
<testsuite name="k8s.io/suite">
<properties>
<property name="go.version" value="go1.6"/>
</properties>
<testcase name="TestBad">
<failure>something bad</failure>
<system-out>out: first line</system-out>
<system-err>err: first line</system-err>
<system-out>out: second line</system-out>
</testcase>
</testsuite>
</testsuites>""")
self.assertEqual(results['failed'], [(
'k8s.io/suite TestBad', 0.0, 'something bad', "junit_filename.xml",
"out: first line\nout: second line\nerr: first line",
)])
def test_nested_testsuites(self):
results = self.parse("""
<testsuites>
<testsuite name="k8s.io/suite">
<testsuite name="k8s.io/suite/sub">
<properties>
<property name="go.version" value="go1.6"/>
</properties>
<testcase name="TestBad" time="0.1">
<failure>something bad</failure>
<system-out>out: first line</system-out>
<system-err>err: first line</system-err>
<system-out>out: second line</system-out>
</testcase>
</testsuite>
</testsuite>
</testsuites>""")
self.assertEqual(results['failed'], [(
'k8s.io/suite/sub TestBad', 0.1, 'something bad', "junit_filename.xml",
"out: first line\nout: second line\nerr: first line",
)])
def test_bad_xml(self):
self.assertEqual(self.parse("""<body />""")['failed'], [])
def test_corrupt_xml(self):
self.assertEqual(self.parse('<a>\xff</a>')['failed'], [])
failures = self.parse("""
<testsuites>
<testsuite name="a">
<testcase name="Corrupt" time="0">
<failure>something bad \xff</failure>
</testcase>
</testsuite>
</testsuites>""")['failed']
self.assertEqual(failures, [('a Corrupt', 0.0, 'something bad ?',
'junit_filename.xml', '')])
def test_not_xml(self):
failures = self.parse('\x01')['failed']
self.assertEqual(failures,
[(failures[0][0], 0.0, 'not well-formed (invalid token): line 1, column 0',
'junit_filename.xml', '')])
def test_empty_output(self):
results = self.parse("""
<testsuites>
<testsuite name="k8s.io/suite">
<testcase name="TestBad" time="0.1">
<failure>something bad</failure>
<system-out></system-out>
</testcase>
</testsuite>
</testsuites>""")
self.assertEqual(results['failed'], [(
'k8s.io/suite TestBad', 0.1, 'something bad', "junit_filename.xml", "")])
class BuildTest(main_test.TestBase):
# pylint: disable=too-many-public-methods
JOB_DIR = '/kubernetes-jenkins/logs/somejob/'
BUILD_DIR = JOB_DIR + '1234/'
def setUp(self):
self.init_stubs()
init_build(self.BUILD_DIR)
testgrid_test.write_config()
def get_build_page(self, trailing=''):
return app.get('/build' + self.BUILD_DIR + trailing)
def test_missing(self):
"""Test that a missing build gives a 404."""
response = app.get('/build' + self.BUILD_DIR.replace('1234', '1235'),
status=404)
self.assertIn('1235', response)
def test_missing_started(self):
"""Test that a missing started.json still renders a proper page."""
build_dir = '/kubernetes-jenkins/logs/job-with-no-started/1234/'
init_build(build_dir, started=False)
response = app.get('/build' + build_dir)
self.assertRegexpMatches(response.body, 'Result.*SUCCESS')
self.assertIn('job-with-no-started', response)
self.assertNotIn('Started', response) # no start timestamp
self.assertNotIn('github.com', response) # no version => no src links
def test_missing_finished(self):
"""Test that a missing finished.json still renders a proper page."""
build_dir = '/kubernetes-jenkins/logs/job-still-running/1234/'
init_build(build_dir, finished=False)
response = app.get('/build' + build_dir)
self.assertRegexpMatches(response.body, 'Result.*Not Finished')
self.assertIn('job-still-running', response)
self.assertIn('Started', response)
def test_build(self):
"""Test that the build page works in the happy case."""
response = self.get_build_page()
self.assertIn('2014-07-28', response) # started
self.assertIn('v1+56', response) # build version
self.assertIn('16m40s', response) # build duration
self.assertIn('Third', response) # test name
self.assertIn('1m36s', response) # test duration
self.assertRegexpMatches(response.body, 'Result.*SUCCESS')
self.assertIn('Error Goes Here', response)
self.assertIn('test.go#L123">', response) # stacktrace link works
def test_finished_has_revision(self):
"""Test that metadata with version in finished works."""
init_build(self.BUILD_DIR, finished_has_version=True)
self.test_build()
def test_build_no_failures(self):
"""Test that builds with no Junit artifacts work."""
gcs.delete(self.BUILD_DIR + 'artifacts/junit_01.xml')
response = self.get_build_page()
self.assertIn('No Test Failures', response)
def test_show_metadata(self):
write(self.BUILD_DIR + 'started.json',
{
'revision': 'v1+56',
'timestamp': 1406535800,
'node': 'agent-light-7',
'pull': 'master:1234,35:abcd,72814',
'metadata': {
'master-version': 'm12'
}
})
write(self.BUILD_DIR + 'finished.json',
{
'timestamp': 1406536800,
'passed': True,
'metadata': {
'skew-version': 'm11'
}
})
response = self.get_build_page()
self.assertIn('v1+56', response)
self.assertIn('agent-light-7', response)
self.assertIn('<td>master-version<td>m12', response)
self.assertIn('<td>skew-version<td>m11', response)
self.assertIn('1234', response)
self.assertIn('abcd', response)
self.assertIn('72814', response)
def test_build_show_log(self):
"""Test that builds that failed with no failures show the build log."""
gcs.delete(self.BUILD_DIR + 'artifacts/junit_01.xml')
write(self.BUILD_DIR + 'finished.json',
{'passed': False, 'timestamp': 1406536800})
# Unable to fetch build-log.txt, still works.
response = self.get_build_page()
self.assertNotIn('Error lines', response)
self.testbed.init_memcache_stub() # clear cached result
write(self.BUILD_DIR + 'build-log.txt',
u'ERROR: test \u039A\n\n\n\n\n\n\n\n\nblah'.encode('utf8'))
response = self.get_build_page()
self.assertIn('Error lines', response)
self.assertIn('No Test Failures', response)
self.assertIn('ERROR</span>: test', response)
self.assertNotIn('blah', response)
def test_build_show_passed_skipped(self):
response = self.get_build_page()
self.assertIn('First', response)
self.assertIn('Second', response)
self.assertIn('Third', response)
self.assertIn('Show 1 Skipped Tests', response)
self.assertIn('Show 1 Passed Tests', response)
def test_build_optional_log(self):
"""Test that passing builds do not show logs by default but display them when requested"""
write(self.BUILD_DIR + 'build-log.txt', 'error or timeout or something')
response = self.get_build_page()
self.assertIn('<a href="?log#log">', response)
self.assertNotIn('timeout', response)
self.assertNotIn('build-log.txt', response)
response = self.get_build_page('?log')
self.assertIn('timeout', response)
self.assertIn('build-log.txt', response)
def test_build_testgrid_links(self):
response = self.get_build_page()
base = 'https://testgrid.k8s.io/k8s#ajob'
self.assertIn('a href="%s"' % base, response)
option = '&include-filter-by-regex=%5EOverall%24%7CThird'
self.assertIn('a href="%s%s"' % (base, option), response)
def test_build_failure_no_text(self):
# Some failures don't have any associated text.
write(self.BUILD_DIR + 'artifacts/junit_01.xml', """
<testsuites>
<testsuite tests="1" failures="1" time="3.274" name="k8s.io/test/integration">
<testcase classname="integration" name="TestUnschedulableNodes" time="0.210">
<failure message="Failed" type=""/>
</testcase>
</testsuite>
</testsuites>""")
response = self.get_build_page()
self.assertIn('TestUnschedulableNodes', response)
self.assertIn('junit_01.xml', response)
def test_build_empty_junit(self):
# Sometimes junit files are actually empty (???)
write(self.BUILD_DIR + 'artifacts/junit_01.xml', '')
response = self.get_build_page()
print response
self.assertIn('No Test Failures', response)
def test_parse_pr_path(self):
def check(prefix, expected):
self.assertEqual(
view_build.parse_pr_path(gcs_path=prefix,
default_org='kubernetes',
default_repo='kubernetes',
),
expected
)
check('kubernetes-jenkins/pr-logs/pull/123', ('123', '', 'kubernetes/kubernetes'))
check('kubernetes-jenkins/pr-logs/pull/charts/123', ('123', 'charts/', 'kubernetes/charts'))
check(
'kubernetes-jenkins/pr-logs/pull/google_cadvisor/296',
('296', 'google/cadvisor/', 'google/cadvisor'))
check(
'kj/pr-logs/pull/kubernetes-sigs_testing_frameworks/49',
('49', 'kubernetes-sigs/testing_frameworks/', 'kubernetes-sigs/testing_frameworks'))
def test_github_commit_links(self):
def check(build_dir, result):
init_build(build_dir)
response = app.get('/build' + build_dir)
self.assertIn(result, response)
check('/kubernetes-jenkins/logs/ci-kubernetes-e2e/2/',
'github.com/kubernetes/kubernetes/commit/')
check('/kubernetes-jenkins/pr-logs/pull/charts/123/e2e/40/',
'github.com/kubernetes/charts/commit/')
check('/kubernetes-jenkins/pr-logs/pull/google_cadvisor/432/e2e/296/',
'github.com/google/cadvisor/commit/')
def test_build_pr_link(self):
""" The build page for a PR build links to the PR results."""
build_dir = '/kubernetes-jenkins/pr-logs/pull/123/e2e/567/'
init_build(build_dir)
response = app.get('/build' + build_dir)
self.assertIn('PR #123', response)
self.assertIn('href="/pr/123"', response)
def test_build_pr_link_other(self):
build_dir = '/kubernetes-jenkins/pr-logs/pull/charts/123/e2e/567/'
init_build(build_dir)
response = app.get('/build' + build_dir)
self.assertIn('PR #123', response)
self.assertIn('href="/pr/charts/123"', response)
def test_build_xref(self):
"""Test that builds show issues that reference them."""
github.models.GHIssueDigest.make(
'org/repo', 123, True, True, [],
{'xrefs': [self.BUILD_DIR[:-1]], 'title': 'an update on testing'}, None).put()
response = app.get('/build' + self.BUILD_DIR)
self.assertIn('PR #123', response)
self.assertIn('an update on testing', response)
self.assertIn('org/repo/issues/123', response)
def test_build_list_xref(self):
"""Test that builds show issues that reference them."""
github.models.GHIssueDigest.make(
'org/repo', 123, False, True, [],
{'xrefs': [self.BUILD_DIR[:-1]], 'title': 'an update on testing'}, None).put()
response = app.get('/builds' + self.JOB_DIR)
self.assertIn('#123', response)
self.assertIn('an update on testing', response)
self.assertIn('org/repo/issues/123', response)
def test_cache(self):
"""Test that caching works at some level."""
response = self.get_build_page()
gcs.delete(self.BUILD_DIR + 'started.json')
gcs.delete(self.BUILD_DIR + 'finished.json')
response2 = self.get_build_page()
self.assertEqual(str(response), str(response2))
def test_build_directory_redir(self):
build_dir = '/kubernetes-jenkins/pr-logs/directory/somejob/1234'
target_dir = '/kubernetes-jenkins/pr-logs/pull/45/somejob/1234'
write(build_dir + '.txt', 'gs:/' + target_dir)
resp = app.get('/build' + build_dir)
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp.location, 'http://localhost/build' + target_dir)
def do_view_build_list_test(self, job_dir='/buck/some-job/', indirect=False):
sta_result = {'timestamp': 12345}
fin_result = {'passed': True, 'result': 'SUCCESS'}
for n in xrange(120):
write('%s%d/started.json' % (job_dir, n), sta_result)
write('%s%d/finished.json' % (job_dir, n), fin_result)
if indirect:
for n in xrange(120):
write('%sdirectory/%d.txt' % (job_dir, n), 'gs:/%s%d' % (job_dir, n))
view_target = job_dir if not indirect else job_dir + 'directory/'
builds, _ = view_build.build_list(view_target, None)
self.assertEqual(builds,
[(str(n), '%s%s' % (job_dir, n), sta_result, fin_result)
for n in range(119, 79, -1)])
# test that ?before works
builds, _ = view_build.build_list(view_target, '80')
self.assertEqual(builds,
[(str(n), '%s%s' % (job_dir, n), sta_result, fin_result)
for n in range(79, 39, -1)])
def test_view_build_list_with_latest(self):
write('/buck/some-job/latest-build.txt', '119')
self.do_view_build_list_test()
def test_view_build_list_with_old_latest(self):
# latest-build.txt is a hint -- it will probe newer by looking for started.json
write('/buck/some-job/latest-build.txt', '110')
self.do_view_build_list_test()
def test_view_build_list_no_latest(self):
self.do_view_build_list_test()
def test_view_build_list_indirect_with_latest(self):
write('/buck/some-job/directory/latest-build.txt', '119')
self.do_view_build_list_test(indirect=True)
def test_view_build_list_indirect_no_latest(self):
self.do_view_build_list_test(indirect=True)
def test_build_list_handler(self):
"""Test that the job page shows a list of builds."""
response = app.get('/builds' + os.path.dirname(self.BUILD_DIR[:-1]))
self.assertIn('/1234/">1234', response)
self.assertIn('gcsweb', response)
def test_job_list(self):
"""Test that the job list shows our job."""
response = app.get('/jobs/kubernetes-jenkins/logs')
self.assertIn('somejob/">somejob</a>', response)
def test_recent_runs_across_prs(self):
"""Test that "Recent Runs Across PRs" links are correct."""
def expect(path, directory):
response = app.get('/builds/' + path)
self.assertIn('href="/builds/%s"' % directory, response)
# pull request job in main repo
expect(
'k-j/pr-logs/pull/514/pull-kubernetes-unit/',
'k-j/pr-logs/directory/pull-kubernetes-unit')
# pull request jobs in different repos
expect(
'k-j/pr-logs/pull/test-infra/4213/pull-test-infra-bazel',
'k-j/pr-logs/directory/pull-test-infra-bazel')
expect(
'i-p/pull/istio_istio/517/istio-presubmit/',
'i-p/directory/istio-presubmit')