blob: 2b865cf110cad7858c5e4298f40d61aea9100ff8 [file] [log] [blame]
James E. Blair59fdbac2015-12-07 17:08:06 -08001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira92cbc82017-01-23 14:56:49 -080017import os
James E. Blair14abdf42015-12-09 16:11:53 -080018import textwrap
James E. Blair59fdbac2015-12-07 17:08:06 -080019
James E. Blairb9c0d772017-03-03 14:34:49 -080020import testtools
21
22import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070023from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000024from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080025
James E. Blair59fdbac2015-12-07 17:08:06 -080026
James E. Blair3f876d52016-07-22 13:07:14 -070027class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080028 # A temporary class to hold new tests while others are disabled
29
James E. Blair2a629ec2015-12-22 15:32:02 -080030 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080031
James E. Blair83005782015-12-11 14:46:03 -080032 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080033 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070034 A.addApproval('code-review', 2)
35 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080036 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080037 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080038 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080039 self.assertEqual(self.getJobFromHistory('python27').result,
40 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080041 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080042 self.assertEqual(A.reported, 2,
43 "A should report start and success")
44 self.assertIn('tenant-one-gate', A.messages[1],
45 "A should transit tenant-one gate")
46 self.assertNotIn('tenant-two-gate', A.messages[1],
47 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080048
James E. Blair96f26942015-12-09 10:15:59 -080049 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -070050 B.addApproval('code-review', 2)
51 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080052 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080053 self.assertEqual(self.getJobFromHistory('python27',
54 'org/project2').result,
55 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080056 self.assertEqual(self.getJobFromHistory('project2-test1').result,
57 'SUCCESS')
58 self.assertEqual(B.data['status'], 'MERGED')
59 self.assertEqual(B.reported, 2,
60 "B should report start and success")
61 self.assertIn('tenant-two-gate', B.messages[1],
62 "B should transit tenant-two gate")
63 self.assertNotIn('tenant-one-gate', B.messages[1],
64 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080065
James E. Blair96f26942015-12-09 10:15:59 -080066 self.assertEqual(A.reported, 2, "Activity in tenant two should"
67 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080068
James E. Blair83005782015-12-11 14:46:03 -080069
James E. Blairff555742017-02-19 11:34:27 -080070class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -080071 # A temporary class to hold new tests while others are disabled
72
James E. Blair2a629ec2015-12-22 15:32:02 -080073 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -080074
James E. Blair83005782015-12-11 14:46:03 -080075 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -080076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070077 A.addApproval('code-review', 2)
78 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -080079 self.waitUntilSettled()
80 self.assertEqual(self.getJobFromHistory('project-test1').result,
81 'SUCCESS')
82 self.assertEqual(A.data['status'], 'MERGED')
83 self.assertEqual(A.reported, 2,
84 "A should report start and success")
85 self.assertIn('tenant-one-gate', A.messages[1],
86 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -080087
James E. Blair8b1dc3f2016-07-05 16:49:00 -070088 def test_dynamic_config(self):
89 in_repo_conf = textwrap.dedent(
90 """
91 - job:
92 name: project-test2
93
94 - project:
95 name: org/project
96 tenant-one-gate:
97 jobs:
98 - project-test2
99 """)
100
James E. Blairc73c73a2017-01-20 15:15:15 -0800101 in_repo_playbook = textwrap.dedent(
102 """
103 - hosts: all
104 tasks: []
105 """)
106
107 file_dict = {'.zuul.yaml': in_repo_conf,
108 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700109 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800110 files=file_dict)
James E. Blair8b5408c2016-08-08 15:37:46 -0700111 A.addApproval('code-review', 2)
112 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700113 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700114 self.assertEqual(A.data['status'], 'MERGED')
115 self.assertEqual(A.reported, 2,
116 "A should report start and success")
117 self.assertIn('tenant-one-gate', A.messages[1],
118 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800119 self.assertHistory([
120 dict(name='project-test2', result='SUCCESS', changes='1,1')])
121
James E. Blairc2a5ed72017-02-20 14:12:01 -0500122 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800123 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500124
James E. Blair646322f2017-01-27 15:50:34 -0800125 # Now that the config change is landed, it should be live for
126 # subsequent changes.
127 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
128 B.addApproval('code-review', 2)
129 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
130 self.waitUntilSettled()
131 self.assertEqual(self.getJobFromHistory('project-test2').result,
132 'SUCCESS')
133 self.assertHistory([
134 dict(name='project-test2', result='SUCCESS', changes='1,1'),
135 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800136
Tobias Henkel0f714002017-06-30 23:30:52 +0200137 def test_dynamic_config_new_patchset(self):
138 self.executor_server.hold_jobs_in_build = True
139
140 tenant = self.sched.abide.tenants.get('tenant-one')
141 check_pipeline = tenant.layout.pipelines['check']
142
143 in_repo_conf = textwrap.dedent(
144 """
145 - job:
146 name: project-test2
147
148 - project:
149 name: org/project
150 check:
151 jobs:
152 - project-test2
153 """)
154
155 in_repo_playbook = textwrap.dedent(
156 """
157 - hosts: all
158 tasks: []
159 """)
160
161 file_dict = {'.zuul.yaml': in_repo_conf,
162 'playbooks/project-test2.yaml': in_repo_playbook}
163 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
164 files=file_dict)
165 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
166 self.waitUntilSettled()
167
168 items = check_pipeline.getAllItems()
169 self.assertEqual(items[0].change.number, '1')
170 self.assertEqual(items[0].change.patchset, '1')
171 self.assertTrue(items[0].live)
172
173 in_repo_conf = textwrap.dedent(
174 """
175 - job:
176 name: project-test2
177
178 - project:
179 name: org/project
180 check:
181 jobs:
182 - project-test1
183 - project-test2
184 """)
185 file_dict = {'.zuul.yaml': in_repo_conf,
186 'playbooks/project-test2.yaml': in_repo_playbook}
187
188 A.addPatchset(files=file_dict)
189 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
190
191 self.waitUntilSettled()
192
193 items = check_pipeline.getAllItems()
194 self.assertEqual(items[0].change.number, '1')
195 self.assertEqual(items[0].change.patchset, '2')
196 self.assertTrue(items[0].live)
197
198 self.executor_server.hold_jobs_in_build = False
199 self.executor_server.release()
200 self.waitUntilSettled()
201
James E. Blairff555742017-02-19 11:34:27 -0800202 def test_in_repo_branch(self):
203 in_repo_conf = textwrap.dedent(
204 """
205 - job:
206 name: project-test2
207
208 - project:
209 name: org/project
210 tenant-one-gate:
211 jobs:
212 - project-test2
213 """)
214
215 in_repo_playbook = textwrap.dedent(
216 """
217 - hosts: all
218 tasks: []
219 """)
220
221 file_dict = {'.zuul.yaml': in_repo_conf,
222 'playbooks/project-test2.yaml': in_repo_playbook}
223 self.create_branch('org/project', 'stable')
224 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
225 files=file_dict)
226 A.addApproval('code-review', 2)
227 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
228 self.waitUntilSettled()
229 self.assertEqual(A.data['status'], 'MERGED')
230 self.assertEqual(A.reported, 2,
231 "A should report start and success")
232 self.assertIn('tenant-one-gate', A.messages[1],
233 "A should transit tenant-one gate")
234 self.assertHistory([
235 dict(name='project-test2', result='SUCCESS', changes='1,1')])
236 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800237 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800238
239 # The config change should not affect master.
240 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
241 B.addApproval('code-review', 2)
242 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
243 self.waitUntilSettled()
244 self.assertHistory([
245 dict(name='project-test2', result='SUCCESS', changes='1,1'),
246 dict(name='project-test1', result='SUCCESS', changes='2,1')])
247
248 # The config change should be live for further changes on
249 # stable.
250 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
251 C.addApproval('code-review', 2)
252 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
253 self.waitUntilSettled()
254 self.assertHistory([
255 dict(name='project-test2', result='SUCCESS', changes='1,1'),
256 dict(name='project-test1', result='SUCCESS', changes='2,1'),
257 dict(name='project-test2', result='SUCCESS', changes='3,1')])
258
James E. Blaira5a12492017-05-03 11:40:48 -0700259 def test_crd_dynamic_config_branch(self):
260 # Test that we can create a job in one repo and be able to use
261 # it from a different branch on a different repo.
262
263 self.create_branch('org/project1', 'stable')
264
265 in_repo_conf = textwrap.dedent(
266 """
267 - job:
268 name: project-test2
269
270 - project:
271 name: org/project
272 check:
273 jobs:
274 - project-test2
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
288 second_repo_conf = textwrap.dedent(
289 """
290 - project:
291 name: org/project1
292 check:
293 jobs:
294 - project-test2
295 """)
296
297 second_file_dict = {'.zuul.yaml': second_repo_conf}
298 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
299 files=second_file_dict)
300 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
301 B.subject, A.data['id'])
302
303 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
304 self.waitUntilSettled()
305 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
306 self.waitUntilSettled()
307
308 self.assertEqual(A.reported, 1, "A should report")
309 self.assertHistory([
310 dict(name='project-test2', result='SUCCESS', changes='1,1'),
311 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
312 ])
313
James E. Blair149b69c2017-03-02 10:48:16 -0800314 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800315 in_repo_conf = textwrap.dedent(
316 """
317 - job:
318 name: project-test2
319 foo: error
320 """)
321
322 file_dict = {'.zuul.yaml': in_repo_conf}
323 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
324 files=file_dict)
325 A.addApproval('code-review', 2)
326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
327 self.waitUntilSettled()
328
329 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200330 self.assertEqual(A.reported, 1,
331 "A should report failure")
332 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800333 "A should have a syntax error reported")
334
James E. Blair149b69c2017-03-02 10:48:16 -0800335 def test_trusted_syntax_error(self):
336 in_repo_conf = textwrap.dedent(
337 """
338 - job:
339 name: project-test2
340 foo: error
341 """)
342
343 file_dict = {'zuul.yaml': in_repo_conf}
344 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
345 files=file_dict)
346 A.addApproval('code-review', 2)
347 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
348 self.waitUntilSettled()
349
350 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200351 self.assertEqual(A.reported, 1,
352 "A should report failure")
353 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800354 "A should have a syntax error reported")
355
James E. Blair6f140c72017-03-03 10:32:07 -0800356 def test_untrusted_yaml_error(self):
357 in_repo_conf = textwrap.dedent(
358 """
359 - job:
360 foo: error
361 """)
362
363 file_dict = {'.zuul.yaml': in_repo_conf}
364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
365 files=file_dict)
366 A.addApproval('code-review', 2)
367 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
368 self.waitUntilSettled()
369
370 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200371 self.assertEqual(A.reported, 1,
372 "A should report failure")
373 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800374 "A should have a syntax error reported")
375
James E. Blairdb04e6a2017-05-03 14:49:36 -0700376 def test_untrusted_shadow_error(self):
377 in_repo_conf = textwrap.dedent(
378 """
379 - job:
380 name: common-config-test
381 """)
382
383 file_dict = {'.zuul.yaml': in_repo_conf}
384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
385 files=file_dict)
386 A.addApproval('code-review', 2)
387 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
388 self.waitUntilSettled()
389
390 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200391 self.assertEqual(A.reported, 1,
392 "A should report failure")
393 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700394 "A should have a syntax error reported")
395
James E. Blaird5656ad2017-06-02 14:29:41 -0700396 def test_untrusted_pipeline_error(self):
397 in_repo_conf = textwrap.dedent(
398 """
399 - pipeline:
400 name: test
401 """)
402
403 file_dict = {'.zuul.yaml': in_repo_conf}
404 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
405 files=file_dict)
406 A.addApproval('code-review', 2)
407 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
408 self.waitUntilSettled()
409
410 self.assertEqual(A.data['status'], 'NEW')
411 self.assertEqual(A.reported, 1,
412 "A should report failure")
413 self.assertIn('Pipelines may not be defined', A.messages[0],
414 "A should have a syntax error reported")
415
416 def test_untrusted_project_error(self):
417 in_repo_conf = textwrap.dedent(
418 """
419 - project:
420 name: org/project1
421 """)
422
423 file_dict = {'.zuul.yaml': in_repo_conf}
424 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
425 files=file_dict)
426 A.addApproval('code-review', 2)
427 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
428 self.waitUntilSettled()
429
430 self.assertEqual(A.data['status'], 'NEW')
431 self.assertEqual(A.reported, 1,
432 "A should report failure")
433 self.assertIn('the only project definition permitted', A.messages[0],
434 "A should have a syntax error reported")
435
James E. Blaire64b0e42017-06-08 11:23:34 -0700436 def test_duplicate_node_error(self):
437 in_repo_conf = textwrap.dedent(
438 """
439 - nodeset:
440 name: duplicate
441 nodes:
442 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700443 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700444 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700445 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700446 """)
447
448 file_dict = {'.zuul.yaml': in_repo_conf}
449 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
450 files=file_dict)
451 A.addApproval('code-review', 2)
452 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
453 self.waitUntilSettled()
454
455 self.assertEqual(A.data['status'], 'NEW')
456 self.assertEqual(A.reported, 1,
457 "A should report failure")
458 self.assertIn('appears multiple times', A.messages[0],
459 "A should have a syntax error reported")
460
461 def test_duplicate_group_error(self):
462 in_repo_conf = textwrap.dedent(
463 """
464 - nodeset:
465 name: duplicate
466 nodes:
467 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700468 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700469 groups:
470 - name: group
471 nodes: compute
472 - name: group
473 nodes: compute
474 """)
475
476 file_dict = {'.zuul.yaml': in_repo_conf}
477 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
478 files=file_dict)
479 A.addApproval('code-review', 2)
480 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
481 self.waitUntilSettled()
482
483 self.assertEqual(A.data['status'], 'NEW')
484 self.assertEqual(A.reported, 1,
485 "A should report failure")
486 self.assertIn('appears multiple times', A.messages[0],
487 "A should have a syntax error reported")
488
James E. Blair09f9ffe2017-07-11 15:30:25 -0700489 def test_multi_repo(self):
490 downstream_repo_conf = textwrap.dedent(
491 """
492 - project:
493 name: org/project1
494 tenant-one-gate:
495 jobs:
496 - project-test1
497
498 - job:
499 name: project1-test1
500 parent: project-test1
501 """)
502
503 file_dict = {'.zuul.yaml': downstream_repo_conf}
504 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
505 files=file_dict)
506 A.addApproval('code-review', 2)
507 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
508 self.waitUntilSettled()
509
510 self.assertEqual(A.data['status'], 'MERGED')
511 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
512 self.waitUntilSettled()
513
514 upstream_repo_conf = textwrap.dedent(
515 """
516 - job:
517 name: project-test1
518
519 - job:
520 name: project-test2
521
522 - project:
523 name: org/project
524 tenant-one-gate:
525 jobs:
526 - project-test1
527 """)
528
529 file_dict = {'.zuul.yaml': upstream_repo_conf}
530 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
531 files=file_dict)
532 B.addApproval('code-review', 2)
533 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
534 self.waitUntilSettled()
535
536 self.assertEqual(B.data['status'], 'MERGED')
537 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
538 self.waitUntilSettled()
539
540 tenant = self.sched.abide.tenants.get('tenant-one')
541 # Ensure the latest change is reflected in the config; if it
542 # isn't this will raise an exception.
543 tenant.layout.getJob('project-test2')
544
James E. Blairc73c73a2017-01-20 15:15:15 -0800545
546class TestAnsible(AnsibleZuulTestCase):
547 # A temporary class to hold new tests while others are disabled
548
549 tenant_config_file = 'config/ansible/main.yaml'
550
551 def test_playbook(self):
552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
553 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
554 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +0200555 build_timeout = self.getJobFromHistory('timeout')
556 self.assertEqual(build_timeout.result, 'TIMED_OUT')
557 build_faillocal = self.getJobFromHistory('faillocal')
558 self.assertEqual(build_faillocal.result, 'FAILURE')
559 build_failpost = self.getJobFromHistory('failpost')
560 self.assertEqual(build_failpost.result, 'POST_FAILURE')
561 build_check_vars = self.getJobFromHistory('check-vars')
562 self.assertEqual(build_check_vars.result, 'SUCCESS')
563 build_hello = self.getJobFromHistory('hello-world')
564 self.assertEqual(build_hello.result, 'SUCCESS')
565 build_python27 = self.getJobFromHistory('python27')
566 self.assertEqual(build_python27.result, 'SUCCESS')
567 flag_path = os.path.join(self.test_root, build_python27.uuid + '.flag')
James E. Blaira92cbc82017-01-23 14:56:49 -0800568 self.assertTrue(os.path.exists(flag_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200569 copied_path = os.path.join(self.test_root, build_python27.uuid +
Monty Taylorc231d932017-02-03 09:57:15 -0600570 '.copied')
571 self.assertTrue(os.path.exists(copied_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200572 failed_path = os.path.join(self.test_root, build_python27.uuid +
Monty Taylorc231d932017-02-03 09:57:15 -0600573 '.failed')
574 self.assertFalse(os.path.exists(failed_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200575 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
James E. Blair66b274e2017-01-31 14:47:52 -0800576 '.pre.flag')
577 self.assertTrue(os.path.exists(pre_flag_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200578 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
James E. Blair66b274e2017-01-31 14:47:52 -0800579 '.post.flag')
580 self.assertTrue(os.path.exists(post_flag_path))
James E. Blair5ac93842017-01-20 06:47:34 -0800581 bare_role_flag_path = os.path.join(self.test_root,
Tobias Henkel077f2f32017-05-30 20:16:46 +0200582 build_python27.uuid +
583 '.bare-role.flag')
James E. Blair5ac93842017-01-20 06:47:34 -0800584 self.assertTrue(os.path.exists(bare_role_flag_path))
James E. Blairb9c0d772017-03-03 14:34:49 -0800585
James E. Blair18f86a32017-03-15 14:43:26 -0700586 secrets_path = os.path.join(self.test_root,
Tobias Henkel077f2f32017-05-30 20:16:46 +0200587 build_python27.uuid + '.secrets')
James E. Blair18f86a32017-03-15 14:43:26 -0700588 with open(secrets_path) as f:
589 self.assertEqual(f.read(), "test-username test-password")
590
Tobias Henkel077f2f32017-05-30 20:16:46 +0200591 msg = A.messages[0]
592 success = "{} https://success.example.com/zuul-logs/{}"
593 fail = "{} https://failure.example.com/zuul-logs/{}"
594 self.assertIn(success.format("python27", build_python27.uuid), msg)
595 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
596 self.assertIn(success.format("check-vars", build_check_vars.uuid), msg)
597 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
598 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
599 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
600
James E. Blairb9c0d772017-03-03 14:34:49 -0800601
James E. Blaira4d4eef2017-06-30 14:49:17 -0700602class TestPrePlaybooks(AnsibleZuulTestCase):
603 # A temporary class to hold new tests while others are disabled
604
605 tenant_config_file = 'config/pre-playbook/main.yaml'
606
607 def test_pre_playbook_fail(self):
608 # Test that we run the post playbooks (but not the actual
609 # playbook) when a pre-playbook fails.
610 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
611 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
612 self.waitUntilSettled()
613 build = self.getJobFromHistory('python27')
614 self.assertIsNone(build.result)
615 self.assertIn('RETRY_LIMIT', A.messages[0])
616 flag_path = os.path.join(self.test_root, build.uuid +
617 '.main.flag')
618 self.assertFalse(os.path.exists(flag_path))
619 pre_flag_path = os.path.join(self.test_root, build.uuid +
620 '.pre.flag')
621 self.assertFalse(os.path.exists(pre_flag_path))
622 post_flag_path = os.path.join(self.test_root, build.uuid +
623 '.post.flag')
624 self.assertTrue(os.path.exists(post_flag_path))
625
626
James E. Blairb9c0d772017-03-03 14:34:49 -0800627class TestBrokenConfig(ZuulTestCase):
628 # Test that we get an appropriate syntax error if we start with a
629 # broken config.
630
631 tenant_config_file = 'config/broken/main.yaml'
632
633 def setUp(self):
634 with testtools.ExpectedException(
635 zuul.configloader.ConfigurationSyntaxError,
636 "\nZuul encountered a syntax error"):
637 super(TestBrokenConfig, self).setUp()
638
639 def test_broken_config_on_startup(self):
640 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000641
642
643class TestProjectKeys(ZuulTestCase):
644 # Test that we can generate project keys
645
646 # Normally the test infrastructure copies a static key in place
647 # for each project before starting tests. This saves time because
648 # Zuul's automatic key-generation on startup can be slow. To make
649 # sure we exercise that code, in this test we allow Zuul to create
650 # keys for the project on startup.
651 create_project_keys = True
652 tenant_config_file = 'config/in-repo/main.yaml'
653
654 def test_key_generation(self):
655 key_root = os.path.join(self.state_root, 'keys')
656 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
657 # Make sure that a proper key was created on startup
658 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -0700659 private_key, public_key = \
660 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000661
662 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
663 fixture_private_key = i.read()
664
665 # Make sure that we didn't just end up with the static fixture
666 # key
667 self.assertNotEqual(fixture_private_key, private_key)
668
669 # Make sure it's the right length
670 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -0700671
672
673class TestRoles(ZuulTestCase):
674 tenant_config_file = 'config/roles/main.yaml'
675
676 def test_role(self):
677 # This exercises a proposed change to a role being checked out
678 # and used.
679 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
680 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
681 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
682 B.subject, A.data['id'])
683 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
684 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
685 self.waitUntilSettled()
686 self.assertHistory([
687 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
688 ])
James E. Blair6459db12017-06-29 14:57:20 -0700689
690
691class TestShadow(ZuulTestCase):
692 tenant_config_file = 'config/shadow/main.yaml'
693
694 def test_shadow(self):
695 # Test that a repo is allowed to shadow another's job definitions.
696 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
697 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
698 self.waitUntilSettled()
699 self.assertHistory([
700 dict(name='test1', result='SUCCESS', changes='1,1'),
701 dict(name='test2', result='SUCCESS', changes='1,1'),
702 ])