blob: 80e6ccc5365f02a4444d122f02be851d39c2bd7b [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
James E. Blair2f589fe2017-10-26 12:57:41 -0700131 run: playbooks/project-test.yaml
Tobias Henkel83167622017-06-30 19:45:03 +0200132
133 - project:
134 name: org/project
135 check:
136 jobs:
137 - project-test
138 """)
139
140 in_repo_playbook = textwrap.dedent(
141 """
142 - hosts: all
143 tasks: []
144 """)
145
146 file_dict = {'.zuul.yaml': in_repo_conf,
147 'playbooks/project-test.yaml': in_repo_playbook}
148 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
149 files=file_dict)
150 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
151 self.waitUntilSettled()
152
153 # The second patch tried to override some variables.
154 # Thus it should fail.
155 self.assertEqual(A.reported, 1)
156 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
James E. Blairc32a8352017-10-11 16:27:50 -0700157 self.assertIn('Unable to modify final job', A.messages[0])
Tobias Henkel83167622017-06-30 19:45:03 +0200158
159
James E. Blair09998792017-10-15 18:02:18 -0700160class TestBranchVariants(ZuulTestCase):
161 tenant_config_file = 'config/branch-variants/main.yaml'
162
163 def test_branch_variants(self):
164 # Test branch variants of jobs with inheritance
165 self.executor_server.hold_jobs_in_build = True
166 # This creates a new branch with a copy of the config in master
167 self.create_branch('puppet-integration', 'stable')
168 self.fake_gerrit.addEvent(
169 self.fake_gerrit.getFakeBranchCreatedEvent(
170 'puppet-integration', 'stable'))
171 self.waitUntilSettled()
172
173 A = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'A')
174 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
175 self.waitUntilSettled()
176
177 self.assertEqual(len(self.builds[0].parameters['pre_playbooks']), 3)
178 self.executor_server.hold_jobs_in_build = False
179 self.executor_server.release()
180 self.waitUntilSettled()
181
James E. Blairc9e77592017-10-24 09:25:23 -0700182 def test_branch_variants_reconfigure(self):
183 # Test branch variants of jobs with inheritance
184 self.executor_server.hold_jobs_in_build = True
185 # This creates a new branch with a copy of the config in master
186 self.create_branch('puppet-integration', 'stable')
187 self.fake_gerrit.addEvent(
188 self.fake_gerrit.getFakeBranchCreatedEvent(
189 'puppet-integration', 'stable'))
190 self.waitUntilSettled()
191
192 with open(os.path.join(FIXTURE_DIR,
193 'config/branch-variants/git/',
194 'puppet-integration/.zuul.yaml')) as f:
195 config = f.read()
196
197 # Push a change that triggers a dynamic reconfiguration
198 file_dict = {'.zuul.yaml': config}
199 A = self.fake_gerrit.addFakeChange('puppet-integration', 'master', 'A',
200 files=file_dict)
201 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
202 self.waitUntilSettled()
203
204 ipath = self.builds[0].parameters['zuul']['_inheritance_path']
205 for i in ipath:
206 self.log.debug("inheritance path %s", i)
207 self.assertEqual(len(ipath), 5)
208 self.executor_server.hold_jobs_in_build = False
209 self.executor_server.release()
210 self.waitUntilSettled()
211
James E. Blairc32a8352017-10-11 16:27:50 -0700212 def test_branch_variants_divergent(self):
213 # Test branches can diverge and become independent
214 self.executor_server.hold_jobs_in_build = True
215 # This creates a new branch with a copy of the config in master
216 self.create_branch('puppet-integration', 'stable')
217 self.fake_gerrit.addEvent(
218 self.fake_gerrit.getFakeBranchCreatedEvent(
219 'puppet-integration', 'stable'))
220 self.waitUntilSettled()
221
222 with open(os.path.join(FIXTURE_DIR,
223 'config/branch-variants/git/',
224 'puppet-integration/stable.zuul.yaml')) as f:
225 config = f.read()
226
227 file_dict = {'.zuul.yaml': config}
228 C = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'C',
229 files=file_dict)
230 C.addApproval('Code-Review', 2)
231 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
232 self.waitUntilSettled()
233 self.fake_gerrit.addEvent(C.getChangeMergedEvent())
234 self.waitUntilSettled()
235
236 A = self.fake_gerrit.addFakeChange('puppet-integration', 'master', 'A')
237 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
238 self.waitUntilSettled()
239 B = self.fake_gerrit.addFakeChange('puppet-integration', 'stable', 'B')
240 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
241 self.waitUntilSettled()
242
243 self.assertEqual(self.builds[0].parameters['zuul']['jobtags'],
244 ['master'])
245
246 self.assertEqual(self.builds[1].parameters['zuul']['jobtags'],
247 ['stable'])
248
249 self.executor_server.hold_jobs_in_build = False
250 self.executor_server.release()
251 self.waitUntilSettled()
252
James E. Blair09998792017-10-15 18:02:18 -0700253
James E. Blairff555742017-02-19 11:34:27 -0800254class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800255 # A temporary class to hold new tests while others are disabled
256
Tobias Henkelabf973e2017-07-28 10:07:34 +0200257 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800258 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800259
James E. Blair83005782015-12-11 14:46:03 -0800260 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200262 A.addApproval('Code-Review', 2)
263 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800264 self.waitUntilSettled()
265 self.assertEqual(self.getJobFromHistory('project-test1').result,
266 'SUCCESS')
267 self.assertEqual(A.data['status'], 'MERGED')
268 self.assertEqual(A.reported, 2,
269 "A should report start and success")
270 self.assertIn('tenant-one-gate', A.messages[1],
271 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800272
James E. Blair3a098dd2017-10-04 14:37:29 -0700273 @skip("This test is useful, but not reliable")
274 def test_full_and_dynamic_reconfig(self):
275 self.executor_server.hold_jobs_in_build = True
276 in_repo_conf = textwrap.dedent(
277 """
278 - job:
279 name: project-test1
280
281 - project:
282 name: org/project
283 tenant-one-gate:
284 jobs:
285 - project-test1
286 """)
287
288 file_dict = {'.zuul.yaml': in_repo_conf}
289 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
290 files=file_dict)
291 A.addApproval('Code-Review', 2)
292 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
293 self.waitUntilSettled()
294 self.sched.reconfigure(self.config)
295 self.waitUntilSettled()
296
297 gc.collect()
298 pipelines = [obj for obj in gc.get_objects()
299 if isinstance(obj, zuul.model.Pipeline)]
300 self.assertEqual(len(pipelines), 4)
301
302 self.executor_server.hold_jobs_in_build = False
303 self.executor_server.release()
304 self.waitUntilSettled()
305
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700306 def test_dynamic_config(self):
307 in_repo_conf = textwrap.dedent(
308 """
309 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200310 name: project-test1
311
312 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700313 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -0700314 run: playbooks/project-test2.yaml
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700315
316 - project:
317 name: org/project
318 tenant-one-gate:
319 jobs:
320 - project-test2
321 """)
322
James E. Blairc73c73a2017-01-20 15:15:15 -0800323 in_repo_playbook = textwrap.dedent(
324 """
325 - hosts: all
326 tasks: []
327 """)
328
329 file_dict = {'.zuul.yaml': in_repo_conf,
330 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700331 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800332 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200333 A.addApproval('Code-Review', 2)
334 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700335 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700336 self.assertEqual(A.data['status'], 'MERGED')
337 self.assertEqual(A.reported, 2,
338 "A should report start and success")
339 self.assertIn('tenant-one-gate', A.messages[1],
340 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800341 self.assertHistory([
342 dict(name='project-test2', result='SUCCESS', changes='1,1')])
343
James E. Blairc2a5ed72017-02-20 14:12:01 -0500344 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800345 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500346
James E. Blair646322f2017-01-27 15:50:34 -0800347 # Now that the config change is landed, it should be live for
348 # subsequent changes.
349 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200350 B.addApproval('Code-Review', 2)
351 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800352 self.waitUntilSettled()
353 self.assertEqual(self.getJobFromHistory('project-test2').result,
354 'SUCCESS')
355 self.assertHistory([
356 dict(name='project-test2', result='SUCCESS', changes='1,1'),
357 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800358
James E. Blair6bc10482017-10-20 11:28:53 -0700359 def test_dynamic_template(self):
360 in_repo_conf = textwrap.dedent(
361 """
362 - job:
363 name: project-test1
364
365 - project-template:
366 name: common-config-template
367 check:
368 jobs:
369 - project-test1
370
371 - project:
372 name: org/project
373 templates: [common-config-template]
374 """)
375
376 file_dict = {'.zuul.yaml': in_repo_conf}
377 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
378 files=file_dict)
379 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
380 self.waitUntilSettled()
381 self.assertHistory([
382 dict(name='template-job', result='SUCCESS', changes='1,1')])
383
Tobias Henkelf02cf512017-07-21 22:55:34 +0200384 def test_dynamic_config_non_existing_job(self):
385 """Test that requesting a non existent job fails"""
386 in_repo_conf = textwrap.dedent(
387 """
388 - job:
389 name: project-test1
390
391 - project:
392 name: org/project
393 check:
394 jobs:
395 - non-existent-job
396 """)
397
398 in_repo_playbook = textwrap.dedent(
399 """
400 - hosts: all
401 tasks: []
402 """)
403
404 file_dict = {'.zuul.yaml': in_repo_conf,
405 'playbooks/project-test2.yaml': in_repo_playbook}
406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
407 files=file_dict)
408 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
409 self.waitUntilSettled()
410 self.assertEqual(A.reported, 1,
411 "A should report failure")
412 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
413 self.assertIn('Job non-existent-job not defined', A.messages[0],
414 "A should have failed the check pipeline")
415 self.assertHistory([])
416
417 def test_dynamic_config_non_existing_job_in_template(self):
418 """Test that requesting a non existent job fails"""
419 in_repo_conf = textwrap.dedent(
420 """
421 - job:
422 name: project-test1
423
424 - project-template:
425 name: test-template
426 check:
427 jobs:
428 - non-existent-job
429
430 - project:
431 name: org/project
432 templates:
433 - test-template
434 """)
435
436 in_repo_playbook = textwrap.dedent(
437 """
438 - hosts: all
439 tasks: []
440 """)
441
442 file_dict = {'.zuul.yaml': in_repo_conf,
443 'playbooks/project-test2.yaml': in_repo_playbook}
444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
445 files=file_dict)
446 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
447 self.waitUntilSettled()
448 self.assertEqual(A.reported, 1,
449 "A should report failure")
450 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
451 self.assertIn('Job non-existent-job not defined', A.messages[0],
452 "A should have failed the check pipeline")
453 self.assertHistory([])
454
Tobias Henkel0f714002017-06-30 23:30:52 +0200455 def test_dynamic_config_new_patchset(self):
456 self.executor_server.hold_jobs_in_build = True
457
458 tenant = self.sched.abide.tenants.get('tenant-one')
459 check_pipeline = tenant.layout.pipelines['check']
460
461 in_repo_conf = textwrap.dedent(
462 """
463 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200464 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -0700465 run: playbooks/project-test1.yaml
Tobias Henkelf02cf512017-07-21 22:55:34 +0200466
467 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200468 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -0700469 run: playbooks/project-test2.yaml
Tobias Henkel0f714002017-06-30 23:30:52 +0200470
471 - project:
472 name: org/project
473 check:
474 jobs:
475 - project-test2
476 """)
477
478 in_repo_playbook = textwrap.dedent(
479 """
480 - hosts: all
481 tasks: []
482 """)
483
484 file_dict = {'.zuul.yaml': in_repo_conf,
485 'playbooks/project-test2.yaml': in_repo_playbook}
486 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
487 files=file_dict)
488 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
489 self.waitUntilSettled()
490
491 items = check_pipeline.getAllItems()
492 self.assertEqual(items[0].change.number, '1')
493 self.assertEqual(items[0].change.patchset, '1')
494 self.assertTrue(items[0].live)
495
496 in_repo_conf = textwrap.dedent(
497 """
498 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200499 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -0700500 run: playbooks/project-test1.yaml
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200501
502 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200503 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -0700504 run: playbooks/project-test2.yaml
Tobias Henkel0f714002017-06-30 23:30:52 +0200505
506 - project:
507 name: org/project
508 check:
509 jobs:
510 - project-test1
511 - project-test2
512 """)
513 file_dict = {'.zuul.yaml': in_repo_conf,
514 'playbooks/project-test2.yaml': in_repo_playbook}
515
516 A.addPatchset(files=file_dict)
517 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
518
519 self.waitUntilSettled()
520
521 items = check_pipeline.getAllItems()
522 self.assertEqual(items[0].change.number, '1')
523 self.assertEqual(items[0].change.patchset, '2')
524 self.assertTrue(items[0].live)
525
526 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200527 self.executor_server.release('project-test1')
528 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200529 self.executor_server.release()
530 self.waitUntilSettled()
531
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200532 self.assertHistory([
533 dict(name='project-test2', result='ABORTED', changes='1,1'),
534 dict(name='project-test1', result='SUCCESS', changes='1,2'),
535 dict(name='project-test2', result='SUCCESS', changes='1,2')])
536
James E. Blairff555742017-02-19 11:34:27 -0800537 def test_in_repo_branch(self):
538 in_repo_conf = textwrap.dedent(
539 """
540 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200541 name: project-test1
542
543 - job:
James E. Blairff555742017-02-19 11:34:27 -0800544 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -0700545 run: playbooks/project-test2.yaml
James E. Blairff555742017-02-19 11:34:27 -0800546
547 - project:
548 name: org/project
549 tenant-one-gate:
550 jobs:
551 - project-test2
552 """)
553
554 in_repo_playbook = textwrap.dedent(
555 """
556 - hosts: all
557 tasks: []
558 """)
559
560 file_dict = {'.zuul.yaml': in_repo_conf,
561 'playbooks/project-test2.yaml': in_repo_playbook}
562 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700563 self.fake_gerrit.addEvent(
564 self.fake_gerrit.getFakeBranchCreatedEvent(
565 'org/project', 'stable'))
James E. Blair6069f2b2017-09-26 16:34:11 -0700566 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800567 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
568 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200569 A.addApproval('Code-Review', 2)
570 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800571 self.waitUntilSettled()
572 self.assertEqual(A.data['status'], 'MERGED')
573 self.assertEqual(A.reported, 2,
574 "A should report start and success")
575 self.assertIn('tenant-one-gate', A.messages[1],
576 "A should transit tenant-one gate")
577 self.assertHistory([
578 dict(name='project-test2', result='SUCCESS', changes='1,1')])
579 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800580 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800581
582 # The config change should not affect master.
583 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200584 B.addApproval('Code-Review', 2)
585 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800586 self.waitUntilSettled()
587 self.assertHistory([
588 dict(name='project-test2', result='SUCCESS', changes='1,1'),
589 dict(name='project-test1', result='SUCCESS', changes='2,1')])
590
591 # The config change should be live for further changes on
592 # stable.
593 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200594 C.addApproval('Code-Review', 2)
595 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800596 self.waitUntilSettled()
597 self.assertHistory([
598 dict(name='project-test2', result='SUCCESS', changes='1,1'),
599 dict(name='project-test1', result='SUCCESS', changes='2,1'),
600 dict(name='project-test2', result='SUCCESS', changes='3,1')])
601
James E. Blaira5a12492017-05-03 11:40:48 -0700602 def test_crd_dynamic_config_branch(self):
603 # Test that we can create a job in one repo and be able to use
604 # it from a different branch on a different repo.
605
606 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700607 self.fake_gerrit.addEvent(
608 self.fake_gerrit.getFakeBranchCreatedEvent(
609 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700610
611 in_repo_conf = textwrap.dedent(
612 """
613 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200614 name: project-test1
615
616 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700617 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -0700618 run: playbooks/project-test2.yaml
James E. Blaira5a12492017-05-03 11:40:48 -0700619
620 - project:
621 name: org/project
622 check:
623 jobs:
624 - project-test2
625 """)
626
627 in_repo_playbook = textwrap.dedent(
628 """
629 - hosts: all
630 tasks: []
631 """)
632
633 file_dict = {'.zuul.yaml': in_repo_conf,
634 'playbooks/project-test2.yaml': in_repo_playbook}
635 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
636 files=file_dict)
637
638 second_repo_conf = textwrap.dedent(
639 """
640 - project:
641 name: org/project1
642 check:
643 jobs:
644 - project-test2
645 """)
646
647 second_file_dict = {'.zuul.yaml': second_repo_conf}
648 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
649 files=second_file_dict)
650 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
651 B.subject, A.data['id'])
652
653 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
654 self.waitUntilSettled()
655 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
656 self.waitUntilSettled()
657
658 self.assertEqual(A.reported, 1, "A should report")
659 self.assertHistory([
660 dict(name='project-test2', result='SUCCESS', changes='1,1'),
661 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
662 ])
663
James E. Blair97043882017-09-06 15:51:17 -0700664 def test_yaml_list_error(self):
665 in_repo_conf = textwrap.dedent(
666 """
667 job: foo
668 """)
669
670 file_dict = {'.zuul.yaml': in_repo_conf}
671 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
672 files=file_dict)
673 A.addApproval('Code-Review', 2)
674 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
675 self.waitUntilSettled()
676
677 self.assertEqual(A.data['status'], 'NEW')
678 self.assertEqual(A.reported, 1,
679 "A should report failure")
680 self.assertIn('not a list', A.messages[0],
681 "A should have a syntax error reported")
682
683 def test_yaml_dict_error(self):
684 in_repo_conf = textwrap.dedent(
685 """
686 - job
687 """)
688
689 file_dict = {'.zuul.yaml': in_repo_conf}
690 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
691 files=file_dict)
692 A.addApproval('Code-Review', 2)
693 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
694 self.waitUntilSettled()
695
696 self.assertEqual(A.data['status'], 'NEW')
697 self.assertEqual(A.reported, 1,
698 "A should report failure")
699 self.assertIn('not a dictionary', A.messages[0],
700 "A should have a syntax error reported")
701
702 def test_yaml_key_error(self):
703 in_repo_conf = textwrap.dedent(
704 """
705 - job:
706 name: project-test2
707 """)
708
709 file_dict = {'.zuul.yaml': in_repo_conf}
710 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
711 files=file_dict)
712 A.addApproval('Code-Review', 2)
713 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
714 self.waitUntilSettled()
715
716 self.assertEqual(A.data['status'], 'NEW')
717 self.assertEqual(A.reported, 1,
718 "A should report failure")
719 self.assertIn('has more than one key', A.messages[0],
720 "A should have a syntax error reported")
721
722 def test_yaml_unknown_error(self):
723 in_repo_conf = textwrap.dedent(
724 """
725 - foobar:
726 foo: bar
727 """)
728
729 file_dict = {'.zuul.yaml': in_repo_conf}
730 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
731 files=file_dict)
732 A.addApproval('Code-Review', 2)
733 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
734 self.waitUntilSettled()
735
736 self.assertEqual(A.data['status'], 'NEW')
737 self.assertEqual(A.reported, 1,
738 "A should report failure")
739 self.assertIn('not recognized', A.messages[0],
740 "A should have a syntax error reported")
741
James E. Blair149b69c2017-03-02 10:48:16 -0800742 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800743 in_repo_conf = textwrap.dedent(
744 """
745 - job:
746 name: project-test2
747 foo: error
748 """)
749
750 file_dict = {'.zuul.yaml': in_repo_conf}
751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
752 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200753 A.addApproval('Code-Review', 2)
754 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800755 self.waitUntilSettled()
756
757 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200758 self.assertEqual(A.reported, 1,
759 "A should report failure")
760 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800761 "A should have a syntax error reported")
762
James E. Blair149b69c2017-03-02 10:48:16 -0800763 def test_trusted_syntax_error(self):
764 in_repo_conf = textwrap.dedent(
765 """
766 - job:
767 name: project-test2
768 foo: error
769 """)
770
771 file_dict = {'zuul.yaml': in_repo_conf}
772 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
773 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200774 A.addApproval('Code-Review', 2)
775 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800776 self.waitUntilSettled()
777
778 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200779 self.assertEqual(A.reported, 1,
780 "A should report failure")
781 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800782 "A should have a syntax error reported")
783
James E. Blair6f140c72017-03-03 10:32:07 -0800784 def test_untrusted_yaml_error(self):
785 in_repo_conf = textwrap.dedent(
786 """
787 - job:
788 foo: error
789 """)
790
791 file_dict = {'.zuul.yaml': in_repo_conf}
792 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
793 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200794 A.addApproval('Code-Review', 2)
795 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800796 self.waitUntilSettled()
797
798 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200799 self.assertEqual(A.reported, 1,
800 "A should report failure")
801 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800802 "A should have a syntax error reported")
803
James E. Blairdb04e6a2017-05-03 14:49:36 -0700804 def test_untrusted_shadow_error(self):
805 in_repo_conf = textwrap.dedent(
806 """
807 - job:
808 name: common-config-test
809 """)
810
811 file_dict = {'.zuul.yaml': in_repo_conf}
812 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
813 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200814 A.addApproval('Code-Review', 2)
815 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700816 self.waitUntilSettled()
817
818 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200819 self.assertEqual(A.reported, 1,
820 "A should report failure")
821 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700822 "A should have a syntax error reported")
823
James E. Blaird5656ad2017-06-02 14:29:41 -0700824 def test_untrusted_pipeline_error(self):
825 in_repo_conf = textwrap.dedent(
826 """
827 - pipeline:
828 name: test
829 """)
830
831 file_dict = {'.zuul.yaml': in_repo_conf}
832 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
833 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200834 A.addApproval('Code-Review', 2)
835 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700836 self.waitUntilSettled()
837
838 self.assertEqual(A.data['status'], 'NEW')
839 self.assertEqual(A.reported, 1,
840 "A should report failure")
841 self.assertIn('Pipelines may not be defined', A.messages[0],
842 "A should have a syntax error reported")
843
844 def test_untrusted_project_error(self):
845 in_repo_conf = textwrap.dedent(
846 """
847 - project:
848 name: org/project1
849 """)
850
851 file_dict = {'.zuul.yaml': in_repo_conf}
852 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
853 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200854 A.addApproval('Code-Review', 2)
855 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700856 self.waitUntilSettled()
857
858 self.assertEqual(A.data['status'], 'NEW')
859 self.assertEqual(A.reported, 1,
860 "A should report failure")
861 self.assertIn('the only project definition permitted', A.messages[0],
862 "A should have a syntax error reported")
863
James E. Blairf03173b2017-10-10 10:46:43 -0700864 def test_untrusted_depends_on_trusted(self):
865 with open(os.path.join(FIXTURE_DIR,
866 'config/in-repo/git/',
867 'common-config/zuul.yaml')) as f:
868 common_config = f.read()
869
870 common_config += textwrap.dedent(
871 """
872 - job:
873 name: project-test9
874 """)
875
876 file_dict = {'zuul.yaml': common_config}
877 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
878 files=file_dict)
879 in_repo_conf = textwrap.dedent(
880 """
881 - job:
882 name: project-test1
883 - project:
884 name: org/project
885 check:
886 jobs:
887 - project-test9
888 """)
889
890 file_dict = {'zuul.yaml': in_repo_conf}
891 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
892 files=file_dict)
893 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
894 B.subject, A.data['id'])
895 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
896 self.waitUntilSettled()
897
898 self.assertEqual(B.data['status'], 'NEW')
899 self.assertEqual(B.reported, 1,
900 "B should report failure")
901 self.assertIn('depends on a change to a config project',
902 B.messages[0],
903 "A should have a syntax error reported")
904
James E. Blaire64b0e42017-06-08 11:23:34 -0700905 def test_duplicate_node_error(self):
906 in_repo_conf = textwrap.dedent(
907 """
908 - nodeset:
909 name: duplicate
910 nodes:
911 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700912 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700913 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700914 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700915 """)
916
917 file_dict = {'.zuul.yaml': in_repo_conf}
918 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
919 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200920 A.addApproval('Code-Review', 2)
921 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700922 self.waitUntilSettled()
923
924 self.assertEqual(A.data['status'], 'NEW')
925 self.assertEqual(A.reported, 1,
926 "A should report failure")
927 self.assertIn('appears multiple times', A.messages[0],
928 "A should have a syntax error reported")
929
930 def test_duplicate_group_error(self):
931 in_repo_conf = textwrap.dedent(
932 """
933 - nodeset:
934 name: duplicate
935 nodes:
936 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700937 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700938 groups:
939 - name: group
940 nodes: compute
941 - name: group
942 nodes: compute
943 """)
944
945 file_dict = {'.zuul.yaml': in_repo_conf}
946 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
947 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200948 A.addApproval('Code-Review', 2)
949 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700950 self.waitUntilSettled()
951
952 self.assertEqual(A.data['status'], 'NEW')
953 self.assertEqual(A.reported, 1,
954 "A should report failure")
955 self.assertIn('appears multiple times', A.messages[0],
956 "A should have a syntax error reported")
957
James E. Blair4ae399f2017-09-20 17:15:09 -0700958 def test_secret_not_found_error(self):
959 in_repo_conf = textwrap.dedent(
960 """
961 - job:
962 name: test
963 secrets: does-not-exist
964 """)
965
966 file_dict = {'.zuul.yaml': in_repo_conf}
967 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
968 files=file_dict)
969 A.addApproval('Code-Review', 2)
970 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
971 self.waitUntilSettled()
972
973 self.assertEqual(A.data['status'], 'NEW')
974 self.assertEqual(A.reported, 1,
975 "A should report failure")
976 self.assertIn('secret "does-not-exist" was not found', A.messages[0],
977 "A should have a syntax error reported")
978
979 def test_nodeset_not_found_error(self):
980 in_repo_conf = textwrap.dedent(
981 """
982 - job:
983 name: test
984 nodeset: does-not-exist
985 """)
986
987 file_dict = {'.zuul.yaml': in_repo_conf}
988 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
989 files=file_dict)
990 A.addApproval('Code-Review', 2)
991 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
992 self.waitUntilSettled()
993
994 self.assertEqual(A.data['status'], 'NEW')
995 self.assertEqual(A.reported, 1,
996 "A should report failure")
997 self.assertIn('nodeset "does-not-exist" was not found', A.messages[0],
998 "A should have a syntax error reported")
999
James E. Blair89e25eb2017-09-26 09:11:31 -07001000 def test_template_not_found_error(self):
1001 in_repo_conf = textwrap.dedent(
1002 """
1003 - job:
1004 name: project-test1
1005 - project:
1006 name: org/project
1007 templates:
1008 - does-not-exist
1009 """)
1010
1011 file_dict = {'.zuul.yaml': in_repo_conf}
1012 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1013 files=file_dict)
1014 A.addApproval('Code-Review', 2)
1015 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1016 self.waitUntilSettled()
1017
1018 self.assertEqual(A.data['status'], 'NEW')
1019 self.assertEqual(A.reported, 1,
1020 "A should report failure")
1021 self.assertIn('project template "does-not-exist" was not found',
1022 A.messages[0],
1023 "A should have a syntax error reported")
1024
Monty Taylor8be3c0c2017-10-06 10:37:37 -05001025 def test_job_list_in_project_template_not_dict_error(self):
1026 in_repo_conf = textwrap.dedent(
1027 """
1028 - job:
1029 name: project-test1
1030 - project-template:
1031 name: some-jobs
1032 check:
1033 jobs:
1034 - project-test1:
1035 - required-projects:
1036 org/project2
1037 """)
1038
1039 file_dict = {'.zuul.yaml': in_repo_conf}
1040 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1041 files=file_dict)
1042 A.addApproval('Code-Review', 2)
1043 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1044 self.waitUntilSettled()
1045
1046 self.assertEqual(A.data['status'], 'NEW')
1047 self.assertEqual(A.reported, 1,
1048 "A should report failure")
1049 self.assertIn('expected str for dictionary value',
1050 A.messages[0], "A should have a syntax error reported")
1051
1052 def test_job_list_in_project_not_dict_error(self):
1053 in_repo_conf = textwrap.dedent(
1054 """
1055 - job:
1056 name: project-test1
1057 - project:
1058 name: org/project1
1059 check:
1060 jobs:
1061 - project-test1:
1062 - required-projects:
1063 org/project2
1064 """)
1065
1066 file_dict = {'.zuul.yaml': in_repo_conf}
1067 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1068 files=file_dict)
1069 A.addApproval('Code-Review', 2)
1070 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1071 self.waitUntilSettled()
1072
1073 self.assertEqual(A.data['status'], 'NEW')
1074 self.assertEqual(A.reported, 1,
1075 "A should report failure")
1076 self.assertIn('expected str for dictionary value',
1077 A.messages[0], "A should have a syntax error reported")
1078
James E. Blair1235f142017-10-07 09:11:43 -07001079 def test_project_template(self):
1080 # Tests that a project template is not modified when used, and
1081 # can therefore be used in subsequent reconfigurations.
1082 in_repo_conf = textwrap.dedent(
1083 """
1084 - job:
1085 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -07001086 run: playbooks/project-test1.yaml
James E. Blair1235f142017-10-07 09:11:43 -07001087 - project-template:
1088 name: some-jobs
1089 tenant-one-gate:
1090 jobs:
1091 - project-test1:
1092 required-projects:
1093 - org/project1
1094 - project:
1095 name: org/project
1096 templates:
1097 - some-jobs
1098 """)
1099
1100 file_dict = {'.zuul.yaml': in_repo_conf}
1101 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1102 files=file_dict)
1103 A.addApproval('Code-Review', 2)
1104 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1105 self.waitUntilSettled()
1106 self.assertEqual(A.data['status'], 'MERGED')
1107 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1108 self.waitUntilSettled()
1109 in_repo_conf = textwrap.dedent(
1110 """
1111 - project:
1112 name: org/project1
1113 templates:
1114 - some-jobs
1115 """)
1116 file_dict = {'.zuul.yaml': in_repo_conf}
1117 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B',
1118 files=file_dict)
1119 B.addApproval('Code-Review', 2)
1120 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1121 self.waitUntilSettled()
1122 self.assertEqual(B.data['status'], 'MERGED')
1123
James E. Blairbccdfcf2017-10-07 13:37:26 -07001124 def test_job_remove_add(self):
1125 # Tests that a job can be removed from one repo and added in another.
1126 # First, remove the current config for project1 since it
1127 # references the job we want to remove.
1128 file_dict = {'.zuul.yaml': None}
1129 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1130 files=file_dict)
1131 A.setMerged()
1132 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1133 self.waitUntilSettled()
1134 # Then propose a change to delete the job from one repo...
1135 file_dict = {'.zuul.yaml': None}
1136 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1137 files=file_dict)
1138 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1139 self.waitUntilSettled()
1140 # ...and a second that depends on it that adds it to another repo.
1141 in_repo_conf = textwrap.dedent(
1142 """
1143 - job:
1144 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -07001145 run: playbooks/project-test1.yaml
James E. Blairbccdfcf2017-10-07 13:37:26 -07001146
1147 - project:
1148 name: org/project1
1149 check:
1150 jobs:
1151 - project-test1
1152 """)
1153 in_repo_playbook = textwrap.dedent(
1154 """
1155 - hosts: all
1156 tasks: []
1157 """)
1158 file_dict = {'.zuul.yaml': in_repo_conf,
1159 'playbooks/project-test1.yaml': in_repo_playbook}
1160 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C',
1161 files=file_dict,
1162 parent='refs/changes/1/1/1')
1163 C.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1164 C.subject, B.data['id'])
1165 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1166 self.waitUntilSettled()
1167 self.assertHistory([
1168 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1169 ], ordered=False)
1170
James E. Blair09f9ffe2017-07-11 15:30:25 -07001171 def test_multi_repo(self):
1172 downstream_repo_conf = textwrap.dedent(
1173 """
1174 - project:
1175 name: org/project1
1176 tenant-one-gate:
1177 jobs:
1178 - project-test1
1179
1180 - job:
1181 name: project1-test1
1182 parent: project-test1
1183 """)
1184
1185 file_dict = {'.zuul.yaml': downstream_repo_conf}
1186 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1187 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001188 A.addApproval('Code-Review', 2)
1189 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -07001190 self.waitUntilSettled()
1191
1192 self.assertEqual(A.data['status'], 'MERGED')
1193 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1194 self.waitUntilSettled()
1195
1196 upstream_repo_conf = textwrap.dedent(
1197 """
1198 - job:
1199 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -07001200 run: playbooks/project-test1.yaml
James E. Blair09f9ffe2017-07-11 15:30:25 -07001201
1202 - job:
1203 name: project-test2
1204
1205 - project:
1206 name: org/project
1207 tenant-one-gate:
1208 jobs:
1209 - project-test1
1210 """)
1211
1212 file_dict = {'.zuul.yaml': upstream_repo_conf}
1213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1214 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001215 B.addApproval('Code-Review', 2)
1216 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -07001217 self.waitUntilSettled()
1218
1219 self.assertEqual(B.data['status'], 'MERGED')
1220 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
1221 self.waitUntilSettled()
1222
1223 tenant = self.sched.abide.tenants.get('tenant-one')
1224 # Ensure the latest change is reflected in the config; if it
1225 # isn't this will raise an exception.
1226 tenant.layout.getJob('project-test2')
1227
James E. Blair332636e2017-09-05 10:14:35 -07001228 def test_pipeline_error(self):
1229 with open(os.path.join(FIXTURE_DIR,
1230 'config/in-repo/git/',
1231 'common-config/zuul.yaml')) as f:
1232 base_common_config = f.read()
1233
1234 in_repo_conf_A = textwrap.dedent(
1235 """
1236 - pipeline:
1237 name: periodic
1238 foo: error
1239 """)
1240
1241 file_dict = {'zuul.yaml': None,
1242 'zuul.d/main.yaml': base_common_config,
1243 'zuul.d/test1.yaml': in_repo_conf_A}
1244 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
1245 files=file_dict)
1246 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1247 self.waitUntilSettled()
1248 self.assertEqual(A.reported, 1,
1249 "A should report failure")
1250 self.assertIn('syntax error',
1251 A.messages[0],
1252 "A should have an error reported")
1253
1254 def test_change_series_error(self):
1255 with open(os.path.join(FIXTURE_DIR,
1256 'config/in-repo/git/',
1257 'common-config/zuul.yaml')) as f:
1258 base_common_config = f.read()
1259
1260 in_repo_conf_A = textwrap.dedent(
1261 """
1262 - pipeline:
1263 name: periodic
1264 foo: error
1265 """)
1266
1267 file_dict = {'zuul.yaml': None,
1268 'zuul.d/main.yaml': base_common_config,
1269 'zuul.d/test1.yaml': in_repo_conf_A}
1270 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
1271 files=file_dict)
1272
1273 in_repo_conf_B = textwrap.dedent(
1274 """
1275 - job:
1276 name: project-test2
1277 foo: error
1278 """)
1279
1280 file_dict = {'zuul.yaml': None,
1281 'zuul.d/main.yaml': base_common_config,
1282 'zuul.d/test1.yaml': in_repo_conf_A,
1283 'zuul.d/test2.yaml': in_repo_conf_B}
1284 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
1285 files=file_dict)
1286 B.setDependsOn(A, 1)
1287 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
1288 C.setDependsOn(B, 1)
1289 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1290 self.waitUntilSettled()
1291
1292 self.assertEqual(C.reported, 1,
1293 "C should report failure")
1294 self.assertIn('depends on a change that failed to merge',
1295 C.messages[0],
1296 "C should have an error reported")
1297
James E. Blairc73c73a2017-01-20 15:15:15 -08001298
James E. Blairc9455002017-09-06 09:22:19 -07001299class TestInRepoJoin(ZuulTestCase):
1300 # In this config, org/project is not a member of any pipelines, so
1301 # that we may test the changes that cause it to join them.
1302
1303 tenant_config_file = 'config/in-repo-join/main.yaml'
1304
1305 def test_dynamic_dependent_pipeline(self):
1306 # Test dynamically adding a project to a
1307 # dependent pipeline for the first time
1308 self.executor_server.hold_jobs_in_build = True
1309
1310 tenant = self.sched.abide.tenants.get('tenant-one')
1311 gate_pipeline = tenant.layout.pipelines['gate']
1312
1313 in_repo_conf = textwrap.dedent(
1314 """
1315 - job:
1316 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -07001317 run: playbooks/project-test1.yaml
James E. Blairc9455002017-09-06 09:22:19 -07001318
1319 - job:
1320 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -07001321 run: playbooks/project-test2.yaml
James E. Blairc9455002017-09-06 09:22:19 -07001322
1323 - project:
1324 name: org/project
1325 gate:
1326 jobs:
1327 - project-test2
1328 """)
1329
1330 in_repo_playbook = textwrap.dedent(
1331 """
1332 - hosts: all
1333 tasks: []
1334 """)
1335
1336 file_dict = {'.zuul.yaml': in_repo_conf,
1337 'playbooks/project-test2.yaml': in_repo_playbook}
1338 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1339 files=file_dict)
1340 A.addApproval('Code-Review', 2)
1341 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1342 self.waitUntilSettled()
1343
1344 items = gate_pipeline.getAllItems()
1345 self.assertEqual(items[0].change.number, '1')
1346 self.assertEqual(items[0].change.patchset, '1')
1347 self.assertTrue(items[0].live)
1348
1349 self.executor_server.hold_jobs_in_build = False
1350 self.executor_server.release()
1351 self.waitUntilSettled()
1352
1353 # Make sure the dynamic queue got cleaned up
1354 self.assertEqual(gate_pipeline.queues, [])
1355
1356 def test_dynamic_dependent_pipeline_failure(self):
1357 # Test that a change behind a failing change adding a project
1358 # to a dependent pipeline is dequeued.
1359 self.executor_server.hold_jobs_in_build = True
1360
1361 in_repo_conf = textwrap.dedent(
1362 """
1363 - job:
1364 name: project-test1
James E. Blair2f589fe2017-10-26 12:57:41 -07001365 run: playbooks/project-test1.yaml
James E. Blairc9455002017-09-06 09:22:19 -07001366
1367 - project:
1368 name: org/project
1369 gate:
1370 jobs:
1371 - project-test1
1372 """)
1373
1374 file_dict = {'.zuul.yaml': in_repo_conf}
1375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1376 files=file_dict)
1377 self.executor_server.failJob('project-test1', A)
1378 A.addApproval('Code-Review', 2)
1379 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1380 self.waitUntilSettled()
1381
1382 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1383 B.addApproval('Code-Review', 2)
1384 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1385 self.waitUntilSettled()
1386
James E. Blair3490c5d2017-09-07 08:33:23 -07001387 self.orderedRelease()
James E. Blairc9455002017-09-06 09:22:19 -07001388 self.waitUntilSettled()
1389 self.assertEqual(A.reported, 2,
1390 "A should report start and failure")
1391 self.assertEqual(A.data['status'], 'NEW')
1392 self.assertEqual(B.reported, 1,
1393 "B should report start")
1394 self.assertHistory([
1395 dict(name='project-test1', result='FAILURE', changes='1,1'),
James E. Blair3490c5d2017-09-07 08:33:23 -07001396 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
James E. Blairc9455002017-09-06 09:22:19 -07001397 ], ordered=False)
1398
James E. Blair0af198f2017-09-06 09:52:35 -07001399 def test_dynamic_dependent_pipeline_absent(self):
1400 # Test that a series of dependent changes don't report merge
1401 # failures to a pipeline they aren't in.
1402 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1403 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1404 B.setDependsOn(A, 1)
1405
1406 A.addApproval('Code-Review', 2)
1407 A.addApproval('Approved', 1)
1408 B.addApproval('Code-Review', 2)
1409 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1410 self.waitUntilSettled()
1411 self.assertEqual(A.reported, 0,
1412 "A should not report")
1413 self.assertEqual(A.data['status'], 'NEW')
1414 self.assertEqual(B.reported, 0,
1415 "B should not report")
1416 self.assertEqual(B.data['status'], 'NEW')
1417 self.assertHistory([])
1418
James E. Blairc9455002017-09-06 09:22:19 -07001419
James E. Blairc73c73a2017-01-20 15:15:15 -08001420class TestAnsible(AnsibleZuulTestCase):
1421 # A temporary class to hold new tests while others are disabled
1422
1423 tenant_config_file = 'config/ansible/main.yaml'
1424
1425 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +11001426 # Keep the jobdir around so we can inspect contents if an
1427 # assert fails.
1428 self.executor_server.keep_jobdir = True
1429 # Output extra ansible info so we might see errors.
1430 self.executor_server.verbose = True
1431 # Add a site variables file, used by check-vars
1432 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
1433 'variables.yaml')
1434 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -08001435 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1436 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1437 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +02001438 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +11001439 with self.jobLog(build_timeout):
1440 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001441 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +11001442 with self.jobLog(build_faillocal):
1443 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001444 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +11001445 with self.jobLog(build_failpost):
1446 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001447 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +11001448 with self.jobLog(build_check_vars):
1449 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -05001450 build_check_secret_names = self.getJobFromHistory('check-secret-names')
1451 with self.jobLog(build_check_secret_names):
1452 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001453 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +11001454 with self.jobLog(build_hello):
1455 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001456 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +11001457 with self.jobLog(build_python27):
1458 self.assertEqual(build_python27.result, 'SUCCESS')
1459 flag_path = os.path.join(self.test_root,
1460 build_python27.uuid + '.flag')
1461 self.assertTrue(os.path.exists(flag_path))
1462 copied_path = os.path.join(self.test_root, build_python27.uuid +
1463 '.copied')
1464 self.assertTrue(os.path.exists(copied_path))
1465 failed_path = os.path.join(self.test_root, build_python27.uuid +
1466 '.failed')
1467 self.assertFalse(os.path.exists(failed_path))
1468 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
1469 '.pre.flag')
1470 self.assertTrue(os.path.exists(pre_flag_path))
1471 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
1472 '.post.flag')
1473 self.assertTrue(os.path.exists(post_flag_path))
1474 bare_role_flag_path = os.path.join(self.test_root,
1475 build_python27.uuid +
1476 '.bare-role.flag')
1477 self.assertTrue(os.path.exists(bare_role_flag_path))
1478 secrets_path = os.path.join(self.test_root,
1479 build_python27.uuid + '.secrets')
1480 with open(secrets_path) as f:
1481 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -08001482
Jamie Lennox7655b552017-03-17 12:33:38 +11001483 msg = A.messages[0]
1484 success = "{} https://success.example.com/zuul-logs/{}"
1485 fail = "{} https://failure.example.com/zuul-logs/{}"
1486 self.assertIn(success.format("python27", build_python27.uuid), msg)
1487 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
1488 self.assertIn(success.format("check-vars",
1489 build_check_vars.uuid), msg)
1490 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
1491 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
1492 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +02001493
James E. Blairabbaa6f2017-04-06 16:11:44 -07001494 def _add_job(self, job_name):
1495 conf = textwrap.dedent(
1496 """
1497 - job:
1498 name: %s
James E. Blair2f589fe2017-10-26 12:57:41 -07001499 run: playbooks/%s.yaml
James E. Blairabbaa6f2017-04-06 16:11:44 -07001500
1501 - project:
1502 name: org/plugin-project
1503 check:
1504 jobs:
1505 - %s
James E. Blair2f589fe2017-10-26 12:57:41 -07001506 """ % (job_name, job_name, job_name))
James E. Blairabbaa6f2017-04-06 16:11:44 -07001507
1508 file_dict = {'.zuul.yaml': conf}
1509 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1510 files=file_dict)
1511 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1512 self.waitUntilSettled()
1513
1514 def test_plugins(self):
1515 # Keep the jobdir around so we can inspect contents if an
1516 # assert fails.
1517 self.executor_server.keep_jobdir = True
1518 # Output extra ansible info so we might see errors.
1519 self.executor_server.verbose = True
1520
1521 count = 0
1522 plugin_tests = [
1523 ('passwd', 'FAILURE'),
1524 ('cartesian', 'SUCCESS'),
1525 ('consul_kv', 'FAILURE'),
1526 ('credstash', 'FAILURE'),
1527 ('csvfile_good', 'SUCCESS'),
1528 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001529 ('uri_bad_path', 'FAILURE'),
1530 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001531 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001532 ('file_local_good', 'SUCCESS'),
1533 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001534 ]
1535 for job_name, result in plugin_tests:
1536 count += 1
1537 self._add_job(job_name)
1538
1539 job = self.getJobFromHistory(job_name)
1540 with self.jobLog(job):
1541 self.assertEqual(count, len(self.history))
1542 build = self.history[-1]
1543 self.assertEqual(build.result, result)
1544
1545 # TODOv3(jeblair): parse the ansible output and verify we're
1546 # getting the exception we expect.
1547
James E. Blairb9c0d772017-03-03 14:34:49 -08001548
James E. Blaira4d4eef2017-06-30 14:49:17 -07001549class TestPrePlaybooks(AnsibleZuulTestCase):
1550 # A temporary class to hold new tests while others are disabled
1551
1552 tenant_config_file = 'config/pre-playbook/main.yaml'
1553
1554 def test_pre_playbook_fail(self):
1555 # Test that we run the post playbooks (but not the actual
1556 # playbook) when a pre-playbook fails.
1557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1558 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1559 self.waitUntilSettled()
1560 build = self.getJobFromHistory('python27')
1561 self.assertIsNone(build.result)
1562 self.assertIn('RETRY_LIMIT', A.messages[0])
1563 flag_path = os.path.join(self.test_root, build.uuid +
1564 '.main.flag')
1565 self.assertFalse(os.path.exists(flag_path))
1566 pre_flag_path = os.path.join(self.test_root, build.uuid +
1567 '.pre.flag')
1568 self.assertFalse(os.path.exists(pre_flag_path))
1569 post_flag_path = os.path.join(self.test_root, build.uuid +
1570 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001571 self.assertTrue(os.path.exists(post_flag_path),
1572 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001573
1574
James E. Blairbacbb882017-10-17 09:48:23 -07001575class TestPostPlaybooks(AnsibleZuulTestCase):
1576 tenant_config_file = 'config/post-playbook/main.yaml'
1577
1578 def test_post_playbook_abort(self):
1579 # Test that when we abort a job in the post playbook, that we
1580 # don't send back POST_FAILURE.
1581 self.executor_server.verbose = True
1582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1583 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1584
1585 while not len(self.builds):
1586 time.sleep(0.1)
1587 build = self.builds[0]
1588
1589 post_start = os.path.join(self.test_root, build.uuid +
1590 '.post_start.flag')
1591 start = time.time()
1592 while time.time() < start + 90:
1593 if os.path.exists(post_start):
1594 break
1595 time.sleep(0.1)
1596 # The post playbook has started, abort the job
1597 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1598 self.waitUntilSettled()
1599
1600 build = self.getJobFromHistory('python27')
1601 self.assertEqual('ABORTED', build.result)
1602
1603 post_end = os.path.join(self.test_root, build.uuid +
1604 '.post_end.flag')
1605 self.assertTrue(os.path.exists(post_start))
1606 self.assertFalse(os.path.exists(post_end))
1607
1608
James E. Blairb9c0d772017-03-03 14:34:49 -08001609class TestBrokenConfig(ZuulTestCase):
1610 # Test that we get an appropriate syntax error if we start with a
1611 # broken config.
1612
1613 tenant_config_file = 'config/broken/main.yaml'
1614
1615 def setUp(self):
1616 with testtools.ExpectedException(
1617 zuul.configloader.ConfigurationSyntaxError,
1618 "\nZuul encountered a syntax error"):
1619 super(TestBrokenConfig, self).setUp()
1620
1621 def test_broken_config_on_startup(self):
1622 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001623
1624
1625class TestProjectKeys(ZuulTestCase):
1626 # Test that we can generate project keys
1627
1628 # Normally the test infrastructure copies a static key in place
1629 # for each project before starting tests. This saves time because
1630 # Zuul's automatic key-generation on startup can be slow. To make
1631 # sure we exercise that code, in this test we allow Zuul to create
1632 # keys for the project on startup.
1633 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001634 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001635 tenant_config_file = 'config/in-repo/main.yaml'
1636
1637 def test_key_generation(self):
1638 key_root = os.path.join(self.state_root, 'keys')
1639 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1640 # Make sure that a proper key was created on startup
1641 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001642 private_key, public_key = \
1643 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001644
1645 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1646 fixture_private_key = i.read()
1647
1648 # Make sure that we didn't just end up with the static fixture
1649 # key
1650 self.assertNotEqual(fixture_private_key, private_key)
1651
1652 # Make sure it's the right length
1653 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001654
1655
James E. Blairbb94dfa2017-07-11 07:45:19 -07001656class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001657 def _assertRolePath(self, build, playbook, content):
1658 path = os.path.join(self.test_root, build.uuid,
1659 'ansible', playbook, 'ansible.cfg')
1660 roles_paths = []
1661 with open(path) as f:
1662 for line in f:
1663 if line.startswith('roles_path'):
1664 roles_paths.append(line)
1665 print(roles_paths)
1666 if content:
1667 self.assertEqual(len(roles_paths), 1,
1668 "Should have one roles_path line in %s" %
1669 (playbook,))
1670 self.assertIn(content, roles_paths[0])
1671 else:
1672 self.assertEqual(len(roles_paths), 0,
1673 "Should have no roles_path line in %s" %
1674 (playbook,))
1675
James E. Blairbb94dfa2017-07-11 07:45:19 -07001676
1677class TestRoles(RoleTestCase):
1678 tenant_config_file = 'config/roles/main.yaml'
1679
James E. Blairbce76932017-05-04 10:03:15 -07001680 def test_role(self):
1681 # This exercises a proposed change to a role being checked out
1682 # and used.
1683 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1684 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1685 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1686 B.subject, A.data['id'])
1687 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1688 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1689 self.waitUntilSettled()
1690 self.assertHistory([
1691 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1692 ])
James E. Blair6459db12017-06-29 14:57:20 -07001693
James E. Blair1b27f6a2017-07-14 14:09:07 -07001694 def test_role_inheritance(self):
1695 self.executor_server.hold_jobs_in_build = True
1696 conf = textwrap.dedent(
1697 """
1698 - job:
1699 name: parent
1700 roles:
1701 - zuul: bare-role
1702 pre-run: playbooks/parent-pre
1703 post-run: playbooks/parent-post
1704
1705 - job:
1706 name: project-test
1707 parent: parent
James E. Blair2f589fe2017-10-26 12:57:41 -07001708 run: playbooks/project-test.yaml
James E. Blair1b27f6a2017-07-14 14:09:07 -07001709 roles:
1710 - zuul: org/project
1711
1712 - project:
1713 name: org/project
1714 check:
1715 jobs:
1716 - project-test
1717 """)
1718
1719 file_dict = {'.zuul.yaml': conf}
1720 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1721 files=file_dict)
1722 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1723 self.waitUntilSettled()
1724
1725 self.assertEqual(len(self.builds), 1)
1726 build = self.getBuildByName('project-test')
1727 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1728 self._assertRolePath(build, 'playbook_0', 'role_0')
1729 self._assertRolePath(build, 'playbook_0', 'role_1')
1730 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1731
1732 self.executor_server.hold_jobs_in_build = False
1733 self.executor_server.release()
1734 self.waitUntilSettled()
1735
1736 self.assertHistory([
1737 dict(name='project-test', result='SUCCESS', changes='1,1'),
1738 ])
1739
James E. Blair6f699732017-07-18 14:19:11 -07001740 def test_role_error(self):
1741 conf = textwrap.dedent(
1742 """
1743 - job:
1744 name: project-test
James E. Blair2f589fe2017-10-26 12:57:41 -07001745 run: playbooks/project-test.yaml
James E. Blair6f699732017-07-18 14:19:11 -07001746 roles:
1747 - zuul: common-config
1748
1749 - project:
1750 name: org/project
1751 check:
1752 jobs:
1753 - project-test
1754 """)
1755
1756 file_dict = {'.zuul.yaml': conf}
1757 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1758 files=file_dict)
1759 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1760 self.waitUntilSettled()
1761 self.assertIn(
1762 '- project-test project-test : ERROR Unable to find role',
1763 A.messages[-1])
1764
James E. Blair6459db12017-06-29 14:57:20 -07001765
James E. Blairbb94dfa2017-07-11 07:45:19 -07001766class TestImplicitRoles(RoleTestCase):
1767 tenant_config_file = 'config/implicit-roles/main.yaml'
1768
1769 def test_missing_roles(self):
1770 # Test implicit and explicit roles for a project which does
1771 # not have roles. The implicit role should be silently
1772 # ignored since the project doesn't supply roles, but if a
1773 # user declares an explicit role, it should error.
1774 self.executor_server.hold_jobs_in_build = True
1775 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1776 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1777 self.waitUntilSettled()
1778
1779 self.assertEqual(len(self.builds), 2)
1780 build = self.getBuildByName('implicit-role-fail')
1781 self._assertRolePath(build, 'playbook_0', None)
1782
1783 self.executor_server.hold_jobs_in_build = False
1784 self.executor_server.release()
1785 self.waitUntilSettled()
1786 # The retry_limit doesn't get recorded
1787 self.assertHistory([
1788 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1789 ])
1790
1791 def test_roles(self):
1792 # Test implicit and explicit roles for a project which does
1793 # have roles. In both cases, we should end up with the role
1794 # in the path. In the explicit case, ensure we end up with
1795 # the name we specified.
1796 self.executor_server.hold_jobs_in_build = True
1797 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1798 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1799 self.waitUntilSettled()
1800
1801 self.assertEqual(len(self.builds), 2)
1802 build = self.getBuildByName('implicit-role-ok')
1803 self._assertRolePath(build, 'playbook_0', 'role_0')
1804
1805 build = self.getBuildByName('explicit-role-ok')
1806 self._assertRolePath(build, 'playbook_0', 'role_0')
1807
1808 self.executor_server.hold_jobs_in_build = False
1809 self.executor_server.release()
1810 self.waitUntilSettled()
1811 self.assertHistory([
1812 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1813 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1814 ], ordered=False)
1815
1816
James E. Blair6459db12017-06-29 14:57:20 -07001817class TestShadow(ZuulTestCase):
1818 tenant_config_file = 'config/shadow/main.yaml'
1819
1820 def test_shadow(self):
1821 # Test that a repo is allowed to shadow another's job definitions.
1822 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1823 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1824 self.waitUntilSettled()
1825 self.assertHistory([
1826 dict(name='test1', result='SUCCESS', changes='1,1'),
1827 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001828 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001829
1830
1831class TestDataReturn(AnsibleZuulTestCase):
1832 tenant_config_file = 'config/data-return/main.yaml'
1833
1834 def test_data_return(self):
James E. Blair196f61a2017-06-30 15:42:29 -07001835 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1836 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1837 self.waitUntilSettled()
1838 self.assertHistory([
1839 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001840 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
James E. Blair698703c2017-09-15 20:58:30 -06001841 dict(name='child', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001842 ], ordered=False)
1843 self.assertIn('- data-return http://example.com/test/log/url/',
1844 A.messages[-1])
1845 self.assertIn('- data-return-relative '
1846 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001847 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001848
1849
1850class TestDiskAccounting(AnsibleZuulTestCase):
1851 config_file = 'zuul-disk-accounting.conf'
1852 tenant_config_file = 'config/disk-accountant/main.yaml'
1853
1854 def test_disk_accountant_kills_job(self):
1855 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1856 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1857 self.waitUntilSettled()
1858 self.assertHistory([
1859 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001860
1861
1862class TestMaxNodesPerJob(AnsibleZuulTestCase):
1863 tenant_config_file = 'config/multi-tenant/main.yaml'
1864
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001865 def test_max_timeout_exceeded(self):
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001866 in_repo_conf = textwrap.dedent(
1867 """
1868 - job:
1869 name: test-job
James E. Blair7e3e6882017-09-20 15:47:13 -07001870 nodeset:
1871 nodes:
1872 - name: node01
1873 label: fake
1874 - name: node02
1875 label: fake
1876 - name: node03
1877 label: fake
1878 - name: node04
1879 label: fake
1880 - name: node05
1881 label: fake
1882 - name: node06
1883 label: fake
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001884 """)
1885 file_dict = {'.zuul.yaml': in_repo_conf}
1886 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1887 files=file_dict)
1888 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1889 self.waitUntilSettled()
1890 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1891 A.messages[0], "A should fail because of nodes limit")
1892
1893 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1894 files=file_dict)
1895 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1896 self.waitUntilSettled()
1897 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1898 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001899
1900
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001901class TestMaxTimeout(AnsibleZuulTestCase):
1902 tenant_config_file = 'config/multi-tenant/main.yaml'
1903
1904 def test_max_nodes_reached(self):
1905 in_repo_conf = textwrap.dedent(
1906 """
1907 - job:
1908 name: test-job
1909 timeout: 3600
1910 """)
1911 file_dict = {'.zuul.yaml': in_repo_conf}
1912 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1913 files=file_dict)
1914 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1915 self.waitUntilSettled()
1916 self.assertIn('The job "test-job" exceeds tenant max-job-timeout',
1917 A.messages[0], "A should fail because of timeout limit")
1918
1919 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1920 files=file_dict)
1921 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1922 self.waitUntilSettled()
1923 self.assertNotIn("exceeds tenant max-job-timeout", B.messages[0],
1924 "B should not fail because of timeout limit")
1925
1926
James E. Blair7edc25f2017-10-26 10:47:14 -07001927class TestPragma(ZuulTestCase):
1928 tenant_config_file = 'config/pragma/main.yaml'
1929
1930 def test_no_pragma(self):
1931 self.create_branch('org/project', 'stable')
1932 with open(os.path.join(FIXTURE_DIR,
1933 'config/pragma/git/',
1934 'org_project/nopragma.yaml')) as f:
1935 config = f.read()
1936 file_dict = {'.zuul.yaml': config}
1937 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1938 files=file_dict)
1939 A.addApproval('Code-Review', 2)
1940 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1941 self.waitUntilSettled()
1942 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1943 self.waitUntilSettled()
1944
1945 # This is an untrusted repo with 2 branches, so it should have
1946 # an implied branch matcher for the job.
1947 tenant = self.sched.abide.tenants.get('tenant-one')
1948 jobs = tenant.layout.getJobs('test-job')
1949 self.assertEqual(len(jobs), 1)
1950 for job in tenant.layout.getJobs('test-job'):
1951 self.assertIsNotNone(job.branch_matcher)
1952
1953 def test_pragma(self):
1954 self.create_branch('org/project', 'stable')
1955 with open(os.path.join(FIXTURE_DIR,
1956 'config/pragma/git/',
1957 'org_project/pragma.yaml')) as f:
1958 config = f.read()
1959 file_dict = {'.zuul.yaml': config}
1960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1961 files=file_dict)
1962 A.addApproval('Code-Review', 2)
1963 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1964 self.waitUntilSettled()
1965 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1966 self.waitUntilSettled()
1967
1968 # This is an untrusted repo with 2 branches, so it would
1969 # normally have an implied branch matcher, but our pragma
1970 # overrides it.
1971 tenant = self.sched.abide.tenants.get('tenant-one')
1972 jobs = tenant.layout.getJobs('test-job')
1973 self.assertEqual(len(jobs), 1)
1974 for job in tenant.layout.getJobs('test-job'):
1975 self.assertIsNone(job.branch_matcher)
1976
1977
James E. Blair2bab6e72017-08-07 09:52:45 -07001978class TestBaseJobs(ZuulTestCase):
1979 tenant_config_file = 'config/base-jobs/main.yaml'
1980
1981 def test_multiple_base_jobs(self):
1982 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1983 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1984 self.waitUntilSettled()
1985 self.assertHistory([
1986 dict(name='my-job', result='SUCCESS', changes='1,1'),
1987 dict(name='other-job', result='SUCCESS', changes='1,1'),
1988 ], ordered=False)
1989 self.assertEqual(self.getJobFromHistory('my-job').
1990 parameters['zuul']['jobtags'],
1991 ['mybase'])
1992 self.assertEqual(self.getJobFromHistory('other-job').
1993 parameters['zuul']['jobtags'],
1994 ['otherbase'])
1995
1996 def test_untrusted_base_job(self):
1997 """Test that a base job may not be defined in an untrusted repo"""
1998 in_repo_conf = textwrap.dedent(
1999 """
2000 - job:
2001 name: fail-base
2002 parent: null
2003 """)
2004
2005 file_dict = {'.zuul.yaml': in_repo_conf}
2006 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
2007 files=file_dict)
2008 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2009 self.waitUntilSettled()
2010 self.assertEqual(A.reported, 1,
2011 "A should report failure")
2012 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
2013 self.assertIn('Base jobs must be defined in config projects',
2014 A.messages[0])
2015 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07002016
2017
James E. Blairdf91ab32017-10-25 17:57:13 -07002018class TestSecretInheritance(ZuulTestCase):
2019 tenant_config_file = 'config/secret-inheritance/main.yaml'
2020
2021 def _getSecrets(self, job, pbtype):
2022 secrets = []
2023 build = self.getJobFromHistory(job)
2024 for pb in build.parameters[pbtype]:
2025 secrets.append(pb['secrets'])
2026 return secrets
2027
2028 def _checkTrustedSecrets(self):
2029 secret = {'longpassword': 'test-passwordtest-password',
2030 'password': 'test-password',
2031 'username': 'test-username'}
2032 self.assertEqual(
2033 self._getSecrets('trusted-secrets', 'playbooks'),
James E. Blair2f589fe2017-10-26 12:57:41 -07002034 [{'trusted-secret': secret}])
James E. Blairdf91ab32017-10-25 17:57:13 -07002035 self.assertEqual(
2036 self._getSecrets('trusted-secrets', 'pre_playbooks'), [])
2037 self.assertEqual(
2038 self._getSecrets('trusted-secrets', 'post_playbooks'), [])
2039
2040 self.assertEqual(
2041 self._getSecrets('trusted-secrets-trusted-child',
James E. Blair2f589fe2017-10-26 12:57:41 -07002042 'playbooks'), [{}])
James E. Blairdf91ab32017-10-25 17:57:13 -07002043 self.assertEqual(
2044 self._getSecrets('trusted-secrets-trusted-child',
2045 'pre_playbooks'), [])
2046 self.assertEqual(
2047 self._getSecrets('trusted-secrets-trusted-child',
2048 'post_playbooks'), [])
2049
2050 self.assertEqual(
2051 self._getSecrets('trusted-secrets-untrusted-child',
James E. Blair2f589fe2017-10-26 12:57:41 -07002052 'playbooks'), [{}])
James E. Blairdf91ab32017-10-25 17:57:13 -07002053 self.assertEqual(
2054 self._getSecrets('trusted-secrets-untrusted-child',
2055 'pre_playbooks'), [])
2056 self.assertEqual(
2057 self._getSecrets('trusted-secrets-untrusted-child',
2058 'post_playbooks'), [])
2059
2060 def _checkUntrustedSecrets(self):
2061 secret = {'longpassword': 'test-passwordtest-password',
2062 'password': 'test-password',
2063 'username': 'test-username'}
2064 self.assertEqual(
2065 self._getSecrets('untrusted-secrets', 'playbooks'),
James E. Blair2f589fe2017-10-26 12:57:41 -07002066 [{'untrusted-secret': secret}])
James E. Blairdf91ab32017-10-25 17:57:13 -07002067 self.assertEqual(
2068 self._getSecrets('untrusted-secrets', 'pre_playbooks'), [])
2069 self.assertEqual(
2070 self._getSecrets('untrusted-secrets', 'post_playbooks'), [])
2071
2072 self.assertEqual(
2073 self._getSecrets('untrusted-secrets-trusted-child',
James E. Blair2f589fe2017-10-26 12:57:41 -07002074 'playbooks'), [{}])
James E. Blairdf91ab32017-10-25 17:57:13 -07002075 self.assertEqual(
2076 self._getSecrets('untrusted-secrets-trusted-child',
2077 'pre_playbooks'), [])
2078 self.assertEqual(
2079 self._getSecrets('untrusted-secrets-trusted-child',
2080 'post_playbooks'), [])
2081
2082 self.assertEqual(
2083 self._getSecrets('untrusted-secrets-untrusted-child',
James E. Blair2f589fe2017-10-26 12:57:41 -07002084 'playbooks'), [{}])
James E. Blairdf91ab32017-10-25 17:57:13 -07002085 self.assertEqual(
2086 self._getSecrets('untrusted-secrets-untrusted-child',
2087 'pre_playbooks'), [])
2088 self.assertEqual(
2089 self._getSecrets('untrusted-secrets-untrusted-child',
2090 'post_playbooks'), [])
2091
2092 def test_trusted_secret_inheritance_check(self):
2093 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
2094 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2095 self.waitUntilSettled()
2096 self.assertHistory([
2097 dict(name='trusted-secrets', result='SUCCESS', changes='1,1'),
2098 dict(name='trusted-secrets-trusted-child',
2099 result='SUCCESS', changes='1,1'),
2100 dict(name='trusted-secrets-untrusted-child',
2101 result='SUCCESS', changes='1,1'),
2102 ], ordered=False)
2103
2104 self._checkTrustedSecrets()
2105
2106 def test_untrusted_secret_inheritance_gate(self):
2107 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
2108 A.addApproval('Code-Review', 2)
2109 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2110 self.waitUntilSettled()
2111 self.assertHistory([
2112 dict(name='untrusted-secrets', result='SUCCESS', changes='1,1'),
2113 dict(name='untrusted-secrets-trusted-child',
2114 result='SUCCESS', changes='1,1'),
2115 dict(name='untrusted-secrets-untrusted-child',
2116 result='SUCCESS', changes='1,1'),
2117 ], ordered=False)
2118
2119 self._checkUntrustedSecrets()
2120
2121 def test_untrusted_secret_inheritance_check(self):
2122 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2123 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2124 self.waitUntilSettled()
2125 # This configuration tries to run untrusted secrets in an
2126 # non-post-review pipeline and should therefore run no jobs.
2127 self.assertHistory([])
2128
2129
James E. Blairdb089032017-08-15 13:42:12 -07002130class TestSecretLeaks(AnsibleZuulTestCase):
2131 tenant_config_file = 'config/secret-leaks/main.yaml'
2132
2133 def searchForContent(self, path, content):
2134 matches = []
2135 for (dirpath, dirnames, filenames) in os.walk(path):
2136 for filename in filenames:
2137 filepath = os.path.join(dirpath, filename)
2138 with open(filepath, 'rb') as f:
2139 if content in f.read():
2140 matches.append(filepath[len(path):])
2141 return matches
2142
2143 def _test_secret_file(self):
2144 # Or rather -- test that they *don't* leak.
2145 # Keep the jobdir around so we can inspect contents.
2146 self.executor_server.keep_jobdir = True
2147 conf = textwrap.dedent(
2148 """
2149 - project:
2150 name: org/project
2151 check:
2152 jobs:
2153 - secret-file
2154 """)
2155
2156 file_dict = {'.zuul.yaml': conf}
2157 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
2158 files=file_dict)
2159 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2160 self.waitUntilSettled()
2161 self.assertHistory([
2162 dict(name='secret-file', result='SUCCESS', changes='1,1'),
2163 ], ordered=False)
2164 matches = self.searchForContent(self.history[0].jobdir.root,
2165 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07002166 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07002167 set(matches))
2168
2169 def test_secret_file(self):
2170 self._test_secret_file()
2171
2172 def test_secret_file_verbose(self):
2173 # Output extra ansible info to exercise alternate logging code
2174 # paths.
2175 self.executor_server.verbose = True
2176 self._test_secret_file()
2177
2178 def _test_secret_file_fail(self):
2179 # Or rather -- test that they *don't* leak.
2180 # Keep the jobdir around so we can inspect contents.
2181 self.executor_server.keep_jobdir = True
2182 conf = textwrap.dedent(
2183 """
2184 - project:
2185 name: org/project
2186 check:
2187 jobs:
2188 - secret-file-fail
2189 """)
2190
2191 file_dict = {'.zuul.yaml': conf}
2192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
2193 files=file_dict)
2194 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2195 self.waitUntilSettled()
2196 self.assertHistory([
2197 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
2198 ], ordered=False)
2199 matches = self.searchForContent(self.history[0].jobdir.root,
2200 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07002201 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07002202 set(matches))
2203
2204 def test_secret_file_fail(self):
2205 self._test_secret_file_fail()
2206
2207 def test_secret_file_fail_verbose(self):
2208 # Output extra ansible info to exercise alternate logging code
2209 # paths.
2210 self.executor_server.verbose = True
2211 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07002212
2213
2214class TestJobOutput(AnsibleZuulTestCase):
2215 tenant_config_file = 'config/job-output/main.yaml'
2216
2217 def _get_file(self, build, path):
2218 p = os.path.join(build.jobdir.root, path)
2219 with open(p) as f:
2220 return f.read()
2221
2222 def test_job_output(self):
Monty Taylor0e2489a2017-10-10 11:57:29 -05002223 # Verify that command standard output appears in the job output,
2224 # and that failures in the final playbook get logged.
James E. Blaira00910c2017-08-23 09:15:04 -07002225
2226 # This currently only verifies we receive output from
2227 # localhost. Notably, it does not verify we receive output
2228 # via zuul_console streaming.
2229 self.executor_server.keep_jobdir = True
2230 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2231 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2232 self.waitUntilSettled()
2233 self.assertHistory([
2234 dict(name='job-output', result='SUCCESS', changes='1,1'),
2235 ], ordered=False)
2236
2237 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
2238 j = json.loads(self._get_file(self.history[0],
2239 'work/logs/job-output.json'))
2240 self.assertEqual(token,
2241 j[0]['plays'][0]['tasks'][0]
2242 ['hosts']['localhost']['stdout'])
2243
2244 print(self._get_file(self.history[0],
2245 'work/logs/job-output.txt'))
2246 self.assertIn(token,
2247 self._get_file(self.history[0],
2248 'work/logs/job-output.txt'))
Monty Taylor0e2489a2017-10-10 11:57:29 -05002249
2250 def test_job_output_failure_log(self):
2251 logger = logging.getLogger('zuul.AnsibleJob')
2252 output = io.StringIO()
2253 logger.addHandler(logging.StreamHandler(output))
2254
2255 # Verify that a failure in the last post playbook emits the contents
2256 # of the json output to the log
2257 self.executor_server.keep_jobdir = True
2258 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
2259 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2260 self.waitUntilSettled()
2261 self.assertHistory([
2262 dict(name='job-output-failure',
2263 result='POST_FAILURE', changes='1,1'),
2264 ], ordered=False)
2265
2266 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
2267 j = json.loads(self._get_file(self.history[0],
2268 'work/logs/job-output.json'))
2269 self.assertEqual(token,
2270 j[0]['plays'][0]['tasks'][0]
2271 ['hosts']['localhost']['stdout'])
2272
2273 print(self._get_file(self.history[0],
2274 'work/logs/job-output.json'))
2275 self.assertIn(token,
2276 self._get_file(self.history[0],
2277 'work/logs/job-output.txt'))
2278
2279 log_output = output.getvalue()
2280 self.assertIn('Final playbook failed', log_output)
2281 self.assertIn('Failure test', log_output)