blob: c833fa2f4ad9b5d427965925cb8596661d04e7b5 [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/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. Blaira002b032017-04-18 10:35:48 -070017import gc
James E. Blairb0fcae42012-07-17 11:12:10 -070018import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010019import textwrap
20
Monty Taylorbc758832013-06-17 17:22:42 -040021import os
James E. Blair4886cc12012-07-18 15:39:41 -070022import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040023import time
Clint Byrum03454a52017-05-26 17:14:02 -070024from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040025
James E. Blair4886cc12012-07-18 15:39:41 -070026import git
Monty Taylorbc758832013-06-17 17:22:42 -040027import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070028
Maru Newby3fe5f852015-01-13 04:22:14 +000029import zuul.change_matcher
Clint Byrumb9de8be2017-05-27 13:49:19 -070030from zuul.driver.gerrit import gerritreporter
James E. Blairb0fcae42012-07-17 11:12:10 -070031import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080032import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040033import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070034
Maru Newby3fe5f852015-01-13 04:22:14 +000035from tests.base import (
Paul Belanger0a21f0a2017-06-13 13:14:42 -040036 SSLZuulTestCase,
Maru Newby3fe5f852015-01-13 04:22:14 +000037 ZuulTestCase,
38 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070039 simple_layout,
James E. Blairb4bed1d2018-02-06 13:43:50 -080040 iterate_timeout,
Maru Newby3fe5f852015-01-13 04:22:14 +000041)
James E. Blairb0fcae42012-07-17 11:12:10 -070042
James E. Blairb0fcae42012-07-17 11:12:10 -070043
Paul Belanger0a21f0a2017-06-13 13:14:42 -040044class TestSchedulerSSL(SSLZuulTestCase):
45 tenant_config_file = 'config/single-tenant/main.yaml'
46
47 def test_jobs_executed(self):
48 "Test that jobs are executed and a change is merged"
Paul Belanger0a21f0a2017-06-13 13:14:42 -040049 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020050 A.addApproval('Code-Review', 2)
51 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Paul Belanger0a21f0a2017-06-13 13:14:42 -040052 self.waitUntilSettled()
53 self.assertEqual(self.getJobFromHistory('project-merge').result,
54 'SUCCESS')
55 self.assertEqual(self.getJobFromHistory('project-test1').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test2').result,
58 'SUCCESS')
59 self.assertEqual(A.data['status'], 'MERGED')
60 self.assertEqual(A.reported, 2)
61 self.assertEqual(self.getJobFromHistory('project-test1').node,
62 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070063 self.assertEqual(self.getJobFromHistory('project-test2').node,
64 'label1')
Paul Belanger0a21f0a2017-06-13 13:14:42 -040065
66
Clark Boylanb640e052014-04-03 16:41:46 -070067class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070068 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010069
Paul Belanger174a8272017-03-14 13:20:10 -040070 def test_jobs_executed(self):
71 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070072
James E. Blairb0fcae42012-07-17 11:12:10 -070073 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020074 A.addApproval('Code-Review', 2)
75 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070076 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040077 self.assertEqual(self.getJobFromHistory('project-merge').result,
78 'SUCCESS')
79 self.assertEqual(self.getJobFromHistory('project-test1').result,
80 'SUCCESS')
81 self.assertEqual(self.getJobFromHistory('project-test2').result,
82 'SUCCESS')
83 self.assertEqual(A.data['status'], 'MERGED')
84 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070085 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070086 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070087 self.assertEqual(self.getJobFromHistory('project-test2').node,
88 'label1')
James E. Blairb0fcae42012-07-17 11:12:10 -070089
James E. Blair80ac1582017-10-09 07:02:40 -070090 for stat in self.statsd.stats:
91 k, v = stat.decode('utf-8').split(':')
92 self.log.debug('stat %s:%s', k, v)
James E. Blair552b54f2016-07-22 13:55:32 -070093 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050094 # Per-driver
95 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
96 # Per-driver per-connection
97 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
98 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070099 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700100 'zuul.tenant.tenant-one.pipeline.gate.current_changes',
101 value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700102 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700103 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
104 'org_project.master.job.project-merge.SUCCESS', kind='ms')
105 self.assertReportedStat(
106 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
107 'org_project.master.job.project-merge.SUCCESS', value='1|c')
108 self.assertReportedStat(
109 'zuul.tenant.tenant-one.pipeline.gate.resident_time', kind='ms')
110 self.assertReportedStat(
111 'zuul.tenant.tenant-one.pipeline.gate.total_changes', value='1|c')
112 self.assertReportedStat(
113 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
114 'org_project.master.resident_time', kind='ms')
115 self.assertReportedStat(
116 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
117 'org_project.master.total_changes', value='1|c')
James E. Blairfaf81982017-10-10 15:42:26 -0700118 exec_key = 'zuul.executor.%s' % self.executor_server.hostname
119 self.assertReportedStat(exec_key + '.builds', value='1|c')
James E. Blair4f1731b2017-10-10 18:11:42 -0700120 self.assertReportedStat('zuul.nodepool.requested', value='1|c')
121 self.assertReportedStat('zuul.nodepool.requested.label.label1',
122 value='1|c')
123 self.assertReportedStat('zuul.nodepool.fulfilled.label.label1',
124 value='1|c')
125 self.assertReportedStat('zuul.nodepool.requested.size.1', value='1|c')
126 self.assertReportedStat('zuul.nodepool.fulfilled.size.1', value='1|c')
127 self.assertReportedStat('zuul.nodepool.current_requests', value='1|g')
James E. Blairbdd50e62017-10-21 08:18:55 -0700128 self.assertReportedStat('zuul.executors.online', value='1|g')
129 self.assertReportedStat('zuul.executors.accepting', value='1|g')
130 self.assertReportedStat('zuul.mergers.online', value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700131
James E. Blair2ef29e92017-07-21 15:25:05 -0700132 for build in self.history:
133 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700134
James E. Blair3cb10702013-08-24 08:56:03 -0700135 def test_initial_pipeline_gauges(self):
136 "Test that each pipeline reported its length on start"
James E. Blair57bf14d2017-10-21 09:09:48 -0700137 self.assertReportedStat('zuul.tenant.tenant-one.pipeline.gate.'
138 'current_changes',
James E. Blair7da51b72016-07-25 15:29:43 -0700139 value='0|g')
James E. Blair57bf14d2017-10-21 09:09:48 -0700140 self.assertReportedStat('zuul.tenant.tenant-one.pipeline.check.'
141 'current_changes',
James E. Blair7da51b72016-07-25 15:29:43 -0700142 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700143
James E. Blair34776ee2016-08-25 13:53:54 -0700144 def test_job_branch(self):
145 "Test the correct variant of a job runs on a branch"
146 self.create_branch('org/project', 'stable')
147 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200148 A.addApproval('Code-Review', 2)
149 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700150 self.waitUntilSettled()
151 self.assertEqual(self.getJobFromHistory('project-test1').result,
152 'SUCCESS')
153 self.assertEqual(self.getJobFromHistory('project-test2').result,
154 'SUCCESS')
155 self.assertEqual(A.data['status'], 'MERGED')
156 self.assertEqual(A.reported, 2,
157 "A should report start and success")
158 self.assertIn('gate', A.messages[1],
159 "A should transit gate")
160 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700161 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700162
James E. Blairda5bb7e2018-01-22 16:12:17 -0800163 @simple_layout('layouts/branch-deletion.yaml')
164 def test_branch_deletion(self):
165 "Test the correct variant of a job runs on a branch"
166 self._startMerger()
167 for f in list(self.executor_server.merger_worker.functions.keys()):
168 f = str(f)
169 if f.startswith('merger:'):
170 self.executor_server.merger_worker.unRegisterFunction(f)
171
172 self.create_branch('org/project', 'stable')
173 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
174 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
175 self.waitUntilSettled()
176 self.assertEqual(self.getJobFromHistory('project-test2').result,
177 'SUCCESS')
178
179 self.delete_branch('org/project', 'stable')
180 path = os.path.join(self.executor_src_root, 'review.example.com')
181 shutil.rmtree(path)
182
183 self.executor_server.hold_jobs_in_build = True
184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
185 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
186 self.waitUntilSettled()
187 build = self.builds[0]
188
189 # Make sure there is no stable branch in the checked out git repo.
190 pname = 'review.example.com/org/project'
191 work = build.getWorkspaceRepos([pname])
192 work = work[pname]
193 heads = set([str(x) for x in work.heads])
194 self.assertEqual(heads, set(['master']))
195 self.executor_server.hold_jobs_in_build = False
196 build.release()
197 self.waitUntilSettled()
198 self.assertEqual(self.getJobFromHistory('project-test1').result,
199 'SUCCESS')
200
James E. Blairb0fcae42012-07-17 11:12:10 -0700201 def test_parallel_changes(self):
202 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700203
Paul Belanger174a8272017-03-14 13:20:10 -0400204 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700205 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
206 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
207 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200208 A.addApproval('Code-Review', 2)
209 B.addApproval('Code-Review', 2)
210 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700211
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200212 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
213 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
214 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700215
216 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400217 self.assertEqual(len(self.builds), 1)
218 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700219 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700220
Paul Belanger174a8272017-03-14 13:20:10 -0400221 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700222 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400223 self.assertEqual(len(self.builds), 3)
224 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700225 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400226 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700227 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400228 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700229 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700230
Paul Belanger174a8272017-03-14 13:20:10 -0400231 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700232 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400233 self.assertEqual(len(self.builds), 5)
234 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700235 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400236 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700237 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700238
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400239 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700240 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400241 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700242 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700243
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400244 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700245 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700246
Paul Belanger174a8272017-03-14 13:20:10 -0400247 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700248 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400249 self.assertEqual(len(self.builds), 6)
250 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700251 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400252 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700253 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700254
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400255 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700256 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400257 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700258 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700259
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400260 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700261 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400262 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700263 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700264
Paul Belanger174a8272017-03-14 13:20:10 -0400265 self.executor_server.hold_jobs_in_build = False
266 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700267 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400268 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700269
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400270 self.assertEqual(len(self.history), 9)
271 self.assertEqual(A.data['status'], 'MERGED')
272 self.assertEqual(B.data['status'], 'MERGED')
273 self.assertEqual(C.data['status'], 'MERGED')
274 self.assertEqual(A.reported, 2)
275 self.assertEqual(B.reported, 2)
276 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700277
278 def test_failed_changes(self):
279 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400280 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700281
James E. Blairb02a3bb2012-07-30 17:49:55 -0700282 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
283 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200284 A.addApproval('Code-Review', 2)
285 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700286
Paul Belanger174a8272017-03-14 13:20:10 -0400287 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700288
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200289 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
290 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700291 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700292 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400293
Paul Belanger174a8272017-03-14 13:20:10 -0400294 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400295 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700296 # A/project-merge is complete
297 self.assertBuilds([
298 dict(name='project-test1', changes='1,1'),
299 dict(name='project-test2', changes='1,1'),
300 dict(name='project-merge', changes='1,1 2,1'),
301 ])
James E. Blaire2819012013-06-28 17:17:26 -0400302
Paul Belanger174a8272017-03-14 13:20:10 -0400303 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400304 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700305 # A/project-merge is complete
306 # B/project-merge is complete
307 self.assertBuilds([
308 dict(name='project-test1', changes='1,1'),
309 dict(name='project-test2', changes='1,1'),
310 dict(name='project-test1', changes='1,1 2,1'),
311 dict(name='project-test2', changes='1,1 2,1'),
312 ])
313
314 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400315 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700316 self.builds[0].release()
317 self.waitUntilSettled()
318
319 self.orderedRelease()
320 self.assertHistory([
321 dict(name='project-merge', result='SUCCESS', changes='1,1'),
322 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
323 dict(name='project-test1', result='FAILURE', changes='1,1'),
324 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
325 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
326 dict(name='project-test2', result='SUCCESS', changes='1,1'),
327 dict(name='project-merge', result='SUCCESS', changes='2,1'),
328 dict(name='project-test1', result='SUCCESS', changes='2,1'),
329 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700330 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700331
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400332 self.assertEqual(A.data['status'], 'NEW')
333 self.assertEqual(B.data['status'], 'MERGED')
334 self.assertEqual(A.reported, 2)
335 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700336
337 def test_independent_queues(self):
338 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700339
Paul Belanger174a8272017-03-14 13:20:10 -0400340 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700342 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
343 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200344 A.addApproval('Code-Review', 2)
345 B.addApproval('Code-Review', 2)
346 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700347
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200348 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800349 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700350
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200351 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800352 self.waitUntilSettled()
353
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200354 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700355 self.waitUntilSettled()
356
357 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700358 self.assertBuilds([
359 dict(name='project-merge', changes='1,1'),
360 dict(name='project-merge', changes='2,1'),
361 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700362
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700363 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700364 self.builds[0].release()
365 self.waitUntilSettled()
366 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700367 self.waitUntilSettled()
368 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400369 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700370 self.waitUntilSettled()
371
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700372 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700373 self.assertBuilds([
374 dict(name='project-test1', changes='1,1'),
375 dict(name='project-test2', changes='1,1'),
376 dict(name='project-test1', changes='2,1'),
377 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500378 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700379 dict(name='project-test1', changes='2,1 3,1'),
380 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800381 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700382 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700383
James E. Blair5f0d49e2016-08-19 09:36:50 -0700384 self.orderedRelease()
385 self.assertHistory([
386 dict(name='project-merge', result='SUCCESS', changes='1,1'),
387 dict(name='project-merge', result='SUCCESS', changes='2,1'),
388 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
389 dict(name='project-test1', result='SUCCESS', changes='1,1'),
390 dict(name='project-test2', result='SUCCESS', changes='1,1'),
391 dict(name='project-test1', result='SUCCESS', changes='2,1'),
392 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500393 dict(
394 name='project1-project2-integration',
395 result='SUCCESS',
396 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700397 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
398 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800399 dict(name='project1-project2-integration',
400 result='SUCCESS',
401 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700402 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700403
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400404 self.assertEqual(A.data['status'], 'MERGED')
405 self.assertEqual(B.data['status'], 'MERGED')
406 self.assertEqual(C.data['status'], 'MERGED')
407 self.assertEqual(A.reported, 2)
408 self.assertEqual(B.reported, 2)
409 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700410
411 def test_failed_change_at_head(self):
412 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700413
Paul Belanger174a8272017-03-14 13:20:10 -0400414 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
416 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
417 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200418 A.addApproval('Code-Review', 2)
419 B.addApproval('Code-Review', 2)
420 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700421
Paul Belanger174a8272017-03-14 13:20:10 -0400422 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700423
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200424 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
425 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
426 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700427
428 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700429
James E. Blairb536ecc2016-08-31 10:11:42 -0700430 self.assertBuilds([
431 dict(name='project-merge', changes='1,1'),
432 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700433
Paul Belanger174a8272017-03-14 13:20:10 -0400434 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700435 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400436 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700437 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400438 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700439 self.waitUntilSettled()
440
James E. Blairb536ecc2016-08-31 10:11:42 -0700441 self.assertBuilds([
442 dict(name='project-test1', changes='1,1'),
443 dict(name='project-test2', changes='1,1'),
444 dict(name='project-test1', changes='1,1 2,1'),
445 dict(name='project-test2', changes='1,1 2,1'),
446 dict(name='project-test1', changes='1,1 2,1 3,1'),
447 dict(name='project-test2', changes='1,1 2,1 3,1'),
448 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700449
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400450 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700451 self.waitUntilSettled()
452
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400453 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700454 self.assertBuilds([
455 dict(name='project-test2', changes='1,1'),
456 dict(name='project-merge', changes='2,1'),
457 ])
458 # Unordered history comparison because the aborts can finish
459 # in any order.
460 self.assertHistory([
461 dict(name='project-merge', result='SUCCESS',
462 changes='1,1'),
463 dict(name='project-merge', result='SUCCESS',
464 changes='1,1 2,1'),
465 dict(name='project-merge', result='SUCCESS',
466 changes='1,1 2,1 3,1'),
467 dict(name='project-test1', result='FAILURE',
468 changes='1,1'),
469 dict(name='project-test1', result='ABORTED',
470 changes='1,1 2,1'),
471 dict(name='project-test2', result='ABORTED',
472 changes='1,1 2,1'),
473 dict(name='project-test1', result='ABORTED',
474 changes='1,1 2,1 3,1'),
475 dict(name='project-test2', result='ABORTED',
476 changes='1,1 2,1 3,1'),
477 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700478
Paul Belanger174a8272017-03-14 13:20:10 -0400479 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700480 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400481 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700482 self.waitUntilSettled()
483 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700484
James E. Blairb536ecc2016-08-31 10:11:42 -0700485 self.assertBuilds([])
486 self.assertHistory([
487 dict(name='project-merge', result='SUCCESS',
488 changes='1,1'),
489 dict(name='project-merge', result='SUCCESS',
490 changes='1,1 2,1'),
491 dict(name='project-merge', result='SUCCESS',
492 changes='1,1 2,1 3,1'),
493 dict(name='project-test1', result='FAILURE',
494 changes='1,1'),
495 dict(name='project-test1', result='ABORTED',
496 changes='1,1 2,1'),
497 dict(name='project-test2', result='ABORTED',
498 changes='1,1 2,1'),
499 dict(name='project-test1', result='ABORTED',
500 changes='1,1 2,1 3,1'),
501 dict(name='project-test2', result='ABORTED',
502 changes='1,1 2,1 3,1'),
503 dict(name='project-merge', result='SUCCESS',
504 changes='2,1'),
505 dict(name='project-merge', result='SUCCESS',
506 changes='2,1 3,1'),
507 dict(name='project-test2', result='SUCCESS',
508 changes='1,1'),
509 dict(name='project-test1', result='SUCCESS',
510 changes='2,1'),
511 dict(name='project-test2', result='SUCCESS',
512 changes='2,1'),
513 dict(name='project-test1', result='SUCCESS',
514 changes='2,1 3,1'),
515 dict(name='project-test2', result='SUCCESS',
516 changes='2,1 3,1'),
517 ], ordered=False)
518
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400519 self.assertEqual(A.data['status'], 'NEW')
520 self.assertEqual(B.data['status'], 'MERGED')
521 self.assertEqual(C.data['status'], 'MERGED')
522 self.assertEqual(A.reported, 2)
523 self.assertEqual(B.reported, 2)
524 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700525
James E. Blair0aac4872013-08-23 14:02:38 -0700526 def test_failed_change_in_middle(self):
527 "Test a failed change in the middle of the queue"
528
Paul Belanger174a8272017-03-14 13:20:10 -0400529 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700530 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
531 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
532 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200533 A.addApproval('Code-Review', 2)
534 B.addApproval('Code-Review', 2)
535 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700536
Paul Belanger174a8272017-03-14 13:20:10 -0400537 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700538
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200539 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
540 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
541 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700542
543 self.waitUntilSettled()
544
Paul Belanger174a8272017-03-14 13:20:10 -0400545 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700546 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400547 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700548 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400549 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700550 self.waitUntilSettled()
551
552 self.assertEqual(len(self.builds), 6)
553 self.assertEqual(self.builds[0].name, 'project-test1')
554 self.assertEqual(self.builds[1].name, 'project-test2')
555 self.assertEqual(self.builds[2].name, 'project-test1')
556 self.assertEqual(self.builds[3].name, 'project-test2')
557 self.assertEqual(self.builds[4].name, 'project-test1')
558 self.assertEqual(self.builds[5].name, 'project-test2')
559
560 self.release(self.builds[2])
561 self.waitUntilSettled()
562
James E. Blair972e3c72013-08-29 12:04:55 -0700563 # project-test1 and project-test2 for A
564 # project-test2 for B
565 # project-merge for C (without B)
566 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700567 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
568
Paul Belanger174a8272017-03-14 13:20:10 -0400569 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700570 self.waitUntilSettled()
571
572 # project-test1 and project-test2 for A
573 # project-test2 for B
574 # project-test1 and project-test2 for C
575 self.assertEqual(len(self.builds), 5)
576
Paul Belanger2e2a0492016-10-30 18:33:37 -0400577 tenant = self.sched.abide.tenants.get('tenant-one')
578 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700579 builds = items[0].current_build_set.getBuilds()
580 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
581 self.assertEqual(self.countJobResults(builds, None), 2)
582 builds = items[1].current_build_set.getBuilds()
583 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
584 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
585 self.assertEqual(self.countJobResults(builds, None), 1)
586 builds = items[2].current_build_set.getBuilds()
587 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700588 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700589
Paul Belanger174a8272017-03-14 13:20:10 -0400590 self.executor_server.hold_jobs_in_build = False
591 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700592 self.waitUntilSettled()
593
594 self.assertEqual(len(self.builds), 0)
595 self.assertEqual(len(self.history), 12)
596 self.assertEqual(A.data['status'], 'MERGED')
597 self.assertEqual(B.data['status'], 'NEW')
598 self.assertEqual(C.data['status'], 'MERGED')
599 self.assertEqual(A.reported, 2)
600 self.assertEqual(B.reported, 2)
601 self.assertEqual(C.reported, 2)
602
James E. Blaird466dc42012-07-31 10:42:56 -0700603 def test_failed_change_at_head_with_queue(self):
604 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700605
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700606 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700607 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
608 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
609 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200610 A.addApproval('Code-Review', 2)
611 B.addApproval('Code-Review', 2)
612 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700613
Paul Belanger174a8272017-03-14 13:20:10 -0400614 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700615
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200616 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
617 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
618 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700619
620 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700621 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400622 self.assertEqual(len(self.builds), 0)
623 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700624 self.assertEqual(queue[0].name, b'executor:execute')
625 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800626 self.assertEqual(job_args['job'], 'project-merge')
627 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700628
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700629 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700630 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700631 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700632 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700633 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700634 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700635 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700636
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400637 self.assertEqual(len(self.builds), 0)
638 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800639
640 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700641 json.loads(queue[0].arguments.decode('utf8'))['job'],
642 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800643 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700644 json.loads(queue[1].arguments.decode('utf8'))['job'],
645 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800646 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700647 json.loads(queue[2].arguments.decode('utf8'))['job'],
648 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800649 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700650 json.loads(queue[3].arguments.decode('utf8'))['job'],
651 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800652 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700653 json.loads(queue[4].arguments.decode('utf8'))['job'],
654 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800655 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700656 json.loads(queue[5].arguments.decode('utf8'))['job'],
657 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700658
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700659 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700660 self.waitUntilSettled()
661
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400662 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700663 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400664 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
665 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700666
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700667 self.gearman_server.hold_jobs_in_queue = False
668 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700669 self.waitUntilSettled()
670
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400671 self.assertEqual(len(self.builds), 0)
672 self.assertEqual(len(self.history), 11)
673 self.assertEqual(A.data['status'], 'NEW')
674 self.assertEqual(B.data['status'], 'MERGED')
675 self.assertEqual(C.data['status'], 'MERGED')
676 self.assertEqual(A.reported, 2)
677 self.assertEqual(B.reported, 2)
678 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700679
James E. Blairce8a2132016-05-19 15:21:52 -0700680 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400681 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700682 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200683 A.addApproval('Code-Review', 2)
684 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700685 self.waitUntilSettled()
686 time.sleep(2)
687
James E. Blairae0f23c2017-09-13 10:55:15 -0600688 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700689 found_job = None
690 for pipeline in data['pipelines']:
691 if pipeline['name'] != 'gate':
692 continue
693 for queue in pipeline['change_queues']:
694 for head in queue['heads']:
695 for item in head:
696 for job in item['jobs']:
697 if job['name'] == 'project-merge':
698 found_job = job
699 break
700
701 self.assertIsNotNone(found_job)
702 if iteration == 1:
703 self.assertIsNotNone(found_job['estimated_time'])
704 self.assertIsNone(found_job['remaining_time'])
705 else:
706 self.assertIsNotNone(found_job['estimated_time'])
707 self.assertTrue(found_job['estimated_time'] >= 2)
708 self.assertIsNotNone(found_job['remaining_time'])
709
Paul Belanger174a8272017-03-14 13:20:10 -0400710 self.executor_server.hold_jobs_in_build = False
711 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700712 self.waitUntilSettled()
713
714 def test_time_database(self):
715 "Test the time database"
716
717 self._test_time_database(1)
718 self._test_time_database(2)
719
James E. Blairfef71632013-09-23 11:15:47 -0700720 def test_two_failed_changes_at_head(self):
721 "Test that changes are reparented correctly if 2 fail at head"
722
Paul Belanger174a8272017-03-14 13:20:10 -0400723 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700724 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
725 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
726 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200727 A.addApproval('Code-Review', 2)
728 B.addApproval('Code-Review', 2)
729 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700730
Paul Belanger174a8272017-03-14 13:20:10 -0400731 self.executor_server.failJob('project-test1', A)
732 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700733
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200734 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
735 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
736 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700737 self.waitUntilSettled()
738
Paul Belanger174a8272017-03-14 13:20:10 -0400739 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700740 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400741 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700742 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400743 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700744 self.waitUntilSettled()
745
746 self.assertEqual(len(self.builds), 6)
747 self.assertEqual(self.builds[0].name, 'project-test1')
748 self.assertEqual(self.builds[1].name, 'project-test2')
749 self.assertEqual(self.builds[2].name, 'project-test1')
750 self.assertEqual(self.builds[3].name, 'project-test2')
751 self.assertEqual(self.builds[4].name, 'project-test1')
752 self.assertEqual(self.builds[5].name, 'project-test2')
753
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400754 self.assertTrue(self.builds[0].hasChanges(A))
755 self.assertTrue(self.builds[2].hasChanges(A))
756 self.assertTrue(self.builds[2].hasChanges(B))
757 self.assertTrue(self.builds[4].hasChanges(A))
758 self.assertTrue(self.builds[4].hasChanges(B))
759 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700760
761 # Fail change B first
762 self.release(self.builds[2])
763 self.waitUntilSettled()
764
765 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400766 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700767 self.waitUntilSettled()
768
769 self.assertEqual(len(self.builds), 5)
770 self.assertEqual(self.builds[0].name, 'project-test1')
771 self.assertEqual(self.builds[1].name, 'project-test2')
772 self.assertEqual(self.builds[2].name, 'project-test2')
773 self.assertEqual(self.builds[3].name, 'project-test1')
774 self.assertEqual(self.builds[4].name, 'project-test2')
775
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400776 self.assertTrue(self.builds[1].hasChanges(A))
777 self.assertTrue(self.builds[2].hasChanges(A))
778 self.assertTrue(self.builds[2].hasChanges(B))
779 self.assertTrue(self.builds[4].hasChanges(A))
780 self.assertFalse(self.builds[4].hasChanges(B))
781 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700782
783 # Finish running all passing jobs for change A
784 self.release(self.builds[1])
785 self.waitUntilSettled()
786 # Fail and report change A
787 self.release(self.builds[0])
788 self.waitUntilSettled()
789
790 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400791 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700792 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400793 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700794 self.waitUntilSettled()
795
796 self.assertEqual(len(self.builds), 4)
797 self.assertEqual(self.builds[0].name, 'project-test1') # B
798 self.assertEqual(self.builds[1].name, 'project-test2') # B
799 self.assertEqual(self.builds[2].name, 'project-test1') # C
800 self.assertEqual(self.builds[3].name, 'project-test2') # C
801
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400802 self.assertFalse(self.builds[1].hasChanges(A))
803 self.assertTrue(self.builds[1].hasChanges(B))
804 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700805
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400806 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700807 # After A failed and B and C restarted, B should be back in
808 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400809 self.assertTrue(self.builds[2].hasChanges(B))
810 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700811
Paul Belanger174a8272017-03-14 13:20:10 -0400812 self.executor_server.hold_jobs_in_build = False
813 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700814 self.waitUntilSettled()
815
816 self.assertEqual(len(self.builds), 0)
817 self.assertEqual(len(self.history), 21)
818 self.assertEqual(A.data['status'], 'NEW')
819 self.assertEqual(B.data['status'], 'NEW')
820 self.assertEqual(C.data['status'], 'MERGED')
821 self.assertEqual(A.reported, 2)
822 self.assertEqual(B.reported, 2)
823 self.assertEqual(C.reported, 2)
824
James E. Blair8c803f82012-07-31 16:25:42 -0700825 def test_patch_order(self):
826 "Test that dependent patches are tested in the right order"
827 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
828 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
829 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200830 A.addApproval('Code-Review', 2)
831 B.addApproval('Code-Review', 2)
832 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700833
834 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
835 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
836 M2.setMerged()
837 M1.setMerged()
838
839 # C -> B -> A -> M1 -> M2
840 # M2 is here to make sure it is never queried. If it is, it
841 # means zuul is walking down the entire history of merged
842 # changes.
843
844 C.setDependsOn(B, 1)
845 B.setDependsOn(A, 1)
846 A.setDependsOn(M1, 1)
847 M1.setDependsOn(M2, 1)
848
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200849 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700850
851 self.waitUntilSettled()
852
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400853 self.assertEqual(A.data['status'], 'NEW')
854 self.assertEqual(B.data['status'], 'NEW')
855 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700856
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200857 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
858 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700859
860 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400861 self.assertEqual(M2.queried, 0)
862 self.assertEqual(A.data['status'], 'MERGED')
863 self.assertEqual(B.data['status'], 'MERGED')
864 self.assertEqual(C.data['status'], 'MERGED')
865 self.assertEqual(A.reported, 2)
866 self.assertEqual(B.reported, 2)
867 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700868
James E. Blair063672f2015-01-29 13:09:12 -0800869 def test_needed_changes_enqueue(self):
870 "Test that a needed change is enqueued ahead"
871 # A Given a git tree like this, if we enqueue
872 # / \ change C, we should walk up and down the tree
873 # B G and enqueue changes in the order ABCDEFG.
874 # /|\ This is also the order that you would get if
875 # *C E F you enqueued changes in the order ABCDEFG, so
876 # / the ordering is stable across re-enqueue events.
877 # D
878
879 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
880 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
881 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
882 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
883 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
884 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
885 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
886 B.setDependsOn(A, 1)
887 C.setDependsOn(B, 1)
888 D.setDependsOn(C, 1)
889 E.setDependsOn(B, 1)
890 F.setDependsOn(B, 1)
891 G.setDependsOn(A, 1)
892
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200893 A.addApproval('Code-Review', 2)
894 B.addApproval('Code-Review', 2)
895 C.addApproval('Code-Review', 2)
896 D.addApproval('Code-Review', 2)
897 E.addApproval('Code-Review', 2)
898 F.addApproval('Code-Review', 2)
899 G.addApproval('Code-Review', 2)
900 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800901
902 self.waitUntilSettled()
903
904 self.assertEqual(A.data['status'], 'NEW')
905 self.assertEqual(B.data['status'], 'NEW')
906 self.assertEqual(C.data['status'], 'NEW')
907 self.assertEqual(D.data['status'], 'NEW')
908 self.assertEqual(E.data['status'], 'NEW')
909 self.assertEqual(F.data['status'], 'NEW')
910 self.assertEqual(G.data['status'], 'NEW')
911
912 # We're about to add approvals to changes without adding the
913 # triggering events to Zuul, so that we can be sure that it is
914 # enqueing the changes based on dependencies, not because of
915 # triggering events. Since it will have the changes cached
916 # already (without approvals), we need to clear the cache
917 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400918 for connection in self.connections.connections.values():
James E. Blairb0a95ab2017-10-18 09:39:18 -0700919 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800920
Paul Belanger174a8272017-03-14 13:20:10 -0400921 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200922 A.addApproval('Approved', 1)
923 B.addApproval('Approved', 1)
924 D.addApproval('Approved', 1)
925 E.addApproval('Approved', 1)
926 F.addApproval('Approved', 1)
927 G.addApproval('Approved', 1)
928 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800929
930 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400931 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800932 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400933 self.executor_server.hold_jobs_in_build = False
934 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800935 self.waitUntilSettled()
936
937 self.assertEqual(A.data['status'], 'MERGED')
938 self.assertEqual(B.data['status'], 'MERGED')
939 self.assertEqual(C.data['status'], 'MERGED')
940 self.assertEqual(D.data['status'], 'MERGED')
941 self.assertEqual(E.data['status'], 'MERGED')
942 self.assertEqual(F.data['status'], 'MERGED')
943 self.assertEqual(G.data['status'], 'MERGED')
944 self.assertEqual(A.reported, 2)
945 self.assertEqual(B.reported, 2)
946 self.assertEqual(C.reported, 2)
947 self.assertEqual(D.reported, 2)
948 self.assertEqual(E.reported, 2)
949 self.assertEqual(F.reported, 2)
950 self.assertEqual(G.reported, 2)
951 self.assertEqual(self.history[6].changes,
952 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
953
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100954 def test_source_cache(self):
955 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400956 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700957
958 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
959 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
960 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200961 A.addApproval('Code-Review', 2)
962 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700963
964 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
965 M1.setMerged()
966
967 B.setDependsOn(A, 1)
968 A.setDependsOn(M1, 1)
969
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200970 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700971 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
972
973 self.waitUntilSettled()
974
975 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700976 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700977 build.release()
978 self.waitUntilSettled()
979 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700980 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700981 build.release()
982 self.waitUntilSettled()
983
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200984 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700985 self.waitUntilSettled()
986
Joshua Hesketh352264b2015-08-11 23:42:08 +1000987 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700988 # there should still be changes in the cache
James E. Blairb0a95ab2017-10-18 09:39:18 -0700989 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700990
Paul Belanger174a8272017-03-14 13:20:10 -0400991 self.executor_server.hold_jobs_in_build = False
992 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700993 self.waitUntilSettled()
994
995 self.assertEqual(A.data['status'], 'MERGED')
996 self.assertEqual(B.data['status'], 'MERGED')
997 self.assertEqual(A.queried, 2) # Initial and isMerged
998 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
999
James E. Blair8c803f82012-07-31 16:25:42 -07001000 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001001 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001002 # TODO: move to test_gerrit (this is a unit test!)
1003 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -04001004 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07001005 (trusted, project) = tenant.getProject('org/project')
1006 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -04001007
1008 # TODO(pabelanger): As we add more source / trigger APIs we should make
1009 # it easier for users to create events for testing.
1010 event = zuul.model.TriggerEvent()
1011 event.trigger_name = 'gerrit'
1012 event.change_number = '1'
1013 event.patch_number = '2'
1014
1015 a = source.getChange(event)
1016 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -07001017 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001018
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001019 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -04001020 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -07001021 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001022
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001023 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -04001024 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -07001025 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -07001026
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001027 def test_project_merge_conflict(self):
1028 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001029
1030 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001031 A = self.fake_gerrit.addFakeChange('org/project',
1032 'master', 'A',
1033 files={'conflict': 'foo'})
1034 B = self.fake_gerrit.addFakeChange('org/project',
1035 'master', 'B',
1036 files={'conflict': 'bar'})
1037 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -07001038 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001039 A.addApproval('Code-Review', 2)
1040 B.addApproval('Code-Review', 2)
1041 C.addApproval('Code-Review', 2)
1042 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1043 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1044 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -07001045 self.waitUntilSettled()
1046
James E. Blair6736beb2013-07-11 15:18:15 -07001047 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -07001048 self.assertEqual(C.reported, 1)
1049
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001050 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001051 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001052 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001053 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001054 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001055 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001056
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001057 self.gearman_server.hold_jobs_in_queue = False
1058 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001059 self.waitUntilSettled()
1060
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001061 self.assertEqual(A.data['status'], 'MERGED')
1062 self.assertEqual(B.data['status'], 'NEW')
1063 self.assertEqual(C.data['status'], 'MERGED')
1064 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001065 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001066 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001067
1068 self.assertHistory([
1069 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1072 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1073 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1074 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1075 ], ordered=False)
1076
1077 def test_delayed_merge_conflict(self):
1078 "Test that delayed check merge conflicts are handled properly"
1079
1080 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001081 # the executor sucesfully merges a change based on an old
1082 # repo state (frozen by the scheduler) which would otherwise
1083 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001084 self.gearman_server.hold_jobs_in_queue = True
1085 A = self.fake_gerrit.addFakeChange('org/project',
1086 'master', 'A',
1087 files={'conflict': 'foo'})
1088 B = self.fake_gerrit.addFakeChange('org/project',
1089 'master', 'B',
1090 files={'conflict': 'bar'})
1091 C = self.fake_gerrit.addFakeChange('org/project',
1092 'master', 'C')
1093 C.setDependsOn(B, 1)
1094
1095 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001096 A.addApproval('Code-Review', 2)
1097 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001098 self.waitUntilSettled()
1099 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001100 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001101 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1102 self.waitUntilSettled()
1103
1104 self.assertEqual(A.reported, 1)
1105 self.assertEqual(B.reported, 0) # Check does not report start
1106 self.assertEqual(C.reported, 0) # Check does not report start
1107
1108 # A merges while B and C are queued in check
1109 # Release A project-merge
1110 queue = self.gearman_server.getQueue()
1111 self.release(queue[0])
1112 self.waitUntilSettled()
1113
1114 # Release A project-test*
1115 # gate has higher precedence, so A's test jobs are added in
1116 # front of the merge jobs for B and C
1117 queue = self.gearman_server.getQueue()
1118 self.release(queue[0])
1119 self.release(queue[1])
1120 self.waitUntilSettled()
1121
1122 self.assertEqual(A.data['status'], 'MERGED')
1123 self.assertEqual(B.data['status'], 'NEW')
1124 self.assertEqual(C.data['status'], 'NEW')
1125 self.assertEqual(A.reported, 2)
1126 self.assertEqual(B.reported, 0)
1127 self.assertEqual(C.reported, 0)
1128 self.assertHistory([
1129 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1130 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1131 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1132 ], ordered=False)
1133
1134 # B and C report merge conflicts
1135 # Release B project-merge
1136 queue = self.gearman_server.getQueue()
1137 self.release(queue[0])
1138 self.waitUntilSettled()
1139
1140 # Release C
1141 self.gearman_server.hold_jobs_in_queue = False
1142 self.gearman_server.release()
1143 self.waitUntilSettled()
1144
1145 self.assertEqual(A.data['status'], 'MERGED')
1146 self.assertEqual(B.data['status'], 'NEW')
1147 self.assertEqual(C.data['status'], 'NEW')
1148 self.assertEqual(A.reported, 2)
1149 self.assertEqual(B.reported, 1)
1150 self.assertEqual(C.reported, 1)
1151
1152 self.assertHistory([
1153 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1154 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1155 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001156 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1157 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1158 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1159 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1160 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1161 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001162 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001163
James E. Blairdaabed22012-08-15 15:38:57 -07001164 def test_post(self):
1165 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001166 p = "review.example.com/org/project"
1167 upstream = self.getUpstreamRepos([p])
1168 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1169 A.setMerged()
1170 A_commit = str(upstream[p].commit('master'))
1171 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001172
Zhongyue Luo5d556072012-09-21 02:00:47 +09001173 e = {
1174 "type": "ref-updated",
1175 "submitter": {
1176 "name": "User Name",
1177 },
1178 "refUpdate": {
1179 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001180 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001181 "refName": "master",
1182 "project": "org/project",
1183 }
1184 }
James E. Blairdaabed22012-08-15 15:38:57 -07001185 self.fake_gerrit.addEvent(e)
1186 self.waitUntilSettled()
1187
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001188 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001189 self.assertEqual(len(self.history), 1)
1190 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001191
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001192 def test_post_ignore_deletes(self):
1193 "Test that deleting refs does not trigger post jobs"
1194
1195 e = {
1196 "type": "ref-updated",
1197 "submitter": {
1198 "name": "User Name",
1199 },
1200 "refUpdate": {
1201 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1202 "newRev": "0000000000000000000000000000000000000000",
1203 "refName": "master",
1204 "project": "org/project",
1205 }
1206 }
1207 self.fake_gerrit.addEvent(e)
1208 self.waitUntilSettled()
1209
1210 job_names = [x.name for x in self.history]
1211 self.assertEqual(len(self.history), 0)
1212 self.assertNotIn('project-post', job_names)
1213
James E. Blaircc324192017-04-19 14:36:08 -07001214 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001215 def test_post_ignore_deletes_negative(self):
1216 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001217 e = {
1218 "type": "ref-updated",
1219 "submitter": {
1220 "name": "User Name",
1221 },
1222 "refUpdate": {
1223 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1224 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001225 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001226 "project": "org/project",
1227 }
1228 }
1229 self.fake_gerrit.addEvent(e)
1230 self.waitUntilSettled()
1231
1232 job_names = [x.name for x in self.history]
1233 self.assertEqual(len(self.history), 1)
1234 self.assertIn('project-post', job_names)
1235
James E. Blairec056492016-07-22 09:45:56 -07001236 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001237 def test_build_configuration_branch_interaction(self):
1238 "Test that switching between branches works"
1239 self.test_build_configuration()
1240 self.test_build_configuration_branch()
1241 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001242 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001243 repo = git.Repo(path)
1244 repo.heads.master.commit = repo.commit('init')
1245 self.test_build_configuration()
1246
James E. Blairdb93b302017-07-19 15:33:11 -07001247 def test_dependent_changes_rebase(self):
1248 # Test that no errors occur when we walk a dependency tree
1249 # with an unused leaf node due to a rebase.
1250 # Start by constructing: C -> B -> A
1251 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1252 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1253 B.setDependsOn(A, 1)
1254
1255 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1256 C.setDependsOn(B, 1)
1257
1258 # Then rebase to form: D -> C -> A
1259 C.addPatchset() # C,2
1260 C.setDependsOn(A, 1)
1261
1262 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1263 D.setDependsOn(C, 2)
1264
1265 # Walk the entire tree
1266 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1267 self.waitUntilSettled()
1268 self.assertEqual(len(self.history), 3)
1269
1270 # Verify that walking just part of the tree still works
1271 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1272 self.waitUntilSettled()
1273 self.assertEqual(len(self.history), 6)
1274
James E. Blaircaec0c52012-08-22 14:52:22 -07001275 def test_dependent_changes_dequeue(self):
1276 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001277
James E. Blaircaec0c52012-08-22 14:52:22 -07001278 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1279 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1280 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001281 A.addApproval('Code-Review', 2)
1282 B.addApproval('Code-Review', 2)
1283 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001284
1285 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1286 M1.setMerged()
1287
1288 # C -> B -> A -> M1
1289
1290 C.setDependsOn(B, 1)
1291 B.setDependsOn(A, 1)
1292 A.setDependsOn(M1, 1)
1293
Paul Belanger174a8272017-03-14 13:20:10 -04001294 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001295
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001296 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1297 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1298 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001299
1300 self.waitUntilSettled()
1301
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001302 self.assertEqual(A.data['status'], 'NEW')
1303 self.assertEqual(A.reported, 2)
1304 self.assertEqual(B.data['status'], 'NEW')
1305 self.assertEqual(B.reported, 2)
1306 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001307 self.assertIn('This change depends on a change that failed to merge.',
1308 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001309 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001310
James E. Blair972e3c72013-08-29 12:04:55 -07001311 def test_failing_dependent_changes(self):
1312 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001313 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001314 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1315 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1316 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1317 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1318 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001319 A.addApproval('Code-Review', 2)
1320 B.addApproval('Code-Review', 2)
1321 C.addApproval('Code-Review', 2)
1322 D.addApproval('Code-Review', 2)
1323 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001324
1325 # E, D -> C -> B, A
1326
1327 D.setDependsOn(C, 1)
1328 C.setDependsOn(B, 1)
1329
Paul Belanger174a8272017-03-14 13:20:10 -04001330 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001331
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001332 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1333 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1334 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1335 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1336 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001337
1338 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001339 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001340 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001341 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001342 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001343 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001344 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001345 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001346 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001347 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001348 self.waitUntilSettled()
1349
Paul Belanger174a8272017-03-14 13:20:10 -04001350 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001351 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001352 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001353 build.release()
1354 self.waitUntilSettled()
1355
Paul Belanger174a8272017-03-14 13:20:10 -04001356 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001357 self.waitUntilSettled()
1358
1359 self.assertEqual(A.data['status'], 'MERGED')
1360 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001361 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001362 self.assertEqual(B.data['status'], 'NEW')
1363 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001364 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001365 self.assertEqual(C.data['status'], 'NEW')
1366 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001367 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001368 self.assertEqual(D.data['status'], 'NEW')
1369 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001370 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001371 self.assertEqual(E.data['status'], 'MERGED')
1372 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001373 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001374 self.assertEqual(len(self.history), 18)
1375
James E. Blairec590122012-08-22 15:19:31 -07001376 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001377 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001378 # If it's dequeued more than once, we should see extra
1379 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001380
Paul Belanger174a8272017-03-14 13:20:10 -04001381 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001382 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1383 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1384 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001385 A.addApproval('Code-Review', 2)
1386 B.addApproval('Code-Review', 2)
1387 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001388
Paul Belanger174a8272017-03-14 13:20:10 -04001389 self.executor_server.failJob('project-test1', A)
1390 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001391
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001392 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1393 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1394 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001395
1396 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001397
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001398 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001399 self.assertEqual(self.builds[0].name, 'project-merge')
1400 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001401
Paul Belanger174a8272017-03-14 13:20:10 -04001402 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001403 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001404 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001405 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001406 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001407 self.waitUntilSettled()
1408
James E. Blair6df67132017-04-19 13:53:20 -07001409 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001410 self.assertEqual(self.builds[0].name, 'project-test1')
1411 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001412 self.assertEqual(self.builds[2].name, 'project-test1')
1413 self.assertEqual(self.builds[3].name, 'project-test2')
1414 self.assertEqual(self.builds[4].name, 'project-test1')
1415 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001416
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001417 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001418 self.waitUntilSettled()
1419
James E. Blair6df67132017-04-19 13:53:20 -07001420 self.assertEqual(len(self.builds), 2) # test2, merge for B
1421 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001422
Paul Belanger174a8272017-03-14 13:20:10 -04001423 self.executor_server.hold_jobs_in_build = False
1424 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001425 self.waitUntilSettled()
1426
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001427 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001428 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001429
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001430 self.assertEqual(A.data['status'], 'NEW')
1431 self.assertEqual(B.data['status'], 'MERGED')
1432 self.assertEqual(C.data['status'], 'MERGED')
1433 self.assertEqual(A.reported, 2)
1434 self.assertEqual(B.reported, 2)
1435 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001436
James E. Blaircc020532017-04-19 13:04:44 -07001437 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001438 def test_nonvoting_job(self):
1439 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001440
James E. Blair4ec821f2012-08-23 15:28:28 -07001441 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1442 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001443 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001444 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001445 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001446
1447 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001448
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001449 self.assertEqual(A.data['status'], 'MERGED')
1450 self.assertEqual(A.reported, 2)
1451 self.assertEqual(
1452 self.getJobFromHistory('nonvoting-project-merge').result,
1453 'SUCCESS')
1454 self.assertEqual(
1455 self.getJobFromHistory('nonvoting-project-test1').result,
1456 'SUCCESS')
1457 self.assertEqual(
1458 self.getJobFromHistory('nonvoting-project-test2').result,
1459 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001460
James E. Blair2ef29e92017-07-21 15:25:05 -07001461 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1462 parameters['zuul']['voting'])
1463 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1464 parameters['zuul']['voting'])
1465 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1466 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001467
James E. Blaire0487072012-08-29 17:38:31 -07001468 def test_check_queue_success(self):
1469 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001470
James E. Blaire0487072012-08-29 17:38:31 -07001471 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1472 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1473
1474 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001475
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001476 self.assertEqual(A.data['status'], 'NEW')
1477 self.assertEqual(A.reported, 1)
1478 self.assertEqual(self.getJobFromHistory('project-merge').result,
1479 'SUCCESS')
1480 self.assertEqual(self.getJobFromHistory('project-test1').result,
1481 'SUCCESS')
1482 self.assertEqual(self.getJobFromHistory('project-test2').result,
1483 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001484
1485 def test_check_queue_failure(self):
1486 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001487
James E. Blaire0487072012-08-29 17:38:31 -07001488 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001489 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001490 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1491
1492 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001493
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001494 self.assertEqual(A.data['status'], 'NEW')
1495 self.assertEqual(A.reported, 1)
1496 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001497 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001498 self.assertEqual(self.getJobFromHistory('project-test1').result,
1499 'SUCCESS')
1500 self.assertEqual(self.getJobFromHistory('project-test2').result,
1501 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001502
David Shrewsburyffab07a2017-07-24 12:45:07 -04001503 @simple_layout('layouts/autohold.yaml')
1504 def test_autohold(self):
David Shrewsburyffab07a2017-07-24 12:45:07 -04001505 client = zuul.rpcclient.RPCClient('127.0.0.1',
1506 self.gearman_server.port)
1507 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001508 r = client.autohold('tenant-one', 'org/project', 'project-test2',
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001509 "", "", "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001510 self.assertTrue(r)
1511
Clark Boylanaeb5a122017-10-20 14:00:11 -07001512 # First check that successful jobs do not autohold
1513 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
David Shrewsburyffab07a2017-07-24 12:45:07 -04001514 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1515
1516 self.waitUntilSettled()
1517
1518 self.assertEqual(A.data['status'], 'NEW')
1519 self.assertEqual(A.reported, 1)
Clark Boylanaeb5a122017-10-20 14:00:11 -07001520 # project-test2
1521 self.assertEqual(self.history[0].result, 'SUCCESS')
1522
1523 # Check nodepool for a held node
1524 held_node = None
1525 for node in self.fake_nodepool.getNodes():
1526 if node['state'] == zuul.model.STATE_HOLD:
1527 held_node = node
1528 break
1529 self.assertIsNone(held_node)
1530
1531 # Now test that failed jobs are autoheld
1532 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1533 self.executor_server.failJob('project-test2', B)
1534 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1535
1536 self.waitUntilSettled()
1537
1538 self.assertEqual(B.data['status'], 'NEW')
1539 self.assertEqual(B.reported, 1)
1540 # project-test2
1541 self.assertEqual(self.history[1].result, 'FAILURE')
David Shrewsburyffab07a2017-07-24 12:45:07 -04001542
1543 # Check nodepool for a held node
1544 held_node = None
1545 for node in self.fake_nodepool.getNodes():
1546 if node['state'] == zuul.model.STATE_HOLD:
1547 held_node = node
1548 break
1549 self.assertIsNotNone(held_node)
1550
1551 # Validate node has recorded the failed job
1552 self.assertEqual(
1553 held_node['hold_job'],
1554 " ".join(['tenant-one',
1555 'review.example.com/org/project',
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001556 'project-test2', '.*'])
David Shrewsburyffab07a2017-07-24 12:45:07 -04001557 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001558 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001559
1560 # Another failed change should not hold any more nodes
Clark Boylanaeb5a122017-10-20 14:00:11 -07001561 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1562 self.executor_server.failJob('project-test2', C)
1563 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
David Shrewsburyffab07a2017-07-24 12:45:07 -04001564 self.waitUntilSettled()
Clark Boylanaeb5a122017-10-20 14:00:11 -07001565 self.assertEqual(C.data['status'], 'NEW')
1566 self.assertEqual(C.reported, 1)
1567 # project-test2
1568 self.assertEqual(self.history[2].result, 'FAILURE')
David Shrewsburyffab07a2017-07-24 12:45:07 -04001569
1570 held_nodes = 0
1571 for node in self.fake_nodepool.getNodes():
1572 if node['state'] == zuul.model.STATE_HOLD:
1573 held_nodes += 1
1574 self.assertEqual(held_nodes, 1)
1575
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001576 def _test_autohold_scoped(self, change_obj, change, ref):
1577 client = zuul.rpcclient.RPCClient('127.0.0.1',
1578 self.gearman_server.port)
1579 self.addCleanup(client.shutdown)
1580
1581 # create two changes on the same project, and autohold request
1582 # for one of them.
1583 other = self.fake_gerrit.addFakeChange(
1584 'org/project', 'master', 'other'
1585 )
1586
1587 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1588 str(change), ref, "reason text", 1)
1589 self.assertTrue(r)
1590
1591 # First, check that an unrelated job does not trigger autohold, even
1592 # when it failed
1593 self.executor_server.failJob('project-test2', other)
1594 self.fake_gerrit.addEvent(other.getPatchsetCreatedEvent(1))
1595
1596 self.waitUntilSettled()
1597
1598 self.assertEqual(other.data['status'], 'NEW')
1599 self.assertEqual(other.reported, 1)
1600 # project-test2
1601 self.assertEqual(self.history[0].result, 'FAILURE')
1602
1603 # Check nodepool for a held node
1604 held_node = None
1605 for node in self.fake_nodepool.getNodes():
1606 if node['state'] == zuul.model.STATE_HOLD:
1607 held_node = node
1608 break
1609 self.assertIsNone(held_node)
1610
1611 # And then verify that failed job for the defined change
1612 # triggers the autohold
1613
1614 self.executor_server.failJob('project-test2', change_obj)
1615 self.fake_gerrit.addEvent(change_obj.getPatchsetCreatedEvent(1))
1616
1617 self.waitUntilSettled()
1618
1619 self.assertEqual(change_obj.data['status'], 'NEW')
1620 self.assertEqual(change_obj.reported, 1)
1621 # project-test2
1622 self.assertEqual(self.history[1].result, 'FAILURE')
1623
1624 # Check nodepool for a held node
1625 held_node = None
1626 for node in self.fake_nodepool.getNodes():
1627 if node['state'] == zuul.model.STATE_HOLD:
1628 held_node = node
1629 break
1630 self.assertIsNotNone(held_node)
1631
1632 # Validate node has recorded the failed job
1633 if change != "":
1634 ref = "refs/changes/%s/%s/.*" % (
1635 str(change_obj.number)[-1:], str(change_obj.number)
1636 )
1637
1638 self.assertEqual(
1639 held_node['hold_job'],
1640 " ".join(['tenant-one',
1641 'review.example.com/org/project',
1642 'project-test2', ref])
1643 )
1644 self.assertEqual(held_node['comment'], "reason text")
1645
1646 @simple_layout('layouts/autohold.yaml')
1647 def test_autohold_change(self):
1648 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1649
1650 self._test_autohold_scoped(A, change=A.number, ref="")
1651
1652 @simple_layout('layouts/autohold.yaml')
1653 def test_autohold_ref(self):
1654 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1655 ref = A.data['currentPatchSet']['ref']
1656 self._test_autohold_scoped(A, change="", ref=ref)
1657
1658 @simple_layout('layouts/autohold.yaml')
1659 def test_autohold_scoping(self):
1660 client = zuul.rpcclient.RPCClient('127.0.0.1',
1661 self.gearman_server.port)
1662 self.addCleanup(client.shutdown)
1663
1664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1665
1666 # create three autohold requests, scoped to job, change and
1667 # a specific ref
1668 change = str(A.number)
1669 ref = A.data['currentPatchSet']['ref']
1670 r1 = client.autohold('tenant-one', 'org/project', 'project-test2',
1671 "", "", "reason text", 1)
1672 self.assertTrue(r1)
1673 r2 = client.autohold('tenant-one', 'org/project', 'project-test2',
1674 change, "", "reason text", 1)
1675 self.assertTrue(r2)
1676 r3 = client.autohold('tenant-one', 'org/project', 'project-test2',
1677 "", ref, "reason text", 1)
1678 self.assertTrue(r3)
1679
1680 # Fail 3 jobs for the same change, and verify that the autohold
1681 # requests are fullfilled in the expected order: from the most
1682 # specific towards the most generic one.
1683
1684 def _fail_job_and_verify_autohold_request(change_obj, ref_filter):
1685 self.executor_server.failJob('project-test2', change_obj)
1686 self.fake_gerrit.addEvent(change_obj.getPatchsetCreatedEvent(1))
1687
1688 self.waitUntilSettled()
1689
1690 # Check nodepool for a held node
1691 held_node = None
1692 for node in self.fake_nodepool.getNodes():
1693 if node['state'] == zuul.model.STATE_HOLD:
1694 held_node = node
1695 break
1696 self.assertIsNotNone(held_node)
1697
1698 self.assertEqual(
1699 held_node['hold_job'],
1700 " ".join(['tenant-one',
1701 'review.example.com/org/project',
1702 'project-test2', ref_filter])
1703 )
1704 self.assertFalse(held_node['_lock'], "Node %s is locked" %
1705 (node['_oid'],))
1706 self.fake_nodepool.removeNode(held_node)
1707
1708 _fail_job_and_verify_autohold_request(A, ref)
1709
1710 ref = "refs/changes/%s/%s/.*" % (str(change)[-1:], str(change))
1711 _fail_job_and_verify_autohold_request(A, ref)
1712 _fail_job_and_verify_autohold_request(A, ".*")
1713
David Shrewsburyc4676132017-10-16 12:41:57 -04001714 @simple_layout('layouts/autohold.yaml')
Clark Boylanaeb5a122017-10-20 14:00:11 -07001715 def test_autohold_ignores_aborted_jobs(self):
1716 client = zuul.rpcclient.RPCClient('127.0.0.1',
1717 self.gearman_server.port)
1718 self.addCleanup(client.shutdown)
1719 r = client.autohold('tenant-one', 'org/project', 'project-test2',
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001720 "", "", "reason text", 1)
Clark Boylanaeb5a122017-10-20 14:00:11 -07001721 self.assertTrue(r)
1722
1723 self.executor_server.hold_jobs_in_build = True
1724
1725 # Create a change that will have its job aborted
1726 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1727 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1728 self.waitUntilSettled()
1729
1730 # Creating new patchset on change A will abort A,1's job because
1731 # a new patchset arrived replacing A,1 with A,2.
1732 A.addPatchset()
1733 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1734
1735 self.waitUntilSettled()
1736 self.executor_server.hold_jobs_in_build = False
1737 self.executor_server.release()
1738 self.waitUntilSettled()
1739
1740 self.assertEqual(A.data['status'], 'NEW')
1741 # Note only the successful job for A,2 will report as we don't
1742 # report aborted builds for old patchsets.
1743 self.assertEqual(A.reported, 1)
1744 # A,1 project-test2
1745 self.assertEqual(self.history[0].result, 'ABORTED')
1746 # A,2 project-test2
1747 self.assertEqual(self.history[1].result, 'SUCCESS')
1748
1749 # Check nodepool for a held node
1750 held_node = None
1751 for node in self.fake_nodepool.getNodes():
1752 if node['state'] == zuul.model.STATE_HOLD:
1753 held_node = node
1754 break
1755 self.assertIsNone(held_node)
1756
1757 @simple_layout('layouts/autohold.yaml')
David Shrewsburyc4676132017-10-16 12:41:57 -04001758 def test_autohold_list(self):
1759 client = zuul.rpcclient.RPCClient('127.0.0.1',
1760 self.gearman_server.port)
1761 self.addCleanup(client.shutdown)
1762
1763 r = client.autohold('tenant-one', 'org/project', 'project-test2',
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001764 "", "", "reason text", 1)
David Shrewsburyc4676132017-10-16 12:41:57 -04001765 self.assertTrue(r)
1766
1767 autohold_requests = client.autohold_list()
1768 self.assertNotEqual({}, autohold_requests)
1769 self.assertEqual(1, len(autohold_requests.keys()))
1770
1771 # The single dict key should be a CSV string value
1772 key = list(autohold_requests.keys())[0]
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001773 tenant, project, job, ref_filter = key.split(',')
David Shrewsburyc4676132017-10-16 12:41:57 -04001774
1775 self.assertEqual('tenant-one', tenant)
1776 self.assertIn('org/project', project)
1777 self.assertEqual('project-test2', job)
Krzysztof Klimonda37d54032017-10-25 12:16:47 +02001778 self.assertEqual(".*", ref_filter)
David Shrewsburyc4676132017-10-16 12:41:57 -04001779
1780 # Note: the value is converted from set to list by json.
1781 self.assertEqual([1, "reason text"], autohold_requests[key])
1782
James E. Blair7cb84542017-04-19 13:35:05 -07001783 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001784 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001785 # This particular test does a large amount of merges and needs a little
1786 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001787 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001788 "test that dependent changes behind dequeued changes work"
1789 # This complicated test is a reproduction of a real life bug
1790 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001791
Paul Belanger174a8272017-03-14 13:20:10 -04001792 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001793 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1794 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1795 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1796 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1797 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1798 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1799 D.setDependsOn(C, 1)
1800 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001801 A.addApproval('Code-Review', 2)
1802 B.addApproval('Code-Review', 2)
1803 C.addApproval('Code-Review', 2)
1804 D.addApproval('Code-Review', 2)
1805 E.addApproval('Code-Review', 2)
1806 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001807
1808 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001809
1810 # Change object re-use in the gerrit trigger is hidden if
1811 # changes are added in quick succession; waiting makes it more
1812 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001813 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001814 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001815 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001816 self.waitUntilSettled()
1817
Paul Belanger174a8272017-03-14 13:20:10 -04001818 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001819 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001820 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001821 self.waitUntilSettled()
1822
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001823 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001824 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001825 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001826 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001827 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001828 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001829 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001830 self.waitUntilSettled()
1831
Paul Belanger174a8272017-03-14 13:20:10 -04001832 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001833 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001834 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001835 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001836 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001837 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001838 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001839 self.waitUntilSettled()
1840
1841 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001842
1843 # Grab pointers to the jobs we want to release before
1844 # releasing any, because list indexes may change as
1845 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001846 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001847 a.release()
1848 b.release()
1849 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001850 self.waitUntilSettled()
1851
Paul Belanger174a8272017-03-14 13:20:10 -04001852 self.executor_server.hold_jobs_in_build = False
1853 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001854 self.waitUntilSettled()
1855
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001856 self.assertEqual(A.data['status'], 'NEW')
1857 self.assertEqual(B.data['status'], 'MERGED')
1858 self.assertEqual(C.data['status'], 'MERGED')
1859 self.assertEqual(D.data['status'], 'MERGED')
1860 self.assertEqual(E.data['status'], 'MERGED')
1861 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001862
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001863 self.assertEqual(A.reported, 2)
1864 self.assertEqual(B.reported, 2)
1865 self.assertEqual(C.reported, 2)
1866 self.assertEqual(D.reported, 2)
1867 self.assertEqual(E.reported, 2)
1868 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001869
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001870 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1871 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001872
1873 def test_merger_repack(self):
1874 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001875
James E. Blair05fed602012-09-07 12:45:24 -07001876 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001877 A.addApproval('Code-Review', 2)
1878 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001879 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001880 self.assertEqual(self.getJobFromHistory('project-merge').result,
1881 'SUCCESS')
1882 self.assertEqual(self.getJobFromHistory('project-test1').result,
1883 'SUCCESS')
1884 self.assertEqual(self.getJobFromHistory('project-test2').result,
1885 'SUCCESS')
1886 self.assertEqual(A.data['status'], 'MERGED')
1887 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001888 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001889 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001890
James E. Blair2a535672017-04-27 12:03:15 -07001891 path = os.path.join(self.merger_src_root, "review.example.com",
1892 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001893 if os.path.exists(path):
1894 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001895 path = os.path.join(self.executor_src_root, "review.example.com",
1896 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001897 if os.path.exists(path):
1898 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001899
1900 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001901 A.addApproval('Code-Review', 2)
1902 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001903 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001904 self.assertEqual(self.getJobFromHistory('project-merge').result,
1905 'SUCCESS')
1906 self.assertEqual(self.getJobFromHistory('project-test1').result,
1907 'SUCCESS')
1908 self.assertEqual(self.getJobFromHistory('project-test2').result,
1909 'SUCCESS')
1910 self.assertEqual(A.data['status'], 'MERGED')
1911 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001912
James E. Blair4886f282012-11-15 09:27:33 -08001913 def test_merger_repack_large_change(self):
1914 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001915 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001916 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001917 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001918 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001919 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001920 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001921 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001922 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001923 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001924 # TODOv3(jeblair): add hostname to upstream root
1925 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001926 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001927 path = os.path.join(self.merger_src_root, 'review.example.com',
1928 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001929 if os.path.exists(path):
1930 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001931 path = os.path.join(self.executor_src_root, 'review.example.com',
1932 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001933 if os.path.exists(path):
1934 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001935
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001936 A.addApproval('Code-Review', 2)
1937 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001938 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001939 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001940 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001941 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001942 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001943 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001944 'SUCCESS')
1945 self.assertEqual(A.data['status'], 'MERGED')
1946 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001947
James E. Blair2fa50962013-01-30 21:50:41 -08001948 def test_new_patchset_dequeues_old(self):
1949 "Test that a new patchset causes the old to be dequeued"
1950 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001951 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001952 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1953 M.setMerged()
1954
1955 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1956 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1957 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1958 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001959 A.addApproval('Code-Review', 2)
1960 B.addApproval('Code-Review', 2)
1961 C.addApproval('Code-Review', 2)
1962 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001963
1964 C.setDependsOn(B, 1)
1965 B.setDependsOn(A, 1)
1966 A.setDependsOn(M, 1)
1967
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001968 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1969 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1970 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1971 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001972 self.waitUntilSettled()
1973
1974 B.addPatchset()
1975 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1976 self.waitUntilSettled()
1977
Paul Belanger174a8272017-03-14 13:20:10 -04001978 self.executor_server.hold_jobs_in_build = False
1979 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001980 self.waitUntilSettled()
1981
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001982 self.assertEqual(A.data['status'], 'MERGED')
1983 self.assertEqual(A.reported, 2)
1984 self.assertEqual(B.data['status'], 'NEW')
1985 self.assertEqual(B.reported, 2)
1986 self.assertEqual(C.data['status'], 'NEW')
1987 self.assertEqual(C.reported, 2)
1988 self.assertEqual(D.data['status'], 'MERGED')
1989 self.assertEqual(D.reported, 2)
1990 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001991
James E. Blairba437362015-02-07 11:41:52 -08001992 def test_new_patchset_check(self):
1993 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001994
Paul Belanger174a8272017-03-14 13:20:10 -04001995 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001996
1997 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001998 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001999 tenant = self.sched.abide.tenants.get('tenant-one')
2000 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08002001
2002 # Add two git-dependent changes
2003 B.setDependsOn(A, 1)
2004 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2005 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01002006 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2007 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08002008
2009 # A live item, and a non-live/live pair
2010 items = check_pipeline.getAllItems()
2011 self.assertEqual(len(items), 3)
2012
2013 self.assertEqual(items[0].change.number, '1')
2014 self.assertEqual(items[0].change.patchset, '1')
2015 self.assertFalse(items[0].live)
2016
2017 self.assertEqual(items[1].change.number, '2')
2018 self.assertEqual(items[1].change.patchset, '1')
2019 self.assertTrue(items[1].live)
2020
2021 self.assertEqual(items[2].change.number, '1')
2022 self.assertEqual(items[2].change.patchset, '1')
2023 self.assertTrue(items[2].live)
2024
2025 # Add a new patchset to A
2026 A.addPatchset()
2027 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2028 self.waitUntilSettled()
2029
2030 # The live copy of A,1 should be gone, but the non-live and B
2031 # should continue, and we should have a new A,2
2032 items = check_pipeline.getAllItems()
2033 self.assertEqual(len(items), 3)
2034
2035 self.assertEqual(items[0].change.number, '1')
2036 self.assertEqual(items[0].change.patchset, '1')
2037 self.assertFalse(items[0].live)
2038
2039 self.assertEqual(items[1].change.number, '2')
2040 self.assertEqual(items[1].change.patchset, '1')
2041 self.assertTrue(items[1].live)
2042
2043 self.assertEqual(items[2].change.number, '1')
2044 self.assertEqual(items[2].change.patchset, '2')
2045 self.assertTrue(items[2].live)
2046
2047 # Add a new patchset to B
2048 B.addPatchset()
2049 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2050 self.waitUntilSettled()
2051
2052 # The live copy of B,1 should be gone, and it's non-live copy of A,1
2053 # but we should have a new B,2 (still based on A,1)
2054 items = check_pipeline.getAllItems()
2055 self.assertEqual(len(items), 3)
2056
2057 self.assertEqual(items[0].change.number, '1')
2058 self.assertEqual(items[0].change.patchset, '2')
2059 self.assertTrue(items[0].live)
2060
2061 self.assertEqual(items[1].change.number, '1')
2062 self.assertEqual(items[1].change.patchset, '1')
2063 self.assertFalse(items[1].live)
2064
2065 self.assertEqual(items[2].change.number, '2')
2066 self.assertEqual(items[2].change.patchset, '2')
2067 self.assertTrue(items[2].live)
2068
2069 self.builds[0].release()
2070 self.waitUntilSettled()
2071 self.builds[0].release()
2072 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002073 self.executor_server.hold_jobs_in_build = False
2074 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08002075 self.waitUntilSettled()
2076
2077 self.assertEqual(A.reported, 1)
2078 self.assertEqual(B.reported, 1)
2079 self.assertEqual(self.history[0].result, 'ABORTED')
2080 self.assertEqual(self.history[0].changes, '1,1')
2081 self.assertEqual(self.history[1].result, 'ABORTED')
2082 self.assertEqual(self.history[1].changes, '1,1 2,1')
2083 self.assertEqual(self.history[2].result, 'SUCCESS')
2084 self.assertEqual(self.history[2].changes, '1,2')
2085 self.assertEqual(self.history[3].result, 'SUCCESS')
2086 self.assertEqual(self.history[3].changes, '1,1 2,2')
2087
2088 def test_abandoned_gate(self):
2089 "Test that an abandoned change is dequeued from gate"
2090
Paul Belanger174a8272017-03-14 13:20:10 -04002091 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08002092
2093 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002094 A.addApproval('Code-Review', 2)
2095 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08002096 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01002097 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
2098 self.assertEqual(self.builds[0].name, 'project-merge')
2099
2100 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2101 self.waitUntilSettled()
2102
Paul Belanger174a8272017-03-14 13:20:10 -04002103 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01002104 self.waitUntilSettled()
2105
Arie5b4048c2016-10-05 16:12:06 +03002106 self.assertBuilds([])
2107 self.assertHistory([
2108 dict(name='project-merge', result='ABORTED', changes='1,1')],
2109 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08002110 self.assertEqual(A.reported, 1,
2111 "Abandoned gate change should report only start")
2112
2113 def test_abandoned_check(self):
2114 "Test that an abandoned change is dequeued from check"
2115
Paul Belanger174a8272017-03-14 13:20:10 -04002116 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08002117
2118 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2119 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04002120 tenant = self.sched.abide.tenants.get('tenant-one')
2121 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08002122
2123 # Add two git-dependent changes
2124 B.setDependsOn(A, 1)
2125 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2126 self.waitUntilSettled()
2127 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2128 self.waitUntilSettled()
2129 # A live item, and a non-live/live pair
2130 items = check_pipeline.getAllItems()
2131 self.assertEqual(len(items), 3)
2132
2133 self.assertEqual(items[0].change.number, '1')
2134 self.assertFalse(items[0].live)
2135
2136 self.assertEqual(items[1].change.number, '2')
2137 self.assertTrue(items[1].live)
2138
2139 self.assertEqual(items[2].change.number, '1')
2140 self.assertTrue(items[2].live)
2141
2142 # Abandon A
2143 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2144 self.waitUntilSettled()
2145
2146 # The live copy of A should be gone, but the non-live and B
2147 # should continue
2148 items = check_pipeline.getAllItems()
2149 self.assertEqual(len(items), 2)
2150
2151 self.assertEqual(items[0].change.number, '1')
2152 self.assertFalse(items[0].live)
2153
2154 self.assertEqual(items[1].change.number, '2')
2155 self.assertTrue(items[1].live)
2156
Paul Belanger174a8272017-03-14 13:20:10 -04002157 self.executor_server.hold_jobs_in_build = False
2158 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08002159 self.waitUntilSettled()
2160
2161 self.assertEqual(len(self.history), 4)
2162 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01002163 'Build should have been aborted')
2164 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08002165 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01002166
Steve Varnau7b78b312015-04-03 14:49:46 -07002167 def test_abandoned_not_timer(self):
2168 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002169 # This test can not use simple_layout because it must start
2170 # with a configuration which does not include a
2171 # timer-triggered job so that we have an opportunity to set
2172 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002173 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07002174 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002175 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07002176 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07002177 # The pipeline triggers every second, so we should have seen
2178 # several by now.
2179 time.sleep(5)
2180 self.waitUntilSettled()
2181 # Stop queuing timer triggered jobs so that the assertions
2182 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08002183 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002184 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07002185 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07002186 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002187 # If APScheduler is in mid-event when we remove the job, we
2188 # can end up with one more event firing, so give it an extra
2189 # second to settle.
2190 time.sleep(1)
2191 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002192
James E. Blair21037782017-07-19 11:56:55 -07002193 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07002194
2195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2196 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2197 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002198 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07002199
2200 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2201 self.waitUntilSettled()
2202
James E. Blair21037782017-07-19 11:56:55 -07002203 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07002204
Paul Belanger174a8272017-03-14 13:20:10 -04002205 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07002206 self.waitUntilSettled()
2207
James E. Blair2fa50962013-01-30 21:50:41 -08002208 def test_new_patchset_dequeues_old_on_head(self):
2209 "Test that a new patchset causes the old to be dequeued (at head)"
2210 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04002211 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002212 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2213 M.setMerged()
2214 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2215 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2216 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2217 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002218 A.addApproval('Code-Review', 2)
2219 B.addApproval('Code-Review', 2)
2220 C.addApproval('Code-Review', 2)
2221 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002222
2223 C.setDependsOn(B, 1)
2224 B.setDependsOn(A, 1)
2225 A.setDependsOn(M, 1)
2226
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002227 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2228 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2229 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2230 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002231 self.waitUntilSettled()
2232
2233 A.addPatchset()
2234 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2235 self.waitUntilSettled()
2236
Paul Belanger174a8272017-03-14 13:20:10 -04002237 self.executor_server.hold_jobs_in_build = False
2238 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002239 self.waitUntilSettled()
2240
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002241 self.assertEqual(A.data['status'], 'NEW')
2242 self.assertEqual(A.reported, 2)
2243 self.assertEqual(B.data['status'], 'NEW')
2244 self.assertEqual(B.reported, 2)
2245 self.assertEqual(C.data['status'], 'NEW')
2246 self.assertEqual(C.reported, 2)
2247 self.assertEqual(D.data['status'], 'MERGED')
2248 self.assertEqual(D.reported, 2)
2249 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002250
2251 def test_new_patchset_dequeues_old_without_dependents(self):
2252 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04002253 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002257 A.addApproval('Code-Review', 2)
2258 B.addApproval('Code-Review', 2)
2259 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002260
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002261 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2262 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2263 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002264 self.waitUntilSettled()
2265
2266 B.addPatchset()
2267 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2268 self.waitUntilSettled()
2269
Paul Belanger174a8272017-03-14 13:20:10 -04002270 self.executor_server.hold_jobs_in_build = False
2271 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002272 self.waitUntilSettled()
2273
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002274 self.assertEqual(A.data['status'], 'MERGED')
2275 self.assertEqual(A.reported, 2)
2276 self.assertEqual(B.data['status'], 'NEW')
2277 self.assertEqual(B.reported, 2)
2278 self.assertEqual(C.data['status'], 'MERGED')
2279 self.assertEqual(C.reported, 2)
2280 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002281
2282 def test_new_patchset_dequeues_old_independent_queue(self):
2283 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002284 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2286 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2287 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2289 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2290 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2291 self.waitUntilSettled()
2292
2293 B.addPatchset()
2294 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2295 self.waitUntilSettled()
2296
Paul Belanger174a8272017-03-14 13:20:10 -04002297 self.executor_server.hold_jobs_in_build = False
2298 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002299 self.waitUntilSettled()
2300
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002301 self.assertEqual(A.data['status'], 'NEW')
2302 self.assertEqual(A.reported, 1)
2303 self.assertEqual(B.data['status'], 'NEW')
2304 self.assertEqual(B.reported, 1)
2305 self.assertEqual(C.data['status'], 'NEW')
2306 self.assertEqual(C.reported, 1)
2307 self.assertEqual(len(self.history), 10)
2308 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002309
James E. Blaircc020532017-04-19 13:04:44 -07002310 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002311 def test_noop_job(self):
2312 "Test that the internal noop job works"
2313 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002314 A.addApproval('Code-Review', 2)
2315 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002316 self.waitUntilSettled()
2317
2318 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2319 self.assertTrue(self.sched._areAllBuildsComplete())
2320 self.assertEqual(len(self.history), 0)
2321 self.assertEqual(A.data['status'], 'MERGED')
2322 self.assertEqual(A.reported, 2)
2323
James E. Blair06cc3922017-04-19 10:08:10 -07002324 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002325 def test_no_job_project(self):
2326 "Test that reports with no jobs don't get sent"
2327 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2328 'master', 'A')
2329 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2330 self.waitUntilSettled()
2331
2332 # Change wasn't reported to
2333 self.assertEqual(A.reported, False)
2334
2335 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002336 tenant = self.sched.abide.tenants.get('tenant-one')
2337 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002338 items = check_pipeline.getAllItems()
2339 self.assertEqual(len(items), 0)
2340
2341 self.assertEqual(len(self.history), 0)
2342
James E. Blair7d0dedc2013-02-21 17:26:09 -08002343 def test_zuul_refs(self):
2344 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002345 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002346 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2347 M1.setMerged()
2348 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2349 M2.setMerged()
2350
2351 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2352 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2353 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2354 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002355 A.addApproval('Code-Review', 2)
2356 B.addApproval('Code-Review', 2)
2357 C.addApproval('Code-Review', 2)
2358 D.addApproval('Code-Review', 2)
2359 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2360 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2361 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2362 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002363
2364 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002365 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002366 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002367 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002368 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002369 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002370 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002371 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002372 self.waitUntilSettled()
2373
Clint Byrum3343e3e2016-11-15 16:05:03 -08002374 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002375 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002376 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002377 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002378 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002379 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002380 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002381 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002382 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002383 d_build = x
2384 if a_build and b_build and c_build and d_build:
2385 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002386
Clint Byrum3343e3e2016-11-15 16:05:03 -08002387 # should have a, not b, and should not be in project2
2388 self.assertTrue(a_build.hasChanges(A))
2389 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002390
Clint Byrum3343e3e2016-11-15 16:05:03 -08002391 # should have a and b, and should not be in project2
2392 self.assertTrue(b_build.hasChanges(A, B))
2393 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002394
Clint Byrum3343e3e2016-11-15 16:05:03 -08002395 # should have a and b in 1, c in 2
2396 self.assertTrue(c_build.hasChanges(A, B, C))
2397 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002398
Clint Byrum3343e3e2016-11-15 16:05:03 -08002399 # should have a and b in 1, c and d in 2
2400 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002401
Paul Belanger174a8272017-03-14 13:20:10 -04002402 self.executor_server.hold_jobs_in_build = False
2403 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002404 self.waitUntilSettled()
2405
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002406 self.assertEqual(A.data['status'], 'MERGED')
2407 self.assertEqual(A.reported, 2)
2408 self.assertEqual(B.data['status'], 'MERGED')
2409 self.assertEqual(B.reported, 2)
2410 self.assertEqual(C.data['status'], 'MERGED')
2411 self.assertEqual(C.reported, 2)
2412 self.assertEqual(D.data['status'], 'MERGED')
2413 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002414
James E. Blair4a28a882013-08-23 15:17:33 -07002415 def test_rerun_on_error(self):
2416 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002417 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002419 A.addApproval('Code-Review', 2)
2420 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002421 self.waitUntilSettled()
2422
James E. Blair412fba82017-01-26 15:00:50 -08002423 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002424 self.executor_server.hold_jobs_in_build = False
2425 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002426 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002427 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002428 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2429
James E. Blair412e5582013-04-22 15:50:12 -07002430 def test_statsd(self):
2431 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002432 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002433 statsd.incr('test-incr')
2434 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002435 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002436 self.assertReportedStat('test-incr', '1|c')
2437 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002438 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002439
James E. Blairdad52252014-02-07 16:59:17 -08002440 def test_stuck_job_cleanup(self):
2441 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002442
2443 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002444 self.gearman_server.hold_jobs_in_queue = True
2445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002446 A.addApproval('Code-Review', 2)
2447 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002448 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002449 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002450 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2451
James E. Blair31cb3e82017-04-20 13:08:56 -07002452 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002453 self.sched.reconfigure(self.config)
2454 self.waitUntilSettled()
2455
James E. Blair18c64442014-03-18 10:14:45 -07002456 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002457 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002458 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002459 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2460 self.assertTrue(self.sched._areAllBuildsComplete())
2461
2462 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002463 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002464 self.assertEqual(self.history[0].result, 'SUCCESS')
2465
James E. Blair879dafb2015-07-17 14:04:49 -07002466 def test_file_head(self):
2467 # This is a regression test for an observed bug. A change
2468 # with a file named "HEAD" in the root directory of the repo
2469 # was processed by a merger. It then was unable to reset the
2470 # repo because of:
2471 # GitCommandError: 'git reset --hard HEAD' returned
2472 # with exit code 128
2473 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2474 # and filename
2475 # Use '--' to separate filenames from revisions'
2476
2477 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002478 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002479 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2480
2481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2482 self.waitUntilSettled()
2483
2484 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2485 self.waitUntilSettled()
2486
2487 self.assertIn('Build succeeded', A.messages[0])
2488 self.assertIn('Build succeeded', B.messages[0])
2489
James E. Blair70c71582013-03-06 08:50:50 -08002490 def test_file_jobs(self):
2491 "Test that file jobs run only when appropriate"
2492 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002493 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002494 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002495 A.addApproval('Code-Review', 2)
2496 B.addApproval('Code-Review', 2)
2497 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2498 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002499 self.waitUntilSettled()
2500
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002501 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002502 if x.name == 'project-testfile']
2503
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002504 self.assertEqual(len(testfile_jobs), 1)
2505 self.assertEqual(testfile_jobs[0].changes, '1,2')
2506 self.assertEqual(A.data['status'], 'MERGED')
2507 self.assertEqual(A.reported, 2)
2508 self.assertEqual(B.data['status'], 'MERGED')
2509 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002510
Clint Byrum3ee39f32016-11-17 23:45:07 -08002511 def _test_irrelevant_files_jobs(self, should_skip):
2512 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002513 if should_skip:
2514 files = {'ignoreme': 'ignored\n'}
2515 else:
2516 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002517
2518 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002519 'master',
2520 'test irrelevant-files',
2521 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002522 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2523 self.waitUntilSettled()
2524
2525 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002526 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002527
2528 if should_skip:
2529 self.assertEqual([], tested_change_ids)
2530 else:
2531 self.assertIn(change.data['number'], tested_change_ids)
2532
James E. Blaircc324192017-04-19 14:36:08 -07002533 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002534 def test_irrelevant_files_match_skips_job(self):
2535 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002536
James E. Blaircc324192017-04-19 14:36:08 -07002537 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002538 def test_irrelevant_files_no_match_runs_job(self):
2539 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002540
James E. Blaircc324192017-04-19 14:36:08 -07002541 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002542 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002543 files = {'ignoreme': 'ignored\n'}
2544
2545 change = self.fake_gerrit.addFakeChange('org/project',
2546 'master',
2547 'test irrelevant-files',
2548 files=files)
2549 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2550 self.waitUntilSettled()
2551
2552 run_jobs = set([build.name for build in self.history])
2553
2554 self.assertEqual(set(['project-test-nomatch-starts-empty',
2555 'project-test-nomatch-starts-full']), run_jobs)
2556
James E. Blairaf8b2082017-10-03 15:38:27 -07002557 @simple_layout('layouts/job-vars.yaml')
2558 def test_inherited_job_variables(self):
2559 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2560 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2561 self.waitUntilSettled()
2562 self.assertHistory([
2563 dict(name='parentjob', result='SUCCESS'),
2564 dict(name='child1', result='SUCCESS'),
2565 dict(name='child2', result='SUCCESS'),
2566 dict(name='child3', result='SUCCESS'),
2567 ], ordered=False)
2568 j = self.getJobFromHistory('parentjob')
2569 rp = set([p['name'] for p in j.parameters['projects']])
2570 self.assertEqual(j.parameters['vars']['override'], 0)
2571 self.assertEqual(j.parameters['vars']['child1override'], 0)
2572 self.assertEqual(j.parameters['vars']['parent'], 0)
2573 self.assertFalse('child1' in j.parameters['vars'])
2574 self.assertFalse('child2' in j.parameters['vars'])
2575 self.assertFalse('child3' in j.parameters['vars'])
2576 self.assertEqual(rp, set(['org/project', 'org/project0',
2577 'org/project0']))
2578 j = self.getJobFromHistory('child1')
2579 rp = set([p['name'] for p in j.parameters['projects']])
2580 self.assertEqual(j.parameters['vars']['override'], 1)
2581 self.assertEqual(j.parameters['vars']['child1override'], 1)
2582 self.assertEqual(j.parameters['vars']['parent'], 0)
2583 self.assertEqual(j.parameters['vars']['child1'], 1)
2584 self.assertFalse('child2' in j.parameters['vars'])
2585 self.assertFalse('child3' in j.parameters['vars'])
2586 self.assertEqual(rp, set(['org/project', 'org/project0',
2587 'org/project1']))
2588 j = self.getJobFromHistory('child2')
2589 rp = set([p['name'] for p in j.parameters['projects']])
2590 self.assertEqual(j.parameters['vars']['override'], 2)
2591 self.assertEqual(j.parameters['vars']['child1override'], 0)
2592 self.assertEqual(j.parameters['vars']['parent'], 0)
2593 self.assertFalse('child1' in j.parameters['vars'])
2594 self.assertEqual(j.parameters['vars']['child2'], 2)
2595 self.assertFalse('child3' in j.parameters['vars'])
2596 self.assertEqual(rp, set(['org/project', 'org/project0',
2597 'org/project2']))
2598 j = self.getJobFromHistory('child3')
2599 rp = set([p['name'] for p in j.parameters['projects']])
2600 self.assertEqual(j.parameters['vars']['override'], 3)
2601 self.assertEqual(j.parameters['vars']['child1override'], 0)
2602 self.assertEqual(j.parameters['vars']['parent'], 0)
2603 self.assertFalse('child1' in j.parameters['vars'])
2604 self.assertFalse('child2' in j.parameters['vars'])
2605 self.assertEqual(j.parameters['vars']['child3'], 3)
2606 self.assertEqual(rp, set(['org/project', 'org/project0',
2607 'org/project3']))
2608
James E. Blairb09a0c52017-10-04 07:35:14 -07002609 @simple_layout('layouts/job-variants.yaml')
2610 def test_job_branch_variants(self):
2611 self.create_branch('org/project', 'stable/diablo')
2612 self.create_branch('org/project', 'stable/essex')
2613 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2614 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2615 self.waitUntilSettled()
2616
2617 B = self.fake_gerrit.addFakeChange('org/project', 'stable/diablo', 'B')
2618 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2619 self.waitUntilSettled()
2620
2621 C = self.fake_gerrit.addFakeChange('org/project', 'stable/essex', 'C')
2622 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2623 self.waitUntilSettled()
2624 self.assertHistory([
2625 dict(name='python27', result='SUCCESS'),
2626 dict(name='python27', result='SUCCESS'),
2627 dict(name='python27', result='SUCCESS'),
2628 ])
2629
2630 p = self.history[0].parameters
2631 self.assertEqual(p['timeout'], 40)
2632 self.assertEqual(len(p['nodes']), 1)
2633 self.assertEqual(p['nodes'][0]['label'], 'new')
2634 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2635 ['base-pre', 'py27-pre'])
2636 self.assertEqual([x['path'] for x in p['post_playbooks']],
2637 ['py27-post-a', 'py27-post-b', 'base-post'])
2638 self.assertEqual([x['path'] for x in p['playbooks']],
James E. Blair2f589fe2017-10-26 12:57:41 -07002639 ['playbooks/python27.yaml'])
James E. Blairb09a0c52017-10-04 07:35:14 -07002640
2641 p = self.history[1].parameters
2642 self.assertEqual(p['timeout'], 50)
2643 self.assertEqual(len(p['nodes']), 1)
2644 self.assertEqual(p['nodes'][0]['label'], 'old')
2645 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2646 ['base-pre', 'py27-pre', 'py27-diablo-pre'])
2647 self.assertEqual([x['path'] for x in p['post_playbooks']],
2648 ['py27-diablo-post', 'py27-post-a', 'py27-post-b',
2649 'base-post'])
2650 self.assertEqual([x['path'] for x in p['playbooks']],
2651 ['py27-diablo'])
2652
2653 p = self.history[2].parameters
2654 self.assertEqual(p['timeout'], 40)
2655 self.assertEqual(len(p['nodes']), 1)
2656 self.assertEqual(p['nodes'][0]['label'], 'new')
2657 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2658 ['base-pre', 'py27-pre', 'py27-essex-pre'])
2659 self.assertEqual([x['path'] for x in p['post_playbooks']],
2660 ['py27-essex-post', 'py27-post-a', 'py27-post-b',
2661 'base-post'])
2662 self.assertEqual([x['path'] for x in p['playbooks']],
James E. Blair2f589fe2017-10-26 12:57:41 -07002663 ['playbooks/python27.yaml'])
James E. Blairb09a0c52017-10-04 07:35:14 -07002664
James E. Blair00292672017-10-26 15:29:39 -07002665 @simple_layout("layouts/no-run.yaml")
2666 def test_job_without_run(self):
2667 "Test that a job without a run playbook errors"
2668 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2669 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2670 self.waitUntilSettled()
2671 self.assertIn('Job base does not specify a run playbook',
2672 A.messages[-1])
2673
James E. Blairc8a1e052014-02-25 09:29:26 -08002674 def test_queue_names(self):
2675 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002676 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002677 (trusted, project1) = tenant.getProject('org/project1')
2678 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002679 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2680 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2681 self.assertEqual(q1.name, 'integrated')
2682 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002683
James E. Blair64ed6f22013-07-10 14:07:23 -07002684 def test_queue_precedence(self):
2685 "Test that queue precedence works"
2686
2687 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002688 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002689 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2690 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002691 A.addApproval('Code-Review', 2)
2692 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002693
2694 self.waitUntilSettled()
2695 self.gearman_server.hold_jobs_in_queue = False
2696 self.gearman_server.release()
2697 self.waitUntilSettled()
2698
James E. Blair8de58bd2013-07-18 16:23:33 -07002699 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002700 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002701 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002702 self.waitUntilSettled()
2703
James E. Blair64ed6f22013-07-10 14:07:23 -07002704 self.log.debug(self.history)
2705 self.assertEqual(self.history[0].pipeline, 'gate')
2706 self.assertEqual(self.history[1].pipeline, 'check')
2707 self.assertEqual(self.history[2].pipeline, 'gate')
2708 self.assertEqual(self.history[3].pipeline, 'gate')
2709 self.assertEqual(self.history[4].pipeline, 'check')
2710 self.assertEqual(self.history[5].pipeline, 'check')
2711
James E. Blair419a8672017-10-18 14:48:25 -07002712 def test_reconfigure_merge(self):
2713 """Test that two reconfigure events are merged"""
2714
2715 tenant = self.sched.abide.tenants['tenant-one']
2716 (trusted, project) = tenant.getProject('org/project')
2717
2718 self.sched.run_handler_lock.acquire()
2719 self.assertEqual(self.sched.management_event_queue.qsize(), 0)
2720
2721 self.sched.reconfigureTenant(tenant, project)
2722 self.assertEqual(self.sched.management_event_queue.qsize(), 1)
2723
2724 self.sched.reconfigureTenant(tenant, project)
2725 # The second event should have been combined with the first
2726 # so we should still only have one entry.
2727 self.assertEqual(self.sched.management_event_queue.qsize(), 1)
2728
2729 self.sched.run_handler_lock.release()
2730 self.waitUntilSettled()
2731
2732 self.assertEqual(self.sched.management_event_queue.qsize(), 0)
2733
James E. Blaircdccd972013-07-01 12:10:22 -07002734 def test_live_reconfiguration(self):
2735 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002736 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002737 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002738 A.addApproval('Code-Review', 2)
2739 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002740 self.waitUntilSettled()
2741
2742 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002743 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002744
Paul Belanger174a8272017-03-14 13:20:10 -04002745 self.executor_server.hold_jobs_in_build = False
2746 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002747 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002748 self.assertEqual(self.getJobFromHistory('project-merge').result,
2749 'SUCCESS')
2750 self.assertEqual(self.getJobFromHistory('project-test1').result,
2751 'SUCCESS')
2752 self.assertEqual(self.getJobFromHistory('project-test2').result,
2753 'SUCCESS')
2754 self.assertEqual(A.data['status'], 'MERGED')
2755 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002756
James E. Blair027ba992017-09-20 13:48:32 -07002757 def test_live_reconfiguration_abort(self):
2758 # Raise an exception during reconfiguration and verify we
2759 # still function.
2760 self.executor_server.hold_jobs_in_build = True
2761 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2762 A.addApproval('Code-Review', 2)
2763 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2764 self.waitUntilSettled()
2765
2766 tenant = self.sched.abide.tenants.get('tenant-one')
2767 pipeline = tenant.layout.pipelines['gate']
2768 change = pipeline.getAllItems()[0].change
2769 # Set this to an invalid value to cause an exception during
2770 # reconfiguration.
2771 change.branch = None
2772
2773 self.sched.reconfigure(self.config)
2774 self.waitUntilSettled()
2775
2776 self.executor_server.hold_jobs_in_build = False
2777 self.executor_server.release()
2778
2779 self.waitUntilSettled()
2780 self.assertEqual(self.getJobFromHistory('project-merge').result,
2781 'ABORTED')
2782 self.assertEqual(A.data['status'], 'NEW')
2783 # The final report fails because of the invalid value set above.
2784 self.assertEqual(A.reported, 1)
2785
James E. Blair6bc782d2015-07-17 16:20:21 -07002786 def test_live_reconfiguration_merge_conflict(self):
2787 # A real-world bug: a change in a gate queue has a merge
2788 # conflict and a job is added to its project while it's
2789 # sitting in the queue. The job gets added to the change and
2790 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002791 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002792
2793 # This change is fine. It's here to stop the queue long
2794 # enough for the next change to be subject to the
2795 # reconfiguration, as well as to provide a conflict for the
2796 # next change. This change will succeed and merge.
2797 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002798 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002799 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002800
2801 # This change will be in merge conflict. During the
2802 # reconfiguration, we will add a job. We want to make sure
2803 # that doesn't cause it to get stuck.
2804 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002805 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002806 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002807
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002808 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2809 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002810
2811 self.waitUntilSettled()
2812
2813 # No jobs have run yet
2814 self.assertEqual(A.data['status'], 'NEW')
2815 self.assertEqual(A.reported, 1)
2816 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002817 self.assertEqual(len(self.history), 0)
2818
2819 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002820 self.commitConfigUpdate('common-config',
2821 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002822 self.sched.reconfigure(self.config)
2823 self.waitUntilSettled()
2824
Paul Belanger174a8272017-03-14 13:20:10 -04002825 self.executor_server.hold_jobs_in_build = False
2826 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002827 self.waitUntilSettled()
2828
2829 self.assertEqual(A.data['status'], 'MERGED')
2830 self.assertEqual(A.reported, 2)
2831 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002832 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002833 self.assertEqual(self.getJobFromHistory('project-merge').result,
2834 'SUCCESS')
2835 self.assertEqual(self.getJobFromHistory('project-test1').result,
2836 'SUCCESS')
2837 self.assertEqual(self.getJobFromHistory('project-test2').result,
2838 'SUCCESS')
2839 self.assertEqual(self.getJobFromHistory('project-test3').result,
2840 'SUCCESS')
2841 self.assertEqual(len(self.history), 4)
2842
James E. Blair400e8fd2015-07-30 17:44:45 -07002843 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002844 # An extrapolation of test_live_reconfiguration_merge_conflict
2845 # that tests a job added to a job tree with a failed root does
2846 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002847 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002848
2849 # This change is fine. It's here to stop the queue long
2850 # enough for the next change to be subject to the
2851 # reconfiguration. This change will succeed and merge.
2852 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002853 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002854 A.addApproval('Code-Review', 2)
2855 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002856 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002857 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002858 self.waitUntilSettled()
2859
2860 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002861 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002862 B.addApproval('Code-Review', 2)
2863 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002864 self.waitUntilSettled()
2865
Paul Belanger174a8272017-03-14 13:20:10 -04002866 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002867 self.waitUntilSettled()
2868
2869 # Both -merge jobs have run, but no others.
2870 self.assertEqual(A.data['status'], 'NEW')
2871 self.assertEqual(A.reported, 1)
2872 self.assertEqual(B.data['status'], 'NEW')
2873 self.assertEqual(B.reported, 1)
2874 self.assertEqual(self.history[0].result, 'SUCCESS')
2875 self.assertEqual(self.history[0].name, 'project-merge')
2876 self.assertEqual(self.history[1].result, 'FAILURE')
2877 self.assertEqual(self.history[1].name, 'project-merge')
2878 self.assertEqual(len(self.history), 2)
2879
2880 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002881 self.commitConfigUpdate('common-config',
2882 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002883 self.sched.reconfigure(self.config)
2884 self.waitUntilSettled()
2885
Paul Belanger174a8272017-03-14 13:20:10 -04002886 self.executor_server.hold_jobs_in_build = False
2887 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002888 self.waitUntilSettled()
2889
2890 self.assertEqual(A.data['status'], 'MERGED')
2891 self.assertEqual(A.reported, 2)
2892 self.assertEqual(B.data['status'], 'NEW')
2893 self.assertEqual(B.reported, 2)
2894 self.assertEqual(self.history[0].result, 'SUCCESS')
2895 self.assertEqual(self.history[0].name, 'project-merge')
2896 self.assertEqual(self.history[1].result, 'FAILURE')
2897 self.assertEqual(self.history[1].name, 'project-merge')
2898 self.assertEqual(self.history[2].result, 'SUCCESS')
2899 self.assertEqual(self.history[3].result, 'SUCCESS')
2900 self.assertEqual(self.history[4].result, 'SUCCESS')
2901 self.assertEqual(len(self.history), 5)
2902
James E. Blair400e8fd2015-07-30 17:44:45 -07002903 def test_live_reconfiguration_failed_job(self):
2904 # Test that a change with a removed failing job does not
2905 # disrupt reconfiguration. If a change has a failed job and
2906 # that job is removed during a reconfiguration, we observed a
2907 # bug where the code to re-set build statuses would run on
2908 # that build and raise an exception because the job no longer
2909 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002910 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002911
2912 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2913
2914 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002915 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002916
2917 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2918 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002919 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002920 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002921 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002922 self.waitUntilSettled()
2923
2924 self.assertEqual(A.data['status'], 'NEW')
2925 self.assertEqual(A.reported, 0)
2926
2927 self.assertEqual(self.getJobFromHistory('project-merge').result,
2928 'SUCCESS')
2929 self.assertEqual(self.getJobFromHistory('project-test1').result,
2930 'FAILURE')
2931 self.assertEqual(len(self.history), 2)
2932
2933 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002934 self.commitConfigUpdate('common-config',
2935 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002936 self.sched.reconfigure(self.config)
2937 self.waitUntilSettled()
2938
Paul Belanger174a8272017-03-14 13:20:10 -04002939 self.executor_server.hold_jobs_in_build = False
2940 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002941 self.waitUntilSettled()
2942
2943 self.assertEqual(self.getJobFromHistory('project-test2').result,
2944 'SUCCESS')
2945 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2946 'SUCCESS')
2947 self.assertEqual(len(self.history), 4)
2948
2949 self.assertEqual(A.data['status'], 'NEW')
2950 self.assertEqual(A.reported, 1)
2951 self.assertIn('Build succeeded', A.messages[0])
2952 # Ensure the removed job was not included in the report.
2953 self.assertNotIn('project-test1', A.messages[0])
2954
James E. Blairfe707d12015-08-05 15:18:15 -07002955 def test_live_reconfiguration_shared_queue(self):
2956 # Test that a change with a failing job which was removed from
2957 # this project but otherwise still exists in the system does
2958 # not disrupt reconfiguration.
2959
Paul Belanger174a8272017-03-14 13:20:10 -04002960 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002961
2962 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2963
Paul Belanger174a8272017-03-14 13:20:10 -04002964 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002965
2966 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2967 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002968 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002969 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002970 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002971 self.waitUntilSettled()
2972
2973 self.assertEqual(A.data['status'], 'NEW')
2974 self.assertEqual(A.reported, 0)
2975
Clint Byrum69663d42017-05-01 14:51:04 -07002976 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002977 'SUCCESS')
2978 self.assertEqual(self.getJobFromHistory(
2979 'project1-project2-integration').result, 'FAILURE')
2980 self.assertEqual(len(self.history), 2)
2981
2982 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002983 self.commitConfigUpdate(
2984 'common-config',
2985 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002986 self.sched.reconfigure(self.config)
2987 self.waitUntilSettled()
2988
Paul Belanger174a8272017-03-14 13:20:10 -04002989 self.executor_server.hold_jobs_in_build = False
2990 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002991 self.waitUntilSettled()
2992
Clint Byrum69663d42017-05-01 14:51:04 -07002993 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002994 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002995 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002996 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002997 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002998 'SUCCESS')
2999 self.assertEqual(self.getJobFromHistory(
3000 'project1-project2-integration').result, 'FAILURE')
3001 self.assertEqual(len(self.history), 4)
3002
3003 self.assertEqual(A.data['status'], 'NEW')
3004 self.assertEqual(A.reported, 1)
3005 self.assertIn('Build succeeded', A.messages[0])
3006 # Ensure the removed job was not included in the report.
3007 self.assertNotIn('project1-project2-integration', A.messages[0])
3008
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003009 def test_double_live_reconfiguration_shared_queue(self):
3010 # This was a real-world regression. A change is added to
3011 # gate; a reconfigure happens, a second change which depends
3012 # on the first is added, and a second reconfiguration happens.
3013 # Ensure that both changes merge.
3014
3015 # A failure may indicate incorrect caching or cleaning up of
3016 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04003017 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003018
3019 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3020 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3021 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003022 A.addApproval('Code-Review', 2)
3023 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003024
3025 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003026 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003027 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003028 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003029 self.waitUntilSettled()
3030
3031 # Reconfigure (with only one change in the pipeline).
3032 self.sched.reconfigure(self.config)
3033 self.waitUntilSettled()
3034
3035 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003036 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003037 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003038 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003039 self.waitUntilSettled()
3040
3041 # Reconfigure (with both in the pipeline).
3042 self.sched.reconfigure(self.config)
3043 self.waitUntilSettled()
3044
Paul Belanger174a8272017-03-14 13:20:10 -04003045 self.executor_server.hold_jobs_in_build = False
3046 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003047 self.waitUntilSettled()
3048
3049 self.assertEqual(len(self.history), 8)
3050
3051 self.assertEqual(A.data['status'], 'MERGED')
3052 self.assertEqual(A.reported, 2)
3053 self.assertEqual(B.data['status'], 'MERGED')
3054 self.assertEqual(B.reported, 2)
3055
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003056 def test_live_reconfiguration_del_project(self):
3057 # Test project deletion from layout
3058 # while changes are enqueued
3059
Paul Belanger174a8272017-03-14 13:20:10 -04003060 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003061 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3062 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3063 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
3064
3065 # A Depends-On: B
3066 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3067 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003068 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003069
3070 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3071 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
3072 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003073 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003074 self.waitUntilSettled()
3075 self.assertEqual(len(self.builds), 5)
3076
3077 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07003078 self.commitConfigUpdate(
3079 'common-config',
3080 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003081 self.sched.reconfigure(self.config)
3082 self.waitUntilSettled()
3083
3084 # Builds for C aborted, builds for A succeed,
3085 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07003086 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003087 self.assertEqual(job_c.changes, '3,1')
3088 self.assertEqual(job_c.result, 'ABORTED')
3089
Paul Belanger174a8272017-03-14 13:20:10 -04003090 self.executor_server.hold_jobs_in_build = False
3091 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003092 self.waitUntilSettled()
3093
Clint Byrum0149ada2017-04-04 12:22:01 -07003094 self.assertEqual(
3095 self.getJobFromHistory('project-test1', 'org/project').changes,
3096 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003097
3098 self.assertEqual(A.data['status'], 'NEW')
3099 self.assertEqual(B.data['status'], 'NEW')
3100 self.assertEqual(C.data['status'], 'NEW')
3101 self.assertEqual(A.reported, 1)
3102 self.assertEqual(B.reported, 0)
3103 self.assertEqual(C.reported, 0)
3104
Clint Byrum0149ada2017-04-04 12:22:01 -07003105 tenant = self.sched.abide.tenants.get('tenant-one')
3106 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003107 self.assertIn('Build succeeded', A.messages[0])
3108
James E. Blair872738f2017-10-31 16:40:36 -07003109 @simple_layout("layouts/reconfigure-failed-head.yaml")
3110 def test_live_reconfiguration_failed_change_at_head(self):
3111 # Test that if we reconfigure with a failed change at head,
3112 # that the change behind it isn't reparented onto it.
3113
3114 self.executor_server.hold_jobs_in_build = True
3115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3116 A.addApproval('Code-Review', 2)
3117 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3118 B.addApproval('Code-Review', 2)
3119
3120 self.executor_server.failJob('job1', A)
3121
3122 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3123 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3124
3125 self.waitUntilSettled()
3126
3127 self.assertBuilds([
3128 dict(name='job1', changes='1,1'),
3129 dict(name='job2', changes='1,1'),
3130 dict(name='job1', changes='1,1 2,1'),
3131 dict(name='job2', changes='1,1 2,1'),
3132 ])
3133
3134 self.release(self.builds[0])
3135 self.waitUntilSettled()
3136
3137 self.assertBuilds([
3138 dict(name='job2', changes='1,1'),
3139 dict(name='job1', changes='2,1'),
3140 dict(name='job2', changes='2,1'),
3141 ])
3142
3143 # Unordered history comparison because the aborts can finish
3144 # in any order.
3145 self.assertHistory([
3146 dict(name='job1', result='FAILURE', changes='1,1'),
3147 dict(name='job1', result='ABORTED', changes='1,1 2,1'),
3148 dict(name='job2', result='ABORTED', changes='1,1 2,1'),
3149 ], ordered=False)
3150
3151 self.sched.reconfigure(self.config)
3152 self.waitUntilSettled()
3153
3154 self.executor_server.hold_jobs_in_build = False
3155 self.executor_server.release()
3156 self.waitUntilSettled()
3157
3158 self.assertBuilds([])
3159
3160 self.assertHistory([
3161 dict(name='job1', result='FAILURE', changes='1,1'),
3162 dict(name='job1', result='ABORTED', changes='1,1 2,1'),
3163 dict(name='job2', result='ABORTED', changes='1,1 2,1'),
3164 dict(name='job2', result='SUCCESS', changes='1,1'),
3165 dict(name='job1', result='SUCCESS', changes='2,1'),
3166 dict(name='job2', result='SUCCESS', changes='2,1'),
3167 ], ordered=False)
3168 self.assertEqual(A.data['status'], 'NEW')
3169 self.assertEqual(B.data['status'], 'MERGED')
3170 self.assertEqual(A.reported, 2)
3171 self.assertEqual(B.reported, 2)
3172
James E. Blair287c06d2013-07-24 10:39:30 -07003173 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07003174 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07003175 files = {'README': ''}
3176 self.addCommitToRepo("org/new-project", 'Initial commit',
3177 files=files, tag='init')
3178 self.newTenantConfig('tenants/delayed-repo-init.yaml')
3179 self.commitConfigUpdate(
3180 'common-config',
3181 'layouts/delayed-repo-init.yaml')
3182 self.sched.reconfigure(self.config)
3183 self.waitUntilSettled()
3184
James E. Blair287c06d2013-07-24 10:39:30 -07003185 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3186
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003187 A.addApproval('Code-Review', 2)
3188 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07003189 self.waitUntilSettled()
3190 self.assertEqual(self.getJobFromHistory('project-merge').result,
3191 'SUCCESS')
3192 self.assertEqual(self.getJobFromHistory('project-test1').result,
3193 'SUCCESS')
3194 self.assertEqual(self.getJobFromHistory('project-test2').result,
3195 'SUCCESS')
3196 self.assertEqual(A.data['status'], 'MERGED')
3197 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003198
James E. Blair9ea70072017-04-19 16:05:30 -07003199 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07003200 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07003201 self.init_repo("org/delete-project")
3202 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3203
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003204 A.addApproval('Code-Review', 2)
3205 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003206 self.waitUntilSettled()
3207 self.assertEqual(self.getJobFromHistory('project-merge').result,
3208 'SUCCESS')
3209 self.assertEqual(self.getJobFromHistory('project-test1').result,
3210 'SUCCESS')
3211 self.assertEqual(self.getJobFromHistory('project-test2').result,
3212 'SUCCESS')
3213 self.assertEqual(A.data['status'], 'MERGED')
3214 self.assertEqual(A.reported, 2)
3215
3216 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08003217 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05003218 if os.path.exists(os.path.join(self.merger_src_root, p)):
3219 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04003220 if os.path.exists(os.path.join(self.executor_src_root, p)):
3221 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003222
3223 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3224
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003225 B.addApproval('Code-Review', 2)
3226 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003227 self.waitUntilSettled()
3228 self.assertEqual(self.getJobFromHistory('project-merge').result,
3229 'SUCCESS')
3230 self.assertEqual(self.getJobFromHistory('project-test1').result,
3231 'SUCCESS')
3232 self.assertEqual(self.getJobFromHistory('project-test2').result,
3233 'SUCCESS')
3234 self.assertEqual(B.data['status'], 'MERGED')
3235 self.assertEqual(B.reported, 2)
3236
James E. Blair892cca62017-08-09 11:36:58 -07003237 @simple_layout('layouts/untrusted-secrets.yaml')
3238 def test_untrusted_secrets(self):
3239 "Test untrusted secrets"
3240 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3241 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3242 self.waitUntilSettled()
3243
3244 self.assertHistory([])
3245 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07003246 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07003247 A.messages[0])
3248
James E. Blaire68d43f2017-04-19 14:03:30 -07003249 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08003250 def test_tags(self):
3251 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08003252 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3253 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3254 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3255 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3256 self.waitUntilSettled()
3257
James E. Blaire68d43f2017-04-19 14:03:30 -07003258 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003259
3260 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07003261 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003262 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07003263 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08003264
3265 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003266 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07003267 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08003268
James E. Blair63bb0ef2013-07-29 17:14:51 -07003269 def test_timer(self):
3270 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003271 # This test can not use simple_layout because it must start
3272 # with a configuration which does not include a
3273 # timer-triggered job so that we have an opportunity to set
3274 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07003275 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04003276 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003277 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003278 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003279
Clark Boylan3ee090a2014-04-03 20:55:09 -07003280 # The pipeline triggers every second, so we should have seen
3281 # several by now.
3282 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003283 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07003284
3285 self.assertEqual(len(self.builds), 2)
3286
Paul Belanger174a8272017-03-14 13:20:10 -04003287 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07003288 # Stop queuing timer triggered jobs so that the assertions
3289 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003290 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003291 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07003292 self.waitUntilSettled()
3293 # If APScheduler is in mid-event when we remove the job, we
3294 # can end up with one more event firing, so give it an extra
3295 # second to settle.
3296 time.sleep(1)
3297 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003298 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07003299 self.waitUntilSettled()
3300
James E. Blair21037782017-07-19 11:56:55 -07003301 self.assertHistory([
3302 dict(name='project-bitrot', result='SUCCESS',
3303 ref='refs/heads/master'),
3304 dict(name='project-bitrot', result='SUCCESS',
3305 ref='refs/heads/stable'),
3306 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003307
James E. Blair4f6033c2014-03-27 15:49:09 -07003308 def test_idle(self):
3309 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003310 # This test can not use simple_layout because it must start
3311 # with a configuration which does not include a
3312 # timer-triggered job so that we have an opportunity to set
3313 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003314 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003315
Clark Boylan3ee090a2014-04-03 20:55:09 -07003316 for x in range(1, 3):
3317 # Test that timer triggers periodic jobs even across
3318 # layout config reloads.
3319 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003320 self.commitConfigUpdate('common-config',
3321 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003322 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003323 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003324
Clark Boylan3ee090a2014-04-03 20:55:09 -07003325 # The pipeline triggers every second, so we should have seen
3326 # several by now.
3327 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003328
3329 # Stop queuing timer triggered jobs so that the assertions
3330 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003331 self.commitConfigUpdate('common-config',
3332 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003333 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003334 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003335 # If APScheduler is in mid-event when we remove the job,
3336 # we can end up with one more event firing, so give it an
3337 # extra second to settle.
3338 time.sleep(1)
3339 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003340 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003341 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003342 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003343 self.waitUntilSettled()
3344 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003345 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003346
James E. Blaircc324192017-04-19 14:36:08 -07003347 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003348 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3350 self.waitUntilSettled()
3351
3352 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3353 self.waitUntilSettled()
3354
James E. Blairff80a2f2013-12-27 13:24:06 -08003355 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003356
3357 # A.messages only holds what FakeGerrit places in it. Thus we
3358 # work on the knowledge of what the first message should be as
3359 # it is only configured to go to SMTP.
3360
3361 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003362 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003363 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003364 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003365 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003366 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003367
3368 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003369 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003370 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003371 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003372 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003373 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003374
James E. Blaire5910202013-12-27 09:50:31 -08003375 def test_timer_smtp(self):
3376 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003377 # This test can not use simple_layout because it must start
3378 # with a configuration which does not include a
3379 # timer-triggered job so that we have an opportunity to set
3380 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003381 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003382 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003383 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003384
Clark Boylan3ee090a2014-04-03 20:55:09 -07003385 # The pipeline triggers every second, so we should have seen
3386 # several by now.
3387 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003388 self.waitUntilSettled()
3389
Clark Boylan3ee090a2014-04-03 20:55:09 -07003390 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003391 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003392 self.waitUntilSettled()
3393 self.assertEqual(len(self.history), 2)
3394
James E. Blaire5910202013-12-27 09:50:31 -08003395 self.assertEqual(self.getJobFromHistory(
3396 'project-bitrot-stable-old').result, 'SUCCESS')
3397 self.assertEqual(self.getJobFromHistory(
3398 'project-bitrot-stable-older').result, 'SUCCESS')
3399
James E. Blairff80a2f2013-12-27 13:24:06 -08003400 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003401
3402 # A.messages only holds what FakeGerrit places in it. Thus we
3403 # work on the knowledge of what the first message should be as
3404 # it is only configured to go to SMTP.
3405
3406 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003407 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003408 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003409 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003410 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003411 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003412
Clark Boylan3ee090a2014-04-03 20:55:09 -07003413 # Stop queuing timer triggered jobs and let any that may have
3414 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003415 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003416 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003417 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003418 # If APScheduler is in mid-event when we remove the job, we
3419 # can end up with one more event firing, so give it an extra
3420 # second to settle.
3421 time.sleep(1)
3422 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003423 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003424 self.waitUntilSettled()
3425
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003426 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003427 def test_timer_sshkey(self):
3428 "Test that a periodic job can setup SSH key authentication"
3429 self.worker.hold_jobs_in_build = True
3430 self.config.set('zuul', 'layout_config',
3431 'tests/fixtures/layout-timer.yaml')
3432 self.sched.reconfigure(self.config)
3433 self.registerJobs()
3434
3435 # The pipeline triggers every second, so we should have seen
3436 # several by now.
3437 time.sleep(5)
3438 self.waitUntilSettled()
3439
3440 self.assertEqual(len(self.builds), 2)
3441
3442 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3443 self.assertTrue(os.path.isfile(ssh_wrapper))
3444 with open(ssh_wrapper) as f:
3445 ssh_wrapper_content = f.read()
3446 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3447 # In the unit tests Merger runs in the same process,
3448 # so we see its' environment variables
3449 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3450
3451 self.worker.release('.*')
3452 self.waitUntilSettled()
3453 self.assertEqual(len(self.history), 2)
3454
3455 self.assertEqual(self.getJobFromHistory(
3456 'project-bitrot-stable-old').result, 'SUCCESS')
3457 self.assertEqual(self.getJobFromHistory(
3458 'project-bitrot-stable-older').result, 'SUCCESS')
3459
3460 # Stop queuing timer triggered jobs and let any that may have
3461 # queued through so that end of test assertions pass.
3462 self.config.set('zuul', 'layout_config',
3463 'tests/fixtures/layout-no-timer.yaml')
3464 self.sched.reconfigure(self.config)
3465 self.registerJobs()
3466 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003467 # If APScheduler is in mid-event when we remove the job, we
3468 # can end up with one more event firing, so give it an extra
3469 # second to settle.
3470 time.sleep(1)
3471 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003472 self.worker.release('.*')
3473 self.waitUntilSettled()
3474
James E. Blair91e34592015-07-31 16:45:59 -07003475 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003476 "Test that the RPC client can enqueue a change"
3477 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003478 A.addApproval('Code-Review', 2)
3479 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003480
3481 client = zuul.rpcclient.RPCClient('127.0.0.1',
3482 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003483 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003484 r = client.enqueue(tenant='tenant-one',
3485 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003486 project='org/project',
3487 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003488 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003489 self.waitUntilSettled()
3490 self.assertEqual(self.getJobFromHistory('project-merge').result,
3491 'SUCCESS')
3492 self.assertEqual(self.getJobFromHistory('project-test1').result,
3493 'SUCCESS')
3494 self.assertEqual(self.getJobFromHistory('project-test2').result,
3495 'SUCCESS')
3496 self.assertEqual(A.data['status'], 'MERGED')
3497 self.assertEqual(A.reported, 2)
3498 self.assertEqual(r, True)
3499
James E. Blair91e34592015-07-31 16:45:59 -07003500 def test_client_enqueue_ref(self):
3501 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003502 p = "review.example.com/org/project"
3503 upstream = self.getUpstreamRepos([p])
3504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3505 A.setMerged()
3506 A_commit = str(upstream[p].commit('master'))
3507 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003508
3509 client = zuul.rpcclient.RPCClient('127.0.0.1',
3510 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003511 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003512 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003513 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003514 pipeline='post',
3515 project='org/project',
3516 trigger='gerrit',
3517 ref='master',
3518 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003519 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003520 self.waitUntilSettled()
3521 job_names = [x.name for x in self.history]
3522 self.assertEqual(len(self.history), 1)
3523 self.assertIn('project-post', job_names)
3524 self.assertEqual(r, True)
3525
James E. Blairad28e912013-11-27 10:43:22 -08003526 def test_client_enqueue_negative(self):
3527 "Test that the RPC client returns errors"
3528 client = zuul.rpcclient.RPCClient('127.0.0.1',
3529 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003530 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003531 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003532 "Invalid tenant"):
3533 r = client.enqueue(tenant='tenant-foo',
3534 pipeline='gate',
3535 project='org/project',
3536 trigger='gerrit',
3537 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003538 self.assertEqual(r, False)
3539
3540 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003541 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003542 r = client.enqueue(tenant='tenant-one',
3543 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003544 project='project-does-not-exist',
3545 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003546 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003547 self.assertEqual(r, False)
3548
3549 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3550 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003551 r = client.enqueue(tenant='tenant-one',
3552 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003553 project='org/project',
3554 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003555 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003556 self.assertEqual(r, False)
3557
3558 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3559 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003560 r = client.enqueue(tenant='tenant-one',
3561 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003562 project='org/project',
3563 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003564 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003565 self.assertEqual(r, False)
3566
3567 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3568 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003569 r = client.enqueue(tenant='tenant-one',
3570 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003571 project='org/project',
3572 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003573 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003574 self.assertEqual(r, False)
3575
3576 self.waitUntilSettled()
3577 self.assertEqual(len(self.history), 0)
3578 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003579
3580 def test_client_promote(self):
3581 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003582 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003583 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3584 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3585 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003586 A.addApproval('Code-Review', 2)
3587 B.addApproval('Code-Review', 2)
3588 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003589
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003590 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3591 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3592 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003593
3594 self.waitUntilSettled()
3595
Paul Belangerbaca3132016-11-04 12:49:54 -04003596 tenant = self.sched.abide.tenants.get('tenant-one')
3597 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003598 enqueue_times = {}
3599 for item in items:
3600 enqueue_times[str(item.change)] = item.enqueue_time
3601
James E. Blair36658cf2013-12-06 17:53:48 -08003602 client = zuul.rpcclient.RPCClient('127.0.0.1',
3603 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003604 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003605 r = client.promote(tenant='tenant-one',
3606 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003607 change_ids=['2,1', '3,1'])
3608
Sean Daguef39b9ca2014-01-10 21:34:35 -05003609 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003610 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003611 for item in items:
3612 self.assertEqual(
3613 enqueue_times[str(item.change)], item.enqueue_time)
3614
James E. Blair78acec92014-02-06 07:11:32 -08003615 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003616 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003617 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003618 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003619 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003620 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003621 self.waitUntilSettled()
3622
3623 self.assertEqual(len(self.builds), 6)
3624 self.assertEqual(self.builds[0].name, 'project-test1')
3625 self.assertEqual(self.builds[1].name, 'project-test2')
3626 self.assertEqual(self.builds[2].name, 'project-test1')
3627 self.assertEqual(self.builds[3].name, 'project-test2')
3628 self.assertEqual(self.builds[4].name, 'project-test1')
3629 self.assertEqual(self.builds[5].name, 'project-test2')
3630
Paul Belangerbaca3132016-11-04 12:49:54 -04003631 self.assertTrue(self.builds[0].hasChanges(B))
3632 self.assertFalse(self.builds[0].hasChanges(A))
3633 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003634
Paul Belangerbaca3132016-11-04 12:49:54 -04003635 self.assertTrue(self.builds[2].hasChanges(B))
3636 self.assertTrue(self.builds[2].hasChanges(C))
3637 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003638
Paul Belangerbaca3132016-11-04 12:49:54 -04003639 self.assertTrue(self.builds[4].hasChanges(B))
3640 self.assertTrue(self.builds[4].hasChanges(C))
3641 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003642
Paul Belanger174a8272017-03-14 13:20:10 -04003643 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003644 self.waitUntilSettled()
3645
3646 self.assertEqual(A.data['status'], 'MERGED')
3647 self.assertEqual(A.reported, 2)
3648 self.assertEqual(B.data['status'], 'MERGED')
3649 self.assertEqual(B.reported, 2)
3650 self.assertEqual(C.data['status'], 'MERGED')
3651 self.assertEqual(C.reported, 2)
3652
James E. Blair36658cf2013-12-06 17:53:48 -08003653 self.assertEqual(r, True)
3654
3655 def test_client_promote_dependent(self):
3656 "Test that the RPC client can promote a dependent change"
3657 # C (depends on B) -> B -> A ; then promote C to get:
3658 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003659 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003660 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3661 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3662 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3663
3664 C.setDependsOn(B, 1)
3665
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003666 A.addApproval('Code-Review', 2)
3667 B.addApproval('Code-Review', 2)
3668 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003669
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003670 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3671 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3672 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003673
3674 self.waitUntilSettled()
3675
3676 client = zuul.rpcclient.RPCClient('127.0.0.1',
3677 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003678 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003679 r = client.promote(tenant='tenant-one',
3680 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003681 change_ids=['3,1'])
3682
James E. Blair78acec92014-02-06 07:11:32 -08003683 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003684 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003685 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003686 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003687 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003688 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003689 self.waitUntilSettled()
3690
3691 self.assertEqual(len(self.builds), 6)
3692 self.assertEqual(self.builds[0].name, 'project-test1')
3693 self.assertEqual(self.builds[1].name, 'project-test2')
3694 self.assertEqual(self.builds[2].name, 'project-test1')
3695 self.assertEqual(self.builds[3].name, 'project-test2')
3696 self.assertEqual(self.builds[4].name, 'project-test1')
3697 self.assertEqual(self.builds[5].name, 'project-test2')
3698
Paul Belangerbaca3132016-11-04 12:49:54 -04003699 self.assertTrue(self.builds[0].hasChanges(B))
3700 self.assertFalse(self.builds[0].hasChanges(A))
3701 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003702
Paul Belangerbaca3132016-11-04 12:49:54 -04003703 self.assertTrue(self.builds[2].hasChanges(B))
3704 self.assertTrue(self.builds[2].hasChanges(C))
3705 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003706
Paul Belangerbaca3132016-11-04 12:49:54 -04003707 self.assertTrue(self.builds[4].hasChanges(B))
3708 self.assertTrue(self.builds[4].hasChanges(C))
3709 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003710
Paul Belanger174a8272017-03-14 13:20:10 -04003711 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003712 self.waitUntilSettled()
3713
3714 self.assertEqual(A.data['status'], 'MERGED')
3715 self.assertEqual(A.reported, 2)
3716 self.assertEqual(B.data['status'], 'MERGED')
3717 self.assertEqual(B.reported, 2)
3718 self.assertEqual(C.data['status'], 'MERGED')
3719 self.assertEqual(C.reported, 2)
3720
James E. Blair36658cf2013-12-06 17:53:48 -08003721 self.assertEqual(r, True)
3722
3723 def test_client_promote_negative(self):
3724 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003725 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003726 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003727 A.addApproval('Code-Review', 2)
3728 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003729 self.waitUntilSettled()
3730
3731 client = zuul.rpcclient.RPCClient('127.0.0.1',
3732 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003733 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003734
3735 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003736 r = client.promote(tenant='tenant-one',
3737 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003738 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003739 self.assertEqual(r, False)
3740
3741 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003742 r = client.promote(tenant='tenant-one',
3743 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003744 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003745 self.assertEqual(r, False)
3746
Paul Belanger174a8272017-03-14 13:20:10 -04003747 self.executor_server.hold_jobs_in_build = False
3748 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003749 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003750
James E. Blaircc324192017-04-19 14:36:08 -07003751 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003752 def test_queue_rate_limiting(self):
3753 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003754 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003755 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3756 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3757 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3758
3759 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003760 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003761
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003762 A.addApproval('Code-Review', 2)
3763 B.addApproval('Code-Review', 2)
3764 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003765
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003766 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3767 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3768 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003769 self.waitUntilSettled()
3770
3771 # Only A and B will have their merge jobs queued because
3772 # window is 2.
3773 self.assertEqual(len(self.builds), 2)
3774 self.assertEqual(self.builds[0].name, 'project-merge')
3775 self.assertEqual(self.builds[1].name, 'project-merge')
3776
James E. Blair732ee862017-04-21 07:49:26 -07003777 # Release the merge jobs one at a time.
3778 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003779 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003780 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003781 self.waitUntilSettled()
3782
3783 # Only A and B will have their test jobs queued because
3784 # window is 2.
3785 self.assertEqual(len(self.builds), 4)
3786 self.assertEqual(self.builds[0].name, 'project-test1')
3787 self.assertEqual(self.builds[1].name, 'project-test2')
3788 self.assertEqual(self.builds[2].name, 'project-test1')
3789 self.assertEqual(self.builds[3].name, 'project-test2')
3790
Paul Belanger174a8272017-03-14 13:20:10 -04003791 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003792 self.waitUntilSettled()
3793
James E. Blaircc324192017-04-19 14:36:08 -07003794 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003795 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003796 # A failed so window is reduced by 1 to 1.
3797 self.assertEqual(queue.window, 1)
3798 self.assertEqual(queue.window_floor, 1)
3799 self.assertEqual(A.data['status'], 'NEW')
3800
3801 # Gate is reset and only B's merge job is queued because
3802 # window shrunk to 1.
3803 self.assertEqual(len(self.builds), 1)
3804 self.assertEqual(self.builds[0].name, 'project-merge')
3805
Paul Belanger174a8272017-03-14 13:20:10 -04003806 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003807 self.waitUntilSettled()
3808
3809 # Only B's test jobs are queued because window is still 1.
3810 self.assertEqual(len(self.builds), 2)
3811 self.assertEqual(self.builds[0].name, 'project-test1')
3812 self.assertEqual(self.builds[1].name, 'project-test2')
3813
Paul Belanger174a8272017-03-14 13:20:10 -04003814 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003815 self.waitUntilSettled()
3816
3817 # B was successfully merged so window is increased to 2.
3818 self.assertEqual(queue.window, 2)
3819 self.assertEqual(queue.window_floor, 1)
3820 self.assertEqual(B.data['status'], 'MERGED')
3821
3822 # Only C is left and its merge job is queued.
3823 self.assertEqual(len(self.builds), 1)
3824 self.assertEqual(self.builds[0].name, 'project-merge')
3825
Paul Belanger174a8272017-03-14 13:20:10 -04003826 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003827 self.waitUntilSettled()
3828
3829 # After successful merge job the test jobs for C are queued.
3830 self.assertEqual(len(self.builds), 2)
3831 self.assertEqual(self.builds[0].name, 'project-test1')
3832 self.assertEqual(self.builds[1].name, 'project-test2')
3833
Paul Belanger174a8272017-03-14 13:20:10 -04003834 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003835 self.waitUntilSettled()
3836
3837 # C successfully merged so window is bumped to 3.
3838 self.assertEqual(queue.window, 3)
3839 self.assertEqual(queue.window_floor, 1)
3840 self.assertEqual(C.data['status'], 'MERGED')
3841
James E. Blaircc324192017-04-19 14:36:08 -07003842 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003843 def test_queue_rate_limiting_dependent(self):
3844 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003845 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003846 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3847 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3848 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3849
3850 B.setDependsOn(A, 1)
3851
Paul Belanger174a8272017-03-14 13:20:10 -04003852 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003853
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003854 A.addApproval('Code-Review', 2)
3855 B.addApproval('Code-Review', 2)
3856 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003857
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003858 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3859 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3860 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003861 self.waitUntilSettled()
3862
3863 # Only A and B will have their merge jobs queued because
3864 # window is 2.
3865 self.assertEqual(len(self.builds), 2)
3866 self.assertEqual(self.builds[0].name, 'project-merge')
3867 self.assertEqual(self.builds[1].name, 'project-merge')
3868
James E. Blairf4a5f022017-04-18 14:01:10 -07003869 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003870
3871 # Only A and B will have their test jobs queued because
3872 # window is 2.
3873 self.assertEqual(len(self.builds), 4)
3874 self.assertEqual(self.builds[0].name, 'project-test1')
3875 self.assertEqual(self.builds[1].name, 'project-test2')
3876 self.assertEqual(self.builds[2].name, 'project-test1')
3877 self.assertEqual(self.builds[3].name, 'project-test2')
3878
Paul Belanger174a8272017-03-14 13:20:10 -04003879 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003880 self.waitUntilSettled()
3881
James E. Blaircc324192017-04-19 14:36:08 -07003882 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003883 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003884 # A failed so window is reduced by 1 to 1.
3885 self.assertEqual(queue.window, 1)
3886 self.assertEqual(queue.window_floor, 1)
3887 self.assertEqual(A.data['status'], 'NEW')
3888 self.assertEqual(B.data['status'], 'NEW')
3889
3890 # Gate is reset and only C's merge job is queued because
3891 # window shrunk to 1 and A and B were dequeued.
3892 self.assertEqual(len(self.builds), 1)
3893 self.assertEqual(self.builds[0].name, 'project-merge')
3894
James E. Blairf4a5f022017-04-18 14:01:10 -07003895 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003896
3897 # Only C's test jobs are queued because window is still 1.
3898 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003899 builds = self.getSortedBuilds()
3900 self.assertEqual(builds[0].name, 'project-test1')
3901 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003902
Paul Belanger174a8272017-03-14 13:20:10 -04003903 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003904 self.waitUntilSettled()
3905
3906 # C was successfully merged so window is increased to 2.
3907 self.assertEqual(queue.window, 2)
3908 self.assertEqual(queue.window_floor, 1)
3909 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003910
James E. Blair8fd207b2017-11-30 13:44:38 -08003911 @simple_layout('layouts/reconfigure-window.yaml')
3912 def test_reconfigure_window_shrink(self):
3913 # Test the active window shrinking during reconfiguration
3914 self.executor_server.hold_jobs_in_build = True
3915
3916 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3917 A.addApproval('Code-Review', 2)
3918 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3919 self.waitUntilSettled()
3920 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3921 B.addApproval('Code-Review', 2)
3922 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3923 self.waitUntilSettled()
3924
3925 tenant = self.sched.abide.tenants.get('tenant-one')
3926 queue = tenant.layout.pipelines['gate'].queues[0]
3927 self.assertEqual(queue.window, 20)
3928 self.assertTrue(len(self.builds), 4)
3929
3930 self.executor_server.release('job1')
3931 self.waitUntilSettled()
3932 self.commitConfigUpdate('org/common-config',
3933 'layouts/reconfigure-window2.yaml')
3934 self.sched.reconfigure(self.config)
3935 tenant = self.sched.abide.tenants.get('tenant-one')
3936 queue = tenant.layout.pipelines['gate'].queues[0]
James E. Blair74be3812017-11-30 14:17:25 -08003937 # Even though we have configured a smaller window, the value
3938 # on the existing shared queue should be used.
3939 self.assertEqual(queue.window, 20)
3940 self.assertTrue(len(self.builds), 4)
3941
3942 self.sched.reconfigure(self.config)
3943 tenant = self.sched.abide.tenants.get('tenant-one')
3944 queue = tenant.layout.pipelines['gate'].queues[0]
3945 self.assertEqual(queue.window, 20)
3946 self.assertTrue(len(self.builds), 4)
3947
3948 self.executor_server.hold_jobs_in_build = False
3949 self.executor_server.release()
3950
3951 self.waitUntilSettled()
3952 self.assertHistory([
3953 dict(name='job1', result='SUCCESS', changes='1,1'),
3954 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3955 dict(name='job2', result='SUCCESS', changes='1,1'),
3956 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3957 ], ordered=False)
3958
3959 @simple_layout('layouts/reconfigure-window-fixed.yaml')
3960 def test_reconfigure_window_fixed(self):
3961 # Test the active window shrinking during reconfiguration
3962 self.executor_server.hold_jobs_in_build = True
3963
3964 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3965 A.addApproval('Code-Review', 2)
3966 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3967 self.waitUntilSettled()
3968 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3969 B.addApproval('Code-Review', 2)
3970 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3971 self.waitUntilSettled()
3972
3973 tenant = self.sched.abide.tenants.get('tenant-one')
3974 queue = tenant.layout.pipelines['gate'].queues[0]
3975 self.assertEqual(queue.window, 2)
3976 self.assertTrue(len(self.builds), 4)
3977
3978 self.executor_server.release('job1')
3979 self.waitUntilSettled()
3980 self.commitConfigUpdate('org/common-config',
3981 'layouts/reconfigure-window-fixed2.yaml')
3982 self.sched.reconfigure(self.config)
3983 tenant = self.sched.abide.tenants.get('tenant-one')
3984 queue = tenant.layout.pipelines['gate'].queues[0]
3985 # Because we have configured a static window, it should
3986 # be allowed to shrink on reconfiguration.
James E. Blair8fd207b2017-11-30 13:44:38 -08003987 self.assertEqual(queue.window, 1)
James E. Blair74be3812017-11-30 14:17:25 -08003988 # B is outside the window, but still marked active until the
3989 # next pass through the queue processor, so its builds haven't
3990 # been canceled.
James E. Blair8fd207b2017-11-30 13:44:38 -08003991 self.assertTrue(len(self.builds), 4)
3992
3993 self.sched.reconfigure(self.config)
3994 tenant = self.sched.abide.tenants.get('tenant-one')
3995 queue = tenant.layout.pipelines['gate'].queues[0]
3996 self.assertEqual(queue.window, 1)
3997 # B's builds have been canceled now
3998 self.assertTrue(len(self.builds), 2)
3999
4000 self.executor_server.hold_jobs_in_build = False
4001 self.executor_server.release()
James E. Blair74be3812017-11-30 14:17:25 -08004002
4003 # B's builds will be restarted and will show up in our history
4004 # twice.
James E. Blair8fd207b2017-11-30 13:44:38 -08004005 self.waitUntilSettled()
4006 self.assertHistory([
4007 dict(name='job1', result='SUCCESS', changes='1,1'),
4008 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
4009 dict(name='job2', result='SUCCESS', changes='1,1'),
4010 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
4011 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
4012 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
4013 ], ordered=False)
4014
4015 @simple_layout('layouts/reconfigure-remove-add.yaml')
4016 def test_reconfigure_remove_add(self):
4017 # Test removing, then adding a job while in queue
4018 self.executor_server.hold_jobs_in_build = True
4019
4020 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4021 A.addApproval('Code-Review', 2)
4022 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4023 self.waitUntilSettled()
4024 self.assertTrue(len(self.builds), 2)
4025 self.executor_server.release('job2')
4026 self.assertTrue(len(self.builds), 1)
4027
4028 # Remove job2
4029 self.commitConfigUpdate('org/common-config',
4030 'layouts/reconfigure-remove-add2.yaml')
4031 self.sched.reconfigure(self.config)
4032 self.assertTrue(len(self.builds), 1)
4033
4034 # Add job2 back
4035 self.commitConfigUpdate('org/common-config',
4036 'layouts/reconfigure-remove-add.yaml')
4037 self.sched.reconfigure(self.config)
4038 self.assertTrue(len(self.builds), 2)
4039
4040 self.executor_server.hold_jobs_in_build = False
4041 self.executor_server.release()
4042 # This will run new builds for B
4043 self.waitUntilSettled()
4044 self.assertHistory([
4045 dict(name='job2', result='SUCCESS', changes='1,1'),
4046 dict(name='job1', result='SUCCESS', changes='1,1'),
4047 dict(name='job2', result='SUCCESS', changes='1,1'),
4048 ], ordered=False)
4049
Joshua Heskethba8776a2014-01-12 14:35:40 +08004050 def test_worker_update_metadata(self):
4051 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04004052 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08004053
4054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004055 A.addApproval('Code-Review', 2)
4056 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08004057 self.waitUntilSettled()
4058
Clint Byrumd4de79a2017-05-27 11:39:13 -07004059 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08004060
4061 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07004062 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08004063
4064 start = time.time()
4065 while True:
4066 if time.time() - start > 10:
4067 raise Exception("Timeout waiting for gearman server to report "
4068 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07004069 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05004070 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08004071 break
4072 else:
4073 time.sleep(0)
4074
4075 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05004076 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08004077
Paul Belanger174a8272017-03-14 13:20:10 -04004078 self.executor_server.hold_jobs_in_build = False
4079 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08004080 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004081
James E. Blaircc324192017-04-19 14:36:08 -07004082 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004083 def test_footer_message(self):
4084 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004085 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004086 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04004087 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004088 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004089 self.waitUntilSettled()
4090
4091 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004092 B.addApproval('Code-Review', 2)
4093 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004094 self.waitUntilSettled()
4095
4096 self.assertEqual(2, len(self.smtp_messages))
4097
Adam Gandelman82258fd2017-02-07 16:51:58 -08004098 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004099Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08004100http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004101
Adam Gandelman82258fd2017-02-07 16:51:58 -08004102 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004103For CI problems and help debugging, contact ci@example.org"""
4104
Adam Gandelman82258fd2017-02-07 16:51:58 -08004105 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
4106 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
4107 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
4108 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11004109
Tobias Henkel9842bd72017-05-16 13:40:03 +02004110 @simple_layout('layouts/unmanaged-project.yaml')
4111 def test_unmanaged_project_start_message(self):
4112 "Test start reporting is not done for unmanaged projects."
4113 self.init_repo("org/project", tag='init')
4114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4115 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4116 self.waitUntilSettled()
4117
4118 self.assertEqual(0, len(A.messages))
4119
Clint Byrumb9de8be2017-05-27 13:49:19 -07004120 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11004121 def test_merge_failure_reporters(self):
4122 """Check that the config is set up correctly"""
4123
Clint Byrumb9de8be2017-05-27 13:49:19 -07004124 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11004125 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00004126 "Merge Failed.\n\nThis change or one of its cross-repo "
4127 "dependencies was unable to be automatically merged with the "
4128 "current state of its repository. Please rebase the change and "
4129 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07004130 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11004131 self.assertEqual(
4132 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07004133 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11004134
4135 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004136 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11004137 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004138 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11004139
4140 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004141 tenant.layout.pipelines['check'].merge_failure_actions[0],
4142 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11004143
4144 self.assertTrue(
4145 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07004146 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004147 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004148 zuul.driver.smtp.smtpreporter.SMTPReporter) and
4149 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004150 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004151 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11004152 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07004153 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004154 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004155 gerritreporter.GerritReporter) and
4156 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004157 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004158 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11004159 )
4160 )
4161
James E. Blairec056492016-07-22 09:45:56 -07004162 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11004163 def test_merge_failure_reports(self):
4164 """Check that when a change fails to merge the correct message is sent
4165 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08004166 self.updateConfigLayout(
4167 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11004168 self.sched.reconfigure(self.config)
4169 self.registerJobs()
4170
4171 # Check a test failure isn't reported to SMTP
4172 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004173 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04004174 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004175 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11004176 self.waitUntilSettled()
4177
4178 self.assertEqual(3, len(self.history)) # 3 jobs
4179 self.assertEqual(0, len(self.smtp_messages))
4180
4181 # Check a merge failure is reported to SMTP
4182 # B should be merged, but C will conflict with B
4183 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4184 B.addPatchset(['conflict'])
4185 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4186 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004187 B.addApproval('Code-Review', 2)
4188 C.addApproval('Code-Review', 2)
4189 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4190 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11004191 self.waitUntilSettled()
4192
4193 self.assertEqual(6, len(self.history)) # A and B jobs
4194 self.assertEqual(1, len(self.smtp_messages))
4195 self.assertEqual('The merge failed! For more information...',
4196 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11004197
James E. Blairec056492016-07-22 09:45:56 -07004198 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08004199 def test_default_merge_failure_reports(self):
4200 """Check that the default merge failure reports are correct."""
4201
4202 # A should report success, B should report merge failure.
4203 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4204 A.addPatchset(['conflict'])
4205 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4206 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004207 A.addApproval('Code-Review', 2)
4208 B.addApproval('Code-Review', 2)
4209 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4210 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08004211 self.waitUntilSettled()
4212
4213 self.assertEqual(3, len(self.history)) # A jobs
4214 self.assertEqual(A.reported, 2)
4215 self.assertEqual(B.reported, 2)
4216 self.assertEqual(A.data['status'], 'MERGED')
4217 self.assertEqual(B.data['status'], 'NEW')
4218 self.assertIn('Build succeeded', A.messages[1])
4219 self.assertIn('Merge Failed', B.messages[1])
4220 self.assertIn('automatically merged', B.messages[1])
4221 self.assertNotIn('logs.example.com', B.messages[1])
4222 self.assertNotIn('SKIPPED', B.messages[1])
4223
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004224 def test_client_get_running_jobs(self):
4225 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04004226 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004227 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004228 A.addApproval('Code-Review', 2)
4229 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004230 self.waitUntilSettled()
4231
4232 client = zuul.rpcclient.RPCClient('127.0.0.1',
4233 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07004234 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004235
4236 # Wait for gearman server to send the initial workData back to zuul
4237 start = time.time()
4238 while True:
4239 if time.time() - start > 10:
4240 raise Exception("Timeout waiting for gearman server to report "
4241 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07004242 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05004243 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004244 break
4245 else:
4246 time.sleep(0)
4247
4248 running_items = client.get_running_jobs()
4249
4250 self.assertEqual(1, len(running_items))
4251 running_item = running_items[0]
4252 self.assertEqual([], running_item['failing_reasons'])
4253 self.assertEqual([], running_item['items_behind'])
James E. Blair0e4c7912018-01-02 14:20:17 -08004254 self.assertEqual('https://review.example.com/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004255 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004256 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004257 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004258 self.assertEqual(True, running_item['active'])
4259 self.assertEqual('1,1', running_item['id'])
4260
4261 self.assertEqual(3, len(running_item['jobs']))
4262 for job in running_item['jobs']:
4263 if job['name'] == 'project-merge':
4264 self.assertEqual('project-merge', job['name'])
4265 self.assertEqual('gate', job['pipeline'])
4266 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05004267 self.assertEqual(
Tristan Cacqueraya4996f12017-09-20 01:00:28 +00004268 'stream.html?uuid={uuid}&logfile=console.log'
Tobias Henkelb4407fc2017-07-07 13:52:56 +02004269 .format(uuid=job['uuid']), job['url'])
4270 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05004271 'finger://{hostname}/{uuid}'.format(
4272 hostname=self.executor_server.hostname,
4273 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02004274 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07004275 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004276 self.assertEqual(False, job['canceled'])
4277 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004278 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004279 self.assertEqual('gate', job['pipeline'])
4280 break
4281
Paul Belanger174a8272017-03-14 13:20:10 -04004282 self.executor_server.hold_jobs_in_build = False
4283 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004284 self.waitUntilSettled()
4285
4286 running_items = client.get_running_jobs()
4287 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07004288
James E. Blairb70e55a2017-04-19 12:57:02 -07004289 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004290 def test_nonvoting_pipeline(self):
4291 "Test that a nonvoting pipeline (experimental) can still report"
4292
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004293 A = self.fake_gerrit.addFakeChange('org/experimental-project',
4294 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4296 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04004297
4298 self.assertEqual(self.getJobFromHistory('project-merge').result,
4299 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004300 self.assertEqual(
4301 self.getJobFromHistory('experimental-project-test').result,
4302 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004303 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08004304
Jesse Keatingcc67b122017-04-19 14:54:57 -07004305 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004306 def test_disable_at(self):
4307 "Test a pipeline will only report to the disabled trigger when failing"
4308
Jesse Keatingcc67b122017-04-19 14:54:57 -07004309 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004310 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004311 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004312 0, tenant.layout.pipelines['check']._consecutive_failures)
4313 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004314
4315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4316 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4317 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4318 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4319 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4320 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4321 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4322 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4323 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4324 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4325 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4326
Paul Belanger174a8272017-03-14 13:20:10 -04004327 self.executor_server.failJob('project-test1', A)
4328 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004329 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004330 self.executor_server.failJob('project-test1', D)
4331 self.executor_server.failJob('project-test1', E)
4332 self.executor_server.failJob('project-test1', F)
4333 self.executor_server.failJob('project-test1', G)
4334 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004335 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004336 self.executor_server.failJob('project-test1', J)
4337 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004338
4339 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4340 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4341 self.waitUntilSettled()
4342
4343 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004344 2, tenant.layout.pipelines['check']._consecutive_failures)
4345 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004346
4347 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4348 self.waitUntilSettled()
4349
4350 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004351 0, tenant.layout.pipelines['check']._consecutive_failures)
4352 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004353
4354 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4355 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4356 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4357 self.waitUntilSettled()
4358
4359 # We should be disabled now
4360 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004361 3, tenant.layout.pipelines['check']._consecutive_failures)
4362 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004363
4364 # We need to wait between each of these patches to make sure the
4365 # smtp messages come back in an expected order
4366 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4367 self.waitUntilSettled()
4368 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4369 self.waitUntilSettled()
4370 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4371 self.waitUntilSettled()
4372
4373 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4374 # leaving a message on each change
4375 self.assertEqual(1, len(A.messages))
4376 self.assertIn('Build failed.', A.messages[0])
4377 self.assertEqual(1, len(B.messages))
4378 self.assertIn('Build failed.', B.messages[0])
4379 self.assertEqual(1, len(C.messages))
4380 self.assertIn('Build succeeded.', C.messages[0])
4381 self.assertEqual(1, len(D.messages))
4382 self.assertIn('Build failed.', D.messages[0])
4383 self.assertEqual(1, len(E.messages))
4384 self.assertIn('Build failed.', E.messages[0])
4385 self.assertEqual(1, len(F.messages))
4386 self.assertIn('Build failed.', F.messages[0])
4387
4388 # The last 3 (GHI) would have only reported via smtp.
4389 self.assertEqual(3, len(self.smtp_messages))
4390 self.assertEqual(0, len(G.messages))
4391 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004392 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004393 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004394 self.assertEqual(0, len(H.messages))
4395 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004396 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004397 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004398 self.assertEqual(0, len(I.messages))
4399 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004400 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004401 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004402
4403 # Now reload the configuration (simulate a HUP) to check the pipeline
4404 # comes out of disabled
4405 self.sched.reconfigure(self.config)
4406
Jesse Keatingcc67b122017-04-19 14:54:57 -07004407 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004408
4409 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004410 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004411 0, tenant.layout.pipelines['check']._consecutive_failures)
4412 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004413
4414 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4415 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4416 self.waitUntilSettled()
4417
4418 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004419 2, tenant.layout.pipelines['check']._consecutive_failures)
4420 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004421
4422 # J and K went back to gerrit
4423 self.assertEqual(1, len(J.messages))
4424 self.assertIn('Build failed.', J.messages[0])
4425 self.assertEqual(1, len(K.messages))
4426 self.assertIn('Build failed.', K.messages[0])
4427 # No more messages reported via smtp
4428 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004429
James E. Blaircc020532017-04-19 13:04:44 -07004430 @simple_layout('layouts/one-job-project.yaml')
4431 def test_one_job_project(self):
4432 "Test that queueing works with one job"
4433 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4434 'master', 'A')
4435 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4436 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004437 A.addApproval('Code-Review', 2)
4438 B.addApproval('Code-Review', 2)
4439 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4440 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004441 self.waitUntilSettled()
4442
4443 self.assertEqual(A.data['status'], 'MERGED')
4444 self.assertEqual(A.reported, 2)
4445 self.assertEqual(B.data['status'], 'MERGED')
4446 self.assertEqual(B.reported, 2)
4447
Paul Belanger32023542017-10-06 14:48:50 -04004448 def test_job_aborted(self):
4449 "Test that if a execute server aborts a job, it is run again"
4450 self.executor_server.hold_jobs_in_build = True
4451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4452 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4453 self.waitUntilSettled()
4454
4455 self.executor_server.release('.*-merge')
4456 self.waitUntilSettled()
4457
4458 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004459
4460 # first abort
4461 self.builds[0].aborted = True
4462 self.executor_server.release('.*-test*')
4463 self.waitUntilSettled()
4464 self.assertEqual(len(self.builds), 1)
4465
4466 # second abort
4467 self.builds[0].aborted = True
4468 self.executor_server.release('.*-test*')
4469 self.waitUntilSettled()
4470 self.assertEqual(len(self.builds), 1)
4471
4472 # third abort
4473 self.builds[0].aborted = True
4474 self.executor_server.release('.*-test*')
4475 self.waitUntilSettled()
4476 self.assertEqual(len(self.builds), 1)
4477
4478 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004479 self.builds[0].aborted = True
4480 self.executor_server.release('.*-test*')
4481 self.waitUntilSettled()
4482 self.assertEqual(len(self.builds), 1)
4483
4484 self.executor_server.hold_jobs_in_build = False
4485 self.executor_server.release()
4486 self.waitUntilSettled()
4487
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004488 self.assertEqual(len(self.history), 7)
4489 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004490 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4491
Paul Belanger71d98172016-11-08 10:56:31 -05004492 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004493 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004494
Paul Belanger174a8272017-03-14 13:20:10 -04004495 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004496 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4497 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4498 self.waitUntilSettled()
4499
Paul Belanger174a8272017-03-14 13:20:10 -04004500 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004501 self.waitUntilSettled()
4502
4503 self.assertEqual(len(self.builds), 2)
4504 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004505 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004506 self.waitUntilSettled()
4507
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004508 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004509 self.assertEqual(len(self.builds), 1,
4510 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004511 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004512 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004513 self.waitUntilSettled()
4514
Paul Belanger174a8272017-03-14 13:20:10 -04004515 self.executor_server.hold_jobs_in_build = False
4516 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004517 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004518 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004519 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4520 self.assertEqual(A.reported, 1)
4521 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004522
James E. Blair15be0e12017-01-03 13:45:20 -08004523 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004524 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004525
4526 self.fake_nodepool.paused = True
4527 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004528 A.addApproval('Code-Review', 2)
4529 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004530 self.waitUntilSettled()
4531
4532 self.zk.client.stop()
4533 self.zk.client.start()
4534 self.fake_nodepool.paused = False
4535 self.waitUntilSettled()
4536
4537 self.assertEqual(A.data['status'], 'MERGED')
4538 self.assertEqual(A.reported, 2)
4539
James E. Blairb4bed1d2018-02-06 13:43:50 -08004540 def test_zookeeper_disconnect2(self):
4541 "Test that jobs are executed after a zookeeper disconnect"
4542
4543 # This tests receiving a ZK disconnect between the arrival of
4544 # a fulfilled request and when we accept its nodes.
4545 self.fake_nodepool.paused = True
4546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4547 A.addApproval('Code-Review', 2)
4548 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4549 self.waitUntilSettled()
4550
4551 # We're waiting on the nodepool request to complete. Stop the
4552 # scheduler from processing further events, then fulfill the
4553 # nodepool request.
4554 self.sched.run_handler_lock.acquire()
4555
4556 # Fulfill the nodepool request.
4557 self.fake_nodepool.paused = False
4558 requests = list(self.sched.nodepool.requests.values())
4559 self.assertEqual(1, len(requests))
4560 request = requests[0]
4561 for x in iterate_timeout(30, 'fulfill request'):
4562 if request.fulfilled:
4563 break
4564 id1 = request.id
4565
4566 # The request is fulfilled, but the scheduler hasn't processed
4567 # it yet. Reconnect ZK.
4568 self.zk.client.stop()
4569 self.zk.client.start()
4570
4571 # Allow the scheduler to continue and process the (now
4572 # out-of-date) notification that nodes are ready.
4573 self.sched.run_handler_lock.release()
4574
4575 # It should resubmit the request, once it's fulfilled, we can
4576 # wait for it to run jobs and settle.
4577 for x in iterate_timeout(30, 'fulfill request'):
4578 if request.fulfilled:
4579 break
4580 self.waitUntilSettled()
4581
4582 id2 = request.id
4583 self.assertEqual(A.data['status'], 'MERGED')
4584 self.assertEqual(A.reported, 2)
4585 # Make sure it was resubmitted (the id's should be different).
4586 self.assertNotEqual(id1, id2)
4587
James E. Blair6ab79e02017-01-06 10:10:17 -08004588 def test_nodepool_failure(self):
4589 "Test that jobs are reported after a nodepool failure"
4590
4591 self.fake_nodepool.paused = True
4592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004593 A.addApproval('Code-Review', 2)
4594 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004595 self.waitUntilSettled()
4596
4597 req = self.fake_nodepool.getNodeRequests()[0]
4598 self.fake_nodepool.addFailRequest(req)
4599
4600 self.fake_nodepool.paused = False
4601 self.waitUntilSettled()
4602
4603 self.assertEqual(A.data['status'], 'NEW')
4604 self.assertEqual(A.reported, 2)
4605 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4606 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4607 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4608
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004609 def test_nodepool_priority(self):
4610 "Test that nodes are requested at the correct priority"
4611
4612 self.fake_nodepool.paused = True
4613
4614 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4615 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4616
4617 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4618 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4619
4620 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4621 C.addApproval('Code-Review', 2)
4622 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4623
4624 self.waitUntilSettled()
4625
4626 reqs = self.fake_nodepool.getNodeRequests()
4627
4628 # The requests come back sorted by oid. Since we have three requests
4629 # for the three changes each with a different priority.
4630 # Also they get a serial number based on order they were received
4631 # so the number on the endof the oid should map to order submitted.
4632
4633 # * gate first - high priority - change C
4634 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4635 self.assertEqual(reqs[0]['node_types'], ['label1'])
4636 # * check second - normal priority - change B
4637 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4638 self.assertEqual(reqs[1]['node_types'], ['label1'])
4639 # * post third - low priority - change A
4640 # additionally, the post job defined uses an ubuntu-xenial node,
4641 # so we include that check just as an extra verification
4642 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4643 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4644
4645 self.fake_nodepool.paused = False
4646 self.waitUntilSettled()
4647
James E. Blairfceaf412017-09-29 13:44:57 -07004648 @simple_layout('layouts/multiple-templates.yaml')
4649 def test_multiple_project_templates(self):
4650 # Test that applying multiple project templates to a project
4651 # doesn't alter them when used for a second project.
4652 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4653 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4654 self.waitUntilSettled()
4655
4656 build = self.getJobFromHistory('py27')
4657 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4658
James E. Blair29c77002017-10-05 14:56:35 -07004659 def test_pending_merge_in_reconfig(self):
4660 # Test that if we are waiting for an outstanding merge on
4661 # reconfiguration that we continue to do so.
4662 self.gearman_server.hold_merge_jobs_in_queue = True
4663 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4664 A.setMerged()
4665 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4666 self.waitUntilSettled()
4667 # Reconfigure while we still have an outstanding merge job
4668 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4669 None)
4670 self.waitUntilSettled()
4671 # Verify the merge job is still running and that the item is
4672 # in the pipeline
4673 self.assertEqual(len(self.sched.merger.jobs), 1)
4674 tenant = self.sched.abide.tenants.get('tenant-one')
4675 pipeline = tenant.layout.pipelines['post']
4676 self.assertEqual(len(pipeline.getAllItems()), 1)
4677 self.gearman_server.hold_merge_jobs_in_queue = False
4678 self.gearman_server.release()
4679 self.waitUntilSettled()
4680
James E. Blairc32a8352017-10-11 16:27:50 -07004681 @simple_layout('layouts/parent-matchers.yaml')
4682 def test_parent_matchers(self):
4683 "Test that if a job's parent does not match, the job does not run"
4684 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4685 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4686 self.waitUntilSettled()
4687
4688 self.assertHistory([])
4689
4690 files = {'foo.txt': ''}
4691 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
4692 files=files)
4693 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4694 self.waitUntilSettled()
4695
4696 files = {'bar.txt': ''}
4697 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C',
4698 files=files)
4699 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4700 self.waitUntilSettled()
4701
4702 files = {'foo.txt': '', 'bar.txt': ''}
4703 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D',
4704 files=files)
4705 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4706 self.waitUntilSettled()
4707
4708 self.assertHistory([
4709 dict(name='child-job', result='SUCCESS', changes='2,1'),
4710 dict(name='child-job', result='SUCCESS', changes='3,1'),
4711 dict(name='child-job', result='SUCCESS', changes='4,1'),
4712 ])
4713
Arieb6f068c2016-10-09 13:11:06 +03004714
James E. Blaira002b032017-04-18 10:35:48 -07004715class TestExecutor(ZuulTestCase):
4716 tenant_config_file = 'config/single-tenant/main.yaml'
4717
4718 def assertFinalState(self):
4719 # In this test, we expect to shut down in a non-final state,
4720 # so skip these checks.
4721 pass
4722
4723 def assertCleanShutdown(self):
4724 self.log.debug("Assert clean shutdown")
4725
4726 # After shutdown, make sure no jobs are running
4727 self.assertEqual({}, self.executor_server.job_workers)
4728
4729 # Make sure that git.Repo objects have been garbage collected.
4730 repos = []
4731 gc.collect()
4732 for obj in gc.get_objects():
4733 if isinstance(obj, git.Repo):
4734 self.log.debug("Leaked git repo object: %s" % repr(obj))
4735 repos.append(obj)
4736 self.assertEqual(len(repos), 0)
4737
4738 def test_executor_shutdown(self):
4739 "Test that the executor can shut down with jobs running"
4740
4741 self.executor_server.hold_jobs_in_build = True
4742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004743 A.addApproval('Code-Review', 2)
4744 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004745 self.waitUntilSettled()
4746
4747
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004748class TestDependencyGraph(ZuulTestCase):
4749 tenant_config_file = 'config/dependency-graph/main.yaml'
4750
4751 def test_dependeny_graph_dispatch_jobs_once(self):
4752 "Test a job in a dependency graph is queued only once"
4753 # Job dependencies, starting with A
4754 # A
4755 # / \
4756 # B C
4757 # / \ / \
4758 # D F E
4759 # |
4760 # G
4761
4762 self.executor_server.hold_jobs_in_build = True
4763 change = self.fake_gerrit.addFakeChange(
4764 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004765 change.addApproval('Code-Review', 2)
4766 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004767
4768 self.waitUntilSettled()
4769 self.assertEqual([b.name for b in self.builds], ['A'])
4770
4771 self.executor_server.release('A')
4772 self.waitUntilSettled()
4773 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4774
4775 self.executor_server.release('B')
4776 self.waitUntilSettled()
4777 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4778
4779 self.executor_server.release('D')
4780 self.waitUntilSettled()
4781 self.assertEqual([b.name for b in self.builds], ['C'])
4782
4783 self.executor_server.release('C')
4784 self.waitUntilSettled()
4785 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4786
4787 self.executor_server.release('F')
4788 self.waitUntilSettled()
4789 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4790
4791 self.executor_server.release('G')
4792 self.waitUntilSettled()
4793 self.assertEqual([b.name for b in self.builds], ['E'])
4794
4795 self.executor_server.release('E')
4796 self.waitUntilSettled()
4797 self.assertEqual(len(self.builds), 0)
4798
4799 self.executor_server.hold_jobs_in_build = False
4800 self.executor_server.release()
4801 self.waitUntilSettled()
4802
4803 self.assertEqual(len(self.builds), 0)
4804 self.assertEqual(len(self.history), 7)
4805
4806 self.assertEqual(change.data['status'], 'MERGED')
4807 self.assertEqual(change.reported, 2)
4808
4809 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4810 "Test that a job waits till all dependencies are successful"
4811 # Job dependencies, starting with A
4812 # A
4813 # / \
4814 # B C*
4815 # / \ / \
4816 # D F E
4817 # |
4818 # G
4819
4820 self.executor_server.hold_jobs_in_build = True
4821 change = self.fake_gerrit.addFakeChange(
4822 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004823 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004824
4825 self.executor_server.failJob('C', change)
4826
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004827 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004828
4829 self.waitUntilSettled()
4830 self.assertEqual([b.name for b in self.builds], ['A'])
4831
4832 self.executor_server.release('A')
4833 self.waitUntilSettled()
4834 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4835
4836 self.executor_server.release('B')
4837 self.waitUntilSettled()
4838 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4839
4840 self.executor_server.release('D')
4841 self.waitUntilSettled()
4842 self.assertEqual([b.name for b in self.builds], ['C'])
4843
4844 self.executor_server.release('C')
4845 self.waitUntilSettled()
4846 self.assertEqual(len(self.builds), 0)
4847
4848 self.executor_server.hold_jobs_in_build = False
4849 self.executor_server.release()
4850 self.waitUntilSettled()
4851
4852 self.assertEqual(len(self.builds), 0)
4853 self.assertEqual(len(self.history), 4)
4854
4855 self.assertEqual(change.data['status'], 'NEW')
4856 self.assertEqual(change.reported, 2)
4857
4858
Arieb6f068c2016-10-09 13:11:06 +03004859class TestDuplicatePipeline(ZuulTestCase):
4860 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4861
4862 def test_duplicate_pipelines(self):
4863 "Test that a change matching multiple pipelines works"
4864
4865 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4866 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4867 self.waitUntilSettled()
4868
4869 self.assertHistory([
4870 dict(name='project-test1', result='SUCCESS', changes='1,1',
4871 pipeline='dup1'),
4872 dict(name='project-test1', result='SUCCESS', changes='1,1',
4873 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004874 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004875
4876 self.assertEqual(len(A.messages), 2)
4877
Paul Belangera46a3742016-11-09 14:23:03 -05004878 if 'dup1' in A.messages[0]:
4879 self.assertIn('dup1', A.messages[0])
4880 self.assertNotIn('dup2', A.messages[0])
4881 self.assertIn('project-test1', A.messages[0])
4882 self.assertIn('dup2', A.messages[1])
4883 self.assertNotIn('dup1', A.messages[1])
4884 self.assertIn('project-test1', A.messages[1])
4885 else:
4886 self.assertIn('dup1', A.messages[1])
4887 self.assertNotIn('dup2', A.messages[1])
4888 self.assertIn('project-test1', A.messages[1])
4889 self.assertIn('dup2', A.messages[0])
4890 self.assertNotIn('dup1', A.messages[0])
4891 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004892
4893
Paul Belanger86085b32016-11-03 12:48:57 -04004894class TestSchedulerTemplatedProject(ZuulTestCase):
4895 tenant_config_file = 'config/templated-project/main.yaml'
4896
Paul Belanger174a8272017-03-14 13:20:10 -04004897 def test_job_from_templates_executed(self):
4898 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004899
4900 A = self.fake_gerrit.addFakeChange(
4901 'org/templated-project', 'master', 'A')
4902 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4903 self.waitUntilSettled()
4904
4905 self.assertEqual(self.getJobFromHistory('project-test1').result,
4906 'SUCCESS')
4907 self.assertEqual(self.getJobFromHistory('project-test2').result,
4908 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004909
4910 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004911 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004912
4913 A = self.fake_gerrit.addFakeChange(
4914 'org/layered-project', 'master', 'A')
4915 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4916 self.waitUntilSettled()
4917
4918 self.assertEqual(self.getJobFromHistory('project-test1').result,
4919 'SUCCESS')
4920 self.assertEqual(self.getJobFromHistory('project-test2').result,
4921 'SUCCESS')
4922 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4923 ).result, 'SUCCESS')
4924 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4925 ).result, 'SUCCESS')
4926 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4927 ).result, 'SUCCESS')
4928 self.assertEqual(self.getJobFromHistory('project-test6').result,
4929 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004930
James E. Blaire74f5712017-09-29 15:14:31 -07004931 def test_unimplied_branch_matchers(self):
4932 # This tests that there are no implied branch matchers added
4933 # by project templates.
4934 self.create_branch('org/layered-project', 'stable')
4935
4936 A = self.fake_gerrit.addFakeChange(
4937 'org/layered-project', 'stable', 'A')
4938
4939 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4940 self.waitUntilSettled()
4941
4942 self.assertEqual(self.getJobFromHistory('project-test1').result,
4943 'SUCCESS')
4944 print(self.getJobFromHistory('project-test1').
4945 parameters['zuul']['_inheritance_path'])
4946
4947 def test_implied_branch_matchers(self):
4948 # This tests that there is an implied branch matcher when a
4949 # template is used on an in-repo project pipeline definition.
4950 self.create_branch('untrusted-config', 'stable')
4951 self.fake_gerrit.addEvent(
4952 self.fake_gerrit.getFakeBranchCreatedEvent(
4953 'untrusted-config', 'stable'))
4954 self.waitUntilSettled()
4955
4956 A = self.fake_gerrit.addFakeChange(
4957 'untrusted-config', 'stable', 'A')
4958
4959 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4960 self.waitUntilSettled()
4961
4962 self.assertEqual(self.getJobFromHistory('project-test1').result,
4963 'SUCCESS')
4964 print(self.getJobFromHistory('project-test1').
4965 parameters['zuul']['_inheritance_path'])
4966
Adam Gandelman94a60062016-11-21 16:43:14 -08004967
4968class TestSchedulerSuccessURL(ZuulTestCase):
4969 tenant_config_file = 'config/success-url/main.yaml'
4970
4971 def test_success_url(self):
4972 "Ensure bad build params are ignored"
4973 self.sched.reconfigure(self.config)
4974 self.init_repo('org/docs')
4975
4976 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4977 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4978 self.waitUntilSettled()
4979
4980 # Both builds ran: docs-draft-test + docs-draft-test2
4981 self.assertEqual(len(self.history), 2)
4982
4983 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004984 for build in self.history:
4985 if build.name == 'docs-draft-test':
4986 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004987 elif build.name == 'docs-draft-test2':
4988 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004989
4990 # Two msgs: 'Starting...' + results
4991 self.assertEqual(len(self.smtp_messages), 2)
4992 body = self.smtp_messages[1]['body'].splitlines()
4993 self.assertEqual('Build succeeded.', body[0])
4994
4995 self.assertIn(
4996 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4997 'docs-draft-test/%s/publish-docs/' % uuid,
4998 body[2])
4999
Paul Belanger174a8272017-03-14 13:20:10 -04005000 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08005001 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05005002 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
5003 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06005004 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08005005 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08005006
5007
Adam Gandelman4da00f62016-12-09 15:47:33 -08005008class TestSchedulerMerges(ZuulTestCase):
5009 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08005010
5011 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04005012 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08005013 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04005014 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08005015 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
5016 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
5017 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005018 A.addApproval('Code-Review', 2)
5019 B.addApproval('Code-Review', 2)
5020 C.addApproval('Code-Review', 2)
5021 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5022 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5023 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08005024 self.waitUntilSettled()
5025
5026 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07005027 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5028 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08005029 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005030 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08005031 repo_messages.reverse()
5032
Paul Belanger174a8272017-03-14 13:20:10 -04005033 self.executor_server.hold_jobs_in_build = False
5034 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08005035 self.waitUntilSettled()
5036
5037 return repo_messages
5038
5039 def _test_merge(self, mode):
James E. Blairda5bb7e2018-01-22 16:12:17 -08005040 us_path = 'file://' + os.path.join(
Adam Gandelman8bd57102016-12-02 12:58:42 -08005041 self.upstream_root, 'org/project-%s' % mode)
5042 expected_messages = [
5043 'initial commit',
5044 'add content from fixture',
5045 # the intermediate commits order is nondeterministic
5046 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
5047 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
5048 ]
5049 result = self._test_project_merge_mode(mode)
5050 self.assertEqual(result[:2], expected_messages[:2])
5051 self.assertEqual(result[-2:], expected_messages[-2:])
5052
5053 def test_project_merge_mode_merge(self):
5054 self._test_merge('merge')
5055
5056 def test_project_merge_mode_merge_resolve(self):
5057 self._test_merge('merge-resolve')
5058
5059 def test_project_merge_mode_cherrypick(self):
5060 expected_messages = [
5061 'initial commit',
5062 'add content from fixture',
5063 'A-1',
5064 'B-1',
5065 'C-1']
5066 result = self._test_project_merge_mode('cherry-pick')
5067 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005068
5069 def test_merge_branch(self):
5070 "Test that the right commits are on alternate branches"
5071 self.create_branch('org/project-merge-branches', 'mp')
5072
Paul Belanger174a8272017-03-14 13:20:10 -04005073 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005074 A = self.fake_gerrit.addFakeChange(
5075 'org/project-merge-branches', 'mp', 'A')
5076 B = self.fake_gerrit.addFakeChange(
5077 'org/project-merge-branches', 'mp', 'B')
5078 C = self.fake_gerrit.addFakeChange(
5079 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005080 A.addApproval('Code-Review', 2)
5081 B.addApproval('Code-Review', 2)
5082 C.addApproval('Code-Review', 2)
5083 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5084 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5085 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005086 self.waitUntilSettled()
5087
Paul Belanger174a8272017-03-14 13:20:10 -04005088 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005089 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005090 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005091 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005092 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005093 self.waitUntilSettled()
5094
5095 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005096 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005097 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5098 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005099 repo = git.Repo(path)
5100
James E. Blair247cab72017-07-20 16:52:36 -07005101 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005102 repo_messages.reverse()
5103 correct_messages = [
5104 'initial commit',
5105 'add content from fixture',
5106 'mp commit',
5107 'A-1', 'B-1', 'C-1']
5108 self.assertEqual(repo_messages, correct_messages)
5109
Paul Belanger174a8272017-03-14 13:20:10 -04005110 self.executor_server.hold_jobs_in_build = False
5111 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005112 self.waitUntilSettled()
5113
5114 def test_merge_multi_branch(self):
5115 "Test that dependent changes on multiple branches are merged"
5116 self.create_branch('org/project-merge-branches', 'mp')
5117
Paul Belanger174a8272017-03-14 13:20:10 -04005118 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005119 A = self.fake_gerrit.addFakeChange(
5120 'org/project-merge-branches', 'master', 'A')
5121 B = self.fake_gerrit.addFakeChange(
5122 'org/project-merge-branches', 'mp', 'B')
5123 C = self.fake_gerrit.addFakeChange(
5124 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005125 A.addApproval('Code-Review', 2)
5126 B.addApproval('Code-Review', 2)
5127 C.addApproval('Code-Review', 2)
5128 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5129 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5130 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005131 self.waitUntilSettled()
5132
5133 job_A = None
5134 for job in self.builds:
5135 if 'project-merge' in job.name:
5136 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005137
James E. Blair2a535672017-04-27 12:03:15 -07005138 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5139 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005140 repo = git.Repo(path)
5141 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005142 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005143 repo_messages.reverse()
5144 correct_messages = [
5145 'initial commit', 'add content from fixture', 'A-1']
5146 self.assertEqual(repo_messages, correct_messages)
5147
Paul Belanger174a8272017-03-14 13:20:10 -04005148 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005149 self.waitUntilSettled()
5150
5151 job_B = None
5152 for job in self.builds:
5153 if 'project-merge' in job.name:
5154 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005155
James E. Blair2a535672017-04-27 12:03:15 -07005156 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5157 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005158 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005159 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005160 repo_messages.reverse()
5161 correct_messages = [
5162 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5163 self.assertEqual(repo_messages, correct_messages)
5164
Paul Belanger174a8272017-03-14 13:20:10 -04005165 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005166 self.waitUntilSettled()
5167
5168 job_C = None
5169 for job in self.builds:
5170 if 'project-merge' in job.name:
5171 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005172
James E. Blair2a535672017-04-27 12:03:15 -07005173 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5174 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005175 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005176 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005177
5178 repo_messages.reverse()
5179 correct_messages = [
5180 'initial commit', 'add content from fixture',
5181 'A-1', 'C-1']
5182 # Ensure the right commits are in the history for this ref
5183 self.assertEqual(repo_messages, correct_messages)
5184
Paul Belanger174a8272017-03-14 13:20:10 -04005185 self.executor_server.hold_jobs_in_build = False
5186 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005187 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005188
5189
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005190class TestSemaphore(ZuulTestCase):
5191 tenant_config_file = 'config/semaphore/main.yaml'
5192
5193 def test_semaphore_one(self):
5194 "Test semaphores with max=1 (mutex)"
5195 tenant = self.sched.abide.tenants.get('tenant-one')
5196
5197 self.executor_server.hold_jobs_in_build = True
5198
5199 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5200 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5201 self.assertFalse('test-semaphore' in
5202 tenant.semaphore_handler.semaphores)
5203
5204 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5205 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5206 self.waitUntilSettled()
5207
5208 self.assertEqual(len(self.builds), 3)
5209 self.assertEqual(self.builds[0].name, 'project-test1')
5210 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5211 self.assertEqual(self.builds[2].name, 'project-test1')
5212
5213 self.executor_server.release('semaphore-one-test1')
5214 self.waitUntilSettled()
5215
5216 self.assertEqual(len(self.builds), 3)
5217 self.assertEqual(self.builds[0].name, 'project-test1')
5218 self.assertEqual(self.builds[1].name, 'project-test1')
5219 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5220 self.assertTrue('test-semaphore' in
5221 tenant.semaphore_handler.semaphores)
5222
5223 self.executor_server.release('semaphore-one-test2')
5224 self.waitUntilSettled()
5225
5226 self.assertEqual(len(self.builds), 3)
5227 self.assertEqual(self.builds[0].name, 'project-test1')
5228 self.assertEqual(self.builds[1].name, 'project-test1')
5229 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5230 self.assertTrue('test-semaphore' in
5231 tenant.semaphore_handler.semaphores)
5232
5233 self.executor_server.release('semaphore-one-test1')
5234 self.waitUntilSettled()
5235
5236 self.assertEqual(len(self.builds), 3)
5237 self.assertEqual(self.builds[0].name, 'project-test1')
5238 self.assertEqual(self.builds[1].name, 'project-test1')
5239 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5240 self.assertTrue('test-semaphore' in
5241 tenant.semaphore_handler.semaphores)
5242
5243 self.executor_server.release('semaphore-one-test2')
5244 self.waitUntilSettled()
5245
5246 self.assertEqual(len(self.builds), 2)
5247 self.assertEqual(self.builds[0].name, 'project-test1')
5248 self.assertEqual(self.builds[1].name, 'project-test1')
5249 self.assertFalse('test-semaphore' in
5250 tenant.semaphore_handler.semaphores)
5251
5252 self.executor_server.hold_jobs_in_build = False
5253 self.executor_server.release()
5254
5255 self.waitUntilSettled()
5256 self.assertEqual(len(self.builds), 0)
5257
5258 self.assertEqual(A.reported, 1)
5259 self.assertEqual(B.reported, 1)
5260 self.assertFalse('test-semaphore' in
5261 tenant.semaphore_handler.semaphores)
5262
5263 def test_semaphore_two(self):
5264 "Test semaphores with max>1"
5265 tenant = self.sched.abide.tenants.get('tenant-one')
5266
5267 self.executor_server.hold_jobs_in_build = True
5268 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5269 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5270 self.assertFalse('test-semaphore-two' in
5271 tenant.semaphore_handler.semaphores)
5272
5273 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5274 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5275 self.waitUntilSettled()
5276
5277 self.assertEqual(len(self.builds), 4)
5278 self.assertEqual(self.builds[0].name, 'project-test1')
5279 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5280 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5281 self.assertEqual(self.builds[3].name, 'project-test1')
5282 self.assertTrue('test-semaphore-two' in
5283 tenant.semaphore_handler.semaphores)
5284 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5285 'test-semaphore-two', [])), 2)
5286
5287 self.executor_server.release('semaphore-two-test1')
5288 self.waitUntilSettled()
5289
5290 self.assertEqual(len(self.builds), 4)
5291 self.assertEqual(self.builds[0].name, 'project-test1')
5292 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5293 self.assertEqual(self.builds[2].name, 'project-test1')
5294 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5295 self.assertTrue('test-semaphore-two' in
5296 tenant.semaphore_handler.semaphores)
5297 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5298 'test-semaphore-two', [])), 2)
5299
5300 self.executor_server.release('semaphore-two-test2')
5301 self.waitUntilSettled()
5302
5303 self.assertEqual(len(self.builds), 4)
5304 self.assertEqual(self.builds[0].name, 'project-test1')
5305 self.assertEqual(self.builds[1].name, 'project-test1')
5306 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5307 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5308 self.assertTrue('test-semaphore-two' in
5309 tenant.semaphore_handler.semaphores)
5310 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5311 'test-semaphore-two', [])), 2)
5312
5313 self.executor_server.release('semaphore-two-test1')
5314 self.waitUntilSettled()
5315
5316 self.assertEqual(len(self.builds), 3)
5317 self.assertEqual(self.builds[0].name, 'project-test1')
5318 self.assertEqual(self.builds[1].name, 'project-test1')
5319 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5320 self.assertTrue('test-semaphore-two' in
5321 tenant.semaphore_handler.semaphores)
5322 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5323 'test-semaphore-two', [])), 1)
5324
5325 self.executor_server.release('semaphore-two-test2')
5326 self.waitUntilSettled()
5327
5328 self.assertEqual(len(self.builds), 2)
5329 self.assertEqual(self.builds[0].name, 'project-test1')
5330 self.assertEqual(self.builds[1].name, 'project-test1')
5331 self.assertFalse('test-semaphore-two' in
5332 tenant.semaphore_handler.semaphores)
5333
5334 self.executor_server.hold_jobs_in_build = False
5335 self.executor_server.release()
5336
5337 self.waitUntilSettled()
5338 self.assertEqual(len(self.builds), 0)
5339
5340 self.assertEqual(A.reported, 1)
5341 self.assertEqual(B.reported, 1)
5342
5343 def test_semaphore_abandon(self):
5344 "Test abandon with job semaphores"
5345 self.executor_server.hold_jobs_in_build = True
5346 tenant = self.sched.abide.tenants.get('tenant-one')
5347 check_pipeline = tenant.layout.pipelines['check']
5348
5349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5350 self.assertFalse('test-semaphore' in
5351 tenant.semaphore_handler.semaphores)
5352
5353 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5354 self.waitUntilSettled()
5355
5356 self.assertTrue('test-semaphore' in
5357 tenant.semaphore_handler.semaphores)
5358
5359 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5360 self.waitUntilSettled()
5361
5362 # The check pipeline should be empty
5363 items = check_pipeline.getAllItems()
5364 self.assertEqual(len(items), 0)
5365
5366 # The semaphore should be released
5367 self.assertFalse('test-semaphore' in
5368 tenant.semaphore_handler.semaphores)
5369
5370 self.executor_server.hold_jobs_in_build = False
5371 self.executor_server.release()
5372 self.waitUntilSettled()
5373
Tobias Henkel0f714002017-06-30 23:30:52 +02005374 def test_semaphore_new_patchset(self):
5375 "Test new patchset with job semaphores"
5376 self.executor_server.hold_jobs_in_build = True
5377 tenant = self.sched.abide.tenants.get('tenant-one')
5378 check_pipeline = tenant.layout.pipelines['check']
5379
5380 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5381 self.assertFalse('test-semaphore' in
5382 tenant.semaphore_handler.semaphores)
5383
5384 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5385 self.waitUntilSettled()
5386
5387 self.assertTrue('test-semaphore' in
5388 tenant.semaphore_handler.semaphores)
5389 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5390 self.assertEqual(len(semaphore), 1)
5391
5392 A.addPatchset()
5393 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5394 self.waitUntilSettled()
5395
5396 self.assertTrue('test-semaphore' in
5397 tenant.semaphore_handler.semaphores)
5398 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5399 self.assertEqual(len(semaphore), 1)
5400
5401 items = check_pipeline.getAllItems()
5402 self.assertEqual(items[0].change.number, '1')
5403 self.assertEqual(items[0].change.patchset, '2')
5404 self.assertTrue(items[0].live)
5405
5406 self.executor_server.hold_jobs_in_build = False
5407 self.executor_server.release()
5408 self.waitUntilSettled()
5409
5410 # The semaphore should be released
5411 self.assertFalse('test-semaphore' in
5412 tenant.semaphore_handler.semaphores)
5413
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005414 def test_semaphore_reconfigure(self):
5415 "Test reconfigure with job semaphores"
5416 self.executor_server.hold_jobs_in_build = True
5417 tenant = self.sched.abide.tenants.get('tenant-one')
5418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5419 self.assertFalse('test-semaphore' in
5420 tenant.semaphore_handler.semaphores)
5421
5422 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5423 self.waitUntilSettled()
5424
5425 self.assertTrue('test-semaphore' in
5426 tenant.semaphore_handler.semaphores)
5427
5428 # reconfigure without layout change
5429 self.sched.reconfigure(self.config)
5430 self.waitUntilSettled()
5431 tenant = self.sched.abide.tenants.get('tenant-one')
5432
5433 # semaphore still must be held
5434 self.assertTrue('test-semaphore' in
5435 tenant.semaphore_handler.semaphores)
5436
5437 self.commitConfigUpdate(
5438 'common-config',
5439 'config/semaphore/zuul-reconfiguration.yaml')
5440 self.sched.reconfigure(self.config)
5441 self.waitUntilSettled()
5442 tenant = self.sched.abide.tenants.get('tenant-one')
5443
5444 self.executor_server.release('project-test1')
5445 self.waitUntilSettled()
5446
5447 # There should be no builds anymore
5448 self.assertEqual(len(self.builds), 0)
5449
5450 # The semaphore should be released
5451 self.assertFalse('test-semaphore' in
5452 tenant.semaphore_handler.semaphores)
5453
5454
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005455class TestSemaphoreMultiTenant(ZuulTestCase):
5456 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5457
5458 def test_semaphore_tenant_isolation(self):
5459 "Test semaphores in multiple tenants"
5460
5461 self.waitUntilSettled()
5462 tenant_one = self.sched.abide.tenants.get('tenant-one')
5463 tenant_two = self.sched.abide.tenants.get('tenant-two')
5464
5465 self.executor_server.hold_jobs_in_build = True
5466 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5467 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5468 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5469 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5470 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5471 self.assertFalse('test-semaphore' in
5472 tenant_one.semaphore_handler.semaphores)
5473 self.assertFalse('test-semaphore' in
5474 tenant_two.semaphore_handler.semaphores)
5475
5476 # add patches to project1 of tenant-one
5477 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5478 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5479 self.waitUntilSettled()
5480
5481 # one build of project1-test1 must run
5482 # semaphore of tenant-one must be acquired once
5483 # semaphore of tenant-two must not be acquired
5484 self.assertEqual(len(self.builds), 1)
5485 self.assertEqual(self.builds[0].name, 'project1-test1')
5486 self.assertTrue('test-semaphore' in
5487 tenant_one.semaphore_handler.semaphores)
5488 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5489 'test-semaphore', [])), 1)
5490 self.assertFalse('test-semaphore' in
5491 tenant_two.semaphore_handler.semaphores)
5492
5493 # add patches to project2 of tenant-two
5494 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5495 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5496 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5497 self.waitUntilSettled()
5498
5499 # one build of project1-test1 must run
5500 # two builds of project2-test1 must run
5501 # semaphore of tenant-one must be acquired once
5502 # semaphore of tenant-two must be acquired twice
5503 self.assertEqual(len(self.builds), 3)
5504 self.assertEqual(self.builds[0].name, 'project1-test1')
5505 self.assertEqual(self.builds[1].name, 'project2-test1')
5506 self.assertEqual(self.builds[2].name, 'project2-test1')
5507 self.assertTrue('test-semaphore' in
5508 tenant_one.semaphore_handler.semaphores)
5509 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5510 'test-semaphore', [])), 1)
5511 self.assertTrue('test-semaphore' in
5512 tenant_two.semaphore_handler.semaphores)
5513 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5514 'test-semaphore', [])), 2)
5515
5516 self.executor_server.release('project1-test1')
5517 self.waitUntilSettled()
5518
5519 # one build of project1-test1 must run
5520 # two builds of project2-test1 must run
5521 # semaphore of tenant-one must be acquired once
5522 # semaphore of tenant-two must be acquired twice
5523 self.assertEqual(len(self.builds), 3)
5524 self.assertEqual(self.builds[0].name, 'project2-test1')
5525 self.assertEqual(self.builds[1].name, 'project2-test1')
5526 self.assertEqual(self.builds[2].name, 'project1-test1')
5527 self.assertTrue('test-semaphore' in
5528 tenant_one.semaphore_handler.semaphores)
5529 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5530 'test-semaphore', [])), 1)
5531 self.assertTrue('test-semaphore' in
5532 tenant_two.semaphore_handler.semaphores)
5533 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5534 'test-semaphore', [])), 2)
5535
5536 self.executor_server.release('project2-test1')
5537 self.waitUntilSettled()
5538
5539 # one build of project1-test1 must run
5540 # one build of project2-test1 must run
5541 # semaphore of tenant-one must be acquired once
5542 # semaphore of tenant-two must be acquired once
5543 self.assertEqual(len(self.builds), 2)
5544 self.assertTrue('test-semaphore' in
5545 tenant_one.semaphore_handler.semaphores)
5546 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5547 'test-semaphore', [])), 1)
5548 self.assertTrue('test-semaphore' in
5549 tenant_two.semaphore_handler.semaphores)
5550 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5551 'test-semaphore', [])), 1)
5552
5553 self.executor_server.hold_jobs_in_build = False
5554 self.executor_server.release()
5555
5556 self.waitUntilSettled()
5557
5558 # no build must run
5559 # semaphore of tenant-one must not be acquired
5560 # semaphore of tenant-two must not be acquired
5561 self.assertEqual(len(self.builds), 0)
5562 self.assertFalse('test-semaphore' in
5563 tenant_one.semaphore_handler.semaphores)
5564 self.assertFalse('test-semaphore' in
5565 tenant_two.semaphore_handler.semaphores)
5566
5567 self.assertEqual(A.reported, 1)
5568 self.assertEqual(B.reported, 1)
5569
5570
Tobias Henkele9a22512017-12-22 08:36:12 +01005571class TestImplicitProject(ZuulTestCase):
5572 tenant_config_file = 'config/implicit-project/main.yaml'
5573
5574 def test_implicit_project(self):
5575 # config project should work with implicit project name
5576 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
5577 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5578
5579 # untrusted project should work with implicit project name
5580 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5581 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5582
5583 self.waitUntilSettled()
5584
5585 self.assertEqual(A.data['status'], 'NEW')
5586 self.assertEqual(A.reported, 1)
5587 self.assertEqual(B.data['status'], 'NEW')
5588 self.assertEqual(B.reported, 1)
5589 self.assertHistory([
5590 dict(name='test-common', result='SUCCESS', changes='1,1'),
5591 dict(name='test-common', result='SUCCESS', changes='2,1'),
5592 dict(name='test-project', result='SUCCESS', changes='2,1'),
5593 ], ordered=False)
5594
5595 # now test adding a further project in repo
5596 in_repo_conf = textwrap.dedent(
5597 """
5598 - job:
5599 name: test-project
5600 run: playbooks/test-project.yaml
5601 - job:
5602 name: test2-project
5603 run: playbooks/test-project.yaml
5604
5605 - project:
5606 check:
5607 jobs:
5608 - test-project
5609 gate:
5610 jobs:
5611 - test-project
5612
5613 - project:
5614 check:
5615 jobs:
5616 - test2-project
5617 gate:
5618 jobs:
5619 - test2-project
5620
5621 """)
5622 file_dict = {'.zuul.yaml': in_repo_conf}
5623 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5624 files=file_dict)
5625 C.addApproval('Code-Review', 2)
5626 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
5627 self.waitUntilSettled()
5628
5629 # change C must be merged
5630 self.assertEqual(C.data['status'], 'MERGED')
5631 self.assertEqual(C.reported, 2)
5632 self.assertHistory([
5633 dict(name='test-common', result='SUCCESS', changes='1,1'),
5634 dict(name='test-common', result='SUCCESS', changes='2,1'),
5635 dict(name='test-project', result='SUCCESS', changes='2,1'),
5636 dict(name='test-common', result='SUCCESS', changes='3,1'),
5637 dict(name='test-project', result='SUCCESS', changes='3,1'),
5638 dict(name='test2-project', result='SUCCESS', changes='3,1'),
5639 ], ordered=False)
5640
5641
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005642class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005643 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005644 tenant_config_file = 'config/in-repo/main.yaml'
5645
5646 def test_semaphore_in_repo(self):
5647 "Test semaphores in repo config"
5648
5649 # This tests dynamic semaphore handling in project repos. The semaphore
5650 # max value should not be evaluated dynamically but must be updated
5651 # after the change lands.
5652
5653 self.waitUntilSettled()
5654 tenant = self.sched.abide.tenants.get('tenant-one')
5655
5656 in_repo_conf = textwrap.dedent(
5657 """
5658 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005659 name: project-test1
5660
5661 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005662 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -07005663 run: playbooks/project-test2.yaml
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005664 semaphore: test-semaphore
5665
5666 - project:
5667 name: org/project
5668 tenant-one-gate:
5669 jobs:
5670 - project-test2
5671
5672 # the max value in dynamic layout must be ignored
5673 - semaphore:
5674 name: test-semaphore
5675 max: 2
5676 """)
5677
5678 in_repo_playbook = textwrap.dedent(
5679 """
5680 - hosts: all
5681 tasks: []
5682 """)
5683
5684 file_dict = {'.zuul.yaml': in_repo_conf,
5685 'playbooks/project-test2.yaml': in_repo_playbook}
5686 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5687 files=file_dict)
5688 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5689 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5690 B.setDependsOn(A, 1)
5691 C.setDependsOn(A, 1)
5692
5693 self.executor_server.hold_jobs_in_build = True
5694
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005695 A.addApproval('Code-Review', 2)
5696 B.addApproval('Code-Review', 2)
5697 C.addApproval('Code-Review', 2)
5698 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5699 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5700 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005701 self.waitUntilSettled()
5702
5703 # check that the layout in a queue item still has max value of 1
5704 # for test-semaphore
5705 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5706 queue = None
5707 for queue_candidate in pipeline.queues:
5708 if queue_candidate.name == 'org/project':
5709 queue = queue_candidate
5710 break
5711 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005712 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005713 dynamic_test_semaphore = \
5714 item_dynamic_layout.semaphores.get('test-semaphore')
5715 self.assertEqual(dynamic_test_semaphore.max, 1)
5716
5717 # one build must be in queue, one semaphores acquired
5718 self.assertEqual(len(self.builds), 1)
5719 self.assertEqual(self.builds[0].name, 'project-test2')
5720 self.assertTrue('test-semaphore' in
5721 tenant.semaphore_handler.semaphores)
5722 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5723 'test-semaphore', [])), 1)
5724
5725 self.executor_server.release('project-test2')
5726 self.waitUntilSettled()
5727
5728 # change A must be merged
5729 self.assertEqual(A.data['status'], 'MERGED')
5730 self.assertEqual(A.reported, 2)
5731
5732 # send change-merged event as the gerrit mock doesn't send it
5733 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5734 self.waitUntilSettled()
5735
5736 # now that change A was merged, the new semaphore max must be effective
5737 tenant = self.sched.abide.tenants.get('tenant-one')
5738 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5739
5740 # two builds must be in queue, two semaphores acquired
5741 self.assertEqual(len(self.builds), 2)
5742 self.assertEqual(self.builds[0].name, 'project-test2')
5743 self.assertEqual(self.builds[1].name, 'project-test2')
5744 self.assertTrue('test-semaphore' in
5745 tenant.semaphore_handler.semaphores)
5746 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5747 'test-semaphore', [])), 2)
5748
5749 self.executor_server.release('project-test2')
5750 self.waitUntilSettled()
5751
5752 self.assertEqual(len(self.builds), 0)
5753 self.assertFalse('test-semaphore' in
5754 tenant.semaphore_handler.semaphores)
5755
5756 self.executor_server.hold_jobs_in_build = False
5757 self.executor_server.release()
5758
5759 self.waitUntilSettled()
5760 self.assertEqual(len(self.builds), 0)
5761
5762 self.assertEqual(A.reported, 2)
5763 self.assertEqual(B.reported, 2)
5764 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005765
5766
5767class TestSchedulerBranchMatcher(ZuulTestCase):
5768
5769 @simple_layout('layouts/matcher-test.yaml')
5770 def test_job_branch_ignored(self):
5771 '''
5772 Test that branch matching logic works.
5773
5774 The 'ignore-branch' job has a branch matcher that is supposed to
5775 match every branch except for the 'featureA' branch, so it should
5776 not be run on a change to that branch.
5777 '''
5778 self.create_branch('org/project', 'featureA')
5779 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5780 A.addApproval('Code-Review', 2)
5781 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5782 self.waitUntilSettled()
5783 self.printHistory()
5784 self.assertEqual(self.getJobFromHistory('project-test1').result,
5785 'SUCCESS')
5786 self.assertJobNotInHistory('ignore-branch')
5787 self.assertEqual(A.data['status'], 'MERGED')
5788 self.assertEqual(A.reported, 2,
5789 "A should report start and success")
5790 self.assertIn('gate', A.messages[1],
5791 "A should transit gate")