blob: 41e8463bdcc19de93aceb32306e240d580bd4aeb [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
James E. Blairec056492016-07-22 09:45:56 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070028from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040029import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070032import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080033import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040034import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070035
Maru Newby3fe5f852015-01-13 04:22:14 +000036from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000037 ZuulTestCase,
38 repack_repo,
39)
James E. Blairb0fcae42012-07-17 11:12:10 -070040
James E. Blairb0fcae42012-07-17 11:12:10 -070041
Clark Boylanb640e052014-04-03 16:41:46 -070042class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070043 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010044
Paul Belanger174a8272017-03-14 13:20:10 -040045 def test_jobs_executed(self):
46 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070047
James E. Blairb0fcae42012-07-17 11:12:10 -070048 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070049 A.addApproval('code-review', 2)
50 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070051 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040052 self.assertEqual(self.getJobFromHistory('project-merge').result,
53 'SUCCESS')
54 self.assertEqual(self.getJobFromHistory('project-test1').result,
55 'SUCCESS')
56 self.assertEqual(self.getJobFromHistory('project-test2').result,
57 'SUCCESS')
58 self.assertEqual(A.data['status'], 'MERGED')
59 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070060 self.assertEqual(self.getJobFromHistory('project-test1').node,
61 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080062 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070063
James E. Blair552b54f2016-07-22 13:55:32 -070064 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070065 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
66 self.assertReportedStat('zuul.pipeline.gate.current_changes',
67 value='1|g')
68 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
69 kind='ms')
70 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
71 value='1|c')
72 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
73 self.assertReportedStat('zuul.pipeline.gate.total_changes',
74 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070075 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070076 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070077 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070078 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070079
James E. Blair5821bd92015-09-16 08:48:15 -070080 for build in self.builds:
81 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
82
James E. Blair3cb10702013-08-24 08:56:03 -070083 def test_initial_pipeline_gauges(self):
84 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070085 self.assertReportedStat('zuul.pipeline.gate.current_changes',
86 value='0|g')
87 self.assertReportedStat('zuul.pipeline.check.current_changes',
88 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070089
James E. Blair34776ee2016-08-25 13:53:54 -070090 def test_job_branch(self):
91 "Test the correct variant of a job runs on a branch"
92 self.create_branch('org/project', 'stable')
93 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
94 A.addApproval('code-review', 2)
95 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
96 self.waitUntilSettled()
97 self.assertEqual(self.getJobFromHistory('project-test1').result,
98 'SUCCESS')
99 self.assertEqual(self.getJobFromHistory('project-test2').result,
100 'SUCCESS')
101 self.assertEqual(A.data['status'], 'MERGED')
102 self.assertEqual(A.reported, 2,
103 "A should report start and success")
104 self.assertIn('gate', A.messages[1],
105 "A should transit gate")
106 self.assertEqual(self.getJobFromHistory('project-test1').node,
107 'image2')
108
James E. Blairb0fcae42012-07-17 11:12:10 -0700109 def test_parallel_changes(self):
110 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700111
Paul Belanger174a8272017-03-14 13:20:10 -0400112 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700113 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
114 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
115 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700116 A.addApproval('code-review', 2)
117 B.addApproval('code-review', 2)
118 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700119
James E. Blair8b5408c2016-08-08 15:37:46 -0700120 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
121 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
122 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700123
124 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400125 self.assertEqual(len(self.builds), 1)
126 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700127 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700128
Paul Belanger174a8272017-03-14 13:20:10 -0400129 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700130 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400131 self.assertEqual(len(self.builds), 3)
132 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700133 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400134 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700135 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400136 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700137 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700138
Paul Belanger174a8272017-03-14 13:20:10 -0400139 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700140 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400141 self.assertEqual(len(self.builds), 5)
142 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700143 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400144 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700145 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700146
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400147 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700148 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400149 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700150 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700151
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400152 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700153 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700154
Paul Belanger174a8272017-03-14 13:20:10 -0400155 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700156 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400157 self.assertEqual(len(self.builds), 6)
158 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700159 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400160 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700161 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700162
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400163 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700164 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400165 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700166 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700167
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400168 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700169 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400170 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700171 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700172
Paul Belanger174a8272017-03-14 13:20:10 -0400173 self.executor_server.hold_jobs_in_build = False
174 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700175 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700177
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400178 self.assertEqual(len(self.history), 9)
179 self.assertEqual(A.data['status'], 'MERGED')
180 self.assertEqual(B.data['status'], 'MERGED')
181 self.assertEqual(C.data['status'], 'MERGED')
182 self.assertEqual(A.reported, 2)
183 self.assertEqual(B.reported, 2)
184 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700185
186 def test_failed_changes(self):
187 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400188 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700189
James E. Blairb02a3bb2012-07-30 17:49:55 -0700190 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
191 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700192 A.addApproval('code-review', 2)
193 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700194
Paul Belanger174a8272017-03-14 13:20:10 -0400195 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700196
James E. Blair8b5408c2016-08-08 15:37:46 -0700197 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
198 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700199 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700200 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400201
Paul Belanger174a8272017-03-14 13:20:10 -0400202 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400203 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700204 # A/project-merge is complete
205 self.assertBuilds([
206 dict(name='project-test1', changes='1,1'),
207 dict(name='project-test2', changes='1,1'),
208 dict(name='project-merge', changes='1,1 2,1'),
209 ])
James E. Blaire2819012013-06-28 17:17:26 -0400210
Paul Belanger174a8272017-03-14 13:20:10 -0400211 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400212 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700213 # A/project-merge is complete
214 # B/project-merge is complete
215 self.assertBuilds([
216 dict(name='project-test1', changes='1,1'),
217 dict(name='project-test2', changes='1,1'),
218 dict(name='project-test1', changes='1,1 2,1'),
219 dict(name='project-test2', changes='1,1 2,1'),
220 ])
221
222 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400223 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700224 self.builds[0].release()
225 self.waitUntilSettled()
226
227 self.orderedRelease()
228 self.assertHistory([
229 dict(name='project-merge', result='SUCCESS', changes='1,1'),
230 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
231 dict(name='project-test1', result='FAILURE', changes='1,1'),
232 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
233 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
234 dict(name='project-test2', result='SUCCESS', changes='1,1'),
235 dict(name='project-merge', result='SUCCESS', changes='2,1'),
236 dict(name='project-test1', result='SUCCESS', changes='2,1'),
237 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700238 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700239
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400240 self.assertEqual(A.data['status'], 'NEW')
241 self.assertEqual(B.data['status'], 'MERGED')
242 self.assertEqual(A.reported, 2)
243 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700244
245 def test_independent_queues(self):
246 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700247
Paul Belanger174a8272017-03-14 13:20:10 -0400248 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900249 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700250 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
251 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700252 A.addApproval('code-review', 2)
253 B.addApproval('code-review', 2)
254 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700255
James E. Blair8b5408c2016-08-08 15:37:46 -0700256 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800257 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700258
James E. Blairf59f3cf2017-02-19 14:50:26 -0800259 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
260 self.waitUntilSettled()
261
262 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700263 self.waitUntilSettled()
264
265 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700266 self.assertBuilds([
267 dict(name='project-merge', changes='1,1'),
268 dict(name='project-merge', changes='2,1'),
269 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700270
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700271 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700272 self.builds[0].release()
273 self.waitUntilSettled()
274 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700275 self.waitUntilSettled()
276 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400277 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700278 self.waitUntilSettled()
279
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700280 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700281 self.assertBuilds([
282 dict(name='project-test1', changes='1,1'),
283 dict(name='project-test2', changes='1,1'),
284 dict(name='project-test1', changes='2,1'),
285 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500286 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700287 dict(name='project-test1', changes='2,1 3,1'),
288 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800289 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700290 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700291
James E. Blair5f0d49e2016-08-19 09:36:50 -0700292 self.orderedRelease()
293 self.assertHistory([
294 dict(name='project-merge', result='SUCCESS', changes='1,1'),
295 dict(name='project-merge', result='SUCCESS', changes='2,1'),
296 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
297 dict(name='project-test1', result='SUCCESS', changes='1,1'),
298 dict(name='project-test2', result='SUCCESS', changes='1,1'),
299 dict(name='project-test1', result='SUCCESS', changes='2,1'),
300 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500301 dict(
302 name='project1-project2-integration',
303 result='SUCCESS',
304 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700305 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
306 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800307 dict(name='project1-project2-integration',
308 result='SUCCESS',
309 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700310 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700311
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400312 self.assertEqual(A.data['status'], 'MERGED')
313 self.assertEqual(B.data['status'], 'MERGED')
314 self.assertEqual(C.data['status'], 'MERGED')
315 self.assertEqual(A.reported, 2)
316 self.assertEqual(B.reported, 2)
317 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700318
319 def test_failed_change_at_head(self):
320 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700321
Paul Belanger174a8272017-03-14 13:20:10 -0400322 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700323 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
324 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
325 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700326 A.addApproval('code-review', 2)
327 B.addApproval('code-review', 2)
328 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700329
Paul Belanger174a8272017-03-14 13:20:10 -0400330 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700331
James E. Blair8b5408c2016-08-08 15:37:46 -0700332 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
333 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
334 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700335
336 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700337
James E. Blairb536ecc2016-08-31 10:11:42 -0700338 self.assertBuilds([
339 dict(name='project-merge', changes='1,1'),
340 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700341
Paul Belanger174a8272017-03-14 13:20:10 -0400342 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700343 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400344 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400346 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700347 self.waitUntilSettled()
348
James E. Blairb536ecc2016-08-31 10:11:42 -0700349 self.assertBuilds([
350 dict(name='project-test1', changes='1,1'),
351 dict(name='project-test2', changes='1,1'),
352 dict(name='project-test1', changes='1,1 2,1'),
353 dict(name='project-test2', changes='1,1 2,1'),
354 dict(name='project-test1', changes='1,1 2,1 3,1'),
355 dict(name='project-test2', changes='1,1 2,1 3,1'),
356 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700357
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400358 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700359 self.waitUntilSettled()
360
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400361 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700362 self.assertBuilds([
363 dict(name='project-test2', changes='1,1'),
364 dict(name='project-merge', changes='2,1'),
365 ])
366 # Unordered history comparison because the aborts can finish
367 # in any order.
368 self.assertHistory([
369 dict(name='project-merge', result='SUCCESS',
370 changes='1,1'),
371 dict(name='project-merge', result='SUCCESS',
372 changes='1,1 2,1'),
373 dict(name='project-merge', result='SUCCESS',
374 changes='1,1 2,1 3,1'),
375 dict(name='project-test1', result='FAILURE',
376 changes='1,1'),
377 dict(name='project-test1', result='ABORTED',
378 changes='1,1 2,1'),
379 dict(name='project-test2', result='ABORTED',
380 changes='1,1 2,1'),
381 dict(name='project-test1', result='ABORTED',
382 changes='1,1 2,1 3,1'),
383 dict(name='project-test2', result='ABORTED',
384 changes='1,1 2,1 3,1'),
385 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700386
Paul Belanger174a8272017-03-14 13:20:10 -0400387 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700388 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400389 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700390 self.waitUntilSettled()
391 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700392
James E. Blairb536ecc2016-08-31 10:11:42 -0700393 self.assertBuilds([])
394 self.assertHistory([
395 dict(name='project-merge', result='SUCCESS',
396 changes='1,1'),
397 dict(name='project-merge', result='SUCCESS',
398 changes='1,1 2,1'),
399 dict(name='project-merge', result='SUCCESS',
400 changes='1,1 2,1 3,1'),
401 dict(name='project-test1', result='FAILURE',
402 changes='1,1'),
403 dict(name='project-test1', result='ABORTED',
404 changes='1,1 2,1'),
405 dict(name='project-test2', result='ABORTED',
406 changes='1,1 2,1'),
407 dict(name='project-test1', result='ABORTED',
408 changes='1,1 2,1 3,1'),
409 dict(name='project-test2', result='ABORTED',
410 changes='1,1 2,1 3,1'),
411 dict(name='project-merge', result='SUCCESS',
412 changes='2,1'),
413 dict(name='project-merge', result='SUCCESS',
414 changes='2,1 3,1'),
415 dict(name='project-test2', result='SUCCESS',
416 changes='1,1'),
417 dict(name='project-test1', result='SUCCESS',
418 changes='2,1'),
419 dict(name='project-test2', result='SUCCESS',
420 changes='2,1'),
421 dict(name='project-test1', result='SUCCESS',
422 changes='2,1 3,1'),
423 dict(name='project-test2', result='SUCCESS',
424 changes='2,1 3,1'),
425 ], ordered=False)
426
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400427 self.assertEqual(A.data['status'], 'NEW')
428 self.assertEqual(B.data['status'], 'MERGED')
429 self.assertEqual(C.data['status'], 'MERGED')
430 self.assertEqual(A.reported, 2)
431 self.assertEqual(B.reported, 2)
432 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700433
James E. Blair0aac4872013-08-23 14:02:38 -0700434 def test_failed_change_in_middle(self):
435 "Test a failed change in the middle of the queue"
436
Paul Belanger174a8272017-03-14 13:20:10 -0400437 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700438 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
439 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
440 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700441 A.addApproval('code-review', 2)
442 B.addApproval('code-review', 2)
443 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700444
Paul Belanger174a8272017-03-14 13:20:10 -0400445 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700446
James E. Blair8b5408c2016-08-08 15:37:46 -0700447 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
448 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
449 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700450
451 self.waitUntilSettled()
452
Paul Belanger174a8272017-03-14 13:20:10 -0400453 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700454 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400455 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700456 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400457 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700458 self.waitUntilSettled()
459
460 self.assertEqual(len(self.builds), 6)
461 self.assertEqual(self.builds[0].name, 'project-test1')
462 self.assertEqual(self.builds[1].name, 'project-test2')
463 self.assertEqual(self.builds[2].name, 'project-test1')
464 self.assertEqual(self.builds[3].name, 'project-test2')
465 self.assertEqual(self.builds[4].name, 'project-test1')
466 self.assertEqual(self.builds[5].name, 'project-test2')
467
468 self.release(self.builds[2])
469 self.waitUntilSettled()
470
James E. Blair972e3c72013-08-29 12:04:55 -0700471 # project-test1 and project-test2 for A
472 # project-test2 for B
473 # project-merge for C (without B)
474 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700475 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
476
Paul Belanger174a8272017-03-14 13:20:10 -0400477 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700478 self.waitUntilSettled()
479
480 # project-test1 and project-test2 for A
481 # project-test2 for B
482 # project-test1 and project-test2 for C
483 self.assertEqual(len(self.builds), 5)
484
Paul Belanger2e2a0492016-10-30 18:33:37 -0400485 tenant = self.sched.abide.tenants.get('tenant-one')
486 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700487 builds = items[0].current_build_set.getBuilds()
488 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
489 self.assertEqual(self.countJobResults(builds, None), 2)
490 builds = items[1].current_build_set.getBuilds()
491 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
492 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
493 self.assertEqual(self.countJobResults(builds, None), 1)
494 builds = items[2].current_build_set.getBuilds()
495 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700496 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700497
Paul Belanger174a8272017-03-14 13:20:10 -0400498 self.executor_server.hold_jobs_in_build = False
499 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700500 self.waitUntilSettled()
501
502 self.assertEqual(len(self.builds), 0)
503 self.assertEqual(len(self.history), 12)
504 self.assertEqual(A.data['status'], 'MERGED')
505 self.assertEqual(B.data['status'], 'NEW')
506 self.assertEqual(C.data['status'], 'MERGED')
507 self.assertEqual(A.reported, 2)
508 self.assertEqual(B.reported, 2)
509 self.assertEqual(C.reported, 2)
510
James E. Blaird466dc42012-07-31 10:42:56 -0700511 def test_failed_change_at_head_with_queue(self):
512 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700513
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700514 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700515 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
516 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
517 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700518 A.addApproval('code-review', 2)
519 B.addApproval('code-review', 2)
520 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700521
Paul Belanger174a8272017-03-14 13:20:10 -0400522 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700523
James E. Blair8b5408c2016-08-08 15:37:46 -0700524 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
525 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
526 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700527
528 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700529 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400530 self.assertEqual(len(self.builds), 0)
531 self.assertEqual(len(queue), 1)
Paul Belanger174a8272017-03-14 13:20:10 -0400532 self.assertEqual(queue[0].name, 'executor:execute')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800533 job_args = json.loads(queue[0].arguments)
534 self.assertEqual(job_args['job'], 'project-merge')
535 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700536
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700537 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700538 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700539 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700540 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700541 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700542 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700543 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700544
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400545 self.assertEqual(len(self.builds), 0)
546 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800547
548 self.assertEqual(
549 json.loads(queue[0].arguments)['job'], 'project-test1')
550 self.assertEqual(
551 json.loads(queue[1].arguments)['job'], 'project-test2')
552 self.assertEqual(
553 json.loads(queue[2].arguments)['job'], 'project-test1')
554 self.assertEqual(
555 json.loads(queue[3].arguments)['job'], 'project-test2')
556 self.assertEqual(
557 json.loads(queue[4].arguments)['job'], 'project-test1')
558 self.assertEqual(
559 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700560
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700561 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700562 self.waitUntilSettled()
563
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400564 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700565 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400566 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
567 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700568
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 self.gearman_server.hold_jobs_in_queue = False
570 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700571 self.waitUntilSettled()
572
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400573 self.assertEqual(len(self.builds), 0)
574 self.assertEqual(len(self.history), 11)
575 self.assertEqual(A.data['status'], 'NEW')
576 self.assertEqual(B.data['status'], 'MERGED')
577 self.assertEqual(C.data['status'], 'MERGED')
578 self.assertEqual(A.reported, 2)
579 self.assertEqual(B.reported, 2)
580 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700581
James E. Blairec056492016-07-22 09:45:56 -0700582 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700583 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400584 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700585 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700586 A.addApproval('code-review', 2)
587 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700588 self.waitUntilSettled()
589 time.sleep(2)
590
591 data = json.loads(self.sched.formatStatusJSON())
592 found_job = None
593 for pipeline in data['pipelines']:
594 if pipeline['name'] != 'gate':
595 continue
596 for queue in pipeline['change_queues']:
597 for head in queue['heads']:
598 for item in head:
599 for job in item['jobs']:
600 if job['name'] == 'project-merge':
601 found_job = job
602 break
603
604 self.assertIsNotNone(found_job)
605 if iteration == 1:
606 self.assertIsNotNone(found_job['estimated_time'])
607 self.assertIsNone(found_job['remaining_time'])
608 else:
609 self.assertIsNotNone(found_job['estimated_time'])
610 self.assertTrue(found_job['estimated_time'] >= 2)
611 self.assertIsNotNone(found_job['remaining_time'])
612
Paul Belanger174a8272017-03-14 13:20:10 -0400613 self.executor_server.hold_jobs_in_build = False
614 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700615 self.waitUntilSettled()
616
James E. Blairec056492016-07-22 09:45:56 -0700617 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700618 def test_time_database(self):
619 "Test the time database"
620
621 self._test_time_database(1)
622 self._test_time_database(2)
623
James E. Blairfef71632013-09-23 11:15:47 -0700624 def test_two_failed_changes_at_head(self):
625 "Test that changes are reparented correctly if 2 fail at head"
626
Paul Belanger174a8272017-03-14 13:20:10 -0400627 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700628 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
629 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
630 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700631 A.addApproval('code-review', 2)
632 B.addApproval('code-review', 2)
633 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700634
Paul Belanger174a8272017-03-14 13:20:10 -0400635 self.executor_server.failJob('project-test1', A)
636 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700637
James E. Blair8b5408c2016-08-08 15:37:46 -0700638 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
639 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
640 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700641 self.waitUntilSettled()
642
Paul Belanger174a8272017-03-14 13:20:10 -0400643 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700644 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400645 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700646 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400647 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700648 self.waitUntilSettled()
649
650 self.assertEqual(len(self.builds), 6)
651 self.assertEqual(self.builds[0].name, 'project-test1')
652 self.assertEqual(self.builds[1].name, 'project-test2')
653 self.assertEqual(self.builds[2].name, 'project-test1')
654 self.assertEqual(self.builds[3].name, 'project-test2')
655 self.assertEqual(self.builds[4].name, 'project-test1')
656 self.assertEqual(self.builds[5].name, 'project-test2')
657
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400658 self.assertTrue(self.builds[0].hasChanges(A))
659 self.assertTrue(self.builds[2].hasChanges(A))
660 self.assertTrue(self.builds[2].hasChanges(B))
661 self.assertTrue(self.builds[4].hasChanges(A))
662 self.assertTrue(self.builds[4].hasChanges(B))
663 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700664
665 # Fail change B first
666 self.release(self.builds[2])
667 self.waitUntilSettled()
668
669 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400670 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700671 self.waitUntilSettled()
672
673 self.assertEqual(len(self.builds), 5)
674 self.assertEqual(self.builds[0].name, 'project-test1')
675 self.assertEqual(self.builds[1].name, 'project-test2')
676 self.assertEqual(self.builds[2].name, 'project-test2')
677 self.assertEqual(self.builds[3].name, 'project-test1')
678 self.assertEqual(self.builds[4].name, 'project-test2')
679
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400680 self.assertTrue(self.builds[1].hasChanges(A))
681 self.assertTrue(self.builds[2].hasChanges(A))
682 self.assertTrue(self.builds[2].hasChanges(B))
683 self.assertTrue(self.builds[4].hasChanges(A))
684 self.assertFalse(self.builds[4].hasChanges(B))
685 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700686
687 # Finish running all passing jobs for change A
688 self.release(self.builds[1])
689 self.waitUntilSettled()
690 # Fail and report change A
691 self.release(self.builds[0])
692 self.waitUntilSettled()
693
694 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400695 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700696 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400697 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700698 self.waitUntilSettled()
699
700 self.assertEqual(len(self.builds), 4)
701 self.assertEqual(self.builds[0].name, 'project-test1') # B
702 self.assertEqual(self.builds[1].name, 'project-test2') # B
703 self.assertEqual(self.builds[2].name, 'project-test1') # C
704 self.assertEqual(self.builds[3].name, 'project-test2') # C
705
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400706 self.assertFalse(self.builds[1].hasChanges(A))
707 self.assertTrue(self.builds[1].hasChanges(B))
708 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700709
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400710 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700711 # After A failed and B and C restarted, B should be back in
712 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400713 self.assertTrue(self.builds[2].hasChanges(B))
714 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700715
Paul Belanger174a8272017-03-14 13:20:10 -0400716 self.executor_server.hold_jobs_in_build = False
717 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700718 self.waitUntilSettled()
719
720 self.assertEqual(len(self.builds), 0)
721 self.assertEqual(len(self.history), 21)
722 self.assertEqual(A.data['status'], 'NEW')
723 self.assertEqual(B.data['status'], 'NEW')
724 self.assertEqual(C.data['status'], 'MERGED')
725 self.assertEqual(A.reported, 2)
726 self.assertEqual(B.reported, 2)
727 self.assertEqual(C.reported, 2)
728
James E. Blair8c803f82012-07-31 16:25:42 -0700729 def test_patch_order(self):
730 "Test that dependent patches are tested in the right order"
731 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
732 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
733 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700734 A.addApproval('code-review', 2)
735 B.addApproval('code-review', 2)
736 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700737
738 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
739 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
740 M2.setMerged()
741 M1.setMerged()
742
743 # C -> B -> A -> M1 -> M2
744 # M2 is here to make sure it is never queried. If it is, it
745 # means zuul is walking down the entire history of merged
746 # changes.
747
748 C.setDependsOn(B, 1)
749 B.setDependsOn(A, 1)
750 A.setDependsOn(M1, 1)
751 M1.setDependsOn(M2, 1)
752
James E. Blair8b5408c2016-08-08 15:37:46 -0700753 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700754
755 self.waitUntilSettled()
756
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400757 self.assertEqual(A.data['status'], 'NEW')
758 self.assertEqual(B.data['status'], 'NEW')
759 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700760
James E. Blair8b5408c2016-08-08 15:37:46 -0700761 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
762 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700763
764 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400765 self.assertEqual(M2.queried, 0)
766 self.assertEqual(A.data['status'], 'MERGED')
767 self.assertEqual(B.data['status'], 'MERGED')
768 self.assertEqual(C.data['status'], 'MERGED')
769 self.assertEqual(A.reported, 2)
770 self.assertEqual(B.reported, 2)
771 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700772
James E. Blair063672f2015-01-29 13:09:12 -0800773 def test_needed_changes_enqueue(self):
774 "Test that a needed change is enqueued ahead"
775 # A Given a git tree like this, if we enqueue
776 # / \ change C, we should walk up and down the tree
777 # B G and enqueue changes in the order ABCDEFG.
778 # /|\ This is also the order that you would get if
779 # *C E F you enqueued changes in the order ABCDEFG, so
780 # / the ordering is stable across re-enqueue events.
781 # D
782
783 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
784 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
785 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
786 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
787 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
788 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
789 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
790 B.setDependsOn(A, 1)
791 C.setDependsOn(B, 1)
792 D.setDependsOn(C, 1)
793 E.setDependsOn(B, 1)
794 F.setDependsOn(B, 1)
795 G.setDependsOn(A, 1)
796
James E. Blair8b5408c2016-08-08 15:37:46 -0700797 A.addApproval('code-review', 2)
798 B.addApproval('code-review', 2)
799 C.addApproval('code-review', 2)
800 D.addApproval('code-review', 2)
801 E.addApproval('code-review', 2)
802 F.addApproval('code-review', 2)
803 G.addApproval('code-review', 2)
804 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800805
806 self.waitUntilSettled()
807
808 self.assertEqual(A.data['status'], 'NEW')
809 self.assertEqual(B.data['status'], 'NEW')
810 self.assertEqual(C.data['status'], 'NEW')
811 self.assertEqual(D.data['status'], 'NEW')
812 self.assertEqual(E.data['status'], 'NEW')
813 self.assertEqual(F.data['status'], 'NEW')
814 self.assertEqual(G.data['status'], 'NEW')
815
816 # We're about to add approvals to changes without adding the
817 # triggering events to Zuul, so that we can be sure that it is
818 # enqueing the changes based on dependencies, not because of
819 # triggering events. Since it will have the changes cached
820 # already (without approvals), we need to clear the cache
821 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400822 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100823 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800824
Paul Belanger174a8272017-03-14 13:20:10 -0400825 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700826 A.addApproval('approved', 1)
827 B.addApproval('approved', 1)
828 D.addApproval('approved', 1)
829 E.addApproval('approved', 1)
830 F.addApproval('approved', 1)
831 G.addApproval('approved', 1)
832 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800833
834 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400835 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800836 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400837 self.executor_server.hold_jobs_in_build = False
838 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800839 self.waitUntilSettled()
840
841 self.assertEqual(A.data['status'], 'MERGED')
842 self.assertEqual(B.data['status'], 'MERGED')
843 self.assertEqual(C.data['status'], 'MERGED')
844 self.assertEqual(D.data['status'], 'MERGED')
845 self.assertEqual(E.data['status'], 'MERGED')
846 self.assertEqual(F.data['status'], 'MERGED')
847 self.assertEqual(G.data['status'], 'MERGED')
848 self.assertEqual(A.reported, 2)
849 self.assertEqual(B.reported, 2)
850 self.assertEqual(C.reported, 2)
851 self.assertEqual(D.reported, 2)
852 self.assertEqual(E.reported, 2)
853 self.assertEqual(F.reported, 2)
854 self.assertEqual(G.reported, 2)
855 self.assertEqual(self.history[6].changes,
856 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
857
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100858 def test_source_cache(self):
859 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400860 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700861
862 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
863 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
864 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700865 A.addApproval('code-review', 2)
866 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700867
868 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
869 M1.setMerged()
870
871 B.setDependsOn(A, 1)
872 A.setDependsOn(M1, 1)
873
James E. Blair8b5408c2016-08-08 15:37:46 -0700874 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700875 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
876
877 self.waitUntilSettled()
878
879 for build in self.builds:
880 if build.parameters['ZUUL_PIPELINE'] == 'check':
881 build.release()
882 self.waitUntilSettled()
883 for build in self.builds:
884 if build.parameters['ZUUL_PIPELINE'] == 'check':
885 build.release()
886 self.waitUntilSettled()
887
James E. Blair8b5408c2016-08-08 15:37:46 -0700888 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700889 self.waitUntilSettled()
890
Joshua Hesketh352264b2015-08-11 23:42:08 +1000891 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700892 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000893 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700894
Paul Belanger174a8272017-03-14 13:20:10 -0400895 self.executor_server.hold_jobs_in_build = False
896 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700897 self.waitUntilSettled()
898
899 self.assertEqual(A.data['status'], 'MERGED')
900 self.assertEqual(B.data['status'], 'MERGED')
901 self.assertEqual(A.queried, 2) # Initial and isMerged
902 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
903
James E. Blair8c803f82012-07-31 16:25:42 -0700904 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700905 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700906 # TODO: move to test_gerrit (this is a unit test!)
907 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400908 tenant = self.sched.abide.tenants.get('tenant-one')
909 source = tenant.layout.pipelines['gate'].source
910
911 # TODO(pabelanger): As we add more source / trigger APIs we should make
912 # it easier for users to create events for testing.
913 event = zuul.model.TriggerEvent()
914 event.trigger_name = 'gerrit'
915 event.change_number = '1'
916 event.patch_number = '2'
917
918 a = source.getChange(event)
919 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700920 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700921
James E. Blair8b5408c2016-08-08 15:37:46 -0700922 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400923 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700924 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700925
James E. Blair8b5408c2016-08-08 15:37:46 -0700926 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400927 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700928 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700929
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700930 def test_project_merge_conflict(self):
931 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700932
933 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700934 A = self.fake_gerrit.addFakeChange('org/project',
935 'master', 'A',
936 files={'conflict': 'foo'})
937 B = self.fake_gerrit.addFakeChange('org/project',
938 'master', 'B',
939 files={'conflict': 'bar'})
940 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700941 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700942 A.addApproval('code-review', 2)
943 B.addApproval('code-review', 2)
944 C.addApproval('code-review', 2)
945 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
946 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
947 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700948 self.waitUntilSettled()
949
James E. Blair6736beb2013-07-11 15:18:15 -0700950 self.assertEqual(A.reported, 1)
951 self.assertEqual(B.reported, 1)
952 self.assertEqual(C.reported, 1)
953
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700954 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700955 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700956 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700957 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700958 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700960
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700961 self.gearman_server.hold_jobs_in_queue = False
962 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700963 self.waitUntilSettled()
964
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400965 self.assertEqual(A.data['status'], 'MERGED')
966 self.assertEqual(B.data['status'], 'NEW')
967 self.assertEqual(C.data['status'], 'MERGED')
968 self.assertEqual(A.reported, 2)
969 self.assertEqual(B.reported, 2)
970 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700971
972 self.assertHistory([
973 dict(name='project-merge', result='SUCCESS', changes='1,1'),
974 dict(name='project-test1', result='SUCCESS', changes='1,1'),
975 dict(name='project-test2', result='SUCCESS', changes='1,1'),
976 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
977 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
978 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
979 ], ordered=False)
980
981 def test_delayed_merge_conflict(self):
982 "Test that delayed check merge conflicts are handled properly"
983
984 # Hold jobs in the gearman queue so that we can test whether
985 # the executor returns a merge failure after the scheduler has
986 # successfully merged.
987 self.gearman_server.hold_jobs_in_queue = True
988 A = self.fake_gerrit.addFakeChange('org/project',
989 'master', 'A',
990 files={'conflict': 'foo'})
991 B = self.fake_gerrit.addFakeChange('org/project',
992 'master', 'B',
993 files={'conflict': 'bar'})
994 C = self.fake_gerrit.addFakeChange('org/project',
995 'master', 'C')
996 C.setDependsOn(B, 1)
997
998 # A enters the gate queue; B and C enter the check queue
999 A.addApproval('code-review', 2)
1000 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1001 self.waitUntilSettled()
1002 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1003 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1004 self.waitUntilSettled()
1005
1006 self.assertEqual(A.reported, 1)
1007 self.assertEqual(B.reported, 0) # Check does not report start
1008 self.assertEqual(C.reported, 0) # Check does not report start
1009
1010 # A merges while B and C are queued in check
1011 # Release A project-merge
1012 queue = self.gearman_server.getQueue()
1013 self.release(queue[0])
1014 self.waitUntilSettled()
1015
1016 # Release A project-test*
1017 # gate has higher precedence, so A's test jobs are added in
1018 # front of the merge jobs for B and C
1019 queue = self.gearman_server.getQueue()
1020 self.release(queue[0])
1021 self.release(queue[1])
1022 self.waitUntilSettled()
1023
1024 self.assertEqual(A.data['status'], 'MERGED')
1025 self.assertEqual(B.data['status'], 'NEW')
1026 self.assertEqual(C.data['status'], 'NEW')
1027 self.assertEqual(A.reported, 2)
1028 self.assertEqual(B.reported, 0)
1029 self.assertEqual(C.reported, 0)
1030 self.assertHistory([
1031 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1032 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1033 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1034 ], ordered=False)
1035
1036 # B and C report merge conflicts
1037 # Release B project-merge
1038 queue = self.gearman_server.getQueue()
1039 self.release(queue[0])
1040 self.waitUntilSettled()
1041
1042 # Release C
1043 self.gearman_server.hold_jobs_in_queue = False
1044 self.gearman_server.release()
1045 self.waitUntilSettled()
1046
1047 self.assertEqual(A.data['status'], 'MERGED')
1048 self.assertEqual(B.data['status'], 'NEW')
1049 self.assertEqual(C.data['status'], 'NEW')
1050 self.assertEqual(A.reported, 2)
1051 self.assertEqual(B.reported, 1)
1052 self.assertEqual(C.reported, 1)
1053
1054 self.assertHistory([
1055 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1056 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1057 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1058 dict(name='project-merge', result='MERGER_FAILURE', changes='2,1'),
1059 dict(name='project-merge', result='MERGER_FAILURE',
1060 changes='2,1 3,1'),
1061 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001062
James E. Blairdaabed22012-08-15 15:38:57 -07001063 def test_post(self):
1064 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001065
Zhongyue Luo5d556072012-09-21 02:00:47 +09001066 e = {
1067 "type": "ref-updated",
1068 "submitter": {
1069 "name": "User Name",
1070 },
1071 "refUpdate": {
1072 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1073 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1074 "refName": "master",
1075 "project": "org/project",
1076 }
1077 }
James E. Blairdaabed22012-08-15 15:38:57 -07001078 self.fake_gerrit.addEvent(e)
1079 self.waitUntilSettled()
1080
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001081 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001082 self.assertEqual(len(self.history), 1)
1083 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001084
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001085 def test_post_ignore_deletes(self):
1086 "Test that deleting refs does not trigger post jobs"
1087
1088 e = {
1089 "type": "ref-updated",
1090 "submitter": {
1091 "name": "User Name",
1092 },
1093 "refUpdate": {
1094 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1095 "newRev": "0000000000000000000000000000000000000000",
1096 "refName": "master",
1097 "project": "org/project",
1098 }
1099 }
1100 self.fake_gerrit.addEvent(e)
1101 self.waitUntilSettled()
1102
1103 job_names = [x.name for x in self.history]
1104 self.assertEqual(len(self.history), 0)
1105 self.assertNotIn('project-post', job_names)
1106
1107 def test_post_ignore_deletes_negative(self):
1108 "Test that deleting refs does trigger post jobs"
1109
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001110 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001111 self.sched.reconfigure(self.config)
1112
1113 e = {
1114 "type": "ref-updated",
1115 "submitter": {
1116 "name": "User Name",
1117 },
1118 "refUpdate": {
1119 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1120 "newRev": "0000000000000000000000000000000000000000",
1121 "refName": "master",
1122 "project": "org/project",
1123 }
1124 }
1125 self.fake_gerrit.addEvent(e)
1126 self.waitUntilSettled()
1127
1128 job_names = [x.name for x in self.history]
1129 self.assertEqual(len(self.history), 1)
1130 self.assertIn('project-post', job_names)
1131
James E. Blairec056492016-07-22 09:45:56 -07001132 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001133 def test_build_configuration_branch_interaction(self):
1134 "Test that switching between branches works"
1135 self.test_build_configuration()
1136 self.test_build_configuration_branch()
1137 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001138 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001139 repo = git.Repo(path)
1140 repo.heads.master.commit = repo.commit('init')
1141 self.test_build_configuration()
1142
James E. Blaircaec0c52012-08-22 14:52:22 -07001143 def test_dependent_changes_dequeue(self):
1144 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001145
James E. Blaircaec0c52012-08-22 14:52:22 -07001146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1147 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1148 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001149 A.addApproval('code-review', 2)
1150 B.addApproval('code-review', 2)
1151 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001152
1153 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1154 M1.setMerged()
1155
1156 # C -> B -> A -> M1
1157
1158 C.setDependsOn(B, 1)
1159 B.setDependsOn(A, 1)
1160 A.setDependsOn(M1, 1)
1161
Paul Belanger174a8272017-03-14 13:20:10 -04001162 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001163
James E. Blair8b5408c2016-08-08 15:37:46 -07001164 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1165 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1166 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001167
1168 self.waitUntilSettled()
1169
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001170 self.assertEqual(A.data['status'], 'NEW')
1171 self.assertEqual(A.reported, 2)
1172 self.assertEqual(B.data['status'], 'NEW')
1173 self.assertEqual(B.reported, 2)
1174 self.assertEqual(C.data['status'], 'NEW')
1175 self.assertEqual(C.reported, 2)
1176 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001177
James E. Blair972e3c72013-08-29 12:04:55 -07001178 def test_failing_dependent_changes(self):
1179 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001180 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001181 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1182 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1183 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1184 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1185 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001186 A.addApproval('code-review', 2)
1187 B.addApproval('code-review', 2)
1188 C.addApproval('code-review', 2)
1189 D.addApproval('code-review', 2)
1190 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001191
1192 # E, D -> C -> B, A
1193
1194 D.setDependsOn(C, 1)
1195 C.setDependsOn(B, 1)
1196
Paul Belanger174a8272017-03-14 13:20:10 -04001197 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001198
James E. Blair8b5408c2016-08-08 15:37:46 -07001199 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1200 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1201 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1202 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1203 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001204
1205 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001206 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001207 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001208 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001209 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001210 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001211 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001212 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001213 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001214 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001215 self.waitUntilSettled()
1216
Paul Belanger174a8272017-03-14 13:20:10 -04001217 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001218 for build in self.builds:
1219 if build.parameters['ZUUL_CHANGE'] != '1':
1220 build.release()
1221 self.waitUntilSettled()
1222
Paul Belanger174a8272017-03-14 13:20:10 -04001223 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001224 self.waitUntilSettled()
1225
1226 self.assertEqual(A.data['status'], 'MERGED')
1227 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001228 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001229 self.assertEqual(B.data['status'], 'NEW')
1230 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001231 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001232 self.assertEqual(C.data['status'], 'NEW')
1233 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001234 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001235 self.assertEqual(D.data['status'], 'NEW')
1236 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001237 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001238 self.assertEqual(E.data['status'], 'MERGED')
1239 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001240 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001241 self.assertEqual(len(self.history), 18)
1242
James E. Blairec590122012-08-22 15:19:31 -07001243 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001244 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001245 # If it's dequeued more than once, we should see extra
1246 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001247
Paul Belanger174a8272017-03-14 13:20:10 -04001248 self.executor_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001249 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1250 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1251 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001252 A.addApproval('code-review', 2)
1253 B.addApproval('code-review', 2)
1254 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001255
Paul Belanger174a8272017-03-14 13:20:10 -04001256 self.executor_server.failJob('project-test1', A)
1257 self.executor_server.failJob('project-test2', A)
1258 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001259
James E. Blair8b5408c2016-08-08 15:37:46 -07001260 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1261 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1262 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001263
1264 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001265
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001266 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001267 self.assertEqual(self.builds[0].name, 'project-merge')
1268 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001269
Paul Belanger174a8272017-03-14 13:20:10 -04001270 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001271 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001272 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001273 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001274 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001275 self.waitUntilSettled()
1276
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001277 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001278 self.assertEqual(self.builds[0].name, 'project-test1')
1279 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001281 self.assertEqual(self.builds[3].name, 'project-test1')
1282 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001283 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001284 self.assertEqual(self.builds[6].name, 'project-test1')
1285 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001286 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001287
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001288 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001289 self.waitUntilSettled()
1290
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001291 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1292 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001293
Paul Belanger174a8272017-03-14 13:20:10 -04001294 self.executor_server.hold_jobs_in_build = False
1295 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001296 self.waitUntilSettled()
1297
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001298 self.assertEqual(len(self.builds), 0)
1299 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001300
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001301 self.assertEqual(A.data['status'], 'NEW')
1302 self.assertEqual(B.data['status'], 'MERGED')
1303 self.assertEqual(C.data['status'], 'MERGED')
1304 self.assertEqual(A.reported, 2)
1305 self.assertEqual(B.reported, 2)
1306 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001307
1308 def test_nonvoting_job(self):
1309 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001310
James E. Blair4ec821f2012-08-23 15:28:28 -07001311 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1312 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001313 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001314 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001315 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001316
1317 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001318
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001319 self.assertEqual(A.data['status'], 'MERGED')
1320 self.assertEqual(A.reported, 2)
1321 self.assertEqual(
1322 self.getJobFromHistory('nonvoting-project-merge').result,
1323 'SUCCESS')
1324 self.assertEqual(
1325 self.getJobFromHistory('nonvoting-project-test1').result,
1326 'SUCCESS')
1327 self.assertEqual(
1328 self.getJobFromHistory('nonvoting-project-test2').result,
1329 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001330
James E. Blair5821bd92015-09-16 08:48:15 -07001331 for build in self.builds:
1332 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1333
James E. Blaire0487072012-08-29 17:38:31 -07001334 def test_check_queue_success(self):
1335 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001336
James E. Blaire0487072012-08-29 17:38:31 -07001337 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1338 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1339
1340 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001341
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001342 self.assertEqual(A.data['status'], 'NEW')
1343 self.assertEqual(A.reported, 1)
1344 self.assertEqual(self.getJobFromHistory('project-merge').result,
1345 'SUCCESS')
1346 self.assertEqual(self.getJobFromHistory('project-test1').result,
1347 'SUCCESS')
1348 self.assertEqual(self.getJobFromHistory('project-test2').result,
1349 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001350
1351 def test_check_queue_failure(self):
1352 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001353
James E. Blaire0487072012-08-29 17:38:31 -07001354 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001355 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001356 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1357
1358 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(A.data['status'], 'NEW')
1361 self.assertEqual(A.reported, 1)
1362 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001363 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001364 self.assertEqual(self.getJobFromHistory('project-test1').result,
1365 'SUCCESS')
1366 self.assertEqual(self.getJobFromHistory('project-test2').result,
1367 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001368
1369 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001370 # This particular test does a large amount of merges and needs a little
1371 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001372 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001373 "test that dependent changes behind dequeued changes work"
1374 # This complicated test is a reproduction of a real life bug
1375 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001376
Paul Belanger174a8272017-03-14 13:20:10 -04001377 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001378 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1379 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1380 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1381 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1382 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1383 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1384 D.setDependsOn(C, 1)
1385 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001386 A.addApproval('code-review', 2)
1387 B.addApproval('code-review', 2)
1388 C.addApproval('code-review', 2)
1389 D.addApproval('code-review', 2)
1390 E.addApproval('code-review', 2)
1391 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001392
1393 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001394
1395 # Change object re-use in the gerrit trigger is hidden if
1396 # changes are added in quick succession; waiting makes it more
1397 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001398 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001399 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001400 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001401 self.waitUntilSettled()
1402
Paul Belanger174a8272017-03-14 13:20:10 -04001403 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001404 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001405 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001406 self.waitUntilSettled()
1407
James E. Blair8b5408c2016-08-08 15:37:46 -07001408 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001409 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001410 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001411 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001412 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001413 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001414 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001415 self.waitUntilSettled()
1416
Paul Belanger174a8272017-03-14 13:20:10 -04001417 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001418 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001419 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001420 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001421 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001422 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001423 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001424 self.waitUntilSettled()
1425
1426 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001427
1428 # Grab pointers to the jobs we want to release before
1429 # releasing any, because list indexes may change as
1430 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001431 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001432 a.release()
1433 b.release()
1434 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001435 self.waitUntilSettled()
1436
Paul Belanger174a8272017-03-14 13:20:10 -04001437 self.executor_server.hold_jobs_in_build = False
1438 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001439 self.waitUntilSettled()
1440
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001441 self.assertEqual(A.data['status'], 'NEW')
1442 self.assertEqual(B.data['status'], 'MERGED')
1443 self.assertEqual(C.data['status'], 'MERGED')
1444 self.assertEqual(D.data['status'], 'MERGED')
1445 self.assertEqual(E.data['status'], 'MERGED')
1446 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001447
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001448 self.assertEqual(A.reported, 2)
1449 self.assertEqual(B.reported, 2)
1450 self.assertEqual(C.reported, 2)
1451 self.assertEqual(D.reported, 2)
1452 self.assertEqual(E.reported, 2)
1453 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001454
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1456 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001457
1458 def test_merger_repack(self):
1459 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001460
James E. Blair05fed602012-09-07 12:45:24 -07001461 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001462 A.addApproval('code-review', 2)
1463 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001464 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001465 self.assertEqual(self.getJobFromHistory('project-merge').result,
1466 'SUCCESS')
1467 self.assertEqual(self.getJobFromHistory('project-test1').result,
1468 'SUCCESS')
1469 self.assertEqual(self.getJobFromHistory('project-test2').result,
1470 'SUCCESS')
1471 self.assertEqual(A.data['status'], 'MERGED')
1472 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001473 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001474 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001475
Monty Taylord642d852017-02-23 14:05:42 -05001476 path = os.path.join(self.merger_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001477 if os.path.exists(path):
1478 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001479 path = os.path.join(self.executor_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001480 if os.path.exists(path):
1481 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001482
1483 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001484 A.addApproval('code-review', 2)
1485 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001486 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001487 self.assertEqual(self.getJobFromHistory('project-merge').result,
1488 'SUCCESS')
1489 self.assertEqual(self.getJobFromHistory('project-test1').result,
1490 'SUCCESS')
1491 self.assertEqual(self.getJobFromHistory('project-test2').result,
1492 'SUCCESS')
1493 self.assertEqual(A.data['status'], 'MERGED')
1494 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001495
James E. Blair4886f282012-11-15 09:27:33 -08001496 def test_merger_repack_large_change(self):
1497 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001498 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001499 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001500 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001501 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001502 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001503 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001504 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1505 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001506 path = os.path.join(self.upstream_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001507 repack_repo(path)
Monty Taylord642d852017-02-23 14:05:42 -05001508 path = os.path.join(self.merger_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001509 if os.path.exists(path):
1510 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001511 path = os.path.join(self.executor_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001512 if os.path.exists(path):
1513 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001514
James E. Blair8b5408c2016-08-08 15:37:46 -07001515 A.addApproval('code-review', 2)
1516 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001517 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001518 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001519 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001520 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001521 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001522 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001523 'SUCCESS')
1524 self.assertEqual(A.data['status'], 'MERGED')
1525 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001526
James E. Blair2fa50962013-01-30 21:50:41 -08001527 def test_new_patchset_dequeues_old(self):
1528 "Test that a new patchset causes the old to be dequeued"
1529 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001530 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001531 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1532 M.setMerged()
1533
1534 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1535 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1536 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1537 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001538 A.addApproval('code-review', 2)
1539 B.addApproval('code-review', 2)
1540 C.addApproval('code-review', 2)
1541 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001542
1543 C.setDependsOn(B, 1)
1544 B.setDependsOn(A, 1)
1545 A.setDependsOn(M, 1)
1546
James E. Blair8b5408c2016-08-08 15:37:46 -07001547 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1548 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1549 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1550 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001551 self.waitUntilSettled()
1552
1553 B.addPatchset()
1554 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1555 self.waitUntilSettled()
1556
Paul Belanger174a8272017-03-14 13:20:10 -04001557 self.executor_server.hold_jobs_in_build = False
1558 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001559 self.waitUntilSettled()
1560
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001561 self.assertEqual(A.data['status'], 'MERGED')
1562 self.assertEqual(A.reported, 2)
1563 self.assertEqual(B.data['status'], 'NEW')
1564 self.assertEqual(B.reported, 2)
1565 self.assertEqual(C.data['status'], 'NEW')
1566 self.assertEqual(C.reported, 2)
1567 self.assertEqual(D.data['status'], 'MERGED')
1568 self.assertEqual(D.reported, 2)
1569 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001570
James E. Blairba437362015-02-07 11:41:52 -08001571 def test_new_patchset_check(self):
1572 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001573
Paul Belanger174a8272017-03-14 13:20:10 -04001574 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001575
1576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001577 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001578 tenant = self.sched.abide.tenants.get('tenant-one')
1579 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001580
1581 # Add two git-dependent changes
1582 B.setDependsOn(A, 1)
1583 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1584 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001585 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1586 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001587
1588 # A live item, and a non-live/live pair
1589 items = check_pipeline.getAllItems()
1590 self.assertEqual(len(items), 3)
1591
1592 self.assertEqual(items[0].change.number, '1')
1593 self.assertEqual(items[0].change.patchset, '1')
1594 self.assertFalse(items[0].live)
1595
1596 self.assertEqual(items[1].change.number, '2')
1597 self.assertEqual(items[1].change.patchset, '1')
1598 self.assertTrue(items[1].live)
1599
1600 self.assertEqual(items[2].change.number, '1')
1601 self.assertEqual(items[2].change.patchset, '1')
1602 self.assertTrue(items[2].live)
1603
1604 # Add a new patchset to A
1605 A.addPatchset()
1606 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1607 self.waitUntilSettled()
1608
1609 # The live copy of A,1 should be gone, but the non-live and B
1610 # should continue, and we should have a new A,2
1611 items = check_pipeline.getAllItems()
1612 self.assertEqual(len(items), 3)
1613
1614 self.assertEqual(items[0].change.number, '1')
1615 self.assertEqual(items[0].change.patchset, '1')
1616 self.assertFalse(items[0].live)
1617
1618 self.assertEqual(items[1].change.number, '2')
1619 self.assertEqual(items[1].change.patchset, '1')
1620 self.assertTrue(items[1].live)
1621
1622 self.assertEqual(items[2].change.number, '1')
1623 self.assertEqual(items[2].change.patchset, '2')
1624 self.assertTrue(items[2].live)
1625
1626 # Add a new patchset to B
1627 B.addPatchset()
1628 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1629 self.waitUntilSettled()
1630
1631 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1632 # but we should have a new B,2 (still based on A,1)
1633 items = check_pipeline.getAllItems()
1634 self.assertEqual(len(items), 3)
1635
1636 self.assertEqual(items[0].change.number, '1')
1637 self.assertEqual(items[0].change.patchset, '2')
1638 self.assertTrue(items[0].live)
1639
1640 self.assertEqual(items[1].change.number, '1')
1641 self.assertEqual(items[1].change.patchset, '1')
1642 self.assertFalse(items[1].live)
1643
1644 self.assertEqual(items[2].change.number, '2')
1645 self.assertEqual(items[2].change.patchset, '2')
1646 self.assertTrue(items[2].live)
1647
1648 self.builds[0].release()
1649 self.waitUntilSettled()
1650 self.builds[0].release()
1651 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001652 self.executor_server.hold_jobs_in_build = False
1653 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001654 self.waitUntilSettled()
1655
1656 self.assertEqual(A.reported, 1)
1657 self.assertEqual(B.reported, 1)
1658 self.assertEqual(self.history[0].result, 'ABORTED')
1659 self.assertEqual(self.history[0].changes, '1,1')
1660 self.assertEqual(self.history[1].result, 'ABORTED')
1661 self.assertEqual(self.history[1].changes, '1,1 2,1')
1662 self.assertEqual(self.history[2].result, 'SUCCESS')
1663 self.assertEqual(self.history[2].changes, '1,2')
1664 self.assertEqual(self.history[3].result, 'SUCCESS')
1665 self.assertEqual(self.history[3].changes, '1,1 2,2')
1666
1667 def test_abandoned_gate(self):
1668 "Test that an abandoned change is dequeued from gate"
1669
Paul Belanger174a8272017-03-14 13:20:10 -04001670 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001671
1672 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001673 A.addApproval('code-review', 2)
1674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001675 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001676 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1677 self.assertEqual(self.builds[0].name, 'project-merge')
1678
1679 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1680 self.waitUntilSettled()
1681
Paul Belanger174a8272017-03-14 13:20:10 -04001682 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001683 self.waitUntilSettled()
1684
Arie5b4048c2016-10-05 16:12:06 +03001685 self.assertBuilds([])
1686 self.assertHistory([
1687 dict(name='project-merge', result='ABORTED', changes='1,1')],
1688 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001689 self.assertEqual(A.reported, 1,
1690 "Abandoned gate change should report only start")
1691
1692 def test_abandoned_check(self):
1693 "Test that an abandoned change is dequeued from check"
1694
Paul Belanger174a8272017-03-14 13:20:10 -04001695 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001696
1697 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1698 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001699 tenant = self.sched.abide.tenants.get('tenant-one')
1700 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001701
1702 # Add two git-dependent changes
1703 B.setDependsOn(A, 1)
1704 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1705 self.waitUntilSettled()
1706 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1707 self.waitUntilSettled()
1708 # A live item, and a non-live/live pair
1709 items = check_pipeline.getAllItems()
1710 self.assertEqual(len(items), 3)
1711
1712 self.assertEqual(items[0].change.number, '1')
1713 self.assertFalse(items[0].live)
1714
1715 self.assertEqual(items[1].change.number, '2')
1716 self.assertTrue(items[1].live)
1717
1718 self.assertEqual(items[2].change.number, '1')
1719 self.assertTrue(items[2].live)
1720
1721 # Abandon A
1722 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1723 self.waitUntilSettled()
1724
1725 # The live copy of A should be gone, but the non-live and B
1726 # should continue
1727 items = check_pipeline.getAllItems()
1728 self.assertEqual(len(items), 2)
1729
1730 self.assertEqual(items[0].change.number, '1')
1731 self.assertFalse(items[0].live)
1732
1733 self.assertEqual(items[1].change.number, '2')
1734 self.assertTrue(items[1].live)
1735
Paul Belanger174a8272017-03-14 13:20:10 -04001736 self.executor_server.hold_jobs_in_build = False
1737 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001738 self.waitUntilSettled()
1739
1740 self.assertEqual(len(self.history), 4)
1741 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001742 'Build should have been aborted')
1743 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001744 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001745
Steve Varnau7b78b312015-04-03 14:49:46 -07001746 def test_abandoned_not_timer(self):
1747 "Test that an abandoned change does not cancel timer jobs"
1748
Paul Belanger174a8272017-03-14 13:20:10 -04001749 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001750
1751 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001752 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001753 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001754 # The pipeline triggers every second, so we should have seen
1755 # several by now.
1756 time.sleep(5)
1757 self.waitUntilSettled()
1758 # Stop queuing timer triggered jobs so that the assertions
1759 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001760 # Must be in same repo, so overwrite config with another one
Clint Byrum58264dc2017-02-07 21:21:22 -08001761 self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clint Byrumead6c562017-02-01 16:34:04 -08001762
Steve Varnau7b78b312015-04-03 14:49:46 -07001763 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001764 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1765
1766 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1767 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1768 self.waitUntilSettled()
1769 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1770
1771 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1772 self.waitUntilSettled()
1773
1774 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1775
Paul Belanger174a8272017-03-14 13:20:10 -04001776 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001777 self.waitUntilSettled()
1778
Arx Cruzb1b010d2013-10-28 19:49:59 -02001779 def test_zuul_url_return(self):
1780 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001781 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001782 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001783
1784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001785 A.addApproval('code-review', 2)
1786 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001787 self.waitUntilSettled()
1788
1789 self.assertEqual(len(self.builds), 1)
1790 for build in self.builds:
1791 self.assertTrue('ZUUL_URL' in build.parameters)
1792
Paul Belanger174a8272017-03-14 13:20:10 -04001793 self.executor_server.hold_jobs_in_build = False
1794 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001795 self.waitUntilSettled()
1796
James E. Blair2fa50962013-01-30 21:50:41 -08001797 def test_new_patchset_dequeues_old_on_head(self):
1798 "Test that a new patchset causes the old to be dequeued (at head)"
1799 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001800 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001801 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1802 M.setMerged()
1803 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1804 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1805 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1806 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001807 A.addApproval('code-review', 2)
1808 B.addApproval('code-review', 2)
1809 C.addApproval('code-review', 2)
1810 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001811
1812 C.setDependsOn(B, 1)
1813 B.setDependsOn(A, 1)
1814 A.setDependsOn(M, 1)
1815
James E. Blair8b5408c2016-08-08 15:37:46 -07001816 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1817 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1818 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1819 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001820 self.waitUntilSettled()
1821
1822 A.addPatchset()
1823 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1824 self.waitUntilSettled()
1825
Paul Belanger174a8272017-03-14 13:20:10 -04001826 self.executor_server.hold_jobs_in_build = False
1827 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001828 self.waitUntilSettled()
1829
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001830 self.assertEqual(A.data['status'], 'NEW')
1831 self.assertEqual(A.reported, 2)
1832 self.assertEqual(B.data['status'], 'NEW')
1833 self.assertEqual(B.reported, 2)
1834 self.assertEqual(C.data['status'], 'NEW')
1835 self.assertEqual(C.reported, 2)
1836 self.assertEqual(D.data['status'], 'MERGED')
1837 self.assertEqual(D.reported, 2)
1838 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001839
1840 def test_new_patchset_dequeues_old_without_dependents(self):
1841 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001842 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001843 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1844 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1845 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001846 A.addApproval('code-review', 2)
1847 B.addApproval('code-review', 2)
1848 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001849
James E. Blair8b5408c2016-08-08 15:37:46 -07001850 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1851 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1852 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001853 self.waitUntilSettled()
1854
1855 B.addPatchset()
1856 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1857 self.waitUntilSettled()
1858
Paul Belanger174a8272017-03-14 13:20:10 -04001859 self.executor_server.hold_jobs_in_build = False
1860 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001861 self.waitUntilSettled()
1862
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001863 self.assertEqual(A.data['status'], 'MERGED')
1864 self.assertEqual(A.reported, 2)
1865 self.assertEqual(B.data['status'], 'NEW')
1866 self.assertEqual(B.reported, 2)
1867 self.assertEqual(C.data['status'], 'MERGED')
1868 self.assertEqual(C.reported, 2)
1869 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001870
1871 def test_new_patchset_dequeues_old_independent_queue(self):
1872 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001873 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001874 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1875 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1876 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1877 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1878 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1879 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1880 self.waitUntilSettled()
1881
1882 B.addPatchset()
1883 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1884 self.waitUntilSettled()
1885
Paul Belanger174a8272017-03-14 13:20:10 -04001886 self.executor_server.hold_jobs_in_build = False
1887 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001888 self.waitUntilSettled()
1889
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001890 self.assertEqual(A.data['status'], 'NEW')
1891 self.assertEqual(A.reported, 1)
1892 self.assertEqual(B.data['status'], 'NEW')
1893 self.assertEqual(B.reported, 1)
1894 self.assertEqual(C.data['status'], 'NEW')
1895 self.assertEqual(C.reported, 1)
1896 self.assertEqual(len(self.history), 10)
1897 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001898
James E. Blair18c64442014-03-18 10:14:45 -07001899 def test_noop_job(self):
1900 "Test that the internal noop job works"
1901 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001902 A.addApproval('code-review', 2)
1903 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001904 self.waitUntilSettled()
1905
1906 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1907 self.assertTrue(self.sched._areAllBuildsComplete())
1908 self.assertEqual(len(self.history), 0)
1909 self.assertEqual(A.data['status'], 'MERGED')
1910 self.assertEqual(A.reported, 2)
1911
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001912 def test_no_job_project(self):
1913 "Test that reports with no jobs don't get sent"
1914 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1915 'master', 'A')
1916 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1917 self.waitUntilSettled()
1918
1919 # Change wasn't reported to
1920 self.assertEqual(A.reported, False)
1921
1922 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001923 tenant = self.sched.abide.tenants.get('tenant-one')
1924 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001925 items = check_pipeline.getAllItems()
1926 self.assertEqual(len(items), 0)
1927
1928 self.assertEqual(len(self.history), 0)
1929
James E. Blair7d0dedc2013-02-21 17:26:09 -08001930 def test_zuul_refs(self):
1931 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001932 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001933 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1934 M1.setMerged()
1935 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1936 M2.setMerged()
1937
1938 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1939 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1940 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1941 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001942 A.addApproval('code-review', 2)
1943 B.addApproval('code-review', 2)
1944 C.addApproval('code-review', 2)
1945 D.addApproval('code-review', 2)
1946 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1947 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1948 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1949 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001950
1951 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001952 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001953 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001954 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001955 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001956 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001957 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001958 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001959 self.waitUntilSettled()
1960
James E. Blair7d0dedc2013-02-21 17:26:09 -08001961 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001962 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001963 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001964 if x.parameters['ZUUL_CHANGE'] == '3':
1965 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001966 a_build = x
1967 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001968 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001969 b_build = x
1970 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001971 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001972 c_build = x
1973 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001974 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001975 d_build = x
1976 if a_build and b_build and c_build and d_build:
1977 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001978
1979 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001980 self.assertIsNotNone(a_zref)
1981 self.assertIsNotNone(b_zref)
1982 self.assertIsNotNone(c_zref)
1983 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001984
1985 # And they should all be different
1986 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001987 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001988
Clint Byrum3343e3e2016-11-15 16:05:03 -08001989 # should have a, not b, and should not be in project2
1990 self.assertTrue(a_build.hasChanges(A))
1991 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001992
Clint Byrum3343e3e2016-11-15 16:05:03 -08001993 # should have a and b, and should not be in project2
1994 self.assertTrue(b_build.hasChanges(A, B))
1995 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001996
Clint Byrum3343e3e2016-11-15 16:05:03 -08001997 # should have a and b in 1, c in 2
1998 self.assertTrue(c_build.hasChanges(A, B, C))
1999 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002000
Clint Byrum3343e3e2016-11-15 16:05:03 -08002001 # should have a and b in 1, c and d in 2
2002 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002003
Paul Belanger174a8272017-03-14 13:20:10 -04002004 self.executor_server.hold_jobs_in_build = False
2005 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002006 self.waitUntilSettled()
2007
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002008 self.assertEqual(A.data['status'], 'MERGED')
2009 self.assertEqual(A.reported, 2)
2010 self.assertEqual(B.data['status'], 'MERGED')
2011 self.assertEqual(B.reported, 2)
2012 self.assertEqual(C.data['status'], 'MERGED')
2013 self.assertEqual(C.reported, 2)
2014 self.assertEqual(D.data['status'], 'MERGED')
2015 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002016
James E. Blair4a28a882013-08-23 15:17:33 -07002017 def test_rerun_on_error(self):
2018 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002019 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002020 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002021 A.addApproval('code-review', 2)
2022 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002023 self.waitUntilSettled()
2024
James E. Blair412fba82017-01-26 15:00:50 -08002025 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002026 self.executor_server.hold_jobs_in_build = False
2027 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002028 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002029 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002030 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2031
James E. Blair412e5582013-04-22 15:50:12 -07002032 def test_statsd(self):
2033 "Test each of the statsd methods used in the scheduler"
2034 import extras
2035 statsd = extras.try_import('statsd.statsd')
2036 statsd.incr('test-incr')
2037 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002038 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002039 self.assertReportedStat('test-incr', '1|c')
2040 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002041 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002042
James E. Blairdad52252014-02-07 16:59:17 -08002043 def test_stuck_job_cleanup(self):
2044 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002045
2046 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002047 self.gearman_server.hold_jobs_in_queue = True
2048 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002049 A.addApproval('code-review', 2)
2050 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002051 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002052 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002053 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2054
Clint Byrum678e2c32017-03-16 16:27:21 -07002055 self.commitLayoutUpdate('common-config', 'layout-no-jobs')
James E. Blairdad52252014-02-07 16:59:17 -08002056 self.sched.reconfigure(self.config)
2057 self.waitUntilSettled()
2058
James E. Blair18c64442014-03-18 10:14:45 -07002059 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002060 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002061 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002062 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2063 self.assertTrue(self.sched._areAllBuildsComplete())
2064
2065 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002066 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002067 self.assertEqual(self.history[0].result, 'SUCCESS')
2068
James E. Blair879dafb2015-07-17 14:04:49 -07002069 def test_file_head(self):
2070 # This is a regression test for an observed bug. A change
2071 # with a file named "HEAD" in the root directory of the repo
2072 # was processed by a merger. It then was unable to reset the
2073 # repo because of:
2074 # GitCommandError: 'git reset --hard HEAD' returned
2075 # with exit code 128
2076 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2077 # and filename
2078 # Use '--' to separate filenames from revisions'
2079
2080 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002081 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002082 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2083
2084 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2085 self.waitUntilSettled()
2086
2087 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2088 self.waitUntilSettled()
2089
2090 self.assertIn('Build succeeded', A.messages[0])
2091 self.assertIn('Build succeeded', B.messages[0])
2092
James E. Blair70c71582013-03-06 08:50:50 -08002093 def test_file_jobs(self):
2094 "Test that file jobs run only when appropriate"
2095 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002096 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002097 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002098 A.addApproval('code-review', 2)
2099 B.addApproval('code-review', 2)
2100 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2101 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002102 self.waitUntilSettled()
2103
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002104 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002105 if x.name == 'project-testfile']
2106
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002107 self.assertEqual(len(testfile_jobs), 1)
2108 self.assertEqual(testfile_jobs[0].changes, '1,2')
2109 self.assertEqual(A.data['status'], 'MERGED')
2110 self.assertEqual(A.reported, 2)
2111 self.assertEqual(B.data['status'], 'MERGED')
2112 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002113
Clint Byrum3ee39f32016-11-17 23:45:07 -08002114 def _test_irrelevant_files_jobs(self, should_skip):
2115 "Test that jobs with irrelevant-files filter run only when appropriate"
2116 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002117 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002118
2119 if should_skip:
2120 files = {'ignoreme': 'ignored\n'}
2121 else:
2122 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002123
2124 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002125 'master',
2126 'test irrelevant-files',
2127 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002128 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2129 self.waitUntilSettled()
2130
2131 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002132 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002133
2134 if should_skip:
2135 self.assertEqual([], tested_change_ids)
2136 else:
2137 self.assertIn(change.data['number'], tested_change_ids)
2138
Clint Byrum3ee39f32016-11-17 23:45:07 -08002139 def test_irrelevant_files_match_skips_job(self):
2140 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002141
Clint Byrum3ee39f32016-11-17 23:45:07 -08002142 def test_irrelevant_files_no_match_runs_job(self):
2143 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002144
Clint Byrum85493602016-11-18 11:59:47 -08002145 def test_inherited_jobs_keep_matchers(self):
2146 self.updateConfigLayout('layout-inheritance')
2147 self.sched.reconfigure(self.config)
2148
2149 files = {'ignoreme': 'ignored\n'}
2150
2151 change = self.fake_gerrit.addFakeChange('org/project',
2152 'master',
2153 'test irrelevant-files',
2154 files=files)
2155 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2156 self.waitUntilSettled()
2157
2158 run_jobs = set([build.name for build in self.history])
2159
2160 self.assertEqual(set(['project-test-nomatch-starts-empty',
2161 'project-test-nomatch-starts-full']), run_jobs)
2162
James E. Blairec056492016-07-22 09:45:56 -07002163 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002164 def test_test_config(self):
2165 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002166 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002167 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002168
James E. Blairc8a1e052014-02-25 09:29:26 -08002169 def test_queue_names(self):
2170 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002171 tenant = self.sched.abide.tenants.get('tenant-one')
2172 source = tenant.layout.pipelines['gate'].source
2173 project1 = source.getProject('org/project1')
2174 project2 = source.getProject('org/project2')
2175 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2176 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2177 self.assertEqual(q1.name, 'integrated')
2178 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002179
James E. Blair64ed6f22013-07-10 14:07:23 -07002180 def test_queue_precedence(self):
2181 "Test that queue precedence works"
2182
2183 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002184 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2186 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002187 A.addApproval('code-review', 2)
2188 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002189
2190 self.waitUntilSettled()
2191 self.gearman_server.hold_jobs_in_queue = False
2192 self.gearman_server.release()
2193 self.waitUntilSettled()
2194
James E. Blair8de58bd2013-07-18 16:23:33 -07002195 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002196 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002197 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002198 self.waitUntilSettled()
2199
James E. Blair64ed6f22013-07-10 14:07:23 -07002200 self.log.debug(self.history)
2201 self.assertEqual(self.history[0].pipeline, 'gate')
2202 self.assertEqual(self.history[1].pipeline, 'check')
2203 self.assertEqual(self.history[2].pipeline, 'gate')
2204 self.assertEqual(self.history[3].pipeline, 'gate')
2205 self.assertEqual(self.history[4].pipeline, 'check')
2206 self.assertEqual(self.history[5].pipeline, 'check')
2207
Clark Boylana5edbe42014-06-03 16:39:10 -07002208 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002209 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002210 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002211 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002212 A.addApproval('code-review', 2)
2213 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002214 self.waitUntilSettled()
2215
Paul Belanger174a8272017-03-14 13:20:10 -04002216 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002217 self.waitUntilSettled()
2218
James E. Blair1843a552013-07-03 14:19:52 -07002219 port = self.webapp.server.socket.getsockname()[1]
2220
Adam Gandelman77a12c72017-02-03 17:43:43 -08002221 req = urllib.request.Request(
2222 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002223 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002224 headers = f.info()
2225 self.assertIn('Content-Length', headers)
2226 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002227 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2228 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002229 self.assertIn('Access-Control-Allow-Origin', headers)
2230 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002231 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002232 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002233 data = f.read()
2234
Paul Belanger174a8272017-03-14 13:20:10 -04002235 self.executor_server.hold_jobs_in_build = False
2236 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002237 self.waitUntilSettled()
2238
2239 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002240 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002241 for p in data['pipelines']:
2242 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002243 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002244 self.assertEqual(q['window'], 20)
2245 else:
2246 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002247 for head in q['heads']:
2248 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002249 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002250 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002251 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002252 status_jobs.append(job)
2253 self.assertEqual('project-merge', status_jobs[0]['name'])
2254 self.assertEqual('https://server/job/project-merge/0/',
2255 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002256 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002257 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002258 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002259 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002260 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002261 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002262 status_jobs[1]['report_url'])
2263
2264 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002265 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002266 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002267 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002268 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002269
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002270 def test_semaphore_one(self):
2271 "Test semaphores with max=1 (mutex)"
2272 self.updateConfigLayout('layout-semaphore')
James E. Blairaf17a972016-02-03 15:07:18 -08002273 self.sched.reconfigure(self.config)
2274
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002275 self.waitUntilSettled()
2276 tenant = self.sched.abide.tenants.get('openstack')
2277
Paul Belanger174a8272017-03-14 13:20:10 -04002278 self.executor_server.hold_jobs_in_build = True
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002279
James E. Blairaf17a972016-02-03 15:07:18 -08002280 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2281 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002282 self.assertFalse('test-semaphore' in
2283 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002284
2285 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2286 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2287 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002288
James E. Blairaf17a972016-02-03 15:07:18 -08002289 self.assertEqual(len(self.builds), 3)
2290 self.assertEqual(self.builds[0].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002291 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002292 self.assertEqual(self.builds[2].name, 'project-test1')
2293
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002294 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002295 self.waitUntilSettled()
2296
2297 self.assertEqual(len(self.builds), 3)
2298 self.assertEqual(self.builds[0].name, 'project-test1')
2299 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002300 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2301 self.assertTrue('test-semaphore' in
2302 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002303
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002304 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002305 self.waitUntilSettled()
2306
2307 self.assertEqual(len(self.builds), 3)
2308 self.assertEqual(self.builds[0].name, 'project-test1')
2309 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002310 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
2311 self.assertTrue('test-semaphore' in
2312 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002313
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002314 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002315 self.waitUntilSettled()
2316
2317 self.assertEqual(len(self.builds), 3)
2318 self.assertEqual(self.builds[0].name, 'project-test1')
2319 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002320 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2321 self.assertTrue('test-semaphore' in
2322 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002323
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002324 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002325 self.waitUntilSettled()
2326
2327 self.assertEqual(len(self.builds), 2)
2328 self.assertEqual(self.builds[0].name, 'project-test1')
2329 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002330 self.assertFalse('test-semaphore' in
2331 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002332
Paul Belanger174a8272017-03-14 13:20:10 -04002333 self.executor_server.hold_jobs_in_build = False
2334 self.executor_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002335
2336 self.waitUntilSettled()
2337 self.assertEqual(len(self.builds), 0)
2338
2339 self.assertEqual(A.reported, 1)
2340 self.assertEqual(B.reported, 1)
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002341 self.assertFalse('test-semaphore' in
2342 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002343
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002344 def test_semaphore_two(self):
2345 "Test semaphores with max>1"
2346 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002347 self.sched.reconfigure(self.config)
2348
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002349 self.waitUntilSettled()
2350 tenant = self.sched.abide.tenants.get('openstack')
2351
2352 self.executor_server.hold_jobs_in_build = True
2353 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2354 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2355 self.assertFalse('test-semaphore-two' in
2356 tenant.semaphore_handler.semaphores)
2357
2358 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2359 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2360 self.waitUntilSettled()
2361
2362 self.assertEqual(len(self.builds), 4)
2363 self.assertEqual(self.builds[0].name, 'project-test1')
2364 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
2365 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2366 self.assertEqual(self.builds[3].name, 'project-test1')
2367 self.assertTrue('test-semaphore-two' in
2368 tenant.semaphore_handler.semaphores)
2369 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2370 'test-semaphore-two', [])), 2)
2371
2372 self.executor_server.release('semaphore-two-test1')
2373 self.waitUntilSettled()
2374
2375 self.assertEqual(len(self.builds), 4)
2376 self.assertEqual(self.builds[0].name, 'project-test1')
2377 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
2378 self.assertEqual(self.builds[2].name, 'project-test1')
2379 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
2380 self.assertTrue('test-semaphore-two' in
2381 tenant.semaphore_handler.semaphores)
2382 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2383 'test-semaphore-two', [])), 2)
2384
2385 self.executor_server.release('semaphore-two-test2')
2386 self.waitUntilSettled()
2387
2388 self.assertEqual(len(self.builds), 4)
2389 self.assertEqual(self.builds[0].name, 'project-test1')
2390 self.assertEqual(self.builds[1].name, 'project-test1')
2391 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
2392 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
2393 self.assertTrue('test-semaphore-two' in
2394 tenant.semaphore_handler.semaphores)
2395 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2396 'test-semaphore-two', [])), 2)
2397
2398 self.executor_server.release('semaphore-two-test1')
2399 self.waitUntilSettled()
2400
2401 self.assertEqual(len(self.builds), 3)
2402 self.assertEqual(self.builds[0].name, 'project-test1')
2403 self.assertEqual(self.builds[1].name, 'project-test1')
2404 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2405 self.assertTrue('test-semaphore-two' in
2406 tenant.semaphore_handler.semaphores)
2407 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2408 'test-semaphore-two', [])), 1)
2409
2410 self.executor_server.release('semaphore-two-test2')
2411 self.waitUntilSettled()
2412
2413 self.assertEqual(len(self.builds), 2)
2414 self.assertEqual(self.builds[0].name, 'project-test1')
2415 self.assertEqual(self.builds[1].name, 'project-test1')
2416 self.assertFalse('test-semaphore-two' in
2417 tenant.semaphore_handler.semaphores)
2418
2419 self.executor_server.hold_jobs_in_build = False
2420 self.executor_server.release()
2421
2422 self.waitUntilSettled()
2423 self.assertEqual(len(self.builds), 0)
2424
2425 self.assertEqual(A.reported, 1)
2426 self.assertEqual(B.reported, 1)
2427
2428 def test_semaphore_abandon(self):
2429 "Test abandon with job semaphores"
2430 self.updateConfigLayout('layout-semaphore')
2431 self.sched.reconfigure(self.config)
2432
2433 self.waitUntilSettled()
2434 tenant = self.sched.abide.tenants.get('openstack')
2435
Paul Belanger174a8272017-03-14 13:20:10 -04002436 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002437
2438 tenant = self.sched.abide.tenants.get('openstack')
2439 check_pipeline = tenant.layout.pipelines['check']
2440
2441 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002442 self.assertFalse('test-semaphore' in
2443 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002444
2445 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2446 self.waitUntilSettled()
2447
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002448 self.assertTrue('test-semaphore' in
2449 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002450
2451 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2452 self.waitUntilSettled()
2453
2454 # The check pipeline should be empty
2455 items = check_pipeline.getAllItems()
2456 self.assertEqual(len(items), 0)
2457
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002458 # The semaphore should be released
2459 self.assertFalse('test-semaphore' in
2460 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002461
Paul Belanger174a8272017-03-14 13:20:10 -04002462 self.executor_server.hold_jobs_in_build = False
2463 self.executor_server.release()
Tobias Henkelfb91a492017-02-15 07:29:43 +01002464 self.waitUntilSettled()
2465
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002466 def test_semaphore_reconfigure(self):
2467 "Test reconfigure with job semaphores"
2468 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002469 self.sched.reconfigure(self.config)
2470
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002471 self.waitUntilSettled()
2472 tenant = self.sched.abide.tenants.get('openstack')
2473
Paul Belanger174a8272017-03-14 13:20:10 -04002474 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002475
2476 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002477 self.assertFalse('test-semaphore' in
2478 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002479
2480 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2481 self.waitUntilSettled()
2482
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002483 self.assertTrue('test-semaphore' in
2484 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002485
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002486 # reconfigure without layout change
Tobias Henkelfb91a492017-02-15 07:29:43 +01002487 self.sched.reconfigure(self.config)
2488 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002489 tenant = self.sched.abide.tenants.get('openstack')
2490
2491 # semaphore still must be held
2492 self.assertTrue('test-semaphore' in
2493 tenant.semaphore_handler.semaphores)
2494
2495 self.updateConfigLayout('layout-semaphore-reconfiguration')
2496 self.sched.reconfigure(self.config)
2497 self.waitUntilSettled()
2498 tenant = self.sched.abide.tenants.get('openstack')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002499
Paul Belanger174a8272017-03-14 13:20:10 -04002500 self.executor_server.release('project-test1')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002501 self.waitUntilSettled()
2502
2503 # There should be no builds anymore
2504 self.assertEqual(len(self.builds), 0)
2505
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002506 # The semaphore should be released
2507 self.assertFalse('test-semaphore' in
2508 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002509
James E. Blaircdccd972013-07-01 12:10:22 -07002510 def test_live_reconfiguration(self):
2511 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002512 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002513 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002514 A.addApproval('code-review', 2)
2515 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002516 self.waitUntilSettled()
2517
2518 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002519 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002520
Paul Belanger174a8272017-03-14 13:20:10 -04002521 self.executor_server.hold_jobs_in_build = False
2522 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002523 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002524 self.assertEqual(self.getJobFromHistory('project-merge').result,
2525 'SUCCESS')
2526 self.assertEqual(self.getJobFromHistory('project-test1').result,
2527 'SUCCESS')
2528 self.assertEqual(self.getJobFromHistory('project-test2').result,
2529 'SUCCESS')
2530 self.assertEqual(A.data['status'], 'MERGED')
2531 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002532
James E. Blairec056492016-07-22 09:45:56 -07002533 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002534 def test_live_reconfiguration_merge_conflict(self):
2535 # A real-world bug: a change in a gate queue has a merge
2536 # conflict and a job is added to its project while it's
2537 # sitting in the queue. The job gets added to the change and
2538 # enqueued and the change gets stuck.
2539 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002540 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002541
2542 # This change is fine. It's here to stop the queue long
2543 # enough for the next change to be subject to the
2544 # reconfiguration, as well as to provide a conflict for the
2545 # next change. This change will succeed and merge.
2546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2547 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002548 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002549
2550 # This change will be in merge conflict. During the
2551 # reconfiguration, we will add a job. We want to make sure
2552 # that doesn't cause it to get stuck.
2553 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2554 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002555 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002556
James E. Blair8b5408c2016-08-08 15:37:46 -07002557 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2558 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002559
2560 self.waitUntilSettled()
2561
2562 # No jobs have run yet
2563 self.assertEqual(A.data['status'], 'NEW')
2564 self.assertEqual(A.reported, 1)
2565 self.assertEqual(B.data['status'], 'NEW')
2566 self.assertEqual(B.reported, 1)
2567 self.assertEqual(len(self.history), 0)
2568
2569 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002570 self.updateConfigLayout(
2571 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002572 self.sched.reconfigure(self.config)
2573 self.waitUntilSettled()
2574
Paul Belanger174a8272017-03-14 13:20:10 -04002575 self.executor_server.hold_jobs_in_build = False
2576 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002577 self.waitUntilSettled()
2578
2579 self.assertEqual(A.data['status'], 'MERGED')
2580 self.assertEqual(A.reported, 2)
2581 self.assertEqual(B.data['status'], 'NEW')
2582 self.assertEqual(B.reported, 2)
2583 self.assertEqual(self.getJobFromHistory('project-merge').result,
2584 'SUCCESS')
2585 self.assertEqual(self.getJobFromHistory('project-test1').result,
2586 'SUCCESS')
2587 self.assertEqual(self.getJobFromHistory('project-test2').result,
2588 'SUCCESS')
2589 self.assertEqual(self.getJobFromHistory('project-test3').result,
2590 'SUCCESS')
2591 self.assertEqual(len(self.history), 4)
2592
James E. Blairec056492016-07-22 09:45:56 -07002593 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002594 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002595 # An extrapolation of test_live_reconfiguration_merge_conflict
2596 # that tests a job added to a job tree with a failed root does
2597 # not run.
2598 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002599 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002600
2601 # This change is fine. It's here to stop the queue long
2602 # enough for the next change to be subject to the
2603 # reconfiguration. This change will succeed and merge.
2604 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2605 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002606 A.addApproval('code-review', 2)
2607 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002608 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002609 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002610 self.waitUntilSettled()
2611
2612 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002613 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002614 B.addApproval('code-review', 2)
2615 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002616 self.waitUntilSettled()
2617
Paul Belanger174a8272017-03-14 13:20:10 -04002618 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002619 self.waitUntilSettled()
2620
2621 # Both -merge jobs have run, but no others.
2622 self.assertEqual(A.data['status'], 'NEW')
2623 self.assertEqual(A.reported, 1)
2624 self.assertEqual(B.data['status'], 'NEW')
2625 self.assertEqual(B.reported, 1)
2626 self.assertEqual(self.history[0].result, 'SUCCESS')
2627 self.assertEqual(self.history[0].name, 'project-merge')
2628 self.assertEqual(self.history[1].result, 'FAILURE')
2629 self.assertEqual(self.history[1].name, 'project-merge')
2630 self.assertEqual(len(self.history), 2)
2631
2632 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002633 self.updateConfigLayout(
2634 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002635 self.sched.reconfigure(self.config)
2636 self.waitUntilSettled()
2637
Paul Belanger174a8272017-03-14 13:20:10 -04002638 self.executor_server.hold_jobs_in_build = False
2639 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002640 self.waitUntilSettled()
2641
2642 self.assertEqual(A.data['status'], 'MERGED')
2643 self.assertEqual(A.reported, 2)
2644 self.assertEqual(B.data['status'], 'NEW')
2645 self.assertEqual(B.reported, 2)
2646 self.assertEqual(self.history[0].result, 'SUCCESS')
2647 self.assertEqual(self.history[0].name, 'project-merge')
2648 self.assertEqual(self.history[1].result, 'FAILURE')
2649 self.assertEqual(self.history[1].name, 'project-merge')
2650 self.assertEqual(self.history[2].result, 'SUCCESS')
2651 self.assertEqual(self.history[3].result, 'SUCCESS')
2652 self.assertEqual(self.history[4].result, 'SUCCESS')
2653 self.assertEqual(len(self.history), 5)
2654
James E. Blairec056492016-07-22 09:45:56 -07002655 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002656 def test_live_reconfiguration_failed_job(self):
2657 # Test that a change with a removed failing job does not
2658 # disrupt reconfiguration. If a change has a failed job and
2659 # that job is removed during a reconfiguration, we observed a
2660 # bug where the code to re-set build statuses would run on
2661 # that build and raise an exception because the job no longer
2662 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002663 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002664
2665 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2666
2667 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002668 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002669
2670 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2671 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002672 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002673 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002674 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002675 self.waitUntilSettled()
2676
2677 self.assertEqual(A.data['status'], 'NEW')
2678 self.assertEqual(A.reported, 0)
2679
2680 self.assertEqual(self.getJobFromHistory('project-merge').result,
2681 'SUCCESS')
2682 self.assertEqual(self.getJobFromHistory('project-test1').result,
2683 'FAILURE')
2684 self.assertEqual(len(self.history), 2)
2685
2686 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002687 self.updateConfigLayout(
2688 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002689 self.sched.reconfigure(self.config)
2690 self.waitUntilSettled()
2691
Paul Belanger174a8272017-03-14 13:20:10 -04002692 self.executor_server.hold_jobs_in_build = False
2693 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002694 self.waitUntilSettled()
2695
2696 self.assertEqual(self.getJobFromHistory('project-test2').result,
2697 'SUCCESS')
2698 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2699 'SUCCESS')
2700 self.assertEqual(len(self.history), 4)
2701
2702 self.assertEqual(A.data['status'], 'NEW')
2703 self.assertEqual(A.reported, 1)
2704 self.assertIn('Build succeeded', A.messages[0])
2705 # Ensure the removed job was not included in the report.
2706 self.assertNotIn('project-test1', A.messages[0])
2707
James E. Blairec056492016-07-22 09:45:56 -07002708 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002709 def test_live_reconfiguration_shared_queue(self):
2710 # Test that a change with a failing job which was removed from
2711 # this project but otherwise still exists in the system does
2712 # not disrupt reconfiguration.
2713
Paul Belanger174a8272017-03-14 13:20:10 -04002714 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002715
2716 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2717
Paul Belanger174a8272017-03-14 13:20:10 -04002718 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002719
2720 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2721 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002722 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002723 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002724 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002725 self.waitUntilSettled()
2726
2727 self.assertEqual(A.data['status'], 'NEW')
2728 self.assertEqual(A.reported, 0)
2729
2730 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2731 'SUCCESS')
2732 self.assertEqual(self.getJobFromHistory(
2733 'project1-project2-integration').result, 'FAILURE')
2734 self.assertEqual(len(self.history), 2)
2735
2736 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002737 self.updateConfigLayout(
2738 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002739 self.sched.reconfigure(self.config)
2740 self.waitUntilSettled()
2741
Paul Belanger174a8272017-03-14 13:20:10 -04002742 self.executor_server.hold_jobs_in_build = False
2743 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002744 self.waitUntilSettled()
2745
2746 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2747 'SUCCESS')
2748 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2749 'SUCCESS')
2750 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2751 'SUCCESS')
2752 self.assertEqual(self.getJobFromHistory(
2753 'project1-project2-integration').result, 'FAILURE')
2754 self.assertEqual(len(self.history), 4)
2755
2756 self.assertEqual(A.data['status'], 'NEW')
2757 self.assertEqual(A.reported, 1)
2758 self.assertIn('Build succeeded', A.messages[0])
2759 # Ensure the removed job was not included in the report.
2760 self.assertNotIn('project1-project2-integration', A.messages[0])
2761
James E. Blairec056492016-07-22 09:45:56 -07002762 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002763 def test_double_live_reconfiguration_shared_queue(self):
2764 # This was a real-world regression. A change is added to
2765 # gate; a reconfigure happens, a second change which depends
2766 # on the first is added, and a second reconfiguration happens.
2767 # Ensure that both changes merge.
2768
2769 # A failure may indicate incorrect caching or cleaning up of
2770 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002771 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002772
2773 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2774 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2775 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002776 A.addApproval('code-review', 2)
2777 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002778
2779 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002780 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002781 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002782 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002783 self.waitUntilSettled()
2784
2785 # Reconfigure (with only one change in the pipeline).
2786 self.sched.reconfigure(self.config)
2787 self.waitUntilSettled()
2788
2789 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002790 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002791 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002792 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002793 self.waitUntilSettled()
2794
2795 # Reconfigure (with both in the pipeline).
2796 self.sched.reconfigure(self.config)
2797 self.waitUntilSettled()
2798
Paul Belanger174a8272017-03-14 13:20:10 -04002799 self.executor_server.hold_jobs_in_build = False
2800 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002801 self.waitUntilSettled()
2802
2803 self.assertEqual(len(self.history), 8)
2804
2805 self.assertEqual(A.data['status'], 'MERGED')
2806 self.assertEqual(A.reported, 2)
2807 self.assertEqual(B.data['status'], 'MERGED')
2808 self.assertEqual(B.reported, 2)
2809
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002810 def test_live_reconfiguration_del_project(self):
2811 # Test project deletion from layout
2812 # while changes are enqueued
2813
Paul Belanger174a8272017-03-14 13:20:10 -04002814 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002815 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2816 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2817 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2818
2819 # A Depends-On: B
2820 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2821 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002822 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002823
2824 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2825 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2826 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002827 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002828 self.waitUntilSettled()
2829 self.assertEqual(len(self.builds), 5)
2830
2831 # This layout defines only org/project, not org/project1
Clint Byrum0149ada2017-04-04 12:22:01 -07002832 self.commitLayoutUpdate('common-config',
2833 'layout-live-reconfiguration-del-project')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002834 self.sched.reconfigure(self.config)
2835 self.waitUntilSettled()
2836
2837 # Builds for C aborted, builds for A succeed,
2838 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002839 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002840 self.assertEqual(job_c.changes, '3,1')
2841 self.assertEqual(job_c.result, 'ABORTED')
2842
Paul Belanger174a8272017-03-14 13:20:10 -04002843 self.executor_server.hold_jobs_in_build = False
2844 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002845 self.waitUntilSettled()
2846
Clint Byrum0149ada2017-04-04 12:22:01 -07002847 self.assertEqual(
2848 self.getJobFromHistory('project-test1', 'org/project').changes,
2849 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002850
2851 self.assertEqual(A.data['status'], 'NEW')
2852 self.assertEqual(B.data['status'], 'NEW')
2853 self.assertEqual(C.data['status'], 'NEW')
2854 self.assertEqual(A.reported, 1)
2855 self.assertEqual(B.reported, 0)
2856 self.assertEqual(C.reported, 0)
2857
Clint Byrum0149ada2017-04-04 12:22:01 -07002858 tenant = self.sched.abide.tenants.get('tenant-one')
2859 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002860 self.assertIn('Build succeeded', A.messages[0])
2861
James E. Blairec056492016-07-22 09:45:56 -07002862 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002863 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002864 self.updateConfigLayout(
2865 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002866 self.sched.reconfigure(self.config)
2867
2868 self.init_repo("org/new-project")
2869 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2870
James E. Blair8b5408c2016-08-08 15:37:46 -07002871 A.addApproval('code-review', 2)
2872 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002873 self.waitUntilSettled()
2874 self.assertEqual(self.getJobFromHistory('project-merge').result,
2875 'SUCCESS')
2876 self.assertEqual(self.getJobFromHistory('project-test1').result,
2877 'SUCCESS')
2878 self.assertEqual(self.getJobFromHistory('project-test2').result,
2879 'SUCCESS')
2880 self.assertEqual(A.data['status'], 'MERGED')
2881 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002882
Clark Boylan6dbbc482013-10-18 10:57:31 -07002883 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002884 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002885 self.sched.reconfigure(self.config)
2886
2887 self.init_repo("org/delete-project")
2888 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2889
James E. Blair8b5408c2016-08-08 15:37:46 -07002890 A.addApproval('code-review', 2)
2891 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002892 self.waitUntilSettled()
2893 self.assertEqual(self.getJobFromHistory('project-merge').result,
2894 'SUCCESS')
2895 self.assertEqual(self.getJobFromHistory('project-test1').result,
2896 'SUCCESS')
2897 self.assertEqual(self.getJobFromHistory('project-test2').result,
2898 'SUCCESS')
2899 self.assertEqual(A.data['status'], 'MERGED')
2900 self.assertEqual(A.reported, 2)
2901
2902 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002903 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002904 if os.path.exists(os.path.join(self.merger_src_root, p)):
2905 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002906 if os.path.exists(os.path.join(self.executor_src_root, p)):
2907 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002908
2909 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2910
James E. Blair8b5408c2016-08-08 15:37:46 -07002911 B.addApproval('code-review', 2)
2912 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002913 self.waitUntilSettled()
2914 self.assertEqual(self.getJobFromHistory('project-merge').result,
2915 'SUCCESS')
2916 self.assertEqual(self.getJobFromHistory('project-test1').result,
2917 'SUCCESS')
2918 self.assertEqual(self.getJobFromHistory('project-test2').result,
2919 'SUCCESS')
2920 self.assertEqual(B.data['status'], 'MERGED')
2921 self.assertEqual(B.reported, 2)
2922
James E. Blair456f2fb2016-02-09 09:29:33 -08002923 def test_tags(self):
2924 "Test job tags"
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002925 self.updateConfigLayout('layout-tags')
James E. Blair456f2fb2016-02-09 09:29:33 -08002926 self.sched.reconfigure(self.config)
2927
2928 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2929 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2930 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2931 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2932 self.waitUntilSettled()
2933
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002934 self.assertEqual(len(self.history), 8)
2935
2936 results = {self.getJobFromHistory('merge',
2937 project='org/project1').uuid: 'extratag merge',
2938 self.getJobFromHistory('merge',
2939 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002940
2941 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002942 self.assertEqual(results.get(build.uuid, ''),
2943 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002944
James E. Blair63bb0ef2013-07-29 17:14:51 -07002945 def test_timer(self):
2946 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002947 self.executor_server.hold_jobs_in_build = True
Clint Byrum5e729e92017-02-08 06:34:57 -08002948 self.updateConfigLayout('layout-timer')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002949 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002950
Clark Boylan3ee090a2014-04-03 20:55:09 -07002951 # The pipeline triggers every second, so we should have seen
2952 # several by now.
2953 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002954 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002955
2956 self.assertEqual(len(self.builds), 2)
2957
James E. Blair63bb0ef2013-07-29 17:14:51 -07002958 port = self.webapp.server.socket.getsockname()[1]
2959
Clint Byrum5e729e92017-02-08 06:34:57 -08002960 req = urllib.request.Request(
2961 "http://localhost:%s/openstack/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002962 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002963 data = f.read()
2964
Paul Belanger174a8272017-03-14 13:20:10 -04002965 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002966 # Stop queuing timer triggered jobs so that the assertions
2967 # below don't race against more jobs being queued.
Clint Byrum5e729e92017-02-08 06:34:57 -08002968 self.commitLayoutUpdate('layout-timer', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002969 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002970 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002971 self.waitUntilSettled()
2972
2973 self.assertEqual(self.getJobFromHistory(
2974 'project-bitrot-stable-old').result, 'SUCCESS')
2975 self.assertEqual(self.getJobFromHistory(
2976 'project-bitrot-stable-older').result, 'SUCCESS')
2977
2978 data = json.loads(data)
2979 status_jobs = set()
2980 for p in data['pipelines']:
2981 for q in p['change_queues']:
2982 for head in q['heads']:
2983 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002984 for job in change['jobs']:
2985 status_jobs.add(job['name'])
2986 self.assertIn('project-bitrot-stable-old', status_jobs)
2987 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002988
James E. Blair4f6033c2014-03-27 15:49:09 -07002989 def test_idle(self):
2990 "Test that frequent periodic jobs work"
Paul Belanger174a8272017-03-14 13:20:10 -04002991 self.executor_server.hold_jobs_in_build = True
Clint Byrum58264dc2017-02-07 21:21:22 -08002992 self.updateConfigLayout('layout-idle')
James E. Blair4f6033c2014-03-27 15:49:09 -07002993
Clark Boylan3ee090a2014-04-03 20:55:09 -07002994 for x in range(1, 3):
2995 # Test that timer triggers periodic jobs even across
2996 # layout config reloads.
2997 # Start timer trigger
Clark Boylan3ee090a2014-04-03 20:55:09 -07002998 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002999 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003000
Clark Boylan3ee090a2014-04-03 20:55:09 -07003001 # The pipeline triggers every second, so we should have seen
3002 # several by now.
3003 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003004
3005 # Stop queuing timer triggered jobs so that the assertions
3006 # below don't race against more jobs being queued.
Clint Byrum58264dc2017-02-07 21:21:22 -08003007 before = self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003008 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003009 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08003010 self.assertEqual(len(self.builds), 2,
3011 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003012 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003013 self.waitUntilSettled()
3014 self.assertEqual(len(self.builds), 0)
3015 self.assertEqual(len(self.history), x * 2)
Clint Byrum58264dc2017-02-07 21:21:22 -08003016 # Revert back to layout-idle
3017 repo = git.Repo(os.path.join(self.test_root,
3018 'upstream',
3019 'layout-idle'))
3020 repo.git.reset('--hard', before)
James E. Blair4f6033c2014-03-27 15:49:09 -07003021
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003022 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05003023 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003024 self.sched.reconfigure(self.config)
3025
3026 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3027 self.waitUntilSettled()
3028
3029 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3030 self.waitUntilSettled()
3031
James E. Blairff80a2f2013-12-27 13:24:06 -08003032 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003033
3034 # A.messages only holds what FakeGerrit places in it. Thus we
3035 # work on the knowledge of what the first message should be as
3036 # it is only configured to go to SMTP.
3037
3038 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003039 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003040 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003041 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003042 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003043 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003044
3045 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003046 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003047 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003048 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003049 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003050 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003051
James E. Blaire5910202013-12-27 09:50:31 -08003052 def test_timer_smtp(self):
3053 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04003054 self.executor_server.hold_jobs_in_build = True
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003055 self.updateConfigLayout('layout-timer-smtp')
James E. Blaire5910202013-12-27 09:50:31 -08003056 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003057
Clark Boylan3ee090a2014-04-03 20:55:09 -07003058 # The pipeline triggers every second, so we should have seen
3059 # several by now.
3060 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003061 self.waitUntilSettled()
3062
Clark Boylan3ee090a2014-04-03 20:55:09 -07003063 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003064 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003065 self.waitUntilSettled()
3066 self.assertEqual(len(self.history), 2)
3067
James E. Blaire5910202013-12-27 09:50:31 -08003068 self.assertEqual(self.getJobFromHistory(
3069 'project-bitrot-stable-old').result, 'SUCCESS')
3070 self.assertEqual(self.getJobFromHistory(
3071 'project-bitrot-stable-older').result, 'SUCCESS')
3072
James E. Blairff80a2f2013-12-27 13:24:06 -08003073 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003074
3075 # A.messages only holds what FakeGerrit places in it. Thus we
3076 # work on the knowledge of what the first message should be as
3077 # it is only configured to go to SMTP.
3078
3079 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003080 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003081 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003082 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003083 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003084 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003085
Clark Boylan3ee090a2014-04-03 20:55:09 -07003086 # Stop queuing timer triggered jobs and let any that may have
3087 # queued through so that end of test assertions pass.
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003088 self.commitLayoutUpdate('layout-timer-smtp', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003089 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003090 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003091 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003092 self.waitUntilSettled()
3093
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003094 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003095 def test_timer_sshkey(self):
3096 "Test that a periodic job can setup SSH key authentication"
3097 self.worker.hold_jobs_in_build = True
3098 self.config.set('zuul', 'layout_config',
3099 'tests/fixtures/layout-timer.yaml')
3100 self.sched.reconfigure(self.config)
3101 self.registerJobs()
3102
3103 # The pipeline triggers every second, so we should have seen
3104 # several by now.
3105 time.sleep(5)
3106 self.waitUntilSettled()
3107
3108 self.assertEqual(len(self.builds), 2)
3109
3110 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3111 self.assertTrue(os.path.isfile(ssh_wrapper))
3112 with open(ssh_wrapper) as f:
3113 ssh_wrapper_content = f.read()
3114 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3115 # In the unit tests Merger runs in the same process,
3116 # so we see its' environment variables
3117 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3118
3119 self.worker.release('.*')
3120 self.waitUntilSettled()
3121 self.assertEqual(len(self.history), 2)
3122
3123 self.assertEqual(self.getJobFromHistory(
3124 'project-bitrot-stable-old').result, 'SUCCESS')
3125 self.assertEqual(self.getJobFromHistory(
3126 'project-bitrot-stable-older').result, 'SUCCESS')
3127
3128 # Stop queuing timer triggered jobs and let any that may have
3129 # queued through so that end of test assertions pass.
3130 self.config.set('zuul', 'layout_config',
3131 'tests/fixtures/layout-no-timer.yaml')
3132 self.sched.reconfigure(self.config)
3133 self.registerJobs()
3134 self.waitUntilSettled()
3135 self.worker.release('.*')
3136 self.waitUntilSettled()
3137
James E. Blair91e34592015-07-31 16:45:59 -07003138 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003139 "Test that the RPC client can enqueue a change"
3140 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003141 A.addApproval('code-review', 2)
3142 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003143
3144 client = zuul.rpcclient.RPCClient('127.0.0.1',
3145 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003146 r = client.enqueue(tenant='tenant-one',
3147 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003148 project='org/project',
3149 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003150 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003151 self.waitUntilSettled()
3152 self.assertEqual(self.getJobFromHistory('project-merge').result,
3153 'SUCCESS')
3154 self.assertEqual(self.getJobFromHistory('project-test1').result,
3155 'SUCCESS')
3156 self.assertEqual(self.getJobFromHistory('project-test2').result,
3157 'SUCCESS')
3158 self.assertEqual(A.data['status'], 'MERGED')
3159 self.assertEqual(A.reported, 2)
3160 self.assertEqual(r, True)
3161
James E. Blair91e34592015-07-31 16:45:59 -07003162 def test_client_enqueue_ref(self):
3163 "Test that the RPC client can enqueue a ref"
3164
3165 client = zuul.rpcclient.RPCClient('127.0.0.1',
3166 self.gearman_server.port)
3167 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003168 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003169 pipeline='post',
3170 project='org/project',
3171 trigger='gerrit',
3172 ref='master',
3173 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3174 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3175 self.waitUntilSettled()
3176 job_names = [x.name for x in self.history]
3177 self.assertEqual(len(self.history), 1)
3178 self.assertIn('project-post', job_names)
3179 self.assertEqual(r, True)
3180
James E. Blairad28e912013-11-27 10:43:22 -08003181 def test_client_enqueue_negative(self):
3182 "Test that the RPC client returns errors"
3183 client = zuul.rpcclient.RPCClient('127.0.0.1',
3184 self.gearman_server.port)
3185 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003186 "Invalid tenant"):
3187 r = client.enqueue(tenant='tenant-foo',
3188 pipeline='gate',
3189 project='org/project',
3190 trigger='gerrit',
3191 change='1,1')
3192 client.shutdown()
3193 self.assertEqual(r, False)
3194
3195 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003196 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003197 r = client.enqueue(tenant='tenant-one',
3198 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003199 project='project-does-not-exist',
3200 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003201 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003202 client.shutdown()
3203 self.assertEqual(r, False)
3204
3205 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3206 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003207 r = client.enqueue(tenant='tenant-one',
3208 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003209 project='org/project',
3210 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003211 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003212 client.shutdown()
3213 self.assertEqual(r, False)
3214
3215 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3216 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003217 r = client.enqueue(tenant='tenant-one',
3218 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003219 project='org/project',
3220 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003221 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003222 client.shutdown()
3223 self.assertEqual(r, False)
3224
3225 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3226 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003227 r = client.enqueue(tenant='tenant-one',
3228 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003229 project='org/project',
3230 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003231 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003232 client.shutdown()
3233 self.assertEqual(r, False)
3234
3235 self.waitUntilSettled()
3236 self.assertEqual(len(self.history), 0)
3237 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003238
3239 def test_client_promote(self):
3240 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003241 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003242 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3243 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3244 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003245 A.addApproval('code-review', 2)
3246 B.addApproval('code-review', 2)
3247 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003248
James E. Blair8b5408c2016-08-08 15:37:46 -07003249 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3250 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3251 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003252
3253 self.waitUntilSettled()
3254
Paul Belangerbaca3132016-11-04 12:49:54 -04003255 tenant = self.sched.abide.tenants.get('tenant-one')
3256 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003257 enqueue_times = {}
3258 for item in items:
3259 enqueue_times[str(item.change)] = item.enqueue_time
3260
James E. Blair36658cf2013-12-06 17:53:48 -08003261 client = zuul.rpcclient.RPCClient('127.0.0.1',
3262 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003263 r = client.promote(tenant='tenant-one',
3264 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003265 change_ids=['2,1', '3,1'])
3266
Sean Daguef39b9ca2014-01-10 21:34:35 -05003267 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003268 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003269 for item in items:
3270 self.assertEqual(
3271 enqueue_times[str(item.change)], item.enqueue_time)
3272
James E. Blair78acec92014-02-06 07:11:32 -08003273 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003274 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003275 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003276 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003277 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003278 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003279 self.waitUntilSettled()
3280
3281 self.assertEqual(len(self.builds), 6)
3282 self.assertEqual(self.builds[0].name, 'project-test1')
3283 self.assertEqual(self.builds[1].name, 'project-test2')
3284 self.assertEqual(self.builds[2].name, 'project-test1')
3285 self.assertEqual(self.builds[3].name, 'project-test2')
3286 self.assertEqual(self.builds[4].name, 'project-test1')
3287 self.assertEqual(self.builds[5].name, 'project-test2')
3288
Paul Belangerbaca3132016-11-04 12:49:54 -04003289 self.assertTrue(self.builds[0].hasChanges(B))
3290 self.assertFalse(self.builds[0].hasChanges(A))
3291 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003292
Paul Belangerbaca3132016-11-04 12:49:54 -04003293 self.assertTrue(self.builds[2].hasChanges(B))
3294 self.assertTrue(self.builds[2].hasChanges(C))
3295 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003296
Paul Belangerbaca3132016-11-04 12:49:54 -04003297 self.assertTrue(self.builds[4].hasChanges(B))
3298 self.assertTrue(self.builds[4].hasChanges(C))
3299 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003300
Paul Belanger174a8272017-03-14 13:20:10 -04003301 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003302 self.waitUntilSettled()
3303
3304 self.assertEqual(A.data['status'], 'MERGED')
3305 self.assertEqual(A.reported, 2)
3306 self.assertEqual(B.data['status'], 'MERGED')
3307 self.assertEqual(B.reported, 2)
3308 self.assertEqual(C.data['status'], 'MERGED')
3309 self.assertEqual(C.reported, 2)
3310
3311 client.shutdown()
3312 self.assertEqual(r, True)
3313
3314 def test_client_promote_dependent(self):
3315 "Test that the RPC client can promote a dependent change"
3316 # C (depends on B) -> B -> A ; then promote C to get:
3317 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003318 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003319 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3320 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3321 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3322
3323 C.setDependsOn(B, 1)
3324
James E. Blair8b5408c2016-08-08 15:37:46 -07003325 A.addApproval('code-review', 2)
3326 B.addApproval('code-review', 2)
3327 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003328
James E. Blair8b5408c2016-08-08 15:37:46 -07003329 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3330 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3331 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003332
3333 self.waitUntilSettled()
3334
3335 client = zuul.rpcclient.RPCClient('127.0.0.1',
3336 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003337 r = client.promote(tenant='tenant-one',
3338 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003339 change_ids=['3,1'])
3340
James E. Blair78acec92014-02-06 07:11:32 -08003341 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003342 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003343 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003344 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003346 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003347 self.waitUntilSettled()
3348
3349 self.assertEqual(len(self.builds), 6)
3350 self.assertEqual(self.builds[0].name, 'project-test1')
3351 self.assertEqual(self.builds[1].name, 'project-test2')
3352 self.assertEqual(self.builds[2].name, 'project-test1')
3353 self.assertEqual(self.builds[3].name, 'project-test2')
3354 self.assertEqual(self.builds[4].name, 'project-test1')
3355 self.assertEqual(self.builds[5].name, 'project-test2')
3356
Paul Belangerbaca3132016-11-04 12:49:54 -04003357 self.assertTrue(self.builds[0].hasChanges(B))
3358 self.assertFalse(self.builds[0].hasChanges(A))
3359 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003360
Paul Belangerbaca3132016-11-04 12:49:54 -04003361 self.assertTrue(self.builds[2].hasChanges(B))
3362 self.assertTrue(self.builds[2].hasChanges(C))
3363 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003364
Paul Belangerbaca3132016-11-04 12:49:54 -04003365 self.assertTrue(self.builds[4].hasChanges(B))
3366 self.assertTrue(self.builds[4].hasChanges(C))
3367 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003368
Paul Belanger174a8272017-03-14 13:20:10 -04003369 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003370 self.waitUntilSettled()
3371
3372 self.assertEqual(A.data['status'], 'MERGED')
3373 self.assertEqual(A.reported, 2)
3374 self.assertEqual(B.data['status'], 'MERGED')
3375 self.assertEqual(B.reported, 2)
3376 self.assertEqual(C.data['status'], 'MERGED')
3377 self.assertEqual(C.reported, 2)
3378
3379 client.shutdown()
3380 self.assertEqual(r, True)
3381
3382 def test_client_promote_negative(self):
3383 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003384 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003386 A.addApproval('code-review', 2)
3387 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003388 self.waitUntilSettled()
3389
3390 client = zuul.rpcclient.RPCClient('127.0.0.1',
3391 self.gearman_server.port)
3392
3393 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003394 r = client.promote(tenant='tenant-one',
3395 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003396 change_ids=['2,1', '3,1'])
3397 client.shutdown()
3398 self.assertEqual(r, False)
3399
3400 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003401 r = client.promote(tenant='tenant-one',
3402 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003403 change_ids=['4,1'])
3404 client.shutdown()
3405 self.assertEqual(r, False)
3406
Paul Belanger174a8272017-03-14 13:20:10 -04003407 self.executor_server.hold_jobs_in_build = False
3408 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003409 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003410
3411 def test_queue_rate_limiting(self):
3412 "Test that DependentPipelines are rate limited with dep across window"
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003413 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003414 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003415 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3417 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3418 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3419
3420 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003421 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003422
James E. Blair8b5408c2016-08-08 15:37:46 -07003423 A.addApproval('code-review', 2)
3424 B.addApproval('code-review', 2)
3425 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003426
James E. Blair8b5408c2016-08-08 15:37:46 -07003427 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3428 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3429 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003430 self.waitUntilSettled()
3431
3432 # Only A and B will have their merge jobs queued because
3433 # window is 2.
3434 self.assertEqual(len(self.builds), 2)
3435 self.assertEqual(self.builds[0].name, 'project-merge')
3436 self.assertEqual(self.builds[1].name, 'project-merge')
3437
Paul Belanger174a8272017-03-14 13:20:10 -04003438 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003439 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003440 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003441 self.waitUntilSettled()
3442
3443 # Only A and B will have their test jobs queued because
3444 # window is 2.
3445 self.assertEqual(len(self.builds), 4)
3446 self.assertEqual(self.builds[0].name, 'project-test1')
3447 self.assertEqual(self.builds[1].name, 'project-test2')
3448 self.assertEqual(self.builds[2].name, 'project-test1')
3449 self.assertEqual(self.builds[3].name, 'project-test2')
3450
Paul Belanger174a8272017-03-14 13:20:10 -04003451 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003452 self.waitUntilSettled()
3453
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003454 tenant = self.sched.abide.tenants.get('openstack')
3455 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003456 # A failed so window is reduced by 1 to 1.
3457 self.assertEqual(queue.window, 1)
3458 self.assertEqual(queue.window_floor, 1)
3459 self.assertEqual(A.data['status'], 'NEW')
3460
3461 # Gate is reset and only B's merge job is queued because
3462 # window shrunk to 1.
3463 self.assertEqual(len(self.builds), 1)
3464 self.assertEqual(self.builds[0].name, 'project-merge')
3465
Paul Belanger174a8272017-03-14 13:20:10 -04003466 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003467 self.waitUntilSettled()
3468
3469 # Only B's test jobs are queued because window is still 1.
3470 self.assertEqual(len(self.builds), 2)
3471 self.assertEqual(self.builds[0].name, 'project-test1')
3472 self.assertEqual(self.builds[1].name, 'project-test2')
3473
Paul Belanger174a8272017-03-14 13:20:10 -04003474 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003475 self.waitUntilSettled()
3476
3477 # B was successfully merged so window is increased to 2.
3478 self.assertEqual(queue.window, 2)
3479 self.assertEqual(queue.window_floor, 1)
3480 self.assertEqual(B.data['status'], 'MERGED')
3481
3482 # Only C is left and its merge job is queued.
3483 self.assertEqual(len(self.builds), 1)
3484 self.assertEqual(self.builds[0].name, 'project-merge')
3485
Paul Belanger174a8272017-03-14 13:20:10 -04003486 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003487 self.waitUntilSettled()
3488
3489 # After successful merge job the test jobs for C are queued.
3490 self.assertEqual(len(self.builds), 2)
3491 self.assertEqual(self.builds[0].name, 'project-test1')
3492 self.assertEqual(self.builds[1].name, 'project-test2')
3493
Paul Belanger174a8272017-03-14 13:20:10 -04003494 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003495 self.waitUntilSettled()
3496
3497 # C successfully merged so window is bumped to 3.
3498 self.assertEqual(queue.window, 3)
3499 self.assertEqual(queue.window_floor, 1)
3500 self.assertEqual(C.data['status'], 'MERGED')
3501
3502 def test_queue_rate_limiting_dependent(self):
3503 "Test that DependentPipelines are rate limited with dep in window"
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003504 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003505 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003506 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003507 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3508 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3509 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3510
3511 B.setDependsOn(A, 1)
3512
Paul Belanger174a8272017-03-14 13:20:10 -04003513 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003514
James E. Blair8b5408c2016-08-08 15:37:46 -07003515 A.addApproval('code-review', 2)
3516 B.addApproval('code-review', 2)
3517 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003518
James E. Blair8b5408c2016-08-08 15:37:46 -07003519 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3520 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3521 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003522 self.waitUntilSettled()
3523
3524 # Only A and B will have their merge jobs queued because
3525 # window is 2.
3526 self.assertEqual(len(self.builds), 2)
3527 self.assertEqual(self.builds[0].name, 'project-merge')
3528 self.assertEqual(self.builds[1].name, 'project-merge')
3529
James E. Blairf4a5f022017-04-18 14:01:10 -07003530 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003531
3532 # Only A and B will have their test jobs queued because
3533 # window is 2.
3534 self.assertEqual(len(self.builds), 4)
3535 self.assertEqual(self.builds[0].name, 'project-test1')
3536 self.assertEqual(self.builds[1].name, 'project-test2')
3537 self.assertEqual(self.builds[2].name, 'project-test1')
3538 self.assertEqual(self.builds[3].name, 'project-test2')
3539
Paul Belanger174a8272017-03-14 13:20:10 -04003540 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003541 self.waitUntilSettled()
3542
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003543 tenant = self.sched.abide.tenants.get('openstack')
3544 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003545 # A failed so window is reduced by 1 to 1.
3546 self.assertEqual(queue.window, 1)
3547 self.assertEqual(queue.window_floor, 1)
3548 self.assertEqual(A.data['status'], 'NEW')
3549 self.assertEqual(B.data['status'], 'NEW')
3550
3551 # Gate is reset and only C's merge job is queued because
3552 # window shrunk to 1 and A and B were dequeued.
3553 self.assertEqual(len(self.builds), 1)
3554 self.assertEqual(self.builds[0].name, 'project-merge')
3555
James E. Blairf4a5f022017-04-18 14:01:10 -07003556 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003557
3558 # Only C's test jobs are queued because window is still 1.
3559 self.assertEqual(len(self.builds), 2)
3560 self.assertEqual(self.builds[0].name, 'project-test1')
3561 self.assertEqual(self.builds[1].name, 'project-test2')
3562
Paul Belanger174a8272017-03-14 13:20:10 -04003563 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003564 self.waitUntilSettled()
3565
3566 # C was successfully merged so window is increased to 2.
3567 self.assertEqual(queue.window, 2)
3568 self.assertEqual(queue.window_floor, 1)
3569 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003570
James E. Blairec056492016-07-22 09:45:56 -07003571 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003572 def test_worker_update_metadata(self):
3573 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003574 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003575
3576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003577 A.addApproval('code-review', 2)
3578 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003579 self.waitUntilSettled()
3580
Paul Belanger174a8272017-03-14 13:20:10 -04003581 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003582
3583 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003584 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003585
3586 start = time.time()
3587 while True:
3588 if time.time() - start > 10:
3589 raise Exception("Timeout waiting for gearman server to report "
3590 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003591 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003592 if build.worker.name == "My Worker":
3593 break
3594 else:
3595 time.sleep(0)
3596
3597 self.log.debug(build)
3598 self.assertEqual("My Worker", build.worker.name)
3599 self.assertEqual("localhost", build.worker.hostname)
3600 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3601 self.assertEqual("zuul.example.org", build.worker.fqdn)
3602 self.assertEqual("FakeBuilder", build.worker.program)
3603 self.assertEqual("v1.1", build.worker.version)
3604 self.assertEqual({'something': 'else'}, build.worker.extra)
3605
Paul Belanger174a8272017-03-14 13:20:10 -04003606 self.executor_server.hold_jobs_in_build = False
3607 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003608 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003609
3610 def test_footer_message(self):
3611 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003612 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003613 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003614
3615 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003616 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003617 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003618 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003619 self.waitUntilSettled()
3620
3621 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003622 B.addApproval('code-review', 2)
3623 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003624 self.waitUntilSettled()
3625
3626 self.assertEqual(2, len(self.smtp_messages))
3627
Adam Gandelman82258fd2017-02-07 16:51:58 -08003628 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003629Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003630http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003631
Adam Gandelman82258fd2017-02-07 16:51:58 -08003632 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003633For CI problems and help debugging, contact ci@example.org"""
3634
Adam Gandelman82258fd2017-02-07 16:51:58 -08003635 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3636 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3637 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3638 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003639
James E. Blairec056492016-07-22 09:45:56 -07003640 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003641 def test_merge_failure_reporters(self):
3642 """Check that the config is set up correctly"""
3643
James E. Blairf84026c2015-12-08 16:11:46 -08003644 self.updateConfigLayout(
3645 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003646 self.sched.reconfigure(self.config)
3647 self.registerJobs()
3648
3649 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003650 "Merge Failed.\n\nThis change or one of its cross-repo "
3651 "dependencies was unable to be automatically merged with the "
3652 "current state of its repository. Please rebase the change and "
3653 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003654 self.sched.layout.pipelines['check'].merge_failure_message)
3655 self.assertEqual(
3656 "The merge failed! For more information...",
3657 self.sched.layout.pipelines['gate'].merge_failure_message)
3658
3659 self.assertEqual(
3660 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3661 self.assertEqual(
3662 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3663
3664 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003665 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3666 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003667
3668 self.assertTrue(
3669 (
3670 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003671 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003672 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003673 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003674 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003675 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003676 ) or (
3677 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003678 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003679 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003680 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003681 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003682 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003683 )
3684 )
3685
James E. Blairec056492016-07-22 09:45:56 -07003686 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003687 def test_merge_failure_reports(self):
3688 """Check that when a change fails to merge the correct message is sent
3689 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003690 self.updateConfigLayout(
3691 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003692 self.sched.reconfigure(self.config)
3693 self.registerJobs()
3694
3695 # Check a test failure isn't reported to SMTP
3696 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003697 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003698 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003699 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003700 self.waitUntilSettled()
3701
3702 self.assertEqual(3, len(self.history)) # 3 jobs
3703 self.assertEqual(0, len(self.smtp_messages))
3704
3705 # Check a merge failure is reported to SMTP
3706 # B should be merged, but C will conflict with B
3707 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3708 B.addPatchset(['conflict'])
3709 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3710 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003711 B.addApproval('code-review', 2)
3712 C.addApproval('code-review', 2)
3713 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3714 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003715 self.waitUntilSettled()
3716
3717 self.assertEqual(6, len(self.history)) # A and B jobs
3718 self.assertEqual(1, len(self.smtp_messages))
3719 self.assertEqual('The merge failed! For more information...',
3720 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003721
James E. Blairec056492016-07-22 09:45:56 -07003722 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003723 def test_default_merge_failure_reports(self):
3724 """Check that the default merge failure reports are correct."""
3725
3726 # A should report success, B should report merge failure.
3727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3728 A.addPatchset(['conflict'])
3729 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3730 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003731 A.addApproval('code-review', 2)
3732 B.addApproval('code-review', 2)
3733 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3734 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003735 self.waitUntilSettled()
3736
3737 self.assertEqual(3, len(self.history)) # A jobs
3738 self.assertEqual(A.reported, 2)
3739 self.assertEqual(B.reported, 2)
3740 self.assertEqual(A.data['status'], 'MERGED')
3741 self.assertEqual(B.data['status'], 'NEW')
3742 self.assertIn('Build succeeded', A.messages[1])
3743 self.assertIn('Merge Failed', B.messages[1])
3744 self.assertIn('automatically merged', B.messages[1])
3745 self.assertNotIn('logs.example.com', B.messages[1])
3746 self.assertNotIn('SKIPPED', B.messages[1])
3747
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003748 def test_client_get_running_jobs(self):
3749 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003750 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003752 A.addApproval('code-review', 2)
3753 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003754 self.waitUntilSettled()
3755
3756 client = zuul.rpcclient.RPCClient('127.0.0.1',
3757 self.gearman_server.port)
3758
3759 # Wait for gearman server to send the initial workData back to zuul
3760 start = time.time()
3761 while True:
3762 if time.time() - start > 10:
3763 raise Exception("Timeout waiting for gearman server to report "
3764 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003765 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003766 if build.worker.name == "My Worker":
3767 break
3768 else:
3769 time.sleep(0)
3770
3771 running_items = client.get_running_jobs()
3772
3773 self.assertEqual(1, len(running_items))
3774 running_item = running_items[0]
3775 self.assertEqual([], running_item['failing_reasons'])
3776 self.assertEqual([], running_item['items_behind'])
3777 self.assertEqual('https://hostname/1', running_item['url'])
3778 self.assertEqual(None, running_item['item_ahead'])
3779 self.assertEqual('org/project', running_item['project'])
3780 self.assertEqual(None, running_item['remaining_time'])
3781 self.assertEqual(True, running_item['active'])
3782 self.assertEqual('1,1', running_item['id'])
3783
3784 self.assertEqual(3, len(running_item['jobs']))
3785 for job in running_item['jobs']:
3786 if job['name'] == 'project-merge':
3787 self.assertEqual('project-merge', job['name'])
3788 self.assertEqual('gate', job['pipeline'])
3789 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003790 self.assertEqual('https://server/job/project-merge/0/',
3791 job['url'])
3792 self.assertEqual(7, len(job['worker']))
3793 self.assertEqual(False, job['canceled'])
3794 self.assertEqual(True, job['voting'])
3795 self.assertEqual(None, job['result'])
3796 self.assertEqual('gate', job['pipeline'])
3797 break
3798
Paul Belanger174a8272017-03-14 13:20:10 -04003799 self.executor_server.hold_jobs_in_build = False
3800 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003801 self.waitUntilSettled()
3802
3803 running_items = client.get_running_jobs()
3804 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003805
3806 def test_nonvoting_pipeline(self):
3807 "Test that a nonvoting pipeline (experimental) can still report"
3808
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003809 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3810 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003811 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3812 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003813
3814 self.assertEqual(self.getJobFromHistory('project-merge').result,
3815 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003816 self.assertEqual(
3817 self.getJobFromHistory('experimental-project-test').result,
3818 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003819 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003820
3821 def test_crd_gate(self):
3822 "Test cross-repo dependencies"
3823 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3824 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003825 A.addApproval('code-review', 2)
3826 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003827
3828 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3829 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3830 AM2.setMerged()
3831 AM1.setMerged()
3832
3833 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3834 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3835 BM2.setMerged()
3836 BM1.setMerged()
3837
3838 # A -> AM1 -> AM2
3839 # B -> BM1 -> BM2
3840 # A Depends-On: B
3841 # M2 is here to make sure it is never queried. If it is, it
3842 # means zuul is walking down the entire history of merged
3843 # changes.
3844
3845 B.setDependsOn(BM1, 1)
3846 BM1.setDependsOn(BM2, 1)
3847
3848 A.setDependsOn(AM1, 1)
3849 AM1.setDependsOn(AM2, 1)
3850
3851 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3852 A.subject, B.data['id'])
3853
James E. Blair8b5408c2016-08-08 15:37:46 -07003854 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003855 self.waitUntilSettled()
3856
3857 self.assertEqual(A.data['status'], 'NEW')
3858 self.assertEqual(B.data['status'], 'NEW')
3859
Paul Belanger5dccbe72016-11-14 11:17:37 -05003860 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003861 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003862
Paul Belanger174a8272017-03-14 13:20:10 -04003863 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003864 B.addApproval('approved', 1)
3865 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003866 self.waitUntilSettled()
3867
Paul Belanger174a8272017-03-14 13:20:10 -04003868 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003869 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003870 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003871 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003872 self.executor_server.hold_jobs_in_build = False
3873 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003874 self.waitUntilSettled()
3875
3876 self.assertEqual(AM2.queried, 0)
3877 self.assertEqual(BM2.queried, 0)
3878 self.assertEqual(A.data['status'], 'MERGED')
3879 self.assertEqual(B.data['status'], 'MERGED')
3880 self.assertEqual(A.reported, 2)
3881 self.assertEqual(B.reported, 2)
3882
Paul Belanger5dccbe72016-11-14 11:17:37 -05003883 changes = self.getJobFromHistory(
3884 'project-merge', 'org/project1').changes
3885 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003886
3887 def test_crd_branch(self):
3888 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003889
3890 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003891 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3892 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3893 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3894 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003895 A.addApproval('code-review', 2)
3896 B.addApproval('code-review', 2)
3897 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003898
3899 # A Depends-On: B+C
3900 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3901 A.subject, B.data['id'])
3902
Paul Belanger174a8272017-03-14 13:20:10 -04003903 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003904 B.addApproval('approved', 1)
3905 C.addApproval('approved', 1)
3906 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003907 self.waitUntilSettled()
3908
Paul Belanger174a8272017-03-14 13:20:10 -04003909 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003910 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003911 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003912 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003913 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003914 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003915 self.executor_server.hold_jobs_in_build = False
3916 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003917 self.waitUntilSettled()
3918
3919 self.assertEqual(A.data['status'], 'MERGED')
3920 self.assertEqual(B.data['status'], 'MERGED')
3921 self.assertEqual(C.data['status'], 'MERGED')
3922 self.assertEqual(A.reported, 2)
3923 self.assertEqual(B.reported, 2)
3924 self.assertEqual(C.reported, 2)
3925
Paul Belanger6379db12016-11-14 13:57:54 -05003926 changes = self.getJobFromHistory(
3927 'project-merge', 'org/project1').changes
3928 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003929
3930 def test_crd_multiline(self):
3931 "Test multiple depends-on lines in commit"
3932 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3933 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3934 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003935 A.addApproval('code-review', 2)
3936 B.addApproval('code-review', 2)
3937 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003938
3939 # A Depends-On: B+C
3940 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3941 A.subject, B.data['id'], C.data['id'])
3942
Paul Belanger174a8272017-03-14 13:20:10 -04003943 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003944 B.addApproval('approved', 1)
3945 C.addApproval('approved', 1)
3946 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003947 self.waitUntilSettled()
3948
Paul Belanger174a8272017-03-14 13:20:10 -04003949 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003950 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003951 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003952 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003953 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003954 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003955 self.executor_server.hold_jobs_in_build = False
3956 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003957 self.waitUntilSettled()
3958
3959 self.assertEqual(A.data['status'], 'MERGED')
3960 self.assertEqual(B.data['status'], 'MERGED')
3961 self.assertEqual(C.data['status'], 'MERGED')
3962 self.assertEqual(A.reported, 2)
3963 self.assertEqual(B.reported, 2)
3964 self.assertEqual(C.reported, 2)
3965
Paul Belanger5dccbe72016-11-14 11:17:37 -05003966 changes = self.getJobFromHistory(
3967 'project-merge', 'org/project1').changes
3968 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003969
3970 def test_crd_unshared_gate(self):
3971 "Test cross-repo dependencies in unshared gate queues"
3972 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3973 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003974 A.addApproval('code-review', 2)
3975 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003976
3977 # A Depends-On: B
3978 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3979 A.subject, B.data['id'])
3980
3981 # A and B do not share a queue, make sure that A is unable to
3982 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003983 B.addApproval('approved', 1)
3984 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003985 self.waitUntilSettled()
3986
3987 self.assertEqual(A.data['status'], 'NEW')
3988 self.assertEqual(B.data['status'], 'NEW')
3989 self.assertEqual(A.reported, 0)
3990 self.assertEqual(B.reported, 0)
3991 self.assertEqual(len(self.history), 0)
3992
3993 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003994 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003995 self.waitUntilSettled()
3996
3997 self.assertEqual(B.data['status'], 'MERGED')
3998 self.assertEqual(B.reported, 2)
3999
4000 # Now that B is merged, A should be able to be enqueued and
4001 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004002 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004003 self.waitUntilSettled()
4004
4005 self.assertEqual(A.data['status'], 'MERGED')
4006 self.assertEqual(A.reported, 2)
4007
James E. Blair96698e22015-04-02 07:48:21 -07004008 def test_crd_gate_reverse(self):
4009 "Test reverse cross-repo dependencies"
4010 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4011 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004012 A.addApproval('code-review', 2)
4013 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004014
4015 # A Depends-On: B
4016
4017 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4018 A.subject, B.data['id'])
4019
James E. Blair8b5408c2016-08-08 15:37:46 -07004020 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004021 self.waitUntilSettled()
4022
4023 self.assertEqual(A.data['status'], 'NEW')
4024 self.assertEqual(B.data['status'], 'NEW')
4025
Paul Belanger174a8272017-03-14 13:20:10 -04004026 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004027 A.addApproval('approved', 1)
4028 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004029 self.waitUntilSettled()
4030
Paul Belanger174a8272017-03-14 13:20:10 -04004031 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004032 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004033 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004034 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004035 self.executor_server.hold_jobs_in_build = False
4036 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004037 self.waitUntilSettled()
4038
4039 self.assertEqual(A.data['status'], 'MERGED')
4040 self.assertEqual(B.data['status'], 'MERGED')
4041 self.assertEqual(A.reported, 2)
4042 self.assertEqual(B.reported, 2)
4043
Paul Belanger5dccbe72016-11-14 11:17:37 -05004044 changes = self.getJobFromHistory(
4045 'project-merge', 'org/project1').changes
4046 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004047
James E. Blair5ee24252014-12-30 10:12:29 -08004048 def test_crd_cycle(self):
4049 "Test cross-repo dependency cycles"
4050 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4051 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004052 A.addApproval('code-review', 2)
4053 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004054
4055 # A -> B -> A (via commit-depends)
4056
4057 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4058 A.subject, B.data['id'])
4059 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4060 B.subject, A.data['id'])
4061
James E. Blair8b5408c2016-08-08 15:37:46 -07004062 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004063 self.waitUntilSettled()
4064
4065 self.assertEqual(A.reported, 0)
4066 self.assertEqual(B.reported, 0)
4067 self.assertEqual(A.data['status'], 'NEW')
4068 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004069
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004070 def test_crd_gate_unknown(self):
4071 "Test unknown projects in dependent pipeline"
4072 self.init_repo("org/unknown")
4073 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4074 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004075 A.addApproval('code-review', 2)
4076 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004077
4078 # A Depends-On: B
4079 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4080 A.subject, B.data['id'])
4081
James E. Blair8b5408c2016-08-08 15:37:46 -07004082 B.addApproval('approved', 1)
4083 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004084 self.waitUntilSettled()
4085
4086 # Unknown projects cannot share a queue with any other
4087 # since they don't have common jobs with any other (they have no jobs).
4088 # Changes which depend on unknown project changes
4089 # should not be processed in dependent pipeline
4090 self.assertEqual(A.data['status'], 'NEW')
4091 self.assertEqual(B.data['status'], 'NEW')
4092 self.assertEqual(A.reported, 0)
4093 self.assertEqual(B.reported, 0)
4094 self.assertEqual(len(self.history), 0)
4095
4096 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004097 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004098 B.setMerged()
4099 self.waitUntilSettled()
4100 self.assertEqual(len(self.history), 0)
4101
4102 # Now that B is merged, A should be able to be enqueued and
4103 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004104 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004105 self.waitUntilSettled()
4106
4107 self.assertEqual(A.data['status'], 'MERGED')
4108 self.assertEqual(A.reported, 2)
4109 self.assertEqual(B.data['status'], 'MERGED')
4110 self.assertEqual(B.reported, 0)
4111
James E. Blairbfb8e042014-12-30 17:01:44 -08004112 def test_crd_check(self):
4113 "Test cross-repo dependencies in independent pipelines"
4114
Paul Belanger174a8272017-03-14 13:20:10 -04004115 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004116 self.gearman_server.hold_jobs_in_queue = True
4117 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4118 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4119
4120 # A Depends-On: B
4121 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4122 A.subject, B.data['id'])
4123
4124 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4125 self.waitUntilSettled()
4126
4127 queue = self.gearman_server.getQueue()
4128 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4129 self.gearman_server.hold_jobs_in_queue = False
4130 self.gearman_server.release()
4131 self.waitUntilSettled()
4132
Paul Belanger174a8272017-03-14 13:20:10 -04004133 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004134 self.waitUntilSettled()
4135
Monty Taylord642d852017-02-23 14:05:42 -05004136 path = os.path.join(self.builds[0].jobdir.src_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004137 repo = git.Repo(path)
4138 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4139 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004140 correct_messages = [
4141 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004142 self.assertEqual(repo_messages, correct_messages)
4143
Monty Taylord642d852017-02-23 14:05:42 -05004144 path = os.path.join(self.builds[0].jobdir.src_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004145 repo = git.Repo(path)
4146 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4147 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004148 correct_messages = [
4149 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004150 self.assertEqual(repo_messages, correct_messages)
4151
Paul Belanger174a8272017-03-14 13:20:10 -04004152 self.executor_server.hold_jobs_in_build = False
4153 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004154 self.waitUntilSettled()
4155
James E. Blairbfb8e042014-12-30 17:01:44 -08004156 self.assertEqual(A.data['status'], 'NEW')
4157 self.assertEqual(B.data['status'], 'NEW')
4158 self.assertEqual(A.reported, 1)
4159 self.assertEqual(B.reported, 0)
4160
4161 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004162 tenant = self.sched.abide.tenants.get('tenant-one')
4163 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004164
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004165 def test_crd_check_git_depends(self):
4166 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004167 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004168 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4169 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4170
4171 # Add two git-dependent changes and make sure they both report
4172 # success.
4173 B.setDependsOn(A, 1)
4174 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4175 self.waitUntilSettled()
4176 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4177 self.waitUntilSettled()
4178
James E. Blairb8c16472015-05-05 14:55:26 -07004179 self.orderedRelease()
4180 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004181 self.waitUntilSettled()
4182
4183 self.assertEqual(A.data['status'], 'NEW')
4184 self.assertEqual(B.data['status'], 'NEW')
4185 self.assertEqual(A.reported, 1)
4186 self.assertEqual(B.reported, 1)
4187
4188 self.assertEqual(self.history[0].changes, '1,1')
4189 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004190 tenant = self.sched.abide.tenants.get('tenant-one')
4191 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004192
4193 self.assertIn('Build succeeded', A.messages[0])
4194 self.assertIn('Build succeeded', B.messages[0])
4195
4196 def test_crd_check_duplicate(self):
4197 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004198 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004199 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4200 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004201 tenant = self.sched.abide.tenants.get('tenant-one')
4202 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004203
4204 # Add two git-dependent changes...
4205 B.setDependsOn(A, 1)
4206 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4207 self.waitUntilSettled()
4208 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4209
4210 # ...make sure the live one is not duplicated...
4211 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4212 self.waitUntilSettled()
4213 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4214
4215 # ...but the non-live one is able to be.
4216 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4217 self.waitUntilSettled()
4218 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4219
Clark Boylandd849822015-03-02 12:38:14 -08004220 # Release jobs in order to avoid races with change A jobs
4221 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004222 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004223 self.executor_server.hold_jobs_in_build = False
4224 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004225 self.waitUntilSettled()
4226
4227 self.assertEqual(A.data['status'], 'NEW')
4228 self.assertEqual(B.data['status'], 'NEW')
4229 self.assertEqual(A.reported, 1)
4230 self.assertEqual(B.reported, 1)
4231
4232 self.assertEqual(self.history[0].changes, '1,1 2,1')
4233 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004234 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004235
4236 self.assertIn('Build succeeded', A.messages[0])
4237 self.assertIn('Build succeeded', B.messages[0])
4238
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004239 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004240 "Test cross-repo dependencies re-enqueued in independent pipelines"
4241
4242 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004243 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4244 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004245
4246 # A Depends-On: B
4247 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4248 A.subject, B.data['id'])
4249
4250 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4251 self.waitUntilSettled()
4252
4253 self.sched.reconfigure(self.config)
4254
4255 # Make sure the items still share a change queue, and the
4256 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004257 tenant = self.sched.abide.tenants.get('tenant-one')
4258 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4259 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004260 first_item = queue.queue[0]
4261 for item in queue.queue:
4262 self.assertEqual(item.queue, first_item.queue)
4263 self.assertFalse(first_item.live)
4264 self.assertTrue(queue.queue[1].live)
4265
4266 self.gearman_server.hold_jobs_in_queue = False
4267 self.gearman_server.release()
4268 self.waitUntilSettled()
4269
4270 self.assertEqual(A.data['status'], 'NEW')
4271 self.assertEqual(B.data['status'], 'NEW')
4272 self.assertEqual(A.reported, 1)
4273 self.assertEqual(B.reported, 0)
4274
4275 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004276 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004277
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004278 def test_crd_check_reconfiguration(self):
4279 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4280
4281 def test_crd_undefined_project(self):
4282 """Test that undefined projects in dependencies are handled for
4283 independent pipelines"""
4284 # It's a hack for fake gerrit,
4285 # as it implies repo creation upon the creation of any change
4286 self.init_repo("org/unknown")
4287 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4288
James E. Blair17dd6772015-02-09 14:45:18 -08004289 def test_crd_check_ignore_dependencies(self):
4290 "Test cross-repo dependencies can be ignored"
Clint Byrum12282ca2017-04-03 11:04:02 -07004291 self.updateConfigLayout('layout-ignore-dependencies')
James E. Blair17dd6772015-02-09 14:45:18 -08004292 self.sched.reconfigure(self.config)
James E. Blair17dd6772015-02-09 14:45:18 -08004293
4294 self.gearman_server.hold_jobs_in_queue = True
4295 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4296 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4297 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4298
4299 # A Depends-On: B
4300 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4301 A.subject, B.data['id'])
4302 # C git-depends on B
4303 C.setDependsOn(B, 1)
4304 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4305 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4306 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4307 self.waitUntilSettled()
4308
4309 # Make sure none of the items share a change queue, and all
4310 # are live.
Clint Byrum12282ca2017-04-03 11:04:02 -07004311 tenant = self.sched.abide.tenants.get('openstack')
4312 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004313 self.assertEqual(len(check_pipeline.queues), 3)
4314 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4315 for item in check_pipeline.getAllItems():
4316 self.assertTrue(item.live)
4317
4318 self.gearman_server.hold_jobs_in_queue = False
4319 self.gearman_server.release()
4320 self.waitUntilSettled()
4321
4322 self.assertEqual(A.data['status'], 'NEW')
4323 self.assertEqual(B.data['status'], 'NEW')
4324 self.assertEqual(C.data['status'], 'NEW')
4325 self.assertEqual(A.reported, 1)
4326 self.assertEqual(B.reported, 1)
4327 self.assertEqual(C.reported, 1)
4328
4329 # Each job should have tested exactly one change
4330 for job in self.history:
4331 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004332
4333 def test_crd_check_transitive(self):
4334 "Test transitive cross-repo dependencies"
4335 # Specifically, if A -> B -> C, and C gets a new patchset and
4336 # A gets a new patchset, ensure the test of A,2 includes B,1
4337 # and C,2 (not C,1 which would indicate stale data in the
4338 # cache for B).
4339 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4340 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4341 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4342
4343 # A Depends-On: B
4344 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4345 A.subject, B.data['id'])
4346
4347 # B Depends-On: C
4348 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4349 B.subject, C.data['id'])
4350
4351 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4352 self.waitUntilSettled()
4353 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4354
4355 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4356 self.waitUntilSettled()
4357 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4358
4359 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4360 self.waitUntilSettled()
4361 self.assertEqual(self.history[-1].changes, '3,1')
4362
4363 C.addPatchset()
4364 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4365 self.waitUntilSettled()
4366 self.assertEqual(self.history[-1].changes, '3,2')
4367
4368 A.addPatchset()
4369 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4370 self.waitUntilSettled()
4371 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004372
James E. Blaira8b90b32016-08-24 15:18:50 -07004373 def test_crd_check_unknown(self):
4374 "Test unknown projects in independent pipeline"
4375 self.init_repo("org/unknown")
4376 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4377 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4378 # A Depends-On: B
4379 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4380 A.subject, B.data['id'])
4381
4382 # Make sure zuul has seen an event on B.
4383 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4384 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4385 self.waitUntilSettled()
4386
4387 self.assertEqual(A.data['status'], 'NEW')
4388 self.assertEqual(A.reported, 1)
4389 self.assertEqual(B.data['status'], 'NEW')
4390 self.assertEqual(B.reported, 0)
4391
James E. Blair92464a22016-04-05 10:21:26 -07004392 def test_crd_cycle_join(self):
4393 "Test an updated change creates a cycle"
4394 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4395
4396 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4397 self.waitUntilSettled()
4398
4399 # Create B->A
4400 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4401 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4402 B.subject, A.data['id'])
4403 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4404 self.waitUntilSettled()
4405
4406 # Update A to add A->B (a cycle).
4407 A.addPatchset()
4408 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4409 A.subject, B.data['id'])
4410 # Normally we would submit the patchset-created event for
4411 # processing here, however, we have no way of noting whether
4412 # the dependency cycle detection correctly raised an
4413 # exception, so instead, we reach into the source driver and
4414 # call the method that would ultimately be called by the event
4415 # processing.
4416
Paul Belanger0e155e22016-11-14 14:12:23 -05004417 tenant = self.sched.abide.tenants.get('tenant-one')
4418 source = tenant.layout.pipelines['gate'].source
4419
4420 # TODO(pabelanger): As we add more source / trigger APIs we should make
4421 # it easier for users to create events for testing.
4422 event = zuul.model.TriggerEvent()
4423 event.trigger_name = 'gerrit'
4424 event.change_number = '1'
4425 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004426 with testtools.ExpectedException(
4427 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004428 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004429 self.log.debug("Got expected dependency cycle exception")
4430
4431 # Now if we update B to remove the depends-on, everything
4432 # should be okay. B; A->B
4433
4434 B.addPatchset()
4435 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004436
4437 source.getChange(event, True)
4438 event.change_number = '2'
4439 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004440
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004441 def test_disable_at(self):
4442 "Test a pipeline will only report to the disabled trigger when failing"
4443
Paul Belanger7dc76e82016-11-11 16:51:08 -05004444 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004445 self.sched.reconfigure(self.config)
4446
Paul Belanger7dc76e82016-11-11 16:51:08 -05004447 tenant = self.sched.abide.tenants.get('openstack')
4448 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004449 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004450 0, tenant.layout.pipelines['check']._consecutive_failures)
4451 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004452
4453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4454 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4455 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4456 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4457 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4458 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4459 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4460 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4461 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4462 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4463 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4464
Paul Belanger174a8272017-03-14 13:20:10 -04004465 self.executor_server.failJob('project-test1', A)
4466 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004467 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004468 self.executor_server.failJob('project-test1', D)
4469 self.executor_server.failJob('project-test1', E)
4470 self.executor_server.failJob('project-test1', F)
4471 self.executor_server.failJob('project-test1', G)
4472 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004473 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004474 self.executor_server.failJob('project-test1', J)
4475 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004476
4477 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4478 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4479 self.waitUntilSettled()
4480
4481 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004482 2, tenant.layout.pipelines['check']._consecutive_failures)
4483 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004484
4485 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4486 self.waitUntilSettled()
4487
4488 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004489 0, tenant.layout.pipelines['check']._consecutive_failures)
4490 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004491
4492 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4493 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4494 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4495 self.waitUntilSettled()
4496
4497 # We should be disabled now
4498 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004499 3, tenant.layout.pipelines['check']._consecutive_failures)
4500 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004501
4502 # We need to wait between each of these patches to make sure the
4503 # smtp messages come back in an expected order
4504 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4505 self.waitUntilSettled()
4506 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4507 self.waitUntilSettled()
4508 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4509 self.waitUntilSettled()
4510
4511 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4512 # leaving a message on each change
4513 self.assertEqual(1, len(A.messages))
4514 self.assertIn('Build failed.', A.messages[0])
4515 self.assertEqual(1, len(B.messages))
4516 self.assertIn('Build failed.', B.messages[0])
4517 self.assertEqual(1, len(C.messages))
4518 self.assertIn('Build succeeded.', C.messages[0])
4519 self.assertEqual(1, len(D.messages))
4520 self.assertIn('Build failed.', D.messages[0])
4521 self.assertEqual(1, len(E.messages))
4522 self.assertIn('Build failed.', E.messages[0])
4523 self.assertEqual(1, len(F.messages))
4524 self.assertIn('Build failed.', F.messages[0])
4525
4526 # The last 3 (GHI) would have only reported via smtp.
4527 self.assertEqual(3, len(self.smtp_messages))
4528 self.assertEqual(0, len(G.messages))
4529 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004530 self.assertIn(
4531 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004532 self.assertEqual(0, len(H.messages))
4533 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004534 self.assertIn(
4535 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004536 self.assertEqual(0, len(I.messages))
4537 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004538 self.assertIn(
4539 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004540
4541 # Now reload the configuration (simulate a HUP) to check the pipeline
4542 # comes out of disabled
4543 self.sched.reconfigure(self.config)
4544
Paul Belanger7dc76e82016-11-11 16:51:08 -05004545 tenant = self.sched.abide.tenants.get('openstack')
4546
4547 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004548 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004549 0, tenant.layout.pipelines['check']._consecutive_failures)
4550 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004551
4552 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4553 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4554 self.waitUntilSettled()
4555
4556 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004557 2, tenant.layout.pipelines['check']._consecutive_failures)
4558 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004559
4560 # J and K went back to gerrit
4561 self.assertEqual(1, len(J.messages))
4562 self.assertIn('Build failed.', J.messages[0])
4563 self.assertEqual(1, len(K.messages))
4564 self.assertIn('Build failed.', K.messages[0])
4565 # No more messages reported via smtp
4566 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004567
Paul Belanger71d98172016-11-08 10:56:31 -05004568 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004569 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004570
Paul Belanger174a8272017-03-14 13:20:10 -04004571 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4573 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4574 self.waitUntilSettled()
4575
Paul Belanger174a8272017-03-14 13:20:10 -04004576 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004577 self.waitUntilSettled()
4578
4579 self.assertEqual(len(self.builds), 2)
4580 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004581 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004582 self.waitUntilSettled()
4583
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004584 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004585 self.assertEqual(len(self.builds), 1,
4586 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004587 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004588 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004589 self.waitUntilSettled()
4590
Paul Belanger174a8272017-03-14 13:20:10 -04004591 self.executor_server.hold_jobs_in_build = False
4592 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004593 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004594 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004595 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4596 self.assertEqual(A.reported, 1)
4597 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004598
James E. Blair15be0e12017-01-03 13:45:20 -08004599 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004600 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004601
4602 self.fake_nodepool.paused = True
4603 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4604 A.addApproval('code-review', 2)
4605 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4606 self.waitUntilSettled()
4607
4608 self.zk.client.stop()
4609 self.zk.client.start()
4610 self.fake_nodepool.paused = False
4611 self.waitUntilSettled()
4612
4613 self.assertEqual(A.data['status'], 'MERGED')
4614 self.assertEqual(A.reported, 2)
4615
James E. Blair6ab79e02017-01-06 10:10:17 -08004616 def test_nodepool_failure(self):
4617 "Test that jobs are reported after a nodepool failure"
4618
4619 self.fake_nodepool.paused = True
4620 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4621 A.addApproval('code-review', 2)
4622 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4623 self.waitUntilSettled()
4624
4625 req = self.fake_nodepool.getNodeRequests()[0]
4626 self.fake_nodepool.addFailRequest(req)
4627
4628 self.fake_nodepool.paused = False
4629 self.waitUntilSettled()
4630
4631 self.assertEqual(A.data['status'], 'NEW')
4632 self.assertEqual(A.reported, 2)
4633 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4634 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4635 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4636
Arieb6f068c2016-10-09 13:11:06 +03004637
James E. Blaira002b032017-04-18 10:35:48 -07004638class TestExecutor(ZuulTestCase):
4639 tenant_config_file = 'config/single-tenant/main.yaml'
4640
4641 def assertFinalState(self):
4642 # In this test, we expect to shut down in a non-final state,
4643 # so skip these checks.
4644 pass
4645
4646 def assertCleanShutdown(self):
4647 self.log.debug("Assert clean shutdown")
4648
4649 # After shutdown, make sure no jobs are running
4650 self.assertEqual({}, self.executor_server.job_workers)
4651
4652 # Make sure that git.Repo objects have been garbage collected.
4653 repos = []
4654 gc.collect()
4655 for obj in gc.get_objects():
4656 if isinstance(obj, git.Repo):
4657 self.log.debug("Leaked git repo object: %s" % repr(obj))
4658 repos.append(obj)
4659 self.assertEqual(len(repos), 0)
4660
4661 def test_executor_shutdown(self):
4662 "Test that the executor can shut down with jobs running"
4663
4664 self.executor_server.hold_jobs_in_build = True
4665 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4666 A.addApproval('code-review', 2)
4667 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4668 self.waitUntilSettled()
4669
4670
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004671class TestDependencyGraph(ZuulTestCase):
4672 tenant_config_file = 'config/dependency-graph/main.yaml'
4673
4674 def test_dependeny_graph_dispatch_jobs_once(self):
4675 "Test a job in a dependency graph is queued only once"
4676 # Job dependencies, starting with A
4677 # A
4678 # / \
4679 # B C
4680 # / \ / \
4681 # D F E
4682 # |
4683 # G
4684
4685 self.executor_server.hold_jobs_in_build = True
4686 change = self.fake_gerrit.addFakeChange(
4687 'org/project', 'master', 'change')
4688 change.addApproval('code-review', 2)
4689 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4690
4691 self.waitUntilSettled()
4692 self.assertEqual([b.name for b in self.builds], ['A'])
4693
4694 self.executor_server.release('A')
4695 self.waitUntilSettled()
4696 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4697
4698 self.executor_server.release('B')
4699 self.waitUntilSettled()
4700 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4701
4702 self.executor_server.release('D')
4703 self.waitUntilSettled()
4704 self.assertEqual([b.name for b in self.builds], ['C'])
4705
4706 self.executor_server.release('C')
4707 self.waitUntilSettled()
4708 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4709
4710 self.executor_server.release('F')
4711 self.waitUntilSettled()
4712 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4713
4714 self.executor_server.release('G')
4715 self.waitUntilSettled()
4716 self.assertEqual([b.name for b in self.builds], ['E'])
4717
4718 self.executor_server.release('E')
4719 self.waitUntilSettled()
4720 self.assertEqual(len(self.builds), 0)
4721
4722 self.executor_server.hold_jobs_in_build = False
4723 self.executor_server.release()
4724 self.waitUntilSettled()
4725
4726 self.assertEqual(len(self.builds), 0)
4727 self.assertEqual(len(self.history), 7)
4728
4729 self.assertEqual(change.data['status'], 'MERGED')
4730 self.assertEqual(change.reported, 2)
4731
4732 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4733 "Test that a job waits till all dependencies are successful"
4734 # Job dependencies, starting with A
4735 # A
4736 # / \
4737 # B C*
4738 # / \ / \
4739 # D F E
4740 # |
4741 # G
4742
4743 self.executor_server.hold_jobs_in_build = True
4744 change = self.fake_gerrit.addFakeChange(
4745 'org/project', 'master', 'change')
4746 change.addApproval('code-review', 2)
4747
4748 self.executor_server.failJob('C', change)
4749
4750 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4751
4752 self.waitUntilSettled()
4753 self.assertEqual([b.name for b in self.builds], ['A'])
4754
4755 self.executor_server.release('A')
4756 self.waitUntilSettled()
4757 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4758
4759 self.executor_server.release('B')
4760 self.waitUntilSettled()
4761 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4762
4763 self.executor_server.release('D')
4764 self.waitUntilSettled()
4765 self.assertEqual([b.name for b in self.builds], ['C'])
4766
4767 self.executor_server.release('C')
4768 self.waitUntilSettled()
4769 self.assertEqual(len(self.builds), 0)
4770
4771 self.executor_server.hold_jobs_in_build = False
4772 self.executor_server.release()
4773 self.waitUntilSettled()
4774
4775 self.assertEqual(len(self.builds), 0)
4776 self.assertEqual(len(self.history), 4)
4777
4778 self.assertEqual(change.data['status'], 'NEW')
4779 self.assertEqual(change.reported, 2)
4780
4781
Arieb6f068c2016-10-09 13:11:06 +03004782class TestDuplicatePipeline(ZuulTestCase):
4783 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4784
4785 def test_duplicate_pipelines(self):
4786 "Test that a change matching multiple pipelines works"
4787
4788 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4789 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4790 self.waitUntilSettled()
4791
4792 self.assertHistory([
4793 dict(name='project-test1', result='SUCCESS', changes='1,1',
4794 pipeline='dup1'),
4795 dict(name='project-test1', result='SUCCESS', changes='1,1',
4796 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004797 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004798
4799 self.assertEqual(len(A.messages), 2)
4800
Paul Belangera46a3742016-11-09 14:23:03 -05004801 if 'dup1' in A.messages[0]:
4802 self.assertIn('dup1', A.messages[0])
4803 self.assertNotIn('dup2', A.messages[0])
4804 self.assertIn('project-test1', A.messages[0])
4805 self.assertIn('dup2', A.messages[1])
4806 self.assertNotIn('dup1', A.messages[1])
4807 self.assertIn('project-test1', A.messages[1])
4808 else:
4809 self.assertIn('dup1', A.messages[1])
4810 self.assertNotIn('dup2', A.messages[1])
4811 self.assertIn('project-test1', A.messages[1])
4812 self.assertIn('dup2', A.messages[0])
4813 self.assertNotIn('dup1', A.messages[0])
4814 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004815
4816
4817class TestSchedulerOneJobProject(ZuulTestCase):
4818 tenant_config_file = 'config/one-job-project/main.yaml'
4819
4820 def test_one_job_project(self):
4821 "Test that queueing works with one job"
4822 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4823 'master', 'A')
4824 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4825 'master', 'B')
4826 A.addApproval('code-review', 2)
4827 B.addApproval('code-review', 2)
4828 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4829 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4830 self.waitUntilSettled()
4831
4832 self.assertEqual(A.data['status'], 'MERGED')
4833 self.assertEqual(A.reported, 2)
4834 self.assertEqual(B.data['status'], 'MERGED')
4835 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004836
4837
4838class TestSchedulerTemplatedProject(ZuulTestCase):
4839 tenant_config_file = 'config/templated-project/main.yaml'
4840
Paul Belanger174a8272017-03-14 13:20:10 -04004841 def test_job_from_templates_executed(self):
4842 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004843
4844 A = self.fake_gerrit.addFakeChange(
4845 'org/templated-project', 'master', 'A')
4846 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4847 self.waitUntilSettled()
4848
4849 self.assertEqual(self.getJobFromHistory('project-test1').result,
4850 'SUCCESS')
4851 self.assertEqual(self.getJobFromHistory('project-test2').result,
4852 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004853
4854 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004855 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004856
4857 A = self.fake_gerrit.addFakeChange(
4858 'org/layered-project', 'master', 'A')
4859 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4860 self.waitUntilSettled()
4861
4862 self.assertEqual(self.getJobFromHistory('project-test1').result,
4863 'SUCCESS')
4864 self.assertEqual(self.getJobFromHistory('project-test2').result,
4865 'SUCCESS')
4866 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4867 ).result, 'SUCCESS')
4868 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4869 ).result, 'SUCCESS')
4870 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4871 ).result, 'SUCCESS')
4872 self.assertEqual(self.getJobFromHistory('project-test6').result,
4873 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004874
4875
4876class TestSchedulerSuccessURL(ZuulTestCase):
4877 tenant_config_file = 'config/success-url/main.yaml'
4878
4879 def test_success_url(self):
4880 "Ensure bad build params are ignored"
4881 self.sched.reconfigure(self.config)
4882 self.init_repo('org/docs')
4883
4884 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4885 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4886 self.waitUntilSettled()
4887
4888 # Both builds ran: docs-draft-test + docs-draft-test2
4889 self.assertEqual(len(self.history), 2)
4890
4891 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004892 for build in self.history:
4893 if build.name == 'docs-draft-test':
4894 uuid = build.uuid[:7]
4895 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004896
4897 # Two msgs: 'Starting...' + results
4898 self.assertEqual(len(self.smtp_messages), 2)
4899 body = self.smtp_messages[1]['body'].splitlines()
4900 self.assertEqual('Build succeeded.', body[0])
4901
4902 self.assertIn(
4903 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4904 'docs-draft-test/%s/publish-docs/' % uuid,
4905 body[2])
4906
Paul Belanger174a8272017-03-14 13:20:10 -04004907 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004908 self.assertIn(
4909 '- docs-draft-test2 https://server/job',
4910 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004911
4912
Adam Gandelman4da00f62016-12-09 15:47:33 -08004913class TestSchedulerMerges(ZuulTestCase):
4914 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004915
4916 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004917 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004918 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004919 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004920 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4921 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4922 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4923 A.addApproval('code-review', 2)
4924 B.addApproval('code-review', 2)
4925 C.addApproval('code-review', 2)
4926 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4927 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4928 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4929 self.waitUntilSettled()
4930
4931 build = self.builds[-1]
4932 ref = self.getParameter(build, 'ZUUL_REF')
4933
Monty Taylord642d852017-02-23 14:05:42 -05004934 path = os.path.join(build.jobdir.src_root, project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004935 repo = git.Repo(path)
4936 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4937 repo_messages.reverse()
4938
Paul Belanger174a8272017-03-14 13:20:10 -04004939 self.executor_server.hold_jobs_in_build = False
4940 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004941 self.waitUntilSettled()
4942
4943 return repo_messages
4944
4945 def _test_merge(self, mode):
4946 us_path = os.path.join(
4947 self.upstream_root, 'org/project-%s' % mode)
4948 expected_messages = [
4949 'initial commit',
4950 'add content from fixture',
4951 # the intermediate commits order is nondeterministic
4952 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4953 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4954 ]
4955 result = self._test_project_merge_mode(mode)
4956 self.assertEqual(result[:2], expected_messages[:2])
4957 self.assertEqual(result[-2:], expected_messages[-2:])
4958
4959 def test_project_merge_mode_merge(self):
4960 self._test_merge('merge')
4961
4962 def test_project_merge_mode_merge_resolve(self):
4963 self._test_merge('merge-resolve')
4964
4965 def test_project_merge_mode_cherrypick(self):
4966 expected_messages = [
4967 'initial commit',
4968 'add content from fixture',
4969 'A-1',
4970 'B-1',
4971 'C-1']
4972 result = self._test_project_merge_mode('cherry-pick')
4973 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004974
4975 def test_merge_branch(self):
4976 "Test that the right commits are on alternate branches"
4977 self.create_branch('org/project-merge-branches', 'mp')
4978
Paul Belanger174a8272017-03-14 13:20:10 -04004979 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004980 A = self.fake_gerrit.addFakeChange(
4981 'org/project-merge-branches', 'mp', 'A')
4982 B = self.fake_gerrit.addFakeChange(
4983 'org/project-merge-branches', 'mp', 'B')
4984 C = self.fake_gerrit.addFakeChange(
4985 'org/project-merge-branches', 'mp', 'C')
4986 A.addApproval('code-review', 2)
4987 B.addApproval('code-review', 2)
4988 C.addApproval('code-review', 2)
4989 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4990 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4991 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4992 self.waitUntilSettled()
4993
Paul Belanger174a8272017-03-14 13:20:10 -04004994 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004995 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004996 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004997 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004998 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004999 self.waitUntilSettled()
5000
5001 build = self.builds[-1]
5002 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
5003 ref = self.getParameter(build, 'ZUUL_REF')
5004 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005005 build.jobdir.src_root, 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005006 repo = git.Repo(path)
5007
5008 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
5009 repo_messages.reverse()
5010 correct_messages = [
5011 'initial commit',
5012 'add content from fixture',
5013 'mp commit',
5014 'A-1', 'B-1', 'C-1']
5015 self.assertEqual(repo_messages, correct_messages)
5016
Paul Belanger174a8272017-03-14 13:20:10 -04005017 self.executor_server.hold_jobs_in_build = False
5018 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005019 self.waitUntilSettled()
5020
5021 def test_merge_multi_branch(self):
5022 "Test that dependent changes on multiple branches are merged"
5023 self.create_branch('org/project-merge-branches', 'mp')
5024
Paul Belanger174a8272017-03-14 13:20:10 -04005025 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005026 A = self.fake_gerrit.addFakeChange(
5027 'org/project-merge-branches', 'master', 'A')
5028 B = self.fake_gerrit.addFakeChange(
5029 'org/project-merge-branches', 'mp', 'B')
5030 C = self.fake_gerrit.addFakeChange(
5031 'org/project-merge-branches', 'master', 'C')
5032 A.addApproval('code-review', 2)
5033 B.addApproval('code-review', 2)
5034 C.addApproval('code-review', 2)
5035 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5036 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5037 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5038 self.waitUntilSettled()
5039
5040 job_A = None
5041 for job in self.builds:
5042 if 'project-merge' in job.name:
5043 job_A = job
5044 ref_A = self.getParameter(job_A, 'ZUUL_REF')
5045 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
5046 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
5047 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
5048
5049 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005050 job_A.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005051 repo = git.Repo(path)
5052 repo_messages = [c.message.strip()
5053 for c in repo.iter_commits(ref_A)]
5054 repo_messages.reverse()
5055 correct_messages = [
5056 'initial commit', 'add content from fixture', 'A-1']
5057 self.assertEqual(repo_messages, correct_messages)
5058
Paul Belanger174a8272017-03-14 13:20:10 -04005059 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005060 self.waitUntilSettled()
5061
5062 job_B = None
5063 for job in self.builds:
5064 if 'project-merge' in job.name:
5065 job_B = job
5066 ref_B = self.getParameter(job_B, 'ZUUL_REF')
5067 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
5068 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
5069 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
5070
5071 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005072 job_B.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005073 repo = git.Repo(path)
5074 repo_messages = [c.message.strip()
5075 for c in repo.iter_commits(ref_B)]
5076 repo_messages.reverse()
5077 correct_messages = [
5078 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5079 self.assertEqual(repo_messages, correct_messages)
5080
Paul Belanger174a8272017-03-14 13:20:10 -04005081 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005082 self.waitUntilSettled()
5083
5084 job_C = None
5085 for job in self.builds:
5086 if 'project-merge' in job.name:
5087 job_C = job
5088 ref_C = self.getParameter(job_C, 'ZUUL_REF')
5089 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
5090 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
5091 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
5092 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005093 job_C.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005094 repo = git.Repo(path)
5095 repo_messages = [c.message.strip()
5096 for c in repo.iter_commits(ref_C)]
5097
5098 repo_messages.reverse()
5099 correct_messages = [
5100 'initial commit', 'add content from fixture',
5101 'A-1', 'C-1']
5102 # Ensure the right commits are in the history for this ref
5103 self.assertEqual(repo_messages, correct_messages)
5104
Paul Belanger174a8272017-03-14 13:20:10 -04005105 self.executor_server.hold_jobs_in_build = False
5106 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005107 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005108
5109
5110class TestSemaphoreMultiTenant(ZuulTestCase):
5111 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5112
5113 def test_semaphore_tenant_isolation(self):
5114 "Test semaphores in multiple tenants"
5115
5116 self.waitUntilSettled()
5117 tenant_one = self.sched.abide.tenants.get('tenant-one')
5118 tenant_two = self.sched.abide.tenants.get('tenant-two')
5119
5120 self.executor_server.hold_jobs_in_build = True
5121 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5122 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5123 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5124 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5125 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5126 self.assertFalse('test-semaphore' in
5127 tenant_one.semaphore_handler.semaphores)
5128 self.assertFalse('test-semaphore' in
5129 tenant_two.semaphore_handler.semaphores)
5130
5131 # add patches to project1 of tenant-one
5132 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5133 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5134 self.waitUntilSettled()
5135
5136 # one build of project1-test1 must run
5137 # semaphore of tenant-one must be acquired once
5138 # semaphore of tenant-two must not be acquired
5139 self.assertEqual(len(self.builds), 1)
5140 self.assertEqual(self.builds[0].name, 'project1-test1')
5141 self.assertTrue('test-semaphore' in
5142 tenant_one.semaphore_handler.semaphores)
5143 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5144 'test-semaphore', [])), 1)
5145 self.assertFalse('test-semaphore' in
5146 tenant_two.semaphore_handler.semaphores)
5147
5148 # add patches to project2 of tenant-two
5149 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5150 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5151 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5152 self.waitUntilSettled()
5153
5154 # one build of project1-test1 must run
5155 # two builds of project2-test1 must run
5156 # semaphore of tenant-one must be acquired once
5157 # semaphore of tenant-two must be acquired twice
5158 self.assertEqual(len(self.builds), 3)
5159 self.assertEqual(self.builds[0].name, 'project1-test1')
5160 self.assertEqual(self.builds[1].name, 'project2-test1')
5161 self.assertEqual(self.builds[2].name, 'project2-test1')
5162 self.assertTrue('test-semaphore' in
5163 tenant_one.semaphore_handler.semaphores)
5164 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5165 'test-semaphore', [])), 1)
5166 self.assertTrue('test-semaphore' in
5167 tenant_two.semaphore_handler.semaphores)
5168 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5169 'test-semaphore', [])), 2)
5170
5171 self.executor_server.release('project1-test1')
5172 self.waitUntilSettled()
5173
5174 # one build of project1-test1 must run
5175 # two builds of project2-test1 must run
5176 # semaphore of tenant-one must be acquired once
5177 # semaphore of tenant-two must be acquired twice
5178 self.assertEqual(len(self.builds), 3)
5179 self.assertEqual(self.builds[0].name, 'project2-test1')
5180 self.assertEqual(self.builds[1].name, 'project2-test1')
5181 self.assertEqual(self.builds[2].name, 'project1-test1')
5182 self.assertTrue('test-semaphore' in
5183 tenant_one.semaphore_handler.semaphores)
5184 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5185 'test-semaphore', [])), 1)
5186 self.assertTrue('test-semaphore' in
5187 tenant_two.semaphore_handler.semaphores)
5188 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5189 'test-semaphore', [])), 2)
5190
5191 self.executor_server.release('project2-test1')
5192 self.waitUntilSettled()
5193
5194 # one build of project1-test1 must run
5195 # one build of project2-test1 must run
5196 # semaphore of tenant-one must be acquired once
5197 # semaphore of tenant-two must be acquired once
5198 self.assertEqual(len(self.builds), 2)
5199 self.assertTrue('test-semaphore' in
5200 tenant_one.semaphore_handler.semaphores)
5201 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5202 'test-semaphore', [])), 1)
5203 self.assertTrue('test-semaphore' in
5204 tenant_two.semaphore_handler.semaphores)
5205 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5206 'test-semaphore', [])), 1)
5207
5208 self.executor_server.hold_jobs_in_build = False
5209 self.executor_server.release()
5210
5211 self.waitUntilSettled()
5212
5213 # no build must run
5214 # semaphore of tenant-one must not be acquired
5215 # semaphore of tenant-two must not be acquired
5216 self.assertEqual(len(self.builds), 0)
5217 self.assertFalse('test-semaphore' in
5218 tenant_one.semaphore_handler.semaphores)
5219 self.assertFalse('test-semaphore' in
5220 tenant_two.semaphore_handler.semaphores)
5221
5222 self.assertEqual(A.reported, 1)
5223 self.assertEqual(B.reported, 1)
5224
5225
5226class TestSemaphoreInRepo(ZuulTestCase):
5227 tenant_config_file = 'config/in-repo/main.yaml'
5228
5229 def test_semaphore_in_repo(self):
5230 "Test semaphores in repo config"
5231
5232 # This tests dynamic semaphore handling in project repos. The semaphore
5233 # max value should not be evaluated dynamically but must be updated
5234 # after the change lands.
5235
5236 self.waitUntilSettled()
5237 tenant = self.sched.abide.tenants.get('tenant-one')
5238
5239 in_repo_conf = textwrap.dedent(
5240 """
5241 - job:
5242 name: project-test2
5243 semaphore: test-semaphore
5244
5245 - project:
5246 name: org/project
5247 tenant-one-gate:
5248 jobs:
5249 - project-test2
5250
5251 # the max value in dynamic layout must be ignored
5252 - semaphore:
5253 name: test-semaphore
5254 max: 2
5255 """)
5256
5257 in_repo_playbook = textwrap.dedent(
5258 """
5259 - hosts: all
5260 tasks: []
5261 """)
5262
5263 file_dict = {'.zuul.yaml': in_repo_conf,
5264 'playbooks/project-test2.yaml': in_repo_playbook}
5265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5266 files=file_dict)
5267 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5268 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5269 B.setDependsOn(A, 1)
5270 C.setDependsOn(A, 1)
5271
5272 self.executor_server.hold_jobs_in_build = True
5273
5274 A.addApproval('code-review', 2)
5275 B.addApproval('code-review', 2)
5276 C.addApproval('code-review', 2)
5277 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5278 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5279 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5280 self.waitUntilSettled()
5281
5282 # check that the layout in a queue item still has max value of 1
5283 # for test-semaphore
5284 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5285 queue = None
5286 for queue_candidate in pipeline.queues:
5287 if queue_candidate.name == 'org/project':
5288 queue = queue_candidate
5289 break
5290 queue_item = queue.queue[0]
5291 item_dynamic_layout = queue_item.current_build_set.layout
5292 dynamic_test_semaphore = \
5293 item_dynamic_layout.semaphores.get('test-semaphore')
5294 self.assertEqual(dynamic_test_semaphore.max, 1)
5295
5296 # one build must be in queue, one semaphores acquired
5297 self.assertEqual(len(self.builds), 1)
5298 self.assertEqual(self.builds[0].name, 'project-test2')
5299 self.assertTrue('test-semaphore' in
5300 tenant.semaphore_handler.semaphores)
5301 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5302 'test-semaphore', [])), 1)
5303
5304 self.executor_server.release('project-test2')
5305 self.waitUntilSettled()
5306
5307 # change A must be merged
5308 self.assertEqual(A.data['status'], 'MERGED')
5309 self.assertEqual(A.reported, 2)
5310
5311 # send change-merged event as the gerrit mock doesn't send it
5312 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5313 self.waitUntilSettled()
5314
5315 # now that change A was merged, the new semaphore max must be effective
5316 tenant = self.sched.abide.tenants.get('tenant-one')
5317 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5318
5319 # two builds must be in queue, two semaphores acquired
5320 self.assertEqual(len(self.builds), 2)
5321 self.assertEqual(self.builds[0].name, 'project-test2')
5322 self.assertEqual(self.builds[1].name, 'project-test2')
5323 self.assertTrue('test-semaphore' in
5324 tenant.semaphore_handler.semaphores)
5325 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5326 'test-semaphore', [])), 2)
5327
5328 self.executor_server.release('project-test2')
5329 self.waitUntilSettled()
5330
5331 self.assertEqual(len(self.builds), 0)
5332 self.assertFalse('test-semaphore' in
5333 tenant.semaphore_handler.semaphores)
5334
5335 self.executor_server.hold_jobs_in_build = False
5336 self.executor_server.release()
5337
5338 self.waitUntilSettled()
5339 self.assertEqual(len(self.builds), 0)
5340
5341 self.assertEqual(A.reported, 2)
5342 self.assertEqual(B.reported, 2)
5343 self.assertEqual(C.reported, 2)