blob: 4aa9b6f5ebb375969bd5f054addd087b2fc47c10 [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
James E. Blair8c1be532017-02-07 14:04:12 -08001380 path = os.path.join(self.merger_git_root, "org/project")
1381 if os.path.exists(path):
1382 repack_repo(path)
1383 path = os.path.join(self.launcher_git_root, "org/project")
1384 if os.path.exists(path):
1385 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001386
1387 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001388 A.addApproval('code-review', 2)
1389 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001390 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001391 self.assertEqual(self.getJobFromHistory('project-merge').result,
1392 'SUCCESS')
1393 self.assertEqual(self.getJobFromHistory('project-test1').result,
1394 'SUCCESS')
1395 self.assertEqual(self.getJobFromHistory('project-test2').result,
1396 'SUCCESS')
1397 self.assertEqual(A.data['status'], 'MERGED')
1398 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001399
James E. Blair4886f282012-11-15 09:27:33 -08001400 def test_merger_repack_large_change(self):
1401 "Test that the merger works with large changes after a repack"
1402 # https://bugs.launchpad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001403 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001404 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001405 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001406 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001407 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001408 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1409 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001410 path = os.path.join(self.upstream_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001411 repack_repo(path)
1412 path = os.path.join(self.merger_git_root, "org/project1")
1413 if os.path.exists(path):
1414 repack_repo(path)
1415 path = os.path.join(self.launcher_git_root, "org/project1")
1416 if os.path.exists(path):
1417 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001418
James E. Blair8b5408c2016-08-08 15:37:46 -07001419 A.addApproval('code-review', 2)
1420 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001421 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001422 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001423 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001424 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001425 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001426 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001427 'SUCCESS')
1428 self.assertEqual(A.data['status'], 'MERGED')
1429 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001430
James E. Blair2fa50962013-01-30 21:50:41 -08001431 def test_new_patchset_dequeues_old(self):
1432 "Test that a new patchset causes the old to be dequeued"
1433 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001434 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001435 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1436 M.setMerged()
1437
1438 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1439 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1440 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1441 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001442 A.addApproval('code-review', 2)
1443 B.addApproval('code-review', 2)
1444 C.addApproval('code-review', 2)
1445 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001446
1447 C.setDependsOn(B, 1)
1448 B.setDependsOn(A, 1)
1449 A.setDependsOn(M, 1)
1450
James E. Blair8b5408c2016-08-08 15:37:46 -07001451 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1452 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1453 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1454 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001455 self.waitUntilSettled()
1456
1457 B.addPatchset()
1458 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1459 self.waitUntilSettled()
1460
James E. Blair08d19992016-08-10 15:25:31 -07001461 self.launch_server.hold_jobs_in_build = False
1462 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001463 self.waitUntilSettled()
1464
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001465 self.assertEqual(A.data['status'], 'MERGED')
1466 self.assertEqual(A.reported, 2)
1467 self.assertEqual(B.data['status'], 'NEW')
1468 self.assertEqual(B.reported, 2)
1469 self.assertEqual(C.data['status'], 'NEW')
1470 self.assertEqual(C.reported, 2)
1471 self.assertEqual(D.data['status'], 'MERGED')
1472 self.assertEqual(D.reported, 2)
1473 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001474
James E. Blairba437362015-02-07 11:41:52 -08001475 def test_new_patchset_check(self):
1476 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001477
James E. Blair08d19992016-08-10 15:25:31 -07001478 self.launch_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001479
1480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001481 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001482 tenant = self.sched.abide.tenants.get('tenant-one')
1483 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001484
1485 # Add two git-dependent changes
1486 B.setDependsOn(A, 1)
1487 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1488 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001489 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1490 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001491
1492 # A live item, and a non-live/live pair
1493 items = check_pipeline.getAllItems()
1494 self.assertEqual(len(items), 3)
1495
1496 self.assertEqual(items[0].change.number, '1')
1497 self.assertEqual(items[0].change.patchset, '1')
1498 self.assertFalse(items[0].live)
1499
1500 self.assertEqual(items[1].change.number, '2')
1501 self.assertEqual(items[1].change.patchset, '1')
1502 self.assertTrue(items[1].live)
1503
1504 self.assertEqual(items[2].change.number, '1')
1505 self.assertEqual(items[2].change.patchset, '1')
1506 self.assertTrue(items[2].live)
1507
1508 # Add a new patchset to A
1509 A.addPatchset()
1510 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1511 self.waitUntilSettled()
1512
1513 # The live copy of A,1 should be gone, but the non-live and B
1514 # should continue, and we should have a new A,2
1515 items = check_pipeline.getAllItems()
1516 self.assertEqual(len(items), 3)
1517
1518 self.assertEqual(items[0].change.number, '1')
1519 self.assertEqual(items[0].change.patchset, '1')
1520 self.assertFalse(items[0].live)
1521
1522 self.assertEqual(items[1].change.number, '2')
1523 self.assertEqual(items[1].change.patchset, '1')
1524 self.assertTrue(items[1].live)
1525
1526 self.assertEqual(items[2].change.number, '1')
1527 self.assertEqual(items[2].change.patchset, '2')
1528 self.assertTrue(items[2].live)
1529
1530 # Add a new patchset to B
1531 B.addPatchset()
1532 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1533 self.waitUntilSettled()
1534
1535 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1536 # but we should have a new B,2 (still based on A,1)
1537 items = check_pipeline.getAllItems()
1538 self.assertEqual(len(items), 3)
1539
1540 self.assertEqual(items[0].change.number, '1')
1541 self.assertEqual(items[0].change.patchset, '2')
1542 self.assertTrue(items[0].live)
1543
1544 self.assertEqual(items[1].change.number, '1')
1545 self.assertEqual(items[1].change.patchset, '1')
1546 self.assertFalse(items[1].live)
1547
1548 self.assertEqual(items[2].change.number, '2')
1549 self.assertEqual(items[2].change.patchset, '2')
1550 self.assertTrue(items[2].live)
1551
1552 self.builds[0].release()
1553 self.waitUntilSettled()
1554 self.builds[0].release()
1555 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001556 self.launch_server.hold_jobs_in_build = False
1557 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001558 self.waitUntilSettled()
1559
1560 self.assertEqual(A.reported, 1)
1561 self.assertEqual(B.reported, 1)
1562 self.assertEqual(self.history[0].result, 'ABORTED')
1563 self.assertEqual(self.history[0].changes, '1,1')
1564 self.assertEqual(self.history[1].result, 'ABORTED')
1565 self.assertEqual(self.history[1].changes, '1,1 2,1')
1566 self.assertEqual(self.history[2].result, 'SUCCESS')
1567 self.assertEqual(self.history[2].changes, '1,2')
1568 self.assertEqual(self.history[3].result, 'SUCCESS')
1569 self.assertEqual(self.history[3].changes, '1,1 2,2')
1570
1571 def test_abandoned_gate(self):
1572 "Test that an abandoned change is dequeued from gate"
1573
James E. Blair08d19992016-08-10 15:25:31 -07001574 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001575
1576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001577 A.addApproval('code-review', 2)
1578 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001579 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001580 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1581 self.assertEqual(self.builds[0].name, 'project-merge')
1582
1583 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1584 self.waitUntilSettled()
1585
James E. Blair08d19992016-08-10 15:25:31 -07001586 self.launch_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001587 self.waitUntilSettled()
1588
Arie5b4048c2016-10-05 16:12:06 +03001589 self.assertBuilds([])
1590 self.assertHistory([
1591 dict(name='project-merge', result='ABORTED', changes='1,1')],
1592 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001593 self.assertEqual(A.reported, 1,
1594 "Abandoned gate change should report only start")
1595
1596 def test_abandoned_check(self):
1597 "Test that an abandoned change is dequeued from check"
1598
James E. Blair08d19992016-08-10 15:25:31 -07001599 self.launch_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001600
1601 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1602 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001603 tenant = self.sched.abide.tenants.get('tenant-one')
1604 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001605
1606 # Add two git-dependent changes
1607 B.setDependsOn(A, 1)
1608 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1609 self.waitUntilSettled()
1610 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1611 self.waitUntilSettled()
1612 # A live item, and a non-live/live pair
1613 items = check_pipeline.getAllItems()
1614 self.assertEqual(len(items), 3)
1615
1616 self.assertEqual(items[0].change.number, '1')
1617 self.assertFalse(items[0].live)
1618
1619 self.assertEqual(items[1].change.number, '2')
1620 self.assertTrue(items[1].live)
1621
1622 self.assertEqual(items[2].change.number, '1')
1623 self.assertTrue(items[2].live)
1624
1625 # Abandon A
1626 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1627 self.waitUntilSettled()
1628
1629 # The live copy of A should be gone, but the non-live and B
1630 # should continue
1631 items = check_pipeline.getAllItems()
1632 self.assertEqual(len(items), 2)
1633
1634 self.assertEqual(items[0].change.number, '1')
1635 self.assertFalse(items[0].live)
1636
1637 self.assertEqual(items[1].change.number, '2')
1638 self.assertTrue(items[1].live)
1639
James E. Blair08d19992016-08-10 15:25:31 -07001640 self.launch_server.hold_jobs_in_build = False
1641 self.launch_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001642 self.waitUntilSettled()
1643
1644 self.assertEqual(len(self.history), 4)
1645 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001646 'Build should have been aborted')
1647 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001648 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001649
Steve Varnau7b78b312015-04-03 14:49:46 -07001650 def test_abandoned_not_timer(self):
1651 "Test that an abandoned change does not cancel timer jobs"
1652
James E. Blair08d19992016-08-10 15:25:31 -07001653 self.launch_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001654
1655 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001656 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001657 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001658 # The pipeline triggers every second, so we should have seen
1659 # several by now.
1660 time.sleep(5)
1661 self.waitUntilSettled()
1662 # Stop queuing timer triggered jobs so that the assertions
1663 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001664 # Must be in same repo, so overwrite config with another one
1665 no_timer_path = os.path.join(self.test_root, 'upstream',
1666 'layout-no-timer', 'zuul.yaml')
1667 with open(no_timer_path, 'r') as nt:
1668 self.addCommitToRepo('layout-idle', 'Removing timer jobs',
1669 {'zuul.yaml': nt.read()})
1670
Steve Varnau7b78b312015-04-03 14:49:46 -07001671 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001672 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1673
1674 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1675 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1676 self.waitUntilSettled()
1677 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1678
1679 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1680 self.waitUntilSettled()
1681
1682 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1683
James E. Blair08d19992016-08-10 15:25:31 -07001684 self.launch_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001685 self.waitUntilSettled()
1686
Arx Cruzb1b010d2013-10-28 19:49:59 -02001687 def test_zuul_url_return(self):
1688 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001689 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
James E. Blair08d19992016-08-10 15:25:31 -07001690 self.launch_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001691
1692 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001693 A.addApproval('code-review', 2)
1694 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001695 self.waitUntilSettled()
1696
1697 self.assertEqual(len(self.builds), 1)
1698 for build in self.builds:
1699 self.assertTrue('ZUUL_URL' in build.parameters)
1700
James E. Blair08d19992016-08-10 15:25:31 -07001701 self.launch_server.hold_jobs_in_build = False
1702 self.launch_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001703 self.waitUntilSettled()
1704
James E. Blair2fa50962013-01-30 21:50:41 -08001705 def test_new_patchset_dequeues_old_on_head(self):
1706 "Test that a new patchset causes the old to be dequeued (at head)"
1707 # D -> C (depends on B) -> B (depends on A) -> A -> M
James E. Blair08d19992016-08-10 15:25:31 -07001708 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001709 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1710 M.setMerged()
1711 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1712 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1713 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1714 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001715 A.addApproval('code-review', 2)
1716 B.addApproval('code-review', 2)
1717 C.addApproval('code-review', 2)
1718 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001719
1720 C.setDependsOn(B, 1)
1721 B.setDependsOn(A, 1)
1722 A.setDependsOn(M, 1)
1723
James E. Blair8b5408c2016-08-08 15:37:46 -07001724 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1725 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1726 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1727 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001728 self.waitUntilSettled()
1729
1730 A.addPatchset()
1731 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1732 self.waitUntilSettled()
1733
James E. Blair08d19992016-08-10 15:25:31 -07001734 self.launch_server.hold_jobs_in_build = False
1735 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001736 self.waitUntilSettled()
1737
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001738 self.assertEqual(A.data['status'], 'NEW')
1739 self.assertEqual(A.reported, 2)
1740 self.assertEqual(B.data['status'], 'NEW')
1741 self.assertEqual(B.reported, 2)
1742 self.assertEqual(C.data['status'], 'NEW')
1743 self.assertEqual(C.reported, 2)
1744 self.assertEqual(D.data['status'], 'MERGED')
1745 self.assertEqual(D.reported, 2)
1746 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001747
1748 def test_new_patchset_dequeues_old_without_dependents(self):
1749 "Test that a new patchset causes only the old to be dequeued"
James E. Blair08d19992016-08-10 15:25:31 -07001750 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001751 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1752 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1753 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001754 A.addApproval('code-review', 2)
1755 B.addApproval('code-review', 2)
1756 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001757
James E. Blair8b5408c2016-08-08 15:37:46 -07001758 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1759 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1760 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001761 self.waitUntilSettled()
1762
1763 B.addPatchset()
1764 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1765 self.waitUntilSettled()
1766
James E. Blair08d19992016-08-10 15:25:31 -07001767 self.launch_server.hold_jobs_in_build = False
1768 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001769 self.waitUntilSettled()
1770
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001771 self.assertEqual(A.data['status'], 'MERGED')
1772 self.assertEqual(A.reported, 2)
1773 self.assertEqual(B.data['status'], 'NEW')
1774 self.assertEqual(B.reported, 2)
1775 self.assertEqual(C.data['status'], 'MERGED')
1776 self.assertEqual(C.reported, 2)
1777 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001778
1779 def test_new_patchset_dequeues_old_independent_queue(self):
1780 "Test that a new patchset causes the old to be dequeued (independent)"
James E. Blair08d19992016-08-10 15:25:31 -07001781 self.launch_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001782 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1783 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1784 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1785 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1786 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1787 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1788 self.waitUntilSettled()
1789
1790 B.addPatchset()
1791 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1792 self.waitUntilSettled()
1793
James E. Blair08d19992016-08-10 15:25:31 -07001794 self.launch_server.hold_jobs_in_build = False
1795 self.launch_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001796 self.waitUntilSettled()
1797
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001798 self.assertEqual(A.data['status'], 'NEW')
1799 self.assertEqual(A.reported, 1)
1800 self.assertEqual(B.data['status'], 'NEW')
1801 self.assertEqual(B.reported, 1)
1802 self.assertEqual(C.data['status'], 'NEW')
1803 self.assertEqual(C.reported, 1)
1804 self.assertEqual(len(self.history), 10)
1805 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001806
James E. Blair18c64442014-03-18 10:14:45 -07001807 def test_noop_job(self):
1808 "Test that the internal noop job works"
1809 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001810 A.addApproval('code-review', 2)
1811 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001812 self.waitUntilSettled()
1813
1814 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1815 self.assertTrue(self.sched._areAllBuildsComplete())
1816 self.assertEqual(len(self.history), 0)
1817 self.assertEqual(A.data['status'], 'MERGED')
1818 self.assertEqual(A.reported, 2)
1819
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001820 def test_no_job_project(self):
1821 "Test that reports with no jobs don't get sent"
1822 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1823 'master', 'A')
1824 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1825 self.waitUntilSettled()
1826
1827 # Change wasn't reported to
1828 self.assertEqual(A.reported, False)
1829
1830 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001831 tenant = self.sched.abide.tenants.get('tenant-one')
1832 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001833 items = check_pipeline.getAllItems()
1834 self.assertEqual(len(items), 0)
1835
1836 self.assertEqual(len(self.history), 0)
1837
James E. Blair7d0dedc2013-02-21 17:26:09 -08001838 def test_zuul_refs(self):
1839 "Test that zuul refs exist and have the right changes"
James E. Blair08d19992016-08-10 15:25:31 -07001840 self.launch_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001841 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1842 M1.setMerged()
1843 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1844 M2.setMerged()
1845
1846 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1847 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1848 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1849 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001850 A.addApproval('code-review', 2)
1851 B.addApproval('code-review', 2)
1852 C.addApproval('code-review', 2)
1853 D.addApproval('code-review', 2)
1854 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1855 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1856 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1857 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001858
1859 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001860 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001861 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001862 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001863 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001864 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001865 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07001866 self.launch_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001867 self.waitUntilSettled()
1868
James E. Blair7d0dedc2013-02-21 17:26:09 -08001869 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001870 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001871 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001872 if x.parameters['ZUUL_CHANGE'] == '3':
1873 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001874 a_build = x
1875 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001876 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001877 b_build = x
1878 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001879 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001880 c_build = x
1881 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001882 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001883 d_build = x
1884 if a_build and b_build and c_build and d_build:
1885 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001886
1887 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001888 self.assertIsNotNone(a_zref)
1889 self.assertIsNotNone(b_zref)
1890 self.assertIsNotNone(c_zref)
1891 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001892
1893 # And they should all be different
1894 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001895 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001896
Clint Byrum3343e3e2016-11-15 16:05:03 -08001897 # should have a, not b, and should not be in project2
1898 self.assertTrue(a_build.hasChanges(A))
1899 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001900
Clint Byrum3343e3e2016-11-15 16:05:03 -08001901 # should have a and b, and should not be in project2
1902 self.assertTrue(b_build.hasChanges(A, B))
1903 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001904
Clint Byrum3343e3e2016-11-15 16:05:03 -08001905 # should have a and b in 1, c in 2
1906 self.assertTrue(c_build.hasChanges(A, B, C))
1907 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001908
Clint Byrum3343e3e2016-11-15 16:05:03 -08001909 # should have a and b in 1, c and d in 2
1910 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001911
James E. Blair08d19992016-08-10 15:25:31 -07001912 self.launch_server.hold_jobs_in_build = False
1913 self.launch_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001914 self.waitUntilSettled()
1915
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001916 self.assertEqual(A.data['status'], 'MERGED')
1917 self.assertEqual(A.reported, 2)
1918 self.assertEqual(B.data['status'], 'MERGED')
1919 self.assertEqual(B.reported, 2)
1920 self.assertEqual(C.data['status'], 'MERGED')
1921 self.assertEqual(C.reported, 2)
1922 self.assertEqual(D.data['status'], 'MERGED')
1923 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001924
James E. Blair4a28a882013-08-23 15:17:33 -07001925 def test_rerun_on_error(self):
1926 "Test that if a worker fails to run a job, it is run again"
James E. Blair08d19992016-08-10 15:25:31 -07001927 self.launch_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07001928 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001929 A.addApproval('code-review', 2)
1930 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07001931 self.waitUntilSettled()
1932
James E. Blair412fba82017-01-26 15:00:50 -08001933 self.builds[0].requeue = True
James E. Blair08d19992016-08-10 15:25:31 -07001934 self.launch_server.hold_jobs_in_build = False
1935 self.launch_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07001936 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08001937 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07001938 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1939
James E. Blair412e5582013-04-22 15:50:12 -07001940 def test_statsd(self):
1941 "Test each of the statsd methods used in the scheduler"
1942 import extras
1943 statsd = extras.try_import('statsd.statsd')
1944 statsd.incr('test-incr')
1945 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001946 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001947 self.assertReportedStat('test-incr', '1|c')
1948 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001949 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001950
James E. Blairec056492016-07-22 09:45:56 -07001951 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08001952 def test_stuck_job_cleanup(self):
1953 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001954 # This job won't be registered at startup because it is not in
1955 # the standard layout, but we need it to already be registerd
1956 # for when we reconfigure, as that is when Zuul will attempt
1957 # to run the new job.
1958 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001959 self.gearman_server.hold_jobs_in_queue = True
1960 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001961 A.addApproval('code-review', 2)
1962 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08001963 self.waitUntilSettled()
1964 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1965
James E. Blairf84026c2015-12-08 16:11:46 -08001966 self.updateConfigLayout(
1967 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08001968 self.sched.reconfigure(self.config)
1969 self.waitUntilSettled()
1970
James E. Blair18c64442014-03-18 10:14:45 -07001971 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001972 self.waitUntilSettled()
1973 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1974 self.assertTrue(self.sched._areAllBuildsComplete())
1975
1976 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001977 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001978 self.assertEqual(self.history[0].result, 'SUCCESS')
1979
James E. Blair879dafb2015-07-17 14:04:49 -07001980 def test_file_head(self):
1981 # This is a regression test for an observed bug. A change
1982 # with a file named "HEAD" in the root directory of the repo
1983 # was processed by a merger. It then was unable to reset the
1984 # repo because of:
1985 # GitCommandError: 'git reset --hard HEAD' returned
1986 # with exit code 128
1987 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
1988 # and filename
1989 # Use '--' to separate filenames from revisions'
1990
1991 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04001992 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07001993 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1994
1995 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1996 self.waitUntilSettled()
1997
1998 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1999 self.waitUntilSettled()
2000
2001 self.assertIn('Build succeeded', A.messages[0])
2002 self.assertIn('Build succeeded', B.messages[0])
2003
James E. Blair70c71582013-03-06 08:50:50 -08002004 def test_file_jobs(self):
2005 "Test that file jobs run only when appropriate"
2006 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002007 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002008 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002009 A.addApproval('code-review', 2)
2010 B.addApproval('code-review', 2)
2011 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2012 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002013 self.waitUntilSettled()
2014
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002015 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002016 if x.name == 'project-testfile']
2017
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002018 self.assertEqual(len(testfile_jobs), 1)
2019 self.assertEqual(testfile_jobs[0].changes, '1,2')
2020 self.assertEqual(A.data['status'], 'MERGED')
2021 self.assertEqual(A.reported, 2)
2022 self.assertEqual(B.data['status'], 'MERGED')
2023 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002024
Clint Byrum3ee39f32016-11-17 23:45:07 -08002025 def _test_irrelevant_files_jobs(self, should_skip):
2026 "Test that jobs with irrelevant-files filter run only when appropriate"
2027 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002028 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002029
2030 if should_skip:
2031 files = {'ignoreme': 'ignored\n'}
2032 else:
2033 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002034
2035 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002036 'master',
2037 'test irrelevant-files',
2038 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002039 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2040 self.waitUntilSettled()
2041
2042 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002043 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002044
2045 if should_skip:
2046 self.assertEqual([], tested_change_ids)
2047 else:
2048 self.assertIn(change.data['number'], tested_change_ids)
2049
Clint Byrum3ee39f32016-11-17 23:45:07 -08002050 def test_irrelevant_files_match_skips_job(self):
2051 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002052
Clint Byrum3ee39f32016-11-17 23:45:07 -08002053 def test_irrelevant_files_no_match_runs_job(self):
2054 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002055
Clint Byrum85493602016-11-18 11:59:47 -08002056 def test_inherited_jobs_keep_matchers(self):
2057 self.updateConfigLayout('layout-inheritance')
2058 self.sched.reconfigure(self.config)
2059
2060 files = {'ignoreme': 'ignored\n'}
2061
2062 change = self.fake_gerrit.addFakeChange('org/project',
2063 'master',
2064 'test irrelevant-files',
2065 files=files)
2066 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2067 self.waitUntilSettled()
2068
2069 run_jobs = set([build.name for build in self.history])
2070
2071 self.assertEqual(set(['project-test-nomatch-starts-empty',
2072 'project-test-nomatch-starts-full']), run_jobs)
2073
James E. Blairec056492016-07-22 09:45:56 -07002074 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002075 def test_test_config(self):
2076 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002077 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002078 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002079
James E. Blairc8a1e052014-02-25 09:29:26 -08002080 def test_queue_names(self):
2081 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002082 tenant = self.sched.abide.tenants.get('tenant-one')
2083 source = tenant.layout.pipelines['gate'].source
2084 project1 = source.getProject('org/project1')
2085 project2 = source.getProject('org/project2')
2086 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2087 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2088 self.assertEqual(q1.name, 'integrated')
2089 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002090
James E. Blair64ed6f22013-07-10 14:07:23 -07002091 def test_queue_precedence(self):
2092 "Test that queue precedence works"
2093
2094 self.gearman_server.hold_jobs_in_queue = True
James E. Blair08d19992016-08-10 15:25:31 -07002095 self.launch_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002096 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2097 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002098 A.addApproval('code-review', 2)
2099 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002100
2101 self.waitUntilSettled()
2102 self.gearman_server.hold_jobs_in_queue = False
2103 self.gearman_server.release()
2104 self.waitUntilSettled()
2105
James E. Blair8de58bd2013-07-18 16:23:33 -07002106 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002107 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07002108 self.launch_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002109 self.waitUntilSettled()
2110
James E. Blair64ed6f22013-07-10 14:07:23 -07002111 self.log.debug(self.history)
2112 self.assertEqual(self.history[0].pipeline, 'gate')
2113 self.assertEqual(self.history[1].pipeline, 'check')
2114 self.assertEqual(self.history[2].pipeline, 'gate')
2115 self.assertEqual(self.history[3].pipeline, 'gate')
2116 self.assertEqual(self.history[4].pipeline, 'check')
2117 self.assertEqual(self.history[5].pipeline, 'check')
2118
Clark Boylana5edbe42014-06-03 16:39:10 -07002119 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002120 "Test that we can retrieve JSON status info"
James E. Blair08d19992016-08-10 15:25:31 -07002121 self.launch_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002122 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002123 A.addApproval('code-review', 2)
2124 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002125 self.waitUntilSettled()
2126
James E. Blair08d19992016-08-10 15:25:31 -07002127 self.launch_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002128 self.waitUntilSettled()
2129
James E. Blair1843a552013-07-03 14:19:52 -07002130 port = self.webapp.server.socket.getsockname()[1]
2131
Adam Gandelman77a12c72017-02-03 17:43:43 -08002132 req = urllib.request.Request(
2133 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002134 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002135 headers = f.info()
2136 self.assertIn('Content-Length', headers)
2137 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002138 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2139 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002140 self.assertIn('Access-Control-Allow-Origin', headers)
2141 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002142 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002143 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002144 data = f.read()
2145
James E. Blair08d19992016-08-10 15:25:31 -07002146 self.launch_server.hold_jobs_in_build = False
2147 self.launch_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002148 self.waitUntilSettled()
2149
2150 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002151 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002152 for p in data['pipelines']:
2153 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002154 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002155 self.assertEqual(q['window'], 20)
2156 else:
2157 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002158 for head in q['heads']:
2159 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002160 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002161 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002162 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002163 status_jobs.append(job)
2164 self.assertEqual('project-merge', status_jobs[0]['name'])
2165 self.assertEqual('https://server/job/project-merge/0/',
2166 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002167 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002168 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002169 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002170 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002171 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002172 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002173 status_jobs[1]['report_url'])
2174
2175 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002176 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002177 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002178 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002179 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002180
James E. Blairec056492016-07-22 09:45:56 -07002181 @skip("Disabled for early v3 development")
James E. Blairc3d428e2013-12-03 15:06:48 -08002182 def test_merging_queues(self):
2183 "Test that transitively-connected change queues are merged"
James E. Blairf84026c2015-12-08 16:11:46 -08002184 self.updateConfigLayout(
2185 'tests/fixtures/layout-merge-queues.yaml')
James E. Blairc3d428e2013-12-03 15:06:48 -08002186 self.sched.reconfigure(self.config)
2187 self.assertEqual(len(self.sched.layout.pipelines['gate'].queues), 1)
2188
James E. Blairaf17a972016-02-03 15:07:18 -08002189 def test_mutex(self):
2190 "Test job mutexes"
Adam Gandelmanea9ac9b2017-02-03 16:59:58 -08002191 self.updateConfigLayout('layout-mutex')
James E. Blairaf17a972016-02-03 15:07:18 -08002192 self.sched.reconfigure(self.config)
2193
James E. Blair08d19992016-08-10 15:25:31 -07002194 self.launch_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2197 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2198
2199 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2200 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2201 self.waitUntilSettled()
2202 self.assertEqual(len(self.builds), 3)
2203 self.assertEqual(self.builds[0].name, 'project-test1')
2204 self.assertEqual(self.builds[1].name, 'mutex-one')
2205 self.assertEqual(self.builds[2].name, 'project-test1')
2206
James E. Blair08d19992016-08-10 15:25:31 -07002207 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002208 self.waitUntilSettled()
2209
2210 self.assertEqual(len(self.builds), 3)
2211 self.assertEqual(self.builds[0].name, 'project-test1')
2212 self.assertEqual(self.builds[1].name, 'project-test1')
2213 self.assertEqual(self.builds[2].name, 'mutex-two')
2214 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2215
James E. Blair08d19992016-08-10 15:25:31 -07002216 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002217 self.waitUntilSettled()
2218
2219 self.assertEqual(len(self.builds), 3)
2220 self.assertEqual(self.builds[0].name, 'project-test1')
2221 self.assertEqual(self.builds[1].name, 'project-test1')
2222 self.assertEqual(self.builds[2].name, 'mutex-one')
2223 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2224
James E. Blair08d19992016-08-10 15:25:31 -07002225 self.launch_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002226 self.waitUntilSettled()
2227
2228 self.assertEqual(len(self.builds), 3)
2229 self.assertEqual(self.builds[0].name, 'project-test1')
2230 self.assertEqual(self.builds[1].name, 'project-test1')
2231 self.assertEqual(self.builds[2].name, 'mutex-two')
2232 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2233
James E. Blair08d19992016-08-10 15:25:31 -07002234 self.launch_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002235 self.waitUntilSettled()
2236
2237 self.assertEqual(len(self.builds), 2)
2238 self.assertEqual(self.builds[0].name, 'project-test1')
2239 self.assertEqual(self.builds[1].name, 'project-test1')
2240 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2241
James E. Blair08d19992016-08-10 15:25:31 -07002242 self.launch_server.hold_jobs_in_build = False
2243 self.launch_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002244
2245 self.waitUntilSettled()
2246 self.assertEqual(len(self.builds), 0)
2247
2248 self.assertEqual(A.reported, 1)
2249 self.assertEqual(B.reported, 1)
2250 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2251
James E. Blaircdccd972013-07-01 12:10:22 -07002252 def test_live_reconfiguration(self):
2253 "Test that live reconfiguration works"
James E. Blair08d19992016-08-10 15:25:31 -07002254 self.launch_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002255 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002256 A.addApproval('code-review', 2)
2257 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002258 self.waitUntilSettled()
2259
2260 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002261 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002262
James E. Blair08d19992016-08-10 15:25:31 -07002263 self.launch_server.hold_jobs_in_build = False
2264 self.launch_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002265 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002266 self.assertEqual(self.getJobFromHistory('project-merge').result,
2267 'SUCCESS')
2268 self.assertEqual(self.getJobFromHistory('project-test1').result,
2269 'SUCCESS')
2270 self.assertEqual(self.getJobFromHistory('project-test2').result,
2271 'SUCCESS')
2272 self.assertEqual(A.data['status'], 'MERGED')
2273 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002274
James E. Blairec056492016-07-22 09:45:56 -07002275 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002276 def test_live_reconfiguration_merge_conflict(self):
2277 # A real-world bug: a change in a gate queue has a merge
2278 # conflict and a job is added to its project while it's
2279 # sitting in the queue. The job gets added to the change and
2280 # enqueued and the change gets stuck.
2281 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002282 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002283
2284 # This change is fine. It's here to stop the queue long
2285 # enough for the next change to be subject to the
2286 # reconfiguration, as well as to provide a conflict for the
2287 # next change. This change will succeed and merge.
2288 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2289 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002290 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002291
2292 # This change will be in merge conflict. During the
2293 # reconfiguration, we will add a job. We want to make sure
2294 # that doesn't cause it to get stuck.
2295 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2296 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002297 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002298
James E. Blair8b5408c2016-08-08 15:37:46 -07002299 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2300 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002301
2302 self.waitUntilSettled()
2303
2304 # No jobs have run yet
2305 self.assertEqual(A.data['status'], 'NEW')
2306 self.assertEqual(A.reported, 1)
2307 self.assertEqual(B.data['status'], 'NEW')
2308 self.assertEqual(B.reported, 1)
2309 self.assertEqual(len(self.history), 0)
2310
2311 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002312 self.updateConfigLayout(
2313 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002314 self.sched.reconfigure(self.config)
2315 self.waitUntilSettled()
2316
James E. Blair08d19992016-08-10 15:25:31 -07002317 self.launch_server.hold_jobs_in_build = False
2318 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002319 self.waitUntilSettled()
2320
2321 self.assertEqual(A.data['status'], 'MERGED')
2322 self.assertEqual(A.reported, 2)
2323 self.assertEqual(B.data['status'], 'NEW')
2324 self.assertEqual(B.reported, 2)
2325 self.assertEqual(self.getJobFromHistory('project-merge').result,
2326 'SUCCESS')
2327 self.assertEqual(self.getJobFromHistory('project-test1').result,
2328 'SUCCESS')
2329 self.assertEqual(self.getJobFromHistory('project-test2').result,
2330 'SUCCESS')
2331 self.assertEqual(self.getJobFromHistory('project-test3').result,
2332 'SUCCESS')
2333 self.assertEqual(len(self.history), 4)
2334
James E. Blairec056492016-07-22 09:45:56 -07002335 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002336 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002337 # An extrapolation of test_live_reconfiguration_merge_conflict
2338 # that tests a job added to a job tree with a failed root does
2339 # not run.
2340 self.worker.registerFunction('build:project-test3')
James E. Blair08d19992016-08-10 15:25:31 -07002341 self.launch_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002342
2343 # This change is fine. It's here to stop the queue long
2344 # enough for the next change to be subject to the
2345 # reconfiguration. This change will succeed and merge.
2346 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2347 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002348 A.addApproval('code-review', 2)
2349 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002350 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002351 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002352 self.waitUntilSettled()
2353
2354 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair08d19992016-08-10 15:25:31 -07002355 self.launch_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002356 B.addApproval('code-review', 2)
2357 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002358 self.waitUntilSettled()
2359
James E. Blair08d19992016-08-10 15:25:31 -07002360 self.launch_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002361 self.waitUntilSettled()
2362
2363 # Both -merge jobs have run, but no others.
2364 self.assertEqual(A.data['status'], 'NEW')
2365 self.assertEqual(A.reported, 1)
2366 self.assertEqual(B.data['status'], 'NEW')
2367 self.assertEqual(B.reported, 1)
2368 self.assertEqual(self.history[0].result, 'SUCCESS')
2369 self.assertEqual(self.history[0].name, 'project-merge')
2370 self.assertEqual(self.history[1].result, 'FAILURE')
2371 self.assertEqual(self.history[1].name, 'project-merge')
2372 self.assertEqual(len(self.history), 2)
2373
2374 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002375 self.updateConfigLayout(
2376 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002377 self.sched.reconfigure(self.config)
2378 self.waitUntilSettled()
2379
James E. Blair08d19992016-08-10 15:25:31 -07002380 self.launch_server.hold_jobs_in_build = False
2381 self.launch_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002382 self.waitUntilSettled()
2383
2384 self.assertEqual(A.data['status'], 'MERGED')
2385 self.assertEqual(A.reported, 2)
2386 self.assertEqual(B.data['status'], 'NEW')
2387 self.assertEqual(B.reported, 2)
2388 self.assertEqual(self.history[0].result, 'SUCCESS')
2389 self.assertEqual(self.history[0].name, 'project-merge')
2390 self.assertEqual(self.history[1].result, 'FAILURE')
2391 self.assertEqual(self.history[1].name, 'project-merge')
2392 self.assertEqual(self.history[2].result, 'SUCCESS')
2393 self.assertEqual(self.history[3].result, 'SUCCESS')
2394 self.assertEqual(self.history[4].result, 'SUCCESS')
2395 self.assertEqual(len(self.history), 5)
2396
James E. Blairec056492016-07-22 09:45:56 -07002397 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002398 def test_live_reconfiguration_failed_job(self):
2399 # Test that a change with a removed failing job does not
2400 # disrupt reconfiguration. If a change has a failed job and
2401 # that job is removed during a reconfiguration, we observed a
2402 # bug where the code to re-set build statuses would run on
2403 # that build and raise an exception because the job no longer
2404 # existed.
James E. Blair08d19992016-08-10 15:25:31 -07002405 self.launch_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002406
2407 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2408
2409 # This change will fail and later be removed by the reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002410 self.launch_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002411
2412 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2413 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002414 self.launch_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002415 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002416 self.launch_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002417 self.waitUntilSettled()
2418
2419 self.assertEqual(A.data['status'], 'NEW')
2420 self.assertEqual(A.reported, 0)
2421
2422 self.assertEqual(self.getJobFromHistory('project-merge').result,
2423 'SUCCESS')
2424 self.assertEqual(self.getJobFromHistory('project-test1').result,
2425 'FAILURE')
2426 self.assertEqual(len(self.history), 2)
2427
2428 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002429 self.updateConfigLayout(
2430 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002431 self.sched.reconfigure(self.config)
2432 self.waitUntilSettled()
2433
James E. Blair08d19992016-08-10 15:25:31 -07002434 self.launch_server.hold_jobs_in_build = False
2435 self.launch_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002436 self.waitUntilSettled()
2437
2438 self.assertEqual(self.getJobFromHistory('project-test2').result,
2439 'SUCCESS')
2440 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2441 'SUCCESS')
2442 self.assertEqual(len(self.history), 4)
2443
2444 self.assertEqual(A.data['status'], 'NEW')
2445 self.assertEqual(A.reported, 1)
2446 self.assertIn('Build succeeded', A.messages[0])
2447 # Ensure the removed job was not included in the report.
2448 self.assertNotIn('project-test1', A.messages[0])
2449
James E. Blairec056492016-07-22 09:45:56 -07002450 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002451 def test_live_reconfiguration_shared_queue(self):
2452 # Test that a change with a failing job which was removed from
2453 # this project but otherwise still exists in the system does
2454 # not disrupt reconfiguration.
2455
James E. Blair08d19992016-08-10 15:25:31 -07002456 self.launch_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002457
2458 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2459
James E. Blair08d19992016-08-10 15:25:31 -07002460 self.launch_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002461
2462 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2463 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002464 self.launch_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002465 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002466 self.launch_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002467 self.waitUntilSettled()
2468
2469 self.assertEqual(A.data['status'], 'NEW')
2470 self.assertEqual(A.reported, 0)
2471
2472 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2473 'SUCCESS')
2474 self.assertEqual(self.getJobFromHistory(
2475 'project1-project2-integration').result, 'FAILURE')
2476 self.assertEqual(len(self.history), 2)
2477
2478 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002479 self.updateConfigLayout(
2480 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002481 self.sched.reconfigure(self.config)
2482 self.waitUntilSettled()
2483
James E. Blair08d19992016-08-10 15:25:31 -07002484 self.launch_server.hold_jobs_in_build = False
2485 self.launch_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002486 self.waitUntilSettled()
2487
2488 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2489 'SUCCESS')
2490 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2491 'SUCCESS')
2492 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2493 'SUCCESS')
2494 self.assertEqual(self.getJobFromHistory(
2495 'project1-project2-integration').result, 'FAILURE')
2496 self.assertEqual(len(self.history), 4)
2497
2498 self.assertEqual(A.data['status'], 'NEW')
2499 self.assertEqual(A.reported, 1)
2500 self.assertIn('Build succeeded', A.messages[0])
2501 # Ensure the removed job was not included in the report.
2502 self.assertNotIn('project1-project2-integration', A.messages[0])
2503
James E. Blairec056492016-07-22 09:45:56 -07002504 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002505 def test_double_live_reconfiguration_shared_queue(self):
2506 # This was a real-world regression. A change is added to
2507 # gate; a reconfigure happens, a second change which depends
2508 # on the first is added, and a second reconfiguration happens.
2509 # Ensure that both changes merge.
2510
2511 # A failure may indicate incorrect caching or cleaning up of
2512 # references during a reconfiguration.
James E. Blair08d19992016-08-10 15:25:31 -07002513 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002514
2515 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2516 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2517 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002518 A.addApproval('code-review', 2)
2519 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002520
2521 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002522 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002523 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002524 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002525 self.waitUntilSettled()
2526
2527 # Reconfigure (with only one change in the pipeline).
2528 self.sched.reconfigure(self.config)
2529 self.waitUntilSettled()
2530
2531 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002532 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002533 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002534 self.launch_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002535 self.waitUntilSettled()
2536
2537 # Reconfigure (with both in the pipeline).
2538 self.sched.reconfigure(self.config)
2539 self.waitUntilSettled()
2540
James E. Blair08d19992016-08-10 15:25:31 -07002541 self.launch_server.hold_jobs_in_build = False
2542 self.launch_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002543 self.waitUntilSettled()
2544
2545 self.assertEqual(len(self.history), 8)
2546
2547 self.assertEqual(A.data['status'], 'MERGED')
2548 self.assertEqual(A.reported, 2)
2549 self.assertEqual(B.data['status'], 'MERGED')
2550 self.assertEqual(B.reported, 2)
2551
James E. Blairec056492016-07-22 09:45:56 -07002552 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002553 def test_live_reconfiguration_del_project(self):
2554 # Test project deletion from layout
2555 # while changes are enqueued
2556
James E. Blair08d19992016-08-10 15:25:31 -07002557 self.launch_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002558 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2559 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2560 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2561
2562 # A Depends-On: B
2563 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2564 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002565 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002566
2567 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2568 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2569 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002570 self.launch_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002571 self.waitUntilSettled()
2572 self.assertEqual(len(self.builds), 5)
2573
2574 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002575 self.updateConfigLayout(
2576 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002577 self.sched.reconfigure(self.config)
2578 self.waitUntilSettled()
2579
2580 # Builds for C aborted, builds for A succeed,
2581 # and have change B applied ahead
2582 job_c = self.getJobFromHistory('project1-test1')
2583 self.assertEqual(job_c.changes, '3,1')
2584 self.assertEqual(job_c.result, 'ABORTED')
2585
James E. Blair08d19992016-08-10 15:25:31 -07002586 self.launch_server.hold_jobs_in_build = False
2587 self.launch_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002588 self.waitUntilSettled()
2589
2590 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2591 '2,1 1,1')
2592
2593 self.assertEqual(A.data['status'], 'NEW')
2594 self.assertEqual(B.data['status'], 'NEW')
2595 self.assertEqual(C.data['status'], 'NEW')
2596 self.assertEqual(A.reported, 1)
2597 self.assertEqual(B.reported, 0)
2598 self.assertEqual(C.reported, 0)
2599
2600 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2601 self.assertIn('Build succeeded', A.messages[0])
2602
James E. Blairec056492016-07-22 09:45:56 -07002603 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002604 def test_live_reconfiguration_functions(self):
2605 "Test live reconfiguration with a custom function"
2606 self.worker.registerFunction('build:node-project-test1:debian')
2607 self.worker.registerFunction('build:node-project-test1:wheezy')
2608 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002609 A.addApproval('code-review', 2)
2610 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002611 self.waitUntilSettled()
2612
2613 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2614 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2615 'debian')
2616 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2617
James E. Blairf84026c2015-12-08 16:11:46 -08002618 self.updateConfigLayout(
2619 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002620 self.sched.reconfigure(self.config)
2621 self.worker.build_history = []
2622
2623 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002624 B.addApproval('code-review', 2)
2625 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002626 self.waitUntilSettled()
2627
2628 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2629 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2630 'wheezy')
2631 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2632
James E. Blairec056492016-07-22 09:45:56 -07002633 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002634 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002635 self.updateConfigLayout(
2636 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002637 self.sched.reconfigure(self.config)
2638
2639 self.init_repo("org/new-project")
2640 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2641
James E. Blair8b5408c2016-08-08 15:37:46 -07002642 A.addApproval('code-review', 2)
2643 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002644 self.waitUntilSettled()
2645 self.assertEqual(self.getJobFromHistory('project-merge').result,
2646 'SUCCESS')
2647 self.assertEqual(self.getJobFromHistory('project-test1').result,
2648 'SUCCESS')
2649 self.assertEqual(self.getJobFromHistory('project-test2').result,
2650 'SUCCESS')
2651 self.assertEqual(A.data['status'], 'MERGED')
2652 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002653
Clark Boylan6dbbc482013-10-18 10:57:31 -07002654 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002655 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002656 self.sched.reconfigure(self.config)
2657
2658 self.init_repo("org/delete-project")
2659 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2660
James E. Blair8b5408c2016-08-08 15:37:46 -07002661 A.addApproval('code-review', 2)
2662 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002663 self.waitUntilSettled()
2664 self.assertEqual(self.getJobFromHistory('project-merge').result,
2665 'SUCCESS')
2666 self.assertEqual(self.getJobFromHistory('project-test1').result,
2667 'SUCCESS')
2668 self.assertEqual(self.getJobFromHistory('project-test2').result,
2669 'SUCCESS')
2670 self.assertEqual(A.data['status'], 'MERGED')
2671 self.assertEqual(A.reported, 2)
2672
2673 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002674 p = 'org/delete-project'
2675 if os.path.exists(os.path.join(self.merger_git_root, p)):
2676 shutil.rmtree(os.path.join(self.merger_git_root, p))
2677 if os.path.exists(os.path.join(self.launcher_git_root, p)):
2678 shutil.rmtree(os.path.join(self.launcher_git_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002679
2680 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2681
James E. Blair8b5408c2016-08-08 15:37:46 -07002682 B.addApproval('code-review', 2)
2683 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002684 self.waitUntilSettled()
2685 self.assertEqual(self.getJobFromHistory('project-merge').result,
2686 'SUCCESS')
2687 self.assertEqual(self.getJobFromHistory('project-test1').result,
2688 'SUCCESS')
2689 self.assertEqual(self.getJobFromHistory('project-test2').result,
2690 'SUCCESS')
2691 self.assertEqual(B.data['status'], 'MERGED')
2692 self.assertEqual(B.reported, 2)
2693
James E. Blairec056492016-07-22 09:45:56 -07002694 @skip("Disabled for early v3 development")
James E. Blair456f2fb2016-02-09 09:29:33 -08002695 def test_tags(self):
2696 "Test job tags"
2697 self.config.set('zuul', 'layout_config',
2698 'tests/fixtures/layout-tags.yaml')
2699 self.sched.reconfigure(self.config)
2700
2701 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2702 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2703 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2704 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2705 self.waitUntilSettled()
2706
2707 results = {'project1-merge': 'extratag merge project1',
2708 'project2-merge': 'merge'}
2709
2710 for build in self.history:
2711 self.assertEqual(results.get(build.name, ''),
2712 build.parameters.get('BUILD_TAGS'))
2713
James E. Blairec056492016-07-22 09:45:56 -07002714 @skip("Disabled for early v3 development")
James E. Blair63bb0ef2013-07-29 17:14:51 -07002715 def test_timer(self):
2716 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002717 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002718 self.updateConfigLayout(
2719 'tests/fixtures/layout-timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002720 self.sched.reconfigure(self.config)
2721 self.registerJobs()
2722
Clark Boylan3ee090a2014-04-03 20:55:09 -07002723 # The pipeline triggers every second, so we should have seen
2724 # several by now.
2725 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002726 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002727
2728 self.assertEqual(len(self.builds), 2)
2729
James E. Blair63bb0ef2013-07-29 17:14:51 -07002730 port = self.webapp.server.socket.getsockname()[1]
2731
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002732 req = urllib.request.Request("http://localhost:%s/status.json" % port)
2733 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002734 data = f.read()
2735
James E. Blair08d19992016-08-10 15:25:31 -07002736 self.launch_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002737 # Stop queuing timer triggered jobs so that the assertions
2738 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002739 self.updateConfigLayout(
2740 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002741 self.sched.reconfigure(self.config)
2742 self.registerJobs()
James E. Blair08d19992016-08-10 15:25:31 -07002743 self.launch_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002744 self.waitUntilSettled()
2745
2746 self.assertEqual(self.getJobFromHistory(
2747 'project-bitrot-stable-old').result, 'SUCCESS')
2748 self.assertEqual(self.getJobFromHistory(
2749 'project-bitrot-stable-older').result, 'SUCCESS')
2750
2751 data = json.loads(data)
2752 status_jobs = set()
2753 for p in data['pipelines']:
2754 for q in p['change_queues']:
2755 for head in q['heads']:
2756 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002757 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002758 for job in change['jobs']:
2759 status_jobs.add(job['name'])
2760 self.assertIn('project-bitrot-stable-old', status_jobs)
2761 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002762
James E. Blairec056492016-07-22 09:45:56 -07002763 @skip("Disabled for early v3 development")
James E. Blair4f6033c2014-03-27 15:49:09 -07002764 def test_idle(self):
2765 "Test that frequent periodic jobs work"
James E. Blair08d19992016-08-10 15:25:31 -07002766 self.launch_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002767
Clark Boylan3ee090a2014-04-03 20:55:09 -07002768 for x in range(1, 3):
2769 # Test that timer triggers periodic jobs even across
2770 # layout config reloads.
2771 # Start timer trigger
James E. Blairf84026c2015-12-08 16:11:46 -08002772 self.updateConfigLayout(
2773 'tests/fixtures/layout-idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002774 self.sched.reconfigure(self.config)
2775 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002776 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002777
Clark Boylan3ee090a2014-04-03 20:55:09 -07002778 # The pipeline triggers every second, so we should have seen
2779 # several by now.
2780 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002781
2782 # Stop queuing timer triggered jobs so that the assertions
2783 # below don't race against more jobs being queued.
James E. Blairf84026c2015-12-08 16:11:46 -08002784 self.updateConfigLayout(
2785 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002786 self.sched.reconfigure(self.config)
2787 self.registerJobs()
James E. Blair995fc0f2016-02-04 16:48:31 -08002788 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002789
2790 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002791 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002792 self.waitUntilSettled()
2793 self.assertEqual(len(self.builds), 0)
2794 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002795
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002796 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002797 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002798 self.sched.reconfigure(self.config)
2799
2800 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2801 self.waitUntilSettled()
2802
2803 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2804 self.waitUntilSettled()
2805
James E. Blairff80a2f2013-12-27 13:24:06 -08002806 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002807
2808 # A.messages only holds what FakeGerrit places in it. Thus we
2809 # work on the knowledge of what the first message should be as
2810 # it is only configured to go to SMTP.
2811
2812 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002813 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002814 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002815 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002816 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002817 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002818
2819 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002820 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002821 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002822 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002823 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002824 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002825
James E. Blairec056492016-07-22 09:45:56 -07002826 @skip("Disabled for early v3 development")
James E. Blaire5910202013-12-27 09:50:31 -08002827 def test_timer_smtp(self):
2828 "Test that a periodic job is triggered"
James E. Blair08d19992016-08-10 15:25:31 -07002829 self.launch_server.hold_jobs_in_build = True
James E. Blairf84026c2015-12-08 16:11:46 -08002830 self.updateConfigLayout(
2831 'tests/fixtures/layout-timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002832 self.sched.reconfigure(self.config)
2833 self.registerJobs()
2834
Clark Boylan3ee090a2014-04-03 20:55:09 -07002835 # The pipeline triggers every second, so we should have seen
2836 # several by now.
2837 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002838 self.waitUntilSettled()
2839
Clark Boylan3ee090a2014-04-03 20:55:09 -07002840 self.assertEqual(len(self.builds), 2)
James E. Blair08d19992016-08-10 15:25:31 -07002841 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002842 self.waitUntilSettled()
2843 self.assertEqual(len(self.history), 2)
2844
James E. Blaire5910202013-12-27 09:50:31 -08002845 self.assertEqual(self.getJobFromHistory(
2846 'project-bitrot-stable-old').result, 'SUCCESS')
2847 self.assertEqual(self.getJobFromHistory(
2848 'project-bitrot-stable-older').result, 'SUCCESS')
2849
James E. Blairff80a2f2013-12-27 13:24:06 -08002850 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002851
2852 # A.messages only holds what FakeGerrit places in it. Thus we
2853 # work on the knowledge of what the first message should be as
2854 # it is only configured to go to SMTP.
2855
2856 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002857 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002858 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002859 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002860 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002861 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002862
Clark Boylan3ee090a2014-04-03 20:55:09 -07002863 # Stop queuing timer triggered jobs and let any that may have
2864 # queued through so that end of test assertions pass.
James E. Blairf84026c2015-12-08 16:11:46 -08002865 self.updateConfigLayout(
2866 'tests/fixtures/layout-no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002867 self.sched.reconfigure(self.config)
2868 self.registerJobs()
James E. Blairf8058972014-08-15 16:09:16 -07002869 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07002870 self.launch_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002871 self.waitUntilSettled()
2872
James E. Blair91e34592015-07-31 16:45:59 -07002873 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002874 "Test that the RPC client can enqueue a change"
2875 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002876 A.addApproval('code-review', 2)
2877 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002878
2879 client = zuul.rpcclient.RPCClient('127.0.0.1',
2880 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002881 r = client.enqueue(tenant='tenant-one',
2882 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002883 project='org/project',
2884 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002885 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002886 self.waitUntilSettled()
2887 self.assertEqual(self.getJobFromHistory('project-merge').result,
2888 'SUCCESS')
2889 self.assertEqual(self.getJobFromHistory('project-test1').result,
2890 'SUCCESS')
2891 self.assertEqual(self.getJobFromHistory('project-test2').result,
2892 'SUCCESS')
2893 self.assertEqual(A.data['status'], 'MERGED')
2894 self.assertEqual(A.reported, 2)
2895 self.assertEqual(r, True)
2896
James E. Blair91e34592015-07-31 16:45:59 -07002897 def test_client_enqueue_ref(self):
2898 "Test that the RPC client can enqueue a ref"
2899
2900 client = zuul.rpcclient.RPCClient('127.0.0.1',
2901 self.gearman_server.port)
2902 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002903 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002904 pipeline='post',
2905 project='org/project',
2906 trigger='gerrit',
2907 ref='master',
2908 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2909 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2910 self.waitUntilSettled()
2911 job_names = [x.name for x in self.history]
2912 self.assertEqual(len(self.history), 1)
2913 self.assertIn('project-post', job_names)
2914 self.assertEqual(r, True)
2915
James E. Blairad28e912013-11-27 10:43:22 -08002916 def test_client_enqueue_negative(self):
2917 "Test that the RPC client returns errors"
2918 client = zuul.rpcclient.RPCClient('127.0.0.1',
2919 self.gearman_server.port)
2920 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002921 "Invalid tenant"):
2922 r = client.enqueue(tenant='tenant-foo',
2923 pipeline='gate',
2924 project='org/project',
2925 trigger='gerrit',
2926 change='1,1')
2927 client.shutdown()
2928 self.assertEqual(r, False)
2929
2930 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002931 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002932 r = client.enqueue(tenant='tenant-one',
2933 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002934 project='project-does-not-exist',
2935 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002936 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002937 client.shutdown()
2938 self.assertEqual(r, False)
2939
2940 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2941 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002942 r = client.enqueue(tenant='tenant-one',
2943 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002944 project='org/project',
2945 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002946 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002947 client.shutdown()
2948 self.assertEqual(r, False)
2949
2950 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2951 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002952 r = client.enqueue(tenant='tenant-one',
2953 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002954 project='org/project',
2955 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002956 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002957 client.shutdown()
2958 self.assertEqual(r, False)
2959
2960 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2961 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002962 r = client.enqueue(tenant='tenant-one',
2963 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002964 project='org/project',
2965 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002966 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002967 client.shutdown()
2968 self.assertEqual(r, False)
2969
2970 self.waitUntilSettled()
2971 self.assertEqual(len(self.history), 0)
2972 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08002973
2974 def test_client_promote(self):
2975 "Test that the RPC client can promote a change"
James E. Blair08d19992016-08-10 15:25:31 -07002976 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08002977 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2978 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2979 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07002980 A.addApproval('code-review', 2)
2981 B.addApproval('code-review', 2)
2982 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08002983
James E. Blair8b5408c2016-08-08 15:37:46 -07002984 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2985 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2986 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08002987
2988 self.waitUntilSettled()
2989
Paul Belangerbaca3132016-11-04 12:49:54 -04002990 tenant = self.sched.abide.tenants.get('tenant-one')
2991 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05002992 enqueue_times = {}
2993 for item in items:
2994 enqueue_times[str(item.change)] = item.enqueue_time
2995
James E. Blair36658cf2013-12-06 17:53:48 -08002996 client = zuul.rpcclient.RPCClient('127.0.0.1',
2997 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002998 r = client.promote(tenant='tenant-one',
2999 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003000 change_ids=['2,1', '3,1'])
3001
Sean Daguef39b9ca2014-01-10 21:34:35 -05003002 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003003 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003004 for item in items:
3005 self.assertEqual(
3006 enqueue_times[str(item.change)], item.enqueue_time)
3007
James E. Blair78acec92014-02-06 07:11:32 -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()
James E. Blair08d19992016-08-10 15:25:31 -07003011 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003012 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003013 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003014 self.waitUntilSettled()
3015
3016 self.assertEqual(len(self.builds), 6)
3017 self.assertEqual(self.builds[0].name, 'project-test1')
3018 self.assertEqual(self.builds[1].name, 'project-test2')
3019 self.assertEqual(self.builds[2].name, 'project-test1')
3020 self.assertEqual(self.builds[3].name, 'project-test2')
3021 self.assertEqual(self.builds[4].name, 'project-test1')
3022 self.assertEqual(self.builds[5].name, 'project-test2')
3023
Paul Belangerbaca3132016-11-04 12:49:54 -04003024 self.assertTrue(self.builds[0].hasChanges(B))
3025 self.assertFalse(self.builds[0].hasChanges(A))
3026 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003027
Paul Belangerbaca3132016-11-04 12:49:54 -04003028 self.assertTrue(self.builds[2].hasChanges(B))
3029 self.assertTrue(self.builds[2].hasChanges(C))
3030 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003031
Paul Belangerbaca3132016-11-04 12:49:54 -04003032 self.assertTrue(self.builds[4].hasChanges(B))
3033 self.assertTrue(self.builds[4].hasChanges(C))
3034 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003035
James E. Blair08d19992016-08-10 15:25:31 -07003036 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003037 self.waitUntilSettled()
3038
3039 self.assertEqual(A.data['status'], 'MERGED')
3040 self.assertEqual(A.reported, 2)
3041 self.assertEqual(B.data['status'], 'MERGED')
3042 self.assertEqual(B.reported, 2)
3043 self.assertEqual(C.data['status'], 'MERGED')
3044 self.assertEqual(C.reported, 2)
3045
3046 client.shutdown()
3047 self.assertEqual(r, True)
3048
3049 def test_client_promote_dependent(self):
3050 "Test that the RPC client can promote a dependent change"
3051 # C (depends on B) -> B -> A ; then promote C to get:
3052 # A -> C (depends on B) -> B
James E. Blair08d19992016-08-10 15:25:31 -07003053 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3055 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3056 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3057
3058 C.setDependsOn(B, 1)
3059
James E. Blair8b5408c2016-08-08 15:37:46 -07003060 A.addApproval('code-review', 2)
3061 B.addApproval('code-review', 2)
3062 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003063
James E. Blair8b5408c2016-08-08 15:37:46 -07003064 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3065 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3066 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003067
3068 self.waitUntilSettled()
3069
3070 client = zuul.rpcclient.RPCClient('127.0.0.1',
3071 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003072 r = client.promote(tenant='tenant-one',
3073 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003074 change_ids=['3,1'])
3075
James E. Blair78acec92014-02-06 07:11:32 -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()
James E. Blair08d19992016-08-10 15:25:31 -07003079 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003080 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003081 self.launch_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003082 self.waitUntilSettled()
3083
3084 self.assertEqual(len(self.builds), 6)
3085 self.assertEqual(self.builds[0].name, 'project-test1')
3086 self.assertEqual(self.builds[1].name, 'project-test2')
3087 self.assertEqual(self.builds[2].name, 'project-test1')
3088 self.assertEqual(self.builds[3].name, 'project-test2')
3089 self.assertEqual(self.builds[4].name, 'project-test1')
3090 self.assertEqual(self.builds[5].name, 'project-test2')
3091
Paul Belangerbaca3132016-11-04 12:49:54 -04003092 self.assertTrue(self.builds[0].hasChanges(B))
3093 self.assertFalse(self.builds[0].hasChanges(A))
3094 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003095
Paul Belangerbaca3132016-11-04 12:49:54 -04003096 self.assertTrue(self.builds[2].hasChanges(B))
3097 self.assertTrue(self.builds[2].hasChanges(C))
3098 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003099
Paul Belangerbaca3132016-11-04 12:49:54 -04003100 self.assertTrue(self.builds[4].hasChanges(B))
3101 self.assertTrue(self.builds[4].hasChanges(C))
3102 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003103
James E. Blair08d19992016-08-10 15:25:31 -07003104 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003105 self.waitUntilSettled()
3106
3107 self.assertEqual(A.data['status'], 'MERGED')
3108 self.assertEqual(A.reported, 2)
3109 self.assertEqual(B.data['status'], 'MERGED')
3110 self.assertEqual(B.reported, 2)
3111 self.assertEqual(C.data['status'], 'MERGED')
3112 self.assertEqual(C.reported, 2)
3113
3114 client.shutdown()
3115 self.assertEqual(r, True)
3116
3117 def test_client_promote_negative(self):
3118 "Test that the RPC client returns errors for promotion"
James E. Blair08d19992016-08-10 15:25:31 -07003119 self.launch_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003120 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003121 A.addApproval('code-review', 2)
3122 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003123 self.waitUntilSettled()
3124
3125 client = zuul.rpcclient.RPCClient('127.0.0.1',
3126 self.gearman_server.port)
3127
3128 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003129 r = client.promote(tenant='tenant-one',
3130 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003131 change_ids=['2,1', '3,1'])
3132 client.shutdown()
3133 self.assertEqual(r, False)
3134
3135 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003136 r = client.promote(tenant='tenant-one',
3137 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003138 change_ids=['4,1'])
3139 client.shutdown()
3140 self.assertEqual(r, False)
3141
James E. Blair08d19992016-08-10 15:25:31 -07003142 self.launch_server.hold_jobs_in_build = False
3143 self.launch_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003144 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003145
James E. Blairec056492016-07-22 09:45:56 -07003146 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003147 def test_queue_rate_limiting(self):
3148 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003149 self.updateConfigLayout(
3150 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003151 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003152 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3154 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3155 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3156
3157 C.setDependsOn(B, 1)
James E. Blair08d19992016-08-10 15:25:31 -07003158 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003159
James E. Blair8b5408c2016-08-08 15:37:46 -07003160 A.addApproval('code-review', 2)
3161 B.addApproval('code-review', 2)
3162 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003163
James E. Blair8b5408c2016-08-08 15:37:46 -07003164 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3165 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3166 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003167 self.waitUntilSettled()
3168
3169 # Only A and B will have their merge jobs queued because
3170 # window is 2.
3171 self.assertEqual(len(self.builds), 2)
3172 self.assertEqual(self.builds[0].name, 'project-merge')
3173 self.assertEqual(self.builds[1].name, 'project-merge')
3174
James E. Blair08d19992016-08-10 15:25:31 -07003175 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003176 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003177 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003178 self.waitUntilSettled()
3179
3180 # Only A and B will have their test jobs queued because
3181 # window is 2.
3182 self.assertEqual(len(self.builds), 4)
3183 self.assertEqual(self.builds[0].name, 'project-test1')
3184 self.assertEqual(self.builds[1].name, 'project-test2')
3185 self.assertEqual(self.builds[2].name, 'project-test1')
3186 self.assertEqual(self.builds[3].name, 'project-test2')
3187
James E. Blair08d19992016-08-10 15:25:31 -07003188 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003189 self.waitUntilSettled()
3190
3191 queue = self.sched.layout.pipelines['gate'].queues[0]
3192 # A failed so window is reduced by 1 to 1.
3193 self.assertEqual(queue.window, 1)
3194 self.assertEqual(queue.window_floor, 1)
3195 self.assertEqual(A.data['status'], 'NEW')
3196
3197 # Gate is reset and only B's merge job is queued because
3198 # window shrunk to 1.
3199 self.assertEqual(len(self.builds), 1)
3200 self.assertEqual(self.builds[0].name, 'project-merge')
3201
James E. Blair08d19992016-08-10 15:25:31 -07003202 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003203 self.waitUntilSettled()
3204
3205 # Only B's test jobs are queued because window is still 1.
3206 self.assertEqual(len(self.builds), 2)
3207 self.assertEqual(self.builds[0].name, 'project-test1')
3208 self.assertEqual(self.builds[1].name, 'project-test2')
3209
James E. Blair08d19992016-08-10 15:25:31 -07003210 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003211 self.waitUntilSettled()
3212
3213 # B was successfully merged so window is increased to 2.
3214 self.assertEqual(queue.window, 2)
3215 self.assertEqual(queue.window_floor, 1)
3216 self.assertEqual(B.data['status'], 'MERGED')
3217
3218 # Only C is left and its merge job is queued.
3219 self.assertEqual(len(self.builds), 1)
3220 self.assertEqual(self.builds[0].name, 'project-merge')
3221
James E. Blair08d19992016-08-10 15:25:31 -07003222 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003223 self.waitUntilSettled()
3224
3225 # After successful merge job the test jobs for C are queued.
3226 self.assertEqual(len(self.builds), 2)
3227 self.assertEqual(self.builds[0].name, 'project-test1')
3228 self.assertEqual(self.builds[1].name, 'project-test2')
3229
James E. Blair08d19992016-08-10 15:25:31 -07003230 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003231 self.waitUntilSettled()
3232
3233 # C successfully merged so window is bumped to 3.
3234 self.assertEqual(queue.window, 3)
3235 self.assertEqual(queue.window_floor, 1)
3236 self.assertEqual(C.data['status'], 'MERGED')
3237
James E. Blairec056492016-07-22 09:45:56 -07003238 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003239 def test_queue_rate_limiting_dependent(self):
3240 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003241 self.updateConfigLayout(
3242 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003243 self.sched.reconfigure(self.config)
James E. Blair08d19992016-08-10 15:25:31 -07003244 self.launch_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003245 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3246 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3247 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3248
3249 B.setDependsOn(A, 1)
3250
James E. Blair08d19992016-08-10 15:25:31 -07003251 self.launch_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003252
James E. Blair8b5408c2016-08-08 15:37:46 -07003253 A.addApproval('code-review', 2)
3254 B.addApproval('code-review', 2)
3255 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003256
James E. Blair8b5408c2016-08-08 15:37:46 -07003257 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3258 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3259 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003260 self.waitUntilSettled()
3261
3262 # Only A and B will have their merge jobs queued because
3263 # window is 2.
3264 self.assertEqual(len(self.builds), 2)
3265 self.assertEqual(self.builds[0].name, 'project-merge')
3266 self.assertEqual(self.builds[1].name, 'project-merge')
3267
James E. Blair08d19992016-08-10 15:25:31 -07003268 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003269 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003270 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003271 self.waitUntilSettled()
3272
3273 # Only A and B will have their test jobs queued because
3274 # window is 2.
3275 self.assertEqual(len(self.builds), 4)
3276 self.assertEqual(self.builds[0].name, 'project-test1')
3277 self.assertEqual(self.builds[1].name, 'project-test2')
3278 self.assertEqual(self.builds[2].name, 'project-test1')
3279 self.assertEqual(self.builds[3].name, 'project-test2')
3280
James E. Blair08d19992016-08-10 15:25:31 -07003281 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003282 self.waitUntilSettled()
3283
3284 queue = self.sched.layout.pipelines['gate'].queues[0]
3285 # A failed so window is reduced by 1 to 1.
3286 self.assertEqual(queue.window, 1)
3287 self.assertEqual(queue.window_floor, 1)
3288 self.assertEqual(A.data['status'], 'NEW')
3289 self.assertEqual(B.data['status'], 'NEW')
3290
3291 # Gate is reset and only C's merge job is queued because
3292 # window shrunk to 1 and A and B were dequeued.
3293 self.assertEqual(len(self.builds), 1)
3294 self.assertEqual(self.builds[0].name, 'project-merge')
3295
James E. Blair08d19992016-08-10 15:25:31 -07003296 self.launch_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003297 self.waitUntilSettled()
3298
3299 # Only C's test jobs are queued because window is still 1.
3300 self.assertEqual(len(self.builds), 2)
3301 self.assertEqual(self.builds[0].name, 'project-test1')
3302 self.assertEqual(self.builds[1].name, 'project-test2')
3303
James E. Blair08d19992016-08-10 15:25:31 -07003304 self.launch_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003305 self.waitUntilSettled()
3306
3307 # C was successfully merged so window is increased to 2.
3308 self.assertEqual(queue.window, 2)
3309 self.assertEqual(queue.window_floor, 1)
3310 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003311
James E. Blairec056492016-07-22 09:45:56 -07003312 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003313 def test_worker_update_metadata(self):
3314 "Test if a worker can send back metadata about itself"
James E. Blair08d19992016-08-10 15:25:31 -07003315 self.launch_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003316
3317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003318 A.addApproval('code-review', 2)
3319 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003320 self.waitUntilSettled()
3321
3322 self.assertEqual(len(self.launcher.builds), 1)
3323
3324 self.log.debug('Current builds:')
3325 self.log.debug(self.launcher.builds)
3326
3327 start = time.time()
3328 while True:
3329 if time.time() - start > 10:
3330 raise Exception("Timeout waiting for gearman server to report "
3331 + "back to the client")
3332 build = self.launcher.builds.values()[0]
3333 if build.worker.name == "My Worker":
3334 break
3335 else:
3336 time.sleep(0)
3337
3338 self.log.debug(build)
3339 self.assertEqual("My Worker", build.worker.name)
3340 self.assertEqual("localhost", build.worker.hostname)
3341 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3342 self.assertEqual("zuul.example.org", build.worker.fqdn)
3343 self.assertEqual("FakeBuilder", build.worker.program)
3344 self.assertEqual("v1.1", build.worker.version)
3345 self.assertEqual({'something': 'else'}, build.worker.extra)
3346
James E. Blair08d19992016-08-10 15:25:31 -07003347 self.launch_server.hold_jobs_in_build = False
3348 self.launch_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003349 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003350
3351 def test_footer_message(self):
3352 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003353 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003354 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003355
3356 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003357 A.addApproval('code-review', 2)
Adam Gandelman82258fd2017-02-07 16:51:58 -08003358 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003359 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003360 self.waitUntilSettled()
3361
3362 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003363 B.addApproval('code-review', 2)
3364 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003365 self.waitUntilSettled()
3366
3367 self.assertEqual(2, len(self.smtp_messages))
3368
Adam Gandelman82258fd2017-02-07 16:51:58 -08003369 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003370Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003371http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003372
Adam Gandelman82258fd2017-02-07 16:51:58 -08003373 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003374For CI problems and help debugging, contact ci@example.org"""
3375
Adam Gandelman82258fd2017-02-07 16:51:58 -08003376 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3377 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3378 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3379 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003380
James E. Blairec056492016-07-22 09:45:56 -07003381 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003382 def test_merge_failure_reporters(self):
3383 """Check that the config is set up correctly"""
3384
James E. Blairf84026c2015-12-08 16:11:46 -08003385 self.updateConfigLayout(
3386 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003387 self.sched.reconfigure(self.config)
3388 self.registerJobs()
3389
3390 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003391 "Merge Failed.\n\nThis change or one of its cross-repo "
3392 "dependencies was unable to be automatically merged with the "
3393 "current state of its repository. Please rebase the change and "
3394 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003395 self.sched.layout.pipelines['check'].merge_failure_message)
3396 self.assertEqual(
3397 "The merge failed! For more information...",
3398 self.sched.layout.pipelines['gate'].merge_failure_message)
3399
3400 self.assertEqual(
3401 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3402 self.assertEqual(
3403 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3404
3405 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003406 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3407 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003408
3409 self.assertTrue(
3410 (
3411 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003412 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003413 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003414 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003415 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003416 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003417 ) or (
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.gerrit.GerritReporter) 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.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003424 )
3425 )
3426
James E. Blairec056492016-07-22 09:45:56 -07003427 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003428 def test_merge_failure_reports(self):
3429 """Check that when a change fails to merge the correct message is sent
3430 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003431 self.updateConfigLayout(
3432 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003433 self.sched.reconfigure(self.config)
3434 self.registerJobs()
3435
3436 # Check a test failure isn't reported to SMTP
3437 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003438 A.addApproval('code-review', 2)
James E. Blair08d19992016-08-10 15:25:31 -07003439 self.launch_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003440 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003441 self.waitUntilSettled()
3442
3443 self.assertEqual(3, len(self.history)) # 3 jobs
3444 self.assertEqual(0, len(self.smtp_messages))
3445
3446 # Check a merge failure is reported to SMTP
3447 # B should be merged, but C will conflict with B
3448 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3449 B.addPatchset(['conflict'])
3450 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3451 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003452 B.addApproval('code-review', 2)
3453 C.addApproval('code-review', 2)
3454 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3455 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003456 self.waitUntilSettled()
3457
3458 self.assertEqual(6, len(self.history)) # A and B jobs
3459 self.assertEqual(1, len(self.smtp_messages))
3460 self.assertEqual('The merge failed! For more information...',
3461 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003462
James E. Blairec056492016-07-22 09:45:56 -07003463 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003464 def test_default_merge_failure_reports(self):
3465 """Check that the default merge failure reports are correct."""
3466
3467 # A should report success, B should report merge failure.
3468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3469 A.addPatchset(['conflict'])
3470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3471 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003472 A.addApproval('code-review', 2)
3473 B.addApproval('code-review', 2)
3474 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3475 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003476 self.waitUntilSettled()
3477
3478 self.assertEqual(3, len(self.history)) # A jobs
3479 self.assertEqual(A.reported, 2)
3480 self.assertEqual(B.reported, 2)
3481 self.assertEqual(A.data['status'], 'MERGED')
3482 self.assertEqual(B.data['status'], 'NEW')
3483 self.assertIn('Build succeeded', A.messages[1])
3484 self.assertIn('Merge Failed', B.messages[1])
3485 self.assertIn('automatically merged', B.messages[1])
3486 self.assertNotIn('logs.example.com', B.messages[1])
3487 self.assertNotIn('SKIPPED', B.messages[1])
3488
James E. Blairec056492016-07-22 09:45:56 -07003489 @skip("Disabled for early v3 development")
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003490 def test_swift_instructions(self):
3491 "Test that the correct swift instructions are sent to the workers"
James E. Blairf84026c2015-12-08 16:11:46 -08003492 self.updateConfigLayout(
3493 'tests/fixtures/layout-swift.yaml')
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003494 self.sched.reconfigure(self.config)
3495 self.registerJobs()
3496
James E. Blair08d19992016-08-10 15:25:31 -07003497 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003498 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3499
James E. Blair8b5408c2016-08-08 15:37:46 -07003500 A.addApproval('code-review', 2)
3501 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003502 self.waitUntilSettled()
3503
3504 self.assertEqual(
3505 "https://storage.example.org/V1/AUTH_account/merge_logs/1/1/1/"
3506 "gate/test-merge/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003507 self.builds[0].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003508 self.assertEqual(5,
3509 len(self.builds[0].parameters['SWIFT_logs_HMAC_BODY'].
3510 split('\n')))
3511 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[0].parameters)
3512
3513 self.assertEqual(
3514 "https://storage.example.org/V1/AUTH_account/logs/1/1/1/"
3515 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003516 self.builds[1].parameters['SWIFT_logs_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003517 self.assertEqual(5,
3518 len(self.builds[1].parameters['SWIFT_logs_HMAC_BODY'].
3519 split('\n')))
3520 self.assertIn('SWIFT_logs_SIGNATURE', self.builds[1].parameters)
3521
3522 self.assertEqual(
3523 "https://storage.example.org/V1/AUTH_account/stash/1/1/1/"
3524 "gate/test-test/",
Joshua Hesketh76dee532014-07-03 15:39:13 +10003525 self.builds[1].parameters['SWIFT_MOSTLY_URL'][:-7])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003526 self.assertEqual(5,
3527 len(self.builds[1].
3528 parameters['SWIFT_MOSTLY_HMAC_BODY'].split('\n')))
3529 self.assertIn('SWIFT_MOSTLY_SIGNATURE', self.builds[1].parameters)
3530
James E. Blair08d19992016-08-10 15:25:31 -07003531 self.launch_server.hold_jobs_in_build = False
3532 self.launch_server.release()
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003533 self.waitUntilSettled()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003534
3535 def test_client_get_running_jobs(self):
3536 "Test that the RPC client can get a list of running jobs"
James E. Blair08d19992016-08-10 15:25:31 -07003537 self.launch_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003538 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003539 A.addApproval('code-review', 2)
3540 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003541 self.waitUntilSettled()
3542
3543 client = zuul.rpcclient.RPCClient('127.0.0.1',
3544 self.gearman_server.port)
3545
3546 # Wait for gearman server to send the initial workData back to zuul
3547 start = time.time()
3548 while True:
3549 if time.time() - start > 10:
3550 raise Exception("Timeout waiting for gearman server to report "
3551 + "back to the client")
Clint Byrum78a69772016-12-05 10:52:07 -08003552 build = self.launch_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003553 if build.worker.name == "My Worker":
3554 break
3555 else:
3556 time.sleep(0)
3557
3558 running_items = client.get_running_jobs()
3559
3560 self.assertEqual(1, len(running_items))
3561 running_item = running_items[0]
3562 self.assertEqual([], running_item['failing_reasons'])
3563 self.assertEqual([], running_item['items_behind'])
3564 self.assertEqual('https://hostname/1', running_item['url'])
3565 self.assertEqual(None, running_item['item_ahead'])
3566 self.assertEqual('org/project', running_item['project'])
3567 self.assertEqual(None, running_item['remaining_time'])
3568 self.assertEqual(True, running_item['active'])
3569 self.assertEqual('1,1', running_item['id'])
3570
3571 self.assertEqual(3, len(running_item['jobs']))
3572 for job in running_item['jobs']:
3573 if job['name'] == 'project-merge':
3574 self.assertEqual('project-merge', job['name'])
3575 self.assertEqual('gate', job['pipeline'])
3576 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003577 self.assertEqual('https://server/job/project-merge/0/',
3578 job['url'])
3579 self.assertEqual(7, len(job['worker']))
3580 self.assertEqual(False, job['canceled'])
3581 self.assertEqual(True, job['voting'])
3582 self.assertEqual(None, job['result'])
3583 self.assertEqual('gate', job['pipeline'])
3584 break
3585
James E. Blair08d19992016-08-10 15:25:31 -07003586 self.launch_server.hold_jobs_in_build = False
3587 self.launch_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003588 self.waitUntilSettled()
3589
3590 running_items = client.get_running_jobs()
3591 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003592
3593 def test_nonvoting_pipeline(self):
3594 "Test that a nonvoting pipeline (experimental) can still report"
3595
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003596 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3597 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003598 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3599 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003600
3601 self.assertEqual(self.getJobFromHistory('project-merge').result,
3602 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003603 self.assertEqual(
3604 self.getJobFromHistory('experimental-project-test').result,
3605 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003606 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003607
3608 def test_crd_gate(self):
3609 "Test cross-repo dependencies"
3610 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3611 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003612 A.addApproval('code-review', 2)
3613 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003614
3615 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3616 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3617 AM2.setMerged()
3618 AM1.setMerged()
3619
3620 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3621 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3622 BM2.setMerged()
3623 BM1.setMerged()
3624
3625 # A -> AM1 -> AM2
3626 # B -> BM1 -> BM2
3627 # A Depends-On: B
3628 # M2 is here to make sure it is never queried. If it is, it
3629 # means zuul is walking down the entire history of merged
3630 # changes.
3631
3632 B.setDependsOn(BM1, 1)
3633 BM1.setDependsOn(BM2, 1)
3634
3635 A.setDependsOn(AM1, 1)
3636 AM1.setDependsOn(AM2, 1)
3637
3638 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3639 A.subject, B.data['id'])
3640
James E. Blair8b5408c2016-08-08 15:37:46 -07003641 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003642 self.waitUntilSettled()
3643
3644 self.assertEqual(A.data['status'], 'NEW')
3645 self.assertEqual(B.data['status'], 'NEW')
3646
Paul Belanger5dccbe72016-11-14 11:17:37 -05003647 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003648 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003649
James E. Blair08d19992016-08-10 15:25:31 -07003650 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003651 B.addApproval('approved', 1)
3652 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003653 self.waitUntilSettled()
3654
James E. Blair08d19992016-08-10 15:25:31 -07003655 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003656 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003657 self.launch_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003658 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003659 self.launch_server.hold_jobs_in_build = False
3660 self.launch_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003661 self.waitUntilSettled()
3662
3663 self.assertEqual(AM2.queried, 0)
3664 self.assertEqual(BM2.queried, 0)
3665 self.assertEqual(A.data['status'], 'MERGED')
3666 self.assertEqual(B.data['status'], 'MERGED')
3667 self.assertEqual(A.reported, 2)
3668 self.assertEqual(B.reported, 2)
3669
Paul Belanger5dccbe72016-11-14 11:17:37 -05003670 changes = self.getJobFromHistory(
3671 'project-merge', 'org/project1').changes
3672 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003673
3674 def test_crd_branch(self):
3675 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003676
3677 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003678 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3679 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3680 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3681 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003682 A.addApproval('code-review', 2)
3683 B.addApproval('code-review', 2)
3684 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003685
3686 # A Depends-On: B+C
3687 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3688 A.subject, B.data['id'])
3689
James E. Blair08d19992016-08-10 15:25:31 -07003690 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003691 B.addApproval('approved', 1)
3692 C.addApproval('approved', 1)
3693 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003694 self.waitUntilSettled()
3695
James E. Blair08d19992016-08-10 15:25:31 -07003696 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003697 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003698 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003699 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003700 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003701 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003702 self.launch_server.hold_jobs_in_build = False
3703 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003704 self.waitUntilSettled()
3705
3706 self.assertEqual(A.data['status'], 'MERGED')
3707 self.assertEqual(B.data['status'], 'MERGED')
3708 self.assertEqual(C.data['status'], 'MERGED')
3709 self.assertEqual(A.reported, 2)
3710 self.assertEqual(B.reported, 2)
3711 self.assertEqual(C.reported, 2)
3712
Paul Belanger6379db12016-11-14 13:57:54 -05003713 changes = self.getJobFromHistory(
3714 'project-merge', 'org/project1').changes
3715 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003716
3717 def test_crd_multiline(self):
3718 "Test multiple depends-on lines in commit"
3719 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3720 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3721 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003722 A.addApproval('code-review', 2)
3723 B.addApproval('code-review', 2)
3724 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003725
3726 # A Depends-On: B+C
3727 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3728 A.subject, B.data['id'], C.data['id'])
3729
James E. Blair08d19992016-08-10 15:25:31 -07003730 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003731 B.addApproval('approved', 1)
3732 C.addApproval('approved', 1)
3733 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003734 self.waitUntilSettled()
3735
James E. Blair08d19992016-08-10 15:25:31 -07003736 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003737 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003738 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003739 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003740 self.launch_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003741 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003742 self.launch_server.hold_jobs_in_build = False
3743 self.launch_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003744 self.waitUntilSettled()
3745
3746 self.assertEqual(A.data['status'], 'MERGED')
3747 self.assertEqual(B.data['status'], 'MERGED')
3748 self.assertEqual(C.data['status'], 'MERGED')
3749 self.assertEqual(A.reported, 2)
3750 self.assertEqual(B.reported, 2)
3751 self.assertEqual(C.reported, 2)
3752
Paul Belanger5dccbe72016-11-14 11:17:37 -05003753 changes = self.getJobFromHistory(
3754 'project-merge', 'org/project1').changes
3755 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003756
3757 def test_crd_unshared_gate(self):
3758 "Test cross-repo dependencies in unshared gate queues"
3759 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3760 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003761 A.addApproval('code-review', 2)
3762 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003763
3764 # A Depends-On: B
3765 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3766 A.subject, B.data['id'])
3767
3768 # A and B do not share a queue, make sure that A is unable to
3769 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003770 B.addApproval('approved', 1)
3771 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003772 self.waitUntilSettled()
3773
3774 self.assertEqual(A.data['status'], 'NEW')
3775 self.assertEqual(B.data['status'], 'NEW')
3776 self.assertEqual(A.reported, 0)
3777 self.assertEqual(B.reported, 0)
3778 self.assertEqual(len(self.history), 0)
3779
3780 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003781 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003782 self.waitUntilSettled()
3783
3784 self.assertEqual(B.data['status'], 'MERGED')
3785 self.assertEqual(B.reported, 2)
3786
3787 # Now that B is merged, A should be able to be enqueued and
3788 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003789 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003790 self.waitUntilSettled()
3791
3792 self.assertEqual(A.data['status'], 'MERGED')
3793 self.assertEqual(A.reported, 2)
3794
James E. Blair96698e22015-04-02 07:48:21 -07003795 def test_crd_gate_reverse(self):
3796 "Test reverse cross-repo dependencies"
3797 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3798 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003799 A.addApproval('code-review', 2)
3800 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003801
3802 # A Depends-On: B
3803
3804 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3805 A.subject, B.data['id'])
3806
James E. Blair8b5408c2016-08-08 15:37:46 -07003807 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003808 self.waitUntilSettled()
3809
3810 self.assertEqual(A.data['status'], 'NEW')
3811 self.assertEqual(B.data['status'], 'NEW')
3812
James E. Blair08d19992016-08-10 15:25:31 -07003813 self.launch_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003814 A.addApproval('approved', 1)
3815 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003816 self.waitUntilSettled()
3817
James E. Blair08d19992016-08-10 15:25:31 -07003818 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003819 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003820 self.launch_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003821 self.waitUntilSettled()
James E. Blair08d19992016-08-10 15:25:31 -07003822 self.launch_server.hold_jobs_in_build = False
3823 self.launch_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003824 self.waitUntilSettled()
3825
3826 self.assertEqual(A.data['status'], 'MERGED')
3827 self.assertEqual(B.data['status'], 'MERGED')
3828 self.assertEqual(A.reported, 2)
3829 self.assertEqual(B.reported, 2)
3830
Paul Belanger5dccbe72016-11-14 11:17:37 -05003831 changes = self.getJobFromHistory(
3832 'project-merge', 'org/project1').changes
3833 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003834
James E. Blair5ee24252014-12-30 10:12:29 -08003835 def test_crd_cycle(self):
3836 "Test cross-repo dependency cycles"
3837 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3838 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003839 A.addApproval('code-review', 2)
3840 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003841
3842 # A -> B -> A (via commit-depends)
3843
3844 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3845 A.subject, B.data['id'])
3846 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3847 B.subject, A.data['id'])
3848
James E. Blair8b5408c2016-08-08 15:37:46 -07003849 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003850 self.waitUntilSettled()
3851
3852 self.assertEqual(A.reported, 0)
3853 self.assertEqual(B.reported, 0)
3854 self.assertEqual(A.data['status'], 'NEW')
3855 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003856
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003857 def test_crd_gate_unknown(self):
3858 "Test unknown projects in dependent pipeline"
3859 self.init_repo("org/unknown")
3860 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3861 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003862 A.addApproval('code-review', 2)
3863 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003864
3865 # A Depends-On: B
3866 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3867 A.subject, B.data['id'])
3868
James E. Blair8b5408c2016-08-08 15:37:46 -07003869 B.addApproval('approved', 1)
3870 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003871 self.waitUntilSettled()
3872
3873 # Unknown projects cannot share a queue with any other
3874 # since they don't have common jobs with any other (they have no jobs).
3875 # Changes which depend on unknown project changes
3876 # should not be processed in dependent pipeline
3877 self.assertEqual(A.data['status'], 'NEW')
3878 self.assertEqual(B.data['status'], 'NEW')
3879 self.assertEqual(A.reported, 0)
3880 self.assertEqual(B.reported, 0)
3881 self.assertEqual(len(self.history), 0)
3882
3883 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003884 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003885 B.setMerged()
3886 self.waitUntilSettled()
3887 self.assertEqual(len(self.history), 0)
3888
3889 # Now that B is merged, A should be able to be enqueued and
3890 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003891 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003892 self.waitUntilSettled()
3893
3894 self.assertEqual(A.data['status'], 'MERGED')
3895 self.assertEqual(A.reported, 2)
3896 self.assertEqual(B.data['status'], 'MERGED')
3897 self.assertEqual(B.reported, 0)
3898
James E. Blairbfb8e042014-12-30 17:01:44 -08003899 def test_crd_check(self):
3900 "Test cross-repo dependencies in independent pipelines"
3901
Paul Belangerb30342b2016-11-14 12:30:43 -05003902 self.launch_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003903 self.gearman_server.hold_jobs_in_queue = True
3904 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3905 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3906
3907 # A Depends-On: B
3908 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3909 A.subject, B.data['id'])
3910
3911 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3912 self.waitUntilSettled()
3913
3914 queue = self.gearman_server.getQueue()
3915 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3916 self.gearman_server.hold_jobs_in_queue = False
3917 self.gearman_server.release()
3918 self.waitUntilSettled()
3919
Paul Belangerb30342b2016-11-14 12:30:43 -05003920 self.launch_server.release('.*-merge')
3921 self.waitUntilSettled()
3922
3923 path = os.path.join(self.builds[0].jobdir.git_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08003924 repo = git.Repo(path)
3925 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3926 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003927 correct_messages = [
3928 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003929 self.assertEqual(repo_messages, correct_messages)
3930
Paul Belangerb30342b2016-11-14 12:30:43 -05003931 path = os.path.join(self.builds[0].jobdir.git_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08003932 repo = git.Repo(path)
3933 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3934 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003935 correct_messages = [
3936 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003937 self.assertEqual(repo_messages, correct_messages)
3938
Paul Belangerb30342b2016-11-14 12:30:43 -05003939 self.launch_server.hold_jobs_in_build = False
3940 self.launch_server.release()
3941 self.waitUntilSettled()
3942
James E. Blairbfb8e042014-12-30 17:01:44 -08003943 self.assertEqual(A.data['status'], 'NEW')
3944 self.assertEqual(B.data['status'], 'NEW')
3945 self.assertEqual(A.reported, 1)
3946 self.assertEqual(B.reported, 0)
3947
3948 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003949 tenant = self.sched.abide.tenants.get('tenant-one')
3950 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003951
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003952 def test_crd_check_git_depends(self):
3953 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003954 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003955 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3956 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3957
3958 # Add two git-dependent changes and make sure they both report
3959 # success.
3960 B.setDependsOn(A, 1)
3961 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3962 self.waitUntilSettled()
3963 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3964 self.waitUntilSettled()
3965
James E. Blairb8c16472015-05-05 14:55:26 -07003966 self.orderedRelease()
3967 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003968 self.waitUntilSettled()
3969
3970 self.assertEqual(A.data['status'], 'NEW')
3971 self.assertEqual(B.data['status'], 'NEW')
3972 self.assertEqual(A.reported, 1)
3973 self.assertEqual(B.reported, 1)
3974
3975 self.assertEqual(self.history[0].changes, '1,1')
3976 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003977 tenant = self.sched.abide.tenants.get('tenant-one')
3978 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003979
3980 self.assertIn('Build succeeded', A.messages[0])
3981 self.assertIn('Build succeeded', B.messages[0])
3982
3983 def test_crd_check_duplicate(self):
3984 "Test duplicate check in independent pipelines"
James E. Blair08d19992016-08-10 15:25:31 -07003985 self.launch_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003986 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3987 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003988 tenant = self.sched.abide.tenants.get('tenant-one')
3989 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003990
3991 # Add two git-dependent changes...
3992 B.setDependsOn(A, 1)
3993 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3994 self.waitUntilSettled()
3995 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3996
3997 # ...make sure the live one is not duplicated...
3998 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3999 self.waitUntilSettled()
4000 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4001
4002 # ...but the non-live one is able to be.
4003 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4004 self.waitUntilSettled()
4005 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4006
Clark Boylandd849822015-03-02 12:38:14 -08004007 # Release jobs in order to avoid races with change A jobs
4008 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004009 self.orderedRelease()
James E. Blair08d19992016-08-10 15:25:31 -07004010 self.launch_server.hold_jobs_in_build = False
4011 self.launch_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004012 self.waitUntilSettled()
4013
4014 self.assertEqual(A.data['status'], 'NEW')
4015 self.assertEqual(B.data['status'], 'NEW')
4016 self.assertEqual(A.reported, 1)
4017 self.assertEqual(B.reported, 1)
4018
4019 self.assertEqual(self.history[0].changes, '1,1 2,1')
4020 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004021 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004022
4023 self.assertIn('Build succeeded', A.messages[0])
4024 self.assertIn('Build succeeded', B.messages[0])
4025
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004026 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004027 "Test cross-repo dependencies re-enqueued in independent pipelines"
4028
4029 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004030 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4031 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004032
4033 # A Depends-On: B
4034 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4035 A.subject, B.data['id'])
4036
4037 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4038 self.waitUntilSettled()
4039
4040 self.sched.reconfigure(self.config)
4041
4042 # Make sure the items still share a change queue, and the
4043 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004044 tenant = self.sched.abide.tenants.get('tenant-one')
4045 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4046 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004047 first_item = queue.queue[0]
4048 for item in queue.queue:
4049 self.assertEqual(item.queue, first_item.queue)
4050 self.assertFalse(first_item.live)
4051 self.assertTrue(queue.queue[1].live)
4052
4053 self.gearman_server.hold_jobs_in_queue = False
4054 self.gearman_server.release()
4055 self.waitUntilSettled()
4056
4057 self.assertEqual(A.data['status'], 'NEW')
4058 self.assertEqual(B.data['status'], 'NEW')
4059 self.assertEqual(A.reported, 1)
4060 self.assertEqual(B.reported, 0)
4061
4062 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004063 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004064
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004065 def test_crd_check_reconfiguration(self):
4066 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4067
4068 def test_crd_undefined_project(self):
4069 """Test that undefined projects in dependencies are handled for
4070 independent pipelines"""
4071 # It's a hack for fake gerrit,
4072 # as it implies repo creation upon the creation of any change
4073 self.init_repo("org/unknown")
4074 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4075
James E. Blairec056492016-07-22 09:45:56 -07004076 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004077 def test_crd_check_ignore_dependencies(self):
4078 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004079 self.updateConfigLayout(
4080 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004081 self.sched.reconfigure(self.config)
4082 self.registerJobs()
4083
4084 self.gearman_server.hold_jobs_in_queue = True
4085 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4086 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4087 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4088
4089 # A Depends-On: B
4090 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4091 A.subject, B.data['id'])
4092 # C git-depends on B
4093 C.setDependsOn(B, 1)
4094 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4095 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4096 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4097 self.waitUntilSettled()
4098
4099 # Make sure none of the items share a change queue, and all
4100 # are live.
4101 check_pipeline = self.sched.layout.pipelines['check']
4102 self.assertEqual(len(check_pipeline.queues), 3)
4103 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4104 for item in check_pipeline.getAllItems():
4105 self.assertTrue(item.live)
4106
4107 self.gearman_server.hold_jobs_in_queue = False
4108 self.gearman_server.release()
4109 self.waitUntilSettled()
4110
4111 self.assertEqual(A.data['status'], 'NEW')
4112 self.assertEqual(B.data['status'], 'NEW')
4113 self.assertEqual(C.data['status'], 'NEW')
4114 self.assertEqual(A.reported, 1)
4115 self.assertEqual(B.reported, 1)
4116 self.assertEqual(C.reported, 1)
4117
4118 # Each job should have tested exactly one change
4119 for job in self.history:
4120 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004121
James E. Blairec056492016-07-22 09:45:56 -07004122 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004123 def test_crd_check_transitive(self):
4124 "Test transitive cross-repo dependencies"
4125 # Specifically, if A -> B -> C, and C gets a new patchset and
4126 # A gets a new patchset, ensure the test of A,2 includes B,1
4127 # and C,2 (not C,1 which would indicate stale data in the
4128 # cache for B).
4129 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4130 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4131 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4132
4133 # A Depends-On: B
4134 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4135 A.subject, B.data['id'])
4136
4137 # B Depends-On: C
4138 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4139 B.subject, C.data['id'])
4140
4141 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4142 self.waitUntilSettled()
4143 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4144
4145 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4146 self.waitUntilSettled()
4147 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4148
4149 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4150 self.waitUntilSettled()
4151 self.assertEqual(self.history[-1].changes, '3,1')
4152
4153 C.addPatchset()
4154 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4155 self.waitUntilSettled()
4156 self.assertEqual(self.history[-1].changes, '3,2')
4157
4158 A.addPatchset()
4159 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4160 self.waitUntilSettled()
4161 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004162
James E. Blaira8b90b32016-08-24 15:18:50 -07004163 def test_crd_check_unknown(self):
4164 "Test unknown projects in independent pipeline"
4165 self.init_repo("org/unknown")
4166 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4167 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4168 # A Depends-On: B
4169 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4170 A.subject, B.data['id'])
4171
4172 # Make sure zuul has seen an event on B.
4173 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4174 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4175 self.waitUntilSettled()
4176
4177 self.assertEqual(A.data['status'], 'NEW')
4178 self.assertEqual(A.reported, 1)
4179 self.assertEqual(B.data['status'], 'NEW')
4180 self.assertEqual(B.reported, 0)
4181
James E. Blair92464a22016-04-05 10:21:26 -07004182 def test_crd_cycle_join(self):
4183 "Test an updated change creates a cycle"
4184 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4185
4186 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4187 self.waitUntilSettled()
4188
4189 # Create B->A
4190 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4191 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4192 B.subject, A.data['id'])
4193 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4194 self.waitUntilSettled()
4195
4196 # Update A to add A->B (a cycle).
4197 A.addPatchset()
4198 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4199 A.subject, B.data['id'])
4200 # Normally we would submit the patchset-created event for
4201 # processing here, however, we have no way of noting whether
4202 # the dependency cycle detection correctly raised an
4203 # exception, so instead, we reach into the source driver and
4204 # call the method that would ultimately be called by the event
4205 # processing.
4206
Paul Belanger0e155e22016-11-14 14:12:23 -05004207 tenant = self.sched.abide.tenants.get('tenant-one')
4208 source = tenant.layout.pipelines['gate'].source
4209
4210 # TODO(pabelanger): As we add more source / trigger APIs we should make
4211 # it easier for users to create events for testing.
4212 event = zuul.model.TriggerEvent()
4213 event.trigger_name = 'gerrit'
4214 event.change_number = '1'
4215 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004216 with testtools.ExpectedException(
4217 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004218 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004219 self.log.debug("Got expected dependency cycle exception")
4220
4221 # Now if we update B to remove the depends-on, everything
4222 # should be okay. B; A->B
4223
4224 B.addPatchset()
4225 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004226
4227 source.getChange(event, True)
4228 event.change_number = '2'
4229 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004230
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004231 def test_disable_at(self):
4232 "Test a pipeline will only report to the disabled trigger when failing"
4233
Paul Belanger7dc76e82016-11-11 16:51:08 -05004234 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004235 self.sched.reconfigure(self.config)
4236
Paul Belanger7dc76e82016-11-11 16:51:08 -05004237 tenant = self.sched.abide.tenants.get('openstack')
4238 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004239 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004240 0, tenant.layout.pipelines['check']._consecutive_failures)
4241 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004242
4243 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4244 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4245 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4246 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4247 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4248 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4249 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4250 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4251 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4252 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4253 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4254
James E. Blair08d19992016-08-10 15:25:31 -07004255 self.launch_server.failJob('project-test1', A)
4256 self.launch_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004257 # Let C pass, resetting the counter
James E. Blair08d19992016-08-10 15:25:31 -07004258 self.launch_server.failJob('project-test1', D)
4259 self.launch_server.failJob('project-test1', E)
4260 self.launch_server.failJob('project-test1', F)
4261 self.launch_server.failJob('project-test1', G)
4262 self.launch_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004263 # I also passes but should only report to the disabled reporters
James E. Blair08d19992016-08-10 15:25:31 -07004264 self.launch_server.failJob('project-test1', J)
4265 self.launch_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004266
4267 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4268 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4269 self.waitUntilSettled()
4270
4271 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004272 2, tenant.layout.pipelines['check']._consecutive_failures)
4273 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004274
4275 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4276 self.waitUntilSettled()
4277
4278 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004279 0, 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(D.getPatchsetCreatedEvent(1))
4283 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4284 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4285 self.waitUntilSettled()
4286
4287 # We should be disabled now
4288 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004289 3, tenant.layout.pipelines['check']._consecutive_failures)
4290 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004291
4292 # We need to wait between each of these patches to make sure the
4293 # smtp messages come back in an expected order
4294 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4295 self.waitUntilSettled()
4296 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4297 self.waitUntilSettled()
4298 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4299 self.waitUntilSettled()
4300
4301 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4302 # leaving a message on each change
4303 self.assertEqual(1, len(A.messages))
4304 self.assertIn('Build failed.', A.messages[0])
4305 self.assertEqual(1, len(B.messages))
4306 self.assertIn('Build failed.', B.messages[0])
4307 self.assertEqual(1, len(C.messages))
4308 self.assertIn('Build succeeded.', C.messages[0])
4309 self.assertEqual(1, len(D.messages))
4310 self.assertIn('Build failed.', D.messages[0])
4311 self.assertEqual(1, len(E.messages))
4312 self.assertIn('Build failed.', E.messages[0])
4313 self.assertEqual(1, len(F.messages))
4314 self.assertIn('Build failed.', F.messages[0])
4315
4316 # The last 3 (GHI) would have only reported via smtp.
4317 self.assertEqual(3, len(self.smtp_messages))
4318 self.assertEqual(0, len(G.messages))
4319 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004320 self.assertIn(
4321 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004322 self.assertEqual(0, len(H.messages))
4323 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004324 self.assertIn(
4325 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004326 self.assertEqual(0, len(I.messages))
4327 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004328 self.assertIn(
4329 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004330
4331 # Now reload the configuration (simulate a HUP) to check the pipeline
4332 # comes out of disabled
4333 self.sched.reconfigure(self.config)
4334
Paul Belanger7dc76e82016-11-11 16:51:08 -05004335 tenant = self.sched.abide.tenants.get('openstack')
4336
4337 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004338 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004339 0, tenant.layout.pipelines['check']._consecutive_failures)
4340 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004341
4342 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4343 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4344 self.waitUntilSettled()
4345
4346 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004347 2, tenant.layout.pipelines['check']._consecutive_failures)
4348 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004349
4350 # J and K went back to gerrit
4351 self.assertEqual(1, len(J.messages))
4352 self.assertIn('Build failed.', J.messages[0])
4353 self.assertEqual(1, len(K.messages))
4354 self.assertIn('Build failed.', K.messages[0])
4355 # No more messages reported via smtp
4356 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004357
Paul Belanger71d98172016-11-08 10:56:31 -05004358 def test_rerun_on_abort(self):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004359 "Test that if a launch server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004360
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004361 self.launch_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004362 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4363 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4364 self.waitUntilSettled()
4365
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004366 self.launch_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004367 self.waitUntilSettled()
4368
4369 self.assertEqual(len(self.builds), 2)
4370 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004371 self.launch_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004372 self.waitUntilSettled()
4373
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004374 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004375 self.assertEqual(len(self.builds), 1,
4376 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004377 self.builds[0].requeue = True
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004378 self.launch_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004379 self.waitUntilSettled()
4380
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004381 self.launch_server.hold_jobs_in_build = False
4382 self.launch_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004383 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004384 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004385 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4386 self.assertEqual(A.reported, 1)
4387 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004388
James E. Blair15be0e12017-01-03 13:45:20 -08004389 def test_zookeeper_disconnect(self):
4390 "Test that jobs are launched after a zookeeper disconnect"
4391
4392 self.fake_nodepool.paused = True
4393 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4394 A.addApproval('code-review', 2)
4395 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4396 self.waitUntilSettled()
4397
4398 self.zk.client.stop()
4399 self.zk.client.start()
4400 self.fake_nodepool.paused = False
4401 self.waitUntilSettled()
4402
4403 self.assertEqual(A.data['status'], 'MERGED')
4404 self.assertEqual(A.reported, 2)
4405
James E. Blair6ab79e02017-01-06 10:10:17 -08004406 def test_nodepool_failure(self):
4407 "Test that jobs are reported after a nodepool failure"
4408
4409 self.fake_nodepool.paused = True
4410 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4411 A.addApproval('code-review', 2)
4412 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4413 self.waitUntilSettled()
4414
4415 req = self.fake_nodepool.getNodeRequests()[0]
4416 self.fake_nodepool.addFailRequest(req)
4417
4418 self.fake_nodepool.paused = False
4419 self.waitUntilSettled()
4420
4421 self.assertEqual(A.data['status'], 'NEW')
4422 self.assertEqual(A.reported, 2)
4423 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4424 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4425 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4426
Arieb6f068c2016-10-09 13:11:06 +03004427
4428class TestDuplicatePipeline(ZuulTestCase):
4429 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4430
4431 def test_duplicate_pipelines(self):
4432 "Test that a change matching multiple pipelines works"
4433
4434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4435 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4436 self.waitUntilSettled()
4437
4438 self.assertHistory([
4439 dict(name='project-test1', result='SUCCESS', changes='1,1',
4440 pipeline='dup1'),
4441 dict(name='project-test1', result='SUCCESS', changes='1,1',
4442 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004443 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004444
4445 self.assertEqual(len(A.messages), 2)
4446
Paul Belangera46a3742016-11-09 14:23:03 -05004447 if 'dup1' in A.messages[0]:
4448 self.assertIn('dup1', A.messages[0])
4449 self.assertNotIn('dup2', A.messages[0])
4450 self.assertIn('project-test1', A.messages[0])
4451 self.assertIn('dup2', A.messages[1])
4452 self.assertNotIn('dup1', A.messages[1])
4453 self.assertIn('project-test1', A.messages[1])
4454 else:
4455 self.assertIn('dup1', A.messages[1])
4456 self.assertNotIn('dup2', A.messages[1])
4457 self.assertIn('project-test1', A.messages[1])
4458 self.assertIn('dup2', A.messages[0])
4459 self.assertNotIn('dup1', A.messages[0])
4460 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004461
4462
4463class TestSchedulerOneJobProject(ZuulTestCase):
4464 tenant_config_file = 'config/one-job-project/main.yaml'
4465
4466 def test_one_job_project(self):
4467 "Test that queueing works with one job"
4468 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4469 'master', 'A')
4470 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4471 'master', 'B')
4472 A.addApproval('code-review', 2)
4473 B.addApproval('code-review', 2)
4474 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4475 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4476 self.waitUntilSettled()
4477
4478 self.assertEqual(A.data['status'], 'MERGED')
4479 self.assertEqual(A.reported, 2)
4480 self.assertEqual(B.data['status'], 'MERGED')
4481 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004482
4483
4484class TestSchedulerTemplatedProject(ZuulTestCase):
4485 tenant_config_file = 'config/templated-project/main.yaml'
4486
4487 def test_job_from_templates_launched(self):
4488 "Test whether a job generated via a template can be launched"
4489
4490 A = self.fake_gerrit.addFakeChange(
4491 'org/templated-project', 'master', 'A')
4492 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4493 self.waitUntilSettled()
4494
4495 self.assertEqual(self.getJobFromHistory('project-test1').result,
4496 'SUCCESS')
4497 self.assertEqual(self.getJobFromHistory('project-test2').result,
4498 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004499
4500 def test_layered_templates(self):
4501 "Test whether a job generated via a template can be launched"
4502
4503 A = self.fake_gerrit.addFakeChange(
4504 'org/layered-project', 'master', 'A')
4505 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4506 self.waitUntilSettled()
4507
4508 self.assertEqual(self.getJobFromHistory('project-test1').result,
4509 'SUCCESS')
4510 self.assertEqual(self.getJobFromHistory('project-test2').result,
4511 'SUCCESS')
4512 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4513 ).result, 'SUCCESS')
4514 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4515 ).result, 'SUCCESS')
4516 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4517 ).result, 'SUCCESS')
4518 self.assertEqual(self.getJobFromHistory('project-test6').result,
4519 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004520
4521
4522class TestSchedulerSuccessURL(ZuulTestCase):
4523 tenant_config_file = 'config/success-url/main.yaml'
4524
4525 def test_success_url(self):
4526 "Ensure bad build params are ignored"
4527 self.sched.reconfigure(self.config)
4528 self.init_repo('org/docs')
4529
4530 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4531 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4532 self.waitUntilSettled()
4533
4534 # Both builds ran: docs-draft-test + docs-draft-test2
4535 self.assertEqual(len(self.history), 2)
4536
4537 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004538 for build in self.history:
4539 if build.name == 'docs-draft-test':
4540 uuid = build.uuid[:7]
4541 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004542
4543 # Two msgs: 'Starting...' + results
4544 self.assertEqual(len(self.smtp_messages), 2)
4545 body = self.smtp_messages[1]['body'].splitlines()
4546 self.assertEqual('Build succeeded.', body[0])
4547
4548 self.assertIn(
4549 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4550 'docs-draft-test/%s/publish-docs/' % uuid,
4551 body[2])
4552
4553 # NOTE: This default URL is currently hard-coded in launcher/server.py
4554 self.assertIn(
4555 '- docs-draft-test2 https://server/job',
4556 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004557
4558
Adam Gandelman4da00f62016-12-09 15:47:33 -08004559class TestSchedulerMerges(ZuulTestCase):
4560 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004561
4562 def _test_project_merge_mode(self, mode):
4563 self.launch_server.keep_jobdir = False
4564 project = 'org/project-%s' % mode
4565 self.launch_server.hold_jobs_in_build = True
4566 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4567 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4568 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4569 A.addApproval('code-review', 2)
4570 B.addApproval('code-review', 2)
4571 C.addApproval('code-review', 2)
4572 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4573 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4574 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4575 self.waitUntilSettled()
4576
4577 build = self.builds[-1]
4578 ref = self.getParameter(build, 'ZUUL_REF')
4579
4580 path = os.path.join(build.jobdir.git_root, project)
4581 repo = git.Repo(path)
4582 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4583 repo_messages.reverse()
4584
4585 self.launch_server.hold_jobs_in_build = False
4586 self.launch_server.release()
4587 self.waitUntilSettled()
4588
4589 return repo_messages
4590
4591 def _test_merge(self, mode):
4592 us_path = os.path.join(
4593 self.upstream_root, 'org/project-%s' % mode)
4594 expected_messages = [
4595 'initial commit',
4596 'add content from fixture',
4597 # the intermediate commits order is nondeterministic
4598 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4599 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4600 ]
4601 result = self._test_project_merge_mode(mode)
4602 self.assertEqual(result[:2], expected_messages[:2])
4603 self.assertEqual(result[-2:], expected_messages[-2:])
4604
4605 def test_project_merge_mode_merge(self):
4606 self._test_merge('merge')
4607
4608 def test_project_merge_mode_merge_resolve(self):
4609 self._test_merge('merge-resolve')
4610
4611 def test_project_merge_mode_cherrypick(self):
4612 expected_messages = [
4613 'initial commit',
4614 'add content from fixture',
4615 'A-1',
4616 'B-1',
4617 'C-1']
4618 result = self._test_project_merge_mode('cherry-pick')
4619 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004620
4621 def test_merge_branch(self):
4622 "Test that the right commits are on alternate branches"
4623 self.create_branch('org/project-merge-branches', 'mp')
4624
4625 self.launch_server.hold_jobs_in_build = True
4626 A = self.fake_gerrit.addFakeChange(
4627 'org/project-merge-branches', 'mp', 'A')
4628 B = self.fake_gerrit.addFakeChange(
4629 'org/project-merge-branches', 'mp', 'B')
4630 C = self.fake_gerrit.addFakeChange(
4631 'org/project-merge-branches', 'mp', 'C')
4632 A.addApproval('code-review', 2)
4633 B.addApproval('code-review', 2)
4634 C.addApproval('code-review', 2)
4635 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4636 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4637 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4638 self.waitUntilSettled()
4639
4640 self.launch_server.release('.*-merge')
4641 self.waitUntilSettled()
4642 self.launch_server.release('.*-merge')
4643 self.waitUntilSettled()
4644 self.launch_server.release('.*-merge')
4645 self.waitUntilSettled()
4646
4647 build = self.builds[-1]
4648 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4649 ref = self.getParameter(build, 'ZUUL_REF')
4650 path = os.path.join(
4651 build.jobdir.git_root, 'org/project-merge-branches')
4652 repo = git.Repo(path)
4653
4654 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4655 repo_messages.reverse()
4656 correct_messages = [
4657 'initial commit',
4658 'add content from fixture',
4659 'mp commit',
4660 'A-1', 'B-1', 'C-1']
4661 self.assertEqual(repo_messages, correct_messages)
4662
4663 self.launch_server.hold_jobs_in_build = False
4664 self.launch_server.release()
4665 self.waitUntilSettled()
4666
4667 def test_merge_multi_branch(self):
4668 "Test that dependent changes on multiple branches are merged"
4669 self.create_branch('org/project-merge-branches', 'mp')
4670
4671 self.launch_server.hold_jobs_in_build = True
4672 A = self.fake_gerrit.addFakeChange(
4673 'org/project-merge-branches', 'master', 'A')
4674 B = self.fake_gerrit.addFakeChange(
4675 'org/project-merge-branches', 'mp', 'B')
4676 C = self.fake_gerrit.addFakeChange(
4677 'org/project-merge-branches', 'master', 'C')
4678 A.addApproval('code-review', 2)
4679 B.addApproval('code-review', 2)
4680 C.addApproval('code-review', 2)
4681 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4682 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4683 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4684 self.waitUntilSettled()
4685
4686 job_A = None
4687 for job in self.builds:
4688 if 'project-merge' in job.name:
4689 job_A = job
4690 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4691 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4692 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4693 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4694
4695 path = os.path.join(
4696 job_A.jobdir.git_root, "org/project-merge-branches")
4697 repo = git.Repo(path)
4698 repo_messages = [c.message.strip()
4699 for c in repo.iter_commits(ref_A)]
4700 repo_messages.reverse()
4701 correct_messages = [
4702 'initial commit', 'add content from fixture', 'A-1']
4703 self.assertEqual(repo_messages, correct_messages)
4704
4705 self.launch_server.release('.*-merge')
4706 self.waitUntilSettled()
4707
4708 job_B = None
4709 for job in self.builds:
4710 if 'project-merge' in job.name:
4711 job_B = job
4712 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4713 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4714 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4715 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4716
4717 path = os.path.join(
4718 job_B.jobdir.git_root, "org/project-merge-branches")
4719 repo = git.Repo(path)
4720 repo_messages = [c.message.strip()
4721 for c in repo.iter_commits(ref_B)]
4722 repo_messages.reverse()
4723 correct_messages = [
4724 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4725 self.assertEqual(repo_messages, correct_messages)
4726
4727 self.launch_server.release('.*-merge')
4728 self.waitUntilSettled()
4729
4730 job_C = None
4731 for job in self.builds:
4732 if 'project-merge' in job.name:
4733 job_C = job
4734 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4735 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4736 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4737 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
4738 path = os.path.join(
4739 job_C.jobdir.git_root, "org/project-merge-branches")
4740 repo = git.Repo(path)
4741 repo_messages = [c.message.strip()
4742 for c in repo.iter_commits(ref_C)]
4743
4744 repo_messages.reverse()
4745 correct_messages = [
4746 'initial commit', 'add content from fixture',
4747 'A-1', 'C-1']
4748 # Ensure the right commits are in the history for this ref
4749 self.assertEqual(repo_messages, correct_messages)
4750
4751 self.launch_server.hold_jobs_in_build = False
4752 self.launch_server.release()
4753 self.waitUntilSettled()