blob: 78524f2402ba2836a46123dafe879e16800ad575 [file] [log] [blame]
James E. Blair59fdbac2015-12-07 17:08:06 -08001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira00910c2017-08-23 09:15:04 -070017import json
James E. Blaira92cbc82017-01-23 14:56:49 -080018import os
James E. Blair14abdf42015-12-09 16:11:53 -080019import textwrap
James E. Blair3a098dd2017-10-04 14:37:29 -070020import gc
21from unittest import skip
James E. Blair59fdbac2015-12-07 17:08:06 -080022
James E. Blairb9c0d772017-03-03 14:34:49 -080023import testtools
24
25import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070026from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000027from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080028
James E. Blair59fdbac2015-12-07 17:08:06 -080029
James E. Blair3f876d52016-07-22 13:07:14 -070030class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080031 # A temporary class to hold new tests while others are disabled
32
James E. Blair2a629ec2015-12-22 15:32:02 -080033 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080034
James E. Blair83005782015-12-11 14:46:03 -080035 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080036 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020037 A.addApproval('Code-Review', 2)
38 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080039 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080040 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080041 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080042 self.assertEqual(self.getJobFromHistory('python27').result,
43 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080044 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080045 self.assertEqual(A.reported, 2,
46 "A should report start and success")
47 self.assertIn('tenant-one-gate', A.messages[1],
48 "A should transit tenant-one gate")
49 self.assertNotIn('tenant-two-gate', A.messages[1],
50 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080051
James E. Blair96f26942015-12-09 10:15:59 -080052 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020053 B.addApproval('Code-Review', 2)
54 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080055 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080056 self.assertEqual(self.getJobFromHistory('python27',
57 'org/project2').result,
58 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080059 self.assertEqual(self.getJobFromHistory('project2-test1').result,
60 'SUCCESS')
61 self.assertEqual(B.data['status'], 'MERGED')
62 self.assertEqual(B.reported, 2,
63 "B should report start and success")
64 self.assertIn('tenant-two-gate', B.messages[1],
65 "B should transit tenant-two gate")
66 self.assertNotIn('tenant-one-gate', B.messages[1],
67 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080068
James E. Blair96f26942015-12-09 10:15:59 -080069 self.assertEqual(A.reported, 2, "Activity in tenant two should"
70 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080071
James E. Blair83005782015-12-11 14:46:03 -080072
Tobias Henkel83167622017-06-30 19:45:03 +020073class TestFinal(ZuulTestCase):
74
75 tenant_config_file = 'config/final/main.yaml'
76
77 def test_final_variant_ok(self):
78 # test clean usage of final parent job
79 in_repo_conf = textwrap.dedent(
80 """
81 - project:
82 name: org/project
83 check:
84 jobs:
85 - job-final
86 """)
87
88 file_dict = {'.zuul.yaml': in_repo_conf}
89 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
90 files=file_dict)
91 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
92 self.waitUntilSettled()
93
94 self.assertEqual(A.reported, 1)
95 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
96
97 def test_final_variant_error(self):
98 # test misuse of final parent job
99 in_repo_conf = textwrap.dedent(
100 """
101 - project:
102 name: org/project
103 check:
104 jobs:
105 - job-final:
106 vars:
107 dont_override_this: bar
108 """)
109 file_dict = {'.zuul.yaml': in_repo_conf}
110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
111 files=file_dict)
112 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
113 self.waitUntilSettled()
114
115 # The second patch tried to override some variables.
116 # Thus it should fail.
117 self.assertEqual(A.reported, 1)
118 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
119 self.assertIn('Unable to modify final job', A.messages[0])
120
121 def test_final_inheritance(self):
122 # test misuse of final parent job
123 in_repo_conf = textwrap.dedent(
124 """
125 - job:
126 name: project-test
127 parent: job-final
128
129 - project:
130 name: org/project
131 check:
132 jobs:
133 - project-test
134 """)
135
136 in_repo_playbook = textwrap.dedent(
137 """
138 - hosts: all
139 tasks: []
140 """)
141
142 file_dict = {'.zuul.yaml': in_repo_conf,
143 'playbooks/project-test.yaml': in_repo_playbook}
144 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
145 files=file_dict)
146 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
147 self.waitUntilSettled()
148
149 # The second patch tried to override some variables.
150 # Thus it should fail.
151 self.assertEqual(A.reported, 1)
152 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
153 self.assertIn('Unable to inherit from final job', A.messages[0])
154
155
James E. Blairff555742017-02-19 11:34:27 -0800156class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800157 # A temporary class to hold new tests while others are disabled
158
Tobias Henkelabf973e2017-07-28 10:07:34 +0200159 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800160 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800161
James E. Blair83005782015-12-11 14:46:03 -0800162 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800163 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200164 A.addApproval('Code-Review', 2)
165 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800166 self.waitUntilSettled()
167 self.assertEqual(self.getJobFromHistory('project-test1').result,
168 'SUCCESS')
169 self.assertEqual(A.data['status'], 'MERGED')
170 self.assertEqual(A.reported, 2,
171 "A should report start and success")
172 self.assertIn('tenant-one-gate', A.messages[1],
173 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800174
James E. Blair3a098dd2017-10-04 14:37:29 -0700175 @skip("This test is useful, but not reliable")
176 def test_full_and_dynamic_reconfig(self):
177 self.executor_server.hold_jobs_in_build = True
178 in_repo_conf = textwrap.dedent(
179 """
180 - job:
181 name: project-test1
182
183 - project:
184 name: org/project
185 tenant-one-gate:
186 jobs:
187 - project-test1
188 """)
189
190 file_dict = {'.zuul.yaml': in_repo_conf}
191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
192 files=file_dict)
193 A.addApproval('Code-Review', 2)
194 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
195 self.waitUntilSettled()
196 self.sched.reconfigure(self.config)
197 self.waitUntilSettled()
198
199 gc.collect()
200 pipelines = [obj for obj in gc.get_objects()
201 if isinstance(obj, zuul.model.Pipeline)]
202 self.assertEqual(len(pipelines), 4)
203
204 self.executor_server.hold_jobs_in_build = False
205 self.executor_server.release()
206 self.waitUntilSettled()
207
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700208 def test_dynamic_config(self):
209 in_repo_conf = textwrap.dedent(
210 """
211 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200212 name: project-test1
213
214 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700215 name: project-test2
216
217 - project:
218 name: org/project
219 tenant-one-gate:
220 jobs:
221 - project-test2
222 """)
223
James E. Blairc73c73a2017-01-20 15:15:15 -0800224 in_repo_playbook = textwrap.dedent(
225 """
226 - hosts: all
227 tasks: []
228 """)
229
230 file_dict = {'.zuul.yaml': in_repo_conf,
231 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700232 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800233 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200234 A.addApproval('Code-Review', 2)
235 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700236 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700237 self.assertEqual(A.data['status'], 'MERGED')
238 self.assertEqual(A.reported, 2,
239 "A should report start and success")
240 self.assertIn('tenant-one-gate', A.messages[1],
241 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800242 self.assertHistory([
243 dict(name='project-test2', result='SUCCESS', changes='1,1')])
244
James E. Blairc2a5ed72017-02-20 14:12:01 -0500245 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800246 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500247
James E. Blair646322f2017-01-27 15:50:34 -0800248 # Now that the config change is landed, it should be live for
249 # subsequent changes.
250 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200251 B.addApproval('Code-Review', 2)
252 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800253 self.waitUntilSettled()
254 self.assertEqual(self.getJobFromHistory('project-test2').result,
255 'SUCCESS')
256 self.assertHistory([
257 dict(name='project-test2', result='SUCCESS', changes='1,1'),
258 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800259
Tobias Henkelf02cf512017-07-21 22:55:34 +0200260 def test_dynamic_config_non_existing_job(self):
261 """Test that requesting a non existent job fails"""
262 in_repo_conf = textwrap.dedent(
263 """
264 - job:
265 name: project-test1
266
267 - project:
268 name: org/project
269 check:
270 jobs:
271 - non-existent-job
272 """)
273
274 in_repo_playbook = textwrap.dedent(
275 """
276 - hosts: all
277 tasks: []
278 """)
279
280 file_dict = {'.zuul.yaml': in_repo_conf,
281 'playbooks/project-test2.yaml': in_repo_playbook}
282 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
283 files=file_dict)
284 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
285 self.waitUntilSettled()
286 self.assertEqual(A.reported, 1,
287 "A should report failure")
288 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
289 self.assertIn('Job non-existent-job not defined', A.messages[0],
290 "A should have failed the check pipeline")
291 self.assertHistory([])
292
293 def test_dynamic_config_non_existing_job_in_template(self):
294 """Test that requesting a non existent job fails"""
295 in_repo_conf = textwrap.dedent(
296 """
297 - job:
298 name: project-test1
299
300 - project-template:
301 name: test-template
302 check:
303 jobs:
304 - non-existent-job
305
306 - project:
307 name: org/project
308 templates:
309 - test-template
310 """)
311
312 in_repo_playbook = textwrap.dedent(
313 """
314 - hosts: all
315 tasks: []
316 """)
317
318 file_dict = {'.zuul.yaml': in_repo_conf,
319 'playbooks/project-test2.yaml': in_repo_playbook}
320 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
321 files=file_dict)
322 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
323 self.waitUntilSettled()
324 self.assertEqual(A.reported, 1,
325 "A should report failure")
326 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
327 self.assertIn('Job non-existent-job not defined', A.messages[0],
328 "A should have failed the check pipeline")
329 self.assertHistory([])
330
Tobias Henkel0f714002017-06-30 23:30:52 +0200331 def test_dynamic_config_new_patchset(self):
332 self.executor_server.hold_jobs_in_build = True
333
334 tenant = self.sched.abide.tenants.get('tenant-one')
335 check_pipeline = tenant.layout.pipelines['check']
336
337 in_repo_conf = textwrap.dedent(
338 """
339 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200340 name: project-test1
341
342 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200343 name: project-test2
344
345 - project:
346 name: org/project
347 check:
348 jobs:
349 - project-test2
350 """)
351
352 in_repo_playbook = textwrap.dedent(
353 """
354 - hosts: all
355 tasks: []
356 """)
357
358 file_dict = {'.zuul.yaml': in_repo_conf,
359 'playbooks/project-test2.yaml': in_repo_playbook}
360 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
361 files=file_dict)
362 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
363 self.waitUntilSettled()
364
365 items = check_pipeline.getAllItems()
366 self.assertEqual(items[0].change.number, '1')
367 self.assertEqual(items[0].change.patchset, '1')
368 self.assertTrue(items[0].live)
369
370 in_repo_conf = textwrap.dedent(
371 """
372 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200373 name: project-test1
374
375 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200376 name: project-test2
377
378 - project:
379 name: org/project
380 check:
381 jobs:
382 - project-test1
383 - project-test2
384 """)
385 file_dict = {'.zuul.yaml': in_repo_conf,
386 'playbooks/project-test2.yaml': in_repo_playbook}
387
388 A.addPatchset(files=file_dict)
389 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
390
391 self.waitUntilSettled()
392
393 items = check_pipeline.getAllItems()
394 self.assertEqual(items[0].change.number, '1')
395 self.assertEqual(items[0].change.patchset, '2')
396 self.assertTrue(items[0].live)
397
398 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200399 self.executor_server.release('project-test1')
400 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200401 self.executor_server.release()
402 self.waitUntilSettled()
403
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200404 self.assertHistory([
405 dict(name='project-test2', result='ABORTED', changes='1,1'),
406 dict(name='project-test1', result='SUCCESS', changes='1,2'),
407 dict(name='project-test2', result='SUCCESS', changes='1,2')])
408
James E. Blairff555742017-02-19 11:34:27 -0800409 def test_in_repo_branch(self):
410 in_repo_conf = textwrap.dedent(
411 """
412 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200413 name: project-test1
414
415 - job:
James E. Blairff555742017-02-19 11:34:27 -0800416 name: project-test2
417
418 - project:
419 name: org/project
420 tenant-one-gate:
421 jobs:
422 - project-test2
423 """)
424
425 in_repo_playbook = textwrap.dedent(
426 """
427 - hosts: all
428 tasks: []
429 """)
430
431 file_dict = {'.zuul.yaml': in_repo_conf,
432 'playbooks/project-test2.yaml': in_repo_playbook}
433 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700434 self.fake_gerrit.addEvent(
435 self.fake_gerrit.getFakeBranchCreatedEvent(
436 'org/project', 'stable'))
James E. Blair6069f2b2017-09-26 16:34:11 -0700437 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800438 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
439 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200440 A.addApproval('Code-Review', 2)
441 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800442 self.waitUntilSettled()
443 self.assertEqual(A.data['status'], 'MERGED')
444 self.assertEqual(A.reported, 2,
445 "A should report start and success")
446 self.assertIn('tenant-one-gate', A.messages[1],
447 "A should transit tenant-one gate")
448 self.assertHistory([
449 dict(name='project-test2', result='SUCCESS', changes='1,1')])
450 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800451 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800452
453 # The config change should not affect master.
454 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200455 B.addApproval('Code-Review', 2)
456 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800457 self.waitUntilSettled()
458 self.assertHistory([
459 dict(name='project-test2', result='SUCCESS', changes='1,1'),
460 dict(name='project-test1', result='SUCCESS', changes='2,1')])
461
462 # The config change should be live for further changes on
463 # stable.
464 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200465 C.addApproval('Code-Review', 2)
466 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800467 self.waitUntilSettled()
468 self.assertHistory([
469 dict(name='project-test2', result='SUCCESS', changes='1,1'),
470 dict(name='project-test1', result='SUCCESS', changes='2,1'),
471 dict(name='project-test2', result='SUCCESS', changes='3,1')])
472
James E. Blaira5a12492017-05-03 11:40:48 -0700473 def test_crd_dynamic_config_branch(self):
474 # Test that we can create a job in one repo and be able to use
475 # it from a different branch on a different repo.
476
477 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700478 self.fake_gerrit.addEvent(
479 self.fake_gerrit.getFakeBranchCreatedEvent(
480 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700481
482 in_repo_conf = textwrap.dedent(
483 """
484 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200485 name: project-test1
486
487 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700488 name: project-test2
489
490 - project:
491 name: org/project
492 check:
493 jobs:
494 - project-test2
495 """)
496
497 in_repo_playbook = textwrap.dedent(
498 """
499 - hosts: all
500 tasks: []
501 """)
502
503 file_dict = {'.zuul.yaml': in_repo_conf,
504 'playbooks/project-test2.yaml': in_repo_playbook}
505 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
506 files=file_dict)
507
508 second_repo_conf = textwrap.dedent(
509 """
510 - project:
511 name: org/project1
512 check:
513 jobs:
514 - project-test2
515 """)
516
517 second_file_dict = {'.zuul.yaml': second_repo_conf}
518 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
519 files=second_file_dict)
520 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
521 B.subject, A.data['id'])
522
523 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
524 self.waitUntilSettled()
525 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
526 self.waitUntilSettled()
527
528 self.assertEqual(A.reported, 1, "A should report")
529 self.assertHistory([
530 dict(name='project-test2', result='SUCCESS', changes='1,1'),
531 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
532 ])
533
James E. Blair97043882017-09-06 15:51:17 -0700534 def test_yaml_list_error(self):
535 in_repo_conf = textwrap.dedent(
536 """
537 job: foo
538 """)
539
540 file_dict = {'.zuul.yaml': in_repo_conf}
541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
542 files=file_dict)
543 A.addApproval('Code-Review', 2)
544 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
545 self.waitUntilSettled()
546
547 self.assertEqual(A.data['status'], 'NEW')
548 self.assertEqual(A.reported, 1,
549 "A should report failure")
550 self.assertIn('not a list', A.messages[0],
551 "A should have a syntax error reported")
552
553 def test_yaml_dict_error(self):
554 in_repo_conf = textwrap.dedent(
555 """
556 - job
557 """)
558
559 file_dict = {'.zuul.yaml': in_repo_conf}
560 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
561 files=file_dict)
562 A.addApproval('Code-Review', 2)
563 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
564 self.waitUntilSettled()
565
566 self.assertEqual(A.data['status'], 'NEW')
567 self.assertEqual(A.reported, 1,
568 "A should report failure")
569 self.assertIn('not a dictionary', A.messages[0],
570 "A should have a syntax error reported")
571
572 def test_yaml_key_error(self):
573 in_repo_conf = textwrap.dedent(
574 """
575 - job:
576 name: project-test2
577 """)
578
579 file_dict = {'.zuul.yaml': in_repo_conf}
580 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
581 files=file_dict)
582 A.addApproval('Code-Review', 2)
583 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
584 self.waitUntilSettled()
585
586 self.assertEqual(A.data['status'], 'NEW')
587 self.assertEqual(A.reported, 1,
588 "A should report failure")
589 self.assertIn('has more than one key', A.messages[0],
590 "A should have a syntax error reported")
591
592 def test_yaml_unknown_error(self):
593 in_repo_conf = textwrap.dedent(
594 """
595 - foobar:
596 foo: bar
597 """)
598
599 file_dict = {'.zuul.yaml': in_repo_conf}
600 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
601 files=file_dict)
602 A.addApproval('Code-Review', 2)
603 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
604 self.waitUntilSettled()
605
606 self.assertEqual(A.data['status'], 'NEW')
607 self.assertEqual(A.reported, 1,
608 "A should report failure")
609 self.assertIn('not recognized', A.messages[0],
610 "A should have a syntax error reported")
611
James E. Blair149b69c2017-03-02 10:48:16 -0800612 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800613 in_repo_conf = textwrap.dedent(
614 """
615 - job:
616 name: project-test2
617 foo: error
618 """)
619
620 file_dict = {'.zuul.yaml': in_repo_conf}
621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
622 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200623 A.addApproval('Code-Review', 2)
624 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800625 self.waitUntilSettled()
626
627 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200628 self.assertEqual(A.reported, 1,
629 "A should report failure")
630 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800631 "A should have a syntax error reported")
632
James E. Blair149b69c2017-03-02 10:48:16 -0800633 def test_trusted_syntax_error(self):
634 in_repo_conf = textwrap.dedent(
635 """
636 - job:
637 name: project-test2
638 foo: error
639 """)
640
641 file_dict = {'zuul.yaml': in_repo_conf}
642 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
643 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200644 A.addApproval('Code-Review', 2)
645 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800646 self.waitUntilSettled()
647
648 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200649 self.assertEqual(A.reported, 1,
650 "A should report failure")
651 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800652 "A should have a syntax error reported")
653
James E. Blair6f140c72017-03-03 10:32:07 -0800654 def test_untrusted_yaml_error(self):
655 in_repo_conf = textwrap.dedent(
656 """
657 - job:
658 foo: error
659 """)
660
661 file_dict = {'.zuul.yaml': in_repo_conf}
662 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
663 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200664 A.addApproval('Code-Review', 2)
665 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800666 self.waitUntilSettled()
667
668 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200669 self.assertEqual(A.reported, 1,
670 "A should report failure")
671 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800672 "A should have a syntax error reported")
673
James E. Blairdb04e6a2017-05-03 14:49:36 -0700674 def test_untrusted_shadow_error(self):
675 in_repo_conf = textwrap.dedent(
676 """
677 - job:
678 name: common-config-test
679 """)
680
681 file_dict = {'.zuul.yaml': in_repo_conf}
682 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
683 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200684 A.addApproval('Code-Review', 2)
685 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700686 self.waitUntilSettled()
687
688 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200689 self.assertEqual(A.reported, 1,
690 "A should report failure")
691 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700692 "A should have a syntax error reported")
693
James E. Blaird5656ad2017-06-02 14:29:41 -0700694 def test_untrusted_pipeline_error(self):
695 in_repo_conf = textwrap.dedent(
696 """
697 - pipeline:
698 name: test
699 """)
700
701 file_dict = {'.zuul.yaml': in_repo_conf}
702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
703 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200704 A.addApproval('Code-Review', 2)
705 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700706 self.waitUntilSettled()
707
708 self.assertEqual(A.data['status'], 'NEW')
709 self.assertEqual(A.reported, 1,
710 "A should report failure")
711 self.assertIn('Pipelines may not be defined', A.messages[0],
712 "A should have a syntax error reported")
713
714 def test_untrusted_project_error(self):
715 in_repo_conf = textwrap.dedent(
716 """
717 - project:
718 name: org/project1
719 """)
720
721 file_dict = {'.zuul.yaml': in_repo_conf}
722 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
723 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200724 A.addApproval('Code-Review', 2)
725 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700726 self.waitUntilSettled()
727
728 self.assertEqual(A.data['status'], 'NEW')
729 self.assertEqual(A.reported, 1,
730 "A should report failure")
731 self.assertIn('the only project definition permitted', A.messages[0],
732 "A should have a syntax error reported")
733
James E. Blaire64b0e42017-06-08 11:23:34 -0700734 def test_duplicate_node_error(self):
735 in_repo_conf = textwrap.dedent(
736 """
737 - nodeset:
738 name: duplicate
739 nodes:
740 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700741 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700742 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700743 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700744 """)
745
746 file_dict = {'.zuul.yaml': in_repo_conf}
747 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
748 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200749 A.addApproval('Code-Review', 2)
750 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700751 self.waitUntilSettled()
752
753 self.assertEqual(A.data['status'], 'NEW')
754 self.assertEqual(A.reported, 1,
755 "A should report failure")
756 self.assertIn('appears multiple times', A.messages[0],
757 "A should have a syntax error reported")
758
759 def test_duplicate_group_error(self):
760 in_repo_conf = textwrap.dedent(
761 """
762 - nodeset:
763 name: duplicate
764 nodes:
765 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700766 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700767 groups:
768 - name: group
769 nodes: compute
770 - name: group
771 nodes: compute
772 """)
773
774 file_dict = {'.zuul.yaml': in_repo_conf}
775 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
776 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200777 A.addApproval('Code-Review', 2)
778 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700779 self.waitUntilSettled()
780
781 self.assertEqual(A.data['status'], 'NEW')
782 self.assertEqual(A.reported, 1,
783 "A should report failure")
784 self.assertIn('appears multiple times', A.messages[0],
785 "A should have a syntax error reported")
786
James E. Blair4ae399f2017-09-20 17:15:09 -0700787 def test_secret_not_found_error(self):
788 in_repo_conf = textwrap.dedent(
789 """
790 - job:
791 name: test
792 secrets: does-not-exist
793 """)
794
795 file_dict = {'.zuul.yaml': in_repo_conf}
796 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
797 files=file_dict)
798 A.addApproval('Code-Review', 2)
799 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
800 self.waitUntilSettled()
801
802 self.assertEqual(A.data['status'], 'NEW')
803 self.assertEqual(A.reported, 1,
804 "A should report failure")
805 self.assertIn('secret "does-not-exist" was not found', A.messages[0],
806 "A should have a syntax error reported")
807
808 def test_nodeset_not_found_error(self):
809 in_repo_conf = textwrap.dedent(
810 """
811 - job:
812 name: test
813 nodeset: does-not-exist
814 """)
815
816 file_dict = {'.zuul.yaml': in_repo_conf}
817 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
818 files=file_dict)
819 A.addApproval('Code-Review', 2)
820 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
821 self.waitUntilSettled()
822
823 self.assertEqual(A.data['status'], 'NEW')
824 self.assertEqual(A.reported, 1,
825 "A should report failure")
826 self.assertIn('nodeset "does-not-exist" was not found', A.messages[0],
827 "A should have a syntax error reported")
828
James E. Blair89e25eb2017-09-26 09:11:31 -0700829 def test_template_not_found_error(self):
830 in_repo_conf = textwrap.dedent(
831 """
832 - job:
833 name: project-test1
834 - project:
835 name: org/project
836 templates:
837 - does-not-exist
838 """)
839
840 file_dict = {'.zuul.yaml': in_repo_conf}
841 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
842 files=file_dict)
843 A.addApproval('Code-Review', 2)
844 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
845 self.waitUntilSettled()
846
847 self.assertEqual(A.data['status'], 'NEW')
848 self.assertEqual(A.reported, 1,
849 "A should report failure")
850 self.assertIn('project template "does-not-exist" was not found',
851 A.messages[0],
852 "A should have a syntax error reported")
853
James E. Blair09f9ffe2017-07-11 15:30:25 -0700854 def test_multi_repo(self):
855 downstream_repo_conf = textwrap.dedent(
856 """
857 - project:
858 name: org/project1
859 tenant-one-gate:
860 jobs:
861 - project-test1
862
863 - job:
864 name: project1-test1
865 parent: project-test1
866 """)
867
868 file_dict = {'.zuul.yaml': downstream_repo_conf}
869 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
870 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200871 A.addApproval('Code-Review', 2)
872 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700873 self.waitUntilSettled()
874
875 self.assertEqual(A.data['status'], 'MERGED')
876 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
877 self.waitUntilSettled()
878
879 upstream_repo_conf = textwrap.dedent(
880 """
881 - job:
882 name: project-test1
883
884 - job:
885 name: project-test2
886
887 - project:
888 name: org/project
889 tenant-one-gate:
890 jobs:
891 - project-test1
892 """)
893
894 file_dict = {'.zuul.yaml': upstream_repo_conf}
895 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
896 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200897 B.addApproval('Code-Review', 2)
898 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700899 self.waitUntilSettled()
900
901 self.assertEqual(B.data['status'], 'MERGED')
902 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
903 self.waitUntilSettled()
904
905 tenant = self.sched.abide.tenants.get('tenant-one')
906 # Ensure the latest change is reflected in the config; if it
907 # isn't this will raise an exception.
908 tenant.layout.getJob('project-test2')
909
James E. Blair332636e2017-09-05 10:14:35 -0700910 def test_pipeline_error(self):
911 with open(os.path.join(FIXTURE_DIR,
912 'config/in-repo/git/',
913 'common-config/zuul.yaml')) as f:
914 base_common_config = f.read()
915
916 in_repo_conf_A = textwrap.dedent(
917 """
918 - pipeline:
919 name: periodic
920 foo: error
921 """)
922
923 file_dict = {'zuul.yaml': None,
924 'zuul.d/main.yaml': base_common_config,
925 'zuul.d/test1.yaml': in_repo_conf_A}
926 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
927 files=file_dict)
928 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
929 self.waitUntilSettled()
930 self.assertEqual(A.reported, 1,
931 "A should report failure")
932 self.assertIn('syntax error',
933 A.messages[0],
934 "A should have an error reported")
935
936 def test_change_series_error(self):
937 with open(os.path.join(FIXTURE_DIR,
938 'config/in-repo/git/',
939 'common-config/zuul.yaml')) as f:
940 base_common_config = f.read()
941
942 in_repo_conf_A = textwrap.dedent(
943 """
944 - pipeline:
945 name: periodic
946 foo: error
947 """)
948
949 file_dict = {'zuul.yaml': None,
950 'zuul.d/main.yaml': base_common_config,
951 'zuul.d/test1.yaml': in_repo_conf_A}
952 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
953 files=file_dict)
954
955 in_repo_conf_B = textwrap.dedent(
956 """
957 - job:
958 name: project-test2
959 foo: error
960 """)
961
962 file_dict = {'zuul.yaml': None,
963 'zuul.d/main.yaml': base_common_config,
964 'zuul.d/test1.yaml': in_repo_conf_A,
965 'zuul.d/test2.yaml': in_repo_conf_B}
966 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
967 files=file_dict)
968 B.setDependsOn(A, 1)
969 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
970 C.setDependsOn(B, 1)
971 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
972 self.waitUntilSettled()
973
974 self.assertEqual(C.reported, 1,
975 "C should report failure")
976 self.assertIn('depends on a change that failed to merge',
977 C.messages[0],
978 "C should have an error reported")
979
James E. Blairc73c73a2017-01-20 15:15:15 -0800980
James E. Blairc9455002017-09-06 09:22:19 -0700981class TestInRepoJoin(ZuulTestCase):
982 # In this config, org/project is not a member of any pipelines, so
983 # that we may test the changes that cause it to join them.
984
985 tenant_config_file = 'config/in-repo-join/main.yaml'
986
987 def test_dynamic_dependent_pipeline(self):
988 # Test dynamically adding a project to a
989 # dependent pipeline for the first time
990 self.executor_server.hold_jobs_in_build = True
991
992 tenant = self.sched.abide.tenants.get('tenant-one')
993 gate_pipeline = tenant.layout.pipelines['gate']
994
995 in_repo_conf = textwrap.dedent(
996 """
997 - job:
998 name: project-test1
999
1000 - job:
1001 name: project-test2
1002
1003 - project:
1004 name: org/project
1005 gate:
1006 jobs:
1007 - project-test2
1008 """)
1009
1010 in_repo_playbook = textwrap.dedent(
1011 """
1012 - hosts: all
1013 tasks: []
1014 """)
1015
1016 file_dict = {'.zuul.yaml': in_repo_conf,
1017 'playbooks/project-test2.yaml': in_repo_playbook}
1018 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1019 files=file_dict)
1020 A.addApproval('Code-Review', 2)
1021 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1022 self.waitUntilSettled()
1023
1024 items = gate_pipeline.getAllItems()
1025 self.assertEqual(items[0].change.number, '1')
1026 self.assertEqual(items[0].change.patchset, '1')
1027 self.assertTrue(items[0].live)
1028
1029 self.executor_server.hold_jobs_in_build = False
1030 self.executor_server.release()
1031 self.waitUntilSettled()
1032
1033 # Make sure the dynamic queue got cleaned up
1034 self.assertEqual(gate_pipeline.queues, [])
1035
1036 def test_dynamic_dependent_pipeline_failure(self):
1037 # Test that a change behind a failing change adding a project
1038 # to a dependent pipeline is dequeued.
1039 self.executor_server.hold_jobs_in_build = True
1040
1041 in_repo_conf = textwrap.dedent(
1042 """
1043 - job:
1044 name: project-test1
1045
1046 - project:
1047 name: org/project
1048 gate:
1049 jobs:
1050 - project-test1
1051 """)
1052
1053 file_dict = {'.zuul.yaml': in_repo_conf}
1054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1055 files=file_dict)
1056 self.executor_server.failJob('project-test1', A)
1057 A.addApproval('Code-Review', 2)
1058 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1059 self.waitUntilSettled()
1060
1061 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1062 B.addApproval('Code-Review', 2)
1063 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1064 self.waitUntilSettled()
1065
James E. Blair3490c5d2017-09-07 08:33:23 -07001066 self.orderedRelease()
James E. Blairc9455002017-09-06 09:22:19 -07001067 self.waitUntilSettled()
1068 self.assertEqual(A.reported, 2,
1069 "A should report start and failure")
1070 self.assertEqual(A.data['status'], 'NEW')
1071 self.assertEqual(B.reported, 1,
1072 "B should report start")
1073 self.assertHistory([
1074 dict(name='project-test1', result='FAILURE', changes='1,1'),
James E. Blair3490c5d2017-09-07 08:33:23 -07001075 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
James E. Blairc9455002017-09-06 09:22:19 -07001076 ], ordered=False)
1077
James E. Blair0af198f2017-09-06 09:52:35 -07001078 def test_dynamic_dependent_pipeline_absent(self):
1079 # Test that a series of dependent changes don't report merge
1080 # failures to a pipeline they aren't in.
1081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1082 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1083 B.setDependsOn(A, 1)
1084
1085 A.addApproval('Code-Review', 2)
1086 A.addApproval('Approved', 1)
1087 B.addApproval('Code-Review', 2)
1088 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1089 self.waitUntilSettled()
1090 self.assertEqual(A.reported, 0,
1091 "A should not report")
1092 self.assertEqual(A.data['status'], 'NEW')
1093 self.assertEqual(B.reported, 0,
1094 "B should not report")
1095 self.assertEqual(B.data['status'], 'NEW')
1096 self.assertHistory([])
1097
James E. Blairc9455002017-09-06 09:22:19 -07001098
James E. Blairc73c73a2017-01-20 15:15:15 -08001099class TestAnsible(AnsibleZuulTestCase):
1100 # A temporary class to hold new tests while others are disabled
1101
1102 tenant_config_file = 'config/ansible/main.yaml'
1103
1104 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +11001105 # Keep the jobdir around so we can inspect contents if an
1106 # assert fails.
1107 self.executor_server.keep_jobdir = True
1108 # Output extra ansible info so we might see errors.
1109 self.executor_server.verbose = True
1110 # Add a site variables file, used by check-vars
1111 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
1112 'variables.yaml')
1113 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -08001114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1115 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1116 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +02001117 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +11001118 with self.jobLog(build_timeout):
1119 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001120 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +11001121 with self.jobLog(build_faillocal):
1122 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001123 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +11001124 with self.jobLog(build_failpost):
1125 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001126 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +11001127 with self.jobLog(build_check_vars):
1128 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -05001129 build_check_secret_names = self.getJobFromHistory('check-secret-names')
1130 with self.jobLog(build_check_secret_names):
1131 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001132 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +11001133 with self.jobLog(build_hello):
1134 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001135 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +11001136 with self.jobLog(build_python27):
1137 self.assertEqual(build_python27.result, 'SUCCESS')
1138 flag_path = os.path.join(self.test_root,
1139 build_python27.uuid + '.flag')
1140 self.assertTrue(os.path.exists(flag_path))
1141 copied_path = os.path.join(self.test_root, build_python27.uuid +
1142 '.copied')
1143 self.assertTrue(os.path.exists(copied_path))
1144 failed_path = os.path.join(self.test_root, build_python27.uuid +
1145 '.failed')
1146 self.assertFalse(os.path.exists(failed_path))
1147 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
1148 '.pre.flag')
1149 self.assertTrue(os.path.exists(pre_flag_path))
1150 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
1151 '.post.flag')
1152 self.assertTrue(os.path.exists(post_flag_path))
1153 bare_role_flag_path = os.path.join(self.test_root,
1154 build_python27.uuid +
1155 '.bare-role.flag')
1156 self.assertTrue(os.path.exists(bare_role_flag_path))
1157 secrets_path = os.path.join(self.test_root,
1158 build_python27.uuid + '.secrets')
1159 with open(secrets_path) as f:
1160 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -08001161
Jamie Lennox7655b552017-03-17 12:33:38 +11001162 msg = A.messages[0]
1163 success = "{} https://success.example.com/zuul-logs/{}"
1164 fail = "{} https://failure.example.com/zuul-logs/{}"
1165 self.assertIn(success.format("python27", build_python27.uuid), msg)
1166 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
1167 self.assertIn(success.format("check-vars",
1168 build_check_vars.uuid), msg)
1169 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
1170 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
1171 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +02001172
James E. Blairabbaa6f2017-04-06 16:11:44 -07001173 def _add_job(self, job_name):
1174 conf = textwrap.dedent(
1175 """
1176 - job:
1177 name: %s
1178
1179 - project:
1180 name: org/plugin-project
1181 check:
1182 jobs:
1183 - %s
1184 """ % (job_name, job_name))
1185
1186 file_dict = {'.zuul.yaml': conf}
1187 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1188 files=file_dict)
1189 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1190 self.waitUntilSettled()
1191
1192 def test_plugins(self):
1193 # Keep the jobdir around so we can inspect contents if an
1194 # assert fails.
1195 self.executor_server.keep_jobdir = True
1196 # Output extra ansible info so we might see errors.
1197 self.executor_server.verbose = True
1198
1199 count = 0
1200 plugin_tests = [
1201 ('passwd', 'FAILURE'),
1202 ('cartesian', 'SUCCESS'),
1203 ('consul_kv', 'FAILURE'),
1204 ('credstash', 'FAILURE'),
1205 ('csvfile_good', 'SUCCESS'),
1206 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001207 ('uri_bad_path', 'FAILURE'),
1208 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001209 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001210 ('file_local_good', 'SUCCESS'),
1211 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001212 ]
1213 for job_name, result in plugin_tests:
1214 count += 1
1215 self._add_job(job_name)
1216
1217 job = self.getJobFromHistory(job_name)
1218 with self.jobLog(job):
1219 self.assertEqual(count, len(self.history))
1220 build = self.history[-1]
1221 self.assertEqual(build.result, result)
1222
1223 # TODOv3(jeblair): parse the ansible output and verify we're
1224 # getting the exception we expect.
1225
James E. Blairb9c0d772017-03-03 14:34:49 -08001226
James E. Blaira4d4eef2017-06-30 14:49:17 -07001227class TestPrePlaybooks(AnsibleZuulTestCase):
1228 # A temporary class to hold new tests while others are disabled
1229
1230 tenant_config_file = 'config/pre-playbook/main.yaml'
1231
1232 def test_pre_playbook_fail(self):
1233 # Test that we run the post playbooks (but not the actual
1234 # playbook) when a pre-playbook fails.
1235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1236 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1237 self.waitUntilSettled()
1238 build = self.getJobFromHistory('python27')
1239 self.assertIsNone(build.result)
1240 self.assertIn('RETRY_LIMIT', A.messages[0])
1241 flag_path = os.path.join(self.test_root, build.uuid +
1242 '.main.flag')
1243 self.assertFalse(os.path.exists(flag_path))
1244 pre_flag_path = os.path.join(self.test_root, build.uuid +
1245 '.pre.flag')
1246 self.assertFalse(os.path.exists(pre_flag_path))
1247 post_flag_path = os.path.join(self.test_root, build.uuid +
1248 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001249 self.assertTrue(os.path.exists(post_flag_path),
1250 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001251
1252
James E. Blairb9c0d772017-03-03 14:34:49 -08001253class TestBrokenConfig(ZuulTestCase):
1254 # Test that we get an appropriate syntax error if we start with a
1255 # broken config.
1256
1257 tenant_config_file = 'config/broken/main.yaml'
1258
1259 def setUp(self):
1260 with testtools.ExpectedException(
1261 zuul.configloader.ConfigurationSyntaxError,
1262 "\nZuul encountered a syntax error"):
1263 super(TestBrokenConfig, self).setUp()
1264
1265 def test_broken_config_on_startup(self):
1266 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001267
1268
1269class TestProjectKeys(ZuulTestCase):
1270 # Test that we can generate project keys
1271
1272 # Normally the test infrastructure copies a static key in place
1273 # for each project before starting tests. This saves time because
1274 # Zuul's automatic key-generation on startup can be slow. To make
1275 # sure we exercise that code, in this test we allow Zuul to create
1276 # keys for the project on startup.
1277 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001278 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001279 tenant_config_file = 'config/in-repo/main.yaml'
1280
1281 def test_key_generation(self):
1282 key_root = os.path.join(self.state_root, 'keys')
1283 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1284 # Make sure that a proper key was created on startup
1285 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001286 private_key, public_key = \
1287 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001288
1289 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1290 fixture_private_key = i.read()
1291
1292 # Make sure that we didn't just end up with the static fixture
1293 # key
1294 self.assertNotEqual(fixture_private_key, private_key)
1295
1296 # Make sure it's the right length
1297 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001298
1299
James E. Blairbb94dfa2017-07-11 07:45:19 -07001300class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001301 def _assertRolePath(self, build, playbook, content):
1302 path = os.path.join(self.test_root, build.uuid,
1303 'ansible', playbook, 'ansible.cfg')
1304 roles_paths = []
1305 with open(path) as f:
1306 for line in f:
1307 if line.startswith('roles_path'):
1308 roles_paths.append(line)
1309 print(roles_paths)
1310 if content:
1311 self.assertEqual(len(roles_paths), 1,
1312 "Should have one roles_path line in %s" %
1313 (playbook,))
1314 self.assertIn(content, roles_paths[0])
1315 else:
1316 self.assertEqual(len(roles_paths), 0,
1317 "Should have no roles_path line in %s" %
1318 (playbook,))
1319
James E. Blairbb94dfa2017-07-11 07:45:19 -07001320
1321class TestRoles(RoleTestCase):
1322 tenant_config_file = 'config/roles/main.yaml'
1323
James E. Blairbce76932017-05-04 10:03:15 -07001324 def test_role(self):
1325 # This exercises a proposed change to a role being checked out
1326 # and used.
1327 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1328 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1329 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1330 B.subject, A.data['id'])
1331 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1332 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1333 self.waitUntilSettled()
1334 self.assertHistory([
1335 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1336 ])
James E. Blair6459db12017-06-29 14:57:20 -07001337
James E. Blair1b27f6a2017-07-14 14:09:07 -07001338 def test_role_inheritance(self):
1339 self.executor_server.hold_jobs_in_build = True
1340 conf = textwrap.dedent(
1341 """
1342 - job:
1343 name: parent
1344 roles:
1345 - zuul: bare-role
1346 pre-run: playbooks/parent-pre
1347 post-run: playbooks/parent-post
1348
1349 - job:
1350 name: project-test
1351 parent: parent
1352 roles:
1353 - zuul: org/project
1354
1355 - project:
1356 name: org/project
1357 check:
1358 jobs:
1359 - project-test
1360 """)
1361
1362 file_dict = {'.zuul.yaml': conf}
1363 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1364 files=file_dict)
1365 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1366 self.waitUntilSettled()
1367
1368 self.assertEqual(len(self.builds), 1)
1369 build = self.getBuildByName('project-test')
1370 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1371 self._assertRolePath(build, 'playbook_0', 'role_0')
1372 self._assertRolePath(build, 'playbook_0', 'role_1')
1373 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1374
1375 self.executor_server.hold_jobs_in_build = False
1376 self.executor_server.release()
1377 self.waitUntilSettled()
1378
1379 self.assertHistory([
1380 dict(name='project-test', result='SUCCESS', changes='1,1'),
1381 ])
1382
James E. Blair6f699732017-07-18 14:19:11 -07001383 def test_role_error(self):
1384 conf = textwrap.dedent(
1385 """
1386 - job:
1387 name: project-test
1388 roles:
1389 - zuul: common-config
1390
1391 - project:
1392 name: org/project
1393 check:
1394 jobs:
1395 - project-test
1396 """)
1397
1398 file_dict = {'.zuul.yaml': conf}
1399 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1400 files=file_dict)
1401 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1402 self.waitUntilSettled()
1403 self.assertIn(
1404 '- project-test project-test : ERROR Unable to find role',
1405 A.messages[-1])
1406
James E. Blair6459db12017-06-29 14:57:20 -07001407
James E. Blairbb94dfa2017-07-11 07:45:19 -07001408class TestImplicitRoles(RoleTestCase):
1409 tenant_config_file = 'config/implicit-roles/main.yaml'
1410
1411 def test_missing_roles(self):
1412 # Test implicit and explicit roles for a project which does
1413 # not have roles. The implicit role should be silently
1414 # ignored since the project doesn't supply roles, but if a
1415 # user declares an explicit role, it should error.
1416 self.executor_server.hold_jobs_in_build = True
1417 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1418 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1419 self.waitUntilSettled()
1420
1421 self.assertEqual(len(self.builds), 2)
1422 build = self.getBuildByName('implicit-role-fail')
1423 self._assertRolePath(build, 'playbook_0', None)
1424
1425 self.executor_server.hold_jobs_in_build = False
1426 self.executor_server.release()
1427 self.waitUntilSettled()
1428 # The retry_limit doesn't get recorded
1429 self.assertHistory([
1430 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1431 ])
1432
1433 def test_roles(self):
1434 # Test implicit and explicit roles for a project which does
1435 # have roles. In both cases, we should end up with the role
1436 # in the path. In the explicit case, ensure we end up with
1437 # the name we specified.
1438 self.executor_server.hold_jobs_in_build = True
1439 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1440 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1441 self.waitUntilSettled()
1442
1443 self.assertEqual(len(self.builds), 2)
1444 build = self.getBuildByName('implicit-role-ok')
1445 self._assertRolePath(build, 'playbook_0', 'role_0')
1446
1447 build = self.getBuildByName('explicit-role-ok')
1448 self._assertRolePath(build, 'playbook_0', 'role_0')
1449
1450 self.executor_server.hold_jobs_in_build = False
1451 self.executor_server.release()
1452 self.waitUntilSettled()
1453 self.assertHistory([
1454 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1455 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1456 ], ordered=False)
1457
1458
James E. Blair6459db12017-06-29 14:57:20 -07001459class TestShadow(ZuulTestCase):
1460 tenant_config_file = 'config/shadow/main.yaml'
1461
1462 def test_shadow(self):
1463 # Test that a repo is allowed to shadow another's job definitions.
1464 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1465 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1466 self.waitUntilSettled()
1467 self.assertHistory([
1468 dict(name='test1', result='SUCCESS', changes='1,1'),
1469 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001470 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001471
1472
1473class TestDataReturn(AnsibleZuulTestCase):
1474 tenant_config_file = 'config/data-return/main.yaml'
1475
1476 def test_data_return(self):
1477 # This exercises a proposed change to a role being checked out
1478 # and used.
1479 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1480 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1481 self.waitUntilSettled()
1482 self.assertHistory([
1483 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001484 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1485 ], ordered=False)
1486 self.assertIn('- data-return http://example.com/test/log/url/',
1487 A.messages[-1])
1488 self.assertIn('- data-return-relative '
1489 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001490 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001491
1492
1493class TestDiskAccounting(AnsibleZuulTestCase):
1494 config_file = 'zuul-disk-accounting.conf'
1495 tenant_config_file = 'config/disk-accountant/main.yaml'
1496
1497 def test_disk_accountant_kills_job(self):
1498 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1499 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1500 self.waitUntilSettled()
1501 self.assertHistory([
1502 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001503
1504
1505class TestMaxNodesPerJob(AnsibleZuulTestCase):
1506 tenant_config_file = 'config/multi-tenant/main.yaml'
1507
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001508 def test_max_timeout_exceeded(self):
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001509 in_repo_conf = textwrap.dedent(
1510 """
1511 - job:
1512 name: test-job
James E. Blair7e3e6882017-09-20 15:47:13 -07001513 nodeset:
1514 nodes:
1515 - name: node01
1516 label: fake
1517 - name: node02
1518 label: fake
1519 - name: node03
1520 label: fake
1521 - name: node04
1522 label: fake
1523 - name: node05
1524 label: fake
1525 - name: node06
1526 label: fake
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001527 """)
1528 file_dict = {'.zuul.yaml': in_repo_conf}
1529 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1530 files=file_dict)
1531 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1532 self.waitUntilSettled()
1533 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1534 A.messages[0], "A should fail because of nodes limit")
1535
1536 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1537 files=file_dict)
1538 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1539 self.waitUntilSettled()
1540 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1541 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001542
1543
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001544class TestMaxTimeout(AnsibleZuulTestCase):
1545 tenant_config_file = 'config/multi-tenant/main.yaml'
1546
1547 def test_max_nodes_reached(self):
1548 in_repo_conf = textwrap.dedent(
1549 """
1550 - job:
1551 name: test-job
1552 timeout: 3600
1553 """)
1554 file_dict = {'.zuul.yaml': in_repo_conf}
1555 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1556 files=file_dict)
1557 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1558 self.waitUntilSettled()
1559 self.assertIn('The job "test-job" exceeds tenant max-job-timeout',
1560 A.messages[0], "A should fail because of timeout limit")
1561
1562 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1563 files=file_dict)
1564 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1565 self.waitUntilSettled()
1566 self.assertNotIn("exceeds tenant max-job-timeout", B.messages[0],
1567 "B should not fail because of timeout limit")
1568
1569
James E. Blair2bab6e72017-08-07 09:52:45 -07001570class TestBaseJobs(ZuulTestCase):
1571 tenant_config_file = 'config/base-jobs/main.yaml'
1572
1573 def test_multiple_base_jobs(self):
1574 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1575 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1576 self.waitUntilSettled()
1577 self.assertHistory([
1578 dict(name='my-job', result='SUCCESS', changes='1,1'),
1579 dict(name='other-job', result='SUCCESS', changes='1,1'),
1580 ], ordered=False)
1581 self.assertEqual(self.getJobFromHistory('my-job').
1582 parameters['zuul']['jobtags'],
1583 ['mybase'])
1584 self.assertEqual(self.getJobFromHistory('other-job').
1585 parameters['zuul']['jobtags'],
1586 ['otherbase'])
1587
1588 def test_untrusted_base_job(self):
1589 """Test that a base job may not be defined in an untrusted repo"""
1590 in_repo_conf = textwrap.dedent(
1591 """
1592 - job:
1593 name: fail-base
1594 parent: null
1595 """)
1596
1597 file_dict = {'.zuul.yaml': in_repo_conf}
1598 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1599 files=file_dict)
1600 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1601 self.waitUntilSettled()
1602 self.assertEqual(A.reported, 1,
1603 "A should report failure")
1604 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1605 self.assertIn('Base jobs must be defined in config projects',
1606 A.messages[0])
1607 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001608
1609
1610class TestSecretLeaks(AnsibleZuulTestCase):
1611 tenant_config_file = 'config/secret-leaks/main.yaml'
1612
1613 def searchForContent(self, path, content):
1614 matches = []
1615 for (dirpath, dirnames, filenames) in os.walk(path):
1616 for filename in filenames:
1617 filepath = os.path.join(dirpath, filename)
1618 with open(filepath, 'rb') as f:
1619 if content in f.read():
1620 matches.append(filepath[len(path):])
1621 return matches
1622
1623 def _test_secret_file(self):
1624 # Or rather -- test that they *don't* leak.
1625 # Keep the jobdir around so we can inspect contents.
1626 self.executor_server.keep_jobdir = True
1627 conf = textwrap.dedent(
1628 """
1629 - project:
1630 name: org/project
1631 check:
1632 jobs:
1633 - secret-file
1634 """)
1635
1636 file_dict = {'.zuul.yaml': conf}
1637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1638 files=file_dict)
1639 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1640 self.waitUntilSettled()
1641 self.assertHistory([
1642 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1643 ], ordered=False)
1644 matches = self.searchForContent(self.history[0].jobdir.root,
1645 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001646 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001647 set(matches))
1648
1649 def test_secret_file(self):
1650 self._test_secret_file()
1651
1652 def test_secret_file_verbose(self):
1653 # Output extra ansible info to exercise alternate logging code
1654 # paths.
1655 self.executor_server.verbose = True
1656 self._test_secret_file()
1657
1658 def _test_secret_file_fail(self):
1659 # Or rather -- test that they *don't* leak.
1660 # Keep the jobdir around so we can inspect contents.
1661 self.executor_server.keep_jobdir = True
1662 conf = textwrap.dedent(
1663 """
1664 - project:
1665 name: org/project
1666 check:
1667 jobs:
1668 - secret-file-fail
1669 """)
1670
1671 file_dict = {'.zuul.yaml': conf}
1672 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1673 files=file_dict)
1674 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1675 self.waitUntilSettled()
1676 self.assertHistory([
1677 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1678 ], ordered=False)
1679 matches = self.searchForContent(self.history[0].jobdir.root,
1680 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001681 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001682 set(matches))
1683
1684 def test_secret_file_fail(self):
1685 self._test_secret_file_fail()
1686
1687 def test_secret_file_fail_verbose(self):
1688 # Output extra ansible info to exercise alternate logging code
1689 # paths.
1690 self.executor_server.verbose = True
1691 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001692
1693
1694class TestJobOutput(AnsibleZuulTestCase):
1695 tenant_config_file = 'config/job-output/main.yaml'
1696
1697 def _get_file(self, build, path):
1698 p = os.path.join(build.jobdir.root, path)
1699 with open(p) as f:
1700 return f.read()
1701
1702 def test_job_output(self):
1703 # Verify that command standard output appears in the job output
1704
1705 # This currently only verifies we receive output from
1706 # localhost. Notably, it does not verify we receive output
1707 # via zuul_console streaming.
1708 self.executor_server.keep_jobdir = True
1709 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1710 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1711 self.waitUntilSettled()
1712 self.assertHistory([
1713 dict(name='job-output', result='SUCCESS', changes='1,1'),
1714 ], ordered=False)
1715
1716 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1717 j = json.loads(self._get_file(self.history[0],
1718 'work/logs/job-output.json'))
1719 self.assertEqual(token,
1720 j[0]['plays'][0]['tasks'][0]
1721 ['hosts']['localhost']['stdout'])
1722
1723 print(self._get_file(self.history[0],
1724 'work/logs/job-output.txt'))
1725 self.assertIn(token,
1726 self._get_file(self.history[0],
1727 'work/logs/job-output.txt'))