blob: 9b1344946f62f63c3439be21b11fee548fd85226 [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
Monty Taylorbc758832013-06-17 17:22:42 -040018import os
James E. Blairb0fcae42012-07-17 11:12:10 -070019import re
James E. Blair4886cc12012-07-18 15:39:41 -070020import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040021import time
James E. Blairec056492016-07-22 09:45:56 -070022from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040023
James E. Blair4886cc12012-07-18 15:39:41 -070024import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070025from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040026import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070027
Maru Newby3fe5f852015-01-13 04:22:14 +000028import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070029import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080030import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040031import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070032
Maru Newby3fe5f852015-01-13 04:22:14 +000033from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000034 ZuulTestCase,
35 repack_repo,
36)
James E. Blairb0fcae42012-07-17 11:12:10 -070037
James E. Blairb0fcae42012-07-17 11:12:10 -070038
Clark Boylanb640e052014-04-03 16:41:46 -070039class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070040 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010041
James E. Blairb0fcae42012-07-17 11:12:10 -070042 def test_jobs_launched(self):
43 "Test that jobs are launched and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070044
James E. Blairb0fcae42012-07-17 11:12:10 -070045 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070046 A.addApproval('code-review', 2)
47 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070048 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040049 self.assertEqual(self.getJobFromHistory('project-merge').result,
50 'SUCCESS')
51 self.assertEqual(self.getJobFromHistory('project-test1').result,
52 'SUCCESS')
53 self.assertEqual(self.getJobFromHistory('project-test2').result,
54 'SUCCESS')
55 self.assertEqual(A.data['status'], 'MERGED')
56 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070057 self.assertEqual(self.getJobFromHistory('project-test1').node,
58 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080059 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070060
James E. Blair552b54f2016-07-22 13:55:32 -070061 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070062 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
63 self.assertReportedStat('zuul.pipeline.gate.current_changes',
64 value='1|g')
65 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
66 kind='ms')
67 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
68 value='1|c')
69 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
70 self.assertReportedStat('zuul.pipeline.gate.total_changes',
71 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070072 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070073 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
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.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070076
James E. Blair5821bd92015-09-16 08:48:15 -070077 for build in self.builds:
78 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
79
James E. Blair3cb10702013-08-24 08:56:03 -070080 def test_initial_pipeline_gauges(self):
81 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070082 self.assertReportedStat('zuul.pipeline.gate.current_changes',
83 value='0|g')
84 self.assertReportedStat('zuul.pipeline.check.current_changes',
85 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070086
James E. Blair34776ee2016-08-25 13:53:54 -070087 def test_job_branch(self):
88 "Test the correct variant of a job runs on a branch"
89 self.create_branch('org/project', 'stable')
90 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
91 A.addApproval('code-review', 2)
92 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
93 self.waitUntilSettled()
94 self.assertEqual(self.getJobFromHistory('project-test1').result,
95 'SUCCESS')
96 self.assertEqual(self.getJobFromHistory('project-test2').result,
97 'SUCCESS')
98 self.assertEqual(A.data['status'], 'MERGED')
99 self.assertEqual(A.reported, 2,
100 "A should report start and success")
101 self.assertIn('gate', A.messages[1],
102 "A should transit gate")
103 self.assertEqual(self.getJobFromHistory('project-test1').node,
104 'image2')
105
James E. Blairb0fcae42012-07-17 11:12:10 -0700106 def test_parallel_changes(self):
107 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700108
James E. Blair791b5392016-08-03 11:25:56 -0700109 self.launch_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
111 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
112 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700113 A.addApproval('code-review', 2)
114 B.addApproval('code-review', 2)
115 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700116
James E. Blair8b5408c2016-08-08 15:37:46 -0700117 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
118 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
119 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700120
121 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400122 self.assertEqual(len(self.builds), 1)
123 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700124 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700125
James E. Blair791b5392016-08-03 11:25:56 -0700126 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700127 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400128 self.assertEqual(len(self.builds), 3)
129 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700130 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400131 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700132 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400133 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700134 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700135
James E. Blair791b5392016-08-03 11:25:56 -0700136 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700137 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400138 self.assertEqual(len(self.builds), 5)
139 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700140 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400141 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700142 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700143
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400144 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700145 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400146 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700147 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700148
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400149 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700150 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700151
James E. Blair791b5392016-08-03 11:25:56 -0700152 self.launch_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700153 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400154 self.assertEqual(len(self.builds), 6)
155 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700156 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400157 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700158 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700159
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400160 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700161 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400162 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700164
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400165 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700166 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400167 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700168 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700169
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.launch_server.hold_jobs_in_build = False
171 self.launch_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400173 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700174
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400175 self.assertEqual(len(self.history), 9)
176 self.assertEqual(A.data['status'], 'MERGED')
177 self.assertEqual(B.data['status'], 'MERGED')
178 self.assertEqual(C.data['status'], 'MERGED')
179 self.assertEqual(A.reported, 2)
180 self.assertEqual(B.reported, 2)
181 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700182
183 def test_failed_changes(self):
184 "Test that a change behind a failed change is retested"
James E. Blair08d19992016-08-10 15:25:31 -0700185 self.launch_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700186
James E. Blairb02a3bb2012-07-30 17:49:55 -0700187 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
188 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700189 A.addApproval('code-review', 2)
190 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700191
James E. Blair08d19992016-08-10 15:25:31 -0700192 self.launch_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700193
James E. Blair8b5408c2016-08-08 15:37:46 -0700194 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
195 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700196 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700197 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400198
James E. Blair08d19992016-08-10 15:25:31 -0700199 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400200 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700201 # A/project-merge is complete
202 self.assertBuilds([
203 dict(name='project-test1', changes='1,1'),
204 dict(name='project-test2', changes='1,1'),
205 dict(name='project-merge', changes='1,1 2,1'),
206 ])
James E. Blaire2819012013-06-28 17:17:26 -0400207
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700208 self.launch_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400209 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700210 # A/project-merge is complete
211 # B/project-merge is complete
212 self.assertBuilds([
213 dict(name='project-test1', changes='1,1'),
214 dict(name='project-test2', changes='1,1'),
215 dict(name='project-test1', changes='1,1 2,1'),
216 dict(name='project-test2', changes='1,1 2,1'),
217 ])
218
219 # Release project-test1 for A which will fail. This will
220 # abort both running B jobs and relaunch project-merge for B.
221 self.builds[0].release()
222 self.waitUntilSettled()
223
224 self.orderedRelease()
225 self.assertHistory([
226 dict(name='project-merge', result='SUCCESS', changes='1,1'),
227 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
228 dict(name='project-test1', result='FAILURE', changes='1,1'),
229 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
230 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
231 dict(name='project-test2', result='SUCCESS', changes='1,1'),
232 dict(name='project-merge', result='SUCCESS', changes='2,1'),
233 dict(name='project-test1', result='SUCCESS', changes='2,1'),
234 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700235 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700236
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400237 self.assertEqual(A.data['status'], 'NEW')
238 self.assertEqual(B.data['status'], 'MERGED')
239 self.assertEqual(A.reported, 2)
240 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700241
242 def test_independent_queues(self):
243 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700244
James E. Blair08d19992016-08-10 15:25:31 -0700245 self.launch_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900246 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700247 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
248 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700249 A.addApproval('code-review', 2)
250 B.addApproval('code-review', 2)
251 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700252
James E. Blair8b5408c2016-08-08 15:37:46 -0700253 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
254 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
255 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700256
James E. Blairb02a3bb2012-07-30 17:49:55 -0700257 self.waitUntilSettled()
258
259 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700260 self.assertBuilds([
261 dict(name='project-merge', changes='1,1'),
262 dict(name='project-merge', changes='2,1'),
263 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700264
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700265 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700266 self.builds[0].release()
267 self.waitUntilSettled()
268 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700269 self.waitUntilSettled()
270 # Release the merge job for project2 which is behind project1
James E. Blair08d19992016-08-10 15:25:31 -0700271 self.launch_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700272 self.waitUntilSettled()
273
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700274 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700275 self.assertBuilds([
276 dict(name='project-test1', changes='1,1'),
277 dict(name='project-test2', changes='1,1'),
278 dict(name='project-test1', changes='2,1'),
279 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500280 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700281 dict(name='project-test1', changes='2,1 3,1'),
282 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800283 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700284 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700285
James E. Blair5f0d49e2016-08-19 09:36:50 -0700286 self.orderedRelease()
287 self.assertHistory([
288 dict(name='project-merge', result='SUCCESS', changes='1,1'),
289 dict(name='project-merge', result='SUCCESS', changes='2,1'),
290 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
291 dict(name='project-test1', result='SUCCESS', changes='1,1'),
292 dict(name='project-test2', result='SUCCESS', changes='1,1'),
293 dict(name='project-test1', result='SUCCESS', changes='2,1'),
294 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500295 dict(
296 name='project1-project2-integration',
297 result='SUCCESS',
298 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700299 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
300 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800301 dict(name='project1-project2-integration',
302 result='SUCCESS',
303 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700304 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700305
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400306 self.assertEqual(A.data['status'], 'MERGED')
307 self.assertEqual(B.data['status'], 'MERGED')
308 self.assertEqual(C.data['status'], 'MERGED')
309 self.assertEqual(A.reported, 2)
310 self.assertEqual(B.reported, 2)
311 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700312
313 def test_failed_change_at_head(self):
314 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700315
James E. Blair08d19992016-08-10 15:25:31 -0700316 self.launch_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
318 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
319 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700320 A.addApproval('code-review', 2)
321 B.addApproval('code-review', 2)
322 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700323
James E. Blair08d19992016-08-10 15:25:31 -0700324 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700325
James E. Blair8b5408c2016-08-08 15:37:46 -0700326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
327 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
328 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700329
330 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700331
James E. Blairb536ecc2016-08-31 10:11:42 -0700332 self.assertBuilds([
333 dict(name='project-merge', changes='1,1'),
334 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700335
James E. Blair08d19992016-08-10 15:25:31 -0700336 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700337 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700338 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700339 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700340 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700341 self.waitUntilSettled()
342
James E. Blairb536ecc2016-08-31 10:11:42 -0700343 self.assertBuilds([
344 dict(name='project-test1', changes='1,1'),
345 dict(name='project-test2', changes='1,1'),
346 dict(name='project-test1', changes='1,1 2,1'),
347 dict(name='project-test2', changes='1,1 2,1'),
348 dict(name='project-test1', changes='1,1 2,1 3,1'),
349 dict(name='project-test2', changes='1,1 2,1 3,1'),
350 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700351
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400352 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700353 self.waitUntilSettled()
354
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400355 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700356 self.assertBuilds([
357 dict(name='project-test2', changes='1,1'),
358 dict(name='project-merge', changes='2,1'),
359 ])
360 # Unordered history comparison because the aborts can finish
361 # in any order.
362 self.assertHistory([
363 dict(name='project-merge', result='SUCCESS',
364 changes='1,1'),
365 dict(name='project-merge', result='SUCCESS',
366 changes='1,1 2,1'),
367 dict(name='project-merge', result='SUCCESS',
368 changes='1,1 2,1 3,1'),
369 dict(name='project-test1', result='FAILURE',
370 changes='1,1'),
371 dict(name='project-test1', result='ABORTED',
372 changes='1,1 2,1'),
373 dict(name='project-test2', result='ABORTED',
374 changes='1,1 2,1'),
375 dict(name='project-test1', result='ABORTED',
376 changes='1,1 2,1 3,1'),
377 dict(name='project-test2', result='ABORTED',
378 changes='1,1 2,1 3,1'),
379 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700380
James E. Blairb536ecc2016-08-31 10:11:42 -0700381 self.launch_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700382 self.waitUntilSettled()
James E. Blairb536ecc2016-08-31 10:11:42 -0700383 self.launch_server.release('.*-merge')
384 self.waitUntilSettled()
385 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700386
James E. Blairb536ecc2016-08-31 10:11:42 -0700387 self.assertBuilds([])
388 self.assertHistory([
389 dict(name='project-merge', result='SUCCESS',
390 changes='1,1'),
391 dict(name='project-merge', result='SUCCESS',
392 changes='1,1 2,1'),
393 dict(name='project-merge', result='SUCCESS',
394 changes='1,1 2,1 3,1'),
395 dict(name='project-test1', result='FAILURE',
396 changes='1,1'),
397 dict(name='project-test1', result='ABORTED',
398 changes='1,1 2,1'),
399 dict(name='project-test2', result='ABORTED',
400 changes='1,1 2,1'),
401 dict(name='project-test1', result='ABORTED',
402 changes='1,1 2,1 3,1'),
403 dict(name='project-test2', result='ABORTED',
404 changes='1,1 2,1 3,1'),
405 dict(name='project-merge', result='SUCCESS',
406 changes='2,1'),
407 dict(name='project-merge', result='SUCCESS',
408 changes='2,1 3,1'),
409 dict(name='project-test2', result='SUCCESS',
410 changes='1,1'),
411 dict(name='project-test1', result='SUCCESS',
412 changes='2,1'),
413 dict(name='project-test2', result='SUCCESS',
414 changes='2,1'),
415 dict(name='project-test1', result='SUCCESS',
416 changes='2,1 3,1'),
417 dict(name='project-test2', result='SUCCESS',
418 changes='2,1 3,1'),
419 ], ordered=False)
420
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400421 self.assertEqual(A.data['status'], 'NEW')
422 self.assertEqual(B.data['status'], 'MERGED')
423 self.assertEqual(C.data['status'], 'MERGED')
424 self.assertEqual(A.reported, 2)
425 self.assertEqual(B.reported, 2)
426 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700427
James E. Blair0aac4872013-08-23 14:02:38 -0700428 def test_failed_change_in_middle(self):
429 "Test a failed change in the middle of the queue"
430
James E. Blair08d19992016-08-10 15:25:31 -0700431 self.launch_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700432 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
433 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
434 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700435 A.addApproval('code-review', 2)
436 B.addApproval('code-review', 2)
437 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700438
James E. Blair08d19992016-08-10 15:25:31 -0700439 self.launch_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700440
James E. Blair8b5408c2016-08-08 15:37:46 -0700441 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
442 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
443 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700444
445 self.waitUntilSettled()
446
James E. Blair08d19992016-08-10 15:25:31 -0700447 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700448 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700449 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700450 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700451 self.launch_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700452 self.waitUntilSettled()
453
454 self.assertEqual(len(self.builds), 6)
455 self.assertEqual(self.builds[0].name, 'project-test1')
456 self.assertEqual(self.builds[1].name, 'project-test2')
457 self.assertEqual(self.builds[2].name, 'project-test1')
458 self.assertEqual(self.builds[3].name, 'project-test2')
459 self.assertEqual(self.builds[4].name, 'project-test1')
460 self.assertEqual(self.builds[5].name, 'project-test2')
461
462 self.release(self.builds[2])
463 self.waitUntilSettled()
464
James E. Blair972e3c72013-08-29 12:04:55 -0700465 # project-test1 and project-test2 for A
466 # project-test2 for B
467 # project-merge for C (without B)
468 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700469 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
470
James E. Blair08d19992016-08-10 15:25:31 -0700471 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700472 self.waitUntilSettled()
473
474 # project-test1 and project-test2 for A
475 # project-test2 for B
476 # project-test1 and project-test2 for C
477 self.assertEqual(len(self.builds), 5)
478
Paul Belanger2e2a0492016-10-30 18:33:37 -0400479 tenant = self.sched.abide.tenants.get('tenant-one')
480 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700481 builds = items[0].current_build_set.getBuilds()
482 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
483 self.assertEqual(self.countJobResults(builds, None), 2)
484 builds = items[1].current_build_set.getBuilds()
485 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
486 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
487 self.assertEqual(self.countJobResults(builds, None), 1)
488 builds = items[2].current_build_set.getBuilds()
489 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700490 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700491
James E. Blair08d19992016-08-10 15:25:31 -0700492 self.launch_server.hold_jobs_in_build = False
493 self.launch_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700494 self.waitUntilSettled()
495
496 self.assertEqual(len(self.builds), 0)
497 self.assertEqual(len(self.history), 12)
498 self.assertEqual(A.data['status'], 'MERGED')
499 self.assertEqual(B.data['status'], 'NEW')
500 self.assertEqual(C.data['status'], 'MERGED')
501 self.assertEqual(A.reported, 2)
502 self.assertEqual(B.reported, 2)
503 self.assertEqual(C.reported, 2)
504
James E. Blaird466dc42012-07-31 10:42:56 -0700505 def test_failed_change_at_head_with_queue(self):
506 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700507
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700508 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700509 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
510 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
511 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700512 A.addApproval('code-review', 2)
513 B.addApproval('code-review', 2)
514 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700515
James E. Blair08d19992016-08-10 15:25:31 -0700516 self.launch_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700517
James E. Blair8b5408c2016-08-08 15:37:46 -0700518 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
519 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
520 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700521
522 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700523 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400524 self.assertEqual(len(self.builds), 0)
525 self.assertEqual(len(queue), 1)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800526 self.assertEqual(queue[0].name, 'launcher:launch')
527 job_args = json.loads(queue[0].arguments)
528 self.assertEqual(job_args['job'], 'project-merge')
529 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700530
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700531 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700532 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700533 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700534 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700535 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700536 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700537 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700538
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400539 self.assertEqual(len(self.builds), 0)
540 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800541
542 self.assertEqual(
543 json.loads(queue[0].arguments)['job'], 'project-test1')
544 self.assertEqual(
545 json.loads(queue[1].arguments)['job'], 'project-test2')
546 self.assertEqual(
547 json.loads(queue[2].arguments)['job'], 'project-test1')
548 self.assertEqual(
549 json.loads(queue[3].arguments)['job'], 'project-test2')
550 self.assertEqual(
551 json.loads(queue[4].arguments)['job'], 'project-test1')
552 self.assertEqual(
553 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700554
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700555 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700556 self.waitUntilSettled()
557
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400558 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700559 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400560 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
561 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700562
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700563 self.gearman_server.hold_jobs_in_queue = False
564 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700565 self.waitUntilSettled()
566
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400567 self.assertEqual(len(self.builds), 0)
568 self.assertEqual(len(self.history), 11)
569 self.assertEqual(A.data['status'], 'NEW')
570 self.assertEqual(B.data['status'], 'MERGED')
571 self.assertEqual(C.data['status'], 'MERGED')
572 self.assertEqual(A.reported, 2)
573 self.assertEqual(B.reported, 2)
574 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700575
James E. Blairec056492016-07-22 09:45:56 -0700576 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700577 def _test_time_database(self, iteration):
James E. Blair08d19992016-08-10 15:25:31 -0700578 self.launch_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700580 A.addApproval('code-review', 2)
581 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700582 self.waitUntilSettled()
583 time.sleep(2)
584
585 data = json.loads(self.sched.formatStatusJSON())
586 found_job = None
587 for pipeline in data['pipelines']:
588 if pipeline['name'] != 'gate':
589 continue
590 for queue in pipeline['change_queues']:
591 for head in queue['heads']:
592 for item in head:
593 for job in item['jobs']:
594 if job['name'] == 'project-merge':
595 found_job = job
596 break
597
598 self.assertIsNotNone(found_job)
599 if iteration == 1:
600 self.assertIsNotNone(found_job['estimated_time'])
601 self.assertIsNone(found_job['remaining_time'])
602 else:
603 self.assertIsNotNone(found_job['estimated_time'])
604 self.assertTrue(found_job['estimated_time'] >= 2)
605 self.assertIsNotNone(found_job['remaining_time'])
606
James E. Blair08d19992016-08-10 15:25:31 -0700607 self.launch_server.hold_jobs_in_build = False
608 self.launch_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700609 self.waitUntilSettled()
610
James E. Blairec056492016-07-22 09:45:56 -0700611 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700612 def test_time_database(self):
613 "Test the time database"
614
615 self._test_time_database(1)
616 self._test_time_database(2)
617
James E. Blairfef71632013-09-23 11:15:47 -0700618 def test_two_failed_changes_at_head(self):
619 "Test that changes are reparented correctly if 2 fail at head"
620
James E. Blair08d19992016-08-10 15:25:31 -0700621 self.launch_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
623 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
624 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700625 A.addApproval('code-review', 2)
626 B.addApproval('code-review', 2)
627 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700628
James E. Blair08d19992016-08-10 15:25:31 -0700629 self.launch_server.failJob('project-test1', A)
630 self.launch_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700631
James E. Blair8b5408c2016-08-08 15:37:46 -0700632 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
633 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
634 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700635 self.waitUntilSettled()
636
James E. Blair08d19992016-08-10 15:25:31 -0700637 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700638 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700639 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700640 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700641 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700642 self.waitUntilSettled()
643
644 self.assertEqual(len(self.builds), 6)
645 self.assertEqual(self.builds[0].name, 'project-test1')
646 self.assertEqual(self.builds[1].name, 'project-test2')
647 self.assertEqual(self.builds[2].name, 'project-test1')
648 self.assertEqual(self.builds[3].name, 'project-test2')
649 self.assertEqual(self.builds[4].name, 'project-test1')
650 self.assertEqual(self.builds[5].name, 'project-test2')
651
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400652 self.assertTrue(self.builds[0].hasChanges(A))
653 self.assertTrue(self.builds[2].hasChanges(A))
654 self.assertTrue(self.builds[2].hasChanges(B))
655 self.assertTrue(self.builds[4].hasChanges(A))
656 self.assertTrue(self.builds[4].hasChanges(B))
657 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700658
659 # Fail change B first
660 self.release(self.builds[2])
661 self.waitUntilSettled()
662
663 # restart of C after B failure
James E. Blair08d19992016-08-10 15:25:31 -0700664 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700665 self.waitUntilSettled()
666
667 self.assertEqual(len(self.builds), 5)
668 self.assertEqual(self.builds[0].name, 'project-test1')
669 self.assertEqual(self.builds[1].name, 'project-test2')
670 self.assertEqual(self.builds[2].name, 'project-test2')
671 self.assertEqual(self.builds[3].name, 'project-test1')
672 self.assertEqual(self.builds[4].name, 'project-test2')
673
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400674 self.assertTrue(self.builds[1].hasChanges(A))
675 self.assertTrue(self.builds[2].hasChanges(A))
676 self.assertTrue(self.builds[2].hasChanges(B))
677 self.assertTrue(self.builds[4].hasChanges(A))
678 self.assertFalse(self.builds[4].hasChanges(B))
679 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700680
681 # Finish running all passing jobs for change A
682 self.release(self.builds[1])
683 self.waitUntilSettled()
684 # Fail and report change A
685 self.release(self.builds[0])
686 self.waitUntilSettled()
687
688 # restart of B,C after A failure
James E. Blair08d19992016-08-10 15:25:31 -0700689 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700690 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700691 self.launch_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700692 self.waitUntilSettled()
693
694 self.assertEqual(len(self.builds), 4)
695 self.assertEqual(self.builds[0].name, 'project-test1') # B
696 self.assertEqual(self.builds[1].name, 'project-test2') # B
697 self.assertEqual(self.builds[2].name, 'project-test1') # C
698 self.assertEqual(self.builds[3].name, 'project-test2') # C
699
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400700 self.assertFalse(self.builds[1].hasChanges(A))
701 self.assertTrue(self.builds[1].hasChanges(B))
702 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700703
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400704 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700705 # After A failed and B and C restarted, B should be back in
706 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400707 self.assertTrue(self.builds[2].hasChanges(B))
708 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700709
James E. Blair08d19992016-08-10 15:25:31 -0700710 self.launch_server.hold_jobs_in_build = False
711 self.launch_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700712 self.waitUntilSettled()
713
714 self.assertEqual(len(self.builds), 0)
715 self.assertEqual(len(self.history), 21)
716 self.assertEqual(A.data['status'], 'NEW')
717 self.assertEqual(B.data['status'], 'NEW')
718 self.assertEqual(C.data['status'], 'MERGED')
719 self.assertEqual(A.reported, 2)
720 self.assertEqual(B.reported, 2)
721 self.assertEqual(C.reported, 2)
722
James E. Blair8c803f82012-07-31 16:25:42 -0700723 def test_patch_order(self):
724 "Test that dependent patches are tested in the right order"
725 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
726 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
727 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700728 A.addApproval('code-review', 2)
729 B.addApproval('code-review', 2)
730 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700731
732 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
733 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
734 M2.setMerged()
735 M1.setMerged()
736
737 # C -> B -> A -> M1 -> M2
738 # M2 is here to make sure it is never queried. If it is, it
739 # means zuul is walking down the entire history of merged
740 # changes.
741
742 C.setDependsOn(B, 1)
743 B.setDependsOn(A, 1)
744 A.setDependsOn(M1, 1)
745 M1.setDependsOn(M2, 1)
746
James E. Blair8b5408c2016-08-08 15:37:46 -0700747 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700748
749 self.waitUntilSettled()
750
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400751 self.assertEqual(A.data['status'], 'NEW')
752 self.assertEqual(B.data['status'], 'NEW')
753 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700754
James E. Blair8b5408c2016-08-08 15:37:46 -0700755 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
756 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700757
758 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400759 self.assertEqual(M2.queried, 0)
760 self.assertEqual(A.data['status'], 'MERGED')
761 self.assertEqual(B.data['status'], 'MERGED')
762 self.assertEqual(C.data['status'], 'MERGED')
763 self.assertEqual(A.reported, 2)
764 self.assertEqual(B.reported, 2)
765 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700766
James E. Blair063672f2015-01-29 13:09:12 -0800767 def test_needed_changes_enqueue(self):
768 "Test that a needed change is enqueued ahead"
769 # A Given a git tree like this, if we enqueue
770 # / \ change C, we should walk up and down the tree
771 # B G and enqueue changes in the order ABCDEFG.
772 # /|\ This is also the order that you would get if
773 # *C E F you enqueued changes in the order ABCDEFG, so
774 # / the ordering is stable across re-enqueue events.
775 # D
776
777 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
778 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
779 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
780 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
781 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
782 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
783 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
784 B.setDependsOn(A, 1)
785 C.setDependsOn(B, 1)
786 D.setDependsOn(C, 1)
787 E.setDependsOn(B, 1)
788 F.setDependsOn(B, 1)
789 G.setDependsOn(A, 1)
790
James E. Blair8b5408c2016-08-08 15:37:46 -0700791 A.addApproval('code-review', 2)
792 B.addApproval('code-review', 2)
793 C.addApproval('code-review', 2)
794 D.addApproval('code-review', 2)
795 E.addApproval('code-review', 2)
796 F.addApproval('code-review', 2)
797 G.addApproval('code-review', 2)
798 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800799
800 self.waitUntilSettled()
801
802 self.assertEqual(A.data['status'], 'NEW')
803 self.assertEqual(B.data['status'], 'NEW')
804 self.assertEqual(C.data['status'], 'NEW')
805 self.assertEqual(D.data['status'], 'NEW')
806 self.assertEqual(E.data['status'], 'NEW')
807 self.assertEqual(F.data['status'], 'NEW')
808 self.assertEqual(G.data['status'], 'NEW')
809
810 # We're about to add approvals to changes without adding the
811 # triggering events to Zuul, so that we can be sure that it is
812 # enqueing the changes based on dependencies, not because of
813 # triggering events. Since it will have the changes cached
814 # already (without approvals), we need to clear the cache
815 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400816 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100817 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800818
James E. Blair08d19992016-08-10 15:25:31 -0700819 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700820 A.addApproval('approved', 1)
821 B.addApproval('approved', 1)
822 D.addApproval('approved', 1)
823 E.addApproval('approved', 1)
824 F.addApproval('approved', 1)
825 G.addApproval('approved', 1)
826 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800827
828 for x in range(8):
James E. Blair08d19992016-08-10 15:25:31 -0700829 self.launch_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800830 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -0700831 self.launch_server.hold_jobs_in_build = False
832 self.launch_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800833 self.waitUntilSettled()
834
835 self.assertEqual(A.data['status'], 'MERGED')
836 self.assertEqual(B.data['status'], 'MERGED')
837 self.assertEqual(C.data['status'], 'MERGED')
838 self.assertEqual(D.data['status'], 'MERGED')
839 self.assertEqual(E.data['status'], 'MERGED')
840 self.assertEqual(F.data['status'], 'MERGED')
841 self.assertEqual(G.data['status'], 'MERGED')
842 self.assertEqual(A.reported, 2)
843 self.assertEqual(B.reported, 2)
844 self.assertEqual(C.reported, 2)
845 self.assertEqual(D.reported, 2)
846 self.assertEqual(E.reported, 2)
847 self.assertEqual(F.reported, 2)
848 self.assertEqual(G.reported, 2)
849 self.assertEqual(self.history[6].changes,
850 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
851
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100852 def test_source_cache(self):
853 "Test that the source cache operates correctly"
James E. Blair08d19992016-08-10 15:25:31 -0700854 self.launch_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700855
856 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
857 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
858 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700859 A.addApproval('code-review', 2)
860 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700861
862 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
863 M1.setMerged()
864
865 B.setDependsOn(A, 1)
866 A.setDependsOn(M1, 1)
867
James E. Blair8b5408c2016-08-08 15:37:46 -0700868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700869 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
870
871 self.waitUntilSettled()
872
873 for build in self.builds:
874 if build.parameters['ZUUL_PIPELINE'] == 'check':
875 build.release()
876 self.waitUntilSettled()
877 for build in self.builds:
878 if build.parameters['ZUUL_PIPELINE'] == 'check':
879 build.release()
880 self.waitUntilSettled()
881
James E. Blair8b5408c2016-08-08 15:37:46 -0700882 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700883 self.waitUntilSettled()
884
Joshua Hesketh352264b2015-08-11 23:42:08 +1000885 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700886 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000887 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700888
James E. Blair08d19992016-08-10 15:25:31 -0700889 self.launch_server.hold_jobs_in_build = False
890 self.launch_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700891 self.waitUntilSettled()
892
893 self.assertEqual(A.data['status'], 'MERGED')
894 self.assertEqual(B.data['status'], 'MERGED')
895 self.assertEqual(A.queried, 2) # Initial and isMerged
896 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
897
James E. Blair8c803f82012-07-31 16:25:42 -0700898 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700899 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700900 # TODO: move to test_gerrit (this is a unit test!)
901 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400902 tenant = self.sched.abide.tenants.get('tenant-one')
903 source = tenant.layout.pipelines['gate'].source
904
905 # TODO(pabelanger): As we add more source / trigger APIs we should make
906 # it easier for users to create events for testing.
907 event = zuul.model.TriggerEvent()
908 event.trigger_name = 'gerrit'
909 event.change_number = '1'
910 event.patch_number = '2'
911
912 a = source.getChange(event)
913 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700914 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700915
James E. Blair8b5408c2016-08-08 15:37:46 -0700916 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400917 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700918 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700919
James E. Blair8b5408c2016-08-08 15:37:46 -0700920 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400921 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700922 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700923
James E. Blairec056492016-07-22 09:45:56 -0700924 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700925 def test_build_configuration_conflict(self):
926 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700927
928 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700929 A = self.fake_gerrit.addFakeChange('org/conflict-project',
930 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700931 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700932 B = self.fake_gerrit.addFakeChange('org/conflict-project',
933 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700934 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700935 C = self.fake_gerrit.addFakeChange('org/conflict-project',
936 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700937 A.addApproval('code-review', 2)
938 B.addApproval('code-review', 2)
939 C.addApproval('code-review', 2)
940 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
941 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
942 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700943 self.waitUntilSettled()
944
James E. Blair6736beb2013-07-11 15:18:15 -0700945 self.assertEqual(A.reported, 1)
946 self.assertEqual(B.reported, 1)
947 self.assertEqual(C.reported, 1)
948
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700949 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700950 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700951 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700952 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700953 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700954 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700955
956 self.assertEqual(len(self.history), 2) # A and C merge jobs
957
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700958 self.gearman_server.hold_jobs_in_queue = False
959 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700960 self.waitUntilSettled()
961
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400962 self.assertEqual(A.data['status'], 'MERGED')
963 self.assertEqual(B.data['status'], 'NEW')
964 self.assertEqual(C.data['status'], 'MERGED')
965 self.assertEqual(A.reported, 2)
966 self.assertEqual(B.reported, 2)
967 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700968 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700969
James E. Blairdaabed22012-08-15 15:38:57 -0700970 def test_post(self):
971 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700972
Zhongyue Luo5d556072012-09-21 02:00:47 +0900973 e = {
974 "type": "ref-updated",
975 "submitter": {
976 "name": "User Name",
977 },
978 "refUpdate": {
979 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
980 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
981 "refName": "master",
982 "project": "org/project",
983 }
984 }
James E. Blairdaabed22012-08-15 15:38:57 -0700985 self.fake_gerrit.addEvent(e)
986 self.waitUntilSettled()
987
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400988 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400989 self.assertEqual(len(self.history), 1)
990 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700991
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700992 def test_post_ignore_deletes(self):
993 "Test that deleting refs does not trigger post jobs"
994
995 e = {
996 "type": "ref-updated",
997 "submitter": {
998 "name": "User Name",
999 },
1000 "refUpdate": {
1001 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1002 "newRev": "0000000000000000000000000000000000000000",
1003 "refName": "master",
1004 "project": "org/project",
1005 }
1006 }
1007 self.fake_gerrit.addEvent(e)
1008 self.waitUntilSettled()
1009
1010 job_names = [x.name for x in self.history]
1011 self.assertEqual(len(self.history), 0)
1012 self.assertNotIn('project-post', job_names)
1013
1014 def test_post_ignore_deletes_negative(self):
1015 "Test that deleting refs does trigger post jobs"
1016
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001017 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001018 self.sched.reconfigure(self.config)
1019
1020 e = {
1021 "type": "ref-updated",
1022 "submitter": {
1023 "name": "User Name",
1024 },
1025 "refUpdate": {
1026 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1027 "newRev": "0000000000000000000000000000000000000000",
1028 "refName": "master",
1029 "project": "org/project",
1030 }
1031 }
1032 self.fake_gerrit.addEvent(e)
1033 self.waitUntilSettled()
1034
1035 job_names = [x.name for x in self.history]
1036 self.assertEqual(len(self.history), 1)
1037 self.assertIn('project-post', job_names)
1038
James E. Blairec056492016-07-22 09:45:56 -07001039 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001040 def test_build_configuration_branch_interaction(self):
1041 "Test that switching between branches works"
1042 self.test_build_configuration()
1043 self.test_build_configuration_branch()
1044 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001045 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001046 repo = git.Repo(path)
1047 repo.heads.master.commit = repo.commit('init')
1048 self.test_build_configuration()
1049
James E. Blaircaec0c52012-08-22 14:52:22 -07001050 def test_dependent_changes_dequeue(self):
1051 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001052
James E. Blaircaec0c52012-08-22 14:52:22 -07001053 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1054 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1055 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001056 A.addApproval('code-review', 2)
1057 B.addApproval('code-review', 2)
1058 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001059
1060 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1061 M1.setMerged()
1062
1063 # C -> B -> A -> M1
1064
1065 C.setDependsOn(B, 1)
1066 B.setDependsOn(A, 1)
1067 A.setDependsOn(M1, 1)
1068
James E. Blair08d19992016-08-10 15:25:31 -07001069 self.launch_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001070
James E. Blair8b5408c2016-08-08 15:37:46 -07001071 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1072 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1073 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001074
1075 self.waitUntilSettled()
1076
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001077 self.assertEqual(A.data['status'], 'NEW')
1078 self.assertEqual(A.reported, 2)
1079 self.assertEqual(B.data['status'], 'NEW')
1080 self.assertEqual(B.reported, 2)
1081 self.assertEqual(C.data['status'], 'NEW')
1082 self.assertEqual(C.reported, 2)
1083 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001084
James E. Blair972e3c72013-08-29 12:04:55 -07001085 def test_failing_dependent_changes(self):
1086 "Test that failing dependent patches are taken out of stream"
James E. Blair08d19992016-08-10 15:25:31 -07001087 self.launch_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001088 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1089 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1090 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1091 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1092 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001093 A.addApproval('code-review', 2)
1094 B.addApproval('code-review', 2)
1095 C.addApproval('code-review', 2)
1096 D.addApproval('code-review', 2)
1097 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001098
1099 # E, D -> C -> B, A
1100
1101 D.setDependsOn(C, 1)
1102 C.setDependsOn(B, 1)
1103
James E. Blair08d19992016-08-10 15:25:31 -07001104 self.launch_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001105
James E. Blair8b5408c2016-08-08 15:37:46 -07001106 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1107 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1108 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1109 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1110 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001111
1112 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001113 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001114 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001115 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001116 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001117 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001118 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001119 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001120 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001121 self.launch_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001122 self.waitUntilSettled()
1123
James E. Blair08d19992016-08-10 15:25:31 -07001124 self.launch_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001125 for build in self.builds:
1126 if build.parameters['ZUUL_CHANGE'] != '1':
1127 build.release()
1128 self.waitUntilSettled()
1129
James E. Blair08d19992016-08-10 15:25:31 -07001130 self.launch_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001131 self.waitUntilSettled()
1132
1133 self.assertEqual(A.data['status'], 'MERGED')
1134 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001135 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001136 self.assertEqual(B.data['status'], 'NEW')
1137 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001138 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001139 self.assertEqual(C.data['status'], 'NEW')
1140 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001141 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001142 self.assertEqual(D.data['status'], 'NEW')
1143 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001144 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001145 self.assertEqual(E.data['status'], 'MERGED')
1146 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001147 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001148 self.assertEqual(len(self.history), 18)
1149
James E. Blairec590122012-08-22 15:19:31 -07001150 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001151 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001152 # If it's dequeued more than once, we should see extra
1153 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001154
James E. Blair08d19992016-08-10 15:25:31 -07001155 self.launch_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001156 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1157 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1158 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001159 A.addApproval('code-review', 2)
1160 B.addApproval('code-review', 2)
1161 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001162
Paul Belanger58bf6912016-11-11 19:36:01 -05001163 self.launch_server.failJob('project-test1', A)
1164 self.launch_server.failJob('project-test2', A)
James E. Blair08d19992016-08-10 15:25:31 -07001165 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001166
James E. Blair8b5408c2016-08-08 15:37:46 -07001167 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1168 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1169 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001170
1171 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001172
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001173 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001174 self.assertEqual(self.builds[0].name, 'project-merge')
1175 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001176
James E. Blair08d19992016-08-10 15:25:31 -07001177 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001178 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001179 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001180 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001181 self.launch_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001182 self.waitUntilSettled()
1183
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001184 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001185 self.assertEqual(self.builds[0].name, 'project-test1')
1186 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001187 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001188 self.assertEqual(self.builds[3].name, 'project-test1')
1189 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001190 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001191 self.assertEqual(self.builds[6].name, 'project-test1')
1192 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001193 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001194
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001195 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001196 self.waitUntilSettled()
1197
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001198 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1199 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001200
James E. Blair08d19992016-08-10 15:25:31 -07001201 self.launch_server.hold_jobs_in_build = False
1202 self.launch_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001203 self.waitUntilSettled()
1204
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001205 self.assertEqual(len(self.builds), 0)
1206 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001207
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001208 self.assertEqual(A.data['status'], 'NEW')
1209 self.assertEqual(B.data['status'], 'MERGED')
1210 self.assertEqual(C.data['status'], 'MERGED')
1211 self.assertEqual(A.reported, 2)
1212 self.assertEqual(B.reported, 2)
1213 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001214
1215 def test_nonvoting_job(self):
1216 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001217
James E. Blair4ec821f2012-08-23 15:28:28 -07001218 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1219 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001220 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07001221 self.launch_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001222 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001223
1224 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001225
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001226 self.assertEqual(A.data['status'], 'MERGED')
1227 self.assertEqual(A.reported, 2)
1228 self.assertEqual(
1229 self.getJobFromHistory('nonvoting-project-merge').result,
1230 'SUCCESS')
1231 self.assertEqual(
1232 self.getJobFromHistory('nonvoting-project-test1').result,
1233 'SUCCESS')
1234 self.assertEqual(
1235 self.getJobFromHistory('nonvoting-project-test2').result,
1236 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001237
James E. Blair5821bd92015-09-16 08:48:15 -07001238 for build in self.builds:
1239 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1240
James E. Blaire0487072012-08-29 17:38:31 -07001241 def test_check_queue_success(self):
1242 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001243
James E. Blaire0487072012-08-29 17:38:31 -07001244 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1245 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1246
1247 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001248
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(A.data['status'], 'NEW')
1250 self.assertEqual(A.reported, 1)
1251 self.assertEqual(self.getJobFromHistory('project-merge').result,
1252 'SUCCESS')
1253 self.assertEqual(self.getJobFromHistory('project-test1').result,
1254 'SUCCESS')
1255 self.assertEqual(self.getJobFromHistory('project-test2').result,
1256 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001257
1258 def test_check_queue_failure(self):
1259 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001260
James E. Blaire0487072012-08-29 17:38:31 -07001261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair08d19992016-08-10 15:25:31 -07001262 self.launch_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001263 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1264
1265 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001266
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001267 self.assertEqual(A.data['status'], 'NEW')
1268 self.assertEqual(A.reported, 1)
1269 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001270 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001271 self.assertEqual(self.getJobFromHistory('project-test1').result,
1272 'SUCCESS')
1273 self.assertEqual(self.getJobFromHistory('project-test2').result,
1274 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001275
1276 def test_dependent_behind_dequeue(self):
1277 "test that dependent changes behind dequeued changes work"
1278 # This complicated test is a reproduction of a real life bug
1279 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001280
James E. Blair08d19992016-08-10 15:25:31 -07001281 self.launch_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001282 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1283 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1284 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1285 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1286 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1287 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1288 D.setDependsOn(C, 1)
1289 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001290 A.addApproval('code-review', 2)
1291 B.addApproval('code-review', 2)
1292 C.addApproval('code-review', 2)
1293 D.addApproval('code-review', 2)
1294 E.addApproval('code-review', 2)
1295 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001296
1297 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001298
1299 # Change object re-use in the gerrit trigger is hidden if
1300 # changes are added in quick succession; waiting makes it more
1301 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001302 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001303 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001304 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001305 self.waitUntilSettled()
1306
James E. Blair08d19992016-08-10 15:25:31 -07001307 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001308 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001309 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001310 self.waitUntilSettled()
1311
James E. Blair8b5408c2016-08-08 15:37:46 -07001312 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001313 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001314 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001315 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001316 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001317 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001318 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001319 self.waitUntilSettled()
1320
James E. Blair08d19992016-08-10 15:25:31 -07001321 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001322 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001323 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001324 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001325 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001326 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001327 self.launch_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001328 self.waitUntilSettled()
1329
1330 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001331
1332 # Grab pointers to the jobs we want to release before
1333 # releasing any, because list indexes may change as
1334 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001335 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001336 a.release()
1337 b.release()
1338 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001339 self.waitUntilSettled()
1340
James E. Blair08d19992016-08-10 15:25:31 -07001341 self.launch_server.hold_jobs_in_build = False
1342 self.launch_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001343 self.waitUntilSettled()
1344
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001345 self.assertEqual(A.data['status'], 'NEW')
1346 self.assertEqual(B.data['status'], 'MERGED')
1347 self.assertEqual(C.data['status'], 'MERGED')
1348 self.assertEqual(D.data['status'], 'MERGED')
1349 self.assertEqual(E.data['status'], 'MERGED')
1350 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001351
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001352 self.assertEqual(A.reported, 2)
1353 self.assertEqual(B.reported, 2)
1354 self.assertEqual(C.reported, 2)
1355 self.assertEqual(D.reported, 2)
1356 self.assertEqual(E.reported, 2)
1357 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001358
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001359 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1360 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001361
1362 def test_merger_repack(self):
1363 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001364
James E. Blair05fed602012-09-07 12:45:24 -07001365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001366 A.addApproval('code-review', 2)
1367 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001368 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001369 self.assertEqual(self.getJobFromHistory('project-merge').result,
1370 'SUCCESS')
1371 self.assertEqual(self.getJobFromHistory('project-test1').result,
1372 'SUCCESS')
1373 self.assertEqual(self.getJobFromHistory('project-test2').result,
1374 'SUCCESS')
1375 self.assertEqual(A.data['status'], 'MERGED')
1376 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001377 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001378 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001379
Monty Taylorbc758832013-06-17 17:22:42 -04001380 path = os.path.join(self.git_root, "org/project")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001381 print(repack_repo(path))
James E. Blair05fed602012-09-07 12:45:24 -07001382
1383 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001384 A.addApproval('code-review', 2)
1385 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001386 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001387 self.assertEqual(self.getJobFromHistory('project-merge').result,
1388 'SUCCESS')
1389 self.assertEqual(self.getJobFromHistory('project-test1').result,
1390 'SUCCESS')
1391 self.assertEqual(self.getJobFromHistory('project-test2').result,
1392 'SUCCESS')
1393 self.assertEqual(A.data['status'], 'MERGED')
1394 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001395
James E. Blair4886f282012-11-15 09:27:33 -08001396 def test_merger_repack_large_change(self):
1397 "Test that the merger works with large changes after a repack"
1398 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001399 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001400 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001401 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001402 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001403 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001404 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1405 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001406 path = os.path.join(self.upstream_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001407 print(repack_repo(path))
Monty Taylorbc758832013-06-17 17:22:42 -04001408 path = os.path.join(self.git_root, "org/project1")
Morgan Fainberg4c6a7742016-05-27 08:42:17 -07001409 print(repack_repo(path))
James E. Blair4886f282012-11-15 09:27:33 -08001410
James E. Blair8b5408c2016-08-08 15:37:46 -07001411 A.addApproval('code-review', 2)
1412 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001413 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001414 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001415 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001416 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001417 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001418 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001419 'SUCCESS')
1420 self.assertEqual(A.data['status'], 'MERGED')
1421 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001422
James E. Blair2fa50962013-01-30 21:50:41 -08001423 def test_new_patchset_dequeues_old(self):
1424 "Test that a new patchset causes the old to be dequeued"
1425 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001426 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001427 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1428 M.setMerged()
1429
1430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1431 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1432 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1433 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001434 A.addApproval('code-review', 2)
1435 B.addApproval('code-review', 2)
1436 C.addApproval('code-review', 2)
1437 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001438
1439 C.setDependsOn(B, 1)
1440 B.setDependsOn(A, 1)
1441 A.setDependsOn(M, 1)
1442
James E. Blair8b5408c2016-08-08 15:37:46 -07001443 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1444 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1445 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1446 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001447 self.waitUntilSettled()
1448
1449 B.addPatchset()
1450 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1451 self.waitUntilSettled()
1452
James E. Blair08d19992016-08-10 15:25:31 -07001453 self.launch_server.hold_jobs_in_build = False
1454 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001455 self.waitUntilSettled()
1456
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001457 self.assertEqual(A.data['status'], 'MERGED')
1458 self.assertEqual(A.reported, 2)
1459 self.assertEqual(B.data['status'], 'NEW')
1460 self.assertEqual(B.reported, 2)
1461 self.assertEqual(C.data['status'], 'NEW')
1462 self.assertEqual(C.reported, 2)
1463 self.assertEqual(D.data['status'], 'MERGED')
1464 self.assertEqual(D.reported, 2)
1465 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001466
James E. Blairba437362015-02-07 11:41:52 -08001467 def test_new_patchset_check(self):
1468 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001469
James E. Blair08d19992016-08-10 15:25:31 -07001470 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001471
1472 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001473 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001474 tenant = self.sched.abide.tenants.get('tenant-one')
1475 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001476
1477 # Add two git-dependent changes
1478 B.setDependsOn(A, 1)
1479 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1480 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1482 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001483
1484 # A live item, and a non-live/live pair
1485 items = check_pipeline.getAllItems()
1486 self.assertEqual(len(items), 3)
1487
1488 self.assertEqual(items[0].change.number, '1')
1489 self.assertEqual(items[0].change.patchset, '1')
1490 self.assertFalse(items[0].live)
1491
1492 self.assertEqual(items[1].change.number, '2')
1493 self.assertEqual(items[1].change.patchset, '1')
1494 self.assertTrue(items[1].live)
1495
1496 self.assertEqual(items[2].change.number, '1')
1497 self.assertEqual(items[2].change.patchset, '1')
1498 self.assertTrue(items[2].live)
1499
1500 # Add a new patchset to A
1501 A.addPatchset()
1502 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1503 self.waitUntilSettled()
1504
1505 # The live copy of A,1 should be gone, but the non-live and B
1506 # should continue, and we should have a new A,2
1507 items = check_pipeline.getAllItems()
1508 self.assertEqual(len(items), 3)
1509
1510 self.assertEqual(items[0].change.number, '1')
1511 self.assertEqual(items[0].change.patchset, '1')
1512 self.assertFalse(items[0].live)
1513
1514 self.assertEqual(items[1].change.number, '2')
1515 self.assertEqual(items[1].change.patchset, '1')
1516 self.assertTrue(items[1].live)
1517
1518 self.assertEqual(items[2].change.number, '1')
1519 self.assertEqual(items[2].change.patchset, '2')
1520 self.assertTrue(items[2].live)
1521
1522 # Add a new patchset to B
1523 B.addPatchset()
1524 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1525 self.waitUntilSettled()
1526
1527 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1528 # but we should have a new B,2 (still based on A,1)
1529 items = check_pipeline.getAllItems()
1530 self.assertEqual(len(items), 3)
1531
1532 self.assertEqual(items[0].change.number, '1')
1533 self.assertEqual(items[0].change.patchset, '2')
1534 self.assertTrue(items[0].live)
1535
1536 self.assertEqual(items[1].change.number, '1')
1537 self.assertEqual(items[1].change.patchset, '1')
1538 self.assertFalse(items[1].live)
1539
1540 self.assertEqual(items[2].change.number, '2')
1541 self.assertEqual(items[2].change.patchset, '2')
1542 self.assertTrue(items[2].live)
1543
1544 self.builds[0].release()
1545 self.waitUntilSettled()
1546 self.builds[0].release()
1547 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001548 self.launch_server.hold_jobs_in_build = False
1549 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001550 self.waitUntilSettled()
1551
1552 self.assertEqual(A.reported, 1)
1553 self.assertEqual(B.reported, 1)
1554 self.assertEqual(self.history[0].result, 'ABORTED')
1555 self.assertEqual(self.history[0].changes, '1,1')
1556 self.assertEqual(self.history[1].result, 'ABORTED')
1557 self.assertEqual(self.history[1].changes, '1,1 2,1')
1558 self.assertEqual(self.history[2].result, 'SUCCESS')
1559 self.assertEqual(self.history[2].changes, '1,2')
1560 self.assertEqual(self.history[3].result, 'SUCCESS')
1561 self.assertEqual(self.history[3].changes, '1,1 2,2')
1562
1563 def test_abandoned_gate(self):
1564 "Test that an abandoned change is dequeued from gate"
1565
James E. Blair08d19992016-08-10 15:25:31 -07001566 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001567
1568 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001569 A.addApproval('code-review', 2)
1570 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001571 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001572 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1573 self.assertEqual(self.builds[0].name, 'project-merge')
1574
1575 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1576 self.waitUntilSettled()
1577
James E. Blair08d19992016-08-10 15:25:31 -07001578 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001579 self.waitUntilSettled()
1580
Arie5b4048c2016-10-05 16:12:06 +03001581 self.assertBuilds([])
1582 self.assertHistory([
1583 dict(name='project-merge', result='ABORTED', changes='1,1')],
1584 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001585 self.assertEqual(A.reported, 1,
1586 "Abandoned gate change should report only start")
1587
1588 def test_abandoned_check(self):
1589 "Test that an abandoned change is dequeued from check"
1590
James E. Blair08d19992016-08-10 15:25:31 -07001591 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001592
1593 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1594 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001595 tenant = self.sched.abide.tenants.get('tenant-one')
1596 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001597
1598 # Add two git-dependent changes
1599 B.setDependsOn(A, 1)
1600 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1601 self.waitUntilSettled()
1602 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1603 self.waitUntilSettled()
1604 # A live item, and a non-live/live pair
1605 items = check_pipeline.getAllItems()
1606 self.assertEqual(len(items), 3)
1607
1608 self.assertEqual(items[0].change.number, '1')
1609 self.assertFalse(items[0].live)
1610
1611 self.assertEqual(items[1].change.number, '2')
1612 self.assertTrue(items[1].live)
1613
1614 self.assertEqual(items[2].change.number, '1')
1615 self.assertTrue(items[2].live)
1616
1617 # Abandon A
1618 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1619 self.waitUntilSettled()
1620
1621 # The live copy of A should be gone, but the non-live and B
1622 # should continue
1623 items = check_pipeline.getAllItems()
1624 self.assertEqual(len(items), 2)
1625
1626 self.assertEqual(items[0].change.number, '1')
1627 self.assertFalse(items[0].live)
1628
1629 self.assertEqual(items[1].change.number, '2')
1630 self.assertTrue(items[1].live)
1631
James E. Blair08d19992016-08-10 15:25:31 -07001632 self.launch_server.hold_jobs_in_build = False
1633 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001634 self.waitUntilSettled()
1635
1636 self.assertEqual(len(self.history), 4)
1637 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001638 'Build should have been aborted')
1639 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001640 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001641
James E. Blairec056492016-07-22 09:45:56 -07001642 @skip("Disabled for early v3 development")
Steve Varnau7b78b312015-04-03 14:49:46 -07001643 def test_abandoned_not_timer(self):
1644 "Test that an abandoned change does not cancel timer jobs"
1645
James E. Blair08d19992016-08-10 15:25:31 -07001646 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001647
1648 # Start timer trigger - also org/project
James E. Blairf84026c2015-12-08 16:11:46 -08001649 self.updateConfigLayout(
1650 'tests/fixtures/layout-idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001651 self.sched.reconfigure(self.config)
1652 self.registerJobs()
1653 # The pipeline triggers every second, so we should have seen
1654 # several by now.
1655 time.sleep(5)
1656 self.waitUntilSettled()
1657 # Stop queuing timer triggered jobs so that the assertions
1658 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08001659 self.updateConfigLayout(
1660 'tests/fixtures/layout-no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001661 self.sched.reconfigure(self.config)
1662 self.registerJobs()
1663 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1664
1665 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1666 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1667 self.waitUntilSettled()
1668 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1669
1670 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1671 self.waitUntilSettled()
1672
1673 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1674
James E. Blair08d19992016-08-10 15:25:31 -07001675 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001676 self.waitUntilSettled()
1677
Arx Cruzb1b010d2013-10-28 19:49:59 -02001678 def test_zuul_url_return(self):
1679 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001680 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001681 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001682
1683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001684 A.addApproval('code-review', 2)
1685 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001686 self.waitUntilSettled()
1687
1688 self.assertEqual(len(self.builds), 1)
1689 for build in self.builds:
1690 self.assertTrue('ZUUL_URL' in build.parameters)
1691
James E. Blair08d19992016-08-10 15:25:31 -07001692 self.launch_server.hold_jobs_in_build = False
1693 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001694 self.waitUntilSettled()
1695
James E. Blair2fa50962013-01-30 21:50:41 -08001696 def test_new_patchset_dequeues_old_on_head(self):
1697 "Test that a new patchset causes the old to be dequeued (at head)"
1698 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001699 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001700 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1701 M.setMerged()
1702 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1703 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1704 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1705 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001706 A.addApproval('code-review', 2)
1707 B.addApproval('code-review', 2)
1708 C.addApproval('code-review', 2)
1709 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001710
1711 C.setDependsOn(B, 1)
1712 B.setDependsOn(A, 1)
1713 A.setDependsOn(M, 1)
1714
James E. Blair8b5408c2016-08-08 15:37:46 -07001715 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1716 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1717 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1718 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001719 self.waitUntilSettled()
1720
1721 A.addPatchset()
1722 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1723 self.waitUntilSettled()
1724
James E. Blair08d19992016-08-10 15:25:31 -07001725 self.launch_server.hold_jobs_in_build = False
1726 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001727 self.waitUntilSettled()
1728
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001729 self.assertEqual(A.data['status'], 'NEW')
1730 self.assertEqual(A.reported, 2)
1731 self.assertEqual(B.data['status'], 'NEW')
1732 self.assertEqual(B.reported, 2)
1733 self.assertEqual(C.data['status'], 'NEW')
1734 self.assertEqual(C.reported, 2)
1735 self.assertEqual(D.data['status'], 'MERGED')
1736 self.assertEqual(D.reported, 2)
1737 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001738
1739 def test_new_patchset_dequeues_old_without_dependents(self):
1740 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001741 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001742 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1743 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1744 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001745 A.addApproval('code-review', 2)
1746 B.addApproval('code-review', 2)
1747 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001748
James E. Blair8b5408c2016-08-08 15:37:46 -07001749 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1750 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1751 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001752 self.waitUntilSettled()
1753
1754 B.addPatchset()
1755 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1756 self.waitUntilSettled()
1757
James E. Blair08d19992016-08-10 15:25:31 -07001758 self.launch_server.hold_jobs_in_build = False
1759 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001760 self.waitUntilSettled()
1761
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001762 self.assertEqual(A.data['status'], 'MERGED')
1763 self.assertEqual(A.reported, 2)
1764 self.assertEqual(B.data['status'], 'NEW')
1765 self.assertEqual(B.reported, 2)
1766 self.assertEqual(C.data['status'], 'MERGED')
1767 self.assertEqual(C.reported, 2)
1768 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001769
1770 def test_new_patchset_dequeues_old_independent_queue(self):
1771 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001772 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001773 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1774 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1775 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1776 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1777 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1778 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1779 self.waitUntilSettled()
1780
1781 B.addPatchset()
1782 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1783 self.waitUntilSettled()
1784
James E. Blair08d19992016-08-10 15:25:31 -07001785 self.launch_server.hold_jobs_in_build = False
1786 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001787 self.waitUntilSettled()
1788
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001789 self.assertEqual(A.data['status'], 'NEW')
1790 self.assertEqual(A.reported, 1)
1791 self.assertEqual(B.data['status'], 'NEW')
1792 self.assertEqual(B.reported, 1)
1793 self.assertEqual(C.data['status'], 'NEW')
1794 self.assertEqual(C.reported, 1)
1795 self.assertEqual(len(self.history), 10)
1796 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001797
James E. Blair18c64442014-03-18 10:14:45 -07001798 def test_noop_job(self):
1799 "Test that the internal noop job works"
1800 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001801 A.addApproval('code-review', 2)
1802 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001803 self.waitUntilSettled()
1804
1805 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1806 self.assertTrue(self.sched._areAllBuildsComplete())
1807 self.assertEqual(len(self.history), 0)
1808 self.assertEqual(A.data['status'], 'MERGED')
1809 self.assertEqual(A.reported, 2)
1810
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001811 def test_no_job_project(self):
1812 "Test that reports with no jobs don't get sent"
1813 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1814 'master', 'A')
1815 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1816 self.waitUntilSettled()
1817
1818 # Change wasn't reported to
1819 self.assertEqual(A.reported, False)
1820
1821 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001822 tenant = self.sched.abide.tenants.get('tenant-one')
1823 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001824 items = check_pipeline.getAllItems()
1825 self.assertEqual(len(items), 0)
1826
1827 self.assertEqual(len(self.history), 0)
1828
James E. Blair7d0dedc2013-02-21 17:26:09 -08001829 def test_zuul_refs(self):
1830 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07001831 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001832 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1833 M1.setMerged()
1834 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1835 M2.setMerged()
1836
1837 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1838 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1839 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1840 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001841 A.addApproval('code-review', 2)
1842 B.addApproval('code-review', 2)
1843 C.addApproval('code-review', 2)
1844 D.addApproval('code-review', 2)
1845 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1846 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1847 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1848 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001849
1850 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001851 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001852 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001853 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001854 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001855 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001856 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001857 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001858 self.waitUntilSettled()
1859
James E. Blair7d0dedc2013-02-21 17:26:09 -08001860 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001861 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001862 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001863 if x.parameters['ZUUL_CHANGE'] == '3':
1864 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001865 a_build = x
1866 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001867 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001868 b_build = x
1869 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001870 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001871 c_build = x
1872 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001873 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001874 d_build = x
1875 if a_build and b_build and c_build and d_build:
1876 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001877
1878 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001879 self.assertIsNotNone(a_zref)
1880 self.assertIsNotNone(b_zref)
1881 self.assertIsNotNone(c_zref)
1882 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001883
1884 # And they should all be different
1885 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001886 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001887
Clint Byrum3343e3e2016-11-15 16:05:03 -08001888 # should have a, not b, and should not be in project2
1889 self.assertTrue(a_build.hasChanges(A))
1890 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001891
Clint Byrum3343e3e2016-11-15 16:05:03 -08001892 # should have a and b, and should not be in project2
1893 self.assertTrue(b_build.hasChanges(A, B))
1894 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001895
Clint Byrum3343e3e2016-11-15 16:05:03 -08001896 # should have a and b in 1, c in 2
1897 self.assertTrue(c_build.hasChanges(A, B, C))
1898 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001899
Clint Byrum3343e3e2016-11-15 16:05:03 -08001900 # should have a and b in 1, c and d in 2
1901 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001902
James E. Blair08d19992016-08-10 15:25:31 -07001903 self.launch_server.hold_jobs_in_build = False
1904 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001905 self.waitUntilSettled()
1906
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001907 self.assertEqual(A.data['status'], 'MERGED')
1908 self.assertEqual(A.reported, 2)
1909 self.assertEqual(B.data['status'], 'MERGED')
1910 self.assertEqual(B.reported, 2)
1911 self.assertEqual(C.data['status'], 'MERGED')
1912 self.assertEqual(C.reported, 2)
1913 self.assertEqual(D.data['status'], 'MERGED')
1914 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001915
James E. Blair4a28a882013-08-23 15:17:33 -07001916 def test_rerun_on_error(self):
1917 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07001918 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07001919 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001920 A.addApproval('code-review', 2)
1921 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07001922 self.waitUntilSettled()
1923
James E. Blair412fba82017-01-26 15:00:50 -08001924 self.builds[0].requeue = True
James E. Blair08d19992016-08-10 15:25:31 -07001925 self.launch_server.hold_jobs_in_build = False
1926 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07001927 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08001928 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07001929 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1930
James E. Blair412e5582013-04-22 15:50:12 -07001931 def test_statsd(self):
1932 "Test each of the statsd methods used in the scheduler"
1933 import extras
1934 statsd = extras.try_import('statsd.statsd')
1935 statsd.incr('test-incr')
1936 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001937 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001938 self.assertReportedStat('test-incr', '1|c')
1939 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001940 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001941
James E. Blairec056492016-07-22 09:45:56 -07001942 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08001943 def test_stuck_job_cleanup(self):
1944 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001945 # This job won't be registered at startup because it is not in
1946 # the standard layout, but we need it to already be registerd
1947 # for when we reconfigure, as that is when Zuul will attempt
1948 # to run the new job.
1949 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001950 self.gearman_server.hold_jobs_in_queue = True
1951 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001952 A.addApproval('code-review', 2)
1953 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08001954 self.waitUntilSettled()
1955 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1956
James E. Blairf84026c2015-12-08 16:11:46 -08001957 self.updateConfigLayout(
1958 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08001959 self.sched.reconfigure(self.config)
1960 self.waitUntilSettled()
1961
James E. Blair18c64442014-03-18 10:14:45 -07001962 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001963 self.waitUntilSettled()
1964 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1965 self.assertTrue(self.sched._areAllBuildsComplete())
1966
1967 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001968 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001969 self.assertEqual(self.history[0].result, 'SUCCESS')
1970
James E. Blair879dafb2015-07-17 14:04:49 -07001971 def test_file_head(self):
1972 # This is a regression test for an observed bug. A change
1973 # with a file named "HEAD" in the root directory of the repo
1974 # was processed by a merger. It then was unable to reset the
1975 # repo because of:
1976 # GitCommandError: 'git reset --hard HEAD' returned
1977 # with exit code 128
1978 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
1979 # and filename
1980 # Use '--' to separate filenames from revisions'
1981
1982 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04001983 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07001984 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1985
1986 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1987 self.waitUntilSettled()
1988
1989 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1990 self.waitUntilSettled()
1991
1992 self.assertIn('Build succeeded', A.messages[0])
1993 self.assertIn('Build succeeded', B.messages[0])
1994
James E. Blair70c71582013-03-06 08:50:50 -08001995 def test_file_jobs(self):
1996 "Test that file jobs run only when appropriate"
1997 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08001998 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08001999 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002000 A.addApproval('code-review', 2)
2001 B.addApproval('code-review', 2)
2002 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2003 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002004 self.waitUntilSettled()
2005
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002006 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002007 if x.name == 'project-testfile']
2008
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(len(testfile_jobs), 1)
2010 self.assertEqual(testfile_jobs[0].changes, '1,2')
2011 self.assertEqual(A.data['status'], 'MERGED')
2012 self.assertEqual(A.reported, 2)
2013 self.assertEqual(B.data['status'], 'MERGED')
2014 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002015
Clint Byrum3ee39f32016-11-17 23:45:07 -08002016 def _test_irrelevant_files_jobs(self, should_skip):
2017 "Test that jobs with irrelevant-files filter run only when appropriate"
2018 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002019 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002020
2021 if should_skip:
2022 files = {'ignoreme': 'ignored\n'}
2023 else:
2024 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002025
2026 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002027 'master',
2028 'test irrelevant-files',
2029 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002030 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2031 self.waitUntilSettled()
2032
2033 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002034 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002035
2036 if should_skip:
2037 self.assertEqual([], tested_change_ids)
2038 else:
2039 self.assertIn(change.data['number'], tested_change_ids)
2040
Clint Byrum3ee39f32016-11-17 23:45:07 -08002041 def test_irrelevant_files_match_skips_job(self):
2042 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002043
Clint Byrum3ee39f32016-11-17 23:45:07 -08002044 def test_irrelevant_files_no_match_runs_job(self):
2045 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002046
Clint Byrum85493602016-11-18 11:59:47 -08002047 def test_inherited_jobs_keep_matchers(self):
2048 self.updateConfigLayout('layout-inheritance')
2049 self.sched.reconfigure(self.config)
2050
2051 files = {'ignoreme': 'ignored\n'}
2052
2053 change = self.fake_gerrit.addFakeChange('org/project',
2054 'master',
2055 'test irrelevant-files',
2056 files=files)
2057 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2058 self.waitUntilSettled()
2059
2060 run_jobs = set([build.name for build in self.history])
2061
2062 self.assertEqual(set(['project-test-nomatch-starts-empty',
2063 'project-test-nomatch-starts-full']), run_jobs)
2064
James E. Blairec056492016-07-22 09:45:56 -07002065 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002066 def test_test_config(self):
2067 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002068 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002069 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002070
James E. Blairec056492016-07-22 09:45:56 -07002071 @skip("Disabled for early v3 development")
James E. Blairc8a1e052014-02-25 09:29:26 -08002072 def test_queue_names(self):
2073 "Test shared change queue names"
2074 project1 = self.sched.layout.projects['org/project1']
2075 project2 = self.sched.layout.projects['org/project2']
2076 q1 = self.sched.layout.pipelines['gate'].getQueue(project1)
2077 q2 = self.sched.layout.pipelines['gate'].getQueue(project2)
2078 self.assertEqual(q1.name, 'integration')
2079 self.assertEqual(q2.name, 'integration')
2080
James E. Blairf84026c2015-12-08 16:11:46 -08002081 self.updateConfigLayout(
2082 'tests/fixtures/layout-bad-queue.yaml')
James E. Blairc8a1e052014-02-25 09:29:26 -08002083 with testtools.ExpectedException(
2084 Exception, "More than one name assigned to change queue"):
2085 self.sched.reconfigure(self.config)
2086
James E. Blair64ed6f22013-07-10 14:07:23 -07002087 def test_queue_precedence(self):
2088 "Test that queue precedence works"
2089
2090 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002091 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002092 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2093 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002094 A.addApproval('code-review', 2)
2095 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002096
2097 self.waitUntilSettled()
2098 self.gearman_server.hold_jobs_in_queue = False
2099 self.gearman_server.release()
2100 self.waitUntilSettled()
2101
James E. Blair8de58bd2013-07-18 16:23:33 -07002102 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002103 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002104 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002105 self.waitUntilSettled()
2106
James E. Blair64ed6f22013-07-10 14:07:23 -07002107 self.log.debug(self.history)
2108 self.assertEqual(self.history[0].pipeline, 'gate')
2109 self.assertEqual(self.history[1].pipeline, 'check')
2110 self.assertEqual(self.history[2].pipeline, 'gate')
2111 self.assertEqual(self.history[3].pipeline, 'gate')
2112 self.assertEqual(self.history[4].pipeline, 'check')
2113 self.assertEqual(self.history[5].pipeline, 'check')
2114
James E. Blairec056492016-07-22 09:45:56 -07002115 @skip("Disabled for early v3 development")
Clark Boylana5edbe42014-06-03 16:39:10 -07002116 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002117 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002118 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002120 A.addApproval('code-review', 2)
2121 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002122 self.waitUntilSettled()
2123
James E. Blair08d19992016-08-10 15:25:31 -07002124 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002125 self.waitUntilSettled()
2126
James E. Blair1843a552013-07-03 14:19:52 -07002127 port = self.webapp.server.socket.getsockname()[1]
2128
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002129 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2130 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002131 headers = f.info()
2132 self.assertIn('Content-Length', headers)
2133 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002134 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2135 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002136 self.assertIn('Access-Control-Allow-Origin', headers)
2137 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002138 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002139 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002140 data = f.read()
2141
James E. Blair08d19992016-08-10 15:25:31 -07002142 self.launch_server.hold_jobs_in_build = False
2143 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002144 self.waitUntilSettled()
2145
2146 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002147 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002148 for p in data['pipelines']:
2149 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002150 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002151 self.assertEqual(q['window'], 20)
2152 else:
2153 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002154 for head in q['heads']:
2155 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002156 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002157 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002158 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002159 status_jobs.append(job)
2160 self.assertEqual('project-merge', status_jobs[0]['name'])
2161 self.assertEqual('https://server/job/project-merge/0/',
2162 status_jobs[0]['url'])
2163 self.assertEqual('http://logs.example.com/1/1/gate/project-merge/0',
2164 status_jobs[0]['report_url'])
2165
2166 self.assertEqual('project-test1', status_jobs[1]['name'])
2167 self.assertEqual('https://server/job/project-test1/1/',
2168 status_jobs[1]['url'])
2169 self.assertEqual('http://logs.example.com/1/1/gate/project-test1/1',
2170 status_jobs[1]['report_url'])
2171
2172 self.assertEqual('project-test2', status_jobs[2]['name'])
2173 self.assertEqual('https://server/job/project-test2/2/',
2174 status_jobs[2]['url'])
2175 self.assertEqual('http://logs.example.com/1/1/gate/project-test2/2',
2176 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002177
James E. Blairec056492016-07-22 09:45:56 -07002178 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002179 def test_merging_queues(self):
2180 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002181 self.updateConfigLayout(
2182 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002183 self.sched.reconfigure(self.config)
2184 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2185
James E. Blairec056492016-07-22 09:45:56 -07002186 @skip("Disabled for early v3 development")
James E. Blairaf17a972016-02-03 15:07:18 -08002187 def test_mutex(self):
2188 "Test job mutexes"
2189 self.config.set('zuul', 'layout_config',
2190 'tests/fixtures/layout-mutex.yaml')
2191 self.sched.reconfigure(self.config)
2192
James E. Blair08d19992016-08-10 15:25:31 -07002193 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2195 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2196 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2197
2198 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2200 self.waitUntilSettled()
2201 self.assertEqual(len(self.builds), 3)
2202 self.assertEqual(self.builds[0].name, 'project-test1')
2203 self.assertEqual(self.builds[1].name, 'mutex-one')
2204 self.assertEqual(self.builds[2].name, 'project-test1')
2205
James E. Blair08d19992016-08-10 15:25:31 -07002206 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002207 self.waitUntilSettled()
2208
2209 self.assertEqual(len(self.builds), 3)
2210 self.assertEqual(self.builds[0].name, 'project-test1')
2211 self.assertEqual(self.builds[1].name, 'project-test1')
2212 self.assertEqual(self.builds[2].name, 'mutex-two')
2213 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2214
James E. Blair08d19992016-08-10 15:25:31 -07002215 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002216 self.waitUntilSettled()
2217
2218 self.assertEqual(len(self.builds), 3)
2219 self.assertEqual(self.builds[0].name, 'project-test1')
2220 self.assertEqual(self.builds[1].name, 'project-test1')
2221 self.assertEqual(self.builds[2].name, 'mutex-one')
2222 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2223
James E. Blair08d19992016-08-10 15:25:31 -07002224 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002225 self.waitUntilSettled()
2226
2227 self.assertEqual(len(self.builds), 3)
2228 self.assertEqual(self.builds[0].name, 'project-test1')
2229 self.assertEqual(self.builds[1].name, 'project-test1')
2230 self.assertEqual(self.builds[2].name, 'mutex-two')
2231 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2232
James E. Blair08d19992016-08-10 15:25:31 -07002233 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002234 self.waitUntilSettled()
2235
2236 self.assertEqual(len(self.builds), 2)
2237 self.assertEqual(self.builds[0].name, 'project-test1')
2238 self.assertEqual(self.builds[1].name, 'project-test1')
2239 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2240
James E. Blair08d19992016-08-10 15:25:31 -07002241 self.launch_server.hold_jobs_in_build = False
2242 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002243
2244 self.waitUntilSettled()
2245 self.assertEqual(len(self.builds), 0)
2246
2247 self.assertEqual(A.reported, 1)
2248 self.assertEqual(B.reported, 1)
2249 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2250
James E. Blaircdccd972013-07-01 12:10:22 -07002251 def test_live_reconfiguration(self):
2252 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002253 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002255 A.addApproval('code-review', 2)
2256 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002257 self.waitUntilSettled()
2258
2259 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002260 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002261
James E. Blair08d19992016-08-10 15:25:31 -07002262 self.launch_server.hold_jobs_in_build = False
2263 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002264 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002265 self.assertEqual(self.getJobFromHistory('project-merge').result,
2266 'SUCCESS')
2267 self.assertEqual(self.getJobFromHistory('project-test1').result,
2268 'SUCCESS')
2269 self.assertEqual(self.getJobFromHistory('project-test2').result,
2270 'SUCCESS')
2271 self.assertEqual(A.data['status'], 'MERGED')
2272 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002273
James E. Blairec056492016-07-22 09:45:56 -07002274 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002275 def test_live_reconfiguration_merge_conflict(self):
2276 # A real-world bug: a change in a gate queue has a merge
2277 # conflict and a job is added to its project while it's
2278 # sitting in the queue. The job gets added to the change and
2279 # enqueued and the change gets stuck.
2280 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002281 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002282
2283 # This change is fine. It's here to stop the queue long
2284 # enough for the next change to be subject to the
2285 # reconfiguration, as well as to provide a conflict for the
2286 # next change. This change will succeed and merge.
2287 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2288 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002289 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002290
2291 # This change will be in merge conflict. During the
2292 # reconfiguration, we will add a job. We want to make sure
2293 # that doesn't cause it to get stuck.
2294 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2295 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002296 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002297
James E. Blair8b5408c2016-08-08 15:37:46 -07002298 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2299 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002300
2301 self.waitUntilSettled()
2302
2303 # No jobs have run yet
2304 self.assertEqual(A.data['status'], 'NEW')
2305 self.assertEqual(A.reported, 1)
2306 self.assertEqual(B.data['status'], 'NEW')
2307 self.assertEqual(B.reported, 1)
2308 self.assertEqual(len(self.history), 0)
2309
2310 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002311 self.updateConfigLayout(
2312 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002313 self.sched.reconfigure(self.config)
2314 self.waitUntilSettled()
2315
James E. Blair08d19992016-08-10 15:25:31 -07002316 self.launch_server.hold_jobs_in_build = False
2317 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002318 self.waitUntilSettled()
2319
2320 self.assertEqual(A.data['status'], 'MERGED')
2321 self.assertEqual(A.reported, 2)
2322 self.assertEqual(B.data['status'], 'NEW')
2323 self.assertEqual(B.reported, 2)
2324 self.assertEqual(self.getJobFromHistory('project-merge').result,
2325 'SUCCESS')
2326 self.assertEqual(self.getJobFromHistory('project-test1').result,
2327 'SUCCESS')
2328 self.assertEqual(self.getJobFromHistory('project-test2').result,
2329 'SUCCESS')
2330 self.assertEqual(self.getJobFromHistory('project-test3').result,
2331 'SUCCESS')
2332 self.assertEqual(len(self.history), 4)
2333
James E. Blairec056492016-07-22 09:45:56 -07002334 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002335 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002336 # An extrapolation of test_live_reconfiguration_merge_conflict
2337 # that tests a job added to a job tree with a failed root does
2338 # not run.
2339 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002340 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002341
2342 # This change is fine. It's here to stop the queue long
2343 # enough for the next change to be subject to the
2344 # reconfiguration. This change will succeed and merge.
2345 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2346 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002347 A.addApproval('code-review', 2)
2348 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002349 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002350 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002351 self.waitUntilSettled()
2352
2353 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002354 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002355 B.addApproval('code-review', 2)
2356 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002357 self.waitUntilSettled()
2358
James E. Blair08d19992016-08-10 15:25:31 -07002359 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002360 self.waitUntilSettled()
2361
2362 # Both -merge jobs have run, but no others.
2363 self.assertEqual(A.data['status'], 'NEW')
2364 self.assertEqual(A.reported, 1)
2365 self.assertEqual(B.data['status'], 'NEW')
2366 self.assertEqual(B.reported, 1)
2367 self.assertEqual(self.history[0].result, 'SUCCESS')
2368 self.assertEqual(self.history[0].name, 'project-merge')
2369 self.assertEqual(self.history[1].result, 'FAILURE')
2370 self.assertEqual(self.history[1].name, 'project-merge')
2371 self.assertEqual(len(self.history), 2)
2372
2373 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002374 self.updateConfigLayout(
2375 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002376 self.sched.reconfigure(self.config)
2377 self.waitUntilSettled()
2378
James E. Blair08d19992016-08-10 15:25:31 -07002379 self.launch_server.hold_jobs_in_build = False
2380 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002381 self.waitUntilSettled()
2382
2383 self.assertEqual(A.data['status'], 'MERGED')
2384 self.assertEqual(A.reported, 2)
2385 self.assertEqual(B.data['status'], 'NEW')
2386 self.assertEqual(B.reported, 2)
2387 self.assertEqual(self.history[0].result, 'SUCCESS')
2388 self.assertEqual(self.history[0].name, 'project-merge')
2389 self.assertEqual(self.history[1].result, 'FAILURE')
2390 self.assertEqual(self.history[1].name, 'project-merge')
2391 self.assertEqual(self.history[2].result, 'SUCCESS')
2392 self.assertEqual(self.history[3].result, 'SUCCESS')
2393 self.assertEqual(self.history[4].result, 'SUCCESS')
2394 self.assertEqual(len(self.history), 5)
2395
James E. Blairec056492016-07-22 09:45:56 -07002396 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002397 def test_live_reconfiguration_failed_job(self):
2398 # Test that a change with a removed failing job does not
2399 # disrupt reconfiguration. If a change has a failed job and
2400 # that job is removed during a reconfiguration, we observed a
2401 # bug where the code to re-set build statuses would run on
2402 # that build and raise an exception because the job no longer
2403 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002404 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002405
2406 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2407
2408 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002409 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002410
2411 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2412 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002413 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002414 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002415 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002416 self.waitUntilSettled()
2417
2418 self.assertEqual(A.data['status'], 'NEW')
2419 self.assertEqual(A.reported, 0)
2420
2421 self.assertEqual(self.getJobFromHistory('project-merge').result,
2422 'SUCCESS')
2423 self.assertEqual(self.getJobFromHistory('project-test1').result,
2424 'FAILURE')
2425 self.assertEqual(len(self.history), 2)
2426
2427 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002428 self.updateConfigLayout(
2429 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002430 self.sched.reconfigure(self.config)
2431 self.waitUntilSettled()
2432
James E. Blair08d19992016-08-10 15:25:31 -07002433 self.launch_server.hold_jobs_in_build = False
2434 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002435 self.waitUntilSettled()
2436
2437 self.assertEqual(self.getJobFromHistory('project-test2').result,
2438 'SUCCESS')
2439 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2440 'SUCCESS')
2441 self.assertEqual(len(self.history), 4)
2442
2443 self.assertEqual(A.data['status'], 'NEW')
2444 self.assertEqual(A.reported, 1)
2445 self.assertIn('Build succeeded', A.messages[0])
2446 # Ensure the removed job was not included in the report.
2447 self.assertNotIn('project-test1', A.messages[0])
2448
James E. Blairec056492016-07-22 09:45:56 -07002449 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002450 def test_live_reconfiguration_shared_queue(self):
2451 # Test that a change with a failing job which was removed from
2452 # this project but otherwise still exists in the system does
2453 # not disrupt reconfiguration.
2454
James E. Blair08d19992016-08-10 15:25:31 -07002455 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002456
2457 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2458
James E. Blair08d19992016-08-10 15:25:31 -07002459 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002460
2461 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2462 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002463 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002464 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002465 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002466 self.waitUntilSettled()
2467
2468 self.assertEqual(A.data['status'], 'NEW')
2469 self.assertEqual(A.reported, 0)
2470
2471 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2472 'SUCCESS')
2473 self.assertEqual(self.getJobFromHistory(
2474 'project1-project2-integration').result, 'FAILURE')
2475 self.assertEqual(len(self.history), 2)
2476
2477 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002478 self.updateConfigLayout(
2479 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002480 self.sched.reconfigure(self.config)
2481 self.waitUntilSettled()
2482
James E. Blair08d19992016-08-10 15:25:31 -07002483 self.launch_server.hold_jobs_in_build = False
2484 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002485 self.waitUntilSettled()
2486
2487 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2488 'SUCCESS')
2489 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2490 'SUCCESS')
2491 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2492 'SUCCESS')
2493 self.assertEqual(self.getJobFromHistory(
2494 'project1-project2-integration').result, 'FAILURE')
2495 self.assertEqual(len(self.history), 4)
2496
2497 self.assertEqual(A.data['status'], 'NEW')
2498 self.assertEqual(A.reported, 1)
2499 self.assertIn('Build succeeded', A.messages[0])
2500 # Ensure the removed job was not included in the report.
2501 self.assertNotIn('project1-project2-integration', A.messages[0])
2502
James E. Blairec056492016-07-22 09:45:56 -07002503 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002504 def test_double_live_reconfiguration_shared_queue(self):
2505 # This was a real-world regression. A change is added to
2506 # gate; a reconfigure happens, a second change which depends
2507 # on the first is added, and a second reconfiguration happens.
2508 # Ensure that both changes merge.
2509
2510 # A failure may indicate incorrect caching or cleaning up of
2511 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002512 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002513
2514 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2515 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2516 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002517 A.addApproval('code-review', 2)
2518 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002519
2520 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002521 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002522 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002523 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002524 self.waitUntilSettled()
2525
2526 # Reconfigure (with only one change in the pipeline).
2527 self.sched.reconfigure(self.config)
2528 self.waitUntilSettled()
2529
2530 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002531 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002532 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002533 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002534 self.waitUntilSettled()
2535
2536 # Reconfigure (with both in the pipeline).
2537 self.sched.reconfigure(self.config)
2538 self.waitUntilSettled()
2539
James E. Blair08d19992016-08-10 15:25:31 -07002540 self.launch_server.hold_jobs_in_build = False
2541 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002542 self.waitUntilSettled()
2543
2544 self.assertEqual(len(self.history), 8)
2545
2546 self.assertEqual(A.data['status'], 'MERGED')
2547 self.assertEqual(A.reported, 2)
2548 self.assertEqual(B.data['status'], 'MERGED')
2549 self.assertEqual(B.reported, 2)
2550
James E. Blairec056492016-07-22 09:45:56 -07002551 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002552 def test_live_reconfiguration_del_project(self):
2553 # Test project deletion from layout
2554 # while changes are enqueued
2555
James E. Blair08d19992016-08-10 15:25:31 -07002556 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2558 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2559 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2560
2561 # A Depends-On: B
2562 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2563 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002564 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002565
2566 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2567 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2568 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002569 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002570 self.waitUntilSettled()
2571 self.assertEqual(len(self.builds), 5)
2572
2573 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002574 self.updateConfigLayout(
2575 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002576 self.sched.reconfigure(self.config)
2577 self.waitUntilSettled()
2578
2579 # Builds for C aborted, builds for A succeed,
2580 # and have change B applied ahead
2581 job_c = self.getJobFromHistory('project1-test1')
2582 self.assertEqual(job_c.changes, '3,1')
2583 self.assertEqual(job_c.result, 'ABORTED')
2584
James E. Blair08d19992016-08-10 15:25:31 -07002585 self.launch_server.hold_jobs_in_build = False
2586 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002587 self.waitUntilSettled()
2588
2589 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2590 '2,1 1,1')
2591
2592 self.assertEqual(A.data['status'], 'NEW')
2593 self.assertEqual(B.data['status'], 'NEW')
2594 self.assertEqual(C.data['status'], 'NEW')
2595 self.assertEqual(A.reported, 1)
2596 self.assertEqual(B.reported, 0)
2597 self.assertEqual(C.reported, 0)
2598
2599 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2600 self.assertIn('Build succeeded', A.messages[0])
2601
James E. Blairec056492016-07-22 09:45:56 -07002602 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002603 def test_live_reconfiguration_functions(self):
2604 "Test live reconfiguration with a custom function"
2605 self.worker.registerFunction('build:node-project-test1:debian')
2606 self.worker.registerFunction('build:node-project-test1:wheezy')
2607 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002608 A.addApproval('code-review', 2)
2609 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002610 self.waitUntilSettled()
2611
2612 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2613 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2614 'debian')
2615 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2616
James E. Blairf84026c2015-12-08 16:11:46 -08002617 self.updateConfigLayout(
2618 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002619 self.sched.reconfigure(self.config)
2620 self.worker.build_history = []
2621
2622 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002623 B.addApproval('code-review', 2)
2624 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002625 self.waitUntilSettled()
2626
2627 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2628 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2629 'wheezy')
2630 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2631
James E. Blairec056492016-07-22 09:45:56 -07002632 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002633 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002634 self.updateConfigLayout(
2635 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002636 self.sched.reconfigure(self.config)
2637
2638 self.init_repo("org/new-project")
2639 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2640
James E. Blair8b5408c2016-08-08 15:37:46 -07002641 A.addApproval('code-review', 2)
2642 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002643 self.waitUntilSettled()
2644 self.assertEqual(self.getJobFromHistory('project-merge').result,
2645 'SUCCESS')
2646 self.assertEqual(self.getJobFromHistory('project-test1').result,
2647 'SUCCESS')
2648 self.assertEqual(self.getJobFromHistory('project-test2').result,
2649 'SUCCESS')
2650 self.assertEqual(A.data['status'], 'MERGED')
2651 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002652
Clark Boylan6dbbc482013-10-18 10:57:31 -07002653 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002654 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002655 self.sched.reconfigure(self.config)
2656
2657 self.init_repo("org/delete-project")
2658 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2659
James E. Blair8b5408c2016-08-08 15:37:46 -07002660 A.addApproval('code-review', 2)
2661 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002662 self.waitUntilSettled()
2663 self.assertEqual(self.getJobFromHistory('project-merge').result,
2664 'SUCCESS')
2665 self.assertEqual(self.getJobFromHistory('project-test1').result,
2666 'SUCCESS')
2667 self.assertEqual(self.getJobFromHistory('project-test2').result,
2668 'SUCCESS')
2669 self.assertEqual(A.data['status'], 'MERGED')
2670 self.assertEqual(A.reported, 2)
2671
2672 # Delete org/new-project zuul repo. Should be recloned.
2673 shutil.rmtree(os.path.join(self.git_root, "org/delete-project"))
2674
2675 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2676
James E. Blair8b5408c2016-08-08 15:37:46 -07002677 B.addApproval('code-review', 2)
2678 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002679 self.waitUntilSettled()
2680 self.assertEqual(self.getJobFromHistory('project-merge').result,
2681 'SUCCESS')
2682 self.assertEqual(self.getJobFromHistory('project-test1').result,
2683 'SUCCESS')
2684 self.assertEqual(self.getJobFromHistory('project-test2').result,
2685 'SUCCESS')
2686 self.assertEqual(B.data['status'], 'MERGED')
2687 self.assertEqual(B.reported, 2)
2688
James E. Blairec056492016-07-22 09:45:56 -07002689 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002690 def test_tags(self):
2691 "Test job tags"
2692 self.config.set('zuul', 'layout_config',
2693 'tests/fixtures/layout-tags.yaml')
2694 self.sched.reconfigure(self.config)
2695
2696 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2697 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2698 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2699 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2700 self.waitUntilSettled()
2701
2702 results = {'project1-merge': 'extratag merge project1',
2703 'project2-merge': 'merge'}
2704
2705 for build in self.history:
2706 self.assertEqual(results.get(build.name, ''),
2707 build.parameters.get('BUILD_TAGS'))
2708
James E. Blairec056492016-07-22 09:45:56 -07002709 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002710 def test_timer(self):
2711 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002712 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002713 self.updateConfigLayout(
2714 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002715 self.sched.reconfigure(self.config)
2716 self.registerJobs()
2717
Clark Boylan3ee090a2014-04-03 20:55:09 -07002718 # The pipeline triggers every second, so we should have seen
2719 # several by now.
2720 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002721 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002722
2723 self.assertEqual(len(self.builds), 2)
2724
James E. Blair63bb0ef2013-07-29 17:14:51 -07002725 port = self.webapp.server.socket.getsockname()[1]
2726
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002727 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2728 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002729 data = f.read()
2730
James E. Blair08d19992016-08-10 15:25:31 -07002731 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002732 # Stop queuing timer triggered jobs so that the assertions
2733 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002734 self.updateConfigLayout(
2735 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002736 self.sched.reconfigure(self.config)
2737 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002738 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002739 self.waitUntilSettled()
2740
2741 self.assertEqual(self.getJobFromHistory(
2742 'project-bitrot-stable-old').result, 'SUCCESS')
2743 self.assertEqual(self.getJobFromHistory(
2744 'project-bitrot-stable-older').result, 'SUCCESS')
2745
2746 data = json.loads(data)
2747 status_jobs = set()
2748 for p in data['pipelines']:
2749 for q in p['change_queues']:
2750 for head in q['heads']:
2751 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002752 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002753 for job in change['jobs']:
2754 status_jobs.add(job['name'])
2755 self.assertIn('project-bitrot-stable-old', status_jobs)
2756 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002757
James E. Blairec056492016-07-22 09:45:56 -07002758 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002759 def test_idle(self):
2760 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002761 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002762
Clark Boylan3ee090a2014-04-03 20:55:09 -07002763 for x in range(1, 3):
2764 # Test that timer triggers periodic jobs even across
2765 # layout config reloads.
2766 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002767 self.updateConfigLayout(
2768 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002769 self.sched.reconfigure(self.config)
2770 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002771 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002772
Clark Boylan3ee090a2014-04-03 20:55:09 -07002773 # The pipeline triggers every second, so we should have seen
2774 # several by now.
2775 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002776
2777 # Stop queuing timer triggered jobs so that the assertions
2778 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002779 self.updateConfigLayout(
2780 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002781 self.sched.reconfigure(self.config)
2782 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002783 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002784
2785 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002786 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002787 self.waitUntilSettled()
2788 self.assertEqual(len(self.builds), 0)
2789 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002790
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002791 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002792 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002793 self.sched.reconfigure(self.config)
2794
2795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2796 self.waitUntilSettled()
2797
2798 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2799 self.waitUntilSettled()
2800
James E. Blairff80a2f2013-12-27 13:24:06 -08002801 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002802
2803 # A.messages only holds what FakeGerrit places in it. Thus we
2804 # work on the knowledge of what the first message should be as
2805 # it is only configured to go to SMTP.
2806
2807 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002808 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002809 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002810 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002811 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002812 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002813
2814 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002815 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002816 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002817 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002818 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002819 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002820
James E. Blairec056492016-07-22 09:45:56 -07002821 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08002822 def test_timer_smtp(self):
2823 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002824 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002825 self.updateConfigLayout(
2826 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002827 self.sched.reconfigure(self.config)
2828 self.registerJobs()
2829
Clark Boylan3ee090a2014-04-03 20:55:09 -07002830 # The pipeline triggers every second, so we should have seen
2831 # several by now.
2832 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002833 self.waitUntilSettled()
2834
Clark Boylan3ee090a2014-04-03 20:55:09 -07002835 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002836 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002837 self.waitUntilSettled()
2838 self.assertEqual(len(self.history), 2)
2839
James E. Blaire5910202013-12-27 09:50:31 -08002840 self.assertEqual(self.getJobFromHistory(
2841 'project-bitrot-stable-old').result, 'SUCCESS')
2842 self.assertEqual(self.getJobFromHistory(
2843 'project-bitrot-stable-older').result, 'SUCCESS')
2844
James E. Blairff80a2f2013-12-27 13:24:06 -08002845 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002846
2847 # A.messages only holds what FakeGerrit places in it. Thus we
2848 # work on the knowledge of what the first message should be as
2849 # it is only configured to go to SMTP.
2850
2851 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002852 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002853 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002854 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002855 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002856 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002857
Clark Boylan3ee090a2014-04-03 20:55:09 -07002858 # Stop queuing timer triggered jobs and let any that may have
2859 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08002860 self.updateConfigLayout(
2861 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002862 self.sched.reconfigure(self.config)
2863 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07002864 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002865 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002866 self.waitUntilSettled()
2867
James E. Blair91e34592015-07-31 16:45:59 -07002868 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002869 "Test that the RPC client can enqueue a change"
2870 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002871 A.addApproval('code-review', 2)
2872 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002873
2874 client = zuul.rpcclient.RPCClient('127.0.0.1',
2875 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002876 r = client.enqueue(tenant='tenant-one',
2877 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002878 project='org/project',
2879 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002880 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002881 self.waitUntilSettled()
2882 self.assertEqual(self.getJobFromHistory('project-merge').result,
2883 'SUCCESS')
2884 self.assertEqual(self.getJobFromHistory('project-test1').result,
2885 'SUCCESS')
2886 self.assertEqual(self.getJobFromHistory('project-test2').result,
2887 'SUCCESS')
2888 self.assertEqual(A.data['status'], 'MERGED')
2889 self.assertEqual(A.reported, 2)
2890 self.assertEqual(r, True)
2891
James E. Blairec056492016-07-22 09:45:56 -07002892 @skip("Disabled for early v3 development")
James E. Blair91e34592015-07-31 16:45:59 -07002893 def test_client_enqueue_ref(self):
2894 "Test that the RPC client can enqueue a ref"
2895
2896 client = zuul.rpcclient.RPCClient('127.0.0.1',
2897 self.gearman_server.port)
2898 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002899 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002900 pipeline='post',
2901 project='org/project',
2902 trigger='gerrit',
2903 ref='master',
2904 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2905 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2906 self.waitUntilSettled()
2907 job_names = [x.name for x in self.history]
2908 self.assertEqual(len(self.history), 1)
2909 self.assertIn('project-post', job_names)
2910 self.assertEqual(r, True)
2911
James E. Blairad28e912013-11-27 10:43:22 -08002912 def test_client_enqueue_negative(self):
2913 "Test that the RPC client returns errors"
2914 client = zuul.rpcclient.RPCClient('127.0.0.1',
2915 self.gearman_server.port)
2916 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002917 "Invalid tenant"):
2918 r = client.enqueue(tenant='tenant-foo',
2919 pipeline='gate',
2920 project='org/project',
2921 trigger='gerrit',
2922 change='1,1')
2923 client.shutdown()
2924 self.assertEqual(r, False)
2925
2926 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002927 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002928 r = client.enqueue(tenant='tenant-one',
2929 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002930 project='project-does-not-exist',
2931 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002932 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002933 client.shutdown()
2934 self.assertEqual(r, False)
2935
2936 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2937 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002938 r = client.enqueue(tenant='tenant-one',
2939 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002940 project='org/project',
2941 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002942 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002943 client.shutdown()
2944 self.assertEqual(r, False)
2945
2946 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2947 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002948 r = client.enqueue(tenant='tenant-one',
2949 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002950 project='org/project',
2951 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002952 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002953 client.shutdown()
2954 self.assertEqual(r, False)
2955
2956 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2957 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002958 r = client.enqueue(tenant='tenant-one',
2959 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002960 project='org/project',
2961 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002962 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002963 client.shutdown()
2964 self.assertEqual(r, False)
2965
2966 self.waitUntilSettled()
2967 self.assertEqual(len(self.history), 0)
2968 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002969
2970 def test_client_promote(self):
2971 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07002972 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08002973 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2974 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2975 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07002976 A.addApproval('code-review', 2)
2977 B.addApproval('code-review', 2)
2978 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08002979
James E. Blair8b5408c2016-08-08 15:37:46 -07002980 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2981 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2982 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08002983
2984 self.waitUntilSettled()
2985
Paul Belangerbaca3132016-11-04 12:49:54 -04002986 tenant = self.sched.abide.tenants.get('tenant-one')
2987 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05002988 enqueue_times = {}
2989 for item in items:
2990 enqueue_times[str(item.change)] = item.enqueue_time
2991
James E. Blair36658cf2013-12-06 17:53:48 -08002992 client = zuul.rpcclient.RPCClient('127.0.0.1',
2993 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002994 r = client.promote(tenant='tenant-one',
2995 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08002996 change_ids=['2,1', '3,1'])
2997
Sean Daguef39b9ca2014-01-10 21:34:35 -05002998 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04002999 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003000 for item in items:
3001 self.assertEqual(
3002 enqueue_times[str(item.change)], item.enqueue_time)
3003
James E. Blair78acec92014-02-06 07:11:32 -08003004 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003005 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003006 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003007 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003008 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003009 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003010 self.waitUntilSettled()
3011
3012 self.assertEqual(len(self.builds), 6)
3013 self.assertEqual(self.builds[0].name, 'project-test1')
3014 self.assertEqual(self.builds[1].name, 'project-test2')
3015 self.assertEqual(self.builds[2].name, 'project-test1')
3016 self.assertEqual(self.builds[3].name, 'project-test2')
3017 self.assertEqual(self.builds[4].name, 'project-test1')
3018 self.assertEqual(self.builds[5].name, 'project-test2')
3019
Paul Belangerbaca3132016-11-04 12:49:54 -04003020 self.assertTrue(self.builds[0].hasChanges(B))
3021 self.assertFalse(self.builds[0].hasChanges(A))
3022 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003023
Paul Belangerbaca3132016-11-04 12:49:54 -04003024 self.assertTrue(self.builds[2].hasChanges(B))
3025 self.assertTrue(self.builds[2].hasChanges(C))
3026 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003027
Paul Belangerbaca3132016-11-04 12:49:54 -04003028 self.assertTrue(self.builds[4].hasChanges(B))
3029 self.assertTrue(self.builds[4].hasChanges(C))
3030 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003031
James E. Blair08d19992016-08-10 15:25:31 -07003032 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003033 self.waitUntilSettled()
3034
3035 self.assertEqual(A.data['status'], 'MERGED')
3036 self.assertEqual(A.reported, 2)
3037 self.assertEqual(B.data['status'], 'MERGED')
3038 self.assertEqual(B.reported, 2)
3039 self.assertEqual(C.data['status'], 'MERGED')
3040 self.assertEqual(C.reported, 2)
3041
3042 client.shutdown()
3043 self.assertEqual(r, True)
3044
3045 def test_client_promote_dependent(self):
3046 "Test that the RPC client can promote a dependent change"
3047 # C (depends on B) -> B -> A ; then promote C to get:
3048 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003049 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3051 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3052 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3053
3054 C.setDependsOn(B, 1)
3055
James E. Blair8b5408c2016-08-08 15:37:46 -07003056 A.addApproval('code-review', 2)
3057 B.addApproval('code-review', 2)
3058 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003059
James E. Blair8b5408c2016-08-08 15:37:46 -07003060 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3061 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3062 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003063
3064 self.waitUntilSettled()
3065
3066 client = zuul.rpcclient.RPCClient('127.0.0.1',
3067 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003068 r = client.promote(tenant='tenant-one',
3069 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003070 change_ids=['3,1'])
3071
James E. Blair78acec92014-02-06 07:11:32 -08003072 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003073 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003074 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003075 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003076 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003077 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003078 self.waitUntilSettled()
3079
3080 self.assertEqual(len(self.builds), 6)
3081 self.assertEqual(self.builds[0].name, 'project-test1')
3082 self.assertEqual(self.builds[1].name, 'project-test2')
3083 self.assertEqual(self.builds[2].name, 'project-test1')
3084 self.assertEqual(self.builds[3].name, 'project-test2')
3085 self.assertEqual(self.builds[4].name, 'project-test1')
3086 self.assertEqual(self.builds[5].name, 'project-test2')
3087
Paul Belangerbaca3132016-11-04 12:49:54 -04003088 self.assertTrue(self.builds[0].hasChanges(B))
3089 self.assertFalse(self.builds[0].hasChanges(A))
3090 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003091
Paul Belangerbaca3132016-11-04 12:49:54 -04003092 self.assertTrue(self.builds[2].hasChanges(B))
3093 self.assertTrue(self.builds[2].hasChanges(C))
3094 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003095
Paul Belangerbaca3132016-11-04 12:49:54 -04003096 self.assertTrue(self.builds[4].hasChanges(B))
3097 self.assertTrue(self.builds[4].hasChanges(C))
3098 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003099
James E. Blair08d19992016-08-10 15:25:31 -07003100 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003101 self.waitUntilSettled()
3102
3103 self.assertEqual(A.data['status'], 'MERGED')
3104 self.assertEqual(A.reported, 2)
3105 self.assertEqual(B.data['status'], 'MERGED')
3106 self.assertEqual(B.reported, 2)
3107 self.assertEqual(C.data['status'], 'MERGED')
3108 self.assertEqual(C.reported, 2)
3109
3110 client.shutdown()
3111 self.assertEqual(r, True)
3112
3113 def test_client_promote_negative(self):
3114 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003115 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003116 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003117 A.addApproval('code-review', 2)
3118 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003119 self.waitUntilSettled()
3120
3121 client = zuul.rpcclient.RPCClient('127.0.0.1',
3122 self.gearman_server.port)
3123
3124 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003125 r = client.promote(tenant='tenant-one',
3126 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003127 change_ids=['2,1', '3,1'])
3128 client.shutdown()
3129 self.assertEqual(r, False)
3130
3131 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003132 r = client.promote(tenant='tenant-one',
3133 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003134 change_ids=['4,1'])
3135 client.shutdown()
3136 self.assertEqual(r, False)
3137
James E. Blair08d19992016-08-10 15:25:31 -07003138 self.launch_server.hold_jobs_in_build = False
3139 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003140 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003141
James E. Blairec056492016-07-22 09:45:56 -07003142 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003143 def test_queue_rate_limiting(self):
3144 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003145 self.updateConfigLayout(
3146 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003147 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003148 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003149 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3150 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3151 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3152
3153 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003154 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003155
James E. Blair8b5408c2016-08-08 15:37:46 -07003156 A.addApproval('code-review', 2)
3157 B.addApproval('code-review', 2)
3158 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003159
James E. Blair8b5408c2016-08-08 15:37:46 -07003160 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3161 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3162 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003163 self.waitUntilSettled()
3164
3165 # Only A and B will have their merge jobs queued because
3166 # window is 2.
3167 self.assertEqual(len(self.builds), 2)
3168 self.assertEqual(self.builds[0].name, 'project-merge')
3169 self.assertEqual(self.builds[1].name, 'project-merge')
3170
James E. Blair08d19992016-08-10 15:25:31 -07003171 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003172 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003173 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003174 self.waitUntilSettled()
3175
3176 # Only A and B will have their test jobs queued because
3177 # window is 2.
3178 self.assertEqual(len(self.builds), 4)
3179 self.assertEqual(self.builds[0].name, 'project-test1')
3180 self.assertEqual(self.builds[1].name, 'project-test2')
3181 self.assertEqual(self.builds[2].name, 'project-test1')
3182 self.assertEqual(self.builds[3].name, 'project-test2')
3183
James E. Blair08d19992016-08-10 15:25:31 -07003184 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003185 self.waitUntilSettled()
3186
3187 queue = self.sched.layout.pipelines['gate'].queues[0]
3188 # A failed so window is reduced by 1 to 1.
3189 self.assertEqual(queue.window, 1)
3190 self.assertEqual(queue.window_floor, 1)
3191 self.assertEqual(A.data['status'], 'NEW')
3192
3193 # Gate is reset and only B's merge job is queued because
3194 # window shrunk to 1.
3195 self.assertEqual(len(self.builds), 1)
3196 self.assertEqual(self.builds[0].name, 'project-merge')
3197
James E. Blair08d19992016-08-10 15:25:31 -07003198 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003199 self.waitUntilSettled()
3200
3201 # Only B's test jobs are queued because window is still 1.
3202 self.assertEqual(len(self.builds), 2)
3203 self.assertEqual(self.builds[0].name, 'project-test1')
3204 self.assertEqual(self.builds[1].name, 'project-test2')
3205
James E. Blair08d19992016-08-10 15:25:31 -07003206 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003207 self.waitUntilSettled()
3208
3209 # B was successfully merged so window is increased to 2.
3210 self.assertEqual(queue.window, 2)
3211 self.assertEqual(queue.window_floor, 1)
3212 self.assertEqual(B.data['status'], 'MERGED')
3213
3214 # Only C is left and its merge job is queued.
3215 self.assertEqual(len(self.builds), 1)
3216 self.assertEqual(self.builds[0].name, 'project-merge')
3217
James E. Blair08d19992016-08-10 15:25:31 -07003218 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003219 self.waitUntilSettled()
3220
3221 # After successful merge job the test jobs for C are queued.
3222 self.assertEqual(len(self.builds), 2)
3223 self.assertEqual(self.builds[0].name, 'project-test1')
3224 self.assertEqual(self.builds[1].name, 'project-test2')
3225
James E. Blair08d19992016-08-10 15:25:31 -07003226 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003227 self.waitUntilSettled()
3228
3229 # C successfully merged so window is bumped to 3.
3230 self.assertEqual(queue.window, 3)
3231 self.assertEqual(queue.window_floor, 1)
3232 self.assertEqual(C.data['status'], 'MERGED')
3233
James E. Blairec056492016-07-22 09:45:56 -07003234 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003235 def test_queue_rate_limiting_dependent(self):
3236 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003237 self.updateConfigLayout(
3238 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003239 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003240 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -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')
3244
3245 B.setDependsOn(A, 1)
3246
James E. Blair08d19992016-08-10 15:25:31 -07003247 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003248
James E. Blair8b5408c2016-08-08 15:37:46 -07003249 A.addApproval('code-review', 2)
3250 B.addApproval('code-review', 2)
3251 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003252
James E. Blair8b5408c2016-08-08 15:37:46 -07003253 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3254 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3255 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003256 self.waitUntilSettled()
3257
3258 # Only A and B will have their merge jobs queued because
3259 # window is 2.
3260 self.assertEqual(len(self.builds), 2)
3261 self.assertEqual(self.builds[0].name, 'project-merge')
3262 self.assertEqual(self.builds[1].name, 'project-merge')
3263
James E. Blair08d19992016-08-10 15:25:31 -07003264 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003265 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003266 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003267 self.waitUntilSettled()
3268
3269 # Only A and B will have their test jobs queued because
3270 # window is 2.
3271 self.assertEqual(len(self.builds), 4)
3272 self.assertEqual(self.builds[0].name, 'project-test1')
3273 self.assertEqual(self.builds[1].name, 'project-test2')
3274 self.assertEqual(self.builds[2].name, 'project-test1')
3275 self.assertEqual(self.builds[3].name, 'project-test2')
3276
James E. Blair08d19992016-08-10 15:25:31 -07003277 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003278 self.waitUntilSettled()
3279
3280 queue = self.sched.layout.pipelines['gate'].queues[0]
3281 # A failed so window is reduced by 1 to 1.
3282 self.assertEqual(queue.window, 1)
3283 self.assertEqual(queue.window_floor, 1)
3284 self.assertEqual(A.data['status'], 'NEW')
3285 self.assertEqual(B.data['status'], 'NEW')
3286
3287 # Gate is reset and only C's merge job is queued because
3288 # window shrunk to 1 and A and B were dequeued.
3289 self.assertEqual(len(self.builds), 1)
3290 self.assertEqual(self.builds[0].name, 'project-merge')
3291
James E. Blair08d19992016-08-10 15:25:31 -07003292 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003293 self.waitUntilSettled()
3294
3295 # Only C's test jobs are queued because window is still 1.
3296 self.assertEqual(len(self.builds), 2)
3297 self.assertEqual(self.builds[0].name, 'project-test1')
3298 self.assertEqual(self.builds[1].name, 'project-test2')
3299
James E. Blair08d19992016-08-10 15:25:31 -07003300 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003301 self.waitUntilSettled()
3302
3303 # C was successfully merged so window is increased to 2.
3304 self.assertEqual(queue.window, 2)
3305 self.assertEqual(queue.window_floor, 1)
3306 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003307
James E. Blairec056492016-07-22 09:45:56 -07003308 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003309 def test_worker_update_metadata(self):
3310 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003311 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003312
3313 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003314 A.addApproval('code-review', 2)
3315 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003316 self.waitUntilSettled()
3317
3318 self.assertEqual(len(self.launcher.builds), 1)
3319
3320 self.log.debug('Current builds:')
3321 self.log.debug(self.launcher.builds)
3322
3323 start = time.time()
3324 while True:
3325 if time.time() - start > 10:
3326 raise Exception("Timeout waiting for gearman server to report "
3327 + "back to the client")
3328 build = self.launcher.builds.values()[0]
3329 if build.worker.name == "My Worker":
3330 break
3331 else:
3332 time.sleep(0)
3333
3334 self.log.debug(build)
3335 self.assertEqual("My Worker", build.worker.name)
3336 self.assertEqual("localhost", build.worker.hostname)
3337 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3338 self.assertEqual("zuul.example.org", build.worker.fqdn)
3339 self.assertEqual("FakeBuilder", build.worker.program)
3340 self.assertEqual("v1.1", build.worker.version)
3341 self.assertEqual({'something': 'else'}, build.worker.extra)
3342
James E. Blair08d19992016-08-10 15:25:31 -07003343 self.launch_server.hold_jobs_in_build = False
3344 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003345 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003346
James E. Blairec056492016-07-22 09:45:56 -07003347 @skip("Disabled for early v3 development")
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003348 def test_footer_message(self):
3349 "Test a pipeline's footer message is correctly added to the report."
James E. Blairf84026c2015-12-08 16:11:46 -08003350 self.updateConfigLayout(
3351 'tests/fixtures/layout-footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003352 self.sched.reconfigure(self.config)
3353 self.registerJobs()
3354
3355 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003356 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003357 self.launch_server.failJob('test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003358 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003359 self.waitUntilSettled()
3360
3361 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003362 B.addApproval('code-review', 2)
3363 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003364 self.waitUntilSettled()
3365
3366 self.assertEqual(2, len(self.smtp_messages))
3367
3368 failure_body = """\
3369Build failed. For information on how to proceed, see \
3370http://wiki.example.org/Test_Failures
3371
3372- test1 http://logs.example.com/1/1/gate/test1/0 : FAILURE in 0s
3373- test2 http://logs.example.com/1/1/gate/test2/1 : SUCCESS in 0s
3374
3375For CI problems and help debugging, contact ci@example.org"""
3376
3377 success_body = """\
3378Build succeeded.
3379
3380- test1 http://logs.example.com/2/1/gate/test1/2 : SUCCESS in 0s
3381- test2 http://logs.example.com/2/1/gate/test2/3 : SUCCESS in 0s
3382
3383For CI problems and help debugging, contact ci@example.org"""
3384
3385 self.assertEqual(failure_body, self.smtp_messages[0]['body'])
3386 self.assertEqual(success_body, self.smtp_messages[1]['body'])
Joshua Heskethb7179772014-01-30 23:30:46 +11003387
James E. Blairec056492016-07-22 09:45:56 -07003388 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003389 def test_merge_failure_reporters(self):
3390 """Check that the config is set up correctly"""
3391
James E. Blairf84026c2015-12-08 16:11:46 -08003392 self.updateConfigLayout(
3393 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003394 self.sched.reconfigure(self.config)
3395 self.registerJobs()
3396
3397 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003398 "Merge Failed.\n\nThis change or one of its cross-repo "
3399 "dependencies was unable to be automatically merged with the "
3400 "current state of its repository. Please rebase the change and "
3401 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003402 self.sched.layout.pipelines['check'].merge_failure_message)
3403 self.assertEqual(
3404 "The merge failed! For more information...",
3405 self.sched.layout.pipelines['gate'].merge_failure_message)
3406
3407 self.assertEqual(
3408 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3409 self.assertEqual(
3410 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3411
3412 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003413 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3414 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003415
3416 self.assertTrue(
3417 (
3418 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003419 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003420 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003421 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003422 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003423 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003424 ) or (
3425 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003426 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003427 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003428 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003429 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003430 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003431 )
3432 )
3433
James E. Blairec056492016-07-22 09:45:56 -07003434 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003435 def test_merge_failure_reports(self):
3436 """Check that when a change fails to merge the correct message is sent
3437 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003438 self.updateConfigLayout(
3439 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003440 self.sched.reconfigure(self.config)
3441 self.registerJobs()
3442
3443 # Check a test failure isn't reported to SMTP
3444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003445 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003446 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003447 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003448 self.waitUntilSettled()
3449
3450 self.assertEqual(3, len(self.history)) # 3 jobs
3451 self.assertEqual(0, len(self.smtp_messages))
3452
3453 # Check a merge failure is reported to SMTP
3454 # B should be merged, but C will conflict with B
3455 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3456 B.addPatchset(['conflict'])
3457 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3458 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003459 B.addApproval('code-review', 2)
3460 C.addApproval('code-review', 2)
3461 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3462 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003463 self.waitUntilSettled()
3464
3465 self.assertEqual(6, len(self.history)) # A and B jobs
3466 self.assertEqual(1, len(self.smtp_messages))
3467 self.assertEqual('The merge failed! For more information...',
3468 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003469
James E. Blairec056492016-07-22 09:45:56 -07003470 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003471 def test_default_merge_failure_reports(self):
3472 """Check that the default merge failure reports are correct."""
3473
3474 # A should report success, B should report merge failure.
3475 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3476 A.addPatchset(['conflict'])
3477 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3478 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003479 A.addApproval('code-review', 2)
3480 B.addApproval('code-review', 2)
3481 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3482 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003483 self.waitUntilSettled()
3484
3485 self.assertEqual(3, len(self.history)) # A jobs
3486 self.assertEqual(A.reported, 2)
3487 self.assertEqual(B.reported, 2)
3488 self.assertEqual(A.data['status'], 'MERGED')
3489 self.assertEqual(B.data['status'], 'NEW')
3490 self.assertIn('Build succeeded', A.messages[1])
3491 self.assertIn('Merge Failed', B.messages[1])
3492 self.assertIn('automatically merged', B.messages[1])
3493 self.assertNotIn('logs.example.com', B.messages[1])
3494 self.assertNotIn('SKIPPED', B.messages[1])
3495
James E. Blairec056492016-07-22 09:45:56 -07003496 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003497 def test_swift_instructions(self):
3498 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003499 self.updateConfigLayout(
3500 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003501 self.sched.reconfigure(self.config)
3502 self.registerJobs()
3503
James E. Blair08d19992016-08-10 15:25:31 -07003504 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003505 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3506
James E. Blair8b5408c2016-08-08 15:37:46 -07003507 A.addApproval('code-review', 2)
3508 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003509 self.waitUntilSettled()
3510
3511 self.assertEqual(
3512 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3513 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003514 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003515 self.assertEqual(5,
3516 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3517 split('\n')))
3518 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3519
3520 self.assertEqual(
3521 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3522 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003523 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003524 self.assertEqual(5,
3525 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3526 split('\n')))
3527 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3528
3529 self.assertEqual(
3530 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3531 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003532 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003533 self.assertEqual(5,
3534 len(self.builds[1].
3535 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3536 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3537
James E. Blair08d19992016-08-10 15:25:31 -07003538 self.launch_server.hold_jobs_in_build = False
3539 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003540 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003541
3542 def test_client_get_running_jobs(self):
3543 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003544 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003546 A.addApproval('code-review', 2)
3547 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003548 self.waitUntilSettled()
3549
3550 client = zuul.rpcclient.RPCClient('127.0.0.1',
3551 self.gearman_server.port)
3552
3553 # Wait for gearman server to send the initial workData back to zuul
3554 start = time.time()
3555 while True:
3556 if time.time() - start > 10:
3557 raise Exception("Timeout waiting for gearman server to report "
3558 + "back to the client")
Clint Byrum78a69772016-12-05 10:52:07 -08003559 build = self.launch_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003560 if build.worker.name == "My Worker":
3561 break
3562 else:
3563 time.sleep(0)
3564
3565 running_items = client.get_running_jobs()
3566
3567 self.assertEqual(1, len(running_items))
3568 running_item = running_items[0]
3569 self.assertEqual([], running_item['failing_reasons'])
3570 self.assertEqual([], running_item['items_behind'])
3571 self.assertEqual('https://hostname/1', running_item['url'])
3572 self.assertEqual(None, running_item['item_ahead'])
3573 self.assertEqual('org/project', running_item['project'])
3574 self.assertEqual(None, running_item['remaining_time'])
3575 self.assertEqual(True, running_item['active'])
3576 self.assertEqual('1,1', running_item['id'])
3577
3578 self.assertEqual(3, len(running_item['jobs']))
3579 for job in running_item['jobs']:
3580 if job['name'] == 'project-merge':
3581 self.assertEqual('project-merge', job['name'])
3582 self.assertEqual('gate', job['pipeline'])
3583 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003584 self.assertEqual('https://server/job/project-merge/0/',
3585 job['url'])
3586 self.assertEqual(7, len(job['worker']))
3587 self.assertEqual(False, job['canceled'])
3588 self.assertEqual(True, job['voting'])
3589 self.assertEqual(None, job['result'])
3590 self.assertEqual('gate', job['pipeline'])
3591 break
3592
James E. Blair08d19992016-08-10 15:25:31 -07003593 self.launch_server.hold_jobs_in_build = False
3594 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003595 self.waitUntilSettled()
3596
3597 running_items = client.get_running_jobs()
3598 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003599
3600 def test_nonvoting_pipeline(self):
3601 "Test that a nonvoting pipeline (experimental) can still report"
3602
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003603 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3604 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003605 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3606 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003607
3608 self.assertEqual(self.getJobFromHistory('project-merge').result,
3609 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003610 self.assertEqual(
3611 self.getJobFromHistory('experimental-project-test').result,
3612 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003613 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003614
3615 def test_crd_gate(self):
3616 "Test cross-repo dependencies"
3617 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3618 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003619 A.addApproval('code-review', 2)
3620 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003621
3622 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3623 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3624 AM2.setMerged()
3625 AM1.setMerged()
3626
3627 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3628 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3629 BM2.setMerged()
3630 BM1.setMerged()
3631
3632 # A -> AM1 -> AM2
3633 # B -> BM1 -> BM2
3634 # A Depends-On: B
3635 # M2 is here to make sure it is never queried. If it is, it
3636 # means zuul is walking down the entire history of merged
3637 # changes.
3638
3639 B.setDependsOn(BM1, 1)
3640 BM1.setDependsOn(BM2, 1)
3641
3642 A.setDependsOn(AM1, 1)
3643 AM1.setDependsOn(AM2, 1)
3644
3645 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3646 A.subject, B.data['id'])
3647
James E. Blair8b5408c2016-08-08 15:37:46 -07003648 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003649 self.waitUntilSettled()
3650
3651 self.assertEqual(A.data['status'], 'NEW')
3652 self.assertEqual(B.data['status'], 'NEW')
3653
Paul Belanger5dccbe72016-11-14 11:17:37 -05003654 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003655 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003656
James E. Blair08d19992016-08-10 15:25:31 -07003657 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003658 B.addApproval('approved', 1)
3659 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003660 self.waitUntilSettled()
3661
James E. Blair08d19992016-08-10 15:25:31 -07003662 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003663 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003664 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003665 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003666 self.launch_server.hold_jobs_in_build = False
3667 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003668 self.waitUntilSettled()
3669
3670 self.assertEqual(AM2.queried, 0)
3671 self.assertEqual(BM2.queried, 0)
3672 self.assertEqual(A.data['status'], 'MERGED')
3673 self.assertEqual(B.data['status'], 'MERGED')
3674 self.assertEqual(A.reported, 2)
3675 self.assertEqual(B.reported, 2)
3676
Paul Belanger5dccbe72016-11-14 11:17:37 -05003677 changes = self.getJobFromHistory(
3678 'project-merge', 'org/project1').changes
3679 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003680
3681 def test_crd_branch(self):
3682 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003683
3684 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003685 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3686 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3687 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3688 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003689 A.addApproval('code-review', 2)
3690 B.addApproval('code-review', 2)
3691 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003692
3693 # A Depends-On: B+C
3694 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3695 A.subject, B.data['id'])
3696
James E. Blair08d19992016-08-10 15:25:31 -07003697 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003698 B.addApproval('approved', 1)
3699 C.addApproval('approved', 1)
3700 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003701 self.waitUntilSettled()
3702
James E. Blair08d19992016-08-10 15:25:31 -07003703 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003704 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003705 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003706 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003707 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003708 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003709 self.launch_server.hold_jobs_in_build = False
3710 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003711 self.waitUntilSettled()
3712
3713 self.assertEqual(A.data['status'], 'MERGED')
3714 self.assertEqual(B.data['status'], 'MERGED')
3715 self.assertEqual(C.data['status'], 'MERGED')
3716 self.assertEqual(A.reported, 2)
3717 self.assertEqual(B.reported, 2)
3718 self.assertEqual(C.reported, 2)
3719
Paul Belanger6379db12016-11-14 13:57:54 -05003720 changes = self.getJobFromHistory(
3721 'project-merge', 'org/project1').changes
3722 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003723
3724 def test_crd_multiline(self):
3725 "Test multiple depends-on lines in commit"
3726 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3727 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3728 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003729 A.addApproval('code-review', 2)
3730 B.addApproval('code-review', 2)
3731 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003732
3733 # A Depends-On: B+C
3734 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3735 A.subject, B.data['id'], C.data['id'])
3736
James E. Blair08d19992016-08-10 15:25:31 -07003737 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003738 B.addApproval('approved', 1)
3739 C.addApproval('approved', 1)
3740 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003741 self.waitUntilSettled()
3742
James E. Blair08d19992016-08-10 15:25:31 -07003743 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003744 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003745 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003746 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003747 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003748 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003749 self.launch_server.hold_jobs_in_build = False
3750 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003751 self.waitUntilSettled()
3752
3753 self.assertEqual(A.data['status'], 'MERGED')
3754 self.assertEqual(B.data['status'], 'MERGED')
3755 self.assertEqual(C.data['status'], 'MERGED')
3756 self.assertEqual(A.reported, 2)
3757 self.assertEqual(B.reported, 2)
3758 self.assertEqual(C.reported, 2)
3759
Paul Belanger5dccbe72016-11-14 11:17:37 -05003760 changes = self.getJobFromHistory(
3761 'project-merge', 'org/project1').changes
3762 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003763
3764 def test_crd_unshared_gate(self):
3765 "Test cross-repo dependencies in unshared gate queues"
3766 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3767 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003768 A.addApproval('code-review', 2)
3769 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003770
3771 # A Depends-On: B
3772 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3773 A.subject, B.data['id'])
3774
3775 # A and B do not share a queue, make sure that A is unable to
3776 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003777 B.addApproval('approved', 1)
3778 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003779 self.waitUntilSettled()
3780
3781 self.assertEqual(A.data['status'], 'NEW')
3782 self.assertEqual(B.data['status'], 'NEW')
3783 self.assertEqual(A.reported, 0)
3784 self.assertEqual(B.reported, 0)
3785 self.assertEqual(len(self.history), 0)
3786
3787 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003788 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003789 self.waitUntilSettled()
3790
3791 self.assertEqual(B.data['status'], 'MERGED')
3792 self.assertEqual(B.reported, 2)
3793
3794 # Now that B is merged, A should be able to be enqueued and
3795 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003796 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003797 self.waitUntilSettled()
3798
3799 self.assertEqual(A.data['status'], 'MERGED')
3800 self.assertEqual(A.reported, 2)
3801
James E. Blair96698e22015-04-02 07:48:21 -07003802 def test_crd_gate_reverse(self):
3803 "Test reverse cross-repo dependencies"
3804 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3805 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003806 A.addApproval('code-review', 2)
3807 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003808
3809 # A Depends-On: B
3810
3811 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3812 A.subject, B.data['id'])
3813
James E. Blair8b5408c2016-08-08 15:37:46 -07003814 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003815 self.waitUntilSettled()
3816
3817 self.assertEqual(A.data['status'], 'NEW')
3818 self.assertEqual(B.data['status'], 'NEW')
3819
James E. Blair08d19992016-08-10 15:25:31 -07003820 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003821 A.addApproval('approved', 1)
3822 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003823 self.waitUntilSettled()
3824
James E. Blair08d19992016-08-10 15:25:31 -07003825 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003826 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003827 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003828 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003829 self.launch_server.hold_jobs_in_build = False
3830 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003831 self.waitUntilSettled()
3832
3833 self.assertEqual(A.data['status'], 'MERGED')
3834 self.assertEqual(B.data['status'], 'MERGED')
3835 self.assertEqual(A.reported, 2)
3836 self.assertEqual(B.reported, 2)
3837
Paul Belanger5dccbe72016-11-14 11:17:37 -05003838 changes = self.getJobFromHistory(
3839 'project-merge', 'org/project1').changes
3840 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003841
James E. Blair5ee24252014-12-30 10:12:29 -08003842 def test_crd_cycle(self):
3843 "Test cross-repo dependency cycles"
3844 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3845 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003846 A.addApproval('code-review', 2)
3847 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003848
3849 # A -> B -> A (via commit-depends)
3850
3851 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3852 A.subject, B.data['id'])
3853 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3854 B.subject, A.data['id'])
3855
James E. Blair8b5408c2016-08-08 15:37:46 -07003856 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003857 self.waitUntilSettled()
3858
3859 self.assertEqual(A.reported, 0)
3860 self.assertEqual(B.reported, 0)
3861 self.assertEqual(A.data['status'], 'NEW')
3862 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003863
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003864 def test_crd_gate_unknown(self):
3865 "Test unknown projects in dependent pipeline"
3866 self.init_repo("org/unknown")
3867 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3868 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003869 A.addApproval('code-review', 2)
3870 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003871
3872 # A Depends-On: B
3873 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3874 A.subject, B.data['id'])
3875
James E. Blair8b5408c2016-08-08 15:37:46 -07003876 B.addApproval('approved', 1)
3877 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003878 self.waitUntilSettled()
3879
3880 # Unknown projects cannot share a queue with any other
3881 # since they don't have common jobs with any other (they have no jobs).
3882 # Changes which depend on unknown project changes
3883 # should not be processed in dependent pipeline
3884 self.assertEqual(A.data['status'], 'NEW')
3885 self.assertEqual(B.data['status'], 'NEW')
3886 self.assertEqual(A.reported, 0)
3887 self.assertEqual(B.reported, 0)
3888 self.assertEqual(len(self.history), 0)
3889
3890 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003891 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003892 B.setMerged()
3893 self.waitUntilSettled()
3894 self.assertEqual(len(self.history), 0)
3895
3896 # Now that B is merged, A should be able to be enqueued and
3897 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003898 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003899 self.waitUntilSettled()
3900
3901 self.assertEqual(A.data['status'], 'MERGED')
3902 self.assertEqual(A.reported, 2)
3903 self.assertEqual(B.data['status'], 'MERGED')
3904 self.assertEqual(B.reported, 0)
3905
James E. Blairbfb8e042014-12-30 17:01:44 -08003906 def test_crd_check(self):
3907 "Test cross-repo dependencies in independent pipelines"
3908
Paul Belangerb30342b2016-11-14 12:30:43 -05003909 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003910 self.gearman_server.hold_jobs_in_queue = True
3911 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3912 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3913
3914 # A Depends-On: B
3915 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3916 A.subject, B.data['id'])
3917
3918 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3919 self.waitUntilSettled()
3920
3921 queue = self.gearman_server.getQueue()
3922 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3923 self.gearman_server.hold_jobs_in_queue = False
3924 self.gearman_server.release()
3925 self.waitUntilSettled()
3926
Paul Belangerb30342b2016-11-14 12:30:43 -05003927 self.launch_server.release('.*-merge')
3928 self.waitUntilSettled()
3929
3930 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08003931 repo = git.Repo(path)
3932 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3933 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003934 correct_messages = [
3935 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003936 self.assertEqual(repo_messages, correct_messages)
3937
Paul Belangerb30342b2016-11-14 12:30:43 -05003938 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08003939 repo = git.Repo(path)
3940 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3941 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003942 correct_messages = [
3943 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003944 self.assertEqual(repo_messages, correct_messages)
3945
Paul Belangerb30342b2016-11-14 12:30:43 -05003946 self.launch_server.hold_jobs_in_build = False
3947 self.launch_server.release()
3948 self.waitUntilSettled()
3949
James E. Blairbfb8e042014-12-30 17:01:44 -08003950 self.assertEqual(A.data['status'], 'NEW')
3951 self.assertEqual(B.data['status'], 'NEW')
3952 self.assertEqual(A.reported, 1)
3953 self.assertEqual(B.reported, 0)
3954
3955 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003956 tenant = self.sched.abide.tenants.get('tenant-one')
3957 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003958
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003959 def test_crd_check_git_depends(self):
3960 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003961 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003962 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3963 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3964
3965 # Add two git-dependent changes and make sure they both report
3966 # success.
3967 B.setDependsOn(A, 1)
3968 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3969 self.waitUntilSettled()
3970 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3971 self.waitUntilSettled()
3972
James E. Blairb8c16472015-05-05 14:55:26 -07003973 self.orderedRelease()
3974 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003975 self.waitUntilSettled()
3976
3977 self.assertEqual(A.data['status'], 'NEW')
3978 self.assertEqual(B.data['status'], 'NEW')
3979 self.assertEqual(A.reported, 1)
3980 self.assertEqual(B.reported, 1)
3981
3982 self.assertEqual(self.history[0].changes, '1,1')
3983 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003984 tenant = self.sched.abide.tenants.get('tenant-one')
3985 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003986
3987 self.assertIn('Build succeeded', A.messages[0])
3988 self.assertIn('Build succeeded', B.messages[0])
3989
3990 def test_crd_check_duplicate(self):
3991 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07003992 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003993 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3994 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003995 tenant = self.sched.abide.tenants.get('tenant-one')
3996 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003997
3998 # Add two git-dependent changes...
3999 B.setDependsOn(A, 1)
4000 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4001 self.waitUntilSettled()
4002 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4003
4004 # ...make sure the live one is not duplicated...
4005 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4006 self.waitUntilSettled()
4007 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4008
4009 # ...but the non-live one is able to be.
4010 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4011 self.waitUntilSettled()
4012 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4013
Clark Boylandd849822015-03-02 12:38:14 -08004014 # Release jobs in order to avoid races with change A jobs
4015 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004016 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004017 self.launch_server.hold_jobs_in_build = False
4018 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004019 self.waitUntilSettled()
4020
4021 self.assertEqual(A.data['status'], 'NEW')
4022 self.assertEqual(B.data['status'], 'NEW')
4023 self.assertEqual(A.reported, 1)
4024 self.assertEqual(B.reported, 1)
4025
4026 self.assertEqual(self.history[0].changes, '1,1 2,1')
4027 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004028 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004029
4030 self.assertIn('Build succeeded', A.messages[0])
4031 self.assertIn('Build succeeded', B.messages[0])
4032
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004033 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004034 "Test cross-repo dependencies re-enqueued in independent pipelines"
4035
4036 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004037 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4038 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004039
4040 # A Depends-On: B
4041 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4042 A.subject, B.data['id'])
4043
4044 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4045 self.waitUntilSettled()
4046
4047 self.sched.reconfigure(self.config)
4048
4049 # Make sure the items still share a change queue, and the
4050 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004051 tenant = self.sched.abide.tenants.get('tenant-one')
4052 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4053 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004054 first_item = queue.queue[0]
4055 for item in queue.queue:
4056 self.assertEqual(item.queue, first_item.queue)
4057 self.assertFalse(first_item.live)
4058 self.assertTrue(queue.queue[1].live)
4059
4060 self.gearman_server.hold_jobs_in_queue = False
4061 self.gearman_server.release()
4062 self.waitUntilSettled()
4063
4064 self.assertEqual(A.data['status'], 'NEW')
4065 self.assertEqual(B.data['status'], 'NEW')
4066 self.assertEqual(A.reported, 1)
4067 self.assertEqual(B.reported, 0)
4068
4069 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004070 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004071
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004072 def test_crd_check_reconfiguration(self):
4073 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4074
4075 def test_crd_undefined_project(self):
4076 """Test that undefined projects in dependencies are handled for
4077 independent pipelines"""
4078 # It's a hack for fake gerrit,
4079 # as it implies repo creation upon the creation of any change
4080 self.init_repo("org/unknown")
4081 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4082
James E. Blairec056492016-07-22 09:45:56 -07004083 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004084 def test_crd_check_ignore_dependencies(self):
4085 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004086 self.updateConfigLayout(
4087 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004088 self.sched.reconfigure(self.config)
4089 self.registerJobs()
4090
4091 self.gearman_server.hold_jobs_in_queue = True
4092 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4093 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4094 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4095
4096 # A Depends-On: B
4097 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4098 A.subject, B.data['id'])
4099 # C git-depends on B
4100 C.setDependsOn(B, 1)
4101 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4102 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4103 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4104 self.waitUntilSettled()
4105
4106 # Make sure none of the items share a change queue, and all
4107 # are live.
4108 check_pipeline = self.sched.layout.pipelines['check']
4109 self.assertEqual(len(check_pipeline.queues), 3)
4110 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4111 for item in check_pipeline.getAllItems():
4112 self.assertTrue(item.live)
4113
4114 self.gearman_server.hold_jobs_in_queue = False
4115 self.gearman_server.release()
4116 self.waitUntilSettled()
4117
4118 self.assertEqual(A.data['status'], 'NEW')
4119 self.assertEqual(B.data['status'], 'NEW')
4120 self.assertEqual(C.data['status'], 'NEW')
4121 self.assertEqual(A.reported, 1)
4122 self.assertEqual(B.reported, 1)
4123 self.assertEqual(C.reported, 1)
4124
4125 # Each job should have tested exactly one change
4126 for job in self.history:
4127 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004128
James E. Blairec056492016-07-22 09:45:56 -07004129 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004130 def test_crd_check_transitive(self):
4131 "Test transitive cross-repo dependencies"
4132 # Specifically, if A -> B -> C, and C gets a new patchset and
4133 # A gets a new patchset, ensure the test of A,2 includes B,1
4134 # and C,2 (not C,1 which would indicate stale data in the
4135 # cache for B).
4136 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4137 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4138 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4139
4140 # A Depends-On: B
4141 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4142 A.subject, B.data['id'])
4143
4144 # B Depends-On: C
4145 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4146 B.subject, C.data['id'])
4147
4148 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4149 self.waitUntilSettled()
4150 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4151
4152 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4153 self.waitUntilSettled()
4154 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4155
4156 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4157 self.waitUntilSettled()
4158 self.assertEqual(self.history[-1].changes, '3,1')
4159
4160 C.addPatchset()
4161 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4162 self.waitUntilSettled()
4163 self.assertEqual(self.history[-1].changes, '3,2')
4164
4165 A.addPatchset()
4166 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4167 self.waitUntilSettled()
4168 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004169
James E. Blaira8b90b32016-08-24 15:18:50 -07004170 def test_crd_check_unknown(self):
4171 "Test unknown projects in independent pipeline"
4172 self.init_repo("org/unknown")
4173 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4174 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4175 # A Depends-On: B
4176 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4177 A.subject, B.data['id'])
4178
4179 # Make sure zuul has seen an event on B.
4180 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4181 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4182 self.waitUntilSettled()
4183
4184 self.assertEqual(A.data['status'], 'NEW')
4185 self.assertEqual(A.reported, 1)
4186 self.assertEqual(B.data['status'], 'NEW')
4187 self.assertEqual(B.reported, 0)
4188
James E. Blair92464a22016-04-05 10:21:26 -07004189 def test_crd_cycle_join(self):
4190 "Test an updated change creates a cycle"
4191 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4192
4193 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4194 self.waitUntilSettled()
4195
4196 # Create B->A
4197 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4198 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4199 B.subject, A.data['id'])
4200 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4201 self.waitUntilSettled()
4202
4203 # Update A to add A->B (a cycle).
4204 A.addPatchset()
4205 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4206 A.subject, B.data['id'])
4207 # Normally we would submit the patchset-created event for
4208 # processing here, however, we have no way of noting whether
4209 # the dependency cycle detection correctly raised an
4210 # exception, so instead, we reach into the source driver and
4211 # call the method that would ultimately be called by the event
4212 # processing.
4213
Paul Belanger0e155e22016-11-14 14:12:23 -05004214 tenant = self.sched.abide.tenants.get('tenant-one')
4215 source = tenant.layout.pipelines['gate'].source
4216
4217 # TODO(pabelanger): As we add more source / trigger APIs we should make
4218 # it easier for users to create events for testing.
4219 event = zuul.model.TriggerEvent()
4220 event.trigger_name = 'gerrit'
4221 event.change_number = '1'
4222 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004223 with testtools.ExpectedException(
4224 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004225 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004226 self.log.debug("Got expected dependency cycle exception")
4227
4228 # Now if we update B to remove the depends-on, everything
4229 # should be okay. B; A->B
4230
4231 B.addPatchset()
4232 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004233
4234 source.getChange(event, True)
4235 event.change_number = '2'
4236 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004237
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004238 def test_disable_at(self):
4239 "Test a pipeline will only report to the disabled trigger when failing"
4240
Paul Belanger7dc76e82016-11-11 16:51:08 -05004241 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004242 self.sched.reconfigure(self.config)
4243
Paul Belanger7dc76e82016-11-11 16:51:08 -05004244 tenant = self.sched.abide.tenants.get('openstack')
4245 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004246 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004247 0, tenant.layout.pipelines['check']._consecutive_failures)
4248 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004249
4250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4251 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4252 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4253 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4254 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4255 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4256 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4257 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4258 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4259 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4260 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4261
James E. Blair08d19992016-08-10 15:25:31 -07004262 self.launch_server.failJob('project-test1', A)
4263 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004264 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004265 self.launch_server.failJob('project-test1', D)
4266 self.launch_server.failJob('project-test1', E)
4267 self.launch_server.failJob('project-test1', F)
4268 self.launch_server.failJob('project-test1', G)
4269 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004270 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004271 self.launch_server.failJob('project-test1', J)
4272 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004273
4274 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4275 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4276 self.waitUntilSettled()
4277
4278 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004279 2, tenant.layout.pipelines['check']._consecutive_failures)
4280 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004281
4282 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4283 self.waitUntilSettled()
4284
4285 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004286 0, tenant.layout.pipelines['check']._consecutive_failures)
4287 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004288
4289 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4290 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4291 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4292 self.waitUntilSettled()
4293
4294 # We should be disabled now
4295 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004296 3, tenant.layout.pipelines['check']._consecutive_failures)
4297 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004298
4299 # We need to wait between each of these patches to make sure the
4300 # smtp messages come back in an expected order
4301 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4302 self.waitUntilSettled()
4303 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4304 self.waitUntilSettled()
4305 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4306 self.waitUntilSettled()
4307
4308 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4309 # leaving a message on each change
4310 self.assertEqual(1, len(A.messages))
4311 self.assertIn('Build failed.', A.messages[0])
4312 self.assertEqual(1, len(B.messages))
4313 self.assertIn('Build failed.', B.messages[0])
4314 self.assertEqual(1, len(C.messages))
4315 self.assertIn('Build succeeded.', C.messages[0])
4316 self.assertEqual(1, len(D.messages))
4317 self.assertIn('Build failed.', D.messages[0])
4318 self.assertEqual(1, len(E.messages))
4319 self.assertIn('Build failed.', E.messages[0])
4320 self.assertEqual(1, len(F.messages))
4321 self.assertIn('Build failed.', F.messages[0])
4322
4323 # The last 3 (GHI) would have only reported via smtp.
4324 self.assertEqual(3, len(self.smtp_messages))
4325 self.assertEqual(0, len(G.messages))
4326 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004327 self.assertIn(
4328 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004329 self.assertEqual(0, len(H.messages))
4330 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004331 self.assertIn(
4332 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004333 self.assertEqual(0, len(I.messages))
4334 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004335 self.assertIn(
4336 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004337
4338 # Now reload the configuration (simulate a HUP) to check the pipeline
4339 # comes out of disabled
4340 self.sched.reconfigure(self.config)
4341
Paul Belanger7dc76e82016-11-11 16:51:08 -05004342 tenant = self.sched.abide.tenants.get('openstack')
4343
4344 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004345 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004346 0, tenant.layout.pipelines['check']._consecutive_failures)
4347 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004348
4349 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4350 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4351 self.waitUntilSettled()
4352
4353 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004354 2, tenant.layout.pipelines['check']._consecutive_failures)
4355 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004356
4357 # J and K went back to gerrit
4358 self.assertEqual(1, len(J.messages))
4359 self.assertIn('Build failed.', J.messages[0])
4360 self.assertEqual(1, len(K.messages))
4361 self.assertIn('Build failed.', K.messages[0])
4362 # No more messages reported via smtp
4363 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004364
Paul Belanger71d98172016-11-08 10:56:31 -05004365 def test_rerun_on_abort(self):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004366 "Test that if a launch server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004367
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004368 self.launch_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004369 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4370 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4371 self.waitUntilSettled()
4372
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004373 self.launch_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004374 self.waitUntilSettled()
4375
4376 self.assertEqual(len(self.builds), 2)
4377 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004378 self.launch_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004379 self.waitUntilSettled()
4380
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004381 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004382 self.assertEqual(len(self.builds), 1,
4383 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004384 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004385 self.launch_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004386 self.waitUntilSettled()
4387
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004388 self.launch_server.hold_jobs_in_build = False
4389 self.launch_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004390 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004391 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004392 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4393 self.assertEqual(A.reported, 1)
4394 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004395
James E. Blair15be0e12017-01-03 13:45:20 -08004396 def test_zookeeper_disconnect(self):
4397 "Test that jobs are launched after a zookeeper disconnect"
4398
4399 self.fake_nodepool.paused = True
4400 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4401 A.addApproval('code-review', 2)
4402 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4403 self.waitUntilSettled()
4404
4405 self.zk.client.stop()
4406 self.zk.client.start()
4407 self.fake_nodepool.paused = False
4408 self.waitUntilSettled()
4409
4410 self.assertEqual(A.data['status'], 'MERGED')
4411 self.assertEqual(A.reported, 2)
4412
James E. Blair6ab79e02017-01-06 10:10:17 -08004413 def test_nodepool_failure(self):
4414 "Test that jobs are reported after a nodepool failure"
4415
4416 self.fake_nodepool.paused = True
4417 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4418 A.addApproval('code-review', 2)
4419 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4420 self.waitUntilSettled()
4421
4422 req = self.fake_nodepool.getNodeRequests()[0]
4423 self.fake_nodepool.addFailRequest(req)
4424
4425 self.fake_nodepool.paused = False
4426 self.waitUntilSettled()
4427
4428 self.assertEqual(A.data['status'], 'NEW')
4429 self.assertEqual(A.reported, 2)
4430 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4431 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4432 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4433
Arieb6f068c2016-10-09 13:11:06 +03004434
4435class TestDuplicatePipeline(ZuulTestCase):
4436 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4437
4438 def test_duplicate_pipelines(self):
4439 "Test that a change matching multiple pipelines works"
4440
4441 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4442 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4443 self.waitUntilSettled()
4444
4445 self.assertHistory([
4446 dict(name='project-test1', result='SUCCESS', changes='1,1',
4447 pipeline='dup1'),
4448 dict(name='project-test1', result='SUCCESS', changes='1,1',
4449 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004450 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004451
4452 self.assertEqual(len(A.messages), 2)
4453
Paul Belangera46a3742016-11-09 14:23:03 -05004454 if 'dup1' in A.messages[0]:
4455 self.assertIn('dup1', A.messages[0])
4456 self.assertNotIn('dup2', A.messages[0])
4457 self.assertIn('project-test1', A.messages[0])
4458 self.assertIn('dup2', A.messages[1])
4459 self.assertNotIn('dup1', A.messages[1])
4460 self.assertIn('project-test1', A.messages[1])
4461 else:
4462 self.assertIn('dup1', A.messages[1])
4463 self.assertNotIn('dup2', A.messages[1])
4464 self.assertIn('project-test1', A.messages[1])
4465 self.assertIn('dup2', A.messages[0])
4466 self.assertNotIn('dup1', A.messages[0])
4467 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004468
4469
4470class TestSchedulerOneJobProject(ZuulTestCase):
4471 tenant_config_file = 'config/one-job-project/main.yaml'
4472
4473 def test_one_job_project(self):
4474 "Test that queueing works with one job"
4475 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4476 'master', 'A')
4477 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4478 'master', 'B')
4479 A.addApproval('code-review', 2)
4480 B.addApproval('code-review', 2)
4481 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4482 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4483 self.waitUntilSettled()
4484
4485 self.assertEqual(A.data['status'], 'MERGED')
4486 self.assertEqual(A.reported, 2)
4487 self.assertEqual(B.data['status'], 'MERGED')
4488 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004489
4490
4491class TestSchedulerTemplatedProject(ZuulTestCase):
4492 tenant_config_file = 'config/templated-project/main.yaml'
4493
4494 def test_job_from_templates_launched(self):
4495 "Test whether a job generated via a template can be launched"
4496
4497 A = self.fake_gerrit.addFakeChange(
4498 'org/templated-project', 'master', 'A')
4499 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4500 self.waitUntilSettled()
4501
4502 self.assertEqual(self.getJobFromHistory('project-test1').result,
4503 'SUCCESS')
4504 self.assertEqual(self.getJobFromHistory('project-test2').result,
4505 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004506
4507 def test_layered_templates(self):
4508 "Test whether a job generated via a template can be launched"
4509
4510 A = self.fake_gerrit.addFakeChange(
4511 'org/layered-project', 'master', 'A')
4512 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4513 self.waitUntilSettled()
4514
4515 self.assertEqual(self.getJobFromHistory('project-test1').result,
4516 'SUCCESS')
4517 self.assertEqual(self.getJobFromHistory('project-test2').result,
4518 'SUCCESS')
4519 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4520 ).result, 'SUCCESS')
4521 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4522 ).result, 'SUCCESS')
4523 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4524 ).result, 'SUCCESS')
4525 self.assertEqual(self.getJobFromHistory('project-test6').result,
4526 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004527
4528
4529class TestSchedulerSuccessURL(ZuulTestCase):
4530 tenant_config_file = 'config/success-url/main.yaml'
4531
4532 def test_success_url(self):
4533 "Ensure bad build params are ignored"
4534 self.sched.reconfigure(self.config)
4535 self.init_repo('org/docs')
4536
4537 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4538 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4539 self.waitUntilSettled()
4540
4541 # Both builds ran: docs-draft-test + docs-draft-test2
4542 self.assertEqual(len(self.history), 2)
4543
4544 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004545 for build in self.history:
4546 if build.name == 'docs-draft-test':
4547 uuid = build.uuid[:7]
4548 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004549
4550 # Two msgs: 'Starting...' + results
4551 self.assertEqual(len(self.smtp_messages), 2)
4552 body = self.smtp_messages[1]['body'].splitlines()
4553 self.assertEqual('Build succeeded.', body[0])
4554
4555 self.assertIn(
4556 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4557 'docs-draft-test/%s/publish-docs/' % uuid,
4558 body[2])
4559
4560 # NOTE: This default URL is currently hard-coded in launcher/server.py
4561 self.assertIn(
4562 '- docs-draft-test2 https://server/job',
4563 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004564
4565
Adam Gandelman4da00f62016-12-09 15:47:33 -08004566class TestSchedulerMerges(ZuulTestCase):
4567 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004568
4569 def _test_project_merge_mode(self, mode):
4570 self.launch_server.keep_jobdir = False
4571 project = 'org/project-%s' % mode
4572 self.launch_server.hold_jobs_in_build = True
4573 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4574 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4575 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4576 A.addApproval('code-review', 2)
4577 B.addApproval('code-review', 2)
4578 C.addApproval('code-review', 2)
4579 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4580 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4581 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4582 self.waitUntilSettled()
4583
4584 build = self.builds[-1]
4585 ref = self.getParameter(build, 'ZUUL_REF')
4586
4587 path = os.path.join(build.jobdir.git_root, project)
4588 repo = git.Repo(path)
4589 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4590 repo_messages.reverse()
4591
4592 self.launch_server.hold_jobs_in_build = False
4593 self.launch_server.release()
4594 self.waitUntilSettled()
4595
4596 return repo_messages
4597
4598 def _test_merge(self, mode):
4599 us_path = os.path.join(
4600 self.upstream_root, 'org/project-%s' % mode)
4601 expected_messages = [
4602 'initial commit',
4603 'add content from fixture',
4604 # the intermediate commits order is nondeterministic
4605 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4606 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4607 ]
4608 result = self._test_project_merge_mode(mode)
4609 self.assertEqual(result[:2], expected_messages[:2])
4610 self.assertEqual(result[-2:], expected_messages[-2:])
4611
4612 def test_project_merge_mode_merge(self):
4613 self._test_merge('merge')
4614
4615 def test_project_merge_mode_merge_resolve(self):
4616 self._test_merge('merge-resolve')
4617
4618 def test_project_merge_mode_cherrypick(self):
4619 expected_messages = [
4620 'initial commit',
4621 'add content from fixture',
4622 'A-1',
4623 'B-1',
4624 'C-1']
4625 result = self._test_project_merge_mode('cherry-pick')
4626 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004627
4628 def test_merge_branch(self):
4629 "Test that the right commits are on alternate branches"
4630 self.create_branch('org/project-merge-branches', 'mp')
4631
4632 self.launch_server.hold_jobs_in_build = True
4633 A = self.fake_gerrit.addFakeChange(
4634 'org/project-merge-branches', 'mp', 'A')
4635 B = self.fake_gerrit.addFakeChange(
4636 'org/project-merge-branches', 'mp', 'B')
4637 C = self.fake_gerrit.addFakeChange(
4638 'org/project-merge-branches', 'mp', 'C')
4639 A.addApproval('code-review', 2)
4640 B.addApproval('code-review', 2)
4641 C.addApproval('code-review', 2)
4642 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4643 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4644 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4645 self.waitUntilSettled()
4646
4647 self.launch_server.release('.*-merge')
4648 self.waitUntilSettled()
4649 self.launch_server.release('.*-merge')
4650 self.waitUntilSettled()
4651 self.launch_server.release('.*-merge')
4652 self.waitUntilSettled()
4653
4654 build = self.builds[-1]
4655 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4656 ref = self.getParameter(build, 'ZUUL_REF')
4657 path = os.path.join(
4658 build.jobdir.git_root, 'org/project-merge-branches')
4659 repo = git.Repo(path)
4660
4661 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4662 repo_messages.reverse()
4663 correct_messages = [
4664 'initial commit',
4665 'add content from fixture',
4666 'mp commit',
4667 'A-1', 'B-1', 'C-1']
4668 self.assertEqual(repo_messages, correct_messages)
4669
4670 self.launch_server.hold_jobs_in_build = False
4671 self.launch_server.release()
4672 self.waitUntilSettled()
4673
4674 def test_merge_multi_branch(self):
4675 "Test that dependent changes on multiple branches are merged"
4676 self.create_branch('org/project-merge-branches', 'mp')
4677
4678 self.launch_server.hold_jobs_in_build = True
4679 A = self.fake_gerrit.addFakeChange(
4680 'org/project-merge-branches', 'master', 'A')
4681 B = self.fake_gerrit.addFakeChange(
4682 'org/project-merge-branches', 'mp', 'B')
4683 C = self.fake_gerrit.addFakeChange(
4684 'org/project-merge-branches', 'master', 'C')
4685 A.addApproval('code-review', 2)
4686 B.addApproval('code-review', 2)
4687 C.addApproval('code-review', 2)
4688 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4689 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4690 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4691 self.waitUntilSettled()
4692
4693 job_A = None
4694 for job in self.builds:
4695 if 'project-merge' in job.name:
4696 job_A = job
4697 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4698 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4699 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4700 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4701
4702 path = os.path.join(
4703 job_A.jobdir.git_root, "org/project-merge-branches")
4704 repo = git.Repo(path)
4705 repo_messages = [c.message.strip()
4706 for c in repo.iter_commits(ref_A)]
4707 repo_messages.reverse()
4708 correct_messages = [
4709 'initial commit', 'add content from fixture', 'A-1']
4710 self.assertEqual(repo_messages, correct_messages)
4711
4712 self.launch_server.release('.*-merge')
4713 self.waitUntilSettled()
4714
4715 job_B = None
4716 for job in self.builds:
4717 if 'project-merge' in job.name:
4718 job_B = job
4719 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4720 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4721 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4722 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4723
4724 path = os.path.join(
4725 job_B.jobdir.git_root, "org/project-merge-branches")
4726 repo = git.Repo(path)
4727 repo_messages = [c.message.strip()
4728 for c in repo.iter_commits(ref_B)]
4729 repo_messages.reverse()
4730 correct_messages = [
4731 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4732 self.assertEqual(repo_messages, correct_messages)
4733
4734 self.launch_server.release('.*-merge')
4735 self.waitUntilSettled()
4736
4737 job_C = None
4738 for job in self.builds:
4739 if 'project-merge' in job.name:
4740 job_C = job
4741 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4742 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4743 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4744 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
4745 path = os.path.join(
4746 job_C.jobdir.git_root, "org/project-merge-branches")
4747 repo = git.Repo(path)
4748 repo_messages = [c.message.strip()
4749 for c in repo.iter_commits(ref_C)]
4750
4751 repo_messages.reverse()
4752 correct_messages = [
4753 'initial commit', 'add content from fixture',
4754 'A-1', 'C-1']
4755 # Ensure the right commits are in the history for this ref
4756 self.assertEqual(repo_messages, correct_messages)
4757
4758 self.launch_server.hold_jobs_in_build = False
4759 self.launch_server.release()
4760 self.waitUntilSettled()