blob: 7c5fa7069a96720a7c9902b15ebe0be05019fdad [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()
Paul Belanger96618ed2017-03-01 09:42:33 -0500499 build = self.getJobFromHistory('timeout')
Clark Boylan53fb1262017-04-25 08:37:56 -0700500 self.assertEqual(build.result, 'TIMED_OUT')
Monty Taylorc231d932017-02-03 09:57:15 -0600501 build = self.getJobFromHistory('faillocal')
502 self.assertEqual(build.result, 'FAILURE')
Paul Belangere2b8d492017-03-22 18:57:40 -0400503 build = self.getJobFromHistory('check-vars')
Paul Belanger30ba93a2017-03-16 16:28:10 -0400504 self.assertEqual(build.result, 'SUCCESS')
Paul Belanger9d9023f2017-05-02 14:51:07 -0400505 build = self.getJobFromHistory('hello-world')
506 self.assertEqual(build.result, 'SUCCESS')
James E. Blaira92cbc82017-01-23 14:56:49 -0800507 build = self.getJobFromHistory('python27')
508 self.assertEqual(build.result, 'SUCCESS')
509 flag_path = os.path.join(self.test_root, build.uuid + '.flag')
510 self.assertTrue(os.path.exists(flag_path))
Monty Taylorc231d932017-02-03 09:57:15 -0600511 copied_path = os.path.join(self.test_root, build.uuid +
512 '.copied')
513 self.assertTrue(os.path.exists(copied_path))
514 failed_path = os.path.join(self.test_root, build.uuid +
515 '.failed')
516 self.assertFalse(os.path.exists(failed_path))
James E. Blair66b274e2017-01-31 14:47:52 -0800517 pre_flag_path = os.path.join(self.test_root, build.uuid +
518 '.pre.flag')
519 self.assertTrue(os.path.exists(pre_flag_path))
520 post_flag_path = os.path.join(self.test_root, build.uuid +
521 '.post.flag')
522 self.assertTrue(os.path.exists(post_flag_path))
James E. Blair5ac93842017-01-20 06:47:34 -0800523 bare_role_flag_path = os.path.join(self.test_root,
524 build.uuid + '.bare-role.flag')
525 self.assertTrue(os.path.exists(bare_role_flag_path))
James E. Blairb9c0d772017-03-03 14:34:49 -0800526
James E. Blair18f86a32017-03-15 14:43:26 -0700527 secrets_path = os.path.join(self.test_root,
528 build.uuid + '.secrets')
529 with open(secrets_path) as f:
530 self.assertEqual(f.read(), "test-username test-password")
531
James E. Blairb9c0d772017-03-03 14:34:49 -0800532
James E. Blaira4d4eef2017-06-30 14:49:17 -0700533class TestPrePlaybooks(AnsibleZuulTestCase):
534 # A temporary class to hold new tests while others are disabled
535
536 tenant_config_file = 'config/pre-playbook/main.yaml'
537
538 def test_pre_playbook_fail(self):
539 # Test that we run the post playbooks (but not the actual
540 # playbook) when a pre-playbook fails.
541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
542 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
543 self.waitUntilSettled()
544 build = self.getJobFromHistory('python27')
545 self.assertIsNone(build.result)
546 self.assertIn('RETRY_LIMIT', A.messages[0])
547 flag_path = os.path.join(self.test_root, build.uuid +
548 '.main.flag')
549 self.assertFalse(os.path.exists(flag_path))
550 pre_flag_path = os.path.join(self.test_root, build.uuid +
551 '.pre.flag')
552 self.assertFalse(os.path.exists(pre_flag_path))
553 post_flag_path = os.path.join(self.test_root, build.uuid +
554 '.post.flag')
555 self.assertTrue(os.path.exists(post_flag_path))
556
557
James E. Blairb9c0d772017-03-03 14:34:49 -0800558class TestBrokenConfig(ZuulTestCase):
559 # Test that we get an appropriate syntax error if we start with a
560 # broken config.
561
562 tenant_config_file = 'config/broken/main.yaml'
563
564 def setUp(self):
565 with testtools.ExpectedException(
566 zuul.configloader.ConfigurationSyntaxError,
567 "\nZuul encountered a syntax error"):
568 super(TestBrokenConfig, self).setUp()
569
570 def test_broken_config_on_startup(self):
571 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000572
573
574class TestProjectKeys(ZuulTestCase):
575 # Test that we can generate project keys
576
577 # Normally the test infrastructure copies a static key in place
578 # for each project before starting tests. This saves time because
579 # Zuul's automatic key-generation on startup can be slow. To make
580 # sure we exercise that code, in this test we allow Zuul to create
581 # keys for the project on startup.
582 create_project_keys = True
583 tenant_config_file = 'config/in-repo/main.yaml'
584
585 def test_key_generation(self):
586 key_root = os.path.join(self.state_root, 'keys')
587 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
588 # Make sure that a proper key was created on startup
589 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -0700590 private_key, public_key = \
591 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +0000592
593 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
594 fixture_private_key = i.read()
595
596 # Make sure that we didn't just end up with the static fixture
597 # key
598 self.assertNotEqual(fixture_private_key, private_key)
599
600 # Make sure it's the right length
601 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -0700602
603
604class TestRoles(ZuulTestCase):
605 tenant_config_file = 'config/roles/main.yaml'
606
607 def test_role(self):
608 # This exercises a proposed change to a role being checked out
609 # and used.
610 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
611 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
612 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
613 B.subject, A.data['id'])
614 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
615 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
616 self.waitUntilSettled()
617 self.assertHistory([
618 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
619 ])