blob: 4b3fbf4b049d7d08c38d8e36d54963eabdde3a07 [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
James E. Blairec056492016-07-22 09:45:56 -07003352 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003353 def test_queue_rate_limiting(self):
3354 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003355 self.updateConfigLayout(
3356 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003357 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003358 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003359 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3360 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3361 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3362
3363 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003364 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003365
James E. Blair8b5408c2016-08-08 15:37:46 -07003366 A.addApproval('code-review', 2)
3367 B.addApproval('code-review', 2)
3368 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003369
James E. Blair8b5408c2016-08-08 15:37:46 -07003370 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3371 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3372 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003373 self.waitUntilSettled()
3374
3375 # Only A and B will have their merge jobs queued because
3376 # window is 2.
3377 self.assertEqual(len(self.builds), 2)
3378 self.assertEqual(self.builds[0].name, 'project-merge')
3379 self.assertEqual(self.builds[1].name, 'project-merge')
3380
Paul Belanger174a8272017-03-14 13:20:10 -04003381 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003382 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003383 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003384 self.waitUntilSettled()
3385
3386 # Only A and B will have their test jobs queued because
3387 # window is 2.
3388 self.assertEqual(len(self.builds), 4)
3389 self.assertEqual(self.builds[0].name, 'project-test1')
3390 self.assertEqual(self.builds[1].name, 'project-test2')
3391 self.assertEqual(self.builds[2].name, 'project-test1')
3392 self.assertEqual(self.builds[3].name, 'project-test2')
3393
Paul Belanger174a8272017-03-14 13:20:10 -04003394 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003395 self.waitUntilSettled()
3396
3397 queue = self.sched.layout.pipelines['gate'].queues[0]
3398 # A failed so window is reduced by 1 to 1.
3399 self.assertEqual(queue.window, 1)
3400 self.assertEqual(queue.window_floor, 1)
3401 self.assertEqual(A.data['status'], 'NEW')
3402
3403 # Gate is reset and only B's merge job is queued because
3404 # window shrunk to 1.
3405 self.assertEqual(len(self.builds), 1)
3406 self.assertEqual(self.builds[0].name, 'project-merge')
3407
Paul Belanger174a8272017-03-14 13:20:10 -04003408 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003409 self.waitUntilSettled()
3410
3411 # Only B's test jobs are queued because window is still 1.
3412 self.assertEqual(len(self.builds), 2)
3413 self.assertEqual(self.builds[0].name, 'project-test1')
3414 self.assertEqual(self.builds[1].name, 'project-test2')
3415
Paul Belanger174a8272017-03-14 13:20:10 -04003416 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003417 self.waitUntilSettled()
3418
3419 # B was successfully merged so window is increased to 2.
3420 self.assertEqual(queue.window, 2)
3421 self.assertEqual(queue.window_floor, 1)
3422 self.assertEqual(B.data['status'], 'MERGED')
3423
3424 # Only C is left and its merge job is queued.
3425 self.assertEqual(len(self.builds), 1)
3426 self.assertEqual(self.builds[0].name, 'project-merge')
3427
Paul Belanger174a8272017-03-14 13:20:10 -04003428 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003429 self.waitUntilSettled()
3430
3431 # After successful merge job the test jobs for C are queued.
3432 self.assertEqual(len(self.builds), 2)
3433 self.assertEqual(self.builds[0].name, 'project-test1')
3434 self.assertEqual(self.builds[1].name, 'project-test2')
3435
Paul Belanger174a8272017-03-14 13:20:10 -04003436 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003437 self.waitUntilSettled()
3438
3439 # C successfully merged so window is bumped to 3.
3440 self.assertEqual(queue.window, 3)
3441 self.assertEqual(queue.window_floor, 1)
3442 self.assertEqual(C.data['status'], 'MERGED')
3443
James E. Blairec056492016-07-22 09:45:56 -07003444 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003445 def test_queue_rate_limiting_dependent(self):
3446 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003447 self.updateConfigLayout(
3448 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003449 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003450 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3452 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3453 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3454
3455 B.setDependsOn(A, 1)
3456
Paul Belanger174a8272017-03-14 13:20:10 -04003457 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003458
James E. Blair8b5408c2016-08-08 15:37:46 -07003459 A.addApproval('code-review', 2)
3460 B.addApproval('code-review', 2)
3461 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003462
James E. Blair8b5408c2016-08-08 15:37:46 -07003463 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3464 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3465 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003466 self.waitUntilSettled()
3467
3468 # Only A and B will have their merge jobs queued because
3469 # window is 2.
3470 self.assertEqual(len(self.builds), 2)
3471 self.assertEqual(self.builds[0].name, 'project-merge')
3472 self.assertEqual(self.builds[1].name, 'project-merge')
3473
Paul Belanger174a8272017-03-14 13:20:10 -04003474 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003475 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003476 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003477 self.waitUntilSettled()
3478
3479 # Only A and B will have their test jobs queued because
3480 # window is 2.
3481 self.assertEqual(len(self.builds), 4)
3482 self.assertEqual(self.builds[0].name, 'project-test1')
3483 self.assertEqual(self.builds[1].name, 'project-test2')
3484 self.assertEqual(self.builds[2].name, 'project-test1')
3485 self.assertEqual(self.builds[3].name, 'project-test2')
3486
Paul Belanger174a8272017-03-14 13:20:10 -04003487 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003488 self.waitUntilSettled()
3489
3490 queue = self.sched.layout.pipelines['gate'].queues[0]
3491 # A failed so window is reduced by 1 to 1.
3492 self.assertEqual(queue.window, 1)
3493 self.assertEqual(queue.window_floor, 1)
3494 self.assertEqual(A.data['status'], 'NEW')
3495 self.assertEqual(B.data['status'], 'NEW')
3496
3497 # Gate is reset and only C's merge job is queued because
3498 # window shrunk to 1 and A and B were dequeued.
3499 self.assertEqual(len(self.builds), 1)
3500 self.assertEqual(self.builds[0].name, 'project-merge')
3501
Paul Belanger174a8272017-03-14 13:20:10 -04003502 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003503 self.waitUntilSettled()
3504
3505 # Only C's test jobs are queued because window is still 1.
3506 self.assertEqual(len(self.builds), 2)
3507 self.assertEqual(self.builds[0].name, 'project-test1')
3508 self.assertEqual(self.builds[1].name, 'project-test2')
3509
Paul Belanger174a8272017-03-14 13:20:10 -04003510 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003511 self.waitUntilSettled()
3512
3513 # C was successfully merged so window is increased to 2.
3514 self.assertEqual(queue.window, 2)
3515 self.assertEqual(queue.window_floor, 1)
3516 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003517
James E. Blairec056492016-07-22 09:45:56 -07003518 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003519 def test_worker_update_metadata(self):
3520 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003521 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003522
3523 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003524 A.addApproval('code-review', 2)
3525 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003526 self.waitUntilSettled()
3527
Paul Belanger174a8272017-03-14 13:20:10 -04003528 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003529
3530 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003531 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003532
3533 start = time.time()
3534 while True:
3535 if time.time() - start > 10:
3536 raise Exception("Timeout waiting for gearman server to report "
3537 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003538 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003539 if build.worker.name == "My Worker":
3540 break
3541 else:
3542 time.sleep(0)
3543
3544 self.log.debug(build)
3545 self.assertEqual("My Worker", build.worker.name)
3546 self.assertEqual("localhost", build.worker.hostname)
3547 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3548 self.assertEqual("zuul.example.org", build.worker.fqdn)
3549 self.assertEqual("FakeBuilder", build.worker.program)
3550 self.assertEqual("v1.1", build.worker.version)
3551 self.assertEqual({'something': 'else'}, build.worker.extra)
3552
Paul Belanger174a8272017-03-14 13:20:10 -04003553 self.executor_server.hold_jobs_in_build = False
3554 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003555 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003556
3557 def test_footer_message(self):
3558 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003559 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003560 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003561
3562 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003563 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003564 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003565 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003566 self.waitUntilSettled()
3567
3568 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003569 B.addApproval('code-review', 2)
3570 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003571 self.waitUntilSettled()
3572
3573 self.assertEqual(2, len(self.smtp_messages))
3574
Adam Gandelman82258fd2017-02-07 16:51:58 -08003575 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003576Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003577http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003578
Adam Gandelman82258fd2017-02-07 16:51:58 -08003579 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003580For CI problems and help debugging, contact ci@example.org"""
3581
Adam Gandelman82258fd2017-02-07 16:51:58 -08003582 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3583 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3584 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3585 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003586
James E. Blairec056492016-07-22 09:45:56 -07003587 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003588 def test_merge_failure_reporters(self):
3589 """Check that the config is set up correctly"""
3590
James E. Blairf84026c2015-12-08 16:11:46 -08003591 self.updateConfigLayout(
3592 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003593 self.sched.reconfigure(self.config)
3594 self.registerJobs()
3595
3596 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003597 "Merge Failed.\n\nThis change or one of its cross-repo "
3598 "dependencies was unable to be automatically merged with the "
3599 "current state of its repository. Please rebase the change and "
3600 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003601 self.sched.layout.pipelines['check'].merge_failure_message)
3602 self.assertEqual(
3603 "The merge failed! For more information...",
3604 self.sched.layout.pipelines['gate'].merge_failure_message)
3605
3606 self.assertEqual(
3607 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3608 self.assertEqual(
3609 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3610
3611 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003612 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3613 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003614
3615 self.assertTrue(
3616 (
3617 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003618 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003619 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003620 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003621 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003622 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003623 ) or (
3624 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003625 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003626 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003627 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003628 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003629 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003630 )
3631 )
3632
James E. Blairec056492016-07-22 09:45:56 -07003633 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003634 def test_merge_failure_reports(self):
3635 """Check that when a change fails to merge the correct message is sent
3636 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003637 self.updateConfigLayout(
3638 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003639 self.sched.reconfigure(self.config)
3640 self.registerJobs()
3641
3642 # Check a test failure isn't reported to SMTP
3643 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003644 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003645 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003646 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003647 self.waitUntilSettled()
3648
3649 self.assertEqual(3, len(self.history)) # 3 jobs
3650 self.assertEqual(0, len(self.smtp_messages))
3651
3652 # Check a merge failure is reported to SMTP
3653 # B should be merged, but C will conflict with B
3654 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3655 B.addPatchset(['conflict'])
3656 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3657 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003658 B.addApproval('code-review', 2)
3659 C.addApproval('code-review', 2)
3660 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3661 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003662 self.waitUntilSettled()
3663
3664 self.assertEqual(6, len(self.history)) # A and B jobs
3665 self.assertEqual(1, len(self.smtp_messages))
3666 self.assertEqual('The merge failed! For more information...',
3667 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003668
James E. Blairec056492016-07-22 09:45:56 -07003669 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003670 def test_default_merge_failure_reports(self):
3671 """Check that the default merge failure reports are correct."""
3672
3673 # A should report success, B should report merge failure.
3674 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3675 A.addPatchset(['conflict'])
3676 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3677 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003678 A.addApproval('code-review', 2)
3679 B.addApproval('code-review', 2)
3680 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3681 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003682 self.waitUntilSettled()
3683
3684 self.assertEqual(3, len(self.history)) # A jobs
3685 self.assertEqual(A.reported, 2)
3686 self.assertEqual(B.reported, 2)
3687 self.assertEqual(A.data['status'], 'MERGED')
3688 self.assertEqual(B.data['status'], 'NEW')
3689 self.assertIn('Build succeeded', A.messages[1])
3690 self.assertIn('Merge Failed', B.messages[1])
3691 self.assertIn('automatically merged', B.messages[1])
3692 self.assertNotIn('logs.example.com', B.messages[1])
3693 self.assertNotIn('SKIPPED', B.messages[1])
3694
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003695 def test_client_get_running_jobs(self):
3696 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003697 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003698 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003699 A.addApproval('code-review', 2)
3700 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003701 self.waitUntilSettled()
3702
3703 client = zuul.rpcclient.RPCClient('127.0.0.1',
3704 self.gearman_server.port)
3705
3706 # Wait for gearman server to send the initial workData back to zuul
3707 start = time.time()
3708 while True:
3709 if time.time() - start > 10:
3710 raise Exception("Timeout waiting for gearman server to report "
3711 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003712 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003713 if build.worker.name == "My Worker":
3714 break
3715 else:
3716 time.sleep(0)
3717
3718 running_items = client.get_running_jobs()
3719
3720 self.assertEqual(1, len(running_items))
3721 running_item = running_items[0]
3722 self.assertEqual([], running_item['failing_reasons'])
3723 self.assertEqual([], running_item['items_behind'])
3724 self.assertEqual('https://hostname/1', running_item['url'])
3725 self.assertEqual(None, running_item['item_ahead'])
3726 self.assertEqual('org/project', running_item['project'])
3727 self.assertEqual(None, running_item['remaining_time'])
3728 self.assertEqual(True, running_item['active'])
3729 self.assertEqual('1,1', running_item['id'])
3730
3731 self.assertEqual(3, len(running_item['jobs']))
3732 for job in running_item['jobs']:
3733 if job['name'] == 'project-merge':
3734 self.assertEqual('project-merge', job['name'])
3735 self.assertEqual('gate', job['pipeline'])
3736 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003737 self.assertEqual('https://server/job/project-merge/0/',
3738 job['url'])
3739 self.assertEqual(7, len(job['worker']))
3740 self.assertEqual(False, job['canceled'])
3741 self.assertEqual(True, job['voting'])
3742 self.assertEqual(None, job['result'])
3743 self.assertEqual('gate', job['pipeline'])
3744 break
3745
Paul Belanger174a8272017-03-14 13:20:10 -04003746 self.executor_server.hold_jobs_in_build = False
3747 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003748 self.waitUntilSettled()
3749
3750 running_items = client.get_running_jobs()
3751 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003752
3753 def test_nonvoting_pipeline(self):
3754 "Test that a nonvoting pipeline (experimental) can still report"
3755
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003756 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3757 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003758 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3759 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003760
3761 self.assertEqual(self.getJobFromHistory('project-merge').result,
3762 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003763 self.assertEqual(
3764 self.getJobFromHistory('experimental-project-test').result,
3765 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003766 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003767
3768 def test_crd_gate(self):
3769 "Test cross-repo dependencies"
3770 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3771 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003772 A.addApproval('code-review', 2)
3773 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003774
3775 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3776 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3777 AM2.setMerged()
3778 AM1.setMerged()
3779
3780 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3781 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3782 BM2.setMerged()
3783 BM1.setMerged()
3784
3785 # A -> AM1 -> AM2
3786 # B -> BM1 -> BM2
3787 # A Depends-On: B
3788 # M2 is here to make sure it is never queried. If it is, it
3789 # means zuul is walking down the entire history of merged
3790 # changes.
3791
3792 B.setDependsOn(BM1, 1)
3793 BM1.setDependsOn(BM2, 1)
3794
3795 A.setDependsOn(AM1, 1)
3796 AM1.setDependsOn(AM2, 1)
3797
3798 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3799 A.subject, B.data['id'])
3800
James E. Blair8b5408c2016-08-08 15:37:46 -07003801 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003802 self.waitUntilSettled()
3803
3804 self.assertEqual(A.data['status'], 'NEW')
3805 self.assertEqual(B.data['status'], 'NEW')
3806
Paul Belanger5dccbe72016-11-14 11:17:37 -05003807 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003808 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003809
Paul Belanger174a8272017-03-14 13:20:10 -04003810 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003811 B.addApproval('approved', 1)
3812 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003813 self.waitUntilSettled()
3814
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.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003818 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003819 self.executor_server.hold_jobs_in_build = False
3820 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003821 self.waitUntilSettled()
3822
3823 self.assertEqual(AM2.queried, 0)
3824 self.assertEqual(BM2.queried, 0)
3825 self.assertEqual(A.data['status'], 'MERGED')
3826 self.assertEqual(B.data['status'], 'MERGED')
3827 self.assertEqual(A.reported, 2)
3828 self.assertEqual(B.reported, 2)
3829
Paul Belanger5dccbe72016-11-14 11:17:37 -05003830 changes = self.getJobFromHistory(
3831 'project-merge', 'org/project1').changes
3832 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003833
3834 def test_crd_branch(self):
3835 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003836
3837 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003838 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3839 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3840 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3841 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003842 A.addApproval('code-review', 2)
3843 B.addApproval('code-review', 2)
3844 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003845
3846 # A Depends-On: B+C
3847 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3848 A.subject, B.data['id'])
3849
Paul Belanger174a8272017-03-14 13:20:10 -04003850 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003851 B.addApproval('approved', 1)
3852 C.addApproval('approved', 1)
3853 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003854 self.waitUntilSettled()
3855
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.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003861 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003862 self.executor_server.hold_jobs_in_build = False
3863 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003864 self.waitUntilSettled()
3865
3866 self.assertEqual(A.data['status'], 'MERGED')
3867 self.assertEqual(B.data['status'], 'MERGED')
3868 self.assertEqual(C.data['status'], 'MERGED')
3869 self.assertEqual(A.reported, 2)
3870 self.assertEqual(B.reported, 2)
3871 self.assertEqual(C.reported, 2)
3872
Paul Belanger6379db12016-11-14 13:57:54 -05003873 changes = self.getJobFromHistory(
3874 'project-merge', 'org/project1').changes
3875 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003876
3877 def test_crd_multiline(self):
3878 "Test multiple depends-on lines in commit"
3879 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3880 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3881 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003882 A.addApproval('code-review', 2)
3883 B.addApproval('code-review', 2)
3884 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003885
3886 # A Depends-On: B+C
3887 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3888 A.subject, B.data['id'], C.data['id'])
3889
Paul Belanger174a8272017-03-14 13:20:10 -04003890 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003891 B.addApproval('approved', 1)
3892 C.addApproval('approved', 1)
3893 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003894 self.waitUntilSettled()
3895
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.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003901 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003902 self.executor_server.hold_jobs_in_build = False
3903 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003904 self.waitUntilSettled()
3905
3906 self.assertEqual(A.data['status'], 'MERGED')
3907 self.assertEqual(B.data['status'], 'MERGED')
3908 self.assertEqual(C.data['status'], 'MERGED')
3909 self.assertEqual(A.reported, 2)
3910 self.assertEqual(B.reported, 2)
3911 self.assertEqual(C.reported, 2)
3912
Paul Belanger5dccbe72016-11-14 11:17:37 -05003913 changes = self.getJobFromHistory(
3914 'project-merge', 'org/project1').changes
3915 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003916
3917 def test_crd_unshared_gate(self):
3918 "Test cross-repo dependencies in unshared gate queues"
3919 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3920 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003921 A.addApproval('code-review', 2)
3922 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003923
3924 # A Depends-On: B
3925 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3926 A.subject, B.data['id'])
3927
3928 # A and B do not share a queue, make sure that A is unable to
3929 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003930 B.addApproval('approved', 1)
3931 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003932 self.waitUntilSettled()
3933
3934 self.assertEqual(A.data['status'], 'NEW')
3935 self.assertEqual(B.data['status'], 'NEW')
3936 self.assertEqual(A.reported, 0)
3937 self.assertEqual(B.reported, 0)
3938 self.assertEqual(len(self.history), 0)
3939
3940 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003941 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003942 self.waitUntilSettled()
3943
3944 self.assertEqual(B.data['status'], 'MERGED')
3945 self.assertEqual(B.reported, 2)
3946
3947 # Now that B is merged, A should be able to be enqueued and
3948 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003949 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003950 self.waitUntilSettled()
3951
3952 self.assertEqual(A.data['status'], 'MERGED')
3953 self.assertEqual(A.reported, 2)
3954
James E. Blair96698e22015-04-02 07:48:21 -07003955 def test_crd_gate_reverse(self):
3956 "Test reverse cross-repo dependencies"
3957 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3958 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003959 A.addApproval('code-review', 2)
3960 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003961
3962 # A Depends-On: B
3963
3964 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3965 A.subject, B.data['id'])
3966
James E. Blair8b5408c2016-08-08 15:37:46 -07003967 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003968 self.waitUntilSettled()
3969
3970 self.assertEqual(A.data['status'], 'NEW')
3971 self.assertEqual(B.data['status'], 'NEW')
3972
Paul Belanger174a8272017-03-14 13:20:10 -04003973 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003974 A.addApproval('approved', 1)
3975 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003976 self.waitUntilSettled()
3977
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.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003981 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003982 self.executor_server.hold_jobs_in_build = False
3983 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003984 self.waitUntilSettled()
3985
3986 self.assertEqual(A.data['status'], 'MERGED')
3987 self.assertEqual(B.data['status'], 'MERGED')
3988 self.assertEqual(A.reported, 2)
3989 self.assertEqual(B.reported, 2)
3990
Paul Belanger5dccbe72016-11-14 11:17:37 -05003991 changes = self.getJobFromHistory(
3992 'project-merge', 'org/project1').changes
3993 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003994
James E. Blair5ee24252014-12-30 10:12:29 -08003995 def test_crd_cycle(self):
3996 "Test cross-repo dependency cycles"
3997 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3998 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003999 A.addApproval('code-review', 2)
4000 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004001
4002 # A -> B -> A (via commit-depends)
4003
4004 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4005 A.subject, B.data['id'])
4006 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4007 B.subject, A.data['id'])
4008
James E. Blair8b5408c2016-08-08 15:37:46 -07004009 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004010 self.waitUntilSettled()
4011
4012 self.assertEqual(A.reported, 0)
4013 self.assertEqual(B.reported, 0)
4014 self.assertEqual(A.data['status'], 'NEW')
4015 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004016
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004017 def test_crd_gate_unknown(self):
4018 "Test unknown projects in dependent pipeline"
4019 self.init_repo("org/unknown")
4020 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4021 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004022 A.addApproval('code-review', 2)
4023 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004024
4025 # A Depends-On: B
4026 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4027 A.subject, B.data['id'])
4028
James E. Blair8b5408c2016-08-08 15:37:46 -07004029 B.addApproval('approved', 1)
4030 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004031 self.waitUntilSettled()
4032
4033 # Unknown projects cannot share a queue with any other
4034 # since they don't have common jobs with any other (they have no jobs).
4035 # Changes which depend on unknown project changes
4036 # should not be processed in dependent pipeline
4037 self.assertEqual(A.data['status'], 'NEW')
4038 self.assertEqual(B.data['status'], 'NEW')
4039 self.assertEqual(A.reported, 0)
4040 self.assertEqual(B.reported, 0)
4041 self.assertEqual(len(self.history), 0)
4042
4043 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004044 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004045 B.setMerged()
4046 self.waitUntilSettled()
4047 self.assertEqual(len(self.history), 0)
4048
4049 # Now that B is merged, A should be able to be enqueued and
4050 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004051 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004052 self.waitUntilSettled()
4053
4054 self.assertEqual(A.data['status'], 'MERGED')
4055 self.assertEqual(A.reported, 2)
4056 self.assertEqual(B.data['status'], 'MERGED')
4057 self.assertEqual(B.reported, 0)
4058
James E. Blairbfb8e042014-12-30 17:01:44 -08004059 def test_crd_check(self):
4060 "Test cross-repo dependencies in independent pipelines"
4061
Paul Belanger174a8272017-03-14 13:20:10 -04004062 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004063 self.gearman_server.hold_jobs_in_queue = True
4064 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4065 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4066
4067 # A Depends-On: B
4068 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4069 A.subject, B.data['id'])
4070
4071 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4072 self.waitUntilSettled()
4073
4074 queue = self.gearman_server.getQueue()
4075 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4076 self.gearman_server.hold_jobs_in_queue = False
4077 self.gearman_server.release()
4078 self.waitUntilSettled()
4079
Paul Belanger174a8272017-03-14 13:20:10 -04004080 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004081 self.waitUntilSettled()
4082
Monty Taylord642d852017-02-23 14:05:42 -05004083 path = os.path.join(self.builds[0].jobdir.src_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004084 repo = git.Repo(path)
4085 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4086 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004087 correct_messages = [
4088 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004089 self.assertEqual(repo_messages, correct_messages)
4090
Monty Taylord642d852017-02-23 14:05:42 -05004091 path = os.path.join(self.builds[0].jobdir.src_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004092 repo = git.Repo(path)
4093 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4094 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004095 correct_messages = [
4096 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004097 self.assertEqual(repo_messages, correct_messages)
4098
Paul Belanger174a8272017-03-14 13:20:10 -04004099 self.executor_server.hold_jobs_in_build = False
4100 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004101 self.waitUntilSettled()
4102
James E. Blairbfb8e042014-12-30 17:01:44 -08004103 self.assertEqual(A.data['status'], 'NEW')
4104 self.assertEqual(B.data['status'], 'NEW')
4105 self.assertEqual(A.reported, 1)
4106 self.assertEqual(B.reported, 0)
4107
4108 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004109 tenant = self.sched.abide.tenants.get('tenant-one')
4110 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004111
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004112 def test_crd_check_git_depends(self):
4113 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004114 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004115 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4116 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4117
4118 # Add two git-dependent changes and make sure they both report
4119 # success.
4120 B.setDependsOn(A, 1)
4121 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4122 self.waitUntilSettled()
4123 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4124 self.waitUntilSettled()
4125
James E. Blairb8c16472015-05-05 14:55:26 -07004126 self.orderedRelease()
4127 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004128 self.waitUntilSettled()
4129
4130 self.assertEqual(A.data['status'], 'NEW')
4131 self.assertEqual(B.data['status'], 'NEW')
4132 self.assertEqual(A.reported, 1)
4133 self.assertEqual(B.reported, 1)
4134
4135 self.assertEqual(self.history[0].changes, '1,1')
4136 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004137 tenant = self.sched.abide.tenants.get('tenant-one')
4138 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004139
4140 self.assertIn('Build succeeded', A.messages[0])
4141 self.assertIn('Build succeeded', B.messages[0])
4142
4143 def test_crd_check_duplicate(self):
4144 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004145 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004146 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4147 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004148 tenant = self.sched.abide.tenants.get('tenant-one')
4149 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004150
4151 # Add two git-dependent changes...
4152 B.setDependsOn(A, 1)
4153 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4154 self.waitUntilSettled()
4155 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4156
4157 # ...make sure the live one is not duplicated...
4158 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4159 self.waitUntilSettled()
4160 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4161
4162 # ...but the non-live one is able to be.
4163 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4164 self.waitUntilSettled()
4165 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4166
Clark Boylandd849822015-03-02 12:38:14 -08004167 # Release jobs in order to avoid races with change A jobs
4168 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004169 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004170 self.executor_server.hold_jobs_in_build = False
4171 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004172 self.waitUntilSettled()
4173
4174 self.assertEqual(A.data['status'], 'NEW')
4175 self.assertEqual(B.data['status'], 'NEW')
4176 self.assertEqual(A.reported, 1)
4177 self.assertEqual(B.reported, 1)
4178
4179 self.assertEqual(self.history[0].changes, '1,1 2,1')
4180 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004181 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004182
4183 self.assertIn('Build succeeded', A.messages[0])
4184 self.assertIn('Build succeeded', B.messages[0])
4185
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004186 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004187 "Test cross-repo dependencies re-enqueued in independent pipelines"
4188
4189 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004190 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4191 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004192
4193 # A Depends-On: B
4194 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4195 A.subject, B.data['id'])
4196
4197 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4198 self.waitUntilSettled()
4199
4200 self.sched.reconfigure(self.config)
4201
4202 # Make sure the items still share a change queue, and the
4203 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004204 tenant = self.sched.abide.tenants.get('tenant-one')
4205 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4206 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004207 first_item = queue.queue[0]
4208 for item in queue.queue:
4209 self.assertEqual(item.queue, first_item.queue)
4210 self.assertFalse(first_item.live)
4211 self.assertTrue(queue.queue[1].live)
4212
4213 self.gearman_server.hold_jobs_in_queue = False
4214 self.gearman_server.release()
4215 self.waitUntilSettled()
4216
4217 self.assertEqual(A.data['status'], 'NEW')
4218 self.assertEqual(B.data['status'], 'NEW')
4219 self.assertEqual(A.reported, 1)
4220 self.assertEqual(B.reported, 0)
4221
4222 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004223 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004224
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004225 def test_crd_check_reconfiguration(self):
4226 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4227
4228 def test_crd_undefined_project(self):
4229 """Test that undefined projects in dependencies are handled for
4230 independent pipelines"""
4231 # It's a hack for fake gerrit,
4232 # as it implies repo creation upon the creation of any change
4233 self.init_repo("org/unknown")
4234 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4235
James E. Blairec056492016-07-22 09:45:56 -07004236 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004237 def test_crd_check_ignore_dependencies(self):
4238 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004239 self.updateConfigLayout(
4240 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004241 self.sched.reconfigure(self.config)
4242 self.registerJobs()
4243
4244 self.gearman_server.hold_jobs_in_queue = True
4245 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4246 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4247 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4248
4249 # A Depends-On: B
4250 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4251 A.subject, B.data['id'])
4252 # C git-depends on B
4253 C.setDependsOn(B, 1)
4254 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4255 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4256 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4257 self.waitUntilSettled()
4258
4259 # Make sure none of the items share a change queue, and all
4260 # are live.
4261 check_pipeline = self.sched.layout.pipelines['check']
4262 self.assertEqual(len(check_pipeline.queues), 3)
4263 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4264 for item in check_pipeline.getAllItems():
4265 self.assertTrue(item.live)
4266
4267 self.gearman_server.hold_jobs_in_queue = False
4268 self.gearman_server.release()
4269 self.waitUntilSettled()
4270
4271 self.assertEqual(A.data['status'], 'NEW')
4272 self.assertEqual(B.data['status'], 'NEW')
4273 self.assertEqual(C.data['status'], 'NEW')
4274 self.assertEqual(A.reported, 1)
4275 self.assertEqual(B.reported, 1)
4276 self.assertEqual(C.reported, 1)
4277
4278 # Each job should have tested exactly one change
4279 for job in self.history:
4280 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004281
James E. Blairec056492016-07-22 09:45:56 -07004282 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004283 def test_crd_check_transitive(self):
4284 "Test transitive cross-repo dependencies"
4285 # Specifically, if A -> B -> C, and C gets a new patchset and
4286 # A gets a new patchset, ensure the test of A,2 includes B,1
4287 # and C,2 (not C,1 which would indicate stale data in the
4288 # cache for B).
4289 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4290 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4291 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4292
4293 # A Depends-On: B
4294 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4295 A.subject, B.data['id'])
4296
4297 # B Depends-On: C
4298 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4299 B.subject, C.data['id'])
4300
4301 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4302 self.waitUntilSettled()
4303 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4304
4305 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4306 self.waitUntilSettled()
4307 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4308
4309 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4310 self.waitUntilSettled()
4311 self.assertEqual(self.history[-1].changes, '3,1')
4312
4313 C.addPatchset()
4314 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4315 self.waitUntilSettled()
4316 self.assertEqual(self.history[-1].changes, '3,2')
4317
4318 A.addPatchset()
4319 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4320 self.waitUntilSettled()
4321 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004322
James E. Blaira8b90b32016-08-24 15:18:50 -07004323 def test_crd_check_unknown(self):
4324 "Test unknown projects in independent pipeline"
4325 self.init_repo("org/unknown")
4326 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4327 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4328 # A Depends-On: B
4329 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4330 A.subject, B.data['id'])
4331
4332 # Make sure zuul has seen an event on B.
4333 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4334 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4335 self.waitUntilSettled()
4336
4337 self.assertEqual(A.data['status'], 'NEW')
4338 self.assertEqual(A.reported, 1)
4339 self.assertEqual(B.data['status'], 'NEW')
4340 self.assertEqual(B.reported, 0)
4341
James E. Blair92464a22016-04-05 10:21:26 -07004342 def test_crd_cycle_join(self):
4343 "Test an updated change creates a cycle"
4344 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4345
4346 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4347 self.waitUntilSettled()
4348
4349 # Create B->A
4350 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4351 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4352 B.subject, A.data['id'])
4353 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4354 self.waitUntilSettled()
4355
4356 # Update A to add A->B (a cycle).
4357 A.addPatchset()
4358 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4359 A.subject, B.data['id'])
4360 # Normally we would submit the patchset-created event for
4361 # processing here, however, we have no way of noting whether
4362 # the dependency cycle detection correctly raised an
4363 # exception, so instead, we reach into the source driver and
4364 # call the method that would ultimately be called by the event
4365 # processing.
4366
Paul Belanger0e155e22016-11-14 14:12:23 -05004367 tenant = self.sched.abide.tenants.get('tenant-one')
4368 source = tenant.layout.pipelines['gate'].source
4369
4370 # TODO(pabelanger): As we add more source / trigger APIs we should make
4371 # it easier for users to create events for testing.
4372 event = zuul.model.TriggerEvent()
4373 event.trigger_name = 'gerrit'
4374 event.change_number = '1'
4375 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004376 with testtools.ExpectedException(
4377 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004378 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004379 self.log.debug("Got expected dependency cycle exception")
4380
4381 # Now if we update B to remove the depends-on, everything
4382 # should be okay. B; A->B
4383
4384 B.addPatchset()
4385 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004386
4387 source.getChange(event, True)
4388 event.change_number = '2'
4389 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004390
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004391 def test_disable_at(self):
4392 "Test a pipeline will only report to the disabled trigger when failing"
4393
Paul Belanger7dc76e82016-11-11 16:51:08 -05004394 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004395 self.sched.reconfigure(self.config)
4396
Paul Belanger7dc76e82016-11-11 16:51:08 -05004397 tenant = self.sched.abide.tenants.get('openstack')
4398 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004399 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004400 0, tenant.layout.pipelines['check']._consecutive_failures)
4401 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004402
4403 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4404 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4405 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4406 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4407 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4408 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4409 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4410 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4411 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4412 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4413 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4414
Paul Belanger174a8272017-03-14 13:20:10 -04004415 self.executor_server.failJob('project-test1', A)
4416 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004417 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004418 self.executor_server.failJob('project-test1', D)
4419 self.executor_server.failJob('project-test1', E)
4420 self.executor_server.failJob('project-test1', F)
4421 self.executor_server.failJob('project-test1', G)
4422 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004423 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004424 self.executor_server.failJob('project-test1', J)
4425 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004426
4427 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4428 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4429 self.waitUntilSettled()
4430
4431 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004432 2, tenant.layout.pipelines['check']._consecutive_failures)
4433 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004434
4435 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4436 self.waitUntilSettled()
4437
4438 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004439 0, tenant.layout.pipelines['check']._consecutive_failures)
4440 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004441
4442 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4443 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4444 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4445 self.waitUntilSettled()
4446
4447 # We should be disabled now
4448 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004449 3, tenant.layout.pipelines['check']._consecutive_failures)
4450 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004451
4452 # We need to wait between each of these patches to make sure the
4453 # smtp messages come back in an expected order
4454 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4455 self.waitUntilSettled()
4456 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4457 self.waitUntilSettled()
4458 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4459 self.waitUntilSettled()
4460
4461 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4462 # leaving a message on each change
4463 self.assertEqual(1, len(A.messages))
4464 self.assertIn('Build failed.', A.messages[0])
4465 self.assertEqual(1, len(B.messages))
4466 self.assertIn('Build failed.', B.messages[0])
4467 self.assertEqual(1, len(C.messages))
4468 self.assertIn('Build succeeded.', C.messages[0])
4469 self.assertEqual(1, len(D.messages))
4470 self.assertIn('Build failed.', D.messages[0])
4471 self.assertEqual(1, len(E.messages))
4472 self.assertIn('Build failed.', E.messages[0])
4473 self.assertEqual(1, len(F.messages))
4474 self.assertIn('Build failed.', F.messages[0])
4475
4476 # The last 3 (GHI) would have only reported via smtp.
4477 self.assertEqual(3, len(self.smtp_messages))
4478 self.assertEqual(0, len(G.messages))
4479 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004480 self.assertIn(
4481 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004482 self.assertEqual(0, len(H.messages))
4483 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004484 self.assertIn(
4485 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004486 self.assertEqual(0, len(I.messages))
4487 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004488 self.assertIn(
4489 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004490
4491 # Now reload the configuration (simulate a HUP) to check the pipeline
4492 # comes out of disabled
4493 self.sched.reconfigure(self.config)
4494
Paul Belanger7dc76e82016-11-11 16:51:08 -05004495 tenant = self.sched.abide.tenants.get('openstack')
4496
4497 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004498 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004499 0, tenant.layout.pipelines['check']._consecutive_failures)
4500 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004501
4502 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4503 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4504 self.waitUntilSettled()
4505
4506 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004507 2, tenant.layout.pipelines['check']._consecutive_failures)
4508 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004509
4510 # J and K went back to gerrit
4511 self.assertEqual(1, len(J.messages))
4512 self.assertIn('Build failed.', J.messages[0])
4513 self.assertEqual(1, len(K.messages))
4514 self.assertIn('Build failed.', K.messages[0])
4515 # No more messages reported via smtp
4516 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004517
Paul Belanger71d98172016-11-08 10:56:31 -05004518 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004519 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004520
Paul Belanger174a8272017-03-14 13:20:10 -04004521 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004522 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4523 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4524 self.waitUntilSettled()
4525
Paul Belanger174a8272017-03-14 13:20:10 -04004526 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004527 self.waitUntilSettled()
4528
4529 self.assertEqual(len(self.builds), 2)
4530 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004531 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004532 self.waitUntilSettled()
4533
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004534 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004535 self.assertEqual(len(self.builds), 1,
4536 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004537 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004538 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004539 self.waitUntilSettled()
4540
Paul Belanger174a8272017-03-14 13:20:10 -04004541 self.executor_server.hold_jobs_in_build = False
4542 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004543 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004544 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004545 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4546 self.assertEqual(A.reported, 1)
4547 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004548
James E. Blair15be0e12017-01-03 13:45:20 -08004549 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004550 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004551
4552 self.fake_nodepool.paused = True
4553 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4554 A.addApproval('code-review', 2)
4555 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4556 self.waitUntilSettled()
4557
4558 self.zk.client.stop()
4559 self.zk.client.start()
4560 self.fake_nodepool.paused = False
4561 self.waitUntilSettled()
4562
4563 self.assertEqual(A.data['status'], 'MERGED')
4564 self.assertEqual(A.reported, 2)
4565
James E. Blair6ab79e02017-01-06 10:10:17 -08004566 def test_nodepool_failure(self):
4567 "Test that jobs are reported after a nodepool failure"
4568
4569 self.fake_nodepool.paused = True
4570 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4571 A.addApproval('code-review', 2)
4572 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4573 self.waitUntilSettled()
4574
4575 req = self.fake_nodepool.getNodeRequests()[0]
4576 self.fake_nodepool.addFailRequest(req)
4577
4578 self.fake_nodepool.paused = False
4579 self.waitUntilSettled()
4580
4581 self.assertEqual(A.data['status'], 'NEW')
4582 self.assertEqual(A.reported, 2)
4583 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4584 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4585 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4586
Arieb6f068c2016-10-09 13:11:06 +03004587
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004588class TestDependencyGraph(ZuulTestCase):
4589 tenant_config_file = 'config/dependency-graph/main.yaml'
4590
4591 def test_dependeny_graph_dispatch_jobs_once(self):
4592 "Test a job in a dependency graph is queued only once"
4593 # Job dependencies, starting with A
4594 # A
4595 # / \
4596 # B C
4597 # / \ / \
4598 # D F E
4599 # |
4600 # G
4601
4602 self.executor_server.hold_jobs_in_build = True
4603 change = self.fake_gerrit.addFakeChange(
4604 'org/project', 'master', 'change')
4605 change.addApproval('code-review', 2)
4606 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4607
4608 self.waitUntilSettled()
4609 self.assertEqual([b.name for b in self.builds], ['A'])
4610
4611 self.executor_server.release('A')
4612 self.waitUntilSettled()
4613 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4614
4615 self.executor_server.release('B')
4616 self.waitUntilSettled()
4617 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4618
4619 self.executor_server.release('D')
4620 self.waitUntilSettled()
4621 self.assertEqual([b.name for b in self.builds], ['C'])
4622
4623 self.executor_server.release('C')
4624 self.waitUntilSettled()
4625 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4626
4627 self.executor_server.release('F')
4628 self.waitUntilSettled()
4629 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4630
4631 self.executor_server.release('G')
4632 self.waitUntilSettled()
4633 self.assertEqual([b.name for b in self.builds], ['E'])
4634
4635 self.executor_server.release('E')
4636 self.waitUntilSettled()
4637 self.assertEqual(len(self.builds), 0)
4638
4639 self.executor_server.hold_jobs_in_build = False
4640 self.executor_server.release()
4641 self.waitUntilSettled()
4642
4643 self.assertEqual(len(self.builds), 0)
4644 self.assertEqual(len(self.history), 7)
4645
4646 self.assertEqual(change.data['status'], 'MERGED')
4647 self.assertEqual(change.reported, 2)
4648
4649 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4650 "Test that a job waits till all dependencies are successful"
4651 # Job dependencies, starting with A
4652 # A
4653 # / \
4654 # B C*
4655 # / \ / \
4656 # D F E
4657 # |
4658 # G
4659
4660 self.executor_server.hold_jobs_in_build = True
4661 change = self.fake_gerrit.addFakeChange(
4662 'org/project', 'master', 'change')
4663 change.addApproval('code-review', 2)
4664
4665 self.executor_server.failJob('C', change)
4666
4667 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4668
4669 self.waitUntilSettled()
4670 self.assertEqual([b.name for b in self.builds], ['A'])
4671
4672 self.executor_server.release('A')
4673 self.waitUntilSettled()
4674 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4675
4676 self.executor_server.release('B')
4677 self.waitUntilSettled()
4678 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4679
4680 self.executor_server.release('D')
4681 self.waitUntilSettled()
4682 self.assertEqual([b.name for b in self.builds], ['C'])
4683
4684 self.executor_server.release('C')
4685 self.waitUntilSettled()
4686 self.assertEqual(len(self.builds), 0)
4687
4688 self.executor_server.hold_jobs_in_build = False
4689 self.executor_server.release()
4690 self.waitUntilSettled()
4691
4692 self.assertEqual(len(self.builds), 0)
4693 self.assertEqual(len(self.history), 4)
4694
4695 self.assertEqual(change.data['status'], 'NEW')
4696 self.assertEqual(change.reported, 2)
4697
4698
Arieb6f068c2016-10-09 13:11:06 +03004699class TestDuplicatePipeline(ZuulTestCase):
4700 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4701
4702 def test_duplicate_pipelines(self):
4703 "Test that a change matching multiple pipelines works"
4704
4705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4706 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4707 self.waitUntilSettled()
4708
4709 self.assertHistory([
4710 dict(name='project-test1', result='SUCCESS', changes='1,1',
4711 pipeline='dup1'),
4712 dict(name='project-test1', result='SUCCESS', changes='1,1',
4713 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004714 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004715
4716 self.assertEqual(len(A.messages), 2)
4717
Paul Belangera46a3742016-11-09 14:23:03 -05004718 if 'dup1' in A.messages[0]:
4719 self.assertIn('dup1', A.messages[0])
4720 self.assertNotIn('dup2', A.messages[0])
4721 self.assertIn('project-test1', A.messages[0])
4722 self.assertIn('dup2', A.messages[1])
4723 self.assertNotIn('dup1', A.messages[1])
4724 self.assertIn('project-test1', A.messages[1])
4725 else:
4726 self.assertIn('dup1', A.messages[1])
4727 self.assertNotIn('dup2', A.messages[1])
4728 self.assertIn('project-test1', A.messages[1])
4729 self.assertIn('dup2', A.messages[0])
4730 self.assertNotIn('dup1', A.messages[0])
4731 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004732
4733
4734class TestSchedulerOneJobProject(ZuulTestCase):
4735 tenant_config_file = 'config/one-job-project/main.yaml'
4736
4737 def test_one_job_project(self):
4738 "Test that queueing works with one job"
4739 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4740 'master', 'A')
4741 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4742 'master', 'B')
4743 A.addApproval('code-review', 2)
4744 B.addApproval('code-review', 2)
4745 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4746 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4747 self.waitUntilSettled()
4748
4749 self.assertEqual(A.data['status'], 'MERGED')
4750 self.assertEqual(A.reported, 2)
4751 self.assertEqual(B.data['status'], 'MERGED')
4752 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004753
4754
4755class TestSchedulerTemplatedProject(ZuulTestCase):
4756 tenant_config_file = 'config/templated-project/main.yaml'
4757
Paul Belanger174a8272017-03-14 13:20:10 -04004758 def test_job_from_templates_executed(self):
4759 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004760
4761 A = self.fake_gerrit.addFakeChange(
4762 'org/templated-project', 'master', 'A')
4763 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4764 self.waitUntilSettled()
4765
4766 self.assertEqual(self.getJobFromHistory('project-test1').result,
4767 'SUCCESS')
4768 self.assertEqual(self.getJobFromHistory('project-test2').result,
4769 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004770
4771 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004772 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004773
4774 A = self.fake_gerrit.addFakeChange(
4775 'org/layered-project', 'master', 'A')
4776 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4777 self.waitUntilSettled()
4778
4779 self.assertEqual(self.getJobFromHistory('project-test1').result,
4780 'SUCCESS')
4781 self.assertEqual(self.getJobFromHistory('project-test2').result,
4782 'SUCCESS')
4783 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4784 ).result, 'SUCCESS')
4785 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4786 ).result, 'SUCCESS')
4787 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4788 ).result, 'SUCCESS')
4789 self.assertEqual(self.getJobFromHistory('project-test6').result,
4790 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004791
4792
4793class TestSchedulerSuccessURL(ZuulTestCase):
4794 tenant_config_file = 'config/success-url/main.yaml'
4795
4796 def test_success_url(self):
4797 "Ensure bad build params are ignored"
4798 self.sched.reconfigure(self.config)
4799 self.init_repo('org/docs')
4800
4801 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4802 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4803 self.waitUntilSettled()
4804
4805 # Both builds ran: docs-draft-test + docs-draft-test2
4806 self.assertEqual(len(self.history), 2)
4807
4808 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004809 for build in self.history:
4810 if build.name == 'docs-draft-test':
4811 uuid = build.uuid[:7]
4812 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004813
4814 # Two msgs: 'Starting...' + results
4815 self.assertEqual(len(self.smtp_messages), 2)
4816 body = self.smtp_messages[1]['body'].splitlines()
4817 self.assertEqual('Build succeeded.', body[0])
4818
4819 self.assertIn(
4820 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4821 'docs-draft-test/%s/publish-docs/' % uuid,
4822 body[2])
4823
Paul Belanger174a8272017-03-14 13:20:10 -04004824 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004825 self.assertIn(
4826 '- docs-draft-test2 https://server/job',
4827 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004828
4829
Adam Gandelman4da00f62016-12-09 15:47:33 -08004830class TestSchedulerMerges(ZuulTestCase):
4831 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004832
4833 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004834 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004835 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004836 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004837 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4838 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4839 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4840 A.addApproval('code-review', 2)
4841 B.addApproval('code-review', 2)
4842 C.addApproval('code-review', 2)
4843 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4844 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4845 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4846 self.waitUntilSettled()
4847
4848 build = self.builds[-1]
4849 ref = self.getParameter(build, 'ZUUL_REF')
4850
Monty Taylord642d852017-02-23 14:05:42 -05004851 path = os.path.join(build.jobdir.src_root, project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004852 repo = git.Repo(path)
4853 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4854 repo_messages.reverse()
4855
Paul Belanger174a8272017-03-14 13:20:10 -04004856 self.executor_server.hold_jobs_in_build = False
4857 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004858 self.waitUntilSettled()
4859
4860 return repo_messages
4861
4862 def _test_merge(self, mode):
4863 us_path = os.path.join(
4864 self.upstream_root, 'org/project-%s' % mode)
4865 expected_messages = [
4866 'initial commit',
4867 'add content from fixture',
4868 # the intermediate commits order is nondeterministic
4869 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4870 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4871 ]
4872 result = self._test_project_merge_mode(mode)
4873 self.assertEqual(result[:2], expected_messages[:2])
4874 self.assertEqual(result[-2:], expected_messages[-2:])
4875
4876 def test_project_merge_mode_merge(self):
4877 self._test_merge('merge')
4878
4879 def test_project_merge_mode_merge_resolve(self):
4880 self._test_merge('merge-resolve')
4881
4882 def test_project_merge_mode_cherrypick(self):
4883 expected_messages = [
4884 'initial commit',
4885 'add content from fixture',
4886 'A-1',
4887 'B-1',
4888 'C-1']
4889 result = self._test_project_merge_mode('cherry-pick')
4890 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004891
4892 def test_merge_branch(self):
4893 "Test that the right commits are on alternate branches"
4894 self.create_branch('org/project-merge-branches', 'mp')
4895
Paul Belanger174a8272017-03-14 13:20:10 -04004896 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004897 A = self.fake_gerrit.addFakeChange(
4898 'org/project-merge-branches', 'mp', 'A')
4899 B = self.fake_gerrit.addFakeChange(
4900 'org/project-merge-branches', 'mp', 'B')
4901 C = self.fake_gerrit.addFakeChange(
4902 'org/project-merge-branches', 'mp', 'C')
4903 A.addApproval('code-review', 2)
4904 B.addApproval('code-review', 2)
4905 C.addApproval('code-review', 2)
4906 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4907 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4908 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4909 self.waitUntilSettled()
4910
Paul Belanger174a8272017-03-14 13:20:10 -04004911 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004912 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004913 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004914 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004915 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004916 self.waitUntilSettled()
4917
4918 build = self.builds[-1]
4919 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4920 ref = self.getParameter(build, 'ZUUL_REF')
4921 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004922 build.jobdir.src_root, 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004923 repo = git.Repo(path)
4924
4925 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4926 repo_messages.reverse()
4927 correct_messages = [
4928 'initial commit',
4929 'add content from fixture',
4930 'mp commit',
4931 'A-1', 'B-1', 'C-1']
4932 self.assertEqual(repo_messages, correct_messages)
4933
Paul Belanger174a8272017-03-14 13:20:10 -04004934 self.executor_server.hold_jobs_in_build = False
4935 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004936 self.waitUntilSettled()
4937
4938 def test_merge_multi_branch(self):
4939 "Test that dependent changes on multiple branches are merged"
4940 self.create_branch('org/project-merge-branches', 'mp')
4941
Paul Belanger174a8272017-03-14 13:20:10 -04004942 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004943 A = self.fake_gerrit.addFakeChange(
4944 'org/project-merge-branches', 'master', 'A')
4945 B = self.fake_gerrit.addFakeChange(
4946 'org/project-merge-branches', 'mp', 'B')
4947 C = self.fake_gerrit.addFakeChange(
4948 'org/project-merge-branches', 'master', 'C')
4949 A.addApproval('code-review', 2)
4950 B.addApproval('code-review', 2)
4951 C.addApproval('code-review', 2)
4952 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4953 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4954 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4955 self.waitUntilSettled()
4956
4957 job_A = None
4958 for job in self.builds:
4959 if 'project-merge' in job.name:
4960 job_A = job
4961 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4962 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4963 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4964 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4965
4966 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004967 job_A.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004968 repo = git.Repo(path)
4969 repo_messages = [c.message.strip()
4970 for c in repo.iter_commits(ref_A)]
4971 repo_messages.reverse()
4972 correct_messages = [
4973 'initial commit', 'add content from fixture', 'A-1']
4974 self.assertEqual(repo_messages, correct_messages)
4975
Paul Belanger174a8272017-03-14 13:20:10 -04004976 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004977 self.waitUntilSettled()
4978
4979 job_B = None
4980 for job in self.builds:
4981 if 'project-merge' in job.name:
4982 job_B = job
4983 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4984 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4985 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4986 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4987
4988 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004989 job_B.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004990 repo = git.Repo(path)
4991 repo_messages = [c.message.strip()
4992 for c in repo.iter_commits(ref_B)]
4993 repo_messages.reverse()
4994 correct_messages = [
4995 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4996 self.assertEqual(repo_messages, correct_messages)
4997
Paul Belanger174a8272017-03-14 13:20:10 -04004998 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004999 self.waitUntilSettled()
5000
5001 job_C = None
5002 for job in self.builds:
5003 if 'project-merge' in job.name:
5004 job_C = job
5005 ref_C = self.getParameter(job_C, 'ZUUL_REF')
5006 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
5007 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
5008 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
5009 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005010 job_C.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005011 repo = git.Repo(path)
5012 repo_messages = [c.message.strip()
5013 for c in repo.iter_commits(ref_C)]
5014
5015 repo_messages.reverse()
5016 correct_messages = [
5017 'initial commit', 'add content from fixture',
5018 'A-1', 'C-1']
5019 # Ensure the right commits are in the history for this ref
5020 self.assertEqual(repo_messages, correct_messages)
5021
Paul Belanger174a8272017-03-14 13:20:10 -04005022 self.executor_server.hold_jobs_in_build = False
5023 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005024 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005025
5026
5027class TestSemaphoreMultiTenant(ZuulTestCase):
5028 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5029
5030 def test_semaphore_tenant_isolation(self):
5031 "Test semaphores in multiple tenants"
5032
5033 self.waitUntilSettled()
5034 tenant_one = self.sched.abide.tenants.get('tenant-one')
5035 tenant_two = self.sched.abide.tenants.get('tenant-two')
5036
5037 self.executor_server.hold_jobs_in_build = True
5038 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5039 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5040 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5041 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5042 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5043 self.assertFalse('test-semaphore' in
5044 tenant_one.semaphore_handler.semaphores)
5045 self.assertFalse('test-semaphore' in
5046 tenant_two.semaphore_handler.semaphores)
5047
5048 # add patches to project1 of tenant-one
5049 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5050 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5051 self.waitUntilSettled()
5052
5053 # one build of project1-test1 must run
5054 # semaphore of tenant-one must be acquired once
5055 # semaphore of tenant-two must not be acquired
5056 self.assertEqual(len(self.builds), 1)
5057 self.assertEqual(self.builds[0].name, 'project1-test1')
5058 self.assertTrue('test-semaphore' in
5059 tenant_one.semaphore_handler.semaphores)
5060 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5061 'test-semaphore', [])), 1)
5062 self.assertFalse('test-semaphore' in
5063 tenant_two.semaphore_handler.semaphores)
5064
5065 # add patches to project2 of tenant-two
5066 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5067 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5068 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5069 self.waitUntilSettled()
5070
5071 # one build of project1-test1 must run
5072 # two builds of project2-test1 must run
5073 # semaphore of tenant-one must be acquired once
5074 # semaphore of tenant-two must be acquired twice
5075 self.assertEqual(len(self.builds), 3)
5076 self.assertEqual(self.builds[0].name, 'project1-test1')
5077 self.assertEqual(self.builds[1].name, 'project2-test1')
5078 self.assertEqual(self.builds[2].name, 'project2-test1')
5079 self.assertTrue('test-semaphore' in
5080 tenant_one.semaphore_handler.semaphores)
5081 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5082 'test-semaphore', [])), 1)
5083 self.assertTrue('test-semaphore' in
5084 tenant_two.semaphore_handler.semaphores)
5085 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5086 'test-semaphore', [])), 2)
5087
5088 self.executor_server.release('project1-test1')
5089 self.waitUntilSettled()
5090
5091 # one build of project1-test1 must run
5092 # two builds of project2-test1 must run
5093 # semaphore of tenant-one must be acquired once
5094 # semaphore of tenant-two must be acquired twice
5095 self.assertEqual(len(self.builds), 3)
5096 self.assertEqual(self.builds[0].name, 'project2-test1')
5097 self.assertEqual(self.builds[1].name, 'project2-test1')
5098 self.assertEqual(self.builds[2].name, 'project1-test1')
5099 self.assertTrue('test-semaphore' in
5100 tenant_one.semaphore_handler.semaphores)
5101 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5102 'test-semaphore', [])), 1)
5103 self.assertTrue('test-semaphore' in
5104 tenant_two.semaphore_handler.semaphores)
5105 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5106 'test-semaphore', [])), 2)
5107
5108 self.executor_server.release('project2-test1')
5109 self.waitUntilSettled()
5110
5111 # one build of project1-test1 must run
5112 # one build of project2-test1 must run
5113 # semaphore of tenant-one must be acquired once
5114 # semaphore of tenant-two must be acquired once
5115 self.assertEqual(len(self.builds), 2)
5116 self.assertTrue('test-semaphore' in
5117 tenant_one.semaphore_handler.semaphores)
5118 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5119 'test-semaphore', [])), 1)
5120 self.assertTrue('test-semaphore' in
5121 tenant_two.semaphore_handler.semaphores)
5122 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5123 'test-semaphore', [])), 1)
5124
5125 self.executor_server.hold_jobs_in_build = False
5126 self.executor_server.release()
5127
5128 self.waitUntilSettled()
5129
5130 # no build must run
5131 # semaphore of tenant-one must not be acquired
5132 # semaphore of tenant-two must not be acquired
5133 self.assertEqual(len(self.builds), 0)
5134 self.assertFalse('test-semaphore' in
5135 tenant_one.semaphore_handler.semaphores)
5136 self.assertFalse('test-semaphore' in
5137 tenant_two.semaphore_handler.semaphores)
5138
5139 self.assertEqual(A.reported, 1)
5140 self.assertEqual(B.reported, 1)
5141
5142
5143class TestSemaphoreInRepo(ZuulTestCase):
5144 tenant_config_file = 'config/in-repo/main.yaml'
5145
5146 def test_semaphore_in_repo(self):
5147 "Test semaphores in repo config"
5148
5149 # This tests dynamic semaphore handling in project repos. The semaphore
5150 # max value should not be evaluated dynamically but must be updated
5151 # after the change lands.
5152
5153 self.waitUntilSettled()
5154 tenant = self.sched.abide.tenants.get('tenant-one')
5155
5156 in_repo_conf = textwrap.dedent(
5157 """
5158 - job:
5159 name: project-test2
5160 semaphore: test-semaphore
5161
5162 - project:
5163 name: org/project
5164 tenant-one-gate:
5165 jobs:
5166 - project-test2
5167
5168 # the max value in dynamic layout must be ignored
5169 - semaphore:
5170 name: test-semaphore
5171 max: 2
5172 """)
5173
5174 in_repo_playbook = textwrap.dedent(
5175 """
5176 - hosts: all
5177 tasks: []
5178 """)
5179
5180 file_dict = {'.zuul.yaml': in_repo_conf,
5181 'playbooks/project-test2.yaml': in_repo_playbook}
5182 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5183 files=file_dict)
5184 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5185 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5186 B.setDependsOn(A, 1)
5187 C.setDependsOn(A, 1)
5188
5189 self.executor_server.hold_jobs_in_build = True
5190
5191 A.addApproval('code-review', 2)
5192 B.addApproval('code-review', 2)
5193 C.addApproval('code-review', 2)
5194 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5195 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5196 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5197 self.waitUntilSettled()
5198
5199 # check that the layout in a queue item still has max value of 1
5200 # for test-semaphore
5201 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5202 queue = None
5203 for queue_candidate in pipeline.queues:
5204 if queue_candidate.name == 'org/project':
5205 queue = queue_candidate
5206 break
5207 queue_item = queue.queue[0]
5208 item_dynamic_layout = queue_item.current_build_set.layout
5209 dynamic_test_semaphore = \
5210 item_dynamic_layout.semaphores.get('test-semaphore')
5211 self.assertEqual(dynamic_test_semaphore.max, 1)
5212
5213 # one build must be in queue, one semaphores acquired
5214 self.assertEqual(len(self.builds), 1)
5215 self.assertEqual(self.builds[0].name, 'project-test2')
5216 self.assertTrue('test-semaphore' in
5217 tenant.semaphore_handler.semaphores)
5218 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5219 'test-semaphore', [])), 1)
5220
5221 self.executor_server.release('project-test2')
5222 self.waitUntilSettled()
5223
5224 # change A must be merged
5225 self.assertEqual(A.data['status'], 'MERGED')
5226 self.assertEqual(A.reported, 2)
5227
5228 # send change-merged event as the gerrit mock doesn't send it
5229 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5230 self.waitUntilSettled()
5231
5232 # now that change A was merged, the new semaphore max must be effective
5233 tenant = self.sched.abide.tenants.get('tenant-one')
5234 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5235
5236 # two builds must be in queue, two semaphores acquired
5237 self.assertEqual(len(self.builds), 2)
5238 self.assertEqual(self.builds[0].name, 'project-test2')
5239 self.assertEqual(self.builds[1].name, 'project-test2')
5240 self.assertTrue('test-semaphore' in
5241 tenant.semaphore_handler.semaphores)
5242 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5243 'test-semaphore', [])), 2)
5244
5245 self.executor_server.release('project-test2')
5246 self.waitUntilSettled()
5247
5248 self.assertEqual(len(self.builds), 0)
5249 self.assertFalse('test-semaphore' in
5250 tenant.semaphore_handler.semaphores)
5251
5252 self.executor_server.hold_jobs_in_build = False
5253 self.executor_server.release()
5254
5255 self.waitUntilSettled()
5256 self.assertEqual(len(self.builds), 0)
5257
5258 self.assertEqual(A.reported, 2)
5259 self.assertEqual(B.reported, 2)
5260 self.assertEqual(C.reported, 2)