blob: b55dd7fe41b406875f38201122ecd412f68ea08e [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
898
James E. Blairc73c73a2017-01-20 15:15:15 -0800899class TestAnsible(AnsibleZuulTestCase):
900 # A temporary class to hold new tests while others are disabled
901
902 tenant_config_file = 'config/ansible/main.yaml'
903
904 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +1100905 # Keep the jobdir around so we can inspect contents if an
906 # assert fails.
907 self.executor_server.keep_jobdir = True
908 # Output extra ansible info so we might see errors.
909 self.executor_server.verbose = True
910 # Add a site variables file, used by check-vars
911 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
912 'variables.yaml')
913 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -0800914 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
915 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
916 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +0200917 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +1100918 with self.jobLog(build_timeout):
919 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200920 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +1100921 with self.jobLog(build_faillocal):
922 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200923 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +1100924 with self.jobLog(build_failpost):
925 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200926 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +1100927 with self.jobLog(build_check_vars):
928 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -0500929 build_check_secret_names = self.getJobFromHistory('check-secret-names')
930 with self.jobLog(build_check_secret_names):
931 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200932 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +1100933 with self.jobLog(build_hello):
934 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200935 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +1100936 with self.jobLog(build_python27):
937 self.assertEqual(build_python27.result, 'SUCCESS')
938 flag_path = os.path.join(self.test_root,
939 build_python27.uuid + '.flag')
940 self.assertTrue(os.path.exists(flag_path))
941 copied_path = os.path.join(self.test_root, build_python27.uuid +
942 '.copied')
943 self.assertTrue(os.path.exists(copied_path))
944 failed_path = os.path.join(self.test_root, build_python27.uuid +
945 '.failed')
946 self.assertFalse(os.path.exists(failed_path))
947 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
948 '.pre.flag')
949 self.assertTrue(os.path.exists(pre_flag_path))
950 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
951 '.post.flag')
952 self.assertTrue(os.path.exists(post_flag_path))
953 bare_role_flag_path = os.path.join(self.test_root,
954 build_python27.uuid +
955 '.bare-role.flag')
956 self.assertTrue(os.path.exists(bare_role_flag_path))
957 secrets_path = os.path.join(self.test_root,
958 build_python27.uuid + '.secrets')
959 with open(secrets_path) as f:
960 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -0800961
Jamie Lennox7655b552017-03-17 12:33:38 +1100962 msg = A.messages[0]
963 success = "{} https://success.example.com/zuul-logs/{}"
964 fail = "{} https://failure.example.com/zuul-logs/{}"
965 self.assertIn(success.format("python27", build_python27.uuid), msg)
966 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
967 self.assertIn(success.format("check-vars",
968 build_check_vars.uuid), msg)
969 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
970 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
971 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +0200972
James E. Blairabbaa6f2017-04-06 16:11:44 -0700973 def _add_job(self, job_name):
974 conf = textwrap.dedent(
975 """
976 - job:
977 name: %s
978
979 - project:
980 name: org/plugin-project
981 check:
982 jobs:
983 - %s
984 """ % (job_name, job_name))
985
986 file_dict = {'.zuul.yaml': conf}
987 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
988 files=file_dict)
989 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
990 self.waitUntilSettled()
991
992 def test_plugins(self):
993 # Keep the jobdir around so we can inspect contents if an
994 # assert fails.
995 self.executor_server.keep_jobdir = True
996 # Output extra ansible info so we might see errors.
997 self.executor_server.verbose = True
998
999 count = 0
1000 plugin_tests = [
1001 ('passwd', 'FAILURE'),
1002 ('cartesian', 'SUCCESS'),
1003 ('consul_kv', 'FAILURE'),
1004 ('credstash', 'FAILURE'),
1005 ('csvfile_good', 'SUCCESS'),
1006 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001007 ('uri_bad_path', 'FAILURE'),
1008 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001009 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001010 ('file_local_good', 'SUCCESS'),
1011 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001012 ]
1013 for job_name, result in plugin_tests:
1014 count += 1
1015 self._add_job(job_name)
1016
1017 job = self.getJobFromHistory(job_name)
1018 with self.jobLog(job):
1019 self.assertEqual(count, len(self.history))
1020 build = self.history[-1]
1021 self.assertEqual(build.result, result)
1022
1023 # TODOv3(jeblair): parse the ansible output and verify we're
1024 # getting the exception we expect.
1025
James E. Blairb9c0d772017-03-03 14:34:49 -08001026
James E. Blaira4d4eef2017-06-30 14:49:17 -07001027class TestPrePlaybooks(AnsibleZuulTestCase):
1028 # A temporary class to hold new tests while others are disabled
1029
1030 tenant_config_file = 'config/pre-playbook/main.yaml'
1031
1032 def test_pre_playbook_fail(self):
1033 # Test that we run the post playbooks (but not the actual
1034 # playbook) when a pre-playbook fails.
1035 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1036 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1037 self.waitUntilSettled()
1038 build = self.getJobFromHistory('python27')
1039 self.assertIsNone(build.result)
1040 self.assertIn('RETRY_LIMIT', A.messages[0])
1041 flag_path = os.path.join(self.test_root, build.uuid +
1042 '.main.flag')
1043 self.assertFalse(os.path.exists(flag_path))
1044 pre_flag_path = os.path.join(self.test_root, build.uuid +
1045 '.pre.flag')
1046 self.assertFalse(os.path.exists(pre_flag_path))
1047 post_flag_path = os.path.join(self.test_root, build.uuid +
1048 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001049 self.assertTrue(os.path.exists(post_flag_path),
1050 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001051
1052
James E. Blairb9c0d772017-03-03 14:34:49 -08001053class TestBrokenConfig(ZuulTestCase):
1054 # Test that we get an appropriate syntax error if we start with a
1055 # broken config.
1056
1057 tenant_config_file = 'config/broken/main.yaml'
1058
1059 def setUp(self):
1060 with testtools.ExpectedException(
1061 zuul.configloader.ConfigurationSyntaxError,
1062 "\nZuul encountered a syntax error"):
1063 super(TestBrokenConfig, self).setUp()
1064
1065 def test_broken_config_on_startup(self):
1066 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001067
1068
1069class TestProjectKeys(ZuulTestCase):
1070 # Test that we can generate project keys
1071
1072 # Normally the test infrastructure copies a static key in place
1073 # for each project before starting tests. This saves time because
1074 # Zuul's automatic key-generation on startup can be slow. To make
1075 # sure we exercise that code, in this test we allow Zuul to create
1076 # keys for the project on startup.
1077 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001078 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001079 tenant_config_file = 'config/in-repo/main.yaml'
1080
1081 def test_key_generation(self):
1082 key_root = os.path.join(self.state_root, 'keys')
1083 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1084 # Make sure that a proper key was created on startup
1085 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001086 private_key, public_key = \
1087 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001088
1089 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1090 fixture_private_key = i.read()
1091
1092 # Make sure that we didn't just end up with the static fixture
1093 # key
1094 self.assertNotEqual(fixture_private_key, private_key)
1095
1096 # Make sure it's the right length
1097 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001098
1099
James E. Blairbb94dfa2017-07-11 07:45:19 -07001100class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001101 def _assertRolePath(self, build, playbook, content):
1102 path = os.path.join(self.test_root, build.uuid,
1103 'ansible', playbook, 'ansible.cfg')
1104 roles_paths = []
1105 with open(path) as f:
1106 for line in f:
1107 if line.startswith('roles_path'):
1108 roles_paths.append(line)
1109 print(roles_paths)
1110 if content:
1111 self.assertEqual(len(roles_paths), 1,
1112 "Should have one roles_path line in %s" %
1113 (playbook,))
1114 self.assertIn(content, roles_paths[0])
1115 else:
1116 self.assertEqual(len(roles_paths), 0,
1117 "Should have no roles_path line in %s" %
1118 (playbook,))
1119
James E. Blairbb94dfa2017-07-11 07:45:19 -07001120
1121class TestRoles(RoleTestCase):
1122 tenant_config_file = 'config/roles/main.yaml'
1123
James E. Blairbce76932017-05-04 10:03:15 -07001124 def test_role(self):
1125 # This exercises a proposed change to a role being checked out
1126 # and used.
1127 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1128 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1129 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1130 B.subject, A.data['id'])
1131 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1132 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1133 self.waitUntilSettled()
1134 self.assertHistory([
1135 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1136 ])
James E. Blair6459db12017-06-29 14:57:20 -07001137
James E. Blair1b27f6a2017-07-14 14:09:07 -07001138 def test_role_inheritance(self):
1139 self.executor_server.hold_jobs_in_build = True
1140 conf = textwrap.dedent(
1141 """
1142 - job:
1143 name: parent
1144 roles:
1145 - zuul: bare-role
1146 pre-run: playbooks/parent-pre
1147 post-run: playbooks/parent-post
1148
1149 - job:
1150 name: project-test
1151 parent: parent
1152 roles:
1153 - zuul: org/project
1154
1155 - project:
1156 name: org/project
1157 check:
1158 jobs:
1159 - project-test
1160 """)
1161
1162 file_dict = {'.zuul.yaml': conf}
1163 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1164 files=file_dict)
1165 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1166 self.waitUntilSettled()
1167
1168 self.assertEqual(len(self.builds), 1)
1169 build = self.getBuildByName('project-test')
1170 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1171 self._assertRolePath(build, 'playbook_0', 'role_0')
1172 self._assertRolePath(build, 'playbook_0', 'role_1')
1173 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1174
1175 self.executor_server.hold_jobs_in_build = False
1176 self.executor_server.release()
1177 self.waitUntilSettled()
1178
1179 self.assertHistory([
1180 dict(name='project-test', result='SUCCESS', changes='1,1'),
1181 ])
1182
James E. Blair6f699732017-07-18 14:19:11 -07001183 def test_role_error(self):
1184 conf = textwrap.dedent(
1185 """
1186 - job:
1187 name: project-test
1188 roles:
1189 - zuul: common-config
1190
1191 - project:
1192 name: org/project
1193 check:
1194 jobs:
1195 - project-test
1196 """)
1197
1198 file_dict = {'.zuul.yaml': conf}
1199 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1200 files=file_dict)
1201 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1202 self.waitUntilSettled()
1203 self.assertIn(
1204 '- project-test project-test : ERROR Unable to find role',
1205 A.messages[-1])
1206
James E. Blair6459db12017-06-29 14:57:20 -07001207
James E. Blairbb94dfa2017-07-11 07:45:19 -07001208class TestImplicitRoles(RoleTestCase):
1209 tenant_config_file = 'config/implicit-roles/main.yaml'
1210
1211 def test_missing_roles(self):
1212 # Test implicit and explicit roles for a project which does
1213 # not have roles. The implicit role should be silently
1214 # ignored since the project doesn't supply roles, but if a
1215 # user declares an explicit role, it should error.
1216 self.executor_server.hold_jobs_in_build = True
1217 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1218 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1219 self.waitUntilSettled()
1220
1221 self.assertEqual(len(self.builds), 2)
1222 build = self.getBuildByName('implicit-role-fail')
1223 self._assertRolePath(build, 'playbook_0', None)
1224
1225 self.executor_server.hold_jobs_in_build = False
1226 self.executor_server.release()
1227 self.waitUntilSettled()
1228 # The retry_limit doesn't get recorded
1229 self.assertHistory([
1230 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1231 ])
1232
1233 def test_roles(self):
1234 # Test implicit and explicit roles for a project which does
1235 # have roles. In both cases, we should end up with the role
1236 # in the path. In the explicit case, ensure we end up with
1237 # the name we specified.
1238 self.executor_server.hold_jobs_in_build = True
1239 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1240 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1241 self.waitUntilSettled()
1242
1243 self.assertEqual(len(self.builds), 2)
1244 build = self.getBuildByName('implicit-role-ok')
1245 self._assertRolePath(build, 'playbook_0', 'role_0')
1246
1247 build = self.getBuildByName('explicit-role-ok')
1248 self._assertRolePath(build, 'playbook_0', 'role_0')
1249
1250 self.executor_server.hold_jobs_in_build = False
1251 self.executor_server.release()
1252 self.waitUntilSettled()
1253 self.assertHistory([
1254 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1255 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1256 ], ordered=False)
1257
1258
James E. Blair6459db12017-06-29 14:57:20 -07001259class TestShadow(ZuulTestCase):
1260 tenant_config_file = 'config/shadow/main.yaml'
1261
1262 def test_shadow(self):
1263 # Test that a repo is allowed to shadow another's job definitions.
1264 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1265 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1266 self.waitUntilSettled()
1267 self.assertHistory([
1268 dict(name='test1', result='SUCCESS', changes='1,1'),
1269 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001270 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001271
1272
1273class TestDataReturn(AnsibleZuulTestCase):
1274 tenant_config_file = 'config/data-return/main.yaml'
1275
1276 def test_data_return(self):
1277 # This exercises a proposed change to a role being checked out
1278 # and used.
1279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1280 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1281 self.waitUntilSettled()
1282 self.assertHistory([
1283 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001284 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1285 ], ordered=False)
1286 self.assertIn('- data-return http://example.com/test/log/url/',
1287 A.messages[-1])
1288 self.assertIn('- data-return-relative '
1289 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001290 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001291
1292
1293class TestDiskAccounting(AnsibleZuulTestCase):
1294 config_file = 'zuul-disk-accounting.conf'
1295 tenant_config_file = 'config/disk-accountant/main.yaml'
1296
1297 def test_disk_accountant_kills_job(self):
1298 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1299 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1300 self.waitUntilSettled()
1301 self.assertHistory([
1302 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001303
1304
1305class TestMaxNodesPerJob(AnsibleZuulTestCase):
1306 tenant_config_file = 'config/multi-tenant/main.yaml'
1307
1308 def test_max_nodes_reached(self):
1309 in_repo_conf = textwrap.dedent(
1310 """
1311 - job:
1312 name: test-job
1313 nodes:
1314 - name: node01
1315 label: fake
1316 - name: node02
1317 label: fake
1318 - name: node03
1319 label: fake
1320 - name: node04
1321 label: fake
1322 - name: node05
1323 label: fake
1324 - name: node06
1325 label: fake
1326 """)
1327 file_dict = {'.zuul.yaml': in_repo_conf}
1328 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1329 files=file_dict)
1330 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1331 self.waitUntilSettled()
1332 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1333 A.messages[0], "A should fail because of nodes limit")
1334
1335 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1336 files=file_dict)
1337 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1338 self.waitUntilSettled()
1339 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1340 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001341
1342
1343class TestBaseJobs(ZuulTestCase):
1344 tenant_config_file = 'config/base-jobs/main.yaml'
1345
1346 def test_multiple_base_jobs(self):
1347 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1348 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1349 self.waitUntilSettled()
1350 self.assertHistory([
1351 dict(name='my-job', result='SUCCESS', changes='1,1'),
1352 dict(name='other-job', result='SUCCESS', changes='1,1'),
1353 ], ordered=False)
1354 self.assertEqual(self.getJobFromHistory('my-job').
1355 parameters['zuul']['jobtags'],
1356 ['mybase'])
1357 self.assertEqual(self.getJobFromHistory('other-job').
1358 parameters['zuul']['jobtags'],
1359 ['otherbase'])
1360
1361 def test_untrusted_base_job(self):
1362 """Test that a base job may not be defined in an untrusted repo"""
1363 in_repo_conf = textwrap.dedent(
1364 """
1365 - job:
1366 name: fail-base
1367 parent: null
1368 """)
1369
1370 file_dict = {'.zuul.yaml': in_repo_conf}
1371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1372 files=file_dict)
1373 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1374 self.waitUntilSettled()
1375 self.assertEqual(A.reported, 1,
1376 "A should report failure")
1377 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1378 self.assertIn('Base jobs must be defined in config projects',
1379 A.messages[0])
1380 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001381
1382
1383class TestSecretLeaks(AnsibleZuulTestCase):
1384 tenant_config_file = 'config/secret-leaks/main.yaml'
1385
1386 def searchForContent(self, path, content):
1387 matches = []
1388 for (dirpath, dirnames, filenames) in os.walk(path):
1389 for filename in filenames:
1390 filepath = os.path.join(dirpath, filename)
1391 with open(filepath, 'rb') as f:
1392 if content in f.read():
1393 matches.append(filepath[len(path):])
1394 return matches
1395
1396 def _test_secret_file(self):
1397 # Or rather -- test that they *don't* leak.
1398 # Keep the jobdir around so we can inspect contents.
1399 self.executor_server.keep_jobdir = True
1400 conf = textwrap.dedent(
1401 """
1402 - project:
1403 name: org/project
1404 check:
1405 jobs:
1406 - secret-file
1407 """)
1408
1409 file_dict = {'.zuul.yaml': conf}
1410 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1411 files=file_dict)
1412 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1413 self.waitUntilSettled()
1414 self.assertHistory([
1415 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1416 ], ordered=False)
1417 matches = self.searchForContent(self.history[0].jobdir.root,
1418 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001419 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001420 set(matches))
1421
1422 def test_secret_file(self):
1423 self._test_secret_file()
1424
1425 def test_secret_file_verbose(self):
1426 # Output extra ansible info to exercise alternate logging code
1427 # paths.
1428 self.executor_server.verbose = True
1429 self._test_secret_file()
1430
1431 def _test_secret_file_fail(self):
1432 # Or rather -- test that they *don't* leak.
1433 # Keep the jobdir around so we can inspect contents.
1434 self.executor_server.keep_jobdir = True
1435 conf = textwrap.dedent(
1436 """
1437 - project:
1438 name: org/project
1439 check:
1440 jobs:
1441 - secret-file-fail
1442 """)
1443
1444 file_dict = {'.zuul.yaml': conf}
1445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1446 files=file_dict)
1447 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1448 self.waitUntilSettled()
1449 self.assertHistory([
1450 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1451 ], ordered=False)
1452 matches = self.searchForContent(self.history[0].jobdir.root,
1453 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001454 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001455 set(matches))
1456
1457 def test_secret_file_fail(self):
1458 self._test_secret_file_fail()
1459
1460 def test_secret_file_fail_verbose(self):
1461 # Output extra ansible info to exercise alternate logging code
1462 # paths.
1463 self.executor_server.verbose = True
1464 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001465
1466
1467class TestJobOutput(AnsibleZuulTestCase):
1468 tenant_config_file = 'config/job-output/main.yaml'
1469
1470 def _get_file(self, build, path):
1471 p = os.path.join(build.jobdir.root, path)
1472 with open(p) as f:
1473 return f.read()
1474
1475 def test_job_output(self):
1476 # Verify that command standard output appears in the job output
1477
1478 # This currently only verifies we receive output from
1479 # localhost. Notably, it does not verify we receive output
1480 # via zuul_console streaming.
1481 self.executor_server.keep_jobdir = True
1482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1483 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1484 self.waitUntilSettled()
1485 self.assertHistory([
1486 dict(name='job-output', result='SUCCESS', changes='1,1'),
1487 ], ordered=False)
1488
1489 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1490 j = json.loads(self._get_file(self.history[0],
1491 'work/logs/job-output.json'))
1492 self.assertEqual(token,
1493 j[0]['plays'][0]['tasks'][0]
1494 ['hosts']['localhost']['stdout'])
1495
1496 print(self._get_file(self.history[0],
1497 'work/logs/job-output.txt'))
1498 self.assertIn(token,
1499 self._get_file(self.history[0],
1500 'work/logs/job-output.txt'))