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