blob: fa7fa528ba3a279d3dbcdf010012965be766e32c [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. Blairb0fcae42012-07-17 11:12:10 -070017import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010018import textwrap
19
Monty Taylorbc758832013-06-17 17:22:42 -040020import os
James E. Blairb0fcae42012-07-17 11:12:10 -070021import re
James E. Blair4886cc12012-07-18 15:39:41 -070022import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040023import time
James E. Blairec056492016-07-22 09:45:56 -070024from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040025
James E. Blair4886cc12012-07-18 15:39:41 -070026import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070027from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070029
Maru Newby3fe5f852015-01-13 04:22:14 +000030import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070031import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080032import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040033import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070034
Maru Newby3fe5f852015-01-13 04:22:14 +000035from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000036 ZuulTestCase,
37 repack_repo,
38)
James E. Blairb0fcae42012-07-17 11:12:10 -070039
James E. Blairb0fcae42012-07-17 11:12:10 -070040
Clark Boylanb640e052014-04-03 16:41:46 -070041class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070042 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010043
Paul Belanger174a8272017-03-14 13:20:10 -040044 def test_jobs_executed(self):
45 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070046
James E. Blairb0fcae42012-07-17 11:12:10 -070047 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070048 A.addApproval('code-review', 2)
49 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070050 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040051 self.assertEqual(self.getJobFromHistory('project-merge').result,
52 'SUCCESS')
53 self.assertEqual(self.getJobFromHistory('project-test1').result,
54 'SUCCESS')
55 self.assertEqual(self.getJobFromHistory('project-test2').result,
56 'SUCCESS')
57 self.assertEqual(A.data['status'], 'MERGED')
58 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070059 self.assertEqual(self.getJobFromHistory('project-test1').node,
60 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080061 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070062
James E. Blair552b54f2016-07-22 13:55:32 -070063 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070064 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
65 self.assertReportedStat('zuul.pipeline.gate.current_changes',
66 value='1|g')
67 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
68 kind='ms')
69 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
70 value='1|c')
71 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
72 self.assertReportedStat('zuul.pipeline.gate.total_changes',
73 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070074 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070075 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070076 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070077 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070078
James E. Blair5821bd92015-09-16 08:48:15 -070079 for build in self.builds:
80 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
81
James E. Blair3cb10702013-08-24 08:56:03 -070082 def test_initial_pipeline_gauges(self):
83 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070084 self.assertReportedStat('zuul.pipeline.gate.current_changes',
85 value='0|g')
86 self.assertReportedStat('zuul.pipeline.check.current_changes',
87 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070088
James E. Blair34776ee2016-08-25 13:53:54 -070089 def test_job_branch(self):
90 "Test the correct variant of a job runs on a branch"
91 self.create_branch('org/project', 'stable')
92 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
93 A.addApproval('code-review', 2)
94 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
95 self.waitUntilSettled()
96 self.assertEqual(self.getJobFromHistory('project-test1').result,
97 'SUCCESS')
98 self.assertEqual(self.getJobFromHistory('project-test2').result,
99 'SUCCESS')
100 self.assertEqual(A.data['status'], 'MERGED')
101 self.assertEqual(A.reported, 2,
102 "A should report start and success")
103 self.assertIn('gate', A.messages[1],
104 "A should transit gate")
105 self.assertEqual(self.getJobFromHistory('project-test1').node,
106 'image2')
107
James E. Blairb0fcae42012-07-17 11:12:10 -0700108 def test_parallel_changes(self):
109 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700110
Paul Belanger174a8272017-03-14 13:20:10 -0400111 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700112 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
113 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
114 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700115 A.addApproval('code-review', 2)
116 B.addApproval('code-review', 2)
117 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700118
James E. Blair8b5408c2016-08-08 15:37:46 -0700119 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
120 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
121 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700122
123 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400124 self.assertEqual(len(self.builds), 1)
125 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700126 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700127
Paul Belanger174a8272017-03-14 13:20:10 -0400128 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700129 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400130 self.assertEqual(len(self.builds), 3)
131 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700132 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400133 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700134 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400135 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700136 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700137
Paul Belanger174a8272017-03-14 13:20:10 -0400138 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700139 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400140 self.assertEqual(len(self.builds), 5)
141 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700142 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400143 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700144 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700145
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400146 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700147 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400148 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700150
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400151 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700152 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700153
Paul Belanger174a8272017-03-14 13:20:10 -0400154 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700155 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400156 self.assertEqual(len(self.builds), 6)
157 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700158 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400159 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700160 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700161
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400162 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700166
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400167 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700168 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400169 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700171
Paul Belanger174a8272017-03-14 13:20:10 -0400172 self.executor_server.hold_jobs_in_build = False
173 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700174 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400175 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700176
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(len(self.history), 9)
178 self.assertEqual(A.data['status'], 'MERGED')
179 self.assertEqual(B.data['status'], 'MERGED')
180 self.assertEqual(C.data['status'], 'MERGED')
181 self.assertEqual(A.reported, 2)
182 self.assertEqual(B.reported, 2)
183 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700184
185 def test_failed_changes(self):
186 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400187 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700188
James E. Blairb02a3bb2012-07-30 17:49:55 -0700189 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
190 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700191 A.addApproval('code-review', 2)
192 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700193
Paul Belanger174a8272017-03-14 13:20:10 -0400194 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700195
James E. Blair8b5408c2016-08-08 15:37:46 -0700196 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
197 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700198 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700199 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400200
Paul Belanger174a8272017-03-14 13:20:10 -0400201 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400202 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700203 # A/project-merge is complete
204 self.assertBuilds([
205 dict(name='project-test1', changes='1,1'),
206 dict(name='project-test2', changes='1,1'),
207 dict(name='project-merge', changes='1,1 2,1'),
208 ])
James E. Blaire2819012013-06-28 17:17:26 -0400209
Paul Belanger174a8272017-03-14 13:20:10 -0400210 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400211 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700212 # A/project-merge is complete
213 # B/project-merge is complete
214 self.assertBuilds([
215 dict(name='project-test1', changes='1,1'),
216 dict(name='project-test2', changes='1,1'),
217 dict(name='project-test1', changes='1,1 2,1'),
218 dict(name='project-test2', changes='1,1 2,1'),
219 ])
220
221 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400222 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700223 self.builds[0].release()
224 self.waitUntilSettled()
225
226 self.orderedRelease()
227 self.assertHistory([
228 dict(name='project-merge', result='SUCCESS', changes='1,1'),
229 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
230 dict(name='project-test1', result='FAILURE', changes='1,1'),
231 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
232 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
233 dict(name='project-test2', result='SUCCESS', changes='1,1'),
234 dict(name='project-merge', result='SUCCESS', changes='2,1'),
235 dict(name='project-test1', result='SUCCESS', changes='2,1'),
236 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700237 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700238
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400239 self.assertEqual(A.data['status'], 'NEW')
240 self.assertEqual(B.data['status'], 'MERGED')
241 self.assertEqual(A.reported, 2)
242 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700243
244 def test_independent_queues(self):
245 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700246
Paul Belanger174a8272017-03-14 13:20:10 -0400247 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700249 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
250 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700251 A.addApproval('code-review', 2)
252 B.addApproval('code-review', 2)
253 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700254
James E. Blair8b5408c2016-08-08 15:37:46 -0700255 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800256 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700257
James E. Blairf59f3cf2017-02-19 14:50:26 -0800258 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
259 self.waitUntilSettled()
260
261 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700262 self.waitUntilSettled()
263
264 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700265 self.assertBuilds([
266 dict(name='project-merge', changes='1,1'),
267 dict(name='project-merge', changes='2,1'),
268 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700269
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700270 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700271 self.builds[0].release()
272 self.waitUntilSettled()
273 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700274 self.waitUntilSettled()
275 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400276 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700277 self.waitUntilSettled()
278
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700279 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700280 self.assertBuilds([
281 dict(name='project-test1', changes='1,1'),
282 dict(name='project-test2', changes='1,1'),
283 dict(name='project-test1', changes='2,1'),
284 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500285 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700286 dict(name='project-test1', changes='2,1 3,1'),
287 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800288 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700289 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700290
James E. Blair5f0d49e2016-08-19 09:36:50 -0700291 self.orderedRelease()
292 self.assertHistory([
293 dict(name='project-merge', result='SUCCESS', changes='1,1'),
294 dict(name='project-merge', result='SUCCESS', changes='2,1'),
295 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
296 dict(name='project-test1', result='SUCCESS', changes='1,1'),
297 dict(name='project-test2', result='SUCCESS', changes='1,1'),
298 dict(name='project-test1', result='SUCCESS', changes='2,1'),
299 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500300 dict(
301 name='project1-project2-integration',
302 result='SUCCESS',
303 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700304 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
305 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800306 dict(name='project1-project2-integration',
307 result='SUCCESS',
308 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700309 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700310
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400311 self.assertEqual(A.data['status'], 'MERGED')
312 self.assertEqual(B.data['status'], 'MERGED')
313 self.assertEqual(C.data['status'], 'MERGED')
314 self.assertEqual(A.reported, 2)
315 self.assertEqual(B.reported, 2)
316 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700317
318 def test_failed_change_at_head(self):
319 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700320
Paul Belanger174a8272017-03-14 13:20:10 -0400321 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700322 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
323 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
324 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700325 A.addApproval('code-review', 2)
326 B.addApproval('code-review', 2)
327 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700328
Paul Belanger174a8272017-03-14 13:20:10 -0400329 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700330
James E. Blair8b5408c2016-08-08 15:37:46 -0700331 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
332 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
333 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700334
335 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700336
James E. Blairb536ecc2016-08-31 10:11:42 -0700337 self.assertBuilds([
338 dict(name='project-merge', changes='1,1'),
339 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700340
Paul Belanger174a8272017-03-14 13:20:10 -0400341 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700342 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400343 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700344 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400345 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700346 self.waitUntilSettled()
347
James E. Blairb536ecc2016-08-31 10:11:42 -0700348 self.assertBuilds([
349 dict(name='project-test1', changes='1,1'),
350 dict(name='project-test2', changes='1,1'),
351 dict(name='project-test1', changes='1,1 2,1'),
352 dict(name='project-test2', changes='1,1 2,1'),
353 dict(name='project-test1', changes='1,1 2,1 3,1'),
354 dict(name='project-test2', changes='1,1 2,1 3,1'),
355 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700356
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400357 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700358 self.waitUntilSettled()
359
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400360 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700361 self.assertBuilds([
362 dict(name='project-test2', changes='1,1'),
363 dict(name='project-merge', changes='2,1'),
364 ])
365 # Unordered history comparison because the aborts can finish
366 # in any order.
367 self.assertHistory([
368 dict(name='project-merge', result='SUCCESS',
369 changes='1,1'),
370 dict(name='project-merge', result='SUCCESS',
371 changes='1,1 2,1'),
372 dict(name='project-merge', result='SUCCESS',
373 changes='1,1 2,1 3,1'),
374 dict(name='project-test1', result='FAILURE',
375 changes='1,1'),
376 dict(name='project-test1', result='ABORTED',
377 changes='1,1 2,1'),
378 dict(name='project-test2', result='ABORTED',
379 changes='1,1 2,1'),
380 dict(name='project-test1', result='ABORTED',
381 changes='1,1 2,1 3,1'),
382 dict(name='project-test2', result='ABORTED',
383 changes='1,1 2,1 3,1'),
384 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700385
Paul Belanger174a8272017-03-14 13:20:10 -0400386 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700387 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400388 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700389 self.waitUntilSettled()
390 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700391
James E. Blairb536ecc2016-08-31 10:11:42 -0700392 self.assertBuilds([])
393 self.assertHistory([
394 dict(name='project-merge', result='SUCCESS',
395 changes='1,1'),
396 dict(name='project-merge', result='SUCCESS',
397 changes='1,1 2,1'),
398 dict(name='project-merge', result='SUCCESS',
399 changes='1,1 2,1 3,1'),
400 dict(name='project-test1', result='FAILURE',
401 changes='1,1'),
402 dict(name='project-test1', result='ABORTED',
403 changes='1,1 2,1'),
404 dict(name='project-test2', result='ABORTED',
405 changes='1,1 2,1'),
406 dict(name='project-test1', result='ABORTED',
407 changes='1,1 2,1 3,1'),
408 dict(name='project-test2', result='ABORTED',
409 changes='1,1 2,1 3,1'),
410 dict(name='project-merge', result='SUCCESS',
411 changes='2,1'),
412 dict(name='project-merge', result='SUCCESS',
413 changes='2,1 3,1'),
414 dict(name='project-test2', result='SUCCESS',
415 changes='1,1'),
416 dict(name='project-test1', result='SUCCESS',
417 changes='2,1'),
418 dict(name='project-test2', result='SUCCESS',
419 changes='2,1'),
420 dict(name='project-test1', result='SUCCESS',
421 changes='2,1 3,1'),
422 dict(name='project-test2', result='SUCCESS',
423 changes='2,1 3,1'),
424 ], ordered=False)
425
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400426 self.assertEqual(A.data['status'], 'NEW')
427 self.assertEqual(B.data['status'], 'MERGED')
428 self.assertEqual(C.data['status'], 'MERGED')
429 self.assertEqual(A.reported, 2)
430 self.assertEqual(B.reported, 2)
431 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700432
James E. Blair0aac4872013-08-23 14:02:38 -0700433 def test_failed_change_in_middle(self):
434 "Test a failed change in the middle of the queue"
435
Paul Belanger174a8272017-03-14 13:20:10 -0400436 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
438 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
439 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700440 A.addApproval('code-review', 2)
441 B.addApproval('code-review', 2)
442 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700443
Paul Belanger174a8272017-03-14 13:20:10 -0400444 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700445
James E. Blair8b5408c2016-08-08 15:37:46 -0700446 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
447 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
448 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700449
450 self.waitUntilSettled()
451
Paul Belanger174a8272017-03-14 13:20:10 -0400452 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700453 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400454 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700455 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400456 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700457 self.waitUntilSettled()
458
459 self.assertEqual(len(self.builds), 6)
460 self.assertEqual(self.builds[0].name, 'project-test1')
461 self.assertEqual(self.builds[1].name, 'project-test2')
462 self.assertEqual(self.builds[2].name, 'project-test1')
463 self.assertEqual(self.builds[3].name, 'project-test2')
464 self.assertEqual(self.builds[4].name, 'project-test1')
465 self.assertEqual(self.builds[5].name, 'project-test2')
466
467 self.release(self.builds[2])
468 self.waitUntilSettled()
469
James E. Blair972e3c72013-08-29 12:04:55 -0700470 # project-test1 and project-test2 for A
471 # project-test2 for B
472 # project-merge for C (without B)
473 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700474 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
475
Paul Belanger174a8272017-03-14 13:20:10 -0400476 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700477 self.waitUntilSettled()
478
479 # project-test1 and project-test2 for A
480 # project-test2 for B
481 # project-test1 and project-test2 for C
482 self.assertEqual(len(self.builds), 5)
483
Paul Belanger2e2a0492016-10-30 18:33:37 -0400484 tenant = self.sched.abide.tenants.get('tenant-one')
485 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700486 builds = items[0].current_build_set.getBuilds()
487 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
488 self.assertEqual(self.countJobResults(builds, None), 2)
489 builds = items[1].current_build_set.getBuilds()
490 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
491 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
492 self.assertEqual(self.countJobResults(builds, None), 1)
493 builds = items[2].current_build_set.getBuilds()
494 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700495 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700496
Paul Belanger174a8272017-03-14 13:20:10 -0400497 self.executor_server.hold_jobs_in_build = False
498 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700499 self.waitUntilSettled()
500
501 self.assertEqual(len(self.builds), 0)
502 self.assertEqual(len(self.history), 12)
503 self.assertEqual(A.data['status'], 'MERGED')
504 self.assertEqual(B.data['status'], 'NEW')
505 self.assertEqual(C.data['status'], 'MERGED')
506 self.assertEqual(A.reported, 2)
507 self.assertEqual(B.reported, 2)
508 self.assertEqual(C.reported, 2)
509
James E. Blaird466dc42012-07-31 10:42:56 -0700510 def test_failed_change_at_head_with_queue(self):
511 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700512
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700513 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
515 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
516 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700517 A.addApproval('code-review', 2)
518 B.addApproval('code-review', 2)
519 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700520
Paul Belanger174a8272017-03-14 13:20:10 -0400521 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700522
James E. Blair8b5408c2016-08-08 15:37:46 -0700523 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
524 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
525 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700526
527 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700528 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400529 self.assertEqual(len(self.builds), 0)
530 self.assertEqual(len(queue), 1)
Paul Belanger174a8272017-03-14 13:20:10 -0400531 self.assertEqual(queue[0].name, 'executor:execute')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800532 job_args = json.loads(queue[0].arguments)
533 self.assertEqual(job_args['job'], 'project-merge')
534 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700535
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700536 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700537 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700539 self.waitUntilSettled()
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. Blair701c5b42013-06-06 09:34:59 -0700542 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700543
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400544 self.assertEqual(len(self.builds), 0)
545 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800546
547 self.assertEqual(
548 json.loads(queue[0].arguments)['job'], 'project-test1')
549 self.assertEqual(
550 json.loads(queue[1].arguments)['job'], 'project-test2')
551 self.assertEqual(
552 json.loads(queue[2].arguments)['job'], 'project-test1')
553 self.assertEqual(
554 json.loads(queue[3].arguments)['job'], 'project-test2')
555 self.assertEqual(
556 json.loads(queue[4].arguments)['job'], 'project-test1')
557 self.assertEqual(
558 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700559
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700560 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700561 self.waitUntilSettled()
562
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400563 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700564 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400565 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
566 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700567
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700568 self.gearman_server.hold_jobs_in_queue = False
569 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700570 self.waitUntilSettled()
571
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400572 self.assertEqual(len(self.builds), 0)
573 self.assertEqual(len(self.history), 11)
574 self.assertEqual(A.data['status'], 'NEW')
575 self.assertEqual(B.data['status'], 'MERGED')
576 self.assertEqual(C.data['status'], 'MERGED')
577 self.assertEqual(A.reported, 2)
578 self.assertEqual(B.reported, 2)
579 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700580
James E. Blairec056492016-07-22 09:45:56 -0700581 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700582 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400583 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700584 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700585 A.addApproval('code-review', 2)
586 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700587 self.waitUntilSettled()
588 time.sleep(2)
589
590 data = json.loads(self.sched.formatStatusJSON())
591 found_job = None
592 for pipeline in data['pipelines']:
593 if pipeline['name'] != 'gate':
594 continue
595 for queue in pipeline['change_queues']:
596 for head in queue['heads']:
597 for item in head:
598 for job in item['jobs']:
599 if job['name'] == 'project-merge':
600 found_job = job
601 break
602
603 self.assertIsNotNone(found_job)
604 if iteration == 1:
605 self.assertIsNotNone(found_job['estimated_time'])
606 self.assertIsNone(found_job['remaining_time'])
607 else:
608 self.assertIsNotNone(found_job['estimated_time'])
609 self.assertTrue(found_job['estimated_time'] >= 2)
610 self.assertIsNotNone(found_job['remaining_time'])
611
Paul Belanger174a8272017-03-14 13:20:10 -0400612 self.executor_server.hold_jobs_in_build = False
613 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700614 self.waitUntilSettled()
615
James E. Blairec056492016-07-22 09:45:56 -0700616 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700617 def test_time_database(self):
618 "Test the time database"
619
620 self._test_time_database(1)
621 self._test_time_database(2)
622
James E. Blairfef71632013-09-23 11:15:47 -0700623 def test_two_failed_changes_at_head(self):
624 "Test that changes are reparented correctly if 2 fail at head"
625
Paul Belanger174a8272017-03-14 13:20:10 -0400626 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700627 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
628 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
629 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700630 A.addApproval('code-review', 2)
631 B.addApproval('code-review', 2)
632 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700633
Paul Belanger174a8272017-03-14 13:20:10 -0400634 self.executor_server.failJob('project-test1', A)
635 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700636
James E. Blair8b5408c2016-08-08 15:37:46 -0700637 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
638 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
639 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700640 self.waitUntilSettled()
641
Paul Belanger174a8272017-03-14 13:20:10 -0400642 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700643 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400644 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700645 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400646 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700647 self.waitUntilSettled()
648
649 self.assertEqual(len(self.builds), 6)
650 self.assertEqual(self.builds[0].name, 'project-test1')
651 self.assertEqual(self.builds[1].name, 'project-test2')
652 self.assertEqual(self.builds[2].name, 'project-test1')
653 self.assertEqual(self.builds[3].name, 'project-test2')
654 self.assertEqual(self.builds[4].name, 'project-test1')
655 self.assertEqual(self.builds[5].name, 'project-test2')
656
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400657 self.assertTrue(self.builds[0].hasChanges(A))
658 self.assertTrue(self.builds[2].hasChanges(A))
659 self.assertTrue(self.builds[2].hasChanges(B))
660 self.assertTrue(self.builds[4].hasChanges(A))
661 self.assertTrue(self.builds[4].hasChanges(B))
662 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700663
664 # Fail change B first
665 self.release(self.builds[2])
666 self.waitUntilSettled()
667
668 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400669 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700670 self.waitUntilSettled()
671
672 self.assertEqual(len(self.builds), 5)
673 self.assertEqual(self.builds[0].name, 'project-test1')
674 self.assertEqual(self.builds[1].name, 'project-test2')
675 self.assertEqual(self.builds[2].name, 'project-test2')
676 self.assertEqual(self.builds[3].name, 'project-test1')
677 self.assertEqual(self.builds[4].name, 'project-test2')
678
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400679 self.assertTrue(self.builds[1].hasChanges(A))
680 self.assertTrue(self.builds[2].hasChanges(A))
681 self.assertTrue(self.builds[2].hasChanges(B))
682 self.assertTrue(self.builds[4].hasChanges(A))
683 self.assertFalse(self.builds[4].hasChanges(B))
684 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700685
686 # Finish running all passing jobs for change A
687 self.release(self.builds[1])
688 self.waitUntilSettled()
689 # Fail and report change A
690 self.release(self.builds[0])
691 self.waitUntilSettled()
692
693 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400694 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700695 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400696 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700697 self.waitUntilSettled()
698
699 self.assertEqual(len(self.builds), 4)
700 self.assertEqual(self.builds[0].name, 'project-test1') # B
701 self.assertEqual(self.builds[1].name, 'project-test2') # B
702 self.assertEqual(self.builds[2].name, 'project-test1') # C
703 self.assertEqual(self.builds[3].name, 'project-test2') # C
704
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400705 self.assertFalse(self.builds[1].hasChanges(A))
706 self.assertTrue(self.builds[1].hasChanges(B))
707 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700708
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400709 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700710 # After A failed and B and C restarted, B should be back in
711 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400712 self.assertTrue(self.builds[2].hasChanges(B))
713 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700714
Paul Belanger174a8272017-03-14 13:20:10 -0400715 self.executor_server.hold_jobs_in_build = False
716 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700717 self.waitUntilSettled()
718
719 self.assertEqual(len(self.builds), 0)
720 self.assertEqual(len(self.history), 21)
721 self.assertEqual(A.data['status'], 'NEW')
722 self.assertEqual(B.data['status'], 'NEW')
723 self.assertEqual(C.data['status'], 'MERGED')
724 self.assertEqual(A.reported, 2)
725 self.assertEqual(B.reported, 2)
726 self.assertEqual(C.reported, 2)
727
James E. Blair8c803f82012-07-31 16:25:42 -0700728 def test_patch_order(self):
729 "Test that dependent patches are tested in the right order"
730 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
731 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
732 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700733 A.addApproval('code-review', 2)
734 B.addApproval('code-review', 2)
735 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700736
737 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
738 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
739 M2.setMerged()
740 M1.setMerged()
741
742 # C -> B -> A -> M1 -> M2
743 # M2 is here to make sure it is never queried. If it is, it
744 # means zuul is walking down the entire history of merged
745 # changes.
746
747 C.setDependsOn(B, 1)
748 B.setDependsOn(A, 1)
749 A.setDependsOn(M1, 1)
750 M1.setDependsOn(M2, 1)
751
James E. Blair8b5408c2016-08-08 15:37:46 -0700752 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700753
754 self.waitUntilSettled()
755
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400756 self.assertEqual(A.data['status'], 'NEW')
757 self.assertEqual(B.data['status'], 'NEW')
758 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700759
James E. Blair8b5408c2016-08-08 15:37:46 -0700760 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
761 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700762
763 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400764 self.assertEqual(M2.queried, 0)
765 self.assertEqual(A.data['status'], 'MERGED')
766 self.assertEqual(B.data['status'], 'MERGED')
767 self.assertEqual(C.data['status'], 'MERGED')
768 self.assertEqual(A.reported, 2)
769 self.assertEqual(B.reported, 2)
770 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700771
James E. Blair063672f2015-01-29 13:09:12 -0800772 def test_needed_changes_enqueue(self):
773 "Test that a needed change is enqueued ahead"
774 # A Given a git tree like this, if we enqueue
775 # / \ change C, we should walk up and down the tree
776 # B G and enqueue changes in the order ABCDEFG.
777 # /|\ This is also the order that you would get if
778 # *C E F you enqueued changes in the order ABCDEFG, so
779 # / the ordering is stable across re-enqueue events.
780 # D
781
782 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
783 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
784 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
785 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
786 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
787 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
788 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
789 B.setDependsOn(A, 1)
790 C.setDependsOn(B, 1)
791 D.setDependsOn(C, 1)
792 E.setDependsOn(B, 1)
793 F.setDependsOn(B, 1)
794 G.setDependsOn(A, 1)
795
James E. Blair8b5408c2016-08-08 15:37:46 -0700796 A.addApproval('code-review', 2)
797 B.addApproval('code-review', 2)
798 C.addApproval('code-review', 2)
799 D.addApproval('code-review', 2)
800 E.addApproval('code-review', 2)
801 F.addApproval('code-review', 2)
802 G.addApproval('code-review', 2)
803 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800804
805 self.waitUntilSettled()
806
807 self.assertEqual(A.data['status'], 'NEW')
808 self.assertEqual(B.data['status'], 'NEW')
809 self.assertEqual(C.data['status'], 'NEW')
810 self.assertEqual(D.data['status'], 'NEW')
811 self.assertEqual(E.data['status'], 'NEW')
812 self.assertEqual(F.data['status'], 'NEW')
813 self.assertEqual(G.data['status'], 'NEW')
814
815 # We're about to add approvals to changes without adding the
816 # triggering events to Zuul, so that we can be sure that it is
817 # enqueing the changes based on dependencies, not because of
818 # triggering events. Since it will have the changes cached
819 # already (without approvals), we need to clear the cache
820 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400821 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100822 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800823
Paul Belanger174a8272017-03-14 13:20:10 -0400824 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700825 A.addApproval('approved', 1)
826 B.addApproval('approved', 1)
827 D.addApproval('approved', 1)
828 E.addApproval('approved', 1)
829 F.addApproval('approved', 1)
830 G.addApproval('approved', 1)
831 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800832
833 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400834 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800835 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400836 self.executor_server.hold_jobs_in_build = False
837 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800838 self.waitUntilSettled()
839
840 self.assertEqual(A.data['status'], 'MERGED')
841 self.assertEqual(B.data['status'], 'MERGED')
842 self.assertEqual(C.data['status'], 'MERGED')
843 self.assertEqual(D.data['status'], 'MERGED')
844 self.assertEqual(E.data['status'], 'MERGED')
845 self.assertEqual(F.data['status'], 'MERGED')
846 self.assertEqual(G.data['status'], 'MERGED')
847 self.assertEqual(A.reported, 2)
848 self.assertEqual(B.reported, 2)
849 self.assertEqual(C.reported, 2)
850 self.assertEqual(D.reported, 2)
851 self.assertEqual(E.reported, 2)
852 self.assertEqual(F.reported, 2)
853 self.assertEqual(G.reported, 2)
854 self.assertEqual(self.history[6].changes,
855 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
856
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100857 def test_source_cache(self):
858 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400859 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700860
861 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
862 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
863 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700864 A.addApproval('code-review', 2)
865 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700866
867 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
868 M1.setMerged()
869
870 B.setDependsOn(A, 1)
871 A.setDependsOn(M1, 1)
872
James E. Blair8b5408c2016-08-08 15:37:46 -0700873 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700874 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
875
876 self.waitUntilSettled()
877
878 for build in self.builds:
879 if build.parameters['ZUUL_PIPELINE'] == 'check':
880 build.release()
881 self.waitUntilSettled()
882 for build in self.builds:
883 if build.parameters['ZUUL_PIPELINE'] == 'check':
884 build.release()
885 self.waitUntilSettled()
886
James E. Blair8b5408c2016-08-08 15:37:46 -0700887 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700888 self.waitUntilSettled()
889
Joshua Hesketh352264b2015-08-11 23:42:08 +1000890 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700891 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000892 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700893
Paul Belanger174a8272017-03-14 13:20:10 -0400894 self.executor_server.hold_jobs_in_build = False
895 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700896 self.waitUntilSettled()
897
898 self.assertEqual(A.data['status'], 'MERGED')
899 self.assertEqual(B.data['status'], 'MERGED')
900 self.assertEqual(A.queried, 2) # Initial and isMerged
901 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
902
James E. Blair8c803f82012-07-31 16:25:42 -0700903 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700904 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700905 # TODO: move to test_gerrit (this is a unit test!)
906 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400907 tenant = self.sched.abide.tenants.get('tenant-one')
908 source = tenant.layout.pipelines['gate'].source
909
910 # TODO(pabelanger): As we add more source / trigger APIs we should make
911 # it easier for users to create events for testing.
912 event = zuul.model.TriggerEvent()
913 event.trigger_name = 'gerrit'
914 event.change_number = '1'
915 event.patch_number = '2'
916
917 a = source.getChange(event)
918 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700919 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700920
James E. Blair8b5408c2016-08-08 15:37:46 -0700921 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400922 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700923 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700924
James E. Blair8b5408c2016-08-08 15:37:46 -0700925 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400926 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700927 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700928
James E. Blairec056492016-07-22 09:45:56 -0700929 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700930 def test_build_configuration_conflict(self):
931 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700932
933 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700934 A = self.fake_gerrit.addFakeChange('org/conflict-project',
935 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700936 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700937 B = self.fake_gerrit.addFakeChange('org/conflict-project',
938 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700939 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700940 C = self.fake_gerrit.addFakeChange('org/conflict-project',
941 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700942 A.addApproval('code-review', 2)
943 B.addApproval('code-review', 2)
944 C.addApproval('code-review', 2)
945 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
946 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
947 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700948 self.waitUntilSettled()
949
James E. Blair6736beb2013-07-11 15:18:15 -0700950 self.assertEqual(A.reported, 1)
951 self.assertEqual(B.reported, 1)
952 self.assertEqual(C.reported, 1)
953
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700954 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700955 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700956 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700957 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700958 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700960
961 self.assertEqual(len(self.history), 2) # A and C merge jobs
962
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700963 self.gearman_server.hold_jobs_in_queue = False
964 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700965 self.waitUntilSettled()
966
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400967 self.assertEqual(A.data['status'], 'MERGED')
968 self.assertEqual(B.data['status'], 'NEW')
969 self.assertEqual(C.data['status'], 'MERGED')
970 self.assertEqual(A.reported, 2)
971 self.assertEqual(B.reported, 2)
972 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700973 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700974
James E. Blairdaabed22012-08-15 15:38:57 -0700975 def test_post(self):
976 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700977
Zhongyue Luo5d556072012-09-21 02:00:47 +0900978 e = {
979 "type": "ref-updated",
980 "submitter": {
981 "name": "User Name",
982 },
983 "refUpdate": {
984 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
985 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
986 "refName": "master",
987 "project": "org/project",
988 }
989 }
James E. Blairdaabed22012-08-15 15:38:57 -0700990 self.fake_gerrit.addEvent(e)
991 self.waitUntilSettled()
992
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400993 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400994 self.assertEqual(len(self.history), 1)
995 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700996
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700997 def test_post_ignore_deletes(self):
998 "Test that deleting refs does not trigger post jobs"
999
1000 e = {
1001 "type": "ref-updated",
1002 "submitter": {
1003 "name": "User Name",
1004 },
1005 "refUpdate": {
1006 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1007 "newRev": "0000000000000000000000000000000000000000",
1008 "refName": "master",
1009 "project": "org/project",
1010 }
1011 }
1012 self.fake_gerrit.addEvent(e)
1013 self.waitUntilSettled()
1014
1015 job_names = [x.name for x in self.history]
1016 self.assertEqual(len(self.history), 0)
1017 self.assertNotIn('project-post', job_names)
1018
1019 def test_post_ignore_deletes_negative(self):
1020 "Test that deleting refs does trigger post jobs"
1021
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001022 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001023 self.sched.reconfigure(self.config)
1024
1025 e = {
1026 "type": "ref-updated",
1027 "submitter": {
1028 "name": "User Name",
1029 },
1030 "refUpdate": {
1031 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1032 "newRev": "0000000000000000000000000000000000000000",
1033 "refName": "master",
1034 "project": "org/project",
1035 }
1036 }
1037 self.fake_gerrit.addEvent(e)
1038 self.waitUntilSettled()
1039
1040 job_names = [x.name for x in self.history]
1041 self.assertEqual(len(self.history), 1)
1042 self.assertIn('project-post', job_names)
1043
James E. Blairec056492016-07-22 09:45:56 -07001044 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001045 def test_build_configuration_branch_interaction(self):
1046 "Test that switching between branches works"
1047 self.test_build_configuration()
1048 self.test_build_configuration_branch()
1049 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001050 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001051 repo = git.Repo(path)
1052 repo.heads.master.commit = repo.commit('init')
1053 self.test_build_configuration()
1054
James E. Blaircaec0c52012-08-22 14:52:22 -07001055 def test_dependent_changes_dequeue(self):
1056 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001057
James E. Blaircaec0c52012-08-22 14:52:22 -07001058 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1059 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1060 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001061 A.addApproval('code-review', 2)
1062 B.addApproval('code-review', 2)
1063 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001064
1065 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1066 M1.setMerged()
1067
1068 # C -> B -> A -> M1
1069
1070 C.setDependsOn(B, 1)
1071 B.setDependsOn(A, 1)
1072 A.setDependsOn(M1, 1)
1073
Paul Belanger174a8272017-03-14 13:20:10 -04001074 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001075
James E. Blair8b5408c2016-08-08 15:37:46 -07001076 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1077 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1078 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001079
1080 self.waitUntilSettled()
1081
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001082 self.assertEqual(A.data['status'], 'NEW')
1083 self.assertEqual(A.reported, 2)
1084 self.assertEqual(B.data['status'], 'NEW')
1085 self.assertEqual(B.reported, 2)
1086 self.assertEqual(C.data['status'], 'NEW')
1087 self.assertEqual(C.reported, 2)
1088 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001089
James E. Blair972e3c72013-08-29 12:04:55 -07001090 def test_failing_dependent_changes(self):
1091 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001092 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001093 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1094 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1095 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1096 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1097 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001098 A.addApproval('code-review', 2)
1099 B.addApproval('code-review', 2)
1100 C.addApproval('code-review', 2)
1101 D.addApproval('code-review', 2)
1102 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001103
1104 # E, D -> C -> B, A
1105
1106 D.setDependsOn(C, 1)
1107 C.setDependsOn(B, 1)
1108
Paul Belanger174a8272017-03-14 13:20:10 -04001109 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001110
James E. Blair8b5408c2016-08-08 15:37:46 -07001111 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1112 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1113 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1114 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1115 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001116
1117 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001118 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001119 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001120 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001121 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001122 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001123 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001124 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001125 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001126 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001127 self.waitUntilSettled()
1128
Paul Belanger174a8272017-03-14 13:20:10 -04001129 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001130 for build in self.builds:
1131 if build.parameters['ZUUL_CHANGE'] != '1':
1132 build.release()
1133 self.waitUntilSettled()
1134
Paul Belanger174a8272017-03-14 13:20:10 -04001135 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001136 self.waitUntilSettled()
1137
1138 self.assertEqual(A.data['status'], 'MERGED')
1139 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001140 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001141 self.assertEqual(B.data['status'], 'NEW')
1142 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001143 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001144 self.assertEqual(C.data['status'], 'NEW')
1145 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001146 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001147 self.assertEqual(D.data['status'], 'NEW')
1148 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001149 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001150 self.assertEqual(E.data['status'], 'MERGED')
1151 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001152 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001153 self.assertEqual(len(self.history), 18)
1154
James E. Blairec590122012-08-22 15:19:31 -07001155 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001156 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001157 # If it's dequeued more than once, we should see extra
1158 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001159
Paul Belanger174a8272017-03-14 13:20:10 -04001160 self.executor_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001161 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1162 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1163 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001164 A.addApproval('code-review', 2)
1165 B.addApproval('code-review', 2)
1166 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001167
Paul Belanger174a8272017-03-14 13:20:10 -04001168 self.executor_server.failJob('project-test1', A)
1169 self.executor_server.failJob('project-test2', A)
1170 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001171
James E. Blair8b5408c2016-08-08 15:37:46 -07001172 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1173 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1174 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001175
1176 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001177
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001178 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001179 self.assertEqual(self.builds[0].name, 'project-merge')
1180 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001181
Paul Belanger174a8272017-03-14 13:20:10 -04001182 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001183 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001184 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001185 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001186 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001187 self.waitUntilSettled()
1188
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001189 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001190 self.assertEqual(self.builds[0].name, 'project-test1')
1191 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001192 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001193 self.assertEqual(self.builds[3].name, 'project-test1')
1194 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001195 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001196 self.assertEqual(self.builds[6].name, 'project-test1')
1197 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001198 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001199
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001200 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001201 self.waitUntilSettled()
1202
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001203 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1204 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001205
Paul Belanger174a8272017-03-14 13:20:10 -04001206 self.executor_server.hold_jobs_in_build = False
1207 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001208 self.waitUntilSettled()
1209
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001210 self.assertEqual(len(self.builds), 0)
1211 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001212
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001213 self.assertEqual(A.data['status'], 'NEW')
1214 self.assertEqual(B.data['status'], 'MERGED')
1215 self.assertEqual(C.data['status'], 'MERGED')
1216 self.assertEqual(A.reported, 2)
1217 self.assertEqual(B.reported, 2)
1218 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001219
1220 def test_nonvoting_job(self):
1221 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001222
James E. Blair4ec821f2012-08-23 15:28:28 -07001223 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1224 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001225 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001226 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001227 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001228
1229 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001230
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001231 self.assertEqual(A.data['status'], 'MERGED')
1232 self.assertEqual(A.reported, 2)
1233 self.assertEqual(
1234 self.getJobFromHistory('nonvoting-project-merge').result,
1235 'SUCCESS')
1236 self.assertEqual(
1237 self.getJobFromHistory('nonvoting-project-test1').result,
1238 'SUCCESS')
1239 self.assertEqual(
1240 self.getJobFromHistory('nonvoting-project-test2').result,
1241 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001242
James E. Blair5821bd92015-09-16 08:48:15 -07001243 for build in self.builds:
1244 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1245
James E. Blaire0487072012-08-29 17:38:31 -07001246 def test_check_queue_success(self):
1247 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001248
James E. Blaire0487072012-08-29 17:38:31 -07001249 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1250 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1251
1252 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001253
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001254 self.assertEqual(A.data['status'], 'NEW')
1255 self.assertEqual(A.reported, 1)
1256 self.assertEqual(self.getJobFromHistory('project-merge').result,
1257 'SUCCESS')
1258 self.assertEqual(self.getJobFromHistory('project-test1').result,
1259 'SUCCESS')
1260 self.assertEqual(self.getJobFromHistory('project-test2').result,
1261 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001262
1263 def test_check_queue_failure(self):
1264 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001265
James E. Blaire0487072012-08-29 17:38:31 -07001266 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001267 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001268 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1269
1270 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001271
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001272 self.assertEqual(A.data['status'], 'NEW')
1273 self.assertEqual(A.reported, 1)
1274 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001275 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001276 self.assertEqual(self.getJobFromHistory('project-test1').result,
1277 'SUCCESS')
1278 self.assertEqual(self.getJobFromHistory('project-test2').result,
1279 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001280
1281 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001282 # This particular test does a large amount of merges and needs a little
1283 # more time to complete
1284 self.wait_timeout = 90
James E. Blair127bc182012-08-28 15:55:15 -07001285 "test that dependent changes behind dequeued changes work"
1286 # This complicated test is a reproduction of a real life bug
1287 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001288
Paul Belanger174a8272017-03-14 13:20:10 -04001289 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001290 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1291 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1292 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1293 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1294 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1295 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1296 D.setDependsOn(C, 1)
1297 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001298 A.addApproval('code-review', 2)
1299 B.addApproval('code-review', 2)
1300 C.addApproval('code-review', 2)
1301 D.addApproval('code-review', 2)
1302 E.addApproval('code-review', 2)
1303 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001304
1305 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001306
1307 # Change object re-use in the gerrit trigger is hidden if
1308 # changes are added in quick succession; waiting makes it more
1309 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001310 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001311 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001312 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001313 self.waitUntilSettled()
1314
Paul Belanger174a8272017-03-14 13:20:10 -04001315 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001316 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001317 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001318 self.waitUntilSettled()
1319
James E. Blair8b5408c2016-08-08 15:37:46 -07001320 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001321 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001322 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001323 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001324 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001325 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001326 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001327 self.waitUntilSettled()
1328
Paul Belanger174a8272017-03-14 13:20:10 -04001329 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001330 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001331 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001332 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001333 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001334 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001335 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001336 self.waitUntilSettled()
1337
1338 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001339
1340 # Grab pointers to the jobs we want to release before
1341 # releasing any, because list indexes may change as
1342 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001343 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001344 a.release()
1345 b.release()
1346 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001347 self.waitUntilSettled()
1348
Paul Belanger174a8272017-03-14 13:20:10 -04001349 self.executor_server.hold_jobs_in_build = False
1350 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001351 self.waitUntilSettled()
1352
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001353 self.assertEqual(A.data['status'], 'NEW')
1354 self.assertEqual(B.data['status'], 'MERGED')
1355 self.assertEqual(C.data['status'], 'MERGED')
1356 self.assertEqual(D.data['status'], 'MERGED')
1357 self.assertEqual(E.data['status'], 'MERGED')
1358 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001359
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001360 self.assertEqual(A.reported, 2)
1361 self.assertEqual(B.reported, 2)
1362 self.assertEqual(C.reported, 2)
1363 self.assertEqual(D.reported, 2)
1364 self.assertEqual(E.reported, 2)
1365 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1368 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001369
1370 def test_merger_repack(self):
1371 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001372
James E. Blair05fed602012-09-07 12:45:24 -07001373 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001374 A.addApproval('code-review', 2)
1375 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001376 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001377 self.assertEqual(self.getJobFromHistory('project-merge').result,
1378 'SUCCESS')
1379 self.assertEqual(self.getJobFromHistory('project-test1').result,
1380 'SUCCESS')
1381 self.assertEqual(self.getJobFromHistory('project-test2').result,
1382 'SUCCESS')
1383 self.assertEqual(A.data['status'], 'MERGED')
1384 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001385 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001386 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001387
Monty Taylord642d852017-02-23 14:05:42 -05001388 path = os.path.join(self.merger_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001389 if os.path.exists(path):
1390 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001391 path = os.path.join(self.executor_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001392 if os.path.exists(path):
1393 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001394
1395 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001396 A.addApproval('code-review', 2)
1397 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001398 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001399 self.assertEqual(self.getJobFromHistory('project-merge').result,
1400 'SUCCESS')
1401 self.assertEqual(self.getJobFromHistory('project-test1').result,
1402 'SUCCESS')
1403 self.assertEqual(self.getJobFromHistory('project-test2').result,
1404 'SUCCESS')
1405 self.assertEqual(A.data['status'], 'MERGED')
1406 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001407
James E. Blair4886f282012-11-15 09:27:33 -08001408 def test_merger_repack_large_change(self):
1409 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001410 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001411 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001412 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001413 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001414 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001415 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001416 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1417 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001418 path = os.path.join(self.upstream_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001419 repack_repo(path)
Monty Taylord642d852017-02-23 14:05:42 -05001420 path = os.path.join(self.merger_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001421 if os.path.exists(path):
1422 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001423 path = os.path.join(self.executor_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001424 if os.path.exists(path):
1425 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001426
James E. Blair8b5408c2016-08-08 15:37:46 -07001427 A.addApproval('code-review', 2)
1428 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001429 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001430 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001431 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001432 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001433 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001434 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001435 'SUCCESS')
1436 self.assertEqual(A.data['status'], 'MERGED')
1437 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001438
James E. Blair2fa50962013-01-30 21:50:41 -08001439 def test_new_patchset_dequeues_old(self):
1440 "Test that a new patchset causes the old to be dequeued"
1441 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001442 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001443 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1444 M.setMerged()
1445
1446 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1447 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1448 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1449 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001450 A.addApproval('code-review', 2)
1451 B.addApproval('code-review', 2)
1452 C.addApproval('code-review', 2)
1453 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001454
1455 C.setDependsOn(B, 1)
1456 B.setDependsOn(A, 1)
1457 A.setDependsOn(M, 1)
1458
James E. Blair8b5408c2016-08-08 15:37:46 -07001459 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1460 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1461 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1462 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001463 self.waitUntilSettled()
1464
1465 B.addPatchset()
1466 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1467 self.waitUntilSettled()
1468
Paul Belanger174a8272017-03-14 13:20:10 -04001469 self.executor_server.hold_jobs_in_build = False
1470 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001471 self.waitUntilSettled()
1472
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001473 self.assertEqual(A.data['status'], 'MERGED')
1474 self.assertEqual(A.reported, 2)
1475 self.assertEqual(B.data['status'], 'NEW')
1476 self.assertEqual(B.reported, 2)
1477 self.assertEqual(C.data['status'], 'NEW')
1478 self.assertEqual(C.reported, 2)
1479 self.assertEqual(D.data['status'], 'MERGED')
1480 self.assertEqual(D.reported, 2)
1481 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001482
James E. Blairba437362015-02-07 11:41:52 -08001483 def test_new_patchset_check(self):
1484 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001485
Paul Belanger174a8272017-03-14 13:20:10 -04001486 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001487
1488 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001489 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001490 tenant = self.sched.abide.tenants.get('tenant-one')
1491 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001492
1493 # Add two git-dependent changes
1494 B.setDependsOn(A, 1)
1495 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1496 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001497 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1498 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001499
1500 # A live item, and a non-live/live pair
1501 items = check_pipeline.getAllItems()
1502 self.assertEqual(len(items), 3)
1503
1504 self.assertEqual(items[0].change.number, '1')
1505 self.assertEqual(items[0].change.patchset, '1')
1506 self.assertFalse(items[0].live)
1507
1508 self.assertEqual(items[1].change.number, '2')
1509 self.assertEqual(items[1].change.patchset, '1')
1510 self.assertTrue(items[1].live)
1511
1512 self.assertEqual(items[2].change.number, '1')
1513 self.assertEqual(items[2].change.patchset, '1')
1514 self.assertTrue(items[2].live)
1515
1516 # Add a new patchset to A
1517 A.addPatchset()
1518 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1519 self.waitUntilSettled()
1520
1521 # The live copy of A,1 should be gone, but the non-live and B
1522 # should continue, and we should have a new A,2
1523 items = check_pipeline.getAllItems()
1524 self.assertEqual(len(items), 3)
1525
1526 self.assertEqual(items[0].change.number, '1')
1527 self.assertEqual(items[0].change.patchset, '1')
1528 self.assertFalse(items[0].live)
1529
1530 self.assertEqual(items[1].change.number, '2')
1531 self.assertEqual(items[1].change.patchset, '1')
1532 self.assertTrue(items[1].live)
1533
1534 self.assertEqual(items[2].change.number, '1')
1535 self.assertEqual(items[2].change.patchset, '2')
1536 self.assertTrue(items[2].live)
1537
1538 # Add a new patchset to B
1539 B.addPatchset()
1540 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1541 self.waitUntilSettled()
1542
1543 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1544 # but we should have a new B,2 (still based on A,1)
1545 items = check_pipeline.getAllItems()
1546 self.assertEqual(len(items), 3)
1547
1548 self.assertEqual(items[0].change.number, '1')
1549 self.assertEqual(items[0].change.patchset, '2')
1550 self.assertTrue(items[0].live)
1551
1552 self.assertEqual(items[1].change.number, '1')
1553 self.assertEqual(items[1].change.patchset, '1')
1554 self.assertFalse(items[1].live)
1555
1556 self.assertEqual(items[2].change.number, '2')
1557 self.assertEqual(items[2].change.patchset, '2')
1558 self.assertTrue(items[2].live)
1559
1560 self.builds[0].release()
1561 self.waitUntilSettled()
1562 self.builds[0].release()
1563 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001564 self.executor_server.hold_jobs_in_build = False
1565 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001566 self.waitUntilSettled()
1567
1568 self.assertEqual(A.reported, 1)
1569 self.assertEqual(B.reported, 1)
1570 self.assertEqual(self.history[0].result, 'ABORTED')
1571 self.assertEqual(self.history[0].changes, '1,1')
1572 self.assertEqual(self.history[1].result, 'ABORTED')
1573 self.assertEqual(self.history[1].changes, '1,1 2,1')
1574 self.assertEqual(self.history[2].result, 'SUCCESS')
1575 self.assertEqual(self.history[2].changes, '1,2')
1576 self.assertEqual(self.history[3].result, 'SUCCESS')
1577 self.assertEqual(self.history[3].changes, '1,1 2,2')
1578
1579 def test_abandoned_gate(self):
1580 "Test that an abandoned change is dequeued from gate"
1581
Paul Belanger174a8272017-03-14 13:20:10 -04001582 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001583
1584 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001585 A.addApproval('code-review', 2)
1586 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001587 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001588 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1589 self.assertEqual(self.builds[0].name, 'project-merge')
1590
1591 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1592 self.waitUntilSettled()
1593
Paul Belanger174a8272017-03-14 13:20:10 -04001594 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001595 self.waitUntilSettled()
1596
Arie5b4048c2016-10-05 16:12:06 +03001597 self.assertBuilds([])
1598 self.assertHistory([
1599 dict(name='project-merge', result='ABORTED', changes='1,1')],
1600 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001601 self.assertEqual(A.reported, 1,
1602 "Abandoned gate change should report only start")
1603
1604 def test_abandoned_check(self):
1605 "Test that an abandoned change is dequeued from check"
1606
Paul Belanger174a8272017-03-14 13:20:10 -04001607 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001608
1609 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1610 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001611 tenant = self.sched.abide.tenants.get('tenant-one')
1612 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001613
1614 # Add two git-dependent changes
1615 B.setDependsOn(A, 1)
1616 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1617 self.waitUntilSettled()
1618 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1619 self.waitUntilSettled()
1620 # A live item, and a non-live/live pair
1621 items = check_pipeline.getAllItems()
1622 self.assertEqual(len(items), 3)
1623
1624 self.assertEqual(items[0].change.number, '1')
1625 self.assertFalse(items[0].live)
1626
1627 self.assertEqual(items[1].change.number, '2')
1628 self.assertTrue(items[1].live)
1629
1630 self.assertEqual(items[2].change.number, '1')
1631 self.assertTrue(items[2].live)
1632
1633 # Abandon A
1634 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1635 self.waitUntilSettled()
1636
1637 # The live copy of A should be gone, but the non-live and B
1638 # should continue
1639 items = check_pipeline.getAllItems()
1640 self.assertEqual(len(items), 2)
1641
1642 self.assertEqual(items[0].change.number, '1')
1643 self.assertFalse(items[0].live)
1644
1645 self.assertEqual(items[1].change.number, '2')
1646 self.assertTrue(items[1].live)
1647
Paul Belanger174a8272017-03-14 13:20:10 -04001648 self.executor_server.hold_jobs_in_build = False
1649 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001650 self.waitUntilSettled()
1651
1652 self.assertEqual(len(self.history), 4)
1653 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001654 'Build should have been aborted')
1655 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001656 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001657
Steve Varnau7b78b312015-04-03 14:49:46 -07001658 def test_abandoned_not_timer(self):
1659 "Test that an abandoned change does not cancel timer jobs"
1660
Paul Belanger174a8272017-03-14 13:20:10 -04001661 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001662
1663 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001664 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001665 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001666 # The pipeline triggers every second, so we should have seen
1667 # several by now.
1668 time.sleep(5)
1669 self.waitUntilSettled()
1670 # Stop queuing timer triggered jobs so that the assertions
1671 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001672 # Must be in same repo, so overwrite config with another one
Clint Byrum58264dc2017-02-07 21:21:22 -08001673 self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clint Byrumead6c562017-02-01 16:34:04 -08001674
Steve Varnau7b78b312015-04-03 14:49:46 -07001675 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001676 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1677
1678 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1679 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1680 self.waitUntilSettled()
1681 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1682
1683 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1684 self.waitUntilSettled()
1685
1686 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1687
Paul Belanger174a8272017-03-14 13:20:10 -04001688 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001689 self.waitUntilSettled()
1690
Arx Cruzb1b010d2013-10-28 19:49:59 -02001691 def test_zuul_url_return(self):
1692 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001693 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001694 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001695
1696 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001697 A.addApproval('code-review', 2)
1698 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001699 self.waitUntilSettled()
1700
1701 self.assertEqual(len(self.builds), 1)
1702 for build in self.builds:
1703 self.assertTrue('ZUUL_URL' in build.parameters)
1704
Paul Belanger174a8272017-03-14 13:20:10 -04001705 self.executor_server.hold_jobs_in_build = False
1706 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001707 self.waitUntilSettled()
1708
James E. Blair2fa50962013-01-30 21:50:41 -08001709 def test_new_patchset_dequeues_old_on_head(self):
1710 "Test that a new patchset causes the old to be dequeued (at head)"
1711 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001712 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001713 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1714 M.setMerged()
1715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1716 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1717 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1718 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001719 A.addApproval('code-review', 2)
1720 B.addApproval('code-review', 2)
1721 C.addApproval('code-review', 2)
1722 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001723
1724 C.setDependsOn(B, 1)
1725 B.setDependsOn(A, 1)
1726 A.setDependsOn(M, 1)
1727
James E. Blair8b5408c2016-08-08 15:37:46 -07001728 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1729 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1730 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1731 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001732 self.waitUntilSettled()
1733
1734 A.addPatchset()
1735 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1736 self.waitUntilSettled()
1737
Paul Belanger174a8272017-03-14 13:20:10 -04001738 self.executor_server.hold_jobs_in_build = False
1739 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001740 self.waitUntilSettled()
1741
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001742 self.assertEqual(A.data['status'], 'NEW')
1743 self.assertEqual(A.reported, 2)
1744 self.assertEqual(B.data['status'], 'NEW')
1745 self.assertEqual(B.reported, 2)
1746 self.assertEqual(C.data['status'], 'NEW')
1747 self.assertEqual(C.reported, 2)
1748 self.assertEqual(D.data['status'], 'MERGED')
1749 self.assertEqual(D.reported, 2)
1750 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001751
1752 def test_new_patchset_dequeues_old_without_dependents(self):
1753 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001754 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001755 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1756 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1757 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001758 A.addApproval('code-review', 2)
1759 B.addApproval('code-review', 2)
1760 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001761
James E. Blair8b5408c2016-08-08 15:37:46 -07001762 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1763 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1764 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001765 self.waitUntilSettled()
1766
1767 B.addPatchset()
1768 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1769 self.waitUntilSettled()
1770
Paul Belanger174a8272017-03-14 13:20:10 -04001771 self.executor_server.hold_jobs_in_build = False
1772 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001773 self.waitUntilSettled()
1774
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001775 self.assertEqual(A.data['status'], 'MERGED')
1776 self.assertEqual(A.reported, 2)
1777 self.assertEqual(B.data['status'], 'NEW')
1778 self.assertEqual(B.reported, 2)
1779 self.assertEqual(C.data['status'], 'MERGED')
1780 self.assertEqual(C.reported, 2)
1781 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001782
1783 def test_new_patchset_dequeues_old_independent_queue(self):
1784 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001785 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001786 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1787 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1788 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1789 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1790 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1791 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1792 self.waitUntilSettled()
1793
1794 B.addPatchset()
1795 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1796 self.waitUntilSettled()
1797
Paul Belanger174a8272017-03-14 13:20:10 -04001798 self.executor_server.hold_jobs_in_build = False
1799 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001800 self.waitUntilSettled()
1801
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001802 self.assertEqual(A.data['status'], 'NEW')
1803 self.assertEqual(A.reported, 1)
1804 self.assertEqual(B.data['status'], 'NEW')
1805 self.assertEqual(B.reported, 1)
1806 self.assertEqual(C.data['status'], 'NEW')
1807 self.assertEqual(C.reported, 1)
1808 self.assertEqual(len(self.history), 10)
1809 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001810
James E. Blair18c64442014-03-18 10:14:45 -07001811 def test_noop_job(self):
1812 "Test that the internal noop job works"
1813 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001814 A.addApproval('code-review', 2)
1815 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001816 self.waitUntilSettled()
1817
1818 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1819 self.assertTrue(self.sched._areAllBuildsComplete())
1820 self.assertEqual(len(self.history), 0)
1821 self.assertEqual(A.data['status'], 'MERGED')
1822 self.assertEqual(A.reported, 2)
1823
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001824 def test_no_job_project(self):
1825 "Test that reports with no jobs don't get sent"
1826 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1827 'master', 'A')
1828 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1829 self.waitUntilSettled()
1830
1831 # Change wasn't reported to
1832 self.assertEqual(A.reported, False)
1833
1834 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001835 tenant = self.sched.abide.tenants.get('tenant-one')
1836 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001837 items = check_pipeline.getAllItems()
1838 self.assertEqual(len(items), 0)
1839
1840 self.assertEqual(len(self.history), 0)
1841
James E. Blair7d0dedc2013-02-21 17:26:09 -08001842 def test_zuul_refs(self):
1843 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001844 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001845 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1846 M1.setMerged()
1847 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1848 M2.setMerged()
1849
1850 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1851 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1852 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1853 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001854 A.addApproval('code-review', 2)
1855 B.addApproval('code-review', 2)
1856 C.addApproval('code-review', 2)
1857 D.addApproval('code-review', 2)
1858 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1859 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1860 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1861 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001862
1863 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001864 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001865 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001866 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001867 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001868 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001869 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001870 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001871 self.waitUntilSettled()
1872
James E. Blair7d0dedc2013-02-21 17:26:09 -08001873 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001874 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001875 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001876 if x.parameters['ZUUL_CHANGE'] == '3':
1877 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001878 a_build = x
1879 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001880 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001881 b_build = x
1882 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001883 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001884 c_build = x
1885 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001886 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001887 d_build = x
1888 if a_build and b_build and c_build and d_build:
1889 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001890
1891 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001892 self.assertIsNotNone(a_zref)
1893 self.assertIsNotNone(b_zref)
1894 self.assertIsNotNone(c_zref)
1895 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001896
1897 # And they should all be different
1898 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001899 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001900
Clint Byrum3343e3e2016-11-15 16:05:03 -08001901 # should have a, not b, and should not be in project2
1902 self.assertTrue(a_build.hasChanges(A))
1903 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001904
Clint Byrum3343e3e2016-11-15 16:05:03 -08001905 # should have a and b, and should not be in project2
1906 self.assertTrue(b_build.hasChanges(A, B))
1907 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001908
Clint Byrum3343e3e2016-11-15 16:05:03 -08001909 # should have a and b in 1, c in 2
1910 self.assertTrue(c_build.hasChanges(A, B, C))
1911 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001912
Clint Byrum3343e3e2016-11-15 16:05:03 -08001913 # should have a and b in 1, c and d in 2
1914 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001915
Paul Belanger174a8272017-03-14 13:20:10 -04001916 self.executor_server.hold_jobs_in_build = False
1917 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001918 self.waitUntilSettled()
1919
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001920 self.assertEqual(A.data['status'], 'MERGED')
1921 self.assertEqual(A.reported, 2)
1922 self.assertEqual(B.data['status'], 'MERGED')
1923 self.assertEqual(B.reported, 2)
1924 self.assertEqual(C.data['status'], 'MERGED')
1925 self.assertEqual(C.reported, 2)
1926 self.assertEqual(D.data['status'], 'MERGED')
1927 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001928
James E. Blair4a28a882013-08-23 15:17:33 -07001929 def test_rerun_on_error(self):
1930 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04001931 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07001932 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001933 A.addApproval('code-review', 2)
1934 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07001935 self.waitUntilSettled()
1936
James E. Blair412fba82017-01-26 15:00:50 -08001937 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04001938 self.executor_server.hold_jobs_in_build = False
1939 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07001940 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08001941 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07001942 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1943
James E. Blair412e5582013-04-22 15:50:12 -07001944 def test_statsd(self):
1945 "Test each of the statsd methods used in the scheduler"
1946 import extras
1947 statsd = extras.try_import('statsd.statsd')
1948 statsd.incr('test-incr')
1949 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001950 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001951 self.assertReportedStat('test-incr', '1|c')
1952 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001953 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001954
James E. Blairdad52252014-02-07 16:59:17 -08001955 def test_stuck_job_cleanup(self):
1956 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07001957
1958 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08001959 self.gearman_server.hold_jobs_in_queue = True
1960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001961 A.addApproval('code-review', 2)
1962 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08001963 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07001964 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08001965 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1966
Clint Byrum678e2c32017-03-16 16:27:21 -07001967 self.commitLayoutUpdate('common-config', 'layout-no-jobs')
James E. Blairdad52252014-02-07 16:59:17 -08001968 self.sched.reconfigure(self.config)
1969 self.waitUntilSettled()
1970
James E. Blair18c64442014-03-18 10:14:45 -07001971 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001972 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07001973 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08001974 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1975 self.assertTrue(self.sched._areAllBuildsComplete())
1976
1977 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001978 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001979 self.assertEqual(self.history[0].result, 'SUCCESS')
1980
James E. Blair879dafb2015-07-17 14:04:49 -07001981 def test_file_head(self):
1982 # This is a regression test for an observed bug. A change
1983 # with a file named "HEAD" in the root directory of the repo
1984 # was processed by a merger. It then was unable to reset the
1985 # repo because of:
1986 # GitCommandError: 'git reset --hard HEAD' returned
1987 # with exit code 128
1988 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
1989 # and filename
1990 # Use '--' to separate filenames from revisions'
1991
1992 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04001993 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07001994 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1995
1996 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1997 self.waitUntilSettled()
1998
1999 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2000 self.waitUntilSettled()
2001
2002 self.assertIn('Build succeeded', A.messages[0])
2003 self.assertIn('Build succeeded', B.messages[0])
2004
James E. Blair70c71582013-03-06 08:50:50 -08002005 def test_file_jobs(self):
2006 "Test that file jobs run only when appropriate"
2007 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002008 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002009 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002010 A.addApproval('code-review', 2)
2011 B.addApproval('code-review', 2)
2012 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2013 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002014 self.waitUntilSettled()
2015
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002016 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002017 if x.name == 'project-testfile']
2018
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002019 self.assertEqual(len(testfile_jobs), 1)
2020 self.assertEqual(testfile_jobs[0].changes, '1,2')
2021 self.assertEqual(A.data['status'], 'MERGED')
2022 self.assertEqual(A.reported, 2)
2023 self.assertEqual(B.data['status'], 'MERGED')
2024 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002025
Clint Byrum3ee39f32016-11-17 23:45:07 -08002026 def _test_irrelevant_files_jobs(self, should_skip):
2027 "Test that jobs with irrelevant-files filter run only when appropriate"
2028 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002029 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002030
2031 if should_skip:
2032 files = {'ignoreme': 'ignored\n'}
2033 else:
2034 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002035
2036 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002037 'master',
2038 'test irrelevant-files',
2039 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002040 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2041 self.waitUntilSettled()
2042
2043 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002044 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002045
2046 if should_skip:
2047 self.assertEqual([], tested_change_ids)
2048 else:
2049 self.assertIn(change.data['number'], tested_change_ids)
2050
Clint Byrum3ee39f32016-11-17 23:45:07 -08002051 def test_irrelevant_files_match_skips_job(self):
2052 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002053
Clint Byrum3ee39f32016-11-17 23:45:07 -08002054 def test_irrelevant_files_no_match_runs_job(self):
2055 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002056
Clint Byrum85493602016-11-18 11:59:47 -08002057 def test_inherited_jobs_keep_matchers(self):
2058 self.updateConfigLayout('layout-inheritance')
2059 self.sched.reconfigure(self.config)
2060
2061 files = {'ignoreme': 'ignored\n'}
2062
2063 change = self.fake_gerrit.addFakeChange('org/project',
2064 'master',
2065 'test irrelevant-files',
2066 files=files)
2067 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2068 self.waitUntilSettled()
2069
2070 run_jobs = set([build.name for build in self.history])
2071
2072 self.assertEqual(set(['project-test-nomatch-starts-empty',
2073 'project-test-nomatch-starts-full']), run_jobs)
2074
James E. Blairec056492016-07-22 09:45:56 -07002075 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002076 def test_test_config(self):
2077 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002078 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002079 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002080
James E. Blairc8a1e052014-02-25 09:29:26 -08002081 def test_queue_names(self):
2082 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002083 tenant = self.sched.abide.tenants.get('tenant-one')
2084 source = tenant.layout.pipelines['gate'].source
2085 project1 = source.getProject('org/project1')
2086 project2 = source.getProject('org/project2')
2087 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2088 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2089 self.assertEqual(q1.name, 'integrated')
2090 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002091
James E. Blair64ed6f22013-07-10 14:07:23 -07002092 def test_queue_precedence(self):
2093 "Test that queue precedence works"
2094
2095 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002096 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002097 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2098 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002099 A.addApproval('code-review', 2)
2100 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002101
2102 self.waitUntilSettled()
2103 self.gearman_server.hold_jobs_in_queue = False
2104 self.gearman_server.release()
2105 self.waitUntilSettled()
2106
James E. Blair8de58bd2013-07-18 16:23:33 -07002107 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002108 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002109 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002110 self.waitUntilSettled()
2111
James E. Blair64ed6f22013-07-10 14:07:23 -07002112 self.log.debug(self.history)
2113 self.assertEqual(self.history[0].pipeline, 'gate')
2114 self.assertEqual(self.history[1].pipeline, 'check')
2115 self.assertEqual(self.history[2].pipeline, 'gate')
2116 self.assertEqual(self.history[3].pipeline, 'gate')
2117 self.assertEqual(self.history[4].pipeline, 'check')
2118 self.assertEqual(self.history[5].pipeline, 'check')
2119
Clark Boylana5edbe42014-06-03 16:39:10 -07002120 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002121 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002122 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002123 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002124 A.addApproval('code-review', 2)
2125 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002126 self.waitUntilSettled()
2127
Paul Belanger174a8272017-03-14 13:20:10 -04002128 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002129 self.waitUntilSettled()
2130
James E. Blair1843a552013-07-03 14:19:52 -07002131 port = self.webapp.server.socket.getsockname()[1]
2132
Adam Gandelman77a12c72017-02-03 17:43:43 -08002133 req = urllib.request.Request(
2134 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002135 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002136 headers = f.info()
2137 self.assertIn('Content-Length', headers)
2138 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002139 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2140 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002141 self.assertIn('Access-Control-Allow-Origin', headers)
2142 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002143 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002144 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002145 data = f.read()
2146
Paul Belanger174a8272017-03-14 13:20:10 -04002147 self.executor_server.hold_jobs_in_build = False
2148 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002149 self.waitUntilSettled()
2150
2151 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002152 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002153 for p in data['pipelines']:
2154 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002155 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002156 self.assertEqual(q['window'], 20)
2157 else:
2158 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002159 for head in q['heads']:
2160 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002161 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002162 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002163 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002164 status_jobs.append(job)
2165 self.assertEqual('project-merge', status_jobs[0]['name'])
2166 self.assertEqual('https://server/job/project-merge/0/',
2167 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002168 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002169 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002170 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002171 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002172 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002173 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002174 status_jobs[1]['report_url'])
2175
2176 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002177 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002178 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002179 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002180 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002181
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002182 def test_semaphore_one(self):
2183 "Test semaphores with max=1 (mutex)"
2184 self.updateConfigLayout('layout-semaphore')
James E. Blairaf17a972016-02-03 15:07:18 -08002185 self.sched.reconfigure(self.config)
2186
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002187 self.waitUntilSettled()
2188 tenant = self.sched.abide.tenants.get('openstack')
2189
Paul Belanger174a8272017-03-14 13:20:10 -04002190 self.executor_server.hold_jobs_in_build = True
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002191
James E. Blairaf17a972016-02-03 15:07:18 -08002192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2193 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002194 self.assertFalse('test-semaphore' in
2195 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002196
2197 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2198 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2199 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002200
James E. Blairaf17a972016-02-03 15:07:18 -08002201 self.assertEqual(len(self.builds), 3)
2202 self.assertEqual(self.builds[0].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002203 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002204 self.assertEqual(self.builds[2].name, 'project-test1')
2205
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002206 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002207 self.waitUntilSettled()
2208
2209 self.assertEqual(len(self.builds), 3)
2210 self.assertEqual(self.builds[0].name, 'project-test1')
2211 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002212 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2213 self.assertTrue('test-semaphore' in
2214 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002215
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002216 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002217 self.waitUntilSettled()
2218
2219 self.assertEqual(len(self.builds), 3)
2220 self.assertEqual(self.builds[0].name, 'project-test1')
2221 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002222 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
2223 self.assertTrue('test-semaphore' in
2224 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002225
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002226 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002227 self.waitUntilSettled()
2228
2229 self.assertEqual(len(self.builds), 3)
2230 self.assertEqual(self.builds[0].name, 'project-test1')
2231 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002232 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2233 self.assertTrue('test-semaphore' in
2234 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002235
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002236 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002237 self.waitUntilSettled()
2238
2239 self.assertEqual(len(self.builds), 2)
2240 self.assertEqual(self.builds[0].name, 'project-test1')
2241 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002242 self.assertFalse('test-semaphore' in
2243 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002244
Paul Belanger174a8272017-03-14 13:20:10 -04002245 self.executor_server.hold_jobs_in_build = False
2246 self.executor_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002247
2248 self.waitUntilSettled()
2249 self.assertEqual(len(self.builds), 0)
2250
2251 self.assertEqual(A.reported, 1)
2252 self.assertEqual(B.reported, 1)
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002253 self.assertFalse('test-semaphore' in
2254 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002255
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002256 def test_semaphore_two(self):
2257 "Test semaphores with max>1"
2258 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002259 self.sched.reconfigure(self.config)
2260
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002261 self.waitUntilSettled()
2262 tenant = self.sched.abide.tenants.get('openstack')
2263
2264 self.executor_server.hold_jobs_in_build = True
2265 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2266 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2267 self.assertFalse('test-semaphore-two' in
2268 tenant.semaphore_handler.semaphores)
2269
2270 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2271 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2272 self.waitUntilSettled()
2273
2274 self.assertEqual(len(self.builds), 4)
2275 self.assertEqual(self.builds[0].name, 'project-test1')
2276 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
2277 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2278 self.assertEqual(self.builds[3].name, 'project-test1')
2279 self.assertTrue('test-semaphore-two' in
2280 tenant.semaphore_handler.semaphores)
2281 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2282 'test-semaphore-two', [])), 2)
2283
2284 self.executor_server.release('semaphore-two-test1')
2285 self.waitUntilSettled()
2286
2287 self.assertEqual(len(self.builds), 4)
2288 self.assertEqual(self.builds[0].name, 'project-test1')
2289 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
2290 self.assertEqual(self.builds[2].name, 'project-test1')
2291 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
2292 self.assertTrue('test-semaphore-two' in
2293 tenant.semaphore_handler.semaphores)
2294 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2295 'test-semaphore-two', [])), 2)
2296
2297 self.executor_server.release('semaphore-two-test2')
2298 self.waitUntilSettled()
2299
2300 self.assertEqual(len(self.builds), 4)
2301 self.assertEqual(self.builds[0].name, 'project-test1')
2302 self.assertEqual(self.builds[1].name, 'project-test1')
2303 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
2304 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
2305 self.assertTrue('test-semaphore-two' in
2306 tenant.semaphore_handler.semaphores)
2307 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2308 'test-semaphore-two', [])), 2)
2309
2310 self.executor_server.release('semaphore-two-test1')
2311 self.waitUntilSettled()
2312
2313 self.assertEqual(len(self.builds), 3)
2314 self.assertEqual(self.builds[0].name, 'project-test1')
2315 self.assertEqual(self.builds[1].name, 'project-test1')
2316 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2317 self.assertTrue('test-semaphore-two' in
2318 tenant.semaphore_handler.semaphores)
2319 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2320 'test-semaphore-two', [])), 1)
2321
2322 self.executor_server.release('semaphore-two-test2')
2323 self.waitUntilSettled()
2324
2325 self.assertEqual(len(self.builds), 2)
2326 self.assertEqual(self.builds[0].name, 'project-test1')
2327 self.assertEqual(self.builds[1].name, 'project-test1')
2328 self.assertFalse('test-semaphore-two' in
2329 tenant.semaphore_handler.semaphores)
2330
2331 self.executor_server.hold_jobs_in_build = False
2332 self.executor_server.release()
2333
2334 self.waitUntilSettled()
2335 self.assertEqual(len(self.builds), 0)
2336
2337 self.assertEqual(A.reported, 1)
2338 self.assertEqual(B.reported, 1)
2339
2340 def test_semaphore_abandon(self):
2341 "Test abandon with job semaphores"
2342 self.updateConfigLayout('layout-semaphore')
2343 self.sched.reconfigure(self.config)
2344
2345 self.waitUntilSettled()
2346 tenant = self.sched.abide.tenants.get('openstack')
2347
Paul Belanger174a8272017-03-14 13:20:10 -04002348 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002349
2350 tenant = self.sched.abide.tenants.get('openstack')
2351 check_pipeline = tenant.layout.pipelines['check']
2352
2353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002354 self.assertFalse('test-semaphore' in
2355 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002356
2357 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2358 self.waitUntilSettled()
2359
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002360 self.assertTrue('test-semaphore' in
2361 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002362
2363 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2364 self.waitUntilSettled()
2365
2366 # The check pipeline should be empty
2367 items = check_pipeline.getAllItems()
2368 self.assertEqual(len(items), 0)
2369
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002370 # The semaphore should be released
2371 self.assertFalse('test-semaphore' in
2372 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002373
Paul Belanger174a8272017-03-14 13:20:10 -04002374 self.executor_server.hold_jobs_in_build = False
2375 self.executor_server.release()
Tobias Henkelfb91a492017-02-15 07:29:43 +01002376 self.waitUntilSettled()
2377
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002378 def test_semaphore_reconfigure(self):
2379 "Test reconfigure with job semaphores"
2380 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002381 self.sched.reconfigure(self.config)
2382
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002383 self.waitUntilSettled()
2384 tenant = self.sched.abide.tenants.get('openstack')
2385
Paul Belanger174a8272017-03-14 13:20:10 -04002386 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002387
2388 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002389 self.assertFalse('test-semaphore' in
2390 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002391
2392 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2393 self.waitUntilSettled()
2394
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002395 self.assertTrue('test-semaphore' in
2396 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002397
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002398 # reconfigure without layout change
Tobias Henkelfb91a492017-02-15 07:29:43 +01002399 self.sched.reconfigure(self.config)
2400 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002401 tenant = self.sched.abide.tenants.get('openstack')
2402
2403 # semaphore still must be held
2404 self.assertTrue('test-semaphore' in
2405 tenant.semaphore_handler.semaphores)
2406
2407 self.updateConfigLayout('layout-semaphore-reconfiguration')
2408 self.sched.reconfigure(self.config)
2409 self.waitUntilSettled()
2410 tenant = self.sched.abide.tenants.get('openstack')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002411
Paul Belanger174a8272017-03-14 13:20:10 -04002412 self.executor_server.release('project-test1')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002413 self.waitUntilSettled()
2414
2415 # There should be no builds anymore
2416 self.assertEqual(len(self.builds), 0)
2417
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002418 # The semaphore should be released
2419 self.assertFalse('test-semaphore' in
2420 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002421
James E. Blaircdccd972013-07-01 12:10:22 -07002422 def test_live_reconfiguration(self):
2423 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002424 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002425 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002426 A.addApproval('code-review', 2)
2427 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002428 self.waitUntilSettled()
2429
2430 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002431 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002432
Paul Belanger174a8272017-03-14 13:20:10 -04002433 self.executor_server.hold_jobs_in_build = False
2434 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002435 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002436 self.assertEqual(self.getJobFromHistory('project-merge').result,
2437 'SUCCESS')
2438 self.assertEqual(self.getJobFromHistory('project-test1').result,
2439 'SUCCESS')
2440 self.assertEqual(self.getJobFromHistory('project-test2').result,
2441 'SUCCESS')
2442 self.assertEqual(A.data['status'], 'MERGED')
2443 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002444
James E. Blairec056492016-07-22 09:45:56 -07002445 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002446 def test_live_reconfiguration_merge_conflict(self):
2447 # A real-world bug: a change in a gate queue has a merge
2448 # conflict and a job is added to its project while it's
2449 # sitting in the queue. The job gets added to the change and
2450 # enqueued and the change gets stuck.
2451 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002452 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002453
2454 # This change is fine. It's here to stop the queue long
2455 # enough for the next change to be subject to the
2456 # reconfiguration, as well as to provide a conflict for the
2457 # next change. This change will succeed and merge.
2458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2459 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002460 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002461
2462 # This change will be in merge conflict. During the
2463 # reconfiguration, we will add a job. We want to make sure
2464 # that doesn't cause it to get stuck.
2465 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2466 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002467 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002468
James E. Blair8b5408c2016-08-08 15:37:46 -07002469 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2470 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002471
2472 self.waitUntilSettled()
2473
2474 # No jobs have run yet
2475 self.assertEqual(A.data['status'], 'NEW')
2476 self.assertEqual(A.reported, 1)
2477 self.assertEqual(B.data['status'], 'NEW')
2478 self.assertEqual(B.reported, 1)
2479 self.assertEqual(len(self.history), 0)
2480
2481 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002482 self.updateConfigLayout(
2483 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002484 self.sched.reconfigure(self.config)
2485 self.waitUntilSettled()
2486
Paul Belanger174a8272017-03-14 13:20:10 -04002487 self.executor_server.hold_jobs_in_build = False
2488 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002489 self.waitUntilSettled()
2490
2491 self.assertEqual(A.data['status'], 'MERGED')
2492 self.assertEqual(A.reported, 2)
2493 self.assertEqual(B.data['status'], 'NEW')
2494 self.assertEqual(B.reported, 2)
2495 self.assertEqual(self.getJobFromHistory('project-merge').result,
2496 'SUCCESS')
2497 self.assertEqual(self.getJobFromHistory('project-test1').result,
2498 'SUCCESS')
2499 self.assertEqual(self.getJobFromHistory('project-test2').result,
2500 'SUCCESS')
2501 self.assertEqual(self.getJobFromHistory('project-test3').result,
2502 'SUCCESS')
2503 self.assertEqual(len(self.history), 4)
2504
James E. Blairec056492016-07-22 09:45:56 -07002505 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002506 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002507 # An extrapolation of test_live_reconfiguration_merge_conflict
2508 # that tests a job added to a job tree with a failed root does
2509 # not run.
2510 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002511 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002512
2513 # This change is fine. It's here to stop the queue long
2514 # enough for the next change to be subject to the
2515 # reconfiguration. This change will succeed and merge.
2516 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2517 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002518 A.addApproval('code-review', 2)
2519 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002520 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002521 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002522 self.waitUntilSettled()
2523
2524 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002525 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002526 B.addApproval('code-review', 2)
2527 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002528 self.waitUntilSettled()
2529
Paul Belanger174a8272017-03-14 13:20:10 -04002530 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002531 self.waitUntilSettled()
2532
2533 # Both -merge jobs have run, but no others.
2534 self.assertEqual(A.data['status'], 'NEW')
2535 self.assertEqual(A.reported, 1)
2536 self.assertEqual(B.data['status'], 'NEW')
2537 self.assertEqual(B.reported, 1)
2538 self.assertEqual(self.history[0].result, 'SUCCESS')
2539 self.assertEqual(self.history[0].name, 'project-merge')
2540 self.assertEqual(self.history[1].result, 'FAILURE')
2541 self.assertEqual(self.history[1].name, 'project-merge')
2542 self.assertEqual(len(self.history), 2)
2543
2544 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002545 self.updateConfigLayout(
2546 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002547 self.sched.reconfigure(self.config)
2548 self.waitUntilSettled()
2549
Paul Belanger174a8272017-03-14 13:20:10 -04002550 self.executor_server.hold_jobs_in_build = False
2551 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002552 self.waitUntilSettled()
2553
2554 self.assertEqual(A.data['status'], 'MERGED')
2555 self.assertEqual(A.reported, 2)
2556 self.assertEqual(B.data['status'], 'NEW')
2557 self.assertEqual(B.reported, 2)
2558 self.assertEqual(self.history[0].result, 'SUCCESS')
2559 self.assertEqual(self.history[0].name, 'project-merge')
2560 self.assertEqual(self.history[1].result, 'FAILURE')
2561 self.assertEqual(self.history[1].name, 'project-merge')
2562 self.assertEqual(self.history[2].result, 'SUCCESS')
2563 self.assertEqual(self.history[3].result, 'SUCCESS')
2564 self.assertEqual(self.history[4].result, 'SUCCESS')
2565 self.assertEqual(len(self.history), 5)
2566
James E. Blairec056492016-07-22 09:45:56 -07002567 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002568 def test_live_reconfiguration_failed_job(self):
2569 # Test that a change with a removed failing job does not
2570 # disrupt reconfiguration. If a change has a failed job and
2571 # that job is removed during a reconfiguration, we observed a
2572 # bug where the code to re-set build statuses would run on
2573 # that build and raise an exception because the job no longer
2574 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002575 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002576
2577 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2578
2579 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002580 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002581
2582 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2583 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002584 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002585 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002586 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002587 self.waitUntilSettled()
2588
2589 self.assertEqual(A.data['status'], 'NEW')
2590 self.assertEqual(A.reported, 0)
2591
2592 self.assertEqual(self.getJobFromHistory('project-merge').result,
2593 'SUCCESS')
2594 self.assertEqual(self.getJobFromHistory('project-test1').result,
2595 'FAILURE')
2596 self.assertEqual(len(self.history), 2)
2597
2598 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002599 self.updateConfigLayout(
2600 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002601 self.sched.reconfigure(self.config)
2602 self.waitUntilSettled()
2603
Paul Belanger174a8272017-03-14 13:20:10 -04002604 self.executor_server.hold_jobs_in_build = False
2605 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002606 self.waitUntilSettled()
2607
2608 self.assertEqual(self.getJobFromHistory('project-test2').result,
2609 'SUCCESS')
2610 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2611 'SUCCESS')
2612 self.assertEqual(len(self.history), 4)
2613
2614 self.assertEqual(A.data['status'], 'NEW')
2615 self.assertEqual(A.reported, 1)
2616 self.assertIn('Build succeeded', A.messages[0])
2617 # Ensure the removed job was not included in the report.
2618 self.assertNotIn('project-test1', A.messages[0])
2619
James E. Blairec056492016-07-22 09:45:56 -07002620 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002621 def test_live_reconfiguration_shared_queue(self):
2622 # Test that a change with a failing job which was removed from
2623 # this project but otherwise still exists in the system does
2624 # not disrupt reconfiguration.
2625
Paul Belanger174a8272017-03-14 13:20:10 -04002626 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002627
2628 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2629
Paul Belanger174a8272017-03-14 13:20:10 -04002630 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002631
2632 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2633 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002634 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002635 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002636 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002637 self.waitUntilSettled()
2638
2639 self.assertEqual(A.data['status'], 'NEW')
2640 self.assertEqual(A.reported, 0)
2641
2642 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2643 'SUCCESS')
2644 self.assertEqual(self.getJobFromHistory(
2645 'project1-project2-integration').result, 'FAILURE')
2646 self.assertEqual(len(self.history), 2)
2647
2648 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002649 self.updateConfigLayout(
2650 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002651 self.sched.reconfigure(self.config)
2652 self.waitUntilSettled()
2653
Paul Belanger174a8272017-03-14 13:20:10 -04002654 self.executor_server.hold_jobs_in_build = False
2655 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002656 self.waitUntilSettled()
2657
2658 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2659 'SUCCESS')
2660 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2661 'SUCCESS')
2662 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2663 'SUCCESS')
2664 self.assertEqual(self.getJobFromHistory(
2665 'project1-project2-integration').result, 'FAILURE')
2666 self.assertEqual(len(self.history), 4)
2667
2668 self.assertEqual(A.data['status'], 'NEW')
2669 self.assertEqual(A.reported, 1)
2670 self.assertIn('Build succeeded', A.messages[0])
2671 # Ensure the removed job was not included in the report.
2672 self.assertNotIn('project1-project2-integration', A.messages[0])
2673
James E. Blairec056492016-07-22 09:45:56 -07002674 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002675 def test_double_live_reconfiguration_shared_queue(self):
2676 # This was a real-world regression. A change is added to
2677 # gate; a reconfigure happens, a second change which depends
2678 # on the first is added, and a second reconfiguration happens.
2679 # Ensure that both changes merge.
2680
2681 # A failure may indicate incorrect caching or cleaning up of
2682 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002683 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002684
2685 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2686 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2687 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002688 A.addApproval('code-review', 2)
2689 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002690
2691 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002692 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002693 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002694 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002695 self.waitUntilSettled()
2696
2697 # Reconfigure (with only one change in the pipeline).
2698 self.sched.reconfigure(self.config)
2699 self.waitUntilSettled()
2700
2701 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002702 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002703 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002704 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002705 self.waitUntilSettled()
2706
2707 # Reconfigure (with both in the pipeline).
2708 self.sched.reconfigure(self.config)
2709 self.waitUntilSettled()
2710
Paul Belanger174a8272017-03-14 13:20:10 -04002711 self.executor_server.hold_jobs_in_build = False
2712 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002713 self.waitUntilSettled()
2714
2715 self.assertEqual(len(self.history), 8)
2716
2717 self.assertEqual(A.data['status'], 'MERGED')
2718 self.assertEqual(A.reported, 2)
2719 self.assertEqual(B.data['status'], 'MERGED')
2720 self.assertEqual(B.reported, 2)
2721
James E. Blairec056492016-07-22 09:45:56 -07002722 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002723 def test_live_reconfiguration_del_project(self):
2724 # Test project deletion from layout
2725 # while changes are enqueued
2726
Paul Belanger174a8272017-03-14 13:20:10 -04002727 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002728 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2729 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2730 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2731
2732 # A Depends-On: B
2733 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2734 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002735 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002736
2737 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2738 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2739 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002740 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002741 self.waitUntilSettled()
2742 self.assertEqual(len(self.builds), 5)
2743
2744 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002745 self.updateConfigLayout(
2746 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002747 self.sched.reconfigure(self.config)
2748 self.waitUntilSettled()
2749
2750 # Builds for C aborted, builds for A succeed,
2751 # and have change B applied ahead
2752 job_c = self.getJobFromHistory('project1-test1')
2753 self.assertEqual(job_c.changes, '3,1')
2754 self.assertEqual(job_c.result, 'ABORTED')
2755
Paul Belanger174a8272017-03-14 13:20:10 -04002756 self.executor_server.hold_jobs_in_build = False
2757 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002758 self.waitUntilSettled()
2759
2760 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2761 '2,1 1,1')
2762
2763 self.assertEqual(A.data['status'], 'NEW')
2764 self.assertEqual(B.data['status'], 'NEW')
2765 self.assertEqual(C.data['status'], 'NEW')
2766 self.assertEqual(A.reported, 1)
2767 self.assertEqual(B.reported, 0)
2768 self.assertEqual(C.reported, 0)
2769
2770 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2771 self.assertIn('Build succeeded', A.messages[0])
2772
James E. Blairec056492016-07-22 09:45:56 -07002773 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002774 def test_live_reconfiguration_functions(self):
2775 "Test live reconfiguration with a custom function"
2776 self.worker.registerFunction('build:node-project-test1:debian')
2777 self.worker.registerFunction('build:node-project-test1:wheezy')
2778 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002779 A.addApproval('code-review', 2)
2780 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002781 self.waitUntilSettled()
2782
2783 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2784 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2785 'debian')
2786 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2787
James E. Blairf84026c2015-12-08 16:11:46 -08002788 self.updateConfigLayout(
2789 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002790 self.sched.reconfigure(self.config)
2791 self.worker.build_history = []
2792
2793 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002794 B.addApproval('code-review', 2)
2795 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002796 self.waitUntilSettled()
2797
2798 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2799 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2800 'wheezy')
2801 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2802
James E. Blairec056492016-07-22 09:45:56 -07002803 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002804 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002805 self.updateConfigLayout(
2806 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002807 self.sched.reconfigure(self.config)
2808
2809 self.init_repo("org/new-project")
2810 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2811
James E. Blair8b5408c2016-08-08 15:37:46 -07002812 A.addApproval('code-review', 2)
2813 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002814 self.waitUntilSettled()
2815 self.assertEqual(self.getJobFromHistory('project-merge').result,
2816 'SUCCESS')
2817 self.assertEqual(self.getJobFromHistory('project-test1').result,
2818 'SUCCESS')
2819 self.assertEqual(self.getJobFromHistory('project-test2').result,
2820 'SUCCESS')
2821 self.assertEqual(A.data['status'], 'MERGED')
2822 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002823
Clark Boylan6dbbc482013-10-18 10:57:31 -07002824 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002825 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002826 self.sched.reconfigure(self.config)
2827
2828 self.init_repo("org/delete-project")
2829 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2830
James E. Blair8b5408c2016-08-08 15:37:46 -07002831 A.addApproval('code-review', 2)
2832 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002833 self.waitUntilSettled()
2834 self.assertEqual(self.getJobFromHistory('project-merge').result,
2835 'SUCCESS')
2836 self.assertEqual(self.getJobFromHistory('project-test1').result,
2837 'SUCCESS')
2838 self.assertEqual(self.getJobFromHistory('project-test2').result,
2839 'SUCCESS')
2840 self.assertEqual(A.data['status'], 'MERGED')
2841 self.assertEqual(A.reported, 2)
2842
2843 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002844 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002845 if os.path.exists(os.path.join(self.merger_src_root, p)):
2846 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002847 if os.path.exists(os.path.join(self.executor_src_root, p)):
2848 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002849
2850 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2851
James E. Blair8b5408c2016-08-08 15:37:46 -07002852 B.addApproval('code-review', 2)
2853 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002854 self.waitUntilSettled()
2855 self.assertEqual(self.getJobFromHistory('project-merge').result,
2856 'SUCCESS')
2857 self.assertEqual(self.getJobFromHistory('project-test1').result,
2858 'SUCCESS')
2859 self.assertEqual(self.getJobFromHistory('project-test2').result,
2860 'SUCCESS')
2861 self.assertEqual(B.data['status'], 'MERGED')
2862 self.assertEqual(B.reported, 2)
2863
James E. Blair456f2fb2016-02-09 09:29:33 -08002864 def test_tags(self):
2865 "Test job tags"
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002866 self.updateConfigLayout('layout-tags')
James E. Blair456f2fb2016-02-09 09:29:33 -08002867 self.sched.reconfigure(self.config)
2868
2869 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2870 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2871 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2872 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2873 self.waitUntilSettled()
2874
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002875 self.assertEqual(len(self.history), 8)
2876
2877 results = {self.getJobFromHistory('merge',
2878 project='org/project1').uuid: 'extratag merge',
2879 self.getJobFromHistory('merge',
2880 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002881
2882 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002883 self.assertEqual(results.get(build.uuid, ''),
2884 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002885
James E. Blair63bb0ef2013-07-29 17:14:51 -07002886 def test_timer(self):
2887 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002888 self.executor_server.hold_jobs_in_build = True
Clint Byrum5e729e92017-02-08 06:34:57 -08002889 self.updateConfigLayout('layout-timer')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002890 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002891
Clark Boylan3ee090a2014-04-03 20:55:09 -07002892 # The pipeline triggers every second, so we should have seen
2893 # several by now.
2894 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002895 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002896
2897 self.assertEqual(len(self.builds), 2)
2898
James E. Blair63bb0ef2013-07-29 17:14:51 -07002899 port = self.webapp.server.socket.getsockname()[1]
2900
Clint Byrum5e729e92017-02-08 06:34:57 -08002901 req = urllib.request.Request(
2902 "http://localhost:%s/openstack/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002903 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002904 data = f.read()
2905
Paul Belanger174a8272017-03-14 13:20:10 -04002906 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002907 # Stop queuing timer triggered jobs so that the assertions
2908 # below don't race against more jobs being queued.
Clint Byrum5e729e92017-02-08 06:34:57 -08002909 self.commitLayoutUpdate('layout-timer', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002910 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002911 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002912 self.waitUntilSettled()
2913
2914 self.assertEqual(self.getJobFromHistory(
2915 'project-bitrot-stable-old').result, 'SUCCESS')
2916 self.assertEqual(self.getJobFromHistory(
2917 'project-bitrot-stable-older').result, 'SUCCESS')
2918
2919 data = json.loads(data)
2920 status_jobs = set()
2921 for p in data['pipelines']:
2922 for q in p['change_queues']:
2923 for head in q['heads']:
2924 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002925 for job in change['jobs']:
2926 status_jobs.add(job['name'])
2927 self.assertIn('project-bitrot-stable-old', status_jobs)
2928 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002929
James E. Blair4f6033c2014-03-27 15:49:09 -07002930 def test_idle(self):
2931 "Test that frequent periodic jobs work"
Paul Belanger174a8272017-03-14 13:20:10 -04002932 self.executor_server.hold_jobs_in_build = True
Clint Byrum58264dc2017-02-07 21:21:22 -08002933 self.updateConfigLayout('layout-idle')
James E. Blair4f6033c2014-03-27 15:49:09 -07002934
Clark Boylan3ee090a2014-04-03 20:55:09 -07002935 for x in range(1, 3):
2936 # Test that timer triggers periodic jobs even across
2937 # layout config reloads.
2938 # Start timer trigger
Clark Boylan3ee090a2014-04-03 20:55:09 -07002939 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002940 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002941
Clark Boylan3ee090a2014-04-03 20:55:09 -07002942 # The pipeline triggers every second, so we should have seen
2943 # several by now.
2944 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002945
2946 # Stop queuing timer triggered jobs so that the assertions
2947 # below don't race against more jobs being queued.
Clint Byrum58264dc2017-02-07 21:21:22 -08002948 before = self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002949 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002950 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002951 self.assertEqual(len(self.builds), 2,
2952 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002953 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002954 self.waitUntilSettled()
2955 self.assertEqual(len(self.builds), 0)
2956 self.assertEqual(len(self.history), x * 2)
Clint Byrum58264dc2017-02-07 21:21:22 -08002957 # Revert back to layout-idle
2958 repo = git.Repo(os.path.join(self.test_root,
2959 'upstream',
2960 'layout-idle'))
2961 repo.git.reset('--hard', before)
James E. Blair4f6033c2014-03-27 15:49:09 -07002962
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002963 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002964 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002965 self.sched.reconfigure(self.config)
2966
2967 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2968 self.waitUntilSettled()
2969
2970 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2971 self.waitUntilSettled()
2972
James E. Blairff80a2f2013-12-27 13:24:06 -08002973 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002974
2975 # A.messages only holds what FakeGerrit places in it. Thus we
2976 # work on the knowledge of what the first message should be as
2977 # it is only configured to go to SMTP.
2978
2979 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002980 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002981 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002982 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002983 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002984 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002985
2986 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002987 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002988 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002989 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002990 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002991 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002992
James E. Blaire5910202013-12-27 09:50:31 -08002993 def test_timer_smtp(self):
2994 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002995 self.executor_server.hold_jobs_in_build = True
Clint Byrum7b71b2f2017-02-08 07:00:52 -08002996 self.updateConfigLayout('layout-timer-smtp')
James E. Blaire5910202013-12-27 09:50:31 -08002997 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002998
Clark Boylan3ee090a2014-04-03 20:55:09 -07002999 # The pipeline triggers every second, so we should have seen
3000 # several by now.
3001 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003002 self.waitUntilSettled()
3003
Clark Boylan3ee090a2014-04-03 20:55:09 -07003004 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003005 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003006 self.waitUntilSettled()
3007 self.assertEqual(len(self.history), 2)
3008
James E. Blaire5910202013-12-27 09:50:31 -08003009 self.assertEqual(self.getJobFromHistory(
3010 'project-bitrot-stable-old').result, 'SUCCESS')
3011 self.assertEqual(self.getJobFromHistory(
3012 'project-bitrot-stable-older').result, 'SUCCESS')
3013
James E. Blairff80a2f2013-12-27 13:24:06 -08003014 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003015
3016 # A.messages only holds what FakeGerrit places in it. Thus we
3017 # work on the knowledge of what the first message should be as
3018 # it is only configured to go to SMTP.
3019
3020 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003021 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003022 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003023 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003024 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003025 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003026
Clark Boylan3ee090a2014-04-03 20:55:09 -07003027 # Stop queuing timer triggered jobs and let any that may have
3028 # queued through so that end of test assertions pass.
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003029 self.commitLayoutUpdate('layout-timer-smtp', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003030 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003031 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003032 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003033 self.waitUntilSettled()
3034
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003035 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003036 def test_timer_sshkey(self):
3037 "Test that a periodic job can setup SSH key authentication"
3038 self.worker.hold_jobs_in_build = True
3039 self.config.set('zuul', 'layout_config',
3040 'tests/fixtures/layout-timer.yaml')
3041 self.sched.reconfigure(self.config)
3042 self.registerJobs()
3043
3044 # The pipeline triggers every second, so we should have seen
3045 # several by now.
3046 time.sleep(5)
3047 self.waitUntilSettled()
3048
3049 self.assertEqual(len(self.builds), 2)
3050
3051 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3052 self.assertTrue(os.path.isfile(ssh_wrapper))
3053 with open(ssh_wrapper) as f:
3054 ssh_wrapper_content = f.read()
3055 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3056 # In the unit tests Merger runs in the same process,
3057 # so we see its' environment variables
3058 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3059
3060 self.worker.release('.*')
3061 self.waitUntilSettled()
3062 self.assertEqual(len(self.history), 2)
3063
3064 self.assertEqual(self.getJobFromHistory(
3065 'project-bitrot-stable-old').result, 'SUCCESS')
3066 self.assertEqual(self.getJobFromHistory(
3067 'project-bitrot-stable-older').result, 'SUCCESS')
3068
3069 # Stop queuing timer triggered jobs and let any that may have
3070 # queued through so that end of test assertions pass.
3071 self.config.set('zuul', 'layout_config',
3072 'tests/fixtures/layout-no-timer.yaml')
3073 self.sched.reconfigure(self.config)
3074 self.registerJobs()
3075 self.waitUntilSettled()
3076 self.worker.release('.*')
3077 self.waitUntilSettled()
3078
James E. Blair91e34592015-07-31 16:45:59 -07003079 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003080 "Test that the RPC client can enqueue a change"
3081 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003082 A.addApproval('code-review', 2)
3083 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003084
3085 client = zuul.rpcclient.RPCClient('127.0.0.1',
3086 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003087 r = client.enqueue(tenant='tenant-one',
3088 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003089 project='org/project',
3090 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003091 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003092 self.waitUntilSettled()
3093 self.assertEqual(self.getJobFromHistory('project-merge').result,
3094 'SUCCESS')
3095 self.assertEqual(self.getJobFromHistory('project-test1').result,
3096 'SUCCESS')
3097 self.assertEqual(self.getJobFromHistory('project-test2').result,
3098 'SUCCESS')
3099 self.assertEqual(A.data['status'], 'MERGED')
3100 self.assertEqual(A.reported, 2)
3101 self.assertEqual(r, True)
3102
James E. Blair91e34592015-07-31 16:45:59 -07003103 def test_client_enqueue_ref(self):
3104 "Test that the RPC client can enqueue a ref"
3105
3106 client = zuul.rpcclient.RPCClient('127.0.0.1',
3107 self.gearman_server.port)
3108 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003109 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003110 pipeline='post',
3111 project='org/project',
3112 trigger='gerrit',
3113 ref='master',
3114 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3115 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3116 self.waitUntilSettled()
3117 job_names = [x.name for x in self.history]
3118 self.assertEqual(len(self.history), 1)
3119 self.assertIn('project-post', job_names)
3120 self.assertEqual(r, True)
3121
James E. Blairad28e912013-11-27 10:43:22 -08003122 def test_client_enqueue_negative(self):
3123 "Test that the RPC client returns errors"
3124 client = zuul.rpcclient.RPCClient('127.0.0.1',
3125 self.gearman_server.port)
3126 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003127 "Invalid tenant"):
3128 r = client.enqueue(tenant='tenant-foo',
3129 pipeline='gate',
3130 project='org/project',
3131 trigger='gerrit',
3132 change='1,1')
3133 client.shutdown()
3134 self.assertEqual(r, False)
3135
3136 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003137 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003138 r = client.enqueue(tenant='tenant-one',
3139 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003140 project='project-does-not-exist',
3141 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003142 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003143 client.shutdown()
3144 self.assertEqual(r, False)
3145
3146 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3147 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003148 r = client.enqueue(tenant='tenant-one',
3149 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003150 project='org/project',
3151 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003152 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003153 client.shutdown()
3154 self.assertEqual(r, False)
3155
3156 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3157 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003158 r = client.enqueue(tenant='tenant-one',
3159 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003160 project='org/project',
3161 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003162 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003163 client.shutdown()
3164 self.assertEqual(r, False)
3165
3166 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3167 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003168 r = client.enqueue(tenant='tenant-one',
3169 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003170 project='org/project',
3171 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003172 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003173 client.shutdown()
3174 self.assertEqual(r, False)
3175
3176 self.waitUntilSettled()
3177 self.assertEqual(len(self.history), 0)
3178 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003179
3180 def test_client_promote(self):
3181 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003182 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003183 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3184 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3185 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003186 A.addApproval('code-review', 2)
3187 B.addApproval('code-review', 2)
3188 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003189
James E. Blair8b5408c2016-08-08 15:37:46 -07003190 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3191 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3192 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003193
3194 self.waitUntilSettled()
3195
Paul Belangerbaca3132016-11-04 12:49:54 -04003196 tenant = self.sched.abide.tenants.get('tenant-one')
3197 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003198 enqueue_times = {}
3199 for item in items:
3200 enqueue_times[str(item.change)] = item.enqueue_time
3201
James E. Blair36658cf2013-12-06 17:53:48 -08003202 client = zuul.rpcclient.RPCClient('127.0.0.1',
3203 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003204 r = client.promote(tenant='tenant-one',
3205 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003206 change_ids=['2,1', '3,1'])
3207
Sean Daguef39b9ca2014-01-10 21:34:35 -05003208 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003209 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003210 for item in items:
3211 self.assertEqual(
3212 enqueue_times[str(item.change)], item.enqueue_time)
3213
James E. Blair78acec92014-02-06 07:11:32 -08003214 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003215 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003216 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003217 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003218 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003219 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003220 self.waitUntilSettled()
3221
3222 self.assertEqual(len(self.builds), 6)
3223 self.assertEqual(self.builds[0].name, 'project-test1')
3224 self.assertEqual(self.builds[1].name, 'project-test2')
3225 self.assertEqual(self.builds[2].name, 'project-test1')
3226 self.assertEqual(self.builds[3].name, 'project-test2')
3227 self.assertEqual(self.builds[4].name, 'project-test1')
3228 self.assertEqual(self.builds[5].name, 'project-test2')
3229
Paul Belangerbaca3132016-11-04 12:49:54 -04003230 self.assertTrue(self.builds[0].hasChanges(B))
3231 self.assertFalse(self.builds[0].hasChanges(A))
3232 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003233
Paul Belangerbaca3132016-11-04 12:49:54 -04003234 self.assertTrue(self.builds[2].hasChanges(B))
3235 self.assertTrue(self.builds[2].hasChanges(C))
3236 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003237
Paul Belangerbaca3132016-11-04 12:49:54 -04003238 self.assertTrue(self.builds[4].hasChanges(B))
3239 self.assertTrue(self.builds[4].hasChanges(C))
3240 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003241
Paul Belanger174a8272017-03-14 13:20:10 -04003242 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003243 self.waitUntilSettled()
3244
3245 self.assertEqual(A.data['status'], 'MERGED')
3246 self.assertEqual(A.reported, 2)
3247 self.assertEqual(B.data['status'], 'MERGED')
3248 self.assertEqual(B.reported, 2)
3249 self.assertEqual(C.data['status'], 'MERGED')
3250 self.assertEqual(C.reported, 2)
3251
3252 client.shutdown()
3253 self.assertEqual(r, True)
3254
3255 def test_client_promote_dependent(self):
3256 "Test that the RPC client can promote a dependent change"
3257 # C (depends on B) -> B -> A ; then promote C to get:
3258 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003259 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3261 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3262 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3263
3264 C.setDependsOn(B, 1)
3265
James E. Blair8b5408c2016-08-08 15:37:46 -07003266 A.addApproval('code-review', 2)
3267 B.addApproval('code-review', 2)
3268 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003269
James E. Blair8b5408c2016-08-08 15:37:46 -07003270 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3271 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3272 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003273
3274 self.waitUntilSettled()
3275
3276 client = zuul.rpcclient.RPCClient('127.0.0.1',
3277 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003278 r = client.promote(tenant='tenant-one',
3279 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003280 change_ids=['3,1'])
3281
James E. Blair78acec92014-02-06 07:11:32 -08003282 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003283 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003285 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003287 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003288 self.waitUntilSettled()
3289
3290 self.assertEqual(len(self.builds), 6)
3291 self.assertEqual(self.builds[0].name, 'project-test1')
3292 self.assertEqual(self.builds[1].name, 'project-test2')
3293 self.assertEqual(self.builds[2].name, 'project-test1')
3294 self.assertEqual(self.builds[3].name, 'project-test2')
3295 self.assertEqual(self.builds[4].name, 'project-test1')
3296 self.assertEqual(self.builds[5].name, 'project-test2')
3297
Paul Belangerbaca3132016-11-04 12:49:54 -04003298 self.assertTrue(self.builds[0].hasChanges(B))
3299 self.assertFalse(self.builds[0].hasChanges(A))
3300 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003301
Paul Belangerbaca3132016-11-04 12:49:54 -04003302 self.assertTrue(self.builds[2].hasChanges(B))
3303 self.assertTrue(self.builds[2].hasChanges(C))
3304 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003305
Paul Belangerbaca3132016-11-04 12:49:54 -04003306 self.assertTrue(self.builds[4].hasChanges(B))
3307 self.assertTrue(self.builds[4].hasChanges(C))
3308 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003309
Paul Belanger174a8272017-03-14 13:20:10 -04003310 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003311 self.waitUntilSettled()
3312
3313 self.assertEqual(A.data['status'], 'MERGED')
3314 self.assertEqual(A.reported, 2)
3315 self.assertEqual(B.data['status'], 'MERGED')
3316 self.assertEqual(B.reported, 2)
3317 self.assertEqual(C.data['status'], 'MERGED')
3318 self.assertEqual(C.reported, 2)
3319
3320 client.shutdown()
3321 self.assertEqual(r, True)
3322
3323 def test_client_promote_negative(self):
3324 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003325 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003326 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003327 A.addApproval('code-review', 2)
3328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003329 self.waitUntilSettled()
3330
3331 client = zuul.rpcclient.RPCClient('127.0.0.1',
3332 self.gearman_server.port)
3333
3334 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003335 r = client.promote(tenant='tenant-one',
3336 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003337 change_ids=['2,1', '3,1'])
3338 client.shutdown()
3339 self.assertEqual(r, False)
3340
3341 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003342 r = client.promote(tenant='tenant-one',
3343 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003344 change_ids=['4,1'])
3345 client.shutdown()
3346 self.assertEqual(r, False)
3347
Paul Belanger174a8272017-03-14 13:20:10 -04003348 self.executor_server.hold_jobs_in_build = False
3349 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003350 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003351
3352 def test_queue_rate_limiting(self):
3353 "Test that DependentPipelines are rate limited with dep across window"
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003354 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003355 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003356 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003357 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3358 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3359 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3360
3361 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003362 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003363
James E. Blair8b5408c2016-08-08 15:37:46 -07003364 A.addApproval('code-review', 2)
3365 B.addApproval('code-review', 2)
3366 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003367
James E. Blair8b5408c2016-08-08 15:37:46 -07003368 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3369 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3370 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003371 self.waitUntilSettled()
3372
3373 # Only A and B will have their merge jobs queued because
3374 # window is 2.
3375 self.assertEqual(len(self.builds), 2)
3376 self.assertEqual(self.builds[0].name, 'project-merge')
3377 self.assertEqual(self.builds[1].name, 'project-merge')
3378
Paul Belanger174a8272017-03-14 13:20:10 -04003379 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003380 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003381 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003382 self.waitUntilSettled()
3383
3384 # Only A and B will have their test jobs queued because
3385 # window is 2.
3386 self.assertEqual(len(self.builds), 4)
3387 self.assertEqual(self.builds[0].name, 'project-test1')
3388 self.assertEqual(self.builds[1].name, 'project-test2')
3389 self.assertEqual(self.builds[2].name, 'project-test1')
3390 self.assertEqual(self.builds[3].name, 'project-test2')
3391
Paul Belanger174a8272017-03-14 13:20:10 -04003392 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003393 self.waitUntilSettled()
3394
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003395 tenant = self.sched.abide.tenants.get('openstack')
3396 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003397 # A failed so window is reduced by 1 to 1.
3398 self.assertEqual(queue.window, 1)
3399 self.assertEqual(queue.window_floor, 1)
3400 self.assertEqual(A.data['status'], 'NEW')
3401
3402 # Gate is reset and only B's merge job is queued because
3403 # window shrunk to 1.
3404 self.assertEqual(len(self.builds), 1)
3405 self.assertEqual(self.builds[0].name, 'project-merge')
3406
Paul Belanger174a8272017-03-14 13:20:10 -04003407 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003408 self.waitUntilSettled()
3409
3410 # Only B's test jobs are queued because window is still 1.
3411 self.assertEqual(len(self.builds), 2)
3412 self.assertEqual(self.builds[0].name, 'project-test1')
3413 self.assertEqual(self.builds[1].name, 'project-test2')
3414
Paul Belanger174a8272017-03-14 13:20:10 -04003415 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003416 self.waitUntilSettled()
3417
3418 # B was successfully merged so window is increased to 2.
3419 self.assertEqual(queue.window, 2)
3420 self.assertEqual(queue.window_floor, 1)
3421 self.assertEqual(B.data['status'], 'MERGED')
3422
3423 # Only C is left and its merge job is queued.
3424 self.assertEqual(len(self.builds), 1)
3425 self.assertEqual(self.builds[0].name, 'project-merge')
3426
Paul Belanger174a8272017-03-14 13:20:10 -04003427 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003428 self.waitUntilSettled()
3429
3430 # After successful merge job the test jobs for C are queued.
3431 self.assertEqual(len(self.builds), 2)
3432 self.assertEqual(self.builds[0].name, 'project-test1')
3433 self.assertEqual(self.builds[1].name, 'project-test2')
3434
Paul Belanger174a8272017-03-14 13:20:10 -04003435 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003436 self.waitUntilSettled()
3437
3438 # C successfully merged so window is bumped to 3.
3439 self.assertEqual(queue.window, 3)
3440 self.assertEqual(queue.window_floor, 1)
3441 self.assertEqual(C.data['status'], 'MERGED')
3442
3443 def test_queue_rate_limiting_dependent(self):
3444 "Test that DependentPipelines are rate limited with dep in window"
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003445 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003446 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003447 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003448 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3449 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3450 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3451
3452 B.setDependsOn(A, 1)
3453
Paul Belanger174a8272017-03-14 13:20:10 -04003454 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003455
James E. Blair8b5408c2016-08-08 15:37:46 -07003456 A.addApproval('code-review', 2)
3457 B.addApproval('code-review', 2)
3458 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003459
James E. Blair8b5408c2016-08-08 15:37:46 -07003460 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3461 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3462 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003463 self.waitUntilSettled()
3464
3465 # Only A and B will have their merge jobs queued because
3466 # window is 2.
3467 self.assertEqual(len(self.builds), 2)
3468 self.assertEqual(self.builds[0].name, 'project-merge')
3469 self.assertEqual(self.builds[1].name, 'project-merge')
3470
Paul Belanger174a8272017-03-14 13:20:10 -04003471 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003472 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003473 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003474 self.waitUntilSettled()
3475
3476 # Only A and B will have their test jobs queued because
3477 # window is 2.
3478 self.assertEqual(len(self.builds), 4)
3479 self.assertEqual(self.builds[0].name, 'project-test1')
3480 self.assertEqual(self.builds[1].name, 'project-test2')
3481 self.assertEqual(self.builds[2].name, 'project-test1')
3482 self.assertEqual(self.builds[3].name, 'project-test2')
3483
Paul Belanger174a8272017-03-14 13:20:10 -04003484 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003485 self.waitUntilSettled()
3486
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003487 tenant = self.sched.abide.tenants.get('openstack')
3488 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003489 # A failed so window is reduced by 1 to 1.
3490 self.assertEqual(queue.window, 1)
3491 self.assertEqual(queue.window_floor, 1)
3492 self.assertEqual(A.data['status'], 'NEW')
3493 self.assertEqual(B.data['status'], 'NEW')
3494
3495 # Gate is reset and only C's merge job is queued because
3496 # window shrunk to 1 and A and B were dequeued.
3497 self.assertEqual(len(self.builds), 1)
3498 self.assertEqual(self.builds[0].name, 'project-merge')
3499
Paul Belanger174a8272017-03-14 13:20:10 -04003500 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003501 self.waitUntilSettled()
3502
3503 # Only C's test jobs are queued because window is still 1.
3504 self.assertEqual(len(self.builds), 2)
3505 self.assertEqual(self.builds[0].name, 'project-test1')
3506 self.assertEqual(self.builds[1].name, 'project-test2')
3507
Paul Belanger174a8272017-03-14 13:20:10 -04003508 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003509 self.waitUntilSettled()
3510
3511 # C was successfully merged so window is increased to 2.
3512 self.assertEqual(queue.window, 2)
3513 self.assertEqual(queue.window_floor, 1)
3514 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003515
James E. Blairec056492016-07-22 09:45:56 -07003516 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003517 def test_worker_update_metadata(self):
3518 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003519 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003520
3521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003522 A.addApproval('code-review', 2)
3523 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003524 self.waitUntilSettled()
3525
Paul Belanger174a8272017-03-14 13:20:10 -04003526 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003527
3528 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003529 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003530
3531 start = time.time()
3532 while True:
3533 if time.time() - start > 10:
3534 raise Exception("Timeout waiting for gearman server to report "
3535 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003536 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003537 if build.worker.name == "My Worker":
3538 break
3539 else:
3540 time.sleep(0)
3541
3542 self.log.debug(build)
3543 self.assertEqual("My Worker", build.worker.name)
3544 self.assertEqual("localhost", build.worker.hostname)
3545 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3546 self.assertEqual("zuul.example.org", build.worker.fqdn)
3547 self.assertEqual("FakeBuilder", build.worker.program)
3548 self.assertEqual("v1.1", build.worker.version)
3549 self.assertEqual({'something': 'else'}, build.worker.extra)
3550
Paul Belanger174a8272017-03-14 13:20:10 -04003551 self.executor_server.hold_jobs_in_build = False
3552 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003553 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003554
3555 def test_footer_message(self):
3556 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003557 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003558 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003559
3560 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003561 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003562 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003563 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003564 self.waitUntilSettled()
3565
3566 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003567 B.addApproval('code-review', 2)
3568 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003569 self.waitUntilSettled()
3570
3571 self.assertEqual(2, len(self.smtp_messages))
3572
Adam Gandelman82258fd2017-02-07 16:51:58 -08003573 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003574Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003575http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003576
Adam Gandelman82258fd2017-02-07 16:51:58 -08003577 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003578For CI problems and help debugging, contact ci@example.org"""
3579
Adam Gandelman82258fd2017-02-07 16:51:58 -08003580 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3581 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3582 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3583 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003584
James E. Blairec056492016-07-22 09:45:56 -07003585 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003586 def test_merge_failure_reporters(self):
3587 """Check that the config is set up correctly"""
3588
James E. Blairf84026c2015-12-08 16:11:46 -08003589 self.updateConfigLayout(
3590 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003591 self.sched.reconfigure(self.config)
3592 self.registerJobs()
3593
3594 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003595 "Merge Failed.\n\nThis change or one of its cross-repo "
3596 "dependencies was unable to be automatically merged with the "
3597 "current state of its repository. Please rebase the change and "
3598 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003599 self.sched.layout.pipelines['check'].merge_failure_message)
3600 self.assertEqual(
3601 "The merge failed! For more information...",
3602 self.sched.layout.pipelines['gate'].merge_failure_message)
3603
3604 self.assertEqual(
3605 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3606 self.assertEqual(
3607 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3608
3609 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003610 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3611 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003612
3613 self.assertTrue(
3614 (
3615 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003616 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003617 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003618 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003619 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003620 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003621 ) or (
3622 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003623 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003624 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003625 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003626 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003627 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003628 )
3629 )
3630
James E. Blairec056492016-07-22 09:45:56 -07003631 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003632 def test_merge_failure_reports(self):
3633 """Check that when a change fails to merge the correct message is sent
3634 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003635 self.updateConfigLayout(
3636 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003637 self.sched.reconfigure(self.config)
3638 self.registerJobs()
3639
3640 # Check a test failure isn't reported to SMTP
3641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003642 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003643 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003644 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003645 self.waitUntilSettled()
3646
3647 self.assertEqual(3, len(self.history)) # 3 jobs
3648 self.assertEqual(0, len(self.smtp_messages))
3649
3650 # Check a merge failure is reported to SMTP
3651 # B should be merged, but C will conflict with B
3652 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3653 B.addPatchset(['conflict'])
3654 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3655 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003656 B.addApproval('code-review', 2)
3657 C.addApproval('code-review', 2)
3658 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3659 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003660 self.waitUntilSettled()
3661
3662 self.assertEqual(6, len(self.history)) # A and B jobs
3663 self.assertEqual(1, len(self.smtp_messages))
3664 self.assertEqual('The merge failed! For more information...',
3665 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003666
James E. Blairec056492016-07-22 09:45:56 -07003667 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003668 def test_default_merge_failure_reports(self):
3669 """Check that the default merge failure reports are correct."""
3670
3671 # A should report success, B should report merge failure.
3672 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3673 A.addPatchset(['conflict'])
3674 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3675 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003676 A.addApproval('code-review', 2)
3677 B.addApproval('code-review', 2)
3678 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3679 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003680 self.waitUntilSettled()
3681
3682 self.assertEqual(3, len(self.history)) # A jobs
3683 self.assertEqual(A.reported, 2)
3684 self.assertEqual(B.reported, 2)
3685 self.assertEqual(A.data['status'], 'MERGED')
3686 self.assertEqual(B.data['status'], 'NEW')
3687 self.assertIn('Build succeeded', A.messages[1])
3688 self.assertIn('Merge Failed', B.messages[1])
3689 self.assertIn('automatically merged', B.messages[1])
3690 self.assertNotIn('logs.example.com', B.messages[1])
3691 self.assertNotIn('SKIPPED', B.messages[1])
3692
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003693 def test_client_get_running_jobs(self):
3694 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003695 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003696 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003697 A.addApproval('code-review', 2)
3698 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003699 self.waitUntilSettled()
3700
3701 client = zuul.rpcclient.RPCClient('127.0.0.1',
3702 self.gearman_server.port)
3703
3704 # Wait for gearman server to send the initial workData back to zuul
3705 start = time.time()
3706 while True:
3707 if time.time() - start > 10:
3708 raise Exception("Timeout waiting for gearman server to report "
3709 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003710 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003711 if build.worker.name == "My Worker":
3712 break
3713 else:
3714 time.sleep(0)
3715
3716 running_items = client.get_running_jobs()
3717
3718 self.assertEqual(1, len(running_items))
3719 running_item = running_items[0]
3720 self.assertEqual([], running_item['failing_reasons'])
3721 self.assertEqual([], running_item['items_behind'])
3722 self.assertEqual('https://hostname/1', running_item['url'])
3723 self.assertEqual(None, running_item['item_ahead'])
3724 self.assertEqual('org/project', running_item['project'])
3725 self.assertEqual(None, running_item['remaining_time'])
3726 self.assertEqual(True, running_item['active'])
3727 self.assertEqual('1,1', running_item['id'])
3728
3729 self.assertEqual(3, len(running_item['jobs']))
3730 for job in running_item['jobs']:
3731 if job['name'] == 'project-merge':
3732 self.assertEqual('project-merge', job['name'])
3733 self.assertEqual('gate', job['pipeline'])
3734 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003735 self.assertEqual('https://server/job/project-merge/0/',
3736 job['url'])
3737 self.assertEqual(7, len(job['worker']))
3738 self.assertEqual(False, job['canceled'])
3739 self.assertEqual(True, job['voting'])
3740 self.assertEqual(None, job['result'])
3741 self.assertEqual('gate', job['pipeline'])
3742 break
3743
Paul Belanger174a8272017-03-14 13:20:10 -04003744 self.executor_server.hold_jobs_in_build = False
3745 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003746 self.waitUntilSettled()
3747
3748 running_items = client.get_running_jobs()
3749 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003750
3751 def test_nonvoting_pipeline(self):
3752 "Test that a nonvoting pipeline (experimental) can still report"
3753
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003754 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3755 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003756 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3757 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003758
3759 self.assertEqual(self.getJobFromHistory('project-merge').result,
3760 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003761 self.assertEqual(
3762 self.getJobFromHistory('experimental-project-test').result,
3763 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003764 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003765
3766 def test_crd_gate(self):
3767 "Test cross-repo dependencies"
3768 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3769 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003770 A.addApproval('code-review', 2)
3771 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003772
3773 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3774 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3775 AM2.setMerged()
3776 AM1.setMerged()
3777
3778 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3779 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3780 BM2.setMerged()
3781 BM1.setMerged()
3782
3783 # A -> AM1 -> AM2
3784 # B -> BM1 -> BM2
3785 # A Depends-On: B
3786 # M2 is here to make sure it is never queried. If it is, it
3787 # means zuul is walking down the entire history of merged
3788 # changes.
3789
3790 B.setDependsOn(BM1, 1)
3791 BM1.setDependsOn(BM2, 1)
3792
3793 A.setDependsOn(AM1, 1)
3794 AM1.setDependsOn(AM2, 1)
3795
3796 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3797 A.subject, B.data['id'])
3798
James E. Blair8b5408c2016-08-08 15:37:46 -07003799 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003800 self.waitUntilSettled()
3801
3802 self.assertEqual(A.data['status'], 'NEW')
3803 self.assertEqual(B.data['status'], 'NEW')
3804
Paul Belanger5dccbe72016-11-14 11:17:37 -05003805 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003806 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003807
Paul Belanger174a8272017-03-14 13:20:10 -04003808 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003809 B.addApproval('approved', 1)
3810 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003811 self.waitUntilSettled()
3812
Paul Belanger174a8272017-03-14 13:20:10 -04003813 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003814 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003815 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003816 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003817 self.executor_server.hold_jobs_in_build = False
3818 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003819 self.waitUntilSettled()
3820
3821 self.assertEqual(AM2.queried, 0)
3822 self.assertEqual(BM2.queried, 0)
3823 self.assertEqual(A.data['status'], 'MERGED')
3824 self.assertEqual(B.data['status'], 'MERGED')
3825 self.assertEqual(A.reported, 2)
3826 self.assertEqual(B.reported, 2)
3827
Paul Belanger5dccbe72016-11-14 11:17:37 -05003828 changes = self.getJobFromHistory(
3829 'project-merge', 'org/project1').changes
3830 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003831
3832 def test_crd_branch(self):
3833 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003834
3835 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003836 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3837 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3838 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3839 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003840 A.addApproval('code-review', 2)
3841 B.addApproval('code-review', 2)
3842 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003843
3844 # A Depends-On: B+C
3845 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3846 A.subject, B.data['id'])
3847
Paul Belanger174a8272017-03-14 13:20:10 -04003848 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003849 B.addApproval('approved', 1)
3850 C.addApproval('approved', 1)
3851 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003852 self.waitUntilSettled()
3853
Paul Belanger174a8272017-03-14 13:20:10 -04003854 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003855 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003856 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003857 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003858 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003859 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003860 self.executor_server.hold_jobs_in_build = False
3861 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003862 self.waitUntilSettled()
3863
3864 self.assertEqual(A.data['status'], 'MERGED')
3865 self.assertEqual(B.data['status'], 'MERGED')
3866 self.assertEqual(C.data['status'], 'MERGED')
3867 self.assertEqual(A.reported, 2)
3868 self.assertEqual(B.reported, 2)
3869 self.assertEqual(C.reported, 2)
3870
Paul Belanger6379db12016-11-14 13:57:54 -05003871 changes = self.getJobFromHistory(
3872 'project-merge', 'org/project1').changes
3873 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003874
3875 def test_crd_multiline(self):
3876 "Test multiple depends-on lines in commit"
3877 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3878 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3879 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003880 A.addApproval('code-review', 2)
3881 B.addApproval('code-review', 2)
3882 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003883
3884 # A Depends-On: B+C
3885 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3886 A.subject, B.data['id'], C.data['id'])
3887
Paul Belanger174a8272017-03-14 13:20:10 -04003888 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003889 B.addApproval('approved', 1)
3890 C.addApproval('approved', 1)
3891 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003892 self.waitUntilSettled()
3893
Paul Belanger174a8272017-03-14 13:20:10 -04003894 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003895 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003896 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003897 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003898 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003899 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003900 self.executor_server.hold_jobs_in_build = False
3901 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003902 self.waitUntilSettled()
3903
3904 self.assertEqual(A.data['status'], 'MERGED')
3905 self.assertEqual(B.data['status'], 'MERGED')
3906 self.assertEqual(C.data['status'], 'MERGED')
3907 self.assertEqual(A.reported, 2)
3908 self.assertEqual(B.reported, 2)
3909 self.assertEqual(C.reported, 2)
3910
Paul Belanger5dccbe72016-11-14 11:17:37 -05003911 changes = self.getJobFromHistory(
3912 'project-merge', 'org/project1').changes
3913 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003914
3915 def test_crd_unshared_gate(self):
3916 "Test cross-repo dependencies in unshared gate queues"
3917 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3918 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003919 A.addApproval('code-review', 2)
3920 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003921
3922 # A Depends-On: B
3923 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3924 A.subject, B.data['id'])
3925
3926 # A and B do not share a queue, make sure that A is unable to
3927 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003928 B.addApproval('approved', 1)
3929 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003930 self.waitUntilSettled()
3931
3932 self.assertEqual(A.data['status'], 'NEW')
3933 self.assertEqual(B.data['status'], 'NEW')
3934 self.assertEqual(A.reported, 0)
3935 self.assertEqual(B.reported, 0)
3936 self.assertEqual(len(self.history), 0)
3937
3938 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003939 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003940 self.waitUntilSettled()
3941
3942 self.assertEqual(B.data['status'], 'MERGED')
3943 self.assertEqual(B.reported, 2)
3944
3945 # Now that B is merged, A should be able to be enqueued and
3946 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003947 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003948 self.waitUntilSettled()
3949
3950 self.assertEqual(A.data['status'], 'MERGED')
3951 self.assertEqual(A.reported, 2)
3952
James E. Blair96698e22015-04-02 07:48:21 -07003953 def test_crd_gate_reverse(self):
3954 "Test reverse cross-repo dependencies"
3955 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3956 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003957 A.addApproval('code-review', 2)
3958 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003959
3960 # A Depends-On: B
3961
3962 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3963 A.subject, B.data['id'])
3964
James E. Blair8b5408c2016-08-08 15:37:46 -07003965 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003966 self.waitUntilSettled()
3967
3968 self.assertEqual(A.data['status'], 'NEW')
3969 self.assertEqual(B.data['status'], 'NEW')
3970
Paul Belanger174a8272017-03-14 13:20:10 -04003971 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003972 A.addApproval('approved', 1)
3973 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003974 self.waitUntilSettled()
3975
Paul Belanger174a8272017-03-14 13:20:10 -04003976 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003977 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003978 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003979 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003980 self.executor_server.hold_jobs_in_build = False
3981 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003982 self.waitUntilSettled()
3983
3984 self.assertEqual(A.data['status'], 'MERGED')
3985 self.assertEqual(B.data['status'], 'MERGED')
3986 self.assertEqual(A.reported, 2)
3987 self.assertEqual(B.reported, 2)
3988
Paul Belanger5dccbe72016-11-14 11:17:37 -05003989 changes = self.getJobFromHistory(
3990 'project-merge', 'org/project1').changes
3991 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003992
James E. Blair5ee24252014-12-30 10:12:29 -08003993 def test_crd_cycle(self):
3994 "Test cross-repo dependency cycles"
3995 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3996 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003997 A.addApproval('code-review', 2)
3998 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003999
4000 # A -> B -> A (via commit-depends)
4001
4002 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4003 A.subject, B.data['id'])
4004 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4005 B.subject, A.data['id'])
4006
James E. Blair8b5408c2016-08-08 15:37:46 -07004007 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004008 self.waitUntilSettled()
4009
4010 self.assertEqual(A.reported, 0)
4011 self.assertEqual(B.reported, 0)
4012 self.assertEqual(A.data['status'], 'NEW')
4013 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004014
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004015 def test_crd_gate_unknown(self):
4016 "Test unknown projects in dependent pipeline"
4017 self.init_repo("org/unknown")
4018 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4019 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004020 A.addApproval('code-review', 2)
4021 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004022
4023 # A Depends-On: B
4024 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4025 A.subject, B.data['id'])
4026
James E. Blair8b5408c2016-08-08 15:37:46 -07004027 B.addApproval('approved', 1)
4028 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004029 self.waitUntilSettled()
4030
4031 # Unknown projects cannot share a queue with any other
4032 # since they don't have common jobs with any other (they have no jobs).
4033 # Changes which depend on unknown project changes
4034 # should not be processed in dependent pipeline
4035 self.assertEqual(A.data['status'], 'NEW')
4036 self.assertEqual(B.data['status'], 'NEW')
4037 self.assertEqual(A.reported, 0)
4038 self.assertEqual(B.reported, 0)
4039 self.assertEqual(len(self.history), 0)
4040
4041 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004042 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004043 B.setMerged()
4044 self.waitUntilSettled()
4045 self.assertEqual(len(self.history), 0)
4046
4047 # Now that B is merged, A should be able to be enqueued and
4048 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004049 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004050 self.waitUntilSettled()
4051
4052 self.assertEqual(A.data['status'], 'MERGED')
4053 self.assertEqual(A.reported, 2)
4054 self.assertEqual(B.data['status'], 'MERGED')
4055 self.assertEqual(B.reported, 0)
4056
James E. Blairbfb8e042014-12-30 17:01:44 -08004057 def test_crd_check(self):
4058 "Test cross-repo dependencies in independent pipelines"
4059
Paul Belanger174a8272017-03-14 13:20:10 -04004060 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004061 self.gearman_server.hold_jobs_in_queue = True
4062 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4063 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4064
4065 # A Depends-On: B
4066 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4067 A.subject, B.data['id'])
4068
4069 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4070 self.waitUntilSettled()
4071
4072 queue = self.gearman_server.getQueue()
4073 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4074 self.gearman_server.hold_jobs_in_queue = False
4075 self.gearman_server.release()
4076 self.waitUntilSettled()
4077
Paul Belanger174a8272017-03-14 13:20:10 -04004078 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004079 self.waitUntilSettled()
4080
Monty Taylord642d852017-02-23 14:05:42 -05004081 path = os.path.join(self.builds[0].jobdir.src_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004082 repo = git.Repo(path)
4083 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4084 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004085 correct_messages = [
4086 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004087 self.assertEqual(repo_messages, correct_messages)
4088
Monty Taylord642d852017-02-23 14:05:42 -05004089 path = os.path.join(self.builds[0].jobdir.src_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004090 repo = git.Repo(path)
4091 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4092 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004093 correct_messages = [
4094 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004095 self.assertEqual(repo_messages, correct_messages)
4096
Paul Belanger174a8272017-03-14 13:20:10 -04004097 self.executor_server.hold_jobs_in_build = False
4098 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004099 self.waitUntilSettled()
4100
James E. Blairbfb8e042014-12-30 17:01:44 -08004101 self.assertEqual(A.data['status'], 'NEW')
4102 self.assertEqual(B.data['status'], 'NEW')
4103 self.assertEqual(A.reported, 1)
4104 self.assertEqual(B.reported, 0)
4105
4106 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004107 tenant = self.sched.abide.tenants.get('tenant-one')
4108 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004109
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004110 def test_crd_check_git_depends(self):
4111 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004112 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004113 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4114 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4115
4116 # Add two git-dependent changes and make sure they both report
4117 # success.
4118 B.setDependsOn(A, 1)
4119 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4120 self.waitUntilSettled()
4121 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4122 self.waitUntilSettled()
4123
James E. Blairb8c16472015-05-05 14:55:26 -07004124 self.orderedRelease()
4125 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004126 self.waitUntilSettled()
4127
4128 self.assertEqual(A.data['status'], 'NEW')
4129 self.assertEqual(B.data['status'], 'NEW')
4130 self.assertEqual(A.reported, 1)
4131 self.assertEqual(B.reported, 1)
4132
4133 self.assertEqual(self.history[0].changes, '1,1')
4134 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004135 tenant = self.sched.abide.tenants.get('tenant-one')
4136 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004137
4138 self.assertIn('Build succeeded', A.messages[0])
4139 self.assertIn('Build succeeded', B.messages[0])
4140
4141 def test_crd_check_duplicate(self):
4142 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004143 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004144 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4145 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004146 tenant = self.sched.abide.tenants.get('tenant-one')
4147 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004148
4149 # Add two git-dependent changes...
4150 B.setDependsOn(A, 1)
4151 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4152 self.waitUntilSettled()
4153 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4154
4155 # ...make sure the live one is not duplicated...
4156 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4157 self.waitUntilSettled()
4158 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4159
4160 # ...but the non-live one is able to be.
4161 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4162 self.waitUntilSettled()
4163 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4164
Clark Boylandd849822015-03-02 12:38:14 -08004165 # Release jobs in order to avoid races with change A jobs
4166 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004167 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004168 self.executor_server.hold_jobs_in_build = False
4169 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004170 self.waitUntilSettled()
4171
4172 self.assertEqual(A.data['status'], 'NEW')
4173 self.assertEqual(B.data['status'], 'NEW')
4174 self.assertEqual(A.reported, 1)
4175 self.assertEqual(B.reported, 1)
4176
4177 self.assertEqual(self.history[0].changes, '1,1 2,1')
4178 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004179 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004180
4181 self.assertIn('Build succeeded', A.messages[0])
4182 self.assertIn('Build succeeded', B.messages[0])
4183
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004184 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004185 "Test cross-repo dependencies re-enqueued in independent pipelines"
4186
4187 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004188 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4189 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004190
4191 # A Depends-On: B
4192 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4193 A.subject, B.data['id'])
4194
4195 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4196 self.waitUntilSettled()
4197
4198 self.sched.reconfigure(self.config)
4199
4200 # Make sure the items still share a change queue, and the
4201 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004202 tenant = self.sched.abide.tenants.get('tenant-one')
4203 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4204 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004205 first_item = queue.queue[0]
4206 for item in queue.queue:
4207 self.assertEqual(item.queue, first_item.queue)
4208 self.assertFalse(first_item.live)
4209 self.assertTrue(queue.queue[1].live)
4210
4211 self.gearman_server.hold_jobs_in_queue = False
4212 self.gearman_server.release()
4213 self.waitUntilSettled()
4214
4215 self.assertEqual(A.data['status'], 'NEW')
4216 self.assertEqual(B.data['status'], 'NEW')
4217 self.assertEqual(A.reported, 1)
4218 self.assertEqual(B.reported, 0)
4219
4220 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004221 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004222
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004223 def test_crd_check_reconfiguration(self):
4224 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4225
4226 def test_crd_undefined_project(self):
4227 """Test that undefined projects in dependencies are handled for
4228 independent pipelines"""
4229 # It's a hack for fake gerrit,
4230 # as it implies repo creation upon the creation of any change
4231 self.init_repo("org/unknown")
4232 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4233
James E. Blair17dd6772015-02-09 14:45:18 -08004234 def test_crd_check_ignore_dependencies(self):
4235 "Test cross-repo dependencies can be ignored"
Clint Byrum12282ca2017-04-03 11:04:02 -07004236 self.updateConfigLayout('layout-ignore-dependencies')
James E. Blair17dd6772015-02-09 14:45:18 -08004237 self.sched.reconfigure(self.config)
James E. Blair17dd6772015-02-09 14:45:18 -08004238
4239 self.gearman_server.hold_jobs_in_queue = True
4240 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4241 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4242 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4243
4244 # A Depends-On: B
4245 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4246 A.subject, B.data['id'])
4247 # C git-depends on B
4248 C.setDependsOn(B, 1)
4249 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4250 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4251 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4252 self.waitUntilSettled()
4253
4254 # Make sure none of the items share a change queue, and all
4255 # are live.
Clint Byrum12282ca2017-04-03 11:04:02 -07004256 tenant = self.sched.abide.tenants.get('openstack')
4257 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004258 self.assertEqual(len(check_pipeline.queues), 3)
4259 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4260 for item in check_pipeline.getAllItems():
4261 self.assertTrue(item.live)
4262
4263 self.gearman_server.hold_jobs_in_queue = False
4264 self.gearman_server.release()
4265 self.waitUntilSettled()
4266
4267 self.assertEqual(A.data['status'], 'NEW')
4268 self.assertEqual(B.data['status'], 'NEW')
4269 self.assertEqual(C.data['status'], 'NEW')
4270 self.assertEqual(A.reported, 1)
4271 self.assertEqual(B.reported, 1)
4272 self.assertEqual(C.reported, 1)
4273
4274 # Each job should have tested exactly one change
4275 for job in self.history:
4276 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004277
4278 def test_crd_check_transitive(self):
4279 "Test transitive cross-repo dependencies"
4280 # Specifically, if A -> B -> C, and C gets a new patchset and
4281 # A gets a new patchset, ensure the test of A,2 includes B,1
4282 # and C,2 (not C,1 which would indicate stale data in the
4283 # cache for B).
4284 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4285 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4286 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4287
4288 # A Depends-On: B
4289 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4290 A.subject, B.data['id'])
4291
4292 # B Depends-On: C
4293 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4294 B.subject, C.data['id'])
4295
4296 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4297 self.waitUntilSettled()
4298 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4299
4300 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4301 self.waitUntilSettled()
4302 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4303
4304 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4305 self.waitUntilSettled()
4306 self.assertEqual(self.history[-1].changes, '3,1')
4307
4308 C.addPatchset()
4309 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4310 self.waitUntilSettled()
4311 self.assertEqual(self.history[-1].changes, '3,2')
4312
4313 A.addPatchset()
4314 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4315 self.waitUntilSettled()
4316 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004317
James E. Blaira8b90b32016-08-24 15:18:50 -07004318 def test_crd_check_unknown(self):
4319 "Test unknown projects in independent pipeline"
4320 self.init_repo("org/unknown")
4321 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4322 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4323 # A Depends-On: B
4324 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4325 A.subject, B.data['id'])
4326
4327 # Make sure zuul has seen an event on B.
4328 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4329 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4330 self.waitUntilSettled()
4331
4332 self.assertEqual(A.data['status'], 'NEW')
4333 self.assertEqual(A.reported, 1)
4334 self.assertEqual(B.data['status'], 'NEW')
4335 self.assertEqual(B.reported, 0)
4336
James E. Blair92464a22016-04-05 10:21:26 -07004337 def test_crd_cycle_join(self):
4338 "Test an updated change creates a cycle"
4339 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4340
4341 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4342 self.waitUntilSettled()
4343
4344 # Create B->A
4345 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4346 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4347 B.subject, A.data['id'])
4348 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4349 self.waitUntilSettled()
4350
4351 # Update A to add A->B (a cycle).
4352 A.addPatchset()
4353 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4354 A.subject, B.data['id'])
4355 # Normally we would submit the patchset-created event for
4356 # processing here, however, we have no way of noting whether
4357 # the dependency cycle detection correctly raised an
4358 # exception, so instead, we reach into the source driver and
4359 # call the method that would ultimately be called by the event
4360 # processing.
4361
Paul Belanger0e155e22016-11-14 14:12:23 -05004362 tenant = self.sched.abide.tenants.get('tenant-one')
4363 source = tenant.layout.pipelines['gate'].source
4364
4365 # TODO(pabelanger): As we add more source / trigger APIs we should make
4366 # it easier for users to create events for testing.
4367 event = zuul.model.TriggerEvent()
4368 event.trigger_name = 'gerrit'
4369 event.change_number = '1'
4370 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004371 with testtools.ExpectedException(
4372 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004373 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004374 self.log.debug("Got expected dependency cycle exception")
4375
4376 # Now if we update B to remove the depends-on, everything
4377 # should be okay. B; A->B
4378
4379 B.addPatchset()
4380 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004381
4382 source.getChange(event, True)
4383 event.change_number = '2'
4384 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004385
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004386 def test_disable_at(self):
4387 "Test a pipeline will only report to the disabled trigger when failing"
4388
Paul Belanger7dc76e82016-11-11 16:51:08 -05004389 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004390 self.sched.reconfigure(self.config)
4391
Paul Belanger7dc76e82016-11-11 16:51:08 -05004392 tenant = self.sched.abide.tenants.get('openstack')
4393 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004394 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004395 0, tenant.layout.pipelines['check']._consecutive_failures)
4396 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004397
4398 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4399 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4400 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4401 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4402 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4403 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4404 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4405 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4406 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4407 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4408 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4409
Paul Belanger174a8272017-03-14 13:20:10 -04004410 self.executor_server.failJob('project-test1', A)
4411 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004412 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004413 self.executor_server.failJob('project-test1', D)
4414 self.executor_server.failJob('project-test1', E)
4415 self.executor_server.failJob('project-test1', F)
4416 self.executor_server.failJob('project-test1', G)
4417 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004418 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004419 self.executor_server.failJob('project-test1', J)
4420 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004421
4422 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4423 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4424 self.waitUntilSettled()
4425
4426 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004427 2, tenant.layout.pipelines['check']._consecutive_failures)
4428 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004429
4430 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4431 self.waitUntilSettled()
4432
4433 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004434 0, tenant.layout.pipelines['check']._consecutive_failures)
4435 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004436
4437 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4438 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4439 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4440 self.waitUntilSettled()
4441
4442 # We should be disabled now
4443 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004444 3, tenant.layout.pipelines['check']._consecutive_failures)
4445 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004446
4447 # We need to wait between each of these patches to make sure the
4448 # smtp messages come back in an expected order
4449 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4450 self.waitUntilSettled()
4451 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4452 self.waitUntilSettled()
4453 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4454 self.waitUntilSettled()
4455
4456 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4457 # leaving a message on each change
4458 self.assertEqual(1, len(A.messages))
4459 self.assertIn('Build failed.', A.messages[0])
4460 self.assertEqual(1, len(B.messages))
4461 self.assertIn('Build failed.', B.messages[0])
4462 self.assertEqual(1, len(C.messages))
4463 self.assertIn('Build succeeded.', C.messages[0])
4464 self.assertEqual(1, len(D.messages))
4465 self.assertIn('Build failed.', D.messages[0])
4466 self.assertEqual(1, len(E.messages))
4467 self.assertIn('Build failed.', E.messages[0])
4468 self.assertEqual(1, len(F.messages))
4469 self.assertIn('Build failed.', F.messages[0])
4470
4471 # The last 3 (GHI) would have only reported via smtp.
4472 self.assertEqual(3, len(self.smtp_messages))
4473 self.assertEqual(0, len(G.messages))
4474 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004475 self.assertIn(
4476 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004477 self.assertEqual(0, len(H.messages))
4478 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004479 self.assertIn(
4480 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004481 self.assertEqual(0, len(I.messages))
4482 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004483 self.assertIn(
4484 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004485
4486 # Now reload the configuration (simulate a HUP) to check the pipeline
4487 # comes out of disabled
4488 self.sched.reconfigure(self.config)
4489
Paul Belanger7dc76e82016-11-11 16:51:08 -05004490 tenant = self.sched.abide.tenants.get('openstack')
4491
4492 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004493 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004494 0, tenant.layout.pipelines['check']._consecutive_failures)
4495 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004496
4497 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4498 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4499 self.waitUntilSettled()
4500
4501 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004502 2, tenant.layout.pipelines['check']._consecutive_failures)
4503 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004504
4505 # J and K went back to gerrit
4506 self.assertEqual(1, len(J.messages))
4507 self.assertIn('Build failed.', J.messages[0])
4508 self.assertEqual(1, len(K.messages))
4509 self.assertIn('Build failed.', K.messages[0])
4510 # No more messages reported via smtp
4511 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004512
Paul Belanger71d98172016-11-08 10:56:31 -05004513 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004514 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004515
Paul Belanger174a8272017-03-14 13:20:10 -04004516 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004517 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4518 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4519 self.waitUntilSettled()
4520
Paul Belanger174a8272017-03-14 13:20:10 -04004521 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004522 self.waitUntilSettled()
4523
4524 self.assertEqual(len(self.builds), 2)
4525 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004526 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004527 self.waitUntilSettled()
4528
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004529 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004530 self.assertEqual(len(self.builds), 1,
4531 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004532 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004533 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004534 self.waitUntilSettled()
4535
Paul Belanger174a8272017-03-14 13:20:10 -04004536 self.executor_server.hold_jobs_in_build = False
4537 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004538 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004539 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004540 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4541 self.assertEqual(A.reported, 1)
4542 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004543
James E. Blair15be0e12017-01-03 13:45:20 -08004544 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004545 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004546
4547 self.fake_nodepool.paused = True
4548 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4549 A.addApproval('code-review', 2)
4550 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4551 self.waitUntilSettled()
4552
4553 self.zk.client.stop()
4554 self.zk.client.start()
4555 self.fake_nodepool.paused = False
4556 self.waitUntilSettled()
4557
4558 self.assertEqual(A.data['status'], 'MERGED')
4559 self.assertEqual(A.reported, 2)
4560
James E. Blair6ab79e02017-01-06 10:10:17 -08004561 def test_nodepool_failure(self):
4562 "Test that jobs are reported after a nodepool failure"
4563
4564 self.fake_nodepool.paused = True
4565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4566 A.addApproval('code-review', 2)
4567 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4568 self.waitUntilSettled()
4569
4570 req = self.fake_nodepool.getNodeRequests()[0]
4571 self.fake_nodepool.addFailRequest(req)
4572
4573 self.fake_nodepool.paused = False
4574 self.waitUntilSettled()
4575
4576 self.assertEqual(A.data['status'], 'NEW')
4577 self.assertEqual(A.reported, 2)
4578 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4579 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4580 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4581
Arieb6f068c2016-10-09 13:11:06 +03004582
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004583class TestDependencyGraph(ZuulTestCase):
4584 tenant_config_file = 'config/dependency-graph/main.yaml'
4585
4586 def test_dependeny_graph_dispatch_jobs_once(self):
4587 "Test a job in a dependency graph is queued only once"
4588 # Job dependencies, starting with A
4589 # A
4590 # / \
4591 # B C
4592 # / \ / \
4593 # D F E
4594 # |
4595 # G
4596
4597 self.executor_server.hold_jobs_in_build = True
4598 change = self.fake_gerrit.addFakeChange(
4599 'org/project', 'master', 'change')
4600 change.addApproval('code-review', 2)
4601 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4602
4603 self.waitUntilSettled()
4604 self.assertEqual([b.name for b in self.builds], ['A'])
4605
4606 self.executor_server.release('A')
4607 self.waitUntilSettled()
4608 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4609
4610 self.executor_server.release('B')
4611 self.waitUntilSettled()
4612 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4613
4614 self.executor_server.release('D')
4615 self.waitUntilSettled()
4616 self.assertEqual([b.name for b in self.builds], ['C'])
4617
4618 self.executor_server.release('C')
4619 self.waitUntilSettled()
4620 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4621
4622 self.executor_server.release('F')
4623 self.waitUntilSettled()
4624 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4625
4626 self.executor_server.release('G')
4627 self.waitUntilSettled()
4628 self.assertEqual([b.name for b in self.builds], ['E'])
4629
4630 self.executor_server.release('E')
4631 self.waitUntilSettled()
4632 self.assertEqual(len(self.builds), 0)
4633
4634 self.executor_server.hold_jobs_in_build = False
4635 self.executor_server.release()
4636 self.waitUntilSettled()
4637
4638 self.assertEqual(len(self.builds), 0)
4639 self.assertEqual(len(self.history), 7)
4640
4641 self.assertEqual(change.data['status'], 'MERGED')
4642 self.assertEqual(change.reported, 2)
4643
4644 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4645 "Test that a job waits till all dependencies are successful"
4646 # Job dependencies, starting with A
4647 # A
4648 # / \
4649 # B C*
4650 # / \ / \
4651 # D F E
4652 # |
4653 # G
4654
4655 self.executor_server.hold_jobs_in_build = True
4656 change = self.fake_gerrit.addFakeChange(
4657 'org/project', 'master', 'change')
4658 change.addApproval('code-review', 2)
4659
4660 self.executor_server.failJob('C', change)
4661
4662 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4663
4664 self.waitUntilSettled()
4665 self.assertEqual([b.name for b in self.builds], ['A'])
4666
4667 self.executor_server.release('A')
4668 self.waitUntilSettled()
4669 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4670
4671 self.executor_server.release('B')
4672 self.waitUntilSettled()
4673 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4674
4675 self.executor_server.release('D')
4676 self.waitUntilSettled()
4677 self.assertEqual([b.name for b in self.builds], ['C'])
4678
4679 self.executor_server.release('C')
4680 self.waitUntilSettled()
4681 self.assertEqual(len(self.builds), 0)
4682
4683 self.executor_server.hold_jobs_in_build = False
4684 self.executor_server.release()
4685 self.waitUntilSettled()
4686
4687 self.assertEqual(len(self.builds), 0)
4688 self.assertEqual(len(self.history), 4)
4689
4690 self.assertEqual(change.data['status'], 'NEW')
4691 self.assertEqual(change.reported, 2)
4692
4693
Arieb6f068c2016-10-09 13:11:06 +03004694class TestDuplicatePipeline(ZuulTestCase):
4695 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4696
4697 def test_duplicate_pipelines(self):
4698 "Test that a change matching multiple pipelines works"
4699
4700 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4701 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4702 self.waitUntilSettled()
4703
4704 self.assertHistory([
4705 dict(name='project-test1', result='SUCCESS', changes='1,1',
4706 pipeline='dup1'),
4707 dict(name='project-test1', result='SUCCESS', changes='1,1',
4708 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004709 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004710
4711 self.assertEqual(len(A.messages), 2)
4712
Paul Belangera46a3742016-11-09 14:23:03 -05004713 if 'dup1' in A.messages[0]:
4714 self.assertIn('dup1', A.messages[0])
4715 self.assertNotIn('dup2', A.messages[0])
4716 self.assertIn('project-test1', A.messages[0])
4717 self.assertIn('dup2', A.messages[1])
4718 self.assertNotIn('dup1', A.messages[1])
4719 self.assertIn('project-test1', A.messages[1])
4720 else:
4721 self.assertIn('dup1', A.messages[1])
4722 self.assertNotIn('dup2', A.messages[1])
4723 self.assertIn('project-test1', A.messages[1])
4724 self.assertIn('dup2', A.messages[0])
4725 self.assertNotIn('dup1', A.messages[0])
4726 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004727
4728
4729class TestSchedulerOneJobProject(ZuulTestCase):
4730 tenant_config_file = 'config/one-job-project/main.yaml'
4731
4732 def test_one_job_project(self):
4733 "Test that queueing works with one job"
4734 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4735 'master', 'A')
4736 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4737 'master', 'B')
4738 A.addApproval('code-review', 2)
4739 B.addApproval('code-review', 2)
4740 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4741 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4742 self.waitUntilSettled()
4743
4744 self.assertEqual(A.data['status'], 'MERGED')
4745 self.assertEqual(A.reported, 2)
4746 self.assertEqual(B.data['status'], 'MERGED')
4747 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004748
4749
4750class TestSchedulerTemplatedProject(ZuulTestCase):
4751 tenant_config_file = 'config/templated-project/main.yaml'
4752
Paul Belanger174a8272017-03-14 13:20:10 -04004753 def test_job_from_templates_executed(self):
4754 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004755
4756 A = self.fake_gerrit.addFakeChange(
4757 'org/templated-project', 'master', 'A')
4758 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4759 self.waitUntilSettled()
4760
4761 self.assertEqual(self.getJobFromHistory('project-test1').result,
4762 'SUCCESS')
4763 self.assertEqual(self.getJobFromHistory('project-test2').result,
4764 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004765
4766 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004767 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004768
4769 A = self.fake_gerrit.addFakeChange(
4770 'org/layered-project', 'master', 'A')
4771 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4772 self.waitUntilSettled()
4773
4774 self.assertEqual(self.getJobFromHistory('project-test1').result,
4775 'SUCCESS')
4776 self.assertEqual(self.getJobFromHistory('project-test2').result,
4777 'SUCCESS')
4778 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4779 ).result, 'SUCCESS')
4780 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4781 ).result, 'SUCCESS')
4782 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4783 ).result, 'SUCCESS')
4784 self.assertEqual(self.getJobFromHistory('project-test6').result,
4785 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004786
4787
4788class TestSchedulerSuccessURL(ZuulTestCase):
4789 tenant_config_file = 'config/success-url/main.yaml'
4790
4791 def test_success_url(self):
4792 "Ensure bad build params are ignored"
4793 self.sched.reconfigure(self.config)
4794 self.init_repo('org/docs')
4795
4796 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4797 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4798 self.waitUntilSettled()
4799
4800 # Both builds ran: docs-draft-test + docs-draft-test2
4801 self.assertEqual(len(self.history), 2)
4802
4803 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004804 for build in self.history:
4805 if build.name == 'docs-draft-test':
4806 uuid = build.uuid[:7]
4807 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004808
4809 # Two msgs: 'Starting...' + results
4810 self.assertEqual(len(self.smtp_messages), 2)
4811 body = self.smtp_messages[1]['body'].splitlines()
4812 self.assertEqual('Build succeeded.', body[0])
4813
4814 self.assertIn(
4815 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4816 'docs-draft-test/%s/publish-docs/' % uuid,
4817 body[2])
4818
Paul Belanger174a8272017-03-14 13:20:10 -04004819 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004820 self.assertIn(
4821 '- docs-draft-test2 https://server/job',
4822 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004823
4824
Adam Gandelman4da00f62016-12-09 15:47:33 -08004825class TestSchedulerMerges(ZuulTestCase):
4826 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004827
4828 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004829 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004830 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004831 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004832 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4833 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4834 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4835 A.addApproval('code-review', 2)
4836 B.addApproval('code-review', 2)
4837 C.addApproval('code-review', 2)
4838 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4839 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4840 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4841 self.waitUntilSettled()
4842
4843 build = self.builds[-1]
4844 ref = self.getParameter(build, 'ZUUL_REF')
4845
Monty Taylord642d852017-02-23 14:05:42 -05004846 path = os.path.join(build.jobdir.src_root, project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004847 repo = git.Repo(path)
4848 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4849 repo_messages.reverse()
4850
Paul Belanger174a8272017-03-14 13:20:10 -04004851 self.executor_server.hold_jobs_in_build = False
4852 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004853 self.waitUntilSettled()
4854
4855 return repo_messages
4856
4857 def _test_merge(self, mode):
4858 us_path = os.path.join(
4859 self.upstream_root, 'org/project-%s' % mode)
4860 expected_messages = [
4861 'initial commit',
4862 'add content from fixture',
4863 # the intermediate commits order is nondeterministic
4864 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4865 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4866 ]
4867 result = self._test_project_merge_mode(mode)
4868 self.assertEqual(result[:2], expected_messages[:2])
4869 self.assertEqual(result[-2:], expected_messages[-2:])
4870
4871 def test_project_merge_mode_merge(self):
4872 self._test_merge('merge')
4873
4874 def test_project_merge_mode_merge_resolve(self):
4875 self._test_merge('merge-resolve')
4876
4877 def test_project_merge_mode_cherrypick(self):
4878 expected_messages = [
4879 'initial commit',
4880 'add content from fixture',
4881 'A-1',
4882 'B-1',
4883 'C-1']
4884 result = self._test_project_merge_mode('cherry-pick')
4885 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004886
4887 def test_merge_branch(self):
4888 "Test that the right commits are on alternate branches"
4889 self.create_branch('org/project-merge-branches', 'mp')
4890
Paul Belanger174a8272017-03-14 13:20:10 -04004891 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004892 A = self.fake_gerrit.addFakeChange(
4893 'org/project-merge-branches', 'mp', 'A')
4894 B = self.fake_gerrit.addFakeChange(
4895 'org/project-merge-branches', 'mp', 'B')
4896 C = self.fake_gerrit.addFakeChange(
4897 'org/project-merge-branches', 'mp', 'C')
4898 A.addApproval('code-review', 2)
4899 B.addApproval('code-review', 2)
4900 C.addApproval('code-review', 2)
4901 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4902 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4903 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4904 self.waitUntilSettled()
4905
Paul Belanger174a8272017-03-14 13:20:10 -04004906 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004907 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004908 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004909 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004910 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004911 self.waitUntilSettled()
4912
4913 build = self.builds[-1]
4914 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4915 ref = self.getParameter(build, 'ZUUL_REF')
4916 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004917 build.jobdir.src_root, 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004918 repo = git.Repo(path)
4919
4920 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4921 repo_messages.reverse()
4922 correct_messages = [
4923 'initial commit',
4924 'add content from fixture',
4925 'mp commit',
4926 'A-1', 'B-1', 'C-1']
4927 self.assertEqual(repo_messages, correct_messages)
4928
Paul Belanger174a8272017-03-14 13:20:10 -04004929 self.executor_server.hold_jobs_in_build = False
4930 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004931 self.waitUntilSettled()
4932
4933 def test_merge_multi_branch(self):
4934 "Test that dependent changes on multiple branches are merged"
4935 self.create_branch('org/project-merge-branches', 'mp')
4936
Paul Belanger174a8272017-03-14 13:20:10 -04004937 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004938 A = self.fake_gerrit.addFakeChange(
4939 'org/project-merge-branches', 'master', 'A')
4940 B = self.fake_gerrit.addFakeChange(
4941 'org/project-merge-branches', 'mp', 'B')
4942 C = self.fake_gerrit.addFakeChange(
4943 'org/project-merge-branches', 'master', 'C')
4944 A.addApproval('code-review', 2)
4945 B.addApproval('code-review', 2)
4946 C.addApproval('code-review', 2)
4947 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4948 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4949 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4950 self.waitUntilSettled()
4951
4952 job_A = None
4953 for job in self.builds:
4954 if 'project-merge' in job.name:
4955 job_A = job
4956 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4957 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4958 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4959 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4960
4961 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004962 job_A.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004963 repo = git.Repo(path)
4964 repo_messages = [c.message.strip()
4965 for c in repo.iter_commits(ref_A)]
4966 repo_messages.reverse()
4967 correct_messages = [
4968 'initial commit', 'add content from fixture', 'A-1']
4969 self.assertEqual(repo_messages, correct_messages)
4970
Paul Belanger174a8272017-03-14 13:20:10 -04004971 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004972 self.waitUntilSettled()
4973
4974 job_B = None
4975 for job in self.builds:
4976 if 'project-merge' in job.name:
4977 job_B = job
4978 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4979 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4980 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4981 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4982
4983 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004984 job_B.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004985 repo = git.Repo(path)
4986 repo_messages = [c.message.strip()
4987 for c in repo.iter_commits(ref_B)]
4988 repo_messages.reverse()
4989 correct_messages = [
4990 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4991 self.assertEqual(repo_messages, correct_messages)
4992
Paul Belanger174a8272017-03-14 13:20:10 -04004993 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004994 self.waitUntilSettled()
4995
4996 job_C = None
4997 for job in self.builds:
4998 if 'project-merge' in job.name:
4999 job_C = job
5000 ref_C = self.getParameter(job_C, 'ZUUL_REF')
5001 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
5002 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
5003 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
5004 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005005 job_C.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005006 repo = git.Repo(path)
5007 repo_messages = [c.message.strip()
5008 for c in repo.iter_commits(ref_C)]
5009
5010 repo_messages.reverse()
5011 correct_messages = [
5012 'initial commit', 'add content from fixture',
5013 'A-1', 'C-1']
5014 # Ensure the right commits are in the history for this ref
5015 self.assertEqual(repo_messages, correct_messages)
5016
Paul Belanger174a8272017-03-14 13:20:10 -04005017 self.executor_server.hold_jobs_in_build = False
5018 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005019 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005020
5021
5022class TestSemaphoreMultiTenant(ZuulTestCase):
5023 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5024
5025 def test_semaphore_tenant_isolation(self):
5026 "Test semaphores in multiple tenants"
5027
5028 self.waitUntilSettled()
5029 tenant_one = self.sched.abide.tenants.get('tenant-one')
5030 tenant_two = self.sched.abide.tenants.get('tenant-two')
5031
5032 self.executor_server.hold_jobs_in_build = True
5033 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5034 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5035 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5036 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5037 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5038 self.assertFalse('test-semaphore' in
5039 tenant_one.semaphore_handler.semaphores)
5040 self.assertFalse('test-semaphore' in
5041 tenant_two.semaphore_handler.semaphores)
5042
5043 # add patches to project1 of tenant-one
5044 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5045 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5046 self.waitUntilSettled()
5047
5048 # one build of project1-test1 must run
5049 # semaphore of tenant-one must be acquired once
5050 # semaphore of tenant-two must not be acquired
5051 self.assertEqual(len(self.builds), 1)
5052 self.assertEqual(self.builds[0].name, 'project1-test1')
5053 self.assertTrue('test-semaphore' in
5054 tenant_one.semaphore_handler.semaphores)
5055 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5056 'test-semaphore', [])), 1)
5057 self.assertFalse('test-semaphore' in
5058 tenant_two.semaphore_handler.semaphores)
5059
5060 # add patches to project2 of tenant-two
5061 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5062 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5063 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5064 self.waitUntilSettled()
5065
5066 # one build of project1-test1 must run
5067 # two builds of project2-test1 must run
5068 # semaphore of tenant-one must be acquired once
5069 # semaphore of tenant-two must be acquired twice
5070 self.assertEqual(len(self.builds), 3)
5071 self.assertEqual(self.builds[0].name, 'project1-test1')
5072 self.assertEqual(self.builds[1].name, 'project2-test1')
5073 self.assertEqual(self.builds[2].name, 'project2-test1')
5074 self.assertTrue('test-semaphore' in
5075 tenant_one.semaphore_handler.semaphores)
5076 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5077 'test-semaphore', [])), 1)
5078 self.assertTrue('test-semaphore' in
5079 tenant_two.semaphore_handler.semaphores)
5080 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5081 'test-semaphore', [])), 2)
5082
5083 self.executor_server.release('project1-test1')
5084 self.waitUntilSettled()
5085
5086 # one build of project1-test1 must run
5087 # two builds of project2-test1 must run
5088 # semaphore of tenant-one must be acquired once
5089 # semaphore of tenant-two must be acquired twice
5090 self.assertEqual(len(self.builds), 3)
5091 self.assertEqual(self.builds[0].name, 'project2-test1')
5092 self.assertEqual(self.builds[1].name, 'project2-test1')
5093 self.assertEqual(self.builds[2].name, 'project1-test1')
5094 self.assertTrue('test-semaphore' in
5095 tenant_one.semaphore_handler.semaphores)
5096 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5097 'test-semaphore', [])), 1)
5098 self.assertTrue('test-semaphore' in
5099 tenant_two.semaphore_handler.semaphores)
5100 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5101 'test-semaphore', [])), 2)
5102
5103 self.executor_server.release('project2-test1')
5104 self.waitUntilSettled()
5105
5106 # one build of project1-test1 must run
5107 # one build of project2-test1 must run
5108 # semaphore of tenant-one must be acquired once
5109 # semaphore of tenant-two must be acquired once
5110 self.assertEqual(len(self.builds), 2)
5111 self.assertTrue('test-semaphore' in
5112 tenant_one.semaphore_handler.semaphores)
5113 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5114 'test-semaphore', [])), 1)
5115 self.assertTrue('test-semaphore' in
5116 tenant_two.semaphore_handler.semaphores)
5117 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5118 'test-semaphore', [])), 1)
5119
5120 self.executor_server.hold_jobs_in_build = False
5121 self.executor_server.release()
5122
5123 self.waitUntilSettled()
5124
5125 # no build must run
5126 # semaphore of tenant-one must not be acquired
5127 # semaphore of tenant-two must not be acquired
5128 self.assertEqual(len(self.builds), 0)
5129 self.assertFalse('test-semaphore' in
5130 tenant_one.semaphore_handler.semaphores)
5131 self.assertFalse('test-semaphore' in
5132 tenant_two.semaphore_handler.semaphores)
5133
5134 self.assertEqual(A.reported, 1)
5135 self.assertEqual(B.reported, 1)
5136
5137
5138class TestSemaphoreInRepo(ZuulTestCase):
5139 tenant_config_file = 'config/in-repo/main.yaml'
5140
5141 def test_semaphore_in_repo(self):
5142 "Test semaphores in repo config"
5143
5144 # This tests dynamic semaphore handling in project repos. The semaphore
5145 # max value should not be evaluated dynamically but must be updated
5146 # after the change lands.
5147
5148 self.waitUntilSettled()
5149 tenant = self.sched.abide.tenants.get('tenant-one')
5150
5151 in_repo_conf = textwrap.dedent(
5152 """
5153 - job:
5154 name: project-test2
5155 semaphore: test-semaphore
5156
5157 - project:
5158 name: org/project
5159 tenant-one-gate:
5160 jobs:
5161 - project-test2
5162
5163 # the max value in dynamic layout must be ignored
5164 - semaphore:
5165 name: test-semaphore
5166 max: 2
5167 """)
5168
5169 in_repo_playbook = textwrap.dedent(
5170 """
5171 - hosts: all
5172 tasks: []
5173 """)
5174
5175 file_dict = {'.zuul.yaml': in_repo_conf,
5176 'playbooks/project-test2.yaml': in_repo_playbook}
5177 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5178 files=file_dict)
5179 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5180 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5181 B.setDependsOn(A, 1)
5182 C.setDependsOn(A, 1)
5183
5184 self.executor_server.hold_jobs_in_build = True
5185
5186 A.addApproval('code-review', 2)
5187 B.addApproval('code-review', 2)
5188 C.addApproval('code-review', 2)
5189 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5190 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5191 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5192 self.waitUntilSettled()
5193
5194 # check that the layout in a queue item still has max value of 1
5195 # for test-semaphore
5196 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5197 queue = None
5198 for queue_candidate in pipeline.queues:
5199 if queue_candidate.name == 'org/project':
5200 queue = queue_candidate
5201 break
5202 queue_item = queue.queue[0]
5203 item_dynamic_layout = queue_item.current_build_set.layout
5204 dynamic_test_semaphore = \
5205 item_dynamic_layout.semaphores.get('test-semaphore')
5206 self.assertEqual(dynamic_test_semaphore.max, 1)
5207
5208 # one build must be in queue, one semaphores acquired
5209 self.assertEqual(len(self.builds), 1)
5210 self.assertEqual(self.builds[0].name, 'project-test2')
5211 self.assertTrue('test-semaphore' in
5212 tenant.semaphore_handler.semaphores)
5213 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5214 'test-semaphore', [])), 1)
5215
5216 self.executor_server.release('project-test2')
5217 self.waitUntilSettled()
5218
5219 # change A must be merged
5220 self.assertEqual(A.data['status'], 'MERGED')
5221 self.assertEqual(A.reported, 2)
5222
5223 # send change-merged event as the gerrit mock doesn't send it
5224 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5225 self.waitUntilSettled()
5226
5227 # now that change A was merged, the new semaphore max must be effective
5228 tenant = self.sched.abide.tenants.get('tenant-one')
5229 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5230
5231 # two builds must be in queue, two semaphores acquired
5232 self.assertEqual(len(self.builds), 2)
5233 self.assertEqual(self.builds[0].name, 'project-test2')
5234 self.assertEqual(self.builds[1].name, 'project-test2')
5235 self.assertTrue('test-semaphore' in
5236 tenant.semaphore_handler.semaphores)
5237 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5238 'test-semaphore', [])), 2)
5239
5240 self.executor_server.release('project-test2')
5241 self.waitUntilSettled()
5242
5243 self.assertEqual(len(self.builds), 0)
5244 self.assertFalse('test-semaphore' in
5245 tenant.semaphore_handler.semaphores)
5246
5247 self.executor_server.hold_jobs_in_build = False
5248 self.executor_server.release()
5249
5250 self.waitUntilSettled()
5251 self.assertEqual(len(self.builds), 0)
5252
5253 self.assertEqual(A.reported, 2)
5254 self.assertEqual(B.reported, 2)
5255 self.assertEqual(C.reported, 2)