blob: 9b5408437d4567d29b141c10fd64f33a7f2bfdab [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',
1509 "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',
1556 'project-test2'])
1557 )
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
David Shrewsburyc4676132017-10-16 12:41:57 -04001576 @simple_layout('layouts/autohold.yaml')
Clark Boylanaeb5a122017-10-20 14:00:11 -07001577 def test_autohold_ignores_aborted_jobs(self):
1578 client = zuul.rpcclient.RPCClient('127.0.0.1',
1579 self.gearman_server.port)
1580 self.addCleanup(client.shutdown)
1581 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1582 "reason text", 1)
1583 self.assertTrue(r)
1584
1585 self.executor_server.hold_jobs_in_build = True
1586
1587 # Create a change that will have its job aborted
1588 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1589 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1590 self.waitUntilSettled()
1591
1592 # Creating new patchset on change A will abort A,1's job because
1593 # a new patchset arrived replacing A,1 with A,2.
1594 A.addPatchset()
1595 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1596
1597 self.waitUntilSettled()
1598 self.executor_server.hold_jobs_in_build = False
1599 self.executor_server.release()
1600 self.waitUntilSettled()
1601
1602 self.assertEqual(A.data['status'], 'NEW')
1603 # Note only the successful job for A,2 will report as we don't
1604 # report aborted builds for old patchsets.
1605 self.assertEqual(A.reported, 1)
1606 # A,1 project-test2
1607 self.assertEqual(self.history[0].result, 'ABORTED')
1608 # A,2 project-test2
1609 self.assertEqual(self.history[1].result, 'SUCCESS')
1610
1611 # Check nodepool for a held node
1612 held_node = None
1613 for node in self.fake_nodepool.getNodes():
1614 if node['state'] == zuul.model.STATE_HOLD:
1615 held_node = node
1616 break
1617 self.assertIsNone(held_node)
1618
1619 @simple_layout('layouts/autohold.yaml')
David Shrewsburyc4676132017-10-16 12:41:57 -04001620 def test_autohold_list(self):
1621 client = zuul.rpcclient.RPCClient('127.0.0.1',
1622 self.gearman_server.port)
1623 self.addCleanup(client.shutdown)
1624
1625 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1626 "reason text", 1)
1627 self.assertTrue(r)
1628
1629 autohold_requests = client.autohold_list()
1630 self.assertNotEqual({}, autohold_requests)
1631 self.assertEqual(1, len(autohold_requests.keys()))
1632
1633 # The single dict key should be a CSV string value
1634 key = list(autohold_requests.keys())[0]
1635 tenant, project, job = key.split(',')
1636
1637 self.assertEqual('tenant-one', tenant)
1638 self.assertIn('org/project', project)
1639 self.assertEqual('project-test2', job)
1640
1641 # Note: the value is converted from set to list by json.
1642 self.assertEqual([1, "reason text"], autohold_requests[key])
1643
James E. Blair7cb84542017-04-19 13:35:05 -07001644 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001645 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001646 # This particular test does a large amount of merges and needs a little
1647 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001648 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001649 "test that dependent changes behind dequeued changes work"
1650 # This complicated test is a reproduction of a real life bug
1651 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001652
Paul Belanger174a8272017-03-14 13:20:10 -04001653 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001654 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1655 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1656 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1657 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1658 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1659 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1660 D.setDependsOn(C, 1)
1661 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001662 A.addApproval('Code-Review', 2)
1663 B.addApproval('Code-Review', 2)
1664 C.addApproval('Code-Review', 2)
1665 D.addApproval('Code-Review', 2)
1666 E.addApproval('Code-Review', 2)
1667 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001668
1669 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001670
1671 # Change object re-use in the gerrit trigger is hidden if
1672 # changes are added in quick succession; waiting makes it more
1673 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001674 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001675 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001676 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001677 self.waitUntilSettled()
1678
Paul Belanger174a8272017-03-14 13:20:10 -04001679 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001680 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001681 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001682 self.waitUntilSettled()
1683
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001684 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001685 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001686 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001687 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001688 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001689 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001690 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001691 self.waitUntilSettled()
1692
Paul Belanger174a8272017-03-14 13:20:10 -04001693 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001694 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001695 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001696 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001697 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001698 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001699 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001700 self.waitUntilSettled()
1701
1702 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001703
1704 # Grab pointers to the jobs we want to release before
1705 # releasing any, because list indexes may change as
1706 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001707 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001708 a.release()
1709 b.release()
1710 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001711 self.waitUntilSettled()
1712
Paul Belanger174a8272017-03-14 13:20:10 -04001713 self.executor_server.hold_jobs_in_build = False
1714 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001715 self.waitUntilSettled()
1716
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001717 self.assertEqual(A.data['status'], 'NEW')
1718 self.assertEqual(B.data['status'], 'MERGED')
1719 self.assertEqual(C.data['status'], 'MERGED')
1720 self.assertEqual(D.data['status'], 'MERGED')
1721 self.assertEqual(E.data['status'], 'MERGED')
1722 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001723
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001724 self.assertEqual(A.reported, 2)
1725 self.assertEqual(B.reported, 2)
1726 self.assertEqual(C.reported, 2)
1727 self.assertEqual(D.reported, 2)
1728 self.assertEqual(E.reported, 2)
1729 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001730
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001731 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1732 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001733
1734 def test_merger_repack(self):
1735 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001736
James E. Blair05fed602012-09-07 12:45:24 -07001737 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001738 A.addApproval('Code-Review', 2)
1739 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001740 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001741 self.assertEqual(self.getJobFromHistory('project-merge').result,
1742 'SUCCESS')
1743 self.assertEqual(self.getJobFromHistory('project-test1').result,
1744 'SUCCESS')
1745 self.assertEqual(self.getJobFromHistory('project-test2').result,
1746 'SUCCESS')
1747 self.assertEqual(A.data['status'], 'MERGED')
1748 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001749 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001750 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001751
James E. Blair2a535672017-04-27 12:03:15 -07001752 path = os.path.join(self.merger_src_root, "review.example.com",
1753 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001754 if os.path.exists(path):
1755 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001756 path = os.path.join(self.executor_src_root, "review.example.com",
1757 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001758 if os.path.exists(path):
1759 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001760
1761 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001762 A.addApproval('Code-Review', 2)
1763 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001764 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001765 self.assertEqual(self.getJobFromHistory('project-merge').result,
1766 'SUCCESS')
1767 self.assertEqual(self.getJobFromHistory('project-test1').result,
1768 'SUCCESS')
1769 self.assertEqual(self.getJobFromHistory('project-test2').result,
1770 'SUCCESS')
1771 self.assertEqual(A.data['status'], 'MERGED')
1772 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001773
James E. Blair4886f282012-11-15 09:27:33 -08001774 def test_merger_repack_large_change(self):
1775 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001776 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001777 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001778 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001779 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001780 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001781 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001782 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001783 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001784 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001785 # TODOv3(jeblair): add hostname to upstream root
1786 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001787 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001788 path = os.path.join(self.merger_src_root, 'review.example.com',
1789 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001790 if os.path.exists(path):
1791 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001792 path = os.path.join(self.executor_src_root, 'review.example.com',
1793 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001794 if os.path.exists(path):
1795 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001796
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001797 A.addApproval('Code-Review', 2)
1798 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001799 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001800 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001801 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001802 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001803 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001804 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001805 'SUCCESS')
1806 self.assertEqual(A.data['status'], 'MERGED')
1807 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001808
James E. Blair2fa50962013-01-30 21:50:41 -08001809 def test_new_patchset_dequeues_old(self):
1810 "Test that a new patchset causes the old to be dequeued"
1811 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001812 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001813 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1814 M.setMerged()
1815
1816 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1817 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1818 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1819 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001820 A.addApproval('Code-Review', 2)
1821 B.addApproval('Code-Review', 2)
1822 C.addApproval('Code-Review', 2)
1823 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001824
1825 C.setDependsOn(B, 1)
1826 B.setDependsOn(A, 1)
1827 A.setDependsOn(M, 1)
1828
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001829 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1830 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1831 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1832 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001833 self.waitUntilSettled()
1834
1835 B.addPatchset()
1836 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1837 self.waitUntilSettled()
1838
Paul Belanger174a8272017-03-14 13:20:10 -04001839 self.executor_server.hold_jobs_in_build = False
1840 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001841 self.waitUntilSettled()
1842
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001843 self.assertEqual(A.data['status'], 'MERGED')
1844 self.assertEqual(A.reported, 2)
1845 self.assertEqual(B.data['status'], 'NEW')
1846 self.assertEqual(B.reported, 2)
1847 self.assertEqual(C.data['status'], 'NEW')
1848 self.assertEqual(C.reported, 2)
1849 self.assertEqual(D.data['status'], 'MERGED')
1850 self.assertEqual(D.reported, 2)
1851 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001852
James E. Blairba437362015-02-07 11:41:52 -08001853 def test_new_patchset_check(self):
1854 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001855
Paul Belanger174a8272017-03-14 13:20:10 -04001856 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001857
1858 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001859 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001860 tenant = self.sched.abide.tenants.get('tenant-one')
1861 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001862
1863 # Add two git-dependent changes
1864 B.setDependsOn(A, 1)
1865 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1866 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001867 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1868 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001869
1870 # A live item, and a non-live/live pair
1871 items = check_pipeline.getAllItems()
1872 self.assertEqual(len(items), 3)
1873
1874 self.assertEqual(items[0].change.number, '1')
1875 self.assertEqual(items[0].change.patchset, '1')
1876 self.assertFalse(items[0].live)
1877
1878 self.assertEqual(items[1].change.number, '2')
1879 self.assertEqual(items[1].change.patchset, '1')
1880 self.assertTrue(items[1].live)
1881
1882 self.assertEqual(items[2].change.number, '1')
1883 self.assertEqual(items[2].change.patchset, '1')
1884 self.assertTrue(items[2].live)
1885
1886 # Add a new patchset to A
1887 A.addPatchset()
1888 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1889 self.waitUntilSettled()
1890
1891 # The live copy of A,1 should be gone, but the non-live and B
1892 # should continue, and we should have a new A,2
1893 items = check_pipeline.getAllItems()
1894 self.assertEqual(len(items), 3)
1895
1896 self.assertEqual(items[0].change.number, '1')
1897 self.assertEqual(items[0].change.patchset, '1')
1898 self.assertFalse(items[0].live)
1899
1900 self.assertEqual(items[1].change.number, '2')
1901 self.assertEqual(items[1].change.patchset, '1')
1902 self.assertTrue(items[1].live)
1903
1904 self.assertEqual(items[2].change.number, '1')
1905 self.assertEqual(items[2].change.patchset, '2')
1906 self.assertTrue(items[2].live)
1907
1908 # Add a new patchset to B
1909 B.addPatchset()
1910 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1911 self.waitUntilSettled()
1912
1913 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1914 # but we should have a new B,2 (still based on A,1)
1915 items = check_pipeline.getAllItems()
1916 self.assertEqual(len(items), 3)
1917
1918 self.assertEqual(items[0].change.number, '1')
1919 self.assertEqual(items[0].change.patchset, '2')
1920 self.assertTrue(items[0].live)
1921
1922 self.assertEqual(items[1].change.number, '1')
1923 self.assertEqual(items[1].change.patchset, '1')
1924 self.assertFalse(items[1].live)
1925
1926 self.assertEqual(items[2].change.number, '2')
1927 self.assertEqual(items[2].change.patchset, '2')
1928 self.assertTrue(items[2].live)
1929
1930 self.builds[0].release()
1931 self.waitUntilSettled()
1932 self.builds[0].release()
1933 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001934 self.executor_server.hold_jobs_in_build = False
1935 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001936 self.waitUntilSettled()
1937
1938 self.assertEqual(A.reported, 1)
1939 self.assertEqual(B.reported, 1)
1940 self.assertEqual(self.history[0].result, 'ABORTED')
1941 self.assertEqual(self.history[0].changes, '1,1')
1942 self.assertEqual(self.history[1].result, 'ABORTED')
1943 self.assertEqual(self.history[1].changes, '1,1 2,1')
1944 self.assertEqual(self.history[2].result, 'SUCCESS')
1945 self.assertEqual(self.history[2].changes, '1,2')
1946 self.assertEqual(self.history[3].result, 'SUCCESS')
1947 self.assertEqual(self.history[3].changes, '1,1 2,2')
1948
1949 def test_abandoned_gate(self):
1950 "Test that an abandoned change is dequeued from gate"
1951
Paul Belanger174a8272017-03-14 13:20:10 -04001952 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001953
1954 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001955 A.addApproval('Code-Review', 2)
1956 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001957 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001958 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1959 self.assertEqual(self.builds[0].name, 'project-merge')
1960
1961 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1962 self.waitUntilSettled()
1963
Paul Belanger174a8272017-03-14 13:20:10 -04001964 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001965 self.waitUntilSettled()
1966
Arie5b4048c2016-10-05 16:12:06 +03001967 self.assertBuilds([])
1968 self.assertHistory([
1969 dict(name='project-merge', result='ABORTED', changes='1,1')],
1970 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001971 self.assertEqual(A.reported, 1,
1972 "Abandoned gate change should report only start")
1973
1974 def test_abandoned_check(self):
1975 "Test that an abandoned change is dequeued from check"
1976
Paul Belanger174a8272017-03-14 13:20:10 -04001977 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001978
1979 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1980 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001981 tenant = self.sched.abide.tenants.get('tenant-one')
1982 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001983
1984 # Add two git-dependent changes
1985 B.setDependsOn(A, 1)
1986 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1987 self.waitUntilSettled()
1988 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1989 self.waitUntilSettled()
1990 # A live item, and a non-live/live pair
1991 items = check_pipeline.getAllItems()
1992 self.assertEqual(len(items), 3)
1993
1994 self.assertEqual(items[0].change.number, '1')
1995 self.assertFalse(items[0].live)
1996
1997 self.assertEqual(items[1].change.number, '2')
1998 self.assertTrue(items[1].live)
1999
2000 self.assertEqual(items[2].change.number, '1')
2001 self.assertTrue(items[2].live)
2002
2003 # Abandon A
2004 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2005 self.waitUntilSettled()
2006
2007 # The live copy of A should be gone, but the non-live and B
2008 # should continue
2009 items = check_pipeline.getAllItems()
2010 self.assertEqual(len(items), 2)
2011
2012 self.assertEqual(items[0].change.number, '1')
2013 self.assertFalse(items[0].live)
2014
2015 self.assertEqual(items[1].change.number, '2')
2016 self.assertTrue(items[1].live)
2017
Paul Belanger174a8272017-03-14 13:20:10 -04002018 self.executor_server.hold_jobs_in_build = False
2019 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08002020 self.waitUntilSettled()
2021
2022 self.assertEqual(len(self.history), 4)
2023 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01002024 'Build should have been aborted')
2025 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08002026 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01002027
Steve Varnau7b78b312015-04-03 14:49:46 -07002028 def test_abandoned_not_timer(self):
2029 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002030 # This test can not use simple_layout because it must start
2031 # with a configuration which does not include a
2032 # timer-triggered job so that we have an opportunity to set
2033 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002034 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07002035 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002036 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07002037 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07002038 # The pipeline triggers every second, so we should have seen
2039 # several by now.
2040 time.sleep(5)
2041 self.waitUntilSettled()
2042 # Stop queuing timer triggered jobs so that the assertions
2043 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08002044 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002045 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07002046 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07002047 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002048 # If APScheduler is in mid-event when we remove the job, we
2049 # can end up with one more event firing, so give it an extra
2050 # second to settle.
2051 time.sleep(1)
2052 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002053
James E. Blair21037782017-07-19 11:56:55 -07002054 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07002055
2056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2057 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2058 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002059 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07002060
2061 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2062 self.waitUntilSettled()
2063
James E. Blair21037782017-07-19 11:56:55 -07002064 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07002065
Paul Belanger174a8272017-03-14 13:20:10 -04002066 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07002067 self.waitUntilSettled()
2068
James E. Blair2fa50962013-01-30 21:50:41 -08002069 def test_new_patchset_dequeues_old_on_head(self):
2070 "Test that a new patchset causes the old to be dequeued (at head)"
2071 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04002072 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002073 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2074 M.setMerged()
2075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2076 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2077 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2078 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002079 A.addApproval('Code-Review', 2)
2080 B.addApproval('Code-Review', 2)
2081 C.addApproval('Code-Review', 2)
2082 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002083
2084 C.setDependsOn(B, 1)
2085 B.setDependsOn(A, 1)
2086 A.setDependsOn(M, 1)
2087
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002088 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2089 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2090 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2091 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002092 self.waitUntilSettled()
2093
2094 A.addPatchset()
2095 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2096 self.waitUntilSettled()
2097
Paul Belanger174a8272017-03-14 13:20:10 -04002098 self.executor_server.hold_jobs_in_build = False
2099 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002100 self.waitUntilSettled()
2101
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002102 self.assertEqual(A.data['status'], 'NEW')
2103 self.assertEqual(A.reported, 2)
2104 self.assertEqual(B.data['status'], 'NEW')
2105 self.assertEqual(B.reported, 2)
2106 self.assertEqual(C.data['status'], 'NEW')
2107 self.assertEqual(C.reported, 2)
2108 self.assertEqual(D.data['status'], 'MERGED')
2109 self.assertEqual(D.reported, 2)
2110 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002111
2112 def test_new_patchset_dequeues_old_without_dependents(self):
2113 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04002114 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2116 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2117 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002118 A.addApproval('Code-Review', 2)
2119 B.addApproval('Code-Review', 2)
2120 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002121
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002122 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2123 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2124 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002125 self.waitUntilSettled()
2126
2127 B.addPatchset()
2128 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2129 self.waitUntilSettled()
2130
Paul Belanger174a8272017-03-14 13:20:10 -04002131 self.executor_server.hold_jobs_in_build = False
2132 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002133 self.waitUntilSettled()
2134
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002135 self.assertEqual(A.data['status'], 'MERGED')
2136 self.assertEqual(A.reported, 2)
2137 self.assertEqual(B.data['status'], 'NEW')
2138 self.assertEqual(B.reported, 2)
2139 self.assertEqual(C.data['status'], 'MERGED')
2140 self.assertEqual(C.reported, 2)
2141 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002142
2143 def test_new_patchset_dequeues_old_independent_queue(self):
2144 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002145 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2147 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2148 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2149 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2150 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2151 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2152 self.waitUntilSettled()
2153
2154 B.addPatchset()
2155 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2156 self.waitUntilSettled()
2157
Paul Belanger174a8272017-03-14 13:20:10 -04002158 self.executor_server.hold_jobs_in_build = False
2159 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002160 self.waitUntilSettled()
2161
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002162 self.assertEqual(A.data['status'], 'NEW')
2163 self.assertEqual(A.reported, 1)
2164 self.assertEqual(B.data['status'], 'NEW')
2165 self.assertEqual(B.reported, 1)
2166 self.assertEqual(C.data['status'], 'NEW')
2167 self.assertEqual(C.reported, 1)
2168 self.assertEqual(len(self.history), 10)
2169 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002170
James E. Blaircc020532017-04-19 13:04:44 -07002171 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002172 def test_noop_job(self):
2173 "Test that the internal noop job works"
2174 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002175 A.addApproval('Code-Review', 2)
2176 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002177 self.waitUntilSettled()
2178
2179 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2180 self.assertTrue(self.sched._areAllBuildsComplete())
2181 self.assertEqual(len(self.history), 0)
2182 self.assertEqual(A.data['status'], 'MERGED')
2183 self.assertEqual(A.reported, 2)
2184
James E. Blair06cc3922017-04-19 10:08:10 -07002185 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002186 def test_no_job_project(self):
2187 "Test that reports with no jobs don't get sent"
2188 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2189 'master', 'A')
2190 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2191 self.waitUntilSettled()
2192
2193 # Change wasn't reported to
2194 self.assertEqual(A.reported, False)
2195
2196 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002197 tenant = self.sched.abide.tenants.get('tenant-one')
2198 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002199 items = check_pipeline.getAllItems()
2200 self.assertEqual(len(items), 0)
2201
2202 self.assertEqual(len(self.history), 0)
2203
James E. Blair7d0dedc2013-02-21 17:26:09 -08002204 def test_zuul_refs(self):
2205 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002206 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002207 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2208 M1.setMerged()
2209 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2210 M2.setMerged()
2211
2212 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2213 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2214 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2215 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002216 A.addApproval('Code-Review', 2)
2217 B.addApproval('Code-Review', 2)
2218 C.addApproval('Code-Review', 2)
2219 D.addApproval('Code-Review', 2)
2220 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2221 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2222 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2223 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002224
2225 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002226 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002227 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002228 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002229 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002230 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002231 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002232 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002233 self.waitUntilSettled()
2234
Clint Byrum3343e3e2016-11-15 16:05:03 -08002235 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002236 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002237 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002238 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002239 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002240 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002241 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002242 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002243 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002244 d_build = x
2245 if a_build and b_build and c_build and d_build:
2246 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002247
Clint Byrum3343e3e2016-11-15 16:05:03 -08002248 # should have a, not b, and should not be in project2
2249 self.assertTrue(a_build.hasChanges(A))
2250 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002251
Clint Byrum3343e3e2016-11-15 16:05:03 -08002252 # should have a and b, and should not be in project2
2253 self.assertTrue(b_build.hasChanges(A, B))
2254 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002255
Clint Byrum3343e3e2016-11-15 16:05:03 -08002256 # should have a and b in 1, c in 2
2257 self.assertTrue(c_build.hasChanges(A, B, C))
2258 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002259
Clint Byrum3343e3e2016-11-15 16:05:03 -08002260 # should have a and b in 1, c and d in 2
2261 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002262
Paul Belanger174a8272017-03-14 13:20:10 -04002263 self.executor_server.hold_jobs_in_build = False
2264 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002265 self.waitUntilSettled()
2266
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002267 self.assertEqual(A.data['status'], 'MERGED')
2268 self.assertEqual(A.reported, 2)
2269 self.assertEqual(B.data['status'], 'MERGED')
2270 self.assertEqual(B.reported, 2)
2271 self.assertEqual(C.data['status'], 'MERGED')
2272 self.assertEqual(C.reported, 2)
2273 self.assertEqual(D.data['status'], 'MERGED')
2274 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002275
James E. Blair4a28a882013-08-23 15:17:33 -07002276 def test_rerun_on_error(self):
2277 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002278 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002280 A.addApproval('Code-Review', 2)
2281 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002282 self.waitUntilSettled()
2283
James E. Blair412fba82017-01-26 15:00:50 -08002284 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002285 self.executor_server.hold_jobs_in_build = False
2286 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002287 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002288 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002289 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2290
James E. Blair412e5582013-04-22 15:50:12 -07002291 def test_statsd(self):
2292 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002293 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002294 statsd.incr('test-incr')
2295 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002296 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002297 self.assertReportedStat('test-incr', '1|c')
2298 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002299 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002300
James E. Blairdad52252014-02-07 16:59:17 -08002301 def test_stuck_job_cleanup(self):
2302 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002303
2304 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002305 self.gearman_server.hold_jobs_in_queue = True
2306 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002307 A.addApproval('Code-Review', 2)
2308 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002309 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002310 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002311 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2312
James E. Blair31cb3e82017-04-20 13:08:56 -07002313 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002314 self.sched.reconfigure(self.config)
2315 self.waitUntilSettled()
2316
James E. Blair18c64442014-03-18 10:14:45 -07002317 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002318 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002319 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002320 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2321 self.assertTrue(self.sched._areAllBuildsComplete())
2322
2323 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002324 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002325 self.assertEqual(self.history[0].result, 'SUCCESS')
2326
James E. Blair879dafb2015-07-17 14:04:49 -07002327 def test_file_head(self):
2328 # This is a regression test for an observed bug. A change
2329 # with a file named "HEAD" in the root directory of the repo
2330 # was processed by a merger. It then was unable to reset the
2331 # repo because of:
2332 # GitCommandError: 'git reset --hard HEAD' returned
2333 # with exit code 128
2334 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2335 # and filename
2336 # Use '--' to separate filenames from revisions'
2337
2338 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002339 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002340 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2341
2342 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2343 self.waitUntilSettled()
2344
2345 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2346 self.waitUntilSettled()
2347
2348 self.assertIn('Build succeeded', A.messages[0])
2349 self.assertIn('Build succeeded', B.messages[0])
2350
James E. Blair70c71582013-03-06 08:50:50 -08002351 def test_file_jobs(self):
2352 "Test that file jobs run only when appropriate"
2353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002354 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002355 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002356 A.addApproval('Code-Review', 2)
2357 B.addApproval('Code-Review', 2)
2358 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2359 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002360 self.waitUntilSettled()
2361
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002362 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002363 if x.name == 'project-testfile']
2364
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002365 self.assertEqual(len(testfile_jobs), 1)
2366 self.assertEqual(testfile_jobs[0].changes, '1,2')
2367 self.assertEqual(A.data['status'], 'MERGED')
2368 self.assertEqual(A.reported, 2)
2369 self.assertEqual(B.data['status'], 'MERGED')
2370 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002371
Clint Byrum3ee39f32016-11-17 23:45:07 -08002372 def _test_irrelevant_files_jobs(self, should_skip):
2373 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002374 if should_skip:
2375 files = {'ignoreme': 'ignored\n'}
2376 else:
2377 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002378
2379 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002380 'master',
2381 'test irrelevant-files',
2382 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002383 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2384 self.waitUntilSettled()
2385
2386 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002387 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002388
2389 if should_skip:
2390 self.assertEqual([], tested_change_ids)
2391 else:
2392 self.assertIn(change.data['number'], tested_change_ids)
2393
James E. Blaircc324192017-04-19 14:36:08 -07002394 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002395 def test_irrelevant_files_match_skips_job(self):
2396 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002397
James E. Blaircc324192017-04-19 14:36:08 -07002398 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002399 def test_irrelevant_files_no_match_runs_job(self):
2400 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002401
James E. Blaircc324192017-04-19 14:36:08 -07002402 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002403 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002404 files = {'ignoreme': 'ignored\n'}
2405
2406 change = self.fake_gerrit.addFakeChange('org/project',
2407 'master',
2408 'test irrelevant-files',
2409 files=files)
2410 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2411 self.waitUntilSettled()
2412
2413 run_jobs = set([build.name for build in self.history])
2414
2415 self.assertEqual(set(['project-test-nomatch-starts-empty',
2416 'project-test-nomatch-starts-full']), run_jobs)
2417
James E. Blairaf8b2082017-10-03 15:38:27 -07002418 @simple_layout('layouts/job-vars.yaml')
2419 def test_inherited_job_variables(self):
2420 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2421 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2422 self.waitUntilSettled()
2423 self.assertHistory([
2424 dict(name='parentjob', result='SUCCESS'),
2425 dict(name='child1', result='SUCCESS'),
2426 dict(name='child2', result='SUCCESS'),
2427 dict(name='child3', result='SUCCESS'),
2428 ], ordered=False)
2429 j = self.getJobFromHistory('parentjob')
2430 rp = set([p['name'] for p in j.parameters['projects']])
2431 self.assertEqual(j.parameters['vars']['override'], 0)
2432 self.assertEqual(j.parameters['vars']['child1override'], 0)
2433 self.assertEqual(j.parameters['vars']['parent'], 0)
2434 self.assertFalse('child1' in j.parameters['vars'])
2435 self.assertFalse('child2' in j.parameters['vars'])
2436 self.assertFalse('child3' in j.parameters['vars'])
2437 self.assertEqual(rp, set(['org/project', 'org/project0',
2438 'org/project0']))
2439 j = self.getJobFromHistory('child1')
2440 rp = set([p['name'] for p in j.parameters['projects']])
2441 self.assertEqual(j.parameters['vars']['override'], 1)
2442 self.assertEqual(j.parameters['vars']['child1override'], 1)
2443 self.assertEqual(j.parameters['vars']['parent'], 0)
2444 self.assertEqual(j.parameters['vars']['child1'], 1)
2445 self.assertFalse('child2' in j.parameters['vars'])
2446 self.assertFalse('child3' in j.parameters['vars'])
2447 self.assertEqual(rp, set(['org/project', 'org/project0',
2448 'org/project1']))
2449 j = self.getJobFromHistory('child2')
2450 rp = set([p['name'] for p in j.parameters['projects']])
2451 self.assertEqual(j.parameters['vars']['override'], 2)
2452 self.assertEqual(j.parameters['vars']['child1override'], 0)
2453 self.assertEqual(j.parameters['vars']['parent'], 0)
2454 self.assertFalse('child1' in j.parameters['vars'])
2455 self.assertEqual(j.parameters['vars']['child2'], 2)
2456 self.assertFalse('child3' in j.parameters['vars'])
2457 self.assertEqual(rp, set(['org/project', 'org/project0',
2458 'org/project2']))
2459 j = self.getJobFromHistory('child3')
2460 rp = set([p['name'] for p in j.parameters['projects']])
2461 self.assertEqual(j.parameters['vars']['override'], 3)
2462 self.assertEqual(j.parameters['vars']['child1override'], 0)
2463 self.assertEqual(j.parameters['vars']['parent'], 0)
2464 self.assertFalse('child1' in j.parameters['vars'])
2465 self.assertFalse('child2' in j.parameters['vars'])
2466 self.assertEqual(j.parameters['vars']['child3'], 3)
2467 self.assertEqual(rp, set(['org/project', 'org/project0',
2468 'org/project3']))
2469
James E. Blairb09a0c52017-10-04 07:35:14 -07002470 @simple_layout('layouts/job-variants.yaml')
2471 def test_job_branch_variants(self):
2472 self.create_branch('org/project', 'stable/diablo')
2473 self.create_branch('org/project', 'stable/essex')
2474 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2475 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2476 self.waitUntilSettled()
2477
2478 B = self.fake_gerrit.addFakeChange('org/project', 'stable/diablo', 'B')
2479 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2480 self.waitUntilSettled()
2481
2482 C = self.fake_gerrit.addFakeChange('org/project', 'stable/essex', 'C')
2483 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2484 self.waitUntilSettled()
2485 self.assertHistory([
2486 dict(name='python27', result='SUCCESS'),
2487 dict(name='python27', result='SUCCESS'),
2488 dict(name='python27', result='SUCCESS'),
2489 ])
2490
2491 p = self.history[0].parameters
2492 self.assertEqual(p['timeout'], 40)
2493 self.assertEqual(len(p['nodes']), 1)
2494 self.assertEqual(p['nodes'][0]['label'], 'new')
2495 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2496 ['base-pre', 'py27-pre'])
2497 self.assertEqual([x['path'] for x in p['post_playbooks']],
2498 ['py27-post-a', 'py27-post-b', 'base-post'])
2499 self.assertEqual([x['path'] for x in p['playbooks']],
James E. Blair2f589fe2017-10-26 12:57:41 -07002500 ['playbooks/python27.yaml'])
James E. Blairb09a0c52017-10-04 07:35:14 -07002501
2502 p = self.history[1].parameters
2503 self.assertEqual(p['timeout'], 50)
2504 self.assertEqual(len(p['nodes']), 1)
2505 self.assertEqual(p['nodes'][0]['label'], 'old')
2506 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2507 ['base-pre', 'py27-pre', 'py27-diablo-pre'])
2508 self.assertEqual([x['path'] for x in p['post_playbooks']],
2509 ['py27-diablo-post', 'py27-post-a', 'py27-post-b',
2510 'base-post'])
2511 self.assertEqual([x['path'] for x in p['playbooks']],
2512 ['py27-diablo'])
2513
2514 p = self.history[2].parameters
2515 self.assertEqual(p['timeout'], 40)
2516 self.assertEqual(len(p['nodes']), 1)
2517 self.assertEqual(p['nodes'][0]['label'], 'new')
2518 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2519 ['base-pre', 'py27-pre', 'py27-essex-pre'])
2520 self.assertEqual([x['path'] for x in p['post_playbooks']],
2521 ['py27-essex-post', 'py27-post-a', 'py27-post-b',
2522 'base-post'])
2523 self.assertEqual([x['path'] for x in p['playbooks']],
James E. Blair2f589fe2017-10-26 12:57:41 -07002524 ['playbooks/python27.yaml'])
James E. Blairb09a0c52017-10-04 07:35:14 -07002525
James E. Blair00292672017-10-26 15:29:39 -07002526 @simple_layout("layouts/no-run.yaml")
2527 def test_job_without_run(self):
2528 "Test that a job without a run playbook errors"
2529 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2530 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2531 self.waitUntilSettled()
2532 self.assertIn('Job base does not specify a run playbook',
2533 A.messages[-1])
2534
James E. Blairc8a1e052014-02-25 09:29:26 -08002535 def test_queue_names(self):
2536 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002537 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002538 (trusted, project1) = tenant.getProject('org/project1')
2539 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002540 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2541 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2542 self.assertEqual(q1.name, 'integrated')
2543 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002544
James E. Blair64ed6f22013-07-10 14:07:23 -07002545 def test_queue_precedence(self):
2546 "Test that queue precedence works"
2547
2548 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002549 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2551 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002552 A.addApproval('Code-Review', 2)
2553 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002554
2555 self.waitUntilSettled()
2556 self.gearman_server.hold_jobs_in_queue = False
2557 self.gearman_server.release()
2558 self.waitUntilSettled()
2559
James E. Blair8de58bd2013-07-18 16:23:33 -07002560 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002561 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002562 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002563 self.waitUntilSettled()
2564
James E. Blair64ed6f22013-07-10 14:07:23 -07002565 self.log.debug(self.history)
2566 self.assertEqual(self.history[0].pipeline, 'gate')
2567 self.assertEqual(self.history[1].pipeline, 'check')
2568 self.assertEqual(self.history[2].pipeline, 'gate')
2569 self.assertEqual(self.history[3].pipeline, 'gate')
2570 self.assertEqual(self.history[4].pipeline, 'check')
2571 self.assertEqual(self.history[5].pipeline, 'check')
2572
James E. Blair419a8672017-10-18 14:48:25 -07002573 def test_reconfigure_merge(self):
2574 """Test that two reconfigure events are merged"""
2575
2576 tenant = self.sched.abide.tenants['tenant-one']
2577 (trusted, project) = tenant.getProject('org/project')
2578
2579 self.sched.run_handler_lock.acquire()
2580 self.assertEqual(self.sched.management_event_queue.qsize(), 0)
2581
2582 self.sched.reconfigureTenant(tenant, project)
2583 self.assertEqual(self.sched.management_event_queue.qsize(), 1)
2584
2585 self.sched.reconfigureTenant(tenant, project)
2586 # The second event should have been combined with the first
2587 # so we should still only have one entry.
2588 self.assertEqual(self.sched.management_event_queue.qsize(), 1)
2589
2590 self.sched.run_handler_lock.release()
2591 self.waitUntilSettled()
2592
2593 self.assertEqual(self.sched.management_event_queue.qsize(), 0)
2594
James E. Blaircdccd972013-07-01 12:10:22 -07002595 def test_live_reconfiguration(self):
2596 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002597 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002598 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002599 A.addApproval('Code-Review', 2)
2600 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002601 self.waitUntilSettled()
2602
2603 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002604 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002605
Paul Belanger174a8272017-03-14 13:20:10 -04002606 self.executor_server.hold_jobs_in_build = False
2607 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002608 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002609 self.assertEqual(self.getJobFromHistory('project-merge').result,
2610 'SUCCESS')
2611 self.assertEqual(self.getJobFromHistory('project-test1').result,
2612 'SUCCESS')
2613 self.assertEqual(self.getJobFromHistory('project-test2').result,
2614 'SUCCESS')
2615 self.assertEqual(A.data['status'], 'MERGED')
2616 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002617
James E. Blair027ba992017-09-20 13:48:32 -07002618 def test_live_reconfiguration_abort(self):
2619 # Raise an exception during reconfiguration and verify we
2620 # still function.
2621 self.executor_server.hold_jobs_in_build = True
2622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2623 A.addApproval('Code-Review', 2)
2624 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2625 self.waitUntilSettled()
2626
2627 tenant = self.sched.abide.tenants.get('tenant-one')
2628 pipeline = tenant.layout.pipelines['gate']
2629 change = pipeline.getAllItems()[0].change
2630 # Set this to an invalid value to cause an exception during
2631 # reconfiguration.
2632 change.branch = None
2633
2634 self.sched.reconfigure(self.config)
2635 self.waitUntilSettled()
2636
2637 self.executor_server.hold_jobs_in_build = False
2638 self.executor_server.release()
2639
2640 self.waitUntilSettled()
2641 self.assertEqual(self.getJobFromHistory('project-merge').result,
2642 'ABORTED')
2643 self.assertEqual(A.data['status'], 'NEW')
2644 # The final report fails because of the invalid value set above.
2645 self.assertEqual(A.reported, 1)
2646
James E. Blair6bc782d2015-07-17 16:20:21 -07002647 def test_live_reconfiguration_merge_conflict(self):
2648 # A real-world bug: a change in a gate queue has a merge
2649 # conflict and a job is added to its project while it's
2650 # sitting in the queue. The job gets added to the change and
2651 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002652 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002653
2654 # This change is fine. It's here to stop the queue long
2655 # enough for the next change to be subject to the
2656 # reconfiguration, as well as to provide a conflict for the
2657 # next change. This change will succeed and merge.
2658 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002659 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002660 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002661
2662 # This change will be in merge conflict. During the
2663 # reconfiguration, we will add a job. We want to make sure
2664 # that doesn't cause it to get stuck.
2665 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002666 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002667 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002668
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002669 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2670 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002671
2672 self.waitUntilSettled()
2673
2674 # No jobs have run yet
2675 self.assertEqual(A.data['status'], 'NEW')
2676 self.assertEqual(A.reported, 1)
2677 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002678 self.assertEqual(len(self.history), 0)
2679
2680 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002681 self.commitConfigUpdate('common-config',
2682 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002683 self.sched.reconfigure(self.config)
2684 self.waitUntilSettled()
2685
Paul Belanger174a8272017-03-14 13:20:10 -04002686 self.executor_server.hold_jobs_in_build = False
2687 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002688 self.waitUntilSettled()
2689
2690 self.assertEqual(A.data['status'], 'MERGED')
2691 self.assertEqual(A.reported, 2)
2692 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002693 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002694 self.assertEqual(self.getJobFromHistory('project-merge').result,
2695 'SUCCESS')
2696 self.assertEqual(self.getJobFromHistory('project-test1').result,
2697 'SUCCESS')
2698 self.assertEqual(self.getJobFromHistory('project-test2').result,
2699 'SUCCESS')
2700 self.assertEqual(self.getJobFromHistory('project-test3').result,
2701 'SUCCESS')
2702 self.assertEqual(len(self.history), 4)
2703
James E. Blair400e8fd2015-07-30 17:44:45 -07002704 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002705 # An extrapolation of test_live_reconfiguration_merge_conflict
2706 # that tests a job added to a job tree with a failed root does
2707 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002708 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002709
2710 # This change is fine. It's here to stop the queue long
2711 # enough for the next change to be subject to the
2712 # reconfiguration. This change will succeed and merge.
2713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002714 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002715 A.addApproval('Code-Review', 2)
2716 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002717 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002718 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002719 self.waitUntilSettled()
2720
2721 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002722 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002723 B.addApproval('Code-Review', 2)
2724 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002725 self.waitUntilSettled()
2726
Paul Belanger174a8272017-03-14 13:20:10 -04002727 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002728 self.waitUntilSettled()
2729
2730 # Both -merge jobs have run, but no others.
2731 self.assertEqual(A.data['status'], 'NEW')
2732 self.assertEqual(A.reported, 1)
2733 self.assertEqual(B.data['status'], 'NEW')
2734 self.assertEqual(B.reported, 1)
2735 self.assertEqual(self.history[0].result, 'SUCCESS')
2736 self.assertEqual(self.history[0].name, 'project-merge')
2737 self.assertEqual(self.history[1].result, 'FAILURE')
2738 self.assertEqual(self.history[1].name, 'project-merge')
2739 self.assertEqual(len(self.history), 2)
2740
2741 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002742 self.commitConfigUpdate('common-config',
2743 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002744 self.sched.reconfigure(self.config)
2745 self.waitUntilSettled()
2746
Paul Belanger174a8272017-03-14 13:20:10 -04002747 self.executor_server.hold_jobs_in_build = False
2748 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002749 self.waitUntilSettled()
2750
2751 self.assertEqual(A.data['status'], 'MERGED')
2752 self.assertEqual(A.reported, 2)
2753 self.assertEqual(B.data['status'], 'NEW')
2754 self.assertEqual(B.reported, 2)
2755 self.assertEqual(self.history[0].result, 'SUCCESS')
2756 self.assertEqual(self.history[0].name, 'project-merge')
2757 self.assertEqual(self.history[1].result, 'FAILURE')
2758 self.assertEqual(self.history[1].name, 'project-merge')
2759 self.assertEqual(self.history[2].result, 'SUCCESS')
2760 self.assertEqual(self.history[3].result, 'SUCCESS')
2761 self.assertEqual(self.history[4].result, 'SUCCESS')
2762 self.assertEqual(len(self.history), 5)
2763
James E. Blair400e8fd2015-07-30 17:44:45 -07002764 def test_live_reconfiguration_failed_job(self):
2765 # Test that a change with a removed failing job does not
2766 # disrupt reconfiguration. If a change has a failed job and
2767 # that job is removed during a reconfiguration, we observed a
2768 # bug where the code to re-set build statuses would run on
2769 # that build and raise an exception because the job no longer
2770 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002771 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002772
2773 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2774
2775 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002776 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002777
2778 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2779 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002780 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002781 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002782 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002783 self.waitUntilSettled()
2784
2785 self.assertEqual(A.data['status'], 'NEW')
2786 self.assertEqual(A.reported, 0)
2787
2788 self.assertEqual(self.getJobFromHistory('project-merge').result,
2789 'SUCCESS')
2790 self.assertEqual(self.getJobFromHistory('project-test1').result,
2791 'FAILURE')
2792 self.assertEqual(len(self.history), 2)
2793
2794 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002795 self.commitConfigUpdate('common-config',
2796 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002797 self.sched.reconfigure(self.config)
2798 self.waitUntilSettled()
2799
Paul Belanger174a8272017-03-14 13:20:10 -04002800 self.executor_server.hold_jobs_in_build = False
2801 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002802 self.waitUntilSettled()
2803
2804 self.assertEqual(self.getJobFromHistory('project-test2').result,
2805 'SUCCESS')
2806 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2807 'SUCCESS')
2808 self.assertEqual(len(self.history), 4)
2809
2810 self.assertEqual(A.data['status'], 'NEW')
2811 self.assertEqual(A.reported, 1)
2812 self.assertIn('Build succeeded', A.messages[0])
2813 # Ensure the removed job was not included in the report.
2814 self.assertNotIn('project-test1', A.messages[0])
2815
James E. Blairfe707d12015-08-05 15:18:15 -07002816 def test_live_reconfiguration_shared_queue(self):
2817 # Test that a change with a failing job which was removed from
2818 # this project but otherwise still exists in the system does
2819 # not disrupt reconfiguration.
2820
Paul Belanger174a8272017-03-14 13:20:10 -04002821 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002822
2823 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2824
Paul Belanger174a8272017-03-14 13:20:10 -04002825 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002826
2827 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2828 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002829 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002830 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002831 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002832 self.waitUntilSettled()
2833
2834 self.assertEqual(A.data['status'], 'NEW')
2835 self.assertEqual(A.reported, 0)
2836
Clint Byrum69663d42017-05-01 14:51:04 -07002837 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002838 'SUCCESS')
2839 self.assertEqual(self.getJobFromHistory(
2840 'project1-project2-integration').result, 'FAILURE')
2841 self.assertEqual(len(self.history), 2)
2842
2843 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002844 self.commitConfigUpdate(
2845 'common-config',
2846 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002847 self.sched.reconfigure(self.config)
2848 self.waitUntilSettled()
2849
Paul Belanger174a8272017-03-14 13:20:10 -04002850 self.executor_server.hold_jobs_in_build = False
2851 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002852 self.waitUntilSettled()
2853
Clint Byrum69663d42017-05-01 14:51:04 -07002854 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002855 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002856 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002857 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002858 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002859 'SUCCESS')
2860 self.assertEqual(self.getJobFromHistory(
2861 'project1-project2-integration').result, 'FAILURE')
2862 self.assertEqual(len(self.history), 4)
2863
2864 self.assertEqual(A.data['status'], 'NEW')
2865 self.assertEqual(A.reported, 1)
2866 self.assertIn('Build succeeded', A.messages[0])
2867 # Ensure the removed job was not included in the report.
2868 self.assertNotIn('project1-project2-integration', A.messages[0])
2869
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002870 def test_double_live_reconfiguration_shared_queue(self):
2871 # This was a real-world regression. A change is added to
2872 # gate; a reconfigure happens, a second change which depends
2873 # on the first is added, and a second reconfiguration happens.
2874 # Ensure that both changes merge.
2875
2876 # A failure may indicate incorrect caching or cleaning up of
2877 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002878 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002879
2880 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2881 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2882 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002883 A.addApproval('Code-Review', 2)
2884 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002885
2886 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002887 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002888 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002889 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002890 self.waitUntilSettled()
2891
2892 # Reconfigure (with only one change in the pipeline).
2893 self.sched.reconfigure(self.config)
2894 self.waitUntilSettled()
2895
2896 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002897 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002898 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002899 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002900 self.waitUntilSettled()
2901
2902 # Reconfigure (with both in the pipeline).
2903 self.sched.reconfigure(self.config)
2904 self.waitUntilSettled()
2905
Paul Belanger174a8272017-03-14 13:20:10 -04002906 self.executor_server.hold_jobs_in_build = False
2907 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002908 self.waitUntilSettled()
2909
2910 self.assertEqual(len(self.history), 8)
2911
2912 self.assertEqual(A.data['status'], 'MERGED')
2913 self.assertEqual(A.reported, 2)
2914 self.assertEqual(B.data['status'], 'MERGED')
2915 self.assertEqual(B.reported, 2)
2916
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002917 def test_live_reconfiguration_del_project(self):
2918 # Test project deletion from layout
2919 # while changes are enqueued
2920
Paul Belanger174a8272017-03-14 13:20:10 -04002921 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002922 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2923 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2924 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2925
2926 # A Depends-On: B
2927 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2928 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002929 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002930
2931 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2932 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2933 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002934 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002935 self.waitUntilSettled()
2936 self.assertEqual(len(self.builds), 5)
2937
2938 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002939 self.commitConfigUpdate(
2940 'common-config',
2941 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002942 self.sched.reconfigure(self.config)
2943 self.waitUntilSettled()
2944
2945 # Builds for C aborted, builds for A succeed,
2946 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002947 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002948 self.assertEqual(job_c.changes, '3,1')
2949 self.assertEqual(job_c.result, 'ABORTED')
2950
Paul Belanger174a8272017-03-14 13:20:10 -04002951 self.executor_server.hold_jobs_in_build = False
2952 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002953 self.waitUntilSettled()
2954
Clint Byrum0149ada2017-04-04 12:22:01 -07002955 self.assertEqual(
2956 self.getJobFromHistory('project-test1', 'org/project').changes,
2957 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002958
2959 self.assertEqual(A.data['status'], 'NEW')
2960 self.assertEqual(B.data['status'], 'NEW')
2961 self.assertEqual(C.data['status'], 'NEW')
2962 self.assertEqual(A.reported, 1)
2963 self.assertEqual(B.reported, 0)
2964 self.assertEqual(C.reported, 0)
2965
Clint Byrum0149ada2017-04-04 12:22:01 -07002966 tenant = self.sched.abide.tenants.get('tenant-one')
2967 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002968 self.assertIn('Build succeeded', A.messages[0])
2969
James E. Blair872738f2017-10-31 16:40:36 -07002970 @simple_layout("layouts/reconfigure-failed-head.yaml")
2971 def test_live_reconfiguration_failed_change_at_head(self):
2972 # Test that if we reconfigure with a failed change at head,
2973 # that the change behind it isn't reparented onto it.
2974
2975 self.executor_server.hold_jobs_in_build = True
2976 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2977 A.addApproval('Code-Review', 2)
2978 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2979 B.addApproval('Code-Review', 2)
2980
2981 self.executor_server.failJob('job1', A)
2982
2983 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2984 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2985
2986 self.waitUntilSettled()
2987
2988 self.assertBuilds([
2989 dict(name='job1', changes='1,1'),
2990 dict(name='job2', changes='1,1'),
2991 dict(name='job1', changes='1,1 2,1'),
2992 dict(name='job2', changes='1,1 2,1'),
2993 ])
2994
2995 self.release(self.builds[0])
2996 self.waitUntilSettled()
2997
2998 self.assertBuilds([
2999 dict(name='job2', changes='1,1'),
3000 dict(name='job1', changes='2,1'),
3001 dict(name='job2', changes='2,1'),
3002 ])
3003
3004 # Unordered history comparison because the aborts can finish
3005 # in any order.
3006 self.assertHistory([
3007 dict(name='job1', result='FAILURE', changes='1,1'),
3008 dict(name='job1', result='ABORTED', changes='1,1 2,1'),
3009 dict(name='job2', result='ABORTED', changes='1,1 2,1'),
3010 ], ordered=False)
3011
3012 self.sched.reconfigure(self.config)
3013 self.waitUntilSettled()
3014
3015 self.executor_server.hold_jobs_in_build = False
3016 self.executor_server.release()
3017 self.waitUntilSettled()
3018
3019 self.assertBuilds([])
3020
3021 self.assertHistory([
3022 dict(name='job1', result='FAILURE', changes='1,1'),
3023 dict(name='job1', result='ABORTED', changes='1,1 2,1'),
3024 dict(name='job2', result='ABORTED', changes='1,1 2,1'),
3025 dict(name='job2', result='SUCCESS', changes='1,1'),
3026 dict(name='job1', result='SUCCESS', changes='2,1'),
3027 dict(name='job2', result='SUCCESS', changes='2,1'),
3028 ], ordered=False)
3029 self.assertEqual(A.data['status'], 'NEW')
3030 self.assertEqual(B.data['status'], 'MERGED')
3031 self.assertEqual(A.reported, 2)
3032 self.assertEqual(B.reported, 2)
3033
James E. Blair287c06d2013-07-24 10:39:30 -07003034 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07003035 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07003036 files = {'README': ''}
3037 self.addCommitToRepo("org/new-project", 'Initial commit',
3038 files=files, tag='init')
3039 self.newTenantConfig('tenants/delayed-repo-init.yaml')
3040 self.commitConfigUpdate(
3041 'common-config',
3042 'layouts/delayed-repo-init.yaml')
3043 self.sched.reconfigure(self.config)
3044 self.waitUntilSettled()
3045
James E. Blair287c06d2013-07-24 10:39:30 -07003046 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3047
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003048 A.addApproval('Code-Review', 2)
3049 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07003050 self.waitUntilSettled()
3051 self.assertEqual(self.getJobFromHistory('project-merge').result,
3052 'SUCCESS')
3053 self.assertEqual(self.getJobFromHistory('project-test1').result,
3054 'SUCCESS')
3055 self.assertEqual(self.getJobFromHistory('project-test2').result,
3056 'SUCCESS')
3057 self.assertEqual(A.data['status'], 'MERGED')
3058 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003059
James E. Blair9ea70072017-04-19 16:05:30 -07003060 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07003061 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07003062 self.init_repo("org/delete-project")
3063 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3064
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003065 A.addApproval('Code-Review', 2)
3066 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003067 self.waitUntilSettled()
3068 self.assertEqual(self.getJobFromHistory('project-merge').result,
3069 'SUCCESS')
3070 self.assertEqual(self.getJobFromHistory('project-test1').result,
3071 'SUCCESS')
3072 self.assertEqual(self.getJobFromHistory('project-test2').result,
3073 'SUCCESS')
3074 self.assertEqual(A.data['status'], 'MERGED')
3075 self.assertEqual(A.reported, 2)
3076
3077 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08003078 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05003079 if os.path.exists(os.path.join(self.merger_src_root, p)):
3080 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04003081 if os.path.exists(os.path.join(self.executor_src_root, p)):
3082 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003083
3084 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3085
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003086 B.addApproval('Code-Review', 2)
3087 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003088 self.waitUntilSettled()
3089 self.assertEqual(self.getJobFromHistory('project-merge').result,
3090 'SUCCESS')
3091 self.assertEqual(self.getJobFromHistory('project-test1').result,
3092 'SUCCESS')
3093 self.assertEqual(self.getJobFromHistory('project-test2').result,
3094 'SUCCESS')
3095 self.assertEqual(B.data['status'], 'MERGED')
3096 self.assertEqual(B.reported, 2)
3097
James E. Blair892cca62017-08-09 11:36:58 -07003098 @simple_layout('layouts/untrusted-secrets.yaml')
3099 def test_untrusted_secrets(self):
3100 "Test untrusted secrets"
3101 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3102 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3103 self.waitUntilSettled()
3104
3105 self.assertHistory([])
3106 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07003107 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07003108 A.messages[0])
3109
James E. Blaire68d43f2017-04-19 14:03:30 -07003110 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08003111 def test_tags(self):
3112 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08003113 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3114 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3115 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3116 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3117 self.waitUntilSettled()
3118
James E. Blaire68d43f2017-04-19 14:03:30 -07003119 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003120
3121 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07003122 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003123 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07003124 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08003125
3126 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003127 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07003128 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08003129
James E. Blair63bb0ef2013-07-29 17:14:51 -07003130 def test_timer(self):
3131 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003132 # This test can not use simple_layout because it must start
3133 # with a configuration which does not include a
3134 # timer-triggered job so that we have an opportunity to set
3135 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07003136 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04003137 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003138 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003139 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003140
Clark Boylan3ee090a2014-04-03 20:55:09 -07003141 # The pipeline triggers every second, so we should have seen
3142 # several by now.
3143 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003144 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07003145
3146 self.assertEqual(len(self.builds), 2)
3147
Paul Belanger174a8272017-03-14 13:20:10 -04003148 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07003149 # Stop queuing timer triggered jobs so that the assertions
3150 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003151 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003152 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07003153 self.waitUntilSettled()
3154 # If APScheduler is in mid-event when we remove the job, we
3155 # can end up with one more event firing, so give it an extra
3156 # second to settle.
3157 time.sleep(1)
3158 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003159 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07003160 self.waitUntilSettled()
3161
James E. Blair21037782017-07-19 11:56:55 -07003162 self.assertHistory([
3163 dict(name='project-bitrot', result='SUCCESS',
3164 ref='refs/heads/master'),
3165 dict(name='project-bitrot', result='SUCCESS',
3166 ref='refs/heads/stable'),
3167 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003168
James E. Blair4f6033c2014-03-27 15:49:09 -07003169 def test_idle(self):
3170 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003171 # This test can not use simple_layout because it must start
3172 # with a configuration which does not include a
3173 # timer-triggered job so that we have an opportunity to set
3174 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003175 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003176
Clark Boylan3ee090a2014-04-03 20:55:09 -07003177 for x in range(1, 3):
3178 # Test that timer triggers periodic jobs even across
3179 # layout config reloads.
3180 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003181 self.commitConfigUpdate('common-config',
3182 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003183 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003184 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003185
Clark Boylan3ee090a2014-04-03 20:55:09 -07003186 # The pipeline triggers every second, so we should have seen
3187 # several by now.
3188 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003189
3190 # Stop queuing timer triggered jobs so that the assertions
3191 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003192 self.commitConfigUpdate('common-config',
3193 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003194 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003195 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003196 # If APScheduler is in mid-event when we remove the job,
3197 # we can end up with one more event firing, so give it an
3198 # extra second to settle.
3199 time.sleep(1)
3200 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003201 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003202 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003203 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003204 self.waitUntilSettled()
3205 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003206 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003207
James E. Blaircc324192017-04-19 14:36:08 -07003208 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003209 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3211 self.waitUntilSettled()
3212
3213 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3214 self.waitUntilSettled()
3215
James E. Blairff80a2f2013-12-27 13:24:06 -08003216 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003217
3218 # A.messages only holds what FakeGerrit places in it. Thus we
3219 # work on the knowledge of what the first message should be as
3220 # it is only configured to go to SMTP.
3221
3222 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003223 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003224 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003225 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003226 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003227 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003228
3229 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003230 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003231 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003232 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003233 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003234 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003235
James E. Blaire5910202013-12-27 09:50:31 -08003236 def test_timer_smtp(self):
3237 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003238 # This test can not use simple_layout because it must start
3239 # with a configuration which does not include a
3240 # timer-triggered job so that we have an opportunity to set
3241 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003242 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003243 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003244 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003245
Clark Boylan3ee090a2014-04-03 20:55:09 -07003246 # The pipeline triggers every second, so we should have seen
3247 # several by now.
3248 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003249 self.waitUntilSettled()
3250
Clark Boylan3ee090a2014-04-03 20:55:09 -07003251 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003252 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003253 self.waitUntilSettled()
3254 self.assertEqual(len(self.history), 2)
3255
James E. Blaire5910202013-12-27 09:50:31 -08003256 self.assertEqual(self.getJobFromHistory(
3257 'project-bitrot-stable-old').result, 'SUCCESS')
3258 self.assertEqual(self.getJobFromHistory(
3259 'project-bitrot-stable-older').result, 'SUCCESS')
3260
James E. Blairff80a2f2013-12-27 13:24:06 -08003261 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003262
3263 # A.messages only holds what FakeGerrit places in it. Thus we
3264 # work on the knowledge of what the first message should be as
3265 # it is only configured to go to SMTP.
3266
3267 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003268 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003269 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003270 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003271 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003272 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003273
Clark Boylan3ee090a2014-04-03 20:55:09 -07003274 # Stop queuing timer triggered jobs and let any that may have
3275 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003276 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003277 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003278 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003279 # If APScheduler is in mid-event when we remove the job, we
3280 # can end up with one more event firing, so give it an extra
3281 # second to settle.
3282 time.sleep(1)
3283 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003284 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003285 self.waitUntilSettled()
3286
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003287 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003288 def test_timer_sshkey(self):
3289 "Test that a periodic job can setup SSH key authentication"
3290 self.worker.hold_jobs_in_build = True
3291 self.config.set('zuul', 'layout_config',
3292 'tests/fixtures/layout-timer.yaml')
3293 self.sched.reconfigure(self.config)
3294 self.registerJobs()
3295
3296 # The pipeline triggers every second, so we should have seen
3297 # several by now.
3298 time.sleep(5)
3299 self.waitUntilSettled()
3300
3301 self.assertEqual(len(self.builds), 2)
3302
3303 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3304 self.assertTrue(os.path.isfile(ssh_wrapper))
3305 with open(ssh_wrapper) as f:
3306 ssh_wrapper_content = f.read()
3307 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3308 # In the unit tests Merger runs in the same process,
3309 # so we see its' environment variables
3310 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3311
3312 self.worker.release('.*')
3313 self.waitUntilSettled()
3314 self.assertEqual(len(self.history), 2)
3315
3316 self.assertEqual(self.getJobFromHistory(
3317 'project-bitrot-stable-old').result, 'SUCCESS')
3318 self.assertEqual(self.getJobFromHistory(
3319 'project-bitrot-stable-older').result, 'SUCCESS')
3320
3321 # Stop queuing timer triggered jobs and let any that may have
3322 # queued through so that end of test assertions pass.
3323 self.config.set('zuul', 'layout_config',
3324 'tests/fixtures/layout-no-timer.yaml')
3325 self.sched.reconfigure(self.config)
3326 self.registerJobs()
3327 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003328 # If APScheduler is in mid-event when we remove the job, we
3329 # can end up with one more event firing, so give it an extra
3330 # second to settle.
3331 time.sleep(1)
3332 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003333 self.worker.release('.*')
3334 self.waitUntilSettled()
3335
James E. Blair91e34592015-07-31 16:45:59 -07003336 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003337 "Test that the RPC client can enqueue a change"
3338 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003339 A.addApproval('Code-Review', 2)
3340 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003341
3342 client = zuul.rpcclient.RPCClient('127.0.0.1',
3343 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003344 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003345 r = client.enqueue(tenant='tenant-one',
3346 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003347 project='org/project',
3348 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003349 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003350 self.waitUntilSettled()
3351 self.assertEqual(self.getJobFromHistory('project-merge').result,
3352 'SUCCESS')
3353 self.assertEqual(self.getJobFromHistory('project-test1').result,
3354 'SUCCESS')
3355 self.assertEqual(self.getJobFromHistory('project-test2').result,
3356 'SUCCESS')
3357 self.assertEqual(A.data['status'], 'MERGED')
3358 self.assertEqual(A.reported, 2)
3359 self.assertEqual(r, True)
3360
James E. Blair91e34592015-07-31 16:45:59 -07003361 def test_client_enqueue_ref(self):
3362 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003363 p = "review.example.com/org/project"
3364 upstream = self.getUpstreamRepos([p])
3365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3366 A.setMerged()
3367 A_commit = str(upstream[p].commit('master'))
3368 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003369
3370 client = zuul.rpcclient.RPCClient('127.0.0.1',
3371 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003372 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003373 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003374 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003375 pipeline='post',
3376 project='org/project',
3377 trigger='gerrit',
3378 ref='master',
3379 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003380 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003381 self.waitUntilSettled()
3382 job_names = [x.name for x in self.history]
3383 self.assertEqual(len(self.history), 1)
3384 self.assertIn('project-post', job_names)
3385 self.assertEqual(r, True)
3386
James E. Blairad28e912013-11-27 10:43:22 -08003387 def test_client_enqueue_negative(self):
3388 "Test that the RPC client returns errors"
3389 client = zuul.rpcclient.RPCClient('127.0.0.1',
3390 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003391 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003392 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003393 "Invalid tenant"):
3394 r = client.enqueue(tenant='tenant-foo',
3395 pipeline='gate',
3396 project='org/project',
3397 trigger='gerrit',
3398 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003399 self.assertEqual(r, False)
3400
3401 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003402 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003403 r = client.enqueue(tenant='tenant-one',
3404 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003405 project='project-does-not-exist',
3406 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003407 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003408 self.assertEqual(r, False)
3409
3410 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3411 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003412 r = client.enqueue(tenant='tenant-one',
3413 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003414 project='org/project',
3415 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003416 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003417 self.assertEqual(r, False)
3418
3419 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3420 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003421 r = client.enqueue(tenant='tenant-one',
3422 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003423 project='org/project',
3424 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003425 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003426 self.assertEqual(r, False)
3427
3428 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3429 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003430 r = client.enqueue(tenant='tenant-one',
3431 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003432 project='org/project',
3433 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003434 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003435 self.assertEqual(r, False)
3436
3437 self.waitUntilSettled()
3438 self.assertEqual(len(self.history), 0)
3439 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003440
3441 def test_client_promote(self):
3442 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003443 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3445 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3446 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003447 A.addApproval('Code-Review', 2)
3448 B.addApproval('Code-Review', 2)
3449 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003450
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003451 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3452 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3453 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003454
3455 self.waitUntilSettled()
3456
Paul Belangerbaca3132016-11-04 12:49:54 -04003457 tenant = self.sched.abide.tenants.get('tenant-one')
3458 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003459 enqueue_times = {}
3460 for item in items:
3461 enqueue_times[str(item.change)] = item.enqueue_time
3462
James E. Blair36658cf2013-12-06 17:53:48 -08003463 client = zuul.rpcclient.RPCClient('127.0.0.1',
3464 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003465 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003466 r = client.promote(tenant='tenant-one',
3467 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003468 change_ids=['2,1', '3,1'])
3469
Sean Daguef39b9ca2014-01-10 21:34:35 -05003470 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003471 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003472 for item in items:
3473 self.assertEqual(
3474 enqueue_times[str(item.change)], item.enqueue_time)
3475
James E. Blair78acec92014-02-06 07:11:32 -08003476 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003477 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003478 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003479 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003480 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003481 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003482 self.waitUntilSettled()
3483
3484 self.assertEqual(len(self.builds), 6)
3485 self.assertEqual(self.builds[0].name, 'project-test1')
3486 self.assertEqual(self.builds[1].name, 'project-test2')
3487 self.assertEqual(self.builds[2].name, 'project-test1')
3488 self.assertEqual(self.builds[3].name, 'project-test2')
3489 self.assertEqual(self.builds[4].name, 'project-test1')
3490 self.assertEqual(self.builds[5].name, 'project-test2')
3491
Paul Belangerbaca3132016-11-04 12:49:54 -04003492 self.assertTrue(self.builds[0].hasChanges(B))
3493 self.assertFalse(self.builds[0].hasChanges(A))
3494 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003495
Paul Belangerbaca3132016-11-04 12:49:54 -04003496 self.assertTrue(self.builds[2].hasChanges(B))
3497 self.assertTrue(self.builds[2].hasChanges(C))
3498 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003499
Paul Belangerbaca3132016-11-04 12:49:54 -04003500 self.assertTrue(self.builds[4].hasChanges(B))
3501 self.assertTrue(self.builds[4].hasChanges(C))
3502 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003503
Paul Belanger174a8272017-03-14 13:20:10 -04003504 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003505 self.waitUntilSettled()
3506
3507 self.assertEqual(A.data['status'], 'MERGED')
3508 self.assertEqual(A.reported, 2)
3509 self.assertEqual(B.data['status'], 'MERGED')
3510 self.assertEqual(B.reported, 2)
3511 self.assertEqual(C.data['status'], 'MERGED')
3512 self.assertEqual(C.reported, 2)
3513
James E. Blair36658cf2013-12-06 17:53:48 -08003514 self.assertEqual(r, True)
3515
3516 def test_client_promote_dependent(self):
3517 "Test that the RPC client can promote a dependent change"
3518 # C (depends on B) -> B -> A ; then promote C to get:
3519 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003520 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3522 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3523 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3524
3525 C.setDependsOn(B, 1)
3526
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003527 A.addApproval('Code-Review', 2)
3528 B.addApproval('Code-Review', 2)
3529 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003530
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003531 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3532 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3533 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003534
3535 self.waitUntilSettled()
3536
3537 client = zuul.rpcclient.RPCClient('127.0.0.1',
3538 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003539 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003540 r = client.promote(tenant='tenant-one',
3541 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003542 change_ids=['3,1'])
3543
James E. Blair78acec92014-02-06 07:11:32 -08003544 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003545 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003546 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003547 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003548 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003549 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003550 self.waitUntilSettled()
3551
3552 self.assertEqual(len(self.builds), 6)
3553 self.assertEqual(self.builds[0].name, 'project-test1')
3554 self.assertEqual(self.builds[1].name, 'project-test2')
3555 self.assertEqual(self.builds[2].name, 'project-test1')
3556 self.assertEqual(self.builds[3].name, 'project-test2')
3557 self.assertEqual(self.builds[4].name, 'project-test1')
3558 self.assertEqual(self.builds[5].name, 'project-test2')
3559
Paul Belangerbaca3132016-11-04 12:49:54 -04003560 self.assertTrue(self.builds[0].hasChanges(B))
3561 self.assertFalse(self.builds[0].hasChanges(A))
3562 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003563
Paul Belangerbaca3132016-11-04 12:49:54 -04003564 self.assertTrue(self.builds[2].hasChanges(B))
3565 self.assertTrue(self.builds[2].hasChanges(C))
3566 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003567
Paul Belangerbaca3132016-11-04 12:49:54 -04003568 self.assertTrue(self.builds[4].hasChanges(B))
3569 self.assertTrue(self.builds[4].hasChanges(C))
3570 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003571
Paul Belanger174a8272017-03-14 13:20:10 -04003572 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003573 self.waitUntilSettled()
3574
3575 self.assertEqual(A.data['status'], 'MERGED')
3576 self.assertEqual(A.reported, 2)
3577 self.assertEqual(B.data['status'], 'MERGED')
3578 self.assertEqual(B.reported, 2)
3579 self.assertEqual(C.data['status'], 'MERGED')
3580 self.assertEqual(C.reported, 2)
3581
James E. Blair36658cf2013-12-06 17:53:48 -08003582 self.assertEqual(r, True)
3583
3584 def test_client_promote_negative(self):
3585 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003586 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003587 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003588 A.addApproval('Code-Review', 2)
3589 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003590 self.waitUntilSettled()
3591
3592 client = zuul.rpcclient.RPCClient('127.0.0.1',
3593 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003594 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003595
3596 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003597 r = client.promote(tenant='tenant-one',
3598 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003599 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003600 self.assertEqual(r, False)
3601
3602 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003603 r = client.promote(tenant='tenant-one',
3604 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003605 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003606 self.assertEqual(r, False)
3607
Paul Belanger174a8272017-03-14 13:20:10 -04003608 self.executor_server.hold_jobs_in_build = False
3609 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003610 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003611
James E. Blaircc324192017-04-19 14:36:08 -07003612 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003613 def test_queue_rate_limiting(self):
3614 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003615 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003616 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3617 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3618 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3619
3620 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003621 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003622
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003623 A.addApproval('Code-Review', 2)
3624 B.addApproval('Code-Review', 2)
3625 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003626
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003627 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3628 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3629 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003630 self.waitUntilSettled()
3631
3632 # Only A and B will have their merge jobs queued because
3633 # window is 2.
3634 self.assertEqual(len(self.builds), 2)
3635 self.assertEqual(self.builds[0].name, 'project-merge')
3636 self.assertEqual(self.builds[1].name, 'project-merge')
3637
James E. Blair732ee862017-04-21 07:49:26 -07003638 # Release the merge jobs one at a time.
3639 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003640 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003641 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003642 self.waitUntilSettled()
3643
3644 # Only A and B will have their test jobs queued because
3645 # window is 2.
3646 self.assertEqual(len(self.builds), 4)
3647 self.assertEqual(self.builds[0].name, 'project-test1')
3648 self.assertEqual(self.builds[1].name, 'project-test2')
3649 self.assertEqual(self.builds[2].name, 'project-test1')
3650 self.assertEqual(self.builds[3].name, 'project-test2')
3651
Paul Belanger174a8272017-03-14 13:20:10 -04003652 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003653 self.waitUntilSettled()
3654
James E. Blaircc324192017-04-19 14:36:08 -07003655 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003656 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003657 # A failed so window is reduced by 1 to 1.
3658 self.assertEqual(queue.window, 1)
3659 self.assertEqual(queue.window_floor, 1)
3660 self.assertEqual(A.data['status'], 'NEW')
3661
3662 # Gate is reset and only B's merge job is queued because
3663 # window shrunk to 1.
3664 self.assertEqual(len(self.builds), 1)
3665 self.assertEqual(self.builds[0].name, 'project-merge')
3666
Paul Belanger174a8272017-03-14 13:20:10 -04003667 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003668 self.waitUntilSettled()
3669
3670 # Only B's test jobs are queued because window is still 1.
3671 self.assertEqual(len(self.builds), 2)
3672 self.assertEqual(self.builds[0].name, 'project-test1')
3673 self.assertEqual(self.builds[1].name, 'project-test2')
3674
Paul Belanger174a8272017-03-14 13:20:10 -04003675 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003676 self.waitUntilSettled()
3677
3678 # B was successfully merged so window is increased to 2.
3679 self.assertEqual(queue.window, 2)
3680 self.assertEqual(queue.window_floor, 1)
3681 self.assertEqual(B.data['status'], 'MERGED')
3682
3683 # Only C is left and its merge job is queued.
3684 self.assertEqual(len(self.builds), 1)
3685 self.assertEqual(self.builds[0].name, 'project-merge')
3686
Paul Belanger174a8272017-03-14 13:20:10 -04003687 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003688 self.waitUntilSettled()
3689
3690 # After successful merge job the test jobs for C are queued.
3691 self.assertEqual(len(self.builds), 2)
3692 self.assertEqual(self.builds[0].name, 'project-test1')
3693 self.assertEqual(self.builds[1].name, 'project-test2')
3694
Paul Belanger174a8272017-03-14 13:20:10 -04003695 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003696 self.waitUntilSettled()
3697
3698 # C successfully merged so window is bumped to 3.
3699 self.assertEqual(queue.window, 3)
3700 self.assertEqual(queue.window_floor, 1)
3701 self.assertEqual(C.data['status'], 'MERGED')
3702
James E. Blaircc324192017-04-19 14:36:08 -07003703 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003704 def test_queue_rate_limiting_dependent(self):
3705 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003706 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003707 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3708 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3709 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3710
3711 B.setDependsOn(A, 1)
3712
Paul Belanger174a8272017-03-14 13:20:10 -04003713 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003714
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003715 A.addApproval('Code-Review', 2)
3716 B.addApproval('Code-Review', 2)
3717 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003718
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003719 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3720 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3721 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003722 self.waitUntilSettled()
3723
3724 # Only A and B will have their merge jobs queued because
3725 # window is 2.
3726 self.assertEqual(len(self.builds), 2)
3727 self.assertEqual(self.builds[0].name, 'project-merge')
3728 self.assertEqual(self.builds[1].name, 'project-merge')
3729
James E. Blairf4a5f022017-04-18 14:01:10 -07003730 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003731
3732 # Only A and B will have their test jobs queued because
3733 # window is 2.
3734 self.assertEqual(len(self.builds), 4)
3735 self.assertEqual(self.builds[0].name, 'project-test1')
3736 self.assertEqual(self.builds[1].name, 'project-test2')
3737 self.assertEqual(self.builds[2].name, 'project-test1')
3738 self.assertEqual(self.builds[3].name, 'project-test2')
3739
Paul Belanger174a8272017-03-14 13:20:10 -04003740 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003741 self.waitUntilSettled()
3742
James E. Blaircc324192017-04-19 14:36:08 -07003743 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003744 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003745 # A failed so window is reduced by 1 to 1.
3746 self.assertEqual(queue.window, 1)
3747 self.assertEqual(queue.window_floor, 1)
3748 self.assertEqual(A.data['status'], 'NEW')
3749 self.assertEqual(B.data['status'], 'NEW')
3750
3751 # Gate is reset and only C's merge job is queued because
3752 # window shrunk to 1 and A and B were dequeued.
3753 self.assertEqual(len(self.builds), 1)
3754 self.assertEqual(self.builds[0].name, 'project-merge')
3755
James E. Blairf4a5f022017-04-18 14:01:10 -07003756 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003757
3758 # Only C's test jobs are queued because window is still 1.
3759 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003760 builds = self.getSortedBuilds()
3761 self.assertEqual(builds[0].name, 'project-test1')
3762 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003763
Paul Belanger174a8272017-03-14 13:20:10 -04003764 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003765 self.waitUntilSettled()
3766
3767 # C was successfully merged so window is increased to 2.
3768 self.assertEqual(queue.window, 2)
3769 self.assertEqual(queue.window_floor, 1)
3770 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003771
James E. Blair8fd207b2017-11-30 13:44:38 -08003772 @simple_layout('layouts/reconfigure-window.yaml')
3773 def test_reconfigure_window_shrink(self):
3774 # Test the active window shrinking during reconfiguration
3775 self.executor_server.hold_jobs_in_build = True
3776
3777 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3778 A.addApproval('Code-Review', 2)
3779 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3780 self.waitUntilSettled()
3781 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3782 B.addApproval('Code-Review', 2)
3783 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3784 self.waitUntilSettled()
3785
3786 tenant = self.sched.abide.tenants.get('tenant-one')
3787 queue = tenant.layout.pipelines['gate'].queues[0]
3788 self.assertEqual(queue.window, 20)
3789 self.assertTrue(len(self.builds), 4)
3790
3791 self.executor_server.release('job1')
3792 self.waitUntilSettled()
3793 self.commitConfigUpdate('org/common-config',
3794 'layouts/reconfigure-window2.yaml')
3795 self.sched.reconfigure(self.config)
3796 tenant = self.sched.abide.tenants.get('tenant-one')
3797 queue = tenant.layout.pipelines['gate'].queues[0]
James E. Blair74be3812017-11-30 14:17:25 -08003798 # Even though we have configured a smaller window, the value
3799 # on the existing shared queue should be used.
3800 self.assertEqual(queue.window, 20)
3801 self.assertTrue(len(self.builds), 4)
3802
3803 self.sched.reconfigure(self.config)
3804 tenant = self.sched.abide.tenants.get('tenant-one')
3805 queue = tenant.layout.pipelines['gate'].queues[0]
3806 self.assertEqual(queue.window, 20)
3807 self.assertTrue(len(self.builds), 4)
3808
3809 self.executor_server.hold_jobs_in_build = False
3810 self.executor_server.release()
3811
3812 self.waitUntilSettled()
3813 self.assertHistory([
3814 dict(name='job1', result='SUCCESS', changes='1,1'),
3815 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3816 dict(name='job2', result='SUCCESS', changes='1,1'),
3817 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3818 ], ordered=False)
3819
3820 @simple_layout('layouts/reconfigure-window-fixed.yaml')
3821 def test_reconfigure_window_fixed(self):
3822 # Test the active window shrinking during reconfiguration
3823 self.executor_server.hold_jobs_in_build = True
3824
3825 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3826 A.addApproval('Code-Review', 2)
3827 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3828 self.waitUntilSettled()
3829 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3830 B.addApproval('Code-Review', 2)
3831 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3832 self.waitUntilSettled()
3833
3834 tenant = self.sched.abide.tenants.get('tenant-one')
3835 queue = tenant.layout.pipelines['gate'].queues[0]
3836 self.assertEqual(queue.window, 2)
3837 self.assertTrue(len(self.builds), 4)
3838
3839 self.executor_server.release('job1')
3840 self.waitUntilSettled()
3841 self.commitConfigUpdate('org/common-config',
3842 'layouts/reconfigure-window-fixed2.yaml')
3843 self.sched.reconfigure(self.config)
3844 tenant = self.sched.abide.tenants.get('tenant-one')
3845 queue = tenant.layout.pipelines['gate'].queues[0]
3846 # Because we have configured a static window, it should
3847 # be allowed to shrink on reconfiguration.
James E. Blair8fd207b2017-11-30 13:44:38 -08003848 self.assertEqual(queue.window, 1)
James E. Blair74be3812017-11-30 14:17:25 -08003849 # B is outside the window, but still marked active until the
3850 # next pass through the queue processor, so its builds haven't
3851 # been canceled.
James E. Blair8fd207b2017-11-30 13:44:38 -08003852 self.assertTrue(len(self.builds), 4)
3853
3854 self.sched.reconfigure(self.config)
3855 tenant = self.sched.abide.tenants.get('tenant-one')
3856 queue = tenant.layout.pipelines['gate'].queues[0]
3857 self.assertEqual(queue.window, 1)
3858 # B's builds have been canceled now
3859 self.assertTrue(len(self.builds), 2)
3860
3861 self.executor_server.hold_jobs_in_build = False
3862 self.executor_server.release()
James E. Blair74be3812017-11-30 14:17:25 -08003863
3864 # B's builds will be restarted and will show up in our history
3865 # twice.
James E. Blair8fd207b2017-11-30 13:44:38 -08003866 self.waitUntilSettled()
3867 self.assertHistory([
3868 dict(name='job1', result='SUCCESS', changes='1,1'),
3869 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3870 dict(name='job2', result='SUCCESS', changes='1,1'),
3871 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3872 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3873 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3874 ], ordered=False)
3875
3876 @simple_layout('layouts/reconfigure-remove-add.yaml')
3877 def test_reconfigure_remove_add(self):
3878 # Test removing, then adding a job while in queue
3879 self.executor_server.hold_jobs_in_build = True
3880
3881 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3882 A.addApproval('Code-Review', 2)
3883 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3884 self.waitUntilSettled()
3885 self.assertTrue(len(self.builds), 2)
3886 self.executor_server.release('job2')
3887 self.assertTrue(len(self.builds), 1)
3888
3889 # Remove job2
3890 self.commitConfigUpdate('org/common-config',
3891 'layouts/reconfigure-remove-add2.yaml')
3892 self.sched.reconfigure(self.config)
3893 self.assertTrue(len(self.builds), 1)
3894
3895 # Add job2 back
3896 self.commitConfigUpdate('org/common-config',
3897 'layouts/reconfigure-remove-add.yaml')
3898 self.sched.reconfigure(self.config)
3899 self.assertTrue(len(self.builds), 2)
3900
3901 self.executor_server.hold_jobs_in_build = False
3902 self.executor_server.release()
3903 # This will run new builds for B
3904 self.waitUntilSettled()
3905 self.assertHistory([
3906 dict(name='job2', result='SUCCESS', changes='1,1'),
3907 dict(name='job1', result='SUCCESS', changes='1,1'),
3908 dict(name='job2', result='SUCCESS', changes='1,1'),
3909 ], ordered=False)
3910
Joshua Heskethba8776a2014-01-12 14:35:40 +08003911 def test_worker_update_metadata(self):
3912 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003913 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003914
3915 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003916 A.addApproval('Code-Review', 2)
3917 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003918 self.waitUntilSettled()
3919
Clint Byrumd4de79a2017-05-27 11:39:13 -07003920 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003921
3922 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003923 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003924
3925 start = time.time()
3926 while True:
3927 if time.time() - start > 10:
3928 raise Exception("Timeout waiting for gearman server to report "
3929 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003930 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003931 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003932 break
3933 else:
3934 time.sleep(0)
3935
3936 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003937 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003938
Paul Belanger174a8272017-03-14 13:20:10 -04003939 self.executor_server.hold_jobs_in_build = False
3940 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003941 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003942
James E. Blaircc324192017-04-19 14:36:08 -07003943 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003944 def test_footer_message(self):
3945 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003946 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003947 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003948 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003949 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003950 self.waitUntilSettled()
3951
3952 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003953 B.addApproval('Code-Review', 2)
3954 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003955 self.waitUntilSettled()
3956
3957 self.assertEqual(2, len(self.smtp_messages))
3958
Adam Gandelman82258fd2017-02-07 16:51:58 -08003959 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003960Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003961http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003962
Adam Gandelman82258fd2017-02-07 16:51:58 -08003963 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003964For CI problems and help debugging, contact ci@example.org"""
3965
Adam Gandelman82258fd2017-02-07 16:51:58 -08003966 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3967 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3968 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3969 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003970
Tobias Henkel9842bd72017-05-16 13:40:03 +02003971 @simple_layout('layouts/unmanaged-project.yaml')
3972 def test_unmanaged_project_start_message(self):
3973 "Test start reporting is not done for unmanaged projects."
3974 self.init_repo("org/project", tag='init')
3975 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3976 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3977 self.waitUntilSettled()
3978
3979 self.assertEqual(0, len(A.messages))
3980
Clint Byrumb9de8be2017-05-27 13:49:19 -07003981 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003982 def test_merge_failure_reporters(self):
3983 """Check that the config is set up correctly"""
3984
Clint Byrumb9de8be2017-05-27 13:49:19 -07003985 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003986 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003987 "Merge Failed.\n\nThis change or one of its cross-repo "
3988 "dependencies was unable to be automatically merged with the "
3989 "current state of its repository. Please rebase the change and "
3990 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003991 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003992 self.assertEqual(
3993 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003994 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003995
3996 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003997 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003998 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003999 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11004000
4001 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004002 tenant.layout.pipelines['check'].merge_failure_actions[0],
4003 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11004004
4005 self.assertTrue(
4006 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07004007 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004008 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004009 zuul.driver.smtp.smtpreporter.SMTPReporter) and
4010 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004011 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004012 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11004013 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07004014 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004015 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004016 gerritreporter.GerritReporter) and
4017 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004018 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004019 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11004020 )
4021 )
4022
James E. Blairec056492016-07-22 09:45:56 -07004023 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11004024 def test_merge_failure_reports(self):
4025 """Check that when a change fails to merge the correct message is sent
4026 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08004027 self.updateConfigLayout(
4028 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11004029 self.sched.reconfigure(self.config)
4030 self.registerJobs()
4031
4032 # Check a test failure isn't reported to SMTP
4033 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004034 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04004035 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004036 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11004037 self.waitUntilSettled()
4038
4039 self.assertEqual(3, len(self.history)) # 3 jobs
4040 self.assertEqual(0, len(self.smtp_messages))
4041
4042 # Check a merge failure is reported to SMTP
4043 # B should be merged, but C will conflict with B
4044 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4045 B.addPatchset(['conflict'])
4046 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4047 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004048 B.addApproval('Code-Review', 2)
4049 C.addApproval('Code-Review', 2)
4050 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4051 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11004052 self.waitUntilSettled()
4053
4054 self.assertEqual(6, len(self.history)) # A and B jobs
4055 self.assertEqual(1, len(self.smtp_messages))
4056 self.assertEqual('The merge failed! For more information...',
4057 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11004058
James E. Blairec056492016-07-22 09:45:56 -07004059 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08004060 def test_default_merge_failure_reports(self):
4061 """Check that the default merge failure reports are correct."""
4062
4063 # A should report success, B should report merge failure.
4064 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4065 A.addPatchset(['conflict'])
4066 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4067 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004068 A.addApproval('Code-Review', 2)
4069 B.addApproval('Code-Review', 2)
4070 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4071 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08004072 self.waitUntilSettled()
4073
4074 self.assertEqual(3, len(self.history)) # A jobs
4075 self.assertEqual(A.reported, 2)
4076 self.assertEqual(B.reported, 2)
4077 self.assertEqual(A.data['status'], 'MERGED')
4078 self.assertEqual(B.data['status'], 'NEW')
4079 self.assertIn('Build succeeded', A.messages[1])
4080 self.assertIn('Merge Failed', B.messages[1])
4081 self.assertIn('automatically merged', B.messages[1])
4082 self.assertNotIn('logs.example.com', B.messages[1])
4083 self.assertNotIn('SKIPPED', B.messages[1])
4084
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004085 def test_client_get_running_jobs(self):
4086 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04004087 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004089 A.addApproval('Code-Review', 2)
4090 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004091 self.waitUntilSettled()
4092
4093 client = zuul.rpcclient.RPCClient('127.0.0.1',
4094 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07004095 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004096
4097 # Wait for gearman server to send the initial workData back to zuul
4098 start = time.time()
4099 while True:
4100 if time.time() - start > 10:
4101 raise Exception("Timeout waiting for gearman server to report "
4102 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07004103 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05004104 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004105 break
4106 else:
4107 time.sleep(0)
4108
4109 running_items = client.get_running_jobs()
4110
4111 self.assertEqual(1, len(running_items))
4112 running_item = running_items[0]
4113 self.assertEqual([], running_item['failing_reasons'])
4114 self.assertEqual([], running_item['items_behind'])
James E. Blair0e4c7912018-01-02 14:20:17 -08004115 self.assertEqual('https://review.example.com/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004116 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004117 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004118 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004119 self.assertEqual(True, running_item['active'])
4120 self.assertEqual('1,1', running_item['id'])
4121
4122 self.assertEqual(3, len(running_item['jobs']))
4123 for job in running_item['jobs']:
4124 if job['name'] == 'project-merge':
4125 self.assertEqual('project-merge', job['name'])
4126 self.assertEqual('gate', job['pipeline'])
4127 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05004128 self.assertEqual(
Tristan Cacqueraya4996f12017-09-20 01:00:28 +00004129 'stream.html?uuid={uuid}&logfile=console.log'
Tobias Henkelb4407fc2017-07-07 13:52:56 +02004130 .format(uuid=job['uuid']), job['url'])
4131 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05004132 'finger://{hostname}/{uuid}'.format(
4133 hostname=self.executor_server.hostname,
4134 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02004135 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07004136 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004137 self.assertEqual(False, job['canceled'])
4138 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004139 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004140 self.assertEqual('gate', job['pipeline'])
4141 break
4142
Paul Belanger174a8272017-03-14 13:20:10 -04004143 self.executor_server.hold_jobs_in_build = False
4144 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004145 self.waitUntilSettled()
4146
4147 running_items = client.get_running_jobs()
4148 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07004149
James E. Blairb70e55a2017-04-19 12:57:02 -07004150 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004151 def test_nonvoting_pipeline(self):
4152 "Test that a nonvoting pipeline (experimental) can still report"
4153
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004154 A = self.fake_gerrit.addFakeChange('org/experimental-project',
4155 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004156 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4157 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04004158
4159 self.assertEqual(self.getJobFromHistory('project-merge').result,
4160 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004161 self.assertEqual(
4162 self.getJobFromHistory('experimental-project-test').result,
4163 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004164 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08004165
Jesse Keatingcc67b122017-04-19 14:54:57 -07004166 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004167 def test_disable_at(self):
4168 "Test a pipeline will only report to the disabled trigger when failing"
4169
Jesse Keatingcc67b122017-04-19 14:54:57 -07004170 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004171 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004172 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004173 0, tenant.layout.pipelines['check']._consecutive_failures)
4174 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004175
4176 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4177 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4178 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4179 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4180 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4181 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4182 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4183 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4184 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4185 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4186 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4187
Paul Belanger174a8272017-03-14 13:20:10 -04004188 self.executor_server.failJob('project-test1', A)
4189 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004190 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004191 self.executor_server.failJob('project-test1', D)
4192 self.executor_server.failJob('project-test1', E)
4193 self.executor_server.failJob('project-test1', F)
4194 self.executor_server.failJob('project-test1', G)
4195 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004196 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004197 self.executor_server.failJob('project-test1', J)
4198 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004199
4200 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4201 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4202 self.waitUntilSettled()
4203
4204 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004205 2, tenant.layout.pipelines['check']._consecutive_failures)
4206 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004207
4208 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4209 self.waitUntilSettled()
4210
4211 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004212 0, tenant.layout.pipelines['check']._consecutive_failures)
4213 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004214
4215 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4216 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4217 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4218 self.waitUntilSettled()
4219
4220 # We should be disabled now
4221 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004222 3, tenant.layout.pipelines['check']._consecutive_failures)
4223 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004224
4225 # We need to wait between each of these patches to make sure the
4226 # smtp messages come back in an expected order
4227 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4228 self.waitUntilSettled()
4229 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4230 self.waitUntilSettled()
4231 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4232 self.waitUntilSettled()
4233
4234 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4235 # leaving a message on each change
4236 self.assertEqual(1, len(A.messages))
4237 self.assertIn('Build failed.', A.messages[0])
4238 self.assertEqual(1, len(B.messages))
4239 self.assertIn('Build failed.', B.messages[0])
4240 self.assertEqual(1, len(C.messages))
4241 self.assertIn('Build succeeded.', C.messages[0])
4242 self.assertEqual(1, len(D.messages))
4243 self.assertIn('Build failed.', D.messages[0])
4244 self.assertEqual(1, len(E.messages))
4245 self.assertIn('Build failed.', E.messages[0])
4246 self.assertEqual(1, len(F.messages))
4247 self.assertIn('Build failed.', F.messages[0])
4248
4249 # The last 3 (GHI) would have only reported via smtp.
4250 self.assertEqual(3, len(self.smtp_messages))
4251 self.assertEqual(0, len(G.messages))
4252 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004253 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004254 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004255 self.assertEqual(0, len(H.messages))
4256 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004257 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004258 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004259 self.assertEqual(0, len(I.messages))
4260 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004261 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004262 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004263
4264 # Now reload the configuration (simulate a HUP) to check the pipeline
4265 # comes out of disabled
4266 self.sched.reconfigure(self.config)
4267
Jesse Keatingcc67b122017-04-19 14:54:57 -07004268 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004269
4270 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004271 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004272 0, tenant.layout.pipelines['check']._consecutive_failures)
4273 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004274
4275 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4276 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4277 self.waitUntilSettled()
4278
4279 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004280 2, tenant.layout.pipelines['check']._consecutive_failures)
4281 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004282
4283 # J and K went back to gerrit
4284 self.assertEqual(1, len(J.messages))
4285 self.assertIn('Build failed.', J.messages[0])
4286 self.assertEqual(1, len(K.messages))
4287 self.assertIn('Build failed.', K.messages[0])
4288 # No more messages reported via smtp
4289 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004290
James E. Blaircc020532017-04-19 13:04:44 -07004291 @simple_layout('layouts/one-job-project.yaml')
4292 def test_one_job_project(self):
4293 "Test that queueing works with one job"
4294 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4295 'master', 'A')
4296 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4297 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004298 A.addApproval('Code-Review', 2)
4299 B.addApproval('Code-Review', 2)
4300 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4301 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004302 self.waitUntilSettled()
4303
4304 self.assertEqual(A.data['status'], 'MERGED')
4305 self.assertEqual(A.reported, 2)
4306 self.assertEqual(B.data['status'], 'MERGED')
4307 self.assertEqual(B.reported, 2)
4308
Paul Belanger32023542017-10-06 14:48:50 -04004309 def test_job_aborted(self):
4310 "Test that if a execute server aborts a job, it is run again"
4311 self.executor_server.hold_jobs_in_build = True
4312 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4313 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4314 self.waitUntilSettled()
4315
4316 self.executor_server.release('.*-merge')
4317 self.waitUntilSettled()
4318
4319 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004320
4321 # first abort
4322 self.builds[0].aborted = True
4323 self.executor_server.release('.*-test*')
4324 self.waitUntilSettled()
4325 self.assertEqual(len(self.builds), 1)
4326
4327 # second abort
4328 self.builds[0].aborted = True
4329 self.executor_server.release('.*-test*')
4330 self.waitUntilSettled()
4331 self.assertEqual(len(self.builds), 1)
4332
4333 # third abort
4334 self.builds[0].aborted = True
4335 self.executor_server.release('.*-test*')
4336 self.waitUntilSettled()
4337 self.assertEqual(len(self.builds), 1)
4338
4339 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004340 self.builds[0].aborted = True
4341 self.executor_server.release('.*-test*')
4342 self.waitUntilSettled()
4343 self.assertEqual(len(self.builds), 1)
4344
4345 self.executor_server.hold_jobs_in_build = False
4346 self.executor_server.release()
4347 self.waitUntilSettled()
4348
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004349 self.assertEqual(len(self.history), 7)
4350 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004351 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4352
Paul Belanger71d98172016-11-08 10:56:31 -05004353 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004354 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004355
Paul Belanger174a8272017-03-14 13:20:10 -04004356 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004357 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4358 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4359 self.waitUntilSettled()
4360
Paul Belanger174a8272017-03-14 13:20:10 -04004361 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004362 self.waitUntilSettled()
4363
4364 self.assertEqual(len(self.builds), 2)
4365 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004366 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004367 self.waitUntilSettled()
4368
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004369 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004370 self.assertEqual(len(self.builds), 1,
4371 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004372 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004373 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004374 self.waitUntilSettled()
4375
Paul Belanger174a8272017-03-14 13:20:10 -04004376 self.executor_server.hold_jobs_in_build = False
4377 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004378 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004379 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004380 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4381 self.assertEqual(A.reported, 1)
4382 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004383
James E. Blair15be0e12017-01-03 13:45:20 -08004384 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004385 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004386
4387 self.fake_nodepool.paused = True
4388 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004389 A.addApproval('Code-Review', 2)
4390 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004391 self.waitUntilSettled()
4392
4393 self.zk.client.stop()
4394 self.zk.client.start()
4395 self.fake_nodepool.paused = False
4396 self.waitUntilSettled()
4397
4398 self.assertEqual(A.data['status'], 'MERGED')
4399 self.assertEqual(A.reported, 2)
4400
James E. Blairb4bed1d2018-02-06 13:43:50 -08004401 def test_zookeeper_disconnect2(self):
4402 "Test that jobs are executed after a zookeeper disconnect"
4403
4404 # This tests receiving a ZK disconnect between the arrival of
4405 # a fulfilled request and when we accept its nodes.
4406 self.fake_nodepool.paused = True
4407 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4408 A.addApproval('Code-Review', 2)
4409 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4410 self.waitUntilSettled()
4411
4412 # We're waiting on the nodepool request to complete. Stop the
4413 # scheduler from processing further events, then fulfill the
4414 # nodepool request.
4415 self.sched.run_handler_lock.acquire()
4416
4417 # Fulfill the nodepool request.
4418 self.fake_nodepool.paused = False
4419 requests = list(self.sched.nodepool.requests.values())
4420 self.assertEqual(1, len(requests))
4421 request = requests[0]
4422 for x in iterate_timeout(30, 'fulfill request'):
4423 if request.fulfilled:
4424 break
4425 id1 = request.id
4426
4427 # The request is fulfilled, but the scheduler hasn't processed
4428 # it yet. Reconnect ZK.
4429 self.zk.client.stop()
4430 self.zk.client.start()
4431
4432 # Allow the scheduler to continue and process the (now
4433 # out-of-date) notification that nodes are ready.
4434 self.sched.run_handler_lock.release()
4435
4436 # It should resubmit the request, once it's fulfilled, we can
4437 # wait for it to run jobs and settle.
4438 for x in iterate_timeout(30, 'fulfill request'):
4439 if request.fulfilled:
4440 break
4441 self.waitUntilSettled()
4442
4443 id2 = request.id
4444 self.assertEqual(A.data['status'], 'MERGED')
4445 self.assertEqual(A.reported, 2)
4446 # Make sure it was resubmitted (the id's should be different).
4447 self.assertNotEqual(id1, id2)
4448
James E. Blair6ab79e02017-01-06 10:10:17 -08004449 def test_nodepool_failure(self):
4450 "Test that jobs are reported after a nodepool failure"
4451
4452 self.fake_nodepool.paused = True
4453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004454 A.addApproval('Code-Review', 2)
4455 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004456 self.waitUntilSettled()
4457
4458 req = self.fake_nodepool.getNodeRequests()[0]
4459 self.fake_nodepool.addFailRequest(req)
4460
4461 self.fake_nodepool.paused = False
4462 self.waitUntilSettled()
4463
4464 self.assertEqual(A.data['status'], 'NEW')
4465 self.assertEqual(A.reported, 2)
4466 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4467 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4468 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4469
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004470 def test_nodepool_priority(self):
4471 "Test that nodes are requested at the correct priority"
4472
4473 self.fake_nodepool.paused = True
4474
4475 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4476 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4477
4478 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4479 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4480
4481 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4482 C.addApproval('Code-Review', 2)
4483 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4484
4485 self.waitUntilSettled()
4486
4487 reqs = self.fake_nodepool.getNodeRequests()
4488
4489 # The requests come back sorted by oid. Since we have three requests
4490 # for the three changes each with a different priority.
4491 # Also they get a serial number based on order they were received
4492 # so the number on the endof the oid should map to order submitted.
4493
4494 # * gate first - high priority - change C
4495 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4496 self.assertEqual(reqs[0]['node_types'], ['label1'])
4497 # * check second - normal priority - change B
4498 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4499 self.assertEqual(reqs[1]['node_types'], ['label1'])
4500 # * post third - low priority - change A
4501 # additionally, the post job defined uses an ubuntu-xenial node,
4502 # so we include that check just as an extra verification
4503 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4504 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4505
4506 self.fake_nodepool.paused = False
4507 self.waitUntilSettled()
4508
James E. Blairfceaf412017-09-29 13:44:57 -07004509 @simple_layout('layouts/multiple-templates.yaml')
4510 def test_multiple_project_templates(self):
4511 # Test that applying multiple project templates to a project
4512 # doesn't alter them when used for a second project.
4513 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4514 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4515 self.waitUntilSettled()
4516
4517 build = self.getJobFromHistory('py27')
4518 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4519
James E. Blair29c77002017-10-05 14:56:35 -07004520 def test_pending_merge_in_reconfig(self):
4521 # Test that if we are waiting for an outstanding merge on
4522 # reconfiguration that we continue to do so.
4523 self.gearman_server.hold_merge_jobs_in_queue = True
4524 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4525 A.setMerged()
4526 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4527 self.waitUntilSettled()
4528 # Reconfigure while we still have an outstanding merge job
4529 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4530 None)
4531 self.waitUntilSettled()
4532 # Verify the merge job is still running and that the item is
4533 # in the pipeline
4534 self.assertEqual(len(self.sched.merger.jobs), 1)
4535 tenant = self.sched.abide.tenants.get('tenant-one')
4536 pipeline = tenant.layout.pipelines['post']
4537 self.assertEqual(len(pipeline.getAllItems()), 1)
4538 self.gearman_server.hold_merge_jobs_in_queue = False
4539 self.gearman_server.release()
4540 self.waitUntilSettled()
4541
James E. Blairc32a8352017-10-11 16:27:50 -07004542 @simple_layout('layouts/parent-matchers.yaml')
4543 def test_parent_matchers(self):
4544 "Test that if a job's parent does not match, the job does not run"
4545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4546 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4547 self.waitUntilSettled()
4548
4549 self.assertHistory([])
4550
4551 files = {'foo.txt': ''}
4552 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
4553 files=files)
4554 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4555 self.waitUntilSettled()
4556
4557 files = {'bar.txt': ''}
4558 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C',
4559 files=files)
4560 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4561 self.waitUntilSettled()
4562
4563 files = {'foo.txt': '', 'bar.txt': ''}
4564 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D',
4565 files=files)
4566 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4567 self.waitUntilSettled()
4568
4569 self.assertHistory([
4570 dict(name='child-job', result='SUCCESS', changes='2,1'),
4571 dict(name='child-job', result='SUCCESS', changes='3,1'),
4572 dict(name='child-job', result='SUCCESS', changes='4,1'),
4573 ])
4574
Arieb6f068c2016-10-09 13:11:06 +03004575
James E. Blaira002b032017-04-18 10:35:48 -07004576class TestExecutor(ZuulTestCase):
4577 tenant_config_file = 'config/single-tenant/main.yaml'
4578
4579 def assertFinalState(self):
4580 # In this test, we expect to shut down in a non-final state,
4581 # so skip these checks.
4582 pass
4583
4584 def assertCleanShutdown(self):
4585 self.log.debug("Assert clean shutdown")
4586
4587 # After shutdown, make sure no jobs are running
4588 self.assertEqual({}, self.executor_server.job_workers)
4589
4590 # Make sure that git.Repo objects have been garbage collected.
4591 repos = []
4592 gc.collect()
4593 for obj in gc.get_objects():
4594 if isinstance(obj, git.Repo):
4595 self.log.debug("Leaked git repo object: %s" % repr(obj))
4596 repos.append(obj)
4597 self.assertEqual(len(repos), 0)
4598
4599 def test_executor_shutdown(self):
4600 "Test that the executor can shut down with jobs running"
4601
4602 self.executor_server.hold_jobs_in_build = True
4603 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004604 A.addApproval('Code-Review', 2)
4605 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004606 self.waitUntilSettled()
4607
4608
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004609class TestDependencyGraph(ZuulTestCase):
4610 tenant_config_file = 'config/dependency-graph/main.yaml'
4611
4612 def test_dependeny_graph_dispatch_jobs_once(self):
4613 "Test a job in a dependency graph is queued only once"
4614 # Job dependencies, starting with A
4615 # A
4616 # / \
4617 # B C
4618 # / \ / \
4619 # D F E
4620 # |
4621 # G
4622
4623 self.executor_server.hold_jobs_in_build = True
4624 change = self.fake_gerrit.addFakeChange(
4625 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004626 change.addApproval('Code-Review', 2)
4627 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004628
4629 self.waitUntilSettled()
4630 self.assertEqual([b.name for b in self.builds], ['A'])
4631
4632 self.executor_server.release('A')
4633 self.waitUntilSettled()
4634 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4635
4636 self.executor_server.release('B')
4637 self.waitUntilSettled()
4638 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4639
4640 self.executor_server.release('D')
4641 self.waitUntilSettled()
4642 self.assertEqual([b.name for b in self.builds], ['C'])
4643
4644 self.executor_server.release('C')
4645 self.waitUntilSettled()
4646 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4647
4648 self.executor_server.release('F')
4649 self.waitUntilSettled()
4650 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4651
4652 self.executor_server.release('G')
4653 self.waitUntilSettled()
4654 self.assertEqual([b.name for b in self.builds], ['E'])
4655
4656 self.executor_server.release('E')
4657 self.waitUntilSettled()
4658 self.assertEqual(len(self.builds), 0)
4659
4660 self.executor_server.hold_jobs_in_build = False
4661 self.executor_server.release()
4662 self.waitUntilSettled()
4663
4664 self.assertEqual(len(self.builds), 0)
4665 self.assertEqual(len(self.history), 7)
4666
4667 self.assertEqual(change.data['status'], 'MERGED')
4668 self.assertEqual(change.reported, 2)
4669
4670 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4671 "Test that a job waits till all dependencies are successful"
4672 # Job dependencies, starting with A
4673 # A
4674 # / \
4675 # B C*
4676 # / \ / \
4677 # D F E
4678 # |
4679 # G
4680
4681 self.executor_server.hold_jobs_in_build = True
4682 change = self.fake_gerrit.addFakeChange(
4683 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004684 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004685
4686 self.executor_server.failJob('C', change)
4687
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004688 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004689
4690 self.waitUntilSettled()
4691 self.assertEqual([b.name for b in self.builds], ['A'])
4692
4693 self.executor_server.release('A')
4694 self.waitUntilSettled()
4695 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4696
4697 self.executor_server.release('B')
4698 self.waitUntilSettled()
4699 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4700
4701 self.executor_server.release('D')
4702 self.waitUntilSettled()
4703 self.assertEqual([b.name for b in self.builds], ['C'])
4704
4705 self.executor_server.release('C')
4706 self.waitUntilSettled()
4707 self.assertEqual(len(self.builds), 0)
4708
4709 self.executor_server.hold_jobs_in_build = False
4710 self.executor_server.release()
4711 self.waitUntilSettled()
4712
4713 self.assertEqual(len(self.builds), 0)
4714 self.assertEqual(len(self.history), 4)
4715
4716 self.assertEqual(change.data['status'], 'NEW')
4717 self.assertEqual(change.reported, 2)
4718
4719
Arieb6f068c2016-10-09 13:11:06 +03004720class TestDuplicatePipeline(ZuulTestCase):
4721 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4722
4723 def test_duplicate_pipelines(self):
4724 "Test that a change matching multiple pipelines works"
4725
4726 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4727 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4728 self.waitUntilSettled()
4729
4730 self.assertHistory([
4731 dict(name='project-test1', result='SUCCESS', changes='1,1',
4732 pipeline='dup1'),
4733 dict(name='project-test1', result='SUCCESS', changes='1,1',
4734 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004735 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004736
4737 self.assertEqual(len(A.messages), 2)
4738
Paul Belangera46a3742016-11-09 14:23:03 -05004739 if 'dup1' in A.messages[0]:
4740 self.assertIn('dup1', A.messages[0])
4741 self.assertNotIn('dup2', A.messages[0])
4742 self.assertIn('project-test1', A.messages[0])
4743 self.assertIn('dup2', A.messages[1])
4744 self.assertNotIn('dup1', A.messages[1])
4745 self.assertIn('project-test1', A.messages[1])
4746 else:
4747 self.assertIn('dup1', A.messages[1])
4748 self.assertNotIn('dup2', A.messages[1])
4749 self.assertIn('project-test1', A.messages[1])
4750 self.assertIn('dup2', A.messages[0])
4751 self.assertNotIn('dup1', A.messages[0])
4752 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004753
4754
Paul Belanger86085b32016-11-03 12:48:57 -04004755class TestSchedulerTemplatedProject(ZuulTestCase):
4756 tenant_config_file = 'config/templated-project/main.yaml'
4757
Paul Belanger174a8272017-03-14 13:20:10 -04004758 def test_job_from_templates_executed(self):
4759 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004760
4761 A = self.fake_gerrit.addFakeChange(
4762 'org/templated-project', 'master', 'A')
4763 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4764 self.waitUntilSettled()
4765
4766 self.assertEqual(self.getJobFromHistory('project-test1').result,
4767 'SUCCESS')
4768 self.assertEqual(self.getJobFromHistory('project-test2').result,
4769 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004770
4771 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004772 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004773
4774 A = self.fake_gerrit.addFakeChange(
4775 'org/layered-project', 'master', 'A')
4776 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4777 self.waitUntilSettled()
4778
4779 self.assertEqual(self.getJobFromHistory('project-test1').result,
4780 'SUCCESS')
4781 self.assertEqual(self.getJobFromHistory('project-test2').result,
4782 'SUCCESS')
4783 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4784 ).result, 'SUCCESS')
4785 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4786 ).result, 'SUCCESS')
4787 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4788 ).result, 'SUCCESS')
4789 self.assertEqual(self.getJobFromHistory('project-test6').result,
4790 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004791
James E. Blaire74f5712017-09-29 15:14:31 -07004792 def test_unimplied_branch_matchers(self):
4793 # This tests that there are no implied branch matchers added
4794 # by project templates.
4795 self.create_branch('org/layered-project', 'stable')
4796
4797 A = self.fake_gerrit.addFakeChange(
4798 'org/layered-project', 'stable', 'A')
4799
4800 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4801 self.waitUntilSettled()
4802
4803 self.assertEqual(self.getJobFromHistory('project-test1').result,
4804 'SUCCESS')
4805 print(self.getJobFromHistory('project-test1').
4806 parameters['zuul']['_inheritance_path'])
4807
4808 def test_implied_branch_matchers(self):
4809 # This tests that there is an implied branch matcher when a
4810 # template is used on an in-repo project pipeline definition.
4811 self.create_branch('untrusted-config', 'stable')
4812 self.fake_gerrit.addEvent(
4813 self.fake_gerrit.getFakeBranchCreatedEvent(
4814 'untrusted-config', 'stable'))
4815 self.waitUntilSettled()
4816
4817 A = self.fake_gerrit.addFakeChange(
4818 'untrusted-config', 'stable', 'A')
4819
4820 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4821 self.waitUntilSettled()
4822
4823 self.assertEqual(self.getJobFromHistory('project-test1').result,
4824 'SUCCESS')
4825 print(self.getJobFromHistory('project-test1').
4826 parameters['zuul']['_inheritance_path'])
4827
Adam Gandelman94a60062016-11-21 16:43:14 -08004828
4829class TestSchedulerSuccessURL(ZuulTestCase):
4830 tenant_config_file = 'config/success-url/main.yaml'
4831
4832 def test_success_url(self):
4833 "Ensure bad build params are ignored"
4834 self.sched.reconfigure(self.config)
4835 self.init_repo('org/docs')
4836
4837 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4838 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4839 self.waitUntilSettled()
4840
4841 # Both builds ran: docs-draft-test + docs-draft-test2
4842 self.assertEqual(len(self.history), 2)
4843
4844 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004845 for build in self.history:
4846 if build.name == 'docs-draft-test':
4847 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004848 elif build.name == 'docs-draft-test2':
4849 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004850
4851 # Two msgs: 'Starting...' + results
4852 self.assertEqual(len(self.smtp_messages), 2)
4853 body = self.smtp_messages[1]['body'].splitlines()
4854 self.assertEqual('Build succeeded.', body[0])
4855
4856 self.assertIn(
4857 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4858 'docs-draft-test/%s/publish-docs/' % uuid,
4859 body[2])
4860
Paul Belanger174a8272017-03-14 13:20:10 -04004861 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004862 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004863 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4864 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004865 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004866 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004867
4868
Adam Gandelman4da00f62016-12-09 15:47:33 -08004869class TestSchedulerMerges(ZuulTestCase):
4870 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004871
4872 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004873 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004874 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004875 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004876 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4877 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4878 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004879 A.addApproval('Code-Review', 2)
4880 B.addApproval('Code-Review', 2)
4881 C.addApproval('Code-Review', 2)
4882 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4883 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4884 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004885 self.waitUntilSettled()
4886
4887 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004888 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4889 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004890 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004891 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004892 repo_messages.reverse()
4893
Paul Belanger174a8272017-03-14 13:20:10 -04004894 self.executor_server.hold_jobs_in_build = False
4895 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004896 self.waitUntilSettled()
4897
4898 return repo_messages
4899
4900 def _test_merge(self, mode):
James E. Blairda5bb7e2018-01-22 16:12:17 -08004901 us_path = 'file://' + os.path.join(
Adam Gandelman8bd57102016-12-02 12:58:42 -08004902 self.upstream_root, 'org/project-%s' % mode)
4903 expected_messages = [
4904 'initial commit',
4905 'add content from fixture',
4906 # the intermediate commits order is nondeterministic
4907 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4908 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4909 ]
4910 result = self._test_project_merge_mode(mode)
4911 self.assertEqual(result[:2], expected_messages[:2])
4912 self.assertEqual(result[-2:], expected_messages[-2:])
4913
4914 def test_project_merge_mode_merge(self):
4915 self._test_merge('merge')
4916
4917 def test_project_merge_mode_merge_resolve(self):
4918 self._test_merge('merge-resolve')
4919
4920 def test_project_merge_mode_cherrypick(self):
4921 expected_messages = [
4922 'initial commit',
4923 'add content from fixture',
4924 'A-1',
4925 'B-1',
4926 'C-1']
4927 result = self._test_project_merge_mode('cherry-pick')
4928 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004929
4930 def test_merge_branch(self):
4931 "Test that the right commits are on alternate branches"
4932 self.create_branch('org/project-merge-branches', 'mp')
4933
Paul Belanger174a8272017-03-14 13:20:10 -04004934 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004935 A = self.fake_gerrit.addFakeChange(
4936 'org/project-merge-branches', 'mp', 'A')
4937 B = self.fake_gerrit.addFakeChange(
4938 'org/project-merge-branches', 'mp', 'B')
4939 C = self.fake_gerrit.addFakeChange(
4940 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004941 A.addApproval('Code-Review', 2)
4942 B.addApproval('Code-Review', 2)
4943 C.addApproval('Code-Review', 2)
4944 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4945 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4946 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004947 self.waitUntilSettled()
4948
Paul Belanger174a8272017-03-14 13:20:10 -04004949 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004950 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004951 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004952 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004953 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004954 self.waitUntilSettled()
4955
4956 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004957 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004958 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4959 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004960 repo = git.Repo(path)
4961
James E. Blair247cab72017-07-20 16:52:36 -07004962 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004963 repo_messages.reverse()
4964 correct_messages = [
4965 'initial commit',
4966 'add content from fixture',
4967 'mp commit',
4968 'A-1', 'B-1', 'C-1']
4969 self.assertEqual(repo_messages, correct_messages)
4970
Paul Belanger174a8272017-03-14 13:20:10 -04004971 self.executor_server.hold_jobs_in_build = False
4972 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004973 self.waitUntilSettled()
4974
4975 def test_merge_multi_branch(self):
4976 "Test that dependent changes on multiple branches are merged"
4977 self.create_branch('org/project-merge-branches', 'mp')
4978
Paul Belanger174a8272017-03-14 13:20:10 -04004979 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004980 A = self.fake_gerrit.addFakeChange(
4981 'org/project-merge-branches', 'master', 'A')
4982 B = self.fake_gerrit.addFakeChange(
4983 'org/project-merge-branches', 'mp', 'B')
4984 C = self.fake_gerrit.addFakeChange(
4985 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004986 A.addApproval('Code-Review', 2)
4987 B.addApproval('Code-Review', 2)
4988 C.addApproval('Code-Review', 2)
4989 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4990 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4991 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004992 self.waitUntilSettled()
4993
4994 job_A = None
4995 for job in self.builds:
4996 if 'project-merge' in job.name:
4997 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004998
James E. Blair2a535672017-04-27 12:03:15 -07004999 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5000 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005001 repo = git.Repo(path)
5002 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005003 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005004 repo_messages.reverse()
5005 correct_messages = [
5006 'initial commit', 'add content from fixture', 'A-1']
5007 self.assertEqual(repo_messages, correct_messages)
5008
Paul Belanger174a8272017-03-14 13:20:10 -04005009 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005010 self.waitUntilSettled()
5011
5012 job_B = None
5013 for job in self.builds:
5014 if 'project-merge' in job.name:
5015 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005016
James E. Blair2a535672017-04-27 12:03:15 -07005017 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5018 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005019 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005020 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005021 repo_messages.reverse()
5022 correct_messages = [
5023 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5024 self.assertEqual(repo_messages, correct_messages)
5025
Paul Belanger174a8272017-03-14 13:20:10 -04005026 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005027 self.waitUntilSettled()
5028
5029 job_C = None
5030 for job in self.builds:
5031 if 'project-merge' in job.name:
5032 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005033
James E. Blair2a535672017-04-27 12:03:15 -07005034 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5035 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005036 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005037 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005038
5039 repo_messages.reverse()
5040 correct_messages = [
5041 'initial commit', 'add content from fixture',
5042 'A-1', 'C-1']
5043 # Ensure the right commits are in the history for this ref
5044 self.assertEqual(repo_messages, correct_messages)
5045
Paul Belanger174a8272017-03-14 13:20:10 -04005046 self.executor_server.hold_jobs_in_build = False
5047 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005048 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005049
5050
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005051class TestSemaphore(ZuulTestCase):
5052 tenant_config_file = 'config/semaphore/main.yaml'
5053
5054 def test_semaphore_one(self):
5055 "Test semaphores with max=1 (mutex)"
5056 tenant = self.sched.abide.tenants.get('tenant-one')
5057
5058 self.executor_server.hold_jobs_in_build = True
5059
5060 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5061 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5062 self.assertFalse('test-semaphore' in
5063 tenant.semaphore_handler.semaphores)
5064
5065 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5066 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5067 self.waitUntilSettled()
5068
5069 self.assertEqual(len(self.builds), 3)
5070 self.assertEqual(self.builds[0].name, 'project-test1')
5071 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5072 self.assertEqual(self.builds[2].name, 'project-test1')
5073
5074 self.executor_server.release('semaphore-one-test1')
5075 self.waitUntilSettled()
5076
5077 self.assertEqual(len(self.builds), 3)
5078 self.assertEqual(self.builds[0].name, 'project-test1')
5079 self.assertEqual(self.builds[1].name, 'project-test1')
5080 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5081 self.assertTrue('test-semaphore' in
5082 tenant.semaphore_handler.semaphores)
5083
5084 self.executor_server.release('semaphore-one-test2')
5085 self.waitUntilSettled()
5086
5087 self.assertEqual(len(self.builds), 3)
5088 self.assertEqual(self.builds[0].name, 'project-test1')
5089 self.assertEqual(self.builds[1].name, 'project-test1')
5090 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5091 self.assertTrue('test-semaphore' in
5092 tenant.semaphore_handler.semaphores)
5093
5094 self.executor_server.release('semaphore-one-test1')
5095 self.waitUntilSettled()
5096
5097 self.assertEqual(len(self.builds), 3)
5098 self.assertEqual(self.builds[0].name, 'project-test1')
5099 self.assertEqual(self.builds[1].name, 'project-test1')
5100 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5101 self.assertTrue('test-semaphore' in
5102 tenant.semaphore_handler.semaphores)
5103
5104 self.executor_server.release('semaphore-one-test2')
5105 self.waitUntilSettled()
5106
5107 self.assertEqual(len(self.builds), 2)
5108 self.assertEqual(self.builds[0].name, 'project-test1')
5109 self.assertEqual(self.builds[1].name, 'project-test1')
5110 self.assertFalse('test-semaphore' in
5111 tenant.semaphore_handler.semaphores)
5112
5113 self.executor_server.hold_jobs_in_build = False
5114 self.executor_server.release()
5115
5116 self.waitUntilSettled()
5117 self.assertEqual(len(self.builds), 0)
5118
5119 self.assertEqual(A.reported, 1)
5120 self.assertEqual(B.reported, 1)
5121 self.assertFalse('test-semaphore' in
5122 tenant.semaphore_handler.semaphores)
5123
5124 def test_semaphore_two(self):
5125 "Test semaphores with max>1"
5126 tenant = self.sched.abide.tenants.get('tenant-one')
5127
5128 self.executor_server.hold_jobs_in_build = True
5129 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5130 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5131 self.assertFalse('test-semaphore-two' in
5132 tenant.semaphore_handler.semaphores)
5133
5134 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5135 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5136 self.waitUntilSettled()
5137
5138 self.assertEqual(len(self.builds), 4)
5139 self.assertEqual(self.builds[0].name, 'project-test1')
5140 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5141 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5142 self.assertEqual(self.builds[3].name, 'project-test1')
5143 self.assertTrue('test-semaphore-two' in
5144 tenant.semaphore_handler.semaphores)
5145 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5146 'test-semaphore-two', [])), 2)
5147
5148 self.executor_server.release('semaphore-two-test1')
5149 self.waitUntilSettled()
5150
5151 self.assertEqual(len(self.builds), 4)
5152 self.assertEqual(self.builds[0].name, 'project-test1')
5153 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5154 self.assertEqual(self.builds[2].name, 'project-test1')
5155 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5156 self.assertTrue('test-semaphore-two' in
5157 tenant.semaphore_handler.semaphores)
5158 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5159 'test-semaphore-two', [])), 2)
5160
5161 self.executor_server.release('semaphore-two-test2')
5162 self.waitUntilSettled()
5163
5164 self.assertEqual(len(self.builds), 4)
5165 self.assertEqual(self.builds[0].name, 'project-test1')
5166 self.assertEqual(self.builds[1].name, 'project-test1')
5167 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5168 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5169 self.assertTrue('test-semaphore-two' in
5170 tenant.semaphore_handler.semaphores)
5171 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5172 'test-semaphore-two', [])), 2)
5173
5174 self.executor_server.release('semaphore-two-test1')
5175 self.waitUntilSettled()
5176
5177 self.assertEqual(len(self.builds), 3)
5178 self.assertEqual(self.builds[0].name, 'project-test1')
5179 self.assertEqual(self.builds[1].name, 'project-test1')
5180 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5181 self.assertTrue('test-semaphore-two' in
5182 tenant.semaphore_handler.semaphores)
5183 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5184 'test-semaphore-two', [])), 1)
5185
5186 self.executor_server.release('semaphore-two-test2')
5187 self.waitUntilSettled()
5188
5189 self.assertEqual(len(self.builds), 2)
5190 self.assertEqual(self.builds[0].name, 'project-test1')
5191 self.assertEqual(self.builds[1].name, 'project-test1')
5192 self.assertFalse('test-semaphore-two' in
5193 tenant.semaphore_handler.semaphores)
5194
5195 self.executor_server.hold_jobs_in_build = False
5196 self.executor_server.release()
5197
5198 self.waitUntilSettled()
5199 self.assertEqual(len(self.builds), 0)
5200
5201 self.assertEqual(A.reported, 1)
5202 self.assertEqual(B.reported, 1)
5203
5204 def test_semaphore_abandon(self):
5205 "Test abandon with job semaphores"
5206 self.executor_server.hold_jobs_in_build = True
5207 tenant = self.sched.abide.tenants.get('tenant-one')
5208 check_pipeline = tenant.layout.pipelines['check']
5209
5210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5211 self.assertFalse('test-semaphore' in
5212 tenant.semaphore_handler.semaphores)
5213
5214 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5215 self.waitUntilSettled()
5216
5217 self.assertTrue('test-semaphore' in
5218 tenant.semaphore_handler.semaphores)
5219
5220 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5221 self.waitUntilSettled()
5222
5223 # The check pipeline should be empty
5224 items = check_pipeline.getAllItems()
5225 self.assertEqual(len(items), 0)
5226
5227 # The semaphore should be released
5228 self.assertFalse('test-semaphore' in
5229 tenant.semaphore_handler.semaphores)
5230
5231 self.executor_server.hold_jobs_in_build = False
5232 self.executor_server.release()
5233 self.waitUntilSettled()
5234
Tobias Henkel0f714002017-06-30 23:30:52 +02005235 def test_semaphore_new_patchset(self):
5236 "Test new patchset with job semaphores"
5237 self.executor_server.hold_jobs_in_build = True
5238 tenant = self.sched.abide.tenants.get('tenant-one')
5239 check_pipeline = tenant.layout.pipelines['check']
5240
5241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5242 self.assertFalse('test-semaphore' in
5243 tenant.semaphore_handler.semaphores)
5244
5245 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5246 self.waitUntilSettled()
5247
5248 self.assertTrue('test-semaphore' in
5249 tenant.semaphore_handler.semaphores)
5250 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5251 self.assertEqual(len(semaphore), 1)
5252
5253 A.addPatchset()
5254 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5255 self.waitUntilSettled()
5256
5257 self.assertTrue('test-semaphore' in
5258 tenant.semaphore_handler.semaphores)
5259 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5260 self.assertEqual(len(semaphore), 1)
5261
5262 items = check_pipeline.getAllItems()
5263 self.assertEqual(items[0].change.number, '1')
5264 self.assertEqual(items[0].change.patchset, '2')
5265 self.assertTrue(items[0].live)
5266
5267 self.executor_server.hold_jobs_in_build = False
5268 self.executor_server.release()
5269 self.waitUntilSettled()
5270
5271 # The semaphore should be released
5272 self.assertFalse('test-semaphore' in
5273 tenant.semaphore_handler.semaphores)
5274
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005275 def test_semaphore_reconfigure(self):
5276 "Test reconfigure with job semaphores"
5277 self.executor_server.hold_jobs_in_build = True
5278 tenant = self.sched.abide.tenants.get('tenant-one')
5279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5280 self.assertFalse('test-semaphore' in
5281 tenant.semaphore_handler.semaphores)
5282
5283 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5284 self.waitUntilSettled()
5285
5286 self.assertTrue('test-semaphore' in
5287 tenant.semaphore_handler.semaphores)
5288
5289 # reconfigure without layout change
5290 self.sched.reconfigure(self.config)
5291 self.waitUntilSettled()
5292 tenant = self.sched.abide.tenants.get('tenant-one')
5293
5294 # semaphore still must be held
5295 self.assertTrue('test-semaphore' in
5296 tenant.semaphore_handler.semaphores)
5297
5298 self.commitConfigUpdate(
5299 'common-config',
5300 'config/semaphore/zuul-reconfiguration.yaml')
5301 self.sched.reconfigure(self.config)
5302 self.waitUntilSettled()
5303 tenant = self.sched.abide.tenants.get('tenant-one')
5304
5305 self.executor_server.release('project-test1')
5306 self.waitUntilSettled()
5307
5308 # There should be no builds anymore
5309 self.assertEqual(len(self.builds), 0)
5310
5311 # The semaphore should be released
5312 self.assertFalse('test-semaphore' in
5313 tenant.semaphore_handler.semaphores)
5314
5315
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005316class TestSemaphoreMultiTenant(ZuulTestCase):
5317 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5318
5319 def test_semaphore_tenant_isolation(self):
5320 "Test semaphores in multiple tenants"
5321
5322 self.waitUntilSettled()
5323 tenant_one = self.sched.abide.tenants.get('tenant-one')
5324 tenant_two = self.sched.abide.tenants.get('tenant-two')
5325
5326 self.executor_server.hold_jobs_in_build = True
5327 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5328 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5329 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5330 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5331 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5332 self.assertFalse('test-semaphore' in
5333 tenant_one.semaphore_handler.semaphores)
5334 self.assertFalse('test-semaphore' in
5335 tenant_two.semaphore_handler.semaphores)
5336
5337 # add patches to project1 of tenant-one
5338 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5339 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5340 self.waitUntilSettled()
5341
5342 # one build of project1-test1 must run
5343 # semaphore of tenant-one must be acquired once
5344 # semaphore of tenant-two must not be acquired
5345 self.assertEqual(len(self.builds), 1)
5346 self.assertEqual(self.builds[0].name, 'project1-test1')
5347 self.assertTrue('test-semaphore' in
5348 tenant_one.semaphore_handler.semaphores)
5349 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5350 'test-semaphore', [])), 1)
5351 self.assertFalse('test-semaphore' in
5352 tenant_two.semaphore_handler.semaphores)
5353
5354 # add patches to project2 of tenant-two
5355 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5356 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5357 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5358 self.waitUntilSettled()
5359
5360 # one build of project1-test1 must run
5361 # two builds of project2-test1 must run
5362 # semaphore of tenant-one must be acquired once
5363 # semaphore of tenant-two must be acquired twice
5364 self.assertEqual(len(self.builds), 3)
5365 self.assertEqual(self.builds[0].name, 'project1-test1')
5366 self.assertEqual(self.builds[1].name, 'project2-test1')
5367 self.assertEqual(self.builds[2].name, 'project2-test1')
5368 self.assertTrue('test-semaphore' in
5369 tenant_one.semaphore_handler.semaphores)
5370 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5371 'test-semaphore', [])), 1)
5372 self.assertTrue('test-semaphore' in
5373 tenant_two.semaphore_handler.semaphores)
5374 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5375 'test-semaphore', [])), 2)
5376
5377 self.executor_server.release('project1-test1')
5378 self.waitUntilSettled()
5379
5380 # one build of project1-test1 must run
5381 # two builds of project2-test1 must run
5382 # semaphore of tenant-one must be acquired once
5383 # semaphore of tenant-two must be acquired twice
5384 self.assertEqual(len(self.builds), 3)
5385 self.assertEqual(self.builds[0].name, 'project2-test1')
5386 self.assertEqual(self.builds[1].name, 'project2-test1')
5387 self.assertEqual(self.builds[2].name, 'project1-test1')
5388 self.assertTrue('test-semaphore' in
5389 tenant_one.semaphore_handler.semaphores)
5390 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5391 'test-semaphore', [])), 1)
5392 self.assertTrue('test-semaphore' in
5393 tenant_two.semaphore_handler.semaphores)
5394 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5395 'test-semaphore', [])), 2)
5396
5397 self.executor_server.release('project2-test1')
5398 self.waitUntilSettled()
5399
5400 # one build of project1-test1 must run
5401 # one build of project2-test1 must run
5402 # semaphore of tenant-one must be acquired once
5403 # semaphore of tenant-two must be acquired once
5404 self.assertEqual(len(self.builds), 2)
5405 self.assertTrue('test-semaphore' in
5406 tenant_one.semaphore_handler.semaphores)
5407 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5408 'test-semaphore', [])), 1)
5409 self.assertTrue('test-semaphore' in
5410 tenant_two.semaphore_handler.semaphores)
5411 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5412 'test-semaphore', [])), 1)
5413
5414 self.executor_server.hold_jobs_in_build = False
5415 self.executor_server.release()
5416
5417 self.waitUntilSettled()
5418
5419 # no build must run
5420 # semaphore of tenant-one must not be acquired
5421 # semaphore of tenant-two must not be acquired
5422 self.assertEqual(len(self.builds), 0)
5423 self.assertFalse('test-semaphore' in
5424 tenant_one.semaphore_handler.semaphores)
5425 self.assertFalse('test-semaphore' in
5426 tenant_two.semaphore_handler.semaphores)
5427
5428 self.assertEqual(A.reported, 1)
5429 self.assertEqual(B.reported, 1)
5430
5431
Tobias Henkele9a22512017-12-22 08:36:12 +01005432class TestImplicitProject(ZuulTestCase):
5433 tenant_config_file = 'config/implicit-project/main.yaml'
5434
5435 def test_implicit_project(self):
5436 # config project should work with implicit project name
5437 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
5438 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5439
5440 # untrusted project should work with implicit project name
5441 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5442 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5443
5444 self.waitUntilSettled()
5445
5446 self.assertEqual(A.data['status'], 'NEW')
5447 self.assertEqual(A.reported, 1)
5448 self.assertEqual(B.data['status'], 'NEW')
5449 self.assertEqual(B.reported, 1)
5450 self.assertHistory([
5451 dict(name='test-common', result='SUCCESS', changes='1,1'),
5452 dict(name='test-common', result='SUCCESS', changes='2,1'),
5453 dict(name='test-project', result='SUCCESS', changes='2,1'),
5454 ], ordered=False)
5455
5456 # now test adding a further project in repo
5457 in_repo_conf = textwrap.dedent(
5458 """
5459 - job:
5460 name: test-project
5461 run: playbooks/test-project.yaml
5462 - job:
5463 name: test2-project
5464 run: playbooks/test-project.yaml
5465
5466 - project:
5467 check:
5468 jobs:
5469 - test-project
5470 gate:
5471 jobs:
5472 - test-project
5473
5474 - project:
5475 check:
5476 jobs:
5477 - test2-project
5478 gate:
5479 jobs:
5480 - test2-project
5481
5482 """)
5483 file_dict = {'.zuul.yaml': in_repo_conf}
5484 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5485 files=file_dict)
5486 C.addApproval('Code-Review', 2)
5487 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
5488 self.waitUntilSettled()
5489
5490 # change C must be merged
5491 self.assertEqual(C.data['status'], 'MERGED')
5492 self.assertEqual(C.reported, 2)
5493 self.assertHistory([
5494 dict(name='test-common', result='SUCCESS', changes='1,1'),
5495 dict(name='test-common', result='SUCCESS', changes='2,1'),
5496 dict(name='test-project', result='SUCCESS', changes='2,1'),
5497 dict(name='test-common', result='SUCCESS', changes='3,1'),
5498 dict(name='test-project', result='SUCCESS', changes='3,1'),
5499 dict(name='test2-project', result='SUCCESS', changes='3,1'),
5500 ], ordered=False)
5501
5502
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005503class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005504 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005505 tenant_config_file = 'config/in-repo/main.yaml'
5506
5507 def test_semaphore_in_repo(self):
5508 "Test semaphores in repo config"
5509
5510 # This tests dynamic semaphore handling in project repos. The semaphore
5511 # max value should not be evaluated dynamically but must be updated
5512 # after the change lands.
5513
5514 self.waitUntilSettled()
5515 tenant = self.sched.abide.tenants.get('tenant-one')
5516
5517 in_repo_conf = textwrap.dedent(
5518 """
5519 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005520 name: project-test1
5521
5522 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005523 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -07005524 run: playbooks/project-test2.yaml
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005525 semaphore: test-semaphore
5526
5527 - project:
5528 name: org/project
5529 tenant-one-gate:
5530 jobs:
5531 - project-test2
5532
5533 # the max value in dynamic layout must be ignored
5534 - semaphore:
5535 name: test-semaphore
5536 max: 2
5537 """)
5538
5539 in_repo_playbook = textwrap.dedent(
5540 """
5541 - hosts: all
5542 tasks: []
5543 """)
5544
5545 file_dict = {'.zuul.yaml': in_repo_conf,
5546 'playbooks/project-test2.yaml': in_repo_playbook}
5547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5548 files=file_dict)
5549 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5550 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5551 B.setDependsOn(A, 1)
5552 C.setDependsOn(A, 1)
5553
5554 self.executor_server.hold_jobs_in_build = True
5555
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005556 A.addApproval('Code-Review', 2)
5557 B.addApproval('Code-Review', 2)
5558 C.addApproval('Code-Review', 2)
5559 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5560 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5561 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005562 self.waitUntilSettled()
5563
5564 # check that the layout in a queue item still has max value of 1
5565 # for test-semaphore
5566 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5567 queue = None
5568 for queue_candidate in pipeline.queues:
5569 if queue_candidate.name == 'org/project':
5570 queue = queue_candidate
5571 break
5572 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005573 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005574 dynamic_test_semaphore = \
5575 item_dynamic_layout.semaphores.get('test-semaphore')
5576 self.assertEqual(dynamic_test_semaphore.max, 1)
5577
5578 # one build must be in queue, one semaphores acquired
5579 self.assertEqual(len(self.builds), 1)
5580 self.assertEqual(self.builds[0].name, 'project-test2')
5581 self.assertTrue('test-semaphore' in
5582 tenant.semaphore_handler.semaphores)
5583 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5584 'test-semaphore', [])), 1)
5585
5586 self.executor_server.release('project-test2')
5587 self.waitUntilSettled()
5588
5589 # change A must be merged
5590 self.assertEqual(A.data['status'], 'MERGED')
5591 self.assertEqual(A.reported, 2)
5592
5593 # send change-merged event as the gerrit mock doesn't send it
5594 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5595 self.waitUntilSettled()
5596
5597 # now that change A was merged, the new semaphore max must be effective
5598 tenant = self.sched.abide.tenants.get('tenant-one')
5599 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5600
5601 # two builds must be in queue, two semaphores acquired
5602 self.assertEqual(len(self.builds), 2)
5603 self.assertEqual(self.builds[0].name, 'project-test2')
5604 self.assertEqual(self.builds[1].name, 'project-test2')
5605 self.assertTrue('test-semaphore' in
5606 tenant.semaphore_handler.semaphores)
5607 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5608 'test-semaphore', [])), 2)
5609
5610 self.executor_server.release('project-test2')
5611 self.waitUntilSettled()
5612
5613 self.assertEqual(len(self.builds), 0)
5614 self.assertFalse('test-semaphore' in
5615 tenant.semaphore_handler.semaphores)
5616
5617 self.executor_server.hold_jobs_in_build = False
5618 self.executor_server.release()
5619
5620 self.waitUntilSettled()
5621 self.assertEqual(len(self.builds), 0)
5622
5623 self.assertEqual(A.reported, 2)
5624 self.assertEqual(B.reported, 2)
5625 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005626
5627
5628class TestSchedulerBranchMatcher(ZuulTestCase):
5629
5630 @simple_layout('layouts/matcher-test.yaml')
5631 def test_job_branch_ignored(self):
5632 '''
5633 Test that branch matching logic works.
5634
5635 The 'ignore-branch' job has a branch matcher that is supposed to
5636 match every branch except for the 'featureA' branch, so it should
5637 not be run on a change to that branch.
5638 '''
5639 self.create_branch('org/project', 'featureA')
5640 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5641 A.addApproval('Code-Review', 2)
5642 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5643 self.waitUntilSettled()
5644 self.printHistory()
5645 self.assertEqual(self.getJobFromHistory('project-test1').result,
5646 'SUCCESS')
5647 self.assertJobNotInHistory('ignore-branch')
5648 self.assertEqual(A.data['status'], 'MERGED')
5649 self.assertEqual(A.reported, 2,
5650 "A should report start and success")
5651 self.assertIn('gate', A.messages[1],
5652 "A should transit gate")