blob: 9d695aaa8319b45c1be2a59c2d468edb19d9a4fc [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
Jesse Keating78f544a2017-07-13 14:27:40 -0700374 def test_dynamic_dependent_pipeline(self):
375 # Test dynamically adding a project to a
376 # dependent pipeline for the first time
377 self.executor_server.hold_jobs_in_build = True
378
379 tenant = self.sched.abide.tenants.get('tenant-one')
380 gate_pipeline = tenant.layout.pipelines['gate']
381
382 in_repo_conf = textwrap.dedent(
383 """
384 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200385 name: project-test1
386
387 - job:
Jesse Keating78f544a2017-07-13 14:27:40 -0700388 name: project-test2
389
390 - project:
391 name: org/project
392 gate:
393 jobs:
394 - project-test2
395 """)
396
397 in_repo_playbook = textwrap.dedent(
398 """
399 - hosts: all
400 tasks: []
401 """)
402
403 file_dict = {'.zuul.yaml': in_repo_conf,
404 'playbooks/project-test2.yaml': in_repo_playbook}
405 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
406 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200407 A.addApproval('Approved', 1)
408 self.fake_gerrit.addEvent(A.addApproval('Code-Review', 2))
Jesse Keating78f544a2017-07-13 14:27:40 -0700409 self.waitUntilSettled()
410
411 items = gate_pipeline.getAllItems()
412 self.assertEqual(items[0].change.number, '1')
413 self.assertEqual(items[0].change.patchset, '1')
414 self.assertTrue(items[0].live)
415
416 self.executor_server.hold_jobs_in_build = False
417 self.executor_server.release()
418 self.waitUntilSettled()
419
420 # Make sure the dynamic queue got cleaned up
421 self.assertEqual(gate_pipeline.queues, [])
422
James E. Blairff555742017-02-19 11:34:27 -0800423 def test_in_repo_branch(self):
424 in_repo_conf = textwrap.dedent(
425 """
426 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200427 name: project-test1
428
429 - job:
James E. Blairff555742017-02-19 11:34:27 -0800430 name: project-test2
431
432 - project:
433 name: org/project
434 tenant-one-gate:
435 jobs:
436 - project-test2
437 """)
438
439 in_repo_playbook = textwrap.dedent(
440 """
441 - hosts: all
442 tasks: []
443 """)
444
445 file_dict = {'.zuul.yaml': in_repo_conf,
446 'playbooks/project-test2.yaml': in_repo_playbook}
447 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700448 self.fake_gerrit.addEvent(
449 self.fake_gerrit.getFakeBranchCreatedEvent(
450 'org/project', 'stable'))
James E. Blairff555742017-02-19 11:34:27 -0800451 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
452 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200453 A.addApproval('Code-Review', 2)
454 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800455 self.waitUntilSettled()
456 self.assertEqual(A.data['status'], 'MERGED')
457 self.assertEqual(A.reported, 2,
458 "A should report start and success")
459 self.assertIn('tenant-one-gate', A.messages[1],
460 "A should transit tenant-one gate")
461 self.assertHistory([
462 dict(name='project-test2', result='SUCCESS', changes='1,1')])
463 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800464 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800465
466 # The config change should not affect master.
467 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200468 B.addApproval('Code-Review', 2)
469 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800470 self.waitUntilSettled()
471 self.assertHistory([
472 dict(name='project-test2', result='SUCCESS', changes='1,1'),
473 dict(name='project-test1', result='SUCCESS', changes='2,1')])
474
475 # The config change should be live for further changes on
476 # stable.
477 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200478 C.addApproval('Code-Review', 2)
479 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800480 self.waitUntilSettled()
481 self.assertHistory([
482 dict(name='project-test2', result='SUCCESS', changes='1,1'),
483 dict(name='project-test1', result='SUCCESS', changes='2,1'),
484 dict(name='project-test2', result='SUCCESS', changes='3,1')])
485
James E. Blaira5a12492017-05-03 11:40:48 -0700486 def test_crd_dynamic_config_branch(self):
487 # Test that we can create a job in one repo and be able to use
488 # it from a different branch on a different repo.
489
490 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700491 self.fake_gerrit.addEvent(
492 self.fake_gerrit.getFakeBranchCreatedEvent(
493 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700494
495 in_repo_conf = textwrap.dedent(
496 """
497 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200498 name: project-test1
499
500 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700501 name: project-test2
502
503 - project:
504 name: org/project
505 check:
506 jobs:
507 - project-test2
508 """)
509
510 in_repo_playbook = textwrap.dedent(
511 """
512 - hosts: all
513 tasks: []
514 """)
515
516 file_dict = {'.zuul.yaml': in_repo_conf,
517 'playbooks/project-test2.yaml': in_repo_playbook}
518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
519 files=file_dict)
520
521 second_repo_conf = textwrap.dedent(
522 """
523 - project:
524 name: org/project1
525 check:
526 jobs:
527 - project-test2
528 """)
529
530 second_file_dict = {'.zuul.yaml': second_repo_conf}
531 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
532 files=second_file_dict)
533 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
534 B.subject, A.data['id'])
535
536 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
537 self.waitUntilSettled()
538 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
539 self.waitUntilSettled()
540
541 self.assertEqual(A.reported, 1, "A should report")
542 self.assertHistory([
543 dict(name='project-test2', result='SUCCESS', changes='1,1'),
544 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
545 ])
546
James E. Blair149b69c2017-03-02 10:48:16 -0800547 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800548 in_repo_conf = textwrap.dedent(
549 """
550 - job:
551 name: project-test2
552 foo: error
553 """)
554
555 file_dict = {'.zuul.yaml': in_repo_conf}
556 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
557 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200558 A.addApproval('Code-Review', 2)
559 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800560 self.waitUntilSettled()
561
562 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200563 self.assertEqual(A.reported, 1,
564 "A should report failure")
565 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800566 "A should have a syntax error reported")
567
James E. Blair149b69c2017-03-02 10:48:16 -0800568 def test_trusted_syntax_error(self):
569 in_repo_conf = textwrap.dedent(
570 """
571 - job:
572 name: project-test2
573 foo: error
574 """)
575
576 file_dict = {'zuul.yaml': in_repo_conf}
577 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
578 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200579 A.addApproval('Code-Review', 2)
580 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800581 self.waitUntilSettled()
582
583 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200584 self.assertEqual(A.reported, 1,
585 "A should report failure")
586 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800587 "A should have a syntax error reported")
588
James E. Blair6f140c72017-03-03 10:32:07 -0800589 def test_untrusted_yaml_error(self):
590 in_repo_conf = textwrap.dedent(
591 """
592 - job:
593 foo: error
594 """)
595
596 file_dict = {'.zuul.yaml': in_repo_conf}
597 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
598 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200599 A.addApproval('Code-Review', 2)
600 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800601 self.waitUntilSettled()
602
603 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200604 self.assertEqual(A.reported, 1,
605 "A should report failure")
606 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800607 "A should have a syntax error reported")
608
James E. Blairdb04e6a2017-05-03 14:49:36 -0700609 def test_untrusted_shadow_error(self):
610 in_repo_conf = textwrap.dedent(
611 """
612 - job:
613 name: common-config-test
614 """)
615
616 file_dict = {'.zuul.yaml': in_repo_conf}
617 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
618 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200619 A.addApproval('Code-Review', 2)
620 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700621 self.waitUntilSettled()
622
623 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200624 self.assertEqual(A.reported, 1,
625 "A should report failure")
626 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700627 "A should have a syntax error reported")
628
James E. Blaird5656ad2017-06-02 14:29:41 -0700629 def test_untrusted_pipeline_error(self):
630 in_repo_conf = textwrap.dedent(
631 """
632 - pipeline:
633 name: test
634 """)
635
636 file_dict = {'.zuul.yaml': in_repo_conf}
637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
638 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200639 A.addApproval('Code-Review', 2)
640 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700641 self.waitUntilSettled()
642
643 self.assertEqual(A.data['status'], 'NEW')
644 self.assertEqual(A.reported, 1,
645 "A should report failure")
646 self.assertIn('Pipelines may not be defined', A.messages[0],
647 "A should have a syntax error reported")
648
649 def test_untrusted_project_error(self):
650 in_repo_conf = textwrap.dedent(
651 """
652 - project:
653 name: org/project1
654 """)
655
656 file_dict = {'.zuul.yaml': in_repo_conf}
657 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
658 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200659 A.addApproval('Code-Review', 2)
660 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700661 self.waitUntilSettled()
662
663 self.assertEqual(A.data['status'], 'NEW')
664 self.assertEqual(A.reported, 1,
665 "A should report failure")
666 self.assertIn('the only project definition permitted', A.messages[0],
667 "A should have a syntax error reported")
668
James E. Blaire64b0e42017-06-08 11:23:34 -0700669 def test_duplicate_node_error(self):
670 in_repo_conf = textwrap.dedent(
671 """
672 - nodeset:
673 name: duplicate
674 nodes:
675 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700676 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700677 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700678 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700679 """)
680
681 file_dict = {'.zuul.yaml': in_repo_conf}
682 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
683 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200684 A.addApproval('Code-Review', 2)
685 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700686 self.waitUntilSettled()
687
688 self.assertEqual(A.data['status'], 'NEW')
689 self.assertEqual(A.reported, 1,
690 "A should report failure")
691 self.assertIn('appears multiple times', A.messages[0],
692 "A should have a syntax error reported")
693
694 def test_duplicate_group_error(self):
695 in_repo_conf = textwrap.dedent(
696 """
697 - nodeset:
698 name: duplicate
699 nodes:
700 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700701 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700702 groups:
703 - name: group
704 nodes: compute
705 - name: group
706 nodes: compute
707 """)
708
709 file_dict = {'.zuul.yaml': in_repo_conf}
710 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
711 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200712 A.addApproval('Code-Review', 2)
713 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700714 self.waitUntilSettled()
715
716 self.assertEqual(A.data['status'], 'NEW')
717 self.assertEqual(A.reported, 1,
718 "A should report failure")
719 self.assertIn('appears multiple times', A.messages[0],
720 "A should have a syntax error reported")
721
James E. Blair09f9ffe2017-07-11 15:30:25 -0700722 def test_multi_repo(self):
723 downstream_repo_conf = textwrap.dedent(
724 """
725 - project:
726 name: org/project1
727 tenant-one-gate:
728 jobs:
729 - project-test1
730
731 - job:
732 name: project1-test1
733 parent: project-test1
734 """)
735
736 file_dict = {'.zuul.yaml': downstream_repo_conf}
737 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
738 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200739 A.addApproval('Code-Review', 2)
740 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700741 self.waitUntilSettled()
742
743 self.assertEqual(A.data['status'], 'MERGED')
744 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
745 self.waitUntilSettled()
746
747 upstream_repo_conf = textwrap.dedent(
748 """
749 - job:
750 name: project-test1
751
752 - job:
753 name: project-test2
754
755 - project:
756 name: org/project
757 tenant-one-gate:
758 jobs:
759 - project-test1
760 """)
761
762 file_dict = {'.zuul.yaml': upstream_repo_conf}
763 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
764 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200765 B.addApproval('Code-Review', 2)
766 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700767 self.waitUntilSettled()
768
769 self.assertEqual(B.data['status'], 'MERGED')
770 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
771 self.waitUntilSettled()
772
773 tenant = self.sched.abide.tenants.get('tenant-one')
774 # Ensure the latest change is reflected in the config; if it
775 # isn't this will raise an exception.
776 tenant.layout.getJob('project-test2')
777
James E. Blairc73c73a2017-01-20 15:15:15 -0800778
779class TestAnsible(AnsibleZuulTestCase):
780 # A temporary class to hold new tests while others are disabled
781
782 tenant_config_file = 'config/ansible/main.yaml'
783
784 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +1100785 # Keep the jobdir around so we can inspect contents if an
786 # assert fails.
787 self.executor_server.keep_jobdir = True
788 # Output extra ansible info so we might see errors.
789 self.executor_server.verbose = True
790 # Add a site variables file, used by check-vars
791 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
792 'variables.yaml')
793 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -0800794 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
795 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
796 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +0200797 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +1100798 with self.jobLog(build_timeout):
799 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200800 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +1100801 with self.jobLog(build_faillocal):
802 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200803 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +1100804 with self.jobLog(build_failpost):
805 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200806 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +1100807 with self.jobLog(build_check_vars):
808 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -0500809 build_check_secret_names = self.getJobFromHistory('check-secret-names')
810 with self.jobLog(build_check_secret_names):
811 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200812 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +1100813 with self.jobLog(build_hello):
814 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +0200815 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +1100816 with self.jobLog(build_python27):
817 self.assertEqual(build_python27.result, 'SUCCESS')
818 flag_path = os.path.join(self.test_root,
819 build_python27.uuid + '.flag')
820 self.assertTrue(os.path.exists(flag_path))
821 copied_path = os.path.join(self.test_root, build_python27.uuid +
822 '.copied')
823 self.assertTrue(os.path.exists(copied_path))
824 failed_path = os.path.join(self.test_root, build_python27.uuid +
825 '.failed')
826 self.assertFalse(os.path.exists(failed_path))
827 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
828 '.pre.flag')
829 self.assertTrue(os.path.exists(pre_flag_path))
830 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
831 '.post.flag')
832 self.assertTrue(os.path.exists(post_flag_path))
833 bare_role_flag_path = os.path.join(self.test_root,
834 build_python27.uuid +
835 '.bare-role.flag')
836 self.assertTrue(os.path.exists(bare_role_flag_path))
837 secrets_path = os.path.join(self.test_root,
838 build_python27.uuid + '.secrets')
839 with open(secrets_path) as f:
840 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -0800841
Jamie Lennox7655b552017-03-17 12:33:38 +1100842 msg = A.messages[0]
843 success = "{} https://success.example.com/zuul-logs/{}"
844 fail = "{} https://failure.example.com/zuul-logs/{}"
845 self.assertIn(success.format("python27", build_python27.uuid), msg)
846 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
847 self.assertIn(success.format("check-vars",
848 build_check_vars.uuid), msg)
849 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
850 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
851 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +0200852
James E. Blairabbaa6f2017-04-06 16:11:44 -0700853 def _add_job(self, job_name):
854 conf = textwrap.dedent(
855 """
856 - job:
857 name: %s
858
859 - project:
860 name: org/plugin-project
861 check:
862 jobs:
863 - %s
864 """ % (job_name, job_name))
865
866 file_dict = {'.zuul.yaml': conf}
867 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
868 files=file_dict)
869 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
870 self.waitUntilSettled()
871
872 def test_plugins(self):
873 # Keep the jobdir around so we can inspect contents if an
874 # assert fails.
875 self.executor_server.keep_jobdir = True
876 # Output extra ansible info so we might see errors.
877 self.executor_server.verbose = True
878
879 count = 0
880 plugin_tests = [
881 ('passwd', 'FAILURE'),
882 ('cartesian', 'SUCCESS'),
883 ('consul_kv', 'FAILURE'),
884 ('credstash', 'FAILURE'),
885 ('csvfile_good', 'SUCCESS'),
886 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -0500887 ('uri_bad_path', 'FAILURE'),
888 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -0500889 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -0500890 ('file_local_good', 'SUCCESS'),
891 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -0700892 ]
893 for job_name, result in plugin_tests:
894 count += 1
895 self._add_job(job_name)
896
897 job = self.getJobFromHistory(job_name)
898 with self.jobLog(job):
899 self.assertEqual(count, len(self.history))
900 build = self.history[-1]
901 self.assertEqual(build.result, result)
902
903 # TODOv3(jeblair): parse the ansible output and verify we're
904 # getting the exception we expect.
905
James E. Blairb9c0d772017-03-03 14:34:49 -0800906
James E. Blaira4d4eef2017-06-30 14:49:17 -0700907class TestPrePlaybooks(AnsibleZuulTestCase):
908 # A temporary class to hold new tests while others are disabled
909
910 tenant_config_file = 'config/pre-playbook/main.yaml'
911
912 def test_pre_playbook_fail(self):
913 # Test that we run the post playbooks (but not the actual
914 # playbook) when a pre-playbook fails.
915 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
916 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
917 self.waitUntilSettled()
918 build = self.getJobFromHistory('python27')
919 self.assertIsNone(build.result)
920 self.assertIn('RETRY_LIMIT', A.messages[0])
921 flag_path = os.path.join(self.test_root, build.uuid +
922 '.main.flag')
923 self.assertFalse(os.path.exists(flag_path))
924 pre_flag_path = os.path.join(self.test_root, build.uuid +
925 '.pre.flag')
926 self.assertFalse(os.path.exists(pre_flag_path))
927 post_flag_path = os.path.join(self.test_root, build.uuid +
928 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -0700929 self.assertTrue(os.path.exists(post_flag_path),
930 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -0700931
932
James E. Blairb9c0d772017-03-03 14:34:49 -0800933class TestBrokenConfig(ZuulTestCase):
934 # Test that we get an appropriate syntax error if we start with a
935 # broken config.
936
937 tenant_config_file = 'config/broken/main.yaml'
938
939 def setUp(self):
940 with testtools.ExpectedException(
941 zuul.configloader.ConfigurationSyntaxError,
942 "\nZuul encountered a syntax error"):
943 super(TestBrokenConfig, self).setUp()
944
945 def test_broken_config_on_startup(self):
946 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000947
948
949class TestProjectKeys(ZuulTestCase):
950 # Test that we can generate project keys
951
952 # Normally the test infrastructure copies a static key in place
953 # for each project before starting tests. This saves time because
954 # Zuul's automatic key-generation on startup can be slow. To make
955 # sure we exercise that code, in this test we allow Zuul to create
956 # keys for the project on startup.
957 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +0200958 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000959 tenant_config_file = 'config/in-repo/main.yaml'
960
961 def test_key_generation(self):
962 key_root = os.path.join(self.state_root, 'keys')
963 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
964 # Make sure that a proper key was created on startup
965 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -0700966 private_key, public_key = \
967 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000968
969 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
970 fixture_private_key = i.read()
971
972 # Make sure that we didn't just end up with the static fixture
973 # key
974 self.assertNotEqual(fixture_private_key, private_key)
975
976 # Make sure it's the right length
977 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -0700978
979
James E. Blairbb94dfa2017-07-11 07:45:19 -0700980class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -0700981 def _assertRolePath(self, build, playbook, content):
982 path = os.path.join(self.test_root, build.uuid,
983 'ansible', playbook, 'ansible.cfg')
984 roles_paths = []
985 with open(path) as f:
986 for line in f:
987 if line.startswith('roles_path'):
988 roles_paths.append(line)
989 print(roles_paths)
990 if content:
991 self.assertEqual(len(roles_paths), 1,
992 "Should have one roles_path line in %s" %
993 (playbook,))
994 self.assertIn(content, roles_paths[0])
995 else:
996 self.assertEqual(len(roles_paths), 0,
997 "Should have no roles_path line in %s" %
998 (playbook,))
999
James E. Blairbb94dfa2017-07-11 07:45:19 -07001000
1001class TestRoles(RoleTestCase):
1002 tenant_config_file = 'config/roles/main.yaml'
1003
James E. Blairbce76932017-05-04 10:03:15 -07001004 def test_role(self):
1005 # This exercises a proposed change to a role being checked out
1006 # and used.
1007 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1008 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1009 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1010 B.subject, A.data['id'])
1011 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1012 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1013 self.waitUntilSettled()
1014 self.assertHistory([
1015 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1016 ])
James E. Blair6459db12017-06-29 14:57:20 -07001017
James E. Blair1b27f6a2017-07-14 14:09:07 -07001018 def test_role_inheritance(self):
1019 self.executor_server.hold_jobs_in_build = True
1020 conf = textwrap.dedent(
1021 """
1022 - job:
1023 name: parent
1024 roles:
1025 - zuul: bare-role
1026 pre-run: playbooks/parent-pre
1027 post-run: playbooks/parent-post
1028
1029 - job:
1030 name: project-test
1031 parent: parent
1032 roles:
1033 - zuul: org/project
1034
1035 - project:
1036 name: org/project
1037 check:
1038 jobs:
1039 - project-test
1040 """)
1041
1042 file_dict = {'.zuul.yaml': conf}
1043 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1044 files=file_dict)
1045 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1046 self.waitUntilSettled()
1047
1048 self.assertEqual(len(self.builds), 1)
1049 build = self.getBuildByName('project-test')
1050 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1051 self._assertRolePath(build, 'playbook_0', 'role_0')
1052 self._assertRolePath(build, 'playbook_0', 'role_1')
1053 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1054
1055 self.executor_server.hold_jobs_in_build = False
1056 self.executor_server.release()
1057 self.waitUntilSettled()
1058
1059 self.assertHistory([
1060 dict(name='project-test', result='SUCCESS', changes='1,1'),
1061 ])
1062
James E. Blair6f699732017-07-18 14:19:11 -07001063 def test_role_error(self):
1064 conf = textwrap.dedent(
1065 """
1066 - job:
1067 name: project-test
1068 roles:
1069 - zuul: common-config
1070
1071 - project:
1072 name: org/project
1073 check:
1074 jobs:
1075 - project-test
1076 """)
1077
1078 file_dict = {'.zuul.yaml': conf}
1079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1080 files=file_dict)
1081 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1082 self.waitUntilSettled()
1083 self.assertIn(
1084 '- project-test project-test : ERROR Unable to find role',
1085 A.messages[-1])
1086
James E. Blair6459db12017-06-29 14:57:20 -07001087
James E. Blairbb94dfa2017-07-11 07:45:19 -07001088class TestImplicitRoles(RoleTestCase):
1089 tenant_config_file = 'config/implicit-roles/main.yaml'
1090
1091 def test_missing_roles(self):
1092 # Test implicit and explicit roles for a project which does
1093 # not have roles. The implicit role should be silently
1094 # ignored since the project doesn't supply roles, but if a
1095 # user declares an explicit role, it should error.
1096 self.executor_server.hold_jobs_in_build = True
1097 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1098 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1099 self.waitUntilSettled()
1100
1101 self.assertEqual(len(self.builds), 2)
1102 build = self.getBuildByName('implicit-role-fail')
1103 self._assertRolePath(build, 'playbook_0', None)
1104
1105 self.executor_server.hold_jobs_in_build = False
1106 self.executor_server.release()
1107 self.waitUntilSettled()
1108 # The retry_limit doesn't get recorded
1109 self.assertHistory([
1110 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1111 ])
1112
1113 def test_roles(self):
1114 # Test implicit and explicit roles for a project which does
1115 # have roles. In both cases, we should end up with the role
1116 # in the path. In the explicit case, ensure we end up with
1117 # the name we specified.
1118 self.executor_server.hold_jobs_in_build = True
1119 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1120 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1121 self.waitUntilSettled()
1122
1123 self.assertEqual(len(self.builds), 2)
1124 build = self.getBuildByName('implicit-role-ok')
1125 self._assertRolePath(build, 'playbook_0', 'role_0')
1126
1127 build = self.getBuildByName('explicit-role-ok')
1128 self._assertRolePath(build, 'playbook_0', 'role_0')
1129
1130 self.executor_server.hold_jobs_in_build = False
1131 self.executor_server.release()
1132 self.waitUntilSettled()
1133 self.assertHistory([
1134 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1135 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1136 ], ordered=False)
1137
1138
James E. Blair6459db12017-06-29 14:57:20 -07001139class TestShadow(ZuulTestCase):
1140 tenant_config_file = 'config/shadow/main.yaml'
1141
1142 def test_shadow(self):
1143 # Test that a repo is allowed to shadow another's job definitions.
1144 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1145 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1146 self.waitUntilSettled()
1147 self.assertHistory([
1148 dict(name='test1', result='SUCCESS', changes='1,1'),
1149 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001150 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001151
1152
1153class TestDataReturn(AnsibleZuulTestCase):
1154 tenant_config_file = 'config/data-return/main.yaml'
1155
1156 def test_data_return(self):
1157 # This exercises a proposed change to a role being checked out
1158 # and used.
1159 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1160 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1161 self.waitUntilSettled()
1162 self.assertHistory([
1163 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001164 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1165 ], ordered=False)
1166 self.assertIn('- data-return http://example.com/test/log/url/',
1167 A.messages[-1])
1168 self.assertIn('- data-return-relative '
1169 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001170 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001171
1172
1173class TestDiskAccounting(AnsibleZuulTestCase):
1174 config_file = 'zuul-disk-accounting.conf'
1175 tenant_config_file = 'config/disk-accountant/main.yaml'
1176
1177 def test_disk_accountant_kills_job(self):
1178 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1179 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1180 self.waitUntilSettled()
1181 self.assertHistory([
1182 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001183
1184
1185class TestMaxNodesPerJob(AnsibleZuulTestCase):
1186 tenant_config_file = 'config/multi-tenant/main.yaml'
1187
1188 def test_max_nodes_reached(self):
1189 in_repo_conf = textwrap.dedent(
1190 """
1191 - job:
1192 name: test-job
1193 nodes:
1194 - name: node01
1195 label: fake
1196 - name: node02
1197 label: fake
1198 - name: node03
1199 label: fake
1200 - name: node04
1201 label: fake
1202 - name: node05
1203 label: fake
1204 - name: node06
1205 label: fake
1206 """)
1207 file_dict = {'.zuul.yaml': in_repo_conf}
1208 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1209 files=file_dict)
1210 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1211 self.waitUntilSettled()
1212 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1213 A.messages[0], "A should fail because of nodes limit")
1214
1215 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1216 files=file_dict)
1217 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1218 self.waitUntilSettled()
1219 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1220 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001221
1222
1223class TestBaseJobs(ZuulTestCase):
1224 tenant_config_file = 'config/base-jobs/main.yaml'
1225
1226 def test_multiple_base_jobs(self):
1227 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1228 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1229 self.waitUntilSettled()
1230 self.assertHistory([
1231 dict(name='my-job', result='SUCCESS', changes='1,1'),
1232 dict(name='other-job', result='SUCCESS', changes='1,1'),
1233 ], ordered=False)
1234 self.assertEqual(self.getJobFromHistory('my-job').
1235 parameters['zuul']['jobtags'],
1236 ['mybase'])
1237 self.assertEqual(self.getJobFromHistory('other-job').
1238 parameters['zuul']['jobtags'],
1239 ['otherbase'])
1240
1241 def test_untrusted_base_job(self):
1242 """Test that a base job may not be defined in an untrusted repo"""
1243 in_repo_conf = textwrap.dedent(
1244 """
1245 - job:
1246 name: fail-base
1247 parent: null
1248 """)
1249
1250 file_dict = {'.zuul.yaml': in_repo_conf}
1251 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1252 files=file_dict)
1253 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1254 self.waitUntilSettled()
1255 self.assertEqual(A.reported, 1,
1256 "A should report failure")
1257 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1258 self.assertIn('Base jobs must be defined in config projects',
1259 A.messages[0])
1260 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001261
1262
1263class TestSecretLeaks(AnsibleZuulTestCase):
1264 tenant_config_file = 'config/secret-leaks/main.yaml'
1265
1266 def searchForContent(self, path, content):
1267 matches = []
1268 for (dirpath, dirnames, filenames) in os.walk(path):
1269 for filename in filenames:
1270 filepath = os.path.join(dirpath, filename)
1271 with open(filepath, 'rb') as f:
1272 if content in f.read():
1273 matches.append(filepath[len(path):])
1274 return matches
1275
1276 def _test_secret_file(self):
1277 # Or rather -- test that they *don't* leak.
1278 # Keep the jobdir around so we can inspect contents.
1279 self.executor_server.keep_jobdir = True
1280 conf = textwrap.dedent(
1281 """
1282 - project:
1283 name: org/project
1284 check:
1285 jobs:
1286 - secret-file
1287 """)
1288
1289 file_dict = {'.zuul.yaml': conf}
1290 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1291 files=file_dict)
1292 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1293 self.waitUntilSettled()
1294 self.assertHistory([
1295 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1296 ], ordered=False)
1297 matches = self.searchForContent(self.history[0].jobdir.root,
1298 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001299 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001300 set(matches))
1301
1302 def test_secret_file(self):
1303 self._test_secret_file()
1304
1305 def test_secret_file_verbose(self):
1306 # Output extra ansible info to exercise alternate logging code
1307 # paths.
1308 self.executor_server.verbose = True
1309 self._test_secret_file()
1310
1311 def _test_secret_file_fail(self):
1312 # Or rather -- test that they *don't* leak.
1313 # Keep the jobdir around so we can inspect contents.
1314 self.executor_server.keep_jobdir = True
1315 conf = textwrap.dedent(
1316 """
1317 - project:
1318 name: org/project
1319 check:
1320 jobs:
1321 - secret-file-fail
1322 """)
1323
1324 file_dict = {'.zuul.yaml': conf}
1325 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1326 files=file_dict)
1327 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1328 self.waitUntilSettled()
1329 self.assertHistory([
1330 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1331 ], ordered=False)
1332 matches = self.searchForContent(self.history[0].jobdir.root,
1333 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001334 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001335 set(matches))
1336
1337 def test_secret_file_fail(self):
1338 self._test_secret_file_fail()
1339
1340 def test_secret_file_fail_verbose(self):
1341 # Output extra ansible info to exercise alternate logging code
1342 # paths.
1343 self.executor_server.verbose = True
1344 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001345
1346
1347class TestJobOutput(AnsibleZuulTestCase):
1348 tenant_config_file = 'config/job-output/main.yaml'
1349
1350 def _get_file(self, build, path):
1351 p = os.path.join(build.jobdir.root, path)
1352 with open(p) as f:
1353 return f.read()
1354
1355 def test_job_output(self):
1356 # Verify that command standard output appears in the job output
1357
1358 # This currently only verifies we receive output from
1359 # localhost. Notably, it does not verify we receive output
1360 # via zuul_console streaming.
1361 self.executor_server.keep_jobdir = True
1362 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1363 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1364 self.waitUntilSettled()
1365 self.assertHistory([
1366 dict(name='job-output', result='SUCCESS', changes='1,1'),
1367 ], ordered=False)
1368
1369 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1370 j = json.loads(self._get_file(self.history[0],
1371 'work/logs/job-output.json'))
1372 self.assertEqual(token,
1373 j[0]['plays'][0]['tasks'][0]
1374 ['hosts']['localhost']['stdout'])
1375
1376 print(self._get_file(self.history[0],
1377 'work/logs/job-output.txt'))
1378 self.assertIn(token,
1379 self._get_file(self.history[0],
1380 'work/logs/job-output.txt'))