blob: 99c87e07e3414f0d6b59b8ee745518536fe71017 [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
23from unittest import skip
James E. Blair59fdbac2015-12-07 17:08:06 -080024
James E. Blairb9c0d772017-03-03 14:34:49 -080025import testtools
26
27import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070028from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000029from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080030
James E. Blair59fdbac2015-12-07 17:08:06 -080031
James E. Blair3f876d52016-07-22 13:07:14 -070032class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080033 # A temporary class to hold new tests while others are disabled
34
James E. Blair2a629ec2015-12-22 15:32:02 -080035 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080036
James E. Blair83005782015-12-11 14:46:03 -080037 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080038 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020039 A.addApproval('Code-Review', 2)
40 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080041 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080042 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080043 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080044 self.assertEqual(self.getJobFromHistory('python27').result,
45 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080046 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080047 self.assertEqual(A.reported, 2,
48 "A should report start and success")
49 self.assertIn('tenant-one-gate', A.messages[1],
50 "A should transit tenant-one gate")
51 self.assertNotIn('tenant-two-gate', A.messages[1],
52 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080053
James E. Blair96f26942015-12-09 10:15:59 -080054 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020055 B.addApproval('Code-Review', 2)
56 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080057 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080058 self.assertEqual(self.getJobFromHistory('python27',
59 'org/project2').result,
60 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080061 self.assertEqual(self.getJobFromHistory('project2-test1').result,
62 'SUCCESS')
63 self.assertEqual(B.data['status'], 'MERGED')
64 self.assertEqual(B.reported, 2,
65 "B should report start and success")
66 self.assertIn('tenant-two-gate', B.messages[1],
67 "B should transit tenant-two gate")
68 self.assertNotIn('tenant-one-gate', B.messages[1],
69 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080070
James E. Blair96f26942015-12-09 10:15:59 -080071 self.assertEqual(A.reported, 2, "Activity in tenant two should"
72 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080073
James E. Blair83005782015-12-11 14:46:03 -080074
Tobias Henkel83167622017-06-30 19:45:03 +020075class TestFinal(ZuulTestCase):
76
77 tenant_config_file = 'config/final/main.yaml'
78
79 def test_final_variant_ok(self):
80 # test clean usage of final parent job
81 in_repo_conf = textwrap.dedent(
82 """
83 - project:
84 name: org/project
85 check:
86 jobs:
87 - job-final
88 """)
89
90 file_dict = {'.zuul.yaml': in_repo_conf}
91 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
92 files=file_dict)
93 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
94 self.waitUntilSettled()
95
96 self.assertEqual(A.reported, 1)
97 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
98
99 def test_final_variant_error(self):
100 # test misuse of final parent job
101 in_repo_conf = textwrap.dedent(
102 """
103 - project:
104 name: org/project
105 check:
106 jobs:
107 - job-final:
108 vars:
109 dont_override_this: bar
110 """)
111 file_dict = {'.zuul.yaml': in_repo_conf}
112 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
113 files=file_dict)
114 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
115 self.waitUntilSettled()
116
117 # The second patch tried to override some variables.
118 # Thus it should fail.
119 self.assertEqual(A.reported, 1)
120 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
121 self.assertIn('Unable to modify final job', A.messages[0])
122
123 def test_final_inheritance(self):
124 # test misuse of final parent job
125 in_repo_conf = textwrap.dedent(
126 """
127 - job:
128 name: project-test
129 parent: job-final
130
131 - project:
132 name: org/project
133 check:
134 jobs:
135 - project-test
136 """)
137
138 in_repo_playbook = textwrap.dedent(
139 """
140 - hosts: all
141 tasks: []
142 """)
143
144 file_dict = {'.zuul.yaml': in_repo_conf,
145 'playbooks/project-test.yaml': in_repo_playbook}
146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
147 files=file_dict)
148 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
149 self.waitUntilSettled()
150
151 # The second patch tried to override some variables.
152 # Thus it should fail.
153 self.assertEqual(A.reported, 1)
154 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
155 self.assertIn('Unable to inherit from final job', A.messages[0])
156
157
James E. Blairff555742017-02-19 11:34:27 -0800158class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800159 # A temporary class to hold new tests while others are disabled
160
Tobias Henkelabf973e2017-07-28 10:07:34 +0200161 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800162 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800163
James E. Blair83005782015-12-11 14:46:03 -0800164 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800165 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200166 A.addApproval('Code-Review', 2)
167 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800168 self.waitUntilSettled()
169 self.assertEqual(self.getJobFromHistory('project-test1').result,
170 'SUCCESS')
171 self.assertEqual(A.data['status'], 'MERGED')
172 self.assertEqual(A.reported, 2,
173 "A should report start and success")
174 self.assertIn('tenant-one-gate', A.messages[1],
175 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800176
James E. Blair3a098dd2017-10-04 14:37:29 -0700177 @skip("This test is useful, but not reliable")
178 def test_full_and_dynamic_reconfig(self):
179 self.executor_server.hold_jobs_in_build = True
180 in_repo_conf = textwrap.dedent(
181 """
182 - job:
183 name: project-test1
184
185 - project:
186 name: org/project
187 tenant-one-gate:
188 jobs:
189 - project-test1
190 """)
191
192 file_dict = {'.zuul.yaml': in_repo_conf}
193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
194 files=file_dict)
195 A.addApproval('Code-Review', 2)
196 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
197 self.waitUntilSettled()
198 self.sched.reconfigure(self.config)
199 self.waitUntilSettled()
200
201 gc.collect()
202 pipelines = [obj for obj in gc.get_objects()
203 if isinstance(obj, zuul.model.Pipeline)]
204 self.assertEqual(len(pipelines), 4)
205
206 self.executor_server.hold_jobs_in_build = False
207 self.executor_server.release()
208 self.waitUntilSettled()
209
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700210 def test_dynamic_config(self):
211 in_repo_conf = textwrap.dedent(
212 """
213 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200214 name: project-test1
215
216 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700217 name: project-test2
218
219 - project:
220 name: org/project
221 tenant-one-gate:
222 jobs:
223 - project-test2
224 """)
225
James E. Blairc73c73a2017-01-20 15:15:15 -0800226 in_repo_playbook = textwrap.dedent(
227 """
228 - hosts: all
229 tasks: []
230 """)
231
232 file_dict = {'.zuul.yaml': in_repo_conf,
233 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700234 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800235 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200236 A.addApproval('Code-Review', 2)
237 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700238 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700239 self.assertEqual(A.data['status'], 'MERGED')
240 self.assertEqual(A.reported, 2,
241 "A should report start and success")
242 self.assertIn('tenant-one-gate', A.messages[1],
243 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800244 self.assertHistory([
245 dict(name='project-test2', result='SUCCESS', changes='1,1')])
246
James E. Blairc2a5ed72017-02-20 14:12:01 -0500247 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800248 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500249
James E. Blair646322f2017-01-27 15:50:34 -0800250 # Now that the config change is landed, it should be live for
251 # subsequent changes.
252 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200253 B.addApproval('Code-Review', 2)
254 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800255 self.waitUntilSettled()
256 self.assertEqual(self.getJobFromHistory('project-test2').result,
257 'SUCCESS')
258 self.assertHistory([
259 dict(name='project-test2', result='SUCCESS', changes='1,1'),
260 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800261
Tobias Henkelf02cf512017-07-21 22:55:34 +0200262 def test_dynamic_config_non_existing_job(self):
263 """Test that requesting a non existent job fails"""
264 in_repo_conf = textwrap.dedent(
265 """
266 - job:
267 name: project-test1
268
269 - project:
270 name: org/project
271 check:
272 jobs:
273 - non-existent-job
274 """)
275
276 in_repo_playbook = textwrap.dedent(
277 """
278 - hosts: all
279 tasks: []
280 """)
281
282 file_dict = {'.zuul.yaml': in_repo_conf,
283 'playbooks/project-test2.yaml': in_repo_playbook}
284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
285 files=file_dict)
286 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
287 self.waitUntilSettled()
288 self.assertEqual(A.reported, 1,
289 "A should report failure")
290 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
291 self.assertIn('Job non-existent-job not defined', A.messages[0],
292 "A should have failed the check pipeline")
293 self.assertHistory([])
294
295 def test_dynamic_config_non_existing_job_in_template(self):
296 """Test that requesting a non existent job fails"""
297 in_repo_conf = textwrap.dedent(
298 """
299 - job:
300 name: project-test1
301
302 - project-template:
303 name: test-template
304 check:
305 jobs:
306 - non-existent-job
307
308 - project:
309 name: org/project
310 templates:
311 - test-template
312 """)
313
314 in_repo_playbook = textwrap.dedent(
315 """
316 - hosts: all
317 tasks: []
318 """)
319
320 file_dict = {'.zuul.yaml': in_repo_conf,
321 'playbooks/project-test2.yaml': in_repo_playbook}
322 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
323 files=file_dict)
324 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
325 self.waitUntilSettled()
326 self.assertEqual(A.reported, 1,
327 "A should report failure")
328 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
329 self.assertIn('Job non-existent-job not defined', A.messages[0],
330 "A should have failed the check pipeline")
331 self.assertHistory([])
332
Tobias Henkel0f714002017-06-30 23:30:52 +0200333 def test_dynamic_config_new_patchset(self):
334 self.executor_server.hold_jobs_in_build = True
335
336 tenant = self.sched.abide.tenants.get('tenant-one')
337 check_pipeline = tenant.layout.pipelines['check']
338
339 in_repo_conf = textwrap.dedent(
340 """
341 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200342 name: project-test1
343
344 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200345 name: project-test2
346
347 - project:
348 name: org/project
349 check:
350 jobs:
351 - project-test2
352 """)
353
354 in_repo_playbook = textwrap.dedent(
355 """
356 - hosts: all
357 tasks: []
358 """)
359
360 file_dict = {'.zuul.yaml': in_repo_conf,
361 'playbooks/project-test2.yaml': in_repo_playbook}
362 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
363 files=file_dict)
364 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
365 self.waitUntilSettled()
366
367 items = check_pipeline.getAllItems()
368 self.assertEqual(items[0].change.number, '1')
369 self.assertEqual(items[0].change.patchset, '1')
370 self.assertTrue(items[0].live)
371
372 in_repo_conf = textwrap.dedent(
373 """
374 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200375 name: project-test1
376
377 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200378 name: project-test2
379
380 - project:
381 name: org/project
382 check:
383 jobs:
384 - project-test1
385 - project-test2
386 """)
387 file_dict = {'.zuul.yaml': in_repo_conf,
388 'playbooks/project-test2.yaml': in_repo_playbook}
389
390 A.addPatchset(files=file_dict)
391 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
392
393 self.waitUntilSettled()
394
395 items = check_pipeline.getAllItems()
396 self.assertEqual(items[0].change.number, '1')
397 self.assertEqual(items[0].change.patchset, '2')
398 self.assertTrue(items[0].live)
399
400 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200401 self.executor_server.release('project-test1')
402 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200403 self.executor_server.release()
404 self.waitUntilSettled()
405
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200406 self.assertHistory([
407 dict(name='project-test2', result='ABORTED', changes='1,1'),
408 dict(name='project-test1', result='SUCCESS', changes='1,2'),
409 dict(name='project-test2', result='SUCCESS', changes='1,2')])
410
James E. Blairff555742017-02-19 11:34:27 -0800411 def test_in_repo_branch(self):
412 in_repo_conf = textwrap.dedent(
413 """
414 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200415 name: project-test1
416
417 - job:
James E. Blairff555742017-02-19 11:34:27 -0800418 name: project-test2
419
420 - project:
421 name: org/project
422 tenant-one-gate:
423 jobs:
424 - project-test2
425 """)
426
427 in_repo_playbook = textwrap.dedent(
428 """
429 - hosts: all
430 tasks: []
431 """)
432
433 file_dict = {'.zuul.yaml': in_repo_conf,
434 'playbooks/project-test2.yaml': in_repo_playbook}
435 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700436 self.fake_gerrit.addEvent(
437 self.fake_gerrit.getFakeBranchCreatedEvent(
438 'org/project', 'stable'))
James E. Blair6069f2b2017-09-26 16:34:11 -0700439 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800440 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
441 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200442 A.addApproval('Code-Review', 2)
443 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800444 self.waitUntilSettled()
445 self.assertEqual(A.data['status'], 'MERGED')
446 self.assertEqual(A.reported, 2,
447 "A should report start and success")
448 self.assertIn('tenant-one-gate', A.messages[1],
449 "A should transit tenant-one gate")
450 self.assertHistory([
451 dict(name='project-test2', result='SUCCESS', changes='1,1')])
452 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800453 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800454
455 # The config change should not affect master.
456 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200457 B.addApproval('Code-Review', 2)
458 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800459 self.waitUntilSettled()
460 self.assertHistory([
461 dict(name='project-test2', result='SUCCESS', changes='1,1'),
462 dict(name='project-test1', result='SUCCESS', changes='2,1')])
463
464 # The config change should be live for further changes on
465 # stable.
466 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200467 C.addApproval('Code-Review', 2)
468 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800469 self.waitUntilSettled()
470 self.assertHistory([
471 dict(name='project-test2', result='SUCCESS', changes='1,1'),
472 dict(name='project-test1', result='SUCCESS', changes='2,1'),
473 dict(name='project-test2', result='SUCCESS', changes='3,1')])
474
James E. Blaira5a12492017-05-03 11:40:48 -0700475 def test_crd_dynamic_config_branch(self):
476 # Test that we can create a job in one repo and be able to use
477 # it from a different branch on a different repo.
478
479 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700480 self.fake_gerrit.addEvent(
481 self.fake_gerrit.getFakeBranchCreatedEvent(
482 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700483
484 in_repo_conf = textwrap.dedent(
485 """
486 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200487 name: project-test1
488
489 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700490 name: project-test2
491
492 - project:
493 name: org/project
494 check:
495 jobs:
496 - project-test2
497 """)
498
499 in_repo_playbook = textwrap.dedent(
500 """
501 - hosts: all
502 tasks: []
503 """)
504
505 file_dict = {'.zuul.yaml': in_repo_conf,
506 'playbooks/project-test2.yaml': in_repo_playbook}
507 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
508 files=file_dict)
509
510 second_repo_conf = textwrap.dedent(
511 """
512 - project:
513 name: org/project1
514 check:
515 jobs:
516 - project-test2
517 """)
518
519 second_file_dict = {'.zuul.yaml': second_repo_conf}
520 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
521 files=second_file_dict)
522 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
523 B.subject, A.data['id'])
524
525 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
526 self.waitUntilSettled()
527 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
528 self.waitUntilSettled()
529
530 self.assertEqual(A.reported, 1, "A should report")
531 self.assertHistory([
532 dict(name='project-test2', result='SUCCESS', changes='1,1'),
533 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
534 ])
535
James E. Blair97043882017-09-06 15:51:17 -0700536 def test_yaml_list_error(self):
537 in_repo_conf = textwrap.dedent(
538 """
539 job: foo
540 """)
541
542 file_dict = {'.zuul.yaml': in_repo_conf}
543 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
544 files=file_dict)
545 A.addApproval('Code-Review', 2)
546 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
547 self.waitUntilSettled()
548
549 self.assertEqual(A.data['status'], 'NEW')
550 self.assertEqual(A.reported, 1,
551 "A should report failure")
552 self.assertIn('not a list', A.messages[0],
553 "A should have a syntax error reported")
554
555 def test_yaml_dict_error(self):
556 in_repo_conf = textwrap.dedent(
557 """
558 - job
559 """)
560
561 file_dict = {'.zuul.yaml': in_repo_conf}
562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
563 files=file_dict)
564 A.addApproval('Code-Review', 2)
565 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
566 self.waitUntilSettled()
567
568 self.assertEqual(A.data['status'], 'NEW')
569 self.assertEqual(A.reported, 1,
570 "A should report failure")
571 self.assertIn('not a dictionary', A.messages[0],
572 "A should have a syntax error reported")
573
574 def test_yaml_key_error(self):
575 in_repo_conf = textwrap.dedent(
576 """
577 - job:
578 name: project-test2
579 """)
580
581 file_dict = {'.zuul.yaml': in_repo_conf}
582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
583 files=file_dict)
584 A.addApproval('Code-Review', 2)
585 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
586 self.waitUntilSettled()
587
588 self.assertEqual(A.data['status'], 'NEW')
589 self.assertEqual(A.reported, 1,
590 "A should report failure")
591 self.assertIn('has more than one key', A.messages[0],
592 "A should have a syntax error reported")
593
594 def test_yaml_unknown_error(self):
595 in_repo_conf = textwrap.dedent(
596 """
597 - foobar:
598 foo: bar
599 """)
600
601 file_dict = {'.zuul.yaml': in_repo_conf}
602 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
603 files=file_dict)
604 A.addApproval('Code-Review', 2)
605 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
606 self.waitUntilSettled()
607
608 self.assertEqual(A.data['status'], 'NEW')
609 self.assertEqual(A.reported, 1,
610 "A should report failure")
611 self.assertIn('not recognized', A.messages[0],
612 "A should have a syntax error reported")
613
James E. Blair149b69c2017-03-02 10:48:16 -0800614 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800615 in_repo_conf = textwrap.dedent(
616 """
617 - job:
618 name: project-test2
619 foo: error
620 """)
621
622 file_dict = {'.zuul.yaml': in_repo_conf}
623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
624 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200625 A.addApproval('Code-Review', 2)
626 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800627 self.waitUntilSettled()
628
629 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200630 self.assertEqual(A.reported, 1,
631 "A should report failure")
632 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800633 "A should have a syntax error reported")
634
James E. Blair149b69c2017-03-02 10:48:16 -0800635 def test_trusted_syntax_error(self):
636 in_repo_conf = textwrap.dedent(
637 """
638 - job:
639 name: project-test2
640 foo: error
641 """)
642
643 file_dict = {'zuul.yaml': in_repo_conf}
644 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
645 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200646 A.addApproval('Code-Review', 2)
647 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800648 self.waitUntilSettled()
649
650 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200651 self.assertEqual(A.reported, 1,
652 "A should report failure")
653 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800654 "A should have a syntax error reported")
655
James E. Blair6f140c72017-03-03 10:32:07 -0800656 def test_untrusted_yaml_error(self):
657 in_repo_conf = textwrap.dedent(
658 """
659 - job:
660 foo: error
661 """)
662
663 file_dict = {'.zuul.yaml': in_repo_conf}
664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
665 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200666 A.addApproval('Code-Review', 2)
667 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800668 self.waitUntilSettled()
669
670 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200671 self.assertEqual(A.reported, 1,
672 "A should report failure")
673 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800674 "A should have a syntax error reported")
675
James E. Blairdb04e6a2017-05-03 14:49:36 -0700676 def test_untrusted_shadow_error(self):
677 in_repo_conf = textwrap.dedent(
678 """
679 - job:
680 name: common-config-test
681 """)
682
683 file_dict = {'.zuul.yaml': in_repo_conf}
684 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
685 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200686 A.addApproval('Code-Review', 2)
687 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700688 self.waitUntilSettled()
689
690 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200691 self.assertEqual(A.reported, 1,
692 "A should report failure")
693 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700694 "A should have a syntax error reported")
695
James E. Blaird5656ad2017-06-02 14:29:41 -0700696 def test_untrusted_pipeline_error(self):
697 in_repo_conf = textwrap.dedent(
698 """
699 - pipeline:
700 name: test
701 """)
702
703 file_dict = {'.zuul.yaml': in_repo_conf}
704 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
705 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200706 A.addApproval('Code-Review', 2)
707 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700708 self.waitUntilSettled()
709
710 self.assertEqual(A.data['status'], 'NEW')
711 self.assertEqual(A.reported, 1,
712 "A should report failure")
713 self.assertIn('Pipelines may not be defined', A.messages[0],
714 "A should have a syntax error reported")
715
716 def test_untrusted_project_error(self):
717 in_repo_conf = textwrap.dedent(
718 """
719 - project:
720 name: org/project1
721 """)
722
723 file_dict = {'.zuul.yaml': in_repo_conf}
724 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
725 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200726 A.addApproval('Code-Review', 2)
727 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700728 self.waitUntilSettled()
729
730 self.assertEqual(A.data['status'], 'NEW')
731 self.assertEqual(A.reported, 1,
732 "A should report failure")
733 self.assertIn('the only project definition permitted', A.messages[0],
734 "A should have a syntax error reported")
735
James E. Blaire64b0e42017-06-08 11:23:34 -0700736 def test_duplicate_node_error(self):
737 in_repo_conf = textwrap.dedent(
738 """
739 - nodeset:
740 name: duplicate
741 nodes:
742 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700743 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700744 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700745 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700746 """)
747
748 file_dict = {'.zuul.yaml': in_repo_conf}
749 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
750 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200751 A.addApproval('Code-Review', 2)
752 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700753 self.waitUntilSettled()
754
755 self.assertEqual(A.data['status'], 'NEW')
756 self.assertEqual(A.reported, 1,
757 "A should report failure")
758 self.assertIn('appears multiple times', A.messages[0],
759 "A should have a syntax error reported")
760
761 def test_duplicate_group_error(self):
762 in_repo_conf = textwrap.dedent(
763 """
764 - nodeset:
765 name: duplicate
766 nodes:
767 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700768 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700769 groups:
770 - name: group
771 nodes: compute
772 - name: group
773 nodes: compute
774 """)
775
776 file_dict = {'.zuul.yaml': in_repo_conf}
777 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
778 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200779 A.addApproval('Code-Review', 2)
780 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700781 self.waitUntilSettled()
782
783 self.assertEqual(A.data['status'], 'NEW')
784 self.assertEqual(A.reported, 1,
785 "A should report failure")
786 self.assertIn('appears multiple times', A.messages[0],
787 "A should have a syntax error reported")
788
James E. Blair4ae399f2017-09-20 17:15:09 -0700789 def test_secret_not_found_error(self):
790 in_repo_conf = textwrap.dedent(
791 """
792 - job:
793 name: test
794 secrets: does-not-exist
795 """)
796
797 file_dict = {'.zuul.yaml': in_repo_conf}
798 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
799 files=file_dict)
800 A.addApproval('Code-Review', 2)
801 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
802 self.waitUntilSettled()
803
804 self.assertEqual(A.data['status'], 'NEW')
805 self.assertEqual(A.reported, 1,
806 "A should report failure")
807 self.assertIn('secret "does-not-exist" was not found', A.messages[0],
808 "A should have a syntax error reported")
809
810 def test_nodeset_not_found_error(self):
811 in_repo_conf = textwrap.dedent(
812 """
813 - job:
814 name: test
815 nodeset: does-not-exist
816 """)
817
818 file_dict = {'.zuul.yaml': in_repo_conf}
819 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
820 files=file_dict)
821 A.addApproval('Code-Review', 2)
822 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
823 self.waitUntilSettled()
824
825 self.assertEqual(A.data['status'], 'NEW')
826 self.assertEqual(A.reported, 1,
827 "A should report failure")
828 self.assertIn('nodeset "does-not-exist" was not found', A.messages[0],
829 "A should have a syntax error reported")
830
James E. Blair89e25eb2017-09-26 09:11:31 -0700831 def test_template_not_found_error(self):
832 in_repo_conf = textwrap.dedent(
833 """
834 - job:
835 name: project-test1
836 - project:
837 name: org/project
838 templates:
839 - does-not-exist
840 """)
841
842 file_dict = {'.zuul.yaml': in_repo_conf}
843 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
844 files=file_dict)
845 A.addApproval('Code-Review', 2)
846 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
847 self.waitUntilSettled()
848
849 self.assertEqual(A.data['status'], 'NEW')
850 self.assertEqual(A.reported, 1,
851 "A should report failure")
852 self.assertIn('project template "does-not-exist" was not found',
853 A.messages[0],
854 "A should have a syntax error reported")
855
Monty Taylor8be3c0c2017-10-06 10:37:37 -0500856 def test_job_list_in_project_template_not_dict_error(self):
857 in_repo_conf = textwrap.dedent(
858 """
859 - job:
860 name: project-test1
861 - project-template:
862 name: some-jobs
863 check:
864 jobs:
865 - project-test1:
866 - required-projects:
867 org/project2
868 """)
869
870 file_dict = {'.zuul.yaml': in_repo_conf}
871 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
872 files=file_dict)
873 A.addApproval('Code-Review', 2)
874 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
875 self.waitUntilSettled()
876
877 self.assertEqual(A.data['status'], 'NEW')
878 self.assertEqual(A.reported, 1,
879 "A should report failure")
880 self.assertIn('expected str for dictionary value',
881 A.messages[0], "A should have a syntax error reported")
882
883 def test_job_list_in_project_not_dict_error(self):
884 in_repo_conf = textwrap.dedent(
885 """
886 - job:
887 name: project-test1
888 - project:
889 name: org/project1
890 check:
891 jobs:
892 - project-test1:
893 - required-projects:
894 org/project2
895 """)
896
897 file_dict = {'.zuul.yaml': in_repo_conf}
898 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
899 files=file_dict)
900 A.addApproval('Code-Review', 2)
901 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
902 self.waitUntilSettled()
903
904 self.assertEqual(A.data['status'], 'NEW')
905 self.assertEqual(A.reported, 1,
906 "A should report failure")
907 self.assertIn('expected str for dictionary value',
908 A.messages[0], "A should have a syntax error reported")
909
James E. Blair1235f142017-10-07 09:11:43 -0700910 def test_project_template(self):
911 # Tests that a project template is not modified when used, and
912 # can therefore be used in subsequent reconfigurations.
913 in_repo_conf = textwrap.dedent(
914 """
915 - job:
916 name: project-test1
917 - project-template:
918 name: some-jobs
919 tenant-one-gate:
920 jobs:
921 - project-test1:
922 required-projects:
923 - org/project1
924 - project:
925 name: org/project
926 templates:
927 - some-jobs
928 """)
929
930 file_dict = {'.zuul.yaml': in_repo_conf}
931 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
932 files=file_dict)
933 A.addApproval('Code-Review', 2)
934 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
935 self.waitUntilSettled()
936 self.assertEqual(A.data['status'], 'MERGED')
937 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
938 self.waitUntilSettled()
939 in_repo_conf = textwrap.dedent(
940 """
941 - project:
942 name: org/project1
943 templates:
944 - some-jobs
945 """)
946 file_dict = {'.zuul.yaml': in_repo_conf}
947 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B',
948 files=file_dict)
949 B.addApproval('Code-Review', 2)
950 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
951 self.waitUntilSettled()
952 self.assertEqual(B.data['status'], 'MERGED')
953
James E. Blairbccdfcf2017-10-07 13:37:26 -0700954 def test_job_remove_add(self):
955 # Tests that a job can be removed from one repo and added in another.
956 # First, remove the current config for project1 since it
957 # references the job we want to remove.
958 file_dict = {'.zuul.yaml': None}
959 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
960 files=file_dict)
961 A.setMerged()
962 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
963 self.waitUntilSettled()
964 # Then propose a change to delete the job from one repo...
965 file_dict = {'.zuul.yaml': None}
966 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
967 files=file_dict)
968 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
969 self.waitUntilSettled()
970 # ...and a second that depends on it that adds it to another repo.
971 in_repo_conf = textwrap.dedent(
972 """
973 - job:
974 name: project-test1
975
976 - project:
977 name: org/project1
978 check:
979 jobs:
980 - project-test1
981 """)
982 in_repo_playbook = textwrap.dedent(
983 """
984 - hosts: all
985 tasks: []
986 """)
987 file_dict = {'.zuul.yaml': in_repo_conf,
988 'playbooks/project-test1.yaml': in_repo_playbook}
989 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C',
990 files=file_dict,
991 parent='refs/changes/1/1/1')
992 C.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
993 C.subject, B.data['id'])
994 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
995 self.waitUntilSettled()
996 self.assertHistory([
997 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
998 ], ordered=False)
999
James E. Blair09f9ffe2017-07-11 15:30:25 -07001000 def test_multi_repo(self):
1001 downstream_repo_conf = textwrap.dedent(
1002 """
1003 - project:
1004 name: org/project1
1005 tenant-one-gate:
1006 jobs:
1007 - project-test1
1008
1009 - job:
1010 name: project1-test1
1011 parent: project-test1
1012 """)
1013
1014 file_dict = {'.zuul.yaml': downstream_repo_conf}
1015 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1016 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001017 A.addApproval('Code-Review', 2)
1018 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -07001019 self.waitUntilSettled()
1020
1021 self.assertEqual(A.data['status'], 'MERGED')
1022 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
1023 self.waitUntilSettled()
1024
1025 upstream_repo_conf = textwrap.dedent(
1026 """
1027 - job:
1028 name: project-test1
1029
1030 - job:
1031 name: project-test2
1032
1033 - project:
1034 name: org/project
1035 tenant-one-gate:
1036 jobs:
1037 - project-test1
1038 """)
1039
1040 file_dict = {'.zuul.yaml': upstream_repo_conf}
1041 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
1042 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001043 B.addApproval('Code-Review', 2)
1044 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -07001045 self.waitUntilSettled()
1046
1047 self.assertEqual(B.data['status'], 'MERGED')
1048 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
1049 self.waitUntilSettled()
1050
1051 tenant = self.sched.abide.tenants.get('tenant-one')
1052 # Ensure the latest change is reflected in the config; if it
1053 # isn't this will raise an exception.
1054 tenant.layout.getJob('project-test2')
1055
James E. Blair332636e2017-09-05 10:14:35 -07001056 def test_pipeline_error(self):
1057 with open(os.path.join(FIXTURE_DIR,
1058 'config/in-repo/git/',
1059 'common-config/zuul.yaml')) as f:
1060 base_common_config = f.read()
1061
1062 in_repo_conf_A = textwrap.dedent(
1063 """
1064 - pipeline:
1065 name: periodic
1066 foo: error
1067 """)
1068
1069 file_dict = {'zuul.yaml': None,
1070 'zuul.d/main.yaml': base_common_config,
1071 'zuul.d/test1.yaml': in_repo_conf_A}
1072 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
1073 files=file_dict)
1074 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1075 self.waitUntilSettled()
1076 self.assertEqual(A.reported, 1,
1077 "A should report failure")
1078 self.assertIn('syntax error',
1079 A.messages[0],
1080 "A should have an error reported")
1081
1082 def test_change_series_error(self):
1083 with open(os.path.join(FIXTURE_DIR,
1084 'config/in-repo/git/',
1085 'common-config/zuul.yaml')) as f:
1086 base_common_config = f.read()
1087
1088 in_repo_conf_A = textwrap.dedent(
1089 """
1090 - pipeline:
1091 name: periodic
1092 foo: error
1093 """)
1094
1095 file_dict = {'zuul.yaml': None,
1096 'zuul.d/main.yaml': base_common_config,
1097 'zuul.d/test1.yaml': in_repo_conf_A}
1098 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
1099 files=file_dict)
1100
1101 in_repo_conf_B = textwrap.dedent(
1102 """
1103 - job:
1104 name: project-test2
1105 foo: error
1106 """)
1107
1108 file_dict = {'zuul.yaml': None,
1109 'zuul.d/main.yaml': base_common_config,
1110 'zuul.d/test1.yaml': in_repo_conf_A,
1111 'zuul.d/test2.yaml': in_repo_conf_B}
1112 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
1113 files=file_dict)
1114 B.setDependsOn(A, 1)
1115 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
1116 C.setDependsOn(B, 1)
1117 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1118 self.waitUntilSettled()
1119
1120 self.assertEqual(C.reported, 1,
1121 "C should report failure")
1122 self.assertIn('depends on a change that failed to merge',
1123 C.messages[0],
1124 "C should have an error reported")
1125
James E. Blairc73c73a2017-01-20 15:15:15 -08001126
James E. Blairc9455002017-09-06 09:22:19 -07001127class TestInRepoJoin(ZuulTestCase):
1128 # In this config, org/project is not a member of any pipelines, so
1129 # that we may test the changes that cause it to join them.
1130
1131 tenant_config_file = 'config/in-repo-join/main.yaml'
1132
1133 def test_dynamic_dependent_pipeline(self):
1134 # Test dynamically adding a project to a
1135 # dependent pipeline for the first time
1136 self.executor_server.hold_jobs_in_build = True
1137
1138 tenant = self.sched.abide.tenants.get('tenant-one')
1139 gate_pipeline = tenant.layout.pipelines['gate']
1140
1141 in_repo_conf = textwrap.dedent(
1142 """
1143 - job:
1144 name: project-test1
1145
1146 - job:
1147 name: project-test2
1148
1149 - project:
1150 name: org/project
1151 gate:
1152 jobs:
1153 - project-test2
1154 """)
1155
1156 in_repo_playbook = textwrap.dedent(
1157 """
1158 - hosts: all
1159 tasks: []
1160 """)
1161
1162 file_dict = {'.zuul.yaml': in_repo_conf,
1163 'playbooks/project-test2.yaml': in_repo_playbook}
1164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1165 files=file_dict)
1166 A.addApproval('Code-Review', 2)
1167 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1168 self.waitUntilSettled()
1169
1170 items = gate_pipeline.getAllItems()
1171 self.assertEqual(items[0].change.number, '1')
1172 self.assertEqual(items[0].change.patchset, '1')
1173 self.assertTrue(items[0].live)
1174
1175 self.executor_server.hold_jobs_in_build = False
1176 self.executor_server.release()
1177 self.waitUntilSettled()
1178
1179 # Make sure the dynamic queue got cleaned up
1180 self.assertEqual(gate_pipeline.queues, [])
1181
1182 def test_dynamic_dependent_pipeline_failure(self):
1183 # Test that a change behind a failing change adding a project
1184 # to a dependent pipeline is dequeued.
1185 self.executor_server.hold_jobs_in_build = True
1186
1187 in_repo_conf = textwrap.dedent(
1188 """
1189 - job:
1190 name: project-test1
1191
1192 - project:
1193 name: org/project
1194 gate:
1195 jobs:
1196 - project-test1
1197 """)
1198
1199 file_dict = {'.zuul.yaml': in_repo_conf}
1200 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1201 files=file_dict)
1202 self.executor_server.failJob('project-test1', A)
1203 A.addApproval('Code-Review', 2)
1204 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1205 self.waitUntilSettled()
1206
1207 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1208 B.addApproval('Code-Review', 2)
1209 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1210 self.waitUntilSettled()
1211
James E. Blair3490c5d2017-09-07 08:33:23 -07001212 self.orderedRelease()
James E. Blairc9455002017-09-06 09:22:19 -07001213 self.waitUntilSettled()
1214 self.assertEqual(A.reported, 2,
1215 "A should report start and failure")
1216 self.assertEqual(A.data['status'], 'NEW')
1217 self.assertEqual(B.reported, 1,
1218 "B should report start")
1219 self.assertHistory([
1220 dict(name='project-test1', result='FAILURE', changes='1,1'),
James E. Blair3490c5d2017-09-07 08:33:23 -07001221 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
James E. Blairc9455002017-09-06 09:22:19 -07001222 ], ordered=False)
1223
James E. Blair0af198f2017-09-06 09:52:35 -07001224 def test_dynamic_dependent_pipeline_absent(self):
1225 # Test that a series of dependent changes don't report merge
1226 # failures to a pipeline they aren't in.
1227 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1228 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1229 B.setDependsOn(A, 1)
1230
1231 A.addApproval('Code-Review', 2)
1232 A.addApproval('Approved', 1)
1233 B.addApproval('Code-Review', 2)
1234 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1235 self.waitUntilSettled()
1236 self.assertEqual(A.reported, 0,
1237 "A should not report")
1238 self.assertEqual(A.data['status'], 'NEW')
1239 self.assertEqual(B.reported, 0,
1240 "B should not report")
1241 self.assertEqual(B.data['status'], 'NEW')
1242 self.assertHistory([])
1243
James E. Blairc9455002017-09-06 09:22:19 -07001244
James E. Blairc73c73a2017-01-20 15:15:15 -08001245class TestAnsible(AnsibleZuulTestCase):
1246 # A temporary class to hold new tests while others are disabled
1247
1248 tenant_config_file = 'config/ansible/main.yaml'
1249
1250 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +11001251 # Keep the jobdir around so we can inspect contents if an
1252 # assert fails.
1253 self.executor_server.keep_jobdir = True
1254 # Output extra ansible info so we might see errors.
1255 self.executor_server.verbose = True
1256 # Add a site variables file, used by check-vars
1257 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
1258 'variables.yaml')
1259 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -08001260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1261 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1262 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +02001263 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +11001264 with self.jobLog(build_timeout):
1265 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001266 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +11001267 with self.jobLog(build_faillocal):
1268 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001269 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +11001270 with self.jobLog(build_failpost):
1271 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001272 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +11001273 with self.jobLog(build_check_vars):
1274 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -05001275 build_check_secret_names = self.getJobFromHistory('check-secret-names')
1276 with self.jobLog(build_check_secret_names):
1277 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001278 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +11001279 with self.jobLog(build_hello):
1280 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001281 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +11001282 with self.jobLog(build_python27):
1283 self.assertEqual(build_python27.result, 'SUCCESS')
1284 flag_path = os.path.join(self.test_root,
1285 build_python27.uuid + '.flag')
1286 self.assertTrue(os.path.exists(flag_path))
1287 copied_path = os.path.join(self.test_root, build_python27.uuid +
1288 '.copied')
1289 self.assertTrue(os.path.exists(copied_path))
1290 failed_path = os.path.join(self.test_root, build_python27.uuid +
1291 '.failed')
1292 self.assertFalse(os.path.exists(failed_path))
1293 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
1294 '.pre.flag')
1295 self.assertTrue(os.path.exists(pre_flag_path))
1296 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
1297 '.post.flag')
1298 self.assertTrue(os.path.exists(post_flag_path))
1299 bare_role_flag_path = os.path.join(self.test_root,
1300 build_python27.uuid +
1301 '.bare-role.flag')
1302 self.assertTrue(os.path.exists(bare_role_flag_path))
1303 secrets_path = os.path.join(self.test_root,
1304 build_python27.uuid + '.secrets')
1305 with open(secrets_path) as f:
1306 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -08001307
Jamie Lennox7655b552017-03-17 12:33:38 +11001308 msg = A.messages[0]
1309 success = "{} https://success.example.com/zuul-logs/{}"
1310 fail = "{} https://failure.example.com/zuul-logs/{}"
1311 self.assertIn(success.format("python27", build_python27.uuid), msg)
1312 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
1313 self.assertIn(success.format("check-vars",
1314 build_check_vars.uuid), msg)
1315 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
1316 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
1317 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +02001318
James E. Blairabbaa6f2017-04-06 16:11:44 -07001319 def _add_job(self, job_name):
1320 conf = textwrap.dedent(
1321 """
1322 - job:
1323 name: %s
1324
1325 - project:
1326 name: org/plugin-project
1327 check:
1328 jobs:
1329 - %s
1330 """ % (job_name, job_name))
1331
1332 file_dict = {'.zuul.yaml': conf}
1333 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1334 files=file_dict)
1335 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1336 self.waitUntilSettled()
1337
1338 def test_plugins(self):
1339 # Keep the jobdir around so we can inspect contents if an
1340 # assert fails.
1341 self.executor_server.keep_jobdir = True
1342 # Output extra ansible info so we might see errors.
1343 self.executor_server.verbose = True
1344
1345 count = 0
1346 plugin_tests = [
1347 ('passwd', 'FAILURE'),
1348 ('cartesian', 'SUCCESS'),
1349 ('consul_kv', 'FAILURE'),
1350 ('credstash', 'FAILURE'),
1351 ('csvfile_good', 'SUCCESS'),
1352 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001353 ('uri_bad_path', 'FAILURE'),
1354 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001355 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001356 ('file_local_good', 'SUCCESS'),
1357 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001358 ]
1359 for job_name, result in plugin_tests:
1360 count += 1
1361 self._add_job(job_name)
1362
1363 job = self.getJobFromHistory(job_name)
1364 with self.jobLog(job):
1365 self.assertEqual(count, len(self.history))
1366 build = self.history[-1]
1367 self.assertEqual(build.result, result)
1368
1369 # TODOv3(jeblair): parse the ansible output and verify we're
1370 # getting the exception we expect.
1371
James E. Blairb9c0d772017-03-03 14:34:49 -08001372
James E. Blaira4d4eef2017-06-30 14:49:17 -07001373class TestPrePlaybooks(AnsibleZuulTestCase):
1374 # A temporary class to hold new tests while others are disabled
1375
1376 tenant_config_file = 'config/pre-playbook/main.yaml'
1377
1378 def test_pre_playbook_fail(self):
1379 # Test that we run the post playbooks (but not the actual
1380 # playbook) when a pre-playbook fails.
1381 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1382 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1383 self.waitUntilSettled()
1384 build = self.getJobFromHistory('python27')
1385 self.assertIsNone(build.result)
1386 self.assertIn('RETRY_LIMIT', A.messages[0])
1387 flag_path = os.path.join(self.test_root, build.uuid +
1388 '.main.flag')
1389 self.assertFalse(os.path.exists(flag_path))
1390 pre_flag_path = os.path.join(self.test_root, build.uuid +
1391 '.pre.flag')
1392 self.assertFalse(os.path.exists(pre_flag_path))
1393 post_flag_path = os.path.join(self.test_root, build.uuid +
1394 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001395 self.assertTrue(os.path.exists(post_flag_path),
1396 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001397
1398
James E. Blairb9c0d772017-03-03 14:34:49 -08001399class TestBrokenConfig(ZuulTestCase):
1400 # Test that we get an appropriate syntax error if we start with a
1401 # broken config.
1402
1403 tenant_config_file = 'config/broken/main.yaml'
1404
1405 def setUp(self):
1406 with testtools.ExpectedException(
1407 zuul.configloader.ConfigurationSyntaxError,
1408 "\nZuul encountered a syntax error"):
1409 super(TestBrokenConfig, self).setUp()
1410
1411 def test_broken_config_on_startup(self):
1412 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001413
1414
1415class TestProjectKeys(ZuulTestCase):
1416 # Test that we can generate project keys
1417
1418 # Normally the test infrastructure copies a static key in place
1419 # for each project before starting tests. This saves time because
1420 # Zuul's automatic key-generation on startup can be slow. To make
1421 # sure we exercise that code, in this test we allow Zuul to create
1422 # keys for the project on startup.
1423 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001424 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001425 tenant_config_file = 'config/in-repo/main.yaml'
1426
1427 def test_key_generation(self):
1428 key_root = os.path.join(self.state_root, 'keys')
1429 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1430 # Make sure that a proper key was created on startup
1431 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001432 private_key, public_key = \
1433 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001434
1435 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1436 fixture_private_key = i.read()
1437
1438 # Make sure that we didn't just end up with the static fixture
1439 # key
1440 self.assertNotEqual(fixture_private_key, private_key)
1441
1442 # Make sure it's the right length
1443 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001444
1445
James E. Blairbb94dfa2017-07-11 07:45:19 -07001446class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001447 def _assertRolePath(self, build, playbook, content):
1448 path = os.path.join(self.test_root, build.uuid,
1449 'ansible', playbook, 'ansible.cfg')
1450 roles_paths = []
1451 with open(path) as f:
1452 for line in f:
1453 if line.startswith('roles_path'):
1454 roles_paths.append(line)
1455 print(roles_paths)
1456 if content:
1457 self.assertEqual(len(roles_paths), 1,
1458 "Should have one roles_path line in %s" %
1459 (playbook,))
1460 self.assertIn(content, roles_paths[0])
1461 else:
1462 self.assertEqual(len(roles_paths), 0,
1463 "Should have no roles_path line in %s" %
1464 (playbook,))
1465
James E. Blairbb94dfa2017-07-11 07:45:19 -07001466
1467class TestRoles(RoleTestCase):
1468 tenant_config_file = 'config/roles/main.yaml'
1469
James E. Blairbce76932017-05-04 10:03:15 -07001470 def test_role(self):
1471 # This exercises a proposed change to a role being checked out
1472 # and used.
1473 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1474 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1475 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1476 B.subject, A.data['id'])
1477 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1478 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1479 self.waitUntilSettled()
1480 self.assertHistory([
1481 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1482 ])
James E. Blair6459db12017-06-29 14:57:20 -07001483
James E. Blair1b27f6a2017-07-14 14:09:07 -07001484 def test_role_inheritance(self):
1485 self.executor_server.hold_jobs_in_build = True
1486 conf = textwrap.dedent(
1487 """
1488 - job:
1489 name: parent
1490 roles:
1491 - zuul: bare-role
1492 pre-run: playbooks/parent-pre
1493 post-run: playbooks/parent-post
1494
1495 - job:
1496 name: project-test
1497 parent: parent
1498 roles:
1499 - zuul: org/project
1500
1501 - project:
1502 name: org/project
1503 check:
1504 jobs:
1505 - project-test
1506 """)
1507
1508 file_dict = {'.zuul.yaml': conf}
1509 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1510 files=file_dict)
1511 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1512 self.waitUntilSettled()
1513
1514 self.assertEqual(len(self.builds), 1)
1515 build = self.getBuildByName('project-test')
1516 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1517 self._assertRolePath(build, 'playbook_0', 'role_0')
1518 self._assertRolePath(build, 'playbook_0', 'role_1')
1519 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1520
1521 self.executor_server.hold_jobs_in_build = False
1522 self.executor_server.release()
1523 self.waitUntilSettled()
1524
1525 self.assertHistory([
1526 dict(name='project-test', result='SUCCESS', changes='1,1'),
1527 ])
1528
James E. Blair6f699732017-07-18 14:19:11 -07001529 def test_role_error(self):
1530 conf = textwrap.dedent(
1531 """
1532 - job:
1533 name: project-test
1534 roles:
1535 - zuul: common-config
1536
1537 - project:
1538 name: org/project
1539 check:
1540 jobs:
1541 - project-test
1542 """)
1543
1544 file_dict = {'.zuul.yaml': conf}
1545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1546 files=file_dict)
1547 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1548 self.waitUntilSettled()
1549 self.assertIn(
1550 '- project-test project-test : ERROR Unable to find role',
1551 A.messages[-1])
1552
James E. Blair6459db12017-06-29 14:57:20 -07001553
James E. Blairbb94dfa2017-07-11 07:45:19 -07001554class TestImplicitRoles(RoleTestCase):
1555 tenant_config_file = 'config/implicit-roles/main.yaml'
1556
1557 def test_missing_roles(self):
1558 # Test implicit and explicit roles for a project which does
1559 # not have roles. The implicit role should be silently
1560 # ignored since the project doesn't supply roles, but if a
1561 # user declares an explicit role, it should error.
1562 self.executor_server.hold_jobs_in_build = True
1563 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1564 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1565 self.waitUntilSettled()
1566
1567 self.assertEqual(len(self.builds), 2)
1568 build = self.getBuildByName('implicit-role-fail')
1569 self._assertRolePath(build, 'playbook_0', None)
1570
1571 self.executor_server.hold_jobs_in_build = False
1572 self.executor_server.release()
1573 self.waitUntilSettled()
1574 # The retry_limit doesn't get recorded
1575 self.assertHistory([
1576 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1577 ])
1578
1579 def test_roles(self):
1580 # Test implicit and explicit roles for a project which does
1581 # have roles. In both cases, we should end up with the role
1582 # in the path. In the explicit case, ensure we end up with
1583 # the name we specified.
1584 self.executor_server.hold_jobs_in_build = True
1585 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1586 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1587 self.waitUntilSettled()
1588
1589 self.assertEqual(len(self.builds), 2)
1590 build = self.getBuildByName('implicit-role-ok')
1591 self._assertRolePath(build, 'playbook_0', 'role_0')
1592
1593 build = self.getBuildByName('explicit-role-ok')
1594 self._assertRolePath(build, 'playbook_0', 'role_0')
1595
1596 self.executor_server.hold_jobs_in_build = False
1597 self.executor_server.release()
1598 self.waitUntilSettled()
1599 self.assertHistory([
1600 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1601 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1602 ], ordered=False)
1603
1604
James E. Blair6459db12017-06-29 14:57:20 -07001605class TestShadow(ZuulTestCase):
1606 tenant_config_file = 'config/shadow/main.yaml'
1607
1608 def test_shadow(self):
1609 # Test that a repo is allowed to shadow another's job definitions.
1610 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1611 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1612 self.waitUntilSettled()
1613 self.assertHistory([
1614 dict(name='test1', result='SUCCESS', changes='1,1'),
1615 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001616 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001617
1618
1619class TestDataReturn(AnsibleZuulTestCase):
1620 tenant_config_file = 'config/data-return/main.yaml'
1621
1622 def test_data_return(self):
1623 # This exercises a proposed change to a role being checked out
1624 # and used.
1625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1626 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1627 self.waitUntilSettled()
1628 self.assertHistory([
1629 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001630 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1631 ], ordered=False)
1632 self.assertIn('- data-return http://example.com/test/log/url/',
1633 A.messages[-1])
1634 self.assertIn('- data-return-relative '
1635 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001636 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001637
1638
1639class TestDiskAccounting(AnsibleZuulTestCase):
1640 config_file = 'zuul-disk-accounting.conf'
1641 tenant_config_file = 'config/disk-accountant/main.yaml'
1642
1643 def test_disk_accountant_kills_job(self):
1644 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1645 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1646 self.waitUntilSettled()
1647 self.assertHistory([
1648 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001649
1650
1651class TestMaxNodesPerJob(AnsibleZuulTestCase):
1652 tenant_config_file = 'config/multi-tenant/main.yaml'
1653
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001654 def test_max_timeout_exceeded(self):
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001655 in_repo_conf = textwrap.dedent(
1656 """
1657 - job:
1658 name: test-job
James E. Blair7e3e6882017-09-20 15:47:13 -07001659 nodeset:
1660 nodes:
1661 - name: node01
1662 label: fake
1663 - name: node02
1664 label: fake
1665 - name: node03
1666 label: fake
1667 - name: node04
1668 label: fake
1669 - name: node05
1670 label: fake
1671 - name: node06
1672 label: fake
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001673 """)
1674 file_dict = {'.zuul.yaml': in_repo_conf}
1675 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1676 files=file_dict)
1677 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1678 self.waitUntilSettled()
1679 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1680 A.messages[0], "A should fail because of nodes limit")
1681
1682 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1683 files=file_dict)
1684 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1685 self.waitUntilSettled()
1686 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1687 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001688
1689
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001690class TestMaxTimeout(AnsibleZuulTestCase):
1691 tenant_config_file = 'config/multi-tenant/main.yaml'
1692
1693 def test_max_nodes_reached(self):
1694 in_repo_conf = textwrap.dedent(
1695 """
1696 - job:
1697 name: test-job
1698 timeout: 3600
1699 """)
1700 file_dict = {'.zuul.yaml': in_repo_conf}
1701 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1702 files=file_dict)
1703 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1704 self.waitUntilSettled()
1705 self.assertIn('The job "test-job" exceeds tenant max-job-timeout',
1706 A.messages[0], "A should fail because of timeout limit")
1707
1708 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1709 files=file_dict)
1710 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1711 self.waitUntilSettled()
1712 self.assertNotIn("exceeds tenant max-job-timeout", B.messages[0],
1713 "B should not fail because of timeout limit")
1714
1715
James E. Blair2bab6e72017-08-07 09:52:45 -07001716class TestBaseJobs(ZuulTestCase):
1717 tenant_config_file = 'config/base-jobs/main.yaml'
1718
1719 def test_multiple_base_jobs(self):
1720 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1721 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1722 self.waitUntilSettled()
1723 self.assertHistory([
1724 dict(name='my-job', result='SUCCESS', changes='1,1'),
1725 dict(name='other-job', result='SUCCESS', changes='1,1'),
1726 ], ordered=False)
1727 self.assertEqual(self.getJobFromHistory('my-job').
1728 parameters['zuul']['jobtags'],
1729 ['mybase'])
1730 self.assertEqual(self.getJobFromHistory('other-job').
1731 parameters['zuul']['jobtags'],
1732 ['otherbase'])
1733
1734 def test_untrusted_base_job(self):
1735 """Test that a base job may not be defined in an untrusted repo"""
1736 in_repo_conf = textwrap.dedent(
1737 """
1738 - job:
1739 name: fail-base
1740 parent: null
1741 """)
1742
1743 file_dict = {'.zuul.yaml': in_repo_conf}
1744 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1745 files=file_dict)
1746 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1747 self.waitUntilSettled()
1748 self.assertEqual(A.reported, 1,
1749 "A should report failure")
1750 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1751 self.assertIn('Base jobs must be defined in config projects',
1752 A.messages[0])
1753 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001754
1755
1756class TestSecretLeaks(AnsibleZuulTestCase):
1757 tenant_config_file = 'config/secret-leaks/main.yaml'
1758
1759 def searchForContent(self, path, content):
1760 matches = []
1761 for (dirpath, dirnames, filenames) in os.walk(path):
1762 for filename in filenames:
1763 filepath = os.path.join(dirpath, filename)
1764 with open(filepath, 'rb') as f:
1765 if content in f.read():
1766 matches.append(filepath[len(path):])
1767 return matches
1768
1769 def _test_secret_file(self):
1770 # Or rather -- test that they *don't* leak.
1771 # Keep the jobdir around so we can inspect contents.
1772 self.executor_server.keep_jobdir = True
1773 conf = textwrap.dedent(
1774 """
1775 - project:
1776 name: org/project
1777 check:
1778 jobs:
1779 - secret-file
1780 """)
1781
1782 file_dict = {'.zuul.yaml': conf}
1783 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1784 files=file_dict)
1785 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1786 self.waitUntilSettled()
1787 self.assertHistory([
1788 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1789 ], ordered=False)
1790 matches = self.searchForContent(self.history[0].jobdir.root,
1791 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001792 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001793 set(matches))
1794
1795 def test_secret_file(self):
1796 self._test_secret_file()
1797
1798 def test_secret_file_verbose(self):
1799 # Output extra ansible info to exercise alternate logging code
1800 # paths.
1801 self.executor_server.verbose = True
1802 self._test_secret_file()
1803
1804 def _test_secret_file_fail(self):
1805 # Or rather -- test that they *don't* leak.
1806 # Keep the jobdir around so we can inspect contents.
1807 self.executor_server.keep_jobdir = True
1808 conf = textwrap.dedent(
1809 """
1810 - project:
1811 name: org/project
1812 check:
1813 jobs:
1814 - secret-file-fail
1815 """)
1816
1817 file_dict = {'.zuul.yaml': conf}
1818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1819 files=file_dict)
1820 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1821 self.waitUntilSettled()
1822 self.assertHistory([
1823 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1824 ], ordered=False)
1825 matches = self.searchForContent(self.history[0].jobdir.root,
1826 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001827 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001828 set(matches))
1829
1830 def test_secret_file_fail(self):
1831 self._test_secret_file_fail()
1832
1833 def test_secret_file_fail_verbose(self):
1834 # Output extra ansible info to exercise alternate logging code
1835 # paths.
1836 self.executor_server.verbose = True
1837 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001838
1839
1840class TestJobOutput(AnsibleZuulTestCase):
1841 tenant_config_file = 'config/job-output/main.yaml'
1842
1843 def _get_file(self, build, path):
1844 p = os.path.join(build.jobdir.root, path)
1845 with open(p) as f:
1846 return f.read()
1847
1848 def test_job_output(self):
Monty Taylor0e2489a2017-10-10 11:57:29 -05001849 # Verify that command standard output appears in the job output,
1850 # and that failures in the final playbook get logged.
James E. Blaira00910c2017-08-23 09:15:04 -07001851
1852 # This currently only verifies we receive output from
1853 # localhost. Notably, it does not verify we receive output
1854 # via zuul_console streaming.
1855 self.executor_server.keep_jobdir = True
1856 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1857 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1858 self.waitUntilSettled()
1859 self.assertHistory([
1860 dict(name='job-output', result='SUCCESS', changes='1,1'),
1861 ], ordered=False)
1862
1863 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1864 j = json.loads(self._get_file(self.history[0],
1865 'work/logs/job-output.json'))
1866 self.assertEqual(token,
1867 j[0]['plays'][0]['tasks'][0]
1868 ['hosts']['localhost']['stdout'])
1869
1870 print(self._get_file(self.history[0],
1871 'work/logs/job-output.txt'))
1872 self.assertIn(token,
1873 self._get_file(self.history[0],
1874 'work/logs/job-output.txt'))
Monty Taylor0e2489a2017-10-10 11:57:29 -05001875
1876 def test_job_output_failure_log(self):
1877 logger = logging.getLogger('zuul.AnsibleJob')
1878 output = io.StringIO()
1879 logger.addHandler(logging.StreamHandler(output))
1880
1881 # Verify that a failure in the last post playbook emits the contents
1882 # of the json output to the log
1883 self.executor_server.keep_jobdir = True
1884 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
1885 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1886 self.waitUntilSettled()
1887 self.assertHistory([
1888 dict(name='job-output-failure',
1889 result='POST_FAILURE', changes='1,1'),
1890 ], ordered=False)
1891
1892 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1893 j = json.loads(self._get_file(self.history[0],
1894 'work/logs/job-output.json'))
1895 self.assertEqual(token,
1896 j[0]['plays'][0]['tasks'][0]
1897 ['hosts']['localhost']['stdout'])
1898
1899 print(self._get_file(self.history[0],
1900 'work/logs/job-output.json'))
1901 self.assertIn(token,
1902 self._get_file(self.history[0],
1903 'work/logs/job-output.txt'))
1904
1905 log_output = output.getvalue()
1906 self.assertIn('Final playbook failed', log_output)
1907 self.assertIn('Failure test', log_output)