blob: 8eba62327a795d3cb4fd300e8f94b23e1c7545fc [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. Blair59fdbac2015-12-07 17:08:06 -080020
James E. Blairb9c0d772017-03-03 14:34:49 -080021import testtools
22
23import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070024from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000025from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080026
James E. Blair59fdbac2015-12-07 17:08:06 -080027
James E. Blair3f876d52016-07-22 13:07:14 -070028class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080029 # A temporary class to hold new tests while others are disabled
30
James E. Blair2a629ec2015-12-22 15:32:02 -080031 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080032
James E. Blair83005782015-12-11 14:46:03 -080033 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080034 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020035 A.addApproval('Code-Review', 2)
36 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080037 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080038 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080039 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080040 self.assertEqual(self.getJobFromHistory('python27').result,
41 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080042 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080043 self.assertEqual(A.reported, 2,
44 "A should report start and success")
45 self.assertIn('tenant-one-gate', A.messages[1],
46 "A should transit tenant-one gate")
47 self.assertNotIn('tenant-two-gate', A.messages[1],
48 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080049
James E. Blair96f26942015-12-09 10:15:59 -080050 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020051 B.addApproval('Code-Review', 2)
52 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080053 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080054 self.assertEqual(self.getJobFromHistory('python27',
55 'org/project2').result,
56 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080057 self.assertEqual(self.getJobFromHistory('project2-test1').result,
58 'SUCCESS')
59 self.assertEqual(B.data['status'], 'MERGED')
60 self.assertEqual(B.reported, 2,
61 "B should report start and success")
62 self.assertIn('tenant-two-gate', B.messages[1],
63 "B should transit tenant-two gate")
64 self.assertNotIn('tenant-one-gate', B.messages[1],
65 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080066
James E. Blair96f26942015-12-09 10:15:59 -080067 self.assertEqual(A.reported, 2, "Activity in tenant two should"
68 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080069
James E. Blair83005782015-12-11 14:46:03 -080070
Tobias Henkel83167622017-06-30 19:45:03 +020071class TestFinal(ZuulTestCase):
72
73 tenant_config_file = 'config/final/main.yaml'
74
75 def test_final_variant_ok(self):
76 # test clean usage of final parent job
77 in_repo_conf = textwrap.dedent(
78 """
79 - project:
80 name: org/project
81 check:
82 jobs:
83 - job-final
84 """)
85
86 file_dict = {'.zuul.yaml': in_repo_conf}
87 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
88 files=file_dict)
89 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
90 self.waitUntilSettled()
91
92 self.assertEqual(A.reported, 1)
93 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
94
95 def test_final_variant_error(self):
96 # test misuse of final parent job
97 in_repo_conf = textwrap.dedent(
98 """
99 - project:
100 name: org/project
101 check:
102 jobs:
103 - job-final:
104 vars:
105 dont_override_this: bar
106 """)
107 file_dict = {'.zuul.yaml': in_repo_conf}
108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
109 files=file_dict)
110 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
111 self.waitUntilSettled()
112
113 # The second patch tried to override some variables.
114 # Thus it should fail.
115 self.assertEqual(A.reported, 1)
116 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
117 self.assertIn('Unable to modify final job', A.messages[0])
118
119 def test_final_inheritance(self):
120 # test misuse of final parent job
121 in_repo_conf = textwrap.dedent(
122 """
123 - job:
124 name: project-test
125 parent: job-final
126
127 - project:
128 name: org/project
129 check:
130 jobs:
131 - project-test
132 """)
133
134 in_repo_playbook = textwrap.dedent(
135 """
136 - hosts: all
137 tasks: []
138 """)
139
140 file_dict = {'.zuul.yaml': in_repo_conf,
141 'playbooks/project-test.yaml': in_repo_playbook}
142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
143 files=file_dict)
144 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
145 self.waitUntilSettled()
146
147 # The second patch tried to override some variables.
148 # Thus it should fail.
149 self.assertEqual(A.reported, 1)
150 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
151 self.assertIn('Unable to inherit from final job', A.messages[0])
152
153
James E. Blairff555742017-02-19 11:34:27 -0800154class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800155 # A temporary class to hold new tests while others are disabled
156
Tobias Henkelabf973e2017-07-28 10:07:34 +0200157 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800158 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800159
James E. Blair83005782015-12-11 14:46:03 -0800160 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800161 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200162 A.addApproval('Code-Review', 2)
163 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800164 self.waitUntilSettled()
165 self.assertEqual(self.getJobFromHistory('project-test1').result,
166 'SUCCESS')
167 self.assertEqual(A.data['status'], 'MERGED')
168 self.assertEqual(A.reported, 2,
169 "A should report start and success")
170 self.assertIn('tenant-one-gate', A.messages[1],
171 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800172
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700173 def test_dynamic_config(self):
174 in_repo_conf = textwrap.dedent(
175 """
176 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200177 name: project-test1
178
179 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700180 name: project-test2
181
182 - project:
183 name: org/project
184 tenant-one-gate:
185 jobs:
186 - project-test2
187 """)
188
James E. Blairc73c73a2017-01-20 15:15:15 -0800189 in_repo_playbook = textwrap.dedent(
190 """
191 - hosts: all
192 tasks: []
193 """)
194
195 file_dict = {'.zuul.yaml': in_repo_conf,
196 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800198 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200199 A.addApproval('Code-Review', 2)
200 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700201 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700202 self.assertEqual(A.data['status'], 'MERGED')
203 self.assertEqual(A.reported, 2,
204 "A should report start and success")
205 self.assertIn('tenant-one-gate', A.messages[1],
206 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800207 self.assertHistory([
208 dict(name='project-test2', result='SUCCESS', changes='1,1')])
209
James E. Blairc2a5ed72017-02-20 14:12:01 -0500210 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800211 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500212
James E. Blair646322f2017-01-27 15:50:34 -0800213 # Now that the config change is landed, it should be live for
214 # subsequent changes.
215 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200216 B.addApproval('Code-Review', 2)
217 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800218 self.waitUntilSettled()
219 self.assertEqual(self.getJobFromHistory('project-test2').result,
220 'SUCCESS')
221 self.assertHistory([
222 dict(name='project-test2', result='SUCCESS', changes='1,1'),
223 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800224
Tobias Henkelf02cf512017-07-21 22:55:34 +0200225 def test_dynamic_config_non_existing_job(self):
226 """Test that requesting a non existent job fails"""
227 in_repo_conf = textwrap.dedent(
228 """
229 - job:
230 name: project-test1
231
232 - project:
233 name: org/project
234 check:
235 jobs:
236 - non-existent-job
237 """)
238
239 in_repo_playbook = textwrap.dedent(
240 """
241 - hosts: all
242 tasks: []
243 """)
244
245 file_dict = {'.zuul.yaml': in_repo_conf,
246 'playbooks/project-test2.yaml': in_repo_playbook}
247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
248 files=file_dict)
249 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
250 self.waitUntilSettled()
251 self.assertEqual(A.reported, 1,
252 "A should report failure")
253 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
254 self.assertIn('Job non-existent-job not defined', A.messages[0],
255 "A should have failed the check pipeline")
256 self.assertHistory([])
257
258 def test_dynamic_config_non_existing_job_in_template(self):
259 """Test that requesting a non existent job fails"""
260 in_repo_conf = textwrap.dedent(
261 """
262 - job:
263 name: project-test1
264
265 - project-template:
266 name: test-template
267 check:
268 jobs:
269 - non-existent-job
270
271 - project:
272 name: org/project
273 templates:
274 - test-template
275 """)
276
277 in_repo_playbook = textwrap.dedent(
278 """
279 - hosts: all
280 tasks: []
281 """)
282
283 file_dict = {'.zuul.yaml': in_repo_conf,
284 'playbooks/project-test2.yaml': in_repo_playbook}
285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
286 files=file_dict)
287 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
288 self.waitUntilSettled()
289 self.assertEqual(A.reported, 1,
290 "A should report failure")
291 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
292 self.assertIn('Job non-existent-job not defined', A.messages[0],
293 "A should have failed the check pipeline")
294 self.assertHistory([])
295
Tobias Henkel0f714002017-06-30 23:30:52 +0200296 def test_dynamic_config_new_patchset(self):
297 self.executor_server.hold_jobs_in_build = True
298
299 tenant = self.sched.abide.tenants.get('tenant-one')
300 check_pipeline = tenant.layout.pipelines['check']
301
302 in_repo_conf = textwrap.dedent(
303 """
304 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200305 name: project-test1
306
307 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200308 name: project-test2
309
310 - project:
311 name: org/project
312 check:
313 jobs:
314 - project-test2
315 """)
316
317 in_repo_playbook = textwrap.dedent(
318 """
319 - hosts: all
320 tasks: []
321 """)
322
323 file_dict = {'.zuul.yaml': in_repo_conf,
324 'playbooks/project-test2.yaml': in_repo_playbook}
325 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
326 files=file_dict)
327 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
328 self.waitUntilSettled()
329
330 items = check_pipeline.getAllItems()
331 self.assertEqual(items[0].change.number, '1')
332 self.assertEqual(items[0].change.patchset, '1')
333 self.assertTrue(items[0].live)
334
335 in_repo_conf = textwrap.dedent(
336 """
337 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200338 name: project-test1
339
340 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200341 name: project-test2
342
343 - project:
344 name: org/project
345 check:
346 jobs:
347 - project-test1
348 - project-test2
349 """)
350 file_dict = {'.zuul.yaml': in_repo_conf,
351 'playbooks/project-test2.yaml': in_repo_playbook}
352
353 A.addPatchset(files=file_dict)
354 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
355
356 self.waitUntilSettled()
357
358 items = check_pipeline.getAllItems()
359 self.assertEqual(items[0].change.number, '1')
360 self.assertEqual(items[0].change.patchset, '2')
361 self.assertTrue(items[0].live)
362
363 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200364 self.executor_server.release('project-test1')
365 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200366 self.executor_server.release()
367 self.waitUntilSettled()
368
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200369 self.assertHistory([
370 dict(name='project-test2', result='ABORTED', changes='1,1'),
371 dict(name='project-test1', result='SUCCESS', changes='1,2'),
372 dict(name='project-test2', result='SUCCESS', changes='1,2')])
373
James E. Blairff555742017-02-19 11:34:27 -0800374 def test_in_repo_branch(self):
375 in_repo_conf = textwrap.dedent(
376 """
377 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200378 name: project-test1
379
380 - job:
James E. Blairff555742017-02-19 11:34:27 -0800381 name: project-test2
382
383 - project:
384 name: org/project
385 tenant-one-gate:
386 jobs:
387 - project-test2
388 """)
389
390 in_repo_playbook = textwrap.dedent(
391 """
392 - hosts: all
393 tasks: []
394 """)
395
396 file_dict = {'.zuul.yaml': in_repo_conf,
397 'playbooks/project-test2.yaml': in_repo_playbook}
398 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700399 self.fake_gerrit.addEvent(
400 self.fake_gerrit.getFakeBranchCreatedEvent(
401 'org/project', 'stable'))
James E. Blairff555742017-02-19 11:34:27 -0800402 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
403 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200404 A.addApproval('Code-Review', 2)
405 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800406 self.waitUntilSettled()
407 self.assertEqual(A.data['status'], 'MERGED')
408 self.assertEqual(A.reported, 2,
409 "A should report start and success")
410 self.assertIn('tenant-one-gate', A.messages[1],
411 "A should transit tenant-one gate")
412 self.assertHistory([
413 dict(name='project-test2', result='SUCCESS', changes='1,1')])
414 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800415 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800416
417 # The config change should not affect master.
418 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200419 B.addApproval('Code-Review', 2)
420 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800421 self.waitUntilSettled()
422 self.assertHistory([
423 dict(name='project-test2', result='SUCCESS', changes='1,1'),
424 dict(name='project-test1', result='SUCCESS', changes='2,1')])
425
426 # The config change should be live for further changes on
427 # stable.
428 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200429 C.addApproval('Code-Review', 2)
430 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800431 self.waitUntilSettled()
432 self.assertHistory([
433 dict(name='project-test2', result='SUCCESS', changes='1,1'),
434 dict(name='project-test1', result='SUCCESS', changes='2,1'),
435 dict(name='project-test2', result='SUCCESS', changes='3,1')])
436
James E. Blaira5a12492017-05-03 11:40:48 -0700437 def test_crd_dynamic_config_branch(self):
438 # Test that we can create a job in one repo and be able to use
439 # it from a different branch on a different repo.
440
441 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700442 self.fake_gerrit.addEvent(
443 self.fake_gerrit.getFakeBranchCreatedEvent(
444 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700445
446 in_repo_conf = textwrap.dedent(
447 """
448 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200449 name: project-test1
450
451 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700452 name: project-test2
453
454 - project:
455 name: org/project
456 check:
457 jobs:
458 - project-test2
459 """)
460
461 in_repo_playbook = textwrap.dedent(
462 """
463 - hosts: all
464 tasks: []
465 """)
466
467 file_dict = {'.zuul.yaml': in_repo_conf,
468 'playbooks/project-test2.yaml': in_repo_playbook}
469 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
470 files=file_dict)
471
472 second_repo_conf = textwrap.dedent(
473 """
474 - project:
475 name: org/project1
476 check:
477 jobs:
478 - project-test2
479 """)
480
481 second_file_dict = {'.zuul.yaml': second_repo_conf}
482 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
483 files=second_file_dict)
484 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
485 B.subject, A.data['id'])
486
487 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
488 self.waitUntilSettled()
489 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
490 self.waitUntilSettled()
491
492 self.assertEqual(A.reported, 1, "A should report")
493 self.assertHistory([
494 dict(name='project-test2', result='SUCCESS', changes='1,1'),
495 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
496 ])
497
James E. Blair149b69c2017-03-02 10:48:16 -0800498 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800499 in_repo_conf = textwrap.dedent(
500 """
501 - job:
502 name: project-test2
503 foo: error
504 """)
505
506 file_dict = {'.zuul.yaml': in_repo_conf}
507 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
508 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200509 A.addApproval('Code-Review', 2)
510 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800511 self.waitUntilSettled()
512
513 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200514 self.assertEqual(A.reported, 1,
515 "A should report failure")
516 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800517 "A should have a syntax error reported")
518
James E. Blair149b69c2017-03-02 10:48:16 -0800519 def test_trusted_syntax_error(self):
520 in_repo_conf = textwrap.dedent(
521 """
522 - job:
523 name: project-test2
524 foo: error
525 """)
526
527 file_dict = {'zuul.yaml': in_repo_conf}
528 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
529 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200530 A.addApproval('Code-Review', 2)
531 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800532 self.waitUntilSettled()
533
534 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200535 self.assertEqual(A.reported, 1,
536 "A should report failure")
537 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800538 "A should have a syntax error reported")
539
James E. Blair6f140c72017-03-03 10:32:07 -0800540 def test_untrusted_yaml_error(self):
541 in_repo_conf = textwrap.dedent(
542 """
543 - job:
544 foo: error
545 """)
546
547 file_dict = {'.zuul.yaml': in_repo_conf}
548 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
549 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200550 A.addApproval('Code-Review', 2)
551 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800552 self.waitUntilSettled()
553
554 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200555 self.assertEqual(A.reported, 1,
556 "A should report failure")
557 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800558 "A should have a syntax error reported")
559
James E. Blairdb04e6a2017-05-03 14:49:36 -0700560 def test_untrusted_shadow_error(self):
561 in_repo_conf = textwrap.dedent(
562 """
563 - job:
564 name: common-config-test
565 """)
566
567 file_dict = {'.zuul.yaml': in_repo_conf}
568 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
569 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200570 A.addApproval('Code-Review', 2)
571 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700572 self.waitUntilSettled()
573
574 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200575 self.assertEqual(A.reported, 1,
576 "A should report failure")
577 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700578 "A should have a syntax error reported")
579
James E. Blaird5656ad2017-06-02 14:29:41 -0700580 def test_untrusted_pipeline_error(self):
581 in_repo_conf = textwrap.dedent(
582 """
583 - pipeline:
584 name: test
585 """)
586
587 file_dict = {'.zuul.yaml': in_repo_conf}
588 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
589 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200590 A.addApproval('Code-Review', 2)
591 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700592 self.waitUntilSettled()
593
594 self.assertEqual(A.data['status'], 'NEW')
595 self.assertEqual(A.reported, 1,
596 "A should report failure")
597 self.assertIn('Pipelines may not be defined', A.messages[0],
598 "A should have a syntax error reported")
599
600 def test_untrusted_project_error(self):
601 in_repo_conf = textwrap.dedent(
602 """
603 - project:
604 name: org/project1
605 """)
606
607 file_dict = {'.zuul.yaml': in_repo_conf}
608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
609 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200610 A.addApproval('Code-Review', 2)
611 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700612 self.waitUntilSettled()
613
614 self.assertEqual(A.data['status'], 'NEW')
615 self.assertEqual(A.reported, 1,
616 "A should report failure")
617 self.assertIn('the only project definition permitted', A.messages[0],
618 "A should have a syntax error reported")
619
James E. Blaire64b0e42017-06-08 11:23:34 -0700620 def test_duplicate_node_error(self):
621 in_repo_conf = textwrap.dedent(
622 """
623 - nodeset:
624 name: duplicate
625 nodes:
626 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700627 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700628 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700629 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700630 """)
631
632 file_dict = {'.zuul.yaml': in_repo_conf}
633 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
634 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200635 A.addApproval('Code-Review', 2)
636 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700637 self.waitUntilSettled()
638
639 self.assertEqual(A.data['status'], 'NEW')
640 self.assertEqual(A.reported, 1,
641 "A should report failure")
642 self.assertIn('appears multiple times', A.messages[0],
643 "A should have a syntax error reported")
644
645 def test_duplicate_group_error(self):
646 in_repo_conf = textwrap.dedent(
647 """
648 - nodeset:
649 name: duplicate
650 nodes:
651 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700652 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700653 groups:
654 - name: group
655 nodes: compute
656 - name: group
657 nodes: compute
658 """)
659
660 file_dict = {'.zuul.yaml': in_repo_conf}
661 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
662 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200663 A.addApproval('Code-Review', 2)
664 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700665 self.waitUntilSettled()
666
667 self.assertEqual(A.data['status'], 'NEW')
668 self.assertEqual(A.reported, 1,
669 "A should report failure")
670 self.assertIn('appears multiple times', A.messages[0],
671 "A should have a syntax error reported")
672
James E. Blair09f9ffe2017-07-11 15:30:25 -0700673 def test_multi_repo(self):
674 downstream_repo_conf = textwrap.dedent(
675 """
676 - project:
677 name: org/project1
678 tenant-one-gate:
679 jobs:
680 - project-test1
681
682 - job:
683 name: project1-test1
684 parent: project-test1
685 """)
686
687 file_dict = {'.zuul.yaml': downstream_repo_conf}
688 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
689 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200690 A.addApproval('Code-Review', 2)
691 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700692 self.waitUntilSettled()
693
694 self.assertEqual(A.data['status'], 'MERGED')
695 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
696 self.waitUntilSettled()
697
698 upstream_repo_conf = textwrap.dedent(
699 """
700 - job:
701 name: project-test1
702
703 - job:
704 name: project-test2
705
706 - project:
707 name: org/project
708 tenant-one-gate:
709 jobs:
710 - project-test1
711 """)
712
713 file_dict = {'.zuul.yaml': upstream_repo_conf}
714 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
715 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200716 B.addApproval('Code-Review', 2)
717 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700718 self.waitUntilSettled()
719
720 self.assertEqual(B.data['status'], 'MERGED')
721 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
722 self.waitUntilSettled()
723
724 tenant = self.sched.abide.tenants.get('tenant-one')
725 # Ensure the latest change is reflected in the config; if it
726 # isn't this will raise an exception.
727 tenant.layout.getJob('project-test2')
728
James E. Blair332636e2017-09-05 10:14:35 -0700729 def test_pipeline_error(self):
730 with open(os.path.join(FIXTURE_DIR,
731 'config/in-repo/git/',
732 'common-config/zuul.yaml')) as f:
733 base_common_config = f.read()
734
735 in_repo_conf_A = textwrap.dedent(
736 """
737 - pipeline:
738 name: periodic
739 foo: error
740 """)
741
742 file_dict = {'zuul.yaml': None,
743 'zuul.d/main.yaml': base_common_config,
744 'zuul.d/test1.yaml': in_repo_conf_A}
745 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
746 files=file_dict)
747 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
748 self.waitUntilSettled()
749 self.assertEqual(A.reported, 1,
750 "A should report failure")
751 self.assertIn('syntax error',
752 A.messages[0],
753 "A should have an error reported")
754
755 def test_change_series_error(self):
756 with open(os.path.join(FIXTURE_DIR,
757 'config/in-repo/git/',
758 'common-config/zuul.yaml')) as f:
759 base_common_config = f.read()
760
761 in_repo_conf_A = textwrap.dedent(
762 """
763 - pipeline:
764 name: periodic
765 foo: error
766 """)
767
768 file_dict = {'zuul.yaml': None,
769 'zuul.d/main.yaml': base_common_config,
770 'zuul.d/test1.yaml': in_repo_conf_A}
771 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
772 files=file_dict)
773
774 in_repo_conf_B = textwrap.dedent(
775 """
776 - job:
777 name: project-test2
778 foo: error
779 """)
780
781 file_dict = {'zuul.yaml': None,
782 'zuul.d/main.yaml': base_common_config,
783 'zuul.d/test1.yaml': in_repo_conf_A,
784 'zuul.d/test2.yaml': in_repo_conf_B}
785 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
786 files=file_dict)
787 B.setDependsOn(A, 1)
788 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
789 C.setDependsOn(B, 1)
790 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
791 self.waitUntilSettled()
792
793 self.assertEqual(C.reported, 1,
794 "C should report failure")
795 self.assertIn('depends on a change that failed to merge',
796 C.messages[0],
797 "C should have an error reported")
798
James E. Blairc73c73a2017-01-20 15:15:15 -0800799
James E. Blairc9455002017-09-06 09:22:19 -0700800class TestInRepoJoin(ZuulTestCase):
801 # In this config, org/project is not a member of any pipelines, so
802 # that we may test the changes that cause it to join them.
803
804 tenant_config_file = 'config/in-repo-join/main.yaml'
805
806 def test_dynamic_dependent_pipeline(self):
807 # Test dynamically adding a project to a
808 # dependent pipeline for the first time
809 self.executor_server.hold_jobs_in_build = True
810
811 tenant = self.sched.abide.tenants.get('tenant-one')
812 gate_pipeline = tenant.layout.pipelines['gate']
813
814 in_repo_conf = textwrap.dedent(
815 """
816 - job:
817 name: project-test1
818
819 - job:
820 name: project-test2
821
822 - project:
823 name: org/project
824 gate:
825 jobs:
826 - project-test2
827 """)
828
829 in_repo_playbook = textwrap.dedent(
830 """
831 - hosts: all
832 tasks: []
833 """)
834
835 file_dict = {'.zuul.yaml': in_repo_conf,
836 'playbooks/project-test2.yaml': in_repo_playbook}
837 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
838 files=file_dict)
839 A.addApproval('Code-Review', 2)
840 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
841 self.waitUntilSettled()
842
843 items = gate_pipeline.getAllItems()
844 self.assertEqual(items[0].change.number, '1')
845 self.assertEqual(items[0].change.patchset, '1')
846 self.assertTrue(items[0].live)
847
848 self.executor_server.hold_jobs_in_build = False
849 self.executor_server.release()
850 self.waitUntilSettled()
851
852 # Make sure the dynamic queue got cleaned up
853 self.assertEqual(gate_pipeline.queues, [])
854
855 def test_dynamic_dependent_pipeline_failure(self):
856 # Test that a change behind a failing change adding a project
857 # to a dependent pipeline is dequeued.
858 self.executor_server.hold_jobs_in_build = True
859
860 in_repo_conf = textwrap.dedent(
861 """
862 - job:
863 name: project-test1
864
865 - project:
866 name: org/project
867 gate:
868 jobs:
869 - project-test1
870 """)
871
872 file_dict = {'.zuul.yaml': in_repo_conf}
873 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
874 files=file_dict)
875 self.executor_server.failJob('project-test1', A)
876 A.addApproval('Code-Review', 2)
877 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
878 self.waitUntilSettled()
879
880 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
881 B.addApproval('Code-Review', 2)
882 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
883 self.waitUntilSettled()
884
885 self.executor_server.hold_jobs_in_build = False
886 self.executor_server.release()
887 self.waitUntilSettled()
888 self.assertEqual(A.reported, 2,
889 "A should report start and failure")
890 self.assertEqual(A.data['status'], 'NEW')
891 self.assertEqual(B.reported, 1,
892 "B should report start")
893 self.assertHistory([
894 dict(name='project-test1', result='FAILURE', changes='1,1'),
895 dict(name='project-test1', result='FAILURE', changes='1,1 2,1'),
896 ], ordered=False)
897
James E. Blair0af198f2017-09-06 09:52:35 -0700898 def test_dynamic_dependent_pipeline_absent(self):
899 # Test that a series of dependent changes don't report merge
900 # failures to a pipeline they aren't in.
901 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
902 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
903 B.setDependsOn(A, 1)
904
905 A.addApproval('Code-Review', 2)
906 A.addApproval('Approved', 1)
907 B.addApproval('Code-Review', 2)
908 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
909 self.waitUntilSettled()
910 self.assertEqual(A.reported, 0,
911 "A should not report")
912 self.assertEqual(A.data['status'], 'NEW')
913 self.assertEqual(B.reported, 0,
914 "B should not report")
915 self.assertEqual(B.data['status'], 'NEW')
916 self.assertHistory([])
917
James E. Blairc9455002017-09-06 09:22:19 -0700918
James E. Blairc73c73a2017-01-20 15:15:15 -0800919class TestAnsible(AnsibleZuulTestCase):
920 # A temporary class to hold new tests while others are disabled
921
922 tenant_config_file = 'config/ansible/main.yaml'
923
924 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +1100925 # Keep the jobdir around so we can inspect contents if an
926 # assert fails.
927 self.executor_server.keep_jobdir = True
928 # Output extra ansible info so we might see errors.
929 self.executor_server.verbose = True
930 # Add a site variables file, used by check-vars
931 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
932 'variables.yaml')
933 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -0800934 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
935 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
936 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +0200937 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +1100938 with self.jobLog(build_timeout):
939 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200940 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +1100941 with self.jobLog(build_faillocal):
942 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200943 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +1100944 with self.jobLog(build_failpost):
945 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200946 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +1100947 with self.jobLog(build_check_vars):
948 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -0500949 build_check_secret_names = self.getJobFromHistory('check-secret-names')
950 with self.jobLog(build_check_secret_names):
951 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200952 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +1100953 with self.jobLog(build_hello):
954 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200955 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +1100956 with self.jobLog(build_python27):
957 self.assertEqual(build_python27.result, 'SUCCESS')
958 flag_path = os.path.join(self.test_root,
959 build_python27.uuid + '.flag')
960 self.assertTrue(os.path.exists(flag_path))
961 copied_path = os.path.join(self.test_root, build_python27.uuid +
962 '.copied')
963 self.assertTrue(os.path.exists(copied_path))
964 failed_path = os.path.join(self.test_root, build_python27.uuid +
965 '.failed')
966 self.assertFalse(os.path.exists(failed_path))
967 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
968 '.pre.flag')
969 self.assertTrue(os.path.exists(pre_flag_path))
970 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
971 '.post.flag')
972 self.assertTrue(os.path.exists(post_flag_path))
973 bare_role_flag_path = os.path.join(self.test_root,
974 build_python27.uuid +
975 '.bare-role.flag')
976 self.assertTrue(os.path.exists(bare_role_flag_path))
977 secrets_path = os.path.join(self.test_root,
978 build_python27.uuid + '.secrets')
979 with open(secrets_path) as f:
980 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -0800981
Jamie Lennox7655b552017-03-17 12:33:38 +1100982 msg = A.messages[0]
983 success = "{} https://success.example.com/zuul-logs/{}"
984 fail = "{} https://failure.example.com/zuul-logs/{}"
985 self.assertIn(success.format("python27", build_python27.uuid), msg)
986 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
987 self.assertIn(success.format("check-vars",
988 build_check_vars.uuid), msg)
989 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
990 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
991 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +0200992
James E. Blairabbaa6f2017-04-06 16:11:44 -0700993 def _add_job(self, job_name):
994 conf = textwrap.dedent(
995 """
996 - job:
997 name: %s
998
999 - project:
1000 name: org/plugin-project
1001 check:
1002 jobs:
1003 - %s
1004 """ % (job_name, job_name))
1005
1006 file_dict = {'.zuul.yaml': conf}
1007 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1008 files=file_dict)
1009 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1010 self.waitUntilSettled()
1011
1012 def test_plugins(self):
1013 # Keep the jobdir around so we can inspect contents if an
1014 # assert fails.
1015 self.executor_server.keep_jobdir = True
1016 # Output extra ansible info so we might see errors.
1017 self.executor_server.verbose = True
1018
1019 count = 0
1020 plugin_tests = [
1021 ('passwd', 'FAILURE'),
1022 ('cartesian', 'SUCCESS'),
1023 ('consul_kv', 'FAILURE'),
1024 ('credstash', 'FAILURE'),
1025 ('csvfile_good', 'SUCCESS'),
1026 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001027 ('uri_bad_path', 'FAILURE'),
1028 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001029 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001030 ('file_local_good', 'SUCCESS'),
1031 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001032 ]
1033 for job_name, result in plugin_tests:
1034 count += 1
1035 self._add_job(job_name)
1036
1037 job = self.getJobFromHistory(job_name)
1038 with self.jobLog(job):
1039 self.assertEqual(count, len(self.history))
1040 build = self.history[-1]
1041 self.assertEqual(build.result, result)
1042
1043 # TODOv3(jeblair): parse the ansible output and verify we're
1044 # getting the exception we expect.
1045
James E. Blairb9c0d772017-03-03 14:34:49 -08001046
James E. Blaira4d4eef2017-06-30 14:49:17 -07001047class TestPrePlaybooks(AnsibleZuulTestCase):
1048 # A temporary class to hold new tests while others are disabled
1049
1050 tenant_config_file = 'config/pre-playbook/main.yaml'
1051
1052 def test_pre_playbook_fail(self):
1053 # Test that we run the post playbooks (but not the actual
1054 # playbook) when a pre-playbook fails.
1055 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1056 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1057 self.waitUntilSettled()
1058 build = self.getJobFromHistory('python27')
1059 self.assertIsNone(build.result)
1060 self.assertIn('RETRY_LIMIT', A.messages[0])
1061 flag_path = os.path.join(self.test_root, build.uuid +
1062 '.main.flag')
1063 self.assertFalse(os.path.exists(flag_path))
1064 pre_flag_path = os.path.join(self.test_root, build.uuid +
1065 '.pre.flag')
1066 self.assertFalse(os.path.exists(pre_flag_path))
1067 post_flag_path = os.path.join(self.test_root, build.uuid +
1068 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001069 self.assertTrue(os.path.exists(post_flag_path),
1070 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001071
1072
James E. Blairb9c0d772017-03-03 14:34:49 -08001073class TestBrokenConfig(ZuulTestCase):
1074 # Test that we get an appropriate syntax error if we start with a
1075 # broken config.
1076
1077 tenant_config_file = 'config/broken/main.yaml'
1078
1079 def setUp(self):
1080 with testtools.ExpectedException(
1081 zuul.configloader.ConfigurationSyntaxError,
1082 "\nZuul encountered a syntax error"):
1083 super(TestBrokenConfig, self).setUp()
1084
1085 def test_broken_config_on_startup(self):
1086 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001087
1088
1089class TestProjectKeys(ZuulTestCase):
1090 # Test that we can generate project keys
1091
1092 # Normally the test infrastructure copies a static key in place
1093 # for each project before starting tests. This saves time because
1094 # Zuul's automatic key-generation on startup can be slow. To make
1095 # sure we exercise that code, in this test we allow Zuul to create
1096 # keys for the project on startup.
1097 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001098 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001099 tenant_config_file = 'config/in-repo/main.yaml'
1100
1101 def test_key_generation(self):
1102 key_root = os.path.join(self.state_root, 'keys')
1103 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1104 # Make sure that a proper key was created on startup
1105 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001106 private_key, public_key = \
1107 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001108
1109 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1110 fixture_private_key = i.read()
1111
1112 # Make sure that we didn't just end up with the static fixture
1113 # key
1114 self.assertNotEqual(fixture_private_key, private_key)
1115
1116 # Make sure it's the right length
1117 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001118
1119
James E. Blairbb94dfa2017-07-11 07:45:19 -07001120class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001121 def _assertRolePath(self, build, playbook, content):
1122 path = os.path.join(self.test_root, build.uuid,
1123 'ansible', playbook, 'ansible.cfg')
1124 roles_paths = []
1125 with open(path) as f:
1126 for line in f:
1127 if line.startswith('roles_path'):
1128 roles_paths.append(line)
1129 print(roles_paths)
1130 if content:
1131 self.assertEqual(len(roles_paths), 1,
1132 "Should have one roles_path line in %s" %
1133 (playbook,))
1134 self.assertIn(content, roles_paths[0])
1135 else:
1136 self.assertEqual(len(roles_paths), 0,
1137 "Should have no roles_path line in %s" %
1138 (playbook,))
1139
James E. Blairbb94dfa2017-07-11 07:45:19 -07001140
1141class TestRoles(RoleTestCase):
1142 tenant_config_file = 'config/roles/main.yaml'
1143
James E. Blairbce76932017-05-04 10:03:15 -07001144 def test_role(self):
1145 # This exercises a proposed change to a role being checked out
1146 # and used.
1147 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1148 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1149 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1150 B.subject, A.data['id'])
1151 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1152 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1153 self.waitUntilSettled()
1154 self.assertHistory([
1155 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1156 ])
James E. Blair6459db12017-06-29 14:57:20 -07001157
James E. Blair1b27f6a2017-07-14 14:09:07 -07001158 def test_role_inheritance(self):
1159 self.executor_server.hold_jobs_in_build = True
1160 conf = textwrap.dedent(
1161 """
1162 - job:
1163 name: parent
1164 roles:
1165 - zuul: bare-role
1166 pre-run: playbooks/parent-pre
1167 post-run: playbooks/parent-post
1168
1169 - job:
1170 name: project-test
1171 parent: parent
1172 roles:
1173 - zuul: org/project
1174
1175 - project:
1176 name: org/project
1177 check:
1178 jobs:
1179 - project-test
1180 """)
1181
1182 file_dict = {'.zuul.yaml': conf}
1183 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1184 files=file_dict)
1185 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1186 self.waitUntilSettled()
1187
1188 self.assertEqual(len(self.builds), 1)
1189 build = self.getBuildByName('project-test')
1190 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1191 self._assertRolePath(build, 'playbook_0', 'role_0')
1192 self._assertRolePath(build, 'playbook_0', 'role_1')
1193 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1194
1195 self.executor_server.hold_jobs_in_build = False
1196 self.executor_server.release()
1197 self.waitUntilSettled()
1198
1199 self.assertHistory([
1200 dict(name='project-test', result='SUCCESS', changes='1,1'),
1201 ])
1202
James E. Blair6f699732017-07-18 14:19:11 -07001203 def test_role_error(self):
1204 conf = textwrap.dedent(
1205 """
1206 - job:
1207 name: project-test
1208 roles:
1209 - zuul: common-config
1210
1211 - project:
1212 name: org/project
1213 check:
1214 jobs:
1215 - project-test
1216 """)
1217
1218 file_dict = {'.zuul.yaml': conf}
1219 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1220 files=file_dict)
1221 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1222 self.waitUntilSettled()
1223 self.assertIn(
1224 '- project-test project-test : ERROR Unable to find role',
1225 A.messages[-1])
1226
James E. Blair6459db12017-06-29 14:57:20 -07001227
James E. Blairbb94dfa2017-07-11 07:45:19 -07001228class TestImplicitRoles(RoleTestCase):
1229 tenant_config_file = 'config/implicit-roles/main.yaml'
1230
1231 def test_missing_roles(self):
1232 # Test implicit and explicit roles for a project which does
1233 # not have roles. The implicit role should be silently
1234 # ignored since the project doesn't supply roles, but if a
1235 # user declares an explicit role, it should error.
1236 self.executor_server.hold_jobs_in_build = True
1237 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1238 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1239 self.waitUntilSettled()
1240
1241 self.assertEqual(len(self.builds), 2)
1242 build = self.getBuildByName('implicit-role-fail')
1243 self._assertRolePath(build, 'playbook_0', None)
1244
1245 self.executor_server.hold_jobs_in_build = False
1246 self.executor_server.release()
1247 self.waitUntilSettled()
1248 # The retry_limit doesn't get recorded
1249 self.assertHistory([
1250 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1251 ])
1252
1253 def test_roles(self):
1254 # Test implicit and explicit roles for a project which does
1255 # have roles. In both cases, we should end up with the role
1256 # in the path. In the explicit case, ensure we end up with
1257 # the name we specified.
1258 self.executor_server.hold_jobs_in_build = True
1259 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1260 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1261 self.waitUntilSettled()
1262
1263 self.assertEqual(len(self.builds), 2)
1264 build = self.getBuildByName('implicit-role-ok')
1265 self._assertRolePath(build, 'playbook_0', 'role_0')
1266
1267 build = self.getBuildByName('explicit-role-ok')
1268 self._assertRolePath(build, 'playbook_0', 'role_0')
1269
1270 self.executor_server.hold_jobs_in_build = False
1271 self.executor_server.release()
1272 self.waitUntilSettled()
1273 self.assertHistory([
1274 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1275 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1276 ], ordered=False)
1277
1278
James E. Blair6459db12017-06-29 14:57:20 -07001279class TestShadow(ZuulTestCase):
1280 tenant_config_file = 'config/shadow/main.yaml'
1281
1282 def test_shadow(self):
1283 # Test that a repo is allowed to shadow another's job definitions.
1284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1285 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1286 self.waitUntilSettled()
1287 self.assertHistory([
1288 dict(name='test1', result='SUCCESS', changes='1,1'),
1289 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001290 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001291
1292
1293class TestDataReturn(AnsibleZuulTestCase):
1294 tenant_config_file = 'config/data-return/main.yaml'
1295
1296 def test_data_return(self):
1297 # This exercises a proposed change to a role being checked out
1298 # and used.
1299 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1300 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1301 self.waitUntilSettled()
1302 self.assertHistory([
1303 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001304 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1305 ], ordered=False)
1306 self.assertIn('- data-return http://example.com/test/log/url/',
1307 A.messages[-1])
1308 self.assertIn('- data-return-relative '
1309 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001310 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001311
1312
1313class TestDiskAccounting(AnsibleZuulTestCase):
1314 config_file = 'zuul-disk-accounting.conf'
1315 tenant_config_file = 'config/disk-accountant/main.yaml'
1316
1317 def test_disk_accountant_kills_job(self):
1318 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1319 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1320 self.waitUntilSettled()
1321 self.assertHistory([
1322 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001323
1324
1325class TestMaxNodesPerJob(AnsibleZuulTestCase):
1326 tenant_config_file = 'config/multi-tenant/main.yaml'
1327
1328 def test_max_nodes_reached(self):
1329 in_repo_conf = textwrap.dedent(
1330 """
1331 - job:
1332 name: test-job
1333 nodes:
1334 - name: node01
1335 label: fake
1336 - name: node02
1337 label: fake
1338 - name: node03
1339 label: fake
1340 - name: node04
1341 label: fake
1342 - name: node05
1343 label: fake
1344 - name: node06
1345 label: fake
1346 """)
1347 file_dict = {'.zuul.yaml': in_repo_conf}
1348 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1349 files=file_dict)
1350 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1351 self.waitUntilSettled()
1352 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1353 A.messages[0], "A should fail because of nodes limit")
1354
1355 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1356 files=file_dict)
1357 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1358 self.waitUntilSettled()
1359 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1360 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001361
1362
1363class TestBaseJobs(ZuulTestCase):
1364 tenant_config_file = 'config/base-jobs/main.yaml'
1365
1366 def test_multiple_base_jobs(self):
1367 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1368 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1369 self.waitUntilSettled()
1370 self.assertHistory([
1371 dict(name='my-job', result='SUCCESS', changes='1,1'),
1372 dict(name='other-job', result='SUCCESS', changes='1,1'),
1373 ], ordered=False)
1374 self.assertEqual(self.getJobFromHistory('my-job').
1375 parameters['zuul']['jobtags'],
1376 ['mybase'])
1377 self.assertEqual(self.getJobFromHistory('other-job').
1378 parameters['zuul']['jobtags'],
1379 ['otherbase'])
1380
1381 def test_untrusted_base_job(self):
1382 """Test that a base job may not be defined in an untrusted repo"""
1383 in_repo_conf = textwrap.dedent(
1384 """
1385 - job:
1386 name: fail-base
1387 parent: null
1388 """)
1389
1390 file_dict = {'.zuul.yaml': in_repo_conf}
1391 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1392 files=file_dict)
1393 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1394 self.waitUntilSettled()
1395 self.assertEqual(A.reported, 1,
1396 "A should report failure")
1397 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1398 self.assertIn('Base jobs must be defined in config projects',
1399 A.messages[0])
1400 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001401
1402
1403class TestSecretLeaks(AnsibleZuulTestCase):
1404 tenant_config_file = 'config/secret-leaks/main.yaml'
1405
1406 def searchForContent(self, path, content):
1407 matches = []
1408 for (dirpath, dirnames, filenames) in os.walk(path):
1409 for filename in filenames:
1410 filepath = os.path.join(dirpath, filename)
1411 with open(filepath, 'rb') as f:
1412 if content in f.read():
1413 matches.append(filepath[len(path):])
1414 return matches
1415
1416 def _test_secret_file(self):
1417 # Or rather -- test that they *don't* leak.
1418 # Keep the jobdir around so we can inspect contents.
1419 self.executor_server.keep_jobdir = True
1420 conf = textwrap.dedent(
1421 """
1422 - project:
1423 name: org/project
1424 check:
1425 jobs:
1426 - secret-file
1427 """)
1428
1429 file_dict = {'.zuul.yaml': conf}
1430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1431 files=file_dict)
1432 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1433 self.waitUntilSettled()
1434 self.assertHistory([
1435 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1436 ], ordered=False)
1437 matches = self.searchForContent(self.history[0].jobdir.root,
1438 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001439 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001440 set(matches))
1441
1442 def test_secret_file(self):
1443 self._test_secret_file()
1444
1445 def test_secret_file_verbose(self):
1446 # Output extra ansible info to exercise alternate logging code
1447 # paths.
1448 self.executor_server.verbose = True
1449 self._test_secret_file()
1450
1451 def _test_secret_file_fail(self):
1452 # Or rather -- test that they *don't* leak.
1453 # Keep the jobdir around so we can inspect contents.
1454 self.executor_server.keep_jobdir = True
1455 conf = textwrap.dedent(
1456 """
1457 - project:
1458 name: org/project
1459 check:
1460 jobs:
1461 - secret-file-fail
1462 """)
1463
1464 file_dict = {'.zuul.yaml': conf}
1465 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1466 files=file_dict)
1467 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1468 self.waitUntilSettled()
1469 self.assertHistory([
1470 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1471 ], ordered=False)
1472 matches = self.searchForContent(self.history[0].jobdir.root,
1473 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001474 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001475 set(matches))
1476
1477 def test_secret_file_fail(self):
1478 self._test_secret_file_fail()
1479
1480 def test_secret_file_fail_verbose(self):
1481 # Output extra ansible info to exercise alternate logging code
1482 # paths.
1483 self.executor_server.verbose = True
1484 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001485
1486
1487class TestJobOutput(AnsibleZuulTestCase):
1488 tenant_config_file = 'config/job-output/main.yaml'
1489
1490 def _get_file(self, build, path):
1491 p = os.path.join(build.jobdir.root, path)
1492 with open(p) as f:
1493 return f.read()
1494
1495 def test_job_output(self):
1496 # Verify that command standard output appears in the job output
1497
1498 # This currently only verifies we receive output from
1499 # localhost. Notably, it does not verify we receive output
1500 # via zuul_console streaming.
1501 self.executor_server.keep_jobdir = True
1502 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1503 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1504 self.waitUntilSettled()
1505 self.assertHistory([
1506 dict(name='job-output', result='SUCCESS', changes='1,1'),
1507 ], ordered=False)
1508
1509 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1510 j = json.loads(self._get_file(self.history[0],
1511 'work/logs/job-output.json'))
1512 self.assertEqual(token,
1513 j[0]['plays'][0]['tasks'][0]
1514 ['hosts']['localhost']['stdout'])
1515
1516 print(self._get_file(self.history[0],
1517 'work/logs/job-output.txt'))
1518 self.assertIn(token,
1519 self._get_file(self.history[0],
1520 'work/logs/job-output.txt'))