blob: 2624944b604230f01750d73a4d881aa6d85e0453 [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
Clint Byrum8897dca2017-05-10 19:14:19 -070024import sys
Monty Taylorbc758832013-06-17 17:22:42 -040025import time
Clint Byrum8897dca2017-05-10 19:14:19 -070026from unittest import (skip, skipIf)
Monty Taylorbc758832013-06-17 17:22:42 -040027
James E. Blair4886cc12012-07-18 15:39:41 -070028import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070029from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040030import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070031
Maru Newby3fe5f852015-01-13 04:22:14 +000032import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070033import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080034import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040035import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070036
Maru Newby3fe5f852015-01-13 04:22:14 +000037from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000038 ZuulTestCase,
39 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070040 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000041)
James E. Blairb0fcae42012-07-17 11:12:10 -070042
James E. Blairb0fcae42012-07-17 11:12:10 -070043
Clark Boylanb640e052014-04-03 16:41:46 -070044class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070045 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010046
Paul Belanger174a8272017-03-14 13:20:10 -040047 def test_jobs_executed(self):
48 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070049
James E. Blairb0fcae42012-07-17 11:12:10 -070050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070051 A.addApproval('code-review', 2)
52 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070053 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040054 self.assertEqual(self.getJobFromHistory('project-merge').result,
55 'SUCCESS')
56 self.assertEqual(self.getJobFromHistory('project-test1').result,
57 'SUCCESS')
58 self.assertEqual(self.getJobFromHistory('project-test2').result,
59 'SUCCESS')
60 self.assertEqual(A.data['status'], 'MERGED')
61 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070062 self.assertEqual(self.getJobFromHistory('project-test1').node,
63 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080064 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070065
James E. Blair552b54f2016-07-22 13:55:32 -070066 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070067 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
68 self.assertReportedStat('zuul.pipeline.gate.current_changes',
69 value='1|g')
70 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
71 kind='ms')
72 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
73 value='1|c')
74 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
75 self.assertReportedStat('zuul.pipeline.gate.total_changes',
76 value='1|c')
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.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070079 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070080 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070081
James E. Blair5821bd92015-09-16 08:48:15 -070082 for build in self.builds:
83 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
84
James E. Blair3cb10702013-08-24 08:56:03 -070085 def test_initial_pipeline_gauges(self):
86 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070087 self.assertReportedStat('zuul.pipeline.gate.current_changes',
88 value='0|g')
89 self.assertReportedStat('zuul.pipeline.check.current_changes',
90 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070091
James E. Blair34776ee2016-08-25 13:53:54 -070092 def test_job_branch(self):
93 "Test the correct variant of a job runs on a branch"
94 self.create_branch('org/project', 'stable')
95 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
96 A.addApproval('code-review', 2)
97 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
98 self.waitUntilSettled()
99 self.assertEqual(self.getJobFromHistory('project-test1').result,
100 'SUCCESS')
101 self.assertEqual(self.getJobFromHistory('project-test2').result,
102 'SUCCESS')
103 self.assertEqual(A.data['status'], 'MERGED')
104 self.assertEqual(A.reported, 2,
105 "A should report start and success")
106 self.assertIn('gate', A.messages[1],
107 "A should transit gate")
108 self.assertEqual(self.getJobFromHistory('project-test1').node,
109 'image2')
110
James E. Blairb0fcae42012-07-17 11:12:10 -0700111 def test_parallel_changes(self):
112 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700113
Paul Belanger174a8272017-03-14 13:20:10 -0400114 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
116 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
117 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700118 A.addApproval('code-review', 2)
119 B.addApproval('code-review', 2)
120 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700121
James E. Blair8b5408c2016-08-08 15:37:46 -0700122 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
123 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
124 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700125
126 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400127 self.assertEqual(len(self.builds), 1)
128 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700129 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700130
Paul Belanger174a8272017-03-14 13:20:10 -0400131 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700132 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400133 self.assertEqual(len(self.builds), 3)
134 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700135 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400136 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700137 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400138 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700139 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700140
Paul Belanger174a8272017-03-14 13:20:10 -0400141 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700142 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400143 self.assertEqual(len(self.builds), 5)
144 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700145 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400146 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700147 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700148
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400149 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700150 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400151 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700152 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700153
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400154 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700155 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700156
Paul Belanger174a8272017-03-14 13:20:10 -0400157 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700158 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400159 self.assertEqual(len(self.builds), 6)
160 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700161 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400162 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700164
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400165 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700166 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400167 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700168 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700169
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400170 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700171 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400172 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700173 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700174
Paul Belanger174a8272017-03-14 13:20:10 -0400175 self.executor_server.hold_jobs_in_build = False
176 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700177 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400178 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700179
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400180 self.assertEqual(len(self.history), 9)
181 self.assertEqual(A.data['status'], 'MERGED')
182 self.assertEqual(B.data['status'], 'MERGED')
183 self.assertEqual(C.data['status'], 'MERGED')
184 self.assertEqual(A.reported, 2)
185 self.assertEqual(B.reported, 2)
186 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700187
188 def test_failed_changes(self):
189 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400190 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700191
James E. Blairb02a3bb2012-07-30 17:49:55 -0700192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
193 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700194 A.addApproval('code-review', 2)
195 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700196
Paul Belanger174a8272017-03-14 13:20:10 -0400197 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700198
James E. Blair8b5408c2016-08-08 15:37:46 -0700199 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
200 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700201 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700202 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400203
Paul Belanger174a8272017-03-14 13:20:10 -0400204 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400205 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700206 # A/project-merge is complete
207 self.assertBuilds([
208 dict(name='project-test1', changes='1,1'),
209 dict(name='project-test2', changes='1,1'),
210 dict(name='project-merge', changes='1,1 2,1'),
211 ])
James E. Blaire2819012013-06-28 17:17:26 -0400212
Paul Belanger174a8272017-03-14 13:20:10 -0400213 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400214 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700215 # A/project-merge is complete
216 # B/project-merge is complete
217 self.assertBuilds([
218 dict(name='project-test1', changes='1,1'),
219 dict(name='project-test2', changes='1,1'),
220 dict(name='project-test1', changes='1,1 2,1'),
221 dict(name='project-test2', changes='1,1 2,1'),
222 ])
223
224 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400225 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700226 self.builds[0].release()
227 self.waitUntilSettled()
228
229 self.orderedRelease()
230 self.assertHistory([
231 dict(name='project-merge', result='SUCCESS', changes='1,1'),
232 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
233 dict(name='project-test1', result='FAILURE', changes='1,1'),
234 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
235 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
236 dict(name='project-test2', result='SUCCESS', changes='1,1'),
237 dict(name='project-merge', result='SUCCESS', changes='2,1'),
238 dict(name='project-test1', result='SUCCESS', changes='2,1'),
239 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700240 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700241
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400242 self.assertEqual(A.data['status'], 'NEW')
243 self.assertEqual(B.data['status'], 'MERGED')
244 self.assertEqual(A.reported, 2)
245 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700246
247 def test_independent_queues(self):
248 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700249
Paul Belanger174a8272017-03-14 13:20:10 -0400250 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900251 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700252 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
253 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700254 A.addApproval('code-review', 2)
255 B.addApproval('code-review', 2)
256 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700257
James E. Blair8b5408c2016-08-08 15:37:46 -0700258 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800259 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260
James E. Blairf59f3cf2017-02-19 14:50:26 -0800261 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
262 self.waitUntilSettled()
263
264 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700265 self.waitUntilSettled()
266
267 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700268 self.assertBuilds([
269 dict(name='project-merge', changes='1,1'),
270 dict(name='project-merge', changes='2,1'),
271 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700272
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700273 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700274 self.builds[0].release()
275 self.waitUntilSettled()
276 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700277 self.waitUntilSettled()
278 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400279 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700280 self.waitUntilSettled()
281
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700282 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700283 self.assertBuilds([
284 dict(name='project-test1', changes='1,1'),
285 dict(name='project-test2', changes='1,1'),
286 dict(name='project-test1', changes='2,1'),
287 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500288 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700289 dict(name='project-test1', changes='2,1 3,1'),
290 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800291 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700292 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293
James E. Blair5f0d49e2016-08-19 09:36:50 -0700294 self.orderedRelease()
295 self.assertHistory([
296 dict(name='project-merge', result='SUCCESS', changes='1,1'),
297 dict(name='project-merge', result='SUCCESS', changes='2,1'),
298 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
299 dict(name='project-test1', result='SUCCESS', changes='1,1'),
300 dict(name='project-test2', result='SUCCESS', changes='1,1'),
301 dict(name='project-test1', result='SUCCESS', changes='2,1'),
302 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500303 dict(
304 name='project1-project2-integration',
305 result='SUCCESS',
306 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700307 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
308 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800309 dict(name='project1-project2-integration',
310 result='SUCCESS',
311 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700312 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700313
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400314 self.assertEqual(A.data['status'], 'MERGED')
315 self.assertEqual(B.data['status'], 'MERGED')
316 self.assertEqual(C.data['status'], 'MERGED')
317 self.assertEqual(A.reported, 2)
318 self.assertEqual(B.reported, 2)
319 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700320
321 def test_failed_change_at_head(self):
322 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700323
Paul Belanger174a8272017-03-14 13:20:10 -0400324 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700325 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
326 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
327 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700328 A.addApproval('code-review', 2)
329 B.addApproval('code-review', 2)
330 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700331
Paul Belanger174a8272017-03-14 13:20:10 -0400332 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700333
James E. Blair8b5408c2016-08-08 15:37:46 -0700334 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
335 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
336 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700337
338 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700339
James E. Blairb536ecc2016-08-31 10:11:42 -0700340 self.assertBuilds([
341 dict(name='project-merge', changes='1,1'),
342 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700343
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()
Paul Belanger174a8272017-03-14 13:20:10 -0400348 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700349 self.waitUntilSettled()
350
James E. Blairb536ecc2016-08-31 10:11:42 -0700351 self.assertBuilds([
352 dict(name='project-test1', changes='1,1'),
353 dict(name='project-test2', changes='1,1'),
354 dict(name='project-test1', changes='1,1 2,1'),
355 dict(name='project-test2', changes='1,1 2,1'),
356 dict(name='project-test1', changes='1,1 2,1 3,1'),
357 dict(name='project-test2', changes='1,1 2,1 3,1'),
358 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700359
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400360 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700361 self.waitUntilSettled()
362
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400363 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700364 self.assertBuilds([
365 dict(name='project-test2', changes='1,1'),
366 dict(name='project-merge', changes='2,1'),
367 ])
368 # Unordered history comparison because the aborts can finish
369 # in any order.
370 self.assertHistory([
371 dict(name='project-merge', result='SUCCESS',
372 changes='1,1'),
373 dict(name='project-merge', result='SUCCESS',
374 changes='1,1 2,1'),
375 dict(name='project-merge', result='SUCCESS',
376 changes='1,1 2,1 3,1'),
377 dict(name='project-test1', result='FAILURE',
378 changes='1,1'),
379 dict(name='project-test1', result='ABORTED',
380 changes='1,1 2,1'),
381 dict(name='project-test2', result='ABORTED',
382 changes='1,1 2,1'),
383 dict(name='project-test1', result='ABORTED',
384 changes='1,1 2,1 3,1'),
385 dict(name='project-test2', result='ABORTED',
386 changes='1,1 2,1 3,1'),
387 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700388
Paul Belanger174a8272017-03-14 13:20:10 -0400389 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700390 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400391 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700392 self.waitUntilSettled()
393 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700394
James E. Blairb536ecc2016-08-31 10:11:42 -0700395 self.assertBuilds([])
396 self.assertHistory([
397 dict(name='project-merge', result='SUCCESS',
398 changes='1,1'),
399 dict(name='project-merge', result='SUCCESS',
400 changes='1,1 2,1'),
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1 2,1 3,1'),
403 dict(name='project-test1', result='FAILURE',
404 changes='1,1'),
405 dict(name='project-test1', result='ABORTED',
406 changes='1,1 2,1'),
407 dict(name='project-test2', result='ABORTED',
408 changes='1,1 2,1'),
409 dict(name='project-test1', result='ABORTED',
410 changes='1,1 2,1 3,1'),
411 dict(name='project-test2', result='ABORTED',
412 changes='1,1 2,1 3,1'),
413 dict(name='project-merge', result='SUCCESS',
414 changes='2,1'),
415 dict(name='project-merge', result='SUCCESS',
416 changes='2,1 3,1'),
417 dict(name='project-test2', result='SUCCESS',
418 changes='1,1'),
419 dict(name='project-test1', result='SUCCESS',
420 changes='2,1'),
421 dict(name='project-test2', result='SUCCESS',
422 changes='2,1'),
423 dict(name='project-test1', result='SUCCESS',
424 changes='2,1 3,1'),
425 dict(name='project-test2', result='SUCCESS',
426 changes='2,1 3,1'),
427 ], ordered=False)
428
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400429 self.assertEqual(A.data['status'], 'NEW')
430 self.assertEqual(B.data['status'], 'MERGED')
431 self.assertEqual(C.data['status'], 'MERGED')
432 self.assertEqual(A.reported, 2)
433 self.assertEqual(B.reported, 2)
434 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700435
James E. Blair0aac4872013-08-23 14:02:38 -0700436 def test_failed_change_in_middle(self):
437 "Test a failed change in the middle of the queue"
438
Paul Belanger174a8272017-03-14 13:20:10 -0400439 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
441 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
442 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700443 A.addApproval('code-review', 2)
444 B.addApproval('code-review', 2)
445 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700446
Paul Belanger174a8272017-03-14 13:20:10 -0400447 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700448
James E. Blair8b5408c2016-08-08 15:37:46 -0700449 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
450 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
451 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700452
453 self.waitUntilSettled()
454
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()
Paul Belanger174a8272017-03-14 13:20:10 -0400459 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700460 self.waitUntilSettled()
461
462 self.assertEqual(len(self.builds), 6)
463 self.assertEqual(self.builds[0].name, 'project-test1')
464 self.assertEqual(self.builds[1].name, 'project-test2')
465 self.assertEqual(self.builds[2].name, 'project-test1')
466 self.assertEqual(self.builds[3].name, 'project-test2')
467 self.assertEqual(self.builds[4].name, 'project-test1')
468 self.assertEqual(self.builds[5].name, 'project-test2')
469
470 self.release(self.builds[2])
471 self.waitUntilSettled()
472
James E. Blair972e3c72013-08-29 12:04:55 -0700473 # project-test1 and project-test2 for A
474 # project-test2 for B
475 # project-merge for C (without B)
476 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700477 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
478
Paul Belanger174a8272017-03-14 13:20:10 -0400479 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700480 self.waitUntilSettled()
481
482 # project-test1 and project-test2 for A
483 # project-test2 for B
484 # project-test1 and project-test2 for C
485 self.assertEqual(len(self.builds), 5)
486
Paul Belanger2e2a0492016-10-30 18:33:37 -0400487 tenant = self.sched.abide.tenants.get('tenant-one')
488 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700489 builds = items[0].current_build_set.getBuilds()
490 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
491 self.assertEqual(self.countJobResults(builds, None), 2)
492 builds = items[1].current_build_set.getBuilds()
493 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
494 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
495 self.assertEqual(self.countJobResults(builds, None), 1)
496 builds = items[2].current_build_set.getBuilds()
497 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700498 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700499
Paul Belanger174a8272017-03-14 13:20:10 -0400500 self.executor_server.hold_jobs_in_build = False
501 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700502 self.waitUntilSettled()
503
504 self.assertEqual(len(self.builds), 0)
505 self.assertEqual(len(self.history), 12)
506 self.assertEqual(A.data['status'], 'MERGED')
507 self.assertEqual(B.data['status'], 'NEW')
508 self.assertEqual(C.data['status'], 'MERGED')
509 self.assertEqual(A.reported, 2)
510 self.assertEqual(B.reported, 2)
511 self.assertEqual(C.reported, 2)
512
Clint Byrum8897dca2017-05-10 19:14:19 -0700513 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blaird466dc42012-07-31 10:42:56 -0700514 def test_failed_change_at_head_with_queue(self):
515 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700516
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700517 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
519 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
520 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700521 A.addApproval('code-review', 2)
522 B.addApproval('code-review', 2)
523 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700524
Paul Belanger174a8272017-03-14 13:20:10 -0400525 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700526
James E. Blair8b5408c2016-08-08 15:37:46 -0700527 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
528 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
529 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700530
531 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700532 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400533 self.assertEqual(len(self.builds), 0)
534 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700535 self.assertEqual(queue[0].name, b'executor:execute')
536 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800537 self.assertEqual(job_args['job'], 'project-merge')
538 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700539
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700541 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700543 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700544 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700545 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700546 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700547
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400548 self.assertEqual(len(self.builds), 0)
549 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800550
551 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700552 json.loads(queue[0].arguments.decode('utf8'))['job'],
553 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800554 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700555 json.loads(queue[1].arguments.decode('utf8'))['job'],
556 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800557 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700558 json.loads(queue[2].arguments.decode('utf8'))['job'],
559 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800560 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700561 json.loads(queue[3].arguments.decode('utf8'))['job'],
562 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800563 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700564 json.loads(queue[4].arguments.decode('utf8'))['job'],
565 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800566 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700567 json.loads(queue[5].arguments.decode('utf8'))['job'],
568 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700569
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570 self.release(queue[0])
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)
James E. Blair701c5b42013-06-06 09:34:59 -0700574 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400575 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
576 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700577
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700578 self.gearman_server.hold_jobs_in_queue = False
579 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700580 self.waitUntilSettled()
581
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400582 self.assertEqual(len(self.builds), 0)
583 self.assertEqual(len(self.history), 11)
584 self.assertEqual(A.data['status'], 'NEW')
585 self.assertEqual(B.data['status'], 'MERGED')
586 self.assertEqual(C.data['status'], 'MERGED')
587 self.assertEqual(A.reported, 2)
588 self.assertEqual(B.reported, 2)
589 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700590
James E. Blairec056492016-07-22 09:45:56 -0700591 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700592 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400593 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700595 A.addApproval('code-review', 2)
596 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700597 self.waitUntilSettled()
598 time.sleep(2)
599
600 data = json.loads(self.sched.formatStatusJSON())
601 found_job = None
602 for pipeline in data['pipelines']:
603 if pipeline['name'] != 'gate':
604 continue
605 for queue in pipeline['change_queues']:
606 for head in queue['heads']:
607 for item in head:
608 for job in item['jobs']:
609 if job['name'] == 'project-merge':
610 found_job = job
611 break
612
613 self.assertIsNotNone(found_job)
614 if iteration == 1:
615 self.assertIsNotNone(found_job['estimated_time'])
616 self.assertIsNone(found_job['remaining_time'])
617 else:
618 self.assertIsNotNone(found_job['estimated_time'])
619 self.assertTrue(found_job['estimated_time'] >= 2)
620 self.assertIsNotNone(found_job['remaining_time'])
621
Paul Belanger174a8272017-03-14 13:20:10 -0400622 self.executor_server.hold_jobs_in_build = False
623 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700624 self.waitUntilSettled()
625
James E. Blairec056492016-07-22 09:45:56 -0700626 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700627 def test_time_database(self):
628 "Test the time database"
629
630 self._test_time_database(1)
631 self._test_time_database(2)
632
James E. Blairfef71632013-09-23 11:15:47 -0700633 def test_two_failed_changes_at_head(self):
634 "Test that changes are reparented correctly if 2 fail at head"
635
Paul Belanger174a8272017-03-14 13:20:10 -0400636 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
638 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
639 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700640 A.addApproval('code-review', 2)
641 B.addApproval('code-review', 2)
642 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700643
Paul Belanger174a8272017-03-14 13:20:10 -0400644 self.executor_server.failJob('project-test1', A)
645 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700646
James E. Blair8b5408c2016-08-08 15:37:46 -0700647 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
648 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
649 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700650 self.waitUntilSettled()
651
Paul Belanger174a8272017-03-14 13:20:10 -0400652 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700653 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400654 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700655 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400656 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700657 self.waitUntilSettled()
658
659 self.assertEqual(len(self.builds), 6)
660 self.assertEqual(self.builds[0].name, 'project-test1')
661 self.assertEqual(self.builds[1].name, 'project-test2')
662 self.assertEqual(self.builds[2].name, 'project-test1')
663 self.assertEqual(self.builds[3].name, 'project-test2')
664 self.assertEqual(self.builds[4].name, 'project-test1')
665 self.assertEqual(self.builds[5].name, 'project-test2')
666
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400667 self.assertTrue(self.builds[0].hasChanges(A))
668 self.assertTrue(self.builds[2].hasChanges(A))
669 self.assertTrue(self.builds[2].hasChanges(B))
670 self.assertTrue(self.builds[4].hasChanges(A))
671 self.assertTrue(self.builds[4].hasChanges(B))
672 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700673
674 # Fail change B first
675 self.release(self.builds[2])
676 self.waitUntilSettled()
677
678 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400679 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700680 self.waitUntilSettled()
681
682 self.assertEqual(len(self.builds), 5)
683 self.assertEqual(self.builds[0].name, 'project-test1')
684 self.assertEqual(self.builds[1].name, 'project-test2')
685 self.assertEqual(self.builds[2].name, 'project-test2')
686 self.assertEqual(self.builds[3].name, 'project-test1')
687 self.assertEqual(self.builds[4].name, 'project-test2')
688
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400689 self.assertTrue(self.builds[1].hasChanges(A))
690 self.assertTrue(self.builds[2].hasChanges(A))
691 self.assertTrue(self.builds[2].hasChanges(B))
692 self.assertTrue(self.builds[4].hasChanges(A))
693 self.assertFalse(self.builds[4].hasChanges(B))
694 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700695
696 # Finish running all passing jobs for change A
697 self.release(self.builds[1])
698 self.waitUntilSettled()
699 # Fail and report change A
700 self.release(self.builds[0])
701 self.waitUntilSettled()
702
703 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400704 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700705 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400706 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700707 self.waitUntilSettled()
708
709 self.assertEqual(len(self.builds), 4)
710 self.assertEqual(self.builds[0].name, 'project-test1') # B
711 self.assertEqual(self.builds[1].name, 'project-test2') # B
712 self.assertEqual(self.builds[2].name, 'project-test1') # C
713 self.assertEqual(self.builds[3].name, 'project-test2') # C
714
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400715 self.assertFalse(self.builds[1].hasChanges(A))
716 self.assertTrue(self.builds[1].hasChanges(B))
717 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700718
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400719 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700720 # After A failed and B and C restarted, B should be back in
721 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400722 self.assertTrue(self.builds[2].hasChanges(B))
723 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700724
Paul Belanger174a8272017-03-14 13:20:10 -0400725 self.executor_server.hold_jobs_in_build = False
726 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700727 self.waitUntilSettled()
728
729 self.assertEqual(len(self.builds), 0)
730 self.assertEqual(len(self.history), 21)
731 self.assertEqual(A.data['status'], 'NEW')
732 self.assertEqual(B.data['status'], 'NEW')
733 self.assertEqual(C.data['status'], 'MERGED')
734 self.assertEqual(A.reported, 2)
735 self.assertEqual(B.reported, 2)
736 self.assertEqual(C.reported, 2)
737
James E. Blair8c803f82012-07-31 16:25:42 -0700738 def test_patch_order(self):
739 "Test that dependent patches are tested in the right order"
740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
741 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
742 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700743 A.addApproval('code-review', 2)
744 B.addApproval('code-review', 2)
745 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700746
747 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
748 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
749 M2.setMerged()
750 M1.setMerged()
751
752 # C -> B -> A -> M1 -> M2
753 # M2 is here to make sure it is never queried. If it is, it
754 # means zuul is walking down the entire history of merged
755 # changes.
756
757 C.setDependsOn(B, 1)
758 B.setDependsOn(A, 1)
759 A.setDependsOn(M1, 1)
760 M1.setDependsOn(M2, 1)
761
James E. Blair8b5408c2016-08-08 15:37:46 -0700762 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700763
764 self.waitUntilSettled()
765
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400766 self.assertEqual(A.data['status'], 'NEW')
767 self.assertEqual(B.data['status'], 'NEW')
768 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700769
James E. Blair8b5408c2016-08-08 15:37:46 -0700770 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
771 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700772
773 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400774 self.assertEqual(M2.queried, 0)
775 self.assertEqual(A.data['status'], 'MERGED')
776 self.assertEqual(B.data['status'], 'MERGED')
777 self.assertEqual(C.data['status'], 'MERGED')
778 self.assertEqual(A.reported, 2)
779 self.assertEqual(B.reported, 2)
780 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700781
James E. Blair063672f2015-01-29 13:09:12 -0800782 def test_needed_changes_enqueue(self):
783 "Test that a needed change is enqueued ahead"
784 # A Given a git tree like this, if we enqueue
785 # / \ change C, we should walk up and down the tree
786 # B G and enqueue changes in the order ABCDEFG.
787 # /|\ This is also the order that you would get if
788 # *C E F you enqueued changes in the order ABCDEFG, so
789 # / the ordering is stable across re-enqueue events.
790 # D
791
792 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
793 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
794 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
795 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
796 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
797 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
798 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
799 B.setDependsOn(A, 1)
800 C.setDependsOn(B, 1)
801 D.setDependsOn(C, 1)
802 E.setDependsOn(B, 1)
803 F.setDependsOn(B, 1)
804 G.setDependsOn(A, 1)
805
James E. Blair8b5408c2016-08-08 15:37:46 -0700806 A.addApproval('code-review', 2)
807 B.addApproval('code-review', 2)
808 C.addApproval('code-review', 2)
809 D.addApproval('code-review', 2)
810 E.addApproval('code-review', 2)
811 F.addApproval('code-review', 2)
812 G.addApproval('code-review', 2)
813 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800814
815 self.waitUntilSettled()
816
817 self.assertEqual(A.data['status'], 'NEW')
818 self.assertEqual(B.data['status'], 'NEW')
819 self.assertEqual(C.data['status'], 'NEW')
820 self.assertEqual(D.data['status'], 'NEW')
821 self.assertEqual(E.data['status'], 'NEW')
822 self.assertEqual(F.data['status'], 'NEW')
823 self.assertEqual(G.data['status'], 'NEW')
824
825 # We're about to add approvals to changes without adding the
826 # triggering events to Zuul, so that we can be sure that it is
827 # enqueing the changes based on dependencies, not because of
828 # triggering events. Since it will have the changes cached
829 # already (without approvals), we need to clear the cache
830 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400831 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100832 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800833
Paul Belanger174a8272017-03-14 13:20:10 -0400834 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700835 A.addApproval('approved', 1)
836 B.addApproval('approved', 1)
837 D.addApproval('approved', 1)
838 E.addApproval('approved', 1)
839 F.addApproval('approved', 1)
840 G.addApproval('approved', 1)
841 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800842
843 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400844 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800845 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400846 self.executor_server.hold_jobs_in_build = False
847 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800848 self.waitUntilSettled()
849
850 self.assertEqual(A.data['status'], 'MERGED')
851 self.assertEqual(B.data['status'], 'MERGED')
852 self.assertEqual(C.data['status'], 'MERGED')
853 self.assertEqual(D.data['status'], 'MERGED')
854 self.assertEqual(E.data['status'], 'MERGED')
855 self.assertEqual(F.data['status'], 'MERGED')
856 self.assertEqual(G.data['status'], 'MERGED')
857 self.assertEqual(A.reported, 2)
858 self.assertEqual(B.reported, 2)
859 self.assertEqual(C.reported, 2)
860 self.assertEqual(D.reported, 2)
861 self.assertEqual(E.reported, 2)
862 self.assertEqual(F.reported, 2)
863 self.assertEqual(G.reported, 2)
864 self.assertEqual(self.history[6].changes,
865 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
866
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100867 def test_source_cache(self):
868 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400869 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700870
871 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
872 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
873 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700874 A.addApproval('code-review', 2)
875 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700876
877 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
878 M1.setMerged()
879
880 B.setDependsOn(A, 1)
881 A.setDependsOn(M1, 1)
882
James E. Blair8b5408c2016-08-08 15:37:46 -0700883 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700884 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
885
886 self.waitUntilSettled()
887
888 for build in self.builds:
889 if build.parameters['ZUUL_PIPELINE'] == 'check':
890 build.release()
891 self.waitUntilSettled()
892 for build in self.builds:
893 if build.parameters['ZUUL_PIPELINE'] == 'check':
894 build.release()
895 self.waitUntilSettled()
896
James E. Blair8b5408c2016-08-08 15:37:46 -0700897 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700898 self.waitUntilSettled()
899
Joshua Hesketh352264b2015-08-11 23:42:08 +1000900 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700901 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000902 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700903
Paul Belanger174a8272017-03-14 13:20:10 -0400904 self.executor_server.hold_jobs_in_build = False
905 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700906 self.waitUntilSettled()
907
908 self.assertEqual(A.data['status'], 'MERGED')
909 self.assertEqual(B.data['status'], 'MERGED')
910 self.assertEqual(A.queried, 2) # Initial and isMerged
911 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
912
James E. Blair8c803f82012-07-31 16:25:42 -0700913 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700914 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700915 # TODO: move to test_gerrit (this is a unit test!)
916 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400917 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700918 (trusted, project) = tenant.getProject('org/project')
919 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400920
921 # TODO(pabelanger): As we add more source / trigger APIs we should make
922 # it easier for users to create events for testing.
923 event = zuul.model.TriggerEvent()
924 event.trigger_name = 'gerrit'
925 event.change_number = '1'
926 event.patch_number = '2'
927
928 a = source.getChange(event)
929 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700930 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700931
James E. Blair8b5408c2016-08-08 15:37:46 -0700932 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400933 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700934 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700935
James E. Blair8b5408c2016-08-08 15:37:46 -0700936 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400937 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700938 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700939
Clint Byrum8897dca2017-05-10 19:14:19 -0700940 @skipIf(sys.version_info.major > 2, 'Fails on py3')
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700941 def test_project_merge_conflict(self):
942 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700943
944 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700945 A = self.fake_gerrit.addFakeChange('org/project',
946 'master', 'A',
947 files={'conflict': 'foo'})
948 B = self.fake_gerrit.addFakeChange('org/project',
949 'master', 'B',
950 files={'conflict': 'bar'})
951 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700952 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700953 A.addApproval('code-review', 2)
954 B.addApproval('code-review', 2)
955 C.addApproval('code-review', 2)
956 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
957 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
958 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
960
James E. Blair6736beb2013-07-11 15:18:15 -0700961 self.assertEqual(A.reported, 1)
962 self.assertEqual(B.reported, 1)
963 self.assertEqual(C.reported, 1)
964
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700965 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700966 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700967 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700968 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700969 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700970 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700971
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700972 self.gearman_server.hold_jobs_in_queue = False
973 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700974 self.waitUntilSettled()
975
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400976 self.assertEqual(A.data['status'], 'MERGED')
977 self.assertEqual(B.data['status'], 'NEW')
978 self.assertEqual(C.data['status'], 'MERGED')
979 self.assertEqual(A.reported, 2)
980 self.assertEqual(B.reported, 2)
981 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700982
983 self.assertHistory([
984 dict(name='project-merge', result='SUCCESS', changes='1,1'),
985 dict(name='project-test1', result='SUCCESS', changes='1,1'),
986 dict(name='project-test2', result='SUCCESS', changes='1,1'),
987 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
988 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
989 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
990 ], ordered=False)
991
Clint Byrum8897dca2017-05-10 19:14:19 -0700992 @skipIf(sys.version_info.major > 2, 'Fails on py3')
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700993 def test_delayed_merge_conflict(self):
994 "Test that delayed check merge conflicts are handled properly"
995
996 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -0700997 # the executor sucesfully merges a change based on an old
998 # repo state (frozen by the scheduler) which would otherwise
999 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001000 self.gearman_server.hold_jobs_in_queue = True
1001 A = self.fake_gerrit.addFakeChange('org/project',
1002 'master', 'A',
1003 files={'conflict': 'foo'})
1004 B = self.fake_gerrit.addFakeChange('org/project',
1005 'master', 'B',
1006 files={'conflict': 'bar'})
1007 C = self.fake_gerrit.addFakeChange('org/project',
1008 'master', 'C')
1009 C.setDependsOn(B, 1)
1010
1011 # A enters the gate queue; B and C enter the check queue
1012 A.addApproval('code-review', 2)
1013 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1014 self.waitUntilSettled()
1015 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001016 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001017 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1018 self.waitUntilSettled()
1019
1020 self.assertEqual(A.reported, 1)
1021 self.assertEqual(B.reported, 0) # Check does not report start
1022 self.assertEqual(C.reported, 0) # Check does not report start
1023
1024 # A merges while B and C are queued in check
1025 # Release A project-merge
1026 queue = self.gearman_server.getQueue()
1027 self.release(queue[0])
1028 self.waitUntilSettled()
1029
1030 # Release A project-test*
1031 # gate has higher precedence, so A's test jobs are added in
1032 # front of the merge jobs for B and C
1033 queue = self.gearman_server.getQueue()
1034 self.release(queue[0])
1035 self.release(queue[1])
1036 self.waitUntilSettled()
1037
1038 self.assertEqual(A.data['status'], 'MERGED')
1039 self.assertEqual(B.data['status'], 'NEW')
1040 self.assertEqual(C.data['status'], 'NEW')
1041 self.assertEqual(A.reported, 2)
1042 self.assertEqual(B.reported, 0)
1043 self.assertEqual(C.reported, 0)
1044 self.assertHistory([
1045 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1046 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1047 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1048 ], ordered=False)
1049
1050 # B and C report merge conflicts
1051 # Release B project-merge
1052 queue = self.gearman_server.getQueue()
1053 self.release(queue[0])
1054 self.waitUntilSettled()
1055
1056 # Release C
1057 self.gearman_server.hold_jobs_in_queue = False
1058 self.gearman_server.release()
1059 self.waitUntilSettled()
1060
1061 self.assertEqual(A.data['status'], 'MERGED')
1062 self.assertEqual(B.data['status'], 'NEW')
1063 self.assertEqual(C.data['status'], 'NEW')
1064 self.assertEqual(A.reported, 2)
1065 self.assertEqual(B.reported, 1)
1066 self.assertEqual(C.reported, 1)
1067
1068 self.assertHistory([
1069 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001072 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1073 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1074 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1075 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1076 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1077 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001078 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001079
James E. Blairdaabed22012-08-15 15:38:57 -07001080 def test_post(self):
1081 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001082
Zhongyue Luo5d556072012-09-21 02:00:47 +09001083 e = {
1084 "type": "ref-updated",
1085 "submitter": {
1086 "name": "User Name",
1087 },
1088 "refUpdate": {
1089 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1090 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1091 "refName": "master",
1092 "project": "org/project",
1093 }
1094 }
James E. Blairdaabed22012-08-15 15:38:57 -07001095 self.fake_gerrit.addEvent(e)
1096 self.waitUntilSettled()
1097
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001098 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001099 self.assertEqual(len(self.history), 1)
1100 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001101
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001102 def test_post_ignore_deletes(self):
1103 "Test that deleting refs does not trigger post jobs"
1104
1105 e = {
1106 "type": "ref-updated",
1107 "submitter": {
1108 "name": "User Name",
1109 },
1110 "refUpdate": {
1111 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1112 "newRev": "0000000000000000000000000000000000000000",
1113 "refName": "master",
1114 "project": "org/project",
1115 }
1116 }
1117 self.fake_gerrit.addEvent(e)
1118 self.waitUntilSettled()
1119
1120 job_names = [x.name for x in self.history]
1121 self.assertEqual(len(self.history), 0)
1122 self.assertNotIn('project-post', job_names)
1123
James E. Blaircc324192017-04-19 14:36:08 -07001124 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001125 def test_post_ignore_deletes_negative(self):
1126 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001127 e = {
1128 "type": "ref-updated",
1129 "submitter": {
1130 "name": "User Name",
1131 },
1132 "refUpdate": {
1133 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1134 "newRev": "0000000000000000000000000000000000000000",
1135 "refName": "master",
1136 "project": "org/project",
1137 }
1138 }
1139 self.fake_gerrit.addEvent(e)
1140 self.waitUntilSettled()
1141
1142 job_names = [x.name for x in self.history]
1143 self.assertEqual(len(self.history), 1)
1144 self.assertIn('project-post', job_names)
1145
James E. Blairec056492016-07-22 09:45:56 -07001146 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001147 def test_build_configuration_branch_interaction(self):
1148 "Test that switching between branches works"
1149 self.test_build_configuration()
1150 self.test_build_configuration_branch()
1151 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001152 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001153 repo = git.Repo(path)
1154 repo.heads.master.commit = repo.commit('init')
1155 self.test_build_configuration()
1156
James E. Blaircaec0c52012-08-22 14:52:22 -07001157 def test_dependent_changes_dequeue(self):
1158 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001159
James E. Blaircaec0c52012-08-22 14:52:22 -07001160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1161 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1162 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001163 A.addApproval('code-review', 2)
1164 B.addApproval('code-review', 2)
1165 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001166
1167 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1168 M1.setMerged()
1169
1170 # C -> B -> A -> M1
1171
1172 C.setDependsOn(B, 1)
1173 B.setDependsOn(A, 1)
1174 A.setDependsOn(M1, 1)
1175
Paul Belanger174a8272017-03-14 13:20:10 -04001176 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001177
James E. Blair8b5408c2016-08-08 15:37:46 -07001178 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1179 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1180 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001181
1182 self.waitUntilSettled()
1183
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001184 self.assertEqual(A.data['status'], 'NEW')
1185 self.assertEqual(A.reported, 2)
1186 self.assertEqual(B.data['status'], 'NEW')
1187 self.assertEqual(B.reported, 2)
1188 self.assertEqual(C.data['status'], 'NEW')
1189 self.assertEqual(C.reported, 2)
1190 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001191
James E. Blair972e3c72013-08-29 12:04:55 -07001192 def test_failing_dependent_changes(self):
1193 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001194 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1197 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1198 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1199 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001200 A.addApproval('code-review', 2)
1201 B.addApproval('code-review', 2)
1202 C.addApproval('code-review', 2)
1203 D.addApproval('code-review', 2)
1204 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001205
1206 # E, D -> C -> B, A
1207
1208 D.setDependsOn(C, 1)
1209 C.setDependsOn(B, 1)
1210
Paul Belanger174a8272017-03-14 13:20:10 -04001211 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001212
James E. Blair8b5408c2016-08-08 15:37:46 -07001213 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1214 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1215 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1216 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1217 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001218
1219 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001220 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001221 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001222 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001223 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001224 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001225 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001226 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001227 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001228 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001229 self.waitUntilSettled()
1230
Paul Belanger174a8272017-03-14 13:20:10 -04001231 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001232 for build in self.builds:
1233 if build.parameters['ZUUL_CHANGE'] != '1':
1234 build.release()
1235 self.waitUntilSettled()
1236
Paul Belanger174a8272017-03-14 13:20:10 -04001237 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001238 self.waitUntilSettled()
1239
1240 self.assertEqual(A.data['status'], 'MERGED')
1241 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001242 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001243 self.assertEqual(B.data['status'], 'NEW')
1244 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001245 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001246 self.assertEqual(C.data['status'], 'NEW')
1247 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001248 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001249 self.assertEqual(D.data['status'], 'NEW')
1250 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001251 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001252 self.assertEqual(E.data['status'], 'MERGED')
1253 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001254 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001255 self.assertEqual(len(self.history), 18)
1256
James E. Blairec590122012-08-22 15:19:31 -07001257 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001258 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001259 # If it's dequeued more than once, we should see extra
1260 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001261
Paul Belanger174a8272017-03-14 13:20:10 -04001262 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001263 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1264 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1265 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001266 A.addApproval('code-review', 2)
1267 B.addApproval('code-review', 2)
1268 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001269
Paul Belanger174a8272017-03-14 13:20:10 -04001270 self.executor_server.failJob('project-test1', A)
1271 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001272
James E. Blair8b5408c2016-08-08 15:37:46 -07001273 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1274 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1275 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001276
1277 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001278
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001279 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001280 self.assertEqual(self.builds[0].name, 'project-merge')
1281 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001282
Paul Belanger174a8272017-03-14 13:20:10 -04001283 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001285 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001288 self.waitUntilSettled()
1289
James E. Blair6df67132017-04-19 13:53:20 -07001290 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001291 self.assertEqual(self.builds[0].name, 'project-test1')
1292 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001293 self.assertEqual(self.builds[2].name, 'project-test1')
1294 self.assertEqual(self.builds[3].name, 'project-test2')
1295 self.assertEqual(self.builds[4].name, 'project-test1')
1296 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001297
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001298 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001299 self.waitUntilSettled()
1300
James E. Blair6df67132017-04-19 13:53:20 -07001301 self.assertEqual(len(self.builds), 2) # test2, merge for B
1302 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001303
Paul Belanger174a8272017-03-14 13:20:10 -04001304 self.executor_server.hold_jobs_in_build = False
1305 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001306 self.waitUntilSettled()
1307
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001308 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001309 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001310
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001311 self.assertEqual(A.data['status'], 'NEW')
1312 self.assertEqual(B.data['status'], 'MERGED')
1313 self.assertEqual(C.data['status'], 'MERGED')
1314 self.assertEqual(A.reported, 2)
1315 self.assertEqual(B.reported, 2)
1316 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001317
James E. Blaircc020532017-04-19 13:04:44 -07001318 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001319 def test_nonvoting_job(self):
1320 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001321
James E. Blair4ec821f2012-08-23 15:28:28 -07001322 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1323 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001324 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001325 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001327
1328 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001329
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001330 self.assertEqual(A.data['status'], 'MERGED')
1331 self.assertEqual(A.reported, 2)
1332 self.assertEqual(
1333 self.getJobFromHistory('nonvoting-project-merge').result,
1334 'SUCCESS')
1335 self.assertEqual(
1336 self.getJobFromHistory('nonvoting-project-test1').result,
1337 'SUCCESS')
1338 self.assertEqual(
1339 self.getJobFromHistory('nonvoting-project-test2').result,
1340 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001341
James E. Blair5821bd92015-09-16 08:48:15 -07001342 for build in self.builds:
1343 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1344
James E. Blaire0487072012-08-29 17:38:31 -07001345 def test_check_queue_success(self):
1346 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001347
James E. Blaire0487072012-08-29 17:38:31 -07001348 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1349 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1350
1351 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001352
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertEqual(A.data['status'], 'NEW')
1354 self.assertEqual(A.reported, 1)
1355 self.assertEqual(self.getJobFromHistory('project-merge').result,
1356 'SUCCESS')
1357 self.assertEqual(self.getJobFromHistory('project-test1').result,
1358 'SUCCESS')
1359 self.assertEqual(self.getJobFromHistory('project-test2').result,
1360 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001361
1362 def test_check_queue_failure(self):
1363 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001364
James E. Blaire0487072012-08-29 17:38:31 -07001365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001366 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001367 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1368
1369 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001370
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001371 self.assertEqual(A.data['status'], 'NEW')
1372 self.assertEqual(A.reported, 1)
1373 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001374 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001375 self.assertEqual(self.getJobFromHistory('project-test1').result,
1376 'SUCCESS')
1377 self.assertEqual(self.getJobFromHistory('project-test2').result,
1378 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001379
James E. Blair7cb84542017-04-19 13:35:05 -07001380 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001381 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001382 # This particular test does a large amount of merges and needs a little
1383 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001384 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001385 "test that dependent changes behind dequeued changes work"
1386 # This complicated test is a reproduction of a real life bug
1387 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001388
Paul Belanger174a8272017-03-14 13:20:10 -04001389 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001390 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1391 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1392 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1393 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1394 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1395 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1396 D.setDependsOn(C, 1)
1397 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001398 A.addApproval('code-review', 2)
1399 B.addApproval('code-review', 2)
1400 C.addApproval('code-review', 2)
1401 D.addApproval('code-review', 2)
1402 E.addApproval('code-review', 2)
1403 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001404
1405 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001406
1407 # Change object re-use in the gerrit trigger is hidden if
1408 # changes are added in quick succession; waiting makes it more
1409 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001410 self.fake_gerrit.addEvent(A.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(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001413 self.waitUntilSettled()
1414
Paul Belanger174a8272017-03-14 13:20:10 -04001415 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001416 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001417 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001418 self.waitUntilSettled()
1419
James E. Blair8b5408c2016-08-08 15:37:46 -07001420 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001422 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001423 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001424 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001425 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001426 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001427 self.waitUntilSettled()
1428
Paul Belanger174a8272017-03-14 13:20:10 -04001429 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001430 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001431 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001432 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001433 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001434 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001435 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001436 self.waitUntilSettled()
1437
1438 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001439
1440 # Grab pointers to the jobs we want to release before
1441 # releasing any, because list indexes may change as
1442 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001443 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001444 a.release()
1445 b.release()
1446 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001447 self.waitUntilSettled()
1448
Paul Belanger174a8272017-03-14 13:20:10 -04001449 self.executor_server.hold_jobs_in_build = False
1450 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001451 self.waitUntilSettled()
1452
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001453 self.assertEqual(A.data['status'], 'NEW')
1454 self.assertEqual(B.data['status'], 'MERGED')
1455 self.assertEqual(C.data['status'], 'MERGED')
1456 self.assertEqual(D.data['status'], 'MERGED')
1457 self.assertEqual(E.data['status'], 'MERGED')
1458 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001459
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001460 self.assertEqual(A.reported, 2)
1461 self.assertEqual(B.reported, 2)
1462 self.assertEqual(C.reported, 2)
1463 self.assertEqual(D.reported, 2)
1464 self.assertEqual(E.reported, 2)
1465 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001466
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001467 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1468 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001469
1470 def test_merger_repack(self):
1471 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001472
James E. Blair05fed602012-09-07 12:45:24 -07001473 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001474 A.addApproval('code-review', 2)
1475 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001476 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001477 self.assertEqual(self.getJobFromHistory('project-merge').result,
1478 'SUCCESS')
1479 self.assertEqual(self.getJobFromHistory('project-test1').result,
1480 'SUCCESS')
1481 self.assertEqual(self.getJobFromHistory('project-test2').result,
1482 'SUCCESS')
1483 self.assertEqual(A.data['status'], 'MERGED')
1484 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001485 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001486 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001487
James E. Blair2a535672017-04-27 12:03:15 -07001488 path = os.path.join(self.merger_src_root, "review.example.com",
1489 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001490 if os.path.exists(path):
1491 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001492 path = os.path.join(self.executor_src_root, "review.example.com",
1493 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001494 if os.path.exists(path):
1495 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001496
1497 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001498 A.addApproval('code-review', 2)
1499 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001500 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001501 self.assertEqual(self.getJobFromHistory('project-merge').result,
1502 'SUCCESS')
1503 self.assertEqual(self.getJobFromHistory('project-test1').result,
1504 'SUCCESS')
1505 self.assertEqual(self.getJobFromHistory('project-test2').result,
1506 'SUCCESS')
1507 self.assertEqual(A.data['status'], 'MERGED')
1508 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001509
James E. Blair4886f282012-11-15 09:27:33 -08001510 def test_merger_repack_large_change(self):
1511 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001512 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001513 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001514 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001515 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001516 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001517 self.executor_server.merger._addProject('review.example.com',
1518 'org/project', url)
James E. Blair6df67132017-04-19 13:53:20 -07001519 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001520 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001521 # TODOv3(jeblair): add hostname to upstream root
1522 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001523 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001524 path = os.path.join(self.merger_src_root, 'review.example.com',
1525 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001526 if os.path.exists(path):
1527 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001528 path = os.path.join(self.executor_src_root, 'review.example.com',
1529 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001530 if os.path.exists(path):
1531 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001532
James E. Blair8b5408c2016-08-08 15:37:46 -07001533 A.addApproval('code-review', 2)
1534 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001535 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001536 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001537 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001538 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001539 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001540 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001541 'SUCCESS')
1542 self.assertEqual(A.data['status'], 'MERGED')
1543 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001544
James E. Blair2fa50962013-01-30 21:50:41 -08001545 def test_new_patchset_dequeues_old(self):
1546 "Test that a new patchset causes the old to be dequeued"
1547 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001548 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001549 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1550 M.setMerged()
1551
1552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1553 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1554 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1555 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001556 A.addApproval('code-review', 2)
1557 B.addApproval('code-review', 2)
1558 C.addApproval('code-review', 2)
1559 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001560
1561 C.setDependsOn(B, 1)
1562 B.setDependsOn(A, 1)
1563 A.setDependsOn(M, 1)
1564
James E. Blair8b5408c2016-08-08 15:37:46 -07001565 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1566 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1567 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1568 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001569 self.waitUntilSettled()
1570
1571 B.addPatchset()
1572 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1573 self.waitUntilSettled()
1574
Paul Belanger174a8272017-03-14 13:20:10 -04001575 self.executor_server.hold_jobs_in_build = False
1576 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001577 self.waitUntilSettled()
1578
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001579 self.assertEqual(A.data['status'], 'MERGED')
1580 self.assertEqual(A.reported, 2)
1581 self.assertEqual(B.data['status'], 'NEW')
1582 self.assertEqual(B.reported, 2)
1583 self.assertEqual(C.data['status'], 'NEW')
1584 self.assertEqual(C.reported, 2)
1585 self.assertEqual(D.data['status'], 'MERGED')
1586 self.assertEqual(D.reported, 2)
1587 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001588
James E. Blairba437362015-02-07 11:41:52 -08001589 def test_new_patchset_check(self):
1590 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001591
Paul Belanger174a8272017-03-14 13:20:10 -04001592 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001593
1594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001595 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001596 tenant = self.sched.abide.tenants.get('tenant-one')
1597 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001598
1599 # Add two git-dependent changes
1600 B.setDependsOn(A, 1)
1601 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1602 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001603 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1604 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001605
1606 # A live item, and a non-live/live pair
1607 items = check_pipeline.getAllItems()
1608 self.assertEqual(len(items), 3)
1609
1610 self.assertEqual(items[0].change.number, '1')
1611 self.assertEqual(items[0].change.patchset, '1')
1612 self.assertFalse(items[0].live)
1613
1614 self.assertEqual(items[1].change.number, '2')
1615 self.assertEqual(items[1].change.patchset, '1')
1616 self.assertTrue(items[1].live)
1617
1618 self.assertEqual(items[2].change.number, '1')
1619 self.assertEqual(items[2].change.patchset, '1')
1620 self.assertTrue(items[2].live)
1621
1622 # Add a new patchset to A
1623 A.addPatchset()
1624 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1625 self.waitUntilSettled()
1626
1627 # The live copy of A,1 should be gone, but the non-live and B
1628 # should continue, and we should have a new A,2
1629 items = check_pipeline.getAllItems()
1630 self.assertEqual(len(items), 3)
1631
1632 self.assertEqual(items[0].change.number, '1')
1633 self.assertEqual(items[0].change.patchset, '1')
1634 self.assertFalse(items[0].live)
1635
1636 self.assertEqual(items[1].change.number, '2')
1637 self.assertEqual(items[1].change.patchset, '1')
1638 self.assertTrue(items[1].live)
1639
1640 self.assertEqual(items[2].change.number, '1')
1641 self.assertEqual(items[2].change.patchset, '2')
1642 self.assertTrue(items[2].live)
1643
1644 # Add a new patchset to B
1645 B.addPatchset()
1646 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1647 self.waitUntilSettled()
1648
1649 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1650 # but we should have a new B,2 (still based on A,1)
1651 items = check_pipeline.getAllItems()
1652 self.assertEqual(len(items), 3)
1653
1654 self.assertEqual(items[0].change.number, '1')
1655 self.assertEqual(items[0].change.patchset, '2')
1656 self.assertTrue(items[0].live)
1657
1658 self.assertEqual(items[1].change.number, '1')
1659 self.assertEqual(items[1].change.patchset, '1')
1660 self.assertFalse(items[1].live)
1661
1662 self.assertEqual(items[2].change.number, '2')
1663 self.assertEqual(items[2].change.patchset, '2')
1664 self.assertTrue(items[2].live)
1665
1666 self.builds[0].release()
1667 self.waitUntilSettled()
1668 self.builds[0].release()
1669 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001670 self.executor_server.hold_jobs_in_build = False
1671 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001672 self.waitUntilSettled()
1673
1674 self.assertEqual(A.reported, 1)
1675 self.assertEqual(B.reported, 1)
1676 self.assertEqual(self.history[0].result, 'ABORTED')
1677 self.assertEqual(self.history[0].changes, '1,1')
1678 self.assertEqual(self.history[1].result, 'ABORTED')
1679 self.assertEqual(self.history[1].changes, '1,1 2,1')
1680 self.assertEqual(self.history[2].result, 'SUCCESS')
1681 self.assertEqual(self.history[2].changes, '1,2')
1682 self.assertEqual(self.history[3].result, 'SUCCESS')
1683 self.assertEqual(self.history[3].changes, '1,1 2,2')
1684
1685 def test_abandoned_gate(self):
1686 "Test that an abandoned change is dequeued from gate"
1687
Paul Belanger174a8272017-03-14 13:20:10 -04001688 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001689
1690 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001691 A.addApproval('code-review', 2)
1692 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001693 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001694 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1695 self.assertEqual(self.builds[0].name, 'project-merge')
1696
1697 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1698 self.waitUntilSettled()
1699
Paul Belanger174a8272017-03-14 13:20:10 -04001700 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001701 self.waitUntilSettled()
1702
Arie5b4048c2016-10-05 16:12:06 +03001703 self.assertBuilds([])
1704 self.assertHistory([
1705 dict(name='project-merge', result='ABORTED', changes='1,1')],
1706 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001707 self.assertEqual(A.reported, 1,
1708 "Abandoned gate change should report only start")
1709
1710 def test_abandoned_check(self):
1711 "Test that an abandoned change is dequeued from check"
1712
Paul Belanger174a8272017-03-14 13:20:10 -04001713 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001714
1715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1716 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001717 tenant = self.sched.abide.tenants.get('tenant-one')
1718 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001719
1720 # Add two git-dependent changes
1721 B.setDependsOn(A, 1)
1722 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1723 self.waitUntilSettled()
1724 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1725 self.waitUntilSettled()
1726 # A live item, and a non-live/live pair
1727 items = check_pipeline.getAllItems()
1728 self.assertEqual(len(items), 3)
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
1736 self.assertEqual(items[2].change.number, '1')
1737 self.assertTrue(items[2].live)
1738
1739 # Abandon A
1740 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1741 self.waitUntilSettled()
1742
1743 # The live copy of A should be gone, but the non-live and B
1744 # should continue
1745 items = check_pipeline.getAllItems()
1746 self.assertEqual(len(items), 2)
1747
1748 self.assertEqual(items[0].change.number, '1')
1749 self.assertFalse(items[0].live)
1750
1751 self.assertEqual(items[1].change.number, '2')
1752 self.assertTrue(items[1].live)
1753
Paul Belanger174a8272017-03-14 13:20:10 -04001754 self.executor_server.hold_jobs_in_build = False
1755 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001756 self.waitUntilSettled()
1757
1758 self.assertEqual(len(self.history), 4)
1759 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001760 'Build should have been aborted')
1761 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001762 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001763
Steve Varnau7b78b312015-04-03 14:49:46 -07001764 def test_abandoned_not_timer(self):
1765 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001766 # This test can not use simple_layout because it must start
1767 # with a configuration which does not include a
1768 # timer-triggered job so that we have an opportunity to set
1769 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001770 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001771 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001772 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001773 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001774 # The pipeline triggers every second, so we should have seen
1775 # several by now.
1776 time.sleep(5)
1777 self.waitUntilSettled()
1778 # Stop queuing timer triggered jobs so that the assertions
1779 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001780 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001781 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001782 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001783
Steve Varnau7b78b312015-04-03 14:49:46 -07001784 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1785
1786 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1788 self.waitUntilSettled()
1789 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1790
1791 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1792 self.waitUntilSettled()
1793
1794 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1795
Paul Belanger174a8272017-03-14 13:20:10 -04001796 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001797 self.waitUntilSettled()
1798
Arx Cruzb1b010d2013-10-28 19:49:59 -02001799 def test_zuul_url_return(self):
1800 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001801 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001802 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001803
1804 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001805 A.addApproval('code-review', 2)
1806 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001807 self.waitUntilSettled()
1808
1809 self.assertEqual(len(self.builds), 1)
1810 for build in self.builds:
1811 self.assertTrue('ZUUL_URL' in build.parameters)
1812
Paul Belanger174a8272017-03-14 13:20:10 -04001813 self.executor_server.hold_jobs_in_build = False
1814 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001815 self.waitUntilSettled()
1816
James E. Blair2fa50962013-01-30 21:50:41 -08001817 def test_new_patchset_dequeues_old_on_head(self):
1818 "Test that a new patchset causes the old to be dequeued (at head)"
1819 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001820 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001821 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1822 M.setMerged()
1823 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1824 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1825 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1826 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001827 A.addApproval('code-review', 2)
1828 B.addApproval('code-review', 2)
1829 C.addApproval('code-review', 2)
1830 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001831
1832 C.setDependsOn(B, 1)
1833 B.setDependsOn(A, 1)
1834 A.setDependsOn(M, 1)
1835
James E. Blair8b5408c2016-08-08 15:37:46 -07001836 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1837 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1838 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1839 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001840 self.waitUntilSettled()
1841
1842 A.addPatchset()
1843 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1844 self.waitUntilSettled()
1845
Paul Belanger174a8272017-03-14 13:20:10 -04001846 self.executor_server.hold_jobs_in_build = False
1847 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001848 self.waitUntilSettled()
1849
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001850 self.assertEqual(A.data['status'], 'NEW')
1851 self.assertEqual(A.reported, 2)
1852 self.assertEqual(B.data['status'], 'NEW')
1853 self.assertEqual(B.reported, 2)
1854 self.assertEqual(C.data['status'], 'NEW')
1855 self.assertEqual(C.reported, 2)
1856 self.assertEqual(D.data['status'], 'MERGED')
1857 self.assertEqual(D.reported, 2)
1858 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001859
1860 def test_new_patchset_dequeues_old_without_dependents(self):
1861 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001862 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1865 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001866 A.addApproval('code-review', 2)
1867 B.addApproval('code-review', 2)
1868 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001869
James E. Blair8b5408c2016-08-08 15:37:46 -07001870 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1871 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1872 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001873 self.waitUntilSettled()
1874
1875 B.addPatchset()
1876 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1877 self.waitUntilSettled()
1878
Paul Belanger174a8272017-03-14 13:20:10 -04001879 self.executor_server.hold_jobs_in_build = False
1880 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001881 self.waitUntilSettled()
1882
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001883 self.assertEqual(A.data['status'], 'MERGED')
1884 self.assertEqual(A.reported, 2)
1885 self.assertEqual(B.data['status'], 'NEW')
1886 self.assertEqual(B.reported, 2)
1887 self.assertEqual(C.data['status'], 'MERGED')
1888 self.assertEqual(C.reported, 2)
1889 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001890
1891 def test_new_patchset_dequeues_old_independent_queue(self):
1892 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001893 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001894 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1895 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1896 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1897 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1898 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1899 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1900 self.waitUntilSettled()
1901
1902 B.addPatchset()
1903 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1904 self.waitUntilSettled()
1905
Paul Belanger174a8272017-03-14 13:20:10 -04001906 self.executor_server.hold_jobs_in_build = False
1907 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001908 self.waitUntilSettled()
1909
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001910 self.assertEqual(A.data['status'], 'NEW')
1911 self.assertEqual(A.reported, 1)
1912 self.assertEqual(B.data['status'], 'NEW')
1913 self.assertEqual(B.reported, 1)
1914 self.assertEqual(C.data['status'], 'NEW')
1915 self.assertEqual(C.reported, 1)
1916 self.assertEqual(len(self.history), 10)
1917 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001918
James E. Blaircc020532017-04-19 13:04:44 -07001919 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001920 def test_noop_job(self):
1921 "Test that the internal noop job works"
1922 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001923 A.addApproval('code-review', 2)
1924 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001925 self.waitUntilSettled()
1926
1927 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1928 self.assertTrue(self.sched._areAllBuildsComplete())
1929 self.assertEqual(len(self.history), 0)
1930 self.assertEqual(A.data['status'], 'MERGED')
1931 self.assertEqual(A.reported, 2)
1932
James E. Blair06cc3922017-04-19 10:08:10 -07001933 @simple_layout('layouts/no-jobs-project.yaml')
Clint Byrum8897dca2017-05-10 19:14:19 -07001934 @skipIf(sys.version_info.major > 2, 'Fails on py3')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001935 def test_no_job_project(self):
1936 "Test that reports with no jobs don't get sent"
1937 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1938 'master', 'A')
1939 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1940 self.waitUntilSettled()
1941
1942 # Change wasn't reported to
1943 self.assertEqual(A.reported, False)
1944
1945 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001946 tenant = self.sched.abide.tenants.get('tenant-one')
1947 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001948 items = check_pipeline.getAllItems()
1949 self.assertEqual(len(items), 0)
1950
1951 self.assertEqual(len(self.history), 0)
1952
James E. Blair7d0dedc2013-02-21 17:26:09 -08001953 def test_zuul_refs(self):
1954 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001955 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001956 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1957 M1.setMerged()
1958 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1959 M2.setMerged()
1960
1961 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1962 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1963 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1964 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001965 A.addApproval('code-review', 2)
1966 B.addApproval('code-review', 2)
1967 C.addApproval('code-review', 2)
1968 D.addApproval('code-review', 2)
1969 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1970 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1971 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1972 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001973
1974 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001975 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001976 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001977 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001978 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001979 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001980 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001981 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001982 self.waitUntilSettled()
1983
James E. Blair7d0dedc2013-02-21 17:26:09 -08001984 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001985 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001986 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001987 if x.parameters['ZUUL_CHANGE'] == '3':
1988 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001989 a_build = x
1990 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001991 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001992 b_build = x
1993 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001994 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001995 c_build = x
1996 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001997 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001998 d_build = x
1999 if a_build and b_build and c_build and d_build:
2000 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002001
2002 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002003 self.assertIsNotNone(a_zref)
2004 self.assertIsNotNone(b_zref)
2005 self.assertIsNotNone(c_zref)
2006 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002007
2008 # And they should all be different
2009 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002010 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002011
Clint Byrum3343e3e2016-11-15 16:05:03 -08002012 # should have a, not b, and should not be in project2
2013 self.assertTrue(a_build.hasChanges(A))
2014 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002015
Clint Byrum3343e3e2016-11-15 16:05:03 -08002016 # should have a and b, and should not be in project2
2017 self.assertTrue(b_build.hasChanges(A, B))
2018 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002019
Clint Byrum3343e3e2016-11-15 16:05:03 -08002020 # should have a and b in 1, c in 2
2021 self.assertTrue(c_build.hasChanges(A, B, C))
2022 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002023
Clint Byrum3343e3e2016-11-15 16:05:03 -08002024 # should have a and b in 1, c and d in 2
2025 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002026
Paul Belanger174a8272017-03-14 13:20:10 -04002027 self.executor_server.hold_jobs_in_build = False
2028 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002029 self.waitUntilSettled()
2030
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002031 self.assertEqual(A.data['status'], 'MERGED')
2032 self.assertEqual(A.reported, 2)
2033 self.assertEqual(B.data['status'], 'MERGED')
2034 self.assertEqual(B.reported, 2)
2035 self.assertEqual(C.data['status'], 'MERGED')
2036 self.assertEqual(C.reported, 2)
2037 self.assertEqual(D.data['status'], 'MERGED')
2038 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002039
James E. Blair4a28a882013-08-23 15:17:33 -07002040 def test_rerun_on_error(self):
2041 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002042 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002043 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002044 A.addApproval('code-review', 2)
2045 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002046 self.waitUntilSettled()
2047
James E. Blair412fba82017-01-26 15:00:50 -08002048 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002049 self.executor_server.hold_jobs_in_build = False
2050 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002051 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002052 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002053 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2054
James E. Blair412e5582013-04-22 15:50:12 -07002055 def test_statsd(self):
2056 "Test each of the statsd methods used in the scheduler"
2057 import extras
2058 statsd = extras.try_import('statsd.statsd')
2059 statsd.incr('test-incr')
2060 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002061 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002062 self.assertReportedStat('test-incr', '1|c')
2063 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002064 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002065
Clint Byrum8897dca2017-05-10 19:14:19 -07002066 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blairdad52252014-02-07 16:59:17 -08002067 def test_stuck_job_cleanup(self):
2068 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002069
2070 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002071 self.gearman_server.hold_jobs_in_queue = True
2072 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002073 A.addApproval('code-review', 2)
2074 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002075 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002076 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002077 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2078
James E. Blair31cb3e82017-04-20 13:08:56 -07002079 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002080 self.sched.reconfigure(self.config)
2081 self.waitUntilSettled()
2082
James E. Blair18c64442014-03-18 10:14:45 -07002083 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002084 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002085 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002086 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2087 self.assertTrue(self.sched._areAllBuildsComplete())
2088
2089 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002090 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002091 self.assertEqual(self.history[0].result, 'SUCCESS')
2092
James E. Blair879dafb2015-07-17 14:04:49 -07002093 def test_file_head(self):
2094 # This is a regression test for an observed bug. A change
2095 # with a file named "HEAD" in the root directory of the repo
2096 # was processed by a merger. It then was unable to reset the
2097 # repo because of:
2098 # GitCommandError: 'git reset --hard HEAD' returned
2099 # with exit code 128
2100 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2101 # and filename
2102 # Use '--' to separate filenames from revisions'
2103
2104 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002105 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002106 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2107
2108 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2109 self.waitUntilSettled()
2110
2111 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2112 self.waitUntilSettled()
2113
2114 self.assertIn('Build succeeded', A.messages[0])
2115 self.assertIn('Build succeeded', B.messages[0])
2116
James E. Blair70c71582013-03-06 08:50:50 -08002117 def test_file_jobs(self):
2118 "Test that file jobs run only when appropriate"
2119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002120 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002121 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002122 A.addApproval('code-review', 2)
2123 B.addApproval('code-review', 2)
2124 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2125 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002126 self.waitUntilSettled()
2127
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002128 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002129 if x.name == 'project-testfile']
2130
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002131 self.assertEqual(len(testfile_jobs), 1)
2132 self.assertEqual(testfile_jobs[0].changes, '1,2')
2133 self.assertEqual(A.data['status'], 'MERGED')
2134 self.assertEqual(A.reported, 2)
2135 self.assertEqual(B.data['status'], 'MERGED')
2136 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002137
Clint Byrum3ee39f32016-11-17 23:45:07 -08002138 def _test_irrelevant_files_jobs(self, should_skip):
2139 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002140 if should_skip:
2141 files = {'ignoreme': 'ignored\n'}
2142 else:
2143 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002144
2145 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002146 'master',
2147 'test irrelevant-files',
2148 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002149 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2150 self.waitUntilSettled()
2151
2152 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002153 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002154
2155 if should_skip:
2156 self.assertEqual([], tested_change_ids)
2157 else:
2158 self.assertIn(change.data['number'], tested_change_ids)
2159
James E. Blaircc324192017-04-19 14:36:08 -07002160 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002161 def test_irrelevant_files_match_skips_job(self):
2162 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002163
James E. Blaircc324192017-04-19 14:36:08 -07002164 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002165 def test_irrelevant_files_no_match_runs_job(self):
2166 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002167
James E. Blaircc324192017-04-19 14:36:08 -07002168 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002169 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002170 files = {'ignoreme': 'ignored\n'}
2171
2172 change = self.fake_gerrit.addFakeChange('org/project',
2173 'master',
2174 'test irrelevant-files',
2175 files=files)
2176 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2177 self.waitUntilSettled()
2178
2179 run_jobs = set([build.name for build in self.history])
2180
2181 self.assertEqual(set(['project-test-nomatch-starts-empty',
2182 'project-test-nomatch-starts-full']), run_jobs)
2183
James E. Blairc8a1e052014-02-25 09:29:26 -08002184 def test_queue_names(self):
2185 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002186 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002187 (trusted, project1) = tenant.getProject('org/project1')
2188 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002189 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2190 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2191 self.assertEqual(q1.name, 'integrated')
2192 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002193
Clint Byrum8897dca2017-05-10 19:14:19 -07002194 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blair64ed6f22013-07-10 14:07:23 -07002195 def test_queue_precedence(self):
2196 "Test that queue precedence works"
2197
2198 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002199 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002200 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2201 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002202 A.addApproval('code-review', 2)
2203 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002204
2205 self.waitUntilSettled()
2206 self.gearman_server.hold_jobs_in_queue = False
2207 self.gearman_server.release()
2208 self.waitUntilSettled()
2209
James E. Blair8de58bd2013-07-18 16:23:33 -07002210 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002211 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002212 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002213 self.waitUntilSettled()
2214
James E. Blair64ed6f22013-07-10 14:07:23 -07002215 self.log.debug(self.history)
2216 self.assertEqual(self.history[0].pipeline, 'gate')
2217 self.assertEqual(self.history[1].pipeline, 'check')
2218 self.assertEqual(self.history[2].pipeline, 'gate')
2219 self.assertEqual(self.history[3].pipeline, 'gate')
2220 self.assertEqual(self.history[4].pipeline, 'check')
2221 self.assertEqual(self.history[5].pipeline, 'check')
2222
Clark Boylana5edbe42014-06-03 16:39:10 -07002223 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002224 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002225 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002226 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002227 A.addApproval('code-review', 2)
2228 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002229 self.waitUntilSettled()
2230
Paul Belanger174a8272017-03-14 13:20:10 -04002231 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002232 self.waitUntilSettled()
2233
James E. Blair1843a552013-07-03 14:19:52 -07002234 port = self.webapp.server.socket.getsockname()[1]
2235
Adam Gandelman77a12c72017-02-03 17:43:43 -08002236 req = urllib.request.Request(
2237 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002238 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002239 headers = f.info()
2240 self.assertIn('Content-Length', headers)
2241 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002242 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2243 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002244 self.assertIn('Access-Control-Allow-Origin', headers)
2245 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002246 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002247 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002248 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002249
Paul Belanger174a8272017-03-14 13:20:10 -04002250 self.executor_server.hold_jobs_in_build = False
2251 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002252 self.waitUntilSettled()
2253
2254 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002255 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002256 for p in data['pipelines']:
2257 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002258 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002259 self.assertEqual(q['window'], 20)
2260 else:
2261 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002262 for head in q['heads']:
2263 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002264 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002265 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002266 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002267 status_jobs.append(job)
2268 self.assertEqual('project-merge', status_jobs[0]['name'])
2269 self.assertEqual('https://server/job/project-merge/0/',
2270 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002271 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002272 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002273 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002274 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002275 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002276 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002277 status_jobs[1]['report_url'])
2278
2279 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002280 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002281 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002282 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002283 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002284
James E. Blaircdccd972013-07-01 12:10:22 -07002285 def test_live_reconfiguration(self):
2286 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002287 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002288 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002289 A.addApproval('code-review', 2)
2290 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002291 self.waitUntilSettled()
2292
2293 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002294 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002295
Paul Belanger174a8272017-03-14 13:20:10 -04002296 self.executor_server.hold_jobs_in_build = False
2297 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002298 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002299 self.assertEqual(self.getJobFromHistory('project-merge').result,
2300 'SUCCESS')
2301 self.assertEqual(self.getJobFromHistory('project-test1').result,
2302 'SUCCESS')
2303 self.assertEqual(self.getJobFromHistory('project-test2').result,
2304 'SUCCESS')
2305 self.assertEqual(A.data['status'], 'MERGED')
2306 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002307
James E. Blair6bc782d2015-07-17 16:20:21 -07002308 def test_live_reconfiguration_merge_conflict(self):
2309 # A real-world bug: a change in a gate queue has a merge
2310 # conflict and a job is added to its project while it's
2311 # sitting in the queue. The job gets added to the change and
2312 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002313 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002314
2315 # This change is fine. It's here to stop the queue long
2316 # enough for the next change to be subject to the
2317 # reconfiguration, as well as to provide a conflict for the
2318 # next change. This change will succeed and merge.
2319 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002320 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002321 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002322
2323 # This change will be in merge conflict. During the
2324 # reconfiguration, we will add a job. We want to make sure
2325 # that doesn't cause it to get stuck.
2326 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002327 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002328 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002329
James E. Blair8b5408c2016-08-08 15:37:46 -07002330 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2331 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002332
2333 self.waitUntilSettled()
2334
2335 # No jobs have run yet
2336 self.assertEqual(A.data['status'], 'NEW')
2337 self.assertEqual(A.reported, 1)
2338 self.assertEqual(B.data['status'], 'NEW')
2339 self.assertEqual(B.reported, 1)
2340 self.assertEqual(len(self.history), 0)
2341
2342 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002343 self.commitConfigUpdate('common-config',
2344 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002345 self.sched.reconfigure(self.config)
2346 self.waitUntilSettled()
2347
Paul Belanger174a8272017-03-14 13:20:10 -04002348 self.executor_server.hold_jobs_in_build = False
2349 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002350 self.waitUntilSettled()
2351
2352 self.assertEqual(A.data['status'], 'MERGED')
2353 self.assertEqual(A.reported, 2)
2354 self.assertEqual(B.data['status'], 'NEW')
2355 self.assertEqual(B.reported, 2)
2356 self.assertEqual(self.getJobFromHistory('project-merge').result,
2357 'SUCCESS')
2358 self.assertEqual(self.getJobFromHistory('project-test1').result,
2359 'SUCCESS')
2360 self.assertEqual(self.getJobFromHistory('project-test2').result,
2361 'SUCCESS')
2362 self.assertEqual(self.getJobFromHistory('project-test3').result,
2363 'SUCCESS')
2364 self.assertEqual(len(self.history), 4)
2365
James E. Blair400e8fd2015-07-30 17:44:45 -07002366 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002367 # An extrapolation of test_live_reconfiguration_merge_conflict
2368 # that tests a job added to a job tree with a failed root does
2369 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002370 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002371
2372 # This change is fine. It's here to stop the queue long
2373 # enough for the next change to be subject to the
2374 # reconfiguration. This change will succeed and merge.
2375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002376 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002377 A.addApproval('code-review', 2)
2378 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002379 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002380 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002381 self.waitUntilSettled()
2382
2383 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002384 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002385 B.addApproval('code-review', 2)
2386 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002387 self.waitUntilSettled()
2388
Paul Belanger174a8272017-03-14 13:20:10 -04002389 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002390 self.waitUntilSettled()
2391
2392 # Both -merge jobs have run, but no others.
2393 self.assertEqual(A.data['status'], 'NEW')
2394 self.assertEqual(A.reported, 1)
2395 self.assertEqual(B.data['status'], 'NEW')
2396 self.assertEqual(B.reported, 1)
2397 self.assertEqual(self.history[0].result, 'SUCCESS')
2398 self.assertEqual(self.history[0].name, 'project-merge')
2399 self.assertEqual(self.history[1].result, 'FAILURE')
2400 self.assertEqual(self.history[1].name, 'project-merge')
2401 self.assertEqual(len(self.history), 2)
2402
2403 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002404 self.commitConfigUpdate('common-config',
2405 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002406 self.sched.reconfigure(self.config)
2407 self.waitUntilSettled()
2408
Paul Belanger174a8272017-03-14 13:20:10 -04002409 self.executor_server.hold_jobs_in_build = False
2410 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002411 self.waitUntilSettled()
2412
2413 self.assertEqual(A.data['status'], 'MERGED')
2414 self.assertEqual(A.reported, 2)
2415 self.assertEqual(B.data['status'], 'NEW')
2416 self.assertEqual(B.reported, 2)
2417 self.assertEqual(self.history[0].result, 'SUCCESS')
2418 self.assertEqual(self.history[0].name, 'project-merge')
2419 self.assertEqual(self.history[1].result, 'FAILURE')
2420 self.assertEqual(self.history[1].name, 'project-merge')
2421 self.assertEqual(self.history[2].result, 'SUCCESS')
2422 self.assertEqual(self.history[3].result, 'SUCCESS')
2423 self.assertEqual(self.history[4].result, 'SUCCESS')
2424 self.assertEqual(len(self.history), 5)
2425
James E. Blair400e8fd2015-07-30 17:44:45 -07002426 def test_live_reconfiguration_failed_job(self):
2427 # Test that a change with a removed failing job does not
2428 # disrupt reconfiguration. If a change has a failed job and
2429 # that job is removed during a reconfiguration, we observed a
2430 # bug where the code to re-set build statuses would run on
2431 # that build and raise an exception because the job no longer
2432 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002433 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002434
2435 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2436
2437 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002438 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002439
2440 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2441 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002442 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002443 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002444 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002445 self.waitUntilSettled()
2446
2447 self.assertEqual(A.data['status'], 'NEW')
2448 self.assertEqual(A.reported, 0)
2449
2450 self.assertEqual(self.getJobFromHistory('project-merge').result,
2451 'SUCCESS')
2452 self.assertEqual(self.getJobFromHistory('project-test1').result,
2453 'FAILURE')
2454 self.assertEqual(len(self.history), 2)
2455
2456 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002457 self.commitConfigUpdate('common-config',
2458 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002459 self.sched.reconfigure(self.config)
2460 self.waitUntilSettled()
2461
Paul Belanger174a8272017-03-14 13:20:10 -04002462 self.executor_server.hold_jobs_in_build = False
2463 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002464 self.waitUntilSettled()
2465
2466 self.assertEqual(self.getJobFromHistory('project-test2').result,
2467 'SUCCESS')
2468 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2469 'SUCCESS')
2470 self.assertEqual(len(self.history), 4)
2471
2472 self.assertEqual(A.data['status'], 'NEW')
2473 self.assertEqual(A.reported, 1)
2474 self.assertIn('Build succeeded', A.messages[0])
2475 # Ensure the removed job was not included in the report.
2476 self.assertNotIn('project-test1', A.messages[0])
2477
James E. Blairfe707d12015-08-05 15:18:15 -07002478 def test_live_reconfiguration_shared_queue(self):
2479 # Test that a change with a failing job which was removed from
2480 # this project but otherwise still exists in the system does
2481 # not disrupt reconfiguration.
2482
Paul Belanger174a8272017-03-14 13:20:10 -04002483 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002484
2485 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2486
Paul Belanger174a8272017-03-14 13:20:10 -04002487 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002488
2489 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2490 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002491 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002492 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002493 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002494 self.waitUntilSettled()
2495
2496 self.assertEqual(A.data['status'], 'NEW')
2497 self.assertEqual(A.reported, 0)
2498
Clint Byrum69663d42017-05-01 14:51:04 -07002499 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002500 'SUCCESS')
2501 self.assertEqual(self.getJobFromHistory(
2502 'project1-project2-integration').result, 'FAILURE')
2503 self.assertEqual(len(self.history), 2)
2504
2505 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002506 self.commitConfigUpdate(
2507 'common-config',
2508 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002509 self.sched.reconfigure(self.config)
2510 self.waitUntilSettled()
2511
Paul Belanger174a8272017-03-14 13:20:10 -04002512 self.executor_server.hold_jobs_in_build = False
2513 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002514 self.waitUntilSettled()
2515
Clint Byrum69663d42017-05-01 14:51:04 -07002516 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002517 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002518 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002519 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002520 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002521 'SUCCESS')
2522 self.assertEqual(self.getJobFromHistory(
2523 'project1-project2-integration').result, 'FAILURE')
2524 self.assertEqual(len(self.history), 4)
2525
2526 self.assertEqual(A.data['status'], 'NEW')
2527 self.assertEqual(A.reported, 1)
2528 self.assertIn('Build succeeded', A.messages[0])
2529 # Ensure the removed job was not included in the report.
2530 self.assertNotIn('project1-project2-integration', A.messages[0])
2531
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002532 def test_double_live_reconfiguration_shared_queue(self):
2533 # This was a real-world regression. A change is added to
2534 # gate; a reconfigure happens, a second change which depends
2535 # on the first is added, and a second reconfiguration happens.
2536 # Ensure that both changes merge.
2537
2538 # A failure may indicate incorrect caching or cleaning up of
2539 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002540 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002541
2542 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2543 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2544 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002545 A.addApproval('code-review', 2)
2546 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002547
2548 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002549 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002550 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002551 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002552 self.waitUntilSettled()
2553
2554 # Reconfigure (with only one change in the pipeline).
2555 self.sched.reconfigure(self.config)
2556 self.waitUntilSettled()
2557
2558 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002559 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002560 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002561 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002562 self.waitUntilSettled()
2563
2564 # Reconfigure (with both in the pipeline).
2565 self.sched.reconfigure(self.config)
2566 self.waitUntilSettled()
2567
Paul Belanger174a8272017-03-14 13:20:10 -04002568 self.executor_server.hold_jobs_in_build = False
2569 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002570 self.waitUntilSettled()
2571
2572 self.assertEqual(len(self.history), 8)
2573
2574 self.assertEqual(A.data['status'], 'MERGED')
2575 self.assertEqual(A.reported, 2)
2576 self.assertEqual(B.data['status'], 'MERGED')
2577 self.assertEqual(B.reported, 2)
2578
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002579 def test_live_reconfiguration_del_project(self):
2580 # Test project deletion from layout
2581 # while changes are enqueued
2582
Paul Belanger174a8272017-03-14 13:20:10 -04002583 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002584 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2585 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2586 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2587
2588 # A Depends-On: B
2589 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2590 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002591 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002592
2593 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2594 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2595 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002596 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002597 self.waitUntilSettled()
2598 self.assertEqual(len(self.builds), 5)
2599
2600 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002601 self.commitConfigUpdate(
2602 'common-config',
2603 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002604 self.sched.reconfigure(self.config)
2605 self.waitUntilSettled()
2606
2607 # Builds for C aborted, builds for A succeed,
2608 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002609 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002610 self.assertEqual(job_c.changes, '3,1')
2611 self.assertEqual(job_c.result, 'ABORTED')
2612
Paul Belanger174a8272017-03-14 13:20:10 -04002613 self.executor_server.hold_jobs_in_build = False
2614 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002615 self.waitUntilSettled()
2616
Clint Byrum0149ada2017-04-04 12:22:01 -07002617 self.assertEqual(
2618 self.getJobFromHistory('project-test1', 'org/project').changes,
2619 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002620
2621 self.assertEqual(A.data['status'], 'NEW')
2622 self.assertEqual(B.data['status'], 'NEW')
2623 self.assertEqual(C.data['status'], 'NEW')
2624 self.assertEqual(A.reported, 1)
2625 self.assertEqual(B.reported, 0)
2626 self.assertEqual(C.reported, 0)
2627
Clint Byrum0149ada2017-04-04 12:22:01 -07002628 tenant = self.sched.abide.tenants.get('tenant-one')
2629 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002630 self.assertIn('Build succeeded', A.messages[0])
2631
James E. Blairec056492016-07-22 09:45:56 -07002632 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002633 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002634 self.updateConfigLayout(
2635 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002636 self.sched.reconfigure(self.config)
2637
2638 self.init_repo("org/new-project")
2639 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2640
James E. Blair8b5408c2016-08-08 15:37:46 -07002641 A.addApproval('code-review', 2)
2642 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002643 self.waitUntilSettled()
2644 self.assertEqual(self.getJobFromHistory('project-merge').result,
2645 'SUCCESS')
2646 self.assertEqual(self.getJobFromHistory('project-test1').result,
2647 'SUCCESS')
2648 self.assertEqual(self.getJobFromHistory('project-test2').result,
2649 'SUCCESS')
2650 self.assertEqual(A.data['status'], 'MERGED')
2651 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002652
James E. Blair9ea70072017-04-19 16:05:30 -07002653 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002654 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002655 self.init_repo("org/delete-project")
2656 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2657
James E. Blair8b5408c2016-08-08 15:37:46 -07002658 A.addApproval('code-review', 2)
2659 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002660 self.waitUntilSettled()
2661 self.assertEqual(self.getJobFromHistory('project-merge').result,
2662 'SUCCESS')
2663 self.assertEqual(self.getJobFromHistory('project-test1').result,
2664 'SUCCESS')
2665 self.assertEqual(self.getJobFromHistory('project-test2').result,
2666 'SUCCESS')
2667 self.assertEqual(A.data['status'], 'MERGED')
2668 self.assertEqual(A.reported, 2)
2669
2670 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002671 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002672 if os.path.exists(os.path.join(self.merger_src_root, p)):
2673 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002674 if os.path.exists(os.path.join(self.executor_src_root, p)):
2675 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002676
2677 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2678
James E. Blair8b5408c2016-08-08 15:37:46 -07002679 B.addApproval('code-review', 2)
2680 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002681 self.waitUntilSettled()
2682 self.assertEqual(self.getJobFromHistory('project-merge').result,
2683 'SUCCESS')
2684 self.assertEqual(self.getJobFromHistory('project-test1').result,
2685 'SUCCESS')
2686 self.assertEqual(self.getJobFromHistory('project-test2').result,
2687 'SUCCESS')
2688 self.assertEqual(B.data['status'], 'MERGED')
2689 self.assertEqual(B.reported, 2)
2690
James E. Blaire68d43f2017-04-19 14:03:30 -07002691 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002692 def test_tags(self):
2693 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002694 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2695 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2696 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2697 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2698 self.waitUntilSettled()
2699
James E. Blaire68d43f2017-04-19 14:03:30 -07002700 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002701
2702 results = {self.getJobFromHistory('merge',
2703 project='org/project1').uuid: 'extratag merge',
2704 self.getJobFromHistory('merge',
2705 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002706
2707 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002708 self.assertEqual(results.get(build.uuid, ''),
2709 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002710
James E. Blair63bb0ef2013-07-29 17:14:51 -07002711 def test_timer(self):
2712 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002713 # This test can not use simple_layout because it must start
2714 # with a configuration which does not include a
2715 # timer-triggered job so that we have an opportunity to set
2716 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002717 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002718 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002719 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002720
Clark Boylan3ee090a2014-04-03 20:55:09 -07002721 # The pipeline triggers every second, so we should have seen
2722 # several by now.
2723 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002724 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002725
2726 self.assertEqual(len(self.builds), 2)
2727
James E. Blair63bb0ef2013-07-29 17:14:51 -07002728 port = self.webapp.server.socket.getsockname()[1]
2729
Clint Byrum5e729e92017-02-08 06:34:57 -08002730 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002731 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002732 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002733 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002734
Paul Belanger174a8272017-03-14 13:20:10 -04002735 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002736 # Stop queuing timer triggered jobs so that the assertions
2737 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002738 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002739 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002740 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002741 self.waitUntilSettled()
2742
2743 self.assertEqual(self.getJobFromHistory(
2744 'project-bitrot-stable-old').result, 'SUCCESS')
2745 self.assertEqual(self.getJobFromHistory(
2746 'project-bitrot-stable-older').result, 'SUCCESS')
2747
2748 data = json.loads(data)
2749 status_jobs = set()
2750 for p in data['pipelines']:
2751 for q in p['change_queues']:
2752 for head in q['heads']:
2753 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002754 for job in change['jobs']:
2755 status_jobs.add(job['name'])
2756 self.assertIn('project-bitrot-stable-old', status_jobs)
2757 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002758
James E. Blair4f6033c2014-03-27 15:49:09 -07002759 def test_idle(self):
2760 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002761 # This test can not use simple_layout because it must start
2762 # with a configuration which does not include a
2763 # timer-triggered job so that we have an opportunity to set
2764 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002765 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002766
Clark Boylan3ee090a2014-04-03 20:55:09 -07002767 for x in range(1, 3):
2768 # Test that timer triggers periodic jobs even across
2769 # layout config reloads.
2770 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002771 self.commitConfigUpdate('common-config',
2772 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002773 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002774 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002775
Clark Boylan3ee090a2014-04-03 20:55:09 -07002776 # The pipeline triggers every second, so we should have seen
2777 # several by now.
2778 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002779
2780 # Stop queuing timer triggered jobs so that the assertions
2781 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002782 self.commitConfigUpdate('common-config',
2783 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002784 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002785 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002786 self.assertEqual(len(self.builds), 2,
2787 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002788 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002789 self.waitUntilSettled()
2790 self.assertEqual(len(self.builds), 0)
2791 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002792
James E. Blaircc324192017-04-19 14:36:08 -07002793 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002794 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2796 self.waitUntilSettled()
2797
2798 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2799 self.waitUntilSettled()
2800
James E. Blairff80a2f2013-12-27 13:24:06 -08002801 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002802
2803 # A.messages only holds what FakeGerrit places in it. Thus we
2804 # work on the knowledge of what the first message should be as
2805 # it is only configured to go to SMTP.
2806
2807 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002808 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002809 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002810 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002811 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002812 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002813
2814 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002815 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002816 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002817 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002818 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002819 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002820
James E. Blaire5910202013-12-27 09:50:31 -08002821 def test_timer_smtp(self):
2822 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002823 # This test can not use simple_layout because it must start
2824 # with a configuration which does not include a
2825 # timer-triggered job so that we have an opportunity to set
2826 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002827 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002828 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002829 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002830
Clark Boylan3ee090a2014-04-03 20:55:09 -07002831 # The pipeline triggers every second, so we should have seen
2832 # several by now.
2833 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002834 self.waitUntilSettled()
2835
Clark Boylan3ee090a2014-04-03 20:55:09 -07002836 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002837 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002838 self.waitUntilSettled()
2839 self.assertEqual(len(self.history), 2)
2840
James E. Blaire5910202013-12-27 09:50:31 -08002841 self.assertEqual(self.getJobFromHistory(
2842 'project-bitrot-stable-old').result, 'SUCCESS')
2843 self.assertEqual(self.getJobFromHistory(
2844 'project-bitrot-stable-older').result, 'SUCCESS')
2845
James E. Blairff80a2f2013-12-27 13:24:06 -08002846 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002847
2848 # A.messages only holds what FakeGerrit places in it. Thus we
2849 # work on the knowledge of what the first message should be as
2850 # it is only configured to go to SMTP.
2851
2852 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002853 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002854 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002855 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002856 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002857 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002858
Clark Boylan3ee090a2014-04-03 20:55:09 -07002859 # Stop queuing timer triggered jobs and let any that may have
2860 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002861 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002862 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002863 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002864 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002865 self.waitUntilSettled()
2866
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002867 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002868 def test_timer_sshkey(self):
2869 "Test that a periodic job can setup SSH key authentication"
2870 self.worker.hold_jobs_in_build = True
2871 self.config.set('zuul', 'layout_config',
2872 'tests/fixtures/layout-timer.yaml')
2873 self.sched.reconfigure(self.config)
2874 self.registerJobs()
2875
2876 # The pipeline triggers every second, so we should have seen
2877 # several by now.
2878 time.sleep(5)
2879 self.waitUntilSettled()
2880
2881 self.assertEqual(len(self.builds), 2)
2882
2883 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2884 self.assertTrue(os.path.isfile(ssh_wrapper))
2885 with open(ssh_wrapper) as f:
2886 ssh_wrapper_content = f.read()
2887 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2888 # In the unit tests Merger runs in the same process,
2889 # so we see its' environment variables
2890 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2891
2892 self.worker.release('.*')
2893 self.waitUntilSettled()
2894 self.assertEqual(len(self.history), 2)
2895
2896 self.assertEqual(self.getJobFromHistory(
2897 'project-bitrot-stable-old').result, 'SUCCESS')
2898 self.assertEqual(self.getJobFromHistory(
2899 'project-bitrot-stable-older').result, 'SUCCESS')
2900
2901 # Stop queuing timer triggered jobs and let any that may have
2902 # queued through so that end of test assertions pass.
2903 self.config.set('zuul', 'layout_config',
2904 'tests/fixtures/layout-no-timer.yaml')
2905 self.sched.reconfigure(self.config)
2906 self.registerJobs()
2907 self.waitUntilSettled()
2908 self.worker.release('.*')
2909 self.waitUntilSettled()
2910
James E. Blair91e34592015-07-31 16:45:59 -07002911 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002912 "Test that the RPC client can enqueue a change"
2913 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002914 A.addApproval('code-review', 2)
2915 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002916
2917 client = zuul.rpcclient.RPCClient('127.0.0.1',
2918 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002919 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002920 r = client.enqueue(tenant='tenant-one',
2921 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002922 project='org/project',
2923 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002924 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002925 self.waitUntilSettled()
2926 self.assertEqual(self.getJobFromHistory('project-merge').result,
2927 'SUCCESS')
2928 self.assertEqual(self.getJobFromHistory('project-test1').result,
2929 'SUCCESS')
2930 self.assertEqual(self.getJobFromHistory('project-test2').result,
2931 'SUCCESS')
2932 self.assertEqual(A.data['status'], 'MERGED')
2933 self.assertEqual(A.reported, 2)
2934 self.assertEqual(r, True)
2935
James E. Blair91e34592015-07-31 16:45:59 -07002936 def test_client_enqueue_ref(self):
2937 "Test that the RPC client can enqueue a ref"
2938
2939 client = zuul.rpcclient.RPCClient('127.0.0.1',
2940 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002941 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002942 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002943 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002944 pipeline='post',
2945 project='org/project',
2946 trigger='gerrit',
2947 ref='master',
2948 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2949 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2950 self.waitUntilSettled()
2951 job_names = [x.name for x in self.history]
2952 self.assertEqual(len(self.history), 1)
2953 self.assertIn('project-post', job_names)
2954 self.assertEqual(r, True)
2955
James E. Blairad28e912013-11-27 10:43:22 -08002956 def test_client_enqueue_negative(self):
2957 "Test that the RPC client returns errors"
2958 client = zuul.rpcclient.RPCClient('127.0.0.1',
2959 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002960 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002961 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002962 "Invalid tenant"):
2963 r = client.enqueue(tenant='tenant-foo',
2964 pipeline='gate',
2965 project='org/project',
2966 trigger='gerrit',
2967 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002968 self.assertEqual(r, False)
2969
2970 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002971 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002972 r = client.enqueue(tenant='tenant-one',
2973 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002974 project='project-does-not-exist',
2975 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002976 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002977 self.assertEqual(r, False)
2978
2979 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2980 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002981 r = client.enqueue(tenant='tenant-one',
2982 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002983 project='org/project',
2984 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002985 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002986 self.assertEqual(r, False)
2987
2988 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2989 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002990 r = client.enqueue(tenant='tenant-one',
2991 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002992 project='org/project',
2993 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002994 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002995 self.assertEqual(r, False)
2996
2997 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2998 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002999 r = client.enqueue(tenant='tenant-one',
3000 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003001 project='org/project',
3002 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003003 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003004 self.assertEqual(r, False)
3005
3006 self.waitUntilSettled()
3007 self.assertEqual(len(self.history), 0)
3008 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003009
3010 def test_client_promote(self):
3011 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003012 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003013 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3014 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3015 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003016 A.addApproval('code-review', 2)
3017 B.addApproval('code-review', 2)
3018 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003019
James E. Blair8b5408c2016-08-08 15:37:46 -07003020 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3021 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3022 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003023
3024 self.waitUntilSettled()
3025
Paul Belangerbaca3132016-11-04 12:49:54 -04003026 tenant = self.sched.abide.tenants.get('tenant-one')
3027 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003028 enqueue_times = {}
3029 for item in items:
3030 enqueue_times[str(item.change)] = item.enqueue_time
3031
James E. Blair36658cf2013-12-06 17:53:48 -08003032 client = zuul.rpcclient.RPCClient('127.0.0.1',
3033 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003034 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003035 r = client.promote(tenant='tenant-one',
3036 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003037 change_ids=['2,1', '3,1'])
3038
Sean Daguef39b9ca2014-01-10 21:34:35 -05003039 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003040 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003041 for item in items:
3042 self.assertEqual(
3043 enqueue_times[str(item.change)], item.enqueue_time)
3044
James E. Blair78acec92014-02-06 07:11:32 -08003045 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003046 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003047 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003048 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003049 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003050 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003051 self.waitUntilSettled()
3052
3053 self.assertEqual(len(self.builds), 6)
3054 self.assertEqual(self.builds[0].name, 'project-test1')
3055 self.assertEqual(self.builds[1].name, 'project-test2')
3056 self.assertEqual(self.builds[2].name, 'project-test1')
3057 self.assertEqual(self.builds[3].name, 'project-test2')
3058 self.assertEqual(self.builds[4].name, 'project-test1')
3059 self.assertEqual(self.builds[5].name, 'project-test2')
3060
Paul Belangerbaca3132016-11-04 12:49:54 -04003061 self.assertTrue(self.builds[0].hasChanges(B))
3062 self.assertFalse(self.builds[0].hasChanges(A))
3063 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003064
Paul Belangerbaca3132016-11-04 12:49:54 -04003065 self.assertTrue(self.builds[2].hasChanges(B))
3066 self.assertTrue(self.builds[2].hasChanges(C))
3067 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003068
Paul Belangerbaca3132016-11-04 12:49:54 -04003069 self.assertTrue(self.builds[4].hasChanges(B))
3070 self.assertTrue(self.builds[4].hasChanges(C))
3071 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003072
Paul Belanger174a8272017-03-14 13:20:10 -04003073 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003074 self.waitUntilSettled()
3075
3076 self.assertEqual(A.data['status'], 'MERGED')
3077 self.assertEqual(A.reported, 2)
3078 self.assertEqual(B.data['status'], 'MERGED')
3079 self.assertEqual(B.reported, 2)
3080 self.assertEqual(C.data['status'], 'MERGED')
3081 self.assertEqual(C.reported, 2)
3082
James E. Blair36658cf2013-12-06 17:53:48 -08003083 self.assertEqual(r, True)
3084
3085 def test_client_promote_dependent(self):
3086 "Test that the RPC client can promote a dependent change"
3087 # C (depends on B) -> B -> A ; then promote C to get:
3088 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003089 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003090 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3091 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3092 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3093
3094 C.setDependsOn(B, 1)
3095
James E. Blair8b5408c2016-08-08 15:37:46 -07003096 A.addApproval('code-review', 2)
3097 B.addApproval('code-review', 2)
3098 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003099
James E. Blair8b5408c2016-08-08 15:37:46 -07003100 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3101 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3102 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003103
3104 self.waitUntilSettled()
3105
3106 client = zuul.rpcclient.RPCClient('127.0.0.1',
3107 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003108 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003109 r = client.promote(tenant='tenant-one',
3110 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003111 change_ids=['3,1'])
3112
James E. Blair78acec92014-02-06 07:11:32 -08003113 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003114 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003115 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003116 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003117 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003118 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003119 self.waitUntilSettled()
3120
3121 self.assertEqual(len(self.builds), 6)
3122 self.assertEqual(self.builds[0].name, 'project-test1')
3123 self.assertEqual(self.builds[1].name, 'project-test2')
3124 self.assertEqual(self.builds[2].name, 'project-test1')
3125 self.assertEqual(self.builds[3].name, 'project-test2')
3126 self.assertEqual(self.builds[4].name, 'project-test1')
3127 self.assertEqual(self.builds[5].name, 'project-test2')
3128
Paul Belangerbaca3132016-11-04 12:49:54 -04003129 self.assertTrue(self.builds[0].hasChanges(B))
3130 self.assertFalse(self.builds[0].hasChanges(A))
3131 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003132
Paul Belangerbaca3132016-11-04 12:49:54 -04003133 self.assertTrue(self.builds[2].hasChanges(B))
3134 self.assertTrue(self.builds[2].hasChanges(C))
3135 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003136
Paul Belangerbaca3132016-11-04 12:49:54 -04003137 self.assertTrue(self.builds[4].hasChanges(B))
3138 self.assertTrue(self.builds[4].hasChanges(C))
3139 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003140
Paul Belanger174a8272017-03-14 13:20:10 -04003141 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003142 self.waitUntilSettled()
3143
3144 self.assertEqual(A.data['status'], 'MERGED')
3145 self.assertEqual(A.reported, 2)
3146 self.assertEqual(B.data['status'], 'MERGED')
3147 self.assertEqual(B.reported, 2)
3148 self.assertEqual(C.data['status'], 'MERGED')
3149 self.assertEqual(C.reported, 2)
3150
James E. Blair36658cf2013-12-06 17:53:48 -08003151 self.assertEqual(r, True)
3152
3153 def test_client_promote_negative(self):
3154 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003155 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003156 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003157 A.addApproval('code-review', 2)
3158 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003159 self.waitUntilSettled()
3160
3161 client = zuul.rpcclient.RPCClient('127.0.0.1',
3162 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003163 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003164
3165 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003166 r = client.promote(tenant='tenant-one',
3167 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003168 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003169 self.assertEqual(r, False)
3170
3171 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003172 r = client.promote(tenant='tenant-one',
3173 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003174 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003175 self.assertEqual(r, False)
3176
Paul Belanger174a8272017-03-14 13:20:10 -04003177 self.executor_server.hold_jobs_in_build = False
3178 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003179 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003180
James E. Blaircc324192017-04-19 14:36:08 -07003181 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003182 def test_queue_rate_limiting(self):
3183 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003184 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3186 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3187 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3188
3189 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003190 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003191
James E. Blair8b5408c2016-08-08 15:37:46 -07003192 A.addApproval('code-review', 2)
3193 B.addApproval('code-review', 2)
3194 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003195
James E. Blair8b5408c2016-08-08 15:37:46 -07003196 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3197 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3198 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003199 self.waitUntilSettled()
3200
3201 # Only A and B will have their merge jobs queued because
3202 # window is 2.
3203 self.assertEqual(len(self.builds), 2)
3204 self.assertEqual(self.builds[0].name, 'project-merge')
3205 self.assertEqual(self.builds[1].name, 'project-merge')
3206
James E. Blair732ee862017-04-21 07:49:26 -07003207 # Release the merge jobs one at a time.
3208 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003209 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003210 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003211 self.waitUntilSettled()
3212
3213 # Only A and B will have their test jobs queued because
3214 # window is 2.
3215 self.assertEqual(len(self.builds), 4)
3216 self.assertEqual(self.builds[0].name, 'project-test1')
3217 self.assertEqual(self.builds[1].name, 'project-test2')
3218 self.assertEqual(self.builds[2].name, 'project-test1')
3219 self.assertEqual(self.builds[3].name, 'project-test2')
3220
Paul Belanger174a8272017-03-14 13:20:10 -04003221 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003222 self.waitUntilSettled()
3223
James E. Blaircc324192017-04-19 14:36:08 -07003224 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003225 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003226 # A failed so window is reduced by 1 to 1.
3227 self.assertEqual(queue.window, 1)
3228 self.assertEqual(queue.window_floor, 1)
3229 self.assertEqual(A.data['status'], 'NEW')
3230
3231 # Gate is reset and only B's merge job is queued because
3232 # window shrunk to 1.
3233 self.assertEqual(len(self.builds), 1)
3234 self.assertEqual(self.builds[0].name, 'project-merge')
3235
Paul Belanger174a8272017-03-14 13:20:10 -04003236 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003237 self.waitUntilSettled()
3238
3239 # Only B's test jobs are queued because window is still 1.
3240 self.assertEqual(len(self.builds), 2)
3241 self.assertEqual(self.builds[0].name, 'project-test1')
3242 self.assertEqual(self.builds[1].name, 'project-test2')
3243
Paul Belanger174a8272017-03-14 13:20:10 -04003244 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003245 self.waitUntilSettled()
3246
3247 # B was successfully merged so window is increased to 2.
3248 self.assertEqual(queue.window, 2)
3249 self.assertEqual(queue.window_floor, 1)
3250 self.assertEqual(B.data['status'], 'MERGED')
3251
3252 # Only C is left and its merge job is queued.
3253 self.assertEqual(len(self.builds), 1)
3254 self.assertEqual(self.builds[0].name, 'project-merge')
3255
Paul Belanger174a8272017-03-14 13:20:10 -04003256 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003257 self.waitUntilSettled()
3258
3259 # After successful merge job the test jobs for C are queued.
3260 self.assertEqual(len(self.builds), 2)
3261 self.assertEqual(self.builds[0].name, 'project-test1')
3262 self.assertEqual(self.builds[1].name, 'project-test2')
3263
Paul Belanger174a8272017-03-14 13:20:10 -04003264 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003265 self.waitUntilSettled()
3266
3267 # C successfully merged so window is bumped to 3.
3268 self.assertEqual(queue.window, 3)
3269 self.assertEqual(queue.window_floor, 1)
3270 self.assertEqual(C.data['status'], 'MERGED')
3271
James E. Blaircc324192017-04-19 14:36:08 -07003272 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003273 def test_queue_rate_limiting_dependent(self):
3274 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003275 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3277 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3278 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3279
3280 B.setDependsOn(A, 1)
3281
Paul Belanger174a8272017-03-14 13:20:10 -04003282 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003283
James E. Blair8b5408c2016-08-08 15:37:46 -07003284 A.addApproval('code-review', 2)
3285 B.addApproval('code-review', 2)
3286 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003287
James E. Blair8b5408c2016-08-08 15:37:46 -07003288 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3289 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3290 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003291 self.waitUntilSettled()
3292
3293 # Only A and B will have their merge jobs queued because
3294 # window is 2.
3295 self.assertEqual(len(self.builds), 2)
3296 self.assertEqual(self.builds[0].name, 'project-merge')
3297 self.assertEqual(self.builds[1].name, 'project-merge')
3298
James E. Blairf4a5f022017-04-18 14:01:10 -07003299 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003300
3301 # Only A and B will have their test jobs queued because
3302 # window is 2.
3303 self.assertEqual(len(self.builds), 4)
3304 self.assertEqual(self.builds[0].name, 'project-test1')
3305 self.assertEqual(self.builds[1].name, 'project-test2')
3306 self.assertEqual(self.builds[2].name, 'project-test1')
3307 self.assertEqual(self.builds[3].name, 'project-test2')
3308
Paul Belanger174a8272017-03-14 13:20:10 -04003309 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003310 self.waitUntilSettled()
3311
James E. Blaircc324192017-04-19 14:36:08 -07003312 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003313 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003314 # A failed so window is reduced by 1 to 1.
3315 self.assertEqual(queue.window, 1)
3316 self.assertEqual(queue.window_floor, 1)
3317 self.assertEqual(A.data['status'], 'NEW')
3318 self.assertEqual(B.data['status'], 'NEW')
3319
3320 # Gate is reset and only C's merge job is queued because
3321 # window shrunk to 1 and A and B were dequeued.
3322 self.assertEqual(len(self.builds), 1)
3323 self.assertEqual(self.builds[0].name, 'project-merge')
3324
James E. Blairf4a5f022017-04-18 14:01:10 -07003325 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003326
3327 # Only C's test jobs are queued because window is still 1.
3328 self.assertEqual(len(self.builds), 2)
3329 self.assertEqual(self.builds[0].name, 'project-test1')
3330 self.assertEqual(self.builds[1].name, 'project-test2')
3331
Paul Belanger174a8272017-03-14 13:20:10 -04003332 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003333 self.waitUntilSettled()
3334
3335 # C was successfully merged so window is increased to 2.
3336 self.assertEqual(queue.window, 2)
3337 self.assertEqual(queue.window_floor, 1)
3338 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003339
James E. Blairec056492016-07-22 09:45:56 -07003340 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003341 def test_worker_update_metadata(self):
3342 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003343 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003344
3345 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003346 A.addApproval('code-review', 2)
3347 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003348 self.waitUntilSettled()
3349
Paul Belanger174a8272017-03-14 13:20:10 -04003350 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003351
3352 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003353 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003354
3355 start = time.time()
3356 while True:
3357 if time.time() - start > 10:
3358 raise Exception("Timeout waiting for gearman server to report "
3359 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003360 build = list(self.executor.builds.values())[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003361 if build.worker.name == "My Worker":
3362 break
3363 else:
3364 time.sleep(0)
3365
3366 self.log.debug(build)
3367 self.assertEqual("My Worker", build.worker.name)
3368 self.assertEqual("localhost", build.worker.hostname)
3369 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3370 self.assertEqual("zuul.example.org", build.worker.fqdn)
3371 self.assertEqual("FakeBuilder", build.worker.program)
3372 self.assertEqual("v1.1", build.worker.version)
3373 self.assertEqual({'something': 'else'}, build.worker.extra)
3374
Paul Belanger174a8272017-03-14 13:20:10 -04003375 self.executor_server.hold_jobs_in_build = False
3376 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003377 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003378
James E. Blaircc324192017-04-19 14:36:08 -07003379 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003380 def test_footer_message(self):
3381 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003382 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003383 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003384 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003385 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003386 self.waitUntilSettled()
3387
3388 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003389 B.addApproval('code-review', 2)
3390 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003391 self.waitUntilSettled()
3392
3393 self.assertEqual(2, len(self.smtp_messages))
3394
Adam Gandelman82258fd2017-02-07 16:51:58 -08003395 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003396Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003397http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003398
Adam Gandelman82258fd2017-02-07 16:51:58 -08003399 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003400For CI problems and help debugging, contact ci@example.org"""
3401
Adam Gandelman82258fd2017-02-07 16:51:58 -08003402 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3403 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3404 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3405 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003406
James E. Blairec056492016-07-22 09:45:56 -07003407 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003408 def test_merge_failure_reporters(self):
3409 """Check that the config is set up correctly"""
3410
James E. Blairf84026c2015-12-08 16:11:46 -08003411 self.updateConfigLayout(
3412 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003413 self.sched.reconfigure(self.config)
3414 self.registerJobs()
3415
3416 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003417 "Merge Failed.\n\nThis change or one of its cross-repo "
3418 "dependencies was unable to be automatically merged with the "
3419 "current state of its repository. Please rebase the change and "
3420 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003421 self.sched.layout.pipelines['check'].merge_failure_message)
3422 self.assertEqual(
3423 "The merge failed! For more information...",
3424 self.sched.layout.pipelines['gate'].merge_failure_message)
3425
3426 self.assertEqual(
3427 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3428 self.assertEqual(
3429 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3430
3431 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003432 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3433 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003434
3435 self.assertTrue(
3436 (
3437 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003438 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003439 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003440 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003441 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003442 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003443 ) or (
3444 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003445 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003446 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003447 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003448 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003449 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003450 )
3451 )
3452
James E. Blairec056492016-07-22 09:45:56 -07003453 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003454 def test_merge_failure_reports(self):
3455 """Check that when a change fails to merge the correct message is sent
3456 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003457 self.updateConfigLayout(
3458 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003459 self.sched.reconfigure(self.config)
3460 self.registerJobs()
3461
3462 # Check a test failure isn't reported to SMTP
3463 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003464 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003465 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003466 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003467 self.waitUntilSettled()
3468
3469 self.assertEqual(3, len(self.history)) # 3 jobs
3470 self.assertEqual(0, len(self.smtp_messages))
3471
3472 # Check a merge failure is reported to SMTP
3473 # B should be merged, but C will conflict with B
3474 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3475 B.addPatchset(['conflict'])
3476 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3477 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003478 B.addApproval('code-review', 2)
3479 C.addApproval('code-review', 2)
3480 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3481 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003482 self.waitUntilSettled()
3483
3484 self.assertEqual(6, len(self.history)) # A and B jobs
3485 self.assertEqual(1, len(self.smtp_messages))
3486 self.assertEqual('The merge failed! For more information...',
3487 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003488
James E. Blairec056492016-07-22 09:45:56 -07003489 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003490 def test_default_merge_failure_reports(self):
3491 """Check that the default merge failure reports are correct."""
3492
3493 # A should report success, B should report merge failure.
3494 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3495 A.addPatchset(['conflict'])
3496 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3497 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003498 A.addApproval('code-review', 2)
3499 B.addApproval('code-review', 2)
3500 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3501 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003502 self.waitUntilSettled()
3503
3504 self.assertEqual(3, len(self.history)) # A jobs
3505 self.assertEqual(A.reported, 2)
3506 self.assertEqual(B.reported, 2)
3507 self.assertEqual(A.data['status'], 'MERGED')
3508 self.assertEqual(B.data['status'], 'NEW')
3509 self.assertIn('Build succeeded', A.messages[1])
3510 self.assertIn('Merge Failed', B.messages[1])
3511 self.assertIn('automatically merged', B.messages[1])
3512 self.assertNotIn('logs.example.com', B.messages[1])
3513 self.assertNotIn('SKIPPED', B.messages[1])
3514
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003515 def test_client_get_running_jobs(self):
3516 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003517 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003519 A.addApproval('code-review', 2)
3520 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003521 self.waitUntilSettled()
3522
3523 client = zuul.rpcclient.RPCClient('127.0.0.1',
3524 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003525 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003526
3527 # Wait for gearman server to send the initial workData back to zuul
3528 start = time.time()
3529 while True:
3530 if time.time() - start > 10:
3531 raise Exception("Timeout waiting for gearman server to report "
3532 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003533 build = list(self.executor_client.builds.values())[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003534 if build.worker.name == "My Worker":
3535 break
3536 else:
3537 time.sleep(0)
3538
3539 running_items = client.get_running_jobs()
3540
3541 self.assertEqual(1, len(running_items))
3542 running_item = running_items[0]
3543 self.assertEqual([], running_item['failing_reasons'])
3544 self.assertEqual([], running_item['items_behind'])
3545 self.assertEqual('https://hostname/1', running_item['url'])
3546 self.assertEqual(None, running_item['item_ahead'])
3547 self.assertEqual('org/project', running_item['project'])
3548 self.assertEqual(None, running_item['remaining_time'])
3549 self.assertEqual(True, running_item['active'])
3550 self.assertEqual('1,1', running_item['id'])
3551
3552 self.assertEqual(3, len(running_item['jobs']))
3553 for job in running_item['jobs']:
3554 if job['name'] == 'project-merge':
3555 self.assertEqual('project-merge', job['name'])
3556 self.assertEqual('gate', job['pipeline'])
3557 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003558 self.assertEqual('https://server/job/project-merge/0/',
3559 job['url'])
3560 self.assertEqual(7, len(job['worker']))
3561 self.assertEqual(False, job['canceled'])
3562 self.assertEqual(True, job['voting'])
3563 self.assertEqual(None, job['result'])
3564 self.assertEqual('gate', job['pipeline'])
3565 break
3566
Paul Belanger174a8272017-03-14 13:20:10 -04003567 self.executor_server.hold_jobs_in_build = False
3568 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003569 self.waitUntilSettled()
3570
3571 running_items = client.get_running_jobs()
3572 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003573
James E. Blairb70e55a2017-04-19 12:57:02 -07003574 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003575 def test_nonvoting_pipeline(self):
3576 "Test that a nonvoting pipeline (experimental) can still report"
3577
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003578 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3579 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003580 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3581 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003582
3583 self.assertEqual(self.getJobFromHistory('project-merge').result,
3584 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003585 self.assertEqual(
3586 self.getJobFromHistory('experimental-project-test').result,
3587 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003588 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003589
3590 def test_crd_gate(self):
3591 "Test cross-repo dependencies"
3592 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3593 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003594 A.addApproval('code-review', 2)
3595 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003596
3597 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3598 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3599 AM2.setMerged()
3600 AM1.setMerged()
3601
3602 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3603 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3604 BM2.setMerged()
3605 BM1.setMerged()
3606
3607 # A -> AM1 -> AM2
3608 # B -> BM1 -> BM2
3609 # A Depends-On: B
3610 # M2 is here to make sure it is never queried. If it is, it
3611 # means zuul is walking down the entire history of merged
3612 # changes.
3613
3614 B.setDependsOn(BM1, 1)
3615 BM1.setDependsOn(BM2, 1)
3616
3617 A.setDependsOn(AM1, 1)
3618 AM1.setDependsOn(AM2, 1)
3619
3620 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3621 A.subject, B.data['id'])
3622
James E. Blair8b5408c2016-08-08 15:37:46 -07003623 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003624 self.waitUntilSettled()
3625
3626 self.assertEqual(A.data['status'], 'NEW')
3627 self.assertEqual(B.data['status'], 'NEW')
3628
Paul Belanger5dccbe72016-11-14 11:17:37 -05003629 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003630 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003631
Paul Belanger174a8272017-03-14 13:20:10 -04003632 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003633 B.addApproval('approved', 1)
3634 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003635 self.waitUntilSettled()
3636
Paul Belanger174a8272017-03-14 13:20:10 -04003637 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003638 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003639 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003640 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003641 self.executor_server.hold_jobs_in_build = False
3642 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003643 self.waitUntilSettled()
3644
3645 self.assertEqual(AM2.queried, 0)
3646 self.assertEqual(BM2.queried, 0)
3647 self.assertEqual(A.data['status'], 'MERGED')
3648 self.assertEqual(B.data['status'], 'MERGED')
3649 self.assertEqual(A.reported, 2)
3650 self.assertEqual(B.reported, 2)
3651
Paul Belanger5dccbe72016-11-14 11:17:37 -05003652 changes = self.getJobFromHistory(
3653 'project-merge', 'org/project1').changes
3654 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003655
3656 def test_crd_branch(self):
3657 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003658
3659 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003660 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3661 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3662 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3663 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003664 A.addApproval('code-review', 2)
3665 B.addApproval('code-review', 2)
3666 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003667
3668 # A Depends-On: B+C
3669 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3670 A.subject, B.data['id'])
3671
Paul Belanger174a8272017-03-14 13:20:10 -04003672 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003673 B.addApproval('approved', 1)
3674 C.addApproval('approved', 1)
3675 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003676 self.waitUntilSettled()
3677
Paul Belanger174a8272017-03-14 13:20:10 -04003678 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003679 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003680 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003681 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003682 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003683 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003684 self.executor_server.hold_jobs_in_build = False
3685 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003686 self.waitUntilSettled()
3687
3688 self.assertEqual(A.data['status'], 'MERGED')
3689 self.assertEqual(B.data['status'], 'MERGED')
3690 self.assertEqual(C.data['status'], 'MERGED')
3691 self.assertEqual(A.reported, 2)
3692 self.assertEqual(B.reported, 2)
3693 self.assertEqual(C.reported, 2)
3694
Paul Belanger6379db12016-11-14 13:57:54 -05003695 changes = self.getJobFromHistory(
3696 'project-merge', 'org/project1').changes
3697 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003698
3699 def test_crd_multiline(self):
3700 "Test multiple depends-on lines in commit"
3701 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3702 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3703 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003704 A.addApproval('code-review', 2)
3705 B.addApproval('code-review', 2)
3706 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003707
3708 # A Depends-On: B+C
3709 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3710 A.subject, B.data['id'], C.data['id'])
3711
Paul Belanger174a8272017-03-14 13:20:10 -04003712 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003713 B.addApproval('approved', 1)
3714 C.addApproval('approved', 1)
3715 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003716 self.waitUntilSettled()
3717
Paul Belanger174a8272017-03-14 13:20:10 -04003718 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003719 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003720 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003721 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003722 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003723 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003724 self.executor_server.hold_jobs_in_build = False
3725 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003726 self.waitUntilSettled()
3727
3728 self.assertEqual(A.data['status'], 'MERGED')
3729 self.assertEqual(B.data['status'], 'MERGED')
3730 self.assertEqual(C.data['status'], 'MERGED')
3731 self.assertEqual(A.reported, 2)
3732 self.assertEqual(B.reported, 2)
3733 self.assertEqual(C.reported, 2)
3734
Paul Belanger5dccbe72016-11-14 11:17:37 -05003735 changes = self.getJobFromHistory(
3736 'project-merge', 'org/project1').changes
3737 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003738
3739 def test_crd_unshared_gate(self):
3740 "Test cross-repo dependencies in unshared gate queues"
3741 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3742 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003743 A.addApproval('code-review', 2)
3744 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003745
3746 # A Depends-On: B
3747 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3748 A.subject, B.data['id'])
3749
3750 # A and B do not share a queue, make sure that A is unable to
3751 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003752 B.addApproval('approved', 1)
3753 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003754 self.waitUntilSettled()
3755
3756 self.assertEqual(A.data['status'], 'NEW')
3757 self.assertEqual(B.data['status'], 'NEW')
3758 self.assertEqual(A.reported, 0)
3759 self.assertEqual(B.reported, 0)
3760 self.assertEqual(len(self.history), 0)
3761
3762 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003763 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003764 self.waitUntilSettled()
3765
3766 self.assertEqual(B.data['status'], 'MERGED')
3767 self.assertEqual(B.reported, 2)
3768
3769 # Now that B is merged, A should be able to be enqueued and
3770 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003771 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003772 self.waitUntilSettled()
3773
3774 self.assertEqual(A.data['status'], 'MERGED')
3775 self.assertEqual(A.reported, 2)
3776
James E. Blair96698e22015-04-02 07:48:21 -07003777 def test_crd_gate_reverse(self):
3778 "Test reverse cross-repo dependencies"
3779 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3780 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003781 A.addApproval('code-review', 2)
3782 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003783
3784 # A Depends-On: B
3785
3786 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3787 A.subject, B.data['id'])
3788
James E. Blair8b5408c2016-08-08 15:37:46 -07003789 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003790 self.waitUntilSettled()
3791
3792 self.assertEqual(A.data['status'], 'NEW')
3793 self.assertEqual(B.data['status'], 'NEW')
3794
Paul Belanger174a8272017-03-14 13:20:10 -04003795 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003796 A.addApproval('approved', 1)
3797 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003798 self.waitUntilSettled()
3799
Paul Belanger174a8272017-03-14 13:20:10 -04003800 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003801 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003802 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003803 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003804 self.executor_server.hold_jobs_in_build = False
3805 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003806 self.waitUntilSettled()
3807
3808 self.assertEqual(A.data['status'], 'MERGED')
3809 self.assertEqual(B.data['status'], 'MERGED')
3810 self.assertEqual(A.reported, 2)
3811 self.assertEqual(B.reported, 2)
3812
Paul Belanger5dccbe72016-11-14 11:17:37 -05003813 changes = self.getJobFromHistory(
3814 'project-merge', 'org/project1').changes
3815 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003816
James E. Blair5ee24252014-12-30 10:12:29 -08003817 def test_crd_cycle(self):
3818 "Test cross-repo dependency cycles"
3819 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3820 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003821 A.addApproval('code-review', 2)
3822 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003823
3824 # A -> B -> A (via commit-depends)
3825
3826 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3827 A.subject, B.data['id'])
3828 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3829 B.subject, A.data['id'])
3830
James E. Blair8b5408c2016-08-08 15:37:46 -07003831 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003832 self.waitUntilSettled()
3833
3834 self.assertEqual(A.reported, 0)
3835 self.assertEqual(B.reported, 0)
3836 self.assertEqual(A.data['status'], 'NEW')
3837 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003838
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003839 def test_crd_gate_unknown(self):
3840 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003841 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003842 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3843 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003844 A.addApproval('code-review', 2)
3845 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003846
3847 # A Depends-On: B
3848 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3849 A.subject, B.data['id'])
3850
James E. Blair8b5408c2016-08-08 15:37:46 -07003851 B.addApproval('approved', 1)
3852 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003853 self.waitUntilSettled()
3854
3855 # Unknown projects cannot share a queue with any other
3856 # since they don't have common jobs with any other (they have no jobs).
3857 # Changes which depend on unknown project changes
3858 # should not be processed in dependent pipeline
3859 self.assertEqual(A.data['status'], 'NEW')
3860 self.assertEqual(B.data['status'], 'NEW')
3861 self.assertEqual(A.reported, 0)
3862 self.assertEqual(B.reported, 0)
3863 self.assertEqual(len(self.history), 0)
3864
3865 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003866 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003867 B.setMerged()
3868 self.waitUntilSettled()
3869 self.assertEqual(len(self.history), 0)
3870
3871 # Now that B is merged, A should be able to be enqueued and
3872 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003873 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003874 self.waitUntilSettled()
3875
3876 self.assertEqual(A.data['status'], 'MERGED')
3877 self.assertEqual(A.reported, 2)
3878 self.assertEqual(B.data['status'], 'MERGED')
3879 self.assertEqual(B.reported, 0)
3880
Clint Byrum8897dca2017-05-10 19:14:19 -07003881 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blairbfb8e042014-12-30 17:01:44 -08003882 def test_crd_check(self):
3883 "Test cross-repo dependencies in independent pipelines"
3884
Paul Belanger174a8272017-03-14 13:20:10 -04003885 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003886 self.gearman_server.hold_jobs_in_queue = True
3887 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3888 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3889
3890 # A Depends-On: B
3891 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3892 A.subject, B.data['id'])
3893
3894 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3895 self.waitUntilSettled()
3896
James E. Blairbfb8e042014-12-30 17:01:44 -08003897 self.gearman_server.hold_jobs_in_queue = False
3898 self.gearman_server.release()
3899 self.waitUntilSettled()
3900
Paul Belanger174a8272017-03-14 13:20:10 -04003901 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003902 self.waitUntilSettled()
3903
James E. Blair2a535672017-04-27 12:03:15 -07003904 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003905
Paul Belanger174a8272017-03-14 13:20:10 -04003906 self.executor_server.hold_jobs_in_build = False
3907 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003908 self.waitUntilSettled()
3909
James E. Blairbfb8e042014-12-30 17:01:44 -08003910 self.assertEqual(A.data['status'], 'NEW')
3911 self.assertEqual(B.data['status'], 'NEW')
3912 self.assertEqual(A.reported, 1)
3913 self.assertEqual(B.reported, 0)
3914
3915 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003916 tenant = self.sched.abide.tenants.get('tenant-one')
3917 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003918
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003919 def test_crd_check_git_depends(self):
3920 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003921 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003922 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3923 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3924
3925 # Add two git-dependent changes and make sure they both report
3926 # success.
3927 B.setDependsOn(A, 1)
3928 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3929 self.waitUntilSettled()
3930 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3931 self.waitUntilSettled()
3932
James E. Blairb8c16472015-05-05 14:55:26 -07003933 self.orderedRelease()
3934 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003935 self.waitUntilSettled()
3936
3937 self.assertEqual(A.data['status'], 'NEW')
3938 self.assertEqual(B.data['status'], 'NEW')
3939 self.assertEqual(A.reported, 1)
3940 self.assertEqual(B.reported, 1)
3941
3942 self.assertEqual(self.history[0].changes, '1,1')
3943 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003944 tenant = self.sched.abide.tenants.get('tenant-one')
3945 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003946
3947 self.assertIn('Build succeeded', A.messages[0])
3948 self.assertIn('Build succeeded', B.messages[0])
3949
3950 def test_crd_check_duplicate(self):
3951 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003952 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003953 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3954 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003955 tenant = self.sched.abide.tenants.get('tenant-one')
3956 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003957
3958 # Add two git-dependent changes...
3959 B.setDependsOn(A, 1)
3960 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3961 self.waitUntilSettled()
3962 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3963
3964 # ...make sure the live one is not duplicated...
3965 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3966 self.waitUntilSettled()
3967 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3968
3969 # ...but the non-live one is able to be.
3970 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3971 self.waitUntilSettled()
3972 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3973
Clark Boylandd849822015-03-02 12:38:14 -08003974 # Release jobs in order to avoid races with change A jobs
3975 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003976 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003977 self.executor_server.hold_jobs_in_build = False
3978 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003979 self.waitUntilSettled()
3980
3981 self.assertEqual(A.data['status'], 'NEW')
3982 self.assertEqual(B.data['status'], 'NEW')
3983 self.assertEqual(A.reported, 1)
3984 self.assertEqual(B.reported, 1)
3985
3986 self.assertEqual(self.history[0].changes, '1,1 2,1')
3987 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003988 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003989
3990 self.assertIn('Build succeeded', A.messages[0])
3991 self.assertIn('Build succeeded', B.messages[0])
3992
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003993 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003994 "Test cross-repo dependencies re-enqueued in independent pipelines"
3995
3996 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003997 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3998 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003999
4000 # A Depends-On: B
4001 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4002 A.subject, B.data['id'])
4003
4004 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4005 self.waitUntilSettled()
4006
4007 self.sched.reconfigure(self.config)
4008
4009 # Make sure the items still share a change queue, and the
4010 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004011 tenant = self.sched.abide.tenants.get('tenant-one')
4012 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4013 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004014 first_item = queue.queue[0]
4015 for item in queue.queue:
4016 self.assertEqual(item.queue, first_item.queue)
4017 self.assertFalse(first_item.live)
4018 self.assertTrue(queue.queue[1].live)
4019
4020 self.gearman_server.hold_jobs_in_queue = False
4021 self.gearman_server.release()
4022 self.waitUntilSettled()
4023
4024 self.assertEqual(A.data['status'], 'NEW')
4025 self.assertEqual(B.data['status'], 'NEW')
4026 self.assertEqual(A.reported, 1)
4027 self.assertEqual(B.reported, 0)
4028
4029 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004030 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004031
Clint Byrum8897dca2017-05-10 19:14:19 -07004032 @skipIf(sys.version_info.major > 2, 'Fails on py3')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004033 def test_crd_check_reconfiguration(self):
4034 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4035
Clint Byrum8897dca2017-05-10 19:14:19 -07004036 @skipIf(sys.version_info.major > 2, 'Fails on py3')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004037 def test_crd_undefined_project(self):
4038 """Test that undefined projects in dependencies are handled for
4039 independent pipelines"""
4040 # It's a hack for fake gerrit,
4041 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004042 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004043 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4044
Jesse Keatingea68ab42017-04-19 15:03:30 -07004045 @simple_layout('layouts/ignore-dependencies.yaml')
Clint Byrum8897dca2017-05-10 19:14:19 -07004046 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blair17dd6772015-02-09 14:45:18 -08004047 def test_crd_check_ignore_dependencies(self):
4048 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004049
4050 self.gearman_server.hold_jobs_in_queue = True
4051 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4052 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4053 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4054
4055 # A Depends-On: B
4056 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4057 A.subject, B.data['id'])
4058 # C git-depends on B
4059 C.setDependsOn(B, 1)
4060 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4061 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4062 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4063 self.waitUntilSettled()
4064
4065 # Make sure none of the items share a change queue, and all
4066 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004067 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004068 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004069 self.assertEqual(len(check_pipeline.queues), 3)
4070 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4071 for item in check_pipeline.getAllItems():
4072 self.assertTrue(item.live)
4073
4074 self.gearman_server.hold_jobs_in_queue = False
4075 self.gearman_server.release()
4076 self.waitUntilSettled()
4077
4078 self.assertEqual(A.data['status'], 'NEW')
4079 self.assertEqual(B.data['status'], 'NEW')
4080 self.assertEqual(C.data['status'], 'NEW')
4081 self.assertEqual(A.reported, 1)
4082 self.assertEqual(B.reported, 1)
4083 self.assertEqual(C.reported, 1)
4084
4085 # Each job should have tested exactly one change
4086 for job in self.history:
4087 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004088
James E. Blair7cb84542017-04-19 13:35:05 -07004089 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004090 def test_crd_check_transitive(self):
4091 "Test transitive cross-repo dependencies"
4092 # Specifically, if A -> B -> C, and C gets a new patchset and
4093 # A gets a new patchset, ensure the test of A,2 includes B,1
4094 # and C,2 (not C,1 which would indicate stale data in the
4095 # cache for B).
4096 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4097 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4098 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4099
4100 # A Depends-On: B
4101 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4102 A.subject, B.data['id'])
4103
4104 # B Depends-On: C
4105 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4106 B.subject, C.data['id'])
4107
4108 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4109 self.waitUntilSettled()
4110 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4111
4112 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4113 self.waitUntilSettled()
4114 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4115
4116 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4117 self.waitUntilSettled()
4118 self.assertEqual(self.history[-1].changes, '3,1')
4119
4120 C.addPatchset()
4121 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4122 self.waitUntilSettled()
4123 self.assertEqual(self.history[-1].changes, '3,2')
4124
4125 A.addPatchset()
4126 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4127 self.waitUntilSettled()
4128 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004129
James E. Blaira8b90b32016-08-24 15:18:50 -07004130 def test_crd_check_unknown(self):
4131 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004132 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004133 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4134 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4135 # A Depends-On: B
4136 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4137 A.subject, B.data['id'])
4138
4139 # Make sure zuul has seen an event on B.
4140 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4141 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4142 self.waitUntilSettled()
4143
4144 self.assertEqual(A.data['status'], 'NEW')
4145 self.assertEqual(A.reported, 1)
4146 self.assertEqual(B.data['status'], 'NEW')
4147 self.assertEqual(B.reported, 0)
4148
James E. Blair92464a22016-04-05 10:21:26 -07004149 def test_crd_cycle_join(self):
4150 "Test an updated change creates a cycle"
4151 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4152
4153 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4154 self.waitUntilSettled()
4155
4156 # Create B->A
4157 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4158 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4159 B.subject, A.data['id'])
4160 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4161 self.waitUntilSettled()
4162
4163 # Update A to add A->B (a cycle).
4164 A.addPatchset()
4165 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4166 A.subject, B.data['id'])
4167 # Normally we would submit the patchset-created event for
4168 # processing here, however, we have no way of noting whether
4169 # the dependency cycle detection correctly raised an
4170 # exception, so instead, we reach into the source driver and
4171 # call the method that would ultimately be called by the event
4172 # processing.
4173
Paul Belanger0e155e22016-11-14 14:12:23 -05004174 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004175 (trusted, project) = tenant.getProject('org/project')
4176 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004177
4178 # TODO(pabelanger): As we add more source / trigger APIs we should make
4179 # it easier for users to create events for testing.
4180 event = zuul.model.TriggerEvent()
4181 event.trigger_name = 'gerrit'
4182 event.change_number = '1'
4183 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004184 with testtools.ExpectedException(
4185 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004186 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004187 self.log.debug("Got expected dependency cycle exception")
4188
4189 # Now if we update B to remove the depends-on, everything
4190 # should be okay. B; A->B
4191
4192 B.addPatchset()
4193 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004194
4195 source.getChange(event, True)
4196 event.change_number = '2'
4197 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004198
Jesse Keatingcc67b122017-04-19 14:54:57 -07004199 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004200 def test_disable_at(self):
4201 "Test a pipeline will only report to the disabled trigger when failing"
4202
Jesse Keatingcc67b122017-04-19 14:54:57 -07004203 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004204 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004205 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004206 0, tenant.layout.pipelines['check']._consecutive_failures)
4207 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004208
4209 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4210 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4211 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4212 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4213 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4214 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4215 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4216 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4217 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4218 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4219 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4220
Paul Belanger174a8272017-03-14 13:20:10 -04004221 self.executor_server.failJob('project-test1', A)
4222 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004223 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004224 self.executor_server.failJob('project-test1', D)
4225 self.executor_server.failJob('project-test1', E)
4226 self.executor_server.failJob('project-test1', F)
4227 self.executor_server.failJob('project-test1', G)
4228 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004229 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004230 self.executor_server.failJob('project-test1', J)
4231 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004232
4233 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4234 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4235 self.waitUntilSettled()
4236
4237 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004238 2, tenant.layout.pipelines['check']._consecutive_failures)
4239 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004240
4241 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4242 self.waitUntilSettled()
4243
4244 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004245 0, tenant.layout.pipelines['check']._consecutive_failures)
4246 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004247
4248 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4249 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4250 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4251 self.waitUntilSettled()
4252
4253 # We should be disabled now
4254 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004255 3, tenant.layout.pipelines['check']._consecutive_failures)
4256 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004257
4258 # We need to wait between each of these patches to make sure the
4259 # smtp messages come back in an expected order
4260 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4261 self.waitUntilSettled()
4262 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4263 self.waitUntilSettled()
4264 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4265 self.waitUntilSettled()
4266
4267 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4268 # leaving a message on each change
4269 self.assertEqual(1, len(A.messages))
4270 self.assertIn('Build failed.', A.messages[0])
4271 self.assertEqual(1, len(B.messages))
4272 self.assertIn('Build failed.', B.messages[0])
4273 self.assertEqual(1, len(C.messages))
4274 self.assertIn('Build succeeded.', C.messages[0])
4275 self.assertEqual(1, len(D.messages))
4276 self.assertIn('Build failed.', D.messages[0])
4277 self.assertEqual(1, len(E.messages))
4278 self.assertIn('Build failed.', E.messages[0])
4279 self.assertEqual(1, len(F.messages))
4280 self.assertIn('Build failed.', F.messages[0])
4281
4282 # The last 3 (GHI) would have only reported via smtp.
4283 self.assertEqual(3, len(self.smtp_messages))
4284 self.assertEqual(0, len(G.messages))
4285 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004286 self.assertIn(
4287 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004288 self.assertEqual(0, len(H.messages))
4289 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004290 self.assertIn(
4291 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004292 self.assertEqual(0, len(I.messages))
4293 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004294 self.assertIn(
4295 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004296
4297 # Now reload the configuration (simulate a HUP) to check the pipeline
4298 # comes out of disabled
4299 self.sched.reconfigure(self.config)
4300
Jesse Keatingcc67b122017-04-19 14:54:57 -07004301 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004302
4303 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004304 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004305 0, tenant.layout.pipelines['check']._consecutive_failures)
4306 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004307
4308 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4309 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4310 self.waitUntilSettled()
4311
4312 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004313 2, tenant.layout.pipelines['check']._consecutive_failures)
4314 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004315
4316 # J and K went back to gerrit
4317 self.assertEqual(1, len(J.messages))
4318 self.assertIn('Build failed.', J.messages[0])
4319 self.assertEqual(1, len(K.messages))
4320 self.assertIn('Build failed.', K.messages[0])
4321 # No more messages reported via smtp
4322 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004323
James E. Blaircc020532017-04-19 13:04:44 -07004324 @simple_layout('layouts/one-job-project.yaml')
4325 def test_one_job_project(self):
4326 "Test that queueing works with one job"
4327 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4328 'master', 'A')
4329 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4330 'master', 'B')
4331 A.addApproval('code-review', 2)
4332 B.addApproval('code-review', 2)
4333 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4334 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4335 self.waitUntilSettled()
4336
4337 self.assertEqual(A.data['status'], 'MERGED')
4338 self.assertEqual(A.reported, 2)
4339 self.assertEqual(B.data['status'], 'MERGED')
4340 self.assertEqual(B.reported, 2)
4341
Paul Belanger71d98172016-11-08 10:56:31 -05004342 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004343 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004344
Paul Belanger174a8272017-03-14 13:20:10 -04004345 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004346 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4347 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4348 self.waitUntilSettled()
4349
Paul Belanger174a8272017-03-14 13:20:10 -04004350 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004351 self.waitUntilSettled()
4352
4353 self.assertEqual(len(self.builds), 2)
4354 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004355 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004356 self.waitUntilSettled()
4357
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004358 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004359 self.assertEqual(len(self.builds), 1,
4360 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004361 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004362 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004363 self.waitUntilSettled()
4364
Paul Belanger174a8272017-03-14 13:20:10 -04004365 self.executor_server.hold_jobs_in_build = False
4366 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004367 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004368 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004369 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4370 self.assertEqual(A.reported, 1)
4371 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004372
James E. Blair15be0e12017-01-03 13:45:20 -08004373 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004374 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004375
4376 self.fake_nodepool.paused = True
4377 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4378 A.addApproval('code-review', 2)
4379 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4380 self.waitUntilSettled()
4381
4382 self.zk.client.stop()
4383 self.zk.client.start()
4384 self.fake_nodepool.paused = False
4385 self.waitUntilSettled()
4386
4387 self.assertEqual(A.data['status'], 'MERGED')
4388 self.assertEqual(A.reported, 2)
4389
James E. Blair6ab79e02017-01-06 10:10:17 -08004390 def test_nodepool_failure(self):
4391 "Test that jobs are reported after a nodepool failure"
4392
4393 self.fake_nodepool.paused = True
4394 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4395 A.addApproval('code-review', 2)
4396 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4397 self.waitUntilSettled()
4398
4399 req = self.fake_nodepool.getNodeRequests()[0]
4400 self.fake_nodepool.addFailRequest(req)
4401
4402 self.fake_nodepool.paused = False
4403 self.waitUntilSettled()
4404
4405 self.assertEqual(A.data['status'], 'NEW')
4406 self.assertEqual(A.reported, 2)
4407 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4408 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4409 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4410
Arieb6f068c2016-10-09 13:11:06 +03004411
James E. Blaira002b032017-04-18 10:35:48 -07004412class TestExecutor(ZuulTestCase):
4413 tenant_config_file = 'config/single-tenant/main.yaml'
4414
4415 def assertFinalState(self):
4416 # In this test, we expect to shut down in a non-final state,
4417 # so skip these checks.
4418 pass
4419
4420 def assertCleanShutdown(self):
4421 self.log.debug("Assert clean shutdown")
4422
4423 # After shutdown, make sure no jobs are running
4424 self.assertEqual({}, self.executor_server.job_workers)
4425
4426 # Make sure that git.Repo objects have been garbage collected.
4427 repos = []
4428 gc.collect()
4429 for obj in gc.get_objects():
4430 if isinstance(obj, git.Repo):
4431 self.log.debug("Leaked git repo object: %s" % repr(obj))
4432 repos.append(obj)
4433 self.assertEqual(len(repos), 0)
4434
4435 def test_executor_shutdown(self):
4436 "Test that the executor can shut down with jobs running"
4437
4438 self.executor_server.hold_jobs_in_build = True
4439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4440 A.addApproval('code-review', 2)
4441 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4442 self.waitUntilSettled()
4443
4444
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004445class TestDependencyGraph(ZuulTestCase):
4446 tenant_config_file = 'config/dependency-graph/main.yaml'
4447
4448 def test_dependeny_graph_dispatch_jobs_once(self):
4449 "Test a job in a dependency graph is queued only once"
4450 # Job dependencies, starting with A
4451 # A
4452 # / \
4453 # B C
4454 # / \ / \
4455 # D F E
4456 # |
4457 # G
4458
4459 self.executor_server.hold_jobs_in_build = True
4460 change = self.fake_gerrit.addFakeChange(
4461 'org/project', 'master', 'change')
4462 change.addApproval('code-review', 2)
4463 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4464
4465 self.waitUntilSettled()
4466 self.assertEqual([b.name for b in self.builds], ['A'])
4467
4468 self.executor_server.release('A')
4469 self.waitUntilSettled()
4470 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4471
4472 self.executor_server.release('B')
4473 self.waitUntilSettled()
4474 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4475
4476 self.executor_server.release('D')
4477 self.waitUntilSettled()
4478 self.assertEqual([b.name for b in self.builds], ['C'])
4479
4480 self.executor_server.release('C')
4481 self.waitUntilSettled()
4482 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4483
4484 self.executor_server.release('F')
4485 self.waitUntilSettled()
4486 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4487
4488 self.executor_server.release('G')
4489 self.waitUntilSettled()
4490 self.assertEqual([b.name for b in self.builds], ['E'])
4491
4492 self.executor_server.release('E')
4493 self.waitUntilSettled()
4494 self.assertEqual(len(self.builds), 0)
4495
4496 self.executor_server.hold_jobs_in_build = False
4497 self.executor_server.release()
4498 self.waitUntilSettled()
4499
4500 self.assertEqual(len(self.builds), 0)
4501 self.assertEqual(len(self.history), 7)
4502
4503 self.assertEqual(change.data['status'], 'MERGED')
4504 self.assertEqual(change.reported, 2)
4505
4506 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4507 "Test that a job waits till all dependencies are successful"
4508 # Job dependencies, starting with A
4509 # A
4510 # / \
4511 # B C*
4512 # / \ / \
4513 # D F E
4514 # |
4515 # G
4516
4517 self.executor_server.hold_jobs_in_build = True
4518 change = self.fake_gerrit.addFakeChange(
4519 'org/project', 'master', 'change')
4520 change.addApproval('code-review', 2)
4521
4522 self.executor_server.failJob('C', change)
4523
4524 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4525
4526 self.waitUntilSettled()
4527 self.assertEqual([b.name for b in self.builds], ['A'])
4528
4529 self.executor_server.release('A')
4530 self.waitUntilSettled()
4531 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4532
4533 self.executor_server.release('B')
4534 self.waitUntilSettled()
4535 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4536
4537 self.executor_server.release('D')
4538 self.waitUntilSettled()
4539 self.assertEqual([b.name for b in self.builds], ['C'])
4540
4541 self.executor_server.release('C')
4542 self.waitUntilSettled()
4543 self.assertEqual(len(self.builds), 0)
4544
4545 self.executor_server.hold_jobs_in_build = False
4546 self.executor_server.release()
4547 self.waitUntilSettled()
4548
4549 self.assertEqual(len(self.builds), 0)
4550 self.assertEqual(len(self.history), 4)
4551
4552 self.assertEqual(change.data['status'], 'NEW')
4553 self.assertEqual(change.reported, 2)
4554
4555
Arieb6f068c2016-10-09 13:11:06 +03004556class TestDuplicatePipeline(ZuulTestCase):
4557 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4558
4559 def test_duplicate_pipelines(self):
4560 "Test that a change matching multiple pipelines works"
4561
4562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4563 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4564 self.waitUntilSettled()
4565
4566 self.assertHistory([
4567 dict(name='project-test1', result='SUCCESS', changes='1,1',
4568 pipeline='dup1'),
4569 dict(name='project-test1', result='SUCCESS', changes='1,1',
4570 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004571 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004572
4573 self.assertEqual(len(A.messages), 2)
4574
Paul Belangera46a3742016-11-09 14:23:03 -05004575 if 'dup1' in A.messages[0]:
4576 self.assertIn('dup1', A.messages[0])
4577 self.assertNotIn('dup2', A.messages[0])
4578 self.assertIn('project-test1', A.messages[0])
4579 self.assertIn('dup2', A.messages[1])
4580 self.assertNotIn('dup1', A.messages[1])
4581 self.assertIn('project-test1', A.messages[1])
4582 else:
4583 self.assertIn('dup1', A.messages[1])
4584 self.assertNotIn('dup2', A.messages[1])
4585 self.assertIn('project-test1', A.messages[1])
4586 self.assertIn('dup2', A.messages[0])
4587 self.assertNotIn('dup1', A.messages[0])
4588 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004589
4590
Paul Belanger86085b32016-11-03 12:48:57 -04004591class TestSchedulerTemplatedProject(ZuulTestCase):
4592 tenant_config_file = 'config/templated-project/main.yaml'
4593
Paul Belanger174a8272017-03-14 13:20:10 -04004594 def test_job_from_templates_executed(self):
4595 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004596
4597 A = self.fake_gerrit.addFakeChange(
4598 'org/templated-project', 'master', 'A')
4599 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4600 self.waitUntilSettled()
4601
4602 self.assertEqual(self.getJobFromHistory('project-test1').result,
4603 'SUCCESS')
4604 self.assertEqual(self.getJobFromHistory('project-test2').result,
4605 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004606
4607 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004608 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004609
4610 A = self.fake_gerrit.addFakeChange(
4611 'org/layered-project', 'master', 'A')
4612 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4613 self.waitUntilSettled()
4614
4615 self.assertEqual(self.getJobFromHistory('project-test1').result,
4616 'SUCCESS')
4617 self.assertEqual(self.getJobFromHistory('project-test2').result,
4618 'SUCCESS')
4619 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4620 ).result, 'SUCCESS')
4621 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4622 ).result, 'SUCCESS')
4623 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4624 ).result, 'SUCCESS')
4625 self.assertEqual(self.getJobFromHistory('project-test6').result,
4626 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004627
4628
4629class TestSchedulerSuccessURL(ZuulTestCase):
4630 tenant_config_file = 'config/success-url/main.yaml'
4631
4632 def test_success_url(self):
4633 "Ensure bad build params are ignored"
4634 self.sched.reconfigure(self.config)
4635 self.init_repo('org/docs')
4636
4637 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4638 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4639 self.waitUntilSettled()
4640
4641 # Both builds ran: docs-draft-test + docs-draft-test2
4642 self.assertEqual(len(self.history), 2)
4643
4644 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004645 for build in self.history:
4646 if build.name == 'docs-draft-test':
4647 uuid = build.uuid[:7]
4648 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004649
4650 # Two msgs: 'Starting...' + results
4651 self.assertEqual(len(self.smtp_messages), 2)
4652 body = self.smtp_messages[1]['body'].splitlines()
4653 self.assertEqual('Build succeeded.', body[0])
4654
4655 self.assertIn(
4656 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4657 'docs-draft-test/%s/publish-docs/' % uuid,
4658 body[2])
4659
Paul Belanger174a8272017-03-14 13:20:10 -04004660 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004661 self.assertIn(
4662 '- docs-draft-test2 https://server/job',
4663 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004664
4665
Adam Gandelman4da00f62016-12-09 15:47:33 -08004666class TestSchedulerMerges(ZuulTestCase):
4667 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004668
4669 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004670 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004671 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004672 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004673 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4674 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4675 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4676 A.addApproval('code-review', 2)
4677 B.addApproval('code-review', 2)
4678 C.addApproval('code-review', 2)
4679 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4680 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4681 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4682 self.waitUntilSettled()
4683
4684 build = self.builds[-1]
4685 ref = self.getParameter(build, 'ZUUL_REF')
4686
James E. Blair2a535672017-04-27 12:03:15 -07004687 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4688 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004689 repo = git.Repo(path)
4690 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4691 repo_messages.reverse()
4692
Paul Belanger174a8272017-03-14 13:20:10 -04004693 self.executor_server.hold_jobs_in_build = False
4694 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004695 self.waitUntilSettled()
4696
4697 return repo_messages
4698
4699 def _test_merge(self, mode):
4700 us_path = os.path.join(
4701 self.upstream_root, 'org/project-%s' % mode)
4702 expected_messages = [
4703 'initial commit',
4704 'add content from fixture',
4705 # the intermediate commits order is nondeterministic
4706 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4707 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4708 ]
4709 result = self._test_project_merge_mode(mode)
4710 self.assertEqual(result[:2], expected_messages[:2])
4711 self.assertEqual(result[-2:], expected_messages[-2:])
4712
4713 def test_project_merge_mode_merge(self):
4714 self._test_merge('merge')
4715
4716 def test_project_merge_mode_merge_resolve(self):
4717 self._test_merge('merge-resolve')
4718
4719 def test_project_merge_mode_cherrypick(self):
4720 expected_messages = [
4721 'initial commit',
4722 'add content from fixture',
4723 'A-1',
4724 'B-1',
4725 'C-1']
4726 result = self._test_project_merge_mode('cherry-pick')
4727 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004728
4729 def test_merge_branch(self):
4730 "Test that the right commits are on alternate branches"
4731 self.create_branch('org/project-merge-branches', 'mp')
4732
Paul Belanger174a8272017-03-14 13:20:10 -04004733 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004734 A = self.fake_gerrit.addFakeChange(
4735 'org/project-merge-branches', 'mp', 'A')
4736 B = self.fake_gerrit.addFakeChange(
4737 'org/project-merge-branches', 'mp', 'B')
4738 C = self.fake_gerrit.addFakeChange(
4739 'org/project-merge-branches', 'mp', 'C')
4740 A.addApproval('code-review', 2)
4741 B.addApproval('code-review', 2)
4742 C.addApproval('code-review', 2)
4743 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4744 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4745 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4746 self.waitUntilSettled()
4747
Paul Belanger174a8272017-03-14 13:20:10 -04004748 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004749 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004750 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004751 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004752 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004753 self.waitUntilSettled()
4754
4755 build = self.builds[-1]
4756 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4757 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004758 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4759 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004760 repo = git.Repo(path)
4761
4762 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4763 repo_messages.reverse()
4764 correct_messages = [
4765 'initial commit',
4766 'add content from fixture',
4767 'mp commit',
4768 'A-1', 'B-1', 'C-1']
4769 self.assertEqual(repo_messages, correct_messages)
4770
Paul Belanger174a8272017-03-14 13:20:10 -04004771 self.executor_server.hold_jobs_in_build = False
4772 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004773 self.waitUntilSettled()
4774
4775 def test_merge_multi_branch(self):
4776 "Test that dependent changes on multiple branches are merged"
4777 self.create_branch('org/project-merge-branches', 'mp')
4778
Paul Belanger174a8272017-03-14 13:20:10 -04004779 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004780 A = self.fake_gerrit.addFakeChange(
4781 'org/project-merge-branches', 'master', 'A')
4782 B = self.fake_gerrit.addFakeChange(
4783 'org/project-merge-branches', 'mp', 'B')
4784 C = self.fake_gerrit.addFakeChange(
4785 'org/project-merge-branches', 'master', 'C')
4786 A.addApproval('code-review', 2)
4787 B.addApproval('code-review', 2)
4788 C.addApproval('code-review', 2)
4789 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4790 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4791 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4792 self.waitUntilSettled()
4793
4794 job_A = None
4795 for job in self.builds:
4796 if 'project-merge' in job.name:
4797 job_A = job
4798 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4799 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4800 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4801 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4802
James E. Blair2a535672017-04-27 12:03:15 -07004803 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4804 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004805 repo = git.Repo(path)
4806 repo_messages = [c.message.strip()
4807 for c in repo.iter_commits(ref_A)]
4808 repo_messages.reverse()
4809 correct_messages = [
4810 'initial commit', 'add content from fixture', 'A-1']
4811 self.assertEqual(repo_messages, correct_messages)
4812
Paul Belanger174a8272017-03-14 13:20:10 -04004813 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004814 self.waitUntilSettled()
4815
4816 job_B = None
4817 for job in self.builds:
4818 if 'project-merge' in job.name:
4819 job_B = job
4820 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4821 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4822 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4823 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4824
James E. Blair2a535672017-04-27 12:03:15 -07004825 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4826 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004827 repo = git.Repo(path)
4828 repo_messages = [c.message.strip()
4829 for c in repo.iter_commits(ref_B)]
4830 repo_messages.reverse()
4831 correct_messages = [
4832 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4833 self.assertEqual(repo_messages, correct_messages)
4834
Paul Belanger174a8272017-03-14 13:20:10 -04004835 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004836 self.waitUntilSettled()
4837
4838 job_C = None
4839 for job in self.builds:
4840 if 'project-merge' in job.name:
4841 job_C = job
4842 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4843 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4844 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4845 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004846 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4847 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004848 repo = git.Repo(path)
4849 repo_messages = [c.message.strip()
4850 for c in repo.iter_commits(ref_C)]
4851
4852 repo_messages.reverse()
4853 correct_messages = [
4854 'initial commit', 'add content from fixture',
4855 'A-1', 'C-1']
4856 # Ensure the right commits are in the history for this ref
4857 self.assertEqual(repo_messages, correct_messages)
4858
Paul Belanger174a8272017-03-14 13:20:10 -04004859 self.executor_server.hold_jobs_in_build = False
4860 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004861 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004862
4863
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004864class TestSemaphore(ZuulTestCase):
4865 tenant_config_file = 'config/semaphore/main.yaml'
4866
4867 def test_semaphore_one(self):
4868 "Test semaphores with max=1 (mutex)"
4869 tenant = self.sched.abide.tenants.get('tenant-one')
4870
4871 self.executor_server.hold_jobs_in_build = True
4872
4873 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4874 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4875 self.assertFalse('test-semaphore' in
4876 tenant.semaphore_handler.semaphores)
4877
4878 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4879 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4880 self.waitUntilSettled()
4881
4882 self.assertEqual(len(self.builds), 3)
4883 self.assertEqual(self.builds[0].name, 'project-test1')
4884 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4885 self.assertEqual(self.builds[2].name, 'project-test1')
4886
4887 self.executor_server.release('semaphore-one-test1')
4888 self.waitUntilSettled()
4889
4890 self.assertEqual(len(self.builds), 3)
4891 self.assertEqual(self.builds[0].name, 'project-test1')
4892 self.assertEqual(self.builds[1].name, 'project-test1')
4893 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4894 self.assertTrue('test-semaphore' in
4895 tenant.semaphore_handler.semaphores)
4896
4897 self.executor_server.release('semaphore-one-test2')
4898 self.waitUntilSettled()
4899
4900 self.assertEqual(len(self.builds), 3)
4901 self.assertEqual(self.builds[0].name, 'project-test1')
4902 self.assertEqual(self.builds[1].name, 'project-test1')
4903 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4904 self.assertTrue('test-semaphore' in
4905 tenant.semaphore_handler.semaphores)
4906
4907 self.executor_server.release('semaphore-one-test1')
4908 self.waitUntilSettled()
4909
4910 self.assertEqual(len(self.builds), 3)
4911 self.assertEqual(self.builds[0].name, 'project-test1')
4912 self.assertEqual(self.builds[1].name, 'project-test1')
4913 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4914 self.assertTrue('test-semaphore' in
4915 tenant.semaphore_handler.semaphores)
4916
4917 self.executor_server.release('semaphore-one-test2')
4918 self.waitUntilSettled()
4919
4920 self.assertEqual(len(self.builds), 2)
4921 self.assertEqual(self.builds[0].name, 'project-test1')
4922 self.assertEqual(self.builds[1].name, 'project-test1')
4923 self.assertFalse('test-semaphore' in
4924 tenant.semaphore_handler.semaphores)
4925
4926 self.executor_server.hold_jobs_in_build = False
4927 self.executor_server.release()
4928
4929 self.waitUntilSettled()
4930 self.assertEqual(len(self.builds), 0)
4931
4932 self.assertEqual(A.reported, 1)
4933 self.assertEqual(B.reported, 1)
4934 self.assertFalse('test-semaphore' in
4935 tenant.semaphore_handler.semaphores)
4936
4937 def test_semaphore_two(self):
4938 "Test semaphores with max>1"
4939 tenant = self.sched.abide.tenants.get('tenant-one')
4940
4941 self.executor_server.hold_jobs_in_build = True
4942 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4943 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4944 self.assertFalse('test-semaphore-two' in
4945 tenant.semaphore_handler.semaphores)
4946
4947 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4948 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4949 self.waitUntilSettled()
4950
4951 self.assertEqual(len(self.builds), 4)
4952 self.assertEqual(self.builds[0].name, 'project-test1')
4953 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4954 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4955 self.assertEqual(self.builds[3].name, 'project-test1')
4956 self.assertTrue('test-semaphore-two' in
4957 tenant.semaphore_handler.semaphores)
4958 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4959 'test-semaphore-two', [])), 2)
4960
4961 self.executor_server.release('semaphore-two-test1')
4962 self.waitUntilSettled()
4963
4964 self.assertEqual(len(self.builds), 4)
4965 self.assertEqual(self.builds[0].name, 'project-test1')
4966 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4967 self.assertEqual(self.builds[2].name, 'project-test1')
4968 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4969 self.assertTrue('test-semaphore-two' in
4970 tenant.semaphore_handler.semaphores)
4971 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4972 'test-semaphore-two', [])), 2)
4973
4974 self.executor_server.release('semaphore-two-test2')
4975 self.waitUntilSettled()
4976
4977 self.assertEqual(len(self.builds), 4)
4978 self.assertEqual(self.builds[0].name, 'project-test1')
4979 self.assertEqual(self.builds[1].name, 'project-test1')
4980 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4981 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4982 self.assertTrue('test-semaphore-two' in
4983 tenant.semaphore_handler.semaphores)
4984 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4985 'test-semaphore-two', [])), 2)
4986
4987 self.executor_server.release('semaphore-two-test1')
4988 self.waitUntilSettled()
4989
4990 self.assertEqual(len(self.builds), 3)
4991 self.assertEqual(self.builds[0].name, 'project-test1')
4992 self.assertEqual(self.builds[1].name, 'project-test1')
4993 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4994 self.assertTrue('test-semaphore-two' in
4995 tenant.semaphore_handler.semaphores)
4996 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4997 'test-semaphore-two', [])), 1)
4998
4999 self.executor_server.release('semaphore-two-test2')
5000 self.waitUntilSettled()
5001
5002 self.assertEqual(len(self.builds), 2)
5003 self.assertEqual(self.builds[0].name, 'project-test1')
5004 self.assertEqual(self.builds[1].name, 'project-test1')
5005 self.assertFalse('test-semaphore-two' in
5006 tenant.semaphore_handler.semaphores)
5007
5008 self.executor_server.hold_jobs_in_build = False
5009 self.executor_server.release()
5010
5011 self.waitUntilSettled()
5012 self.assertEqual(len(self.builds), 0)
5013
5014 self.assertEqual(A.reported, 1)
5015 self.assertEqual(B.reported, 1)
5016
5017 def test_semaphore_abandon(self):
5018 "Test abandon with job semaphores"
5019 self.executor_server.hold_jobs_in_build = True
5020 tenant = self.sched.abide.tenants.get('tenant-one')
5021 check_pipeline = tenant.layout.pipelines['check']
5022
5023 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5024 self.assertFalse('test-semaphore' in
5025 tenant.semaphore_handler.semaphores)
5026
5027 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5028 self.waitUntilSettled()
5029
5030 self.assertTrue('test-semaphore' in
5031 tenant.semaphore_handler.semaphores)
5032
5033 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5034 self.waitUntilSettled()
5035
5036 # The check pipeline should be empty
5037 items = check_pipeline.getAllItems()
5038 self.assertEqual(len(items), 0)
5039
5040 # The semaphore should be released
5041 self.assertFalse('test-semaphore' in
5042 tenant.semaphore_handler.semaphores)
5043
5044 self.executor_server.hold_jobs_in_build = False
5045 self.executor_server.release()
5046 self.waitUntilSettled()
5047
5048 def test_semaphore_reconfigure(self):
5049 "Test reconfigure with job semaphores"
5050 self.executor_server.hold_jobs_in_build = True
5051 tenant = self.sched.abide.tenants.get('tenant-one')
5052 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5053 self.assertFalse('test-semaphore' in
5054 tenant.semaphore_handler.semaphores)
5055
5056 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5057 self.waitUntilSettled()
5058
5059 self.assertTrue('test-semaphore' in
5060 tenant.semaphore_handler.semaphores)
5061
5062 # reconfigure without layout change
5063 self.sched.reconfigure(self.config)
5064 self.waitUntilSettled()
5065 tenant = self.sched.abide.tenants.get('tenant-one')
5066
5067 # semaphore still must be held
5068 self.assertTrue('test-semaphore' in
5069 tenant.semaphore_handler.semaphores)
5070
5071 self.commitConfigUpdate(
5072 'common-config',
5073 'config/semaphore/zuul-reconfiguration.yaml')
5074 self.sched.reconfigure(self.config)
5075 self.waitUntilSettled()
5076 tenant = self.sched.abide.tenants.get('tenant-one')
5077
5078 self.executor_server.release('project-test1')
5079 self.waitUntilSettled()
5080
5081 # There should be no builds anymore
5082 self.assertEqual(len(self.builds), 0)
5083
5084 # The semaphore should be released
5085 self.assertFalse('test-semaphore' in
5086 tenant.semaphore_handler.semaphores)
5087
5088
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005089class TestSemaphoreMultiTenant(ZuulTestCase):
5090 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5091
5092 def test_semaphore_tenant_isolation(self):
5093 "Test semaphores in multiple tenants"
5094
5095 self.waitUntilSettled()
5096 tenant_one = self.sched.abide.tenants.get('tenant-one')
5097 tenant_two = self.sched.abide.tenants.get('tenant-two')
5098
5099 self.executor_server.hold_jobs_in_build = True
5100 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5101 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5102 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5103 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5104 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5105 self.assertFalse('test-semaphore' in
5106 tenant_one.semaphore_handler.semaphores)
5107 self.assertFalse('test-semaphore' in
5108 tenant_two.semaphore_handler.semaphores)
5109
5110 # add patches to project1 of tenant-one
5111 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5112 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5113 self.waitUntilSettled()
5114
5115 # one build of project1-test1 must run
5116 # semaphore of tenant-one must be acquired once
5117 # semaphore of tenant-two must not be acquired
5118 self.assertEqual(len(self.builds), 1)
5119 self.assertEqual(self.builds[0].name, 'project1-test1')
5120 self.assertTrue('test-semaphore' in
5121 tenant_one.semaphore_handler.semaphores)
5122 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5123 'test-semaphore', [])), 1)
5124 self.assertFalse('test-semaphore' in
5125 tenant_two.semaphore_handler.semaphores)
5126
5127 # add patches to project2 of tenant-two
5128 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5129 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5130 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5131 self.waitUntilSettled()
5132
5133 # one build of project1-test1 must run
5134 # two builds of project2-test1 must run
5135 # semaphore of tenant-one must be acquired once
5136 # semaphore of tenant-two must be acquired twice
5137 self.assertEqual(len(self.builds), 3)
5138 self.assertEqual(self.builds[0].name, 'project1-test1')
5139 self.assertEqual(self.builds[1].name, 'project2-test1')
5140 self.assertEqual(self.builds[2].name, 'project2-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.assertTrue('test-semaphore' in
5146 tenant_two.semaphore_handler.semaphores)
5147 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5148 'test-semaphore', [])), 2)
5149
5150 self.executor_server.release('project1-test1')
5151 self.waitUntilSettled()
5152
5153 # one build of project1-test1 must run
5154 # two builds of project2-test1 must run
5155 # semaphore of tenant-one must be acquired once
5156 # semaphore of tenant-two must be acquired twice
5157 self.assertEqual(len(self.builds), 3)
5158 self.assertEqual(self.builds[0].name, 'project2-test1')
5159 self.assertEqual(self.builds[1].name, 'project2-test1')
5160 self.assertEqual(self.builds[2].name, 'project1-test1')
5161 self.assertTrue('test-semaphore' in
5162 tenant_one.semaphore_handler.semaphores)
5163 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5164 'test-semaphore', [])), 1)
5165 self.assertTrue('test-semaphore' in
5166 tenant_two.semaphore_handler.semaphores)
5167 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5168 'test-semaphore', [])), 2)
5169
5170 self.executor_server.release('project2-test1')
5171 self.waitUntilSettled()
5172
5173 # one build of project1-test1 must run
5174 # one build of project2-test1 must run
5175 # semaphore of tenant-one must be acquired once
5176 # semaphore of tenant-two must be acquired once
5177 self.assertEqual(len(self.builds), 2)
5178 self.assertTrue('test-semaphore' in
5179 tenant_one.semaphore_handler.semaphores)
5180 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5181 'test-semaphore', [])), 1)
5182 self.assertTrue('test-semaphore' in
5183 tenant_two.semaphore_handler.semaphores)
5184 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5185 'test-semaphore', [])), 1)
5186
5187 self.executor_server.hold_jobs_in_build = False
5188 self.executor_server.release()
5189
5190 self.waitUntilSettled()
5191
5192 # no build must run
5193 # semaphore of tenant-one must not be acquired
5194 # semaphore of tenant-two must not be acquired
5195 self.assertEqual(len(self.builds), 0)
5196 self.assertFalse('test-semaphore' in
5197 tenant_one.semaphore_handler.semaphores)
5198 self.assertFalse('test-semaphore' in
5199 tenant_two.semaphore_handler.semaphores)
5200
5201 self.assertEqual(A.reported, 1)
5202 self.assertEqual(B.reported, 1)
5203
5204
5205class TestSemaphoreInRepo(ZuulTestCase):
5206 tenant_config_file = 'config/in-repo/main.yaml'
5207
5208 def test_semaphore_in_repo(self):
5209 "Test semaphores in repo config"
5210
5211 # This tests dynamic semaphore handling in project repos. The semaphore
5212 # max value should not be evaluated dynamically but must be updated
5213 # after the change lands.
5214
5215 self.waitUntilSettled()
5216 tenant = self.sched.abide.tenants.get('tenant-one')
5217
5218 in_repo_conf = textwrap.dedent(
5219 """
5220 - job:
5221 name: project-test2
5222 semaphore: test-semaphore
5223
5224 - project:
5225 name: org/project
5226 tenant-one-gate:
5227 jobs:
5228 - project-test2
5229
5230 # the max value in dynamic layout must be ignored
5231 - semaphore:
5232 name: test-semaphore
5233 max: 2
5234 """)
5235
5236 in_repo_playbook = textwrap.dedent(
5237 """
5238 - hosts: all
5239 tasks: []
5240 """)
5241
5242 file_dict = {'.zuul.yaml': in_repo_conf,
5243 'playbooks/project-test2.yaml': in_repo_playbook}
5244 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5245 files=file_dict)
5246 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5247 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5248 B.setDependsOn(A, 1)
5249 C.setDependsOn(A, 1)
5250
5251 self.executor_server.hold_jobs_in_build = True
5252
5253 A.addApproval('code-review', 2)
5254 B.addApproval('code-review', 2)
5255 C.addApproval('code-review', 2)
5256 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5257 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5258 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5259 self.waitUntilSettled()
5260
5261 # check that the layout in a queue item still has max value of 1
5262 # for test-semaphore
5263 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5264 queue = None
5265 for queue_candidate in pipeline.queues:
5266 if queue_candidate.name == 'org/project':
5267 queue = queue_candidate
5268 break
5269 queue_item = queue.queue[0]
5270 item_dynamic_layout = queue_item.current_build_set.layout
5271 dynamic_test_semaphore = \
5272 item_dynamic_layout.semaphores.get('test-semaphore')
5273 self.assertEqual(dynamic_test_semaphore.max, 1)
5274
5275 # one build must be in queue, one semaphores acquired
5276 self.assertEqual(len(self.builds), 1)
5277 self.assertEqual(self.builds[0].name, 'project-test2')
5278 self.assertTrue('test-semaphore' in
5279 tenant.semaphore_handler.semaphores)
5280 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5281 'test-semaphore', [])), 1)
5282
5283 self.executor_server.release('project-test2')
5284 self.waitUntilSettled()
5285
5286 # change A must be merged
5287 self.assertEqual(A.data['status'], 'MERGED')
5288 self.assertEqual(A.reported, 2)
5289
5290 # send change-merged event as the gerrit mock doesn't send it
5291 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5292 self.waitUntilSettled()
5293
5294 # now that change A was merged, the new semaphore max must be effective
5295 tenant = self.sched.abide.tenants.get('tenant-one')
5296 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5297
5298 # two builds must be in queue, two semaphores acquired
5299 self.assertEqual(len(self.builds), 2)
5300 self.assertEqual(self.builds[0].name, 'project-test2')
5301 self.assertEqual(self.builds[1].name, 'project-test2')
5302 self.assertTrue('test-semaphore' in
5303 tenant.semaphore_handler.semaphores)
5304 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5305 'test-semaphore', [])), 2)
5306
5307 self.executor_server.release('project-test2')
5308 self.waitUntilSettled()
5309
5310 self.assertEqual(len(self.builds), 0)
5311 self.assertFalse('test-semaphore' in
5312 tenant.semaphore_handler.semaphores)
5313
5314 self.executor_server.hold_jobs_in_build = False
5315 self.executor_server.release()
5316
5317 self.waitUntilSettled()
5318 self.assertEqual(len(self.builds), 0)
5319
5320 self.assertEqual(A.reported, 2)
5321 self.assertEqual(B.reported, 2)
5322 self.assertEqual(C.reported, 2)