blob: d3e60945c8b6d964a74d47bd37bd7d36dbba4f10 [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
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700929 def test_project_merge_conflict(self):
930 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700931
932 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700933 A = self.fake_gerrit.addFakeChange('org/project',
934 'master', 'A',
935 files={'conflict': 'foo'})
936 B = self.fake_gerrit.addFakeChange('org/project',
937 'master', 'B',
938 files={'conflict': 'bar'})
939 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700940 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700941 A.addApproval('code-review', 2)
942 B.addApproval('code-review', 2)
943 C.addApproval('code-review', 2)
944 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
945 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
946 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700947 self.waitUntilSettled()
948
James E. Blair6736beb2013-07-11 15:18:15 -0700949 self.assertEqual(A.reported, 1)
950 self.assertEqual(B.reported, 1)
951 self.assertEqual(C.reported, 1)
952
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700953 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700954 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700955 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700956 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700957 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700958 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700959
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700960 self.gearman_server.hold_jobs_in_queue = False
961 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700962 self.waitUntilSettled()
963
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400964 self.assertEqual(A.data['status'], 'MERGED')
965 self.assertEqual(B.data['status'], 'NEW')
966 self.assertEqual(C.data['status'], 'MERGED')
967 self.assertEqual(A.reported, 2)
968 self.assertEqual(B.reported, 2)
969 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700970
971 self.assertHistory([
972 dict(name='project-merge', result='SUCCESS', changes='1,1'),
973 dict(name='project-test1', result='SUCCESS', changes='1,1'),
974 dict(name='project-test2', result='SUCCESS', changes='1,1'),
975 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
976 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
977 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
978 ], ordered=False)
979
980 def test_delayed_merge_conflict(self):
981 "Test that delayed check merge conflicts are handled properly"
982
983 # Hold jobs in the gearman queue so that we can test whether
984 # the executor returns a merge failure after the scheduler has
985 # successfully merged.
986 self.gearman_server.hold_jobs_in_queue = True
987 A = self.fake_gerrit.addFakeChange('org/project',
988 'master', 'A',
989 files={'conflict': 'foo'})
990 B = self.fake_gerrit.addFakeChange('org/project',
991 'master', 'B',
992 files={'conflict': 'bar'})
993 C = self.fake_gerrit.addFakeChange('org/project',
994 'master', 'C')
995 C.setDependsOn(B, 1)
996
997 # A enters the gate queue; B and C enter the check queue
998 A.addApproval('code-review', 2)
999 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1000 self.waitUntilSettled()
1001 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1002 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1003 self.waitUntilSettled()
1004
1005 self.assertEqual(A.reported, 1)
1006 self.assertEqual(B.reported, 0) # Check does not report start
1007 self.assertEqual(C.reported, 0) # Check does not report start
1008
1009 # A merges while B and C are queued in check
1010 # Release A project-merge
1011 queue = self.gearman_server.getQueue()
1012 self.release(queue[0])
1013 self.waitUntilSettled()
1014
1015 # Release A project-test*
1016 # gate has higher precedence, so A's test jobs are added in
1017 # front of the merge jobs for B and C
1018 queue = self.gearman_server.getQueue()
1019 self.release(queue[0])
1020 self.release(queue[1])
1021 self.waitUntilSettled()
1022
1023 self.assertEqual(A.data['status'], 'MERGED')
1024 self.assertEqual(B.data['status'], 'NEW')
1025 self.assertEqual(C.data['status'], 'NEW')
1026 self.assertEqual(A.reported, 2)
1027 self.assertEqual(B.reported, 0)
1028 self.assertEqual(C.reported, 0)
1029 self.assertHistory([
1030 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1031 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1032 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1033 ], ordered=False)
1034
1035 # B and C report merge conflicts
1036 # Release B project-merge
1037 queue = self.gearman_server.getQueue()
1038 self.release(queue[0])
1039 self.waitUntilSettled()
1040
1041 # Release C
1042 self.gearman_server.hold_jobs_in_queue = False
1043 self.gearman_server.release()
1044 self.waitUntilSettled()
1045
1046 self.assertEqual(A.data['status'], 'MERGED')
1047 self.assertEqual(B.data['status'], 'NEW')
1048 self.assertEqual(C.data['status'], 'NEW')
1049 self.assertEqual(A.reported, 2)
1050 self.assertEqual(B.reported, 1)
1051 self.assertEqual(C.reported, 1)
1052
1053 self.assertHistory([
1054 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1055 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1056 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1057 dict(name='project-merge', result='MERGER_FAILURE', changes='2,1'),
1058 dict(name='project-merge', result='MERGER_FAILURE',
1059 changes='2,1 3,1'),
1060 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001061
James E. Blairdaabed22012-08-15 15:38:57 -07001062 def test_post(self):
1063 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001064
Zhongyue Luo5d556072012-09-21 02:00:47 +09001065 e = {
1066 "type": "ref-updated",
1067 "submitter": {
1068 "name": "User Name",
1069 },
1070 "refUpdate": {
1071 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1072 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1073 "refName": "master",
1074 "project": "org/project",
1075 }
1076 }
James E. Blairdaabed22012-08-15 15:38:57 -07001077 self.fake_gerrit.addEvent(e)
1078 self.waitUntilSettled()
1079
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001080 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001081 self.assertEqual(len(self.history), 1)
1082 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001083
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001084 def test_post_ignore_deletes(self):
1085 "Test that deleting refs does not trigger post jobs"
1086
1087 e = {
1088 "type": "ref-updated",
1089 "submitter": {
1090 "name": "User Name",
1091 },
1092 "refUpdate": {
1093 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1094 "newRev": "0000000000000000000000000000000000000000",
1095 "refName": "master",
1096 "project": "org/project",
1097 }
1098 }
1099 self.fake_gerrit.addEvent(e)
1100 self.waitUntilSettled()
1101
1102 job_names = [x.name for x in self.history]
1103 self.assertEqual(len(self.history), 0)
1104 self.assertNotIn('project-post', job_names)
1105
1106 def test_post_ignore_deletes_negative(self):
1107 "Test that deleting refs does trigger post jobs"
1108
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001109 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001110 self.sched.reconfigure(self.config)
1111
1112 e = {
1113 "type": "ref-updated",
1114 "submitter": {
1115 "name": "User Name",
1116 },
1117 "refUpdate": {
1118 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1119 "newRev": "0000000000000000000000000000000000000000",
1120 "refName": "master",
1121 "project": "org/project",
1122 }
1123 }
1124 self.fake_gerrit.addEvent(e)
1125 self.waitUntilSettled()
1126
1127 job_names = [x.name for x in self.history]
1128 self.assertEqual(len(self.history), 1)
1129 self.assertIn('project-post', job_names)
1130
James E. Blairec056492016-07-22 09:45:56 -07001131 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001132 def test_build_configuration_branch_interaction(self):
1133 "Test that switching between branches works"
1134 self.test_build_configuration()
1135 self.test_build_configuration_branch()
1136 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001137 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001138 repo = git.Repo(path)
1139 repo.heads.master.commit = repo.commit('init')
1140 self.test_build_configuration()
1141
James E. Blaircaec0c52012-08-22 14:52:22 -07001142 def test_dependent_changes_dequeue(self):
1143 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001144
James E. Blaircaec0c52012-08-22 14:52:22 -07001145 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1146 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1147 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001148 A.addApproval('code-review', 2)
1149 B.addApproval('code-review', 2)
1150 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001151
1152 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1153 M1.setMerged()
1154
1155 # C -> B -> A -> M1
1156
1157 C.setDependsOn(B, 1)
1158 B.setDependsOn(A, 1)
1159 A.setDependsOn(M1, 1)
1160
Paul Belanger174a8272017-03-14 13:20:10 -04001161 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001162
James E. Blair8b5408c2016-08-08 15:37:46 -07001163 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1164 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1165 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001166
1167 self.waitUntilSettled()
1168
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001169 self.assertEqual(A.data['status'], 'NEW')
1170 self.assertEqual(A.reported, 2)
1171 self.assertEqual(B.data['status'], 'NEW')
1172 self.assertEqual(B.reported, 2)
1173 self.assertEqual(C.data['status'], 'NEW')
1174 self.assertEqual(C.reported, 2)
1175 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001176
James E. Blair972e3c72013-08-29 12:04:55 -07001177 def test_failing_dependent_changes(self):
1178 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001179 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1181 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1182 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1183 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1184 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001185 A.addApproval('code-review', 2)
1186 B.addApproval('code-review', 2)
1187 C.addApproval('code-review', 2)
1188 D.addApproval('code-review', 2)
1189 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001190
1191 # E, D -> C -> B, A
1192
1193 D.setDependsOn(C, 1)
1194 C.setDependsOn(B, 1)
1195
Paul Belanger174a8272017-03-14 13:20:10 -04001196 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001197
James E. Blair8b5408c2016-08-08 15:37:46 -07001198 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1199 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1200 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1201 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1202 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001203
1204 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001205 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001206 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001207 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001208 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001209 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001210 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001211 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001212 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001213 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001214 self.waitUntilSettled()
1215
Paul Belanger174a8272017-03-14 13:20:10 -04001216 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001217 for build in self.builds:
1218 if build.parameters['ZUUL_CHANGE'] != '1':
1219 build.release()
1220 self.waitUntilSettled()
1221
Paul Belanger174a8272017-03-14 13:20:10 -04001222 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001223 self.waitUntilSettled()
1224
1225 self.assertEqual(A.data['status'], 'MERGED')
1226 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001227 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001228 self.assertEqual(B.data['status'], 'NEW')
1229 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001230 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001231 self.assertEqual(C.data['status'], 'NEW')
1232 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001233 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001234 self.assertEqual(D.data['status'], 'NEW')
1235 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001236 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001237 self.assertEqual(E.data['status'], 'MERGED')
1238 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001239 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001240 self.assertEqual(len(self.history), 18)
1241
James E. Blairec590122012-08-22 15:19:31 -07001242 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001243 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001244 # If it's dequeued more than once, we should see extra
1245 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001246
Paul Belanger174a8272017-03-14 13:20:10 -04001247 self.executor_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001248 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1249 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1250 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001251 A.addApproval('code-review', 2)
1252 B.addApproval('code-review', 2)
1253 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001254
Paul Belanger174a8272017-03-14 13:20:10 -04001255 self.executor_server.failJob('project-test1', A)
1256 self.executor_server.failJob('project-test2', A)
1257 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001258
James E. Blair8b5408c2016-08-08 15:37:46 -07001259 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1260 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1261 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001262
1263 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001264
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001265 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001266 self.assertEqual(self.builds[0].name, 'project-merge')
1267 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001268
Paul Belanger174a8272017-03-14 13:20:10 -04001269 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001270 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001271 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001273 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001274 self.waitUntilSettled()
1275
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001276 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001277 self.assertEqual(self.builds[0].name, 'project-test1')
1278 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001279 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001280 self.assertEqual(self.builds[3].name, 'project-test1')
1281 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001282 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001283 self.assertEqual(self.builds[6].name, 'project-test1')
1284 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001285 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001286
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001287 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001288 self.waitUntilSettled()
1289
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001290 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1291 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001292
Paul Belanger174a8272017-03-14 13:20:10 -04001293 self.executor_server.hold_jobs_in_build = False
1294 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001295 self.waitUntilSettled()
1296
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001297 self.assertEqual(len(self.builds), 0)
1298 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001299
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001300 self.assertEqual(A.data['status'], 'NEW')
1301 self.assertEqual(B.data['status'], 'MERGED')
1302 self.assertEqual(C.data['status'], 'MERGED')
1303 self.assertEqual(A.reported, 2)
1304 self.assertEqual(B.reported, 2)
1305 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001306
1307 def test_nonvoting_job(self):
1308 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001309
James E. Blair4ec821f2012-08-23 15:28:28 -07001310 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1311 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001312 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001313 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001314 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001315
1316 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001317
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001318 self.assertEqual(A.data['status'], 'MERGED')
1319 self.assertEqual(A.reported, 2)
1320 self.assertEqual(
1321 self.getJobFromHistory('nonvoting-project-merge').result,
1322 'SUCCESS')
1323 self.assertEqual(
1324 self.getJobFromHistory('nonvoting-project-test1').result,
1325 'SUCCESS')
1326 self.assertEqual(
1327 self.getJobFromHistory('nonvoting-project-test2').result,
1328 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001329
James E. Blair5821bd92015-09-16 08:48:15 -07001330 for build in self.builds:
1331 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1332
James E. Blaire0487072012-08-29 17:38:31 -07001333 def test_check_queue_success(self):
1334 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001335
James E. Blaire0487072012-08-29 17:38:31 -07001336 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1337 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1338
1339 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001340
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001341 self.assertEqual(A.data['status'], 'NEW')
1342 self.assertEqual(A.reported, 1)
1343 self.assertEqual(self.getJobFromHistory('project-merge').result,
1344 'SUCCESS')
1345 self.assertEqual(self.getJobFromHistory('project-test1').result,
1346 'SUCCESS')
1347 self.assertEqual(self.getJobFromHistory('project-test2').result,
1348 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001349
1350 def test_check_queue_failure(self):
1351 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001352
James E. Blaire0487072012-08-29 17:38:31 -07001353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001354 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001355 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1356
1357 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001358
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001359 self.assertEqual(A.data['status'], 'NEW')
1360 self.assertEqual(A.reported, 1)
1361 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001362 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001363 self.assertEqual(self.getJobFromHistory('project-test1').result,
1364 'SUCCESS')
1365 self.assertEqual(self.getJobFromHistory('project-test2').result,
1366 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001367
1368 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001369 # This particular test does a large amount of merges and needs a little
1370 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001371 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001372 "test that dependent changes behind dequeued changes work"
1373 # This complicated test is a reproduction of a real life bug
1374 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001375
Paul Belanger174a8272017-03-14 13:20:10 -04001376 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001377 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1378 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1379 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1380 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1381 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1382 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1383 D.setDependsOn(C, 1)
1384 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001385 A.addApproval('code-review', 2)
1386 B.addApproval('code-review', 2)
1387 C.addApproval('code-review', 2)
1388 D.addApproval('code-review', 2)
1389 E.addApproval('code-review', 2)
1390 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001391
1392 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001393
1394 # Change object re-use in the gerrit trigger is hidden if
1395 # changes are added in quick succession; waiting makes it more
1396 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001397 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001398 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001399 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001400 self.waitUntilSettled()
1401
Paul Belanger174a8272017-03-14 13:20:10 -04001402 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001403 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001404 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001405 self.waitUntilSettled()
1406
James E. Blair8b5408c2016-08-08 15:37:46 -07001407 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001408 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001409 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001410 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001411 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001412 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001413 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001414 self.waitUntilSettled()
1415
Paul Belanger174a8272017-03-14 13:20:10 -04001416 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001417 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001418 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001419 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001420 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001421 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001422 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001423 self.waitUntilSettled()
1424
1425 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001426
1427 # Grab pointers to the jobs we want to release before
1428 # releasing any, because list indexes may change as
1429 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001430 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001431 a.release()
1432 b.release()
1433 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001434 self.waitUntilSettled()
1435
Paul Belanger174a8272017-03-14 13:20:10 -04001436 self.executor_server.hold_jobs_in_build = False
1437 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001438 self.waitUntilSettled()
1439
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001440 self.assertEqual(A.data['status'], 'NEW')
1441 self.assertEqual(B.data['status'], 'MERGED')
1442 self.assertEqual(C.data['status'], 'MERGED')
1443 self.assertEqual(D.data['status'], 'MERGED')
1444 self.assertEqual(E.data['status'], 'MERGED')
1445 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001446
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001447 self.assertEqual(A.reported, 2)
1448 self.assertEqual(B.reported, 2)
1449 self.assertEqual(C.reported, 2)
1450 self.assertEqual(D.reported, 2)
1451 self.assertEqual(E.reported, 2)
1452 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001453
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001454 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1455 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001456
1457 def test_merger_repack(self):
1458 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001459
James E. Blair05fed602012-09-07 12:45:24 -07001460 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001461 A.addApproval('code-review', 2)
1462 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001463 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001464 self.assertEqual(self.getJobFromHistory('project-merge').result,
1465 'SUCCESS')
1466 self.assertEqual(self.getJobFromHistory('project-test1').result,
1467 'SUCCESS')
1468 self.assertEqual(self.getJobFromHistory('project-test2').result,
1469 'SUCCESS')
1470 self.assertEqual(A.data['status'], 'MERGED')
1471 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001472 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001473 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001474
Monty Taylord642d852017-02-23 14:05:42 -05001475 path = os.path.join(self.merger_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001476 if os.path.exists(path):
1477 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001478 path = os.path.join(self.executor_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001479 if os.path.exists(path):
1480 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001481
1482 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001483 A.addApproval('code-review', 2)
1484 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001485 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001486 self.assertEqual(self.getJobFromHistory('project-merge').result,
1487 'SUCCESS')
1488 self.assertEqual(self.getJobFromHistory('project-test1').result,
1489 'SUCCESS')
1490 self.assertEqual(self.getJobFromHistory('project-test2').result,
1491 'SUCCESS')
1492 self.assertEqual(A.data['status'], 'MERGED')
1493 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001494
James E. Blair4886f282012-11-15 09:27:33 -08001495 def test_merger_repack_large_change(self):
1496 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001497 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001498 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001499 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001500 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001501 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001502 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001503 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1504 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001505 path = os.path.join(self.upstream_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001506 repack_repo(path)
Monty Taylord642d852017-02-23 14:05:42 -05001507 path = os.path.join(self.merger_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001508 if os.path.exists(path):
1509 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001510 path = os.path.join(self.executor_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001511 if os.path.exists(path):
1512 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001513
James E. Blair8b5408c2016-08-08 15:37:46 -07001514 A.addApproval('code-review', 2)
1515 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001516 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001517 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001518 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001519 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001520 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001521 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001522 'SUCCESS')
1523 self.assertEqual(A.data['status'], 'MERGED')
1524 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001525
James E. Blair2fa50962013-01-30 21:50:41 -08001526 def test_new_patchset_dequeues_old(self):
1527 "Test that a new patchset causes the old to be dequeued"
1528 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001529 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001530 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1531 M.setMerged()
1532
1533 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1534 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1535 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1536 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001537 A.addApproval('code-review', 2)
1538 B.addApproval('code-review', 2)
1539 C.addApproval('code-review', 2)
1540 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001541
1542 C.setDependsOn(B, 1)
1543 B.setDependsOn(A, 1)
1544 A.setDependsOn(M, 1)
1545
James E. Blair8b5408c2016-08-08 15:37:46 -07001546 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1547 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1548 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1549 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001550 self.waitUntilSettled()
1551
1552 B.addPatchset()
1553 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1554 self.waitUntilSettled()
1555
Paul Belanger174a8272017-03-14 13:20:10 -04001556 self.executor_server.hold_jobs_in_build = False
1557 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001558 self.waitUntilSettled()
1559
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001560 self.assertEqual(A.data['status'], 'MERGED')
1561 self.assertEqual(A.reported, 2)
1562 self.assertEqual(B.data['status'], 'NEW')
1563 self.assertEqual(B.reported, 2)
1564 self.assertEqual(C.data['status'], 'NEW')
1565 self.assertEqual(C.reported, 2)
1566 self.assertEqual(D.data['status'], 'MERGED')
1567 self.assertEqual(D.reported, 2)
1568 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001569
James E. Blairba437362015-02-07 11:41:52 -08001570 def test_new_patchset_check(self):
1571 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001572
Paul Belanger174a8272017-03-14 13:20:10 -04001573 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001574
1575 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001576 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001577 tenant = self.sched.abide.tenants.get('tenant-one')
1578 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001579
1580 # Add two git-dependent changes
1581 B.setDependsOn(A, 1)
1582 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1583 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001584 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1585 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001586
1587 # A live item, and a non-live/live pair
1588 items = check_pipeline.getAllItems()
1589 self.assertEqual(len(items), 3)
1590
1591 self.assertEqual(items[0].change.number, '1')
1592 self.assertEqual(items[0].change.patchset, '1')
1593 self.assertFalse(items[0].live)
1594
1595 self.assertEqual(items[1].change.number, '2')
1596 self.assertEqual(items[1].change.patchset, '1')
1597 self.assertTrue(items[1].live)
1598
1599 self.assertEqual(items[2].change.number, '1')
1600 self.assertEqual(items[2].change.patchset, '1')
1601 self.assertTrue(items[2].live)
1602
1603 # Add a new patchset to A
1604 A.addPatchset()
1605 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1606 self.waitUntilSettled()
1607
1608 # The live copy of A,1 should be gone, but the non-live and B
1609 # should continue, and we should have a new A,2
1610 items = check_pipeline.getAllItems()
1611 self.assertEqual(len(items), 3)
1612
1613 self.assertEqual(items[0].change.number, '1')
1614 self.assertEqual(items[0].change.patchset, '1')
1615 self.assertFalse(items[0].live)
1616
1617 self.assertEqual(items[1].change.number, '2')
1618 self.assertEqual(items[1].change.patchset, '1')
1619 self.assertTrue(items[1].live)
1620
1621 self.assertEqual(items[2].change.number, '1')
1622 self.assertEqual(items[2].change.patchset, '2')
1623 self.assertTrue(items[2].live)
1624
1625 # Add a new patchset to B
1626 B.addPatchset()
1627 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1628 self.waitUntilSettled()
1629
1630 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1631 # but we should have a new B,2 (still based on A,1)
1632 items = check_pipeline.getAllItems()
1633 self.assertEqual(len(items), 3)
1634
1635 self.assertEqual(items[0].change.number, '1')
1636 self.assertEqual(items[0].change.patchset, '2')
1637 self.assertTrue(items[0].live)
1638
1639 self.assertEqual(items[1].change.number, '1')
1640 self.assertEqual(items[1].change.patchset, '1')
1641 self.assertFalse(items[1].live)
1642
1643 self.assertEqual(items[2].change.number, '2')
1644 self.assertEqual(items[2].change.patchset, '2')
1645 self.assertTrue(items[2].live)
1646
1647 self.builds[0].release()
1648 self.waitUntilSettled()
1649 self.builds[0].release()
1650 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001651 self.executor_server.hold_jobs_in_build = False
1652 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001653 self.waitUntilSettled()
1654
1655 self.assertEqual(A.reported, 1)
1656 self.assertEqual(B.reported, 1)
1657 self.assertEqual(self.history[0].result, 'ABORTED')
1658 self.assertEqual(self.history[0].changes, '1,1')
1659 self.assertEqual(self.history[1].result, 'ABORTED')
1660 self.assertEqual(self.history[1].changes, '1,1 2,1')
1661 self.assertEqual(self.history[2].result, 'SUCCESS')
1662 self.assertEqual(self.history[2].changes, '1,2')
1663 self.assertEqual(self.history[3].result, 'SUCCESS')
1664 self.assertEqual(self.history[3].changes, '1,1 2,2')
1665
1666 def test_abandoned_gate(self):
1667 "Test that an abandoned change is dequeued from gate"
1668
Paul Belanger174a8272017-03-14 13:20:10 -04001669 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001670
1671 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001672 A.addApproval('code-review', 2)
1673 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001674 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001675 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1676 self.assertEqual(self.builds[0].name, 'project-merge')
1677
1678 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1679 self.waitUntilSettled()
1680
Paul Belanger174a8272017-03-14 13:20:10 -04001681 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001682 self.waitUntilSettled()
1683
Arie5b4048c2016-10-05 16:12:06 +03001684 self.assertBuilds([])
1685 self.assertHistory([
1686 dict(name='project-merge', result='ABORTED', changes='1,1')],
1687 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001688 self.assertEqual(A.reported, 1,
1689 "Abandoned gate change should report only start")
1690
1691 def test_abandoned_check(self):
1692 "Test that an abandoned change is dequeued from check"
1693
Paul Belanger174a8272017-03-14 13:20:10 -04001694 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001695
1696 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1697 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001698 tenant = self.sched.abide.tenants.get('tenant-one')
1699 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001700
1701 # Add two git-dependent changes
1702 B.setDependsOn(A, 1)
1703 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1704 self.waitUntilSettled()
1705 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1706 self.waitUntilSettled()
1707 # A live item, and a non-live/live pair
1708 items = check_pipeline.getAllItems()
1709 self.assertEqual(len(items), 3)
1710
1711 self.assertEqual(items[0].change.number, '1')
1712 self.assertFalse(items[0].live)
1713
1714 self.assertEqual(items[1].change.number, '2')
1715 self.assertTrue(items[1].live)
1716
1717 self.assertEqual(items[2].change.number, '1')
1718 self.assertTrue(items[2].live)
1719
1720 # Abandon A
1721 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1722 self.waitUntilSettled()
1723
1724 # The live copy of A should be gone, but the non-live and B
1725 # should continue
1726 items = check_pipeline.getAllItems()
1727 self.assertEqual(len(items), 2)
1728
1729 self.assertEqual(items[0].change.number, '1')
1730 self.assertFalse(items[0].live)
1731
1732 self.assertEqual(items[1].change.number, '2')
1733 self.assertTrue(items[1].live)
1734
Paul Belanger174a8272017-03-14 13:20:10 -04001735 self.executor_server.hold_jobs_in_build = False
1736 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001737 self.waitUntilSettled()
1738
1739 self.assertEqual(len(self.history), 4)
1740 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001741 'Build should have been aborted')
1742 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001743 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001744
Steve Varnau7b78b312015-04-03 14:49:46 -07001745 def test_abandoned_not_timer(self):
1746 "Test that an abandoned change does not cancel timer jobs"
1747
Paul Belanger174a8272017-03-14 13:20:10 -04001748 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001749
1750 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001751 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001752 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001753 # The pipeline triggers every second, so we should have seen
1754 # several by now.
1755 time.sleep(5)
1756 self.waitUntilSettled()
1757 # Stop queuing timer triggered jobs so that the assertions
1758 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001759 # Must be in same repo, so overwrite config with another one
Clint Byrum58264dc2017-02-07 21:21:22 -08001760 self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clint Byrumead6c562017-02-01 16:34:04 -08001761
Steve Varnau7b78b312015-04-03 14:49:46 -07001762 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001763 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1764
1765 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1766 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1767 self.waitUntilSettled()
1768 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1769
1770 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1771 self.waitUntilSettled()
1772
1773 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1774
Paul Belanger174a8272017-03-14 13:20:10 -04001775 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001776 self.waitUntilSettled()
1777
Arx Cruzb1b010d2013-10-28 19:49:59 -02001778 def test_zuul_url_return(self):
1779 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001780 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001781 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001782
1783 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001784 A.addApproval('code-review', 2)
1785 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001786 self.waitUntilSettled()
1787
1788 self.assertEqual(len(self.builds), 1)
1789 for build in self.builds:
1790 self.assertTrue('ZUUL_URL' in build.parameters)
1791
Paul Belanger174a8272017-03-14 13:20:10 -04001792 self.executor_server.hold_jobs_in_build = False
1793 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001794 self.waitUntilSettled()
1795
James E. Blair2fa50962013-01-30 21:50:41 -08001796 def test_new_patchset_dequeues_old_on_head(self):
1797 "Test that a new patchset causes the old to be dequeued (at head)"
1798 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001799 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001800 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1801 M.setMerged()
1802 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1803 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1804 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1805 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001806 A.addApproval('code-review', 2)
1807 B.addApproval('code-review', 2)
1808 C.addApproval('code-review', 2)
1809 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001810
1811 C.setDependsOn(B, 1)
1812 B.setDependsOn(A, 1)
1813 A.setDependsOn(M, 1)
1814
James E. Blair8b5408c2016-08-08 15:37:46 -07001815 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1816 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1817 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1818 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001819 self.waitUntilSettled()
1820
1821 A.addPatchset()
1822 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1823 self.waitUntilSettled()
1824
Paul Belanger174a8272017-03-14 13:20:10 -04001825 self.executor_server.hold_jobs_in_build = False
1826 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001827 self.waitUntilSettled()
1828
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001829 self.assertEqual(A.data['status'], 'NEW')
1830 self.assertEqual(A.reported, 2)
1831 self.assertEqual(B.data['status'], 'NEW')
1832 self.assertEqual(B.reported, 2)
1833 self.assertEqual(C.data['status'], 'NEW')
1834 self.assertEqual(C.reported, 2)
1835 self.assertEqual(D.data['status'], 'MERGED')
1836 self.assertEqual(D.reported, 2)
1837 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001838
1839 def test_new_patchset_dequeues_old_without_dependents(self):
1840 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001841 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001842 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1843 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1844 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001845 A.addApproval('code-review', 2)
1846 B.addApproval('code-review', 2)
1847 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001848
James E. Blair8b5408c2016-08-08 15:37:46 -07001849 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1850 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1851 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001852 self.waitUntilSettled()
1853
1854 B.addPatchset()
1855 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1856 self.waitUntilSettled()
1857
Paul Belanger174a8272017-03-14 13:20:10 -04001858 self.executor_server.hold_jobs_in_build = False
1859 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001860 self.waitUntilSettled()
1861
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001862 self.assertEqual(A.data['status'], 'MERGED')
1863 self.assertEqual(A.reported, 2)
1864 self.assertEqual(B.data['status'], 'NEW')
1865 self.assertEqual(B.reported, 2)
1866 self.assertEqual(C.data['status'], 'MERGED')
1867 self.assertEqual(C.reported, 2)
1868 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001869
1870 def test_new_patchset_dequeues_old_independent_queue(self):
1871 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001872 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001873 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1874 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1875 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1876 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1877 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1878 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1879 self.waitUntilSettled()
1880
1881 B.addPatchset()
1882 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1883 self.waitUntilSettled()
1884
Paul Belanger174a8272017-03-14 13:20:10 -04001885 self.executor_server.hold_jobs_in_build = False
1886 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001887 self.waitUntilSettled()
1888
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001889 self.assertEqual(A.data['status'], 'NEW')
1890 self.assertEqual(A.reported, 1)
1891 self.assertEqual(B.data['status'], 'NEW')
1892 self.assertEqual(B.reported, 1)
1893 self.assertEqual(C.data['status'], 'NEW')
1894 self.assertEqual(C.reported, 1)
1895 self.assertEqual(len(self.history), 10)
1896 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001897
James E. Blair18c64442014-03-18 10:14:45 -07001898 def test_noop_job(self):
1899 "Test that the internal noop job works"
1900 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001901 A.addApproval('code-review', 2)
1902 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001903 self.waitUntilSettled()
1904
1905 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1906 self.assertTrue(self.sched._areAllBuildsComplete())
1907 self.assertEqual(len(self.history), 0)
1908 self.assertEqual(A.data['status'], 'MERGED')
1909 self.assertEqual(A.reported, 2)
1910
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001911 def test_no_job_project(self):
1912 "Test that reports with no jobs don't get sent"
1913 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1914 'master', 'A')
1915 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1916 self.waitUntilSettled()
1917
1918 # Change wasn't reported to
1919 self.assertEqual(A.reported, False)
1920
1921 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001922 tenant = self.sched.abide.tenants.get('tenant-one')
1923 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001924 items = check_pipeline.getAllItems()
1925 self.assertEqual(len(items), 0)
1926
1927 self.assertEqual(len(self.history), 0)
1928
James E. Blair7d0dedc2013-02-21 17:26:09 -08001929 def test_zuul_refs(self):
1930 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001931 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001932 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1933 M1.setMerged()
1934 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1935 M2.setMerged()
1936
1937 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1938 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1939 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1940 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001941 A.addApproval('code-review', 2)
1942 B.addApproval('code-review', 2)
1943 C.addApproval('code-review', 2)
1944 D.addApproval('code-review', 2)
1945 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1946 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1947 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1948 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001949
1950 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001951 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001952 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001953 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001954 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001955 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001956 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001957 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001958 self.waitUntilSettled()
1959
James E. Blair7d0dedc2013-02-21 17:26:09 -08001960 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001961 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001962 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001963 if x.parameters['ZUUL_CHANGE'] == '3':
1964 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001965 a_build = x
1966 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001967 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001968 b_build = x
1969 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001970 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001971 c_build = x
1972 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001973 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001974 d_build = x
1975 if a_build and b_build and c_build and d_build:
1976 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977
1978 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001979 self.assertIsNotNone(a_zref)
1980 self.assertIsNotNone(b_zref)
1981 self.assertIsNotNone(c_zref)
1982 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001983
1984 # And they should all be different
1985 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001986 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001987
Clint Byrum3343e3e2016-11-15 16:05:03 -08001988 # should have a, not b, and should not be in project2
1989 self.assertTrue(a_build.hasChanges(A))
1990 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001991
Clint Byrum3343e3e2016-11-15 16:05:03 -08001992 # should have a and b, and should not be in project2
1993 self.assertTrue(b_build.hasChanges(A, B))
1994 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001995
Clint Byrum3343e3e2016-11-15 16:05:03 -08001996 # should have a and b in 1, c in 2
1997 self.assertTrue(c_build.hasChanges(A, B, C))
1998 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001999
Clint Byrum3343e3e2016-11-15 16:05:03 -08002000 # should have a and b in 1, c and d in 2
2001 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002002
Paul Belanger174a8272017-03-14 13:20:10 -04002003 self.executor_server.hold_jobs_in_build = False
2004 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002005 self.waitUntilSettled()
2006
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002007 self.assertEqual(A.data['status'], 'MERGED')
2008 self.assertEqual(A.reported, 2)
2009 self.assertEqual(B.data['status'], 'MERGED')
2010 self.assertEqual(B.reported, 2)
2011 self.assertEqual(C.data['status'], 'MERGED')
2012 self.assertEqual(C.reported, 2)
2013 self.assertEqual(D.data['status'], 'MERGED')
2014 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002015
James E. Blair4a28a882013-08-23 15:17:33 -07002016 def test_rerun_on_error(self):
2017 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002018 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002019 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002020 A.addApproval('code-review', 2)
2021 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002022 self.waitUntilSettled()
2023
James E. Blair412fba82017-01-26 15:00:50 -08002024 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002025 self.executor_server.hold_jobs_in_build = False
2026 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002027 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002028 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002029 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2030
James E. Blair412e5582013-04-22 15:50:12 -07002031 def test_statsd(self):
2032 "Test each of the statsd methods used in the scheduler"
2033 import extras
2034 statsd = extras.try_import('statsd.statsd')
2035 statsd.incr('test-incr')
2036 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002037 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002038 self.assertReportedStat('test-incr', '1|c')
2039 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002040 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002041
James E. Blairdad52252014-02-07 16:59:17 -08002042 def test_stuck_job_cleanup(self):
2043 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002044
2045 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002046 self.gearman_server.hold_jobs_in_queue = True
2047 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002048 A.addApproval('code-review', 2)
2049 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002050 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002051 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002052 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2053
Clint Byrum678e2c32017-03-16 16:27:21 -07002054 self.commitLayoutUpdate('common-config', 'layout-no-jobs')
James E. Blairdad52252014-02-07 16:59:17 -08002055 self.sched.reconfigure(self.config)
2056 self.waitUntilSettled()
2057
James E. Blair18c64442014-03-18 10:14:45 -07002058 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002059 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002060 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002061 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2062 self.assertTrue(self.sched._areAllBuildsComplete())
2063
2064 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002065 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002066 self.assertEqual(self.history[0].result, 'SUCCESS')
2067
James E. Blair879dafb2015-07-17 14:04:49 -07002068 def test_file_head(self):
2069 # This is a regression test for an observed bug. A change
2070 # with a file named "HEAD" in the root directory of the repo
2071 # was processed by a merger. It then was unable to reset the
2072 # repo because of:
2073 # GitCommandError: 'git reset --hard HEAD' returned
2074 # with exit code 128
2075 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2076 # and filename
2077 # Use '--' to separate filenames from revisions'
2078
2079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002080 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002081 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2082
2083 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2084 self.waitUntilSettled()
2085
2086 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2087 self.waitUntilSettled()
2088
2089 self.assertIn('Build succeeded', A.messages[0])
2090 self.assertIn('Build succeeded', B.messages[0])
2091
James E. Blair70c71582013-03-06 08:50:50 -08002092 def test_file_jobs(self):
2093 "Test that file jobs run only when appropriate"
2094 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002095 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002096 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002097 A.addApproval('code-review', 2)
2098 B.addApproval('code-review', 2)
2099 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2100 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002101 self.waitUntilSettled()
2102
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002103 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002104 if x.name == 'project-testfile']
2105
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002106 self.assertEqual(len(testfile_jobs), 1)
2107 self.assertEqual(testfile_jobs[0].changes, '1,2')
2108 self.assertEqual(A.data['status'], 'MERGED')
2109 self.assertEqual(A.reported, 2)
2110 self.assertEqual(B.data['status'], 'MERGED')
2111 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002112
Clint Byrum3ee39f32016-11-17 23:45:07 -08002113 def _test_irrelevant_files_jobs(self, should_skip):
2114 "Test that jobs with irrelevant-files filter run only when appropriate"
2115 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002116 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002117
2118 if should_skip:
2119 files = {'ignoreme': 'ignored\n'}
2120 else:
2121 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002122
2123 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002124 'master',
2125 'test irrelevant-files',
2126 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002127 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2128 self.waitUntilSettled()
2129
2130 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002131 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002132
2133 if should_skip:
2134 self.assertEqual([], tested_change_ids)
2135 else:
2136 self.assertIn(change.data['number'], tested_change_ids)
2137
Clint Byrum3ee39f32016-11-17 23:45:07 -08002138 def test_irrelevant_files_match_skips_job(self):
2139 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002140
Clint Byrum3ee39f32016-11-17 23:45:07 -08002141 def test_irrelevant_files_no_match_runs_job(self):
2142 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002143
Clint Byrum85493602016-11-18 11:59:47 -08002144 def test_inherited_jobs_keep_matchers(self):
2145 self.updateConfigLayout('layout-inheritance')
2146 self.sched.reconfigure(self.config)
2147
2148 files = {'ignoreme': 'ignored\n'}
2149
2150 change = self.fake_gerrit.addFakeChange('org/project',
2151 'master',
2152 'test irrelevant-files',
2153 files=files)
2154 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2155 self.waitUntilSettled()
2156
2157 run_jobs = set([build.name for build in self.history])
2158
2159 self.assertEqual(set(['project-test-nomatch-starts-empty',
2160 'project-test-nomatch-starts-full']), run_jobs)
2161
James E. Blairec056492016-07-22 09:45:56 -07002162 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002163 def test_test_config(self):
2164 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002165 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002166 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002167
James E. Blairc8a1e052014-02-25 09:29:26 -08002168 def test_queue_names(self):
2169 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002170 tenant = self.sched.abide.tenants.get('tenant-one')
2171 source = tenant.layout.pipelines['gate'].source
2172 project1 = source.getProject('org/project1')
2173 project2 = source.getProject('org/project2')
2174 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2175 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2176 self.assertEqual(q1.name, 'integrated')
2177 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002178
James E. Blair64ed6f22013-07-10 14:07:23 -07002179 def test_queue_precedence(self):
2180 "Test that queue precedence works"
2181
2182 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002183 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2185 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002186 A.addApproval('code-review', 2)
2187 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002188
2189 self.waitUntilSettled()
2190 self.gearman_server.hold_jobs_in_queue = False
2191 self.gearman_server.release()
2192 self.waitUntilSettled()
2193
James E. Blair8de58bd2013-07-18 16:23:33 -07002194 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002195 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002196 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002197 self.waitUntilSettled()
2198
James E. Blair64ed6f22013-07-10 14:07:23 -07002199 self.log.debug(self.history)
2200 self.assertEqual(self.history[0].pipeline, 'gate')
2201 self.assertEqual(self.history[1].pipeline, 'check')
2202 self.assertEqual(self.history[2].pipeline, 'gate')
2203 self.assertEqual(self.history[3].pipeline, 'gate')
2204 self.assertEqual(self.history[4].pipeline, 'check')
2205 self.assertEqual(self.history[5].pipeline, 'check')
2206
Clark Boylana5edbe42014-06-03 16:39:10 -07002207 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002208 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002209 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002211 A.addApproval('code-review', 2)
2212 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002213 self.waitUntilSettled()
2214
Paul Belanger174a8272017-03-14 13:20:10 -04002215 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002216 self.waitUntilSettled()
2217
James E. Blair1843a552013-07-03 14:19:52 -07002218 port = self.webapp.server.socket.getsockname()[1]
2219
Adam Gandelman77a12c72017-02-03 17:43:43 -08002220 req = urllib.request.Request(
2221 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002222 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002223 headers = f.info()
2224 self.assertIn('Content-Length', headers)
2225 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002226 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2227 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002228 self.assertIn('Access-Control-Allow-Origin', headers)
2229 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002230 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002231 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002232 data = f.read()
2233
Paul Belanger174a8272017-03-14 13:20:10 -04002234 self.executor_server.hold_jobs_in_build = False
2235 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002236 self.waitUntilSettled()
2237
2238 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002239 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002240 for p in data['pipelines']:
2241 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002242 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002243 self.assertEqual(q['window'], 20)
2244 else:
2245 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002246 for head in q['heads']:
2247 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002248 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002249 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002250 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002251 status_jobs.append(job)
2252 self.assertEqual('project-merge', status_jobs[0]['name'])
2253 self.assertEqual('https://server/job/project-merge/0/',
2254 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002255 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002256 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002257 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002258 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002259 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002260 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002261 status_jobs[1]['report_url'])
2262
2263 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002264 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002265 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002266 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002267 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002268
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002269 def test_semaphore_one(self):
2270 "Test semaphores with max=1 (mutex)"
2271 self.updateConfigLayout('layout-semaphore')
James E. Blairaf17a972016-02-03 15:07:18 -08002272 self.sched.reconfigure(self.config)
2273
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002274 self.waitUntilSettled()
2275 tenant = self.sched.abide.tenants.get('openstack')
2276
Paul Belanger174a8272017-03-14 13:20:10 -04002277 self.executor_server.hold_jobs_in_build = True
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002278
James E. Blairaf17a972016-02-03 15:07:18 -08002279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2280 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002281 self.assertFalse('test-semaphore' in
2282 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002283
2284 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2285 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2286 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002287
James E. Blairaf17a972016-02-03 15:07:18 -08002288 self.assertEqual(len(self.builds), 3)
2289 self.assertEqual(self.builds[0].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002290 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002291 self.assertEqual(self.builds[2].name, 'project-test1')
2292
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002293 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002294 self.waitUntilSettled()
2295
2296 self.assertEqual(len(self.builds), 3)
2297 self.assertEqual(self.builds[0].name, 'project-test1')
2298 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002299 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2300 self.assertTrue('test-semaphore' in
2301 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002302
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002303 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002304 self.waitUntilSettled()
2305
2306 self.assertEqual(len(self.builds), 3)
2307 self.assertEqual(self.builds[0].name, 'project-test1')
2308 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002309 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
2310 self.assertTrue('test-semaphore' in
2311 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002312
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002313 self.executor_server.release('semaphore-one-test1')
James E. Blairaf17a972016-02-03 15:07:18 -08002314 self.waitUntilSettled()
2315
2316 self.assertEqual(len(self.builds), 3)
2317 self.assertEqual(self.builds[0].name, 'project-test1')
2318 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002319 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
2320 self.assertTrue('test-semaphore' in
2321 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002322
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002323 self.executor_server.release('semaphore-one-test2')
James E. Blairaf17a972016-02-03 15:07:18 -08002324 self.waitUntilSettled()
2325
2326 self.assertEqual(len(self.builds), 2)
2327 self.assertEqual(self.builds[0].name, 'project-test1')
2328 self.assertEqual(self.builds[1].name, 'project-test1')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002329 self.assertFalse('test-semaphore' in
2330 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002331
Paul Belanger174a8272017-03-14 13:20:10 -04002332 self.executor_server.hold_jobs_in_build = False
2333 self.executor_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002334
2335 self.waitUntilSettled()
2336 self.assertEqual(len(self.builds), 0)
2337
2338 self.assertEqual(A.reported, 1)
2339 self.assertEqual(B.reported, 1)
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002340 self.assertFalse('test-semaphore' in
2341 tenant.semaphore_handler.semaphores)
James E. Blairaf17a972016-02-03 15:07:18 -08002342
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002343 def test_semaphore_two(self):
2344 "Test semaphores with max>1"
2345 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002346 self.sched.reconfigure(self.config)
2347
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002348 self.waitUntilSettled()
2349 tenant = self.sched.abide.tenants.get('openstack')
2350
2351 self.executor_server.hold_jobs_in_build = True
2352 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2353 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2354 self.assertFalse('test-semaphore-two' in
2355 tenant.semaphore_handler.semaphores)
2356
2357 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2358 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2359 self.waitUntilSettled()
2360
2361 self.assertEqual(len(self.builds), 4)
2362 self.assertEqual(self.builds[0].name, 'project-test1')
2363 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
2364 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2365 self.assertEqual(self.builds[3].name, 'project-test1')
2366 self.assertTrue('test-semaphore-two' in
2367 tenant.semaphore_handler.semaphores)
2368 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2369 'test-semaphore-two', [])), 2)
2370
2371 self.executor_server.release('semaphore-two-test1')
2372 self.waitUntilSettled()
2373
2374 self.assertEqual(len(self.builds), 4)
2375 self.assertEqual(self.builds[0].name, 'project-test1')
2376 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
2377 self.assertEqual(self.builds[2].name, 'project-test1')
2378 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
2379 self.assertTrue('test-semaphore-two' in
2380 tenant.semaphore_handler.semaphores)
2381 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2382 'test-semaphore-two', [])), 2)
2383
2384 self.executor_server.release('semaphore-two-test2')
2385 self.waitUntilSettled()
2386
2387 self.assertEqual(len(self.builds), 4)
2388 self.assertEqual(self.builds[0].name, 'project-test1')
2389 self.assertEqual(self.builds[1].name, 'project-test1')
2390 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
2391 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
2392 self.assertTrue('test-semaphore-two' in
2393 tenant.semaphore_handler.semaphores)
2394 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2395 'test-semaphore-two', [])), 2)
2396
2397 self.executor_server.release('semaphore-two-test1')
2398 self.waitUntilSettled()
2399
2400 self.assertEqual(len(self.builds), 3)
2401 self.assertEqual(self.builds[0].name, 'project-test1')
2402 self.assertEqual(self.builds[1].name, 'project-test1')
2403 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
2404 self.assertTrue('test-semaphore-two' in
2405 tenant.semaphore_handler.semaphores)
2406 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
2407 'test-semaphore-two', [])), 1)
2408
2409 self.executor_server.release('semaphore-two-test2')
2410 self.waitUntilSettled()
2411
2412 self.assertEqual(len(self.builds), 2)
2413 self.assertEqual(self.builds[0].name, 'project-test1')
2414 self.assertEqual(self.builds[1].name, 'project-test1')
2415 self.assertFalse('test-semaphore-two' in
2416 tenant.semaphore_handler.semaphores)
2417
2418 self.executor_server.hold_jobs_in_build = False
2419 self.executor_server.release()
2420
2421 self.waitUntilSettled()
2422 self.assertEqual(len(self.builds), 0)
2423
2424 self.assertEqual(A.reported, 1)
2425 self.assertEqual(B.reported, 1)
2426
2427 def test_semaphore_abandon(self):
2428 "Test abandon with job semaphores"
2429 self.updateConfigLayout('layout-semaphore')
2430 self.sched.reconfigure(self.config)
2431
2432 self.waitUntilSettled()
2433 tenant = self.sched.abide.tenants.get('openstack')
2434
Paul Belanger174a8272017-03-14 13:20:10 -04002435 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002436
2437 tenant = self.sched.abide.tenants.get('openstack')
2438 check_pipeline = tenant.layout.pipelines['check']
2439
2440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002441 self.assertFalse('test-semaphore' in
2442 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002443
2444 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2445 self.waitUntilSettled()
2446
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002447 self.assertTrue('test-semaphore' in
2448 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002449
2450 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2451 self.waitUntilSettled()
2452
2453 # The check pipeline should be empty
2454 items = check_pipeline.getAllItems()
2455 self.assertEqual(len(items), 0)
2456
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002457 # The semaphore should be released
2458 self.assertFalse('test-semaphore' in
2459 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002460
Paul Belanger174a8272017-03-14 13:20:10 -04002461 self.executor_server.hold_jobs_in_build = False
2462 self.executor_server.release()
Tobias Henkelfb91a492017-02-15 07:29:43 +01002463 self.waitUntilSettled()
2464
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002465 def test_semaphore_reconfigure(self):
2466 "Test reconfigure with job semaphores"
2467 self.updateConfigLayout('layout-semaphore')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002468 self.sched.reconfigure(self.config)
2469
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002470 self.waitUntilSettled()
2471 tenant = self.sched.abide.tenants.get('openstack')
2472
Paul Belanger174a8272017-03-14 13:20:10 -04002473 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002474
2475 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002476 self.assertFalse('test-semaphore' in
2477 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002478
2479 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2480 self.waitUntilSettled()
2481
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002482 self.assertTrue('test-semaphore' in
2483 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002484
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002485 # reconfigure without layout change
Tobias Henkelfb91a492017-02-15 07:29:43 +01002486 self.sched.reconfigure(self.config)
2487 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002488 tenant = self.sched.abide.tenants.get('openstack')
2489
2490 # semaphore still must be held
2491 self.assertTrue('test-semaphore' in
2492 tenant.semaphore_handler.semaphores)
2493
2494 self.updateConfigLayout('layout-semaphore-reconfiguration')
2495 self.sched.reconfigure(self.config)
2496 self.waitUntilSettled()
2497 tenant = self.sched.abide.tenants.get('openstack')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002498
Paul Belanger174a8272017-03-14 13:20:10 -04002499 self.executor_server.release('project-test1')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002500 self.waitUntilSettled()
2501
2502 # There should be no builds anymore
2503 self.assertEqual(len(self.builds), 0)
2504
Tobias Henkel9a0e1942017-03-20 16:16:02 +01002505 # The semaphore should be released
2506 self.assertFalse('test-semaphore' in
2507 tenant.semaphore_handler.semaphores)
Tobias Henkelfb91a492017-02-15 07:29:43 +01002508
James E. Blaircdccd972013-07-01 12:10:22 -07002509 def test_live_reconfiguration(self):
2510 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002511 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002512 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002513 A.addApproval('code-review', 2)
2514 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002515 self.waitUntilSettled()
2516
2517 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002518 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002519
Paul Belanger174a8272017-03-14 13:20:10 -04002520 self.executor_server.hold_jobs_in_build = False
2521 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002522 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002523 self.assertEqual(self.getJobFromHistory('project-merge').result,
2524 'SUCCESS')
2525 self.assertEqual(self.getJobFromHistory('project-test1').result,
2526 'SUCCESS')
2527 self.assertEqual(self.getJobFromHistory('project-test2').result,
2528 'SUCCESS')
2529 self.assertEqual(A.data['status'], 'MERGED')
2530 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002531
James E. Blairec056492016-07-22 09:45:56 -07002532 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002533 def test_live_reconfiguration_merge_conflict(self):
2534 # A real-world bug: a change in a gate queue has a merge
2535 # conflict and a job is added to its project while it's
2536 # sitting in the queue. The job gets added to the change and
2537 # enqueued and the change gets stuck.
2538 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002539 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002540
2541 # This change is fine. It's here to stop the queue long
2542 # enough for the next change to be subject to the
2543 # reconfiguration, as well as to provide a conflict for the
2544 # next change. This change will succeed and merge.
2545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2546 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002547 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002548
2549 # This change will be in merge conflict. During the
2550 # reconfiguration, we will add a job. We want to make sure
2551 # that doesn't cause it to get stuck.
2552 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2553 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002554 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002555
James E. Blair8b5408c2016-08-08 15:37:46 -07002556 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2557 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002558
2559 self.waitUntilSettled()
2560
2561 # No jobs have run yet
2562 self.assertEqual(A.data['status'], 'NEW')
2563 self.assertEqual(A.reported, 1)
2564 self.assertEqual(B.data['status'], 'NEW')
2565 self.assertEqual(B.reported, 1)
2566 self.assertEqual(len(self.history), 0)
2567
2568 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002569 self.updateConfigLayout(
2570 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002571 self.sched.reconfigure(self.config)
2572 self.waitUntilSettled()
2573
Paul Belanger174a8272017-03-14 13:20:10 -04002574 self.executor_server.hold_jobs_in_build = False
2575 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002576 self.waitUntilSettled()
2577
2578 self.assertEqual(A.data['status'], 'MERGED')
2579 self.assertEqual(A.reported, 2)
2580 self.assertEqual(B.data['status'], 'NEW')
2581 self.assertEqual(B.reported, 2)
2582 self.assertEqual(self.getJobFromHistory('project-merge').result,
2583 'SUCCESS')
2584 self.assertEqual(self.getJobFromHistory('project-test1').result,
2585 'SUCCESS')
2586 self.assertEqual(self.getJobFromHistory('project-test2').result,
2587 'SUCCESS')
2588 self.assertEqual(self.getJobFromHistory('project-test3').result,
2589 'SUCCESS')
2590 self.assertEqual(len(self.history), 4)
2591
James E. Blairec056492016-07-22 09:45:56 -07002592 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002593 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002594 # An extrapolation of test_live_reconfiguration_merge_conflict
2595 # that tests a job added to a job tree with a failed root does
2596 # not run.
2597 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002598 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002599
2600 # This change is fine. It's here to stop the queue long
2601 # enough for the next change to be subject to the
2602 # reconfiguration. This change will succeed and merge.
2603 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2604 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002605 A.addApproval('code-review', 2)
2606 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002607 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002608 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002609 self.waitUntilSettled()
2610
2611 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002612 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002613 B.addApproval('code-review', 2)
2614 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002615 self.waitUntilSettled()
2616
Paul Belanger174a8272017-03-14 13:20:10 -04002617 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002618 self.waitUntilSettled()
2619
2620 # Both -merge jobs have run, but no others.
2621 self.assertEqual(A.data['status'], 'NEW')
2622 self.assertEqual(A.reported, 1)
2623 self.assertEqual(B.data['status'], 'NEW')
2624 self.assertEqual(B.reported, 1)
2625 self.assertEqual(self.history[0].result, 'SUCCESS')
2626 self.assertEqual(self.history[0].name, 'project-merge')
2627 self.assertEqual(self.history[1].result, 'FAILURE')
2628 self.assertEqual(self.history[1].name, 'project-merge')
2629 self.assertEqual(len(self.history), 2)
2630
2631 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002632 self.updateConfigLayout(
2633 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002634 self.sched.reconfigure(self.config)
2635 self.waitUntilSettled()
2636
Paul Belanger174a8272017-03-14 13:20:10 -04002637 self.executor_server.hold_jobs_in_build = False
2638 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002639 self.waitUntilSettled()
2640
2641 self.assertEqual(A.data['status'], 'MERGED')
2642 self.assertEqual(A.reported, 2)
2643 self.assertEqual(B.data['status'], 'NEW')
2644 self.assertEqual(B.reported, 2)
2645 self.assertEqual(self.history[0].result, 'SUCCESS')
2646 self.assertEqual(self.history[0].name, 'project-merge')
2647 self.assertEqual(self.history[1].result, 'FAILURE')
2648 self.assertEqual(self.history[1].name, 'project-merge')
2649 self.assertEqual(self.history[2].result, 'SUCCESS')
2650 self.assertEqual(self.history[3].result, 'SUCCESS')
2651 self.assertEqual(self.history[4].result, 'SUCCESS')
2652 self.assertEqual(len(self.history), 5)
2653
James E. Blairec056492016-07-22 09:45:56 -07002654 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002655 def test_live_reconfiguration_failed_job(self):
2656 # Test that a change with a removed failing job does not
2657 # disrupt reconfiguration. If a change has a failed job and
2658 # that job is removed during a reconfiguration, we observed a
2659 # bug where the code to re-set build statuses would run on
2660 # that build and raise an exception because the job no longer
2661 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002662 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002663
2664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2665
2666 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002667 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002668
2669 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2670 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002671 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002672 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002673 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002674 self.waitUntilSettled()
2675
2676 self.assertEqual(A.data['status'], 'NEW')
2677 self.assertEqual(A.reported, 0)
2678
2679 self.assertEqual(self.getJobFromHistory('project-merge').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory('project-test1').result,
2682 'FAILURE')
2683 self.assertEqual(len(self.history), 2)
2684
2685 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002686 self.updateConfigLayout(
2687 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002688 self.sched.reconfigure(self.config)
2689 self.waitUntilSettled()
2690
Paul Belanger174a8272017-03-14 13:20:10 -04002691 self.executor_server.hold_jobs_in_build = False
2692 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002693 self.waitUntilSettled()
2694
2695 self.assertEqual(self.getJobFromHistory('project-test2').result,
2696 'SUCCESS')
2697 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2698 'SUCCESS')
2699 self.assertEqual(len(self.history), 4)
2700
2701 self.assertEqual(A.data['status'], 'NEW')
2702 self.assertEqual(A.reported, 1)
2703 self.assertIn('Build succeeded', A.messages[0])
2704 # Ensure the removed job was not included in the report.
2705 self.assertNotIn('project-test1', A.messages[0])
2706
James E. Blairec056492016-07-22 09:45:56 -07002707 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002708 def test_live_reconfiguration_shared_queue(self):
2709 # Test that a change with a failing job which was removed from
2710 # this project but otherwise still exists in the system does
2711 # not disrupt reconfiguration.
2712
Paul Belanger174a8272017-03-14 13:20:10 -04002713 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002714
2715 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2716
Paul Belanger174a8272017-03-14 13:20:10 -04002717 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002718
2719 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2720 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002721 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002722 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002723 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002724 self.waitUntilSettled()
2725
2726 self.assertEqual(A.data['status'], 'NEW')
2727 self.assertEqual(A.reported, 0)
2728
2729 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2730 'SUCCESS')
2731 self.assertEqual(self.getJobFromHistory(
2732 'project1-project2-integration').result, 'FAILURE')
2733 self.assertEqual(len(self.history), 2)
2734
2735 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002736 self.updateConfigLayout(
2737 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002738 self.sched.reconfigure(self.config)
2739 self.waitUntilSettled()
2740
Paul Belanger174a8272017-03-14 13:20:10 -04002741 self.executor_server.hold_jobs_in_build = False
2742 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002743 self.waitUntilSettled()
2744
2745 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2746 'SUCCESS')
2747 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2748 'SUCCESS')
2749 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2750 'SUCCESS')
2751 self.assertEqual(self.getJobFromHistory(
2752 'project1-project2-integration').result, 'FAILURE')
2753 self.assertEqual(len(self.history), 4)
2754
2755 self.assertEqual(A.data['status'], 'NEW')
2756 self.assertEqual(A.reported, 1)
2757 self.assertIn('Build succeeded', A.messages[0])
2758 # Ensure the removed job was not included in the report.
2759 self.assertNotIn('project1-project2-integration', A.messages[0])
2760
James E. Blairec056492016-07-22 09:45:56 -07002761 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002762 def test_double_live_reconfiguration_shared_queue(self):
2763 # This was a real-world regression. A change is added to
2764 # gate; a reconfigure happens, a second change which depends
2765 # on the first is added, and a second reconfiguration happens.
2766 # Ensure that both changes merge.
2767
2768 # A failure may indicate incorrect caching or cleaning up of
2769 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002770 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002771
2772 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2773 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2774 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002775 A.addApproval('code-review', 2)
2776 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002777
2778 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002779 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002780 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002781 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002782 self.waitUntilSettled()
2783
2784 # Reconfigure (with only one change in the pipeline).
2785 self.sched.reconfigure(self.config)
2786 self.waitUntilSettled()
2787
2788 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002789 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002790 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002791 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002792 self.waitUntilSettled()
2793
2794 # Reconfigure (with both in the pipeline).
2795 self.sched.reconfigure(self.config)
2796 self.waitUntilSettled()
2797
Paul Belanger174a8272017-03-14 13:20:10 -04002798 self.executor_server.hold_jobs_in_build = False
2799 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002800 self.waitUntilSettled()
2801
2802 self.assertEqual(len(self.history), 8)
2803
2804 self.assertEqual(A.data['status'], 'MERGED')
2805 self.assertEqual(A.reported, 2)
2806 self.assertEqual(B.data['status'], 'MERGED')
2807 self.assertEqual(B.reported, 2)
2808
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002809 def test_live_reconfiguration_del_project(self):
2810 # Test project deletion from layout
2811 # while changes are enqueued
2812
Paul Belanger174a8272017-03-14 13:20:10 -04002813 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002814 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2815 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2816 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2817
2818 # A Depends-On: B
2819 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2820 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002821 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002822
2823 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2824 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2825 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002826 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002827 self.waitUntilSettled()
2828 self.assertEqual(len(self.builds), 5)
2829
2830 # This layout defines only org/project, not org/project1
Clint Byrum0149ada2017-04-04 12:22:01 -07002831 self.commitLayoutUpdate('common-config',
2832 'layout-live-reconfiguration-del-project')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002833 self.sched.reconfigure(self.config)
2834 self.waitUntilSettled()
2835
2836 # Builds for C aborted, builds for A succeed,
2837 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002838 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002839 self.assertEqual(job_c.changes, '3,1')
2840 self.assertEqual(job_c.result, 'ABORTED')
2841
Paul Belanger174a8272017-03-14 13:20:10 -04002842 self.executor_server.hold_jobs_in_build = False
2843 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002844 self.waitUntilSettled()
2845
Clint Byrum0149ada2017-04-04 12:22:01 -07002846 self.assertEqual(
2847 self.getJobFromHistory('project-test1', 'org/project').changes,
2848 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002849
2850 self.assertEqual(A.data['status'], 'NEW')
2851 self.assertEqual(B.data['status'], 'NEW')
2852 self.assertEqual(C.data['status'], 'NEW')
2853 self.assertEqual(A.reported, 1)
2854 self.assertEqual(B.reported, 0)
2855 self.assertEqual(C.reported, 0)
2856
Clint Byrum0149ada2017-04-04 12:22:01 -07002857 tenant = self.sched.abide.tenants.get('tenant-one')
2858 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002859 self.assertIn('Build succeeded', A.messages[0])
2860
James E. Blairec056492016-07-22 09:45:56 -07002861 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002862 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002863 self.updateConfigLayout(
2864 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002865 self.sched.reconfigure(self.config)
2866
2867 self.init_repo("org/new-project")
2868 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2869
James E. Blair8b5408c2016-08-08 15:37:46 -07002870 A.addApproval('code-review', 2)
2871 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002872 self.waitUntilSettled()
2873 self.assertEqual(self.getJobFromHistory('project-merge').result,
2874 'SUCCESS')
2875 self.assertEqual(self.getJobFromHistory('project-test1').result,
2876 'SUCCESS')
2877 self.assertEqual(self.getJobFromHistory('project-test2').result,
2878 'SUCCESS')
2879 self.assertEqual(A.data['status'], 'MERGED')
2880 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002881
Clark Boylan6dbbc482013-10-18 10:57:31 -07002882 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002883 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002884 self.sched.reconfigure(self.config)
2885
2886 self.init_repo("org/delete-project")
2887 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2888
James E. Blair8b5408c2016-08-08 15:37:46 -07002889 A.addApproval('code-review', 2)
2890 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002891 self.waitUntilSettled()
2892 self.assertEqual(self.getJobFromHistory('project-merge').result,
2893 'SUCCESS')
2894 self.assertEqual(self.getJobFromHistory('project-test1').result,
2895 'SUCCESS')
2896 self.assertEqual(self.getJobFromHistory('project-test2').result,
2897 'SUCCESS')
2898 self.assertEqual(A.data['status'], 'MERGED')
2899 self.assertEqual(A.reported, 2)
2900
2901 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002902 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002903 if os.path.exists(os.path.join(self.merger_src_root, p)):
2904 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002905 if os.path.exists(os.path.join(self.executor_src_root, p)):
2906 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002907
2908 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2909
James E. Blair8b5408c2016-08-08 15:37:46 -07002910 B.addApproval('code-review', 2)
2911 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002912 self.waitUntilSettled()
2913 self.assertEqual(self.getJobFromHistory('project-merge').result,
2914 'SUCCESS')
2915 self.assertEqual(self.getJobFromHistory('project-test1').result,
2916 'SUCCESS')
2917 self.assertEqual(self.getJobFromHistory('project-test2').result,
2918 'SUCCESS')
2919 self.assertEqual(B.data['status'], 'MERGED')
2920 self.assertEqual(B.reported, 2)
2921
James E. Blair456f2fb2016-02-09 09:29:33 -08002922 def test_tags(self):
2923 "Test job tags"
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002924 self.updateConfigLayout('layout-tags')
James E. Blair456f2fb2016-02-09 09:29:33 -08002925 self.sched.reconfigure(self.config)
2926
2927 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2928 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2929 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2930 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2931 self.waitUntilSettled()
2932
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002933 self.assertEqual(len(self.history), 8)
2934
2935 results = {self.getJobFromHistory('merge',
2936 project='org/project1').uuid: 'extratag merge',
2937 self.getJobFromHistory('merge',
2938 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002939
2940 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002941 self.assertEqual(results.get(build.uuid, ''),
2942 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002943
James E. Blair63bb0ef2013-07-29 17:14:51 -07002944 def test_timer(self):
2945 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002946 self.executor_server.hold_jobs_in_build = True
Clint Byrum5e729e92017-02-08 06:34:57 -08002947 self.updateConfigLayout('layout-timer')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002948 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002949
Clark Boylan3ee090a2014-04-03 20:55:09 -07002950 # The pipeline triggers every second, so we should have seen
2951 # several by now.
2952 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002953 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002954
2955 self.assertEqual(len(self.builds), 2)
2956
James E. Blair63bb0ef2013-07-29 17:14:51 -07002957 port = self.webapp.server.socket.getsockname()[1]
2958
Clint Byrum5e729e92017-02-08 06:34:57 -08002959 req = urllib.request.Request(
2960 "http://localhost:%s/openstack/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002961 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002962 data = f.read()
2963
Paul Belanger174a8272017-03-14 13:20:10 -04002964 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002965 # Stop queuing timer triggered jobs so that the assertions
2966 # below don't race against more jobs being queued.
Clint Byrum5e729e92017-02-08 06:34:57 -08002967 self.commitLayoutUpdate('layout-timer', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002968 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002969 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002970 self.waitUntilSettled()
2971
2972 self.assertEqual(self.getJobFromHistory(
2973 'project-bitrot-stable-old').result, 'SUCCESS')
2974 self.assertEqual(self.getJobFromHistory(
2975 'project-bitrot-stable-older').result, 'SUCCESS')
2976
2977 data = json.loads(data)
2978 status_jobs = set()
2979 for p in data['pipelines']:
2980 for q in p['change_queues']:
2981 for head in q['heads']:
2982 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002983 for job in change['jobs']:
2984 status_jobs.add(job['name'])
2985 self.assertIn('project-bitrot-stable-old', status_jobs)
2986 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002987
James E. Blair4f6033c2014-03-27 15:49:09 -07002988 def test_idle(self):
2989 "Test that frequent periodic jobs work"
Paul Belanger174a8272017-03-14 13:20:10 -04002990 self.executor_server.hold_jobs_in_build = True
Clint Byrum58264dc2017-02-07 21:21:22 -08002991 self.updateConfigLayout('layout-idle')
James E. Blair4f6033c2014-03-27 15:49:09 -07002992
Clark Boylan3ee090a2014-04-03 20:55:09 -07002993 for x in range(1, 3):
2994 # Test that timer triggers periodic jobs even across
2995 # layout config reloads.
2996 # Start timer trigger
Clark Boylan3ee090a2014-04-03 20:55:09 -07002997 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002998 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002999
Clark Boylan3ee090a2014-04-03 20:55:09 -07003000 # The pipeline triggers every second, so we should have seen
3001 # several by now.
3002 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003003
3004 # Stop queuing timer triggered jobs so that the assertions
3005 # below don't race against more jobs being queued.
Clint Byrum58264dc2017-02-07 21:21:22 -08003006 before = self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003007 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003008 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08003009 self.assertEqual(len(self.builds), 2,
3010 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003011 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003012 self.waitUntilSettled()
3013 self.assertEqual(len(self.builds), 0)
3014 self.assertEqual(len(self.history), x * 2)
Clint Byrum58264dc2017-02-07 21:21:22 -08003015 # Revert back to layout-idle
3016 repo = git.Repo(os.path.join(self.test_root,
3017 'upstream',
3018 'layout-idle'))
3019 repo.git.reset('--hard', before)
James E. Blair4f6033c2014-03-27 15:49:09 -07003020
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003021 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05003022 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003023 self.sched.reconfigure(self.config)
3024
3025 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3026 self.waitUntilSettled()
3027
3028 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3029 self.waitUntilSettled()
3030
James E. Blairff80a2f2013-12-27 13:24:06 -08003031 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003032
3033 # A.messages only holds what FakeGerrit places in it. Thus we
3034 # work on the knowledge of what the first message should be as
3035 # it is only configured to go to SMTP.
3036
3037 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003038 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003039 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003040 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003041 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003042 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003043
3044 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003045 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003046 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003047 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003048 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003049 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003050
James E. Blaire5910202013-12-27 09:50:31 -08003051 def test_timer_smtp(self):
3052 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04003053 self.executor_server.hold_jobs_in_build = True
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003054 self.updateConfigLayout('layout-timer-smtp')
James E. Blaire5910202013-12-27 09:50:31 -08003055 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003056
Clark Boylan3ee090a2014-04-03 20:55:09 -07003057 # The pipeline triggers every second, so we should have seen
3058 # several by now.
3059 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003060 self.waitUntilSettled()
3061
Clark Boylan3ee090a2014-04-03 20:55:09 -07003062 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003063 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003064 self.waitUntilSettled()
3065 self.assertEqual(len(self.history), 2)
3066
James E. Blaire5910202013-12-27 09:50:31 -08003067 self.assertEqual(self.getJobFromHistory(
3068 'project-bitrot-stable-old').result, 'SUCCESS')
3069 self.assertEqual(self.getJobFromHistory(
3070 'project-bitrot-stable-older').result, 'SUCCESS')
3071
James E. Blairff80a2f2013-12-27 13:24:06 -08003072 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003073
3074 # A.messages only holds what FakeGerrit places in it. Thus we
3075 # work on the knowledge of what the first message should be as
3076 # it is only configured to go to SMTP.
3077
3078 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003079 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003080 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003081 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003082 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003083 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003084
Clark Boylan3ee090a2014-04-03 20:55:09 -07003085 # Stop queuing timer triggered jobs and let any that may have
3086 # queued through so that end of test assertions pass.
Clint Byrum7b71b2f2017-02-08 07:00:52 -08003087 self.commitLayoutUpdate('layout-timer-smtp', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003088 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003089 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003090 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003091 self.waitUntilSettled()
3092
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003093 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003094 def test_timer_sshkey(self):
3095 "Test that a periodic job can setup SSH key authentication"
3096 self.worker.hold_jobs_in_build = True
3097 self.config.set('zuul', 'layout_config',
3098 'tests/fixtures/layout-timer.yaml')
3099 self.sched.reconfigure(self.config)
3100 self.registerJobs()
3101
3102 # The pipeline triggers every second, so we should have seen
3103 # several by now.
3104 time.sleep(5)
3105 self.waitUntilSettled()
3106
3107 self.assertEqual(len(self.builds), 2)
3108
3109 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3110 self.assertTrue(os.path.isfile(ssh_wrapper))
3111 with open(ssh_wrapper) as f:
3112 ssh_wrapper_content = f.read()
3113 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3114 # In the unit tests Merger runs in the same process,
3115 # so we see its' environment variables
3116 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3117
3118 self.worker.release('.*')
3119 self.waitUntilSettled()
3120 self.assertEqual(len(self.history), 2)
3121
3122 self.assertEqual(self.getJobFromHistory(
3123 'project-bitrot-stable-old').result, 'SUCCESS')
3124 self.assertEqual(self.getJobFromHistory(
3125 'project-bitrot-stable-older').result, 'SUCCESS')
3126
3127 # Stop queuing timer triggered jobs and let any that may have
3128 # queued through so that end of test assertions pass.
3129 self.config.set('zuul', 'layout_config',
3130 'tests/fixtures/layout-no-timer.yaml')
3131 self.sched.reconfigure(self.config)
3132 self.registerJobs()
3133 self.waitUntilSettled()
3134 self.worker.release('.*')
3135 self.waitUntilSettled()
3136
James E. Blair91e34592015-07-31 16:45:59 -07003137 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003138 "Test that the RPC client can enqueue a change"
3139 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003140 A.addApproval('code-review', 2)
3141 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003142
3143 client = zuul.rpcclient.RPCClient('127.0.0.1',
3144 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003145 r = client.enqueue(tenant='tenant-one',
3146 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003147 project='org/project',
3148 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003149 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003150 self.waitUntilSettled()
3151 self.assertEqual(self.getJobFromHistory('project-merge').result,
3152 'SUCCESS')
3153 self.assertEqual(self.getJobFromHistory('project-test1').result,
3154 'SUCCESS')
3155 self.assertEqual(self.getJobFromHistory('project-test2').result,
3156 'SUCCESS')
3157 self.assertEqual(A.data['status'], 'MERGED')
3158 self.assertEqual(A.reported, 2)
3159 self.assertEqual(r, True)
3160
James E. Blair91e34592015-07-31 16:45:59 -07003161 def test_client_enqueue_ref(self):
3162 "Test that the RPC client can enqueue a ref"
3163
3164 client = zuul.rpcclient.RPCClient('127.0.0.1',
3165 self.gearman_server.port)
3166 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003167 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003168 pipeline='post',
3169 project='org/project',
3170 trigger='gerrit',
3171 ref='master',
3172 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3173 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3174 self.waitUntilSettled()
3175 job_names = [x.name for x in self.history]
3176 self.assertEqual(len(self.history), 1)
3177 self.assertIn('project-post', job_names)
3178 self.assertEqual(r, True)
3179
James E. Blairad28e912013-11-27 10:43:22 -08003180 def test_client_enqueue_negative(self):
3181 "Test that the RPC client returns errors"
3182 client = zuul.rpcclient.RPCClient('127.0.0.1',
3183 self.gearman_server.port)
3184 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003185 "Invalid tenant"):
3186 r = client.enqueue(tenant='tenant-foo',
3187 pipeline='gate',
3188 project='org/project',
3189 trigger='gerrit',
3190 change='1,1')
3191 client.shutdown()
3192 self.assertEqual(r, False)
3193
3194 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003195 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003196 r = client.enqueue(tenant='tenant-one',
3197 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003198 project='project-does-not-exist',
3199 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003200 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003201 client.shutdown()
3202 self.assertEqual(r, False)
3203
3204 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3205 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003206 r = client.enqueue(tenant='tenant-one',
3207 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003208 project='org/project',
3209 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003210 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003211 client.shutdown()
3212 self.assertEqual(r, False)
3213
3214 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3215 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003216 r = client.enqueue(tenant='tenant-one',
3217 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003218 project='org/project',
3219 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003220 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003221 client.shutdown()
3222 self.assertEqual(r, False)
3223
3224 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3225 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003226 r = client.enqueue(tenant='tenant-one',
3227 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003228 project='org/project',
3229 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003230 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003231 client.shutdown()
3232 self.assertEqual(r, False)
3233
3234 self.waitUntilSettled()
3235 self.assertEqual(len(self.history), 0)
3236 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003237
3238 def test_client_promote(self):
3239 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003240 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3242 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3243 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003244 A.addApproval('code-review', 2)
3245 B.addApproval('code-review', 2)
3246 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003247
James E. Blair8b5408c2016-08-08 15:37:46 -07003248 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3249 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3250 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003251
3252 self.waitUntilSettled()
3253
Paul Belangerbaca3132016-11-04 12:49:54 -04003254 tenant = self.sched.abide.tenants.get('tenant-one')
3255 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003256 enqueue_times = {}
3257 for item in items:
3258 enqueue_times[str(item.change)] = item.enqueue_time
3259
James E. Blair36658cf2013-12-06 17:53:48 -08003260 client = zuul.rpcclient.RPCClient('127.0.0.1',
3261 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003262 r = client.promote(tenant='tenant-one',
3263 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003264 change_ids=['2,1', '3,1'])
3265
Sean Daguef39b9ca2014-01-10 21:34:35 -05003266 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003267 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003268 for item in items:
3269 self.assertEqual(
3270 enqueue_times[str(item.change)], item.enqueue_time)
3271
James E. Blair78acec92014-02-06 07:11:32 -08003272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003273 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003274 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003275 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003276 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003277 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003278 self.waitUntilSettled()
3279
3280 self.assertEqual(len(self.builds), 6)
3281 self.assertEqual(self.builds[0].name, 'project-test1')
3282 self.assertEqual(self.builds[1].name, 'project-test2')
3283 self.assertEqual(self.builds[2].name, 'project-test1')
3284 self.assertEqual(self.builds[3].name, 'project-test2')
3285 self.assertEqual(self.builds[4].name, 'project-test1')
3286 self.assertEqual(self.builds[5].name, 'project-test2')
3287
Paul Belangerbaca3132016-11-04 12:49:54 -04003288 self.assertTrue(self.builds[0].hasChanges(B))
3289 self.assertFalse(self.builds[0].hasChanges(A))
3290 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003291
Paul Belangerbaca3132016-11-04 12:49:54 -04003292 self.assertTrue(self.builds[2].hasChanges(B))
3293 self.assertTrue(self.builds[2].hasChanges(C))
3294 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003295
Paul Belangerbaca3132016-11-04 12:49:54 -04003296 self.assertTrue(self.builds[4].hasChanges(B))
3297 self.assertTrue(self.builds[4].hasChanges(C))
3298 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003299
Paul Belanger174a8272017-03-14 13:20:10 -04003300 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003301 self.waitUntilSettled()
3302
3303 self.assertEqual(A.data['status'], 'MERGED')
3304 self.assertEqual(A.reported, 2)
3305 self.assertEqual(B.data['status'], 'MERGED')
3306 self.assertEqual(B.reported, 2)
3307 self.assertEqual(C.data['status'], 'MERGED')
3308 self.assertEqual(C.reported, 2)
3309
3310 client.shutdown()
3311 self.assertEqual(r, True)
3312
3313 def test_client_promote_dependent(self):
3314 "Test that the RPC client can promote a dependent change"
3315 # C (depends on B) -> B -> A ; then promote C to get:
3316 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003317 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003318 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3319 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3320 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3321
3322 C.setDependsOn(B, 1)
3323
James E. Blair8b5408c2016-08-08 15:37:46 -07003324 A.addApproval('code-review', 2)
3325 B.addApproval('code-review', 2)
3326 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003327
James E. Blair8b5408c2016-08-08 15:37:46 -07003328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3329 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3330 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003331
3332 self.waitUntilSettled()
3333
3334 client = zuul.rpcclient.RPCClient('127.0.0.1',
3335 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003336 r = client.promote(tenant='tenant-one',
3337 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003338 change_ids=['3,1'])
3339
James E. Blair78acec92014-02-06 07:11:32 -08003340 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003341 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003342 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003343 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003344 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003345 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003346 self.waitUntilSettled()
3347
3348 self.assertEqual(len(self.builds), 6)
3349 self.assertEqual(self.builds[0].name, 'project-test1')
3350 self.assertEqual(self.builds[1].name, 'project-test2')
3351 self.assertEqual(self.builds[2].name, 'project-test1')
3352 self.assertEqual(self.builds[3].name, 'project-test2')
3353 self.assertEqual(self.builds[4].name, 'project-test1')
3354 self.assertEqual(self.builds[5].name, 'project-test2')
3355
Paul Belangerbaca3132016-11-04 12:49:54 -04003356 self.assertTrue(self.builds[0].hasChanges(B))
3357 self.assertFalse(self.builds[0].hasChanges(A))
3358 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003359
Paul Belangerbaca3132016-11-04 12:49:54 -04003360 self.assertTrue(self.builds[2].hasChanges(B))
3361 self.assertTrue(self.builds[2].hasChanges(C))
3362 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003363
Paul Belangerbaca3132016-11-04 12:49:54 -04003364 self.assertTrue(self.builds[4].hasChanges(B))
3365 self.assertTrue(self.builds[4].hasChanges(C))
3366 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003367
Paul Belanger174a8272017-03-14 13:20:10 -04003368 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003369 self.waitUntilSettled()
3370
3371 self.assertEqual(A.data['status'], 'MERGED')
3372 self.assertEqual(A.reported, 2)
3373 self.assertEqual(B.data['status'], 'MERGED')
3374 self.assertEqual(B.reported, 2)
3375 self.assertEqual(C.data['status'], 'MERGED')
3376 self.assertEqual(C.reported, 2)
3377
3378 client.shutdown()
3379 self.assertEqual(r, True)
3380
3381 def test_client_promote_negative(self):
3382 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003383 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003385 A.addApproval('code-review', 2)
3386 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003387 self.waitUntilSettled()
3388
3389 client = zuul.rpcclient.RPCClient('127.0.0.1',
3390 self.gearman_server.port)
3391
3392 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003393 r = client.promote(tenant='tenant-one',
3394 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003395 change_ids=['2,1', '3,1'])
3396 client.shutdown()
3397 self.assertEqual(r, False)
3398
3399 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003400 r = client.promote(tenant='tenant-one',
3401 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003402 change_ids=['4,1'])
3403 client.shutdown()
3404 self.assertEqual(r, False)
3405
Paul Belanger174a8272017-03-14 13:20:10 -04003406 self.executor_server.hold_jobs_in_build = False
3407 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003408 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003409
3410 def test_queue_rate_limiting(self):
3411 "Test that DependentPipelines are rate limited with dep across window"
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003412 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003413 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003414 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003415 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3416 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3417 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3418
3419 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003420 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003421
James E. Blair8b5408c2016-08-08 15:37:46 -07003422 A.addApproval('code-review', 2)
3423 B.addApproval('code-review', 2)
3424 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003425
James E. Blair8b5408c2016-08-08 15:37:46 -07003426 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3427 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3428 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003429 self.waitUntilSettled()
3430
3431 # Only A and B will have their merge jobs queued because
3432 # window is 2.
3433 self.assertEqual(len(self.builds), 2)
3434 self.assertEqual(self.builds[0].name, 'project-merge')
3435 self.assertEqual(self.builds[1].name, 'project-merge')
3436
Paul Belanger174a8272017-03-14 13:20:10 -04003437 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003438 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003439 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003440 self.waitUntilSettled()
3441
3442 # Only A and B will have their test jobs queued because
3443 # window is 2.
3444 self.assertEqual(len(self.builds), 4)
3445 self.assertEqual(self.builds[0].name, 'project-test1')
3446 self.assertEqual(self.builds[1].name, 'project-test2')
3447 self.assertEqual(self.builds[2].name, 'project-test1')
3448 self.assertEqual(self.builds[3].name, 'project-test2')
3449
Paul Belanger174a8272017-03-14 13:20:10 -04003450 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003451 self.waitUntilSettled()
3452
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003453 tenant = self.sched.abide.tenants.get('openstack')
3454 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003455 # A failed so window is reduced by 1 to 1.
3456 self.assertEqual(queue.window, 1)
3457 self.assertEqual(queue.window_floor, 1)
3458 self.assertEqual(A.data['status'], 'NEW')
3459
3460 # Gate is reset and only B's merge job is queued because
3461 # window shrunk to 1.
3462 self.assertEqual(len(self.builds), 1)
3463 self.assertEqual(self.builds[0].name, 'project-merge')
3464
Paul Belanger174a8272017-03-14 13:20:10 -04003465 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003466 self.waitUntilSettled()
3467
3468 # Only B's test jobs are queued because window is still 1.
3469 self.assertEqual(len(self.builds), 2)
3470 self.assertEqual(self.builds[0].name, 'project-test1')
3471 self.assertEqual(self.builds[1].name, 'project-test2')
3472
Paul Belanger174a8272017-03-14 13:20:10 -04003473 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003474 self.waitUntilSettled()
3475
3476 # B was successfully merged so window is increased to 2.
3477 self.assertEqual(queue.window, 2)
3478 self.assertEqual(queue.window_floor, 1)
3479 self.assertEqual(B.data['status'], 'MERGED')
3480
3481 # Only C is left and its merge job is queued.
3482 self.assertEqual(len(self.builds), 1)
3483 self.assertEqual(self.builds[0].name, 'project-merge')
3484
Paul Belanger174a8272017-03-14 13:20:10 -04003485 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
3488 # After successful merge job the test jobs for C are queued.
3489 self.assertEqual(len(self.builds), 2)
3490 self.assertEqual(self.builds[0].name, 'project-test1')
3491 self.assertEqual(self.builds[1].name, 'project-test2')
3492
Paul Belanger174a8272017-03-14 13:20:10 -04003493 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003494 self.waitUntilSettled()
3495
3496 # C successfully merged so window is bumped to 3.
3497 self.assertEqual(queue.window, 3)
3498 self.assertEqual(queue.window_floor, 1)
3499 self.assertEqual(C.data['status'], 'MERGED')
3500
3501 def test_queue_rate_limiting_dependent(self):
3502 "Test that DependentPipelines are rate limited with dep in window"
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003503 self.updateConfigLayout('layout-rate-limit')
Clark Boylan7603a372014-01-21 11:43:20 -08003504 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003505 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003506 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3507 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3508 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3509
3510 B.setDependsOn(A, 1)
3511
Paul Belanger174a8272017-03-14 13:20:10 -04003512 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003513
James E. Blair8b5408c2016-08-08 15:37:46 -07003514 A.addApproval('code-review', 2)
3515 B.addApproval('code-review', 2)
3516 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003517
James E. Blair8b5408c2016-08-08 15:37:46 -07003518 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3519 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3520 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003521 self.waitUntilSettled()
3522
3523 # Only A and B will have their merge jobs queued because
3524 # window is 2.
3525 self.assertEqual(len(self.builds), 2)
3526 self.assertEqual(self.builds[0].name, 'project-merge')
3527 self.assertEqual(self.builds[1].name, 'project-merge')
3528
Paul Belanger174a8272017-03-14 13:20:10 -04003529 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003530 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003531 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003532 self.waitUntilSettled()
3533
3534 # Only A and B will have their test jobs queued because
3535 # window is 2.
3536 self.assertEqual(len(self.builds), 4)
3537 self.assertEqual(self.builds[0].name, 'project-test1')
3538 self.assertEqual(self.builds[1].name, 'project-test2')
3539 self.assertEqual(self.builds[2].name, 'project-test1')
3540 self.assertEqual(self.builds[3].name, 'project-test2')
3541
Paul Belanger174a8272017-03-14 13:20:10 -04003542 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003543 self.waitUntilSettled()
3544
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003545 tenant = self.sched.abide.tenants.get('openstack')
3546 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003547 # A failed so window is reduced by 1 to 1.
3548 self.assertEqual(queue.window, 1)
3549 self.assertEqual(queue.window_floor, 1)
3550 self.assertEqual(A.data['status'], 'NEW')
3551 self.assertEqual(B.data['status'], 'NEW')
3552
3553 # Gate is reset and only C's merge job is queued because
3554 # window shrunk to 1 and A and B were dequeued.
3555 self.assertEqual(len(self.builds), 1)
3556 self.assertEqual(self.builds[0].name, 'project-merge')
3557
Paul Belanger174a8272017-03-14 13:20:10 -04003558 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003559 self.waitUntilSettled()
3560
3561 # Only C's test jobs are queued because window is still 1.
3562 self.assertEqual(len(self.builds), 2)
3563 self.assertEqual(self.builds[0].name, 'project-test1')
3564 self.assertEqual(self.builds[1].name, 'project-test2')
3565
Paul Belanger174a8272017-03-14 13:20:10 -04003566 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003567 self.waitUntilSettled()
3568
3569 # C was successfully merged so window is increased to 2.
3570 self.assertEqual(queue.window, 2)
3571 self.assertEqual(queue.window_floor, 1)
3572 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003573
James E. Blairec056492016-07-22 09:45:56 -07003574 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003575 def test_worker_update_metadata(self):
3576 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003577 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003578
3579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003580 A.addApproval('code-review', 2)
3581 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003582 self.waitUntilSettled()
3583
Paul Belanger174a8272017-03-14 13:20:10 -04003584 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003585
3586 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003587 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003588
3589 start = time.time()
3590 while True:
3591 if time.time() - start > 10:
3592 raise Exception("Timeout waiting for gearman server to report "
3593 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003594 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003595 if build.worker.name == "My Worker":
3596 break
3597 else:
3598 time.sleep(0)
3599
3600 self.log.debug(build)
3601 self.assertEqual("My Worker", build.worker.name)
3602 self.assertEqual("localhost", build.worker.hostname)
3603 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3604 self.assertEqual("zuul.example.org", build.worker.fqdn)
3605 self.assertEqual("FakeBuilder", build.worker.program)
3606 self.assertEqual("v1.1", build.worker.version)
3607 self.assertEqual({'something': 'else'}, build.worker.extra)
3608
Paul Belanger174a8272017-03-14 13:20:10 -04003609 self.executor_server.hold_jobs_in_build = False
3610 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003611 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003612
3613 def test_footer_message(self):
3614 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003615 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003616 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003617
3618 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003619 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003620 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003621 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003622 self.waitUntilSettled()
3623
3624 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003625 B.addApproval('code-review', 2)
3626 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003627 self.waitUntilSettled()
3628
3629 self.assertEqual(2, len(self.smtp_messages))
3630
Adam Gandelman82258fd2017-02-07 16:51:58 -08003631 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003632Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003633http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003634
Adam Gandelman82258fd2017-02-07 16:51:58 -08003635 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003636For CI problems and help debugging, contact ci@example.org"""
3637
Adam Gandelman82258fd2017-02-07 16:51:58 -08003638 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3639 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3640 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3641 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003642
James E. Blairec056492016-07-22 09:45:56 -07003643 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003644 def test_merge_failure_reporters(self):
3645 """Check that the config is set up correctly"""
3646
James E. Blairf84026c2015-12-08 16:11:46 -08003647 self.updateConfigLayout(
3648 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003649 self.sched.reconfigure(self.config)
3650 self.registerJobs()
3651
3652 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003653 "Merge Failed.\n\nThis change or one of its cross-repo "
3654 "dependencies was unable to be automatically merged with the "
3655 "current state of its repository. Please rebase the change and "
3656 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003657 self.sched.layout.pipelines['check'].merge_failure_message)
3658 self.assertEqual(
3659 "The merge failed! For more information...",
3660 self.sched.layout.pipelines['gate'].merge_failure_message)
3661
3662 self.assertEqual(
3663 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3664 self.assertEqual(
3665 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3666
3667 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003668 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3669 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003670
3671 self.assertTrue(
3672 (
3673 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003674 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003675 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003676 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003677 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003678 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003679 ) or (
3680 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003681 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003682 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003683 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003684 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003685 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003686 )
3687 )
3688
James E. Blairec056492016-07-22 09:45:56 -07003689 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003690 def test_merge_failure_reports(self):
3691 """Check that when a change fails to merge the correct message is sent
3692 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003693 self.updateConfigLayout(
3694 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003695 self.sched.reconfigure(self.config)
3696 self.registerJobs()
3697
3698 # Check a test failure isn't reported to SMTP
3699 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003700 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003701 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003702 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003703 self.waitUntilSettled()
3704
3705 self.assertEqual(3, len(self.history)) # 3 jobs
3706 self.assertEqual(0, len(self.smtp_messages))
3707
3708 # Check a merge failure is reported to SMTP
3709 # B should be merged, but C will conflict with B
3710 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3711 B.addPatchset(['conflict'])
3712 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3713 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003714 B.addApproval('code-review', 2)
3715 C.addApproval('code-review', 2)
3716 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3717 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003718 self.waitUntilSettled()
3719
3720 self.assertEqual(6, len(self.history)) # A and B jobs
3721 self.assertEqual(1, len(self.smtp_messages))
3722 self.assertEqual('The merge failed! For more information...',
3723 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003724
James E. Blairec056492016-07-22 09:45:56 -07003725 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003726 def test_default_merge_failure_reports(self):
3727 """Check that the default merge failure reports are correct."""
3728
3729 # A should report success, B should report merge failure.
3730 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3731 A.addPatchset(['conflict'])
3732 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3733 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003734 A.addApproval('code-review', 2)
3735 B.addApproval('code-review', 2)
3736 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3737 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003738 self.waitUntilSettled()
3739
3740 self.assertEqual(3, len(self.history)) # A jobs
3741 self.assertEqual(A.reported, 2)
3742 self.assertEqual(B.reported, 2)
3743 self.assertEqual(A.data['status'], 'MERGED')
3744 self.assertEqual(B.data['status'], 'NEW')
3745 self.assertIn('Build succeeded', A.messages[1])
3746 self.assertIn('Merge Failed', B.messages[1])
3747 self.assertIn('automatically merged', B.messages[1])
3748 self.assertNotIn('logs.example.com', B.messages[1])
3749 self.assertNotIn('SKIPPED', B.messages[1])
3750
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003751 def test_client_get_running_jobs(self):
3752 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003753 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003754 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003755 A.addApproval('code-review', 2)
3756 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003757 self.waitUntilSettled()
3758
3759 client = zuul.rpcclient.RPCClient('127.0.0.1',
3760 self.gearman_server.port)
3761
3762 # Wait for gearman server to send the initial workData back to zuul
3763 start = time.time()
3764 while True:
3765 if time.time() - start > 10:
3766 raise Exception("Timeout waiting for gearman server to report "
3767 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003768 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003769 if build.worker.name == "My Worker":
3770 break
3771 else:
3772 time.sleep(0)
3773
3774 running_items = client.get_running_jobs()
3775
3776 self.assertEqual(1, len(running_items))
3777 running_item = running_items[0]
3778 self.assertEqual([], running_item['failing_reasons'])
3779 self.assertEqual([], running_item['items_behind'])
3780 self.assertEqual('https://hostname/1', running_item['url'])
3781 self.assertEqual(None, running_item['item_ahead'])
3782 self.assertEqual('org/project', running_item['project'])
3783 self.assertEqual(None, running_item['remaining_time'])
3784 self.assertEqual(True, running_item['active'])
3785 self.assertEqual('1,1', running_item['id'])
3786
3787 self.assertEqual(3, len(running_item['jobs']))
3788 for job in running_item['jobs']:
3789 if job['name'] == 'project-merge':
3790 self.assertEqual('project-merge', job['name'])
3791 self.assertEqual('gate', job['pipeline'])
3792 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003793 self.assertEqual('https://server/job/project-merge/0/',
3794 job['url'])
3795 self.assertEqual(7, len(job['worker']))
3796 self.assertEqual(False, job['canceled'])
3797 self.assertEqual(True, job['voting'])
3798 self.assertEqual(None, job['result'])
3799 self.assertEqual('gate', job['pipeline'])
3800 break
3801
Paul Belanger174a8272017-03-14 13:20:10 -04003802 self.executor_server.hold_jobs_in_build = False
3803 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003804 self.waitUntilSettled()
3805
3806 running_items = client.get_running_jobs()
3807 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003808
3809 def test_nonvoting_pipeline(self):
3810 "Test that a nonvoting pipeline (experimental) can still report"
3811
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003812 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3813 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003814 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3815 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003816
3817 self.assertEqual(self.getJobFromHistory('project-merge').result,
3818 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003819 self.assertEqual(
3820 self.getJobFromHistory('experimental-project-test').result,
3821 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003822 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003823
3824 def test_crd_gate(self):
3825 "Test cross-repo dependencies"
3826 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3827 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003828 A.addApproval('code-review', 2)
3829 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003830
3831 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3832 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3833 AM2.setMerged()
3834 AM1.setMerged()
3835
3836 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3837 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3838 BM2.setMerged()
3839 BM1.setMerged()
3840
3841 # A -> AM1 -> AM2
3842 # B -> BM1 -> BM2
3843 # A Depends-On: B
3844 # M2 is here to make sure it is never queried. If it is, it
3845 # means zuul is walking down the entire history of merged
3846 # changes.
3847
3848 B.setDependsOn(BM1, 1)
3849 BM1.setDependsOn(BM2, 1)
3850
3851 A.setDependsOn(AM1, 1)
3852 AM1.setDependsOn(AM2, 1)
3853
3854 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3855 A.subject, B.data['id'])
3856
James E. Blair8b5408c2016-08-08 15:37:46 -07003857 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003858 self.waitUntilSettled()
3859
3860 self.assertEqual(A.data['status'], 'NEW')
3861 self.assertEqual(B.data['status'], 'NEW')
3862
Paul Belanger5dccbe72016-11-14 11:17:37 -05003863 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003864 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003865
Paul Belanger174a8272017-03-14 13:20:10 -04003866 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003867 B.addApproval('approved', 1)
3868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003869 self.waitUntilSettled()
3870
Paul Belanger174a8272017-03-14 13:20:10 -04003871 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003872 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003873 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003874 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003875 self.executor_server.hold_jobs_in_build = False
3876 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003877 self.waitUntilSettled()
3878
3879 self.assertEqual(AM2.queried, 0)
3880 self.assertEqual(BM2.queried, 0)
3881 self.assertEqual(A.data['status'], 'MERGED')
3882 self.assertEqual(B.data['status'], 'MERGED')
3883 self.assertEqual(A.reported, 2)
3884 self.assertEqual(B.reported, 2)
3885
Paul Belanger5dccbe72016-11-14 11:17:37 -05003886 changes = self.getJobFromHistory(
3887 'project-merge', 'org/project1').changes
3888 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003889
3890 def test_crd_branch(self):
3891 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003892
3893 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003894 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3895 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3896 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3897 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003898 A.addApproval('code-review', 2)
3899 B.addApproval('code-review', 2)
3900 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003901
3902 # A Depends-On: B+C
3903 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3904 A.subject, B.data['id'])
3905
Paul Belanger174a8272017-03-14 13:20:10 -04003906 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003907 B.addApproval('approved', 1)
3908 C.addApproval('approved', 1)
3909 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003910 self.waitUntilSettled()
3911
Paul Belanger174a8272017-03-14 13:20:10 -04003912 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003913 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003914 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003915 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003916 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003917 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003918 self.executor_server.hold_jobs_in_build = False
3919 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003920 self.waitUntilSettled()
3921
3922 self.assertEqual(A.data['status'], 'MERGED')
3923 self.assertEqual(B.data['status'], 'MERGED')
3924 self.assertEqual(C.data['status'], 'MERGED')
3925 self.assertEqual(A.reported, 2)
3926 self.assertEqual(B.reported, 2)
3927 self.assertEqual(C.reported, 2)
3928
Paul Belanger6379db12016-11-14 13:57:54 -05003929 changes = self.getJobFromHistory(
3930 'project-merge', 'org/project1').changes
3931 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003932
3933 def test_crd_multiline(self):
3934 "Test multiple depends-on lines in commit"
3935 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3936 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3937 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003938 A.addApproval('code-review', 2)
3939 B.addApproval('code-review', 2)
3940 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003941
3942 # A Depends-On: B+C
3943 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3944 A.subject, B.data['id'], C.data['id'])
3945
Paul Belanger174a8272017-03-14 13:20:10 -04003946 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003947 B.addApproval('approved', 1)
3948 C.addApproval('approved', 1)
3949 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003950 self.waitUntilSettled()
3951
Paul Belanger174a8272017-03-14 13:20:10 -04003952 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003953 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003954 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003955 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003956 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003957 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003958 self.executor_server.hold_jobs_in_build = False
3959 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003960 self.waitUntilSettled()
3961
3962 self.assertEqual(A.data['status'], 'MERGED')
3963 self.assertEqual(B.data['status'], 'MERGED')
3964 self.assertEqual(C.data['status'], 'MERGED')
3965 self.assertEqual(A.reported, 2)
3966 self.assertEqual(B.reported, 2)
3967 self.assertEqual(C.reported, 2)
3968
Paul Belanger5dccbe72016-11-14 11:17:37 -05003969 changes = self.getJobFromHistory(
3970 'project-merge', 'org/project1').changes
3971 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003972
3973 def test_crd_unshared_gate(self):
3974 "Test cross-repo dependencies in unshared gate queues"
3975 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3976 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003977 A.addApproval('code-review', 2)
3978 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003979
3980 # A Depends-On: B
3981 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3982 A.subject, B.data['id'])
3983
3984 # A and B do not share a queue, make sure that A is unable to
3985 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003986 B.addApproval('approved', 1)
3987 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003988 self.waitUntilSettled()
3989
3990 self.assertEqual(A.data['status'], 'NEW')
3991 self.assertEqual(B.data['status'], 'NEW')
3992 self.assertEqual(A.reported, 0)
3993 self.assertEqual(B.reported, 0)
3994 self.assertEqual(len(self.history), 0)
3995
3996 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003997 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003998 self.waitUntilSettled()
3999
4000 self.assertEqual(B.data['status'], 'MERGED')
4001 self.assertEqual(B.reported, 2)
4002
4003 # Now that B is merged, A should be able to be enqueued and
4004 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004005 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004006 self.waitUntilSettled()
4007
4008 self.assertEqual(A.data['status'], 'MERGED')
4009 self.assertEqual(A.reported, 2)
4010
James E. Blair96698e22015-04-02 07:48:21 -07004011 def test_crd_gate_reverse(self):
4012 "Test reverse cross-repo dependencies"
4013 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4014 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004015 A.addApproval('code-review', 2)
4016 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004017
4018 # A Depends-On: B
4019
4020 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4021 A.subject, B.data['id'])
4022
James E. Blair8b5408c2016-08-08 15:37:46 -07004023 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004024 self.waitUntilSettled()
4025
4026 self.assertEqual(A.data['status'], 'NEW')
4027 self.assertEqual(B.data['status'], 'NEW')
4028
Paul Belanger174a8272017-03-14 13:20:10 -04004029 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07004030 A.addApproval('approved', 1)
4031 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004032 self.waitUntilSettled()
4033
Paul Belanger174a8272017-03-14 13:20:10 -04004034 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004035 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004036 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004037 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004038 self.executor_server.hold_jobs_in_build = False
4039 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004040 self.waitUntilSettled()
4041
4042 self.assertEqual(A.data['status'], 'MERGED')
4043 self.assertEqual(B.data['status'], 'MERGED')
4044 self.assertEqual(A.reported, 2)
4045 self.assertEqual(B.reported, 2)
4046
Paul Belanger5dccbe72016-11-14 11:17:37 -05004047 changes = self.getJobFromHistory(
4048 'project-merge', 'org/project1').changes
4049 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004050
James E. Blair5ee24252014-12-30 10:12:29 -08004051 def test_crd_cycle(self):
4052 "Test cross-repo dependency cycles"
4053 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4054 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004055 A.addApproval('code-review', 2)
4056 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004057
4058 # A -> B -> A (via commit-depends)
4059
4060 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4061 A.subject, B.data['id'])
4062 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4063 B.subject, A.data['id'])
4064
James E. Blair8b5408c2016-08-08 15:37:46 -07004065 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004066 self.waitUntilSettled()
4067
4068 self.assertEqual(A.reported, 0)
4069 self.assertEqual(B.reported, 0)
4070 self.assertEqual(A.data['status'], 'NEW')
4071 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004072
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004073 def test_crd_gate_unknown(self):
4074 "Test unknown projects in dependent pipeline"
4075 self.init_repo("org/unknown")
4076 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4077 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07004078 A.addApproval('code-review', 2)
4079 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004080
4081 # A Depends-On: B
4082 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4083 A.subject, B.data['id'])
4084
James E. Blair8b5408c2016-08-08 15:37:46 -07004085 B.addApproval('approved', 1)
4086 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004087 self.waitUntilSettled()
4088
4089 # Unknown projects cannot share a queue with any other
4090 # since they don't have common jobs with any other (they have no jobs).
4091 # Changes which depend on unknown project changes
4092 # should not be processed in dependent pipeline
4093 self.assertEqual(A.data['status'], 'NEW')
4094 self.assertEqual(B.data['status'], 'NEW')
4095 self.assertEqual(A.reported, 0)
4096 self.assertEqual(B.reported, 0)
4097 self.assertEqual(len(self.history), 0)
4098
4099 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07004100 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004101 B.setMerged()
4102 self.waitUntilSettled()
4103 self.assertEqual(len(self.history), 0)
4104
4105 # Now that B is merged, A should be able to be enqueued and
4106 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07004107 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004108 self.waitUntilSettled()
4109
4110 self.assertEqual(A.data['status'], 'MERGED')
4111 self.assertEqual(A.reported, 2)
4112 self.assertEqual(B.data['status'], 'MERGED')
4113 self.assertEqual(B.reported, 0)
4114
James E. Blairbfb8e042014-12-30 17:01:44 -08004115 def test_crd_check(self):
4116 "Test cross-repo dependencies in independent pipelines"
4117
Paul Belanger174a8272017-03-14 13:20:10 -04004118 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004119 self.gearman_server.hold_jobs_in_queue = True
4120 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4121 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4122
4123 # A Depends-On: B
4124 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4125 A.subject, B.data['id'])
4126
4127 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4128 self.waitUntilSettled()
4129
4130 queue = self.gearman_server.getQueue()
4131 ref = self.getParameter(queue[-1], 'ZUUL_REF')
4132 self.gearman_server.hold_jobs_in_queue = False
4133 self.gearman_server.release()
4134 self.waitUntilSettled()
4135
Paul Belanger174a8272017-03-14 13:20:10 -04004136 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004137 self.waitUntilSettled()
4138
Monty Taylord642d852017-02-23 14:05:42 -05004139 path = os.path.join(self.builds[0].jobdir.src_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08004140 repo = git.Repo(path)
4141 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4142 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004143 correct_messages = [
4144 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004145 self.assertEqual(repo_messages, correct_messages)
4146
Monty Taylord642d852017-02-23 14:05:42 -05004147 path = os.path.join(self.builds[0].jobdir.src_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08004148 repo = git.Repo(path)
4149 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4150 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05004151 correct_messages = [
4152 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08004153 self.assertEqual(repo_messages, correct_messages)
4154
Paul Belanger174a8272017-03-14 13:20:10 -04004155 self.executor_server.hold_jobs_in_build = False
4156 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004157 self.waitUntilSettled()
4158
James E. Blairbfb8e042014-12-30 17:01:44 -08004159 self.assertEqual(A.data['status'], 'NEW')
4160 self.assertEqual(B.data['status'], 'NEW')
4161 self.assertEqual(A.reported, 1)
4162 self.assertEqual(B.reported, 0)
4163
4164 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004165 tenant = self.sched.abide.tenants.get('tenant-one')
4166 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004167
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004168 def test_crd_check_git_depends(self):
4169 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004170 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004171 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4172 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4173
4174 # Add two git-dependent changes and make sure they both report
4175 # success.
4176 B.setDependsOn(A, 1)
4177 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4178 self.waitUntilSettled()
4179 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4180 self.waitUntilSettled()
4181
James E. Blairb8c16472015-05-05 14:55:26 -07004182 self.orderedRelease()
4183 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004184 self.waitUntilSettled()
4185
4186 self.assertEqual(A.data['status'], 'NEW')
4187 self.assertEqual(B.data['status'], 'NEW')
4188 self.assertEqual(A.reported, 1)
4189 self.assertEqual(B.reported, 1)
4190
4191 self.assertEqual(self.history[0].changes, '1,1')
4192 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004193 tenant = self.sched.abide.tenants.get('tenant-one')
4194 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004195
4196 self.assertIn('Build succeeded', A.messages[0])
4197 self.assertIn('Build succeeded', B.messages[0])
4198
4199 def test_crd_check_duplicate(self):
4200 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004201 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004202 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4203 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004204 tenant = self.sched.abide.tenants.get('tenant-one')
4205 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004206
4207 # Add two git-dependent changes...
4208 B.setDependsOn(A, 1)
4209 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4210 self.waitUntilSettled()
4211 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4212
4213 # ...make sure the live one is not duplicated...
4214 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4215 self.waitUntilSettled()
4216 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4217
4218 # ...but the non-live one is able to be.
4219 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4220 self.waitUntilSettled()
4221 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4222
Clark Boylandd849822015-03-02 12:38:14 -08004223 # Release jobs in order to avoid races with change A jobs
4224 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004225 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004226 self.executor_server.hold_jobs_in_build = False
4227 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004228 self.waitUntilSettled()
4229
4230 self.assertEqual(A.data['status'], 'NEW')
4231 self.assertEqual(B.data['status'], 'NEW')
4232 self.assertEqual(A.reported, 1)
4233 self.assertEqual(B.reported, 1)
4234
4235 self.assertEqual(self.history[0].changes, '1,1 2,1')
4236 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004237 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004238
4239 self.assertIn('Build succeeded', A.messages[0])
4240 self.assertIn('Build succeeded', B.messages[0])
4241
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004242 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004243 "Test cross-repo dependencies re-enqueued in independent pipelines"
4244
4245 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004246 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4247 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004248
4249 # A Depends-On: B
4250 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4251 A.subject, B.data['id'])
4252
4253 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4254 self.waitUntilSettled()
4255
4256 self.sched.reconfigure(self.config)
4257
4258 # Make sure the items still share a change queue, and the
4259 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004260 tenant = self.sched.abide.tenants.get('tenant-one')
4261 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4262 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004263 first_item = queue.queue[0]
4264 for item in queue.queue:
4265 self.assertEqual(item.queue, first_item.queue)
4266 self.assertFalse(first_item.live)
4267 self.assertTrue(queue.queue[1].live)
4268
4269 self.gearman_server.hold_jobs_in_queue = False
4270 self.gearman_server.release()
4271 self.waitUntilSettled()
4272
4273 self.assertEqual(A.data['status'], 'NEW')
4274 self.assertEqual(B.data['status'], 'NEW')
4275 self.assertEqual(A.reported, 1)
4276 self.assertEqual(B.reported, 0)
4277
4278 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004279 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004280
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004281 def test_crd_check_reconfiguration(self):
4282 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4283
4284 def test_crd_undefined_project(self):
4285 """Test that undefined projects in dependencies are handled for
4286 independent pipelines"""
4287 # It's a hack for fake gerrit,
4288 # as it implies repo creation upon the creation of any change
4289 self.init_repo("org/unknown")
4290 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4291
James E. Blair17dd6772015-02-09 14:45:18 -08004292 def test_crd_check_ignore_dependencies(self):
4293 "Test cross-repo dependencies can be ignored"
Clint Byrum12282ca2017-04-03 11:04:02 -07004294 self.updateConfigLayout('layout-ignore-dependencies')
James E. Blair17dd6772015-02-09 14:45:18 -08004295 self.sched.reconfigure(self.config)
James E. Blair17dd6772015-02-09 14:45:18 -08004296
4297 self.gearman_server.hold_jobs_in_queue = True
4298 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4299 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4300 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4301
4302 # A Depends-On: B
4303 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4304 A.subject, B.data['id'])
4305 # C git-depends on B
4306 C.setDependsOn(B, 1)
4307 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4308 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4309 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4310 self.waitUntilSettled()
4311
4312 # Make sure none of the items share a change queue, and all
4313 # are live.
Clint Byrum12282ca2017-04-03 11:04:02 -07004314 tenant = self.sched.abide.tenants.get('openstack')
4315 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004316 self.assertEqual(len(check_pipeline.queues), 3)
4317 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4318 for item in check_pipeline.getAllItems():
4319 self.assertTrue(item.live)
4320
4321 self.gearman_server.hold_jobs_in_queue = False
4322 self.gearman_server.release()
4323 self.waitUntilSettled()
4324
4325 self.assertEqual(A.data['status'], 'NEW')
4326 self.assertEqual(B.data['status'], 'NEW')
4327 self.assertEqual(C.data['status'], 'NEW')
4328 self.assertEqual(A.reported, 1)
4329 self.assertEqual(B.reported, 1)
4330 self.assertEqual(C.reported, 1)
4331
4332 # Each job should have tested exactly one change
4333 for job in self.history:
4334 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004335
4336 def test_crd_check_transitive(self):
4337 "Test transitive cross-repo dependencies"
4338 # Specifically, if A -> B -> C, and C gets a new patchset and
4339 # A gets a new patchset, ensure the test of A,2 includes B,1
4340 # and C,2 (not C,1 which would indicate stale data in the
4341 # cache for B).
4342 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4343 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4344 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4345
4346 # A Depends-On: B
4347 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4348 A.subject, B.data['id'])
4349
4350 # B Depends-On: C
4351 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4352 B.subject, C.data['id'])
4353
4354 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4355 self.waitUntilSettled()
4356 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4357
4358 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4359 self.waitUntilSettled()
4360 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4361
4362 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4363 self.waitUntilSettled()
4364 self.assertEqual(self.history[-1].changes, '3,1')
4365
4366 C.addPatchset()
4367 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4368 self.waitUntilSettled()
4369 self.assertEqual(self.history[-1].changes, '3,2')
4370
4371 A.addPatchset()
4372 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4373 self.waitUntilSettled()
4374 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004375
James E. Blaira8b90b32016-08-24 15:18:50 -07004376 def test_crd_check_unknown(self):
4377 "Test unknown projects in independent pipeline"
4378 self.init_repo("org/unknown")
4379 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4380 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4381 # A Depends-On: B
4382 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4383 A.subject, B.data['id'])
4384
4385 # Make sure zuul has seen an event on B.
4386 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4387 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4388 self.waitUntilSettled()
4389
4390 self.assertEqual(A.data['status'], 'NEW')
4391 self.assertEqual(A.reported, 1)
4392 self.assertEqual(B.data['status'], 'NEW')
4393 self.assertEqual(B.reported, 0)
4394
James E. Blair92464a22016-04-05 10:21:26 -07004395 def test_crd_cycle_join(self):
4396 "Test an updated change creates a cycle"
4397 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4398
4399 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4400 self.waitUntilSettled()
4401
4402 # Create B->A
4403 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4404 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4405 B.subject, A.data['id'])
4406 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4407 self.waitUntilSettled()
4408
4409 # Update A to add A->B (a cycle).
4410 A.addPatchset()
4411 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4412 A.subject, B.data['id'])
4413 # Normally we would submit the patchset-created event for
4414 # processing here, however, we have no way of noting whether
4415 # the dependency cycle detection correctly raised an
4416 # exception, so instead, we reach into the source driver and
4417 # call the method that would ultimately be called by the event
4418 # processing.
4419
Paul Belanger0e155e22016-11-14 14:12:23 -05004420 tenant = self.sched.abide.tenants.get('tenant-one')
4421 source = tenant.layout.pipelines['gate'].source
4422
4423 # TODO(pabelanger): As we add more source / trigger APIs we should make
4424 # it easier for users to create events for testing.
4425 event = zuul.model.TriggerEvent()
4426 event.trigger_name = 'gerrit'
4427 event.change_number = '1'
4428 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004429 with testtools.ExpectedException(
4430 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004431 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004432 self.log.debug("Got expected dependency cycle exception")
4433
4434 # Now if we update B to remove the depends-on, everything
4435 # should be okay. B; A->B
4436
4437 B.addPatchset()
4438 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004439
4440 source.getChange(event, True)
4441 event.change_number = '2'
4442 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004443
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004444 def test_disable_at(self):
4445 "Test a pipeline will only report to the disabled trigger when failing"
4446
Paul Belanger7dc76e82016-11-11 16:51:08 -05004447 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004448 self.sched.reconfigure(self.config)
4449
Paul Belanger7dc76e82016-11-11 16:51:08 -05004450 tenant = self.sched.abide.tenants.get('openstack')
4451 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004452 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004453 0, tenant.layout.pipelines['check']._consecutive_failures)
4454 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004455
4456 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4457 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4458 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4459 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4460 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4461 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4462 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4463 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4464 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4465 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4466 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4467
Paul Belanger174a8272017-03-14 13:20:10 -04004468 self.executor_server.failJob('project-test1', A)
4469 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004470 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004471 self.executor_server.failJob('project-test1', D)
4472 self.executor_server.failJob('project-test1', E)
4473 self.executor_server.failJob('project-test1', F)
4474 self.executor_server.failJob('project-test1', G)
4475 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004476 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004477 self.executor_server.failJob('project-test1', J)
4478 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004479
4480 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4481 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4482 self.waitUntilSettled()
4483
4484 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004485 2, tenant.layout.pipelines['check']._consecutive_failures)
4486 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004487
4488 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4489 self.waitUntilSettled()
4490
4491 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004492 0, tenant.layout.pipelines['check']._consecutive_failures)
4493 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004494
4495 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4496 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4497 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4498 self.waitUntilSettled()
4499
4500 # We should be disabled now
4501 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004502 3, tenant.layout.pipelines['check']._consecutive_failures)
4503 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004504
4505 # We need to wait between each of these patches to make sure the
4506 # smtp messages come back in an expected order
4507 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4508 self.waitUntilSettled()
4509 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4510 self.waitUntilSettled()
4511 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4512 self.waitUntilSettled()
4513
4514 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4515 # leaving a message on each change
4516 self.assertEqual(1, len(A.messages))
4517 self.assertIn('Build failed.', A.messages[0])
4518 self.assertEqual(1, len(B.messages))
4519 self.assertIn('Build failed.', B.messages[0])
4520 self.assertEqual(1, len(C.messages))
4521 self.assertIn('Build succeeded.', C.messages[0])
4522 self.assertEqual(1, len(D.messages))
4523 self.assertIn('Build failed.', D.messages[0])
4524 self.assertEqual(1, len(E.messages))
4525 self.assertIn('Build failed.', E.messages[0])
4526 self.assertEqual(1, len(F.messages))
4527 self.assertIn('Build failed.', F.messages[0])
4528
4529 # The last 3 (GHI) would have only reported via smtp.
4530 self.assertEqual(3, len(self.smtp_messages))
4531 self.assertEqual(0, len(G.messages))
4532 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004533 self.assertIn(
4534 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004535 self.assertEqual(0, len(H.messages))
4536 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004537 self.assertIn(
4538 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004539 self.assertEqual(0, len(I.messages))
4540 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004541 self.assertIn(
4542 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004543
4544 # Now reload the configuration (simulate a HUP) to check the pipeline
4545 # comes out of disabled
4546 self.sched.reconfigure(self.config)
4547
Paul Belanger7dc76e82016-11-11 16:51:08 -05004548 tenant = self.sched.abide.tenants.get('openstack')
4549
4550 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004551 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004552 0, tenant.layout.pipelines['check']._consecutive_failures)
4553 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004554
4555 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4556 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4557 self.waitUntilSettled()
4558
4559 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004560 2, tenant.layout.pipelines['check']._consecutive_failures)
4561 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004562
4563 # J and K went back to gerrit
4564 self.assertEqual(1, len(J.messages))
4565 self.assertIn('Build failed.', J.messages[0])
4566 self.assertEqual(1, len(K.messages))
4567 self.assertIn('Build failed.', K.messages[0])
4568 # No more messages reported via smtp
4569 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004570
Paul Belanger71d98172016-11-08 10:56:31 -05004571 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004572 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004573
Paul Belanger174a8272017-03-14 13:20:10 -04004574 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004575 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4576 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4577 self.waitUntilSettled()
4578
Paul Belanger174a8272017-03-14 13:20:10 -04004579 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004580 self.waitUntilSettled()
4581
4582 self.assertEqual(len(self.builds), 2)
4583 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004584 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004585 self.waitUntilSettled()
4586
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004587 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004588 self.assertEqual(len(self.builds), 1,
4589 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004590 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004591 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004592 self.waitUntilSettled()
4593
Paul Belanger174a8272017-03-14 13:20:10 -04004594 self.executor_server.hold_jobs_in_build = False
4595 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004596 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004597 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004598 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4599 self.assertEqual(A.reported, 1)
4600 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004601
James E. Blair15be0e12017-01-03 13:45:20 -08004602 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004603 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004604
4605 self.fake_nodepool.paused = True
4606 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4607 A.addApproval('code-review', 2)
4608 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4609 self.waitUntilSettled()
4610
4611 self.zk.client.stop()
4612 self.zk.client.start()
4613 self.fake_nodepool.paused = False
4614 self.waitUntilSettled()
4615
4616 self.assertEqual(A.data['status'], 'MERGED')
4617 self.assertEqual(A.reported, 2)
4618
James E. Blair6ab79e02017-01-06 10:10:17 -08004619 def test_nodepool_failure(self):
4620 "Test that jobs are reported after a nodepool failure"
4621
4622 self.fake_nodepool.paused = True
4623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4624 A.addApproval('code-review', 2)
4625 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4626 self.waitUntilSettled()
4627
4628 req = self.fake_nodepool.getNodeRequests()[0]
4629 self.fake_nodepool.addFailRequest(req)
4630
4631 self.fake_nodepool.paused = False
4632 self.waitUntilSettled()
4633
4634 self.assertEqual(A.data['status'], 'NEW')
4635 self.assertEqual(A.reported, 2)
4636 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4637 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4638 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4639
Arieb6f068c2016-10-09 13:11:06 +03004640
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004641class TestDependencyGraph(ZuulTestCase):
4642 tenant_config_file = 'config/dependency-graph/main.yaml'
4643
4644 def test_dependeny_graph_dispatch_jobs_once(self):
4645 "Test a job in a dependency graph is queued only once"
4646 # Job dependencies, starting with A
4647 # A
4648 # / \
4649 # B C
4650 # / \ / \
4651 # D F E
4652 # |
4653 # G
4654
4655 self.executor_server.hold_jobs_in_build = True
4656 change = self.fake_gerrit.addFakeChange(
4657 'org/project', 'master', 'change')
4658 change.addApproval('code-review', 2)
4659 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4660
4661 self.waitUntilSettled()
4662 self.assertEqual([b.name for b in self.builds], ['A'])
4663
4664 self.executor_server.release('A')
4665 self.waitUntilSettled()
4666 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4667
4668 self.executor_server.release('B')
4669 self.waitUntilSettled()
4670 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4671
4672 self.executor_server.release('D')
4673 self.waitUntilSettled()
4674 self.assertEqual([b.name for b in self.builds], ['C'])
4675
4676 self.executor_server.release('C')
4677 self.waitUntilSettled()
4678 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4679
4680 self.executor_server.release('F')
4681 self.waitUntilSettled()
4682 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4683
4684 self.executor_server.release('G')
4685 self.waitUntilSettled()
4686 self.assertEqual([b.name for b in self.builds], ['E'])
4687
4688 self.executor_server.release('E')
4689 self.waitUntilSettled()
4690 self.assertEqual(len(self.builds), 0)
4691
4692 self.executor_server.hold_jobs_in_build = False
4693 self.executor_server.release()
4694 self.waitUntilSettled()
4695
4696 self.assertEqual(len(self.builds), 0)
4697 self.assertEqual(len(self.history), 7)
4698
4699 self.assertEqual(change.data['status'], 'MERGED')
4700 self.assertEqual(change.reported, 2)
4701
4702 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4703 "Test that a job waits till all dependencies are successful"
4704 # Job dependencies, starting with A
4705 # A
4706 # / \
4707 # B C*
4708 # / \ / \
4709 # D F E
4710 # |
4711 # G
4712
4713 self.executor_server.hold_jobs_in_build = True
4714 change = self.fake_gerrit.addFakeChange(
4715 'org/project', 'master', 'change')
4716 change.addApproval('code-review', 2)
4717
4718 self.executor_server.failJob('C', change)
4719
4720 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4721
4722 self.waitUntilSettled()
4723 self.assertEqual([b.name for b in self.builds], ['A'])
4724
4725 self.executor_server.release('A')
4726 self.waitUntilSettled()
4727 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4728
4729 self.executor_server.release('B')
4730 self.waitUntilSettled()
4731 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4732
4733 self.executor_server.release('D')
4734 self.waitUntilSettled()
4735 self.assertEqual([b.name for b in self.builds], ['C'])
4736
4737 self.executor_server.release('C')
4738 self.waitUntilSettled()
4739 self.assertEqual(len(self.builds), 0)
4740
4741 self.executor_server.hold_jobs_in_build = False
4742 self.executor_server.release()
4743 self.waitUntilSettled()
4744
4745 self.assertEqual(len(self.builds), 0)
4746 self.assertEqual(len(self.history), 4)
4747
4748 self.assertEqual(change.data['status'], 'NEW')
4749 self.assertEqual(change.reported, 2)
4750
4751
Arieb6f068c2016-10-09 13:11:06 +03004752class TestDuplicatePipeline(ZuulTestCase):
4753 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4754
4755 def test_duplicate_pipelines(self):
4756 "Test that a change matching multiple pipelines works"
4757
4758 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4759 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4760 self.waitUntilSettled()
4761
4762 self.assertHistory([
4763 dict(name='project-test1', result='SUCCESS', changes='1,1',
4764 pipeline='dup1'),
4765 dict(name='project-test1', result='SUCCESS', changes='1,1',
4766 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004767 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004768
4769 self.assertEqual(len(A.messages), 2)
4770
Paul Belangera46a3742016-11-09 14:23:03 -05004771 if 'dup1' in A.messages[0]:
4772 self.assertIn('dup1', A.messages[0])
4773 self.assertNotIn('dup2', A.messages[0])
4774 self.assertIn('project-test1', A.messages[0])
4775 self.assertIn('dup2', A.messages[1])
4776 self.assertNotIn('dup1', A.messages[1])
4777 self.assertIn('project-test1', A.messages[1])
4778 else:
4779 self.assertIn('dup1', A.messages[1])
4780 self.assertNotIn('dup2', A.messages[1])
4781 self.assertIn('project-test1', A.messages[1])
4782 self.assertIn('dup2', A.messages[0])
4783 self.assertNotIn('dup1', A.messages[0])
4784 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004785
4786
4787class TestSchedulerOneJobProject(ZuulTestCase):
4788 tenant_config_file = 'config/one-job-project/main.yaml'
4789
4790 def test_one_job_project(self):
4791 "Test that queueing works with one job"
4792 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4793 'master', 'A')
4794 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4795 'master', 'B')
4796 A.addApproval('code-review', 2)
4797 B.addApproval('code-review', 2)
4798 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4799 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4800 self.waitUntilSettled()
4801
4802 self.assertEqual(A.data['status'], 'MERGED')
4803 self.assertEqual(A.reported, 2)
4804 self.assertEqual(B.data['status'], 'MERGED')
4805 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004806
4807
4808class TestSchedulerTemplatedProject(ZuulTestCase):
4809 tenant_config_file = 'config/templated-project/main.yaml'
4810
Paul Belanger174a8272017-03-14 13:20:10 -04004811 def test_job_from_templates_executed(self):
4812 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004813
4814 A = self.fake_gerrit.addFakeChange(
4815 'org/templated-project', 'master', 'A')
4816 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4817 self.waitUntilSettled()
4818
4819 self.assertEqual(self.getJobFromHistory('project-test1').result,
4820 'SUCCESS')
4821 self.assertEqual(self.getJobFromHistory('project-test2').result,
4822 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004823
4824 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004825 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004826
4827 A = self.fake_gerrit.addFakeChange(
4828 'org/layered-project', 'master', 'A')
4829 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4830 self.waitUntilSettled()
4831
4832 self.assertEqual(self.getJobFromHistory('project-test1').result,
4833 'SUCCESS')
4834 self.assertEqual(self.getJobFromHistory('project-test2').result,
4835 'SUCCESS')
4836 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4837 ).result, 'SUCCESS')
4838 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4839 ).result, 'SUCCESS')
4840 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4841 ).result, 'SUCCESS')
4842 self.assertEqual(self.getJobFromHistory('project-test6').result,
4843 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004844
4845
4846class TestSchedulerSuccessURL(ZuulTestCase):
4847 tenant_config_file = 'config/success-url/main.yaml'
4848
4849 def test_success_url(self):
4850 "Ensure bad build params are ignored"
4851 self.sched.reconfigure(self.config)
4852 self.init_repo('org/docs')
4853
4854 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4855 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4856 self.waitUntilSettled()
4857
4858 # Both builds ran: docs-draft-test + docs-draft-test2
4859 self.assertEqual(len(self.history), 2)
4860
4861 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004862 for build in self.history:
4863 if build.name == 'docs-draft-test':
4864 uuid = build.uuid[:7]
4865 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004866
4867 # Two msgs: 'Starting...' + results
4868 self.assertEqual(len(self.smtp_messages), 2)
4869 body = self.smtp_messages[1]['body'].splitlines()
4870 self.assertEqual('Build succeeded.', body[0])
4871
4872 self.assertIn(
4873 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4874 'docs-draft-test/%s/publish-docs/' % uuid,
4875 body[2])
4876
Paul Belanger174a8272017-03-14 13:20:10 -04004877 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004878 self.assertIn(
4879 '- docs-draft-test2 https://server/job',
4880 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004881
4882
Adam Gandelman4da00f62016-12-09 15:47:33 -08004883class TestSchedulerMerges(ZuulTestCase):
4884 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004885
4886 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004887 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004888 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004889 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004890 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4891 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4892 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4893 A.addApproval('code-review', 2)
4894 B.addApproval('code-review', 2)
4895 C.addApproval('code-review', 2)
4896 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4897 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4898 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4899 self.waitUntilSettled()
4900
4901 build = self.builds[-1]
4902 ref = self.getParameter(build, 'ZUUL_REF')
4903
Monty Taylord642d852017-02-23 14:05:42 -05004904 path = os.path.join(build.jobdir.src_root, project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004905 repo = git.Repo(path)
4906 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4907 repo_messages.reverse()
4908
Paul Belanger174a8272017-03-14 13:20:10 -04004909 self.executor_server.hold_jobs_in_build = False
4910 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004911 self.waitUntilSettled()
4912
4913 return repo_messages
4914
4915 def _test_merge(self, mode):
4916 us_path = os.path.join(
4917 self.upstream_root, 'org/project-%s' % mode)
4918 expected_messages = [
4919 'initial commit',
4920 'add content from fixture',
4921 # the intermediate commits order is nondeterministic
4922 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4923 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4924 ]
4925 result = self._test_project_merge_mode(mode)
4926 self.assertEqual(result[:2], expected_messages[:2])
4927 self.assertEqual(result[-2:], expected_messages[-2:])
4928
4929 def test_project_merge_mode_merge(self):
4930 self._test_merge('merge')
4931
4932 def test_project_merge_mode_merge_resolve(self):
4933 self._test_merge('merge-resolve')
4934
4935 def test_project_merge_mode_cherrypick(self):
4936 expected_messages = [
4937 'initial commit',
4938 'add content from fixture',
4939 'A-1',
4940 'B-1',
4941 'C-1']
4942 result = self._test_project_merge_mode('cherry-pick')
4943 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004944
4945 def test_merge_branch(self):
4946 "Test that the right commits are on alternate branches"
4947 self.create_branch('org/project-merge-branches', 'mp')
4948
Paul Belanger174a8272017-03-14 13:20:10 -04004949 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004950 A = self.fake_gerrit.addFakeChange(
4951 'org/project-merge-branches', 'mp', 'A')
4952 B = self.fake_gerrit.addFakeChange(
4953 'org/project-merge-branches', 'mp', 'B')
4954 C = self.fake_gerrit.addFakeChange(
4955 'org/project-merge-branches', 'mp', 'C')
4956 A.addApproval('code-review', 2)
4957 B.addApproval('code-review', 2)
4958 C.addApproval('code-review', 2)
4959 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4960 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4961 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4962 self.waitUntilSettled()
4963
Paul Belanger174a8272017-03-14 13:20:10 -04004964 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004965 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004966 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004967 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004968 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004969 self.waitUntilSettled()
4970
4971 build = self.builds[-1]
4972 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4973 ref = self.getParameter(build, 'ZUUL_REF')
4974 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004975 build.jobdir.src_root, 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004976 repo = git.Repo(path)
4977
4978 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4979 repo_messages.reverse()
4980 correct_messages = [
4981 'initial commit',
4982 'add content from fixture',
4983 'mp commit',
4984 'A-1', 'B-1', 'C-1']
4985 self.assertEqual(repo_messages, correct_messages)
4986
Paul Belanger174a8272017-03-14 13:20:10 -04004987 self.executor_server.hold_jobs_in_build = False
4988 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004989 self.waitUntilSettled()
4990
4991 def test_merge_multi_branch(self):
4992 "Test that dependent changes on multiple branches are merged"
4993 self.create_branch('org/project-merge-branches', 'mp')
4994
Paul Belanger174a8272017-03-14 13:20:10 -04004995 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004996 A = self.fake_gerrit.addFakeChange(
4997 'org/project-merge-branches', 'master', 'A')
4998 B = self.fake_gerrit.addFakeChange(
4999 'org/project-merge-branches', 'mp', 'B')
5000 C = self.fake_gerrit.addFakeChange(
5001 'org/project-merge-branches', 'master', 'C')
5002 A.addApproval('code-review', 2)
5003 B.addApproval('code-review', 2)
5004 C.addApproval('code-review', 2)
5005 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5006 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5007 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5008 self.waitUntilSettled()
5009
5010 job_A = None
5011 for job in self.builds:
5012 if 'project-merge' in job.name:
5013 job_A = job
5014 ref_A = self.getParameter(job_A, 'ZUUL_REF')
5015 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
5016 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
5017 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
5018
5019 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005020 job_A.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005021 repo = git.Repo(path)
5022 repo_messages = [c.message.strip()
5023 for c in repo.iter_commits(ref_A)]
5024 repo_messages.reverse()
5025 correct_messages = [
5026 'initial commit', 'add content from fixture', 'A-1']
5027 self.assertEqual(repo_messages, correct_messages)
5028
Paul Belanger174a8272017-03-14 13:20:10 -04005029 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005030 self.waitUntilSettled()
5031
5032 job_B = None
5033 for job in self.builds:
5034 if 'project-merge' in job.name:
5035 job_B = job
5036 ref_B = self.getParameter(job_B, 'ZUUL_REF')
5037 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
5038 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
5039 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
5040
5041 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005042 job_B.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005043 repo = git.Repo(path)
5044 repo_messages = [c.message.strip()
5045 for c in repo.iter_commits(ref_B)]
5046 repo_messages.reverse()
5047 correct_messages = [
5048 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5049 self.assertEqual(repo_messages, correct_messages)
5050
Paul Belanger174a8272017-03-14 13:20:10 -04005051 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005052 self.waitUntilSettled()
5053
5054 job_C = None
5055 for job in self.builds:
5056 if 'project-merge' in job.name:
5057 job_C = job
5058 ref_C = self.getParameter(job_C, 'ZUUL_REF')
5059 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
5060 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
5061 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
5062 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05005063 job_C.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08005064 repo = git.Repo(path)
5065 repo_messages = [c.message.strip()
5066 for c in repo.iter_commits(ref_C)]
5067
5068 repo_messages.reverse()
5069 correct_messages = [
5070 'initial commit', 'add content from fixture',
5071 'A-1', 'C-1']
5072 # Ensure the right commits are in the history for this ref
5073 self.assertEqual(repo_messages, correct_messages)
5074
Paul Belanger174a8272017-03-14 13:20:10 -04005075 self.executor_server.hold_jobs_in_build = False
5076 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005077 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005078
5079
5080class TestSemaphoreMultiTenant(ZuulTestCase):
5081 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5082
5083 def test_semaphore_tenant_isolation(self):
5084 "Test semaphores in multiple tenants"
5085
5086 self.waitUntilSettled()
5087 tenant_one = self.sched.abide.tenants.get('tenant-one')
5088 tenant_two = self.sched.abide.tenants.get('tenant-two')
5089
5090 self.executor_server.hold_jobs_in_build = True
5091 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5092 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5093 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5094 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5095 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5096 self.assertFalse('test-semaphore' in
5097 tenant_one.semaphore_handler.semaphores)
5098 self.assertFalse('test-semaphore' in
5099 tenant_two.semaphore_handler.semaphores)
5100
5101 # add patches to project1 of tenant-one
5102 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5103 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5104 self.waitUntilSettled()
5105
5106 # one build of project1-test1 must run
5107 # semaphore of tenant-one must be acquired once
5108 # semaphore of tenant-two must not be acquired
5109 self.assertEqual(len(self.builds), 1)
5110 self.assertEqual(self.builds[0].name, 'project1-test1')
5111 self.assertTrue('test-semaphore' in
5112 tenant_one.semaphore_handler.semaphores)
5113 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5114 'test-semaphore', [])), 1)
5115 self.assertFalse('test-semaphore' in
5116 tenant_two.semaphore_handler.semaphores)
5117
5118 # add patches to project2 of tenant-two
5119 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5120 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5121 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5122 self.waitUntilSettled()
5123
5124 # one build of project1-test1 must run
5125 # two builds of project2-test1 must run
5126 # semaphore of tenant-one must be acquired once
5127 # semaphore of tenant-two must be acquired twice
5128 self.assertEqual(len(self.builds), 3)
5129 self.assertEqual(self.builds[0].name, 'project1-test1')
5130 self.assertEqual(self.builds[1].name, 'project2-test1')
5131 self.assertEqual(self.builds[2].name, 'project2-test1')
5132 self.assertTrue('test-semaphore' in
5133 tenant_one.semaphore_handler.semaphores)
5134 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5135 'test-semaphore', [])), 1)
5136 self.assertTrue('test-semaphore' in
5137 tenant_two.semaphore_handler.semaphores)
5138 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5139 'test-semaphore', [])), 2)
5140
5141 self.executor_server.release('project1-test1')
5142 self.waitUntilSettled()
5143
5144 # one build of project1-test1 must run
5145 # two builds of project2-test1 must run
5146 # semaphore of tenant-one must be acquired once
5147 # semaphore of tenant-two must be acquired twice
5148 self.assertEqual(len(self.builds), 3)
5149 self.assertEqual(self.builds[0].name, 'project2-test1')
5150 self.assertEqual(self.builds[1].name, 'project2-test1')
5151 self.assertEqual(self.builds[2].name, 'project1-test1')
5152 self.assertTrue('test-semaphore' in
5153 tenant_one.semaphore_handler.semaphores)
5154 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5155 'test-semaphore', [])), 1)
5156 self.assertTrue('test-semaphore' in
5157 tenant_two.semaphore_handler.semaphores)
5158 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5159 'test-semaphore', [])), 2)
5160
5161 self.executor_server.release('project2-test1')
5162 self.waitUntilSettled()
5163
5164 # one build of project1-test1 must run
5165 # one build of project2-test1 must run
5166 # semaphore of tenant-one must be acquired once
5167 # semaphore of tenant-two must be acquired once
5168 self.assertEqual(len(self.builds), 2)
5169 self.assertTrue('test-semaphore' in
5170 tenant_one.semaphore_handler.semaphores)
5171 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5172 'test-semaphore', [])), 1)
5173 self.assertTrue('test-semaphore' in
5174 tenant_two.semaphore_handler.semaphores)
5175 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5176 'test-semaphore', [])), 1)
5177
5178 self.executor_server.hold_jobs_in_build = False
5179 self.executor_server.release()
5180
5181 self.waitUntilSettled()
5182
5183 # no build must run
5184 # semaphore of tenant-one must not be acquired
5185 # semaphore of tenant-two must not be acquired
5186 self.assertEqual(len(self.builds), 0)
5187 self.assertFalse('test-semaphore' in
5188 tenant_one.semaphore_handler.semaphores)
5189 self.assertFalse('test-semaphore' in
5190 tenant_two.semaphore_handler.semaphores)
5191
5192 self.assertEqual(A.reported, 1)
5193 self.assertEqual(B.reported, 1)
5194
5195
5196class TestSemaphoreInRepo(ZuulTestCase):
5197 tenant_config_file = 'config/in-repo/main.yaml'
5198
5199 def test_semaphore_in_repo(self):
5200 "Test semaphores in repo config"
5201
5202 # This tests dynamic semaphore handling in project repos. The semaphore
5203 # max value should not be evaluated dynamically but must be updated
5204 # after the change lands.
5205
5206 self.waitUntilSettled()
5207 tenant = self.sched.abide.tenants.get('tenant-one')
5208
5209 in_repo_conf = textwrap.dedent(
5210 """
5211 - job:
5212 name: project-test2
5213 semaphore: test-semaphore
5214
5215 - project:
5216 name: org/project
5217 tenant-one-gate:
5218 jobs:
5219 - project-test2
5220
5221 # the max value in dynamic layout must be ignored
5222 - semaphore:
5223 name: test-semaphore
5224 max: 2
5225 """)
5226
5227 in_repo_playbook = textwrap.dedent(
5228 """
5229 - hosts: all
5230 tasks: []
5231 """)
5232
5233 file_dict = {'.zuul.yaml': in_repo_conf,
5234 'playbooks/project-test2.yaml': in_repo_playbook}
5235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5236 files=file_dict)
5237 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5238 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5239 B.setDependsOn(A, 1)
5240 C.setDependsOn(A, 1)
5241
5242 self.executor_server.hold_jobs_in_build = True
5243
5244 A.addApproval('code-review', 2)
5245 B.addApproval('code-review', 2)
5246 C.addApproval('code-review', 2)
5247 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5248 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5249 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5250 self.waitUntilSettled()
5251
5252 # check that the layout in a queue item still has max value of 1
5253 # for test-semaphore
5254 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5255 queue = None
5256 for queue_candidate in pipeline.queues:
5257 if queue_candidate.name == 'org/project':
5258 queue = queue_candidate
5259 break
5260 queue_item = queue.queue[0]
5261 item_dynamic_layout = queue_item.current_build_set.layout
5262 dynamic_test_semaphore = \
5263 item_dynamic_layout.semaphores.get('test-semaphore')
5264 self.assertEqual(dynamic_test_semaphore.max, 1)
5265
5266 # one build must be in queue, one semaphores acquired
5267 self.assertEqual(len(self.builds), 1)
5268 self.assertEqual(self.builds[0].name, 'project-test2')
5269 self.assertTrue('test-semaphore' in
5270 tenant.semaphore_handler.semaphores)
5271 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5272 'test-semaphore', [])), 1)
5273
5274 self.executor_server.release('project-test2')
5275 self.waitUntilSettled()
5276
5277 # change A must be merged
5278 self.assertEqual(A.data['status'], 'MERGED')
5279 self.assertEqual(A.reported, 2)
5280
5281 # send change-merged event as the gerrit mock doesn't send it
5282 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5283 self.waitUntilSettled()
5284
5285 # now that change A was merged, the new semaphore max must be effective
5286 tenant = self.sched.abide.tenants.get('tenant-one')
5287 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5288
5289 # two builds must be in queue, two semaphores acquired
5290 self.assertEqual(len(self.builds), 2)
5291 self.assertEqual(self.builds[0].name, 'project-test2')
5292 self.assertEqual(self.builds[1].name, 'project-test2')
5293 self.assertTrue('test-semaphore' in
5294 tenant.semaphore_handler.semaphores)
5295 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5296 'test-semaphore', [])), 2)
5297
5298 self.executor_server.release('project-test2')
5299 self.waitUntilSettled()
5300
5301 self.assertEqual(len(self.builds), 0)
5302 self.assertFalse('test-semaphore' in
5303 tenant.semaphore_handler.semaphores)
5304
5305 self.executor_server.hold_jobs_in_build = False
5306 self.executor_server.release()
5307
5308 self.waitUntilSettled()
5309 self.assertEqual(len(self.builds), 0)
5310
5311 self.assertEqual(A.reported, 2)
5312 self.assertEqual(B.reported, 2)
5313 self.assertEqual(C.reported, 2)