blob: 112f48c826ee02034392faba4855dee94c3d979d [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. Blairc73c73a2017-01-20 15:15:15 -0800489
490class TestAnsible(AnsibleZuulTestCase):
491 # A temporary class to hold new tests while others are disabled
492
493 tenant_config_file = 'config/ansible/main.yaml'
494
495 def test_playbook(self):
496 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
497 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
498 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +0200499 build_timeout = self.getJobFromHistory('timeout')
500 self.assertEqual(build_timeout.result, 'TIMED_OUT')
501 build_faillocal = self.getJobFromHistory('faillocal')
502 self.assertEqual(build_faillocal.result, 'FAILURE')
503 build_failpost = self.getJobFromHistory('failpost')
504 self.assertEqual(build_failpost.result, 'POST_FAILURE')
505 build_check_vars = self.getJobFromHistory('check-vars')
506 self.assertEqual(build_check_vars.result, 'SUCCESS')
507 build_hello = self.getJobFromHistory('hello-world')
508 self.assertEqual(build_hello.result, 'SUCCESS')
509 build_python27 = self.getJobFromHistory('python27')
510 self.assertEqual(build_python27.result, 'SUCCESS')
511 flag_path = os.path.join(self.test_root, build_python27.uuid + '.flag')
James E. Blaira92cbc82017-01-23 14:56:49 -0800512 self.assertTrue(os.path.exists(flag_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200513 copied_path = os.path.join(self.test_root, build_python27.uuid +
Monty Taylorc231d932017-02-03 09:57:15 -0600514 '.copied')
515 self.assertTrue(os.path.exists(copied_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200516 failed_path = os.path.join(self.test_root, build_python27.uuid +
Monty Taylorc231d932017-02-03 09:57:15 -0600517 '.failed')
518 self.assertFalse(os.path.exists(failed_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200519 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
James E. Blair66b274e2017-01-31 14:47:52 -0800520 '.pre.flag')
521 self.assertTrue(os.path.exists(pre_flag_path))
Tobias Henkel077f2f32017-05-30 20:16:46 +0200522 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
James E. Blair66b274e2017-01-31 14:47:52 -0800523 '.post.flag')
524 self.assertTrue(os.path.exists(post_flag_path))
James E. Blair5ac93842017-01-20 06:47:34 -0800525 bare_role_flag_path = os.path.join(self.test_root,
Tobias Henkel077f2f32017-05-30 20:16:46 +0200526 build_python27.uuid +
527 '.bare-role.flag')
James E. Blair5ac93842017-01-20 06:47:34 -0800528 self.assertTrue(os.path.exists(bare_role_flag_path))
James E. Blairb9c0d772017-03-03 14:34:49 -0800529
James E. Blair18f86a32017-03-15 14:43:26 -0700530 secrets_path = os.path.join(self.test_root,
Tobias Henkel077f2f32017-05-30 20:16:46 +0200531 build_python27.uuid + '.secrets')
James E. Blair18f86a32017-03-15 14:43:26 -0700532 with open(secrets_path) as f:
533 self.assertEqual(f.read(), "test-username test-password")
534
Tobias Henkel077f2f32017-05-30 20:16:46 +0200535 msg = A.messages[0]
536 success = "{} https://success.example.com/zuul-logs/{}"
537 fail = "{} https://failure.example.com/zuul-logs/{}"
538 self.assertIn(success.format("python27", build_python27.uuid), msg)
539 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
540 self.assertIn(success.format("check-vars", build_check_vars.uuid), msg)
541 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
542 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
543 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
544
James E. Blairb9c0d772017-03-03 14:34:49 -0800545
James E. Blaira4d4eef2017-06-30 14:49:17 -0700546class TestPrePlaybooks(AnsibleZuulTestCase):
547 # A temporary class to hold new tests while others are disabled
548
549 tenant_config_file = 'config/pre-playbook/main.yaml'
550
551 def test_pre_playbook_fail(self):
552 # Test that we run the post playbooks (but not the actual
553 # playbook) when a pre-playbook fails.
554 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
555 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
556 self.waitUntilSettled()
557 build = self.getJobFromHistory('python27')
558 self.assertIsNone(build.result)
559 self.assertIn('RETRY_LIMIT', A.messages[0])
560 flag_path = os.path.join(self.test_root, build.uuid +
561 '.main.flag')
562 self.assertFalse(os.path.exists(flag_path))
563 pre_flag_path = os.path.join(self.test_root, build.uuid +
564 '.pre.flag')
565 self.assertFalse(os.path.exists(pre_flag_path))
566 post_flag_path = os.path.join(self.test_root, build.uuid +
567 '.post.flag')
568 self.assertTrue(os.path.exists(post_flag_path))
569
570
James E. Blairb9c0d772017-03-03 14:34:49 -0800571class TestBrokenConfig(ZuulTestCase):
572 # Test that we get an appropriate syntax error if we start with a
573 # broken config.
574
575 tenant_config_file = 'config/broken/main.yaml'
576
577 def setUp(self):
578 with testtools.ExpectedException(
579 zuul.configloader.ConfigurationSyntaxError,
580 "\nZuul encountered a syntax error"):
581 super(TestBrokenConfig, self).setUp()
582
583 def test_broken_config_on_startup(self):
584 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000585
586
587class TestProjectKeys(ZuulTestCase):
588 # Test that we can generate project keys
589
590 # Normally the test infrastructure copies a static key in place
591 # for each project before starting tests. This saves time because
592 # Zuul's automatic key-generation on startup can be slow. To make
593 # sure we exercise that code, in this test we allow Zuul to create
594 # keys for the project on startup.
595 create_project_keys = True
596 tenant_config_file = 'config/in-repo/main.yaml'
597
598 def test_key_generation(self):
599 key_root = os.path.join(self.state_root, 'keys')
600 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
601 # Make sure that a proper key was created on startup
602 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -0700603 private_key, public_key = \
604 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000605
606 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
607 fixture_private_key = i.read()
608
609 # Make sure that we didn't just end up with the static fixture
610 # key
611 self.assertNotEqual(fixture_private_key, private_key)
612
613 # Make sure it's the right length
614 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -0700615
616
617class TestRoles(ZuulTestCase):
618 tenant_config_file = 'config/roles/main.yaml'
619
620 def test_role(self):
621 # This exercises a proposed change to a role being checked out
622 # and used.
623 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
624 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
625 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
626 B.subject, A.data['id'])
627 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
628 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
629 self.waitUntilSettled()
630 self.assertHistory([
631 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
632 ])
James E. Blair6459db12017-06-29 14:57:20 -0700633
634
635class TestShadow(ZuulTestCase):
636 tenant_config_file = 'config/shadow/main.yaml'
637
638 def test_shadow(self):
639 # Test that a repo is allowed to shadow another's job definitions.
640 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
641 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
642 self.waitUntilSettled()
643 self.assertHistory([
644 dict(name='test1', result='SUCCESS', changes='1,1'),
645 dict(name='test2', result='SUCCESS', changes='1,1'),
646 ])