blob: ec88737b63bb9242798acb7077f5690f0ba1fc0f [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
Paul Belanger174a8272017-03-14 13:20:10 -040042 def test_jobs_executed(self):
43 "Test that jobs are executed 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
Paul Belanger174a8272017-03-14 13:20:10 -0400109 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400126 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400136 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400152 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400170 self.executor_server.hold_jobs_in_build = False
171 self.executor_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"
Paul Belanger174a8272017-03-14 13:20:10 -0400185 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400192 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400199 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400208 self.executor_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
Paul Belanger174a8272017-03-14 13:20:10 -0400220 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700221 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
Paul Belanger174a8272017-03-14 13:20:10 -0400245 self.executor_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))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800254 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700255
James E. Blairf59f3cf2017-02-19 14:50:26 -0800256 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
257 self.waitUntilSettled()
258
259 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260 self.waitUntilSettled()
261
262 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700263 self.assertBuilds([
264 dict(name='project-merge', changes='1,1'),
265 dict(name='project-merge', changes='2,1'),
266 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700267
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700268 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700269 self.builds[0].release()
270 self.waitUntilSettled()
271 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700272 self.waitUntilSettled()
273 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400274 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700275 self.waitUntilSettled()
276
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700277 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700278 self.assertBuilds([
279 dict(name='project-test1', changes='1,1'),
280 dict(name='project-test2', changes='1,1'),
281 dict(name='project-test1', changes='2,1'),
282 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500283 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700284 dict(name='project-test1', changes='2,1 3,1'),
285 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800286 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700287 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700288
James E. Blair5f0d49e2016-08-19 09:36:50 -0700289 self.orderedRelease()
290 self.assertHistory([
291 dict(name='project-merge', result='SUCCESS', changes='1,1'),
292 dict(name='project-merge', result='SUCCESS', changes='2,1'),
293 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
294 dict(name='project-test1', result='SUCCESS', changes='1,1'),
295 dict(name='project-test2', result='SUCCESS', changes='1,1'),
296 dict(name='project-test1', result='SUCCESS', changes='2,1'),
297 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500298 dict(
299 name='project1-project2-integration',
300 result='SUCCESS',
301 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700302 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
303 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800304 dict(name='project1-project2-integration',
305 result='SUCCESS',
306 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700307 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700308
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400309 self.assertEqual(A.data['status'], 'MERGED')
310 self.assertEqual(B.data['status'], 'MERGED')
311 self.assertEqual(C.data['status'], 'MERGED')
312 self.assertEqual(A.reported, 2)
313 self.assertEqual(B.reported, 2)
314 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700315
316 def test_failed_change_at_head(self):
317 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700318
Paul Belanger174a8272017-03-14 13:20:10 -0400319 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700320 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
321 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
322 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700323 A.addApproval('code-review', 2)
324 B.addApproval('code-review', 2)
325 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700326
Paul Belanger174a8272017-03-14 13:20:10 -0400327 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700328
James E. Blair8b5408c2016-08-08 15:37:46 -0700329 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
330 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
331 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700332
333 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700334
James E. Blairb536ecc2016-08-31 10:11:42 -0700335 self.assertBuilds([
336 dict(name='project-merge', changes='1,1'),
337 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700338
Paul Belanger174a8272017-03-14 13:20:10 -0400339 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700340 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400341 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700342 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400343 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700344 self.waitUntilSettled()
345
James E. Blairb536ecc2016-08-31 10:11:42 -0700346 self.assertBuilds([
347 dict(name='project-test1', changes='1,1'),
348 dict(name='project-test2', changes='1,1'),
349 dict(name='project-test1', changes='1,1 2,1'),
350 dict(name='project-test2', changes='1,1 2,1'),
351 dict(name='project-test1', changes='1,1 2,1 3,1'),
352 dict(name='project-test2', changes='1,1 2,1 3,1'),
353 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700354
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400355 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700356 self.waitUntilSettled()
357
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400358 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700359 self.assertBuilds([
360 dict(name='project-test2', changes='1,1'),
361 dict(name='project-merge', changes='2,1'),
362 ])
363 # Unordered history comparison because the aborts can finish
364 # in any order.
365 self.assertHistory([
366 dict(name='project-merge', result='SUCCESS',
367 changes='1,1'),
368 dict(name='project-merge', result='SUCCESS',
369 changes='1,1 2,1'),
370 dict(name='project-merge', result='SUCCESS',
371 changes='1,1 2,1 3,1'),
372 dict(name='project-test1', result='FAILURE',
373 changes='1,1'),
374 dict(name='project-test1', result='ABORTED',
375 changes='1,1 2,1'),
376 dict(name='project-test2', result='ABORTED',
377 changes='1,1 2,1'),
378 dict(name='project-test1', result='ABORTED',
379 changes='1,1 2,1 3,1'),
380 dict(name='project-test2', result='ABORTED',
381 changes='1,1 2,1 3,1'),
382 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700383
Paul Belanger174a8272017-03-14 13:20:10 -0400384 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700385 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400386 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700387 self.waitUntilSettled()
388 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700389
James E. Blairb536ecc2016-08-31 10:11:42 -0700390 self.assertBuilds([])
391 self.assertHistory([
392 dict(name='project-merge', result='SUCCESS',
393 changes='1,1'),
394 dict(name='project-merge', result='SUCCESS',
395 changes='1,1 2,1'),
396 dict(name='project-merge', result='SUCCESS',
397 changes='1,1 2,1 3,1'),
398 dict(name='project-test1', result='FAILURE',
399 changes='1,1'),
400 dict(name='project-test1', result='ABORTED',
401 changes='1,1 2,1'),
402 dict(name='project-test2', result='ABORTED',
403 changes='1,1 2,1'),
404 dict(name='project-test1', result='ABORTED',
405 changes='1,1 2,1 3,1'),
406 dict(name='project-test2', result='ABORTED',
407 changes='1,1 2,1 3,1'),
408 dict(name='project-merge', result='SUCCESS',
409 changes='2,1'),
410 dict(name='project-merge', result='SUCCESS',
411 changes='2,1 3,1'),
412 dict(name='project-test2', result='SUCCESS',
413 changes='1,1'),
414 dict(name='project-test1', result='SUCCESS',
415 changes='2,1'),
416 dict(name='project-test2', result='SUCCESS',
417 changes='2,1'),
418 dict(name='project-test1', result='SUCCESS',
419 changes='2,1 3,1'),
420 dict(name='project-test2', result='SUCCESS',
421 changes='2,1 3,1'),
422 ], ordered=False)
423
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400424 self.assertEqual(A.data['status'], 'NEW')
425 self.assertEqual(B.data['status'], 'MERGED')
426 self.assertEqual(C.data['status'], 'MERGED')
427 self.assertEqual(A.reported, 2)
428 self.assertEqual(B.reported, 2)
429 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700430
James E. Blair0aac4872013-08-23 14:02:38 -0700431 def test_failed_change_in_middle(self):
432 "Test a failed change in the middle of the queue"
433
Paul Belanger174a8272017-03-14 13:20:10 -0400434 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700435 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
436 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
437 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700438 A.addApproval('code-review', 2)
439 B.addApproval('code-review', 2)
440 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700441
Paul Belanger174a8272017-03-14 13:20:10 -0400442 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700443
James E. Blair8b5408c2016-08-08 15:37:46 -0700444 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
445 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
446 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700447
448 self.waitUntilSettled()
449
Paul Belanger174a8272017-03-14 13:20:10 -0400450 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700451 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400452 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700453 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400454 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700455 self.waitUntilSettled()
456
457 self.assertEqual(len(self.builds), 6)
458 self.assertEqual(self.builds[0].name, 'project-test1')
459 self.assertEqual(self.builds[1].name, 'project-test2')
460 self.assertEqual(self.builds[2].name, 'project-test1')
461 self.assertEqual(self.builds[3].name, 'project-test2')
462 self.assertEqual(self.builds[4].name, 'project-test1')
463 self.assertEqual(self.builds[5].name, 'project-test2')
464
465 self.release(self.builds[2])
466 self.waitUntilSettled()
467
James E. Blair972e3c72013-08-29 12:04:55 -0700468 # project-test1 and project-test2 for A
469 # project-test2 for B
470 # project-merge for C (without B)
471 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700472 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
473
Paul Belanger174a8272017-03-14 13:20:10 -0400474 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700475 self.waitUntilSettled()
476
477 # project-test1 and project-test2 for A
478 # project-test2 for B
479 # project-test1 and project-test2 for C
480 self.assertEqual(len(self.builds), 5)
481
Paul Belanger2e2a0492016-10-30 18:33:37 -0400482 tenant = self.sched.abide.tenants.get('tenant-one')
483 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700484 builds = items[0].current_build_set.getBuilds()
485 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
486 self.assertEqual(self.countJobResults(builds, None), 2)
487 builds = items[1].current_build_set.getBuilds()
488 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
489 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
490 self.assertEqual(self.countJobResults(builds, None), 1)
491 builds = items[2].current_build_set.getBuilds()
492 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700493 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700494
Paul Belanger174a8272017-03-14 13:20:10 -0400495 self.executor_server.hold_jobs_in_build = False
496 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700497 self.waitUntilSettled()
498
499 self.assertEqual(len(self.builds), 0)
500 self.assertEqual(len(self.history), 12)
501 self.assertEqual(A.data['status'], 'MERGED')
502 self.assertEqual(B.data['status'], 'NEW')
503 self.assertEqual(C.data['status'], 'MERGED')
504 self.assertEqual(A.reported, 2)
505 self.assertEqual(B.reported, 2)
506 self.assertEqual(C.reported, 2)
507
James E. Blaird466dc42012-07-31 10:42:56 -0700508 def test_failed_change_at_head_with_queue(self):
509 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700510
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700511 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700512 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
513 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
514 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700515 A.addApproval('code-review', 2)
516 B.addApproval('code-review', 2)
517 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700518
Paul Belanger174a8272017-03-14 13:20:10 -0400519 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700520
James E. Blair8b5408c2016-08-08 15:37:46 -0700521 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
522 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
523 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700524
525 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700526 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400527 self.assertEqual(len(self.builds), 0)
528 self.assertEqual(len(queue), 1)
Paul Belanger174a8272017-03-14 13:20:10 -0400529 self.assertEqual(queue[0].name, 'executor:execute')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800530 job_args = json.loads(queue[0].arguments)
531 self.assertEqual(job_args['job'], 'project-merge')
532 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700533
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700534 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700535 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700536 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700537 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700538 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700539 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700540 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700541
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400542 self.assertEqual(len(self.builds), 0)
543 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800544
545 self.assertEqual(
546 json.loads(queue[0].arguments)['job'], 'project-test1')
547 self.assertEqual(
548 json.loads(queue[1].arguments)['job'], 'project-test2')
549 self.assertEqual(
550 json.loads(queue[2].arguments)['job'], 'project-test1')
551 self.assertEqual(
552 json.loads(queue[3].arguments)['job'], 'project-test2')
553 self.assertEqual(
554 json.loads(queue[4].arguments)['job'], 'project-test1')
555 self.assertEqual(
556 json.loads(queue[5].arguments)['job'], 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700557
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700558 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700559 self.waitUntilSettled()
560
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400561 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700562 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400563 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
564 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700565
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700566 self.gearman_server.hold_jobs_in_queue = False
567 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700568 self.waitUntilSettled()
569
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400570 self.assertEqual(len(self.builds), 0)
571 self.assertEqual(len(self.history), 11)
572 self.assertEqual(A.data['status'], 'NEW')
573 self.assertEqual(B.data['status'], 'MERGED')
574 self.assertEqual(C.data['status'], 'MERGED')
575 self.assertEqual(A.reported, 2)
576 self.assertEqual(B.reported, 2)
577 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700578
James E. Blairec056492016-07-22 09:45:56 -0700579 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700580 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400581 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700583 A.addApproval('code-review', 2)
584 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700585 self.waitUntilSettled()
586 time.sleep(2)
587
588 data = json.loads(self.sched.formatStatusJSON())
589 found_job = None
590 for pipeline in data['pipelines']:
591 if pipeline['name'] != 'gate':
592 continue
593 for queue in pipeline['change_queues']:
594 for head in queue['heads']:
595 for item in head:
596 for job in item['jobs']:
597 if job['name'] == 'project-merge':
598 found_job = job
599 break
600
601 self.assertIsNotNone(found_job)
602 if iteration == 1:
603 self.assertIsNotNone(found_job['estimated_time'])
604 self.assertIsNone(found_job['remaining_time'])
605 else:
606 self.assertIsNotNone(found_job['estimated_time'])
607 self.assertTrue(found_job['estimated_time'] >= 2)
608 self.assertIsNotNone(found_job['remaining_time'])
609
Paul Belanger174a8272017-03-14 13:20:10 -0400610 self.executor_server.hold_jobs_in_build = False
611 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700612 self.waitUntilSettled()
613
James E. Blairec056492016-07-22 09:45:56 -0700614 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700615 def test_time_database(self):
616 "Test the time database"
617
618 self._test_time_database(1)
619 self._test_time_database(2)
620
James E. Blairfef71632013-09-23 11:15:47 -0700621 def test_two_failed_changes_at_head(self):
622 "Test that changes are reparented correctly if 2 fail at head"
623
Paul Belanger174a8272017-03-14 13:20:10 -0400624 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
626 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
627 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700628 A.addApproval('code-review', 2)
629 B.addApproval('code-review', 2)
630 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700631
Paul Belanger174a8272017-03-14 13:20:10 -0400632 self.executor_server.failJob('project-test1', A)
633 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700634
James E. Blair8b5408c2016-08-08 15:37:46 -0700635 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
636 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
637 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700638 self.waitUntilSettled()
639
Paul Belanger174a8272017-03-14 13:20:10 -0400640 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700641 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400642 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700643 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400644 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700645 self.waitUntilSettled()
646
647 self.assertEqual(len(self.builds), 6)
648 self.assertEqual(self.builds[0].name, 'project-test1')
649 self.assertEqual(self.builds[1].name, 'project-test2')
650 self.assertEqual(self.builds[2].name, 'project-test1')
651 self.assertEqual(self.builds[3].name, 'project-test2')
652 self.assertEqual(self.builds[4].name, 'project-test1')
653 self.assertEqual(self.builds[5].name, 'project-test2')
654
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400655 self.assertTrue(self.builds[0].hasChanges(A))
656 self.assertTrue(self.builds[2].hasChanges(A))
657 self.assertTrue(self.builds[2].hasChanges(B))
658 self.assertTrue(self.builds[4].hasChanges(A))
659 self.assertTrue(self.builds[4].hasChanges(B))
660 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700661
662 # Fail change B first
663 self.release(self.builds[2])
664 self.waitUntilSettled()
665
666 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400667 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700668 self.waitUntilSettled()
669
670 self.assertEqual(len(self.builds), 5)
671 self.assertEqual(self.builds[0].name, 'project-test1')
672 self.assertEqual(self.builds[1].name, 'project-test2')
673 self.assertEqual(self.builds[2].name, 'project-test2')
674 self.assertEqual(self.builds[3].name, 'project-test1')
675 self.assertEqual(self.builds[4].name, 'project-test2')
676
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400677 self.assertTrue(self.builds[1].hasChanges(A))
678 self.assertTrue(self.builds[2].hasChanges(A))
679 self.assertTrue(self.builds[2].hasChanges(B))
680 self.assertTrue(self.builds[4].hasChanges(A))
681 self.assertFalse(self.builds[4].hasChanges(B))
682 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700683
684 # Finish running all passing jobs for change A
685 self.release(self.builds[1])
686 self.waitUntilSettled()
687 # Fail and report change A
688 self.release(self.builds[0])
689 self.waitUntilSettled()
690
691 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400692 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700693 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400694 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700695 self.waitUntilSettled()
696
697 self.assertEqual(len(self.builds), 4)
698 self.assertEqual(self.builds[0].name, 'project-test1') # B
699 self.assertEqual(self.builds[1].name, 'project-test2') # B
700 self.assertEqual(self.builds[2].name, 'project-test1') # C
701 self.assertEqual(self.builds[3].name, 'project-test2') # C
702
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400703 self.assertFalse(self.builds[1].hasChanges(A))
704 self.assertTrue(self.builds[1].hasChanges(B))
705 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700706
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400707 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700708 # After A failed and B and C restarted, B should be back in
709 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400710 self.assertTrue(self.builds[2].hasChanges(B))
711 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700712
Paul Belanger174a8272017-03-14 13:20:10 -0400713 self.executor_server.hold_jobs_in_build = False
714 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700715 self.waitUntilSettled()
716
717 self.assertEqual(len(self.builds), 0)
718 self.assertEqual(len(self.history), 21)
719 self.assertEqual(A.data['status'], 'NEW')
720 self.assertEqual(B.data['status'], 'NEW')
721 self.assertEqual(C.data['status'], 'MERGED')
722 self.assertEqual(A.reported, 2)
723 self.assertEqual(B.reported, 2)
724 self.assertEqual(C.reported, 2)
725
James E. Blair8c803f82012-07-31 16:25:42 -0700726 def test_patch_order(self):
727 "Test that dependent patches are tested in the right order"
728 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
729 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
730 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700731 A.addApproval('code-review', 2)
732 B.addApproval('code-review', 2)
733 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700734
735 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
736 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
737 M2.setMerged()
738 M1.setMerged()
739
740 # C -> B -> A -> M1 -> M2
741 # M2 is here to make sure it is never queried. If it is, it
742 # means zuul is walking down the entire history of merged
743 # changes.
744
745 C.setDependsOn(B, 1)
746 B.setDependsOn(A, 1)
747 A.setDependsOn(M1, 1)
748 M1.setDependsOn(M2, 1)
749
James E. Blair8b5408c2016-08-08 15:37:46 -0700750 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700751
752 self.waitUntilSettled()
753
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400754 self.assertEqual(A.data['status'], 'NEW')
755 self.assertEqual(B.data['status'], 'NEW')
756 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700757
James E. Blair8b5408c2016-08-08 15:37:46 -0700758 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
759 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700760
761 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400762 self.assertEqual(M2.queried, 0)
763 self.assertEqual(A.data['status'], 'MERGED')
764 self.assertEqual(B.data['status'], 'MERGED')
765 self.assertEqual(C.data['status'], 'MERGED')
766 self.assertEqual(A.reported, 2)
767 self.assertEqual(B.reported, 2)
768 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700769
James E. Blair063672f2015-01-29 13:09:12 -0800770 def test_needed_changes_enqueue(self):
771 "Test that a needed change is enqueued ahead"
772 # A Given a git tree like this, if we enqueue
773 # / \ change C, we should walk up and down the tree
774 # B G and enqueue changes in the order ABCDEFG.
775 # /|\ This is also the order that you would get if
776 # *C E F you enqueued changes in the order ABCDEFG, so
777 # / the ordering is stable across re-enqueue events.
778 # D
779
780 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
781 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
782 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
783 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
784 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
785 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
786 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
787 B.setDependsOn(A, 1)
788 C.setDependsOn(B, 1)
789 D.setDependsOn(C, 1)
790 E.setDependsOn(B, 1)
791 F.setDependsOn(B, 1)
792 G.setDependsOn(A, 1)
793
James E. Blair8b5408c2016-08-08 15:37:46 -0700794 A.addApproval('code-review', 2)
795 B.addApproval('code-review', 2)
796 C.addApproval('code-review', 2)
797 D.addApproval('code-review', 2)
798 E.addApproval('code-review', 2)
799 F.addApproval('code-review', 2)
800 G.addApproval('code-review', 2)
801 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800802
803 self.waitUntilSettled()
804
805 self.assertEqual(A.data['status'], 'NEW')
806 self.assertEqual(B.data['status'], 'NEW')
807 self.assertEqual(C.data['status'], 'NEW')
808 self.assertEqual(D.data['status'], 'NEW')
809 self.assertEqual(E.data['status'], 'NEW')
810 self.assertEqual(F.data['status'], 'NEW')
811 self.assertEqual(G.data['status'], 'NEW')
812
813 # We're about to add approvals to changes without adding the
814 # triggering events to Zuul, so that we can be sure that it is
815 # enqueing the changes based on dependencies, not because of
816 # triggering events. Since it will have the changes cached
817 # already (without approvals), we need to clear the cache
818 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400819 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100820 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800821
Paul Belanger174a8272017-03-14 13:20:10 -0400822 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700823 A.addApproval('approved', 1)
824 B.addApproval('approved', 1)
825 D.addApproval('approved', 1)
826 E.addApproval('approved', 1)
827 F.addApproval('approved', 1)
828 G.addApproval('approved', 1)
829 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800830
831 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400832 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800833 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400834 self.executor_server.hold_jobs_in_build = False
835 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800836 self.waitUntilSettled()
837
838 self.assertEqual(A.data['status'], 'MERGED')
839 self.assertEqual(B.data['status'], 'MERGED')
840 self.assertEqual(C.data['status'], 'MERGED')
841 self.assertEqual(D.data['status'], 'MERGED')
842 self.assertEqual(E.data['status'], 'MERGED')
843 self.assertEqual(F.data['status'], 'MERGED')
844 self.assertEqual(G.data['status'], 'MERGED')
845 self.assertEqual(A.reported, 2)
846 self.assertEqual(B.reported, 2)
847 self.assertEqual(C.reported, 2)
848 self.assertEqual(D.reported, 2)
849 self.assertEqual(E.reported, 2)
850 self.assertEqual(F.reported, 2)
851 self.assertEqual(G.reported, 2)
852 self.assertEqual(self.history[6].changes,
853 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
854
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100855 def test_source_cache(self):
856 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400857 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700858
859 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
860 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
861 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700862 A.addApproval('code-review', 2)
863 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700864
865 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
866 M1.setMerged()
867
868 B.setDependsOn(A, 1)
869 A.setDependsOn(M1, 1)
870
James E. Blair8b5408c2016-08-08 15:37:46 -0700871 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700872 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
873
874 self.waitUntilSettled()
875
876 for build in self.builds:
877 if build.parameters['ZUUL_PIPELINE'] == 'check':
878 build.release()
879 self.waitUntilSettled()
880 for build in self.builds:
881 if build.parameters['ZUUL_PIPELINE'] == 'check':
882 build.release()
883 self.waitUntilSettled()
884
James E. Blair8b5408c2016-08-08 15:37:46 -0700885 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700886 self.waitUntilSettled()
887
Joshua Hesketh352264b2015-08-11 23:42:08 +1000888 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700889 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000890 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700891
Paul Belanger174a8272017-03-14 13:20:10 -0400892 self.executor_server.hold_jobs_in_build = False
893 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700894 self.waitUntilSettled()
895
896 self.assertEqual(A.data['status'], 'MERGED')
897 self.assertEqual(B.data['status'], 'MERGED')
898 self.assertEqual(A.queried, 2) # Initial and isMerged
899 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
900
James E. Blair8c803f82012-07-31 16:25:42 -0700901 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700902 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700903 # TODO: move to test_gerrit (this is a unit test!)
904 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400905 tenant = self.sched.abide.tenants.get('tenant-one')
906 source = tenant.layout.pipelines['gate'].source
907
908 # TODO(pabelanger): As we add more source / trigger APIs we should make
909 # it easier for users to create events for testing.
910 event = zuul.model.TriggerEvent()
911 event.trigger_name = 'gerrit'
912 event.change_number = '1'
913 event.patch_number = '2'
914
915 a = source.getChange(event)
916 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700917 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700918
James E. Blair8b5408c2016-08-08 15:37:46 -0700919 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400920 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700921 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700922
James E. Blair8b5408c2016-08-08 15:37:46 -0700923 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400924 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700925 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700926
James E. Blairec056492016-07-22 09:45:56 -0700927 @skip("Disabled for early v3 development")
James E. Blair973721f2012-08-15 10:19:43 -0700928 def test_build_configuration_conflict(self):
929 "Test that merge conflicts are handled"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700930
931 self.gearman_server.hold_jobs_in_queue = True
James E. Blair6736beb2013-07-11 15:18:15 -0700932 A = self.fake_gerrit.addFakeChange('org/conflict-project',
933 'master', 'A')
James E. Blair973721f2012-08-15 10:19:43 -0700934 A.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700935 B = self.fake_gerrit.addFakeChange('org/conflict-project',
936 'master', 'B')
James E. Blair973721f2012-08-15 10:19:43 -0700937 B.addPatchset(['conflict'])
James E. Blair6736beb2013-07-11 15:18:15 -0700938 C = self.fake_gerrit.addFakeChange('org/conflict-project',
939 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700940 A.addApproval('code-review', 2)
941 B.addApproval('code-review', 2)
942 C.addApproval('code-review', 2)
943 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
944 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
945 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700946 self.waitUntilSettled()
947
James E. Blair6736beb2013-07-11 15:18:15 -0700948 self.assertEqual(A.reported, 1)
949 self.assertEqual(B.reported, 1)
950 self.assertEqual(C.reported, 1)
951
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700952 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700953 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700954 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700955 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700956 self.gearman_server.release('.*-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700957 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700958
959 self.assertEqual(len(self.history), 2) # A and C merge jobs
960
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700961 self.gearman_server.hold_jobs_in_queue = False
962 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700963 self.waitUntilSettled()
964
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400965 self.assertEqual(A.data['status'], 'MERGED')
966 self.assertEqual(B.data['status'], 'NEW')
967 self.assertEqual(C.data['status'], 'MERGED')
968 self.assertEqual(A.reported, 2)
969 self.assertEqual(B.reported, 2)
970 self.assertEqual(C.reported, 2)
James E. Blair972e3c72013-08-29 12:04:55 -0700971 self.assertEqual(len(self.history), 6)
James E. Blair6736beb2013-07-11 15:18:15 -0700972
James E. Blairdaabed22012-08-15 15:38:57 -0700973 def test_post(self):
974 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700975
Zhongyue Luo5d556072012-09-21 02:00:47 +0900976 e = {
977 "type": "ref-updated",
978 "submitter": {
979 "name": "User Name",
980 },
981 "refUpdate": {
982 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
983 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
984 "refName": "master",
985 "project": "org/project",
986 }
987 }
James E. Blairdaabed22012-08-15 15:38:57 -0700988 self.fake_gerrit.addEvent(e)
989 self.waitUntilSettled()
990
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400991 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400992 self.assertEqual(len(self.history), 1)
993 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -0700994
K Jonathan Harkerf95e7232015-04-29 13:33:16 -0700995 def test_post_ignore_deletes(self):
996 "Test that deleting refs does not trigger post jobs"
997
998 e = {
999 "type": "ref-updated",
1000 "submitter": {
1001 "name": "User Name",
1002 },
1003 "refUpdate": {
1004 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1005 "newRev": "0000000000000000000000000000000000000000",
1006 "refName": "master",
1007 "project": "org/project",
1008 }
1009 }
1010 self.fake_gerrit.addEvent(e)
1011 self.waitUntilSettled()
1012
1013 job_names = [x.name for x in self.history]
1014 self.assertEqual(len(self.history), 0)
1015 self.assertNotIn('project-post', job_names)
1016
1017 def test_post_ignore_deletes_negative(self):
1018 "Test that deleting refs does trigger post jobs"
1019
Adam Gandelmanc5e4f1d2016-11-29 14:27:17 -08001020 self.updateConfigLayout('layout-dont-ignore-ref-deletes')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001021 self.sched.reconfigure(self.config)
1022
1023 e = {
1024 "type": "ref-updated",
1025 "submitter": {
1026 "name": "User Name",
1027 },
1028 "refUpdate": {
1029 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1030 "newRev": "0000000000000000000000000000000000000000",
1031 "refName": "master",
1032 "project": "org/project",
1033 }
1034 }
1035 self.fake_gerrit.addEvent(e)
1036 self.waitUntilSettled()
1037
1038 job_names = [x.name for x in self.history]
1039 self.assertEqual(len(self.history), 1)
1040 self.assertIn('project-post', job_names)
1041
James E. Blairec056492016-07-22 09:45:56 -07001042 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001043 def test_build_configuration_branch_interaction(self):
1044 "Test that switching between branches works"
1045 self.test_build_configuration()
1046 self.test_build_configuration_branch()
1047 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001048 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001049 repo = git.Repo(path)
1050 repo.heads.master.commit = repo.commit('init')
1051 self.test_build_configuration()
1052
James E. Blaircaec0c52012-08-22 14:52:22 -07001053 def test_dependent_changes_dequeue(self):
1054 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001055
James E. Blaircaec0c52012-08-22 14:52:22 -07001056 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1057 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1058 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001059 A.addApproval('code-review', 2)
1060 B.addApproval('code-review', 2)
1061 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001062
1063 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1064 M1.setMerged()
1065
1066 # C -> B -> A -> M1
1067
1068 C.setDependsOn(B, 1)
1069 B.setDependsOn(A, 1)
1070 A.setDependsOn(M1, 1)
1071
Paul Belanger174a8272017-03-14 13:20:10 -04001072 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001073
James E. Blair8b5408c2016-08-08 15:37:46 -07001074 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1075 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1076 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001077
1078 self.waitUntilSettled()
1079
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001080 self.assertEqual(A.data['status'], 'NEW')
1081 self.assertEqual(A.reported, 2)
1082 self.assertEqual(B.data['status'], 'NEW')
1083 self.assertEqual(B.reported, 2)
1084 self.assertEqual(C.data['status'], 'NEW')
1085 self.assertEqual(C.reported, 2)
1086 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001087
James E. Blair972e3c72013-08-29 12:04:55 -07001088 def test_failing_dependent_changes(self):
1089 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001090 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001091 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1092 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1093 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1094 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1095 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001096 A.addApproval('code-review', 2)
1097 B.addApproval('code-review', 2)
1098 C.addApproval('code-review', 2)
1099 D.addApproval('code-review', 2)
1100 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001101
1102 # E, D -> C -> B, A
1103
1104 D.setDependsOn(C, 1)
1105 C.setDependsOn(B, 1)
1106
Paul Belanger174a8272017-03-14 13:20:10 -04001107 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001108
James E. Blair8b5408c2016-08-08 15:37:46 -07001109 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1110 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1111 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1112 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1113 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001114
1115 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001116 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001117 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001118 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001119 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001120 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001121 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001122 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001123 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001124 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001125 self.waitUntilSettled()
1126
Paul Belanger174a8272017-03-14 13:20:10 -04001127 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001128 for build in self.builds:
1129 if build.parameters['ZUUL_CHANGE'] != '1':
1130 build.release()
1131 self.waitUntilSettled()
1132
Paul Belanger174a8272017-03-14 13:20:10 -04001133 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001134 self.waitUntilSettled()
1135
1136 self.assertEqual(A.data['status'], 'MERGED')
1137 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001138 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001139 self.assertEqual(B.data['status'], 'NEW')
1140 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001141 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001142 self.assertEqual(C.data['status'], 'NEW')
1143 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001144 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001145 self.assertEqual(D.data['status'], 'NEW')
1146 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001147 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001148 self.assertEqual(E.data['status'], 'MERGED')
1149 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001150 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001151 self.assertEqual(len(self.history), 18)
1152
James E. Blairec590122012-08-22 15:19:31 -07001153 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001154 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001155 # If it's dequeued more than once, we should see extra
1156 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001157
Paul Belanger174a8272017-03-14 13:20:10 -04001158 self.executor_server.hold_jobs_in_build = True
James E. Blairec590122012-08-22 15:19:31 -07001159 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1160 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1161 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001162 A.addApproval('code-review', 2)
1163 B.addApproval('code-review', 2)
1164 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001165
Paul Belanger174a8272017-03-14 13:20:10 -04001166 self.executor_server.failJob('project-test1', A)
1167 self.executor_server.failJob('project-test2', A)
1168 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairec590122012-08-22 15:19:31 -07001169
James E. Blair8b5408c2016-08-08 15:37:46 -07001170 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1171 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1172 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001173
1174 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001175
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001176 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001177 self.assertEqual(self.builds[0].name, 'project-merge')
1178 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001179
Paul Belanger174a8272017-03-14 13:20:10 -04001180 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001181 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001182 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001183 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001184 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001185 self.waitUntilSettled()
1186
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001187 self.assertEqual(len(self.builds), 9)
Paul Belanger58bf6912016-11-11 19:36:01 -05001188 self.assertEqual(self.builds[0].name, 'project-test1')
1189 self.assertEqual(self.builds[1].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001190 self.assertEqual(self.builds[2].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001191 self.assertEqual(self.builds[3].name, 'project-test1')
1192 self.assertEqual(self.builds[4].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001193 self.assertEqual(self.builds[5].name, 'project1-project2-integration')
Paul Belanger58bf6912016-11-11 19:36:01 -05001194 self.assertEqual(self.builds[6].name, 'project-test1')
1195 self.assertEqual(self.builds[7].name, 'project-test2')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001196 self.assertEqual(self.builds[8].name, 'project1-project2-integration')
James E. Blairec590122012-08-22 15:19:31 -07001197
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001198 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001199 self.waitUntilSettled()
1200
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001201 self.assertEqual(len(self.builds), 3) # test2,integration, merge for B
1202 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 6)
James E. Blairec590122012-08-22 15:19:31 -07001203
Paul Belanger174a8272017-03-14 13:20:10 -04001204 self.executor_server.hold_jobs_in_build = False
1205 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001206 self.waitUntilSettled()
1207
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001208 self.assertEqual(len(self.builds), 0)
1209 self.assertEqual(len(self.history), 20)
James E. Blaircaec0c52012-08-22 14:52:22 -07001210
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001211 self.assertEqual(A.data['status'], 'NEW')
1212 self.assertEqual(B.data['status'], 'MERGED')
1213 self.assertEqual(C.data['status'], 'MERGED')
1214 self.assertEqual(A.reported, 2)
1215 self.assertEqual(B.reported, 2)
1216 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001217
1218 def test_nonvoting_job(self):
1219 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001220
James E. Blair4ec821f2012-08-23 15:28:28 -07001221 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1222 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001223 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001224 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001225 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001226
1227 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001228
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001229 self.assertEqual(A.data['status'], 'MERGED')
1230 self.assertEqual(A.reported, 2)
1231 self.assertEqual(
1232 self.getJobFromHistory('nonvoting-project-merge').result,
1233 'SUCCESS')
1234 self.assertEqual(
1235 self.getJobFromHistory('nonvoting-project-test1').result,
1236 'SUCCESS')
1237 self.assertEqual(
1238 self.getJobFromHistory('nonvoting-project-test2').result,
1239 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001240
James E. Blair5821bd92015-09-16 08:48:15 -07001241 for build in self.builds:
1242 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1243
James E. Blaire0487072012-08-29 17:38:31 -07001244 def test_check_queue_success(self):
1245 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001246
James E. Blaire0487072012-08-29 17:38:31 -07001247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1248 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1249
1250 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001251
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001252 self.assertEqual(A.data['status'], 'NEW')
1253 self.assertEqual(A.reported, 1)
1254 self.assertEqual(self.getJobFromHistory('project-merge').result,
1255 'SUCCESS')
1256 self.assertEqual(self.getJobFromHistory('project-test1').result,
1257 'SUCCESS')
1258 self.assertEqual(self.getJobFromHistory('project-test2').result,
1259 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001260
1261 def test_check_queue_failure(self):
1262 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001263
James E. Blaire0487072012-08-29 17:38:31 -07001264 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001265 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001266 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1267
1268 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001269
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(A.data['status'], 'NEW')
1271 self.assertEqual(A.reported, 1)
1272 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001273 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001274 self.assertEqual(self.getJobFromHistory('project-test1').result,
1275 'SUCCESS')
1276 self.assertEqual(self.getJobFromHistory('project-test2').result,
1277 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001278
1279 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001280 # This particular test does a large amount of merges and needs a little
1281 # more time to complete
1282 self.wait_timeout = 90
James E. Blair127bc182012-08-28 15:55:15 -07001283 "test that dependent changes behind dequeued changes work"
1284 # This complicated test is a reproduction of a real life bug
1285 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001286
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001288 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1289 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1290 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1291 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1292 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1293 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1294 D.setDependsOn(C, 1)
1295 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001296 A.addApproval('code-review', 2)
1297 B.addApproval('code-review', 2)
1298 C.addApproval('code-review', 2)
1299 D.addApproval('code-review', 2)
1300 E.addApproval('code-review', 2)
1301 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001302
1303 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001304
1305 # Change object re-use in the gerrit trigger is hidden if
1306 # changes are added in quick succession; waiting makes it more
1307 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001308 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001309 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001310 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001311 self.waitUntilSettled()
1312
Paul Belanger174a8272017-03-14 13:20:10 -04001313 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001314 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001315 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001316 self.waitUntilSettled()
1317
James E. Blair8b5408c2016-08-08 15:37:46 -07001318 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001319 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001320 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001321 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001322 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001323 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001324 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001325 self.waitUntilSettled()
1326
Paul Belanger174a8272017-03-14 13:20:10 -04001327 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001328 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001329 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001330 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001331 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001332 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001333 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001334 self.waitUntilSettled()
1335
1336 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001337
1338 # Grab pointers to the jobs we want to release before
1339 # releasing any, because list indexes may change as
1340 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001341 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001342 a.release()
1343 b.release()
1344 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001345 self.waitUntilSettled()
1346
Paul Belanger174a8272017-03-14 13:20:10 -04001347 self.executor_server.hold_jobs_in_build = False
1348 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001349 self.waitUntilSettled()
1350
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001351 self.assertEqual(A.data['status'], 'NEW')
1352 self.assertEqual(B.data['status'], 'MERGED')
1353 self.assertEqual(C.data['status'], 'MERGED')
1354 self.assertEqual(D.data['status'], 'MERGED')
1355 self.assertEqual(E.data['status'], 'MERGED')
1356 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001357
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001358 self.assertEqual(A.reported, 2)
1359 self.assertEqual(B.reported, 2)
1360 self.assertEqual(C.reported, 2)
1361 self.assertEqual(D.reported, 2)
1362 self.assertEqual(E.reported, 2)
1363 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001364
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001365 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1366 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001367
1368 def test_merger_repack(self):
1369 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001370
James E. Blair05fed602012-09-07 12:45:24 -07001371 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001372 A.addApproval('code-review', 2)
1373 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001374 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001375 self.assertEqual(self.getJobFromHistory('project-merge').result,
1376 'SUCCESS')
1377 self.assertEqual(self.getJobFromHistory('project-test1').result,
1378 'SUCCESS')
1379 self.assertEqual(self.getJobFromHistory('project-test2').result,
1380 'SUCCESS')
1381 self.assertEqual(A.data['status'], 'MERGED')
1382 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001383 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001384 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001385
Monty Taylord642d852017-02-23 14:05:42 -05001386 path = os.path.join(self.merger_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001387 if os.path.exists(path):
1388 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001389 path = os.path.join(self.executor_src_root, "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001390 if os.path.exists(path):
1391 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001392
1393 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001394 A.addApproval('code-review', 2)
1395 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001396 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001397 self.assertEqual(self.getJobFromHistory('project-merge').result,
1398 'SUCCESS')
1399 self.assertEqual(self.getJobFromHistory('project-test1').result,
1400 'SUCCESS')
1401 self.assertEqual(self.getJobFromHistory('project-test2').result,
1402 'SUCCESS')
1403 self.assertEqual(A.data['status'], 'MERGED')
1404 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001405
James E. Blair4886f282012-11-15 09:27:33 -08001406 def test_merger_repack_large_change(self):
1407 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001408 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001409 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001410 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Hesketh352264b2015-08-11 23:42:08 +10001411 url = self.fake_gerrit.getGitUrl(
Paul Belanger503bded2016-11-11 19:43:38 -05001412 tenant.layout.project_configs.get('org/project1'))
James E. Blair4076e2b2014-01-28 12:42:20 -08001413 self.merge_server.merger.addProject('org/project1', url)
James E. Blair4886f282012-11-15 09:27:33 -08001414 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1415 A.addPatchset(large=True)
Monty Taylorbc758832013-06-17 17:22:42 -04001416 path = os.path.join(self.upstream_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001417 repack_repo(path)
Monty Taylord642d852017-02-23 14:05:42 -05001418 path = os.path.join(self.merger_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001419 if os.path.exists(path):
1420 repack_repo(path)
Paul Belanger174a8272017-03-14 13:20:10 -04001421 path = os.path.join(self.executor_src_root, "org/project1")
James E. Blair8c1be532017-02-07 14:04:12 -08001422 if os.path.exists(path):
1423 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001424
James E. Blair8b5408c2016-08-08 15:37:46 -07001425 A.addApproval('code-review', 2)
1426 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001427 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001428 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001429 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001430 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001431 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001432 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001433 'SUCCESS')
1434 self.assertEqual(A.data['status'], 'MERGED')
1435 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001436
James E. Blair2fa50962013-01-30 21:50:41 -08001437 def test_new_patchset_dequeues_old(self):
1438 "Test that a new patchset causes the old to be dequeued"
1439 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001440 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001441 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1442 M.setMerged()
1443
1444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1445 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1446 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1447 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001448 A.addApproval('code-review', 2)
1449 B.addApproval('code-review', 2)
1450 C.addApproval('code-review', 2)
1451 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001452
1453 C.setDependsOn(B, 1)
1454 B.setDependsOn(A, 1)
1455 A.setDependsOn(M, 1)
1456
James E. Blair8b5408c2016-08-08 15:37:46 -07001457 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1458 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1459 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1460 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001461 self.waitUntilSettled()
1462
1463 B.addPatchset()
1464 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1465 self.waitUntilSettled()
1466
Paul Belanger174a8272017-03-14 13:20:10 -04001467 self.executor_server.hold_jobs_in_build = False
1468 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001469 self.waitUntilSettled()
1470
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001471 self.assertEqual(A.data['status'], 'MERGED')
1472 self.assertEqual(A.reported, 2)
1473 self.assertEqual(B.data['status'], 'NEW')
1474 self.assertEqual(B.reported, 2)
1475 self.assertEqual(C.data['status'], 'NEW')
1476 self.assertEqual(C.reported, 2)
1477 self.assertEqual(D.data['status'], 'MERGED')
1478 self.assertEqual(D.reported, 2)
1479 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001480
James E. Blairba437362015-02-07 11:41:52 -08001481 def test_new_patchset_check(self):
1482 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001483
Paul Belanger174a8272017-03-14 13:20:10 -04001484 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001485
1486 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001487 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001488 tenant = self.sched.abide.tenants.get('tenant-one')
1489 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001490
1491 # Add two git-dependent changes
1492 B.setDependsOn(A, 1)
1493 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1494 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001495 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1496 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001497
1498 # A live item, and a non-live/live pair
1499 items = check_pipeline.getAllItems()
1500 self.assertEqual(len(items), 3)
1501
1502 self.assertEqual(items[0].change.number, '1')
1503 self.assertEqual(items[0].change.patchset, '1')
1504 self.assertFalse(items[0].live)
1505
1506 self.assertEqual(items[1].change.number, '2')
1507 self.assertEqual(items[1].change.patchset, '1')
1508 self.assertTrue(items[1].live)
1509
1510 self.assertEqual(items[2].change.number, '1')
1511 self.assertEqual(items[2].change.patchset, '1')
1512 self.assertTrue(items[2].live)
1513
1514 # Add a new patchset to A
1515 A.addPatchset()
1516 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1517 self.waitUntilSettled()
1518
1519 # The live copy of A,1 should be gone, but the non-live and B
1520 # should continue, and we should have a new A,2
1521 items = check_pipeline.getAllItems()
1522 self.assertEqual(len(items), 3)
1523
1524 self.assertEqual(items[0].change.number, '1')
1525 self.assertEqual(items[0].change.patchset, '1')
1526 self.assertFalse(items[0].live)
1527
1528 self.assertEqual(items[1].change.number, '2')
1529 self.assertEqual(items[1].change.patchset, '1')
1530 self.assertTrue(items[1].live)
1531
1532 self.assertEqual(items[2].change.number, '1')
1533 self.assertEqual(items[2].change.patchset, '2')
1534 self.assertTrue(items[2].live)
1535
1536 # Add a new patchset to B
1537 B.addPatchset()
1538 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1539 self.waitUntilSettled()
1540
1541 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1542 # but we should have a new B,2 (still based on A,1)
1543 items = check_pipeline.getAllItems()
1544 self.assertEqual(len(items), 3)
1545
1546 self.assertEqual(items[0].change.number, '1')
1547 self.assertEqual(items[0].change.patchset, '2')
1548 self.assertTrue(items[0].live)
1549
1550 self.assertEqual(items[1].change.number, '1')
1551 self.assertEqual(items[1].change.patchset, '1')
1552 self.assertFalse(items[1].live)
1553
1554 self.assertEqual(items[2].change.number, '2')
1555 self.assertEqual(items[2].change.patchset, '2')
1556 self.assertTrue(items[2].live)
1557
1558 self.builds[0].release()
1559 self.waitUntilSettled()
1560 self.builds[0].release()
1561 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001562 self.executor_server.hold_jobs_in_build = False
1563 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001564 self.waitUntilSettled()
1565
1566 self.assertEqual(A.reported, 1)
1567 self.assertEqual(B.reported, 1)
1568 self.assertEqual(self.history[0].result, 'ABORTED')
1569 self.assertEqual(self.history[0].changes, '1,1')
1570 self.assertEqual(self.history[1].result, 'ABORTED')
1571 self.assertEqual(self.history[1].changes, '1,1 2,1')
1572 self.assertEqual(self.history[2].result, 'SUCCESS')
1573 self.assertEqual(self.history[2].changes, '1,2')
1574 self.assertEqual(self.history[3].result, 'SUCCESS')
1575 self.assertEqual(self.history[3].changes, '1,1 2,2')
1576
1577 def test_abandoned_gate(self):
1578 "Test that an abandoned change is dequeued from gate"
1579
Paul Belanger174a8272017-03-14 13:20:10 -04001580 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001581
1582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001583 A.addApproval('code-review', 2)
1584 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001585 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001586 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1587 self.assertEqual(self.builds[0].name, 'project-merge')
1588
1589 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1590 self.waitUntilSettled()
1591
Paul Belanger174a8272017-03-14 13:20:10 -04001592 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001593 self.waitUntilSettled()
1594
Arie5b4048c2016-10-05 16:12:06 +03001595 self.assertBuilds([])
1596 self.assertHistory([
1597 dict(name='project-merge', result='ABORTED', changes='1,1')],
1598 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001599 self.assertEqual(A.reported, 1,
1600 "Abandoned gate change should report only start")
1601
1602 def test_abandoned_check(self):
1603 "Test that an abandoned change is dequeued from check"
1604
Paul Belanger174a8272017-03-14 13:20:10 -04001605 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001606
1607 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1608 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001609 tenant = self.sched.abide.tenants.get('tenant-one')
1610 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001611
1612 # Add two git-dependent changes
1613 B.setDependsOn(A, 1)
1614 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1615 self.waitUntilSettled()
1616 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1617 self.waitUntilSettled()
1618 # A live item, and a non-live/live pair
1619 items = check_pipeline.getAllItems()
1620 self.assertEqual(len(items), 3)
1621
1622 self.assertEqual(items[0].change.number, '1')
1623 self.assertFalse(items[0].live)
1624
1625 self.assertEqual(items[1].change.number, '2')
1626 self.assertTrue(items[1].live)
1627
1628 self.assertEqual(items[2].change.number, '1')
1629 self.assertTrue(items[2].live)
1630
1631 # Abandon A
1632 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1633 self.waitUntilSettled()
1634
1635 # The live copy of A should be gone, but the non-live and B
1636 # should continue
1637 items = check_pipeline.getAllItems()
1638 self.assertEqual(len(items), 2)
1639
1640 self.assertEqual(items[0].change.number, '1')
1641 self.assertFalse(items[0].live)
1642
1643 self.assertEqual(items[1].change.number, '2')
1644 self.assertTrue(items[1].live)
1645
Paul Belanger174a8272017-03-14 13:20:10 -04001646 self.executor_server.hold_jobs_in_build = False
1647 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001648 self.waitUntilSettled()
1649
1650 self.assertEqual(len(self.history), 4)
1651 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001652 'Build should have been aborted')
1653 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001654 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001655
Steve Varnau7b78b312015-04-03 14:49:46 -07001656 def test_abandoned_not_timer(self):
1657 "Test that an abandoned change does not cancel timer jobs"
1658
Paul Belanger174a8272017-03-14 13:20:10 -04001659 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001660
1661 # Start timer trigger - also org/project
Clint Byrumead6c562017-02-01 16:34:04 -08001662 self.updateConfigLayout('layout-idle')
Steve Varnau7b78b312015-04-03 14:49:46 -07001663 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001664 # The pipeline triggers every second, so we should have seen
1665 # several by now.
1666 time.sleep(5)
1667 self.waitUntilSettled()
1668 # Stop queuing timer triggered jobs so that the assertions
1669 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001670 # Must be in same repo, so overwrite config with another one
Clint Byrum58264dc2017-02-07 21:21:22 -08001671 self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clint Byrumead6c562017-02-01 16:34:04 -08001672
Steve Varnau7b78b312015-04-03 14:49:46 -07001673 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001674 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1675
1676 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1677 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1678 self.waitUntilSettled()
1679 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1680
1681 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1682 self.waitUntilSettled()
1683
1684 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1685
Paul Belanger174a8272017-03-14 13:20:10 -04001686 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001687 self.waitUntilSettled()
1688
Arx Cruzb1b010d2013-10-28 19:49:59 -02001689 def test_zuul_url_return(self):
1690 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001691 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001692 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001693
1694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001695 A.addApproval('code-review', 2)
1696 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001697 self.waitUntilSettled()
1698
1699 self.assertEqual(len(self.builds), 1)
1700 for build in self.builds:
1701 self.assertTrue('ZUUL_URL' in build.parameters)
1702
Paul Belanger174a8272017-03-14 13:20:10 -04001703 self.executor_server.hold_jobs_in_build = False
1704 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001705 self.waitUntilSettled()
1706
James E. Blair2fa50962013-01-30 21:50:41 -08001707 def test_new_patchset_dequeues_old_on_head(self):
1708 "Test that a new patchset causes the old to be dequeued (at head)"
1709 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001710 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001711 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1712 M.setMerged()
1713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1714 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1715 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1716 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001717 A.addApproval('code-review', 2)
1718 B.addApproval('code-review', 2)
1719 C.addApproval('code-review', 2)
1720 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001721
1722 C.setDependsOn(B, 1)
1723 B.setDependsOn(A, 1)
1724 A.setDependsOn(M, 1)
1725
James E. Blair8b5408c2016-08-08 15:37:46 -07001726 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1727 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1728 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1729 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001730 self.waitUntilSettled()
1731
1732 A.addPatchset()
1733 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1734 self.waitUntilSettled()
1735
Paul Belanger174a8272017-03-14 13:20:10 -04001736 self.executor_server.hold_jobs_in_build = False
1737 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001738 self.waitUntilSettled()
1739
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001740 self.assertEqual(A.data['status'], 'NEW')
1741 self.assertEqual(A.reported, 2)
1742 self.assertEqual(B.data['status'], 'NEW')
1743 self.assertEqual(B.reported, 2)
1744 self.assertEqual(C.data['status'], 'NEW')
1745 self.assertEqual(C.reported, 2)
1746 self.assertEqual(D.data['status'], 'MERGED')
1747 self.assertEqual(D.reported, 2)
1748 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001749
1750 def test_new_patchset_dequeues_old_without_dependents(self):
1751 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001752 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001753 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1754 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1755 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001756 A.addApproval('code-review', 2)
1757 B.addApproval('code-review', 2)
1758 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001759
James E. Blair8b5408c2016-08-08 15:37:46 -07001760 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1761 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1762 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001763 self.waitUntilSettled()
1764
1765 B.addPatchset()
1766 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1767 self.waitUntilSettled()
1768
Paul Belanger174a8272017-03-14 13:20:10 -04001769 self.executor_server.hold_jobs_in_build = False
1770 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001771 self.waitUntilSettled()
1772
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001773 self.assertEqual(A.data['status'], 'MERGED')
1774 self.assertEqual(A.reported, 2)
1775 self.assertEqual(B.data['status'], 'NEW')
1776 self.assertEqual(B.reported, 2)
1777 self.assertEqual(C.data['status'], 'MERGED')
1778 self.assertEqual(C.reported, 2)
1779 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001780
1781 def test_new_patchset_dequeues_old_independent_queue(self):
1782 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001783 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001784 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1785 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1786 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1788 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1789 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1790 self.waitUntilSettled()
1791
1792 B.addPatchset()
1793 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1794 self.waitUntilSettled()
1795
Paul Belanger174a8272017-03-14 13:20:10 -04001796 self.executor_server.hold_jobs_in_build = False
1797 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001798 self.waitUntilSettled()
1799
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001800 self.assertEqual(A.data['status'], 'NEW')
1801 self.assertEqual(A.reported, 1)
1802 self.assertEqual(B.data['status'], 'NEW')
1803 self.assertEqual(B.reported, 1)
1804 self.assertEqual(C.data['status'], 'NEW')
1805 self.assertEqual(C.reported, 1)
1806 self.assertEqual(len(self.history), 10)
1807 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001808
James E. Blair18c64442014-03-18 10:14:45 -07001809 def test_noop_job(self):
1810 "Test that the internal noop job works"
1811 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001812 A.addApproval('code-review', 2)
1813 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001814 self.waitUntilSettled()
1815
1816 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1817 self.assertTrue(self.sched._areAllBuildsComplete())
1818 self.assertEqual(len(self.history), 0)
1819 self.assertEqual(A.data['status'], 'MERGED')
1820 self.assertEqual(A.reported, 2)
1821
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001822 def test_no_job_project(self):
1823 "Test that reports with no jobs don't get sent"
1824 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1825 'master', 'A')
1826 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1827 self.waitUntilSettled()
1828
1829 # Change wasn't reported to
1830 self.assertEqual(A.reported, False)
1831
1832 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001833 tenant = self.sched.abide.tenants.get('tenant-one')
1834 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001835 items = check_pipeline.getAllItems()
1836 self.assertEqual(len(items), 0)
1837
1838 self.assertEqual(len(self.history), 0)
1839
James E. Blair7d0dedc2013-02-21 17:26:09 -08001840 def test_zuul_refs(self):
1841 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001842 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001843 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1844 M1.setMerged()
1845 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1846 M2.setMerged()
1847
1848 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1849 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1850 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1851 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001852 A.addApproval('code-review', 2)
1853 B.addApproval('code-review', 2)
1854 C.addApproval('code-review', 2)
1855 D.addApproval('code-review', 2)
1856 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1857 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1858 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1859 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001860
1861 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001862 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001863 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001864 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001865 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001866 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001867 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001868 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001869 self.waitUntilSettled()
1870
James E. Blair7d0dedc2013-02-21 17:26:09 -08001871 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001872 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001873 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001874 if x.parameters['ZUUL_CHANGE'] == '3':
1875 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001876 a_build = x
1877 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001878 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001879 b_build = x
1880 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001881 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001882 c_build = x
1883 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001884 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001885 d_build = x
1886 if a_build and b_build and c_build and d_build:
1887 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001888
1889 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001890 self.assertIsNotNone(a_zref)
1891 self.assertIsNotNone(b_zref)
1892 self.assertIsNotNone(c_zref)
1893 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001894
1895 # And they should all be different
1896 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001897 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001898
Clint Byrum3343e3e2016-11-15 16:05:03 -08001899 # should have a, not b, and should not be in project2
1900 self.assertTrue(a_build.hasChanges(A))
1901 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001902
Clint Byrum3343e3e2016-11-15 16:05:03 -08001903 # should have a and b, and should not be in project2
1904 self.assertTrue(b_build.hasChanges(A, B))
1905 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001906
Clint Byrum3343e3e2016-11-15 16:05:03 -08001907 # should have a and b in 1, c in 2
1908 self.assertTrue(c_build.hasChanges(A, B, C))
1909 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001910
Clint Byrum3343e3e2016-11-15 16:05:03 -08001911 # should have a and b in 1, c and d in 2
1912 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001913
Paul Belanger174a8272017-03-14 13:20:10 -04001914 self.executor_server.hold_jobs_in_build = False
1915 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08001916 self.waitUntilSettled()
1917
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001918 self.assertEqual(A.data['status'], 'MERGED')
1919 self.assertEqual(A.reported, 2)
1920 self.assertEqual(B.data['status'], 'MERGED')
1921 self.assertEqual(B.reported, 2)
1922 self.assertEqual(C.data['status'], 'MERGED')
1923 self.assertEqual(C.reported, 2)
1924 self.assertEqual(D.data['status'], 'MERGED')
1925 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08001926
James E. Blair4a28a882013-08-23 15:17:33 -07001927 def test_rerun_on_error(self):
1928 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04001929 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07001930 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001931 A.addApproval('code-review', 2)
1932 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07001933 self.waitUntilSettled()
1934
James E. Blair412fba82017-01-26 15:00:50 -08001935 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04001936 self.executor_server.hold_jobs_in_build = False
1937 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07001938 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08001939 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07001940 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
1941
James E. Blair412e5582013-04-22 15:50:12 -07001942 def test_statsd(self):
1943 "Test each of the statsd methods used in the scheduler"
1944 import extras
1945 statsd = extras.try_import('statsd.statsd')
1946 statsd.incr('test-incr')
1947 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07001948 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07001949 self.assertReportedStat('test-incr', '1|c')
1950 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07001951 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07001952
James E. Blairec056492016-07-22 09:45:56 -07001953 @skip("Disabled for early v3 development")
James E. Blairdad52252014-02-07 16:59:17 -08001954 def test_stuck_job_cleanup(self):
1955 "Test that pending jobs are cleaned up if removed from layout"
James E. Blair18c64442014-03-18 10:14:45 -07001956 # This job won't be registered at startup because it is not in
1957 # the standard layout, but we need it to already be registerd
1958 # for when we reconfigure, as that is when Zuul will attempt
1959 # to run the new job.
1960 self.worker.registerFunction('build:gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001961 self.gearman_server.hold_jobs_in_queue = True
1962 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001963 A.addApproval('code-review', 2)
1964 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08001965 self.waitUntilSettled()
1966 self.assertEqual(len(self.gearman_server.getQueue()), 1)
1967
James E. Blairf84026c2015-12-08 16:11:46 -08001968 self.updateConfigLayout(
1969 'tests/fixtures/layout-no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08001970 self.sched.reconfigure(self.config)
1971 self.waitUntilSettled()
1972
James E. Blair18c64442014-03-18 10:14:45 -07001973 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001974 self.waitUntilSettled()
1975 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1976 self.assertTrue(self.sched._areAllBuildsComplete())
1977
1978 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07001979 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08001980 self.assertEqual(self.history[0].result, 'SUCCESS')
1981
James E. Blair879dafb2015-07-17 14:04:49 -07001982 def test_file_head(self):
1983 # This is a regression test for an observed bug. A change
1984 # with a file named "HEAD" in the root directory of the repo
1985 # was processed by a merger. It then was unable to reset the
1986 # repo because of:
1987 # GitCommandError: 'git reset --hard HEAD' returned
1988 # with exit code 128
1989 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
1990 # and filename
1991 # Use '--' to separate filenames from revisions'
1992
1993 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04001994 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07001995 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1996
1997 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1998 self.waitUntilSettled()
1999
2000 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2001 self.waitUntilSettled()
2002
2003 self.assertIn('Build succeeded', A.messages[0])
2004 self.assertIn('Build succeeded', B.messages[0])
2005
James E. Blair70c71582013-03-06 08:50:50 -08002006 def test_file_jobs(self):
2007 "Test that file jobs run only when appropriate"
2008 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002009 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002010 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002011 A.addApproval('code-review', 2)
2012 B.addApproval('code-review', 2)
2013 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2014 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002015 self.waitUntilSettled()
2016
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002017 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002018 if x.name == 'project-testfile']
2019
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002020 self.assertEqual(len(testfile_jobs), 1)
2021 self.assertEqual(testfile_jobs[0].changes, '1,2')
2022 self.assertEqual(A.data['status'], 'MERGED')
2023 self.assertEqual(A.reported, 2)
2024 self.assertEqual(B.data['status'], 'MERGED')
2025 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002026
Clint Byrum3ee39f32016-11-17 23:45:07 -08002027 def _test_irrelevant_files_jobs(self, should_skip):
2028 "Test that jobs with irrelevant-files filter run only when appropriate"
2029 self.updateConfigLayout('layout-irrelevant-files')
Maru Newby3fe5f852015-01-13 04:22:14 +00002030 self.sched.reconfigure(self.config)
Clint Byrum3ee39f32016-11-17 23:45:07 -08002031
2032 if should_skip:
2033 files = {'ignoreme': 'ignored\n'}
2034 else:
2035 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002036
2037 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002038 'master',
2039 'test irrelevant-files',
2040 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002041 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2042 self.waitUntilSettled()
2043
2044 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002045 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002046
2047 if should_skip:
2048 self.assertEqual([], tested_change_ids)
2049 else:
2050 self.assertIn(change.data['number'], tested_change_ids)
2051
Clint Byrum3ee39f32016-11-17 23:45:07 -08002052 def test_irrelevant_files_match_skips_job(self):
2053 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002054
Clint Byrum3ee39f32016-11-17 23:45:07 -08002055 def test_irrelevant_files_no_match_runs_job(self):
2056 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002057
Clint Byrum85493602016-11-18 11:59:47 -08002058 def test_inherited_jobs_keep_matchers(self):
2059 self.updateConfigLayout('layout-inheritance')
2060 self.sched.reconfigure(self.config)
2061
2062 files = {'ignoreme': 'ignored\n'}
2063
2064 change = self.fake_gerrit.addFakeChange('org/project',
2065 'master',
2066 'test irrelevant-files',
2067 files=files)
2068 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2069 self.waitUntilSettled()
2070
2071 run_jobs = set([build.name for build in self.history])
2072
2073 self.assertEqual(set(['project-test-nomatch-starts-empty',
2074 'project-test-nomatch-starts-full']), run_jobs)
2075
James E. Blairec056492016-07-22 09:45:56 -07002076 @skip("Disabled for early v3 development")
James E. Blair3c5e5b52013-04-26 11:17:03 -07002077 def test_test_config(self):
2078 "Test that we can test the config"
James E. Blairf84026c2015-12-08 16:11:46 -08002079 self.sched.testConfig(self.config.get('zuul', 'tenant_config'),
Joshua Hesketh352264b2015-08-11 23:42:08 +10002080 self.connections)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07002081
James E. Blairc8a1e052014-02-25 09:29:26 -08002082 def test_queue_names(self):
2083 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002084 tenant = self.sched.abide.tenants.get('tenant-one')
2085 source = tenant.layout.pipelines['gate'].source
2086 project1 = source.getProject('org/project1')
2087 project2 = source.getProject('org/project2')
2088 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2089 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2090 self.assertEqual(q1.name, 'integrated')
2091 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002092
James E. Blair64ed6f22013-07-10 14:07:23 -07002093 def test_queue_precedence(self):
2094 "Test that queue precedence works"
2095
2096 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002097 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002098 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2099 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002100 A.addApproval('code-review', 2)
2101 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002102
2103 self.waitUntilSettled()
2104 self.gearman_server.hold_jobs_in_queue = False
2105 self.gearman_server.release()
2106 self.waitUntilSettled()
2107
James E. Blair8de58bd2013-07-18 16:23:33 -07002108 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002109 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002110 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002111 self.waitUntilSettled()
2112
James E. Blair64ed6f22013-07-10 14:07:23 -07002113 self.log.debug(self.history)
2114 self.assertEqual(self.history[0].pipeline, 'gate')
2115 self.assertEqual(self.history[1].pipeline, 'check')
2116 self.assertEqual(self.history[2].pipeline, 'gate')
2117 self.assertEqual(self.history[3].pipeline, 'gate')
2118 self.assertEqual(self.history[4].pipeline, 'check')
2119 self.assertEqual(self.history[5].pipeline, 'check')
2120
Clark Boylana5edbe42014-06-03 16:39:10 -07002121 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002122 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002123 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002124 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002125 A.addApproval('code-review', 2)
2126 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002127 self.waitUntilSettled()
2128
Paul Belanger174a8272017-03-14 13:20:10 -04002129 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002130 self.waitUntilSettled()
2131
James E. Blair1843a552013-07-03 14:19:52 -07002132 port = self.webapp.server.socket.getsockname()[1]
2133
Adam Gandelman77a12c72017-02-03 17:43:43 -08002134 req = urllib.request.Request(
2135 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002136 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002137 headers = f.info()
2138 self.assertIn('Content-Length', headers)
2139 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002140 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2141 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002142 self.assertIn('Access-Control-Allow-Origin', headers)
2143 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002144 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002145 self.assertIn('Expires', headers)
James E. Blair1843a552013-07-03 14:19:52 -07002146 data = f.read()
2147
Paul Belanger174a8272017-03-14 13:20:10 -04002148 self.executor_server.hold_jobs_in_build = False
2149 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002150 self.waitUntilSettled()
2151
2152 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002153 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002154 for p in data['pipelines']:
2155 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002156 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002157 self.assertEqual(q['window'], 20)
2158 else:
2159 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002160 for head in q['heads']:
2161 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002162 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002163 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002164 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002165 status_jobs.append(job)
2166 self.assertEqual('project-merge', status_jobs[0]['name'])
2167 self.assertEqual('https://server/job/project-merge/0/',
2168 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002169 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002170 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002171 self.assertEqual('project-test1', status_jobs[1]['name'])
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]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002174 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002175 status_jobs[1]['report_url'])
2176
2177 self.assertEqual('project-test2', status_jobs[2]['name'])
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]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002180 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002181 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002182
James E. Blairaf17a972016-02-03 15:07:18 -08002183 def test_mutex(self):
2184 "Test job mutexes"
Adam Gandelmanea9ac9b2017-02-03 16:59:58 -08002185 self.updateConfigLayout('layout-mutex')
James E. Blairaf17a972016-02-03 15:07:18 -08002186 self.sched.reconfigure(self.config)
2187
Paul Belanger174a8272017-03-14 13:20:10 -04002188 self.executor_server.hold_jobs_in_build = True
James E. Blairaf17a972016-02-03 15:07:18 -08002189 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2190 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2191 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2192
2193 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2194 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2195 self.waitUntilSettled()
2196 self.assertEqual(len(self.builds), 3)
2197 self.assertEqual(self.builds[0].name, 'project-test1')
2198 self.assertEqual(self.builds[1].name, 'mutex-one')
2199 self.assertEqual(self.builds[2].name, 'project-test1')
2200
Paul Belanger174a8272017-03-14 13:20:10 -04002201 self.executor_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002202 self.waitUntilSettled()
2203
2204 self.assertEqual(len(self.builds), 3)
2205 self.assertEqual(self.builds[0].name, 'project-test1')
2206 self.assertEqual(self.builds[1].name, 'project-test1')
2207 self.assertEqual(self.builds[2].name, 'mutex-two')
2208 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2209
Paul Belanger174a8272017-03-14 13:20:10 -04002210 self.executor_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002211 self.waitUntilSettled()
2212
2213 self.assertEqual(len(self.builds), 3)
2214 self.assertEqual(self.builds[0].name, 'project-test1')
2215 self.assertEqual(self.builds[1].name, 'project-test1')
2216 self.assertEqual(self.builds[2].name, 'mutex-one')
2217 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2218
Paul Belanger174a8272017-03-14 13:20:10 -04002219 self.executor_server.release('mutex-one')
James E. Blairaf17a972016-02-03 15:07:18 -08002220 self.waitUntilSettled()
2221
2222 self.assertEqual(len(self.builds), 3)
2223 self.assertEqual(self.builds[0].name, 'project-test1')
2224 self.assertEqual(self.builds[1].name, 'project-test1')
2225 self.assertEqual(self.builds[2].name, 'mutex-two')
2226 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2227
Paul Belanger174a8272017-03-14 13:20:10 -04002228 self.executor_server.release('mutex-two')
James E. Blairaf17a972016-02-03 15:07:18 -08002229 self.waitUntilSettled()
2230
2231 self.assertEqual(len(self.builds), 2)
2232 self.assertEqual(self.builds[0].name, 'project-test1')
2233 self.assertEqual(self.builds[1].name, 'project-test1')
2234 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2235
Paul Belanger174a8272017-03-14 13:20:10 -04002236 self.executor_server.hold_jobs_in_build = False
2237 self.executor_server.release()
James E. Blairaf17a972016-02-03 15:07:18 -08002238
2239 self.waitUntilSettled()
2240 self.assertEqual(len(self.builds), 0)
2241
2242 self.assertEqual(A.reported, 1)
2243 self.assertEqual(B.reported, 1)
2244 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2245
Tobias Henkelfb91a492017-02-15 07:29:43 +01002246 def test_mutex_abandon(self):
2247 "Test abandon with job mutexes"
2248 self.updateConfigLayout('layout-mutex')
2249 self.sched.reconfigure(self.config)
2250
Paul Belanger174a8272017-03-14 13:20:10 -04002251 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002252
2253 tenant = self.sched.abide.tenants.get('openstack')
2254 check_pipeline = tenant.layout.pipelines['check']
2255
2256 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2257 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2258
2259 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2260 self.waitUntilSettled()
2261
2262 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2263
2264 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
2265 self.waitUntilSettled()
2266
2267 # The check pipeline should be empty
2268 items = check_pipeline.getAllItems()
2269 self.assertEqual(len(items), 0)
2270
2271 # The mutex should be released
2272 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2273
Paul Belanger174a8272017-03-14 13:20:10 -04002274 self.executor_server.hold_jobs_in_build = False
2275 self.executor_server.release()
Tobias Henkelfb91a492017-02-15 07:29:43 +01002276 self.waitUntilSettled()
2277
2278 def test_mutex_reconfigure(self):
2279 "Test reconfigure with job mutexes"
2280 self.updateConfigLayout('layout-mutex')
2281 self.sched.reconfigure(self.config)
2282
Paul Belanger174a8272017-03-14 13:20:10 -04002283 self.executor_server.hold_jobs_in_build = True
Tobias Henkelfb91a492017-02-15 07:29:43 +01002284
2285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2286 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2287
2288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2289 self.waitUntilSettled()
2290
2291 self.assertTrue('test-mutex' in self.sched.mutex.mutexes)
2292
2293 self.updateConfigLayout('layout-mutex-reconfiguration')
2294 self.sched.reconfigure(self.config)
2295 self.waitUntilSettled()
2296
Paul Belanger174a8272017-03-14 13:20:10 -04002297 self.executor_server.release('project-test1')
Tobias Henkelfb91a492017-02-15 07:29:43 +01002298 self.waitUntilSettled()
2299
2300 # There should be no builds anymore
2301 self.assertEqual(len(self.builds), 0)
2302
2303 # The mutex should be released
2304 self.assertFalse('test-mutex' in self.sched.mutex.mutexes)
2305
James E. Blaircdccd972013-07-01 12:10:22 -07002306 def test_live_reconfiguration(self):
2307 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002308 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002309 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002310 A.addApproval('code-review', 2)
2311 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002312 self.waitUntilSettled()
2313
2314 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002315 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002316
Paul Belanger174a8272017-03-14 13:20:10 -04002317 self.executor_server.hold_jobs_in_build = False
2318 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002319 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002320 self.assertEqual(self.getJobFromHistory('project-merge').result,
2321 'SUCCESS')
2322 self.assertEqual(self.getJobFromHistory('project-test1').result,
2323 'SUCCESS')
2324 self.assertEqual(self.getJobFromHistory('project-test2').result,
2325 'SUCCESS')
2326 self.assertEqual(A.data['status'], 'MERGED')
2327 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002328
James E. Blairec056492016-07-22 09:45:56 -07002329 @skip("Disabled for early v3 development")
James E. Blair6bc782d2015-07-17 16:20:21 -07002330 def test_live_reconfiguration_merge_conflict(self):
2331 # A real-world bug: a change in a gate queue has a merge
2332 # conflict and a job is added to its project while it's
2333 # sitting in the queue. The job gets added to the change and
2334 # enqueued and the change gets stuck.
2335 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002336 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002337
2338 # This change is fine. It's here to stop the queue long
2339 # enough for the next change to be subject to the
2340 # reconfiguration, as well as to provide a conflict for the
2341 # next change. This change will succeed and merge.
2342 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2343 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002344 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002345
2346 # This change will be in merge conflict. During the
2347 # reconfiguration, we will add a job. We want to make sure
2348 # that doesn't cause it to get stuck.
2349 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2350 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002351 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002352
James E. Blair8b5408c2016-08-08 15:37:46 -07002353 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2354 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002355
2356 self.waitUntilSettled()
2357
2358 # No jobs have run yet
2359 self.assertEqual(A.data['status'], 'NEW')
2360 self.assertEqual(A.reported, 1)
2361 self.assertEqual(B.data['status'], 'NEW')
2362 self.assertEqual(B.reported, 1)
2363 self.assertEqual(len(self.history), 0)
2364
2365 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002366 self.updateConfigLayout(
2367 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002368 self.sched.reconfigure(self.config)
2369 self.waitUntilSettled()
2370
Paul Belanger174a8272017-03-14 13:20:10 -04002371 self.executor_server.hold_jobs_in_build = False
2372 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002373 self.waitUntilSettled()
2374
2375 self.assertEqual(A.data['status'], 'MERGED')
2376 self.assertEqual(A.reported, 2)
2377 self.assertEqual(B.data['status'], 'NEW')
2378 self.assertEqual(B.reported, 2)
2379 self.assertEqual(self.getJobFromHistory('project-merge').result,
2380 'SUCCESS')
2381 self.assertEqual(self.getJobFromHistory('project-test1').result,
2382 'SUCCESS')
2383 self.assertEqual(self.getJobFromHistory('project-test2').result,
2384 'SUCCESS')
2385 self.assertEqual(self.getJobFromHistory('project-test3').result,
2386 'SUCCESS')
2387 self.assertEqual(len(self.history), 4)
2388
James E. Blairec056492016-07-22 09:45:56 -07002389 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002390 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002391 # An extrapolation of test_live_reconfiguration_merge_conflict
2392 # that tests a job added to a job tree with a failed root does
2393 # not run.
2394 self.worker.registerFunction('build:project-test3')
Paul Belanger174a8272017-03-14 13:20:10 -04002395 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002396
2397 # This change is fine. It's here to stop the queue long
2398 # enough for the next change to be subject to the
2399 # reconfiguration. This change will succeed and merge.
2400 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2401 A.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002402 A.addApproval('code-review', 2)
2403 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002404 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002405 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002406 self.waitUntilSettled()
2407
2408 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002409 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002410 B.addApproval('code-review', 2)
2411 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002412 self.waitUntilSettled()
2413
Paul Belanger174a8272017-03-14 13:20:10 -04002414 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002415 self.waitUntilSettled()
2416
2417 # Both -merge jobs have run, but no others.
2418 self.assertEqual(A.data['status'], 'NEW')
2419 self.assertEqual(A.reported, 1)
2420 self.assertEqual(B.data['status'], 'NEW')
2421 self.assertEqual(B.reported, 1)
2422 self.assertEqual(self.history[0].result, 'SUCCESS')
2423 self.assertEqual(self.history[0].name, 'project-merge')
2424 self.assertEqual(self.history[1].result, 'FAILURE')
2425 self.assertEqual(self.history[1].name, 'project-merge')
2426 self.assertEqual(len(self.history), 2)
2427
2428 # Add the "project-test3" job.
James E. Blairf84026c2015-12-08 16:11:46 -08002429 self.updateConfigLayout(
2430 'tests/fixtures/layout-live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002431 self.sched.reconfigure(self.config)
2432 self.waitUntilSettled()
2433
Paul Belanger174a8272017-03-14 13:20:10 -04002434 self.executor_server.hold_jobs_in_build = False
2435 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002436 self.waitUntilSettled()
2437
2438 self.assertEqual(A.data['status'], 'MERGED')
2439 self.assertEqual(A.reported, 2)
2440 self.assertEqual(B.data['status'], 'NEW')
2441 self.assertEqual(B.reported, 2)
2442 self.assertEqual(self.history[0].result, 'SUCCESS')
2443 self.assertEqual(self.history[0].name, 'project-merge')
2444 self.assertEqual(self.history[1].result, 'FAILURE')
2445 self.assertEqual(self.history[1].name, 'project-merge')
2446 self.assertEqual(self.history[2].result, 'SUCCESS')
2447 self.assertEqual(self.history[3].result, 'SUCCESS')
2448 self.assertEqual(self.history[4].result, 'SUCCESS')
2449 self.assertEqual(len(self.history), 5)
2450
James E. Blairec056492016-07-22 09:45:56 -07002451 @skip("Disabled for early v3 development")
James E. Blair400e8fd2015-07-30 17:44:45 -07002452 def test_live_reconfiguration_failed_job(self):
2453 # Test that a change with a removed failing job does not
2454 # disrupt reconfiguration. If a change has a failed job and
2455 # that job is removed during a reconfiguration, we observed a
2456 # bug where the code to re-set build statuses would run on
2457 # that build and raise an exception because the job no longer
2458 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002459 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002460
2461 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2462
2463 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002464 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002465
2466 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2467 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002468 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002469 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002470 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002471 self.waitUntilSettled()
2472
2473 self.assertEqual(A.data['status'], 'NEW')
2474 self.assertEqual(A.reported, 0)
2475
2476 self.assertEqual(self.getJobFromHistory('project-merge').result,
2477 'SUCCESS')
2478 self.assertEqual(self.getJobFromHistory('project-test1').result,
2479 'FAILURE')
2480 self.assertEqual(len(self.history), 2)
2481
2482 # Remove the test1 job.
James E. Blairf84026c2015-12-08 16:11:46 -08002483 self.updateConfigLayout(
2484 'tests/fixtures/layout-live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002485 self.sched.reconfigure(self.config)
2486 self.waitUntilSettled()
2487
Paul Belanger174a8272017-03-14 13:20:10 -04002488 self.executor_server.hold_jobs_in_build = False
2489 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002490 self.waitUntilSettled()
2491
2492 self.assertEqual(self.getJobFromHistory('project-test2').result,
2493 'SUCCESS')
2494 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2495 'SUCCESS')
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('project-test1', A.messages[0])
2503
James E. Blairec056492016-07-22 09:45:56 -07002504 @skip("Disabled for early v3 development")
James E. Blairfe707d12015-08-05 15:18:15 -07002505 def test_live_reconfiguration_shared_queue(self):
2506 # Test that a change with a failing job which was removed from
2507 # this project but otherwise still exists in the system does
2508 # not disrupt reconfiguration.
2509
Paul Belanger174a8272017-03-14 13:20:10 -04002510 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002511
2512 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2513
Paul Belanger174a8272017-03-14 13:20:10 -04002514 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002515
2516 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2517 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002518 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002519 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002520 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002521 self.waitUntilSettled()
2522
2523 self.assertEqual(A.data['status'], 'NEW')
2524 self.assertEqual(A.reported, 0)
2525
2526 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2527 'SUCCESS')
2528 self.assertEqual(self.getJobFromHistory(
2529 'project1-project2-integration').result, 'FAILURE')
2530 self.assertEqual(len(self.history), 2)
2531
2532 # Remove the integration job.
James E. Blairf84026c2015-12-08 16:11:46 -08002533 self.updateConfigLayout(
2534 'tests/fixtures/layout-live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002535 self.sched.reconfigure(self.config)
2536 self.waitUntilSettled()
2537
Paul Belanger174a8272017-03-14 13:20:10 -04002538 self.executor_server.hold_jobs_in_build = False
2539 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002540 self.waitUntilSettled()
2541
2542 self.assertEqual(self.getJobFromHistory('project1-merge').result,
2543 'SUCCESS')
2544 self.assertEqual(self.getJobFromHistory('project1-test1').result,
2545 'SUCCESS')
2546 self.assertEqual(self.getJobFromHistory('project1-test2').result,
2547 'SUCCESS')
2548 self.assertEqual(self.getJobFromHistory(
2549 'project1-project2-integration').result, 'FAILURE')
2550 self.assertEqual(len(self.history), 4)
2551
2552 self.assertEqual(A.data['status'], 'NEW')
2553 self.assertEqual(A.reported, 1)
2554 self.assertIn('Build succeeded', A.messages[0])
2555 # Ensure the removed job was not included in the report.
2556 self.assertNotIn('project1-project2-integration', A.messages[0])
2557
James E. Blairec056492016-07-22 09:45:56 -07002558 @skip("Disabled for early v3 development")
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002559 def test_double_live_reconfiguration_shared_queue(self):
2560 # This was a real-world regression. A change is added to
2561 # gate; a reconfigure happens, a second change which depends
2562 # on the first is added, and a second reconfiguration happens.
2563 # Ensure that both changes merge.
2564
2565 # A failure may indicate incorrect caching or cleaning up of
2566 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002567 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002568
2569 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2570 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2571 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002572 A.addApproval('code-review', 2)
2573 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002574
2575 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002576 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002577 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002578 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002579 self.waitUntilSettled()
2580
2581 # Reconfigure (with only one change in the pipeline).
2582 self.sched.reconfigure(self.config)
2583 self.waitUntilSettled()
2584
2585 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002586 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002587 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002588 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002589 self.waitUntilSettled()
2590
2591 # Reconfigure (with both in the pipeline).
2592 self.sched.reconfigure(self.config)
2593 self.waitUntilSettled()
2594
Paul Belanger174a8272017-03-14 13:20:10 -04002595 self.executor_server.hold_jobs_in_build = False
2596 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002597 self.waitUntilSettled()
2598
2599 self.assertEqual(len(self.history), 8)
2600
2601 self.assertEqual(A.data['status'], 'MERGED')
2602 self.assertEqual(A.reported, 2)
2603 self.assertEqual(B.data['status'], 'MERGED')
2604 self.assertEqual(B.reported, 2)
2605
James E. Blairec056492016-07-22 09:45:56 -07002606 @skip("Disabled for early v3 development")
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002607 def test_live_reconfiguration_del_project(self):
2608 # Test project deletion from layout
2609 # while changes are enqueued
2610
Paul Belanger174a8272017-03-14 13:20:10 -04002611 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002612 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2613 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2614 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2615
2616 # A Depends-On: B
2617 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2618 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002619 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002620
2621 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2622 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2623 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002624 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002625 self.waitUntilSettled()
2626 self.assertEqual(len(self.builds), 5)
2627
2628 # This layout defines only org/project, not org/project1
James E. Blairf84026c2015-12-08 16:11:46 -08002629 self.updateConfigLayout(
2630 'tests/fixtures/layout-live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002631 self.sched.reconfigure(self.config)
2632 self.waitUntilSettled()
2633
2634 # Builds for C aborted, builds for A succeed,
2635 # and have change B applied ahead
2636 job_c = self.getJobFromHistory('project1-test1')
2637 self.assertEqual(job_c.changes, '3,1')
2638 self.assertEqual(job_c.result, 'ABORTED')
2639
Paul Belanger174a8272017-03-14 13:20:10 -04002640 self.executor_server.hold_jobs_in_build = False
2641 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002642 self.waitUntilSettled()
2643
2644 self.assertEqual(self.getJobFromHistory('project-test1').changes,
2645 '2,1 1,1')
2646
2647 self.assertEqual(A.data['status'], 'NEW')
2648 self.assertEqual(B.data['status'], 'NEW')
2649 self.assertEqual(C.data['status'], 'NEW')
2650 self.assertEqual(A.reported, 1)
2651 self.assertEqual(B.reported, 0)
2652 self.assertEqual(C.reported, 0)
2653
2654 self.assertEqual(len(self.sched.layout.pipelines['check'].queues), 0)
2655 self.assertIn('Build succeeded', A.messages[0])
2656
James E. Blairec056492016-07-22 09:45:56 -07002657 @skip("Disabled for early v3 development")
James E. Blaire712d9f2013-07-31 11:40:11 -07002658 def test_live_reconfiguration_functions(self):
2659 "Test live reconfiguration with a custom function"
2660 self.worker.registerFunction('build:node-project-test1:debian')
2661 self.worker.registerFunction('build:node-project-test1:wheezy')
2662 A = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002663 A.addApproval('code-review', 2)
2664 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002665 self.waitUntilSettled()
2666
2667 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2668 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2669 'debian')
2670 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2671
James E. Blairf84026c2015-12-08 16:11:46 -08002672 self.updateConfigLayout(
2673 'tests/fixtures/layout-live-reconfiguration-functions.yaml')
James E. Blaire712d9f2013-07-31 11:40:11 -07002674 self.sched.reconfigure(self.config)
2675 self.worker.build_history = []
2676
2677 B = self.fake_gerrit.addFakeChange('org/node-project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002678 B.addApproval('code-review', 2)
2679 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blaire712d9f2013-07-31 11:40:11 -07002680 self.waitUntilSettled()
2681
2682 self.assertIsNone(self.getJobFromHistory('node-project-merge').node)
2683 self.assertEqual(self.getJobFromHistory('node-project-test1').node,
2684 'wheezy')
2685 self.assertIsNone(self.getJobFromHistory('node-project-test2').node)
2686
James E. Blairec056492016-07-22 09:45:56 -07002687 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002688 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002689 self.updateConfigLayout(
2690 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002691 self.sched.reconfigure(self.config)
2692
2693 self.init_repo("org/new-project")
2694 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2695
James E. Blair8b5408c2016-08-08 15:37:46 -07002696 A.addApproval('code-review', 2)
2697 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002698 self.waitUntilSettled()
2699 self.assertEqual(self.getJobFromHistory('project-merge').result,
2700 'SUCCESS')
2701 self.assertEqual(self.getJobFromHistory('project-test1').result,
2702 'SUCCESS')
2703 self.assertEqual(self.getJobFromHistory('project-test2').result,
2704 'SUCCESS')
2705 self.assertEqual(A.data['status'], 'MERGED')
2706 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002707
Clark Boylan6dbbc482013-10-18 10:57:31 -07002708 def test_repo_deleted(self):
Paul Belanger66e95962016-11-11 12:11:06 -05002709 self.updateConfigLayout('layout-repo-deleted')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002710 self.sched.reconfigure(self.config)
2711
2712 self.init_repo("org/delete-project")
2713 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2714
James E. Blair8b5408c2016-08-08 15:37:46 -07002715 A.addApproval('code-review', 2)
2716 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002717 self.waitUntilSettled()
2718 self.assertEqual(self.getJobFromHistory('project-merge').result,
2719 'SUCCESS')
2720 self.assertEqual(self.getJobFromHistory('project-test1').result,
2721 'SUCCESS')
2722 self.assertEqual(self.getJobFromHistory('project-test2').result,
2723 'SUCCESS')
2724 self.assertEqual(A.data['status'], 'MERGED')
2725 self.assertEqual(A.reported, 2)
2726
2727 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002728 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002729 if os.path.exists(os.path.join(self.merger_src_root, p)):
2730 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002731 if os.path.exists(os.path.join(self.executor_src_root, p)):
2732 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002733
2734 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2735
James E. Blair8b5408c2016-08-08 15:37:46 -07002736 B.addApproval('code-review', 2)
2737 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002738 self.waitUntilSettled()
2739 self.assertEqual(self.getJobFromHistory('project-merge').result,
2740 'SUCCESS')
2741 self.assertEqual(self.getJobFromHistory('project-test1').result,
2742 'SUCCESS')
2743 self.assertEqual(self.getJobFromHistory('project-test2').result,
2744 'SUCCESS')
2745 self.assertEqual(B.data['status'], 'MERGED')
2746 self.assertEqual(B.reported, 2)
2747
James E. Blair456f2fb2016-02-09 09:29:33 -08002748 def test_tags(self):
2749 "Test job tags"
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002750 self.updateConfigLayout('layout-tags')
James E. Blair456f2fb2016-02-09 09:29:33 -08002751 self.sched.reconfigure(self.config)
2752
2753 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2754 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2755 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2756 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2757 self.waitUntilSettled()
2758
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002759 self.assertEqual(len(self.history), 8)
2760
2761 results = {self.getJobFromHistory('merge',
2762 project='org/project1').uuid: 'extratag merge',
2763 self.getJobFromHistory('merge',
2764 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002765
2766 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002767 self.assertEqual(results.get(build.uuid, ''),
2768 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002769
James E. Blair63bb0ef2013-07-29 17:14:51 -07002770 def test_timer(self):
2771 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002772 self.executor_server.hold_jobs_in_build = True
Clint Byrum5e729e92017-02-08 06:34:57 -08002773 self.updateConfigLayout('layout-timer')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002774 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002775
Clark Boylan3ee090a2014-04-03 20:55:09 -07002776 # The pipeline triggers every second, so we should have seen
2777 # several by now.
2778 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002779 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002780
2781 self.assertEqual(len(self.builds), 2)
2782
James E. Blair63bb0ef2013-07-29 17:14:51 -07002783 port = self.webapp.server.socket.getsockname()[1]
2784
Clint Byrum5e729e92017-02-08 06:34:57 -08002785 req = urllib.request.Request(
2786 "http://localhost:%s/openstack/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002787 f = urllib.request.urlopen(req)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002788 data = f.read()
2789
Paul Belanger174a8272017-03-14 13:20:10 -04002790 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002791 # Stop queuing timer triggered jobs so that the assertions
2792 # below don't race against more jobs being queued.
Clint Byrum5e729e92017-02-08 06:34:57 -08002793 self.commitLayoutUpdate('layout-timer', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002794 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002795 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002796 self.waitUntilSettled()
2797
2798 self.assertEqual(self.getJobFromHistory(
2799 'project-bitrot-stable-old').result, 'SUCCESS')
2800 self.assertEqual(self.getJobFromHistory(
2801 'project-bitrot-stable-older').result, 'SUCCESS')
2802
2803 data = json.loads(data)
2804 status_jobs = set()
2805 for p in data['pipelines']:
2806 for q in p['change_queues']:
2807 for head in q['heads']:
2808 for change in head:
Alex Gaynorddb9ef32013-09-16 21:04:58 -07002809 self.assertEqual(change['id'], None)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002810 for job in change['jobs']:
2811 status_jobs.add(job['name'])
2812 self.assertIn('project-bitrot-stable-old', status_jobs)
2813 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002814
James E. Blair4f6033c2014-03-27 15:49:09 -07002815 def test_idle(self):
2816 "Test that frequent periodic jobs work"
Paul Belanger174a8272017-03-14 13:20:10 -04002817 self.executor_server.hold_jobs_in_build = True
Clint Byrum58264dc2017-02-07 21:21:22 -08002818 self.updateConfigLayout('layout-idle')
James E. Blair4f6033c2014-03-27 15:49:09 -07002819
Clark Boylan3ee090a2014-04-03 20:55:09 -07002820 for x in range(1, 3):
2821 # Test that timer triggers periodic jobs even across
2822 # layout config reloads.
2823 # Start timer trigger
Clark Boylan3ee090a2014-04-03 20:55:09 -07002824 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002825 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002826
Clark Boylan3ee090a2014-04-03 20:55:09 -07002827 # The pipeline triggers every second, so we should have seen
2828 # several by now.
2829 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002830
2831 # Stop queuing timer triggered jobs so that the assertions
2832 # below don't race against more jobs being queued.
Clint Byrum58264dc2017-02-07 21:21:22 -08002833 before = self.commitLayoutUpdate('layout-idle', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002834 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002835 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002836 self.assertEqual(len(self.builds), 2,
2837 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002838 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002839 self.waitUntilSettled()
2840 self.assertEqual(len(self.builds), 0)
2841 self.assertEqual(len(self.history), x * 2)
Clint Byrum58264dc2017-02-07 21:21:22 -08002842 # Revert back to layout-idle
2843 repo = git.Repo(os.path.join(self.test_root,
2844 'upstream',
2845 'layout-idle'))
2846 repo.git.reset('--hard', before)
James E. Blair4f6033c2014-03-27 15:49:09 -07002847
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002848 def test_check_smtp_pool(self):
Paul Belanger01be8ca2016-11-11 12:21:06 -05002849 self.updateConfigLayout('layout-smtp')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002850 self.sched.reconfigure(self.config)
2851
2852 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2853 self.waitUntilSettled()
2854
2855 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2856 self.waitUntilSettled()
2857
James E. Blairff80a2f2013-12-27 13:24:06 -08002858 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002859
2860 # A.messages only holds what FakeGerrit places in it. Thus we
2861 # work on the knowledge of what the first message should be as
2862 # it is only configured to go to SMTP.
2863
2864 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002865 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002866 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002867 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002868 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002869 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002870
2871 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002872 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002873 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002874 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002875 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002876 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002877
James E. Blaire5910202013-12-27 09:50:31 -08002878 def test_timer_smtp(self):
2879 "Test that a periodic job is triggered"
Paul Belanger174a8272017-03-14 13:20:10 -04002880 self.executor_server.hold_jobs_in_build = True
Clint Byrum7b71b2f2017-02-08 07:00:52 -08002881 self.updateConfigLayout('layout-timer-smtp')
James E. Blaire5910202013-12-27 09:50:31 -08002882 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002883
Clark Boylan3ee090a2014-04-03 20:55:09 -07002884 # The pipeline triggers every second, so we should have seen
2885 # several by now.
2886 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002887 self.waitUntilSettled()
2888
Clark Boylan3ee090a2014-04-03 20:55:09 -07002889 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002890 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002891 self.waitUntilSettled()
2892 self.assertEqual(len(self.history), 2)
2893
James E. Blaire5910202013-12-27 09:50:31 -08002894 self.assertEqual(self.getJobFromHistory(
2895 'project-bitrot-stable-old').result, 'SUCCESS')
2896 self.assertEqual(self.getJobFromHistory(
2897 'project-bitrot-stable-older').result, 'SUCCESS')
2898
James E. Blairff80a2f2013-12-27 13:24:06 -08002899 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002900
2901 # A.messages only holds what FakeGerrit places in it. Thus we
2902 # work on the knowledge of what the first message should be as
2903 # it is only configured to go to SMTP.
2904
2905 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002906 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002907 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002908 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002909 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002910 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002911
Clark Boylan3ee090a2014-04-03 20:55:09 -07002912 # Stop queuing timer triggered jobs and let any that may have
2913 # queued through so that end of test assertions pass.
Clint Byrum7b71b2f2017-02-08 07:00:52 -08002914 self.commitLayoutUpdate('layout-timer-smtp', 'layout-no-timer')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002915 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002916 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002917 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002918 self.waitUntilSettled()
2919
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002920 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002921 def test_timer_sshkey(self):
2922 "Test that a periodic job can setup SSH key authentication"
2923 self.worker.hold_jobs_in_build = True
2924 self.config.set('zuul', 'layout_config',
2925 'tests/fixtures/layout-timer.yaml')
2926 self.sched.reconfigure(self.config)
2927 self.registerJobs()
2928
2929 # The pipeline triggers every second, so we should have seen
2930 # several by now.
2931 time.sleep(5)
2932 self.waitUntilSettled()
2933
2934 self.assertEqual(len(self.builds), 2)
2935
2936 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2937 self.assertTrue(os.path.isfile(ssh_wrapper))
2938 with open(ssh_wrapper) as f:
2939 ssh_wrapper_content = f.read()
2940 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2941 # In the unit tests Merger runs in the same process,
2942 # so we see its' environment variables
2943 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2944
2945 self.worker.release('.*')
2946 self.waitUntilSettled()
2947 self.assertEqual(len(self.history), 2)
2948
2949 self.assertEqual(self.getJobFromHistory(
2950 'project-bitrot-stable-old').result, 'SUCCESS')
2951 self.assertEqual(self.getJobFromHistory(
2952 'project-bitrot-stable-older').result, 'SUCCESS')
2953
2954 # Stop queuing timer triggered jobs and let any that may have
2955 # queued through so that end of test assertions pass.
2956 self.config.set('zuul', 'layout_config',
2957 'tests/fixtures/layout-no-timer.yaml')
2958 self.sched.reconfigure(self.config)
2959 self.registerJobs()
2960 self.waitUntilSettled()
2961 self.worker.release('.*')
2962 self.waitUntilSettled()
2963
James E. Blair91e34592015-07-31 16:45:59 -07002964 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002965 "Test that the RPC client can enqueue a change"
2966 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002967 A.addApproval('code-review', 2)
2968 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002969
2970 client = zuul.rpcclient.RPCClient('127.0.0.1',
2971 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04002972 r = client.enqueue(tenant='tenant-one',
2973 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002974 project='org/project',
2975 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002976 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002977 self.waitUntilSettled()
2978 self.assertEqual(self.getJobFromHistory('project-merge').result,
2979 'SUCCESS')
2980 self.assertEqual(self.getJobFromHistory('project-test1').result,
2981 'SUCCESS')
2982 self.assertEqual(self.getJobFromHistory('project-test2').result,
2983 'SUCCESS')
2984 self.assertEqual(A.data['status'], 'MERGED')
2985 self.assertEqual(A.reported, 2)
2986 self.assertEqual(r, True)
2987
James E. Blair91e34592015-07-31 16:45:59 -07002988 def test_client_enqueue_ref(self):
2989 "Test that the RPC client can enqueue a ref"
2990
2991 client = zuul.rpcclient.RPCClient('127.0.0.1',
2992 self.gearman_server.port)
2993 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002994 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002995 pipeline='post',
2996 project='org/project',
2997 trigger='gerrit',
2998 ref='master',
2999 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3000 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3001 self.waitUntilSettled()
3002 job_names = [x.name for x in self.history]
3003 self.assertEqual(len(self.history), 1)
3004 self.assertIn('project-post', job_names)
3005 self.assertEqual(r, True)
3006
James E. Blairad28e912013-11-27 10:43:22 -08003007 def test_client_enqueue_negative(self):
3008 "Test that the RPC client returns errors"
3009 client = zuul.rpcclient.RPCClient('127.0.0.1',
3010 self.gearman_server.port)
3011 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003012 "Invalid tenant"):
3013 r = client.enqueue(tenant='tenant-foo',
3014 pipeline='gate',
3015 project='org/project',
3016 trigger='gerrit',
3017 change='1,1')
3018 client.shutdown()
3019 self.assertEqual(r, False)
3020
3021 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003022 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003023 r = client.enqueue(tenant='tenant-one',
3024 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003025 project='project-does-not-exist',
3026 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003027 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003028 client.shutdown()
3029 self.assertEqual(r, False)
3030
3031 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3032 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003033 r = client.enqueue(tenant='tenant-one',
3034 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003035 project='org/project',
3036 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003037 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003038 client.shutdown()
3039 self.assertEqual(r, False)
3040
3041 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3042 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003043 r = client.enqueue(tenant='tenant-one',
3044 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003045 project='org/project',
3046 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003047 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003048 client.shutdown()
3049 self.assertEqual(r, False)
3050
3051 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3052 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003053 r = client.enqueue(tenant='tenant-one',
3054 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003055 project='org/project',
3056 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003057 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003058 client.shutdown()
3059 self.assertEqual(r, False)
3060
3061 self.waitUntilSettled()
3062 self.assertEqual(len(self.history), 0)
3063 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003064
3065 def test_client_promote(self):
3066 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003067 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003068 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3069 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3070 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003071 A.addApproval('code-review', 2)
3072 B.addApproval('code-review', 2)
3073 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003074
James E. Blair8b5408c2016-08-08 15:37:46 -07003075 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3076 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3077 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003078
3079 self.waitUntilSettled()
3080
Paul Belangerbaca3132016-11-04 12:49:54 -04003081 tenant = self.sched.abide.tenants.get('tenant-one')
3082 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003083 enqueue_times = {}
3084 for item in items:
3085 enqueue_times[str(item.change)] = item.enqueue_time
3086
James E. Blair36658cf2013-12-06 17:53:48 -08003087 client = zuul.rpcclient.RPCClient('127.0.0.1',
3088 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003089 r = client.promote(tenant='tenant-one',
3090 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003091 change_ids=['2,1', '3,1'])
3092
Sean Daguef39b9ca2014-01-10 21:34:35 -05003093 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003094 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003095 for item in items:
3096 self.assertEqual(
3097 enqueue_times[str(item.change)], item.enqueue_time)
3098
James E. Blair78acec92014-02-06 07:11:32 -08003099 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003100 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003101 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003102 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003103 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003104 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003105 self.waitUntilSettled()
3106
3107 self.assertEqual(len(self.builds), 6)
3108 self.assertEqual(self.builds[0].name, 'project-test1')
3109 self.assertEqual(self.builds[1].name, 'project-test2')
3110 self.assertEqual(self.builds[2].name, 'project-test1')
3111 self.assertEqual(self.builds[3].name, 'project-test2')
3112 self.assertEqual(self.builds[4].name, 'project-test1')
3113 self.assertEqual(self.builds[5].name, 'project-test2')
3114
Paul Belangerbaca3132016-11-04 12:49:54 -04003115 self.assertTrue(self.builds[0].hasChanges(B))
3116 self.assertFalse(self.builds[0].hasChanges(A))
3117 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003118
Paul Belangerbaca3132016-11-04 12:49:54 -04003119 self.assertTrue(self.builds[2].hasChanges(B))
3120 self.assertTrue(self.builds[2].hasChanges(C))
3121 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003122
Paul Belangerbaca3132016-11-04 12:49:54 -04003123 self.assertTrue(self.builds[4].hasChanges(B))
3124 self.assertTrue(self.builds[4].hasChanges(C))
3125 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003126
Paul Belanger174a8272017-03-14 13:20:10 -04003127 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003128 self.waitUntilSettled()
3129
3130 self.assertEqual(A.data['status'], 'MERGED')
3131 self.assertEqual(A.reported, 2)
3132 self.assertEqual(B.data['status'], 'MERGED')
3133 self.assertEqual(B.reported, 2)
3134 self.assertEqual(C.data['status'], 'MERGED')
3135 self.assertEqual(C.reported, 2)
3136
3137 client.shutdown()
3138 self.assertEqual(r, True)
3139
3140 def test_client_promote_dependent(self):
3141 "Test that the RPC client can promote a dependent change"
3142 # C (depends on B) -> B -> A ; then promote C to get:
3143 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003144 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003145 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3146 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3147 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3148
3149 C.setDependsOn(B, 1)
3150
James E. Blair8b5408c2016-08-08 15:37:46 -07003151 A.addApproval('code-review', 2)
3152 B.addApproval('code-review', 2)
3153 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003154
James E. Blair8b5408c2016-08-08 15:37:46 -07003155 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3156 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3157 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003158
3159 self.waitUntilSettled()
3160
3161 client = zuul.rpcclient.RPCClient('127.0.0.1',
3162 self.gearman_server.port)
Paul Belangerbaca3132016-11-04 12:49:54 -04003163 r = client.promote(tenant='tenant-one',
3164 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003165 change_ids=['3,1'])
3166
James E. Blair78acec92014-02-06 07:11:32 -08003167 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003168 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003169 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003170 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003171 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003172 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003173 self.waitUntilSettled()
3174
3175 self.assertEqual(len(self.builds), 6)
3176 self.assertEqual(self.builds[0].name, 'project-test1')
3177 self.assertEqual(self.builds[1].name, 'project-test2')
3178 self.assertEqual(self.builds[2].name, 'project-test1')
3179 self.assertEqual(self.builds[3].name, 'project-test2')
3180 self.assertEqual(self.builds[4].name, 'project-test1')
3181 self.assertEqual(self.builds[5].name, 'project-test2')
3182
Paul Belangerbaca3132016-11-04 12:49:54 -04003183 self.assertTrue(self.builds[0].hasChanges(B))
3184 self.assertFalse(self.builds[0].hasChanges(A))
3185 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003186
Paul Belangerbaca3132016-11-04 12:49:54 -04003187 self.assertTrue(self.builds[2].hasChanges(B))
3188 self.assertTrue(self.builds[2].hasChanges(C))
3189 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003190
Paul Belangerbaca3132016-11-04 12:49:54 -04003191 self.assertTrue(self.builds[4].hasChanges(B))
3192 self.assertTrue(self.builds[4].hasChanges(C))
3193 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003194
Paul Belanger174a8272017-03-14 13:20:10 -04003195 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003196 self.waitUntilSettled()
3197
3198 self.assertEqual(A.data['status'], 'MERGED')
3199 self.assertEqual(A.reported, 2)
3200 self.assertEqual(B.data['status'], 'MERGED')
3201 self.assertEqual(B.reported, 2)
3202 self.assertEqual(C.data['status'], 'MERGED')
3203 self.assertEqual(C.reported, 2)
3204
3205 client.shutdown()
3206 self.assertEqual(r, True)
3207
3208 def test_client_promote_negative(self):
3209 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003210 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003211 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003212 A.addApproval('code-review', 2)
3213 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003214 self.waitUntilSettled()
3215
3216 client = zuul.rpcclient.RPCClient('127.0.0.1',
3217 self.gearman_server.port)
3218
3219 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003220 r = client.promote(tenant='tenant-one',
3221 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003222 change_ids=['2,1', '3,1'])
3223 client.shutdown()
3224 self.assertEqual(r, False)
3225
3226 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003227 r = client.promote(tenant='tenant-one',
3228 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003229 change_ids=['4,1'])
3230 client.shutdown()
3231 self.assertEqual(r, False)
3232
Paul Belanger174a8272017-03-14 13:20:10 -04003233 self.executor_server.hold_jobs_in_build = False
3234 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003235 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003236
James E. Blairec056492016-07-22 09:45:56 -07003237 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003238 def test_queue_rate_limiting(self):
3239 "Test that DependentPipelines are rate limited with dep across window"
James E. Blairf84026c2015-12-08 16:11:46 -08003240 self.updateConfigLayout(
3241 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003242 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003243 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003244 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3245 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3246 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3247
3248 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003249 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003250
James E. Blair8b5408c2016-08-08 15:37:46 -07003251 A.addApproval('code-review', 2)
3252 B.addApproval('code-review', 2)
3253 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003254
James E. Blair8b5408c2016-08-08 15:37:46 -07003255 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3256 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3257 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003258 self.waitUntilSettled()
3259
3260 # Only A and B will have their merge jobs queued because
3261 # window is 2.
3262 self.assertEqual(len(self.builds), 2)
3263 self.assertEqual(self.builds[0].name, 'project-merge')
3264 self.assertEqual(self.builds[1].name, 'project-merge')
3265
Paul Belanger174a8272017-03-14 13:20:10 -04003266 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003267 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003268 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003269 self.waitUntilSettled()
3270
3271 # Only A and B will have their test jobs queued because
3272 # window is 2.
3273 self.assertEqual(len(self.builds), 4)
3274 self.assertEqual(self.builds[0].name, 'project-test1')
3275 self.assertEqual(self.builds[1].name, 'project-test2')
3276 self.assertEqual(self.builds[2].name, 'project-test1')
3277 self.assertEqual(self.builds[3].name, 'project-test2')
3278
Paul Belanger174a8272017-03-14 13:20:10 -04003279 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003280 self.waitUntilSettled()
3281
3282 queue = self.sched.layout.pipelines['gate'].queues[0]
3283 # A failed so window is reduced by 1 to 1.
3284 self.assertEqual(queue.window, 1)
3285 self.assertEqual(queue.window_floor, 1)
3286 self.assertEqual(A.data['status'], 'NEW')
3287
3288 # Gate is reset and only B's merge job is queued because
3289 # window shrunk to 1.
3290 self.assertEqual(len(self.builds), 1)
3291 self.assertEqual(self.builds[0].name, 'project-merge')
3292
Paul Belanger174a8272017-03-14 13:20:10 -04003293 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003294 self.waitUntilSettled()
3295
3296 # Only B's test jobs are queued because window is still 1.
3297 self.assertEqual(len(self.builds), 2)
3298 self.assertEqual(self.builds[0].name, 'project-test1')
3299 self.assertEqual(self.builds[1].name, 'project-test2')
3300
Paul Belanger174a8272017-03-14 13:20:10 -04003301 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003302 self.waitUntilSettled()
3303
3304 # B was successfully merged so window is increased to 2.
3305 self.assertEqual(queue.window, 2)
3306 self.assertEqual(queue.window_floor, 1)
3307 self.assertEqual(B.data['status'], 'MERGED')
3308
3309 # Only C is left and its merge job is queued.
3310 self.assertEqual(len(self.builds), 1)
3311 self.assertEqual(self.builds[0].name, 'project-merge')
3312
Paul Belanger174a8272017-03-14 13:20:10 -04003313 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003314 self.waitUntilSettled()
3315
3316 # After successful merge job the test jobs for C are queued.
3317 self.assertEqual(len(self.builds), 2)
3318 self.assertEqual(self.builds[0].name, 'project-test1')
3319 self.assertEqual(self.builds[1].name, 'project-test2')
3320
Paul Belanger174a8272017-03-14 13:20:10 -04003321 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003322 self.waitUntilSettled()
3323
3324 # C successfully merged so window is bumped to 3.
3325 self.assertEqual(queue.window, 3)
3326 self.assertEqual(queue.window_floor, 1)
3327 self.assertEqual(C.data['status'], 'MERGED')
3328
James E. Blairec056492016-07-22 09:45:56 -07003329 @skip("Disabled for early v3 development")
Clark Boylan7603a372014-01-21 11:43:20 -08003330 def test_queue_rate_limiting_dependent(self):
3331 "Test that DependentPipelines are rate limited with dep in window"
James E. Blairf84026c2015-12-08 16:11:46 -08003332 self.updateConfigLayout(
3333 'tests/fixtures/layout-rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003334 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04003335 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003336 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3337 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3338 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3339
3340 B.setDependsOn(A, 1)
3341
Paul Belanger174a8272017-03-14 13:20:10 -04003342 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003343
James E. Blair8b5408c2016-08-08 15:37:46 -07003344 A.addApproval('code-review', 2)
3345 B.addApproval('code-review', 2)
3346 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003347
James E. Blair8b5408c2016-08-08 15:37:46 -07003348 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3349 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3350 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003351 self.waitUntilSettled()
3352
3353 # Only A and B will have their merge jobs queued because
3354 # window is 2.
3355 self.assertEqual(len(self.builds), 2)
3356 self.assertEqual(self.builds[0].name, 'project-merge')
3357 self.assertEqual(self.builds[1].name, 'project-merge')
3358
Paul Belanger174a8272017-03-14 13:20:10 -04003359 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003360 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003361 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003362 self.waitUntilSettled()
3363
3364 # Only A and B will have their test jobs queued because
3365 # window is 2.
3366 self.assertEqual(len(self.builds), 4)
3367 self.assertEqual(self.builds[0].name, 'project-test1')
3368 self.assertEqual(self.builds[1].name, 'project-test2')
3369 self.assertEqual(self.builds[2].name, 'project-test1')
3370 self.assertEqual(self.builds[3].name, 'project-test2')
3371
Paul Belanger174a8272017-03-14 13:20:10 -04003372 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003373 self.waitUntilSettled()
3374
3375 queue = self.sched.layout.pipelines['gate'].queues[0]
3376 # A failed so window is reduced by 1 to 1.
3377 self.assertEqual(queue.window, 1)
3378 self.assertEqual(queue.window_floor, 1)
3379 self.assertEqual(A.data['status'], 'NEW')
3380 self.assertEqual(B.data['status'], 'NEW')
3381
3382 # Gate is reset and only C's merge job is queued because
3383 # window shrunk to 1 and A and B were dequeued.
3384 self.assertEqual(len(self.builds), 1)
3385 self.assertEqual(self.builds[0].name, 'project-merge')
3386
Paul Belanger174a8272017-03-14 13:20:10 -04003387 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003388 self.waitUntilSettled()
3389
3390 # Only C's test jobs are queued because window is still 1.
3391 self.assertEqual(len(self.builds), 2)
3392 self.assertEqual(self.builds[0].name, 'project-test1')
3393 self.assertEqual(self.builds[1].name, 'project-test2')
3394
Paul Belanger174a8272017-03-14 13:20:10 -04003395 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003396 self.waitUntilSettled()
3397
3398 # C was successfully merged so window is increased to 2.
3399 self.assertEqual(queue.window, 2)
3400 self.assertEqual(queue.window_floor, 1)
3401 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003402
James E. Blairec056492016-07-22 09:45:56 -07003403 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003404 def test_worker_update_metadata(self):
3405 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003406 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003407
3408 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003409 A.addApproval('code-review', 2)
3410 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003411 self.waitUntilSettled()
3412
Paul Belanger174a8272017-03-14 13:20:10 -04003413 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003414
3415 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003416 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003417
3418 start = time.time()
3419 while True:
3420 if time.time() - start > 10:
3421 raise Exception("Timeout waiting for gearman server to report "
3422 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003423 build = self.executor.builds.values()[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003424 if build.worker.name == "My Worker":
3425 break
3426 else:
3427 time.sleep(0)
3428
3429 self.log.debug(build)
3430 self.assertEqual("My Worker", build.worker.name)
3431 self.assertEqual("localhost", build.worker.hostname)
3432 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3433 self.assertEqual("zuul.example.org", build.worker.fqdn)
3434 self.assertEqual("FakeBuilder", build.worker.program)
3435 self.assertEqual("v1.1", build.worker.version)
3436 self.assertEqual({'something': 'else'}, build.worker.extra)
3437
Paul Belanger174a8272017-03-14 13:20:10 -04003438 self.executor_server.hold_jobs_in_build = False
3439 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003440 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003441
3442 def test_footer_message(self):
3443 "Test a pipeline's footer message is correctly added to the report."
Adam Gandelman82258fd2017-02-07 16:51:58 -08003444 self.updateConfigLayout('layout-footer-message')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003445 self.sched.reconfigure(self.config)
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003446
3447 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003448 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003449 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003450 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003451 self.waitUntilSettled()
3452
3453 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003454 B.addApproval('code-review', 2)
3455 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003456 self.waitUntilSettled()
3457
3458 self.assertEqual(2, len(self.smtp_messages))
3459
Adam Gandelman82258fd2017-02-07 16:51:58 -08003460 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003461Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003462http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003463
Adam Gandelman82258fd2017-02-07 16:51:58 -08003464 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003465For CI problems and help debugging, contact ci@example.org"""
3466
Adam Gandelman82258fd2017-02-07 16:51:58 -08003467 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3468 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3469 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3470 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003471
James E. Blairec056492016-07-22 09:45:56 -07003472 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003473 def test_merge_failure_reporters(self):
3474 """Check that the config is set up correctly"""
3475
James E. Blairf84026c2015-12-08 16:11:46 -08003476 self.updateConfigLayout(
3477 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003478 self.sched.reconfigure(self.config)
3479 self.registerJobs()
3480
3481 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003482 "Merge Failed.\n\nThis change or one of its cross-repo "
3483 "dependencies was unable to be automatically merged with the "
3484 "current state of its repository. Please rebase the change and "
3485 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003486 self.sched.layout.pipelines['check'].merge_failure_message)
3487 self.assertEqual(
3488 "The merge failed! For more information...",
3489 self.sched.layout.pipelines['gate'].merge_failure_message)
3490
3491 self.assertEqual(
3492 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3493 self.assertEqual(
3494 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3495
3496 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003497 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3498 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003499
3500 self.assertTrue(
3501 (
3502 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003503 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003504 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003505 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003506 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003507 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003508 ) or (
3509 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003510 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003511 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003512 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003513 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003514 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003515 )
3516 )
3517
James E. Blairec056492016-07-22 09:45:56 -07003518 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003519 def test_merge_failure_reports(self):
3520 """Check that when a change fails to merge the correct message is sent
3521 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003522 self.updateConfigLayout(
3523 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003524 self.sched.reconfigure(self.config)
3525 self.registerJobs()
3526
3527 # Check a test failure isn't reported to SMTP
3528 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003529 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003530 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003531 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003532 self.waitUntilSettled()
3533
3534 self.assertEqual(3, len(self.history)) # 3 jobs
3535 self.assertEqual(0, len(self.smtp_messages))
3536
3537 # Check a merge failure is reported to SMTP
3538 # B should be merged, but C will conflict with B
3539 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3540 B.addPatchset(['conflict'])
3541 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3542 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003543 B.addApproval('code-review', 2)
3544 C.addApproval('code-review', 2)
3545 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3546 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003547 self.waitUntilSettled()
3548
3549 self.assertEqual(6, len(self.history)) # A and B jobs
3550 self.assertEqual(1, len(self.smtp_messages))
3551 self.assertEqual('The merge failed! For more information...',
3552 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003553
James E. Blairec056492016-07-22 09:45:56 -07003554 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003555 def test_default_merge_failure_reports(self):
3556 """Check that the default merge failure reports are correct."""
3557
3558 # A should report success, B should report merge failure.
3559 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3560 A.addPatchset(['conflict'])
3561 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3562 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003563 A.addApproval('code-review', 2)
3564 B.addApproval('code-review', 2)
3565 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3566 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003567 self.waitUntilSettled()
3568
3569 self.assertEqual(3, len(self.history)) # A jobs
3570 self.assertEqual(A.reported, 2)
3571 self.assertEqual(B.reported, 2)
3572 self.assertEqual(A.data['status'], 'MERGED')
3573 self.assertEqual(B.data['status'], 'NEW')
3574 self.assertIn('Build succeeded', A.messages[1])
3575 self.assertIn('Merge Failed', B.messages[1])
3576 self.assertIn('automatically merged', B.messages[1])
3577 self.assertNotIn('logs.example.com', B.messages[1])
3578 self.assertNotIn('SKIPPED', B.messages[1])
3579
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003580 def test_client_get_running_jobs(self):
3581 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003582 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003583 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003584 A.addApproval('code-review', 2)
3585 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003586 self.waitUntilSettled()
3587
3588 client = zuul.rpcclient.RPCClient('127.0.0.1',
3589 self.gearman_server.port)
3590
3591 # Wait for gearman server to send the initial workData back to zuul
3592 start = time.time()
3593 while True:
3594 if time.time() - start > 10:
3595 raise Exception("Timeout waiting for gearman server to report "
3596 + "back to the client")
Paul Belanger174a8272017-03-14 13:20:10 -04003597 build = self.executor_client.builds.values()[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003598 if build.worker.name == "My Worker":
3599 break
3600 else:
3601 time.sleep(0)
3602
3603 running_items = client.get_running_jobs()
3604
3605 self.assertEqual(1, len(running_items))
3606 running_item = running_items[0]
3607 self.assertEqual([], running_item['failing_reasons'])
3608 self.assertEqual([], running_item['items_behind'])
3609 self.assertEqual('https://hostname/1', running_item['url'])
3610 self.assertEqual(None, running_item['item_ahead'])
3611 self.assertEqual('org/project', running_item['project'])
3612 self.assertEqual(None, running_item['remaining_time'])
3613 self.assertEqual(True, running_item['active'])
3614 self.assertEqual('1,1', running_item['id'])
3615
3616 self.assertEqual(3, len(running_item['jobs']))
3617 for job in running_item['jobs']:
3618 if job['name'] == 'project-merge':
3619 self.assertEqual('project-merge', job['name'])
3620 self.assertEqual('gate', job['pipeline'])
3621 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003622 self.assertEqual('https://server/job/project-merge/0/',
3623 job['url'])
3624 self.assertEqual(7, len(job['worker']))
3625 self.assertEqual(False, job['canceled'])
3626 self.assertEqual(True, job['voting'])
3627 self.assertEqual(None, job['result'])
3628 self.assertEqual('gate', job['pipeline'])
3629 break
3630
Paul Belanger174a8272017-03-14 13:20:10 -04003631 self.executor_server.hold_jobs_in_build = False
3632 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003633 self.waitUntilSettled()
3634
3635 running_items = client.get_running_jobs()
3636 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003637
3638 def test_nonvoting_pipeline(self):
3639 "Test that a nonvoting pipeline (experimental) can still report"
3640
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003641 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3642 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003643 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3644 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003645
3646 self.assertEqual(self.getJobFromHistory('project-merge').result,
3647 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003648 self.assertEqual(
3649 self.getJobFromHistory('experimental-project-test').result,
3650 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003651 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003652
3653 def test_crd_gate(self):
3654 "Test cross-repo dependencies"
3655 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3656 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003657 A.addApproval('code-review', 2)
3658 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003659
3660 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3661 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3662 AM2.setMerged()
3663 AM1.setMerged()
3664
3665 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3666 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3667 BM2.setMerged()
3668 BM1.setMerged()
3669
3670 # A -> AM1 -> AM2
3671 # B -> BM1 -> BM2
3672 # A Depends-On: B
3673 # M2 is here to make sure it is never queried. If it is, it
3674 # means zuul is walking down the entire history of merged
3675 # changes.
3676
3677 B.setDependsOn(BM1, 1)
3678 BM1.setDependsOn(BM2, 1)
3679
3680 A.setDependsOn(AM1, 1)
3681 AM1.setDependsOn(AM2, 1)
3682
3683 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3684 A.subject, B.data['id'])
3685
James E. Blair8b5408c2016-08-08 15:37:46 -07003686 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003687 self.waitUntilSettled()
3688
3689 self.assertEqual(A.data['status'], 'NEW')
3690 self.assertEqual(B.data['status'], 'NEW')
3691
Paul Belanger5dccbe72016-11-14 11:17:37 -05003692 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003693 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003694
Paul Belanger174a8272017-03-14 13:20:10 -04003695 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003696 B.addApproval('approved', 1)
3697 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003698 self.waitUntilSettled()
3699
Paul Belanger174a8272017-03-14 13:20:10 -04003700 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003701 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003702 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003703 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003704 self.executor_server.hold_jobs_in_build = False
3705 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003706 self.waitUntilSettled()
3707
3708 self.assertEqual(AM2.queried, 0)
3709 self.assertEqual(BM2.queried, 0)
3710 self.assertEqual(A.data['status'], 'MERGED')
3711 self.assertEqual(B.data['status'], 'MERGED')
3712 self.assertEqual(A.reported, 2)
3713 self.assertEqual(B.reported, 2)
3714
Paul Belanger5dccbe72016-11-14 11:17:37 -05003715 changes = self.getJobFromHistory(
3716 'project-merge', 'org/project1').changes
3717 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003718
3719 def test_crd_branch(self):
3720 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003721
3722 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003723 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3724 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3725 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3726 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003727 A.addApproval('code-review', 2)
3728 B.addApproval('code-review', 2)
3729 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003730
3731 # A Depends-On: B+C
3732 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3733 A.subject, B.data['id'])
3734
Paul Belanger174a8272017-03-14 13:20:10 -04003735 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003736 B.addApproval('approved', 1)
3737 C.addApproval('approved', 1)
3738 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003739 self.waitUntilSettled()
3740
Paul Belanger174a8272017-03-14 13:20:10 -04003741 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003742 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003743 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003744 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003745 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003746 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003747 self.executor_server.hold_jobs_in_build = False
3748 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003749 self.waitUntilSettled()
3750
3751 self.assertEqual(A.data['status'], 'MERGED')
3752 self.assertEqual(B.data['status'], 'MERGED')
3753 self.assertEqual(C.data['status'], 'MERGED')
3754 self.assertEqual(A.reported, 2)
3755 self.assertEqual(B.reported, 2)
3756 self.assertEqual(C.reported, 2)
3757
Paul Belanger6379db12016-11-14 13:57:54 -05003758 changes = self.getJobFromHistory(
3759 'project-merge', 'org/project1').changes
3760 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003761
3762 def test_crd_multiline(self):
3763 "Test multiple depends-on lines in commit"
3764 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3765 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3766 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003767 A.addApproval('code-review', 2)
3768 B.addApproval('code-review', 2)
3769 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003770
3771 # A Depends-On: B+C
3772 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3773 A.subject, B.data['id'], C.data['id'])
3774
Paul Belanger174a8272017-03-14 13:20:10 -04003775 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003776 B.addApproval('approved', 1)
3777 C.addApproval('approved', 1)
3778 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003779 self.waitUntilSettled()
3780
Paul Belanger174a8272017-03-14 13:20:10 -04003781 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003782 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003783 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003784 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003785 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003786 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003787 self.executor_server.hold_jobs_in_build = False
3788 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003789 self.waitUntilSettled()
3790
3791 self.assertEqual(A.data['status'], 'MERGED')
3792 self.assertEqual(B.data['status'], 'MERGED')
3793 self.assertEqual(C.data['status'], 'MERGED')
3794 self.assertEqual(A.reported, 2)
3795 self.assertEqual(B.reported, 2)
3796 self.assertEqual(C.reported, 2)
3797
Paul Belanger5dccbe72016-11-14 11:17:37 -05003798 changes = self.getJobFromHistory(
3799 'project-merge', 'org/project1').changes
3800 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003801
3802 def test_crd_unshared_gate(self):
3803 "Test cross-repo dependencies in unshared gate queues"
3804 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3805 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003806 A.addApproval('code-review', 2)
3807 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003808
3809 # A Depends-On: B
3810 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3811 A.subject, B.data['id'])
3812
3813 # A and B do not share a queue, make sure that A is unable to
3814 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003815 B.addApproval('approved', 1)
3816 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003817 self.waitUntilSettled()
3818
3819 self.assertEqual(A.data['status'], 'NEW')
3820 self.assertEqual(B.data['status'], 'NEW')
3821 self.assertEqual(A.reported, 0)
3822 self.assertEqual(B.reported, 0)
3823 self.assertEqual(len(self.history), 0)
3824
3825 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003826 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003827 self.waitUntilSettled()
3828
3829 self.assertEqual(B.data['status'], 'MERGED')
3830 self.assertEqual(B.reported, 2)
3831
3832 # Now that B is merged, A should be able to be enqueued and
3833 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003834 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003835 self.waitUntilSettled()
3836
3837 self.assertEqual(A.data['status'], 'MERGED')
3838 self.assertEqual(A.reported, 2)
3839
James E. Blair96698e22015-04-02 07:48:21 -07003840 def test_crd_gate_reverse(self):
3841 "Test reverse cross-repo dependencies"
3842 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3843 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003844 A.addApproval('code-review', 2)
3845 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003846
3847 # A Depends-On: B
3848
3849 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3850 A.subject, B.data['id'])
3851
James E. Blair8b5408c2016-08-08 15:37:46 -07003852 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003853 self.waitUntilSettled()
3854
3855 self.assertEqual(A.data['status'], 'NEW')
3856 self.assertEqual(B.data['status'], 'NEW')
3857
Paul Belanger174a8272017-03-14 13:20:10 -04003858 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003859 A.addApproval('approved', 1)
3860 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003861 self.waitUntilSettled()
3862
Paul Belanger174a8272017-03-14 13:20:10 -04003863 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003864 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003865 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003866 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003867 self.executor_server.hold_jobs_in_build = False
3868 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003869 self.waitUntilSettled()
3870
3871 self.assertEqual(A.data['status'], 'MERGED')
3872 self.assertEqual(B.data['status'], 'MERGED')
3873 self.assertEqual(A.reported, 2)
3874 self.assertEqual(B.reported, 2)
3875
Paul Belanger5dccbe72016-11-14 11:17:37 -05003876 changes = self.getJobFromHistory(
3877 'project-merge', 'org/project1').changes
3878 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003879
James E. Blair5ee24252014-12-30 10:12:29 -08003880 def test_crd_cycle(self):
3881 "Test cross-repo dependency cycles"
3882 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3883 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003884 A.addApproval('code-review', 2)
3885 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003886
3887 # A -> B -> A (via commit-depends)
3888
3889 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3890 A.subject, B.data['id'])
3891 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3892 B.subject, A.data['id'])
3893
James E. Blair8b5408c2016-08-08 15:37:46 -07003894 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003895 self.waitUntilSettled()
3896
3897 self.assertEqual(A.reported, 0)
3898 self.assertEqual(B.reported, 0)
3899 self.assertEqual(A.data['status'], 'NEW')
3900 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003901
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003902 def test_crd_gate_unknown(self):
3903 "Test unknown projects in dependent pipeline"
3904 self.init_repo("org/unknown")
3905 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3906 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003907 A.addApproval('code-review', 2)
3908 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003909
3910 # A Depends-On: B
3911 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3912 A.subject, B.data['id'])
3913
James E. Blair8b5408c2016-08-08 15:37:46 -07003914 B.addApproval('approved', 1)
3915 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003916 self.waitUntilSettled()
3917
3918 # Unknown projects cannot share a queue with any other
3919 # since they don't have common jobs with any other (they have no jobs).
3920 # Changes which depend on unknown project changes
3921 # should not be processed in dependent pipeline
3922 self.assertEqual(A.data['status'], 'NEW')
3923 self.assertEqual(B.data['status'], 'NEW')
3924 self.assertEqual(A.reported, 0)
3925 self.assertEqual(B.reported, 0)
3926 self.assertEqual(len(self.history), 0)
3927
3928 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003929 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003930 B.setMerged()
3931 self.waitUntilSettled()
3932 self.assertEqual(len(self.history), 0)
3933
3934 # Now that B is merged, A should be able to be enqueued and
3935 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003936 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003937 self.waitUntilSettled()
3938
3939 self.assertEqual(A.data['status'], 'MERGED')
3940 self.assertEqual(A.reported, 2)
3941 self.assertEqual(B.data['status'], 'MERGED')
3942 self.assertEqual(B.reported, 0)
3943
James E. Blairbfb8e042014-12-30 17:01:44 -08003944 def test_crd_check(self):
3945 "Test cross-repo dependencies in independent pipelines"
3946
Paul Belanger174a8272017-03-14 13:20:10 -04003947 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003948 self.gearman_server.hold_jobs_in_queue = True
3949 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3950 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3951
3952 # A Depends-On: B
3953 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3954 A.subject, B.data['id'])
3955
3956 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3957 self.waitUntilSettled()
3958
3959 queue = self.gearman_server.getQueue()
3960 ref = self.getParameter(queue[-1], 'ZUUL_REF')
3961 self.gearman_server.hold_jobs_in_queue = False
3962 self.gearman_server.release()
3963 self.waitUntilSettled()
3964
Paul Belanger174a8272017-03-14 13:20:10 -04003965 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003966 self.waitUntilSettled()
3967
Monty Taylord642d852017-02-23 14:05:42 -05003968 path = os.path.join(self.builds[0].jobdir.src_root, "org/project1")
James E. Blairbfb8e042014-12-30 17:01:44 -08003969 repo = git.Repo(path)
3970 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3971 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003972 correct_messages = [
3973 'initial commit', 'add content from fixture', 'A-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003974 self.assertEqual(repo_messages, correct_messages)
3975
Monty Taylord642d852017-02-23 14:05:42 -05003976 path = os.path.join(self.builds[0].jobdir.src_root, "org/project2")
James E. Blairbfb8e042014-12-30 17:01:44 -08003977 repo = git.Repo(path)
3978 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
3979 repo_messages.reverse()
Paul Belangerb30342b2016-11-14 12:30:43 -05003980 correct_messages = [
3981 'initial commit', 'add content from fixture', 'B-1']
James E. Blairbfb8e042014-12-30 17:01:44 -08003982 self.assertEqual(repo_messages, correct_messages)
3983
Paul Belanger174a8272017-03-14 13:20:10 -04003984 self.executor_server.hold_jobs_in_build = False
3985 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003986 self.waitUntilSettled()
3987
James E. Blairbfb8e042014-12-30 17:01:44 -08003988 self.assertEqual(A.data['status'], 'NEW')
3989 self.assertEqual(B.data['status'], 'NEW')
3990 self.assertEqual(A.reported, 1)
3991 self.assertEqual(B.reported, 0)
3992
3993 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003994 tenant = self.sched.abide.tenants.get('tenant-one')
3995 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003996
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003997 def test_crd_check_git_depends(self):
3998 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003999 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004000 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4001 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4002
4003 # Add two git-dependent changes and make sure they both report
4004 # success.
4005 B.setDependsOn(A, 1)
4006 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4007 self.waitUntilSettled()
4008 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4009 self.waitUntilSettled()
4010
James E. Blairb8c16472015-05-05 14:55:26 -07004011 self.orderedRelease()
4012 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004013 self.waitUntilSettled()
4014
4015 self.assertEqual(A.data['status'], 'NEW')
4016 self.assertEqual(B.data['status'], 'NEW')
4017 self.assertEqual(A.reported, 1)
4018 self.assertEqual(B.reported, 1)
4019
4020 self.assertEqual(self.history[0].changes, '1,1')
4021 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004022 tenant = self.sched.abide.tenants.get('tenant-one')
4023 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004024
4025 self.assertIn('Build succeeded', A.messages[0])
4026 self.assertIn('Build succeeded', B.messages[0])
4027
4028 def test_crd_check_duplicate(self):
4029 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004030 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004031 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4032 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004033 tenant = self.sched.abide.tenants.get('tenant-one')
4034 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004035
4036 # Add two git-dependent changes...
4037 B.setDependsOn(A, 1)
4038 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4039 self.waitUntilSettled()
4040 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4041
4042 # ...make sure the live one is not duplicated...
4043 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4044 self.waitUntilSettled()
4045 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4046
4047 # ...but the non-live one is able to be.
4048 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4049 self.waitUntilSettled()
4050 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4051
Clark Boylandd849822015-03-02 12:38:14 -08004052 # Release jobs in order to avoid races with change A jobs
4053 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004054 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004055 self.executor_server.hold_jobs_in_build = False
4056 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004057 self.waitUntilSettled()
4058
4059 self.assertEqual(A.data['status'], 'NEW')
4060 self.assertEqual(B.data['status'], 'NEW')
4061 self.assertEqual(A.reported, 1)
4062 self.assertEqual(B.reported, 1)
4063
4064 self.assertEqual(self.history[0].changes, '1,1 2,1')
4065 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004066 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004067
4068 self.assertIn('Build succeeded', A.messages[0])
4069 self.assertIn('Build succeeded', B.messages[0])
4070
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004071 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004072 "Test cross-repo dependencies re-enqueued in independent pipelines"
4073
4074 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004075 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4076 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004077
4078 # A Depends-On: B
4079 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4080 A.subject, B.data['id'])
4081
4082 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4083 self.waitUntilSettled()
4084
4085 self.sched.reconfigure(self.config)
4086
4087 # Make sure the items still share a change queue, and the
4088 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004089 tenant = self.sched.abide.tenants.get('tenant-one')
4090 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4091 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004092 first_item = queue.queue[0]
4093 for item in queue.queue:
4094 self.assertEqual(item.queue, first_item.queue)
4095 self.assertFalse(first_item.live)
4096 self.assertTrue(queue.queue[1].live)
4097
4098 self.gearman_server.hold_jobs_in_queue = False
4099 self.gearman_server.release()
4100 self.waitUntilSettled()
4101
4102 self.assertEqual(A.data['status'], 'NEW')
4103 self.assertEqual(B.data['status'], 'NEW')
4104 self.assertEqual(A.reported, 1)
4105 self.assertEqual(B.reported, 0)
4106
4107 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004108 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004109
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004110 def test_crd_check_reconfiguration(self):
4111 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4112
4113 def test_crd_undefined_project(self):
4114 """Test that undefined projects in dependencies are handled for
4115 independent pipelines"""
4116 # It's a hack for fake gerrit,
4117 # as it implies repo creation upon the creation of any change
4118 self.init_repo("org/unknown")
4119 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4120
James E. Blairec056492016-07-22 09:45:56 -07004121 @skip("Disabled for early v3 development")
James E. Blair17dd6772015-02-09 14:45:18 -08004122 def test_crd_check_ignore_dependencies(self):
4123 "Test cross-repo dependencies can be ignored"
James E. Blairf84026c2015-12-08 16:11:46 -08004124 self.updateConfigLayout(
4125 'tests/fixtures/layout-ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004126 self.sched.reconfigure(self.config)
4127 self.registerJobs()
4128
4129 self.gearman_server.hold_jobs_in_queue = True
4130 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4131 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4132 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4133
4134 # A Depends-On: B
4135 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4136 A.subject, B.data['id'])
4137 # C git-depends on B
4138 C.setDependsOn(B, 1)
4139 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4140 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4141 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4142 self.waitUntilSettled()
4143
4144 # Make sure none of the items share a change queue, and all
4145 # are live.
4146 check_pipeline = self.sched.layout.pipelines['check']
4147 self.assertEqual(len(check_pipeline.queues), 3)
4148 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4149 for item in check_pipeline.getAllItems():
4150 self.assertTrue(item.live)
4151
4152 self.gearman_server.hold_jobs_in_queue = False
4153 self.gearman_server.release()
4154 self.waitUntilSettled()
4155
4156 self.assertEqual(A.data['status'], 'NEW')
4157 self.assertEqual(B.data['status'], 'NEW')
4158 self.assertEqual(C.data['status'], 'NEW')
4159 self.assertEqual(A.reported, 1)
4160 self.assertEqual(B.reported, 1)
4161 self.assertEqual(C.reported, 1)
4162
4163 # Each job should have tested exactly one change
4164 for job in self.history:
4165 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004166
James E. Blairec056492016-07-22 09:45:56 -07004167 @skip("Disabled for early v3 development")
James E. Blair96698e22015-04-02 07:48:21 -07004168 def test_crd_check_transitive(self):
4169 "Test transitive cross-repo dependencies"
4170 # Specifically, if A -> B -> C, and C gets a new patchset and
4171 # A gets a new patchset, ensure the test of A,2 includes B,1
4172 # and C,2 (not C,1 which would indicate stale data in the
4173 # cache for B).
4174 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4175 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4176 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4177
4178 # A Depends-On: B
4179 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4180 A.subject, B.data['id'])
4181
4182 # B Depends-On: C
4183 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4184 B.subject, C.data['id'])
4185
4186 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4187 self.waitUntilSettled()
4188 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4189
4190 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4191 self.waitUntilSettled()
4192 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4193
4194 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4195 self.waitUntilSettled()
4196 self.assertEqual(self.history[-1].changes, '3,1')
4197
4198 C.addPatchset()
4199 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4200 self.waitUntilSettled()
4201 self.assertEqual(self.history[-1].changes, '3,2')
4202
4203 A.addPatchset()
4204 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4205 self.waitUntilSettled()
4206 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004207
James E. Blaira8b90b32016-08-24 15:18:50 -07004208 def test_crd_check_unknown(self):
4209 "Test unknown projects in independent pipeline"
4210 self.init_repo("org/unknown")
4211 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4212 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4213 # A Depends-On: B
4214 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4215 A.subject, B.data['id'])
4216
4217 # Make sure zuul has seen an event on B.
4218 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4219 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4220 self.waitUntilSettled()
4221
4222 self.assertEqual(A.data['status'], 'NEW')
4223 self.assertEqual(A.reported, 1)
4224 self.assertEqual(B.data['status'], 'NEW')
4225 self.assertEqual(B.reported, 0)
4226
James E. Blair92464a22016-04-05 10:21:26 -07004227 def test_crd_cycle_join(self):
4228 "Test an updated change creates a cycle"
4229 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4230
4231 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4232 self.waitUntilSettled()
4233
4234 # Create B->A
4235 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4236 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4237 B.subject, A.data['id'])
4238 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4239 self.waitUntilSettled()
4240
4241 # Update A to add A->B (a cycle).
4242 A.addPatchset()
4243 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4244 A.subject, B.data['id'])
4245 # Normally we would submit the patchset-created event for
4246 # processing here, however, we have no way of noting whether
4247 # the dependency cycle detection correctly raised an
4248 # exception, so instead, we reach into the source driver and
4249 # call the method that would ultimately be called by the event
4250 # processing.
4251
Paul Belanger0e155e22016-11-14 14:12:23 -05004252 tenant = self.sched.abide.tenants.get('tenant-one')
4253 source = tenant.layout.pipelines['gate'].source
4254
4255 # TODO(pabelanger): As we add more source / trigger APIs we should make
4256 # it easier for users to create events for testing.
4257 event = zuul.model.TriggerEvent()
4258 event.trigger_name = 'gerrit'
4259 event.change_number = '1'
4260 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004261 with testtools.ExpectedException(
4262 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004263 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004264 self.log.debug("Got expected dependency cycle exception")
4265
4266 # Now if we update B to remove the depends-on, everything
4267 # should be okay. B; A->B
4268
4269 B.addPatchset()
4270 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004271
4272 source.getChange(event, True)
4273 event.change_number = '2'
4274 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004275
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004276 def test_disable_at(self):
4277 "Test a pipeline will only report to the disabled trigger when failing"
4278
Paul Belanger7dc76e82016-11-11 16:51:08 -05004279 self.updateConfigLayout('layout-disabled-at')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004280 self.sched.reconfigure(self.config)
4281
Paul Belanger7dc76e82016-11-11 16:51:08 -05004282 tenant = self.sched.abide.tenants.get('openstack')
4283 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004284 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004285 0, tenant.layout.pipelines['check']._consecutive_failures)
4286 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004287
4288 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4289 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4290 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4291 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4292 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4293 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4294 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4295 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4296 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4297 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4298 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4299
Paul Belanger174a8272017-03-14 13:20:10 -04004300 self.executor_server.failJob('project-test1', A)
4301 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004302 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004303 self.executor_server.failJob('project-test1', D)
4304 self.executor_server.failJob('project-test1', E)
4305 self.executor_server.failJob('project-test1', F)
4306 self.executor_server.failJob('project-test1', G)
4307 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004308 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004309 self.executor_server.failJob('project-test1', J)
4310 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004311
4312 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4313 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4314 self.waitUntilSettled()
4315
4316 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004317 2, tenant.layout.pipelines['check']._consecutive_failures)
4318 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004319
4320 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4321 self.waitUntilSettled()
4322
4323 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004324 0, tenant.layout.pipelines['check']._consecutive_failures)
4325 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004326
4327 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4328 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4329 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4330 self.waitUntilSettled()
4331
4332 # We should be disabled now
4333 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004334 3, tenant.layout.pipelines['check']._consecutive_failures)
4335 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004336
4337 # We need to wait between each of these patches to make sure the
4338 # smtp messages come back in an expected order
4339 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4340 self.waitUntilSettled()
4341 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4342 self.waitUntilSettled()
4343 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4344 self.waitUntilSettled()
4345
4346 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4347 # leaving a message on each change
4348 self.assertEqual(1, len(A.messages))
4349 self.assertIn('Build failed.', A.messages[0])
4350 self.assertEqual(1, len(B.messages))
4351 self.assertIn('Build failed.', B.messages[0])
4352 self.assertEqual(1, len(C.messages))
4353 self.assertIn('Build succeeded.', C.messages[0])
4354 self.assertEqual(1, len(D.messages))
4355 self.assertIn('Build failed.', D.messages[0])
4356 self.assertEqual(1, len(E.messages))
4357 self.assertIn('Build failed.', E.messages[0])
4358 self.assertEqual(1, len(F.messages))
4359 self.assertIn('Build failed.', F.messages[0])
4360
4361 # The last 3 (GHI) would have only reported via smtp.
4362 self.assertEqual(3, len(self.smtp_messages))
4363 self.assertEqual(0, len(G.messages))
4364 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004365 self.assertIn(
4366 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004367 self.assertEqual(0, len(H.messages))
4368 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004369 self.assertIn(
4370 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004371 self.assertEqual(0, len(I.messages))
4372 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004373 self.assertIn(
4374 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004375
4376 # Now reload the configuration (simulate a HUP) to check the pipeline
4377 # comes out of disabled
4378 self.sched.reconfigure(self.config)
4379
Paul Belanger7dc76e82016-11-11 16:51:08 -05004380 tenant = self.sched.abide.tenants.get('openstack')
4381
4382 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004383 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004384 0, tenant.layout.pipelines['check']._consecutive_failures)
4385 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004386
4387 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4388 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4389 self.waitUntilSettled()
4390
4391 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004392 2, tenant.layout.pipelines['check']._consecutive_failures)
4393 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004394
4395 # J and K went back to gerrit
4396 self.assertEqual(1, len(J.messages))
4397 self.assertIn('Build failed.', J.messages[0])
4398 self.assertEqual(1, len(K.messages))
4399 self.assertIn('Build failed.', K.messages[0])
4400 # No more messages reported via smtp
4401 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004402
Paul Belanger71d98172016-11-08 10:56:31 -05004403 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004404 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004405
Paul Belanger174a8272017-03-14 13:20:10 -04004406 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004407 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4408 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4409 self.waitUntilSettled()
4410
Paul Belanger174a8272017-03-14 13:20:10 -04004411 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004412 self.waitUntilSettled()
4413
4414 self.assertEqual(len(self.builds), 2)
4415 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004416 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004417 self.waitUntilSettled()
4418
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004419 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004420 self.assertEqual(len(self.builds), 1,
4421 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004422 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004423 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004424 self.waitUntilSettled()
4425
Paul Belanger174a8272017-03-14 13:20:10 -04004426 self.executor_server.hold_jobs_in_build = False
4427 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004428 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004429 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004430 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4431 self.assertEqual(A.reported, 1)
4432 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004433
James E. Blair15be0e12017-01-03 13:45:20 -08004434 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004435 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004436
4437 self.fake_nodepool.paused = True
4438 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4439 A.addApproval('code-review', 2)
4440 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4441 self.waitUntilSettled()
4442
4443 self.zk.client.stop()
4444 self.zk.client.start()
4445 self.fake_nodepool.paused = False
4446 self.waitUntilSettled()
4447
4448 self.assertEqual(A.data['status'], 'MERGED')
4449 self.assertEqual(A.reported, 2)
4450
James E. Blair6ab79e02017-01-06 10:10:17 -08004451 def test_nodepool_failure(self):
4452 "Test that jobs are reported after a nodepool failure"
4453
4454 self.fake_nodepool.paused = True
4455 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4456 A.addApproval('code-review', 2)
4457 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4458 self.waitUntilSettled()
4459
4460 req = self.fake_nodepool.getNodeRequests()[0]
4461 self.fake_nodepool.addFailRequest(req)
4462
4463 self.fake_nodepool.paused = False
4464 self.waitUntilSettled()
4465
4466 self.assertEqual(A.data['status'], 'NEW')
4467 self.assertEqual(A.reported, 2)
4468 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4469 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4470 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4471
Arieb6f068c2016-10-09 13:11:06 +03004472
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004473class TestDependencyGraph(ZuulTestCase):
4474 tenant_config_file = 'config/dependency-graph/main.yaml'
4475
4476 def test_dependeny_graph_dispatch_jobs_once(self):
4477 "Test a job in a dependency graph is queued only once"
4478 # Job dependencies, starting with A
4479 # A
4480 # / \
4481 # B C
4482 # / \ / \
4483 # D F E
4484 # |
4485 # G
4486
4487 self.executor_server.hold_jobs_in_build = True
4488 change = self.fake_gerrit.addFakeChange(
4489 'org/project', 'master', 'change')
4490 change.addApproval('code-review', 2)
4491 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4492
4493 self.waitUntilSettled()
4494 self.assertEqual([b.name for b in self.builds], ['A'])
4495
4496 self.executor_server.release('A')
4497 self.waitUntilSettled()
4498 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4499
4500 self.executor_server.release('B')
4501 self.waitUntilSettled()
4502 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4503
4504 self.executor_server.release('D')
4505 self.waitUntilSettled()
4506 self.assertEqual([b.name for b in self.builds], ['C'])
4507
4508 self.executor_server.release('C')
4509 self.waitUntilSettled()
4510 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4511
4512 self.executor_server.release('F')
4513 self.waitUntilSettled()
4514 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4515
4516 self.executor_server.release('G')
4517 self.waitUntilSettled()
4518 self.assertEqual([b.name for b in self.builds], ['E'])
4519
4520 self.executor_server.release('E')
4521 self.waitUntilSettled()
4522 self.assertEqual(len(self.builds), 0)
4523
4524 self.executor_server.hold_jobs_in_build = False
4525 self.executor_server.release()
4526 self.waitUntilSettled()
4527
4528 self.assertEqual(len(self.builds), 0)
4529 self.assertEqual(len(self.history), 7)
4530
4531 self.assertEqual(change.data['status'], 'MERGED')
4532 self.assertEqual(change.reported, 2)
4533
4534 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4535 "Test that a job waits till all dependencies are successful"
4536 # Job dependencies, starting with A
4537 # A
4538 # / \
4539 # B C*
4540 # / \ / \
4541 # D F E
4542 # |
4543 # G
4544
4545 self.executor_server.hold_jobs_in_build = True
4546 change = self.fake_gerrit.addFakeChange(
4547 'org/project', 'master', 'change')
4548 change.addApproval('code-review', 2)
4549
4550 self.executor_server.failJob('C', change)
4551
4552 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4553
4554 self.waitUntilSettled()
4555 self.assertEqual([b.name for b in self.builds], ['A'])
4556
4557 self.executor_server.release('A')
4558 self.waitUntilSettled()
4559 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4560
4561 self.executor_server.release('B')
4562 self.waitUntilSettled()
4563 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4564
4565 self.executor_server.release('D')
4566 self.waitUntilSettled()
4567 self.assertEqual([b.name for b in self.builds], ['C'])
4568
4569 self.executor_server.release('C')
4570 self.waitUntilSettled()
4571 self.assertEqual(len(self.builds), 0)
4572
4573 self.executor_server.hold_jobs_in_build = False
4574 self.executor_server.release()
4575 self.waitUntilSettled()
4576
4577 self.assertEqual(len(self.builds), 0)
4578 self.assertEqual(len(self.history), 4)
4579
4580 self.assertEqual(change.data['status'], 'NEW')
4581 self.assertEqual(change.reported, 2)
4582
4583
Arieb6f068c2016-10-09 13:11:06 +03004584class TestDuplicatePipeline(ZuulTestCase):
4585 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4586
4587 def test_duplicate_pipelines(self):
4588 "Test that a change matching multiple pipelines works"
4589
4590 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4591 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4592 self.waitUntilSettled()
4593
4594 self.assertHistory([
4595 dict(name='project-test1', result='SUCCESS', changes='1,1',
4596 pipeline='dup1'),
4597 dict(name='project-test1', result='SUCCESS', changes='1,1',
4598 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004599 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004600
4601 self.assertEqual(len(A.messages), 2)
4602
Paul Belangera46a3742016-11-09 14:23:03 -05004603 if 'dup1' in A.messages[0]:
4604 self.assertIn('dup1', A.messages[0])
4605 self.assertNotIn('dup2', A.messages[0])
4606 self.assertIn('project-test1', A.messages[0])
4607 self.assertIn('dup2', A.messages[1])
4608 self.assertNotIn('dup1', A.messages[1])
4609 self.assertIn('project-test1', A.messages[1])
4610 else:
4611 self.assertIn('dup1', A.messages[1])
4612 self.assertNotIn('dup2', A.messages[1])
4613 self.assertIn('project-test1', A.messages[1])
4614 self.assertIn('dup2', A.messages[0])
4615 self.assertNotIn('dup1', A.messages[0])
4616 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004617
4618
4619class TestSchedulerOneJobProject(ZuulTestCase):
4620 tenant_config_file = 'config/one-job-project/main.yaml'
4621
4622 def test_one_job_project(self):
4623 "Test that queueing works with one job"
4624 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4625 'master', 'A')
4626 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4627 'master', 'B')
4628 A.addApproval('code-review', 2)
4629 B.addApproval('code-review', 2)
4630 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4631 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4632 self.waitUntilSettled()
4633
4634 self.assertEqual(A.data['status'], 'MERGED')
4635 self.assertEqual(A.reported, 2)
4636 self.assertEqual(B.data['status'], 'MERGED')
4637 self.assertEqual(B.reported, 2)
Paul Belanger86085b32016-11-03 12:48:57 -04004638
4639
4640class TestSchedulerTemplatedProject(ZuulTestCase):
4641 tenant_config_file = 'config/templated-project/main.yaml'
4642
Paul Belanger174a8272017-03-14 13:20:10 -04004643 def test_job_from_templates_executed(self):
4644 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004645
4646 A = self.fake_gerrit.addFakeChange(
4647 'org/templated-project', 'master', 'A')
4648 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4649 self.waitUntilSettled()
4650
4651 self.assertEqual(self.getJobFromHistory('project-test1').result,
4652 'SUCCESS')
4653 self.assertEqual(self.getJobFromHistory('project-test2').result,
4654 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004655
4656 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004657 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004658
4659 A = self.fake_gerrit.addFakeChange(
4660 'org/layered-project', 'master', 'A')
4661 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4662 self.waitUntilSettled()
4663
4664 self.assertEqual(self.getJobFromHistory('project-test1').result,
4665 'SUCCESS')
4666 self.assertEqual(self.getJobFromHistory('project-test2').result,
4667 'SUCCESS')
4668 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4669 ).result, 'SUCCESS')
4670 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4671 ).result, 'SUCCESS')
4672 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4673 ).result, 'SUCCESS')
4674 self.assertEqual(self.getJobFromHistory('project-test6').result,
4675 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004676
4677
4678class TestSchedulerSuccessURL(ZuulTestCase):
4679 tenant_config_file = 'config/success-url/main.yaml'
4680
4681 def test_success_url(self):
4682 "Ensure bad build params are ignored"
4683 self.sched.reconfigure(self.config)
4684 self.init_repo('org/docs')
4685
4686 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4687 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4688 self.waitUntilSettled()
4689
4690 # Both builds ran: docs-draft-test + docs-draft-test2
4691 self.assertEqual(len(self.history), 2)
4692
4693 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004694 for build in self.history:
4695 if build.name == 'docs-draft-test':
4696 uuid = build.uuid[:7]
4697 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004698
4699 # Two msgs: 'Starting...' + results
4700 self.assertEqual(len(self.smtp_messages), 2)
4701 body = self.smtp_messages[1]['body'].splitlines()
4702 self.assertEqual('Build succeeded.', body[0])
4703
4704 self.assertIn(
4705 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4706 'docs-draft-test/%s/publish-docs/' % uuid,
4707 body[2])
4708
Paul Belanger174a8272017-03-14 13:20:10 -04004709 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004710 self.assertIn(
4711 '- docs-draft-test2 https://server/job',
4712 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004713
4714
Adam Gandelman4da00f62016-12-09 15:47:33 -08004715class TestSchedulerMerges(ZuulTestCase):
4716 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004717
4718 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004719 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004720 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004721 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004722 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4723 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4724 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4725 A.addApproval('code-review', 2)
4726 B.addApproval('code-review', 2)
4727 C.addApproval('code-review', 2)
4728 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4729 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4730 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4731 self.waitUntilSettled()
4732
4733 build = self.builds[-1]
4734 ref = self.getParameter(build, 'ZUUL_REF')
4735
Monty Taylord642d852017-02-23 14:05:42 -05004736 path = os.path.join(build.jobdir.src_root, project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004737 repo = git.Repo(path)
4738 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4739 repo_messages.reverse()
4740
Paul Belanger174a8272017-03-14 13:20:10 -04004741 self.executor_server.hold_jobs_in_build = False
4742 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004743 self.waitUntilSettled()
4744
4745 return repo_messages
4746
4747 def _test_merge(self, mode):
4748 us_path = os.path.join(
4749 self.upstream_root, 'org/project-%s' % mode)
4750 expected_messages = [
4751 'initial commit',
4752 'add content from fixture',
4753 # the intermediate commits order is nondeterministic
4754 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4755 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4756 ]
4757 result = self._test_project_merge_mode(mode)
4758 self.assertEqual(result[:2], expected_messages[:2])
4759 self.assertEqual(result[-2:], expected_messages[-2:])
4760
4761 def test_project_merge_mode_merge(self):
4762 self._test_merge('merge')
4763
4764 def test_project_merge_mode_merge_resolve(self):
4765 self._test_merge('merge-resolve')
4766
4767 def test_project_merge_mode_cherrypick(self):
4768 expected_messages = [
4769 'initial commit',
4770 'add content from fixture',
4771 'A-1',
4772 'B-1',
4773 'C-1']
4774 result = self._test_project_merge_mode('cherry-pick')
4775 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004776
4777 def test_merge_branch(self):
4778 "Test that the right commits are on alternate branches"
4779 self.create_branch('org/project-merge-branches', 'mp')
4780
Paul Belanger174a8272017-03-14 13:20:10 -04004781 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004782 A = self.fake_gerrit.addFakeChange(
4783 'org/project-merge-branches', 'mp', 'A')
4784 B = self.fake_gerrit.addFakeChange(
4785 'org/project-merge-branches', 'mp', 'B')
4786 C = self.fake_gerrit.addFakeChange(
4787 'org/project-merge-branches', 'mp', 'C')
4788 A.addApproval('code-review', 2)
4789 B.addApproval('code-review', 2)
4790 C.addApproval('code-review', 2)
4791 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4792 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4793 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4794 self.waitUntilSettled()
4795
Paul Belanger174a8272017-03-14 13:20:10 -04004796 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004797 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004798 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004799 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004800 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004801 self.waitUntilSettled()
4802
4803 build = self.builds[-1]
4804 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4805 ref = self.getParameter(build, 'ZUUL_REF')
4806 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004807 build.jobdir.src_root, 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004808 repo = git.Repo(path)
4809
4810 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4811 repo_messages.reverse()
4812 correct_messages = [
4813 'initial commit',
4814 'add content from fixture',
4815 'mp commit',
4816 'A-1', 'B-1', 'C-1']
4817 self.assertEqual(repo_messages, correct_messages)
4818
Paul Belanger174a8272017-03-14 13:20:10 -04004819 self.executor_server.hold_jobs_in_build = False
4820 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004821 self.waitUntilSettled()
4822
4823 def test_merge_multi_branch(self):
4824 "Test that dependent changes on multiple branches are merged"
4825 self.create_branch('org/project-merge-branches', 'mp')
4826
Paul Belanger174a8272017-03-14 13:20:10 -04004827 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004828 A = self.fake_gerrit.addFakeChange(
4829 'org/project-merge-branches', 'master', 'A')
4830 B = self.fake_gerrit.addFakeChange(
4831 'org/project-merge-branches', 'mp', 'B')
4832 C = self.fake_gerrit.addFakeChange(
4833 'org/project-merge-branches', 'master', 'C')
4834 A.addApproval('code-review', 2)
4835 B.addApproval('code-review', 2)
4836 C.addApproval('code-review', 2)
4837 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4838 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4839 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4840 self.waitUntilSettled()
4841
4842 job_A = None
4843 for job in self.builds:
4844 if 'project-merge' in job.name:
4845 job_A = job
4846 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4847 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4848 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4849 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4850
4851 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004852 job_A.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004853 repo = git.Repo(path)
4854 repo_messages = [c.message.strip()
4855 for c in repo.iter_commits(ref_A)]
4856 repo_messages.reverse()
4857 correct_messages = [
4858 'initial commit', 'add content from fixture', 'A-1']
4859 self.assertEqual(repo_messages, correct_messages)
4860
Paul Belanger174a8272017-03-14 13:20:10 -04004861 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004862 self.waitUntilSettled()
4863
4864 job_B = None
4865 for job in self.builds:
4866 if 'project-merge' in job.name:
4867 job_B = job
4868 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4869 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4870 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4871 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4872
4873 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004874 job_B.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004875 repo = git.Repo(path)
4876 repo_messages = [c.message.strip()
4877 for c in repo.iter_commits(ref_B)]
4878 repo_messages.reverse()
4879 correct_messages = [
4880 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4881 self.assertEqual(repo_messages, correct_messages)
4882
Paul Belanger174a8272017-03-14 13:20:10 -04004883 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004884 self.waitUntilSettled()
4885
4886 job_C = None
4887 for job in self.builds:
4888 if 'project-merge' in job.name:
4889 job_C = job
4890 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4891 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4892 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4893 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
4894 path = os.path.join(
Monty Taylord642d852017-02-23 14:05:42 -05004895 job_C.jobdir.src_root, "org/project-merge-branches")
Adam Gandelman4da00f62016-12-09 15:47:33 -08004896 repo = git.Repo(path)
4897 repo_messages = [c.message.strip()
4898 for c in repo.iter_commits(ref_C)]
4899
4900 repo_messages.reverse()
4901 correct_messages = [
4902 'initial commit', 'add content from fixture',
4903 'A-1', 'C-1']
4904 # Ensure the right commits are in the history for this ref
4905 self.assertEqual(repo_messages, correct_messages)
4906
Paul Belanger174a8272017-03-14 13:20:10 -04004907 self.executor_server.hold_jobs_in_build = False
4908 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004909 self.waitUntilSettled()