blob: 1c633ba8ca2d826f2b5827ec5f9e60d693e158c4 [file] [log] [blame]
James E. Blair59fdbac2015-12-07 17:08:06 -08001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira00910c2017-08-23 09:15:04 -070017import json
James E. Blaira92cbc82017-01-23 14:56:49 -080018import os
James E. Blair14abdf42015-12-09 16:11:53 -080019import textwrap
James E. Blair59fdbac2015-12-07 17:08:06 -080020
James E. Blairb9c0d772017-03-03 14:34:49 -080021import testtools
22
23import zuul.configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070024from zuul.lib import encryption
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +000025from tests.base import AnsibleZuulTestCase, ZuulTestCase, FIXTURE_DIR
James E. Blair59fdbac2015-12-07 17:08:06 -080026
James E. Blair59fdbac2015-12-07 17:08:06 -080027
James E. Blair3f876d52016-07-22 13:07:14 -070028class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080029 # A temporary class to hold new tests while others are disabled
30
James E. Blair2a629ec2015-12-22 15:32:02 -080031 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080032
James E. Blair83005782015-12-11 14:46:03 -080033 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080034 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020035 A.addApproval('Code-Review', 2)
36 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080037 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080038 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080039 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080040 self.assertEqual(self.getJobFromHistory('python27').result,
41 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080042 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080043 self.assertEqual(A.reported, 2,
44 "A should report start and success")
45 self.assertIn('tenant-one-gate', A.messages[1],
46 "A should transit tenant-one gate")
47 self.assertNotIn('tenant-two-gate', A.messages[1],
48 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080049
James E. Blair96f26942015-12-09 10:15:59 -080050 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020051 B.addApproval('Code-Review', 2)
52 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080053 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080054 self.assertEqual(self.getJobFromHistory('python27',
55 'org/project2').result,
56 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080057 self.assertEqual(self.getJobFromHistory('project2-test1').result,
58 'SUCCESS')
59 self.assertEqual(B.data['status'], 'MERGED')
60 self.assertEqual(B.reported, 2,
61 "B should report start and success")
62 self.assertIn('tenant-two-gate', B.messages[1],
63 "B should transit tenant-two gate")
64 self.assertNotIn('tenant-one-gate', B.messages[1],
65 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080066
James E. Blair96f26942015-12-09 10:15:59 -080067 self.assertEqual(A.reported, 2, "Activity in tenant two should"
68 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080069
James E. Blair83005782015-12-11 14:46:03 -080070
Tobias Henkel83167622017-06-30 19:45:03 +020071class TestFinal(ZuulTestCase):
72
73 tenant_config_file = 'config/final/main.yaml'
74
75 def test_final_variant_ok(self):
76 # test clean usage of final parent job
77 in_repo_conf = textwrap.dedent(
78 """
79 - project:
80 name: org/project
81 check:
82 jobs:
83 - job-final
84 """)
85
86 file_dict = {'.zuul.yaml': in_repo_conf}
87 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
88 files=file_dict)
89 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
90 self.waitUntilSettled()
91
92 self.assertEqual(A.reported, 1)
93 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
94
95 def test_final_variant_error(self):
96 # test misuse of final parent job
97 in_repo_conf = textwrap.dedent(
98 """
99 - project:
100 name: org/project
101 check:
102 jobs:
103 - job-final:
104 vars:
105 dont_override_this: bar
106 """)
107 file_dict = {'.zuul.yaml': in_repo_conf}
108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
109 files=file_dict)
110 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
111 self.waitUntilSettled()
112
113 # The second patch tried to override some variables.
114 # Thus it should fail.
115 self.assertEqual(A.reported, 1)
116 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
117 self.assertIn('Unable to modify final job', A.messages[0])
118
119 def test_final_inheritance(self):
120 # test misuse of final parent job
121 in_repo_conf = textwrap.dedent(
122 """
123 - job:
124 name: project-test
125 parent: job-final
126
127 - project:
128 name: org/project
129 check:
130 jobs:
131 - project-test
132 """)
133
134 in_repo_playbook = textwrap.dedent(
135 """
136 - hosts: all
137 tasks: []
138 """)
139
140 file_dict = {'.zuul.yaml': in_repo_conf,
141 'playbooks/project-test.yaml': in_repo_playbook}
142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
143 files=file_dict)
144 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
145 self.waitUntilSettled()
146
147 # The second patch tried to override some variables.
148 # Thus it should fail.
149 self.assertEqual(A.reported, 1)
150 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '-1')
151 self.assertIn('Unable to inherit from final job', A.messages[0])
152
153
James E. Blairff555742017-02-19 11:34:27 -0800154class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -0800155 # A temporary class to hold new tests while others are disabled
156
Tobias Henkelabf973e2017-07-28 10:07:34 +0200157 config_file = 'zuul-connections-gerrit-and-github.conf'
James E. Blair2a629ec2015-12-22 15:32:02 -0800158 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -0800159
James E. Blair83005782015-12-11 14:46:03 -0800160 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -0800161 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200162 A.addApproval('Code-Review', 2)
163 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -0800164 self.waitUntilSettled()
165 self.assertEqual(self.getJobFromHistory('project-test1').result,
166 'SUCCESS')
167 self.assertEqual(A.data['status'], 'MERGED')
168 self.assertEqual(A.reported, 2,
169 "A should report start and success")
170 self.assertIn('tenant-one-gate', A.messages[1],
171 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -0800172
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700173 def test_dynamic_config(self):
174 in_repo_conf = textwrap.dedent(
175 """
176 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200177 name: project-test1
178
179 - job:
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700180 name: project-test2
181
182 - project:
183 name: org/project
184 tenant-one-gate:
185 jobs:
186 - project-test2
187 """)
188
James E. Blairc73c73a2017-01-20 15:15:15 -0800189 in_repo_playbook = textwrap.dedent(
190 """
191 - hosts: all
192 tasks: []
193 """)
194
195 file_dict = {'.zuul.yaml': in_repo_conf,
196 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800198 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200199 A.addApproval('Code-Review', 2)
200 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700201 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700202 self.assertEqual(A.data['status'], 'MERGED')
203 self.assertEqual(A.reported, 2,
204 "A should report start and success")
205 self.assertIn('tenant-one-gate', A.messages[1],
206 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800207 self.assertHistory([
208 dict(name='project-test2', result='SUCCESS', changes='1,1')])
209
James E. Blairc2a5ed72017-02-20 14:12:01 -0500210 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800211 self.waitUntilSettled()
James E. Blairc2a5ed72017-02-20 14:12:01 -0500212
James E. Blair646322f2017-01-27 15:50:34 -0800213 # Now that the config change is landed, it should be live for
214 # subsequent changes.
215 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200216 B.addApproval('Code-Review', 2)
217 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair646322f2017-01-27 15:50:34 -0800218 self.waitUntilSettled()
219 self.assertEqual(self.getJobFromHistory('project-test2').result,
220 'SUCCESS')
221 self.assertHistory([
222 dict(name='project-test2', result='SUCCESS', changes='1,1'),
223 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800224
Tobias Henkelf02cf512017-07-21 22:55:34 +0200225 def test_dynamic_config_non_existing_job(self):
226 """Test that requesting a non existent job fails"""
227 in_repo_conf = textwrap.dedent(
228 """
229 - job:
230 name: project-test1
231
232 - project:
233 name: org/project
234 check:
235 jobs:
236 - non-existent-job
237 """)
238
239 in_repo_playbook = textwrap.dedent(
240 """
241 - hosts: all
242 tasks: []
243 """)
244
245 file_dict = {'.zuul.yaml': in_repo_conf,
246 'playbooks/project-test2.yaml': in_repo_playbook}
247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
248 files=file_dict)
249 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
250 self.waitUntilSettled()
251 self.assertEqual(A.reported, 1,
252 "A should report failure")
253 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
254 self.assertIn('Job non-existent-job not defined', A.messages[0],
255 "A should have failed the check pipeline")
256 self.assertHistory([])
257
258 def test_dynamic_config_non_existing_job_in_template(self):
259 """Test that requesting a non existent job fails"""
260 in_repo_conf = textwrap.dedent(
261 """
262 - job:
263 name: project-test1
264
265 - project-template:
266 name: test-template
267 check:
268 jobs:
269 - non-existent-job
270
271 - project:
272 name: org/project
273 templates:
274 - test-template
275 """)
276
277 in_repo_playbook = textwrap.dedent(
278 """
279 - hosts: all
280 tasks: []
281 """)
282
283 file_dict = {'.zuul.yaml': in_repo_conf,
284 'playbooks/project-test2.yaml': in_repo_playbook}
285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
286 files=file_dict)
287 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
288 self.waitUntilSettled()
289 self.assertEqual(A.reported, 1,
290 "A should report failure")
291 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
292 self.assertIn('Job non-existent-job not defined', A.messages[0],
293 "A should have failed the check pipeline")
294 self.assertHistory([])
295
Tobias Henkel0f714002017-06-30 23:30:52 +0200296 def test_dynamic_config_new_patchset(self):
297 self.executor_server.hold_jobs_in_build = True
298
299 tenant = self.sched.abide.tenants.get('tenant-one')
300 check_pipeline = tenant.layout.pipelines['check']
301
302 in_repo_conf = textwrap.dedent(
303 """
304 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200305 name: project-test1
306
307 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200308 name: project-test2
309
310 - project:
311 name: org/project
312 check:
313 jobs:
314 - project-test2
315 """)
316
317 in_repo_playbook = textwrap.dedent(
318 """
319 - hosts: all
320 tasks: []
321 """)
322
323 file_dict = {'.zuul.yaml': in_repo_conf,
324 'playbooks/project-test2.yaml': in_repo_playbook}
325 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
326 files=file_dict)
327 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
328 self.waitUntilSettled()
329
330 items = check_pipeline.getAllItems()
331 self.assertEqual(items[0].change.number, '1')
332 self.assertEqual(items[0].change.patchset, '1')
333 self.assertTrue(items[0].live)
334
335 in_repo_conf = textwrap.dedent(
336 """
337 - job:
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200338 name: project-test1
339
340 - job:
Tobias Henkel0f714002017-06-30 23:30:52 +0200341 name: project-test2
342
343 - project:
344 name: org/project
345 check:
346 jobs:
347 - project-test1
348 - project-test2
349 """)
350 file_dict = {'.zuul.yaml': in_repo_conf,
351 'playbooks/project-test2.yaml': in_repo_playbook}
352
353 A.addPatchset(files=file_dict)
354 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
355
356 self.waitUntilSettled()
357
358 items = check_pipeline.getAllItems()
359 self.assertEqual(items[0].change.number, '1')
360 self.assertEqual(items[0].change.patchset, '2')
361 self.assertTrue(items[0].live)
362
363 self.executor_server.hold_jobs_in_build = False
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200364 self.executor_server.release('project-test1')
365 self.waitUntilSettled()
Tobias Henkel0f714002017-06-30 23:30:52 +0200366 self.executor_server.release()
367 self.waitUntilSettled()
368
Tobias Henkel0ce7ec62017-07-21 22:50:17 +0200369 self.assertHistory([
370 dict(name='project-test2', result='ABORTED', changes='1,1'),
371 dict(name='project-test1', result='SUCCESS', changes='1,2'),
372 dict(name='project-test2', result='SUCCESS', changes='1,2')])
373
James E. Blairff555742017-02-19 11:34:27 -0800374 def test_in_repo_branch(self):
375 in_repo_conf = textwrap.dedent(
376 """
377 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200378 name: project-test1
379
380 - job:
James E. Blairff555742017-02-19 11:34:27 -0800381 name: project-test2
382
383 - project:
384 name: org/project
385 tenant-one-gate:
386 jobs:
387 - project-test2
388 """)
389
390 in_repo_playbook = textwrap.dedent(
391 """
392 - hosts: all
393 tasks: []
394 """)
395
396 file_dict = {'.zuul.yaml': in_repo_conf,
397 'playbooks/project-test2.yaml': in_repo_playbook}
398 self.create_branch('org/project', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700399 self.fake_gerrit.addEvent(
400 self.fake_gerrit.getFakeBranchCreatedEvent(
401 'org/project', 'stable'))
James E. Blair6069f2b2017-09-26 16:34:11 -0700402 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800403 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
404 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200405 A.addApproval('Code-Review', 2)
406 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800407 self.waitUntilSettled()
408 self.assertEqual(A.data['status'], 'MERGED')
409 self.assertEqual(A.reported, 2,
410 "A should report start and success")
411 self.assertIn('tenant-one-gate', A.messages[1],
412 "A should transit tenant-one gate")
413 self.assertHistory([
414 dict(name='project-test2', result='SUCCESS', changes='1,1')])
415 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
James E. Blair7bbd7a32017-03-06 11:36:13 -0800416 self.waitUntilSettled()
James E. Blairff555742017-02-19 11:34:27 -0800417
418 # The config change should not affect master.
419 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200420 B.addApproval('Code-Review', 2)
421 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800422 self.waitUntilSettled()
423 self.assertHistory([
424 dict(name='project-test2', result='SUCCESS', changes='1,1'),
425 dict(name='project-test1', result='SUCCESS', changes='2,1')])
426
427 # The config change should be live for further changes on
428 # stable.
429 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200430 C.addApproval('Code-Review', 2)
431 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairff555742017-02-19 11:34:27 -0800432 self.waitUntilSettled()
433 self.assertHistory([
434 dict(name='project-test2', result='SUCCESS', changes='1,1'),
435 dict(name='project-test1', result='SUCCESS', changes='2,1'),
436 dict(name='project-test2', result='SUCCESS', changes='3,1')])
437
James E. Blaira5a12492017-05-03 11:40:48 -0700438 def test_crd_dynamic_config_branch(self):
439 # Test that we can create a job in one repo and be able to use
440 # it from a different branch on a different repo.
441
442 self.create_branch('org/project1', 'stable')
James E. Blair72facdc2017-08-17 10:29:12 -0700443 self.fake_gerrit.addEvent(
444 self.fake_gerrit.getFakeBranchCreatedEvent(
445 'org/project1', 'stable'))
James E. Blaira5a12492017-05-03 11:40:48 -0700446
447 in_repo_conf = textwrap.dedent(
448 """
449 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +0200450 name: project-test1
451
452 - job:
James E. Blaira5a12492017-05-03 11:40:48 -0700453 name: project-test2
454
455 - project:
456 name: org/project
457 check:
458 jobs:
459 - project-test2
460 """)
461
462 in_repo_playbook = textwrap.dedent(
463 """
464 - hosts: all
465 tasks: []
466 """)
467
468 file_dict = {'.zuul.yaml': in_repo_conf,
469 'playbooks/project-test2.yaml': in_repo_playbook}
470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
471 files=file_dict)
472
473 second_repo_conf = textwrap.dedent(
474 """
475 - project:
476 name: org/project1
477 check:
478 jobs:
479 - project-test2
480 """)
481
482 second_file_dict = {'.zuul.yaml': second_repo_conf}
483 B = self.fake_gerrit.addFakeChange('org/project1', 'stable', 'B',
484 files=second_file_dict)
485 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
486 B.subject, A.data['id'])
487
488 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
489 self.waitUntilSettled()
490 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
491 self.waitUntilSettled()
492
493 self.assertEqual(A.reported, 1, "A should report")
494 self.assertHistory([
495 dict(name='project-test2', result='SUCCESS', changes='1,1'),
496 dict(name='project-test2', result='SUCCESS', changes='1,1 2,1'),
497 ])
498
James E. Blair97043882017-09-06 15:51:17 -0700499 def test_yaml_list_error(self):
500 in_repo_conf = textwrap.dedent(
501 """
502 job: foo
503 """)
504
505 file_dict = {'.zuul.yaml': in_repo_conf}
506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
507 files=file_dict)
508 A.addApproval('Code-Review', 2)
509 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
510 self.waitUntilSettled()
511
512 self.assertEqual(A.data['status'], 'NEW')
513 self.assertEqual(A.reported, 1,
514 "A should report failure")
515 self.assertIn('not a list', A.messages[0],
516 "A should have a syntax error reported")
517
518 def test_yaml_dict_error(self):
519 in_repo_conf = textwrap.dedent(
520 """
521 - job
522 """)
523
524 file_dict = {'.zuul.yaml': in_repo_conf}
525 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
526 files=file_dict)
527 A.addApproval('Code-Review', 2)
528 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
529 self.waitUntilSettled()
530
531 self.assertEqual(A.data['status'], 'NEW')
532 self.assertEqual(A.reported, 1,
533 "A should report failure")
534 self.assertIn('not a dictionary', A.messages[0],
535 "A should have a syntax error reported")
536
537 def test_yaml_key_error(self):
538 in_repo_conf = textwrap.dedent(
539 """
540 - job:
541 name: project-test2
542 """)
543
544 file_dict = {'.zuul.yaml': in_repo_conf}
545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
546 files=file_dict)
547 A.addApproval('Code-Review', 2)
548 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
549 self.waitUntilSettled()
550
551 self.assertEqual(A.data['status'], 'NEW')
552 self.assertEqual(A.reported, 1,
553 "A should report failure")
554 self.assertIn('has more than one key', A.messages[0],
555 "A should have a syntax error reported")
556
557 def test_yaml_unknown_error(self):
558 in_repo_conf = textwrap.dedent(
559 """
560 - foobar:
561 foo: bar
562 """)
563
564 file_dict = {'.zuul.yaml': in_repo_conf}
565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
566 files=file_dict)
567 A.addApproval('Code-Review', 2)
568 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
569 self.waitUntilSettled()
570
571 self.assertEqual(A.data['status'], 'NEW')
572 self.assertEqual(A.reported, 1,
573 "A should report failure")
574 self.assertIn('not recognized', A.messages[0],
575 "A should have a syntax error reported")
576
James E. Blair149b69c2017-03-02 10:48:16 -0800577 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800578 in_repo_conf = textwrap.dedent(
579 """
580 - job:
581 name: project-test2
582 foo: error
583 """)
584
585 file_dict = {'.zuul.yaml': in_repo_conf}
586 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
587 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200588 A.addApproval('Code-Review', 2)
589 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire53250c2017-03-01 14:34:36 -0800590 self.waitUntilSettled()
591
592 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200593 self.assertEqual(A.reported, 1,
594 "A should report failure")
595 self.assertIn('syntax error', A.messages[0],
James E. Blaire53250c2017-03-01 14:34:36 -0800596 "A should have a syntax error reported")
597
James E. Blair149b69c2017-03-02 10:48:16 -0800598 def test_trusted_syntax_error(self):
599 in_repo_conf = textwrap.dedent(
600 """
601 - job:
602 name: project-test2
603 foo: error
604 """)
605
606 file_dict = {'zuul.yaml': in_repo_conf}
607 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
608 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200609 A.addApproval('Code-Review', 2)
610 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair149b69c2017-03-02 10:48:16 -0800611 self.waitUntilSettled()
612
613 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200614 self.assertEqual(A.reported, 1,
615 "A should report failure")
616 self.assertIn('syntax error', A.messages[0],
James E. Blair149b69c2017-03-02 10:48:16 -0800617 "A should have a syntax error reported")
618
James E. Blair6f140c72017-03-03 10:32:07 -0800619 def test_untrusted_yaml_error(self):
620 in_repo_conf = textwrap.dedent(
621 """
622 - job:
623 foo: error
624 """)
625
626 file_dict = {'.zuul.yaml': in_repo_conf}
627 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
628 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200629 A.addApproval('Code-Review', 2)
630 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6f140c72017-03-03 10:32:07 -0800631 self.waitUntilSettled()
632
633 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200634 self.assertEqual(A.reported, 1,
635 "A should report failure")
636 self.assertIn('syntax error', A.messages[0],
James E. Blair6f140c72017-03-03 10:32:07 -0800637 "A should have a syntax error reported")
638
James E. Blairdb04e6a2017-05-03 14:49:36 -0700639 def test_untrusted_shadow_error(self):
640 in_repo_conf = textwrap.dedent(
641 """
642 - job:
643 name: common-config-test
644 """)
645
646 file_dict = {'.zuul.yaml': in_repo_conf}
647 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
648 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200649 A.addApproval('Code-Review', 2)
650 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdb04e6a2017-05-03 14:49:36 -0700651 self.waitUntilSettled()
652
653 self.assertEqual(A.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +0200654 self.assertEqual(A.reported, 1,
655 "A should report failure")
656 self.assertIn('not permitted to shadow', A.messages[0],
James E. Blairdb04e6a2017-05-03 14:49:36 -0700657 "A should have a syntax error reported")
658
James E. Blaird5656ad2017-06-02 14:29:41 -0700659 def test_untrusted_pipeline_error(self):
660 in_repo_conf = textwrap.dedent(
661 """
662 - pipeline:
663 name: test
664 """)
665
666 file_dict = {'.zuul.yaml': in_repo_conf}
667 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
668 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200669 A.addApproval('Code-Review', 2)
670 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700671 self.waitUntilSettled()
672
673 self.assertEqual(A.data['status'], 'NEW')
674 self.assertEqual(A.reported, 1,
675 "A should report failure")
676 self.assertIn('Pipelines may not be defined', A.messages[0],
677 "A should have a syntax error reported")
678
679 def test_untrusted_project_error(self):
680 in_repo_conf = textwrap.dedent(
681 """
682 - project:
683 name: org/project1
684 """)
685
686 file_dict = {'.zuul.yaml': in_repo_conf}
687 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
688 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200689 A.addApproval('Code-Review', 2)
690 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaird5656ad2017-06-02 14:29:41 -0700691 self.waitUntilSettled()
692
693 self.assertEqual(A.data['status'], 'NEW')
694 self.assertEqual(A.reported, 1,
695 "A should report failure")
696 self.assertIn('the only project definition permitted', A.messages[0],
697 "A should have a syntax error reported")
698
James E. Blaire64b0e42017-06-08 11:23:34 -0700699 def test_duplicate_node_error(self):
700 in_repo_conf = textwrap.dedent(
701 """
702 - nodeset:
703 name: duplicate
704 nodes:
705 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700706 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700707 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700708 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700709 """)
710
711 file_dict = {'.zuul.yaml': in_repo_conf}
712 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
713 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200714 A.addApproval('Code-Review', 2)
715 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700716 self.waitUntilSettled()
717
718 self.assertEqual(A.data['status'], 'NEW')
719 self.assertEqual(A.reported, 1,
720 "A should report failure")
721 self.assertIn('appears multiple times', A.messages[0],
722 "A should have a syntax error reported")
723
724 def test_duplicate_group_error(self):
725 in_repo_conf = textwrap.dedent(
726 """
727 - nodeset:
728 name: duplicate
729 nodes:
730 - name: compute
James E. Blair16d96a02017-06-08 11:32:56 -0700731 label: foo
James E. Blaire64b0e42017-06-08 11:23:34 -0700732 groups:
733 - name: group
734 nodes: compute
735 - name: group
736 nodes: compute
737 """)
738
739 file_dict = {'.zuul.yaml': in_repo_conf}
740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
741 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200742 A.addApproval('Code-Review', 2)
743 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaire64b0e42017-06-08 11:23:34 -0700744 self.waitUntilSettled()
745
746 self.assertEqual(A.data['status'], 'NEW')
747 self.assertEqual(A.reported, 1,
748 "A should report failure")
749 self.assertIn('appears multiple times', A.messages[0],
750 "A should have a syntax error reported")
751
James E. Blair4ae399f2017-09-20 17:15:09 -0700752 def test_secret_not_found_error(self):
753 in_repo_conf = textwrap.dedent(
754 """
755 - job:
756 name: test
757 secrets: does-not-exist
758 """)
759
760 file_dict = {'.zuul.yaml': in_repo_conf}
761 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
762 files=file_dict)
763 A.addApproval('Code-Review', 2)
764 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
765 self.waitUntilSettled()
766
767 self.assertEqual(A.data['status'], 'NEW')
768 self.assertEqual(A.reported, 1,
769 "A should report failure")
770 self.assertIn('secret "does-not-exist" was not found', A.messages[0],
771 "A should have a syntax error reported")
772
773 def test_nodeset_not_found_error(self):
774 in_repo_conf = textwrap.dedent(
775 """
776 - job:
777 name: test
778 nodeset: does-not-exist
779 """)
780
781 file_dict = {'.zuul.yaml': in_repo_conf}
782 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
783 files=file_dict)
784 A.addApproval('Code-Review', 2)
785 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
786 self.waitUntilSettled()
787
788 self.assertEqual(A.data['status'], 'NEW')
789 self.assertEqual(A.reported, 1,
790 "A should report failure")
791 self.assertIn('nodeset "does-not-exist" was not found', A.messages[0],
792 "A should have a syntax error reported")
793
James E. Blair89e25eb2017-09-26 09:11:31 -0700794 def test_template_not_found_error(self):
795 in_repo_conf = textwrap.dedent(
796 """
797 - job:
798 name: project-test1
799 - project:
800 name: org/project
801 templates:
802 - does-not-exist
803 """)
804
805 file_dict = {'.zuul.yaml': in_repo_conf}
806 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
807 files=file_dict)
808 A.addApproval('Code-Review', 2)
809 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
810 self.waitUntilSettled()
811
812 self.assertEqual(A.data['status'], 'NEW')
813 self.assertEqual(A.reported, 1,
814 "A should report failure")
815 self.assertIn('project template "does-not-exist" was not found',
816 A.messages[0],
817 "A should have a syntax error reported")
818
James E. Blair09f9ffe2017-07-11 15:30:25 -0700819 def test_multi_repo(self):
820 downstream_repo_conf = textwrap.dedent(
821 """
822 - project:
823 name: org/project1
824 tenant-one-gate:
825 jobs:
826 - project-test1
827
828 - job:
829 name: project1-test1
830 parent: project-test1
831 """)
832
833 file_dict = {'.zuul.yaml': downstream_repo_conf}
834 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
835 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200836 A.addApproval('Code-Review', 2)
837 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700838 self.waitUntilSettled()
839
840 self.assertEqual(A.data['status'], 'MERGED')
841 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
842 self.waitUntilSettled()
843
844 upstream_repo_conf = textwrap.dedent(
845 """
846 - job:
847 name: project-test1
848
849 - job:
850 name: project-test2
851
852 - project:
853 name: org/project
854 tenant-one-gate:
855 jobs:
856 - project-test1
857 """)
858
859 file_dict = {'.zuul.yaml': upstream_repo_conf}
860 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
861 files=file_dict)
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200862 B.addApproval('Code-Review', 2)
863 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair09f9ffe2017-07-11 15:30:25 -0700864 self.waitUntilSettled()
865
866 self.assertEqual(B.data['status'], 'MERGED')
867 self.fake_gerrit.addEvent(B.getChangeMergedEvent())
868 self.waitUntilSettled()
869
870 tenant = self.sched.abide.tenants.get('tenant-one')
871 # Ensure the latest change is reflected in the config; if it
872 # isn't this will raise an exception.
873 tenant.layout.getJob('project-test2')
874
James E. Blair332636e2017-09-05 10:14:35 -0700875 def test_pipeline_error(self):
876 with open(os.path.join(FIXTURE_DIR,
877 'config/in-repo/git/',
878 'common-config/zuul.yaml')) as f:
879 base_common_config = f.read()
880
881 in_repo_conf_A = textwrap.dedent(
882 """
883 - pipeline:
884 name: periodic
885 foo: error
886 """)
887
888 file_dict = {'zuul.yaml': None,
889 'zuul.d/main.yaml': base_common_config,
890 'zuul.d/test1.yaml': in_repo_conf_A}
891 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
892 files=file_dict)
893 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
894 self.waitUntilSettled()
895 self.assertEqual(A.reported, 1,
896 "A should report failure")
897 self.assertIn('syntax error',
898 A.messages[0],
899 "A should have an error reported")
900
901 def test_change_series_error(self):
902 with open(os.path.join(FIXTURE_DIR,
903 'config/in-repo/git/',
904 'common-config/zuul.yaml')) as f:
905 base_common_config = f.read()
906
907 in_repo_conf_A = textwrap.dedent(
908 """
909 - pipeline:
910 name: periodic
911 foo: error
912 """)
913
914 file_dict = {'zuul.yaml': None,
915 'zuul.d/main.yaml': base_common_config,
916 'zuul.d/test1.yaml': in_repo_conf_A}
917 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
918 files=file_dict)
919
920 in_repo_conf_B = textwrap.dedent(
921 """
922 - job:
923 name: project-test2
924 foo: error
925 """)
926
927 file_dict = {'zuul.yaml': None,
928 'zuul.d/main.yaml': base_common_config,
929 'zuul.d/test1.yaml': in_repo_conf_A,
930 'zuul.d/test2.yaml': in_repo_conf_B}
931 B = self.fake_gerrit.addFakeChange('common-config', 'master', 'B',
932 files=file_dict)
933 B.setDependsOn(A, 1)
934 C = self.fake_gerrit.addFakeChange('common-config', 'master', 'C')
935 C.setDependsOn(B, 1)
936 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
937 self.waitUntilSettled()
938
939 self.assertEqual(C.reported, 1,
940 "C should report failure")
941 self.assertIn('depends on a change that failed to merge',
942 C.messages[0],
943 "C should have an error reported")
944
James E. Blairc73c73a2017-01-20 15:15:15 -0800945
James E. Blairc9455002017-09-06 09:22:19 -0700946class TestInRepoJoin(ZuulTestCase):
947 # In this config, org/project is not a member of any pipelines, so
948 # that we may test the changes that cause it to join them.
949
950 tenant_config_file = 'config/in-repo-join/main.yaml'
951
952 def test_dynamic_dependent_pipeline(self):
953 # Test dynamically adding a project to a
954 # dependent pipeline for the first time
955 self.executor_server.hold_jobs_in_build = True
956
957 tenant = self.sched.abide.tenants.get('tenant-one')
958 gate_pipeline = tenant.layout.pipelines['gate']
959
960 in_repo_conf = textwrap.dedent(
961 """
962 - job:
963 name: project-test1
964
965 - job:
966 name: project-test2
967
968 - project:
969 name: org/project
970 gate:
971 jobs:
972 - project-test2
973 """)
974
975 in_repo_playbook = textwrap.dedent(
976 """
977 - hosts: all
978 tasks: []
979 """)
980
981 file_dict = {'.zuul.yaml': in_repo_conf,
982 'playbooks/project-test2.yaml': in_repo_playbook}
983 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
984 files=file_dict)
985 A.addApproval('Code-Review', 2)
986 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
987 self.waitUntilSettled()
988
989 items = gate_pipeline.getAllItems()
990 self.assertEqual(items[0].change.number, '1')
991 self.assertEqual(items[0].change.patchset, '1')
992 self.assertTrue(items[0].live)
993
994 self.executor_server.hold_jobs_in_build = False
995 self.executor_server.release()
996 self.waitUntilSettled()
997
998 # Make sure the dynamic queue got cleaned up
999 self.assertEqual(gate_pipeline.queues, [])
1000
1001 def test_dynamic_dependent_pipeline_failure(self):
1002 # Test that a change behind a failing change adding a project
1003 # to a dependent pipeline is dequeued.
1004 self.executor_server.hold_jobs_in_build = True
1005
1006 in_repo_conf = textwrap.dedent(
1007 """
1008 - job:
1009 name: project-test1
1010
1011 - project:
1012 name: org/project
1013 gate:
1014 jobs:
1015 - project-test1
1016 """)
1017
1018 file_dict = {'.zuul.yaml': in_repo_conf}
1019 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1020 files=file_dict)
1021 self.executor_server.failJob('project-test1', A)
1022 A.addApproval('Code-Review', 2)
1023 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1024 self.waitUntilSettled()
1025
1026 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1027 B.addApproval('Code-Review', 2)
1028 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1029 self.waitUntilSettled()
1030
James E. Blair3490c5d2017-09-07 08:33:23 -07001031 self.orderedRelease()
James E. Blairc9455002017-09-06 09:22:19 -07001032 self.waitUntilSettled()
1033 self.assertEqual(A.reported, 2,
1034 "A should report start and failure")
1035 self.assertEqual(A.data['status'], 'NEW')
1036 self.assertEqual(B.reported, 1,
1037 "B should report start")
1038 self.assertHistory([
1039 dict(name='project-test1', result='FAILURE', changes='1,1'),
James E. Blair3490c5d2017-09-07 08:33:23 -07001040 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
James E. Blairc9455002017-09-06 09:22:19 -07001041 ], ordered=False)
1042
James E. Blair0af198f2017-09-06 09:52:35 -07001043 def test_dynamic_dependent_pipeline_absent(self):
1044 # Test that a series of dependent changes don't report merge
1045 # failures to a pipeline they aren't in.
1046 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1047 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1048 B.setDependsOn(A, 1)
1049
1050 A.addApproval('Code-Review', 2)
1051 A.addApproval('Approved', 1)
1052 B.addApproval('Code-Review', 2)
1053 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1054 self.waitUntilSettled()
1055 self.assertEqual(A.reported, 0,
1056 "A should not report")
1057 self.assertEqual(A.data['status'], 'NEW')
1058 self.assertEqual(B.reported, 0,
1059 "B should not report")
1060 self.assertEqual(B.data['status'], 'NEW')
1061 self.assertHistory([])
1062
James E. Blairc9455002017-09-06 09:22:19 -07001063
James E. Blairc73c73a2017-01-20 15:15:15 -08001064class TestAnsible(AnsibleZuulTestCase):
1065 # A temporary class to hold new tests while others are disabled
1066
1067 tenant_config_file = 'config/ansible/main.yaml'
1068
1069 def test_playbook(self):
Jamie Lennox7655b552017-03-17 12:33:38 +11001070 # Keep the jobdir around so we can inspect contents if an
1071 # assert fails.
1072 self.executor_server.keep_jobdir = True
1073 # Output extra ansible info so we might see errors.
1074 self.executor_server.verbose = True
1075 # Add a site variables file, used by check-vars
1076 path = os.path.join(FIXTURE_DIR, 'config', 'ansible',
1077 'variables.yaml')
1078 self.config.set('executor', 'variables', path)
James E. Blairc73c73a2017-01-20 15:15:15 -08001079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1080 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1081 self.waitUntilSettled()
Tobias Henkel077f2f32017-05-30 20:16:46 +02001082 build_timeout = self.getJobFromHistory('timeout')
Jamie Lennox7655b552017-03-17 12:33:38 +11001083 with self.jobLog(build_timeout):
1084 self.assertEqual(build_timeout.result, 'TIMED_OUT')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001085 build_faillocal = self.getJobFromHistory('faillocal')
Jamie Lennox7655b552017-03-17 12:33:38 +11001086 with self.jobLog(build_faillocal):
1087 self.assertEqual(build_faillocal.result, 'FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001088 build_failpost = self.getJobFromHistory('failpost')
Jamie Lennox7655b552017-03-17 12:33:38 +11001089 with self.jobLog(build_failpost):
1090 self.assertEqual(build_failpost.result, 'POST_FAILURE')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001091 build_check_vars = self.getJobFromHistory('check-vars')
Jamie Lennox7655b552017-03-17 12:33:38 +11001092 with self.jobLog(build_check_vars):
1093 self.assertEqual(build_check_vars.result, 'SUCCESS')
Monty Tayloraff8b402017-08-16 18:40:41 -05001094 build_check_secret_names = self.getJobFromHistory('check-secret-names')
1095 with self.jobLog(build_check_secret_names):
1096 self.assertEqual(build_check_secret_names.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001097 build_hello = self.getJobFromHistory('hello-world')
Jamie Lennox7655b552017-03-17 12:33:38 +11001098 with self.jobLog(build_hello):
1099 self.assertEqual(build_hello.result, 'SUCCESS')
Tobias Henkel077f2f32017-05-30 20:16:46 +02001100 build_python27 = self.getJobFromHistory('python27')
Jamie Lennox7655b552017-03-17 12:33:38 +11001101 with self.jobLog(build_python27):
1102 self.assertEqual(build_python27.result, 'SUCCESS')
1103 flag_path = os.path.join(self.test_root,
1104 build_python27.uuid + '.flag')
1105 self.assertTrue(os.path.exists(flag_path))
1106 copied_path = os.path.join(self.test_root, build_python27.uuid +
1107 '.copied')
1108 self.assertTrue(os.path.exists(copied_path))
1109 failed_path = os.path.join(self.test_root, build_python27.uuid +
1110 '.failed')
1111 self.assertFalse(os.path.exists(failed_path))
1112 pre_flag_path = os.path.join(self.test_root, build_python27.uuid +
1113 '.pre.flag')
1114 self.assertTrue(os.path.exists(pre_flag_path))
1115 post_flag_path = os.path.join(self.test_root, build_python27.uuid +
1116 '.post.flag')
1117 self.assertTrue(os.path.exists(post_flag_path))
1118 bare_role_flag_path = os.path.join(self.test_root,
1119 build_python27.uuid +
1120 '.bare-role.flag')
1121 self.assertTrue(os.path.exists(bare_role_flag_path))
1122 secrets_path = os.path.join(self.test_root,
1123 build_python27.uuid + '.secrets')
1124 with open(secrets_path) as f:
1125 self.assertEqual(f.read(), "test-username test-password")
James E. Blairb9c0d772017-03-03 14:34:49 -08001126
Jamie Lennox7655b552017-03-17 12:33:38 +11001127 msg = A.messages[0]
1128 success = "{} https://success.example.com/zuul-logs/{}"
1129 fail = "{} https://failure.example.com/zuul-logs/{}"
1130 self.assertIn(success.format("python27", build_python27.uuid), msg)
1131 self.assertIn(fail.format("faillocal", build_faillocal.uuid), msg)
1132 self.assertIn(success.format("check-vars",
1133 build_check_vars.uuid), msg)
1134 self.assertIn(success.format("hello-world", build_hello.uuid), msg)
1135 self.assertIn(fail.format("timeout", build_timeout.uuid), msg)
1136 self.assertIn(fail.format("failpost", build_failpost.uuid), msg)
Tobias Henkel077f2f32017-05-30 20:16:46 +02001137
James E. Blairabbaa6f2017-04-06 16:11:44 -07001138 def _add_job(self, job_name):
1139 conf = textwrap.dedent(
1140 """
1141 - job:
1142 name: %s
1143
1144 - project:
1145 name: org/plugin-project
1146 check:
1147 jobs:
1148 - %s
1149 """ % (job_name, job_name))
1150
1151 file_dict = {'.zuul.yaml': conf}
1152 A = self.fake_gerrit.addFakeChange('org/plugin-project', 'master', 'A',
1153 files=file_dict)
1154 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1155 self.waitUntilSettled()
1156
1157 def test_plugins(self):
1158 # Keep the jobdir around so we can inspect contents if an
1159 # assert fails.
1160 self.executor_server.keep_jobdir = True
1161 # Output extra ansible info so we might see errors.
1162 self.executor_server.verbose = True
1163
1164 count = 0
1165 plugin_tests = [
1166 ('passwd', 'FAILURE'),
1167 ('cartesian', 'SUCCESS'),
1168 ('consul_kv', 'FAILURE'),
1169 ('credstash', 'FAILURE'),
1170 ('csvfile_good', 'SUCCESS'),
1171 ('csvfile_bad', 'FAILURE'),
Monty Taylor93ad2212017-08-02 14:59:50 -05001172 ('uri_bad_path', 'FAILURE'),
1173 ('uri_bad_scheme', 'FAILURE'),
Monty Taylor788a40e2017-08-02 16:14:05 -05001174 ('block_local_override', 'FAILURE'),
Monty Taylor8da768f2017-08-31 14:15:35 -05001175 ('file_local_good', 'SUCCESS'),
1176 ('file_local_bad', 'FAILURE'),
James E. Blairabbaa6f2017-04-06 16:11:44 -07001177 ]
1178 for job_name, result in plugin_tests:
1179 count += 1
1180 self._add_job(job_name)
1181
1182 job = self.getJobFromHistory(job_name)
1183 with self.jobLog(job):
1184 self.assertEqual(count, len(self.history))
1185 build = self.history[-1]
1186 self.assertEqual(build.result, result)
1187
1188 # TODOv3(jeblair): parse the ansible output and verify we're
1189 # getting the exception we expect.
1190
James E. Blairb9c0d772017-03-03 14:34:49 -08001191
James E. Blaira4d4eef2017-06-30 14:49:17 -07001192class TestPrePlaybooks(AnsibleZuulTestCase):
1193 # A temporary class to hold new tests while others are disabled
1194
1195 tenant_config_file = 'config/pre-playbook/main.yaml'
1196
1197 def test_pre_playbook_fail(self):
1198 # Test that we run the post playbooks (but not the actual
1199 # playbook) when a pre-playbook fails.
1200 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1201 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1202 self.waitUntilSettled()
1203 build = self.getJobFromHistory('python27')
1204 self.assertIsNone(build.result)
1205 self.assertIn('RETRY_LIMIT', A.messages[0])
1206 flag_path = os.path.join(self.test_root, build.uuid +
1207 '.main.flag')
1208 self.assertFalse(os.path.exists(flag_path))
1209 pre_flag_path = os.path.join(self.test_root, build.uuid +
1210 '.pre.flag')
1211 self.assertFalse(os.path.exists(pre_flag_path))
1212 post_flag_path = os.path.join(self.test_root, build.uuid +
1213 '.post.flag')
James E. Blair21037782017-07-19 11:56:55 -07001214 self.assertTrue(os.path.exists(post_flag_path),
1215 "The file %s should exist" % post_flag_path)
James E. Blaira4d4eef2017-06-30 14:49:17 -07001216
1217
James E. Blairb9c0d772017-03-03 14:34:49 -08001218class TestBrokenConfig(ZuulTestCase):
1219 # Test that we get an appropriate syntax error if we start with a
1220 # broken config.
1221
1222 tenant_config_file = 'config/broken/main.yaml'
1223
1224 def setUp(self):
1225 with testtools.ExpectedException(
1226 zuul.configloader.ConfigurationSyntaxError,
1227 "\nZuul encountered a syntax error"):
1228 super(TestBrokenConfig, self).setUp()
1229
1230 def test_broken_config_on_startup(self):
1231 pass
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001232
1233
1234class TestProjectKeys(ZuulTestCase):
1235 # Test that we can generate project keys
1236
1237 # Normally the test infrastructure copies a static key in place
1238 # for each project before starting tests. This saves time because
1239 # Zuul's automatic key-generation on startup can be slow. To make
1240 # sure we exercise that code, in this test we allow Zuul to create
1241 # keys for the project on startup.
1242 create_project_keys = True
Tobias Henkelabf973e2017-07-28 10:07:34 +02001243 config_file = 'zuul-connections-gerrit-and-github.conf'
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001244 tenant_config_file = 'config/in-repo/main.yaml'
1245
1246 def test_key_generation(self):
1247 key_root = os.path.join(self.state_root, 'keys')
1248 private_key_file = os.path.join(key_root, 'gerrit/org/project.pem')
1249 # Make sure that a proper key was created on startup
1250 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -07001251 private_key, public_key = \
1252 encryption.deserialize_rsa_keypair(f.read())
Ricardo Carrillo Cruz22994f92016-12-02 11:41:58 +00001253
1254 with open(os.path.join(FIXTURE_DIR, 'private.pem')) as i:
1255 fixture_private_key = i.read()
1256
1257 # Make sure that we didn't just end up with the static fixture
1258 # key
1259 self.assertNotEqual(fixture_private_key, private_key)
1260
1261 # Make sure it's the right length
1262 self.assertEqual(4096, private_key.key_size)
James E. Blairbce76932017-05-04 10:03:15 -07001263
1264
James E. Blairbb94dfa2017-07-11 07:45:19 -07001265class RoleTestCase(ZuulTestCase):
James E. Blair1b27f6a2017-07-14 14:09:07 -07001266 def _assertRolePath(self, build, playbook, content):
1267 path = os.path.join(self.test_root, build.uuid,
1268 'ansible', playbook, 'ansible.cfg')
1269 roles_paths = []
1270 with open(path) as f:
1271 for line in f:
1272 if line.startswith('roles_path'):
1273 roles_paths.append(line)
1274 print(roles_paths)
1275 if content:
1276 self.assertEqual(len(roles_paths), 1,
1277 "Should have one roles_path line in %s" %
1278 (playbook,))
1279 self.assertIn(content, roles_paths[0])
1280 else:
1281 self.assertEqual(len(roles_paths), 0,
1282 "Should have no roles_path line in %s" %
1283 (playbook,))
1284
James E. Blairbb94dfa2017-07-11 07:45:19 -07001285
1286class TestRoles(RoleTestCase):
1287 tenant_config_file = 'config/roles/main.yaml'
1288
James E. Blairbce76932017-05-04 10:03:15 -07001289 def test_role(self):
1290 # This exercises a proposed change to a role being checked out
1291 # and used.
1292 A = self.fake_gerrit.addFakeChange('bare-role', 'master', 'A')
1293 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1294 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
1295 B.subject, A.data['id'])
1296 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1297 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1298 self.waitUntilSettled()
1299 self.assertHistory([
1300 dict(name='project-test', result='SUCCESS', changes='1,1 2,1'),
1301 ])
James E. Blair6459db12017-06-29 14:57:20 -07001302
James E. Blair1b27f6a2017-07-14 14:09:07 -07001303 def test_role_inheritance(self):
1304 self.executor_server.hold_jobs_in_build = True
1305 conf = textwrap.dedent(
1306 """
1307 - job:
1308 name: parent
1309 roles:
1310 - zuul: bare-role
1311 pre-run: playbooks/parent-pre
1312 post-run: playbooks/parent-post
1313
1314 - job:
1315 name: project-test
1316 parent: parent
1317 roles:
1318 - zuul: org/project
1319
1320 - project:
1321 name: org/project
1322 check:
1323 jobs:
1324 - project-test
1325 """)
1326
1327 file_dict = {'.zuul.yaml': conf}
1328 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1329 files=file_dict)
1330 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1331 self.waitUntilSettled()
1332
1333 self.assertEqual(len(self.builds), 1)
1334 build = self.getBuildByName('project-test')
1335 self._assertRolePath(build, 'pre_playbook_0', 'role_0')
1336 self._assertRolePath(build, 'playbook_0', 'role_0')
1337 self._assertRolePath(build, 'playbook_0', 'role_1')
1338 self._assertRolePath(build, 'post_playbook_0', 'role_0')
1339
1340 self.executor_server.hold_jobs_in_build = False
1341 self.executor_server.release()
1342 self.waitUntilSettled()
1343
1344 self.assertHistory([
1345 dict(name='project-test', result='SUCCESS', changes='1,1'),
1346 ])
1347
James E. Blair6f699732017-07-18 14:19:11 -07001348 def test_role_error(self):
1349 conf = textwrap.dedent(
1350 """
1351 - job:
1352 name: project-test
1353 roles:
1354 - zuul: common-config
1355
1356 - project:
1357 name: org/project
1358 check:
1359 jobs:
1360 - project-test
1361 """)
1362
1363 file_dict = {'.zuul.yaml': conf}
1364 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1365 files=file_dict)
1366 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1367 self.waitUntilSettled()
1368 self.assertIn(
1369 '- project-test project-test : ERROR Unable to find role',
1370 A.messages[-1])
1371
James E. Blair6459db12017-06-29 14:57:20 -07001372
James E. Blairbb94dfa2017-07-11 07:45:19 -07001373class TestImplicitRoles(RoleTestCase):
1374 tenant_config_file = 'config/implicit-roles/main.yaml'
1375
1376 def test_missing_roles(self):
1377 # Test implicit and explicit roles for a project which does
1378 # not have roles. The implicit role should be silently
1379 # ignored since the project doesn't supply roles, but if a
1380 # user declares an explicit role, it should error.
1381 self.executor_server.hold_jobs_in_build = True
1382 A = self.fake_gerrit.addFakeChange('org/norole-project', 'master', 'A')
1383 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1384 self.waitUntilSettled()
1385
1386 self.assertEqual(len(self.builds), 2)
1387 build = self.getBuildByName('implicit-role-fail')
1388 self._assertRolePath(build, 'playbook_0', None)
1389
1390 self.executor_server.hold_jobs_in_build = False
1391 self.executor_server.release()
1392 self.waitUntilSettled()
1393 # The retry_limit doesn't get recorded
1394 self.assertHistory([
1395 dict(name='implicit-role-fail', result='SUCCESS', changes='1,1'),
1396 ])
1397
1398 def test_roles(self):
1399 # Test implicit and explicit roles for a project which does
1400 # have roles. In both cases, we should end up with the role
1401 # in the path. In the explicit case, ensure we end up with
1402 # the name we specified.
1403 self.executor_server.hold_jobs_in_build = True
1404 A = self.fake_gerrit.addFakeChange('org/role-project', 'master', 'A')
1405 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1406 self.waitUntilSettled()
1407
1408 self.assertEqual(len(self.builds), 2)
1409 build = self.getBuildByName('implicit-role-ok')
1410 self._assertRolePath(build, 'playbook_0', 'role_0')
1411
1412 build = self.getBuildByName('explicit-role-ok')
1413 self._assertRolePath(build, 'playbook_0', 'role_0')
1414
1415 self.executor_server.hold_jobs_in_build = False
1416 self.executor_server.release()
1417 self.waitUntilSettled()
1418 self.assertHistory([
1419 dict(name='implicit-role-ok', result='SUCCESS', changes='1,1'),
1420 dict(name='explicit-role-ok', result='SUCCESS', changes='1,1'),
1421 ], ordered=False)
1422
1423
James E. Blair6459db12017-06-29 14:57:20 -07001424class TestShadow(ZuulTestCase):
1425 tenant_config_file = 'config/shadow/main.yaml'
1426
1427 def test_shadow(self):
1428 # Test that a repo is allowed to shadow another's job definitions.
1429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1430 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1431 self.waitUntilSettled()
1432 self.assertHistory([
1433 dict(name='test1', result='SUCCESS', changes='1,1'),
1434 dict(name='test2', result='SUCCESS', changes='1,1'),
James E. Blairadafa6c2017-07-12 08:50:56 -07001435 ], ordered=False)
James E. Blair196f61a2017-06-30 15:42:29 -07001436
1437
1438class TestDataReturn(AnsibleZuulTestCase):
1439 tenant_config_file = 'config/data-return/main.yaml'
1440
1441 def test_data_return(self):
1442 # This exercises a proposed change to a role being checked out
1443 # and used.
1444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1445 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1446 self.waitUntilSettled()
1447 self.assertHistory([
1448 dict(name='data-return', result='SUCCESS', changes='1,1'),
James E. Blair88e79c02017-07-07 13:36:54 -07001449 dict(name='data-return-relative', result='SUCCESS', changes='1,1'),
1450 ], ordered=False)
1451 self.assertIn('- data-return http://example.com/test/log/url/',
1452 A.messages[-1])
1453 self.assertIn('- data-return-relative '
1454 'http://example.com/test/log/url/docs/index.html',
James E. Blair196f61a2017-06-30 15:42:29 -07001455 A.messages[-1])
Clint Byrumdc8a0902017-07-20 16:36:27 -07001456
1457
1458class TestDiskAccounting(AnsibleZuulTestCase):
1459 config_file = 'zuul-disk-accounting.conf'
1460 tenant_config_file = 'config/disk-accountant/main.yaml'
1461
1462 def test_disk_accountant_kills_job(self):
1463 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1464 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1465 self.waitUntilSettled()
1466 self.assertHistory([
1467 dict(name='dd-big-empty-file', result='ABORTED', changes='1,1')])
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001468
1469
1470class TestMaxNodesPerJob(AnsibleZuulTestCase):
1471 tenant_config_file = 'config/multi-tenant/main.yaml'
1472
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001473 def test_max_timeout_exceeded(self):
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001474 in_repo_conf = textwrap.dedent(
1475 """
1476 - job:
1477 name: test-job
James E. Blair7e3e6882017-09-20 15:47:13 -07001478 nodeset:
1479 nodes:
1480 - name: node01
1481 label: fake
1482 - name: node02
1483 label: fake
1484 - name: node03
1485 label: fake
1486 - name: node04
1487 label: fake
1488 - name: node05
1489 label: fake
1490 - name: node06
1491 label: fake
Tristan Cacqueray82f864b2017-08-01 05:54:42 +00001492 """)
1493 file_dict = {'.zuul.yaml': in_repo_conf}
1494 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1495 files=file_dict)
1496 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1497 self.waitUntilSettled()
1498 self.assertIn('The job "test-job" exceeds tenant max-nodes-per-job 5.',
1499 A.messages[0], "A should fail because of nodes limit")
1500
1501 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1502 files=file_dict)
1503 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1504 self.waitUntilSettled()
1505 self.assertNotIn("exceeds tenant max-nodes", B.messages[0],
1506 "B should not fail because of nodes limit")
James E. Blair2bab6e72017-08-07 09:52:45 -07001507
1508
Tristan Cacquerayc98bff72017-09-10 15:25:26 +00001509class TestMaxTimeout(AnsibleZuulTestCase):
1510 tenant_config_file = 'config/multi-tenant/main.yaml'
1511
1512 def test_max_nodes_reached(self):
1513 in_repo_conf = textwrap.dedent(
1514 """
1515 - job:
1516 name: test-job
1517 timeout: 3600
1518 """)
1519 file_dict = {'.zuul.yaml': in_repo_conf}
1520 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A',
1521 files=file_dict)
1522 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1523 self.waitUntilSettled()
1524 self.assertIn('The job "test-job" exceeds tenant max-job-timeout',
1525 A.messages[0], "A should fail because of timeout limit")
1526
1527 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A',
1528 files=file_dict)
1529 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1530 self.waitUntilSettled()
1531 self.assertNotIn("exceeds tenant max-job-timeout", B.messages[0],
1532 "B should not fail because of timeout limit")
1533
1534
James E. Blair2bab6e72017-08-07 09:52:45 -07001535class TestBaseJobs(ZuulTestCase):
1536 tenant_config_file = 'config/base-jobs/main.yaml'
1537
1538 def test_multiple_base_jobs(self):
1539 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1540 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1541 self.waitUntilSettled()
1542 self.assertHistory([
1543 dict(name='my-job', result='SUCCESS', changes='1,1'),
1544 dict(name='other-job', result='SUCCESS', changes='1,1'),
1545 ], ordered=False)
1546 self.assertEqual(self.getJobFromHistory('my-job').
1547 parameters['zuul']['jobtags'],
1548 ['mybase'])
1549 self.assertEqual(self.getJobFromHistory('other-job').
1550 parameters['zuul']['jobtags'],
1551 ['otherbase'])
1552
1553 def test_untrusted_base_job(self):
1554 """Test that a base job may not be defined in an untrusted repo"""
1555 in_repo_conf = textwrap.dedent(
1556 """
1557 - job:
1558 name: fail-base
1559 parent: null
1560 """)
1561
1562 file_dict = {'.zuul.yaml': in_repo_conf}
1563 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1564 files=file_dict)
1565 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1566 self.waitUntilSettled()
1567 self.assertEqual(A.reported, 1,
1568 "A should report failure")
1569 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
1570 self.assertIn('Base jobs must be defined in config projects',
1571 A.messages[0])
1572 self.assertHistory([])
James E. Blairdb089032017-08-15 13:42:12 -07001573
1574
1575class TestSecretLeaks(AnsibleZuulTestCase):
1576 tenant_config_file = 'config/secret-leaks/main.yaml'
1577
1578 def searchForContent(self, path, content):
1579 matches = []
1580 for (dirpath, dirnames, filenames) in os.walk(path):
1581 for filename in filenames:
1582 filepath = os.path.join(dirpath, filename)
1583 with open(filepath, 'rb') as f:
1584 if content in f.read():
1585 matches.append(filepath[len(path):])
1586 return matches
1587
1588 def _test_secret_file(self):
1589 # Or rather -- test that they *don't* leak.
1590 # Keep the jobdir around so we can inspect contents.
1591 self.executor_server.keep_jobdir = True
1592 conf = textwrap.dedent(
1593 """
1594 - project:
1595 name: org/project
1596 check:
1597 jobs:
1598 - secret-file
1599 """)
1600
1601 file_dict = {'.zuul.yaml': conf}
1602 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1603 files=file_dict)
1604 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1605 self.waitUntilSettled()
1606 self.assertHistory([
1607 dict(name='secret-file', result='SUCCESS', changes='1,1'),
1608 ], ordered=False)
1609 matches = self.searchForContent(self.history[0].jobdir.root,
1610 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001611 self.assertEqual(set(['/work/secret-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001612 set(matches))
1613
1614 def test_secret_file(self):
1615 self._test_secret_file()
1616
1617 def test_secret_file_verbose(self):
1618 # Output extra ansible info to exercise alternate logging code
1619 # paths.
1620 self.executor_server.verbose = True
1621 self._test_secret_file()
1622
1623 def _test_secret_file_fail(self):
1624 # Or rather -- test that they *don't* leak.
1625 # Keep the jobdir around so we can inspect contents.
1626 self.executor_server.keep_jobdir = True
1627 conf = textwrap.dedent(
1628 """
1629 - project:
1630 name: org/project
1631 check:
1632 jobs:
1633 - secret-file-fail
1634 """)
1635
1636 file_dict = {'.zuul.yaml': conf}
1637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
1638 files=file_dict)
1639 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1640 self.waitUntilSettled()
1641 self.assertHistory([
1642 dict(name='secret-file-fail', result='FAILURE', changes='1,1'),
1643 ], ordered=False)
1644 matches = self.searchForContent(self.history[0].jobdir.root,
1645 b'test-password')
James E. Blaird6a71ca2017-08-18 14:15:05 -07001646 self.assertEqual(set(['/work/failure-file.txt']),
James E. Blairdb089032017-08-15 13:42:12 -07001647 set(matches))
1648
1649 def test_secret_file_fail(self):
1650 self._test_secret_file_fail()
1651
1652 def test_secret_file_fail_verbose(self):
1653 # Output extra ansible info to exercise alternate logging code
1654 # paths.
1655 self.executor_server.verbose = True
1656 self._test_secret_file_fail()
James E. Blaira00910c2017-08-23 09:15:04 -07001657
1658
1659class TestJobOutput(AnsibleZuulTestCase):
1660 tenant_config_file = 'config/job-output/main.yaml'
1661
1662 def _get_file(self, build, path):
1663 p = os.path.join(build.jobdir.root, path)
1664 with open(p) as f:
1665 return f.read()
1666
1667 def test_job_output(self):
1668 # Verify that command standard output appears in the job output
1669
1670 # This currently only verifies we receive output from
1671 # localhost. Notably, it does not verify we receive output
1672 # via zuul_console streaming.
1673 self.executor_server.keep_jobdir = True
1674 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1675 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1676 self.waitUntilSettled()
1677 self.assertHistory([
1678 dict(name='job-output', result='SUCCESS', changes='1,1'),
1679 ], ordered=False)
1680
1681 token = 'Standard output test %s' % (self.history[0].jobdir.src_root)
1682 j = json.loads(self._get_file(self.history[0],
1683 'work/logs/job-output.json'))
1684 self.assertEqual(token,
1685 j[0]['plays'][0]['tasks'][0]
1686 ['hosts']['localhost']['stdout'])
1687
1688 print(self._get_file(self.history[0],
1689 'work/logs/job-output.txt'))
1690 self.assertIn(token,
1691 self._get_file(self.history[0],
1692 'work/logs/job-output.txt'))