blob: b9ae04bc2f126c9ff3a94a89e42bf79851efe622 [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
Monty Taylor0e2489a2017-10-10 11:57:29 -050017import io
James E. Blaira00910c2017-08-23 09:15:04 -070018import json
Monty Taylor0e2489a2017-10-10 11:57:29 -050019import logging
James E. Blaira92cbc82017-01-23 14:56:49 -080020import os
James E. Blair14abdf42015-12-09 16:11:53 -080021import textwrap
James E. Blair3a098dd2017-10-04 14:37:29 -070022import gc
James E. Blairbacbb882017-10-17 09:48:23 -070023import time
James E. Blair3a098dd2017-10-04 14:37:29 -070024from unittest import skip
James E. Blair59fdbac2015-12-07 17:08:06 -080025
James E. Blairb9c0d772017-03-03 14:34:49 -080026import testtools
27
28import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070029from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000030from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080031
James E. Blair59fdbac2015-12-07 17:08:06 -080032
James E. Blair3f876d52016-07-22 13:07:14 -070033class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080034 # A temporary class to hold new tests while others are disabled
35
James E. Blair2a629ec2015-12-22 15:32:02 -080036 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080037
James E. Blair83005782015-12-11 14:46:03 -080038 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080039 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020040 A.addApproval('Code-Review', 2)
41 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080042 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080043 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080044 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080045 self.assertEqual(self.getJobFromHistory('python27').result,
46 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080047 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080048 self.assertEqual(A.reported, 2,
49 "A should report start and success")
50 self.assertIn('tenant-one-gate', A.messages[1],
51 "A should transit tenant-one gate")
52 self.assertNotIn('tenant-two-gate', A.messages[1],
53 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080054
James E. Blair96f26942015-12-09 10:15:59 -080055 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020056 B.addApproval('Code-Review', 2)
57 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080058 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080059 self.assertEqual(self.getJobFromHistory('python27',
60 'org/project2').result,
61 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080062 self.assertEqual(self.getJobFromHistory('project2-test1').result,
63 'SUCCESS')
64 self.assertEqual(B.data['status'], 'MERGED')
65 self.assertEqual(B.reported, 2,
66 "B should report start and success")
67 self.assertIn('tenant-two-gate', B.messages[1],
68 "B should transit tenant-two gate")
69 self.assertNotIn('tenant-one-gate', B.messages[1],
70 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080071
James E. Blair96f26942015-12-09 10:15:59 -080072 self.assertEqual(A.reported, 2, "Activity in tenant two should"
73 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080074
James E. Blair83005782015-12-11 14:46:03 -080075
Tobias Henkel83167622017-06-30 19:45:03 +020076class TestFinal(ZuulTestCase):
77
78 tenant_config_file = 'config/final/main.yaml'
79
80 def test_final_variant_ok(self):
81 # test clean usage of final parent job
82 in_repo_conf = textwrap.dedent(
83 """
84 - project:
85 name: org/project
86 check:
87 jobs:
88 - job-final
89 """)
90
91 file_dict = {'.zuul.yaml': in_repo_conf}
92 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
93 files=file_dict)
94 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
95 self.waitUntilSettled()
96
97 self.assertEqual(A.reported, 1)
98 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
99
100 def test_final_variant_error(self):
101 # test misuse of final parent job
102 in_repo_conf = textwrap.dedent(
103 """
104 - project:
105 name: org/project
106 check:
107 jobs:
108 - job-final:
109 vars:
110 dont_override_this: bar
111 """)
112 file_dict = {'.zuul.yaml': in_repo_conf}
113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
114 files=file_dict)
115 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
116 self.waitUntilSettled()
117
118 # The second patch tried to override some variables.
119 # Thus it should fail.
120 self.assertEqual(A.reported, 1)
121 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
122 self.assertIn('Unable to modify final job', A.messages[0])
123
124 def test_final_inheritance(self):
125 # test misuse of final parent job
126 in_repo_conf = textwrap.dedent(
127 """
128 - job:
129 name: project-test
130 parent: job-final
131
132 - project:
133 name: org/project
134 check:
135 jobs:
136 - project-test
137 """)
138
139 in_repo_playbook = textwrap.dedent(
140 """
141 - hosts: all
142 tasks: []
143 """)
144
145 file_dict = {'.zuul.yaml': in_repo_conf,
146 'playbooks/project-test.yaml': in_repo_playbook}
147 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
148 files=file_dict)
149 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
150 self.waitUntilSettled()
151
152 # The second patch tried to override some variables.
153 # Thus it should fail.
154 self.assertEqual(A.reported, 1)
155 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
James E. Blairc32a8352017-10-11 16:27:50 -0700156 self.assertIn('Unable to modify final job', A.messages[0])
Tobias Henkel83167622017-06-30 19:45:03 +0200157
158
James E. Blair09998792017-10-15 18:02:18 -0700159class TestBranchVariants(ZuulTestCase):
160 tenant_config_file = 'config/branch-variants/main.yaml'
161
162 def test_branch_variants(self):
163 # Test branch variants of jobs with inheritance
164 self.executor_server.hold_jobs_in_build = True
165 # This creates a new branch with a copy of the config in master
166 self.create_branch('puppet-integration', 'stable')
167 self.fake_gerrit.addEvent(
168 self.fake_gerrit.getFakeBranchCreatedEvent(
169 'puppet-integration', 'stable'))
170 self.waitUntilSettled()
171
172 A = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'A')
173 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
174 self.waitUntilSettled()
175
176 self.assertEqual(len(self.builds[0].parameters['pre_playbooks']), 3)
177 self.executor_server.hold_jobs_in_build = False
178 self.executor_server.release()
179 self.waitUntilSettled()
180
James E. Blairc9e77592017-10-24 09:25:23 -0700181 def test_branch_variants_reconfigure(self):
182 # Test branch variants of jobs with inheritance
183 self.executor_server.hold_jobs_in_build = True
184 # This creates a new branch with a copy of the config in master
185 self.create_branch('puppet-integration', 'stable')
186 self.fake_gerrit.addEvent(
187 self.fake_gerrit.getFakeBranchCreatedEvent(
188 'puppet-integration', 'stable'))
189 self.waitUntilSettled()
190
191 with open(os.path.join(FIXTURE_DIR,
192 'config/branch-variants/git/',
193 'puppet-integration/.zuul.yaml')) as f:
194 config = f.read()
195
196 # Push a change that triggers a dynamic reconfiguration
197 file_dict = {'.zuul.yaml': config}
198 A = self.fake_gerrit.addFakeChange('puppet-integration', 'master', 'A',
199 files=file_dict)
200 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
201 self.waitUntilSettled()
202
203 ipath = self.builds[0].parameters['zuul']['_inheritance_path']
204 for i in ipath:
205 self.log.debug("inheritance path %s", i)
206 self.assertEqual(len(ipath), 5)
207 self.executor_server.hold_jobs_in_build = False
208 self.executor_server.release()
209 self.waitUntilSettled()
210
James E. Blairc32a8352017-10-11 16:27:50 -0700211 def test_branch_variants_divergent(self):
212 # Test branches can diverge and become independent
213 self.executor_server.hold_jobs_in_build = True
214 # This creates a new branch with a copy of the config in master
215 self.create_branch('puppet-integration', 'stable')
216 self.fake_gerrit.addEvent(
217 self.fake_gerrit.getFakeBranchCreatedEvent(
218 'puppet-integration', 'stable'))
219 self.waitUntilSettled()
220
221 with open(os.path.join(FIXTURE_DIR,
222 'config/branch-variants/git/',
223 'puppet-integration/stable.zuul.yaml')) as f:
224 config = f.read()
225
226 file_dict = {'.zuul.yaml': config}
227 C = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'C',
228 files=file_dict)
229 C.addApproval('Code-Review', 2)
230 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
231 self.waitUntilSettled()
232 self.fake_gerrit.addEvent(C.getChangeMergedEvent())
233 self.waitUntilSettled()
234
235 A = self.fake_gerrit.addFakeChange('puppet-integration', 'master', 'A')
236 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
237 self.waitUntilSettled()
238 B = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'B')
239 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
240 self.waitUntilSettled()
241
242 self.assertEqual(self.builds[0].parameters['zuul']['jobtags'],
243 ['master'])
244
245 self.assertEqual(self.builds[1].parameters['zuul']['jobtags'],
246 ['stable'])
247
248 self.executor_server.hold_jobs_in_build = False
249 self.executor_server.release()
250 self.waitUntilSettled()
251
James E. Blair09998792017-10-15 18:02:18 -0700252
James E. Blairff555742017-02-19 11:34:27 -0800253class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800254 # A temporary class to hold new tests while others are disabled
255
Tobias Henkelabf973e2017-07-28 10:07:34 +0200256 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800257 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800258
James E. Blair83005782015-12-11 14:46:03 -0800259 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200261 A.addApproval('Code-Review', 2)
262 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800263 self.waitUntilSettled()
264 self.assertEqual(self.getJobFromHistory('project-test1').result,
265 'SUCCESS')
266 self.assertEqual(A.data['status'], 'MERGED')
267 self.assertEqual(A.reported, 2,
268 "A should report start and success")
269 self.assertIn('tenant-one-gate', A.messages[1],
270 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800271
James E. Blair3a098dd2017-10-04 14:37:29 -0700272 @skip("This test is useful, but not reliable")
273 def test_full_and_dynamic_reconfig(self):
274 self.executor_server.hold_jobs_in_build = True
275 in_repo_conf = textwrap.dedent(
276 """
277 - job:
278 name: project-test1
279
280 - project:
281 name: org/project
282 tenant-one-gate:
283 jobs:
284 - project-test1
285 """)
286
287 file_dict = {'.zuul.yaml': in_repo_conf}
288 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
289 files=file_dict)
290 A.addApproval('Code-Review', 2)
291 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
292 self.waitUntilSettled()
293 self.sched.reconfigure(self.config)
294 self.waitUntilSettled()
295
296 gc.collect()
297 pipelines = [obj for obj in gc.get_objects()
298 if isinstance(obj, zuul.model.Pipeline)]
299 self.assertEqual(len(pipelines), 4)
300
301 self.executor_server.hold_jobs_in_build = False
302 self.executor_server.release()
303 self.waitUntilSettled()
304
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700305 def test_dynamic_config(self):
306 in_repo_conf = textwrap.dedent(
307 """
308 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200309 name: project-test1
310
311 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700312 name: project-test2
313
314 - project:
315 name: org/project
316 tenant-one-gate:
317 jobs:
318 - project-test2
319 """)
320
James E. Blairc73c73a2017-01-20 15:15:15 -0800321 in_repo_playbook = textwrap.dedent(
322 """
323 - hosts: all
324 tasks: []
325 """)
326
327 file_dict = {'.zuul.yaml': in_repo_conf,
328 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800330 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200331 A.addApproval('Code-Review', 2)
332 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700333 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700334 self.assertEqual(A.data['status'], 'MERGED')
335 self.assertEqual(A.reported, 2,
336 "A should report start and success")
337 self.assertIn('tenant-one-gate', A.messages[1],
338 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800339 self.assertHistory([
340 dict(name='project-test2', result='SUCCESS', changes='1,1')])
341
James E. Blairc2a5ed72017-02-20 14:12:01 -0500342 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800343 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500344
James E. Blair646322f2017-01-27 15:50:34 -0800345 # Now that the config change is landed, it should be live for
346 # subsequent changes.
347 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200348 B.addApproval('Code-Review', 2)
349 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800350 self.waitUntilSettled()
351 self.assertEqual(self.getJobFromHistory('project-test2').result,
352 'SUCCESS')
353 self.assertHistory([
354 dict(name='project-test2', result='SUCCESS', changes='1,1'),
355 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800356
James E. Blair6bc10482017-10-20 11:28:53 -0700357 def test_dynamic_template(self):
358 in_repo_conf = textwrap.dedent(
359 """
360 - job:
361 name: project-test1
362
363 - project-template:
364 name: common-config-template
365 check:
366 jobs:
367 - project-test1
368
369 - project:
370 name: org/project
371 templates: [common-config-template]
372 """)
373
374 file_dict = {'.zuul.yaml': in_repo_conf}
375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
376 files=file_dict)
377 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
378 self.waitUntilSettled()
379 self.assertHistory([
380 dict(name='template-job', result='SUCCESS', changes='1,1')])
381
Tobias Henkelf02cf512017-07-21 22:55:34 +0200382 def test_dynamic_config_non_existing_job(self):
383 """Test that requesting a non existent job fails"""
384 in_repo_conf = textwrap.dedent(
385 """
386 - job:
387 name: project-test1
388
389 - project:
390 name: org/project
391 check:
392 jobs:
393 - non-existent-job
394 """)
395
396 in_repo_playbook = textwrap.dedent(
397 """
398 - hosts: all
399 tasks: []
400 """)
401
402 file_dict = {'.zuul.yaml': in_repo_conf,
403 'playbooks/project-test2.yaml': in_repo_playbook}
404 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
405 files=file_dict)
406 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
407 self.waitUntilSettled()
408 self.assertEqual(A.reported, 1,
409 "A should report failure")
410 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
411 self.assertIn('Job non-existent-job not defined', A.messages[0],
412 "A should have failed the check pipeline")
413 self.assertHistory([])
414
415 def test_dynamic_config_non_existing_job_in_template(self):
416 """Test that requesting a non existent job fails"""
417 in_repo_conf = textwrap.dedent(
418 """
419 - job:
420 name: project-test1
421
422 - project-template:
423 name: test-template
424 check:
425 jobs:
426 - non-existent-job
427
428 - project:
429 name: org/project
430 templates:
431 - test-template
432 """)
433
434 in_repo_playbook = textwrap.dedent(
435 """
436 - hosts: all
437 tasks: []
438 """)
439
440 file_dict = {'.zuul.yaml': in_repo_conf,
441 'playbooks/project-test2.yaml': in_repo_playbook}
442 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
443 files=file_dict)
444 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
445 self.waitUntilSettled()
446 self.assertEqual(A.reported, 1,
447 "A should report failure")
448 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
449 self.assertIn('Job non-existent-job not defined', A.messages[0],
450 "A should have failed the check pipeline")
451 self.assertHistory([])
452
Tobias Henkel0f714002017-06-30 23:30:52 +0200453 def test_dynamic_config_new_patchset(self):
454 self.executor_server.hold_jobs_in_build = True
455
456 tenant = self.sched.abide.tenants.get('tenant-one')
457 check_pipeline = tenant.layout.pipelines['check']
458
459 in_repo_conf = textwrap.dedent(
460 """
461 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200462 name: project-test1
463
464 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200465 name: project-test2
466
467 - project:
468 name: org/project
469 check:
470 jobs:
471 - project-test2
472 """)
473
474 in_repo_playbook = textwrap.dedent(
475 """
476 - hosts: all
477 tasks: []
478 """)
479
480 file_dict = {'.zuul.yaml': in_repo_conf,
481 'playbooks/project-test2.yaml': in_repo_playbook}
482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
483 files=file_dict)
484 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
485 self.waitUntilSettled()
486
487 items = check_pipeline.getAllItems()
488 self.assertEqual(items[0].change.number, '1')
489 self.assertEqual(items[0].change.patchset, '1')
490 self.assertTrue(items[0].live)
491
492 in_repo_conf = textwrap.dedent(
493 """
494 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200495 name: project-test1
496
497 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200498 name: project-test2
499
500 - project:
501 name: org/project
502 check:
503 jobs:
504 - project-test1
505 - project-test2
506 """)
507 file_dict = {'.zuul.yaml': in_repo_conf,
508 'playbooks/project-test2.yaml': in_repo_playbook}
509
510 A.addPatchset(files=file_dict)
511 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
512
513 self.waitUntilSettled()
514
515 items = check_pipeline.getAllItems()
516 self.assertEqual(items[0].change.number, '1')
517 self.assertEqual(items[0].change.patchset, '2')
518 self.assertTrue(items[0].live)
519
520 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200521 self.executor_server.release('project-test1')
522 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200523 self.executor_server.release()
524 self.waitUntilSettled()
525
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200526 self.assertHistory([
527 dict(name='project-test2', result='ABORTED', changes='1,1'),
528 dict(name='project-test1', result='SUCCESS', changes='1,2'),
529 dict(name='project-test2', result='SUCCESS', changes='1,2')])
530
James E. Blairff555742017-02-19 11:34:27 -0800531 def test_in_repo_branch(self):
532 in_repo_conf = textwrap.dedent(
533 """
534 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200535 name: project-test1
536
537 - job:
James E. Blairff555742017-02-19 11:34:27 -0800538 name: project-test2
539
540 - project:
541 name: org/project
542 tenant-one-gate:
543 jobs:
544 - project-test2
545 """)
546
547 in_repo_playbook = textwrap.dedent(
548 """
549 - hosts: all
550 tasks: []
551 """)
552
553 file_dict = {'.zuul.yaml': in_repo_conf,
554 'playbooks/project-test2.yaml': in_repo_playbook}
555 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700556 self.fake_gerrit.addEvent(
557 self.fake_gerrit.getFakeBranchCreatedEvent(
558 'org/project', 'stable'))
James E. Blair6069f2b2017-09-26 16:34:11 -0700559 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800560 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
561 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200562 A.addApproval('Code-Review', 2)
563 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800564 self.waitUntilSettled()
565 self.assertEqual(A.data['status'], 'MERGED')
566 self.assertEqual(A.reported, 2,
567 "A should report start and success")
568 self.assertIn('tenant-one-gate', A.messages[1],
569 "A should transit tenant-one gate")
570 self.assertHistory([
571 dict(name='project-test2', result='SUCCESS', changes='1,1')])
572 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800573 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800574
575 # The config change should not affect master.
576 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200577 B.addApproval('Code-Review', 2)
578 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800579 self.waitUntilSettled()
580 self.assertHistory([
581 dict(name='project-test2', result='SUCCESS', changes='1,1'),
582 dict(name='project-test1', result='SUCCESS', changes='2,1')])
583
584 # The config change should be live for further changes on
585 # stable.
586 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200587 C.addApproval('Code-Review', 2)
588 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800589 self.waitUntilSettled()
590 self.assertHistory([
591 dict(name='project-test2', result='SUCCESS', changes='1,1'),
592 dict(name='project-test1', result='SUCCESS', changes='2,1'),
593 dict(name='project-test2', result='SUCCESS', changes='3,1')])
594
James E. Blaira5a12492017-05-03 11:40:48 -0700595 def test_crd_dynamic_config_branch(self):
596 # Test that we can create a job in one repo and be able to use
597 # it from a different branch on a different repo.
598
599 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700600 self.fake_gerrit.addEvent(
601 self.fake_gerrit.getFakeBranchCreatedEvent(
602 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700603
604 in_repo_conf = textwrap.dedent(
605 """
606 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200607 name: project-test1
608
609 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700610 name: project-test2
611
612 - project:
613 name: org/project
614 check:
615 jobs:
616 - project-test2
617 """)
618
619 in_repo_playbook = textwrap.dedent(
620 """
621 - hosts: all
622 tasks: []
623 """)
624
625 file_dict = {'.zuul.yaml': in_repo_conf,
626 'playbooks/project-test2.yaml': in_repo_playbook}
627 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
628 files=file_dict)
629
630 second_repo_conf = textwrap.dedent(
631 """
632 - project:
633 name: org/project1
634 check:
635 jobs:
636 - project-test2
637 """)
638
639 second_file_dict = {'.zuul.yaml': second_repo_conf}
640 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
641 files=second_file_dict)
642 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
643 B.subject, A.data['id'])
644
645 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
646 self.waitUntilSettled()
647 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
648 self.waitUntilSettled()
649
650 self.assertEqual(A.reported, 1, "A should report")
651 self.assertHistory([
652 dict(name='project-test2', result='SUCCESS', changes='1,1'),
653 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
654 ])
655
James E. Blair97043882017-09-06 15:51:17 -0700656 def test_yaml_list_error(self):
657 in_repo_conf = textwrap.dedent(
658 """
659 job: foo
660 """)
661
662 file_dict = {'.zuul.yaml': in_repo_conf}
663 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
664 files=file_dict)
665 A.addApproval('Code-Review', 2)
666 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
667 self.waitUntilSettled()
668
669 self.assertEqual(A.data['status'], 'NEW')
670 self.assertEqual(A.reported, 1,
671 "A should report failure")
672 self.assertIn('not a list', A.messages[0],
673 "A should have a syntax error reported")
674
675 def test_yaml_dict_error(self):
676 in_repo_conf = textwrap.dedent(
677 """
678 - job
679 """)
680
681 file_dict = {'.zuul.yaml': in_repo_conf}
682 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
683 files=file_dict)
684 A.addApproval('Code-Review', 2)
685 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
686 self.waitUntilSettled()
687
688 self.assertEqual(A.data['status'], 'NEW')
689 self.assertEqual(A.reported, 1,
690 "A should report failure")
691 self.assertIn('not a dictionary', A.messages[0],
692 "A should have a syntax error reported")
693
694 def test_yaml_key_error(self):
695 in_repo_conf = textwrap.dedent(
696 """
697 - job:
698 name: project-test2
699 """)
700
701 file_dict = {'.zuul.yaml': in_repo_conf}
702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
703 files=file_dict)
704 A.addApproval('Code-Review', 2)
705 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
706 self.waitUntilSettled()
707
708 self.assertEqual(A.data['status'], 'NEW')
709 self.assertEqual(A.reported, 1,
710 "A should report failure")
711 self.assertIn('has more than one key', A.messages[0],
712 "A should have a syntax error reported")
713
714 def test_yaml_unknown_error(self):
715 in_repo_conf = textwrap.dedent(
716 """
717 - foobar:
718 foo: bar
719 """)
720
721 file_dict = {'.zuul.yaml': in_repo_conf}
722 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
723 files=file_dict)
724 A.addApproval('Code-Review', 2)
725 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
726 self.waitUntilSettled()
727
728 self.assertEqual(A.data['status'], 'NEW')
729 self.assertEqual(A.reported, 1,
730 "A should report failure")
731 self.assertIn('not recognized', A.messages[0],
732 "A should have a syntax error reported")
733
James E. Blair149b69c2017-03-02 10:48:16 -0800734 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800735 in_repo_conf = textwrap.dedent(
736 """
737 - job:
738 name: project-test2
739 foo: error
740 """)
741
742 file_dict = {'.zuul.yaml': in_repo_conf}
743 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
744 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200745 A.addApproval('Code-Review', 2)
746 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800747 self.waitUntilSettled()
748
749 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200750 self.assertEqual(A.reported, 1,
751 "A should report failure")
752 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800753 "A should have a syntax error reported")
754
James E. Blair149b69c2017-03-02 10:48:16 -0800755 def test_trusted_syntax_error(self):
756 in_repo_conf = textwrap.dedent(
757 """
758 - job:
759 name: project-test2
760 foo: error
761 """)
762
763 file_dict = {'zuul.yaml': in_repo_conf}
764 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
765 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200766 A.addApproval('Code-Review', 2)
767 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800768 self.waitUntilSettled()
769
770 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200771 self.assertEqual(A.reported, 1,
772 "A should report failure")
773 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800774 "A should have a syntax error reported")
775
James E. Blair6f140c72017-03-03 10:32:07 -0800776 def test_untrusted_yaml_error(self):
777 in_repo_conf = textwrap.dedent(
778 """
779 - job:
780 foo: error
781 """)
782
783 file_dict = {'.zuul.yaml': in_repo_conf}
784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
785 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200786 A.addApproval('Code-Review', 2)
787 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800788 self.waitUntilSettled()
789
790 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200791 self.assertEqual(A.reported, 1,
792 "A should report failure")
793 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800794 "A should have a syntax error reported")
795
James E. Blairdb04e6a2017-05-03 14:49:36 -0700796 def test_untrusted_shadow_error(self):
797 in_repo_conf = textwrap.dedent(
798 """
799 - job:
800 name: common-config-test
801 """)
802
803 file_dict = {'.zuul.yaml': in_repo_conf}
804 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
805 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200806 A.addApproval('Code-Review', 2)
807 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700808 self.waitUntilSettled()
809
810 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200811 self.assertEqual(A.reported, 1,
812 "A should report failure")
813 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700814 "A should have a syntax error reported")
815
James E. Blaird5656ad2017-06-02 14:29:41 -0700816 def test_untrusted_pipeline_error(self):
817 in_repo_conf = textwrap.dedent(
818 """
819 - pipeline:
820 name: test
821 """)
822
823 file_dict = {'.zuul.yaml': in_repo_conf}
824 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
825 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200826 A.addApproval('Code-Review', 2)
827 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700828 self.waitUntilSettled()
829
830 self.assertEqual(A.data['status'], 'NEW')
831 self.assertEqual(A.reported, 1,
832 "A should report failure")
833 self.assertIn('Pipelines may not be defined', A.messages[0],
834 "A should have a syntax error reported")
835
836 def test_untrusted_project_error(self):
837 in_repo_conf = textwrap.dedent(
838 """
839 - project:
840 name: org/project1
841 """)
842
843 file_dict = {'.zuul.yaml': in_repo_conf}
844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
845 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200846 A.addApproval('Code-Review', 2)
847 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700848 self.waitUntilSettled()
849
850 self.assertEqual(A.data['status'], 'NEW')
851 self.assertEqual(A.reported, 1,
852 "A should report failure")
853 self.assertIn('the only project definition permitted', A.messages[0],
854 "A should have a syntax error reported")
855
James E. Blairf03173b2017-10-10 10:46:43 -0700856 def test_untrusted_depends_on_trusted(self):
857 with open(os.path.join(FIXTURE_DIR,
858 'config/in-repo/git/',
859 'common-config/zuul.yaml')) as f:
860 common_config = f.read()
861
862 common_config += textwrap.dedent(
863 """
864 - job:
865 name: project-test9
866 """)
867
868 file_dict = {'zuul.yaml': common_config}
869 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
870 files=file_dict)
871 in_repo_conf = textwrap.dedent(
872 """
873 - job:
874 name: project-test1
875 - project:
876 name: org/project
877 check:
878 jobs:
879 - project-test9
880 """)
881
882 file_dict = {'zuul.yaml': in_repo_conf}
883 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
884 files=file_dict)
885 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
886 B.subject, A.data['id'])
887 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
888 self.waitUntilSettled()
889
890 self.assertEqual(B.data['status'], 'NEW')
891 self.assertEqual(B.reported, 1,
892 "B should report failure")
893 self.assertIn('depends on a change to a config project',
894 B.messages[0],
895 "A should have a syntax error reported")
896
James E. Blaire64b0e42017-06-08 11:23:34 -0700897 def test_duplicate_node_error(self):
898 in_repo_conf = textwrap.dedent(
899 """
900 - nodeset:
901 name: duplicate
902 nodes:
903 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700904 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700905 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700906 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700907 """)
908
909 file_dict = {'.zuul.yaml': in_repo_conf}
910 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
911 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200912 A.addApproval('Code-Review', 2)
913 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700914 self.waitUntilSettled()
915
916 self.assertEqual(A.data['status'], 'NEW')
917 self.assertEqual(A.reported, 1,
918 "A should report failure")
919 self.assertIn('appears multiple times', A.messages[0],
920 "A should have a syntax error reported")
921
922 def test_duplicate_group_error(self):
923 in_repo_conf = textwrap.dedent(
924 """
925 - nodeset:
926 name: duplicate
927 nodes:
928 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700929 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700930 groups:
931 - name: group
932 nodes: compute
933 - name: group
934 nodes: compute
935 """)
936
937 file_dict = {'.zuul.yaml': in_repo_conf}
938 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
939 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200940 A.addApproval('Code-Review', 2)
941 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700942 self.waitUntilSettled()
943
944 self.assertEqual(A.data['status'], 'NEW')
945 self.assertEqual(A.reported, 1,
946 "A should report failure")
947 self.assertIn('appears multiple times', A.messages[0],
948 "A should have a syntax error reported")
949
James E. Blair4ae399f2017-09-20 17:15:09 -0700950 def test_secret_not_found_error(self):
951 in_repo_conf = textwrap.dedent(
952 """
953 - job:
954 name: test
955 secrets: does-not-exist
956 """)
957
958 file_dict = {'.zuul.yaml': in_repo_conf}
959 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
960 files=file_dict)
961 A.addApproval('Code-Review', 2)
962 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
963 self.waitUntilSettled()
964
965 self.assertEqual(A.data['status'], 'NEW')
966 self.assertEqual(A.reported, 1,
967 "A should report failure")
968 self.assertIn('secret "does-not-exist" was not found', A.messages[0],
969 "A should have a syntax error reported")
970
971 def test_nodeset_not_found_error(self):
972 in_repo_conf = textwrap.dedent(
973 """
974 - job:
975 name: test
976 nodeset: does-not-exist
977 """)
978
979 file_dict = {'.zuul.yaml': in_repo_conf}
980 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
981 files=file_dict)
982 A.addApproval('Code-Review', 2)
983 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
984 self.waitUntilSettled()
985
986 self.assertEqual(A.data['status'], 'NEW')
987 self.assertEqual(A.reported, 1,
988 "A should report failure")
989 self.assertIn('nodeset "does-not-exist" was not found', A.messages[0],
990 "A should have a syntax error reported")
991
James E. Blair89e25eb2017-09-26 09:11:31 -0700992 def test_template_not_found_error(self):
993 in_repo_conf = textwrap.dedent(
994 """
995 - job:
996 name: project-test1
997 - project:
998 name: org/project
999 templates:
1000 - does-not-exist
1001 """)
1002
1003 file_dict = {'.zuul.yaml': in_repo_conf}
1004 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1005 files=file_dict)
1006 A.addApproval('Code-Review', 2)
1007 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1008 self.waitUntilSettled()
1009
1010 self.assertEqual(A.data['status'], 'NEW')
1011 self.assertEqual(A.reported, 1,
1012 "A should report failure")
1013 self.assertIn('project template "does-not-exist" was not found',
1014 A.messages[0],
1015 "A should have a syntax error reported")
1016
Monty Taylor8be3c0c2017-10-06 10:37:37 -05001017 def test_job_list_in_project_template_not_dict_error(self):
1018 in_repo_conf = textwrap.dedent(
1019 """
1020 - job:
1021 name: project-test1
1022 - project-template:
1023 name: some-jobs
1024 check:
1025 jobs:
1026 - project-test1:
1027 - required-projects:
1028 org/project2
1029 """)
1030
1031 file_dict = {'.zuul.yaml': in_repo_conf}
1032 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1033 files=file_dict)
1034 A.addApproval('Code-Review', 2)
1035 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1036 self.waitUntilSettled()
1037
1038 self.assertEqual(A.data['status'], 'NEW')
1039 self.assertEqual(A.reported, 1,
1040 "A should report failure")
1041 self.assertIn('expected str for dictionary value',
1042 A.messages[0], "A should have a syntax error reported")
1043
1044 def test_job_list_in_project_not_dict_error(self):
1045 in_repo_conf = textwrap.dedent(
1046 """
1047 - job:
1048 name: project-test1
1049 - project:
1050 name: org/project1
1051 check:
1052 jobs:
1053 - project-test1:
1054 - required-projects:
1055 org/project2
1056 """)
1057
1058 file_dict = {'.zuul.yaml': in_repo_conf}
1059 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1060 files=file_dict)
1061 A.addApproval('Code-Review', 2)
1062 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1063 self.waitUntilSettled()
1064
1065 self.assertEqual(A.data['status'], 'NEW')
1066 self.assertEqual(A.reported, 1,
1067 "A should report failure")
1068 self.assertIn('expected str for dictionary value',
1069 A.messages[0], "A should have a syntax error reported")
1070
James E. Blair1235f142017-10-07 09:11:43 -07001071 def test_project_template(self):
1072 # Tests that a project template is not modified when used, and
1073 # can therefore be used in subsequent reconfigurations.
1074 in_repo_conf = textwrap.dedent(
1075 """
1076 - job:
1077 name: project-test1
1078 - project-template:
1079 name: some-jobs
1080 tenant-one-gate:
1081 jobs:
1082 - project-test1:
1083 required-projects:
1084 - org/project1
1085 - project:
1086 name: org/project
1087 templates:
1088 - some-jobs
1089 """)
1090
1091 file_dict = {'.zuul.yaml': in_repo_conf}
1092 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1093 files=file_dict)
1094 A.addApproval('Code-Review', 2)
1095 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1096 self.waitUntilSettled()
1097 self.assertEqual(A.data['status'], 'MERGED')
1098 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1099 self.waitUntilSettled()
1100 in_repo_conf = textwrap.dedent(
1101 """
1102 - project:
1103 name: org/project1
1104 templates:
1105 - some-jobs
1106 """)
1107 file_dict = {'.zuul.yaml': in_repo_conf}
1108 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B',
1109 files=file_dict)
1110 B.addApproval('Code-Review', 2)
1111 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1112 self.waitUntilSettled()
1113 self.assertEqual(B.data['status'], 'MERGED')
1114
James E. Blairbccdfcf2017-10-07 13:37:26 -07001115 def test_job_remove_add(self):
1116 # Tests that a job can be removed from one repo and added in another.
1117 # First, remove the current config for project1 since it
1118 # references the job we want to remove.
1119 file_dict = {'.zuul.yaml': None}
1120 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1121 files=file_dict)
1122 A.setMerged()
1123 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1124 self.waitUntilSettled()
1125 # Then propose a change to delete the job from one repo...
1126 file_dict = {'.zuul.yaml': None}
1127 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1128 files=file_dict)
1129 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1130 self.waitUntilSettled()
1131 # ...and a second that depends on it that adds it to another repo.
1132 in_repo_conf = textwrap.dedent(
1133 """
1134 - job:
1135 name: project-test1
1136
1137 - project:
1138 name: org/project1
1139 check:
1140 jobs:
1141 - project-test1
1142 """)
1143 in_repo_playbook = textwrap.dedent(
1144 """
1145 - hosts: all
1146 tasks: []
1147 """)
1148 file_dict = {'.zuul.yaml': in_repo_conf,
1149 'playbooks/project-test1.yaml': in_repo_playbook}
1150 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C',
1151 files=file_dict,
1152 parent='refs/changes/1/1/1')
1153 C.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1154 C.subject, B.data['id'])
1155 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1156 self.waitUntilSettled()
1157 self.assertHistory([
1158 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1159 ], ordered=False)
1160
James E. Blair09f9ffe2017-07-11 15:30:25 -07001161 def test_multi_repo(self):
1162 downstream_repo_conf = textwrap.dedent(
1163 """
1164 - project:
1165 name: org/project1
1166 tenant-one-gate:
1167 jobs:
1168 - project-test1
1169
1170 - job:
1171 name: project1-test1
1172 parent: project-test1
1173 """)
1174
1175 file_dict = {'.zuul.yaml': downstream_repo_conf}
1176 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1177 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001178 A.addApproval('Code-Review', 2)
1179 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -07001180 self.waitUntilSettled()
1181
1182 self.assertEqual(A.data['status'], 'MERGED')
1183 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1184 self.waitUntilSettled()
1185
1186 upstream_repo_conf = textwrap.dedent(
1187 """
1188 - job:
1189 name: project-test1
1190
1191 - job:
1192 name: project-test2
1193
1194 - project:
1195 name: org/project
1196 tenant-one-gate:
1197 jobs:
1198 - project-test1
1199 """)
1200
1201 file_dict = {'.zuul.yaml': upstream_repo_conf}
1202 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1203 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001204 B.addApproval('Code-Review', 2)
1205 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -07001206 self.waitUntilSettled()
1207
1208 self.assertEqual(B.data['status'], 'MERGED')
1209 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
1210 self.waitUntilSettled()
1211
1212 tenant = self.sched.abide.tenants.get('tenant-one')
1213 # Ensure the latest change is reflected in the config; if it
1214 # isn't this will raise an exception.
1215 tenant.layout.getJob('project-test2')
1216
James E. Blair332636e2017-09-05 10:14:35 -07001217 def test_pipeline_error(self):
1218 with open(os.path.join(FIXTURE_DIR,
1219 'config/in-repo/git/',
1220 'common-config/zuul.yaml')) as f:
1221 base_common_config = f.read()
1222
1223 in_repo_conf_A = textwrap.dedent(
1224 """
1225 - pipeline:
1226 name: periodic
1227 foo: error
1228 """)
1229
1230 file_dict = {'zuul.yaml': None,
1231 'zuul.d/main.yaml': base_common_config,
1232 'zuul.d/test1.yaml': in_repo_conf_A}
1233 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
1234 files=file_dict)
1235 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1236 self.waitUntilSettled()
1237 self.assertEqual(A.reported, 1,
1238 "A should report failure")
1239 self.assertIn('syntax error',
1240 A.messages[0],
1241 "A should have an error reported")
1242
1243 def test_change_series_error(self):
1244 with open(os.path.join(FIXTURE_DIR,
1245 'config/in-repo/git/',
1246 'common-config/zuul.yaml')) as f:
1247 base_common_config = f.read()
1248
1249 in_repo_conf_A = textwrap.dedent(
1250 """
1251 - pipeline:
1252 name: periodic
1253 foo: error
1254 """)
1255
1256 file_dict = {'zuul.yaml': None,
1257 'zuul.d/main.yaml': base_common_config,
1258 'zuul.d/test1.yaml': in_repo_conf_A}
1259 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
1260 files=file_dict)
1261
1262 in_repo_conf_B = textwrap.dedent(
1263 """
1264 - job:
1265 name: project-test2
1266 foo: error
1267 """)
1268
1269 file_dict = {'zuul.yaml': None,
1270 'zuul.d/main.yaml': base_common_config,
1271 'zuul.d/test1.yaml': in_repo_conf_A,
1272 'zuul.d/test2.yaml': in_repo_conf_B}
1273 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
1274 files=file_dict)
1275 B.setDependsOn(A, 1)
1276 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
1277 C.setDependsOn(B, 1)
1278 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1279 self.waitUntilSettled()
1280
1281 self.assertEqual(C.reported, 1,
1282 "C should report failure")
1283 self.assertIn('depends on a change that failed to merge',
1284 C.messages[0],
1285 "C should have an error reported")
1286
James E. Blairc73c73a2017-01-20 15:15:15 -08001287
James E. Blairc9455002017-09-06 09:22:19 -07001288class TestInRepoJoin(ZuulTestCase):
1289 # In this config, org/project is not a member of any pipelines, so
1290 # that we may test the changes that cause it to join them.
1291
1292 tenant_config_file = 'config/in-repo-join/main.yaml'
1293
1294 def test_dynamic_dependent_pipeline(self):
1295 # Test dynamically adding a project to a
1296 # dependent pipeline for the first time
1297 self.executor_server.hold_jobs_in_build = True
1298
1299 tenant = self.sched.abide.tenants.get('tenant-one')
1300 gate_pipeline = tenant.layout.pipelines['gate']
1301
1302 in_repo_conf = textwrap.dedent(
1303 """
1304 - job:
1305 name: project-test1
1306
1307 - job:
1308 name: project-test2
1309
1310 - project:
1311 name: org/project
1312 gate:
1313 jobs:
1314 - project-test2
1315 """)
1316
1317 in_repo_playbook = textwrap.dedent(
1318 """
1319 - hosts: all
1320 tasks: []
1321 """)
1322
1323 file_dict = {'.zuul.yaml': in_repo_conf,
1324 'playbooks/project-test2.yaml': in_repo_playbook}
1325 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1326 files=file_dict)
1327 A.addApproval('Code-Review', 2)
1328 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1329 self.waitUntilSettled()
1330
1331 items = gate_pipeline.getAllItems()
1332 self.assertEqual(items[0].change.number, '1')
1333 self.assertEqual(items[0].change.patchset, '1')
1334 self.assertTrue(items[0].live)
1335
1336 self.executor_server.hold_jobs_in_build = False
1337 self.executor_server.release()
1338 self.waitUntilSettled()
1339
1340 # Make sure the dynamic queue got cleaned up
1341 self.assertEqual(gate_pipeline.queues, [])
1342
1343 def test_dynamic_dependent_pipeline_failure(self):
1344 # Test that a change behind a failing change adding a project
1345 # to a dependent pipeline is dequeued.
1346 self.executor_server.hold_jobs_in_build = True
1347
1348 in_repo_conf = textwrap.dedent(
1349 """
1350 - job:
1351 name: project-test1
1352
1353 - project:
1354 name: org/project
1355 gate:
1356 jobs:
1357 - project-test1
1358 """)
1359
1360 file_dict = {'.zuul.yaml': in_repo_conf}
1361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1362 files=file_dict)
1363 self.executor_server.failJob('project-test1', A)
1364 A.addApproval('Code-Review', 2)
1365 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1366 self.waitUntilSettled()
1367
1368 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1369 B.addApproval('Code-Review', 2)
1370 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1371 self.waitUntilSettled()
1372
James E. Blair3490c5d2017-09-07 08:33:23 -07001373 self.orderedRelease()
James E. Blairc9455002017-09-06 09:22:19 -07001374 self.waitUntilSettled()
1375 self.assertEqual(A.reported, 2,
1376 "A should report start and failure")
1377 self.assertEqual(A.data['status'], 'NEW')
1378 self.assertEqual(B.reported, 1,
1379 "B should report start")
1380 self.assertHistory([
1381 dict(name='project-test1', result='FAILURE', changes='1,1'),
James E. Blair3490c5d2017-09-07 08:33:23 -07001382 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
James E. Blairc9455002017-09-06 09:22:19 -07001383 ], ordered=False)
1384
James E. Blair0af198f2017-09-06 09:52:35 -07001385 def test_dynamic_dependent_pipeline_absent(self):
1386 # Test that a series of dependent changes don't report merge
1387 # failures to a pipeline they aren't in.
1388 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1389 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1390 B.setDependsOn(A, 1)
1391
1392 A.addApproval('Code-Review', 2)
1393 A.addApproval('Approved', 1)
1394 B.addApproval('Code-Review', 2)
1395 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1396 self.waitUntilSettled()
1397 self.assertEqual(A.reported, 0,
1398 "A should not report")
1399 self.assertEqual(A.data['status'], 'NEW')
1400 self.assertEqual(B.reported, 0,
1401 "B should not report")
1402 self.assertEqual(B.data['status'], 'NEW')
1403 self.assertHistory([])
1404
James E. Blairc9455002017-09-06 09:22:19 -07001405
James E. Blairc73c73a2017-01-20 15:15:15 -08001406class TestAnsible(AnsibleZuulTestCase):
1407 # A temporary class to hold new tests while others are disabled
1408
1409 tenant_config_file = 'config/ansible/main.yaml'
1410
1411 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +11001412 # Keep the jobdir around so we can inspect contents if an
1413 # assert fails.
1414 self.executor_server.keep_jobdir = True
1415 # Output extra ansible info so we might see errors.
1416 self.executor_server.verbose = True
1417 # Add a site variables file, used by check-vars
1418 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
1419 'variables.yaml')
1420 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -08001421 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1422 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1423 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +02001424 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +11001425 with self.jobLog(build_timeout):
1426 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001427 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +11001428 with self.jobLog(build_faillocal):
1429 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001430 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +11001431 with self.jobLog(build_failpost):
1432 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001433 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +11001434 with self.jobLog(build_check_vars):
1435 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -05001436 build_check_secret_names = self.getJobFromHistory('check-secret-names')
1437 with self.jobLog(build_check_secret_names):
1438 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001439 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +11001440 with self.jobLog(build_hello):
1441 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001442 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +11001443 with self.jobLog(build_python27):
1444 self.assertEqual(build_python27.result, 'SUCCESS')
1445 flag_path = os.path.join(self.test_root,
1446 build_python27.uuid + '.flag')
1447 self.assertTrue(os.path.exists(flag_path))
1448 copied_path = os.path.join(self.test_root, build_python27.uuid +
1449 '.copied')
1450 self.assertTrue(os.path.exists(copied_path))
1451 failed_path = os.path.join(self.test_root, build_python27.uuid +
1452 '.failed')
1453 self.assertFalse(os.path.exists(failed_path))
1454 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
1455 '.pre.flag')
1456 self.assertTrue(os.path.exists(pre_flag_path))
1457 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
1458 '.post.flag')
1459 self.assertTrue(os.path.exists(post_flag_path))
1460 bare_role_flag_path = os.path.join(self.test_root,
1461 build_python27.uuid +
1462 '.bare-role.flag')
1463 self.assertTrue(os.path.exists(bare_role_flag_path))
1464 secrets_path = os.path.join(self.test_root,
1465 build_python27.uuid + '.secrets')
1466 with open(secrets_path) as f:
1467 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -08001468
Jamie Lennox7655b552017-03-17 12:33:38 +11001469 msg = A.messages[0]
1470 success = "{} https://success.example.com/zuul-logs/{}"
1471 fail = "{} https://failure.example.com/zuul-logs/{}"
1472 self.assertIn(success.format("python27", build_python27.uuid), msg)
1473 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
1474 self.assertIn(success.format("check-vars",
1475 build_check_vars.uuid), msg)
1476 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
1477 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
1478 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +02001479
James E. Blairabbaa6f2017-04-06 16:11:44 -07001480 def _add_job(self, job_name):
1481 conf = textwrap.dedent(
1482 """
1483 - job:
1484 name: %s
1485
1486 - project:
1487 name: org/plugin-project
1488 check:
1489 jobs:
1490 - %s
1491 """ % (job_name, job_name))
1492
1493 file_dict = {'.zuul.yaml': conf}
1494 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1495 files=file_dict)
1496 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1497 self.waitUntilSettled()
1498
1499 def test_plugins(self):
1500 # Keep the jobdir around so we can inspect contents if an
1501 # assert fails.
1502 self.executor_server.keep_jobdir = True
1503 # Output extra ansible info so we might see errors.
1504 self.executor_server.verbose = True
1505
1506 count = 0
1507 plugin_tests = [
1508 ('passwd', 'FAILURE'),
1509 ('cartesian', 'SUCCESS'),
1510 ('consul_kv', 'FAILURE'),
1511 ('credstash', 'FAILURE'),
1512 ('csvfile_good', 'SUCCESS'),
1513 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001514 ('uri_bad_path', 'FAILURE'),
1515 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001516 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001517 ('file_local_good', 'SUCCESS'),
1518 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001519 ]
1520 for job_name, result in plugin_tests:
1521 count += 1
1522 self._add_job(job_name)
1523
1524 job = self.getJobFromHistory(job_name)
1525 with self.jobLog(job):
1526 self.assertEqual(count, len(self.history))
1527 build = self.history[-1]
1528 self.assertEqual(build.result, result)
1529
1530 # TODOv3(jeblair): parse the ansible output and verify we're
1531 # getting the exception we expect.
1532
James E. Blairb9c0d772017-03-03 14:34:49 -08001533
James E. Blaira4d4eef2017-06-30 14:49:17 -07001534class TestPrePlaybooks(AnsibleZuulTestCase):
1535 # A temporary class to hold new tests while others are disabled
1536
1537 tenant_config_file = 'config/pre-playbook/main.yaml'
1538
1539 def test_pre_playbook_fail(self):
1540 # Test that we run the post playbooks (but not the actual
1541 # playbook) when a pre-playbook fails.
1542 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1543 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1544 self.waitUntilSettled()
1545 build = self.getJobFromHistory('python27')
1546 self.assertIsNone(build.result)
1547 self.assertIn('RETRY_LIMIT', A.messages[0])
1548 flag_path = os.path.join(self.test_root, build.uuid +
1549 '.main.flag')
1550 self.assertFalse(os.path.exists(flag_path))
1551 pre_flag_path = os.path.join(self.test_root, build.uuid +
1552 '.pre.flag')
1553 self.assertFalse(os.path.exists(pre_flag_path))
1554 post_flag_path = os.path.join(self.test_root, build.uuid +
1555 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001556 self.assertTrue(os.path.exists(post_flag_path),
1557 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001558
1559
James E. Blairbacbb882017-10-17 09:48:23 -07001560class TestPostPlaybooks(AnsibleZuulTestCase):
1561 tenant_config_file = 'config/post-playbook/main.yaml'
1562
1563 def test_post_playbook_abort(self):
1564 # Test that when we abort a job in the post playbook, that we
1565 # don't send back POST_FAILURE.
1566 self.executor_server.verbose = True
1567 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1568 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1569
1570 while not len(self.builds):
1571 time.sleep(0.1)
1572 build = self.builds[0]
1573
1574 post_start = os.path.join(self.test_root, build.uuid +
1575 '.post_start.flag')
1576 start = time.time()
1577 while time.time() < start + 90:
1578 if os.path.exists(post_start):
1579 break
1580 time.sleep(0.1)
1581 # The post playbook has started, abort the job
1582 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1583 self.waitUntilSettled()
1584
1585 build = self.getJobFromHistory('python27')
1586 self.assertEqual('ABORTED', build.result)
1587
1588 post_end = os.path.join(self.test_root, build.uuid +
1589 '.post_end.flag')
1590 self.assertTrue(os.path.exists(post_start))
1591 self.assertFalse(os.path.exists(post_end))
1592
1593
James E. Blairb9c0d772017-03-03 14:34:49 -08001594class TestBrokenConfig(ZuulTestCase):
1595 # Test that we get an appropriate syntax error if we start with a
1596 # broken config.
1597
1598 tenant_config_file = 'config/broken/main.yaml'
1599
1600 def setUp(self):
1601 with testtools.ExpectedException(
1602 zuul.configloader.ConfigurationSyntaxError,
1603 "\nZuul encountered a syntax error"):
1604 super(TestBrokenConfig, self).setUp()
1605
1606 def test_broken_config_on_startup(self):
1607 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001608
1609
1610class TestProjectKeys(ZuulTestCase):
1611 # Test that we can generate project keys
1612
1613 # Normally the test infrastructure copies a static key in place
1614 # for each project before starting tests. This saves time because
1615 # Zuul's automatic key-generation on startup can be slow. To make
1616 # sure we exercise that code, in this test we allow Zuul to create
1617 # keys for the project on startup.
1618 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001619 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001620 tenant_config_file = 'config/in-repo/main.yaml'
1621
1622 def test_key_generation(self):
1623 key_root = os.path.join(self.state_root, 'keys')
1624 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1625 # Make sure that a proper key was created on startup
1626 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001627 private_key, public_key = \
1628 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001629
1630 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1631 fixture_private_key = i.read()
1632
1633 # Make sure that we didn't just end up with the static fixture
1634 # key
1635 self.assertNotEqual(fixture_private_key, private_key)
1636
1637 # Make sure it's the right length
1638 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001639
1640
James E. Blairbb94dfa2017-07-11 07:45:19 -07001641class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001642 def _assertRolePath(self, build, playbook, content):
1643 path = os.path.join(self.test_root, build.uuid,
1644 'ansible', playbook, 'ansible.cfg')
1645 roles_paths = []
1646 with open(path) as f:
1647 for line in f:
1648 if line.startswith('roles_path'):
1649 roles_paths.append(line)
1650 print(roles_paths)
1651 if content:
1652 self.assertEqual(len(roles_paths), 1,
1653 "Should have one roles_path line in %s" %
1654 (playbook,))
1655 self.assertIn(content, roles_paths[0])
1656 else:
1657 self.assertEqual(len(roles_paths), 0,
1658 "Should have no roles_path line in %s" %
1659 (playbook,))
1660
James E. Blairbb94dfa2017-07-11 07:45:19 -07001661
1662class TestRoles(RoleTestCase):
1663 tenant_config_file = 'config/roles/main.yaml'
1664
James E. Blairbce76932017-05-04 10:03:15 -07001665 def test_role(self):
1666 # This exercises a proposed change to a role being checked out
1667 # and used.
1668 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1669 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1670 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1671 B.subject, A.data['id'])
1672 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1673 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1674 self.waitUntilSettled()
1675 self.assertHistory([
1676 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1677 ])
James E. Blair6459db12017-06-29 14:57:20 -07001678
James E. Blair1b27f6a2017-07-14 14:09:07 -07001679 def test_role_inheritance(self):
1680 self.executor_server.hold_jobs_in_build = True
1681 conf = textwrap.dedent(
1682 """
1683 - job:
1684 name: parent
1685 roles:
1686 - zuul: bare-role
1687 pre-run: playbooks/parent-pre
1688 post-run: playbooks/parent-post
1689
1690 - job:
1691 name: project-test
1692 parent: parent
1693 roles:
1694 - zuul: org/project
1695
1696 - project:
1697 name: org/project
1698 check:
1699 jobs:
1700 - project-test
1701 """)
1702
1703 file_dict = {'.zuul.yaml': conf}
1704 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1705 files=file_dict)
1706 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1707 self.waitUntilSettled()
1708
1709 self.assertEqual(len(self.builds), 1)
1710 build = self.getBuildByName('project-test')
1711 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1712 self._assertRolePath(build, 'playbook_0', 'role_0')
1713 self._assertRolePath(build, 'playbook_0', 'role_1')
1714 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1715
1716 self.executor_server.hold_jobs_in_build = False
1717 self.executor_server.release()
1718 self.waitUntilSettled()
1719
1720 self.assertHistory([
1721 dict(name='project-test', result='SUCCESS', changes='1,1'),
1722 ])
1723
James E. Blair6f699732017-07-18 14:19:11 -07001724 def test_role_error(self):
1725 conf = textwrap.dedent(
1726 """
1727 - job:
1728 name: project-test
1729 roles:
1730 - zuul: common-config
1731
1732 - project:
1733 name: org/project
1734 check:
1735 jobs:
1736 - project-test
1737 """)
1738
1739 file_dict = {'.zuul.yaml': conf}
1740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1741 files=file_dict)
1742 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1743 self.waitUntilSettled()
1744 self.assertIn(
1745 '- project-test project-test : ERROR Unable to find role',
1746 A.messages[-1])
1747
James E. Blair6459db12017-06-29 14:57:20 -07001748
James E. Blairbb94dfa2017-07-11 07:45:19 -07001749class TestImplicitRoles(RoleTestCase):
1750 tenant_config_file = 'config/implicit-roles/main.yaml'
1751
1752 def test_missing_roles(self):
1753 # Test implicit and explicit roles for a project which does
1754 # not have roles. The implicit role should be silently
1755 # ignored since the project doesn't supply roles, but if a
1756 # user declares an explicit role, it should error.
1757 self.executor_server.hold_jobs_in_build = True
1758 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1759 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1760 self.waitUntilSettled()
1761
1762 self.assertEqual(len(self.builds), 2)
1763 build = self.getBuildByName('implicit-role-fail')
1764 self._assertRolePath(build, 'playbook_0', None)
1765
1766 self.executor_server.hold_jobs_in_build = False
1767 self.executor_server.release()
1768 self.waitUntilSettled()
1769 # The retry_limit doesn't get recorded
1770 self.assertHistory([
1771 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1772 ])
1773
1774 def test_roles(self):
1775 # Test implicit and explicit roles for a project which does
1776 # have roles. In both cases, we should end up with the role
1777 # in the path. In the explicit case, ensure we end up with
1778 # the name we specified.
1779 self.executor_server.hold_jobs_in_build = True
1780 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1781 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1782 self.waitUntilSettled()
1783
1784 self.assertEqual(len(self.builds), 2)
1785 build = self.getBuildByName('implicit-role-ok')
1786 self._assertRolePath(build, 'playbook_0', 'role_0')
1787
1788 build = self.getBuildByName('explicit-role-ok')
1789 self._assertRolePath(build, 'playbook_0', 'role_0')
1790
1791 self.executor_server.hold_jobs_in_build = False
1792 self.executor_server.release()
1793 self.waitUntilSettled()
1794 self.assertHistory([
1795 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1796 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1797 ], ordered=False)
1798
1799
James E. Blair6459db12017-06-29 14:57:20 -07001800class TestShadow(ZuulTestCase):
1801 tenant_config_file = 'config/shadow/main.yaml'
1802
1803 def test_shadow(self):
1804 # Test that a repo is allowed to shadow another's job definitions.
1805 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1806 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1807 self.waitUntilSettled()
1808 self.assertHistory([
1809 dict(name='test1', result='SUCCESS', changes='1,1'),
1810 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001811 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001812
1813
1814class TestDataReturn(AnsibleZuulTestCase):
1815 tenant_config_file = 'config/data-return/main.yaml'
1816
1817 def test_data_return(self):
James E. Blair196f61a2017-06-30 15:42:29 -07001818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1819 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1820 self.waitUntilSettled()
1821 self.assertHistory([
1822 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001823 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
James E. Blair698703c2017-09-15 20:58:30 -06001824 dict(name='child', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001825 ], ordered=False)
1826 self.assertIn('- data-return http://example.com/test/log/url/',
1827 A.messages[-1])
1828 self.assertIn('- data-return-relative '
1829 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001830 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001831
1832
1833class TestDiskAccounting(AnsibleZuulTestCase):
1834 config_file = 'zuul-disk-accounting.conf'
1835 tenant_config_file = 'config/disk-accountant/main.yaml'
1836
1837 def test_disk_accountant_kills_job(self):
1838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1839 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1840 self.waitUntilSettled()
1841 self.assertHistory([
1842 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001843
1844
1845class TestMaxNodesPerJob(AnsibleZuulTestCase):
1846 tenant_config_file = 'config/multi-tenant/main.yaml'
1847
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001848 def test_max_timeout_exceeded(self):
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001849 in_repo_conf = textwrap.dedent(
1850 """
1851 - job:
1852 name: test-job
James E. Blair7e3e6882017-09-20 15:47:13 -07001853 nodeset:
1854 nodes:
1855 - name: node01
1856 label: fake
1857 - name: node02
1858 label: fake
1859 - name: node03
1860 label: fake
1861 - name: node04
1862 label: fake
1863 - name: node05
1864 label: fake
1865 - name: node06
1866 label: fake
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001867 """)
1868 file_dict = {'.zuul.yaml': in_repo_conf}
1869 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1870 files=file_dict)
1871 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1872 self.waitUntilSettled()
1873 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1874 A.messages[0], "A should fail because of nodes limit")
1875
1876 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1877 files=file_dict)
1878 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1879 self.waitUntilSettled()
1880 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1881 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001882
1883
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001884class TestMaxTimeout(AnsibleZuulTestCase):
1885 tenant_config_file = 'config/multi-tenant/main.yaml'
1886
1887 def test_max_nodes_reached(self):
1888 in_repo_conf = textwrap.dedent(
1889 """
1890 - job:
1891 name: test-job
1892 timeout: 3600
1893 """)
1894 file_dict = {'.zuul.yaml': in_repo_conf}
1895 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1896 files=file_dict)
1897 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1898 self.waitUntilSettled()
1899 self.assertIn('The job "test-job" exceeds tenant max-job-timeout',
1900 A.messages[0], "A should fail because of timeout limit")
1901
1902 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1903 files=file_dict)
1904 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1905 self.waitUntilSettled()
1906 self.assertNotIn("exceeds tenant max-job-timeout", B.messages[0],
1907 "B should not fail because of timeout limit")
1908
1909
James E. Blair7edc25f2017-10-26 10:47:14 -07001910class TestPragma(ZuulTestCase):
1911 tenant_config_file = 'config/pragma/main.yaml'
1912
1913 def test_no_pragma(self):
1914 self.create_branch('org/project', 'stable')
1915 with open(os.path.join(FIXTURE_DIR,
1916 'config/pragma/git/',
1917 'org_project/nopragma.yaml')) as f:
1918 config = f.read()
1919 file_dict = {'.zuul.yaml': config}
1920 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1921 files=file_dict)
1922 A.addApproval('Code-Review', 2)
1923 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1924 self.waitUntilSettled()
1925 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1926 self.waitUntilSettled()
1927
1928 # This is an untrusted repo with 2 branches, so it should have
1929 # an implied branch matcher for the job.
1930 tenant = self.sched.abide.tenants.get('tenant-one')
1931 jobs = tenant.layout.getJobs('test-job')
1932 self.assertEqual(len(jobs), 1)
1933 for job in tenant.layout.getJobs('test-job'):
1934 self.assertIsNotNone(job.branch_matcher)
1935
1936 def test_pragma(self):
1937 self.create_branch('org/project', 'stable')
1938 with open(os.path.join(FIXTURE_DIR,
1939 'config/pragma/git/',
1940 'org_project/pragma.yaml')) as f:
1941 config = f.read()
1942 file_dict = {'.zuul.yaml': config}
1943 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1944 files=file_dict)
1945 A.addApproval('Code-Review', 2)
1946 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1947 self.waitUntilSettled()
1948 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1949 self.waitUntilSettled()
1950
1951 # This is an untrusted repo with 2 branches, so it would
1952 # normally have an implied branch matcher, but our pragma
1953 # overrides it.
1954 tenant = self.sched.abide.tenants.get('tenant-one')
1955 jobs = tenant.layout.getJobs('test-job')
1956 self.assertEqual(len(jobs), 1)
1957 for job in tenant.layout.getJobs('test-job'):
1958 self.assertIsNone(job.branch_matcher)
1959
1960
James E. Blair2bab6e72017-08-07 09:52:45 -07001961class TestBaseJobs(ZuulTestCase):
1962 tenant_config_file = 'config/base-jobs/main.yaml'
1963
1964 def test_multiple_base_jobs(self):
1965 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1966 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1967 self.waitUntilSettled()
1968 self.assertHistory([
1969 dict(name='my-job', result='SUCCESS', changes='1,1'),
1970 dict(name='other-job', result='SUCCESS', changes='1,1'),
1971 ], ordered=False)
1972 self.assertEqual(self.getJobFromHistory('my-job').
1973 parameters['zuul']['jobtags'],
1974 ['mybase'])
1975 self.assertEqual(self.getJobFromHistory('other-job').
1976 parameters['zuul']['jobtags'],
1977 ['otherbase'])
1978
1979 def test_untrusted_base_job(self):
1980 """Test that a base job may not be defined in an untrusted repo"""
1981 in_repo_conf = textwrap.dedent(
1982 """
1983 - job:
1984 name: fail-base
1985 parent: null
1986 """)
1987
1988 file_dict = {'.zuul.yaml': in_repo_conf}
1989 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1990 files=file_dict)
1991 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1992 self.waitUntilSettled()
1993 self.assertEqual(A.reported, 1,
1994 "A should report failure")
1995 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1996 self.assertIn('Base jobs must be defined in config projects',
1997 A.messages[0])
1998 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001999
2000
James E. Blairdf91ab32017-10-25 17:57:13 -07002001class TestSecretInheritance(ZuulTestCase):
2002 tenant_config_file = 'config/secret-inheritance/main.yaml'
2003
2004 def _getSecrets(self, job, pbtype):
2005 secrets = []
2006 build = self.getJobFromHistory(job)
2007 for pb in build.parameters[pbtype]:
2008 secrets.append(pb['secrets'])
2009 return secrets
2010
2011 def _checkTrustedSecrets(self):
2012 secret = {'longpassword': 'test-passwordtest-password',
2013 'password': 'test-password',
2014 'username': 'test-username'}
2015 self.assertEqual(
2016 self._getSecrets('trusted-secrets', 'playbooks'),
2017 [{'trusted-secret': secret}, {}])
2018 self.assertEqual(
2019 self._getSecrets('trusted-secrets', 'pre_playbooks'), [])
2020 self.assertEqual(
2021 self._getSecrets('trusted-secrets', 'post_playbooks'), [])
2022
2023 self.assertEqual(
2024 self._getSecrets('trusted-secrets-trusted-child',
2025 'playbooks'),
2026 [{}, {'trusted-secret': secret}, {}])
2027 self.assertEqual(
2028 self._getSecrets('trusted-secrets-trusted-child',
2029 'pre_playbooks'), [])
2030 self.assertEqual(
2031 self._getSecrets('trusted-secrets-trusted-child',
2032 'post_playbooks'), [])
2033
2034 self.assertEqual(
2035 self._getSecrets('trusted-secrets-untrusted-child',
2036 'playbooks'),
2037 [{}, {'trusted-secret': secret}, {}])
2038 self.assertEqual(
2039 self._getSecrets('trusted-secrets-untrusted-child',
2040 'pre_playbooks'), [])
2041 self.assertEqual(
2042 self._getSecrets('trusted-secrets-untrusted-child',
2043 'post_playbooks'), [])
2044
2045 def _checkUntrustedSecrets(self):
2046 secret = {'longpassword': 'test-passwordtest-password',
2047 'password': 'test-password',
2048 'username': 'test-username'}
2049 self.assertEqual(
2050 self._getSecrets('untrusted-secrets', 'playbooks'),
2051 [{'untrusted-secret': secret}, {}])
2052 self.assertEqual(
2053 self._getSecrets('untrusted-secrets', 'pre_playbooks'), [])
2054 self.assertEqual(
2055 self._getSecrets('untrusted-secrets', 'post_playbooks'), [])
2056
2057 self.assertEqual(
2058 self._getSecrets('untrusted-secrets-trusted-child',
2059 'playbooks'),
2060 [{}, {'untrusted-secret': secret}, {}])
2061 self.assertEqual(
2062 self._getSecrets('untrusted-secrets-trusted-child',
2063 'pre_playbooks'), [])
2064 self.assertEqual(
2065 self._getSecrets('untrusted-secrets-trusted-child',
2066 'post_playbooks'), [])
2067
2068 self.assertEqual(
2069 self._getSecrets('untrusted-secrets-untrusted-child',
2070 'playbooks'),
2071 [{}, {'untrusted-secret': secret}, {}])
2072 self.assertEqual(
2073 self._getSecrets('untrusted-secrets-untrusted-child',
2074 'pre_playbooks'), [])
2075 self.assertEqual(
2076 self._getSecrets('untrusted-secrets-untrusted-child',
2077 'post_playbooks'), [])
2078
2079 def test_trusted_secret_inheritance_check(self):
2080 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
2081 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2082 self.waitUntilSettled()
2083 self.assertHistory([
2084 dict(name='trusted-secrets', result='SUCCESS', changes='1,1'),
2085 dict(name='trusted-secrets-trusted-child',
2086 result='SUCCESS', changes='1,1'),
2087 dict(name='trusted-secrets-untrusted-child',
2088 result='SUCCESS', changes='1,1'),
2089 ], ordered=False)
2090
2091 self._checkTrustedSecrets()
2092
2093 def test_untrusted_secret_inheritance_gate(self):
2094 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
2095 A.addApproval('Code-Review', 2)
2096 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2097 self.waitUntilSettled()
2098 self.assertHistory([
2099 dict(name='untrusted-secrets', result='SUCCESS', changes='1,1'),
2100 dict(name='untrusted-secrets-trusted-child',
2101 result='SUCCESS', changes='1,1'),
2102 dict(name='untrusted-secrets-untrusted-child',
2103 result='SUCCESS', changes='1,1'),
2104 ], ordered=False)
2105
2106 self._checkUntrustedSecrets()
2107
2108 def test_untrusted_secret_inheritance_check(self):
2109 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2110 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2111 self.waitUntilSettled()
2112 # This configuration tries to run untrusted secrets in an
2113 # non-post-review pipeline and should therefore run no jobs.
2114 self.assertHistory([])
2115
2116
James E. Blairdb089032017-08-15 13:42:12 -07002117class TestSecretLeaks(AnsibleZuulTestCase):
2118 tenant_config_file = 'config/secret-leaks/main.yaml'
2119
2120 def searchForContent(self, path, content):
2121 matches = []
2122 for (dirpath, dirnames, filenames) in os.walk(path):
2123 for filename in filenames:
2124 filepath = os.path.join(dirpath, filename)
2125 with open(filepath, 'rb') as f:
2126 if content in f.read():
2127 matches.append(filepath[len(path):])
2128 return matches
2129
2130 def _test_secret_file(self):
2131 # Or rather -- test that they *don't* leak.
2132 # Keep the jobdir around so we can inspect contents.
2133 self.executor_server.keep_jobdir = True
2134 conf = textwrap.dedent(
2135 """
2136 - project:
2137 name: org/project
2138 check:
2139 jobs:
2140 - secret-file
2141 """)
2142
2143 file_dict = {'.zuul.yaml': conf}
2144 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
2145 files=file_dict)
2146 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2147 self.waitUntilSettled()
2148 self.assertHistory([
2149 dict(name='secret-file', result='SUCCESS', changes='1,1'),
2150 ], ordered=False)
2151 matches = self.searchForContent(self.history[0].jobdir.root,
2152 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07002153 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07002154 set(matches))
2155
2156 def test_secret_file(self):
2157 self._test_secret_file()
2158
2159 def test_secret_file_verbose(self):
2160 # Output extra ansible info to exercise alternate logging code
2161 # paths.
2162 self.executor_server.verbose = True
2163 self._test_secret_file()
2164
2165 def _test_secret_file_fail(self):
2166 # Or rather -- test that they *don't* leak.
2167 # Keep the jobdir around so we can inspect contents.
2168 self.executor_server.keep_jobdir = True
2169 conf = textwrap.dedent(
2170 """
2171 - project:
2172 name: org/project
2173 check:
2174 jobs:
2175 - secret-file-fail
2176 """)
2177
2178 file_dict = {'.zuul.yaml': conf}
2179 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
2180 files=file_dict)
2181 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2182 self.waitUntilSettled()
2183 self.assertHistory([
2184 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
2185 ], ordered=False)
2186 matches = self.searchForContent(self.history[0].jobdir.root,
2187 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07002188 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07002189 set(matches))
2190
2191 def test_secret_file_fail(self):
2192 self._test_secret_file_fail()
2193
2194 def test_secret_file_fail_verbose(self):
2195 # Output extra ansible info to exercise alternate logging code
2196 # paths.
2197 self.executor_server.verbose = True
2198 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07002199
2200
2201class TestJobOutput(AnsibleZuulTestCase):
2202 tenant_config_file = 'config/job-output/main.yaml'
2203
2204 def _get_file(self, build, path):
2205 p = os.path.join(build.jobdir.root, path)
2206 with open(p) as f:
2207 return f.read()
2208
2209 def test_job_output(self):
Monty Taylor0e2489a2017-10-10 11:57:29 -05002210 # Verify that command standard output appears in the job output,
2211 # and that failures in the final playbook get logged.
James E. Blaira00910c2017-08-23 09:15:04 -07002212
2213 # This currently only verifies we receive output from
2214 # localhost. Notably, it does not verify we receive output
2215 # via zuul_console streaming.
2216 self.executor_server.keep_jobdir = True
2217 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2218 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2219 self.waitUntilSettled()
2220 self.assertHistory([
2221 dict(name='job-output', result='SUCCESS', changes='1,1'),
2222 ], ordered=False)
2223
2224 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
2225 j = json.loads(self._get_file(self.history[0],
2226 'work/logs/job-output.json'))
2227 self.assertEqual(token,
2228 j[0]['plays'][0]['tasks'][0]
2229 ['hosts']['localhost']['stdout'])
2230
2231 print(self._get_file(self.history[0],
2232 'work/logs/job-output.txt'))
2233 self.assertIn(token,
2234 self._get_file(self.history[0],
2235 'work/logs/job-output.txt'))
Monty Taylor0e2489a2017-10-10 11:57:29 -05002236
2237 def test_job_output_failure_log(self):
2238 logger = logging.getLogger('zuul.AnsibleJob')
2239 output = io.StringIO()
2240 logger.addHandler(logging.StreamHandler(output))
2241
2242 # Verify that a failure in the last post playbook emits the contents
2243 # of the json output to the log
2244 self.executor_server.keep_jobdir = True
2245 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
2246 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2247 self.waitUntilSettled()
2248 self.assertHistory([
2249 dict(name='job-output-failure',
2250 result='POST_FAILURE', changes='1,1'),
2251 ], ordered=False)
2252
2253 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
2254 j = json.loads(self._get_file(self.history[0],
2255 'work/logs/job-output.json'))
2256 self.assertEqual(token,
2257 j[0]['plays'][0]['tasks'][0]
2258 ['hosts']['localhost']['stdout'])
2259
2260 print(self._get_file(self.history[0],
2261 'work/logs/job-output.json'))
2262 self.assertIn(token,
2263 self._get_file(self.history[0],
2264 'work/logs/job-output.txt'))
2265
2266 log_output = output.getvalue()
2267 self.assertIn('Final playbook failed', log_output)
2268 self.assertIn('Failure test', log_output)