blob: 3d76510105385973d324b2fe92590412d5d10988 [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. Blairb0fcae42012-07-17 11:12:10 -070022import re
James E. Blair4886cc12012-07-18 15:39:41 -070023import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040024import time
Clint Byrum03454a52017-05-26 17:14:02 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
Monty Taylorb934c1a2017-06-16 19:31:47 -050029import urllib
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
Clint Byrumb9de8be2017-05-27 13:49:19 -070032from zuul.driver.gerrit import gerritreporter
James E. Blairb0fcae42012-07-17 11:12:10 -070033import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080034import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040035import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070036
Maru Newby3fe5f852015-01-13 04:22:14 +000037from tests.base import (
Paul Belanger0a21f0a2017-06-13 13:14:42 -040038 SSLZuulTestCase,
Maru Newby3fe5f852015-01-13 04:22:14 +000039 ZuulTestCase,
40 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070041 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000042)
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044
Paul Belanger0a21f0a2017-06-13 13:14:42 -040045class TestSchedulerSSL(SSLZuulTestCase):
46 tenant_config_file = 'config/single-tenant/main.yaml'
47
48 def test_jobs_executed(self):
49 "Test that jobs are executed and a change is merged"
Paul Belanger0a21f0a2017-06-13 13:14:42 -040050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020051 A.addApproval('Code-Review', 2)
52 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Paul Belanger0a21f0a2017-06-13 13:14:42 -040053 self.waitUntilSettled()
54 self.assertEqual(self.getJobFromHistory('project-merge').result,
55 'SUCCESS')
56 self.assertEqual(self.getJobFromHistory('project-test1').result,
57 'SUCCESS')
58 self.assertEqual(self.getJobFromHistory('project-test2').result,
59 'SUCCESS')
60 self.assertEqual(A.data['status'], 'MERGED')
61 self.assertEqual(A.reported, 2)
62 self.assertEqual(self.getJobFromHistory('project-test1').node,
63 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070064 self.assertEqual(self.getJobFromHistory('project-test2').node,
65 'label1')
Paul Belanger0a21f0a2017-06-13 13:14:42 -040066
67
Clark Boylanb640e052014-04-03 16:41:46 -070068class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070069 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010070
Paul Belanger174a8272017-03-14 13:20:10 -040071 def test_jobs_executed(self):
72 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070073
James E. Blairb0fcae42012-07-17 11:12:10 -070074 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020075 A.addApproval('Code-Review', 2)
76 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070077 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040078 self.assertEqual(self.getJobFromHistory('project-merge').result,
79 'SUCCESS')
80 self.assertEqual(self.getJobFromHistory('project-test1').result,
81 'SUCCESS')
82 self.assertEqual(self.getJobFromHistory('project-test2').result,
83 'SUCCESS')
84 self.assertEqual(A.data['status'], 'MERGED')
85 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070086 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070087 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070088 self.assertEqual(self.getJobFromHistory('project-test2').node,
89 'label1')
James E. Blairb0fcae42012-07-17 11:12:10 -070090
James E. Blair80ac1582017-10-09 07:02:40 -070091 for stat in self.statsd.stats:
92 k, v = stat.decode('utf-8').split(':')
93 self.log.debug('stat %s:%s', k, v)
James E. Blair552b54f2016-07-22 13:55:32 -070094 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050095 # Per-driver
96 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
97 # Per-driver per-connection
98 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
99 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700100 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700101 'zuul.tenant.tenant-one.pipeline.gate.current_changes',
102 value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700103 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700104 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
105 'org_project.master.job.project-merge.SUCCESS', kind='ms')
106 self.assertReportedStat(
107 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
108 'org_project.master.job.project-merge.SUCCESS', value='1|c')
109 self.assertReportedStat(
110 'zuul.tenant.tenant-one.pipeline.gate.resident_time', kind='ms')
111 self.assertReportedStat(
112 'zuul.tenant.tenant-one.pipeline.gate.total_changes', value='1|c')
113 self.assertReportedStat(
114 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
115 'org_project.master.resident_time', kind='ms')
116 self.assertReportedStat(
117 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
118 'org_project.master.total_changes', value='1|c')
James E. Blairfaf81982017-10-10 15:42:26 -0700119 exec_key = 'zuul.executor.%s' % self.executor_server.hostname
120 self.assertReportedStat(exec_key + '.builds', value='1|c')
James E. Blair4f1731b2017-10-10 18:11:42 -0700121 self.assertReportedStat('zuul.nodepool.requested', value='1|c')
122 self.assertReportedStat('zuul.nodepool.requested.label.label1',
123 value='1|c')
124 self.assertReportedStat('zuul.nodepool.fulfilled.label.label1',
125 value='1|c')
126 self.assertReportedStat('zuul.nodepool.requested.size.1', value='1|c')
127 self.assertReportedStat('zuul.nodepool.fulfilled.size.1', value='1|c')
128 self.assertReportedStat('zuul.nodepool.current_requests', value='1|g')
James E. Blairbdd50e62017-10-21 08:18:55 -0700129 self.assertReportedStat('zuul.executors.online', value='1|g')
130 self.assertReportedStat('zuul.executors.accepting', value='1|g')
131 self.assertReportedStat('zuul.mergers.online', value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700132
James E. Blair2ef29e92017-07-21 15:25:05 -0700133 for build in self.history:
134 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700135
James E. Blair3cb10702013-08-24 08:56:03 -0700136 def test_initial_pipeline_gauges(self):
137 "Test that each pipeline reported its length on start"
James E. Blair57bf14d2017-10-21 09:09:48 -0700138 self.assertReportedStat('zuul.tenant.tenant-one.pipeline.gate.'
139 'current_changes',
James E. Blair7da51b72016-07-25 15:29:43 -0700140 value='0|g')
James E. Blair57bf14d2017-10-21 09:09:48 -0700141 self.assertReportedStat('zuul.tenant.tenant-one.pipeline.check.'
142 'current_changes',
James E. Blair7da51b72016-07-25 15:29:43 -0700143 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700144
James E. Blair34776ee2016-08-25 13:53:54 -0700145 def test_job_branch(self):
146 "Test the correct variant of a job runs on a branch"
147 self.create_branch('org/project', 'stable')
148 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200149 A.addApproval('Code-Review', 2)
150 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700151 self.waitUntilSettled()
152 self.assertEqual(self.getJobFromHistory('project-test1').result,
153 'SUCCESS')
154 self.assertEqual(self.getJobFromHistory('project-test2').result,
155 'SUCCESS')
156 self.assertEqual(A.data['status'], 'MERGED')
157 self.assertEqual(A.reported, 2,
158 "A should report start and success")
159 self.assertIn('gate', A.messages[1],
160 "A should transit gate")
161 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700162 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700163
James E. Blairda5bb7e2018-01-22 16:12:17 -0800164 @simple_layout('layouts/branch-deletion.yaml')
165 def test_branch_deletion(self):
166 "Test the correct variant of a job runs on a branch"
167 self._startMerger()
168 for f in list(self.executor_server.merger_worker.functions.keys()):
169 f = str(f)
170 if f.startswith('merger:'):
171 self.executor_server.merger_worker.unRegisterFunction(f)
172
173 self.create_branch('org/project', 'stable')
174 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
175 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
176 self.waitUntilSettled()
177 self.assertEqual(self.getJobFromHistory('project-test2').result,
178 'SUCCESS')
179
180 self.delete_branch('org/project', 'stable')
181 path = os.path.join(self.executor_src_root, 'review.example.com')
182 shutil.rmtree(path)
183
184 self.executor_server.hold_jobs_in_build = True
185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
186 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
187 self.waitUntilSettled()
188 build = self.builds[0]
189
190 # Make sure there is no stable branch in the checked out git repo.
191 pname = 'review.example.com/org/project'
192 work = build.getWorkspaceRepos([pname])
193 work = work[pname]
194 heads = set([str(x) for x in work.heads])
195 self.assertEqual(heads, set(['master']))
196 self.executor_server.hold_jobs_in_build = False
197 build.release()
198 self.waitUntilSettled()
199 self.assertEqual(self.getJobFromHistory('project-test1').result,
200 'SUCCESS')
201
James E. Blairb0fcae42012-07-17 11:12:10 -0700202 def test_parallel_changes(self):
203 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700204
Paul Belanger174a8272017-03-14 13:20:10 -0400205 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700206 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
207 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
208 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200209 A.addApproval('Code-Review', 2)
210 B.addApproval('Code-Review', 2)
211 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700212
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200213 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
214 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
215 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700216
217 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400218 self.assertEqual(len(self.builds), 1)
219 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700220 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700221
Paul Belanger174a8272017-03-14 13:20:10 -0400222 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700223 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400224 self.assertEqual(len(self.builds), 3)
225 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700226 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400227 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700228 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400229 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700230 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700231
Paul Belanger174a8272017-03-14 13:20:10 -0400232 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700233 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400234 self.assertEqual(len(self.builds), 5)
235 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700236 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400237 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700238 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700239
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400240 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700241 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400242 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700243 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700244
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400245 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700246 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700247
Paul Belanger174a8272017-03-14 13:20:10 -0400248 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700249 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400250 self.assertEqual(len(self.builds), 6)
251 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700252 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400253 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700254 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700255
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400256 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700257 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400258 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700259 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700260
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400261 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700262 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400263 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700264 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700265
Paul Belanger174a8272017-03-14 13:20:10 -0400266 self.executor_server.hold_jobs_in_build = False
267 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700268 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400269 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700270
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400271 self.assertEqual(len(self.history), 9)
272 self.assertEqual(A.data['status'], 'MERGED')
273 self.assertEqual(B.data['status'], 'MERGED')
274 self.assertEqual(C.data['status'], 'MERGED')
275 self.assertEqual(A.reported, 2)
276 self.assertEqual(B.reported, 2)
277 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700278
279 def test_failed_changes(self):
280 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400281 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700282
James E. Blairb02a3bb2012-07-30 17:49:55 -0700283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
284 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200285 A.addApproval('Code-Review', 2)
286 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700287
Paul Belanger174a8272017-03-14 13:20:10 -0400288 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700289
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200290 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
291 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700292 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700293 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400294
Paul Belanger174a8272017-03-14 13:20:10 -0400295 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400296 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700297 # A/project-merge is complete
298 self.assertBuilds([
299 dict(name='project-test1', changes='1,1'),
300 dict(name='project-test2', changes='1,1'),
301 dict(name='project-merge', changes='1,1 2,1'),
302 ])
James E. Blaire2819012013-06-28 17:17:26 -0400303
Paul Belanger174a8272017-03-14 13:20:10 -0400304 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400305 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700306 # A/project-merge is complete
307 # B/project-merge is complete
308 self.assertBuilds([
309 dict(name='project-test1', changes='1,1'),
310 dict(name='project-test2', changes='1,1'),
311 dict(name='project-test1', changes='1,1 2,1'),
312 dict(name='project-test2', changes='1,1 2,1'),
313 ])
314
315 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400316 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700317 self.builds[0].release()
318 self.waitUntilSettled()
319
320 self.orderedRelease()
321 self.assertHistory([
322 dict(name='project-merge', result='SUCCESS', changes='1,1'),
323 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
324 dict(name='project-test1', result='FAILURE', changes='1,1'),
325 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
326 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
327 dict(name='project-test2', result='SUCCESS', changes='1,1'),
328 dict(name='project-merge', result='SUCCESS', changes='2,1'),
329 dict(name='project-test1', result='SUCCESS', changes='2,1'),
330 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700331 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700332
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400333 self.assertEqual(A.data['status'], 'NEW')
334 self.assertEqual(B.data['status'], 'MERGED')
335 self.assertEqual(A.reported, 2)
336 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700337
338 def test_independent_queues(self):
339 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700340
Paul Belanger174a8272017-03-14 13:20:10 -0400341 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900342 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700343 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
344 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200345 A.addApproval('Code-Review', 2)
346 B.addApproval('Code-Review', 2)
347 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700348
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200349 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800350 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700351
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200352 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800353 self.waitUntilSettled()
354
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200355 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700356 self.waitUntilSettled()
357
358 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700359 self.assertBuilds([
360 dict(name='project-merge', changes='1,1'),
361 dict(name='project-merge', changes='2,1'),
362 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700363
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700364 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700365 self.builds[0].release()
366 self.waitUntilSettled()
367 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700368 self.waitUntilSettled()
369 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400370 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700371 self.waitUntilSettled()
372
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700373 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700374 self.assertBuilds([
375 dict(name='project-test1', changes='1,1'),
376 dict(name='project-test2', changes='1,1'),
377 dict(name='project-test1', changes='2,1'),
378 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500379 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700380 dict(name='project-test1', changes='2,1 3,1'),
381 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800382 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700383 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700384
James E. Blair5f0d49e2016-08-19 09:36:50 -0700385 self.orderedRelease()
386 self.assertHistory([
387 dict(name='project-merge', result='SUCCESS', changes='1,1'),
388 dict(name='project-merge', result='SUCCESS', changes='2,1'),
389 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
390 dict(name='project-test1', result='SUCCESS', changes='1,1'),
391 dict(name='project-test2', result='SUCCESS', changes='1,1'),
392 dict(name='project-test1', result='SUCCESS', changes='2,1'),
393 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500394 dict(
395 name='project1-project2-integration',
396 result='SUCCESS',
397 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700398 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
399 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800400 dict(name='project1-project2-integration',
401 result='SUCCESS',
402 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700403 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700404
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400405 self.assertEqual(A.data['status'], 'MERGED')
406 self.assertEqual(B.data['status'], 'MERGED')
407 self.assertEqual(C.data['status'], 'MERGED')
408 self.assertEqual(A.reported, 2)
409 self.assertEqual(B.reported, 2)
410 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700411
412 def test_failed_change_at_head(self):
413 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700414
Paul Belanger174a8272017-03-14 13:20:10 -0400415 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
417 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
418 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200419 A.addApproval('Code-Review', 2)
420 B.addApproval('Code-Review', 2)
421 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700422
Paul Belanger174a8272017-03-14 13:20:10 -0400423 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700424
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200425 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
426 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
427 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700428
429 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700430
James E. Blairb536ecc2016-08-31 10:11:42 -0700431 self.assertBuilds([
432 dict(name='project-merge', changes='1,1'),
433 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700434
Paul Belanger174a8272017-03-14 13:20:10 -0400435 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700436 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400437 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700438 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400439 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700440 self.waitUntilSettled()
441
James E. Blairb536ecc2016-08-31 10:11:42 -0700442 self.assertBuilds([
443 dict(name='project-test1', changes='1,1'),
444 dict(name='project-test2', changes='1,1'),
445 dict(name='project-test1', changes='1,1 2,1'),
446 dict(name='project-test2', changes='1,1 2,1'),
447 dict(name='project-test1', changes='1,1 2,1 3,1'),
448 dict(name='project-test2', changes='1,1 2,1 3,1'),
449 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700450
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400451 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700452 self.waitUntilSettled()
453
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400454 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700455 self.assertBuilds([
456 dict(name='project-test2', changes='1,1'),
457 dict(name='project-merge', changes='2,1'),
458 ])
459 # Unordered history comparison because the aborts can finish
460 # in any order.
461 self.assertHistory([
462 dict(name='project-merge', result='SUCCESS',
463 changes='1,1'),
464 dict(name='project-merge', result='SUCCESS',
465 changes='1,1 2,1'),
466 dict(name='project-merge', result='SUCCESS',
467 changes='1,1 2,1 3,1'),
468 dict(name='project-test1', result='FAILURE',
469 changes='1,1'),
470 dict(name='project-test1', result='ABORTED',
471 changes='1,1 2,1'),
472 dict(name='project-test2', result='ABORTED',
473 changes='1,1 2,1'),
474 dict(name='project-test1', result='ABORTED',
475 changes='1,1 2,1 3,1'),
476 dict(name='project-test2', result='ABORTED',
477 changes='1,1 2,1 3,1'),
478 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700479
Paul Belanger174a8272017-03-14 13:20:10 -0400480 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700481 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400482 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700483 self.waitUntilSettled()
484 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700485
James E. Blairb536ecc2016-08-31 10:11:42 -0700486 self.assertBuilds([])
487 self.assertHistory([
488 dict(name='project-merge', result='SUCCESS',
489 changes='1,1'),
490 dict(name='project-merge', result='SUCCESS',
491 changes='1,1 2,1'),
492 dict(name='project-merge', result='SUCCESS',
493 changes='1,1 2,1 3,1'),
494 dict(name='project-test1', result='FAILURE',
495 changes='1,1'),
496 dict(name='project-test1', result='ABORTED',
497 changes='1,1 2,1'),
498 dict(name='project-test2', result='ABORTED',
499 changes='1,1 2,1'),
500 dict(name='project-test1', result='ABORTED',
501 changes='1,1 2,1 3,1'),
502 dict(name='project-test2', result='ABORTED',
503 changes='1,1 2,1 3,1'),
504 dict(name='project-merge', result='SUCCESS',
505 changes='2,1'),
506 dict(name='project-merge', result='SUCCESS',
507 changes='2,1 3,1'),
508 dict(name='project-test2', result='SUCCESS',
509 changes='1,1'),
510 dict(name='project-test1', result='SUCCESS',
511 changes='2,1'),
512 dict(name='project-test2', result='SUCCESS',
513 changes='2,1'),
514 dict(name='project-test1', result='SUCCESS',
515 changes='2,1 3,1'),
516 dict(name='project-test2', result='SUCCESS',
517 changes='2,1 3,1'),
518 ], ordered=False)
519
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400520 self.assertEqual(A.data['status'], 'NEW')
521 self.assertEqual(B.data['status'], 'MERGED')
522 self.assertEqual(C.data['status'], 'MERGED')
523 self.assertEqual(A.reported, 2)
524 self.assertEqual(B.reported, 2)
525 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700526
James E. Blair0aac4872013-08-23 14:02:38 -0700527 def test_failed_change_in_middle(self):
528 "Test a failed change in the middle of the queue"
529
Paul Belanger174a8272017-03-14 13:20:10 -0400530 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700531 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
532 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
533 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200534 A.addApproval('Code-Review', 2)
535 B.addApproval('Code-Review', 2)
536 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700537
Paul Belanger174a8272017-03-14 13:20:10 -0400538 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700539
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200540 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
541 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
542 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700543
544 self.waitUntilSettled()
545
Paul Belanger174a8272017-03-14 13:20:10 -0400546 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700547 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400548 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700549 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400550 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700551 self.waitUntilSettled()
552
553 self.assertEqual(len(self.builds), 6)
554 self.assertEqual(self.builds[0].name, 'project-test1')
555 self.assertEqual(self.builds[1].name, 'project-test2')
556 self.assertEqual(self.builds[2].name, 'project-test1')
557 self.assertEqual(self.builds[3].name, 'project-test2')
558 self.assertEqual(self.builds[4].name, 'project-test1')
559 self.assertEqual(self.builds[5].name, 'project-test2')
560
561 self.release(self.builds[2])
562 self.waitUntilSettled()
563
James E. Blair972e3c72013-08-29 12:04:55 -0700564 # project-test1 and project-test2 for A
565 # project-test2 for B
566 # project-merge for C (without B)
567 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700568 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
569
Paul Belanger174a8272017-03-14 13:20:10 -0400570 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700571 self.waitUntilSettled()
572
573 # project-test1 and project-test2 for A
574 # project-test2 for B
575 # project-test1 and project-test2 for C
576 self.assertEqual(len(self.builds), 5)
577
Paul Belanger2e2a0492016-10-30 18:33:37 -0400578 tenant = self.sched.abide.tenants.get('tenant-one')
579 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700580 builds = items[0].current_build_set.getBuilds()
581 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
582 self.assertEqual(self.countJobResults(builds, None), 2)
583 builds = items[1].current_build_set.getBuilds()
584 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
585 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
586 self.assertEqual(self.countJobResults(builds, None), 1)
587 builds = items[2].current_build_set.getBuilds()
588 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700589 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700590
Paul Belanger174a8272017-03-14 13:20:10 -0400591 self.executor_server.hold_jobs_in_build = False
592 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700593 self.waitUntilSettled()
594
595 self.assertEqual(len(self.builds), 0)
596 self.assertEqual(len(self.history), 12)
597 self.assertEqual(A.data['status'], 'MERGED')
598 self.assertEqual(B.data['status'], 'NEW')
599 self.assertEqual(C.data['status'], 'MERGED')
600 self.assertEqual(A.reported, 2)
601 self.assertEqual(B.reported, 2)
602 self.assertEqual(C.reported, 2)
603
James E. Blaird466dc42012-07-31 10:42:56 -0700604 def test_failed_change_at_head_with_queue(self):
605 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700606
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700607 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
609 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
610 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200611 A.addApproval('Code-Review', 2)
612 B.addApproval('Code-Review', 2)
613 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700614
Paul Belanger174a8272017-03-14 13:20:10 -0400615 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700616
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200617 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
618 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
619 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700620
621 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700622 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400623 self.assertEqual(len(self.builds), 0)
624 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700625 self.assertEqual(queue[0].name, b'executor:execute')
626 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800627 self.assertEqual(job_args['job'], 'project-merge')
628 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700629
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700630 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700631 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700632 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700633 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700634 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700635 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700636 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700637
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400638 self.assertEqual(len(self.builds), 0)
639 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800640
641 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700642 json.loads(queue[0].arguments.decode('utf8'))['job'],
643 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800644 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700645 json.loads(queue[1].arguments.decode('utf8'))['job'],
646 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800647 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700648 json.loads(queue[2].arguments.decode('utf8'))['job'],
649 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800650 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700651 json.loads(queue[3].arguments.decode('utf8'))['job'],
652 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800653 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700654 json.loads(queue[4].arguments.decode('utf8'))['job'],
655 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800656 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700657 json.loads(queue[5].arguments.decode('utf8'))['job'],
658 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700659
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700660 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700661 self.waitUntilSettled()
662
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400663 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700664 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400665 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
666 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700667
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700668 self.gearman_server.hold_jobs_in_queue = False
669 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700670 self.waitUntilSettled()
671
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400672 self.assertEqual(len(self.builds), 0)
673 self.assertEqual(len(self.history), 11)
674 self.assertEqual(A.data['status'], 'NEW')
675 self.assertEqual(B.data['status'], 'MERGED')
676 self.assertEqual(C.data['status'], 'MERGED')
677 self.assertEqual(A.reported, 2)
678 self.assertEqual(B.reported, 2)
679 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700680
James E. Blairce8a2132016-05-19 15:21:52 -0700681 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400682 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200684 A.addApproval('Code-Review', 2)
685 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700686 self.waitUntilSettled()
687 time.sleep(2)
688
James E. Blairae0f23c2017-09-13 10:55:15 -0600689 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700690 found_job = None
691 for pipeline in data['pipelines']:
692 if pipeline['name'] != 'gate':
693 continue
694 for queue in pipeline['change_queues']:
695 for head in queue['heads']:
696 for item in head:
697 for job in item['jobs']:
698 if job['name'] == 'project-merge':
699 found_job = job
700 break
701
702 self.assertIsNotNone(found_job)
703 if iteration == 1:
704 self.assertIsNotNone(found_job['estimated_time'])
705 self.assertIsNone(found_job['remaining_time'])
706 else:
707 self.assertIsNotNone(found_job['estimated_time'])
708 self.assertTrue(found_job['estimated_time'] >= 2)
709 self.assertIsNotNone(found_job['remaining_time'])
710
Paul Belanger174a8272017-03-14 13:20:10 -0400711 self.executor_server.hold_jobs_in_build = False
712 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700713 self.waitUntilSettled()
714
715 def test_time_database(self):
716 "Test the time database"
717
718 self._test_time_database(1)
719 self._test_time_database(2)
720
James E. Blairfef71632013-09-23 11:15:47 -0700721 def test_two_failed_changes_at_head(self):
722 "Test that changes are reparented correctly if 2 fail at head"
723
Paul Belanger174a8272017-03-14 13:20:10 -0400724 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700725 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
726 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
727 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200728 A.addApproval('Code-Review', 2)
729 B.addApproval('Code-Review', 2)
730 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700731
Paul Belanger174a8272017-03-14 13:20:10 -0400732 self.executor_server.failJob('project-test1', A)
733 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700734
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200735 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
736 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
737 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700738 self.waitUntilSettled()
739
Paul Belanger174a8272017-03-14 13:20:10 -0400740 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700741 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400742 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700743 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400744 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700745 self.waitUntilSettled()
746
747 self.assertEqual(len(self.builds), 6)
748 self.assertEqual(self.builds[0].name, 'project-test1')
749 self.assertEqual(self.builds[1].name, 'project-test2')
750 self.assertEqual(self.builds[2].name, 'project-test1')
751 self.assertEqual(self.builds[3].name, 'project-test2')
752 self.assertEqual(self.builds[4].name, 'project-test1')
753 self.assertEqual(self.builds[5].name, 'project-test2')
754
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400755 self.assertTrue(self.builds[0].hasChanges(A))
756 self.assertTrue(self.builds[2].hasChanges(A))
757 self.assertTrue(self.builds[2].hasChanges(B))
758 self.assertTrue(self.builds[4].hasChanges(A))
759 self.assertTrue(self.builds[4].hasChanges(B))
760 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700761
762 # Fail change B first
763 self.release(self.builds[2])
764 self.waitUntilSettled()
765
766 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400767 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700768 self.waitUntilSettled()
769
770 self.assertEqual(len(self.builds), 5)
771 self.assertEqual(self.builds[0].name, 'project-test1')
772 self.assertEqual(self.builds[1].name, 'project-test2')
773 self.assertEqual(self.builds[2].name, 'project-test2')
774 self.assertEqual(self.builds[3].name, 'project-test1')
775 self.assertEqual(self.builds[4].name, 'project-test2')
776
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400777 self.assertTrue(self.builds[1].hasChanges(A))
778 self.assertTrue(self.builds[2].hasChanges(A))
779 self.assertTrue(self.builds[2].hasChanges(B))
780 self.assertTrue(self.builds[4].hasChanges(A))
781 self.assertFalse(self.builds[4].hasChanges(B))
782 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700783
784 # Finish running all passing jobs for change A
785 self.release(self.builds[1])
786 self.waitUntilSettled()
787 # Fail and report change A
788 self.release(self.builds[0])
789 self.waitUntilSettled()
790
791 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400792 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700793 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400794 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700795 self.waitUntilSettled()
796
797 self.assertEqual(len(self.builds), 4)
798 self.assertEqual(self.builds[0].name, 'project-test1') # B
799 self.assertEqual(self.builds[1].name, 'project-test2') # B
800 self.assertEqual(self.builds[2].name, 'project-test1') # C
801 self.assertEqual(self.builds[3].name, 'project-test2') # C
802
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400803 self.assertFalse(self.builds[1].hasChanges(A))
804 self.assertTrue(self.builds[1].hasChanges(B))
805 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700806
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400807 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700808 # After A failed and B and C restarted, B should be back in
809 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400810 self.assertTrue(self.builds[2].hasChanges(B))
811 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700812
Paul Belanger174a8272017-03-14 13:20:10 -0400813 self.executor_server.hold_jobs_in_build = False
814 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700815 self.waitUntilSettled()
816
817 self.assertEqual(len(self.builds), 0)
818 self.assertEqual(len(self.history), 21)
819 self.assertEqual(A.data['status'], 'NEW')
820 self.assertEqual(B.data['status'], 'NEW')
821 self.assertEqual(C.data['status'], 'MERGED')
822 self.assertEqual(A.reported, 2)
823 self.assertEqual(B.reported, 2)
824 self.assertEqual(C.reported, 2)
825
James E. Blair8c803f82012-07-31 16:25:42 -0700826 def test_patch_order(self):
827 "Test that dependent patches are tested in the right order"
828 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
829 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
830 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200831 A.addApproval('Code-Review', 2)
832 B.addApproval('Code-Review', 2)
833 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700834
835 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
836 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
837 M2.setMerged()
838 M1.setMerged()
839
840 # C -> B -> A -> M1 -> M2
841 # M2 is here to make sure it is never queried. If it is, it
842 # means zuul is walking down the entire history of merged
843 # changes.
844
845 C.setDependsOn(B, 1)
846 B.setDependsOn(A, 1)
847 A.setDependsOn(M1, 1)
848 M1.setDependsOn(M2, 1)
849
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200850 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700851
852 self.waitUntilSettled()
853
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400854 self.assertEqual(A.data['status'], 'NEW')
855 self.assertEqual(B.data['status'], 'NEW')
856 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700857
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200858 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
859 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700860
861 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400862 self.assertEqual(M2.queried, 0)
863 self.assertEqual(A.data['status'], 'MERGED')
864 self.assertEqual(B.data['status'], 'MERGED')
865 self.assertEqual(C.data['status'], 'MERGED')
866 self.assertEqual(A.reported, 2)
867 self.assertEqual(B.reported, 2)
868 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700869
James E. Blair063672f2015-01-29 13:09:12 -0800870 def test_needed_changes_enqueue(self):
871 "Test that a needed change is enqueued ahead"
872 # A Given a git tree like this, if we enqueue
873 # / \ change C, we should walk up and down the tree
874 # B G and enqueue changes in the order ABCDEFG.
875 # /|\ This is also the order that you would get if
876 # *C E F you enqueued changes in the order ABCDEFG, so
877 # / the ordering is stable across re-enqueue events.
878 # D
879
880 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
881 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
882 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
883 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
884 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
885 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
886 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
887 B.setDependsOn(A, 1)
888 C.setDependsOn(B, 1)
889 D.setDependsOn(C, 1)
890 E.setDependsOn(B, 1)
891 F.setDependsOn(B, 1)
892 G.setDependsOn(A, 1)
893
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200894 A.addApproval('Code-Review', 2)
895 B.addApproval('Code-Review', 2)
896 C.addApproval('Code-Review', 2)
897 D.addApproval('Code-Review', 2)
898 E.addApproval('Code-Review', 2)
899 F.addApproval('Code-Review', 2)
900 G.addApproval('Code-Review', 2)
901 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800902
903 self.waitUntilSettled()
904
905 self.assertEqual(A.data['status'], 'NEW')
906 self.assertEqual(B.data['status'], 'NEW')
907 self.assertEqual(C.data['status'], 'NEW')
908 self.assertEqual(D.data['status'], 'NEW')
909 self.assertEqual(E.data['status'], 'NEW')
910 self.assertEqual(F.data['status'], 'NEW')
911 self.assertEqual(G.data['status'], 'NEW')
912
913 # We're about to add approvals to changes without adding the
914 # triggering events to Zuul, so that we can be sure that it is
915 # enqueing the changes based on dependencies, not because of
916 # triggering events. Since it will have the changes cached
917 # already (without approvals), we need to clear the cache
918 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400919 for connection in self.connections.connections.values():
James E. Blairb0a95ab2017-10-18 09:39:18 -0700920 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800921
Paul Belanger174a8272017-03-14 13:20:10 -0400922 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200923 A.addApproval('Approved', 1)
924 B.addApproval('Approved', 1)
925 D.addApproval('Approved', 1)
926 E.addApproval('Approved', 1)
927 F.addApproval('Approved', 1)
928 G.addApproval('Approved', 1)
929 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800930
931 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400932 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800933 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400934 self.executor_server.hold_jobs_in_build = False
935 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800936 self.waitUntilSettled()
937
938 self.assertEqual(A.data['status'], 'MERGED')
939 self.assertEqual(B.data['status'], 'MERGED')
940 self.assertEqual(C.data['status'], 'MERGED')
941 self.assertEqual(D.data['status'], 'MERGED')
942 self.assertEqual(E.data['status'], 'MERGED')
943 self.assertEqual(F.data['status'], 'MERGED')
944 self.assertEqual(G.data['status'], 'MERGED')
945 self.assertEqual(A.reported, 2)
946 self.assertEqual(B.reported, 2)
947 self.assertEqual(C.reported, 2)
948 self.assertEqual(D.reported, 2)
949 self.assertEqual(E.reported, 2)
950 self.assertEqual(F.reported, 2)
951 self.assertEqual(G.reported, 2)
952 self.assertEqual(self.history[6].changes,
953 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
954
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100955 def test_source_cache(self):
956 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400957 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700958
959 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
960 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
961 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200962 A.addApproval('Code-Review', 2)
963 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700964
965 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
966 M1.setMerged()
967
968 B.setDependsOn(A, 1)
969 A.setDependsOn(M1, 1)
970
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200971 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700972 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
973
974 self.waitUntilSettled()
975
976 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700977 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700978 build.release()
979 self.waitUntilSettled()
980 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700981 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700982 build.release()
983 self.waitUntilSettled()
984
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200985 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700986 self.waitUntilSettled()
987
Joshua Hesketh352264b2015-08-11 23:42:08 +1000988 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700989 # there should still be changes in the cache
James E. Blairb0a95ab2017-10-18 09:39:18 -0700990 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700991
Paul Belanger174a8272017-03-14 13:20:10 -0400992 self.executor_server.hold_jobs_in_build = False
993 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700994 self.waitUntilSettled()
995
996 self.assertEqual(A.data['status'], 'MERGED')
997 self.assertEqual(B.data['status'], 'MERGED')
998 self.assertEqual(A.queried, 2) # Initial and isMerged
999 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
1000
James E. Blair8c803f82012-07-31 16:25:42 -07001001 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -07001002 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -07001003 # TODO: move to test_gerrit (this is a unit test!)
1004 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -04001005 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07001006 (trusted, project) = tenant.getProject('org/project')
1007 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -04001008
1009 # TODO(pabelanger): As we add more source / trigger APIs we should make
1010 # it easier for users to create events for testing.
1011 event = zuul.model.TriggerEvent()
1012 event.trigger_name = 'gerrit'
1013 event.change_number = '1'
1014 event.patch_number = '2'
1015
1016 a = source.getChange(event)
1017 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -07001018 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001019
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001020 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -04001021 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -07001022 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -07001023
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001024 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -04001025 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -07001026 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -07001027
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001028 def test_project_merge_conflict(self):
1029 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001030
1031 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001032 A = self.fake_gerrit.addFakeChange('org/project',
1033 'master', 'A',
1034 files={'conflict': 'foo'})
1035 B = self.fake_gerrit.addFakeChange('org/project',
1036 'master', 'B',
1037 files={'conflict': 'bar'})
1038 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -07001039 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001040 A.addApproval('Code-Review', 2)
1041 B.addApproval('Code-Review', 2)
1042 C.addApproval('Code-Review', 2)
1043 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1044 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1045 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -07001046 self.waitUntilSettled()
1047
James E. Blair6736beb2013-07-11 15:18:15 -07001048 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -07001049 self.assertEqual(C.reported, 1)
1050
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001051 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001052 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001053 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001054 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001055 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001056 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001057
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001058 self.gearman_server.hold_jobs_in_queue = False
1059 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001060 self.waitUntilSettled()
1061
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001062 self.assertEqual(A.data['status'], 'MERGED')
1063 self.assertEqual(B.data['status'], 'NEW')
1064 self.assertEqual(C.data['status'], 'MERGED')
1065 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001066 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001067 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001068
1069 self.assertHistory([
1070 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1072 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1073 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1074 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1075 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1076 ], ordered=False)
1077
1078 def test_delayed_merge_conflict(self):
1079 "Test that delayed check merge conflicts are handled properly"
1080
1081 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001082 # the executor sucesfully merges a change based on an old
1083 # repo state (frozen by the scheduler) which would otherwise
1084 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001085 self.gearman_server.hold_jobs_in_queue = True
1086 A = self.fake_gerrit.addFakeChange('org/project',
1087 'master', 'A',
1088 files={'conflict': 'foo'})
1089 B = self.fake_gerrit.addFakeChange('org/project',
1090 'master', 'B',
1091 files={'conflict': 'bar'})
1092 C = self.fake_gerrit.addFakeChange('org/project',
1093 'master', 'C')
1094 C.setDependsOn(B, 1)
1095
1096 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001097 A.addApproval('Code-Review', 2)
1098 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001099 self.waitUntilSettled()
1100 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001101 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001102 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1103 self.waitUntilSettled()
1104
1105 self.assertEqual(A.reported, 1)
1106 self.assertEqual(B.reported, 0) # Check does not report start
1107 self.assertEqual(C.reported, 0) # Check does not report start
1108
1109 # A merges while B and C are queued in check
1110 # Release A project-merge
1111 queue = self.gearman_server.getQueue()
1112 self.release(queue[0])
1113 self.waitUntilSettled()
1114
1115 # Release A project-test*
1116 # gate has higher precedence, so A's test jobs are added in
1117 # front of the merge jobs for B and C
1118 queue = self.gearman_server.getQueue()
1119 self.release(queue[0])
1120 self.release(queue[1])
1121 self.waitUntilSettled()
1122
1123 self.assertEqual(A.data['status'], 'MERGED')
1124 self.assertEqual(B.data['status'], 'NEW')
1125 self.assertEqual(C.data['status'], 'NEW')
1126 self.assertEqual(A.reported, 2)
1127 self.assertEqual(B.reported, 0)
1128 self.assertEqual(C.reported, 0)
1129 self.assertHistory([
1130 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1131 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1132 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1133 ], ordered=False)
1134
1135 # B and C report merge conflicts
1136 # Release B project-merge
1137 queue = self.gearman_server.getQueue()
1138 self.release(queue[0])
1139 self.waitUntilSettled()
1140
1141 # Release C
1142 self.gearman_server.hold_jobs_in_queue = False
1143 self.gearman_server.release()
1144 self.waitUntilSettled()
1145
1146 self.assertEqual(A.data['status'], 'MERGED')
1147 self.assertEqual(B.data['status'], 'NEW')
1148 self.assertEqual(C.data['status'], 'NEW')
1149 self.assertEqual(A.reported, 2)
1150 self.assertEqual(B.reported, 1)
1151 self.assertEqual(C.reported, 1)
1152
1153 self.assertHistory([
1154 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1155 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1156 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001157 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1158 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1159 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1160 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1161 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1162 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001163 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001164
James E. Blairdaabed22012-08-15 15:38:57 -07001165 def test_post(self):
1166 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001167 p = "review.example.com/org/project"
1168 upstream = self.getUpstreamRepos([p])
1169 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1170 A.setMerged()
1171 A_commit = str(upstream[p].commit('master'))
1172 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001173
Zhongyue Luo5d556072012-09-21 02:00:47 +09001174 e = {
1175 "type": "ref-updated",
1176 "submitter": {
1177 "name": "User Name",
1178 },
1179 "refUpdate": {
1180 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001181 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001182 "refName": "master",
1183 "project": "org/project",
1184 }
1185 }
James E. Blairdaabed22012-08-15 15:38:57 -07001186 self.fake_gerrit.addEvent(e)
1187 self.waitUntilSettled()
1188
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001189 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001190 self.assertEqual(len(self.history), 1)
1191 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001192
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001193 def test_post_ignore_deletes(self):
1194 "Test that deleting refs does not trigger post jobs"
1195
1196 e = {
1197 "type": "ref-updated",
1198 "submitter": {
1199 "name": "User Name",
1200 },
1201 "refUpdate": {
1202 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1203 "newRev": "0000000000000000000000000000000000000000",
1204 "refName": "master",
1205 "project": "org/project",
1206 }
1207 }
1208 self.fake_gerrit.addEvent(e)
1209 self.waitUntilSettled()
1210
1211 job_names = [x.name for x in self.history]
1212 self.assertEqual(len(self.history), 0)
1213 self.assertNotIn('project-post', job_names)
1214
James E. Blaircc324192017-04-19 14:36:08 -07001215 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001216 def test_post_ignore_deletes_negative(self):
1217 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001218 e = {
1219 "type": "ref-updated",
1220 "submitter": {
1221 "name": "User Name",
1222 },
1223 "refUpdate": {
1224 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1225 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001226 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001227 "project": "org/project",
1228 }
1229 }
1230 self.fake_gerrit.addEvent(e)
1231 self.waitUntilSettled()
1232
1233 job_names = [x.name for x in self.history]
1234 self.assertEqual(len(self.history), 1)
1235 self.assertIn('project-post', job_names)
1236
James E. Blairec056492016-07-22 09:45:56 -07001237 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001238 def test_build_configuration_branch_interaction(self):
1239 "Test that switching between branches works"
1240 self.test_build_configuration()
1241 self.test_build_configuration_branch()
1242 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001243 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001244 repo = git.Repo(path)
1245 repo.heads.master.commit = repo.commit('init')
1246 self.test_build_configuration()
1247
James E. Blairdb93b302017-07-19 15:33:11 -07001248 def test_dependent_changes_rebase(self):
1249 # Test that no errors occur when we walk a dependency tree
1250 # with an unused leaf node due to a rebase.
1251 # Start by constructing: C -> B -> A
1252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1253 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1254 B.setDependsOn(A, 1)
1255
1256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1257 C.setDependsOn(B, 1)
1258
1259 # Then rebase to form: D -> C -> A
1260 C.addPatchset() # C,2
1261 C.setDependsOn(A, 1)
1262
1263 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1264 D.setDependsOn(C, 2)
1265
1266 # Walk the entire tree
1267 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1268 self.waitUntilSettled()
1269 self.assertEqual(len(self.history), 3)
1270
1271 # Verify that walking just part of the tree still works
1272 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1273 self.waitUntilSettled()
1274 self.assertEqual(len(self.history), 6)
1275
James E. Blaircaec0c52012-08-22 14:52:22 -07001276 def test_dependent_changes_dequeue(self):
1277 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001278
James E. Blaircaec0c52012-08-22 14:52:22 -07001279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1280 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1281 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001282 A.addApproval('Code-Review', 2)
1283 B.addApproval('Code-Review', 2)
1284 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001285
1286 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1287 M1.setMerged()
1288
1289 # C -> B -> A -> M1
1290
1291 C.setDependsOn(B, 1)
1292 B.setDependsOn(A, 1)
1293 A.setDependsOn(M1, 1)
1294
Paul Belanger174a8272017-03-14 13:20:10 -04001295 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001296
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001297 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1298 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1299 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001300
1301 self.waitUntilSettled()
1302
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001303 self.assertEqual(A.data['status'], 'NEW')
1304 self.assertEqual(A.reported, 2)
1305 self.assertEqual(B.data['status'], 'NEW')
1306 self.assertEqual(B.reported, 2)
1307 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001308 self.assertIn('This change depends on a change that failed to merge.',
1309 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001310 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001311
James E. Blair972e3c72013-08-29 12:04:55 -07001312 def test_failing_dependent_changes(self):
1313 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001314 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1316 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1317 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1318 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1319 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001320 A.addApproval('Code-Review', 2)
1321 B.addApproval('Code-Review', 2)
1322 C.addApproval('Code-Review', 2)
1323 D.addApproval('Code-Review', 2)
1324 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001325
1326 # E, D -> C -> B, A
1327
1328 D.setDependsOn(C, 1)
1329 C.setDependsOn(B, 1)
1330
Paul Belanger174a8272017-03-14 13:20:10 -04001331 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001332
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001333 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1334 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1335 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1336 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1337 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001338
1339 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001340 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001341 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001342 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001343 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001344 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001346 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001347 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001348 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001349 self.waitUntilSettled()
1350
Paul Belanger174a8272017-03-14 13:20:10 -04001351 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001352 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001353 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001354 build.release()
1355 self.waitUntilSettled()
1356
Paul Belanger174a8272017-03-14 13:20:10 -04001357 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001358 self.waitUntilSettled()
1359
1360 self.assertEqual(A.data['status'], 'MERGED')
1361 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001362 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001363 self.assertEqual(B.data['status'], 'NEW')
1364 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001365 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001366 self.assertEqual(C.data['status'], 'NEW')
1367 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001368 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001369 self.assertEqual(D.data['status'], 'NEW')
1370 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001371 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001372 self.assertEqual(E.data['status'], 'MERGED')
1373 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001374 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001375 self.assertEqual(len(self.history), 18)
1376
James E. Blairec590122012-08-22 15:19:31 -07001377 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001378 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001379 # If it's dequeued more than once, we should see extra
1380 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001381
Paul Belanger174a8272017-03-14 13:20:10 -04001382 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001383 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1384 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1385 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001386 A.addApproval('Code-Review', 2)
1387 B.addApproval('Code-Review', 2)
1388 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001389
Paul Belanger174a8272017-03-14 13:20:10 -04001390 self.executor_server.failJob('project-test1', A)
1391 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001392
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001393 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1394 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1395 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001396
1397 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001398
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001399 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001400 self.assertEqual(self.builds[0].name, 'project-merge')
1401 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001402
Paul Belanger174a8272017-03-14 13:20:10 -04001403 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001404 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001405 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001406 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001407 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001408 self.waitUntilSettled()
1409
James E. Blair6df67132017-04-19 13:53:20 -07001410 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001411 self.assertEqual(self.builds[0].name, 'project-test1')
1412 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001413 self.assertEqual(self.builds[2].name, 'project-test1')
1414 self.assertEqual(self.builds[3].name, 'project-test2')
1415 self.assertEqual(self.builds[4].name, 'project-test1')
1416 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001417
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001418 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001419 self.waitUntilSettled()
1420
James E. Blair6df67132017-04-19 13:53:20 -07001421 self.assertEqual(len(self.builds), 2) # test2, merge for B
1422 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001423
Paul Belanger174a8272017-03-14 13:20:10 -04001424 self.executor_server.hold_jobs_in_build = False
1425 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001426 self.waitUntilSettled()
1427
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001428 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001429 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001430
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001431 self.assertEqual(A.data['status'], 'NEW')
1432 self.assertEqual(B.data['status'], 'MERGED')
1433 self.assertEqual(C.data['status'], 'MERGED')
1434 self.assertEqual(A.reported, 2)
1435 self.assertEqual(B.reported, 2)
1436 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001437
James E. Blaircc020532017-04-19 13:04:44 -07001438 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001439 def test_nonvoting_job(self):
1440 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001441
James E. Blair4ec821f2012-08-23 15:28:28 -07001442 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1443 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001444 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001445 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001446 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001447
1448 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001449
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001450 self.assertEqual(A.data['status'], 'MERGED')
1451 self.assertEqual(A.reported, 2)
1452 self.assertEqual(
1453 self.getJobFromHistory('nonvoting-project-merge').result,
1454 'SUCCESS')
1455 self.assertEqual(
1456 self.getJobFromHistory('nonvoting-project-test1').result,
1457 'SUCCESS')
1458 self.assertEqual(
1459 self.getJobFromHistory('nonvoting-project-test2').result,
1460 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001461
James E. Blair2ef29e92017-07-21 15:25:05 -07001462 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1463 parameters['zuul']['voting'])
1464 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1465 parameters['zuul']['voting'])
1466 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1467 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001468
James E. Blaire0487072012-08-29 17:38:31 -07001469 def test_check_queue_success(self):
1470 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001471
James E. Blaire0487072012-08-29 17:38:31 -07001472 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1473 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1474
1475 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001476
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001477 self.assertEqual(A.data['status'], 'NEW')
1478 self.assertEqual(A.reported, 1)
1479 self.assertEqual(self.getJobFromHistory('project-merge').result,
1480 'SUCCESS')
1481 self.assertEqual(self.getJobFromHistory('project-test1').result,
1482 'SUCCESS')
1483 self.assertEqual(self.getJobFromHistory('project-test2').result,
1484 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001485
1486 def test_check_queue_failure(self):
1487 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001488
James E. Blaire0487072012-08-29 17:38:31 -07001489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001490 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001491 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1492
1493 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001494
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001495 self.assertEqual(A.data['status'], 'NEW')
1496 self.assertEqual(A.reported, 1)
1497 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001498 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001499 self.assertEqual(self.getJobFromHistory('project-test1').result,
1500 'SUCCESS')
1501 self.assertEqual(self.getJobFromHistory('project-test2').result,
1502 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001503
David Shrewsburyffab07a2017-07-24 12:45:07 -04001504 @simple_layout('layouts/autohold.yaml')
1505 def test_autohold(self):
David Shrewsburyffab07a2017-07-24 12:45:07 -04001506 client = zuul.rpcclient.RPCClient('127.0.0.1',
1507 self.gearman_server.port)
1508 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001509 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1510 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001511 self.assertTrue(r)
1512
Clark Boylanaeb5a122017-10-20 14:00:11 -07001513 # First check that successful jobs do not autohold
1514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
David Shrewsburyffab07a2017-07-24 12:45:07 -04001515 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1516
1517 self.waitUntilSettled()
1518
1519 self.assertEqual(A.data['status'], 'NEW')
1520 self.assertEqual(A.reported, 1)
Clark Boylanaeb5a122017-10-20 14:00:11 -07001521 # project-test2
1522 self.assertEqual(self.history[0].result, 'SUCCESS')
1523
1524 # Check nodepool for a held node
1525 held_node = None
1526 for node in self.fake_nodepool.getNodes():
1527 if node['state'] == zuul.model.STATE_HOLD:
1528 held_node = node
1529 break
1530 self.assertIsNone(held_node)
1531
1532 # Now test that failed jobs are autoheld
1533 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1534 self.executor_server.failJob('project-test2', B)
1535 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1536
1537 self.waitUntilSettled()
1538
1539 self.assertEqual(B.data['status'], 'NEW')
1540 self.assertEqual(B.reported, 1)
1541 # project-test2
1542 self.assertEqual(self.history[1].result, 'FAILURE')
David Shrewsburyffab07a2017-07-24 12:45:07 -04001543
1544 # Check nodepool for a held node
1545 held_node = None
1546 for node in self.fake_nodepool.getNodes():
1547 if node['state'] == zuul.model.STATE_HOLD:
1548 held_node = node
1549 break
1550 self.assertIsNotNone(held_node)
1551
1552 # Validate node has recorded the failed job
1553 self.assertEqual(
1554 held_node['hold_job'],
1555 " ".join(['tenant-one',
1556 'review.example.com/org/project',
1557 'project-test2'])
1558 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001559 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001560
1561 # Another failed change should not hold any more nodes
Clark Boylanaeb5a122017-10-20 14:00:11 -07001562 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1563 self.executor_server.failJob('project-test2', C)
1564 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
David Shrewsburyffab07a2017-07-24 12:45:07 -04001565 self.waitUntilSettled()
Clark Boylanaeb5a122017-10-20 14:00:11 -07001566 self.assertEqual(C.data['status'], 'NEW')
1567 self.assertEqual(C.reported, 1)
1568 # project-test2
1569 self.assertEqual(self.history[2].result, 'FAILURE')
David Shrewsburyffab07a2017-07-24 12:45:07 -04001570
1571 held_nodes = 0
1572 for node in self.fake_nodepool.getNodes():
1573 if node['state'] == zuul.model.STATE_HOLD:
1574 held_nodes += 1
1575 self.assertEqual(held_nodes, 1)
1576
David Shrewsburyc4676132017-10-16 12:41:57 -04001577 @simple_layout('layouts/autohold.yaml')
Clark Boylanaeb5a122017-10-20 14:00:11 -07001578 def test_autohold_ignores_aborted_jobs(self):
1579 client = zuul.rpcclient.RPCClient('127.0.0.1',
1580 self.gearman_server.port)
1581 self.addCleanup(client.shutdown)
1582 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1583 "reason text", 1)
1584 self.assertTrue(r)
1585
1586 self.executor_server.hold_jobs_in_build = True
1587
1588 # Create a change that will have its job aborted
1589 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1590 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1591 self.waitUntilSettled()
1592
1593 # Creating new patchset on change A will abort A,1's job because
1594 # a new patchset arrived replacing A,1 with A,2.
1595 A.addPatchset()
1596 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1597
1598 self.waitUntilSettled()
1599 self.executor_server.hold_jobs_in_build = False
1600 self.executor_server.release()
1601 self.waitUntilSettled()
1602
1603 self.assertEqual(A.data['status'], 'NEW')
1604 # Note only the successful job for A,2 will report as we don't
1605 # report aborted builds for old patchsets.
1606 self.assertEqual(A.reported, 1)
1607 # A,1 project-test2
1608 self.assertEqual(self.history[0].result, 'ABORTED')
1609 # A,2 project-test2
1610 self.assertEqual(self.history[1].result, 'SUCCESS')
1611
1612 # Check nodepool for a held node
1613 held_node = None
1614 for node in self.fake_nodepool.getNodes():
1615 if node['state'] == zuul.model.STATE_HOLD:
1616 held_node = node
1617 break
1618 self.assertIsNone(held_node)
1619
1620 @simple_layout('layouts/autohold.yaml')
David Shrewsburyc4676132017-10-16 12:41:57 -04001621 def test_autohold_list(self):
1622 client = zuul.rpcclient.RPCClient('127.0.0.1',
1623 self.gearman_server.port)
1624 self.addCleanup(client.shutdown)
1625
1626 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1627 "reason text", 1)
1628 self.assertTrue(r)
1629
1630 autohold_requests = client.autohold_list()
1631 self.assertNotEqual({}, autohold_requests)
1632 self.assertEqual(1, len(autohold_requests.keys()))
1633
1634 # The single dict key should be a CSV string value
1635 key = list(autohold_requests.keys())[0]
1636 tenant, project, job = key.split(',')
1637
1638 self.assertEqual('tenant-one', tenant)
1639 self.assertIn('org/project', project)
1640 self.assertEqual('project-test2', job)
1641
1642 # Note: the value is converted from set to list by json.
1643 self.assertEqual([1, "reason text"], autohold_requests[key])
1644
James E. Blair7cb84542017-04-19 13:35:05 -07001645 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001646 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001647 # This particular test does a large amount of merges and needs a little
1648 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001649 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001650 "test that dependent changes behind dequeued changes work"
1651 # This complicated test is a reproduction of a real life bug
1652 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001653
Paul Belanger174a8272017-03-14 13:20:10 -04001654 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001655 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1656 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1657 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1658 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1659 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1660 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1661 D.setDependsOn(C, 1)
1662 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001663 A.addApproval('Code-Review', 2)
1664 B.addApproval('Code-Review', 2)
1665 C.addApproval('Code-Review', 2)
1666 D.addApproval('Code-Review', 2)
1667 E.addApproval('Code-Review', 2)
1668 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001669
1670 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001671
1672 # Change object re-use in the gerrit trigger is hidden if
1673 # changes are added in quick succession; waiting makes it more
1674 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001675 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001676 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001677 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001678 self.waitUntilSettled()
1679
Paul Belanger174a8272017-03-14 13:20:10 -04001680 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001681 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001682 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001683 self.waitUntilSettled()
1684
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001685 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001686 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001687 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001688 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001689 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001690 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001691 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001692 self.waitUntilSettled()
1693
Paul Belanger174a8272017-03-14 13:20:10 -04001694 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001695 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001696 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001697 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001698 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001699 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001700 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001701 self.waitUntilSettled()
1702
1703 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001704
1705 # Grab pointers to the jobs we want to release before
1706 # releasing any, because list indexes may change as
1707 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001708 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001709 a.release()
1710 b.release()
1711 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001712 self.waitUntilSettled()
1713
Paul Belanger174a8272017-03-14 13:20:10 -04001714 self.executor_server.hold_jobs_in_build = False
1715 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001716 self.waitUntilSettled()
1717
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001718 self.assertEqual(A.data['status'], 'NEW')
1719 self.assertEqual(B.data['status'], 'MERGED')
1720 self.assertEqual(C.data['status'], 'MERGED')
1721 self.assertEqual(D.data['status'], 'MERGED')
1722 self.assertEqual(E.data['status'], 'MERGED')
1723 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001724
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001725 self.assertEqual(A.reported, 2)
1726 self.assertEqual(B.reported, 2)
1727 self.assertEqual(C.reported, 2)
1728 self.assertEqual(D.reported, 2)
1729 self.assertEqual(E.reported, 2)
1730 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001731
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001732 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1733 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001734
1735 def test_merger_repack(self):
1736 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001737
James E. Blair05fed602012-09-07 12:45:24 -07001738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001739 A.addApproval('Code-Review', 2)
1740 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001741 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001742 self.assertEqual(self.getJobFromHistory('project-merge').result,
1743 'SUCCESS')
1744 self.assertEqual(self.getJobFromHistory('project-test1').result,
1745 'SUCCESS')
1746 self.assertEqual(self.getJobFromHistory('project-test2').result,
1747 'SUCCESS')
1748 self.assertEqual(A.data['status'], 'MERGED')
1749 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001750 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001751 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001752
James E. Blair2a535672017-04-27 12:03:15 -07001753 path = os.path.join(self.merger_src_root, "review.example.com",
1754 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001755 if os.path.exists(path):
1756 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001757 path = os.path.join(self.executor_src_root, "review.example.com",
1758 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001759 if os.path.exists(path):
1760 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001761
1762 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001763 A.addApproval('Code-Review', 2)
1764 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001765 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001766 self.assertEqual(self.getJobFromHistory('project-merge').result,
1767 'SUCCESS')
1768 self.assertEqual(self.getJobFromHistory('project-test1').result,
1769 'SUCCESS')
1770 self.assertEqual(self.getJobFromHistory('project-test2').result,
1771 'SUCCESS')
1772 self.assertEqual(A.data['status'], 'MERGED')
1773 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001774
James E. Blair4886f282012-11-15 09:27:33 -08001775 def test_merger_repack_large_change(self):
1776 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001777 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001778 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001779 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001780 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001781 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001782 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001783 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001785 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001786 # TODOv3(jeblair): add hostname to upstream root
1787 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001788 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001789 path = os.path.join(self.merger_src_root, 'review.example.com',
1790 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001791 if os.path.exists(path):
1792 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001793 path = os.path.join(self.executor_src_root, 'review.example.com',
1794 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001795 if os.path.exists(path):
1796 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001797
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001798 A.addApproval('Code-Review', 2)
1799 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001800 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001801 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001802 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001803 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001804 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001805 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001806 'SUCCESS')
1807 self.assertEqual(A.data['status'], 'MERGED')
1808 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001809
James E. Blair2fa50962013-01-30 21:50:41 -08001810 def test_new_patchset_dequeues_old(self):
1811 "Test that a new patchset causes the old to be dequeued"
1812 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001813 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001814 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1815 M.setMerged()
1816
1817 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1818 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1819 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1820 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001821 A.addApproval('Code-Review', 2)
1822 B.addApproval('Code-Review', 2)
1823 C.addApproval('Code-Review', 2)
1824 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001825
1826 C.setDependsOn(B, 1)
1827 B.setDependsOn(A, 1)
1828 A.setDependsOn(M, 1)
1829
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001830 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1831 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1832 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1833 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001834 self.waitUntilSettled()
1835
1836 B.addPatchset()
1837 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1838 self.waitUntilSettled()
1839
Paul Belanger174a8272017-03-14 13:20:10 -04001840 self.executor_server.hold_jobs_in_build = False
1841 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001842 self.waitUntilSettled()
1843
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001844 self.assertEqual(A.data['status'], 'MERGED')
1845 self.assertEqual(A.reported, 2)
1846 self.assertEqual(B.data['status'], 'NEW')
1847 self.assertEqual(B.reported, 2)
1848 self.assertEqual(C.data['status'], 'NEW')
1849 self.assertEqual(C.reported, 2)
1850 self.assertEqual(D.data['status'], 'MERGED')
1851 self.assertEqual(D.reported, 2)
1852 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001853
James E. Blairba437362015-02-07 11:41:52 -08001854 def test_new_patchset_check(self):
1855 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001856
Paul Belanger174a8272017-03-14 13:20:10 -04001857 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001858
1859 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001860 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001861 tenant = self.sched.abide.tenants.get('tenant-one')
1862 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001863
1864 # Add two git-dependent changes
1865 B.setDependsOn(A, 1)
1866 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1867 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001868 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1869 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001870
1871 # A live item, and a non-live/live pair
1872 items = check_pipeline.getAllItems()
1873 self.assertEqual(len(items), 3)
1874
1875 self.assertEqual(items[0].change.number, '1')
1876 self.assertEqual(items[0].change.patchset, '1')
1877 self.assertFalse(items[0].live)
1878
1879 self.assertEqual(items[1].change.number, '2')
1880 self.assertEqual(items[1].change.patchset, '1')
1881 self.assertTrue(items[1].live)
1882
1883 self.assertEqual(items[2].change.number, '1')
1884 self.assertEqual(items[2].change.patchset, '1')
1885 self.assertTrue(items[2].live)
1886
1887 # Add a new patchset to A
1888 A.addPatchset()
1889 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1890 self.waitUntilSettled()
1891
1892 # The live copy of A,1 should be gone, but the non-live and B
1893 # should continue, and we should have a new A,2
1894 items = check_pipeline.getAllItems()
1895 self.assertEqual(len(items), 3)
1896
1897 self.assertEqual(items[0].change.number, '1')
1898 self.assertEqual(items[0].change.patchset, '1')
1899 self.assertFalse(items[0].live)
1900
1901 self.assertEqual(items[1].change.number, '2')
1902 self.assertEqual(items[1].change.patchset, '1')
1903 self.assertTrue(items[1].live)
1904
1905 self.assertEqual(items[2].change.number, '1')
1906 self.assertEqual(items[2].change.patchset, '2')
1907 self.assertTrue(items[2].live)
1908
1909 # Add a new patchset to B
1910 B.addPatchset()
1911 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1912 self.waitUntilSettled()
1913
1914 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1915 # but we should have a new B,2 (still based on A,1)
1916 items = check_pipeline.getAllItems()
1917 self.assertEqual(len(items), 3)
1918
1919 self.assertEqual(items[0].change.number, '1')
1920 self.assertEqual(items[0].change.patchset, '2')
1921 self.assertTrue(items[0].live)
1922
1923 self.assertEqual(items[1].change.number, '1')
1924 self.assertEqual(items[1].change.patchset, '1')
1925 self.assertFalse(items[1].live)
1926
1927 self.assertEqual(items[2].change.number, '2')
1928 self.assertEqual(items[2].change.patchset, '2')
1929 self.assertTrue(items[2].live)
1930
1931 self.builds[0].release()
1932 self.waitUntilSettled()
1933 self.builds[0].release()
1934 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001935 self.executor_server.hold_jobs_in_build = False
1936 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001937 self.waitUntilSettled()
1938
1939 self.assertEqual(A.reported, 1)
1940 self.assertEqual(B.reported, 1)
1941 self.assertEqual(self.history[0].result, 'ABORTED')
1942 self.assertEqual(self.history[0].changes, '1,1')
1943 self.assertEqual(self.history[1].result, 'ABORTED')
1944 self.assertEqual(self.history[1].changes, '1,1 2,1')
1945 self.assertEqual(self.history[2].result, 'SUCCESS')
1946 self.assertEqual(self.history[2].changes, '1,2')
1947 self.assertEqual(self.history[3].result, 'SUCCESS')
1948 self.assertEqual(self.history[3].changes, '1,1 2,2')
1949
1950 def test_abandoned_gate(self):
1951 "Test that an abandoned change is dequeued from gate"
1952
Paul Belanger174a8272017-03-14 13:20:10 -04001953 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001954
1955 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001956 A.addApproval('Code-Review', 2)
1957 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001958 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001959 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1960 self.assertEqual(self.builds[0].name, 'project-merge')
1961
1962 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1963 self.waitUntilSettled()
1964
Paul Belanger174a8272017-03-14 13:20:10 -04001965 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001966 self.waitUntilSettled()
1967
Arie5b4048c2016-10-05 16:12:06 +03001968 self.assertBuilds([])
1969 self.assertHistory([
1970 dict(name='project-merge', result='ABORTED', changes='1,1')],
1971 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001972 self.assertEqual(A.reported, 1,
1973 "Abandoned gate change should report only start")
1974
1975 def test_abandoned_check(self):
1976 "Test that an abandoned change is dequeued from check"
1977
Paul Belanger174a8272017-03-14 13:20:10 -04001978 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001979
1980 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1981 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001982 tenant = self.sched.abide.tenants.get('tenant-one')
1983 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001984
1985 # Add two git-dependent changes
1986 B.setDependsOn(A, 1)
1987 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1988 self.waitUntilSettled()
1989 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1990 self.waitUntilSettled()
1991 # A live item, and a non-live/live pair
1992 items = check_pipeline.getAllItems()
1993 self.assertEqual(len(items), 3)
1994
1995 self.assertEqual(items[0].change.number, '1')
1996 self.assertFalse(items[0].live)
1997
1998 self.assertEqual(items[1].change.number, '2')
1999 self.assertTrue(items[1].live)
2000
2001 self.assertEqual(items[2].change.number, '1')
2002 self.assertTrue(items[2].live)
2003
2004 # Abandon A
2005 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2006 self.waitUntilSettled()
2007
2008 # The live copy of A should be gone, but the non-live and B
2009 # should continue
2010 items = check_pipeline.getAllItems()
2011 self.assertEqual(len(items), 2)
2012
2013 self.assertEqual(items[0].change.number, '1')
2014 self.assertFalse(items[0].live)
2015
2016 self.assertEqual(items[1].change.number, '2')
2017 self.assertTrue(items[1].live)
2018
Paul Belanger174a8272017-03-14 13:20:10 -04002019 self.executor_server.hold_jobs_in_build = False
2020 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08002021 self.waitUntilSettled()
2022
2023 self.assertEqual(len(self.history), 4)
2024 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01002025 'Build should have been aborted')
2026 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08002027 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01002028
Steve Varnau7b78b312015-04-03 14:49:46 -07002029 def test_abandoned_not_timer(self):
2030 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002031 # This test can not use simple_layout because it must start
2032 # with a configuration which does not include a
2033 # timer-triggered job so that we have an opportunity to set
2034 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002035 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07002036 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002037 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07002038 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07002039 # The pipeline triggers every second, so we should have seen
2040 # several by now.
2041 time.sleep(5)
2042 self.waitUntilSettled()
2043 # Stop queuing timer triggered jobs so that the assertions
2044 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08002045 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002046 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07002047 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07002048 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002049 # If APScheduler is in mid-event when we remove the job, we
2050 # can end up with one more event firing, so give it an extra
2051 # second to settle.
2052 time.sleep(1)
2053 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002054
James E. Blair21037782017-07-19 11:56:55 -07002055 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07002056
2057 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2058 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2059 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002060 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07002061
2062 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2063 self.waitUntilSettled()
2064
James E. Blair21037782017-07-19 11:56:55 -07002065 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07002066
Paul Belanger174a8272017-03-14 13:20:10 -04002067 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07002068 self.waitUntilSettled()
2069
James E. Blair2fa50962013-01-30 21:50:41 -08002070 def test_new_patchset_dequeues_old_on_head(self):
2071 "Test that a new patchset causes the old to be dequeued (at head)"
2072 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04002073 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002074 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
2075 M.setMerged()
2076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2077 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2078 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2079 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002080 A.addApproval('Code-Review', 2)
2081 B.addApproval('Code-Review', 2)
2082 C.addApproval('Code-Review', 2)
2083 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002084
2085 C.setDependsOn(B, 1)
2086 B.setDependsOn(A, 1)
2087 A.setDependsOn(M, 1)
2088
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002089 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2090 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2091 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2092 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002093 self.waitUntilSettled()
2094
2095 A.addPatchset()
2096 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2097 self.waitUntilSettled()
2098
Paul Belanger174a8272017-03-14 13:20:10 -04002099 self.executor_server.hold_jobs_in_build = False
2100 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002101 self.waitUntilSettled()
2102
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002103 self.assertEqual(A.data['status'], 'NEW')
2104 self.assertEqual(A.reported, 2)
2105 self.assertEqual(B.data['status'], 'NEW')
2106 self.assertEqual(B.reported, 2)
2107 self.assertEqual(C.data['status'], 'NEW')
2108 self.assertEqual(C.reported, 2)
2109 self.assertEqual(D.data['status'], 'MERGED')
2110 self.assertEqual(D.reported, 2)
2111 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002112
2113 def test_new_patchset_dequeues_old_without_dependents(self):
2114 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04002115 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002116 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2117 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2118 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002119 A.addApproval('Code-Review', 2)
2120 B.addApproval('Code-Review', 2)
2121 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002122
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002123 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2124 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2125 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002126 self.waitUntilSettled()
2127
2128 B.addPatchset()
2129 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2130 self.waitUntilSettled()
2131
Paul Belanger174a8272017-03-14 13:20:10 -04002132 self.executor_server.hold_jobs_in_build = False
2133 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002134 self.waitUntilSettled()
2135
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002136 self.assertEqual(A.data['status'], 'MERGED')
2137 self.assertEqual(A.reported, 2)
2138 self.assertEqual(B.data['status'], 'NEW')
2139 self.assertEqual(B.reported, 2)
2140 self.assertEqual(C.data['status'], 'MERGED')
2141 self.assertEqual(C.reported, 2)
2142 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002143
2144 def test_new_patchset_dequeues_old_independent_queue(self):
2145 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002146 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002147 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2148 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2149 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2150 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2151 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2152 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2153 self.waitUntilSettled()
2154
2155 B.addPatchset()
2156 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2157 self.waitUntilSettled()
2158
Paul Belanger174a8272017-03-14 13:20:10 -04002159 self.executor_server.hold_jobs_in_build = False
2160 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002161 self.waitUntilSettled()
2162
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002163 self.assertEqual(A.data['status'], 'NEW')
2164 self.assertEqual(A.reported, 1)
2165 self.assertEqual(B.data['status'], 'NEW')
2166 self.assertEqual(B.reported, 1)
2167 self.assertEqual(C.data['status'], 'NEW')
2168 self.assertEqual(C.reported, 1)
2169 self.assertEqual(len(self.history), 10)
2170 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002171
James E. Blaircc020532017-04-19 13:04:44 -07002172 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002173 def test_noop_job(self):
2174 "Test that the internal noop job works"
2175 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002176 A.addApproval('Code-Review', 2)
2177 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002178 self.waitUntilSettled()
2179
2180 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2181 self.assertTrue(self.sched._areAllBuildsComplete())
2182 self.assertEqual(len(self.history), 0)
2183 self.assertEqual(A.data['status'], 'MERGED')
2184 self.assertEqual(A.reported, 2)
2185
James E. Blair06cc3922017-04-19 10:08:10 -07002186 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002187 def test_no_job_project(self):
2188 "Test that reports with no jobs don't get sent"
2189 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2190 'master', 'A')
2191 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2192 self.waitUntilSettled()
2193
2194 # Change wasn't reported to
2195 self.assertEqual(A.reported, False)
2196
2197 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002198 tenant = self.sched.abide.tenants.get('tenant-one')
2199 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002200 items = check_pipeline.getAllItems()
2201 self.assertEqual(len(items), 0)
2202
2203 self.assertEqual(len(self.history), 0)
2204
James E. Blair7d0dedc2013-02-21 17:26:09 -08002205 def test_zuul_refs(self):
2206 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002207 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002208 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2209 M1.setMerged()
2210 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2211 M2.setMerged()
2212
2213 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2214 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2215 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2216 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002217 A.addApproval('Code-Review', 2)
2218 B.addApproval('Code-Review', 2)
2219 C.addApproval('Code-Review', 2)
2220 D.addApproval('Code-Review', 2)
2221 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2222 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2223 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2224 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002225
2226 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002227 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002228 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002229 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002230 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002231 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002232 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002233 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002234 self.waitUntilSettled()
2235
Clint Byrum3343e3e2016-11-15 16:05:03 -08002236 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002237 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002238 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002239 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002240 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002241 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002242 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002243 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002244 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002245 d_build = x
2246 if a_build and b_build and c_build and d_build:
2247 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002248
Clint Byrum3343e3e2016-11-15 16:05:03 -08002249 # should have a, not b, and should not be in project2
2250 self.assertTrue(a_build.hasChanges(A))
2251 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002252
Clint Byrum3343e3e2016-11-15 16:05:03 -08002253 # should have a and b, and should not be in project2
2254 self.assertTrue(b_build.hasChanges(A, B))
2255 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002256
Clint Byrum3343e3e2016-11-15 16:05:03 -08002257 # should have a and b in 1, c in 2
2258 self.assertTrue(c_build.hasChanges(A, B, C))
2259 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002260
Clint Byrum3343e3e2016-11-15 16:05:03 -08002261 # should have a and b in 1, c and d in 2
2262 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002263
Paul Belanger174a8272017-03-14 13:20:10 -04002264 self.executor_server.hold_jobs_in_build = False
2265 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002266 self.waitUntilSettled()
2267
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002268 self.assertEqual(A.data['status'], 'MERGED')
2269 self.assertEqual(A.reported, 2)
2270 self.assertEqual(B.data['status'], 'MERGED')
2271 self.assertEqual(B.reported, 2)
2272 self.assertEqual(C.data['status'], 'MERGED')
2273 self.assertEqual(C.reported, 2)
2274 self.assertEqual(D.data['status'], 'MERGED')
2275 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002276
James E. Blair4a28a882013-08-23 15:17:33 -07002277 def test_rerun_on_error(self):
2278 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002279 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002280 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002281 A.addApproval('Code-Review', 2)
2282 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002283 self.waitUntilSettled()
2284
James E. Blair412fba82017-01-26 15:00:50 -08002285 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002286 self.executor_server.hold_jobs_in_build = False
2287 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002288 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002289 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002290 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2291
James E. Blair412e5582013-04-22 15:50:12 -07002292 def test_statsd(self):
2293 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002294 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002295 statsd.incr('test-incr')
2296 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002297 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002298 self.assertReportedStat('test-incr', '1|c')
2299 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002300 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002301
James E. Blairdad52252014-02-07 16:59:17 -08002302 def test_stuck_job_cleanup(self):
2303 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002304
2305 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002306 self.gearman_server.hold_jobs_in_queue = True
2307 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002308 A.addApproval('Code-Review', 2)
2309 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002310 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002311 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002312 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2313
James E. Blair31cb3e82017-04-20 13:08:56 -07002314 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002315 self.sched.reconfigure(self.config)
2316 self.waitUntilSettled()
2317
James E. Blair18c64442014-03-18 10:14:45 -07002318 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002319 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002320 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002321 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2322 self.assertTrue(self.sched._areAllBuildsComplete())
2323
2324 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002325 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002326 self.assertEqual(self.history[0].result, 'SUCCESS')
2327
James E. Blair879dafb2015-07-17 14:04:49 -07002328 def test_file_head(self):
2329 # This is a regression test for an observed bug. A change
2330 # with a file named "HEAD" in the root directory of the repo
2331 # was processed by a merger. It then was unable to reset the
2332 # repo because of:
2333 # GitCommandError: 'git reset --hard HEAD' returned
2334 # with exit code 128
2335 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2336 # and filename
2337 # Use '--' to separate filenames from revisions'
2338
2339 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002340 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002341 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2342
2343 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2344 self.waitUntilSettled()
2345
2346 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2347 self.waitUntilSettled()
2348
2349 self.assertIn('Build succeeded', A.messages[0])
2350 self.assertIn('Build succeeded', B.messages[0])
2351
James E. Blair70c71582013-03-06 08:50:50 -08002352 def test_file_jobs(self):
2353 "Test that file jobs run only when appropriate"
2354 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002355 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002356 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002357 A.addApproval('Code-Review', 2)
2358 B.addApproval('Code-Review', 2)
2359 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2360 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002361 self.waitUntilSettled()
2362
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002363 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002364 if x.name == 'project-testfile']
2365
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002366 self.assertEqual(len(testfile_jobs), 1)
2367 self.assertEqual(testfile_jobs[0].changes, '1,2')
2368 self.assertEqual(A.data['status'], 'MERGED')
2369 self.assertEqual(A.reported, 2)
2370 self.assertEqual(B.data['status'], 'MERGED')
2371 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002372
Clint Byrum3ee39f32016-11-17 23:45:07 -08002373 def _test_irrelevant_files_jobs(self, should_skip):
2374 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002375 if should_skip:
2376 files = {'ignoreme': 'ignored\n'}
2377 else:
2378 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002379
2380 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002381 'master',
2382 'test irrelevant-files',
2383 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002384 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2385 self.waitUntilSettled()
2386
2387 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002388 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002389
2390 if should_skip:
2391 self.assertEqual([], tested_change_ids)
2392 else:
2393 self.assertIn(change.data['number'], tested_change_ids)
2394
James E. Blaircc324192017-04-19 14:36:08 -07002395 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002396 def test_irrelevant_files_match_skips_job(self):
2397 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002398
James E. Blaircc324192017-04-19 14:36:08 -07002399 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002400 def test_irrelevant_files_no_match_runs_job(self):
2401 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002402
James E. Blaircc324192017-04-19 14:36:08 -07002403 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002404 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002405 files = {'ignoreme': 'ignored\n'}
2406
2407 change = self.fake_gerrit.addFakeChange('org/project',
2408 'master',
2409 'test irrelevant-files',
2410 files=files)
2411 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2412 self.waitUntilSettled()
2413
2414 run_jobs = set([build.name for build in self.history])
2415
2416 self.assertEqual(set(['project-test-nomatch-starts-empty',
2417 'project-test-nomatch-starts-full']), run_jobs)
2418
James E. Blairaf8b2082017-10-03 15:38:27 -07002419 @simple_layout('layouts/job-vars.yaml')
2420 def test_inherited_job_variables(self):
2421 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2422 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2423 self.waitUntilSettled()
2424 self.assertHistory([
2425 dict(name='parentjob', result='SUCCESS'),
2426 dict(name='child1', result='SUCCESS'),
2427 dict(name='child2', result='SUCCESS'),
2428 dict(name='child3', result='SUCCESS'),
2429 ], ordered=False)
2430 j = self.getJobFromHistory('parentjob')
2431 rp = set([p['name'] for p in j.parameters['projects']])
2432 self.assertEqual(j.parameters['vars']['override'], 0)
2433 self.assertEqual(j.parameters['vars']['child1override'], 0)
2434 self.assertEqual(j.parameters['vars']['parent'], 0)
2435 self.assertFalse('child1' in j.parameters['vars'])
2436 self.assertFalse('child2' in j.parameters['vars'])
2437 self.assertFalse('child3' in j.parameters['vars'])
2438 self.assertEqual(rp, set(['org/project', 'org/project0',
2439 'org/project0']))
2440 j = self.getJobFromHistory('child1')
2441 rp = set([p['name'] for p in j.parameters['projects']])
2442 self.assertEqual(j.parameters['vars']['override'], 1)
2443 self.assertEqual(j.parameters['vars']['child1override'], 1)
2444 self.assertEqual(j.parameters['vars']['parent'], 0)
2445 self.assertEqual(j.parameters['vars']['child1'], 1)
2446 self.assertFalse('child2' in j.parameters['vars'])
2447 self.assertFalse('child3' in j.parameters['vars'])
2448 self.assertEqual(rp, set(['org/project', 'org/project0',
2449 'org/project1']))
2450 j = self.getJobFromHistory('child2')
2451 rp = set([p['name'] for p in j.parameters['projects']])
2452 self.assertEqual(j.parameters['vars']['override'], 2)
2453 self.assertEqual(j.parameters['vars']['child1override'], 0)
2454 self.assertEqual(j.parameters['vars']['parent'], 0)
2455 self.assertFalse('child1' in j.parameters['vars'])
2456 self.assertEqual(j.parameters['vars']['child2'], 2)
2457 self.assertFalse('child3' in j.parameters['vars'])
2458 self.assertEqual(rp, set(['org/project', 'org/project0',
2459 'org/project2']))
2460 j = self.getJobFromHistory('child3')
2461 rp = set([p['name'] for p in j.parameters['projects']])
2462 self.assertEqual(j.parameters['vars']['override'], 3)
2463 self.assertEqual(j.parameters['vars']['child1override'], 0)
2464 self.assertEqual(j.parameters['vars']['parent'], 0)
2465 self.assertFalse('child1' in j.parameters['vars'])
2466 self.assertFalse('child2' in j.parameters['vars'])
2467 self.assertEqual(j.parameters['vars']['child3'], 3)
2468 self.assertEqual(rp, set(['org/project', 'org/project0',
2469 'org/project3']))
2470
James E. Blairb09a0c52017-10-04 07:35:14 -07002471 @simple_layout('layouts/job-variants.yaml')
2472 def test_job_branch_variants(self):
2473 self.create_branch('org/project', 'stable/diablo')
2474 self.create_branch('org/project', 'stable/essex')
2475 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2476 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2477 self.waitUntilSettled()
2478
2479 B = self.fake_gerrit.addFakeChange('org/project', 'stable/diablo', 'B')
2480 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2481 self.waitUntilSettled()
2482
2483 C = self.fake_gerrit.addFakeChange('org/project', 'stable/essex', 'C')
2484 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2485 self.waitUntilSettled()
2486 self.assertHistory([
2487 dict(name='python27', result='SUCCESS'),
2488 dict(name='python27', result='SUCCESS'),
2489 dict(name='python27', result='SUCCESS'),
2490 ])
2491
2492 p = self.history[0].parameters
2493 self.assertEqual(p['timeout'], 40)
2494 self.assertEqual(len(p['nodes']), 1)
2495 self.assertEqual(p['nodes'][0]['label'], 'new')
2496 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2497 ['base-pre', 'py27-pre'])
2498 self.assertEqual([x['path'] for x in p['post_playbooks']],
2499 ['py27-post-a', 'py27-post-b', 'base-post'])
2500 self.assertEqual([x['path'] for x in p['playbooks']],
James E. Blair2f589fe2017-10-26 12:57:41 -07002501 ['playbooks/python27.yaml'])
James E. Blairb09a0c52017-10-04 07:35:14 -07002502
2503 p = self.history[1].parameters
2504 self.assertEqual(p['timeout'], 50)
2505 self.assertEqual(len(p['nodes']), 1)
2506 self.assertEqual(p['nodes'][0]['label'], 'old')
2507 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2508 ['base-pre', 'py27-pre', 'py27-diablo-pre'])
2509 self.assertEqual([x['path'] for x in p['post_playbooks']],
2510 ['py27-diablo-post', 'py27-post-a', 'py27-post-b',
2511 'base-post'])
2512 self.assertEqual([x['path'] for x in p['playbooks']],
2513 ['py27-diablo'])
2514
2515 p = self.history[2].parameters
2516 self.assertEqual(p['timeout'], 40)
2517 self.assertEqual(len(p['nodes']), 1)
2518 self.assertEqual(p['nodes'][0]['label'], 'new')
2519 self.assertEqual([x['path'] for x in p['pre_playbooks']],
2520 ['base-pre', 'py27-pre', 'py27-essex-pre'])
2521 self.assertEqual([x['path'] for x in p['post_playbooks']],
2522 ['py27-essex-post', 'py27-post-a', 'py27-post-b',
2523 'base-post'])
2524 self.assertEqual([x['path'] for x in p['playbooks']],
James E. Blair2f589fe2017-10-26 12:57:41 -07002525 ['playbooks/python27.yaml'])
James E. Blairb09a0c52017-10-04 07:35:14 -07002526
James E. Blair00292672017-10-26 15:29:39 -07002527 @simple_layout("layouts/no-run.yaml")
2528 def test_job_without_run(self):
2529 "Test that a job without a run playbook errors"
2530 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2531 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2532 self.waitUntilSettled()
2533 self.assertIn('Job base does not specify a run playbook',
2534 A.messages[-1])
2535
James E. Blairc8a1e052014-02-25 09:29:26 -08002536 def test_queue_names(self):
2537 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002538 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002539 (trusted, project1) = tenant.getProject('org/project1')
2540 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002541 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2542 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2543 self.assertEqual(q1.name, 'integrated')
2544 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002545
James E. Blair64ed6f22013-07-10 14:07:23 -07002546 def test_queue_precedence(self):
2547 "Test that queue precedence works"
2548
2549 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002550 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002551 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2552 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002553 A.addApproval('Code-Review', 2)
2554 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002555
2556 self.waitUntilSettled()
2557 self.gearman_server.hold_jobs_in_queue = False
2558 self.gearman_server.release()
2559 self.waitUntilSettled()
2560
James E. Blair8de58bd2013-07-18 16:23:33 -07002561 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002562 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002563 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002564 self.waitUntilSettled()
2565
James E. Blair64ed6f22013-07-10 14:07:23 -07002566 self.log.debug(self.history)
2567 self.assertEqual(self.history[0].pipeline, 'gate')
2568 self.assertEqual(self.history[1].pipeline, 'check')
2569 self.assertEqual(self.history[2].pipeline, 'gate')
2570 self.assertEqual(self.history[3].pipeline, 'gate')
2571 self.assertEqual(self.history[4].pipeline, 'check')
2572 self.assertEqual(self.history[5].pipeline, 'check')
2573
Clark Boylana5edbe42014-06-03 16:39:10 -07002574 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002575 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002576 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002577 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002578 A.addApproval('Code-Review', 2)
2579 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002580 self.waitUntilSettled()
2581
Paul Belanger174a8272017-03-14 13:20:10 -04002582 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002583 self.waitUntilSettled()
2584
James E. Blair1843a552013-07-03 14:19:52 -07002585 port = self.webapp.server.socket.getsockname()[1]
2586
Adam Gandelman77a12c72017-02-03 17:43:43 -08002587 req = urllib.request.Request(
2588 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002589 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002590 headers = f.info()
2591 self.assertIn('Content-Length', headers)
2592 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002593 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2594 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002595 self.assertIn('Access-Control-Allow-Origin', headers)
2596 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002597 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002598 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002599 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002600
Paul Belanger174a8272017-03-14 13:20:10 -04002601 self.executor_server.hold_jobs_in_build = False
2602 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002603 self.waitUntilSettled()
2604
2605 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002606 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002607 for p in data['pipelines']:
2608 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002609 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002610 self.assertEqual(q['window'], 20)
2611 else:
2612 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002613 for head in q['heads']:
2614 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002615 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002616 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002617 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002618 status_jobs.append(job)
2619 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002620 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002621 self.assertEqual(
Tristan Cacqueraya4996f12017-09-20 01:00:28 +00002622 'stream.html?uuid={uuid}&logfile=console.log'.format(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002623 uuid=status_jobs[0]['uuid']),
2624 status_jobs[0]['url'])
2625 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002626 'finger://{hostname}/{uuid}'.format(
2627 hostname=self.executor_server.hostname,
2628 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002629 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002630 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002631 self.assertEqual(
2632 'finger://{hostname}/{uuid}'.format(
2633 hostname=self.executor_server.hostname,
2634 uuid=status_jobs[0]['uuid']),
2635 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002636 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002637 self.assertEqual(
Tristan Cacqueraya4996f12017-09-20 01:00:28 +00002638 'stream.html?uuid={uuid}&logfile=console.log'.format(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002639 uuid=status_jobs[1]['uuid']),
2640 status_jobs[1]['url'])
2641 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002642 'finger://{hostname}/{uuid}'.format(
2643 hostname=self.executor_server.hostname,
2644 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002645 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002646 self.assertEqual(
2647 'finger://{hostname}/{uuid}'.format(
2648 hostname=self.executor_server.hostname,
2649 uuid=status_jobs[1]['uuid']),
2650 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002651
2652 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002653 self.assertEqual(
Tristan Cacqueraya4996f12017-09-20 01:00:28 +00002654 'stream.html?uuid={uuid}&logfile=console.log'.format(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002655 uuid=status_jobs[2]['uuid']),
2656 status_jobs[2]['url'])
2657 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002658 'finger://{hostname}/{uuid}'.format(
2659 hostname=self.executor_server.hostname,
2660 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002661 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002662 self.assertEqual(
2663 'finger://{hostname}/{uuid}'.format(
2664 hostname=self.executor_server.hostname,
2665 uuid=status_jobs[2]['uuid']),
2666 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002667
Tobias Henkel65639f82017-07-10 10:25:42 +02002668 # check job dependencies
2669 self.assertIsNotNone(status_jobs[0]['dependencies'])
2670 self.assertIsNotNone(status_jobs[1]['dependencies'])
2671 self.assertIsNotNone(status_jobs[2]['dependencies'])
2672 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2673 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2674 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2675 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2676 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2677
James E. Blair419a8672017-10-18 14:48:25 -07002678 def test_reconfigure_merge(self):
2679 """Test that two reconfigure events are merged"""
2680
2681 tenant = self.sched.abide.tenants['tenant-one']
2682 (trusted, project) = tenant.getProject('org/project')
2683
2684 self.sched.run_handler_lock.acquire()
2685 self.assertEqual(self.sched.management_event_queue.qsize(), 0)
2686
2687 self.sched.reconfigureTenant(tenant, project)
2688 self.assertEqual(self.sched.management_event_queue.qsize(), 1)
2689
2690 self.sched.reconfigureTenant(tenant, project)
2691 # The second event should have been combined with the first
2692 # so we should still only have one entry.
2693 self.assertEqual(self.sched.management_event_queue.qsize(), 1)
2694
2695 self.sched.run_handler_lock.release()
2696 self.waitUntilSettled()
2697
2698 self.assertEqual(self.sched.management_event_queue.qsize(), 0)
2699
James E. Blaircdccd972013-07-01 12:10:22 -07002700 def test_live_reconfiguration(self):
2701 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002702 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002703 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002704 A.addApproval('Code-Review', 2)
2705 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002706 self.waitUntilSettled()
2707
2708 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002709 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002710
Paul Belanger174a8272017-03-14 13:20:10 -04002711 self.executor_server.hold_jobs_in_build = False
2712 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002713 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002714 self.assertEqual(self.getJobFromHistory('project-merge').result,
2715 'SUCCESS')
2716 self.assertEqual(self.getJobFromHistory('project-test1').result,
2717 'SUCCESS')
2718 self.assertEqual(self.getJobFromHistory('project-test2').result,
2719 'SUCCESS')
2720 self.assertEqual(A.data['status'], 'MERGED')
2721 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002722
James E. Blair027ba992017-09-20 13:48:32 -07002723 def test_live_reconfiguration_abort(self):
2724 # Raise an exception during reconfiguration and verify we
2725 # still function.
2726 self.executor_server.hold_jobs_in_build = True
2727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2728 A.addApproval('Code-Review', 2)
2729 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2730 self.waitUntilSettled()
2731
2732 tenant = self.sched.abide.tenants.get('tenant-one')
2733 pipeline = tenant.layout.pipelines['gate']
2734 change = pipeline.getAllItems()[0].change
2735 # Set this to an invalid value to cause an exception during
2736 # reconfiguration.
2737 change.branch = None
2738
2739 self.sched.reconfigure(self.config)
2740 self.waitUntilSettled()
2741
2742 self.executor_server.hold_jobs_in_build = False
2743 self.executor_server.release()
2744
2745 self.waitUntilSettled()
2746 self.assertEqual(self.getJobFromHistory('project-merge').result,
2747 'ABORTED')
2748 self.assertEqual(A.data['status'], 'NEW')
2749 # The final report fails because of the invalid value set above.
2750 self.assertEqual(A.reported, 1)
2751
James E. Blair6bc782d2015-07-17 16:20:21 -07002752 def test_live_reconfiguration_merge_conflict(self):
2753 # A real-world bug: a change in a gate queue has a merge
2754 # conflict and a job is added to its project while it's
2755 # sitting in the queue. The job gets added to the change and
2756 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002757 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002758
2759 # This change is fine. It's here to stop the queue long
2760 # enough for the next change to be subject to the
2761 # reconfiguration, as well as to provide a conflict for the
2762 # next change. This change will succeed and merge.
2763 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002764 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002765 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002766
2767 # This change will be in merge conflict. During the
2768 # reconfiguration, we will add a job. We want to make sure
2769 # that doesn't cause it to get stuck.
2770 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002771 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002772 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002773
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002774 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2775 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002776
2777 self.waitUntilSettled()
2778
2779 # No jobs have run yet
2780 self.assertEqual(A.data['status'], 'NEW')
2781 self.assertEqual(A.reported, 1)
2782 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002783 self.assertEqual(len(self.history), 0)
2784
2785 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002786 self.commitConfigUpdate('common-config',
2787 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002788 self.sched.reconfigure(self.config)
2789 self.waitUntilSettled()
2790
Paul Belanger174a8272017-03-14 13:20:10 -04002791 self.executor_server.hold_jobs_in_build = False
2792 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002793 self.waitUntilSettled()
2794
2795 self.assertEqual(A.data['status'], 'MERGED')
2796 self.assertEqual(A.reported, 2)
2797 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002798 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002799 self.assertEqual(self.getJobFromHistory('project-merge').result,
2800 'SUCCESS')
2801 self.assertEqual(self.getJobFromHistory('project-test1').result,
2802 'SUCCESS')
2803 self.assertEqual(self.getJobFromHistory('project-test2').result,
2804 'SUCCESS')
2805 self.assertEqual(self.getJobFromHistory('project-test3').result,
2806 'SUCCESS')
2807 self.assertEqual(len(self.history), 4)
2808
James E. Blair400e8fd2015-07-30 17:44:45 -07002809 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002810 # An extrapolation of test_live_reconfiguration_merge_conflict
2811 # that tests a job added to a job tree with a failed root does
2812 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002813 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002814
2815 # This change is fine. It's here to stop the queue long
2816 # enough for the next change to be subject to the
2817 # reconfiguration. This change will succeed and merge.
2818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002819 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002820 A.addApproval('Code-Review', 2)
2821 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002822 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002823 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002824 self.waitUntilSettled()
2825
2826 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002827 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002828 B.addApproval('Code-Review', 2)
2829 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002830 self.waitUntilSettled()
2831
Paul Belanger174a8272017-03-14 13:20:10 -04002832 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002833 self.waitUntilSettled()
2834
2835 # Both -merge jobs have run, but no others.
2836 self.assertEqual(A.data['status'], 'NEW')
2837 self.assertEqual(A.reported, 1)
2838 self.assertEqual(B.data['status'], 'NEW')
2839 self.assertEqual(B.reported, 1)
2840 self.assertEqual(self.history[0].result, 'SUCCESS')
2841 self.assertEqual(self.history[0].name, 'project-merge')
2842 self.assertEqual(self.history[1].result, 'FAILURE')
2843 self.assertEqual(self.history[1].name, 'project-merge')
2844 self.assertEqual(len(self.history), 2)
2845
2846 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002847 self.commitConfigUpdate('common-config',
2848 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002849 self.sched.reconfigure(self.config)
2850 self.waitUntilSettled()
2851
Paul Belanger174a8272017-03-14 13:20:10 -04002852 self.executor_server.hold_jobs_in_build = False
2853 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002854 self.waitUntilSettled()
2855
2856 self.assertEqual(A.data['status'], 'MERGED')
2857 self.assertEqual(A.reported, 2)
2858 self.assertEqual(B.data['status'], 'NEW')
2859 self.assertEqual(B.reported, 2)
2860 self.assertEqual(self.history[0].result, 'SUCCESS')
2861 self.assertEqual(self.history[0].name, 'project-merge')
2862 self.assertEqual(self.history[1].result, 'FAILURE')
2863 self.assertEqual(self.history[1].name, 'project-merge')
2864 self.assertEqual(self.history[2].result, 'SUCCESS')
2865 self.assertEqual(self.history[3].result, 'SUCCESS')
2866 self.assertEqual(self.history[4].result, 'SUCCESS')
2867 self.assertEqual(len(self.history), 5)
2868
James E. Blair400e8fd2015-07-30 17:44:45 -07002869 def test_live_reconfiguration_failed_job(self):
2870 # Test that a change with a removed failing job does not
2871 # disrupt reconfiguration. If a change has a failed job and
2872 # that job is removed during a reconfiguration, we observed a
2873 # bug where the code to re-set build statuses would run on
2874 # that build and raise an exception because the job no longer
2875 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002876 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002877
2878 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2879
2880 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002881 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002882
2883 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2884 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002885 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002886 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002887 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002888 self.waitUntilSettled()
2889
2890 self.assertEqual(A.data['status'], 'NEW')
2891 self.assertEqual(A.reported, 0)
2892
2893 self.assertEqual(self.getJobFromHistory('project-merge').result,
2894 'SUCCESS')
2895 self.assertEqual(self.getJobFromHistory('project-test1').result,
2896 'FAILURE')
2897 self.assertEqual(len(self.history), 2)
2898
2899 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002900 self.commitConfigUpdate('common-config',
2901 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002902 self.sched.reconfigure(self.config)
2903 self.waitUntilSettled()
2904
Paul Belanger174a8272017-03-14 13:20:10 -04002905 self.executor_server.hold_jobs_in_build = False
2906 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002907 self.waitUntilSettled()
2908
2909 self.assertEqual(self.getJobFromHistory('project-test2').result,
2910 'SUCCESS')
2911 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2912 'SUCCESS')
2913 self.assertEqual(len(self.history), 4)
2914
2915 self.assertEqual(A.data['status'], 'NEW')
2916 self.assertEqual(A.reported, 1)
2917 self.assertIn('Build succeeded', A.messages[0])
2918 # Ensure the removed job was not included in the report.
2919 self.assertNotIn('project-test1', A.messages[0])
2920
James E. Blairfe707d12015-08-05 15:18:15 -07002921 def test_live_reconfiguration_shared_queue(self):
2922 # Test that a change with a failing job which was removed from
2923 # this project but otherwise still exists in the system does
2924 # not disrupt reconfiguration.
2925
Paul Belanger174a8272017-03-14 13:20:10 -04002926 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002927
2928 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2929
Paul Belanger174a8272017-03-14 13:20:10 -04002930 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002931
2932 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2933 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002934 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002935 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002936 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002937 self.waitUntilSettled()
2938
2939 self.assertEqual(A.data['status'], 'NEW')
2940 self.assertEqual(A.reported, 0)
2941
Clint Byrum69663d42017-05-01 14:51:04 -07002942 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002943 'SUCCESS')
2944 self.assertEqual(self.getJobFromHistory(
2945 'project1-project2-integration').result, 'FAILURE')
2946 self.assertEqual(len(self.history), 2)
2947
2948 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002949 self.commitConfigUpdate(
2950 'common-config',
2951 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002952 self.sched.reconfigure(self.config)
2953 self.waitUntilSettled()
2954
Paul Belanger174a8272017-03-14 13:20:10 -04002955 self.executor_server.hold_jobs_in_build = False
2956 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002957 self.waitUntilSettled()
2958
Clint Byrum69663d42017-05-01 14:51:04 -07002959 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002960 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002961 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002962 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002963 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002964 'SUCCESS')
2965 self.assertEqual(self.getJobFromHistory(
2966 'project1-project2-integration').result, 'FAILURE')
2967 self.assertEqual(len(self.history), 4)
2968
2969 self.assertEqual(A.data['status'], 'NEW')
2970 self.assertEqual(A.reported, 1)
2971 self.assertIn('Build succeeded', A.messages[0])
2972 # Ensure the removed job was not included in the report.
2973 self.assertNotIn('project1-project2-integration', A.messages[0])
2974
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002975 def test_double_live_reconfiguration_shared_queue(self):
2976 # This was a real-world regression. A change is added to
2977 # gate; a reconfigure happens, a second change which depends
2978 # on the first is added, and a second reconfiguration happens.
2979 # Ensure that both changes merge.
2980
2981 # A failure may indicate incorrect caching or cleaning up of
2982 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002983 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002984
2985 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2986 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2987 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002988 A.addApproval('Code-Review', 2)
2989 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002990
2991 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002992 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002993 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002994 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002995 self.waitUntilSettled()
2996
2997 # Reconfigure (with only one change in the pipeline).
2998 self.sched.reconfigure(self.config)
2999 self.waitUntilSettled()
3000
3001 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003002 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003003 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003004 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003005 self.waitUntilSettled()
3006
3007 # Reconfigure (with both in the pipeline).
3008 self.sched.reconfigure(self.config)
3009 self.waitUntilSettled()
3010
Paul Belanger174a8272017-03-14 13:20:10 -04003011 self.executor_server.hold_jobs_in_build = False
3012 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003013 self.waitUntilSettled()
3014
3015 self.assertEqual(len(self.history), 8)
3016
3017 self.assertEqual(A.data['status'], 'MERGED')
3018 self.assertEqual(A.reported, 2)
3019 self.assertEqual(B.data['status'], 'MERGED')
3020 self.assertEqual(B.reported, 2)
3021
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003022 def test_live_reconfiguration_del_project(self):
3023 # Test project deletion from layout
3024 # while changes are enqueued
3025
Paul Belanger174a8272017-03-14 13:20:10 -04003026 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003027 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3028 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3029 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
3030
3031 # A Depends-On: B
3032 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3033 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003034 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003035
3036 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3037 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
3038 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003039 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003040 self.waitUntilSettled()
3041 self.assertEqual(len(self.builds), 5)
3042
3043 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07003044 self.commitConfigUpdate(
3045 'common-config',
3046 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003047 self.sched.reconfigure(self.config)
3048 self.waitUntilSettled()
3049
3050 # Builds for C aborted, builds for A succeed,
3051 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07003052 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003053 self.assertEqual(job_c.changes, '3,1')
3054 self.assertEqual(job_c.result, 'ABORTED')
3055
Paul Belanger174a8272017-03-14 13:20:10 -04003056 self.executor_server.hold_jobs_in_build = False
3057 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003058 self.waitUntilSettled()
3059
Clint Byrum0149ada2017-04-04 12:22:01 -07003060 self.assertEqual(
3061 self.getJobFromHistory('project-test1', 'org/project').changes,
3062 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003063
3064 self.assertEqual(A.data['status'], 'NEW')
3065 self.assertEqual(B.data['status'], 'NEW')
3066 self.assertEqual(C.data['status'], 'NEW')
3067 self.assertEqual(A.reported, 1)
3068 self.assertEqual(B.reported, 0)
3069 self.assertEqual(C.reported, 0)
3070
Clint Byrum0149ada2017-04-04 12:22:01 -07003071 tenant = self.sched.abide.tenants.get('tenant-one')
3072 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003073 self.assertIn('Build succeeded', A.messages[0])
3074
James E. Blair872738f2017-10-31 16:40:36 -07003075 @simple_layout("layouts/reconfigure-failed-head.yaml")
3076 def test_live_reconfiguration_failed_change_at_head(self):
3077 # Test that if we reconfigure with a failed change at head,
3078 # that the change behind it isn't reparented onto it.
3079
3080 self.executor_server.hold_jobs_in_build = True
3081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3082 A.addApproval('Code-Review', 2)
3083 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3084 B.addApproval('Code-Review', 2)
3085
3086 self.executor_server.failJob('job1', A)
3087
3088 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3089 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3090
3091 self.waitUntilSettled()
3092
3093 self.assertBuilds([
3094 dict(name='job1', changes='1,1'),
3095 dict(name='job2', changes='1,1'),
3096 dict(name='job1', changes='1,1 2,1'),
3097 dict(name='job2', changes='1,1 2,1'),
3098 ])
3099
3100 self.release(self.builds[0])
3101 self.waitUntilSettled()
3102
3103 self.assertBuilds([
3104 dict(name='job2', changes='1,1'),
3105 dict(name='job1', changes='2,1'),
3106 dict(name='job2', changes='2,1'),
3107 ])
3108
3109 # Unordered history comparison because the aborts can finish
3110 # in any order.
3111 self.assertHistory([
3112 dict(name='job1', result='FAILURE', changes='1,1'),
3113 dict(name='job1', result='ABORTED', changes='1,1 2,1'),
3114 dict(name='job2', result='ABORTED', changes='1,1 2,1'),
3115 ], ordered=False)
3116
3117 self.sched.reconfigure(self.config)
3118 self.waitUntilSettled()
3119
3120 self.executor_server.hold_jobs_in_build = False
3121 self.executor_server.release()
3122 self.waitUntilSettled()
3123
3124 self.assertBuilds([])
3125
3126 self.assertHistory([
3127 dict(name='job1', result='FAILURE', changes='1,1'),
3128 dict(name='job1', result='ABORTED', changes='1,1 2,1'),
3129 dict(name='job2', result='ABORTED', changes='1,1 2,1'),
3130 dict(name='job2', result='SUCCESS', changes='1,1'),
3131 dict(name='job1', result='SUCCESS', changes='2,1'),
3132 dict(name='job2', result='SUCCESS', changes='2,1'),
3133 ], ordered=False)
3134 self.assertEqual(A.data['status'], 'NEW')
3135 self.assertEqual(B.data['status'], 'MERGED')
3136 self.assertEqual(A.reported, 2)
3137 self.assertEqual(B.reported, 2)
3138
James E. Blair287c06d2013-07-24 10:39:30 -07003139 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07003140 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07003141 files = {'README': ''}
3142 self.addCommitToRepo("org/new-project", 'Initial commit',
3143 files=files, tag='init')
3144 self.newTenantConfig('tenants/delayed-repo-init.yaml')
3145 self.commitConfigUpdate(
3146 'common-config',
3147 'layouts/delayed-repo-init.yaml')
3148 self.sched.reconfigure(self.config)
3149 self.waitUntilSettled()
3150
James E. Blair287c06d2013-07-24 10:39:30 -07003151 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
3152
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003153 A.addApproval('Code-Review', 2)
3154 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07003155 self.waitUntilSettled()
3156 self.assertEqual(self.getJobFromHistory('project-merge').result,
3157 'SUCCESS')
3158 self.assertEqual(self.getJobFromHistory('project-test1').result,
3159 'SUCCESS')
3160 self.assertEqual(self.getJobFromHistory('project-test2').result,
3161 'SUCCESS')
3162 self.assertEqual(A.data['status'], 'MERGED')
3163 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003164
James E. Blair9ea70072017-04-19 16:05:30 -07003165 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07003166 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07003167 self.init_repo("org/delete-project")
3168 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
3169
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003170 A.addApproval('Code-Review', 2)
3171 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003172 self.waitUntilSettled()
3173 self.assertEqual(self.getJobFromHistory('project-merge').result,
3174 'SUCCESS')
3175 self.assertEqual(self.getJobFromHistory('project-test1').result,
3176 'SUCCESS')
3177 self.assertEqual(self.getJobFromHistory('project-test2').result,
3178 'SUCCESS')
3179 self.assertEqual(A.data['status'], 'MERGED')
3180 self.assertEqual(A.reported, 2)
3181
3182 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08003183 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05003184 if os.path.exists(os.path.join(self.merger_src_root, p)):
3185 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04003186 if os.path.exists(os.path.join(self.executor_src_root, p)):
3187 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003188
3189 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
3190
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003191 B.addApproval('Code-Review', 2)
3192 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07003193 self.waitUntilSettled()
3194 self.assertEqual(self.getJobFromHistory('project-merge').result,
3195 'SUCCESS')
3196 self.assertEqual(self.getJobFromHistory('project-test1').result,
3197 'SUCCESS')
3198 self.assertEqual(self.getJobFromHistory('project-test2').result,
3199 'SUCCESS')
3200 self.assertEqual(B.data['status'], 'MERGED')
3201 self.assertEqual(B.reported, 2)
3202
James E. Blair892cca62017-08-09 11:36:58 -07003203 @simple_layout('layouts/untrusted-secrets.yaml')
3204 def test_untrusted_secrets(self):
3205 "Test untrusted secrets"
3206 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3207 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3208 self.waitUntilSettled()
3209
3210 self.assertHistory([])
3211 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07003212 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07003213 A.messages[0])
3214
James E. Blaire68d43f2017-04-19 14:03:30 -07003215 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08003216 def test_tags(self):
3217 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08003218 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3219 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3220 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3221 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3222 self.waitUntilSettled()
3223
James E. Blaire68d43f2017-04-19 14:03:30 -07003224 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003225
3226 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07003227 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003228 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07003229 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08003230
3231 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06003232 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07003233 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08003234
James E. Blair63bb0ef2013-07-29 17:14:51 -07003235 def test_timer(self):
3236 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003237 # This test can not use simple_layout because it must start
3238 # with a configuration which does not include a
3239 # timer-triggered job so that we have an opportunity to set
3240 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07003241 self.create_branch('org/project', 'stable')
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.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003244 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003245
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. Blair63bb0ef2013-07-29 17:14:51 -07003249 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07003250
3251 self.assertEqual(len(self.builds), 2)
3252
James E. Blair63bb0ef2013-07-29 17:14:51 -07003253 port = self.webapp.server.socket.getsockname()[1]
3254
Clint Byrum5e729e92017-02-08 06:34:57 -08003255 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003256 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07003257 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07003258 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003259
Paul Belanger174a8272017-03-14 13:20:10 -04003260 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07003261 # Stop queuing timer triggered jobs so that the assertions
3262 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003263 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003264 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07003265 self.waitUntilSettled()
3266 # If APScheduler is in mid-event when we remove the job, we
3267 # can end up with one more event firing, so give it an extra
3268 # second to settle.
3269 time.sleep(1)
3270 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003271 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07003272 self.waitUntilSettled()
3273
James E. Blair21037782017-07-19 11:56:55 -07003274 self.assertHistory([
3275 dict(name='project-bitrot', result='SUCCESS',
3276 ref='refs/heads/master'),
3277 dict(name='project-bitrot', result='SUCCESS',
3278 ref='refs/heads/stable'),
3279 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003280
3281 data = json.loads(data)
3282 status_jobs = set()
3283 for p in data['pipelines']:
3284 for q in p['change_queues']:
3285 for head in q['heads']:
3286 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07003287 for job in change['jobs']:
3288 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07003289 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003290
James E. Blair4f6033c2014-03-27 15:49:09 -07003291 def test_idle(self):
3292 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003293 # This test can not use simple_layout because it must start
3294 # with a configuration which does not include a
3295 # timer-triggered job so that we have an opportunity to set
3296 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003297 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003298
Clark Boylan3ee090a2014-04-03 20:55:09 -07003299 for x in range(1, 3):
3300 # Test that timer triggers periodic jobs even across
3301 # layout config reloads.
3302 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003303 self.commitConfigUpdate('common-config',
3304 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003305 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003306 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003307
Clark Boylan3ee090a2014-04-03 20:55:09 -07003308 # The pipeline triggers every second, so we should have seen
3309 # several by now.
3310 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003311
3312 # Stop queuing timer triggered jobs so that the assertions
3313 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003314 self.commitConfigUpdate('common-config',
3315 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003316 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003317 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003318 # If APScheduler is in mid-event when we remove the job,
3319 # we can end up with one more event firing, so give it an
3320 # extra second to settle.
3321 time.sleep(1)
3322 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003323 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003324 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003325 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003326 self.waitUntilSettled()
3327 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003328 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003329
James E. Blaircc324192017-04-19 14:36:08 -07003330 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003331 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003332 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3333 self.waitUntilSettled()
3334
3335 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3336 self.waitUntilSettled()
3337
James E. Blairff80a2f2013-12-27 13:24:06 -08003338 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003339
3340 # A.messages only holds what FakeGerrit places in it. Thus we
3341 # work on the knowledge of what the first message should be as
3342 # it is only configured to go to SMTP.
3343
3344 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003345 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003346 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003347 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003348 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003349 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003350
3351 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003352 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003353 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003354 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003355 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003356 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003357
James E. Blaire5910202013-12-27 09:50:31 -08003358 def test_timer_smtp(self):
3359 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003360 # This test can not use simple_layout because it must start
3361 # with a configuration which does not include a
3362 # timer-triggered job so that we have an opportunity to set
3363 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003364 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003365 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003366 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003367
Clark Boylan3ee090a2014-04-03 20:55:09 -07003368 # The pipeline triggers every second, so we should have seen
3369 # several by now.
3370 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003371 self.waitUntilSettled()
3372
Clark Boylan3ee090a2014-04-03 20:55:09 -07003373 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003374 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003375 self.waitUntilSettled()
3376 self.assertEqual(len(self.history), 2)
3377
James E. Blaire5910202013-12-27 09:50:31 -08003378 self.assertEqual(self.getJobFromHistory(
3379 'project-bitrot-stable-old').result, 'SUCCESS')
3380 self.assertEqual(self.getJobFromHistory(
3381 'project-bitrot-stable-older').result, 'SUCCESS')
3382
James E. Blairff80a2f2013-12-27 13:24:06 -08003383 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003384
3385 # A.messages only holds what FakeGerrit places in it. Thus we
3386 # work on the knowledge of what the first message should be as
3387 # it is only configured to go to SMTP.
3388
3389 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003390 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003391 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003392 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003393 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003394 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003395
Clark Boylan3ee090a2014-04-03 20:55:09 -07003396 # Stop queuing timer triggered jobs and let any that may have
3397 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003398 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003399 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003400 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003401 # If APScheduler is in mid-event when we remove the job, we
3402 # can end up with one more event firing, so give it an extra
3403 # second to settle.
3404 time.sleep(1)
3405 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003406 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003407 self.waitUntilSettled()
3408
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003409 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003410 def test_timer_sshkey(self):
3411 "Test that a periodic job can setup SSH key authentication"
3412 self.worker.hold_jobs_in_build = True
3413 self.config.set('zuul', 'layout_config',
3414 'tests/fixtures/layout-timer.yaml')
3415 self.sched.reconfigure(self.config)
3416 self.registerJobs()
3417
3418 # The pipeline triggers every second, so we should have seen
3419 # several by now.
3420 time.sleep(5)
3421 self.waitUntilSettled()
3422
3423 self.assertEqual(len(self.builds), 2)
3424
3425 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3426 self.assertTrue(os.path.isfile(ssh_wrapper))
3427 with open(ssh_wrapper) as f:
3428 ssh_wrapper_content = f.read()
3429 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3430 # In the unit tests Merger runs in the same process,
3431 # so we see its' environment variables
3432 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3433
3434 self.worker.release('.*')
3435 self.waitUntilSettled()
3436 self.assertEqual(len(self.history), 2)
3437
3438 self.assertEqual(self.getJobFromHistory(
3439 'project-bitrot-stable-old').result, 'SUCCESS')
3440 self.assertEqual(self.getJobFromHistory(
3441 'project-bitrot-stable-older').result, 'SUCCESS')
3442
3443 # Stop queuing timer triggered jobs and let any that may have
3444 # queued through so that end of test assertions pass.
3445 self.config.set('zuul', 'layout_config',
3446 'tests/fixtures/layout-no-timer.yaml')
3447 self.sched.reconfigure(self.config)
3448 self.registerJobs()
3449 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003450 # If APScheduler is in mid-event when we remove the job, we
3451 # can end up with one more event firing, so give it an extra
3452 # second to settle.
3453 time.sleep(1)
3454 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003455 self.worker.release('.*')
3456 self.waitUntilSettled()
3457
James E. Blair91e34592015-07-31 16:45:59 -07003458 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003459 "Test that the RPC client can enqueue a change"
3460 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003461 A.addApproval('Code-Review', 2)
3462 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003463
3464 client = zuul.rpcclient.RPCClient('127.0.0.1',
3465 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003466 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003467 r = client.enqueue(tenant='tenant-one',
3468 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003469 project='org/project',
3470 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003471 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003472 self.waitUntilSettled()
3473 self.assertEqual(self.getJobFromHistory('project-merge').result,
3474 'SUCCESS')
3475 self.assertEqual(self.getJobFromHistory('project-test1').result,
3476 'SUCCESS')
3477 self.assertEqual(self.getJobFromHistory('project-test2').result,
3478 'SUCCESS')
3479 self.assertEqual(A.data['status'], 'MERGED')
3480 self.assertEqual(A.reported, 2)
3481 self.assertEqual(r, True)
3482
James E. Blair91e34592015-07-31 16:45:59 -07003483 def test_client_enqueue_ref(self):
3484 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003485 p = "review.example.com/org/project"
3486 upstream = self.getUpstreamRepos([p])
3487 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3488 A.setMerged()
3489 A_commit = str(upstream[p].commit('master'))
3490 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003491
3492 client = zuul.rpcclient.RPCClient('127.0.0.1',
3493 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003494 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003495 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003496 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003497 pipeline='post',
3498 project='org/project',
3499 trigger='gerrit',
3500 ref='master',
3501 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003502 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003503 self.waitUntilSettled()
3504 job_names = [x.name for x in self.history]
3505 self.assertEqual(len(self.history), 1)
3506 self.assertIn('project-post', job_names)
3507 self.assertEqual(r, True)
3508
James E. Blairad28e912013-11-27 10:43:22 -08003509 def test_client_enqueue_negative(self):
3510 "Test that the RPC client returns errors"
3511 client = zuul.rpcclient.RPCClient('127.0.0.1',
3512 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003513 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003514 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003515 "Invalid tenant"):
3516 r = client.enqueue(tenant='tenant-foo',
3517 pipeline='gate',
3518 project='org/project',
3519 trigger='gerrit',
3520 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003521 self.assertEqual(r, False)
3522
3523 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003524 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003525 r = client.enqueue(tenant='tenant-one',
3526 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003527 project='project-does-not-exist',
3528 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003529 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003530 self.assertEqual(r, False)
3531
3532 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3533 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003534 r = client.enqueue(tenant='tenant-one',
3535 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003536 project='org/project',
3537 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003538 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003539 self.assertEqual(r, False)
3540
3541 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3542 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003543 r = client.enqueue(tenant='tenant-one',
3544 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003545 project='org/project',
3546 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003547 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003548 self.assertEqual(r, False)
3549
3550 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3551 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003552 r = client.enqueue(tenant='tenant-one',
3553 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003554 project='org/project',
3555 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003556 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003557 self.assertEqual(r, False)
3558
3559 self.waitUntilSettled()
3560 self.assertEqual(len(self.history), 0)
3561 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003562
3563 def test_client_promote(self):
3564 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003565 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003566 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3567 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3568 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003569 A.addApproval('Code-Review', 2)
3570 B.addApproval('Code-Review', 2)
3571 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003572
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003573 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3574 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3575 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003576
3577 self.waitUntilSettled()
3578
Paul Belangerbaca3132016-11-04 12:49:54 -04003579 tenant = self.sched.abide.tenants.get('tenant-one')
3580 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003581 enqueue_times = {}
3582 for item in items:
3583 enqueue_times[str(item.change)] = item.enqueue_time
3584
James E. Blair36658cf2013-12-06 17:53:48 -08003585 client = zuul.rpcclient.RPCClient('127.0.0.1',
3586 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003587 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003588 r = client.promote(tenant='tenant-one',
3589 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003590 change_ids=['2,1', '3,1'])
3591
Sean Daguef39b9ca2014-01-10 21:34:35 -05003592 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003593 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003594 for item in items:
3595 self.assertEqual(
3596 enqueue_times[str(item.change)], item.enqueue_time)
3597
James E. Blair78acec92014-02-06 07:11:32 -08003598 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003599 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003600 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003601 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003602 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003603 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003604 self.waitUntilSettled()
3605
3606 self.assertEqual(len(self.builds), 6)
3607 self.assertEqual(self.builds[0].name, 'project-test1')
3608 self.assertEqual(self.builds[1].name, 'project-test2')
3609 self.assertEqual(self.builds[2].name, 'project-test1')
3610 self.assertEqual(self.builds[3].name, 'project-test2')
3611 self.assertEqual(self.builds[4].name, 'project-test1')
3612 self.assertEqual(self.builds[5].name, 'project-test2')
3613
Paul Belangerbaca3132016-11-04 12:49:54 -04003614 self.assertTrue(self.builds[0].hasChanges(B))
3615 self.assertFalse(self.builds[0].hasChanges(A))
3616 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003617
Paul Belangerbaca3132016-11-04 12:49:54 -04003618 self.assertTrue(self.builds[2].hasChanges(B))
3619 self.assertTrue(self.builds[2].hasChanges(C))
3620 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003621
Paul Belangerbaca3132016-11-04 12:49:54 -04003622 self.assertTrue(self.builds[4].hasChanges(B))
3623 self.assertTrue(self.builds[4].hasChanges(C))
3624 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003625
Paul Belanger174a8272017-03-14 13:20:10 -04003626 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003627 self.waitUntilSettled()
3628
3629 self.assertEqual(A.data['status'], 'MERGED')
3630 self.assertEqual(A.reported, 2)
3631 self.assertEqual(B.data['status'], 'MERGED')
3632 self.assertEqual(B.reported, 2)
3633 self.assertEqual(C.data['status'], 'MERGED')
3634 self.assertEqual(C.reported, 2)
3635
James E. Blair36658cf2013-12-06 17:53:48 -08003636 self.assertEqual(r, True)
3637
3638 def test_client_promote_dependent(self):
3639 "Test that the RPC client can promote a dependent change"
3640 # C (depends on B) -> B -> A ; then promote C to get:
3641 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003642 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003643 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3644 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3645 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3646
3647 C.setDependsOn(B, 1)
3648
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003649 A.addApproval('Code-Review', 2)
3650 B.addApproval('Code-Review', 2)
3651 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003652
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003653 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3654 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3655 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003656
3657 self.waitUntilSettled()
3658
3659 client = zuul.rpcclient.RPCClient('127.0.0.1',
3660 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003661 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003662 r = client.promote(tenant='tenant-one',
3663 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003664 change_ids=['3,1'])
3665
James E. Blair78acec92014-02-06 07:11:32 -08003666 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003667 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003668 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003669 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003670 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003671 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003672 self.waitUntilSettled()
3673
3674 self.assertEqual(len(self.builds), 6)
3675 self.assertEqual(self.builds[0].name, 'project-test1')
3676 self.assertEqual(self.builds[1].name, 'project-test2')
3677 self.assertEqual(self.builds[2].name, 'project-test1')
3678 self.assertEqual(self.builds[3].name, 'project-test2')
3679 self.assertEqual(self.builds[4].name, 'project-test1')
3680 self.assertEqual(self.builds[5].name, 'project-test2')
3681
Paul Belangerbaca3132016-11-04 12:49:54 -04003682 self.assertTrue(self.builds[0].hasChanges(B))
3683 self.assertFalse(self.builds[0].hasChanges(A))
3684 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003685
Paul Belangerbaca3132016-11-04 12:49:54 -04003686 self.assertTrue(self.builds[2].hasChanges(B))
3687 self.assertTrue(self.builds[2].hasChanges(C))
3688 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003689
Paul Belangerbaca3132016-11-04 12:49:54 -04003690 self.assertTrue(self.builds[4].hasChanges(B))
3691 self.assertTrue(self.builds[4].hasChanges(C))
3692 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003693
Paul Belanger174a8272017-03-14 13:20:10 -04003694 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003695 self.waitUntilSettled()
3696
3697 self.assertEqual(A.data['status'], 'MERGED')
3698 self.assertEqual(A.reported, 2)
3699 self.assertEqual(B.data['status'], 'MERGED')
3700 self.assertEqual(B.reported, 2)
3701 self.assertEqual(C.data['status'], 'MERGED')
3702 self.assertEqual(C.reported, 2)
3703
James E. Blair36658cf2013-12-06 17:53:48 -08003704 self.assertEqual(r, True)
3705
3706 def test_client_promote_negative(self):
3707 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003708 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003709 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003710 A.addApproval('Code-Review', 2)
3711 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003712 self.waitUntilSettled()
3713
3714 client = zuul.rpcclient.RPCClient('127.0.0.1',
3715 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003716 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003717
3718 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003719 r = client.promote(tenant='tenant-one',
3720 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003721 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003722 self.assertEqual(r, False)
3723
3724 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003725 r = client.promote(tenant='tenant-one',
3726 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003727 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003728 self.assertEqual(r, False)
3729
Paul Belanger174a8272017-03-14 13:20:10 -04003730 self.executor_server.hold_jobs_in_build = False
3731 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003732 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003733
James E. Blaircc324192017-04-19 14:36:08 -07003734 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003735 def test_queue_rate_limiting(self):
3736 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003737 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3739 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3740 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3741
3742 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003743 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003744
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003745 A.addApproval('Code-Review', 2)
3746 B.addApproval('Code-Review', 2)
3747 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003748
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003749 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3750 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3751 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003752 self.waitUntilSettled()
3753
3754 # Only A and B will have their merge jobs queued because
3755 # window is 2.
3756 self.assertEqual(len(self.builds), 2)
3757 self.assertEqual(self.builds[0].name, 'project-merge')
3758 self.assertEqual(self.builds[1].name, 'project-merge')
3759
James E. Blair732ee862017-04-21 07:49:26 -07003760 # Release the merge jobs one at a time.
3761 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003762 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003763 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003764 self.waitUntilSettled()
3765
3766 # Only A and B will have their test jobs queued because
3767 # window is 2.
3768 self.assertEqual(len(self.builds), 4)
3769 self.assertEqual(self.builds[0].name, 'project-test1')
3770 self.assertEqual(self.builds[1].name, 'project-test2')
3771 self.assertEqual(self.builds[2].name, 'project-test1')
3772 self.assertEqual(self.builds[3].name, 'project-test2')
3773
Paul Belanger174a8272017-03-14 13:20:10 -04003774 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003775 self.waitUntilSettled()
3776
James E. Blaircc324192017-04-19 14:36:08 -07003777 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003778 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003779 # A failed so window is reduced by 1 to 1.
3780 self.assertEqual(queue.window, 1)
3781 self.assertEqual(queue.window_floor, 1)
3782 self.assertEqual(A.data['status'], 'NEW')
3783
3784 # Gate is reset and only B's merge job is queued because
3785 # window shrunk to 1.
3786 self.assertEqual(len(self.builds), 1)
3787 self.assertEqual(self.builds[0].name, 'project-merge')
3788
Paul Belanger174a8272017-03-14 13:20:10 -04003789 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003790 self.waitUntilSettled()
3791
3792 # Only B's test jobs are queued because window is still 1.
3793 self.assertEqual(len(self.builds), 2)
3794 self.assertEqual(self.builds[0].name, 'project-test1')
3795 self.assertEqual(self.builds[1].name, 'project-test2')
3796
Paul Belanger174a8272017-03-14 13:20:10 -04003797 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003798 self.waitUntilSettled()
3799
3800 # B was successfully merged so window is increased to 2.
3801 self.assertEqual(queue.window, 2)
3802 self.assertEqual(queue.window_floor, 1)
3803 self.assertEqual(B.data['status'], 'MERGED')
3804
3805 # Only C is left and its merge job is queued.
3806 self.assertEqual(len(self.builds), 1)
3807 self.assertEqual(self.builds[0].name, 'project-merge')
3808
Paul Belanger174a8272017-03-14 13:20:10 -04003809 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003810 self.waitUntilSettled()
3811
3812 # After successful merge job the test jobs for C are queued.
3813 self.assertEqual(len(self.builds), 2)
3814 self.assertEqual(self.builds[0].name, 'project-test1')
3815 self.assertEqual(self.builds[1].name, 'project-test2')
3816
Paul Belanger174a8272017-03-14 13:20:10 -04003817 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003818 self.waitUntilSettled()
3819
3820 # C successfully merged so window is bumped to 3.
3821 self.assertEqual(queue.window, 3)
3822 self.assertEqual(queue.window_floor, 1)
3823 self.assertEqual(C.data['status'], 'MERGED')
3824
James E. Blaircc324192017-04-19 14:36:08 -07003825 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003826 def test_queue_rate_limiting_dependent(self):
3827 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003828 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003829 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3830 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3831 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3832
3833 B.setDependsOn(A, 1)
3834
Paul Belanger174a8272017-03-14 13:20:10 -04003835 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003836
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003837 A.addApproval('Code-Review', 2)
3838 B.addApproval('Code-Review', 2)
3839 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003840
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003841 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3842 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3843 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003844 self.waitUntilSettled()
3845
3846 # Only A and B will have their merge jobs queued because
3847 # window is 2.
3848 self.assertEqual(len(self.builds), 2)
3849 self.assertEqual(self.builds[0].name, 'project-merge')
3850 self.assertEqual(self.builds[1].name, 'project-merge')
3851
James E. Blairf4a5f022017-04-18 14:01:10 -07003852 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003853
3854 # Only A and B will have their test jobs queued because
3855 # window is 2.
3856 self.assertEqual(len(self.builds), 4)
3857 self.assertEqual(self.builds[0].name, 'project-test1')
3858 self.assertEqual(self.builds[1].name, 'project-test2')
3859 self.assertEqual(self.builds[2].name, 'project-test1')
3860 self.assertEqual(self.builds[3].name, 'project-test2')
3861
Paul Belanger174a8272017-03-14 13:20:10 -04003862 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003863 self.waitUntilSettled()
3864
James E. Blaircc324192017-04-19 14:36:08 -07003865 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003866 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003867 # A failed so window is reduced by 1 to 1.
3868 self.assertEqual(queue.window, 1)
3869 self.assertEqual(queue.window_floor, 1)
3870 self.assertEqual(A.data['status'], 'NEW')
3871 self.assertEqual(B.data['status'], 'NEW')
3872
3873 # Gate is reset and only C's merge job is queued because
3874 # window shrunk to 1 and A and B were dequeued.
3875 self.assertEqual(len(self.builds), 1)
3876 self.assertEqual(self.builds[0].name, 'project-merge')
3877
James E. Blairf4a5f022017-04-18 14:01:10 -07003878 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003879
3880 # Only C's test jobs are queued because window is still 1.
3881 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003882 builds = self.getSortedBuilds()
3883 self.assertEqual(builds[0].name, 'project-test1')
3884 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003885
Paul Belanger174a8272017-03-14 13:20:10 -04003886 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003887 self.waitUntilSettled()
3888
3889 # C was successfully merged so window is increased to 2.
3890 self.assertEqual(queue.window, 2)
3891 self.assertEqual(queue.window_floor, 1)
3892 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003893
James E. Blair8fd207b2017-11-30 13:44:38 -08003894 @simple_layout('layouts/reconfigure-window.yaml')
3895 def test_reconfigure_window_shrink(self):
3896 # Test the active window shrinking during reconfiguration
3897 self.executor_server.hold_jobs_in_build = True
3898
3899 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3900 A.addApproval('Code-Review', 2)
3901 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3902 self.waitUntilSettled()
3903 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3904 B.addApproval('Code-Review', 2)
3905 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3906 self.waitUntilSettled()
3907
3908 tenant = self.sched.abide.tenants.get('tenant-one')
3909 queue = tenant.layout.pipelines['gate'].queues[0]
3910 self.assertEqual(queue.window, 20)
3911 self.assertTrue(len(self.builds), 4)
3912
3913 self.executor_server.release('job1')
3914 self.waitUntilSettled()
3915 self.commitConfigUpdate('org/common-config',
3916 'layouts/reconfigure-window2.yaml')
3917 self.sched.reconfigure(self.config)
3918 tenant = self.sched.abide.tenants.get('tenant-one')
3919 queue = tenant.layout.pipelines['gate'].queues[0]
James E. Blair74be3812017-11-30 14:17:25 -08003920 # Even though we have configured a smaller window, the value
3921 # on the existing shared queue should be used.
3922 self.assertEqual(queue.window, 20)
3923 self.assertTrue(len(self.builds), 4)
3924
3925 self.sched.reconfigure(self.config)
3926 tenant = self.sched.abide.tenants.get('tenant-one')
3927 queue = tenant.layout.pipelines['gate'].queues[0]
3928 self.assertEqual(queue.window, 20)
3929 self.assertTrue(len(self.builds), 4)
3930
3931 self.executor_server.hold_jobs_in_build = False
3932 self.executor_server.release()
3933
3934 self.waitUntilSettled()
3935 self.assertHistory([
3936 dict(name='job1', result='SUCCESS', changes='1,1'),
3937 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3938 dict(name='job2', result='SUCCESS', changes='1,1'),
3939 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3940 ], ordered=False)
3941
3942 @simple_layout('layouts/reconfigure-window-fixed.yaml')
3943 def test_reconfigure_window_fixed(self):
3944 # Test the active window shrinking during reconfiguration
3945 self.executor_server.hold_jobs_in_build = True
3946
3947 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3948 A.addApproval('Code-Review', 2)
3949 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3950 self.waitUntilSettled()
3951 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3952 B.addApproval('Code-Review', 2)
3953 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3954 self.waitUntilSettled()
3955
3956 tenant = self.sched.abide.tenants.get('tenant-one')
3957 queue = tenant.layout.pipelines['gate'].queues[0]
3958 self.assertEqual(queue.window, 2)
3959 self.assertTrue(len(self.builds), 4)
3960
3961 self.executor_server.release('job1')
3962 self.waitUntilSettled()
3963 self.commitConfigUpdate('org/common-config',
3964 'layouts/reconfigure-window-fixed2.yaml')
3965 self.sched.reconfigure(self.config)
3966 tenant = self.sched.abide.tenants.get('tenant-one')
3967 queue = tenant.layout.pipelines['gate'].queues[0]
3968 # Because we have configured a static window, it should
3969 # be allowed to shrink on reconfiguration.
James E. Blair8fd207b2017-11-30 13:44:38 -08003970 self.assertEqual(queue.window, 1)
James E. Blair74be3812017-11-30 14:17:25 -08003971 # B is outside the window, but still marked active until the
3972 # next pass through the queue processor, so its builds haven't
3973 # been canceled.
James E. Blair8fd207b2017-11-30 13:44:38 -08003974 self.assertTrue(len(self.builds), 4)
3975
3976 self.sched.reconfigure(self.config)
3977 tenant = self.sched.abide.tenants.get('tenant-one')
3978 queue = tenant.layout.pipelines['gate'].queues[0]
3979 self.assertEqual(queue.window, 1)
3980 # B's builds have been canceled now
3981 self.assertTrue(len(self.builds), 2)
3982
3983 self.executor_server.hold_jobs_in_build = False
3984 self.executor_server.release()
James E. Blair74be3812017-11-30 14:17:25 -08003985
3986 # B's builds will be restarted and will show up in our history
3987 # twice.
James E. Blair8fd207b2017-11-30 13:44:38 -08003988 self.waitUntilSettled()
3989 self.assertHistory([
3990 dict(name='job1', result='SUCCESS', changes='1,1'),
3991 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3992 dict(name='job2', result='SUCCESS', changes='1,1'),
3993 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3994 dict(name='job1', result='SUCCESS', changes='1,1 2,1'),
3995 dict(name='job2', result='SUCCESS', changes='1,1 2,1'),
3996 ], ordered=False)
3997
3998 @simple_layout('layouts/reconfigure-remove-add.yaml')
3999 def test_reconfigure_remove_add(self):
4000 # Test removing, then adding a job while in queue
4001 self.executor_server.hold_jobs_in_build = True
4002
4003 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4004 A.addApproval('Code-Review', 2)
4005 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4006 self.waitUntilSettled()
4007 self.assertTrue(len(self.builds), 2)
4008 self.executor_server.release('job2')
4009 self.assertTrue(len(self.builds), 1)
4010
4011 # Remove job2
4012 self.commitConfigUpdate('org/common-config',
4013 'layouts/reconfigure-remove-add2.yaml')
4014 self.sched.reconfigure(self.config)
4015 self.assertTrue(len(self.builds), 1)
4016
4017 # Add job2 back
4018 self.commitConfigUpdate('org/common-config',
4019 'layouts/reconfigure-remove-add.yaml')
4020 self.sched.reconfigure(self.config)
4021 self.assertTrue(len(self.builds), 2)
4022
4023 self.executor_server.hold_jobs_in_build = False
4024 self.executor_server.release()
4025 # This will run new builds for B
4026 self.waitUntilSettled()
4027 self.assertHistory([
4028 dict(name='job2', result='SUCCESS', changes='1,1'),
4029 dict(name='job1', result='SUCCESS', changes='1,1'),
4030 dict(name='job2', result='SUCCESS', changes='1,1'),
4031 ], ordered=False)
4032
Joshua Heskethba8776a2014-01-12 14:35:40 +08004033 def test_worker_update_metadata(self):
4034 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04004035 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08004036
4037 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004038 A.addApproval('Code-Review', 2)
4039 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08004040 self.waitUntilSettled()
4041
Clint Byrumd4de79a2017-05-27 11:39:13 -07004042 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08004043
4044 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07004045 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08004046
4047 start = time.time()
4048 while True:
4049 if time.time() - start > 10:
4050 raise Exception("Timeout waiting for gearman server to report "
4051 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07004052 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05004053 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08004054 break
4055 else:
4056 time.sleep(0)
4057
4058 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05004059 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08004060
Paul Belanger174a8272017-03-14 13:20:10 -04004061 self.executor_server.hold_jobs_in_build = False
4062 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08004063 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004064
James E. Blaircc324192017-04-19 14:36:08 -07004065 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004066 def test_footer_message(self):
4067 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004068 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004069 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04004070 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004071 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004072 self.waitUntilSettled()
4073
4074 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004075 B.addApproval('Code-Review', 2)
4076 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004077 self.waitUntilSettled()
4078
4079 self.assertEqual(2, len(self.smtp_messages))
4080
Adam Gandelman82258fd2017-02-07 16:51:58 -08004081 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004082Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08004083http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004084
Adam Gandelman82258fd2017-02-07 16:51:58 -08004085 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11004086For CI problems and help debugging, contact ci@example.org"""
4087
Adam Gandelman82258fd2017-02-07 16:51:58 -08004088 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
4089 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
4090 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
4091 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11004092
Tobias Henkel9842bd72017-05-16 13:40:03 +02004093 @simple_layout('layouts/unmanaged-project.yaml')
4094 def test_unmanaged_project_start_message(self):
4095 "Test start reporting is not done for unmanaged projects."
4096 self.init_repo("org/project", tag='init')
4097 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4098 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4099 self.waitUntilSettled()
4100
4101 self.assertEqual(0, len(A.messages))
4102
Clint Byrumb9de8be2017-05-27 13:49:19 -07004103 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11004104 def test_merge_failure_reporters(self):
4105 """Check that the config is set up correctly"""
4106
Clint Byrumb9de8be2017-05-27 13:49:19 -07004107 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11004108 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00004109 "Merge Failed.\n\nThis change or one of its cross-repo "
4110 "dependencies was unable to be automatically merged with the "
4111 "current state of its repository. Please rebase the change and "
4112 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07004113 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11004114 self.assertEqual(
4115 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07004116 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11004117
4118 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004119 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11004120 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004121 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11004122
4123 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07004124 tenant.layout.pipelines['check'].merge_failure_actions[0],
4125 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11004126
4127 self.assertTrue(
4128 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07004129 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004130 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004131 zuul.driver.smtp.smtpreporter.SMTPReporter) and
4132 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004133 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004134 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11004135 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07004136 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004137 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004138 gerritreporter.GerritReporter) and
4139 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11004140 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07004141 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11004142 )
4143 )
4144
James E. Blairec056492016-07-22 09:45:56 -07004145 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11004146 def test_merge_failure_reports(self):
4147 """Check that when a change fails to merge the correct message is sent
4148 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08004149 self.updateConfigLayout(
4150 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11004151 self.sched.reconfigure(self.config)
4152 self.registerJobs()
4153
4154 # Check a test failure isn't reported to SMTP
4155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004156 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04004157 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004158 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11004159 self.waitUntilSettled()
4160
4161 self.assertEqual(3, len(self.history)) # 3 jobs
4162 self.assertEqual(0, len(self.smtp_messages))
4163
4164 # Check a merge failure is reported to SMTP
4165 # B should be merged, but C will conflict with B
4166 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4167 B.addPatchset(['conflict'])
4168 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4169 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004170 B.addApproval('Code-Review', 2)
4171 C.addApproval('Code-Review', 2)
4172 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4173 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11004174 self.waitUntilSettled()
4175
4176 self.assertEqual(6, len(self.history)) # A and B jobs
4177 self.assertEqual(1, len(self.smtp_messages))
4178 self.assertEqual('The merge failed! For more information...',
4179 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11004180
James E. Blairec056492016-07-22 09:45:56 -07004181 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08004182 def test_default_merge_failure_reports(self):
4183 """Check that the default merge failure reports are correct."""
4184
4185 # A should report success, B should report merge failure.
4186 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4187 A.addPatchset(['conflict'])
4188 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4189 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004190 A.addApproval('Code-Review', 2)
4191 B.addApproval('Code-Review', 2)
4192 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4193 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08004194 self.waitUntilSettled()
4195
4196 self.assertEqual(3, len(self.history)) # A jobs
4197 self.assertEqual(A.reported, 2)
4198 self.assertEqual(B.reported, 2)
4199 self.assertEqual(A.data['status'], 'MERGED')
4200 self.assertEqual(B.data['status'], 'NEW')
4201 self.assertIn('Build succeeded', A.messages[1])
4202 self.assertIn('Merge Failed', B.messages[1])
4203 self.assertIn('automatically merged', B.messages[1])
4204 self.assertNotIn('logs.example.com', B.messages[1])
4205 self.assertNotIn('SKIPPED', B.messages[1])
4206
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004207 def test_client_get_running_jobs(self):
4208 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04004209 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004211 A.addApproval('Code-Review', 2)
4212 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004213 self.waitUntilSettled()
4214
4215 client = zuul.rpcclient.RPCClient('127.0.0.1',
4216 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07004217 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004218
4219 # Wait for gearman server to send the initial workData back to zuul
4220 start = time.time()
4221 while True:
4222 if time.time() - start > 10:
4223 raise Exception("Timeout waiting for gearman server to report "
4224 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07004225 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05004226 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004227 break
4228 else:
4229 time.sleep(0)
4230
4231 running_items = client.get_running_jobs()
4232
4233 self.assertEqual(1, len(running_items))
4234 running_item = running_items[0]
4235 self.assertEqual([], running_item['failing_reasons'])
4236 self.assertEqual([], running_item['items_behind'])
James E. Blair0e4c7912018-01-02 14:20:17 -08004237 self.assertEqual('https://review.example.com/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004238 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004239 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004240 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004241 self.assertEqual(True, running_item['active'])
4242 self.assertEqual('1,1', running_item['id'])
4243
4244 self.assertEqual(3, len(running_item['jobs']))
4245 for job in running_item['jobs']:
4246 if job['name'] == 'project-merge':
4247 self.assertEqual('project-merge', job['name'])
4248 self.assertEqual('gate', job['pipeline'])
4249 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05004250 self.assertEqual(
Tristan Cacqueraya4996f12017-09-20 01:00:28 +00004251 'stream.html?uuid={uuid}&logfile=console.log'
Tobias Henkelb4407fc2017-07-07 13:52:56 +02004252 .format(uuid=job['uuid']), job['url'])
4253 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05004254 'finger://{hostname}/{uuid}'.format(
4255 hostname=self.executor_server.hostname,
4256 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02004257 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07004258 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004259 self.assertEqual(False, job['canceled'])
4260 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05004261 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004262 self.assertEqual('gate', job['pipeline'])
4263 break
4264
Paul Belanger174a8272017-03-14 13:20:10 -04004265 self.executor_server.hold_jobs_in_build = False
4266 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08004267 self.waitUntilSettled()
4268
4269 running_items = client.get_running_jobs()
4270 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07004271
James E. Blairb70e55a2017-04-19 12:57:02 -07004272 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004273 def test_nonvoting_pipeline(self):
4274 "Test that a nonvoting pipeline (experimental) can still report"
4275
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004276 A = self.fake_gerrit.addFakeChange('org/experimental-project',
4277 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004278 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4279 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04004280
4281 self.assertEqual(self.getJobFromHistory('project-merge').result,
4282 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10004283 self.assertEqual(
4284 self.getJobFromHistory('experimental-project-test').result,
4285 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07004286 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08004287
Jesse Keatingcc67b122017-04-19 14:54:57 -07004288 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004289 def test_disable_at(self):
4290 "Test a pipeline will only report to the disabled trigger when failing"
4291
Jesse Keatingcc67b122017-04-19 14:54:57 -07004292 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004293 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004294 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004295 0, tenant.layout.pipelines['check']._consecutive_failures)
4296 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004297
4298 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4299 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4300 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4301 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4302 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4303 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4304 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4305 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4306 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4307 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4308 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4309
Paul Belanger174a8272017-03-14 13:20:10 -04004310 self.executor_server.failJob('project-test1', A)
4311 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004312 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004313 self.executor_server.failJob('project-test1', D)
4314 self.executor_server.failJob('project-test1', E)
4315 self.executor_server.failJob('project-test1', F)
4316 self.executor_server.failJob('project-test1', G)
4317 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004318 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004319 self.executor_server.failJob('project-test1', J)
4320 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004321
4322 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4323 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4324 self.waitUntilSettled()
4325
4326 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004327 2, tenant.layout.pipelines['check']._consecutive_failures)
4328 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004329
4330 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4331 self.waitUntilSettled()
4332
4333 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004334 0, tenant.layout.pipelines['check']._consecutive_failures)
4335 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004336
4337 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4338 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4339 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4340 self.waitUntilSettled()
4341
4342 # We should be disabled now
4343 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004344 3, tenant.layout.pipelines['check']._consecutive_failures)
4345 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004346
4347 # We need to wait between each of these patches to make sure the
4348 # smtp messages come back in an expected order
4349 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4350 self.waitUntilSettled()
4351 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4352 self.waitUntilSettled()
4353 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4354 self.waitUntilSettled()
4355
4356 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4357 # leaving a message on each change
4358 self.assertEqual(1, len(A.messages))
4359 self.assertIn('Build failed.', A.messages[0])
4360 self.assertEqual(1, len(B.messages))
4361 self.assertIn('Build failed.', B.messages[0])
4362 self.assertEqual(1, len(C.messages))
4363 self.assertIn('Build succeeded.', C.messages[0])
4364 self.assertEqual(1, len(D.messages))
4365 self.assertIn('Build failed.', D.messages[0])
4366 self.assertEqual(1, len(E.messages))
4367 self.assertIn('Build failed.', E.messages[0])
4368 self.assertEqual(1, len(F.messages))
4369 self.assertIn('Build failed.', F.messages[0])
4370
4371 # The last 3 (GHI) would have only reported via smtp.
4372 self.assertEqual(3, len(self.smtp_messages))
4373 self.assertEqual(0, len(G.messages))
4374 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004375 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004376 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004377 self.assertEqual(0, len(H.messages))
4378 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004379 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004380 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004381 self.assertEqual(0, len(I.messages))
4382 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004383 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004384 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004385
4386 # Now reload the configuration (simulate a HUP) to check the pipeline
4387 # comes out of disabled
4388 self.sched.reconfigure(self.config)
4389
Jesse Keatingcc67b122017-04-19 14:54:57 -07004390 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004391
4392 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004393 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004394 0, tenant.layout.pipelines['check']._consecutive_failures)
4395 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004396
4397 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4398 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4399 self.waitUntilSettled()
4400
4401 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004402 2, tenant.layout.pipelines['check']._consecutive_failures)
4403 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004404
4405 # J and K went back to gerrit
4406 self.assertEqual(1, len(J.messages))
4407 self.assertIn('Build failed.', J.messages[0])
4408 self.assertEqual(1, len(K.messages))
4409 self.assertIn('Build failed.', K.messages[0])
4410 # No more messages reported via smtp
4411 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004412
James E. Blaircc020532017-04-19 13:04:44 -07004413 @simple_layout('layouts/one-job-project.yaml')
4414 def test_one_job_project(self):
4415 "Test that queueing works with one job"
4416 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4417 'master', 'A')
4418 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4419 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004420 A.addApproval('Code-Review', 2)
4421 B.addApproval('Code-Review', 2)
4422 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4423 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004424 self.waitUntilSettled()
4425
4426 self.assertEqual(A.data['status'], 'MERGED')
4427 self.assertEqual(A.reported, 2)
4428 self.assertEqual(B.data['status'], 'MERGED')
4429 self.assertEqual(B.reported, 2)
4430
Paul Belanger32023542017-10-06 14:48:50 -04004431 def test_job_aborted(self):
4432 "Test that if a execute server aborts a job, it is run again"
4433 self.executor_server.hold_jobs_in_build = True
4434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4435 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4436 self.waitUntilSettled()
4437
4438 self.executor_server.release('.*-merge')
4439 self.waitUntilSettled()
4440
4441 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004442
4443 # first abort
4444 self.builds[0].aborted = True
4445 self.executor_server.release('.*-test*')
4446 self.waitUntilSettled()
4447 self.assertEqual(len(self.builds), 1)
4448
4449 # second abort
4450 self.builds[0].aborted = True
4451 self.executor_server.release('.*-test*')
4452 self.waitUntilSettled()
4453 self.assertEqual(len(self.builds), 1)
4454
4455 # third abort
4456 self.builds[0].aborted = True
4457 self.executor_server.release('.*-test*')
4458 self.waitUntilSettled()
4459 self.assertEqual(len(self.builds), 1)
4460
4461 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004462 self.builds[0].aborted = True
4463 self.executor_server.release('.*-test*')
4464 self.waitUntilSettled()
4465 self.assertEqual(len(self.builds), 1)
4466
4467 self.executor_server.hold_jobs_in_build = False
4468 self.executor_server.release()
4469 self.waitUntilSettled()
4470
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004471 self.assertEqual(len(self.history), 7)
4472 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004473 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4474
Paul Belanger71d98172016-11-08 10:56:31 -05004475 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004476 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004477
Paul Belanger174a8272017-03-14 13:20:10 -04004478 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004479 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4480 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4481 self.waitUntilSettled()
4482
Paul Belanger174a8272017-03-14 13:20:10 -04004483 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004484 self.waitUntilSettled()
4485
4486 self.assertEqual(len(self.builds), 2)
4487 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004488 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004489 self.waitUntilSettled()
4490
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004491 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004492 self.assertEqual(len(self.builds), 1,
4493 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004494 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004495 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004496 self.waitUntilSettled()
4497
Paul Belanger174a8272017-03-14 13:20:10 -04004498 self.executor_server.hold_jobs_in_build = False
4499 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004500 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004501 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004502 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4503 self.assertEqual(A.reported, 1)
4504 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004505
James E. Blair15be0e12017-01-03 13:45:20 -08004506 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004507 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004508
4509 self.fake_nodepool.paused = True
4510 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004511 A.addApproval('Code-Review', 2)
4512 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004513 self.waitUntilSettled()
4514
4515 self.zk.client.stop()
4516 self.zk.client.start()
4517 self.fake_nodepool.paused = False
4518 self.waitUntilSettled()
4519
4520 self.assertEqual(A.data['status'], 'MERGED')
4521 self.assertEqual(A.reported, 2)
4522
James E. Blair6ab79e02017-01-06 10:10:17 -08004523 def test_nodepool_failure(self):
4524 "Test that jobs are reported after a nodepool failure"
4525
4526 self.fake_nodepool.paused = True
4527 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004528 A.addApproval('Code-Review', 2)
4529 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004530 self.waitUntilSettled()
4531
4532 req = self.fake_nodepool.getNodeRequests()[0]
4533 self.fake_nodepool.addFailRequest(req)
4534
4535 self.fake_nodepool.paused = False
4536 self.waitUntilSettled()
4537
4538 self.assertEqual(A.data['status'], 'NEW')
4539 self.assertEqual(A.reported, 2)
4540 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4541 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4542 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4543
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004544 def test_nodepool_priority(self):
4545 "Test that nodes are requested at the correct priority"
4546
4547 self.fake_nodepool.paused = True
4548
4549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4550 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4551
4552 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4553 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4554
4555 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4556 C.addApproval('Code-Review', 2)
4557 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4558
4559 self.waitUntilSettled()
4560
4561 reqs = self.fake_nodepool.getNodeRequests()
4562
4563 # The requests come back sorted by oid. Since we have three requests
4564 # for the three changes each with a different priority.
4565 # Also they get a serial number based on order they were received
4566 # so the number on the endof the oid should map to order submitted.
4567
4568 # * gate first - high priority - change C
4569 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4570 self.assertEqual(reqs[0]['node_types'], ['label1'])
4571 # * check second - normal priority - change B
4572 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4573 self.assertEqual(reqs[1]['node_types'], ['label1'])
4574 # * post third - low priority - change A
4575 # additionally, the post job defined uses an ubuntu-xenial node,
4576 # so we include that check just as an extra verification
4577 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4578 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4579
4580 self.fake_nodepool.paused = False
4581 self.waitUntilSettled()
4582
James E. Blairfceaf412017-09-29 13:44:57 -07004583 @simple_layout('layouts/multiple-templates.yaml')
4584 def test_multiple_project_templates(self):
4585 # Test that applying multiple project templates to a project
4586 # doesn't alter them when used for a second project.
4587 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4588 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4589 self.waitUntilSettled()
4590
4591 build = self.getJobFromHistory('py27')
4592 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4593
James E. Blair29c77002017-10-05 14:56:35 -07004594 def test_pending_merge_in_reconfig(self):
4595 # Test that if we are waiting for an outstanding merge on
4596 # reconfiguration that we continue to do so.
4597 self.gearman_server.hold_merge_jobs_in_queue = True
4598 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4599 A.setMerged()
4600 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4601 self.waitUntilSettled()
4602 # Reconfigure while we still have an outstanding merge job
4603 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4604 None)
4605 self.waitUntilSettled()
4606 # Verify the merge job is still running and that the item is
4607 # in the pipeline
4608 self.assertEqual(len(self.sched.merger.jobs), 1)
4609 tenant = self.sched.abide.tenants.get('tenant-one')
4610 pipeline = tenant.layout.pipelines['post']
4611 self.assertEqual(len(pipeline.getAllItems()), 1)
4612 self.gearman_server.hold_merge_jobs_in_queue = False
4613 self.gearman_server.release()
4614 self.waitUntilSettled()
4615
James E. Blairc32a8352017-10-11 16:27:50 -07004616 @simple_layout('layouts/parent-matchers.yaml')
4617 def test_parent_matchers(self):
4618 "Test that if a job's parent does not match, the job does not run"
4619 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4620 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4621 self.waitUntilSettled()
4622
4623 self.assertHistory([])
4624
4625 files = {'foo.txt': ''}
4626 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B',
4627 files=files)
4628 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4629 self.waitUntilSettled()
4630
4631 files = {'bar.txt': ''}
4632 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C',
4633 files=files)
4634 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4635 self.waitUntilSettled()
4636
4637 files = {'foo.txt': '', 'bar.txt': ''}
4638 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D',
4639 files=files)
4640 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4641 self.waitUntilSettled()
4642
4643 self.assertHistory([
4644 dict(name='child-job', result='SUCCESS', changes='2,1'),
4645 dict(name='child-job', result='SUCCESS', changes='3,1'),
4646 dict(name='child-job', result='SUCCESS', changes='4,1'),
4647 ])
4648
Arieb6f068c2016-10-09 13:11:06 +03004649
James E. Blaira002b032017-04-18 10:35:48 -07004650class TestExecutor(ZuulTestCase):
4651 tenant_config_file = 'config/single-tenant/main.yaml'
4652
4653 def assertFinalState(self):
4654 # In this test, we expect to shut down in a non-final state,
4655 # so skip these checks.
4656 pass
4657
4658 def assertCleanShutdown(self):
4659 self.log.debug("Assert clean shutdown")
4660
4661 # After shutdown, make sure no jobs are running
4662 self.assertEqual({}, self.executor_server.job_workers)
4663
4664 # Make sure that git.Repo objects have been garbage collected.
4665 repos = []
4666 gc.collect()
4667 for obj in gc.get_objects():
4668 if isinstance(obj, git.Repo):
4669 self.log.debug("Leaked git repo object: %s" % repr(obj))
4670 repos.append(obj)
4671 self.assertEqual(len(repos), 0)
4672
4673 def test_executor_shutdown(self):
4674 "Test that the executor can shut down with jobs running"
4675
4676 self.executor_server.hold_jobs_in_build = True
4677 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004678 A.addApproval('Code-Review', 2)
4679 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004680 self.waitUntilSettled()
4681
4682
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004683class TestDependencyGraph(ZuulTestCase):
4684 tenant_config_file = 'config/dependency-graph/main.yaml'
4685
4686 def test_dependeny_graph_dispatch_jobs_once(self):
4687 "Test a job in a dependency graph is queued only once"
4688 # Job dependencies, starting with A
4689 # A
4690 # / \
4691 # B C
4692 # / \ / \
4693 # D F E
4694 # |
4695 # G
4696
4697 self.executor_server.hold_jobs_in_build = True
4698 change = self.fake_gerrit.addFakeChange(
4699 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004700 change.addApproval('Code-Review', 2)
4701 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004702
4703 self.waitUntilSettled()
4704 self.assertEqual([b.name for b in self.builds], ['A'])
4705
4706 self.executor_server.release('A')
4707 self.waitUntilSettled()
4708 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4709
4710 self.executor_server.release('B')
4711 self.waitUntilSettled()
4712 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4713
4714 self.executor_server.release('D')
4715 self.waitUntilSettled()
4716 self.assertEqual([b.name for b in self.builds], ['C'])
4717
4718 self.executor_server.release('C')
4719 self.waitUntilSettled()
4720 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4721
4722 self.executor_server.release('F')
4723 self.waitUntilSettled()
4724 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4725
4726 self.executor_server.release('G')
4727 self.waitUntilSettled()
4728 self.assertEqual([b.name for b in self.builds], ['E'])
4729
4730 self.executor_server.release('E')
4731 self.waitUntilSettled()
4732 self.assertEqual(len(self.builds), 0)
4733
4734 self.executor_server.hold_jobs_in_build = False
4735 self.executor_server.release()
4736 self.waitUntilSettled()
4737
4738 self.assertEqual(len(self.builds), 0)
4739 self.assertEqual(len(self.history), 7)
4740
4741 self.assertEqual(change.data['status'], 'MERGED')
4742 self.assertEqual(change.reported, 2)
4743
4744 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4745 "Test that a job waits till all dependencies are successful"
4746 # Job dependencies, starting with A
4747 # A
4748 # / \
4749 # B C*
4750 # / \ / \
4751 # D F E
4752 # |
4753 # G
4754
4755 self.executor_server.hold_jobs_in_build = True
4756 change = self.fake_gerrit.addFakeChange(
4757 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004758 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004759
4760 self.executor_server.failJob('C', change)
4761
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004762 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004763
4764 self.waitUntilSettled()
4765 self.assertEqual([b.name for b in self.builds], ['A'])
4766
4767 self.executor_server.release('A')
4768 self.waitUntilSettled()
4769 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4770
4771 self.executor_server.release('B')
4772 self.waitUntilSettled()
4773 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4774
4775 self.executor_server.release('D')
4776 self.waitUntilSettled()
4777 self.assertEqual([b.name for b in self.builds], ['C'])
4778
4779 self.executor_server.release('C')
4780 self.waitUntilSettled()
4781 self.assertEqual(len(self.builds), 0)
4782
4783 self.executor_server.hold_jobs_in_build = False
4784 self.executor_server.release()
4785 self.waitUntilSettled()
4786
4787 self.assertEqual(len(self.builds), 0)
4788 self.assertEqual(len(self.history), 4)
4789
4790 self.assertEqual(change.data['status'], 'NEW')
4791 self.assertEqual(change.reported, 2)
4792
4793
Arieb6f068c2016-10-09 13:11:06 +03004794class TestDuplicatePipeline(ZuulTestCase):
4795 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4796
4797 def test_duplicate_pipelines(self):
4798 "Test that a change matching multiple pipelines works"
4799
4800 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4801 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4802 self.waitUntilSettled()
4803
4804 self.assertHistory([
4805 dict(name='project-test1', result='SUCCESS', changes='1,1',
4806 pipeline='dup1'),
4807 dict(name='project-test1', result='SUCCESS', changes='1,1',
4808 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004809 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004810
4811 self.assertEqual(len(A.messages), 2)
4812
Paul Belangera46a3742016-11-09 14:23:03 -05004813 if 'dup1' in A.messages[0]:
4814 self.assertIn('dup1', A.messages[0])
4815 self.assertNotIn('dup2', A.messages[0])
4816 self.assertIn('project-test1', A.messages[0])
4817 self.assertIn('dup2', A.messages[1])
4818 self.assertNotIn('dup1', A.messages[1])
4819 self.assertIn('project-test1', A.messages[1])
4820 else:
4821 self.assertIn('dup1', A.messages[1])
4822 self.assertNotIn('dup2', A.messages[1])
4823 self.assertIn('project-test1', A.messages[1])
4824 self.assertIn('dup2', A.messages[0])
4825 self.assertNotIn('dup1', A.messages[0])
4826 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004827
4828
Paul Belanger86085b32016-11-03 12:48:57 -04004829class TestSchedulerTemplatedProject(ZuulTestCase):
4830 tenant_config_file = 'config/templated-project/main.yaml'
4831
Paul Belanger174a8272017-03-14 13:20:10 -04004832 def test_job_from_templates_executed(self):
4833 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004834
4835 A = self.fake_gerrit.addFakeChange(
4836 'org/templated-project', 'master', 'A')
4837 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4838 self.waitUntilSettled()
4839
4840 self.assertEqual(self.getJobFromHistory('project-test1').result,
4841 'SUCCESS')
4842 self.assertEqual(self.getJobFromHistory('project-test2').result,
4843 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004844
4845 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004846 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004847
4848 A = self.fake_gerrit.addFakeChange(
4849 'org/layered-project', 'master', 'A')
4850 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4851 self.waitUntilSettled()
4852
4853 self.assertEqual(self.getJobFromHistory('project-test1').result,
4854 'SUCCESS')
4855 self.assertEqual(self.getJobFromHistory('project-test2').result,
4856 'SUCCESS')
4857 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4858 ).result, 'SUCCESS')
4859 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4860 ).result, 'SUCCESS')
4861 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4862 ).result, 'SUCCESS')
4863 self.assertEqual(self.getJobFromHistory('project-test6').result,
4864 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004865
James E. Blaire74f5712017-09-29 15:14:31 -07004866 def test_unimplied_branch_matchers(self):
4867 # This tests that there are no implied branch matchers added
4868 # by project templates.
4869 self.create_branch('org/layered-project', 'stable')
4870
4871 A = self.fake_gerrit.addFakeChange(
4872 'org/layered-project', 'stable', 'A')
4873
4874 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4875 self.waitUntilSettled()
4876
4877 self.assertEqual(self.getJobFromHistory('project-test1').result,
4878 'SUCCESS')
4879 print(self.getJobFromHistory('project-test1').
4880 parameters['zuul']['_inheritance_path'])
4881
4882 def test_implied_branch_matchers(self):
4883 # This tests that there is an implied branch matcher when a
4884 # template is used on an in-repo project pipeline definition.
4885 self.create_branch('untrusted-config', 'stable')
4886 self.fake_gerrit.addEvent(
4887 self.fake_gerrit.getFakeBranchCreatedEvent(
4888 'untrusted-config', 'stable'))
4889 self.waitUntilSettled()
4890
4891 A = self.fake_gerrit.addFakeChange(
4892 'untrusted-config', 'stable', 'A')
4893
4894 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4895 self.waitUntilSettled()
4896
4897 self.assertEqual(self.getJobFromHistory('project-test1').result,
4898 'SUCCESS')
4899 print(self.getJobFromHistory('project-test1').
4900 parameters['zuul']['_inheritance_path'])
4901
Adam Gandelman94a60062016-11-21 16:43:14 -08004902
4903class TestSchedulerSuccessURL(ZuulTestCase):
4904 tenant_config_file = 'config/success-url/main.yaml'
4905
4906 def test_success_url(self):
4907 "Ensure bad build params are ignored"
4908 self.sched.reconfigure(self.config)
4909 self.init_repo('org/docs')
4910
4911 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4912 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4913 self.waitUntilSettled()
4914
4915 # Both builds ran: docs-draft-test + docs-draft-test2
4916 self.assertEqual(len(self.history), 2)
4917
4918 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004919 for build in self.history:
4920 if build.name == 'docs-draft-test':
4921 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004922 elif build.name == 'docs-draft-test2':
4923 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004924
4925 # Two msgs: 'Starting...' + results
4926 self.assertEqual(len(self.smtp_messages), 2)
4927 body = self.smtp_messages[1]['body'].splitlines()
4928 self.assertEqual('Build succeeded.', body[0])
4929
4930 self.assertIn(
4931 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4932 'docs-draft-test/%s/publish-docs/' % uuid,
4933 body[2])
4934
Paul Belanger174a8272017-03-14 13:20:10 -04004935 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004936 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004937 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4938 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004939 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004940 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004941
4942
Adam Gandelman4da00f62016-12-09 15:47:33 -08004943class TestSchedulerMerges(ZuulTestCase):
4944 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004945
4946 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004947 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004948 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004949 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004950 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4951 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4952 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004953 A.addApproval('Code-Review', 2)
4954 B.addApproval('Code-Review', 2)
4955 C.addApproval('Code-Review', 2)
4956 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4957 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4958 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004959 self.waitUntilSettled()
4960
4961 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004962 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4963 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004964 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004965 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004966 repo_messages.reverse()
4967
Paul Belanger174a8272017-03-14 13:20:10 -04004968 self.executor_server.hold_jobs_in_build = False
4969 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004970 self.waitUntilSettled()
4971
4972 return repo_messages
4973
4974 def _test_merge(self, mode):
James E. Blairda5bb7e2018-01-22 16:12:17 -08004975 us_path = 'file://' + os.path.join(
Adam Gandelman8bd57102016-12-02 12:58:42 -08004976 self.upstream_root, 'org/project-%s' % mode)
4977 expected_messages = [
4978 'initial commit',
4979 'add content from fixture',
4980 # the intermediate commits order is nondeterministic
4981 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4982 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4983 ]
4984 result = self._test_project_merge_mode(mode)
4985 self.assertEqual(result[:2], expected_messages[:2])
4986 self.assertEqual(result[-2:], expected_messages[-2:])
4987
4988 def test_project_merge_mode_merge(self):
4989 self._test_merge('merge')
4990
4991 def test_project_merge_mode_merge_resolve(self):
4992 self._test_merge('merge-resolve')
4993
4994 def test_project_merge_mode_cherrypick(self):
4995 expected_messages = [
4996 'initial commit',
4997 'add content from fixture',
4998 'A-1',
4999 'B-1',
5000 'C-1']
5001 result = self._test_project_merge_mode('cherry-pick')
5002 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005003
5004 def test_merge_branch(self):
5005 "Test that the right commits are on alternate branches"
5006 self.create_branch('org/project-merge-branches', 'mp')
5007
Paul Belanger174a8272017-03-14 13:20:10 -04005008 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005009 A = self.fake_gerrit.addFakeChange(
5010 'org/project-merge-branches', 'mp', 'A')
5011 B = self.fake_gerrit.addFakeChange(
5012 'org/project-merge-branches', 'mp', 'B')
5013 C = self.fake_gerrit.addFakeChange(
5014 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005015 A.addApproval('Code-Review', 2)
5016 B.addApproval('Code-Review', 2)
5017 C.addApproval('Code-Review', 2)
5018 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5019 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5020 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005021 self.waitUntilSettled()
5022
Paul Belanger174a8272017-03-14 13:20:10 -04005023 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005024 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005025 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005026 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005027 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005028 self.waitUntilSettled()
5029
5030 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005031 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005032 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5033 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005034 repo = git.Repo(path)
5035
James E. Blair247cab72017-07-20 16:52:36 -07005036 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005037 repo_messages.reverse()
5038 correct_messages = [
5039 'initial commit',
5040 'add content from fixture',
5041 'mp commit',
5042 'A-1', 'B-1', 'C-1']
5043 self.assertEqual(repo_messages, correct_messages)
5044
Paul Belanger174a8272017-03-14 13:20:10 -04005045 self.executor_server.hold_jobs_in_build = False
5046 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005047 self.waitUntilSettled()
5048
5049 def test_merge_multi_branch(self):
5050 "Test that dependent changes on multiple branches are merged"
5051 self.create_branch('org/project-merge-branches', 'mp')
5052
Paul Belanger174a8272017-03-14 13:20:10 -04005053 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005054 A = self.fake_gerrit.addFakeChange(
5055 'org/project-merge-branches', 'master', 'A')
5056 B = self.fake_gerrit.addFakeChange(
5057 'org/project-merge-branches', 'mp', 'B')
5058 C = self.fake_gerrit.addFakeChange(
5059 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005060 A.addApproval('Code-Review', 2)
5061 B.addApproval('Code-Review', 2)
5062 C.addApproval('Code-Review', 2)
5063 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5064 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5065 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005066 self.waitUntilSettled()
5067
5068 job_A = None
5069 for job in self.builds:
5070 if 'project-merge' in job.name:
5071 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005072
James E. Blair2a535672017-04-27 12:03:15 -07005073 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5074 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005075 repo = git.Repo(path)
5076 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005077 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005078 repo_messages.reverse()
5079 correct_messages = [
5080 'initial commit', 'add content from fixture', 'A-1']
5081 self.assertEqual(repo_messages, correct_messages)
5082
Paul Belanger174a8272017-03-14 13:20:10 -04005083 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005084 self.waitUntilSettled()
5085
5086 job_B = None
5087 for job in self.builds:
5088 if 'project-merge' in job.name:
5089 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005090
James E. Blair2a535672017-04-27 12:03:15 -07005091 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5092 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005093 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005094 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005095 repo_messages.reverse()
5096 correct_messages = [
5097 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5098 self.assertEqual(repo_messages, correct_messages)
5099
Paul Belanger174a8272017-03-14 13:20:10 -04005100 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005101 self.waitUntilSettled()
5102
5103 job_C = None
5104 for job in self.builds:
5105 if 'project-merge' in job.name:
5106 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005107
James E. Blair2a535672017-04-27 12:03:15 -07005108 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5109 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005110 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005111 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005112
5113 repo_messages.reverse()
5114 correct_messages = [
5115 'initial commit', 'add content from fixture',
5116 'A-1', 'C-1']
5117 # Ensure the right commits are in the history for this ref
5118 self.assertEqual(repo_messages, correct_messages)
5119
Paul Belanger174a8272017-03-14 13:20:10 -04005120 self.executor_server.hold_jobs_in_build = False
5121 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005122 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005123
5124
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005125class TestSemaphore(ZuulTestCase):
5126 tenant_config_file = 'config/semaphore/main.yaml'
5127
5128 def test_semaphore_one(self):
5129 "Test semaphores with max=1 (mutex)"
5130 tenant = self.sched.abide.tenants.get('tenant-one')
5131
5132 self.executor_server.hold_jobs_in_build = True
5133
5134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5135 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5136 self.assertFalse('test-semaphore' in
5137 tenant.semaphore_handler.semaphores)
5138
5139 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5140 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5141 self.waitUntilSettled()
5142
5143 self.assertEqual(len(self.builds), 3)
5144 self.assertEqual(self.builds[0].name, 'project-test1')
5145 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5146 self.assertEqual(self.builds[2].name, 'project-test1')
5147
5148 self.executor_server.release('semaphore-one-test1')
5149 self.waitUntilSettled()
5150
5151 self.assertEqual(len(self.builds), 3)
5152 self.assertEqual(self.builds[0].name, 'project-test1')
5153 self.assertEqual(self.builds[1].name, 'project-test1')
5154 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5155 self.assertTrue('test-semaphore' in
5156 tenant.semaphore_handler.semaphores)
5157
5158 self.executor_server.release('semaphore-one-test2')
5159 self.waitUntilSettled()
5160
5161 self.assertEqual(len(self.builds), 3)
5162 self.assertEqual(self.builds[0].name, 'project-test1')
5163 self.assertEqual(self.builds[1].name, 'project-test1')
5164 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5165 self.assertTrue('test-semaphore' in
5166 tenant.semaphore_handler.semaphores)
5167
5168 self.executor_server.release('semaphore-one-test1')
5169 self.waitUntilSettled()
5170
5171 self.assertEqual(len(self.builds), 3)
5172 self.assertEqual(self.builds[0].name, 'project-test1')
5173 self.assertEqual(self.builds[1].name, 'project-test1')
5174 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5175 self.assertTrue('test-semaphore' in
5176 tenant.semaphore_handler.semaphores)
5177
5178 self.executor_server.release('semaphore-one-test2')
5179 self.waitUntilSettled()
5180
5181 self.assertEqual(len(self.builds), 2)
5182 self.assertEqual(self.builds[0].name, 'project-test1')
5183 self.assertEqual(self.builds[1].name, 'project-test1')
5184 self.assertFalse('test-semaphore' in
5185 tenant.semaphore_handler.semaphores)
5186
5187 self.executor_server.hold_jobs_in_build = False
5188 self.executor_server.release()
5189
5190 self.waitUntilSettled()
5191 self.assertEqual(len(self.builds), 0)
5192
5193 self.assertEqual(A.reported, 1)
5194 self.assertEqual(B.reported, 1)
5195 self.assertFalse('test-semaphore' in
5196 tenant.semaphore_handler.semaphores)
5197
5198 def test_semaphore_two(self):
5199 "Test semaphores with max>1"
5200 tenant = self.sched.abide.tenants.get('tenant-one')
5201
5202 self.executor_server.hold_jobs_in_build = True
5203 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5204 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5205 self.assertFalse('test-semaphore-two' in
5206 tenant.semaphore_handler.semaphores)
5207
5208 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5209 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5210 self.waitUntilSettled()
5211
5212 self.assertEqual(len(self.builds), 4)
5213 self.assertEqual(self.builds[0].name, 'project-test1')
5214 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5215 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5216 self.assertEqual(self.builds[3].name, 'project-test1')
5217 self.assertTrue('test-semaphore-two' in
5218 tenant.semaphore_handler.semaphores)
5219 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5220 'test-semaphore-two', [])), 2)
5221
5222 self.executor_server.release('semaphore-two-test1')
5223 self.waitUntilSettled()
5224
5225 self.assertEqual(len(self.builds), 4)
5226 self.assertEqual(self.builds[0].name, 'project-test1')
5227 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5228 self.assertEqual(self.builds[2].name, 'project-test1')
5229 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5230 self.assertTrue('test-semaphore-two' in
5231 tenant.semaphore_handler.semaphores)
5232 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5233 'test-semaphore-two', [])), 2)
5234
5235 self.executor_server.release('semaphore-two-test2')
5236 self.waitUntilSettled()
5237
5238 self.assertEqual(len(self.builds), 4)
5239 self.assertEqual(self.builds[0].name, 'project-test1')
5240 self.assertEqual(self.builds[1].name, 'project-test1')
5241 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5242 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5243 self.assertTrue('test-semaphore-two' in
5244 tenant.semaphore_handler.semaphores)
5245 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5246 'test-semaphore-two', [])), 2)
5247
5248 self.executor_server.release('semaphore-two-test1')
5249 self.waitUntilSettled()
5250
5251 self.assertEqual(len(self.builds), 3)
5252 self.assertEqual(self.builds[0].name, 'project-test1')
5253 self.assertEqual(self.builds[1].name, 'project-test1')
5254 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5255 self.assertTrue('test-semaphore-two' in
5256 tenant.semaphore_handler.semaphores)
5257 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5258 'test-semaphore-two', [])), 1)
5259
5260 self.executor_server.release('semaphore-two-test2')
5261 self.waitUntilSettled()
5262
5263 self.assertEqual(len(self.builds), 2)
5264 self.assertEqual(self.builds[0].name, 'project-test1')
5265 self.assertEqual(self.builds[1].name, 'project-test1')
5266 self.assertFalse('test-semaphore-two' in
5267 tenant.semaphore_handler.semaphores)
5268
5269 self.executor_server.hold_jobs_in_build = False
5270 self.executor_server.release()
5271
5272 self.waitUntilSettled()
5273 self.assertEqual(len(self.builds), 0)
5274
5275 self.assertEqual(A.reported, 1)
5276 self.assertEqual(B.reported, 1)
5277
5278 def test_semaphore_abandon(self):
5279 "Test abandon with job semaphores"
5280 self.executor_server.hold_jobs_in_build = True
5281 tenant = self.sched.abide.tenants.get('tenant-one')
5282 check_pipeline = tenant.layout.pipelines['check']
5283
5284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5285 self.assertFalse('test-semaphore' in
5286 tenant.semaphore_handler.semaphores)
5287
5288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5289 self.waitUntilSettled()
5290
5291 self.assertTrue('test-semaphore' in
5292 tenant.semaphore_handler.semaphores)
5293
5294 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5295 self.waitUntilSettled()
5296
5297 # The check pipeline should be empty
5298 items = check_pipeline.getAllItems()
5299 self.assertEqual(len(items), 0)
5300
5301 # The semaphore should be released
5302 self.assertFalse('test-semaphore' in
5303 tenant.semaphore_handler.semaphores)
5304
5305 self.executor_server.hold_jobs_in_build = False
5306 self.executor_server.release()
5307 self.waitUntilSettled()
5308
Tobias Henkel0f714002017-06-30 23:30:52 +02005309 def test_semaphore_new_patchset(self):
5310 "Test new patchset with job semaphores"
5311 self.executor_server.hold_jobs_in_build = True
5312 tenant = self.sched.abide.tenants.get('tenant-one')
5313 check_pipeline = tenant.layout.pipelines['check']
5314
5315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5316 self.assertFalse('test-semaphore' in
5317 tenant.semaphore_handler.semaphores)
5318
5319 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5320 self.waitUntilSettled()
5321
5322 self.assertTrue('test-semaphore' in
5323 tenant.semaphore_handler.semaphores)
5324 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5325 self.assertEqual(len(semaphore), 1)
5326
5327 A.addPatchset()
5328 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5329 self.waitUntilSettled()
5330
5331 self.assertTrue('test-semaphore' in
5332 tenant.semaphore_handler.semaphores)
5333 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5334 self.assertEqual(len(semaphore), 1)
5335
5336 items = check_pipeline.getAllItems()
5337 self.assertEqual(items[0].change.number, '1')
5338 self.assertEqual(items[0].change.patchset, '2')
5339 self.assertTrue(items[0].live)
5340
5341 self.executor_server.hold_jobs_in_build = False
5342 self.executor_server.release()
5343 self.waitUntilSettled()
5344
5345 # The semaphore should be released
5346 self.assertFalse('test-semaphore' in
5347 tenant.semaphore_handler.semaphores)
5348
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005349 def test_semaphore_reconfigure(self):
5350 "Test reconfigure with job semaphores"
5351 self.executor_server.hold_jobs_in_build = True
5352 tenant = self.sched.abide.tenants.get('tenant-one')
5353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5354 self.assertFalse('test-semaphore' in
5355 tenant.semaphore_handler.semaphores)
5356
5357 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5358 self.waitUntilSettled()
5359
5360 self.assertTrue('test-semaphore' in
5361 tenant.semaphore_handler.semaphores)
5362
5363 # reconfigure without layout change
5364 self.sched.reconfigure(self.config)
5365 self.waitUntilSettled()
5366 tenant = self.sched.abide.tenants.get('tenant-one')
5367
5368 # semaphore still must be held
5369 self.assertTrue('test-semaphore' in
5370 tenant.semaphore_handler.semaphores)
5371
5372 self.commitConfigUpdate(
5373 'common-config',
5374 'config/semaphore/zuul-reconfiguration.yaml')
5375 self.sched.reconfigure(self.config)
5376 self.waitUntilSettled()
5377 tenant = self.sched.abide.tenants.get('tenant-one')
5378
5379 self.executor_server.release('project-test1')
5380 self.waitUntilSettled()
5381
5382 # There should be no builds anymore
5383 self.assertEqual(len(self.builds), 0)
5384
5385 # The semaphore should be released
5386 self.assertFalse('test-semaphore' in
5387 tenant.semaphore_handler.semaphores)
5388
5389
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005390class TestSemaphoreMultiTenant(ZuulTestCase):
5391 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5392
5393 def test_semaphore_tenant_isolation(self):
5394 "Test semaphores in multiple tenants"
5395
5396 self.waitUntilSettled()
5397 tenant_one = self.sched.abide.tenants.get('tenant-one')
5398 tenant_two = self.sched.abide.tenants.get('tenant-two')
5399
5400 self.executor_server.hold_jobs_in_build = True
5401 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5402 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5403 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5404 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5405 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5406 self.assertFalse('test-semaphore' in
5407 tenant_one.semaphore_handler.semaphores)
5408 self.assertFalse('test-semaphore' in
5409 tenant_two.semaphore_handler.semaphores)
5410
5411 # add patches to project1 of tenant-one
5412 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5413 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5414 self.waitUntilSettled()
5415
5416 # one build of project1-test1 must run
5417 # semaphore of tenant-one must be acquired once
5418 # semaphore of tenant-two must not be acquired
5419 self.assertEqual(len(self.builds), 1)
5420 self.assertEqual(self.builds[0].name, 'project1-test1')
5421 self.assertTrue('test-semaphore' in
5422 tenant_one.semaphore_handler.semaphores)
5423 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5424 'test-semaphore', [])), 1)
5425 self.assertFalse('test-semaphore' in
5426 tenant_two.semaphore_handler.semaphores)
5427
5428 # add patches to project2 of tenant-two
5429 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5430 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5431 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5432 self.waitUntilSettled()
5433
5434 # one build of project1-test1 must run
5435 # two builds of project2-test1 must run
5436 # semaphore of tenant-one must be acquired once
5437 # semaphore of tenant-two must be acquired twice
5438 self.assertEqual(len(self.builds), 3)
5439 self.assertEqual(self.builds[0].name, 'project1-test1')
5440 self.assertEqual(self.builds[1].name, 'project2-test1')
5441 self.assertEqual(self.builds[2].name, 'project2-test1')
5442 self.assertTrue('test-semaphore' in
5443 tenant_one.semaphore_handler.semaphores)
5444 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5445 'test-semaphore', [])), 1)
5446 self.assertTrue('test-semaphore' in
5447 tenant_two.semaphore_handler.semaphores)
5448 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5449 'test-semaphore', [])), 2)
5450
5451 self.executor_server.release('project1-test1')
5452 self.waitUntilSettled()
5453
5454 # one build of project1-test1 must run
5455 # two builds of project2-test1 must run
5456 # semaphore of tenant-one must be acquired once
5457 # semaphore of tenant-two must be acquired twice
5458 self.assertEqual(len(self.builds), 3)
5459 self.assertEqual(self.builds[0].name, 'project2-test1')
5460 self.assertEqual(self.builds[1].name, 'project2-test1')
5461 self.assertEqual(self.builds[2].name, 'project1-test1')
5462 self.assertTrue('test-semaphore' in
5463 tenant_one.semaphore_handler.semaphores)
5464 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5465 'test-semaphore', [])), 1)
5466 self.assertTrue('test-semaphore' in
5467 tenant_two.semaphore_handler.semaphores)
5468 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5469 'test-semaphore', [])), 2)
5470
5471 self.executor_server.release('project2-test1')
5472 self.waitUntilSettled()
5473
5474 # one build of project1-test1 must run
5475 # one build of project2-test1 must run
5476 # semaphore of tenant-one must be acquired once
5477 # semaphore of tenant-two must be acquired once
5478 self.assertEqual(len(self.builds), 2)
5479 self.assertTrue('test-semaphore' in
5480 tenant_one.semaphore_handler.semaphores)
5481 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5482 'test-semaphore', [])), 1)
5483 self.assertTrue('test-semaphore' in
5484 tenant_two.semaphore_handler.semaphores)
5485 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5486 'test-semaphore', [])), 1)
5487
5488 self.executor_server.hold_jobs_in_build = False
5489 self.executor_server.release()
5490
5491 self.waitUntilSettled()
5492
5493 # no build must run
5494 # semaphore of tenant-one must not be acquired
5495 # semaphore of tenant-two must not be acquired
5496 self.assertEqual(len(self.builds), 0)
5497 self.assertFalse('test-semaphore' in
5498 tenant_one.semaphore_handler.semaphores)
5499 self.assertFalse('test-semaphore' in
5500 tenant_two.semaphore_handler.semaphores)
5501
5502 self.assertEqual(A.reported, 1)
5503 self.assertEqual(B.reported, 1)
5504
5505
Tobias Henkele9a22512017-12-22 08:36:12 +01005506class TestImplicitProject(ZuulTestCase):
5507 tenant_config_file = 'config/implicit-project/main.yaml'
5508
5509 def test_implicit_project(self):
5510 # config project should work with implicit project name
5511 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A')
5512 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5513
5514 # untrusted project should work with implicit project name
5515 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5516 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5517
5518 self.waitUntilSettled()
5519
5520 self.assertEqual(A.data['status'], 'NEW')
5521 self.assertEqual(A.reported, 1)
5522 self.assertEqual(B.data['status'], 'NEW')
5523 self.assertEqual(B.reported, 1)
5524 self.assertHistory([
5525 dict(name='test-common', result='SUCCESS', changes='1,1'),
5526 dict(name='test-common', result='SUCCESS', changes='2,1'),
5527 dict(name='test-project', result='SUCCESS', changes='2,1'),
5528 ], ordered=False)
5529
5530 # now test adding a further project in repo
5531 in_repo_conf = textwrap.dedent(
5532 """
5533 - job:
5534 name: test-project
5535 run: playbooks/test-project.yaml
5536 - job:
5537 name: test2-project
5538 run: playbooks/test-project.yaml
5539
5540 - project:
5541 check:
5542 jobs:
5543 - test-project
5544 gate:
5545 jobs:
5546 - test-project
5547
5548 - project:
5549 check:
5550 jobs:
5551 - test2-project
5552 gate:
5553 jobs:
5554 - test2-project
5555
5556 """)
5557 file_dict = {'.zuul.yaml': in_repo_conf}
5558 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5559 files=file_dict)
5560 C.addApproval('Code-Review', 2)
5561 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
5562 self.waitUntilSettled()
5563
5564 # change C must be merged
5565 self.assertEqual(C.data['status'], 'MERGED')
5566 self.assertEqual(C.reported, 2)
5567 self.assertHistory([
5568 dict(name='test-common', result='SUCCESS', changes='1,1'),
5569 dict(name='test-common', result='SUCCESS', changes='2,1'),
5570 dict(name='test-project', result='SUCCESS', changes='2,1'),
5571 dict(name='test-common', result='SUCCESS', changes='3,1'),
5572 dict(name='test-project', result='SUCCESS', changes='3,1'),
5573 dict(name='test2-project', result='SUCCESS', changes='3,1'),
5574 ], ordered=False)
5575
5576
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005577class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005578 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005579 tenant_config_file = 'config/in-repo/main.yaml'
5580
5581 def test_semaphore_in_repo(self):
5582 "Test semaphores in repo config"
5583
5584 # This tests dynamic semaphore handling in project repos. The semaphore
5585 # max value should not be evaluated dynamically but must be updated
5586 # after the change lands.
5587
5588 self.waitUntilSettled()
5589 tenant = self.sched.abide.tenants.get('tenant-one')
5590
5591 in_repo_conf = textwrap.dedent(
5592 """
5593 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005594 name: project-test1
5595
5596 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005597 name: project-test2
James E. Blair2f589fe2017-10-26 12:57:41 -07005598 run: playbooks/project-test2.yaml
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005599 semaphore: test-semaphore
5600
5601 - project:
5602 name: org/project
5603 tenant-one-gate:
5604 jobs:
5605 - project-test2
5606
5607 # the max value in dynamic layout must be ignored
5608 - semaphore:
5609 name: test-semaphore
5610 max: 2
5611 """)
5612
5613 in_repo_playbook = textwrap.dedent(
5614 """
5615 - hosts: all
5616 tasks: []
5617 """)
5618
5619 file_dict = {'.zuul.yaml': in_repo_conf,
5620 'playbooks/project-test2.yaml': in_repo_playbook}
5621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5622 files=file_dict)
5623 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5624 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5625 B.setDependsOn(A, 1)
5626 C.setDependsOn(A, 1)
5627
5628 self.executor_server.hold_jobs_in_build = True
5629
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005630 A.addApproval('Code-Review', 2)
5631 B.addApproval('Code-Review', 2)
5632 C.addApproval('Code-Review', 2)
5633 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5634 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5635 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005636 self.waitUntilSettled()
5637
5638 # check that the layout in a queue item still has max value of 1
5639 # for test-semaphore
5640 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5641 queue = None
5642 for queue_candidate in pipeline.queues:
5643 if queue_candidate.name == 'org/project':
5644 queue = queue_candidate
5645 break
5646 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005647 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005648 dynamic_test_semaphore = \
5649 item_dynamic_layout.semaphores.get('test-semaphore')
5650 self.assertEqual(dynamic_test_semaphore.max, 1)
5651
5652 # one build must be in queue, one semaphores acquired
5653 self.assertEqual(len(self.builds), 1)
5654 self.assertEqual(self.builds[0].name, 'project-test2')
5655 self.assertTrue('test-semaphore' in
5656 tenant.semaphore_handler.semaphores)
5657 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5658 'test-semaphore', [])), 1)
5659
5660 self.executor_server.release('project-test2')
5661 self.waitUntilSettled()
5662
5663 # change A must be merged
5664 self.assertEqual(A.data['status'], 'MERGED')
5665 self.assertEqual(A.reported, 2)
5666
5667 # send change-merged event as the gerrit mock doesn't send it
5668 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5669 self.waitUntilSettled()
5670
5671 # now that change A was merged, the new semaphore max must be effective
5672 tenant = self.sched.abide.tenants.get('tenant-one')
5673 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5674
5675 # two builds must be in queue, two semaphores acquired
5676 self.assertEqual(len(self.builds), 2)
5677 self.assertEqual(self.builds[0].name, 'project-test2')
5678 self.assertEqual(self.builds[1].name, 'project-test2')
5679 self.assertTrue('test-semaphore' in
5680 tenant.semaphore_handler.semaphores)
5681 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5682 'test-semaphore', [])), 2)
5683
5684 self.executor_server.release('project-test2')
5685 self.waitUntilSettled()
5686
5687 self.assertEqual(len(self.builds), 0)
5688 self.assertFalse('test-semaphore' in
5689 tenant.semaphore_handler.semaphores)
5690
5691 self.executor_server.hold_jobs_in_build = False
5692 self.executor_server.release()
5693
5694 self.waitUntilSettled()
5695 self.assertEqual(len(self.builds), 0)
5696
5697 self.assertEqual(A.reported, 2)
5698 self.assertEqual(B.reported, 2)
5699 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005700
5701
5702class TestSchedulerBranchMatcher(ZuulTestCase):
5703
5704 @simple_layout('layouts/matcher-test.yaml')
5705 def test_job_branch_ignored(self):
5706 '''
5707 Test that branch matching logic works.
5708
5709 The 'ignore-branch' job has a branch matcher that is supposed to
5710 match every branch except for the 'featureA' branch, so it should
5711 not be run on a change to that branch.
5712 '''
5713 self.create_branch('org/project', 'featureA')
5714 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5715 A.addApproval('Code-Review', 2)
5716 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5717 self.waitUntilSettled()
5718 self.printHistory()
5719 self.assertEqual(self.getJobFromHistory('project-test1').result,
5720 'SUCCESS')
5721 self.assertJobNotInHistory('ignore-branch')
5722 self.assertEqual(A.data['status'], 'MERGED')
5723 self.assertEqual(A.reported, 2,
5724 "A should report start and success")
5725 self.assertIn('gate', A.messages[1],
5726 "A should transit gate")