blob: 2b27b0ec16040bdb245e080a226cacdbd6e2e000 [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. Blair97043882017-09-06 15:51:17 -0700498 def test_yaml_list_error(self):
499 in_repo_conf = textwrap.dedent(
500 """
501 job: foo
502 """)
503
504 file_dict = {'.zuul.yaml': in_repo_conf}
505 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
506 files=file_dict)
507 A.addApproval('Code-Review', 2)
508 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
509 self.waitUntilSettled()
510
511 self.assertEqual(A.data['status'], 'NEW')
512 self.assertEqual(A.reported, 1,
513 "A should report failure")
514 self.assertIn('not a list', A.messages[0],
515 "A should have a syntax error reported")
516
517 def test_yaml_dict_error(self):
518 in_repo_conf = textwrap.dedent(
519 """
520 - job
521 """)
522
523 file_dict = {'.zuul.yaml': in_repo_conf}
524 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
525 files=file_dict)
526 A.addApproval('Code-Review', 2)
527 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
528 self.waitUntilSettled()
529
530 self.assertEqual(A.data['status'], 'NEW')
531 self.assertEqual(A.reported, 1,
532 "A should report failure")
533 self.assertIn('not a dictionary', A.messages[0],
534 "A should have a syntax error reported")
535
536 def test_yaml_key_error(self):
537 in_repo_conf = textwrap.dedent(
538 """
539 - job:
540 name: project-test2
541 """)
542
543 file_dict = {'.zuul.yaml': in_repo_conf}
544 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
545 files=file_dict)
546 A.addApproval('Code-Review', 2)
547 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
548 self.waitUntilSettled()
549
550 self.assertEqual(A.data['status'], 'NEW')
551 self.assertEqual(A.reported, 1,
552 "A should report failure")
553 self.assertIn('has more than one key', A.messages[0],
554 "A should have a syntax error reported")
555
556 def test_yaml_unknown_error(self):
557 in_repo_conf = textwrap.dedent(
558 """
559 - foobar:
560 foo: bar
561 """)
562
563 file_dict = {'.zuul.yaml': in_repo_conf}
564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
565 files=file_dict)
566 A.addApproval('Code-Review', 2)
567 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
568 self.waitUntilSettled()
569
570 self.assertEqual(A.data['status'], 'NEW')
571 self.assertEqual(A.reported, 1,
572 "A should report failure")
573 self.assertIn('not recognized', A.messages[0],
574 "A should have a syntax error reported")
575
James E. Blair149b69c2017-03-02 10:48:16 -0800576 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800577 in_repo_conf = textwrap.dedent(
578 """
579 - job:
580 name: project-test2
581 foo: error
582 """)
583
584 file_dict = {'.zuul.yaml': in_repo_conf}
585 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
586 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200587 A.addApproval('Code-Review', 2)
588 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800589 self.waitUntilSettled()
590
591 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200592 self.assertEqual(A.reported, 1,
593 "A should report failure")
594 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800595 "A should have a syntax error reported")
596
James E. Blair149b69c2017-03-02 10:48:16 -0800597 def test_trusted_syntax_error(self):
598 in_repo_conf = textwrap.dedent(
599 """
600 - job:
601 name: project-test2
602 foo: error
603 """)
604
605 file_dict = {'zuul.yaml': in_repo_conf}
606 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
607 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200608 A.addApproval('Code-Review', 2)
609 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800610 self.waitUntilSettled()
611
612 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200613 self.assertEqual(A.reported, 1,
614 "A should report failure")
615 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800616 "A should have a syntax error reported")
617
James E. Blair6f140c72017-03-03 10:32:07 -0800618 def test_untrusted_yaml_error(self):
619 in_repo_conf = textwrap.dedent(
620 """
621 - job:
622 foo: error
623 """)
624
625 file_dict = {'.zuul.yaml': in_repo_conf}
626 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
627 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200628 A.addApproval('Code-Review', 2)
629 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800630 self.waitUntilSettled()
631
632 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200633 self.assertEqual(A.reported, 1,
634 "A should report failure")
635 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800636 "A should have a syntax error reported")
637
James E. Blairdb04e6a2017-05-03 14:49:36 -0700638 def test_untrusted_shadow_error(self):
639 in_repo_conf = textwrap.dedent(
640 """
641 - job:
642 name: common-config-test
643 """)
644
645 file_dict = {'.zuul.yaml': in_repo_conf}
646 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
647 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200648 A.addApproval('Code-Review', 2)
649 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700650 self.waitUntilSettled()
651
652 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200653 self.assertEqual(A.reported, 1,
654 "A should report failure")
655 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700656 "A should have a syntax error reported")
657
James E. Blaird5656ad2017-06-02 14:29:41 -0700658 def test_untrusted_pipeline_error(self):
659 in_repo_conf = textwrap.dedent(
660 """
661 - pipeline:
662 name: test
663 """)
664
665 file_dict = {'.zuul.yaml': in_repo_conf}
666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
667 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200668 A.addApproval('Code-Review', 2)
669 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700670 self.waitUntilSettled()
671
672 self.assertEqual(A.data['status'], 'NEW')
673 self.assertEqual(A.reported, 1,
674 "A should report failure")
675 self.assertIn('Pipelines may not be defined', A.messages[0],
676 "A should have a syntax error reported")
677
678 def test_untrusted_project_error(self):
679 in_repo_conf = textwrap.dedent(
680 """
681 - project:
682 name: org/project1
683 """)
684
685 file_dict = {'.zuul.yaml': in_repo_conf}
686 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
687 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200688 A.addApproval('Code-Review', 2)
689 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700690 self.waitUntilSettled()
691
692 self.assertEqual(A.data['status'], 'NEW')
693 self.assertEqual(A.reported, 1,
694 "A should report failure")
695 self.assertIn('the only project definition permitted', A.messages[0],
696 "A should have a syntax error reported")
697
James E. Blaire64b0e42017-06-08 11:23:34 -0700698 def test_duplicate_node_error(self):
699 in_repo_conf = textwrap.dedent(
700 """
701 - nodeset:
702 name: duplicate
703 nodes:
704 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700705 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700706 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700707 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700708 """)
709
710 file_dict = {'.zuul.yaml': in_repo_conf}
711 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
712 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200713 A.addApproval('Code-Review', 2)
714 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700715 self.waitUntilSettled()
716
717 self.assertEqual(A.data['status'], 'NEW')
718 self.assertEqual(A.reported, 1,
719 "A should report failure")
720 self.assertIn('appears multiple times', A.messages[0],
721 "A should have a syntax error reported")
722
723 def test_duplicate_group_error(self):
724 in_repo_conf = textwrap.dedent(
725 """
726 - nodeset:
727 name: duplicate
728 nodes:
729 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700730 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700731 groups:
732 - name: group
733 nodes: compute
734 - name: group
735 nodes: compute
736 """)
737
738 file_dict = {'.zuul.yaml': in_repo_conf}
739 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
740 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200741 A.addApproval('Code-Review', 2)
742 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700743 self.waitUntilSettled()
744
745 self.assertEqual(A.data['status'], 'NEW')
746 self.assertEqual(A.reported, 1,
747 "A should report failure")
748 self.assertIn('appears multiple times', A.messages[0],
749 "A should have a syntax error reported")
750
James E. Blair09f9ffe2017-07-11 15:30:25 -0700751 def test_multi_repo(self):
752 downstream_repo_conf = textwrap.dedent(
753 """
754 - project:
755 name: org/project1
756 tenant-one-gate:
757 jobs:
758 - project-test1
759
760 - job:
761 name: project1-test1
762 parent: project-test1
763 """)
764
765 file_dict = {'.zuul.yaml': downstream_repo_conf}
766 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
767 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200768 A.addApproval('Code-Review', 2)
769 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700770 self.waitUntilSettled()
771
772 self.assertEqual(A.data['status'], 'MERGED')
773 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
774 self.waitUntilSettled()
775
776 upstream_repo_conf = textwrap.dedent(
777 """
778 - job:
779 name: project-test1
780
781 - job:
782 name: project-test2
783
784 - project:
785 name: org/project
786 tenant-one-gate:
787 jobs:
788 - project-test1
789 """)
790
791 file_dict = {'.zuul.yaml': upstream_repo_conf}
792 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
793 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200794 B.addApproval('Code-Review', 2)
795 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700796 self.waitUntilSettled()
797
798 self.assertEqual(B.data['status'], 'MERGED')
799 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
800 self.waitUntilSettled()
801
802 tenant = self.sched.abide.tenants.get('tenant-one')
803 # Ensure the latest change is reflected in the config; if it
804 # isn't this will raise an exception.
805 tenant.layout.getJob('project-test2')
806
James E. Blair332636e2017-09-05 10:14:35 -0700807 def test_pipeline_error(self):
808 with open(os.path.join(FIXTURE_DIR,
809 'config/in-repo/git/',
810 'common-config/zuul.yaml')) as f:
811 base_common_config = f.read()
812
813 in_repo_conf_A = textwrap.dedent(
814 """
815 - pipeline:
816 name: periodic
817 foo: error
818 """)
819
820 file_dict = {'zuul.yaml': None,
821 'zuul.d/main.yaml': base_common_config,
822 'zuul.d/test1.yaml': in_repo_conf_A}
823 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
824 files=file_dict)
825 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
826 self.waitUntilSettled()
827 self.assertEqual(A.reported, 1,
828 "A should report failure")
829 self.assertIn('syntax error',
830 A.messages[0],
831 "A should have an error reported")
832
833 def test_change_series_error(self):
834 with open(os.path.join(FIXTURE_DIR,
835 'config/in-repo/git/',
836 'common-config/zuul.yaml')) as f:
837 base_common_config = f.read()
838
839 in_repo_conf_A = textwrap.dedent(
840 """
841 - pipeline:
842 name: periodic
843 foo: error
844 """)
845
846 file_dict = {'zuul.yaml': None,
847 'zuul.d/main.yaml': base_common_config,
848 'zuul.d/test1.yaml': in_repo_conf_A}
849 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
850 files=file_dict)
851
852 in_repo_conf_B = textwrap.dedent(
853 """
854 - job:
855 name: project-test2
856 foo: error
857 """)
858
859 file_dict = {'zuul.yaml': None,
860 'zuul.d/main.yaml': base_common_config,
861 'zuul.d/test1.yaml': in_repo_conf_A,
862 'zuul.d/test2.yaml': in_repo_conf_B}
863 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
864 files=file_dict)
865 B.setDependsOn(A, 1)
866 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
867 C.setDependsOn(B, 1)
868 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
869 self.waitUntilSettled()
870
871 self.assertEqual(C.reported, 1,
872 "C should report failure")
873 self.assertIn('depends on a change that failed to merge',
874 C.messages[0],
875 "C should have an error reported")
876
James E. Blairc73c73a2017-01-20 15:15:15 -0800877
James E. Blairc9455002017-09-06 09:22:19 -0700878class TestInRepoJoin(ZuulTestCase):
879 # In this config, org/project is not a member of any pipelines, so
880 # that we may test the changes that cause it to join them.
881
882 tenant_config_file = 'config/in-repo-join/main.yaml'
883
884 def test_dynamic_dependent_pipeline(self):
885 # Test dynamically adding a project to a
886 # dependent pipeline for the first time
887 self.executor_server.hold_jobs_in_build = True
888
889 tenant = self.sched.abide.tenants.get('tenant-one')
890 gate_pipeline = tenant.layout.pipelines['gate']
891
892 in_repo_conf = textwrap.dedent(
893 """
894 - job:
895 name: project-test1
896
897 - job:
898 name: project-test2
899
900 - project:
901 name: org/project
902 gate:
903 jobs:
904 - project-test2
905 """)
906
907 in_repo_playbook = textwrap.dedent(
908 """
909 - hosts: all
910 tasks: []
911 """)
912
913 file_dict = {'.zuul.yaml': in_repo_conf,
914 'playbooks/project-test2.yaml': in_repo_playbook}
915 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
916 files=file_dict)
917 A.addApproval('Code-Review', 2)
918 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
919 self.waitUntilSettled()
920
921 items = gate_pipeline.getAllItems()
922 self.assertEqual(items[0].change.number, '1')
923 self.assertEqual(items[0].change.patchset, '1')
924 self.assertTrue(items[0].live)
925
926 self.executor_server.hold_jobs_in_build = False
927 self.executor_server.release()
928 self.waitUntilSettled()
929
930 # Make sure the dynamic queue got cleaned up
931 self.assertEqual(gate_pipeline.queues, [])
932
933 def test_dynamic_dependent_pipeline_failure(self):
934 # Test that a change behind a failing change adding a project
935 # to a dependent pipeline is dequeued.
936 self.executor_server.hold_jobs_in_build = True
937
938 in_repo_conf = textwrap.dedent(
939 """
940 - job:
941 name: project-test1
942
943 - project:
944 name: org/project
945 gate:
946 jobs:
947 - project-test1
948 """)
949
950 file_dict = {'.zuul.yaml': in_repo_conf}
951 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
952 files=file_dict)
953 self.executor_server.failJob('project-test1', A)
954 A.addApproval('Code-Review', 2)
955 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
956 self.waitUntilSettled()
957
958 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
959 B.addApproval('Code-Review', 2)
960 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
961 self.waitUntilSettled()
962
James E. Blair3490c5d2017-09-07 08:33:23 -0700963 self.orderedRelease()
James E. Blairc9455002017-09-06 09:22:19 -0700964 self.waitUntilSettled()
965 self.assertEqual(A.reported, 2,
966 "A should report start and failure")
967 self.assertEqual(A.data['status'], 'NEW')
968 self.assertEqual(B.reported, 1,
969 "B should report start")
970 self.assertHistory([
971 dict(name='project-test1', result='FAILURE', changes='1,1'),
James E. Blair3490c5d2017-09-07 08:33:23 -0700972 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
James E. Blairc9455002017-09-06 09:22:19 -0700973 ], ordered=False)
974
James E. Blair0af198f2017-09-06 09:52:35 -0700975 def test_dynamic_dependent_pipeline_absent(self):
976 # Test that a series of dependent changes don't report merge
977 # failures to a pipeline they aren't in.
978 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
979 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
980 B.setDependsOn(A, 1)
981
982 A.addApproval('Code-Review', 2)
983 A.addApproval('Approved', 1)
984 B.addApproval('Code-Review', 2)
985 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
986 self.waitUntilSettled()
987 self.assertEqual(A.reported, 0,
988 "A should not report")
989 self.assertEqual(A.data['status'], 'NEW')
990 self.assertEqual(B.reported, 0,
991 "B should not report")
992 self.assertEqual(B.data['status'], 'NEW')
993 self.assertHistory([])
994
James E. Blairc9455002017-09-06 09:22:19 -0700995
James E. Blairc73c73a2017-01-20 15:15:15 -0800996class TestAnsible(AnsibleZuulTestCase):
997 # A temporary class to hold new tests while others are disabled
998
999 tenant_config_file = 'config/ansible/main.yaml'
1000
1001 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +11001002 # Keep the jobdir around so we can inspect contents if an
1003 # assert fails.
1004 self.executor_server.keep_jobdir = True
1005 # Output extra ansible info so we might see errors.
1006 self.executor_server.verbose = True
1007 # Add a site variables file, used by check-vars
1008 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
1009 'variables.yaml')
1010 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -08001011 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1012 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1013 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +02001014 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +11001015 with self.jobLog(build_timeout):
1016 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001017 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +11001018 with self.jobLog(build_faillocal):
1019 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001020 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +11001021 with self.jobLog(build_failpost):
1022 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001023 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +11001024 with self.jobLog(build_check_vars):
1025 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -05001026 build_check_secret_names = self.getJobFromHistory('check-secret-names')
1027 with self.jobLog(build_check_secret_names):
1028 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001029 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +11001030 with self.jobLog(build_hello):
1031 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001032 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +11001033 with self.jobLog(build_python27):
1034 self.assertEqual(build_python27.result, 'SUCCESS')
1035 flag_path = os.path.join(self.test_root,
1036 build_python27.uuid + '.flag')
1037 self.assertTrue(os.path.exists(flag_path))
1038 copied_path = os.path.join(self.test_root, build_python27.uuid +
1039 '.copied')
1040 self.assertTrue(os.path.exists(copied_path))
1041 failed_path = os.path.join(self.test_root, build_python27.uuid +
1042 '.failed')
1043 self.assertFalse(os.path.exists(failed_path))
1044 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
1045 '.pre.flag')
1046 self.assertTrue(os.path.exists(pre_flag_path))
1047 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
1048 '.post.flag')
1049 self.assertTrue(os.path.exists(post_flag_path))
1050 bare_role_flag_path = os.path.join(self.test_root,
1051 build_python27.uuid +
1052 '.bare-role.flag')
1053 self.assertTrue(os.path.exists(bare_role_flag_path))
1054 secrets_path = os.path.join(self.test_root,
1055 build_python27.uuid + '.secrets')
1056 with open(secrets_path) as f:
1057 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -08001058
Jamie Lennox7655b552017-03-17 12:33:38 +11001059 msg = A.messages[0]
1060 success = "{} https://success.example.com/zuul-logs/{}"
1061 fail = "{} https://failure.example.com/zuul-logs/{}"
1062 self.assertIn(success.format("python27", build_python27.uuid), msg)
1063 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
1064 self.assertIn(success.format("check-vars",
1065 build_check_vars.uuid), msg)
1066 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
1067 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
1068 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +02001069
James E. Blairabbaa6f2017-04-06 16:11:44 -07001070 def _add_job(self, job_name):
1071 conf = textwrap.dedent(
1072 """
1073 - job:
1074 name: %s
1075
1076 - project:
1077 name: org/plugin-project
1078 check:
1079 jobs:
1080 - %s
1081 """ % (job_name, job_name))
1082
1083 file_dict = {'.zuul.yaml': conf}
1084 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1085 files=file_dict)
1086 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1087 self.waitUntilSettled()
1088
1089 def test_plugins(self):
1090 # Keep the jobdir around so we can inspect contents if an
1091 # assert fails.
1092 self.executor_server.keep_jobdir = True
1093 # Output extra ansible info so we might see errors.
1094 self.executor_server.verbose = True
1095
1096 count = 0
1097 plugin_tests = [
1098 ('passwd', 'FAILURE'),
1099 ('cartesian', 'SUCCESS'),
1100 ('consul_kv', 'FAILURE'),
1101 ('credstash', 'FAILURE'),
1102 ('csvfile_good', 'SUCCESS'),
1103 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001104 ('uri_bad_path', 'FAILURE'),
1105 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001106 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001107 ('file_local_good', 'SUCCESS'),
1108 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001109 ]
1110 for job_name, result in plugin_tests:
1111 count += 1
1112 self._add_job(job_name)
1113
1114 job = self.getJobFromHistory(job_name)
1115 with self.jobLog(job):
1116 self.assertEqual(count, len(self.history))
1117 build = self.history[-1]
1118 self.assertEqual(build.result, result)
1119
1120 # TODOv3(jeblair): parse the ansible output and verify we're
1121 # getting the exception we expect.
1122
James E. Blairb9c0d772017-03-03 14:34:49 -08001123
James E. Blaira4d4eef2017-06-30 14:49:17 -07001124class TestPrePlaybooks(AnsibleZuulTestCase):
1125 # A temporary class to hold new tests while others are disabled
1126
1127 tenant_config_file = 'config/pre-playbook/main.yaml'
1128
1129 def test_pre_playbook_fail(self):
1130 # Test that we run the post playbooks (but not the actual
1131 # playbook) when a pre-playbook fails.
1132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1133 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1134 self.waitUntilSettled()
1135 build = self.getJobFromHistory('python27')
1136 self.assertIsNone(build.result)
1137 self.assertIn('RETRY_LIMIT', A.messages[0])
1138 flag_path = os.path.join(self.test_root, build.uuid +
1139 '.main.flag')
1140 self.assertFalse(os.path.exists(flag_path))
1141 pre_flag_path = os.path.join(self.test_root, build.uuid +
1142 '.pre.flag')
1143 self.assertFalse(os.path.exists(pre_flag_path))
1144 post_flag_path = os.path.join(self.test_root, build.uuid +
1145 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001146 self.assertTrue(os.path.exists(post_flag_path),
1147 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001148
1149
James E. Blairb9c0d772017-03-03 14:34:49 -08001150class TestBrokenConfig(ZuulTestCase):
1151 # Test that we get an appropriate syntax error if we start with a
1152 # broken config.
1153
1154 tenant_config_file = 'config/broken/main.yaml'
1155
1156 def setUp(self):
1157 with testtools.ExpectedException(
1158 zuul.configloader.ConfigurationSyntaxError,
1159 "\nZuul encountered a syntax error"):
1160 super(TestBrokenConfig, self).setUp()
1161
1162 def test_broken_config_on_startup(self):
1163 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001164
1165
1166class TestProjectKeys(ZuulTestCase):
1167 # Test that we can generate project keys
1168
1169 # Normally the test infrastructure copies a static key in place
1170 # for each project before starting tests. This saves time because
1171 # Zuul's automatic key-generation on startup can be slow. To make
1172 # sure we exercise that code, in this test we allow Zuul to create
1173 # keys for the project on startup.
1174 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001175 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001176 tenant_config_file = 'config/in-repo/main.yaml'
1177
1178 def test_key_generation(self):
1179 key_root = os.path.join(self.state_root, 'keys')
1180 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1181 # Make sure that a proper key was created on startup
1182 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001183 private_key, public_key = \
1184 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001185
1186 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1187 fixture_private_key = i.read()
1188
1189 # Make sure that we didn't just end up with the static fixture
1190 # key
1191 self.assertNotEqual(fixture_private_key, private_key)
1192
1193 # Make sure it's the right length
1194 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001195
1196
James E. Blairbb94dfa2017-07-11 07:45:19 -07001197class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001198 def _assertRolePath(self, build, playbook, content):
1199 path = os.path.join(self.test_root, build.uuid,
1200 'ansible', playbook, 'ansible.cfg')
1201 roles_paths = []
1202 with open(path) as f:
1203 for line in f:
1204 if line.startswith('roles_path'):
1205 roles_paths.append(line)
1206 print(roles_paths)
1207 if content:
1208 self.assertEqual(len(roles_paths), 1,
1209 "Should have one roles_path line in %s" %
1210 (playbook,))
1211 self.assertIn(content, roles_paths[0])
1212 else:
1213 self.assertEqual(len(roles_paths), 0,
1214 "Should have no roles_path line in %s" %
1215 (playbook,))
1216
James E. Blairbb94dfa2017-07-11 07:45:19 -07001217
1218class TestRoles(RoleTestCase):
1219 tenant_config_file = 'config/roles/main.yaml'
1220
James E. Blairbce76932017-05-04 10:03:15 -07001221 def test_role(self):
1222 # This exercises a proposed change to a role being checked out
1223 # and used.
1224 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1225 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1226 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1227 B.subject, A.data['id'])
1228 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1229 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1230 self.waitUntilSettled()
1231 self.assertHistory([
1232 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1233 ])
James E. Blair6459db12017-06-29 14:57:20 -07001234
James E. Blair1b27f6a2017-07-14 14:09:07 -07001235 def test_role_inheritance(self):
1236 self.executor_server.hold_jobs_in_build = True
1237 conf = textwrap.dedent(
1238 """
1239 - job:
1240 name: parent
1241 roles:
1242 - zuul: bare-role
1243 pre-run: playbooks/parent-pre
1244 post-run: playbooks/parent-post
1245
1246 - job:
1247 name: project-test
1248 parent: parent
1249 roles:
1250 - zuul: org/project
1251
1252 - project:
1253 name: org/project
1254 check:
1255 jobs:
1256 - project-test
1257 """)
1258
1259 file_dict = {'.zuul.yaml': conf}
1260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1261 files=file_dict)
1262 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1263 self.waitUntilSettled()
1264
1265 self.assertEqual(len(self.builds), 1)
1266 build = self.getBuildByName('project-test')
1267 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1268 self._assertRolePath(build, 'playbook_0', 'role_0')
1269 self._assertRolePath(build, 'playbook_0', 'role_1')
1270 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1271
1272 self.executor_server.hold_jobs_in_build = False
1273 self.executor_server.release()
1274 self.waitUntilSettled()
1275
1276 self.assertHistory([
1277 dict(name='project-test', result='SUCCESS', changes='1,1'),
1278 ])
1279
James E. Blair6f699732017-07-18 14:19:11 -07001280 def test_role_error(self):
1281 conf = textwrap.dedent(
1282 """
1283 - job:
1284 name: project-test
1285 roles:
1286 - zuul: common-config
1287
1288 - project:
1289 name: org/project
1290 check:
1291 jobs:
1292 - project-test
1293 """)
1294
1295 file_dict = {'.zuul.yaml': conf}
1296 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1297 files=file_dict)
1298 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1299 self.waitUntilSettled()
1300 self.assertIn(
1301 '- project-test project-test : ERROR Unable to find role',
1302 A.messages[-1])
1303
James E. Blair6459db12017-06-29 14:57:20 -07001304
James E. Blairbb94dfa2017-07-11 07:45:19 -07001305class TestImplicitRoles(RoleTestCase):
1306 tenant_config_file = 'config/implicit-roles/main.yaml'
1307
1308 def test_missing_roles(self):
1309 # Test implicit and explicit roles for a project which does
1310 # not have roles. The implicit role should be silently
1311 # ignored since the project doesn't supply roles, but if a
1312 # user declares an explicit role, it should error.
1313 self.executor_server.hold_jobs_in_build = True
1314 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1315 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1316 self.waitUntilSettled()
1317
1318 self.assertEqual(len(self.builds), 2)
1319 build = self.getBuildByName('implicit-role-fail')
1320 self._assertRolePath(build, 'playbook_0', None)
1321
1322 self.executor_server.hold_jobs_in_build = False
1323 self.executor_server.release()
1324 self.waitUntilSettled()
1325 # The retry_limit doesn't get recorded
1326 self.assertHistory([
1327 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1328 ])
1329
1330 def test_roles(self):
1331 # Test implicit and explicit roles for a project which does
1332 # have roles. In both cases, we should end up with the role
1333 # in the path. In the explicit case, ensure we end up with
1334 # the name we specified.
1335 self.executor_server.hold_jobs_in_build = True
1336 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1337 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1338 self.waitUntilSettled()
1339
1340 self.assertEqual(len(self.builds), 2)
1341 build = self.getBuildByName('implicit-role-ok')
1342 self._assertRolePath(build, 'playbook_0', 'role_0')
1343
1344 build = self.getBuildByName('explicit-role-ok')
1345 self._assertRolePath(build, 'playbook_0', 'role_0')
1346
1347 self.executor_server.hold_jobs_in_build = False
1348 self.executor_server.release()
1349 self.waitUntilSettled()
1350 self.assertHistory([
1351 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1352 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1353 ], ordered=False)
1354
1355
James E. Blair6459db12017-06-29 14:57:20 -07001356class TestShadow(ZuulTestCase):
1357 tenant_config_file = 'config/shadow/main.yaml'
1358
1359 def test_shadow(self):
1360 # Test that a repo is allowed to shadow another's job definitions.
1361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1362 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1363 self.waitUntilSettled()
1364 self.assertHistory([
1365 dict(name='test1', result='SUCCESS', changes='1,1'),
1366 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001367 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001368
1369
1370class TestDataReturn(AnsibleZuulTestCase):
1371 tenant_config_file = 'config/data-return/main.yaml'
1372
1373 def test_data_return(self):
1374 # This exercises a proposed change to a role being checked out
1375 # and used.
1376 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1377 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1378 self.waitUntilSettled()
1379 self.assertHistory([
1380 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001381 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1382 ], ordered=False)
1383 self.assertIn('- data-return http://example.com/test/log/url/',
1384 A.messages[-1])
1385 self.assertIn('- data-return-relative '
1386 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001387 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001388
1389
1390class TestDiskAccounting(AnsibleZuulTestCase):
1391 config_file = 'zuul-disk-accounting.conf'
1392 tenant_config_file = 'config/disk-accountant/main.yaml'
1393
1394 def test_disk_accountant_kills_job(self):
1395 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1396 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1397 self.waitUntilSettled()
1398 self.assertHistory([
1399 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001400
1401
1402class TestMaxNodesPerJob(AnsibleZuulTestCase):
1403 tenant_config_file = 'config/multi-tenant/main.yaml'
1404
1405 def test_max_nodes_reached(self):
1406 in_repo_conf = textwrap.dedent(
1407 """
1408 - job:
1409 name: test-job
1410 nodes:
1411 - name: node01
1412 label: fake
1413 - name: node02
1414 label: fake
1415 - name: node03
1416 label: fake
1417 - name: node04
1418 label: fake
1419 - name: node05
1420 label: fake
1421 - name: node06
1422 label: fake
1423 """)
1424 file_dict = {'.zuul.yaml': in_repo_conf}
1425 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1426 files=file_dict)
1427 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1428 self.waitUntilSettled()
1429 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1430 A.messages[0], "A should fail because of nodes limit")
1431
1432 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1433 files=file_dict)
1434 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1435 self.waitUntilSettled()
1436 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1437 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001438
1439
1440class TestBaseJobs(ZuulTestCase):
1441 tenant_config_file = 'config/base-jobs/main.yaml'
1442
1443 def test_multiple_base_jobs(self):
1444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1445 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1446 self.waitUntilSettled()
1447 self.assertHistory([
1448 dict(name='my-job', result='SUCCESS', changes='1,1'),
1449 dict(name='other-job', result='SUCCESS', changes='1,1'),
1450 ], ordered=False)
1451 self.assertEqual(self.getJobFromHistory('my-job').
1452 parameters['zuul']['jobtags'],
1453 ['mybase'])
1454 self.assertEqual(self.getJobFromHistory('other-job').
1455 parameters['zuul']['jobtags'],
1456 ['otherbase'])
1457
1458 def test_untrusted_base_job(self):
1459 """Test that a base job may not be defined in an untrusted repo"""
1460 in_repo_conf = textwrap.dedent(
1461 """
1462 - job:
1463 name: fail-base
1464 parent: null
1465 """)
1466
1467 file_dict = {'.zuul.yaml': in_repo_conf}
1468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1469 files=file_dict)
1470 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1471 self.waitUntilSettled()
1472 self.assertEqual(A.reported, 1,
1473 "A should report failure")
1474 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1475 self.assertIn('Base jobs must be defined in config projects',
1476 A.messages[0])
1477 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001478
1479
1480class TestSecretLeaks(AnsibleZuulTestCase):
1481 tenant_config_file = 'config/secret-leaks/main.yaml'
1482
1483 def searchForContent(self, path, content):
1484 matches = []
1485 for (dirpath, dirnames, filenames) in os.walk(path):
1486 for filename in filenames:
1487 filepath = os.path.join(dirpath, filename)
1488 with open(filepath, 'rb') as f:
1489 if content in f.read():
1490 matches.append(filepath[len(path):])
1491 return matches
1492
1493 def _test_secret_file(self):
1494 # Or rather -- test that they *don't* leak.
1495 # Keep the jobdir around so we can inspect contents.
1496 self.executor_server.keep_jobdir = True
1497 conf = textwrap.dedent(
1498 """
1499 - project:
1500 name: org/project
1501 check:
1502 jobs:
1503 - secret-file
1504 """)
1505
1506 file_dict = {'.zuul.yaml': conf}
1507 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1508 files=file_dict)
1509 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1510 self.waitUntilSettled()
1511 self.assertHistory([
1512 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1513 ], ordered=False)
1514 matches = self.searchForContent(self.history[0].jobdir.root,
1515 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001516 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001517 set(matches))
1518
1519 def test_secret_file(self):
1520 self._test_secret_file()
1521
1522 def test_secret_file_verbose(self):
1523 # Output extra ansible info to exercise alternate logging code
1524 # paths.
1525 self.executor_server.verbose = True
1526 self._test_secret_file()
1527
1528 def _test_secret_file_fail(self):
1529 # Or rather -- test that they *don't* leak.
1530 # Keep the jobdir around so we can inspect contents.
1531 self.executor_server.keep_jobdir = True
1532 conf = textwrap.dedent(
1533 """
1534 - project:
1535 name: org/project
1536 check:
1537 jobs:
1538 - secret-file-fail
1539 """)
1540
1541 file_dict = {'.zuul.yaml': conf}
1542 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1543 files=file_dict)
1544 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1545 self.waitUntilSettled()
1546 self.assertHistory([
1547 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1548 ], ordered=False)
1549 matches = self.searchForContent(self.history[0].jobdir.root,
1550 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001551 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001552 set(matches))
1553
1554 def test_secret_file_fail(self):
1555 self._test_secret_file_fail()
1556
1557 def test_secret_file_fail_verbose(self):
1558 # Output extra ansible info to exercise alternate logging code
1559 # paths.
1560 self.executor_server.verbose = True
1561 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001562
1563
1564class TestJobOutput(AnsibleZuulTestCase):
1565 tenant_config_file = 'config/job-output/main.yaml'
1566
1567 def _get_file(self, build, path):
1568 p = os.path.join(build.jobdir.root, path)
1569 with open(p) as f:
1570 return f.read()
1571
1572 def test_job_output(self):
1573 # Verify that command standard output appears in the job output
1574
1575 # This currently only verifies we receive output from
1576 # localhost. Notably, it does not verify we receive output
1577 # via zuul_console streaming.
1578 self.executor_server.keep_jobdir = True
1579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1580 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1581 self.waitUntilSettled()
1582 self.assertHistory([
1583 dict(name='job-output', result='SUCCESS', changes='1,1'),
1584 ], ordered=False)
1585
1586 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1587 j = json.loads(self._get_file(self.history[0],
1588 'work/logs/job-output.json'))
1589 self.assertEqual(token,
1590 j[0]['plays'][0]['tasks'][0]
1591 ['hosts']['localhost']['stdout'])
1592
1593 print(self._get_file(self.history[0],
1594 'work/logs/job-output.txt'))
1595 self.assertIn(token,
1596 self._get_file(self.history[0],
1597 'work/logs/job-output.txt'))