blob: 98cffcc29a8fcb8a595bc79a90fd51881bdc904d [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. Blaira92cbc82017-01-23 14:56:49 -080017import os
James E. Blair14abdf42015-12-09 16:11:53 -080018import textwrap
James E. Blair59fdbac2015-12-07 17:08:06 -080019
James E. Blairb9c0d772017-03-03 14:34:49 -080020import testtools
21
22import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070023from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000024from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080025
James E. Blair59fdbac2015-12-07 17:08:06 -080026
James E. Blair3f876d52016-07-22 13:07:14 -070027class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080028 # A temporary class to hold new tests while others are disabled
29
James E. Blair2a629ec2015-12-22 15:32:02 -080030 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080031
James E. Blair83005782015-12-11 14:46:03 -080032 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080033 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020034 A.addApproval('Code-Review', 2)
35 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080036 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080037 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080038 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080039 self.assertEqual(self.getJobFromHistory('python27').result,
40 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080041 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080042 self.assertEqual(A.reported, 2,
43 "A should report start and success")
44 self.assertIn('tenant-one-gate', A.messages[1],
45 "A should transit tenant-one gate")
46 self.assertNotIn('tenant-two-gate', A.messages[1],
47 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080048
James E. Blair96f26942015-12-09 10:15:59 -080049 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020050 B.addApproval('Code-Review', 2)
51 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080052 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080053 self.assertEqual(self.getJobFromHistory('python27',
54 'org/project2').result,
55 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080056 self.assertEqual(self.getJobFromHistory('project2-test1').result,
57 'SUCCESS')
58 self.assertEqual(B.data['status'], 'MERGED')
59 self.assertEqual(B.reported, 2,
60 "B should report start and success")
61 self.assertIn('tenant-two-gate', B.messages[1],
62 "B should transit tenant-two gate")
63 self.assertNotIn('tenant-one-gate', B.messages[1],
64 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080065
James E. Blair96f26942015-12-09 10:15:59 -080066 self.assertEqual(A.reported, 2, "Activity in tenant two should"
67 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080068
James E. Blair83005782015-12-11 14:46:03 -080069
Tobias Henkel83167622017-06-30 19:45:03 +020070class TestFinal(ZuulTestCase):
71
72 tenant_config_file = 'config/final/main.yaml'
73
74 def test_final_variant_ok(self):
75 # test clean usage of final parent job
76 in_repo_conf = textwrap.dedent(
77 """
78 - project:
79 name: org/project
80 check:
81 jobs:
82 - job-final
83 """)
84
85 file_dict = {'.zuul.yaml': in_repo_conf}
86 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
87 files=file_dict)
88 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
89 self.waitUntilSettled()
90
91 self.assertEqual(A.reported, 1)
92 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
93
94 def test_final_variant_error(self):
95 # test misuse of final parent job
96 in_repo_conf = textwrap.dedent(
97 """
98 - project:
99 name: org/project
100 check:
101 jobs:
102 - job-final:
103 vars:
104 dont_override_this: bar
105 """)
106 file_dict = {'.zuul.yaml': in_repo_conf}
107 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
108 files=file_dict)
109 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
110 self.waitUntilSettled()
111
112 # The second patch tried to override some variables.
113 # Thus it should fail.
114 self.assertEqual(A.reported, 1)
115 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
116 self.assertIn('Unable to modify final job', A.messages[0])
117
118 def test_final_inheritance(self):
119 # test misuse of final parent job
120 in_repo_conf = textwrap.dedent(
121 """
122 - job:
123 name: project-test
124 parent: job-final
125
126 - project:
127 name: org/project
128 check:
129 jobs:
130 - project-test
131 """)
132
133 in_repo_playbook = textwrap.dedent(
134 """
135 - hosts: all
136 tasks: []
137 """)
138
139 file_dict = {'.zuul.yaml': in_repo_conf,
140 'playbooks/project-test.yaml': in_repo_playbook}
141 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
142 files=file_dict)
143 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
144 self.waitUntilSettled()
145
146 # The second patch tried to override some variables.
147 # Thus it should fail.
148 self.assertEqual(A.reported, 1)
149 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
150 self.assertIn('Unable to inherit from final job', A.messages[0])
151
152
James E. Blairff555742017-02-19 11:34:27 -0800153class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800154 # A temporary class to hold new tests while others are disabled
155
Tobias Henkelabf973e2017-07-28 10:07:34 +0200156 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800157 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800158
James E. Blair83005782015-12-11 14:46:03 -0800159 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200161 A.addApproval('Code-Review', 2)
162 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800163 self.waitUntilSettled()
164 self.assertEqual(self.getJobFromHistory('project-test1').result,
165 'SUCCESS')
166 self.assertEqual(A.data['status'], 'MERGED')
167 self.assertEqual(A.reported, 2,
168 "A should report start and success")
169 self.assertIn('tenant-one-gate', A.messages[1],
170 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800171
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700172 def test_dynamic_config(self):
173 in_repo_conf = textwrap.dedent(
174 """
175 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200176 name: project-test1
177
178 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700179 name: project-test2
180
181 - project:
182 name: org/project
183 tenant-one-gate:
184 jobs:
185 - project-test2
186 """)
187
James E. Blairc73c73a2017-01-20 15:15:15 -0800188 in_repo_playbook = textwrap.dedent(
189 """
190 - hosts: all
191 tasks: []
192 """)
193
194 file_dict = {'.zuul.yaml': in_repo_conf,
195 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800197 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200198 A.addApproval('Code-Review', 2)
199 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700200 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700201 self.assertEqual(A.data['status'], 'MERGED')
202 self.assertEqual(A.reported, 2,
203 "A should report start and success")
204 self.assertIn('tenant-one-gate', A.messages[1],
205 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800206 self.assertHistory([
207 dict(name='project-test2', result='SUCCESS', changes='1,1')])
208
James E. Blairc2a5ed72017-02-20 14:12:01 -0500209 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800210 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500211
James E. Blair646322f2017-01-27 15:50:34 -0800212 # Now that the config change is landed, it should be live for
213 # subsequent changes.
214 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200215 B.addApproval('Code-Review', 2)
216 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800217 self.waitUntilSettled()
218 self.assertEqual(self.getJobFromHistory('project-test2').result,
219 'SUCCESS')
220 self.assertHistory([
221 dict(name='project-test2', result='SUCCESS', changes='1,1'),
222 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800223
Tobias Henkelf02cf512017-07-21 22:55:34 +0200224 def test_dynamic_config_non_existing_job(self):
225 """Test that requesting a non existent job fails"""
226 in_repo_conf = textwrap.dedent(
227 """
228 - job:
229 name: project-test1
230
231 - project:
232 name: org/project
233 check:
234 jobs:
235 - non-existent-job
236 """)
237
238 in_repo_playbook = textwrap.dedent(
239 """
240 - hosts: all
241 tasks: []
242 """)
243
244 file_dict = {'.zuul.yaml': in_repo_conf,
245 'playbooks/project-test2.yaml': in_repo_playbook}
246 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
247 files=file_dict)
248 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
249 self.waitUntilSettled()
250 self.assertEqual(A.reported, 1,
251 "A should report failure")
252 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
253 self.assertIn('Job non-existent-job not defined', A.messages[0],
254 "A should have failed the check pipeline")
255 self.assertHistory([])
256
257 def test_dynamic_config_non_existing_job_in_template(self):
258 """Test that requesting a non existent job fails"""
259 in_repo_conf = textwrap.dedent(
260 """
261 - job:
262 name: project-test1
263
264 - project-template:
265 name: test-template
266 check:
267 jobs:
268 - non-existent-job
269
270 - project:
271 name: org/project
272 templates:
273 - test-template
274 """)
275
276 in_repo_playbook = textwrap.dedent(
277 """
278 - hosts: all
279 tasks: []
280 """)
281
282 file_dict = {'.zuul.yaml': in_repo_conf,
283 'playbooks/project-test2.yaml': in_repo_playbook}
284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
285 files=file_dict)
286 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
287 self.waitUntilSettled()
288 self.assertEqual(A.reported, 1,
289 "A should report failure")
290 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
291 self.assertIn('Job non-existent-job not defined', A.messages[0],
292 "A should have failed the check pipeline")
293 self.assertHistory([])
294
Tobias Henkel0f714002017-06-30 23:30:52 +0200295 def test_dynamic_config_new_patchset(self):
296 self.executor_server.hold_jobs_in_build = True
297
298 tenant = self.sched.abide.tenants.get('tenant-one')
299 check_pipeline = tenant.layout.pipelines['check']
300
301 in_repo_conf = textwrap.dedent(
302 """
303 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200304 name: project-test1
305
306 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200307 name: project-test2
308
309 - project:
310 name: org/project
311 check:
312 jobs:
313 - project-test2
314 """)
315
316 in_repo_playbook = textwrap.dedent(
317 """
318 - hosts: all
319 tasks: []
320 """)
321
322 file_dict = {'.zuul.yaml': in_repo_conf,
323 'playbooks/project-test2.yaml': in_repo_playbook}
324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
325 files=file_dict)
326 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
327 self.waitUntilSettled()
328
329 items = check_pipeline.getAllItems()
330 self.assertEqual(items[0].change.number, '1')
331 self.assertEqual(items[0].change.patchset, '1')
332 self.assertTrue(items[0].live)
333
334 in_repo_conf = textwrap.dedent(
335 """
336 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200337 name: project-test1
338
339 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200340 name: project-test2
341
342 - project:
343 name: org/project
344 check:
345 jobs:
346 - project-test1
347 - project-test2
348 """)
349 file_dict = {'.zuul.yaml': in_repo_conf,
350 'playbooks/project-test2.yaml': in_repo_playbook}
351
352 A.addPatchset(files=file_dict)
353 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
354
355 self.waitUntilSettled()
356
357 items = check_pipeline.getAllItems()
358 self.assertEqual(items[0].change.number, '1')
359 self.assertEqual(items[0].change.patchset, '2')
360 self.assertTrue(items[0].live)
361
362 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200363 self.executor_server.release('project-test1')
364 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200365 self.executor_server.release()
366 self.waitUntilSettled()
367
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200368 self.assertHistory([
369 dict(name='project-test2', result='ABORTED', changes='1,1'),
370 dict(name='project-test1', result='SUCCESS', changes='1,2'),
371 dict(name='project-test2', result='SUCCESS', changes='1,2')])
372
Jesse Keating78f544a2017-07-13 14:27:40 -0700373 def test_dynamic_dependent_pipeline(self):
374 # Test dynamically adding a project to a
375 # dependent pipeline for the first time
376 self.executor_server.hold_jobs_in_build = True
377
378 tenant = self.sched.abide.tenants.get('tenant-one')
379 gate_pipeline = tenant.layout.pipelines['gate']
380
381 in_repo_conf = textwrap.dedent(
382 """
383 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200384 name: project-test1
385
386 - job:
Jesse Keating78f544a2017-07-13 14:27:40 -0700387 name: project-test2
388
389 - project:
390 name: org/project
391 gate:
392 jobs:
393 - project-test2
394 """)
395
396 in_repo_playbook = textwrap.dedent(
397 """
398 - hosts: all
399 tasks: []
400 """)
401
402 file_dict = {'.zuul.yaml': in_repo_conf,
403 'playbooks/project-test2.yaml': in_repo_playbook}
404 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
405 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200406 A.addApproval('Approved', 1)
407 self.fake_gerrit.addEvent(A.addApproval('Code-Review', 2))
Jesse Keating78f544a2017-07-13 14:27:40 -0700408 self.waitUntilSettled()
409
410 items = gate_pipeline.getAllItems()
411 self.assertEqual(items[0].change.number, '1')
412 self.assertEqual(items[0].change.patchset, '1')
413 self.assertTrue(items[0].live)
414
415 self.executor_server.hold_jobs_in_build = False
416 self.executor_server.release()
417 self.waitUntilSettled()
418
419 # Make sure the dynamic queue got cleaned up
420 self.assertEqual(gate_pipeline.queues, [])
421
James E. Blairff555742017-02-19 11:34:27 -0800422 def test_in_repo_branch(self):
423 in_repo_conf = textwrap.dedent(
424 """
425 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200426 name: project-test1
427
428 - job:
James E. Blairff555742017-02-19 11:34:27 -0800429 name: project-test2
430
431 - project:
432 name: org/project
433 tenant-one-gate:
434 jobs:
435 - project-test2
436 """)
437
438 in_repo_playbook = textwrap.dedent(
439 """
440 - hosts: all
441 tasks: []
442 """)
443
444 file_dict = {'.zuul.yaml': in_repo_conf,
445 'playbooks/project-test2.yaml': in_repo_playbook}
446 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700447 self.fake_gerrit.addEvent(
448 self.fake_gerrit.getFakeBranchCreatedEvent(
449 'org/project', 'stable'))
James E. Blairff555742017-02-19 11:34:27 -0800450 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
451 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200452 A.addApproval('Code-Review', 2)
453 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800454 self.waitUntilSettled()
455 self.assertEqual(A.data['status'], 'MERGED')
456 self.assertEqual(A.reported, 2,
457 "A should report start and success")
458 self.assertIn('tenant-one-gate', A.messages[1],
459 "A should transit tenant-one gate")
460 self.assertHistory([
461 dict(name='project-test2', result='SUCCESS', changes='1,1')])
462 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800463 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800464
465 # The config change should not affect master.
466 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200467 B.addApproval('Code-Review', 2)
468 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800469 self.waitUntilSettled()
470 self.assertHistory([
471 dict(name='project-test2', result='SUCCESS', changes='1,1'),
472 dict(name='project-test1', result='SUCCESS', changes='2,1')])
473
474 # The config change should be live for further changes on
475 # stable.
476 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200477 C.addApproval('Code-Review', 2)
478 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800479 self.waitUntilSettled()
480 self.assertHistory([
481 dict(name='project-test2', result='SUCCESS', changes='1,1'),
482 dict(name='project-test1', result='SUCCESS', changes='2,1'),
483 dict(name='project-test2', result='SUCCESS', changes='3,1')])
484
James E. Blaira5a12492017-05-03 11:40:48 -0700485 def test_crd_dynamic_config_branch(self):
486 # Test that we can create a job in one repo and be able to use
487 # it from a different branch on a different repo.
488
489 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700490 self.fake_gerrit.addEvent(
491 self.fake_gerrit.getFakeBranchCreatedEvent(
492 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700493
494 in_repo_conf = textwrap.dedent(
495 """
496 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200497 name: project-test1
498
499 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700500 name: project-test2
501
502 - project:
503 name: org/project
504 check:
505 jobs:
506 - project-test2
507 """)
508
509 in_repo_playbook = textwrap.dedent(
510 """
511 - hosts: all
512 tasks: []
513 """)
514
515 file_dict = {'.zuul.yaml': in_repo_conf,
516 'playbooks/project-test2.yaml': in_repo_playbook}
517 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
518 files=file_dict)
519
520 second_repo_conf = textwrap.dedent(
521 """
522 - project:
523 name: org/project1
524 check:
525 jobs:
526 - project-test2
527 """)
528
529 second_file_dict = {'.zuul.yaml': second_repo_conf}
530 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
531 files=second_file_dict)
532 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
533 B.subject, A.data['id'])
534
535 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
536 self.waitUntilSettled()
537 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
538 self.waitUntilSettled()
539
540 self.assertEqual(A.reported, 1, "A should report")
541 self.assertHistory([
542 dict(name='project-test2', result='SUCCESS', changes='1,1'),
543 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
544 ])
545
James E. Blair149b69c2017-03-02 10:48:16 -0800546 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800547 in_repo_conf = textwrap.dedent(
548 """
549 - job:
550 name: project-test2
551 foo: error
552 """)
553
554 file_dict = {'.zuul.yaml': in_repo_conf}
555 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
556 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200557 A.addApproval('Code-Review', 2)
558 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800559 self.waitUntilSettled()
560
561 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200562 self.assertEqual(A.reported, 1,
563 "A should report failure")
564 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800565 "A should have a syntax error reported")
566
James E. Blair149b69c2017-03-02 10:48:16 -0800567 def test_trusted_syntax_error(self):
568 in_repo_conf = textwrap.dedent(
569 """
570 - job:
571 name: project-test2
572 foo: error
573 """)
574
575 file_dict = {'zuul.yaml': in_repo_conf}
576 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
577 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200578 A.addApproval('Code-Review', 2)
579 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800580 self.waitUntilSettled()
581
582 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200583 self.assertEqual(A.reported, 1,
584 "A should report failure")
585 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800586 "A should have a syntax error reported")
587
James E. Blair6f140c72017-03-03 10:32:07 -0800588 def test_untrusted_yaml_error(self):
589 in_repo_conf = textwrap.dedent(
590 """
591 - job:
592 foo: error
593 """)
594
595 file_dict = {'.zuul.yaml': in_repo_conf}
596 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
597 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200598 A.addApproval('Code-Review', 2)
599 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800600 self.waitUntilSettled()
601
602 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200603 self.assertEqual(A.reported, 1,
604 "A should report failure")
605 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800606 "A should have a syntax error reported")
607
James E. Blairdb04e6a2017-05-03 14:49:36 -0700608 def test_untrusted_shadow_error(self):
609 in_repo_conf = textwrap.dedent(
610 """
611 - job:
612 name: common-config-test
613 """)
614
615 file_dict = {'.zuul.yaml': in_repo_conf}
616 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
617 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200618 A.addApproval('Code-Review', 2)
619 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700620 self.waitUntilSettled()
621
622 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200623 self.assertEqual(A.reported, 1,
624 "A should report failure")
625 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700626 "A should have a syntax error reported")
627
James E. Blaird5656ad2017-06-02 14:29:41 -0700628 def test_untrusted_pipeline_error(self):
629 in_repo_conf = textwrap.dedent(
630 """
631 - pipeline:
632 name: test
633 """)
634
635 file_dict = {'.zuul.yaml': in_repo_conf}
636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
637 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200638 A.addApproval('Code-Review', 2)
639 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700640 self.waitUntilSettled()
641
642 self.assertEqual(A.data['status'], 'NEW')
643 self.assertEqual(A.reported, 1,
644 "A should report failure")
645 self.assertIn('Pipelines may not be defined', A.messages[0],
646 "A should have a syntax error reported")
647
648 def test_untrusted_project_error(self):
649 in_repo_conf = textwrap.dedent(
650 """
651 - project:
652 name: org/project1
653 """)
654
655 file_dict = {'.zuul.yaml': in_repo_conf}
656 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
657 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200658 A.addApproval('Code-Review', 2)
659 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700660 self.waitUntilSettled()
661
662 self.assertEqual(A.data['status'], 'NEW')
663 self.assertEqual(A.reported, 1,
664 "A should report failure")
665 self.assertIn('the only project definition permitted', A.messages[0],
666 "A should have a syntax error reported")
667
James E. Blaire64b0e42017-06-08 11:23:34 -0700668 def test_duplicate_node_error(self):
669 in_repo_conf = textwrap.dedent(
670 """
671 - nodeset:
672 name: duplicate
673 nodes:
674 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700675 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700676 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700677 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700678 """)
679
680 file_dict = {'.zuul.yaml': in_repo_conf}
681 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
682 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200683 A.addApproval('Code-Review', 2)
684 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700685 self.waitUntilSettled()
686
687 self.assertEqual(A.data['status'], 'NEW')
688 self.assertEqual(A.reported, 1,
689 "A should report failure")
690 self.assertIn('appears multiple times', A.messages[0],
691 "A should have a syntax error reported")
692
693 def test_duplicate_group_error(self):
694 in_repo_conf = textwrap.dedent(
695 """
696 - nodeset:
697 name: duplicate
698 nodes:
699 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700700 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700701 groups:
702 - name: group
703 nodes: compute
704 - name: group
705 nodes: compute
706 """)
707
708 file_dict = {'.zuul.yaml': in_repo_conf}
709 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
710 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200711 A.addApproval('Code-Review', 2)
712 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700713 self.waitUntilSettled()
714
715 self.assertEqual(A.data['status'], 'NEW')
716 self.assertEqual(A.reported, 1,
717 "A should report failure")
718 self.assertIn('appears multiple times', A.messages[0],
719 "A should have a syntax error reported")
720
James E. Blair09f9ffe2017-07-11 15:30:25 -0700721 def test_multi_repo(self):
722 downstream_repo_conf = textwrap.dedent(
723 """
724 - project:
725 name: org/project1
726 tenant-one-gate:
727 jobs:
728 - project-test1
729
730 - job:
731 name: project1-test1
732 parent: project-test1
733 """)
734
735 file_dict = {'.zuul.yaml': downstream_repo_conf}
736 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
737 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200738 A.addApproval('Code-Review', 2)
739 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700740 self.waitUntilSettled()
741
742 self.assertEqual(A.data['status'], 'MERGED')
743 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
744 self.waitUntilSettled()
745
746 upstream_repo_conf = textwrap.dedent(
747 """
748 - job:
749 name: project-test1
750
751 - job:
752 name: project-test2
753
754 - project:
755 name: org/project
756 tenant-one-gate:
757 jobs:
758 - project-test1
759 """)
760
761 file_dict = {'.zuul.yaml': upstream_repo_conf}
762 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
763 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200764 B.addApproval('Code-Review', 2)
765 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700766 self.waitUntilSettled()
767
768 self.assertEqual(B.data['status'], 'MERGED')
769 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
770 self.waitUntilSettled()
771
772 tenant = self.sched.abide.tenants.get('tenant-one')
773 # Ensure the latest change is reflected in the config; if it
774 # isn't this will raise an exception.
775 tenant.layout.getJob('project-test2')
776
James E. Blairc73c73a2017-01-20 15:15:15 -0800777
778class TestAnsible(AnsibleZuulTestCase):
779 # A temporary class to hold new tests while others are disabled
780
781 tenant_config_file = 'config/ansible/main.yaml'
782
783 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +1100784 # Keep the jobdir around so we can inspect contents if an
785 # assert fails.
786 self.executor_server.keep_jobdir = True
787 # Output extra ansible info so we might see errors.
788 self.executor_server.verbose = True
789 # Add a site variables file, used by check-vars
790 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
791 'variables.yaml')
792 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -0800793 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
794 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
795 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +0200796 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +1100797 with self.jobLog(build_timeout):
798 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200799 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +1100800 with self.jobLog(build_faillocal):
801 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200802 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +1100803 with self.jobLog(build_failpost):
804 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200805 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +1100806 with self.jobLog(build_check_vars):
807 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -0500808 build_check_secret_names = self.getJobFromHistory('check-secret-names')
809 with self.jobLog(build_check_secret_names):
810 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200811 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +1100812 with self.jobLog(build_hello):
813 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200814 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +1100815 with self.jobLog(build_python27):
816 self.assertEqual(build_python27.result, 'SUCCESS')
817 flag_path = os.path.join(self.test_root,
818 build_python27.uuid + '.flag')
819 self.assertTrue(os.path.exists(flag_path))
820 copied_path = os.path.join(self.test_root, build_python27.uuid +
821 '.copied')
822 self.assertTrue(os.path.exists(copied_path))
823 failed_path = os.path.join(self.test_root, build_python27.uuid +
824 '.failed')
825 self.assertFalse(os.path.exists(failed_path))
826 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
827 '.pre.flag')
828 self.assertTrue(os.path.exists(pre_flag_path))
829 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
830 '.post.flag')
831 self.assertTrue(os.path.exists(post_flag_path))
832 bare_role_flag_path = os.path.join(self.test_root,
833 build_python27.uuid +
834 '.bare-role.flag')
835 self.assertTrue(os.path.exists(bare_role_flag_path))
836 secrets_path = os.path.join(self.test_root,
837 build_python27.uuid + '.secrets')
838 with open(secrets_path) as f:
839 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -0800840
Jamie Lennox7655b552017-03-17 12:33:38 +1100841 msg = A.messages[0]
842 success = "{} https://success.example.com/zuul-logs/{}"
843 fail = "{} https://failure.example.com/zuul-logs/{}"
844 self.assertIn(success.format("python27", build_python27.uuid), msg)
845 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
846 self.assertIn(success.format("check-vars",
847 build_check_vars.uuid), msg)
848 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
849 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
850 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +0200851
James E. Blairabbaa6f2017-04-06 16:11:44 -0700852 def _add_job(self, job_name):
853 conf = textwrap.dedent(
854 """
855 - job:
856 name: %s
857
858 - project:
859 name: org/plugin-project
860 check:
861 jobs:
862 - %s
863 """ % (job_name, job_name))
864
865 file_dict = {'.zuul.yaml': conf}
866 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
867 files=file_dict)
868 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
869 self.waitUntilSettled()
870
871 def test_plugins(self):
872 # Keep the jobdir around so we can inspect contents if an
873 # assert fails.
874 self.executor_server.keep_jobdir = True
875 # Output extra ansible info so we might see errors.
876 self.executor_server.verbose = True
877
878 count = 0
879 plugin_tests = [
880 ('passwd', 'FAILURE'),
881 ('cartesian', 'SUCCESS'),
882 ('consul_kv', 'FAILURE'),
883 ('credstash', 'FAILURE'),
884 ('csvfile_good', 'SUCCESS'),
885 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -0500886 ('uri_bad_path', 'FAILURE'),
887 ('uri_bad_scheme', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -0700888 ]
889 for job_name, result in plugin_tests:
890 count += 1
891 self._add_job(job_name)
892
893 job = self.getJobFromHistory(job_name)
894 with self.jobLog(job):
895 self.assertEqual(count, len(self.history))
896 build = self.history[-1]
897 self.assertEqual(build.result, result)
898
899 # TODOv3(jeblair): parse the ansible output and verify we're
900 # getting the exception we expect.
901
James E. Blairb9c0d772017-03-03 14:34:49 -0800902
James E. Blaira4d4eef2017-06-30 14:49:17 -0700903class TestPrePlaybooks(AnsibleZuulTestCase):
904 # A temporary class to hold new tests while others are disabled
905
906 tenant_config_file = 'config/pre-playbook/main.yaml'
907
908 def test_pre_playbook_fail(self):
909 # Test that we run the post playbooks (but not the actual
910 # playbook) when a pre-playbook fails.
911 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
912 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
913 self.waitUntilSettled()
914 build = self.getJobFromHistory('python27')
915 self.assertIsNone(build.result)
916 self.assertIn('RETRY_LIMIT', A.messages[0])
917 flag_path = os.path.join(self.test_root, build.uuid +
918 '.main.flag')
919 self.assertFalse(os.path.exists(flag_path))
920 pre_flag_path = os.path.join(self.test_root, build.uuid +
921 '.pre.flag')
922 self.assertFalse(os.path.exists(pre_flag_path))
923 post_flag_path = os.path.join(self.test_root, build.uuid +
924 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -0700925 self.assertTrue(os.path.exists(post_flag_path),
926 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -0700927
928
James E. Blairb9c0d772017-03-03 14:34:49 -0800929class TestBrokenConfig(ZuulTestCase):
930 # Test that we get an appropriate syntax error if we start with a
931 # broken config.
932
933 tenant_config_file = 'config/broken/main.yaml'
934
935 def setUp(self):
936 with testtools.ExpectedException(
937 zuul.configloader.ConfigurationSyntaxError,
938 "\nZuul encountered a syntax error"):
939 super(TestBrokenConfig, self).setUp()
940
941 def test_broken_config_on_startup(self):
942 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000943
944
945class TestProjectKeys(ZuulTestCase):
946 # Test that we can generate project keys
947
948 # Normally the test infrastructure copies a static key in place
949 # for each project before starting tests. This saves time because
950 # Zuul's automatic key-generation on startup can be slow. To make
951 # sure we exercise that code, in this test we allow Zuul to create
952 # keys for the project on startup.
953 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +0200954 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000955 tenant_config_file = 'config/in-repo/main.yaml'
956
957 def test_key_generation(self):
958 key_root = os.path.join(self.state_root, 'keys')
959 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
960 # Make sure that a proper key was created on startup
961 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -0700962 private_key, public_key = \
963 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000964
965 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
966 fixture_private_key = i.read()
967
968 # Make sure that we didn't just end up with the static fixture
969 # key
970 self.assertNotEqual(fixture_private_key, private_key)
971
972 # Make sure it's the right length
973 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -0700974
975
James E. Blairbb94dfa2017-07-11 07:45:19 -0700976class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -0700977 def _assertRolePath(self, build, playbook, content):
978 path = os.path.join(self.test_root, build.uuid,
979 'ansible', playbook, 'ansible.cfg')
980 roles_paths = []
981 with open(path) as f:
982 for line in f:
983 if line.startswith('roles_path'):
984 roles_paths.append(line)
985 print(roles_paths)
986 if content:
987 self.assertEqual(len(roles_paths), 1,
988 "Should have one roles_path line in %s" %
989 (playbook,))
990 self.assertIn(content, roles_paths[0])
991 else:
992 self.assertEqual(len(roles_paths), 0,
993 "Should have no roles_path line in %s" %
994 (playbook,))
995
James E. Blairbb94dfa2017-07-11 07:45:19 -0700996
997class TestRoles(RoleTestCase):
998 tenant_config_file = 'config/roles/main.yaml'
999
James E. Blairbce76932017-05-04 10:03:15 -07001000 def test_role(self):
1001 # This exercises a proposed change to a role being checked out
1002 # and used.
1003 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1004 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1005 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1006 B.subject, A.data['id'])
1007 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1008 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1009 self.waitUntilSettled()
1010 self.assertHistory([
1011 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1012 ])
James E. Blair6459db12017-06-29 14:57:20 -07001013
James E. Blair1b27f6a2017-07-14 14:09:07 -07001014 def test_role_inheritance(self):
1015 self.executor_server.hold_jobs_in_build = True
1016 conf = textwrap.dedent(
1017 """
1018 - job:
1019 name: parent
1020 roles:
1021 - zuul: bare-role
1022 pre-run: playbooks/parent-pre
1023 post-run: playbooks/parent-post
1024
1025 - job:
1026 name: project-test
1027 parent: parent
1028 roles:
1029 - zuul: org/project
1030
1031 - project:
1032 name: org/project
1033 check:
1034 jobs:
1035 - project-test
1036 """)
1037
1038 file_dict = {'.zuul.yaml': conf}
1039 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1040 files=file_dict)
1041 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1042 self.waitUntilSettled()
1043
1044 self.assertEqual(len(self.builds), 1)
1045 build = self.getBuildByName('project-test')
1046 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1047 self._assertRolePath(build, 'playbook_0', 'role_0')
1048 self._assertRolePath(build, 'playbook_0', 'role_1')
1049 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1050
1051 self.executor_server.hold_jobs_in_build = False
1052 self.executor_server.release()
1053 self.waitUntilSettled()
1054
1055 self.assertHistory([
1056 dict(name='project-test', result='SUCCESS', changes='1,1'),
1057 ])
1058
James E. Blair6f699732017-07-18 14:19:11 -07001059 def test_role_error(self):
1060 conf = textwrap.dedent(
1061 """
1062 - job:
1063 name: project-test
1064 roles:
1065 - zuul: common-config
1066
1067 - project:
1068 name: org/project
1069 check:
1070 jobs:
1071 - project-test
1072 """)
1073
1074 file_dict = {'.zuul.yaml': conf}
1075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1076 files=file_dict)
1077 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1078 self.waitUntilSettled()
1079 self.assertIn(
1080 '- project-test project-test : ERROR Unable to find role',
1081 A.messages[-1])
1082
James E. Blair6459db12017-06-29 14:57:20 -07001083
James E. Blairbb94dfa2017-07-11 07:45:19 -07001084class TestImplicitRoles(RoleTestCase):
1085 tenant_config_file = 'config/implicit-roles/main.yaml'
1086
1087 def test_missing_roles(self):
1088 # Test implicit and explicit roles for a project which does
1089 # not have roles. The implicit role should be silently
1090 # ignored since the project doesn't supply roles, but if a
1091 # user declares an explicit role, it should error.
1092 self.executor_server.hold_jobs_in_build = True
1093 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1094 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1095 self.waitUntilSettled()
1096
1097 self.assertEqual(len(self.builds), 2)
1098 build = self.getBuildByName('implicit-role-fail')
1099 self._assertRolePath(build, 'playbook_0', None)
1100
1101 self.executor_server.hold_jobs_in_build = False
1102 self.executor_server.release()
1103 self.waitUntilSettled()
1104 # The retry_limit doesn't get recorded
1105 self.assertHistory([
1106 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1107 ])
1108
1109 def test_roles(self):
1110 # Test implicit and explicit roles for a project which does
1111 # have roles. In both cases, we should end up with the role
1112 # in the path. In the explicit case, ensure we end up with
1113 # the name we specified.
1114 self.executor_server.hold_jobs_in_build = True
1115 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1116 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1117 self.waitUntilSettled()
1118
1119 self.assertEqual(len(self.builds), 2)
1120 build = self.getBuildByName('implicit-role-ok')
1121 self._assertRolePath(build, 'playbook_0', 'role_0')
1122
1123 build = self.getBuildByName('explicit-role-ok')
1124 self._assertRolePath(build, 'playbook_0', 'role_0')
1125
1126 self.executor_server.hold_jobs_in_build = False
1127 self.executor_server.release()
1128 self.waitUntilSettled()
1129 self.assertHistory([
1130 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1131 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1132 ], ordered=False)
1133
1134
James E. Blair6459db12017-06-29 14:57:20 -07001135class TestShadow(ZuulTestCase):
1136 tenant_config_file = 'config/shadow/main.yaml'
1137
1138 def test_shadow(self):
1139 # Test that a repo is allowed to shadow another's job definitions.
1140 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1141 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1142 self.waitUntilSettled()
1143 self.assertHistory([
1144 dict(name='test1', result='SUCCESS', changes='1,1'),
1145 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001146 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001147
1148
1149class TestDataReturn(AnsibleZuulTestCase):
1150 tenant_config_file = 'config/data-return/main.yaml'
1151
1152 def test_data_return(self):
1153 # This exercises a proposed change to a role being checked out
1154 # and used.
1155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1156 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1157 self.waitUntilSettled()
1158 self.assertHistory([
1159 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001160 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1161 ], ordered=False)
1162 self.assertIn('- data-return http://example.com/test/log/url/',
1163 A.messages[-1])
1164 self.assertIn('- data-return-relative '
1165 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001166 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001167
1168
1169class TestDiskAccounting(AnsibleZuulTestCase):
1170 config_file = 'zuul-disk-accounting.conf'
1171 tenant_config_file = 'config/disk-accountant/main.yaml'
1172
1173 def test_disk_accountant_kills_job(self):
1174 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1175 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1176 self.waitUntilSettled()
1177 self.assertHistory([
1178 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001179
1180
1181class TestMaxNodesPerJob(AnsibleZuulTestCase):
1182 tenant_config_file = 'config/multi-tenant/main.yaml'
1183
1184 def test_max_nodes_reached(self):
1185 in_repo_conf = textwrap.dedent(
1186 """
1187 - job:
1188 name: test-job
1189 nodes:
1190 - name: node01
1191 label: fake
1192 - name: node02
1193 label: fake
1194 - name: node03
1195 label: fake
1196 - name: node04
1197 label: fake
1198 - name: node05
1199 label: fake
1200 - name: node06
1201 label: fake
1202 """)
1203 file_dict = {'.zuul.yaml': in_repo_conf}
1204 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1205 files=file_dict)
1206 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1207 self.waitUntilSettled()
1208 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1209 A.messages[0], "A should fail because of nodes limit")
1210
1211 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1212 files=file_dict)
1213 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1214 self.waitUntilSettled()
1215 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1216 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001217
1218
1219class TestBaseJobs(ZuulTestCase):
1220 tenant_config_file = 'config/base-jobs/main.yaml'
1221
1222 def test_multiple_base_jobs(self):
1223 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1224 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1225 self.waitUntilSettled()
1226 self.assertHistory([
1227 dict(name='my-job', result='SUCCESS', changes='1,1'),
1228 dict(name='other-job', result='SUCCESS', changes='1,1'),
1229 ], ordered=False)
1230 self.assertEqual(self.getJobFromHistory('my-job').
1231 parameters['zuul']['jobtags'],
1232 ['mybase'])
1233 self.assertEqual(self.getJobFromHistory('other-job').
1234 parameters['zuul']['jobtags'],
1235 ['otherbase'])
1236
1237 def test_untrusted_base_job(self):
1238 """Test that a base job may not be defined in an untrusted repo"""
1239 in_repo_conf = textwrap.dedent(
1240 """
1241 - job:
1242 name: fail-base
1243 parent: null
1244 """)
1245
1246 file_dict = {'.zuul.yaml': in_repo_conf}
1247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1248 files=file_dict)
1249 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1250 self.waitUntilSettled()
1251 self.assertEqual(A.reported, 1,
1252 "A should report failure")
1253 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1254 self.assertIn('Base jobs must be defined in config projects',
1255 A.messages[0])
1256 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001257
1258
1259class TestSecretLeaks(AnsibleZuulTestCase):
1260 tenant_config_file = 'config/secret-leaks/main.yaml'
1261
1262 def searchForContent(self, path, content):
1263 matches = []
1264 for (dirpath, dirnames, filenames) in os.walk(path):
1265 for filename in filenames:
1266 filepath = os.path.join(dirpath, filename)
1267 with open(filepath, 'rb') as f:
1268 if content in f.read():
1269 matches.append(filepath[len(path):])
1270 return matches
1271
1272 def _test_secret_file(self):
1273 # Or rather -- test that they *don't* leak.
1274 # Keep the jobdir around so we can inspect contents.
1275 self.executor_server.keep_jobdir = True
1276 conf = textwrap.dedent(
1277 """
1278 - project:
1279 name: org/project
1280 check:
1281 jobs:
1282 - secret-file
1283 """)
1284
1285 file_dict = {'.zuul.yaml': conf}
1286 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1287 files=file_dict)
1288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1289 self.waitUntilSettled()
1290 self.assertHistory([
1291 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1292 ], ordered=False)
1293 matches = self.searchForContent(self.history[0].jobdir.root,
1294 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001295 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001296 set(matches))
1297
1298 def test_secret_file(self):
1299 self._test_secret_file()
1300
1301 def test_secret_file_verbose(self):
1302 # Output extra ansible info to exercise alternate logging code
1303 # paths.
1304 self.executor_server.verbose = True
1305 self._test_secret_file()
1306
1307 def _test_secret_file_fail(self):
1308 # Or rather -- test that they *don't* leak.
1309 # Keep the jobdir around so we can inspect contents.
1310 self.executor_server.keep_jobdir = True
1311 conf = textwrap.dedent(
1312 """
1313 - project:
1314 name: org/project
1315 check:
1316 jobs:
1317 - secret-file-fail
1318 """)
1319
1320 file_dict = {'.zuul.yaml': conf}
1321 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1322 files=file_dict)
1323 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1324 self.waitUntilSettled()
1325 self.assertHistory([
1326 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1327 ], ordered=False)
1328 matches = self.searchForContent(self.history[0].jobdir.root,
1329 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001330 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001331 set(matches))
1332
1333 def test_secret_file_fail(self):
1334 self._test_secret_file_fail()
1335
1336 def test_secret_file_fail_verbose(self):
1337 # Output extra ansible info to exercise alternate logging code
1338 # paths.
1339 self.executor_server.verbose = True
1340 self._test_secret_file_fail()