blob: f394c0cf2b279b9391f835c6603aceeefd0ca660 [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. Blaira002b032017-04-18 10:35:48 -070017import gc
James E. Blairb0fcae42012-07-17 11:12:10 -070018import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010019import textwrap
20
Monty Taylorbc758832013-06-17 17:22:42 -040021import os
James E. Blairb0fcae42012-07-17 11:12:10 -070022import re
James E. Blair4886cc12012-07-18 15:39:41 -070023import shutil
Clint Byrum8897dca2017-05-10 19:14:19 -070024import sys
Monty Taylorbc758832013-06-17 17:22:42 -040025import time
Clint Byrum8897dca2017-05-10 19:14:19 -070026from unittest import (skip, skipIf)
Monty Taylorbc758832013-06-17 17:22:42 -040027
James E. Blair4886cc12012-07-18 15:39:41 -070028import git
Morgan Fainberg293f7f82016-05-30 14:01:22 -070029from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040030import testtools
James E. Blairb0fcae42012-07-17 11:12:10 -070031
Maru Newby3fe5f852015-01-13 04:22:14 +000032import zuul.change_matcher
James E. Blairb0fcae42012-07-17 11:12:10 -070033import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080034import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040035import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070036
Maru Newby3fe5f852015-01-13 04:22:14 +000037from tests.base import (
Maru Newby3fe5f852015-01-13 04:22:14 +000038 ZuulTestCase,
39 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070040 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000041)
James E. Blairb0fcae42012-07-17 11:12:10 -070042
James E. Blairb0fcae42012-07-17 11:12:10 -070043
Clark Boylanb640e052014-04-03 16:41:46 -070044class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070045 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010046
Paul Belanger174a8272017-03-14 13:20:10 -040047 def test_jobs_executed(self):
48 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070049
James E. Blairb0fcae42012-07-17 11:12:10 -070050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070051 A.addApproval('code-review', 2)
52 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070053 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040054 self.assertEqual(self.getJobFromHistory('project-merge').result,
55 'SUCCESS')
56 self.assertEqual(self.getJobFromHistory('project-test1').result,
57 'SUCCESS')
58 self.assertEqual(self.getJobFromHistory('project-test2').result,
59 'SUCCESS')
60 self.assertEqual(A.data['status'], 'MERGED')
61 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070062 self.assertEqual(self.getJobFromHistory('project-test1').node,
63 'image1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080064 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070065
James E. Blair552b54f2016-07-22 13:55:32 -070066 # TODOv3(jeblair): we may want to report stats by tenant (also?).
James E. Blair66eeebf2013-07-27 17:44:32 -070067 self.assertReportedStat('gerrit.event.comment-added', value='1|c')
68 self.assertReportedStat('zuul.pipeline.gate.current_changes',
69 value='1|g')
70 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
71 kind='ms')
72 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
73 value='1|c')
74 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
75 self.assertReportedStat('zuul.pipeline.gate.total_changes',
76 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070077 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070078 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070079 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070080 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070081
James E. Blair5821bd92015-09-16 08:48:15 -070082 for build in self.builds:
83 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
84
James E. Blair3cb10702013-08-24 08:56:03 -070085 def test_initial_pipeline_gauges(self):
86 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070087 self.assertReportedStat('zuul.pipeline.gate.current_changes',
88 value='0|g')
89 self.assertReportedStat('zuul.pipeline.check.current_changes',
90 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070091
James E. Blair34776ee2016-08-25 13:53:54 -070092 def test_job_branch(self):
93 "Test the correct variant of a job runs on a branch"
94 self.create_branch('org/project', 'stable')
95 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
96 A.addApproval('code-review', 2)
97 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
98 self.waitUntilSettled()
99 self.assertEqual(self.getJobFromHistory('project-test1').result,
100 'SUCCESS')
101 self.assertEqual(self.getJobFromHistory('project-test2').result,
102 'SUCCESS')
103 self.assertEqual(A.data['status'], 'MERGED')
104 self.assertEqual(A.reported, 2,
105 "A should report start and success")
106 self.assertIn('gate', A.messages[1],
107 "A should transit gate")
108 self.assertEqual(self.getJobFromHistory('project-test1').node,
109 'image2')
110
James E. Blairb0fcae42012-07-17 11:12:10 -0700111 def test_parallel_changes(self):
112 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700113
Paul Belanger174a8272017-03-14 13:20:10 -0400114 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
116 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
117 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700118 A.addApproval('code-review', 2)
119 B.addApproval('code-review', 2)
120 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700121
James E. Blair8b5408c2016-08-08 15:37:46 -0700122 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
123 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
124 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700125
126 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400127 self.assertEqual(len(self.builds), 1)
128 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700129 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700130
Paul Belanger174a8272017-03-14 13:20:10 -0400131 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700132 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400133 self.assertEqual(len(self.builds), 3)
134 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700135 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400136 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700137 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400138 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700139 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700140
Paul Belanger174a8272017-03-14 13:20:10 -0400141 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700142 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400143 self.assertEqual(len(self.builds), 5)
144 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700145 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400146 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700147 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700148
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400149 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700150 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400151 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700152 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700153
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400154 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700155 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700156
Paul Belanger174a8272017-03-14 13:20:10 -0400157 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700158 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400159 self.assertEqual(len(self.builds), 6)
160 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700161 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400162 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700164
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400165 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700166 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400167 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700168 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700169
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400170 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700171 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400172 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700173 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700174
Paul Belanger174a8272017-03-14 13:20:10 -0400175 self.executor_server.hold_jobs_in_build = False
176 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700177 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400178 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700179
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400180 self.assertEqual(len(self.history), 9)
181 self.assertEqual(A.data['status'], 'MERGED')
182 self.assertEqual(B.data['status'], 'MERGED')
183 self.assertEqual(C.data['status'], 'MERGED')
184 self.assertEqual(A.reported, 2)
185 self.assertEqual(B.reported, 2)
186 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700187
188 def test_failed_changes(self):
189 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400190 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700191
James E. Blairb02a3bb2012-07-30 17:49:55 -0700192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
193 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700194 A.addApproval('code-review', 2)
195 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700196
Paul Belanger174a8272017-03-14 13:20:10 -0400197 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700198
James E. Blair8b5408c2016-08-08 15:37:46 -0700199 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
200 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700201 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700202 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400203
Paul Belanger174a8272017-03-14 13:20:10 -0400204 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400205 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700206 # A/project-merge is complete
207 self.assertBuilds([
208 dict(name='project-test1', changes='1,1'),
209 dict(name='project-test2', changes='1,1'),
210 dict(name='project-merge', changes='1,1 2,1'),
211 ])
James E. Blaire2819012013-06-28 17:17:26 -0400212
Paul Belanger174a8272017-03-14 13:20:10 -0400213 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400214 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700215 # A/project-merge is complete
216 # B/project-merge is complete
217 self.assertBuilds([
218 dict(name='project-test1', changes='1,1'),
219 dict(name='project-test2', changes='1,1'),
220 dict(name='project-test1', changes='1,1 2,1'),
221 dict(name='project-test2', changes='1,1 2,1'),
222 ])
223
224 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400225 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700226 self.builds[0].release()
227 self.waitUntilSettled()
228
229 self.orderedRelease()
230 self.assertHistory([
231 dict(name='project-merge', result='SUCCESS', changes='1,1'),
232 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
233 dict(name='project-test1', result='FAILURE', changes='1,1'),
234 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
235 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
236 dict(name='project-test2', result='SUCCESS', changes='1,1'),
237 dict(name='project-merge', result='SUCCESS', changes='2,1'),
238 dict(name='project-test1', result='SUCCESS', changes='2,1'),
239 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700240 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700241
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400242 self.assertEqual(A.data['status'], 'NEW')
243 self.assertEqual(B.data['status'], 'MERGED')
244 self.assertEqual(A.reported, 2)
245 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700246
247 def test_independent_queues(self):
248 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700249
Paul Belanger174a8272017-03-14 13:20:10 -0400250 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900251 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700252 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
253 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700254 A.addApproval('code-review', 2)
255 B.addApproval('code-review', 2)
256 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700257
James E. Blair8b5408c2016-08-08 15:37:46 -0700258 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800259 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700260
James E. Blairf59f3cf2017-02-19 14:50:26 -0800261 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
262 self.waitUntilSettled()
263
264 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700265 self.waitUntilSettled()
266
267 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700268 self.assertBuilds([
269 dict(name='project-merge', changes='1,1'),
270 dict(name='project-merge', changes='2,1'),
271 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700272
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700273 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700274 self.builds[0].release()
275 self.waitUntilSettled()
276 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700277 self.waitUntilSettled()
278 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400279 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700280 self.waitUntilSettled()
281
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700282 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700283 self.assertBuilds([
284 dict(name='project-test1', changes='1,1'),
285 dict(name='project-test2', changes='1,1'),
286 dict(name='project-test1', changes='2,1'),
287 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500288 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700289 dict(name='project-test1', changes='2,1 3,1'),
290 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800291 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700292 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293
James E. Blair5f0d49e2016-08-19 09:36:50 -0700294 self.orderedRelease()
295 self.assertHistory([
296 dict(name='project-merge', result='SUCCESS', changes='1,1'),
297 dict(name='project-merge', result='SUCCESS', changes='2,1'),
298 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
299 dict(name='project-test1', result='SUCCESS', changes='1,1'),
300 dict(name='project-test2', result='SUCCESS', changes='1,1'),
301 dict(name='project-test1', result='SUCCESS', changes='2,1'),
302 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500303 dict(
304 name='project1-project2-integration',
305 result='SUCCESS',
306 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700307 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
308 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800309 dict(name='project1-project2-integration',
310 result='SUCCESS',
311 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700312 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700313
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400314 self.assertEqual(A.data['status'], 'MERGED')
315 self.assertEqual(B.data['status'], 'MERGED')
316 self.assertEqual(C.data['status'], 'MERGED')
317 self.assertEqual(A.reported, 2)
318 self.assertEqual(B.reported, 2)
319 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700320
321 def test_failed_change_at_head(self):
322 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700323
Paul Belanger174a8272017-03-14 13:20:10 -0400324 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700325 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
326 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
327 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700328 A.addApproval('code-review', 2)
329 B.addApproval('code-review', 2)
330 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700331
Paul Belanger174a8272017-03-14 13:20:10 -0400332 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700333
James E. Blair8b5408c2016-08-08 15:37:46 -0700334 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
335 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
336 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700337
338 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700339
James E. Blairb536ecc2016-08-31 10:11:42 -0700340 self.assertBuilds([
341 dict(name='project-merge', changes='1,1'),
342 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700343
Paul Belanger174a8272017-03-14 13:20:10 -0400344 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400346 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700347 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400348 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700349 self.waitUntilSettled()
350
James E. Blairb536ecc2016-08-31 10:11:42 -0700351 self.assertBuilds([
352 dict(name='project-test1', changes='1,1'),
353 dict(name='project-test2', changes='1,1'),
354 dict(name='project-test1', changes='1,1 2,1'),
355 dict(name='project-test2', changes='1,1 2,1'),
356 dict(name='project-test1', changes='1,1 2,1 3,1'),
357 dict(name='project-test2', changes='1,1 2,1 3,1'),
358 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700359
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400360 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700361 self.waitUntilSettled()
362
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400363 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700364 self.assertBuilds([
365 dict(name='project-test2', changes='1,1'),
366 dict(name='project-merge', changes='2,1'),
367 ])
368 # Unordered history comparison because the aborts can finish
369 # in any order.
370 self.assertHistory([
371 dict(name='project-merge', result='SUCCESS',
372 changes='1,1'),
373 dict(name='project-merge', result='SUCCESS',
374 changes='1,1 2,1'),
375 dict(name='project-merge', result='SUCCESS',
376 changes='1,1 2,1 3,1'),
377 dict(name='project-test1', result='FAILURE',
378 changes='1,1'),
379 dict(name='project-test1', result='ABORTED',
380 changes='1,1 2,1'),
381 dict(name='project-test2', result='ABORTED',
382 changes='1,1 2,1'),
383 dict(name='project-test1', result='ABORTED',
384 changes='1,1 2,1 3,1'),
385 dict(name='project-test2', result='ABORTED',
386 changes='1,1 2,1 3,1'),
387 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700388
Paul Belanger174a8272017-03-14 13:20:10 -0400389 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700390 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400391 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700392 self.waitUntilSettled()
393 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700394
James E. Blairb536ecc2016-08-31 10:11:42 -0700395 self.assertBuilds([])
396 self.assertHistory([
397 dict(name='project-merge', result='SUCCESS',
398 changes='1,1'),
399 dict(name='project-merge', result='SUCCESS',
400 changes='1,1 2,1'),
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1 2,1 3,1'),
403 dict(name='project-test1', result='FAILURE',
404 changes='1,1'),
405 dict(name='project-test1', result='ABORTED',
406 changes='1,1 2,1'),
407 dict(name='project-test2', result='ABORTED',
408 changes='1,1 2,1'),
409 dict(name='project-test1', result='ABORTED',
410 changes='1,1 2,1 3,1'),
411 dict(name='project-test2', result='ABORTED',
412 changes='1,1 2,1 3,1'),
413 dict(name='project-merge', result='SUCCESS',
414 changes='2,1'),
415 dict(name='project-merge', result='SUCCESS',
416 changes='2,1 3,1'),
417 dict(name='project-test2', result='SUCCESS',
418 changes='1,1'),
419 dict(name='project-test1', result='SUCCESS',
420 changes='2,1'),
421 dict(name='project-test2', result='SUCCESS',
422 changes='2,1'),
423 dict(name='project-test1', result='SUCCESS',
424 changes='2,1 3,1'),
425 dict(name='project-test2', result='SUCCESS',
426 changes='2,1 3,1'),
427 ], ordered=False)
428
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400429 self.assertEqual(A.data['status'], 'NEW')
430 self.assertEqual(B.data['status'], 'MERGED')
431 self.assertEqual(C.data['status'], 'MERGED')
432 self.assertEqual(A.reported, 2)
433 self.assertEqual(B.reported, 2)
434 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700435
James E. Blair0aac4872013-08-23 14:02:38 -0700436 def test_failed_change_in_middle(self):
437 "Test a failed change in the middle of the queue"
438
Paul Belanger174a8272017-03-14 13:20:10 -0400439 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
441 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
442 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700443 A.addApproval('code-review', 2)
444 B.addApproval('code-review', 2)
445 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700446
Paul Belanger174a8272017-03-14 13:20:10 -0400447 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700448
James E. Blair8b5408c2016-08-08 15:37:46 -0700449 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
450 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
451 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700452
453 self.waitUntilSettled()
454
Paul Belanger174a8272017-03-14 13:20:10 -0400455 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700456 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400457 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700458 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400459 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700460 self.waitUntilSettled()
461
462 self.assertEqual(len(self.builds), 6)
463 self.assertEqual(self.builds[0].name, 'project-test1')
464 self.assertEqual(self.builds[1].name, 'project-test2')
465 self.assertEqual(self.builds[2].name, 'project-test1')
466 self.assertEqual(self.builds[3].name, 'project-test2')
467 self.assertEqual(self.builds[4].name, 'project-test1')
468 self.assertEqual(self.builds[5].name, 'project-test2')
469
470 self.release(self.builds[2])
471 self.waitUntilSettled()
472
James E. Blair972e3c72013-08-29 12:04:55 -0700473 # project-test1 and project-test2 for A
474 # project-test2 for B
475 # project-merge for C (without B)
476 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700477 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
478
Paul Belanger174a8272017-03-14 13:20:10 -0400479 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700480 self.waitUntilSettled()
481
482 # project-test1 and project-test2 for A
483 # project-test2 for B
484 # project-test1 and project-test2 for C
485 self.assertEqual(len(self.builds), 5)
486
Paul Belanger2e2a0492016-10-30 18:33:37 -0400487 tenant = self.sched.abide.tenants.get('tenant-one')
488 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700489 builds = items[0].current_build_set.getBuilds()
490 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
491 self.assertEqual(self.countJobResults(builds, None), 2)
492 builds = items[1].current_build_set.getBuilds()
493 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
494 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
495 self.assertEqual(self.countJobResults(builds, None), 1)
496 builds = items[2].current_build_set.getBuilds()
497 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700498 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700499
Paul Belanger174a8272017-03-14 13:20:10 -0400500 self.executor_server.hold_jobs_in_build = False
501 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700502 self.waitUntilSettled()
503
504 self.assertEqual(len(self.builds), 0)
505 self.assertEqual(len(self.history), 12)
506 self.assertEqual(A.data['status'], 'MERGED')
507 self.assertEqual(B.data['status'], 'NEW')
508 self.assertEqual(C.data['status'], 'MERGED')
509 self.assertEqual(A.reported, 2)
510 self.assertEqual(B.reported, 2)
511 self.assertEqual(C.reported, 2)
512
Clint Byrum8897dca2017-05-10 19:14:19 -0700513 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blaird466dc42012-07-31 10:42:56 -0700514 def test_failed_change_at_head_with_queue(self):
515 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700516
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700517 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700518 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
519 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
520 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700521 A.addApproval('code-review', 2)
522 B.addApproval('code-review', 2)
523 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700524
Paul Belanger174a8272017-03-14 13:20:10 -0400525 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700526
James E. Blair8b5408c2016-08-08 15:37:46 -0700527 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
528 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
529 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700530
531 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700532 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400533 self.assertEqual(len(self.builds), 0)
534 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700535 self.assertEqual(queue[0].name, b'executor:execute')
536 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800537 self.assertEqual(job_args['job'], 'project-merge')
538 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700539
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700540 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700541 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700542 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700543 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700544 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700545 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700546 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700547
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400548 self.assertEqual(len(self.builds), 0)
549 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800550
551 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700552 json.loads(queue[0].arguments.decode('utf8'))['job'],
553 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800554 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700555 json.loads(queue[1].arguments.decode('utf8'))['job'],
556 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800557 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700558 json.loads(queue[2].arguments.decode('utf8'))['job'],
559 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800560 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700561 json.loads(queue[3].arguments.decode('utf8'))['job'],
562 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800563 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700564 json.loads(queue[4].arguments.decode('utf8'))['job'],
565 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800566 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700567 json.loads(queue[5].arguments.decode('utf8'))['job'],
568 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700569
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700570 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700571 self.waitUntilSettled()
572
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400573 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700574 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400575 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
576 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700577
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700578 self.gearman_server.hold_jobs_in_queue = False
579 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700580 self.waitUntilSettled()
581
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400582 self.assertEqual(len(self.builds), 0)
583 self.assertEqual(len(self.history), 11)
584 self.assertEqual(A.data['status'], 'NEW')
585 self.assertEqual(B.data['status'], 'MERGED')
586 self.assertEqual(C.data['status'], 'MERGED')
587 self.assertEqual(A.reported, 2)
588 self.assertEqual(B.reported, 2)
589 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700590
James E. Blairec056492016-07-22 09:45:56 -0700591 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700592 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400593 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700595 A.addApproval('code-review', 2)
596 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700597 self.waitUntilSettled()
598 time.sleep(2)
599
600 data = json.loads(self.sched.formatStatusJSON())
601 found_job = None
602 for pipeline in data['pipelines']:
603 if pipeline['name'] != 'gate':
604 continue
605 for queue in pipeline['change_queues']:
606 for head in queue['heads']:
607 for item in head:
608 for job in item['jobs']:
609 if job['name'] == 'project-merge':
610 found_job = job
611 break
612
613 self.assertIsNotNone(found_job)
614 if iteration == 1:
615 self.assertIsNotNone(found_job['estimated_time'])
616 self.assertIsNone(found_job['remaining_time'])
617 else:
618 self.assertIsNotNone(found_job['estimated_time'])
619 self.assertTrue(found_job['estimated_time'] >= 2)
620 self.assertIsNotNone(found_job['remaining_time'])
621
Paul Belanger174a8272017-03-14 13:20:10 -0400622 self.executor_server.hold_jobs_in_build = False
623 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700624 self.waitUntilSettled()
625
James E. Blairec056492016-07-22 09:45:56 -0700626 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700627 def test_time_database(self):
628 "Test the time database"
629
630 self._test_time_database(1)
631 self._test_time_database(2)
632
James E. Blairfef71632013-09-23 11:15:47 -0700633 def test_two_failed_changes_at_head(self):
634 "Test that changes are reparented correctly if 2 fail at head"
635
Paul Belanger174a8272017-03-14 13:20:10 -0400636 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700637 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
638 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
639 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700640 A.addApproval('code-review', 2)
641 B.addApproval('code-review', 2)
642 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700643
Paul Belanger174a8272017-03-14 13:20:10 -0400644 self.executor_server.failJob('project-test1', A)
645 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700646
James E. Blair8b5408c2016-08-08 15:37:46 -0700647 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
648 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
649 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700650 self.waitUntilSettled()
651
Paul Belanger174a8272017-03-14 13:20:10 -0400652 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700653 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400654 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700655 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400656 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700657 self.waitUntilSettled()
658
659 self.assertEqual(len(self.builds), 6)
660 self.assertEqual(self.builds[0].name, 'project-test1')
661 self.assertEqual(self.builds[1].name, 'project-test2')
662 self.assertEqual(self.builds[2].name, 'project-test1')
663 self.assertEqual(self.builds[3].name, 'project-test2')
664 self.assertEqual(self.builds[4].name, 'project-test1')
665 self.assertEqual(self.builds[5].name, 'project-test2')
666
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400667 self.assertTrue(self.builds[0].hasChanges(A))
668 self.assertTrue(self.builds[2].hasChanges(A))
669 self.assertTrue(self.builds[2].hasChanges(B))
670 self.assertTrue(self.builds[4].hasChanges(A))
671 self.assertTrue(self.builds[4].hasChanges(B))
672 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700673
674 # Fail change B first
675 self.release(self.builds[2])
676 self.waitUntilSettled()
677
678 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400679 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700680 self.waitUntilSettled()
681
682 self.assertEqual(len(self.builds), 5)
683 self.assertEqual(self.builds[0].name, 'project-test1')
684 self.assertEqual(self.builds[1].name, 'project-test2')
685 self.assertEqual(self.builds[2].name, 'project-test2')
686 self.assertEqual(self.builds[3].name, 'project-test1')
687 self.assertEqual(self.builds[4].name, 'project-test2')
688
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400689 self.assertTrue(self.builds[1].hasChanges(A))
690 self.assertTrue(self.builds[2].hasChanges(A))
691 self.assertTrue(self.builds[2].hasChanges(B))
692 self.assertTrue(self.builds[4].hasChanges(A))
693 self.assertFalse(self.builds[4].hasChanges(B))
694 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700695
696 # Finish running all passing jobs for change A
697 self.release(self.builds[1])
698 self.waitUntilSettled()
699 # Fail and report change A
700 self.release(self.builds[0])
701 self.waitUntilSettled()
702
703 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400704 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700705 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400706 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700707 self.waitUntilSettled()
708
709 self.assertEqual(len(self.builds), 4)
710 self.assertEqual(self.builds[0].name, 'project-test1') # B
711 self.assertEqual(self.builds[1].name, 'project-test2') # B
712 self.assertEqual(self.builds[2].name, 'project-test1') # C
713 self.assertEqual(self.builds[3].name, 'project-test2') # C
714
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400715 self.assertFalse(self.builds[1].hasChanges(A))
716 self.assertTrue(self.builds[1].hasChanges(B))
717 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700718
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400719 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700720 # After A failed and B and C restarted, B should be back in
721 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400722 self.assertTrue(self.builds[2].hasChanges(B))
723 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700724
Paul Belanger174a8272017-03-14 13:20:10 -0400725 self.executor_server.hold_jobs_in_build = False
726 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700727 self.waitUntilSettled()
728
729 self.assertEqual(len(self.builds), 0)
730 self.assertEqual(len(self.history), 21)
731 self.assertEqual(A.data['status'], 'NEW')
732 self.assertEqual(B.data['status'], 'NEW')
733 self.assertEqual(C.data['status'], 'MERGED')
734 self.assertEqual(A.reported, 2)
735 self.assertEqual(B.reported, 2)
736 self.assertEqual(C.reported, 2)
737
James E. Blair8c803f82012-07-31 16:25:42 -0700738 def test_patch_order(self):
739 "Test that dependent patches are tested in the right order"
740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
741 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
742 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700743 A.addApproval('code-review', 2)
744 B.addApproval('code-review', 2)
745 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700746
747 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
748 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
749 M2.setMerged()
750 M1.setMerged()
751
752 # C -> B -> A -> M1 -> M2
753 # M2 is here to make sure it is never queried. If it is, it
754 # means zuul is walking down the entire history of merged
755 # changes.
756
757 C.setDependsOn(B, 1)
758 B.setDependsOn(A, 1)
759 A.setDependsOn(M1, 1)
760 M1.setDependsOn(M2, 1)
761
James E. Blair8b5408c2016-08-08 15:37:46 -0700762 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700763
764 self.waitUntilSettled()
765
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400766 self.assertEqual(A.data['status'], 'NEW')
767 self.assertEqual(B.data['status'], 'NEW')
768 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700769
James E. Blair8b5408c2016-08-08 15:37:46 -0700770 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
771 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700772
773 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400774 self.assertEqual(M2.queried, 0)
775 self.assertEqual(A.data['status'], 'MERGED')
776 self.assertEqual(B.data['status'], 'MERGED')
777 self.assertEqual(C.data['status'], 'MERGED')
778 self.assertEqual(A.reported, 2)
779 self.assertEqual(B.reported, 2)
780 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700781
James E. Blair063672f2015-01-29 13:09:12 -0800782 def test_needed_changes_enqueue(self):
783 "Test that a needed change is enqueued ahead"
784 # A Given a git tree like this, if we enqueue
785 # / \ change C, we should walk up and down the tree
786 # B G and enqueue changes in the order ABCDEFG.
787 # /|\ This is also the order that you would get if
788 # *C E F you enqueued changes in the order ABCDEFG, so
789 # / the ordering is stable across re-enqueue events.
790 # D
791
792 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
793 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
794 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
795 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
796 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
797 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
798 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
799 B.setDependsOn(A, 1)
800 C.setDependsOn(B, 1)
801 D.setDependsOn(C, 1)
802 E.setDependsOn(B, 1)
803 F.setDependsOn(B, 1)
804 G.setDependsOn(A, 1)
805
James E. Blair8b5408c2016-08-08 15:37:46 -0700806 A.addApproval('code-review', 2)
807 B.addApproval('code-review', 2)
808 C.addApproval('code-review', 2)
809 D.addApproval('code-review', 2)
810 E.addApproval('code-review', 2)
811 F.addApproval('code-review', 2)
812 G.addApproval('code-review', 2)
813 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800814
815 self.waitUntilSettled()
816
817 self.assertEqual(A.data['status'], 'NEW')
818 self.assertEqual(B.data['status'], 'NEW')
819 self.assertEqual(C.data['status'], 'NEW')
820 self.assertEqual(D.data['status'], 'NEW')
821 self.assertEqual(E.data['status'], 'NEW')
822 self.assertEqual(F.data['status'], 'NEW')
823 self.assertEqual(G.data['status'], 'NEW')
824
825 # We're about to add approvals to changes without adding the
826 # triggering events to Zuul, so that we can be sure that it is
827 # enqueing the changes based on dependencies, not because of
828 # triggering events. Since it will have the changes cached
829 # already (without approvals), we need to clear the cache
830 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400831 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100832 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800833
Paul Belanger174a8272017-03-14 13:20:10 -0400834 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700835 A.addApproval('approved', 1)
836 B.addApproval('approved', 1)
837 D.addApproval('approved', 1)
838 E.addApproval('approved', 1)
839 F.addApproval('approved', 1)
840 G.addApproval('approved', 1)
841 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800842
843 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400844 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800845 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400846 self.executor_server.hold_jobs_in_build = False
847 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800848 self.waitUntilSettled()
849
850 self.assertEqual(A.data['status'], 'MERGED')
851 self.assertEqual(B.data['status'], 'MERGED')
852 self.assertEqual(C.data['status'], 'MERGED')
853 self.assertEqual(D.data['status'], 'MERGED')
854 self.assertEqual(E.data['status'], 'MERGED')
855 self.assertEqual(F.data['status'], 'MERGED')
856 self.assertEqual(G.data['status'], 'MERGED')
857 self.assertEqual(A.reported, 2)
858 self.assertEqual(B.reported, 2)
859 self.assertEqual(C.reported, 2)
860 self.assertEqual(D.reported, 2)
861 self.assertEqual(E.reported, 2)
862 self.assertEqual(F.reported, 2)
863 self.assertEqual(G.reported, 2)
864 self.assertEqual(self.history[6].changes,
865 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
866
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100867 def test_source_cache(self):
868 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400869 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700870
871 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
872 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
873 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700874 A.addApproval('code-review', 2)
875 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700876
877 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
878 M1.setMerged()
879
880 B.setDependsOn(A, 1)
881 A.setDependsOn(M1, 1)
882
James E. Blair8b5408c2016-08-08 15:37:46 -0700883 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700884 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
885
886 self.waitUntilSettled()
887
888 for build in self.builds:
889 if build.parameters['ZUUL_PIPELINE'] == 'check':
890 build.release()
891 self.waitUntilSettled()
892 for build in self.builds:
893 if build.parameters['ZUUL_PIPELINE'] == 'check':
894 build.release()
895 self.waitUntilSettled()
896
James E. Blair8b5408c2016-08-08 15:37:46 -0700897 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700898 self.waitUntilSettled()
899
Joshua Hesketh352264b2015-08-11 23:42:08 +1000900 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700901 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000902 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700903
Paul Belanger174a8272017-03-14 13:20:10 -0400904 self.executor_server.hold_jobs_in_build = False
905 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700906 self.waitUntilSettled()
907
908 self.assertEqual(A.data['status'], 'MERGED')
909 self.assertEqual(B.data['status'], 'MERGED')
910 self.assertEqual(A.queried, 2) # Initial and isMerged
911 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
912
James E. Blair8c803f82012-07-31 16:25:42 -0700913 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700914 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700915 # TODO: move to test_gerrit (this is a unit test!)
916 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400917 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700918 (trusted, project) = tenant.getProject('org/project')
919 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400920
921 # TODO(pabelanger): As we add more source / trigger APIs we should make
922 # it easier for users to create events for testing.
923 event = zuul.model.TriggerEvent()
924 event.trigger_name = 'gerrit'
925 event.change_number = '1'
926 event.patch_number = '2'
927
928 a = source.getChange(event)
929 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700930 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700931
James E. Blair8b5408c2016-08-08 15:37:46 -0700932 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400933 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700934 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700935
James E. Blair8b5408c2016-08-08 15:37:46 -0700936 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400937 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700938 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700939
Clint Byrum8897dca2017-05-10 19:14:19 -0700940 @skipIf(sys.version_info.major > 2, 'Fails on py3')
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700941 def test_project_merge_conflict(self):
942 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700943
944 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700945 A = self.fake_gerrit.addFakeChange('org/project',
946 'master', 'A',
947 files={'conflict': 'foo'})
948 B = self.fake_gerrit.addFakeChange('org/project',
949 'master', 'B',
950 files={'conflict': 'bar'})
951 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700952 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700953 A.addApproval('code-review', 2)
954 B.addApproval('code-review', 2)
955 C.addApproval('code-review', 2)
956 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
957 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
958 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700959 self.waitUntilSettled()
960
James E. Blair6736beb2013-07-11 15:18:15 -0700961 self.assertEqual(A.reported, 1)
962 self.assertEqual(B.reported, 1)
963 self.assertEqual(C.reported, 1)
964
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700965 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700966 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700967 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700968 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700969 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700970 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700971
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700972 self.gearman_server.hold_jobs_in_queue = False
973 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700974 self.waitUntilSettled()
975
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400976 self.assertEqual(A.data['status'], 'MERGED')
977 self.assertEqual(B.data['status'], 'NEW')
978 self.assertEqual(C.data['status'], 'MERGED')
979 self.assertEqual(A.reported, 2)
980 self.assertEqual(B.reported, 2)
981 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700982
983 self.assertHistory([
984 dict(name='project-merge', result='SUCCESS', changes='1,1'),
985 dict(name='project-test1', result='SUCCESS', changes='1,1'),
986 dict(name='project-test2', result='SUCCESS', changes='1,1'),
987 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
988 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
989 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
990 ], ordered=False)
991
Clint Byrum8897dca2017-05-10 19:14:19 -0700992 @skipIf(sys.version_info.major > 2, 'Fails on py3')
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700993 def test_delayed_merge_conflict(self):
994 "Test that delayed check merge conflicts are handled properly"
995
996 # Hold jobs in the gearman queue so that we can test whether
997 # the executor returns a merge failure after the scheduler has
998 # successfully merged.
999 self.gearman_server.hold_jobs_in_queue = True
1000 A = self.fake_gerrit.addFakeChange('org/project',
1001 'master', 'A',
1002 files={'conflict': 'foo'})
1003 B = self.fake_gerrit.addFakeChange('org/project',
1004 'master', 'B',
1005 files={'conflict': 'bar'})
1006 C = self.fake_gerrit.addFakeChange('org/project',
1007 'master', 'C')
1008 C.setDependsOn(B, 1)
1009
1010 # A enters the gate queue; B and C enter the check queue
1011 A.addApproval('code-review', 2)
1012 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1013 self.waitUntilSettled()
1014 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1015 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1016 self.waitUntilSettled()
1017
1018 self.assertEqual(A.reported, 1)
1019 self.assertEqual(B.reported, 0) # Check does not report start
1020 self.assertEqual(C.reported, 0) # Check does not report start
1021
1022 # A merges while B and C are queued in check
1023 # Release A project-merge
1024 queue = self.gearman_server.getQueue()
1025 self.release(queue[0])
1026 self.waitUntilSettled()
1027
1028 # Release A project-test*
1029 # gate has higher precedence, so A's test jobs are added in
1030 # front of the merge jobs for B and C
1031 queue = self.gearman_server.getQueue()
1032 self.release(queue[0])
1033 self.release(queue[1])
1034 self.waitUntilSettled()
1035
1036 self.assertEqual(A.data['status'], 'MERGED')
1037 self.assertEqual(B.data['status'], 'NEW')
1038 self.assertEqual(C.data['status'], 'NEW')
1039 self.assertEqual(A.reported, 2)
1040 self.assertEqual(B.reported, 0)
1041 self.assertEqual(C.reported, 0)
1042 self.assertHistory([
1043 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1044 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1045 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1046 ], ordered=False)
1047
1048 # B and C report merge conflicts
1049 # Release B project-merge
1050 queue = self.gearman_server.getQueue()
1051 self.release(queue[0])
1052 self.waitUntilSettled()
1053
1054 # Release C
1055 self.gearman_server.hold_jobs_in_queue = False
1056 self.gearman_server.release()
1057 self.waitUntilSettled()
1058
1059 self.assertEqual(A.data['status'], 'MERGED')
1060 self.assertEqual(B.data['status'], 'NEW')
1061 self.assertEqual(C.data['status'], 'NEW')
1062 self.assertEqual(A.reported, 2)
1063 self.assertEqual(B.reported, 1)
1064 self.assertEqual(C.reported, 1)
1065
1066 self.assertHistory([
1067 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1068 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1069 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1070 dict(name='project-merge', result='MERGER_FAILURE', changes='2,1'),
1071 dict(name='project-merge', result='MERGER_FAILURE',
1072 changes='2,1 3,1'),
1073 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001074
James E. Blairdaabed22012-08-15 15:38:57 -07001075 def test_post(self):
1076 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001077
Zhongyue Luo5d556072012-09-21 02:00:47 +09001078 e = {
1079 "type": "ref-updated",
1080 "submitter": {
1081 "name": "User Name",
1082 },
1083 "refUpdate": {
1084 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1085 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1086 "refName": "master",
1087 "project": "org/project",
1088 }
1089 }
James E. Blairdaabed22012-08-15 15:38:57 -07001090 self.fake_gerrit.addEvent(e)
1091 self.waitUntilSettled()
1092
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001093 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001094 self.assertEqual(len(self.history), 1)
1095 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001096
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001097 def test_post_ignore_deletes(self):
1098 "Test that deleting refs does not trigger post jobs"
1099
1100 e = {
1101 "type": "ref-updated",
1102 "submitter": {
1103 "name": "User Name",
1104 },
1105 "refUpdate": {
1106 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1107 "newRev": "0000000000000000000000000000000000000000",
1108 "refName": "master",
1109 "project": "org/project",
1110 }
1111 }
1112 self.fake_gerrit.addEvent(e)
1113 self.waitUntilSettled()
1114
1115 job_names = [x.name for x in self.history]
1116 self.assertEqual(len(self.history), 0)
1117 self.assertNotIn('project-post', job_names)
1118
James E. Blaircc324192017-04-19 14:36:08 -07001119 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001120 def test_post_ignore_deletes_negative(self):
1121 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001122 e = {
1123 "type": "ref-updated",
1124 "submitter": {
1125 "name": "User Name",
1126 },
1127 "refUpdate": {
1128 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1129 "newRev": "0000000000000000000000000000000000000000",
1130 "refName": "master",
1131 "project": "org/project",
1132 }
1133 }
1134 self.fake_gerrit.addEvent(e)
1135 self.waitUntilSettled()
1136
1137 job_names = [x.name for x in self.history]
1138 self.assertEqual(len(self.history), 1)
1139 self.assertIn('project-post', job_names)
1140
James E. Blairec056492016-07-22 09:45:56 -07001141 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001142 def test_build_configuration_branch_interaction(self):
1143 "Test that switching between branches works"
1144 self.test_build_configuration()
1145 self.test_build_configuration_branch()
1146 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001147 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001148 repo = git.Repo(path)
1149 repo.heads.master.commit = repo.commit('init')
1150 self.test_build_configuration()
1151
James E. Blaircaec0c52012-08-22 14:52:22 -07001152 def test_dependent_changes_dequeue(self):
1153 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001154
James E. Blaircaec0c52012-08-22 14:52:22 -07001155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1156 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1157 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001158 A.addApproval('code-review', 2)
1159 B.addApproval('code-review', 2)
1160 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001161
1162 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1163 M1.setMerged()
1164
1165 # C -> B -> A -> M1
1166
1167 C.setDependsOn(B, 1)
1168 B.setDependsOn(A, 1)
1169 A.setDependsOn(M1, 1)
1170
Paul Belanger174a8272017-03-14 13:20:10 -04001171 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001172
James E. Blair8b5408c2016-08-08 15:37:46 -07001173 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1174 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1175 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001176
1177 self.waitUntilSettled()
1178
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001179 self.assertEqual(A.data['status'], 'NEW')
1180 self.assertEqual(A.reported, 2)
1181 self.assertEqual(B.data['status'], 'NEW')
1182 self.assertEqual(B.reported, 2)
1183 self.assertEqual(C.data['status'], 'NEW')
1184 self.assertEqual(C.reported, 2)
1185 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001186
James E. Blair972e3c72013-08-29 12:04:55 -07001187 def test_failing_dependent_changes(self):
1188 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001189 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001190 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1191 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1192 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1193 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1194 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001195 A.addApproval('code-review', 2)
1196 B.addApproval('code-review', 2)
1197 C.addApproval('code-review', 2)
1198 D.addApproval('code-review', 2)
1199 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001200
1201 # E, D -> C -> B, A
1202
1203 D.setDependsOn(C, 1)
1204 C.setDependsOn(B, 1)
1205
Paul Belanger174a8272017-03-14 13:20:10 -04001206 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001207
James E. Blair8b5408c2016-08-08 15:37:46 -07001208 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1209 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1210 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1211 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1212 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001213
1214 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001215 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001216 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001217 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001218 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001219 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001220 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001221 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001222 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001223 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001224 self.waitUntilSettled()
1225
Paul Belanger174a8272017-03-14 13:20:10 -04001226 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001227 for build in self.builds:
1228 if build.parameters['ZUUL_CHANGE'] != '1':
1229 build.release()
1230 self.waitUntilSettled()
1231
Paul Belanger174a8272017-03-14 13:20:10 -04001232 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001233 self.waitUntilSettled()
1234
1235 self.assertEqual(A.data['status'], 'MERGED')
1236 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001237 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001238 self.assertEqual(B.data['status'], 'NEW')
1239 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001240 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001241 self.assertEqual(C.data['status'], 'NEW')
1242 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001243 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001244 self.assertEqual(D.data['status'], 'NEW')
1245 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001246 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001247 self.assertEqual(E.data['status'], 'MERGED')
1248 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001249 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001250 self.assertEqual(len(self.history), 18)
1251
James E. Blairec590122012-08-22 15:19:31 -07001252 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001253 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001254 # If it's dequeued more than once, we should see extra
1255 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001256
Paul Belanger174a8272017-03-14 13:20:10 -04001257 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001258 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1259 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1260 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001261 A.addApproval('code-review', 2)
1262 B.addApproval('code-review', 2)
1263 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001264
Paul Belanger174a8272017-03-14 13:20:10 -04001265 self.executor_server.failJob('project-test1', A)
1266 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001267
James E. Blair8b5408c2016-08-08 15:37:46 -07001268 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1269 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1270 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001271
1272 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001273
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001274 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001275 self.assertEqual(self.builds[0].name, 'project-merge')
1276 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001277
Paul Belanger174a8272017-03-14 13:20:10 -04001278 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001279 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001280 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001281 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001282 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001283 self.waitUntilSettled()
1284
James E. Blair6df67132017-04-19 13:53:20 -07001285 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001286 self.assertEqual(self.builds[0].name, 'project-test1')
1287 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001288 self.assertEqual(self.builds[2].name, 'project-test1')
1289 self.assertEqual(self.builds[3].name, 'project-test2')
1290 self.assertEqual(self.builds[4].name, 'project-test1')
1291 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001292
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001293 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001294 self.waitUntilSettled()
1295
James E. Blair6df67132017-04-19 13:53:20 -07001296 self.assertEqual(len(self.builds), 2) # test2, merge for B
1297 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001298
Paul Belanger174a8272017-03-14 13:20:10 -04001299 self.executor_server.hold_jobs_in_build = False
1300 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001301 self.waitUntilSettled()
1302
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001303 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001304 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001305
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001306 self.assertEqual(A.data['status'], 'NEW')
1307 self.assertEqual(B.data['status'], 'MERGED')
1308 self.assertEqual(C.data['status'], 'MERGED')
1309 self.assertEqual(A.reported, 2)
1310 self.assertEqual(B.reported, 2)
1311 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001312
James E. Blaircc020532017-04-19 13:04:44 -07001313 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001314 def test_nonvoting_job(self):
1315 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001316
James E. Blair4ec821f2012-08-23 15:28:28 -07001317 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1318 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001319 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001320 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001321 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001322
1323 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001324
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001325 self.assertEqual(A.data['status'], 'MERGED')
1326 self.assertEqual(A.reported, 2)
1327 self.assertEqual(
1328 self.getJobFromHistory('nonvoting-project-merge').result,
1329 'SUCCESS')
1330 self.assertEqual(
1331 self.getJobFromHistory('nonvoting-project-test1').result,
1332 'SUCCESS')
1333 self.assertEqual(
1334 self.getJobFromHistory('nonvoting-project-test2').result,
1335 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001336
James E. Blair5821bd92015-09-16 08:48:15 -07001337 for build in self.builds:
1338 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1339
James E. Blaire0487072012-08-29 17:38:31 -07001340 def test_check_queue_success(self):
1341 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001342
James E. Blaire0487072012-08-29 17:38:31 -07001343 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1344 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1345
1346 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001347
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001348 self.assertEqual(A.data['status'], 'NEW')
1349 self.assertEqual(A.reported, 1)
1350 self.assertEqual(self.getJobFromHistory('project-merge').result,
1351 'SUCCESS')
1352 self.assertEqual(self.getJobFromHistory('project-test1').result,
1353 'SUCCESS')
1354 self.assertEqual(self.getJobFromHistory('project-test2').result,
1355 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001356
1357 def test_check_queue_failure(self):
1358 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001359
James E. Blaire0487072012-08-29 17:38:31 -07001360 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001361 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001362 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1363
1364 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001365
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001366 self.assertEqual(A.data['status'], 'NEW')
1367 self.assertEqual(A.reported, 1)
1368 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001369 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001370 self.assertEqual(self.getJobFromHistory('project-test1').result,
1371 'SUCCESS')
1372 self.assertEqual(self.getJobFromHistory('project-test2').result,
1373 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001374
James E. Blair7cb84542017-04-19 13:35:05 -07001375 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001376 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001377 # This particular test does a large amount of merges and needs a little
1378 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001379 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001380 "test that dependent changes behind dequeued changes work"
1381 # This complicated test is a reproduction of a real life bug
1382 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001383
Paul Belanger174a8272017-03-14 13:20:10 -04001384 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001385 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1386 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1387 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1388 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1389 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1390 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1391 D.setDependsOn(C, 1)
1392 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001393 A.addApproval('code-review', 2)
1394 B.addApproval('code-review', 2)
1395 C.addApproval('code-review', 2)
1396 D.addApproval('code-review', 2)
1397 E.addApproval('code-review', 2)
1398 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001399
1400 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001401
1402 # Change object re-use in the gerrit trigger is hidden if
1403 # changes are added in quick succession; waiting makes it more
1404 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001405 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001406 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001407 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001408 self.waitUntilSettled()
1409
Paul Belanger174a8272017-03-14 13:20:10 -04001410 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001411 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001412 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001413 self.waitUntilSettled()
1414
James E. Blair8b5408c2016-08-08 15:37:46 -07001415 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001416 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001417 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001418 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001419 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001420 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001421 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001422 self.waitUntilSettled()
1423
Paul Belanger174a8272017-03-14 13:20:10 -04001424 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001425 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001426 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001427 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001428 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001429 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001430 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001431 self.waitUntilSettled()
1432
1433 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001434
1435 # Grab pointers to the jobs we want to release before
1436 # releasing any, because list indexes may change as
1437 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001438 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001439 a.release()
1440 b.release()
1441 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001442 self.waitUntilSettled()
1443
Paul Belanger174a8272017-03-14 13:20:10 -04001444 self.executor_server.hold_jobs_in_build = False
1445 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001446 self.waitUntilSettled()
1447
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001448 self.assertEqual(A.data['status'], 'NEW')
1449 self.assertEqual(B.data['status'], 'MERGED')
1450 self.assertEqual(C.data['status'], 'MERGED')
1451 self.assertEqual(D.data['status'], 'MERGED')
1452 self.assertEqual(E.data['status'], 'MERGED')
1453 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001454
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(A.reported, 2)
1456 self.assertEqual(B.reported, 2)
1457 self.assertEqual(C.reported, 2)
1458 self.assertEqual(D.reported, 2)
1459 self.assertEqual(E.reported, 2)
1460 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001461
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001462 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1463 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001464
1465 def test_merger_repack(self):
1466 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001467
James E. Blair05fed602012-09-07 12:45:24 -07001468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001469 A.addApproval('code-review', 2)
1470 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001471 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001472 self.assertEqual(self.getJobFromHistory('project-merge').result,
1473 'SUCCESS')
1474 self.assertEqual(self.getJobFromHistory('project-test1').result,
1475 'SUCCESS')
1476 self.assertEqual(self.getJobFromHistory('project-test2').result,
1477 'SUCCESS')
1478 self.assertEqual(A.data['status'], 'MERGED')
1479 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001480 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001481 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001482
James E. Blair2a535672017-04-27 12:03:15 -07001483 path = os.path.join(self.merger_src_root, "review.example.com",
1484 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001485 if os.path.exists(path):
1486 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001487 path = os.path.join(self.executor_src_root, "review.example.com",
1488 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001489 if os.path.exists(path):
1490 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001491
1492 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001493 A.addApproval('code-review', 2)
1494 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001495 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001496 self.assertEqual(self.getJobFromHistory('project-merge').result,
1497 'SUCCESS')
1498 self.assertEqual(self.getJobFromHistory('project-test1').result,
1499 'SUCCESS')
1500 self.assertEqual(self.getJobFromHistory('project-test2').result,
1501 'SUCCESS')
1502 self.assertEqual(A.data['status'], 'MERGED')
1503 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001504
James E. Blair4886f282012-11-15 09:27:33 -08001505 def test_merger_repack_large_change(self):
1506 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001507 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001508 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001509 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001510 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001511 url = self.fake_gerrit.getGitUrl(project)
James E. Blair2a535672017-04-27 12:03:15 -07001512 self.merge_server.merger._addProject('review.example.com',
1513 'org/project', url)
James E. Blair6df67132017-04-19 13:53:20 -07001514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001515 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001516 # TODOv3(jeblair): add hostname to upstream root
1517 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001518 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001519 path = os.path.join(self.merger_src_root, 'review.example.com',
1520 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001521 if os.path.exists(path):
1522 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001523 path = os.path.join(self.executor_src_root, 'review.example.com',
1524 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001525 if os.path.exists(path):
1526 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001527
James E. Blair8b5408c2016-08-08 15:37:46 -07001528 A.addApproval('code-review', 2)
1529 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001530 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001531 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001532 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001533 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001534 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001535 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001536 'SUCCESS')
1537 self.assertEqual(A.data['status'], 'MERGED')
1538 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001539
James E. Blair2fa50962013-01-30 21:50:41 -08001540 def test_new_patchset_dequeues_old(self):
1541 "Test that a new patchset causes the old to be dequeued"
1542 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001543 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001544 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1545 M.setMerged()
1546
1547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1548 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1549 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1550 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001551 A.addApproval('code-review', 2)
1552 B.addApproval('code-review', 2)
1553 C.addApproval('code-review', 2)
1554 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001555
1556 C.setDependsOn(B, 1)
1557 B.setDependsOn(A, 1)
1558 A.setDependsOn(M, 1)
1559
James E. Blair8b5408c2016-08-08 15:37:46 -07001560 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1561 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1562 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1563 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001564 self.waitUntilSettled()
1565
1566 B.addPatchset()
1567 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1568 self.waitUntilSettled()
1569
Paul Belanger174a8272017-03-14 13:20:10 -04001570 self.executor_server.hold_jobs_in_build = False
1571 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001572 self.waitUntilSettled()
1573
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001574 self.assertEqual(A.data['status'], 'MERGED')
1575 self.assertEqual(A.reported, 2)
1576 self.assertEqual(B.data['status'], 'NEW')
1577 self.assertEqual(B.reported, 2)
1578 self.assertEqual(C.data['status'], 'NEW')
1579 self.assertEqual(C.reported, 2)
1580 self.assertEqual(D.data['status'], 'MERGED')
1581 self.assertEqual(D.reported, 2)
1582 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001583
James E. Blairba437362015-02-07 11:41:52 -08001584 def test_new_patchset_check(self):
1585 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001586
Paul Belanger174a8272017-03-14 13:20:10 -04001587 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001588
1589 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001590 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001591 tenant = self.sched.abide.tenants.get('tenant-one')
1592 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001593
1594 # Add two git-dependent changes
1595 B.setDependsOn(A, 1)
1596 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1597 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001598 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1599 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001600
1601 # A live item, and a non-live/live pair
1602 items = check_pipeline.getAllItems()
1603 self.assertEqual(len(items), 3)
1604
1605 self.assertEqual(items[0].change.number, '1')
1606 self.assertEqual(items[0].change.patchset, '1')
1607 self.assertFalse(items[0].live)
1608
1609 self.assertEqual(items[1].change.number, '2')
1610 self.assertEqual(items[1].change.patchset, '1')
1611 self.assertTrue(items[1].live)
1612
1613 self.assertEqual(items[2].change.number, '1')
1614 self.assertEqual(items[2].change.patchset, '1')
1615 self.assertTrue(items[2].live)
1616
1617 # Add a new patchset to A
1618 A.addPatchset()
1619 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1620 self.waitUntilSettled()
1621
1622 # The live copy of A,1 should be gone, but the non-live and B
1623 # should continue, and we should have a new A,2
1624 items = check_pipeline.getAllItems()
1625 self.assertEqual(len(items), 3)
1626
1627 self.assertEqual(items[0].change.number, '1')
1628 self.assertEqual(items[0].change.patchset, '1')
1629 self.assertFalse(items[0].live)
1630
1631 self.assertEqual(items[1].change.number, '2')
1632 self.assertEqual(items[1].change.patchset, '1')
1633 self.assertTrue(items[1].live)
1634
1635 self.assertEqual(items[2].change.number, '1')
1636 self.assertEqual(items[2].change.patchset, '2')
1637 self.assertTrue(items[2].live)
1638
1639 # Add a new patchset to B
1640 B.addPatchset()
1641 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1642 self.waitUntilSettled()
1643
1644 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1645 # but we should have a new B,2 (still based on A,1)
1646 items = check_pipeline.getAllItems()
1647 self.assertEqual(len(items), 3)
1648
1649 self.assertEqual(items[0].change.number, '1')
1650 self.assertEqual(items[0].change.patchset, '2')
1651 self.assertTrue(items[0].live)
1652
1653 self.assertEqual(items[1].change.number, '1')
1654 self.assertEqual(items[1].change.patchset, '1')
1655 self.assertFalse(items[1].live)
1656
1657 self.assertEqual(items[2].change.number, '2')
1658 self.assertEqual(items[2].change.patchset, '2')
1659 self.assertTrue(items[2].live)
1660
1661 self.builds[0].release()
1662 self.waitUntilSettled()
1663 self.builds[0].release()
1664 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001665 self.executor_server.hold_jobs_in_build = False
1666 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001667 self.waitUntilSettled()
1668
1669 self.assertEqual(A.reported, 1)
1670 self.assertEqual(B.reported, 1)
1671 self.assertEqual(self.history[0].result, 'ABORTED')
1672 self.assertEqual(self.history[0].changes, '1,1')
1673 self.assertEqual(self.history[1].result, 'ABORTED')
1674 self.assertEqual(self.history[1].changes, '1,1 2,1')
1675 self.assertEqual(self.history[2].result, 'SUCCESS')
1676 self.assertEqual(self.history[2].changes, '1,2')
1677 self.assertEqual(self.history[3].result, 'SUCCESS')
1678 self.assertEqual(self.history[3].changes, '1,1 2,2')
1679
1680 def test_abandoned_gate(self):
1681 "Test that an abandoned change is dequeued from gate"
1682
Paul Belanger174a8272017-03-14 13:20:10 -04001683 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001684
1685 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001686 A.addApproval('code-review', 2)
1687 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001688 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001689 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1690 self.assertEqual(self.builds[0].name, 'project-merge')
1691
1692 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1693 self.waitUntilSettled()
1694
Paul Belanger174a8272017-03-14 13:20:10 -04001695 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001696 self.waitUntilSettled()
1697
Arie5b4048c2016-10-05 16:12:06 +03001698 self.assertBuilds([])
1699 self.assertHistory([
1700 dict(name='project-merge', result='ABORTED', changes='1,1')],
1701 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001702 self.assertEqual(A.reported, 1,
1703 "Abandoned gate change should report only start")
1704
1705 def test_abandoned_check(self):
1706 "Test that an abandoned change is dequeued from check"
1707
Paul Belanger174a8272017-03-14 13:20:10 -04001708 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001709
1710 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1711 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001712 tenant = self.sched.abide.tenants.get('tenant-one')
1713 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001714
1715 # Add two git-dependent changes
1716 B.setDependsOn(A, 1)
1717 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1718 self.waitUntilSettled()
1719 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1720 self.waitUntilSettled()
1721 # A live item, and a non-live/live pair
1722 items = check_pipeline.getAllItems()
1723 self.assertEqual(len(items), 3)
1724
1725 self.assertEqual(items[0].change.number, '1')
1726 self.assertFalse(items[0].live)
1727
1728 self.assertEqual(items[1].change.number, '2')
1729 self.assertTrue(items[1].live)
1730
1731 self.assertEqual(items[2].change.number, '1')
1732 self.assertTrue(items[2].live)
1733
1734 # Abandon A
1735 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1736 self.waitUntilSettled()
1737
1738 # The live copy of A should be gone, but the non-live and B
1739 # should continue
1740 items = check_pipeline.getAllItems()
1741 self.assertEqual(len(items), 2)
1742
1743 self.assertEqual(items[0].change.number, '1')
1744 self.assertFalse(items[0].live)
1745
1746 self.assertEqual(items[1].change.number, '2')
1747 self.assertTrue(items[1].live)
1748
Paul Belanger174a8272017-03-14 13:20:10 -04001749 self.executor_server.hold_jobs_in_build = False
1750 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001751 self.waitUntilSettled()
1752
1753 self.assertEqual(len(self.history), 4)
1754 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001755 'Build should have been aborted')
1756 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001757 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001758
Steve Varnau7b78b312015-04-03 14:49:46 -07001759 def test_abandoned_not_timer(self):
1760 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001761 # This test can not use simple_layout because it must start
1762 # with a configuration which does not include a
1763 # timer-triggered job so that we have an opportunity to set
1764 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001765 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001766 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001767 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001768 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001769 # The pipeline triggers every second, so we should have seen
1770 # several by now.
1771 time.sleep(5)
1772 self.waitUntilSettled()
1773 # Stop queuing timer triggered jobs so that the assertions
1774 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001775 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001776 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001777 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001778
Steve Varnau7b78b312015-04-03 14:49:46 -07001779 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1780
1781 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1782 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1783 self.waitUntilSettled()
1784 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1785
1786 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1787 self.waitUntilSettled()
1788
1789 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1790
Paul Belanger174a8272017-03-14 13:20:10 -04001791 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001792 self.waitUntilSettled()
1793
Arx Cruzb1b010d2013-10-28 19:49:59 -02001794 def test_zuul_url_return(self):
1795 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001796 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001797 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001798
1799 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001800 A.addApproval('code-review', 2)
1801 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001802 self.waitUntilSettled()
1803
1804 self.assertEqual(len(self.builds), 1)
1805 for build in self.builds:
1806 self.assertTrue('ZUUL_URL' in build.parameters)
1807
Paul Belanger174a8272017-03-14 13:20:10 -04001808 self.executor_server.hold_jobs_in_build = False
1809 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001810 self.waitUntilSettled()
1811
James E. Blair2fa50962013-01-30 21:50:41 -08001812 def test_new_patchset_dequeues_old_on_head(self):
1813 "Test that a new patchset causes the old to be dequeued (at head)"
1814 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001815 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001816 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1817 M.setMerged()
1818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1819 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1820 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1821 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001822 A.addApproval('code-review', 2)
1823 B.addApproval('code-review', 2)
1824 C.addApproval('code-review', 2)
1825 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001826
1827 C.setDependsOn(B, 1)
1828 B.setDependsOn(A, 1)
1829 A.setDependsOn(M, 1)
1830
James E. Blair8b5408c2016-08-08 15:37:46 -07001831 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1832 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1833 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1834 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001835 self.waitUntilSettled()
1836
1837 A.addPatchset()
1838 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1839 self.waitUntilSettled()
1840
Paul Belanger174a8272017-03-14 13:20:10 -04001841 self.executor_server.hold_jobs_in_build = False
1842 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001843 self.waitUntilSettled()
1844
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001845 self.assertEqual(A.data['status'], 'NEW')
1846 self.assertEqual(A.reported, 2)
1847 self.assertEqual(B.data['status'], 'NEW')
1848 self.assertEqual(B.reported, 2)
1849 self.assertEqual(C.data['status'], 'NEW')
1850 self.assertEqual(C.reported, 2)
1851 self.assertEqual(D.data['status'], 'MERGED')
1852 self.assertEqual(D.reported, 2)
1853 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001854
1855 def test_new_patchset_dequeues_old_without_dependents(self):
1856 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001857 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001858 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1859 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1860 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001861 A.addApproval('code-review', 2)
1862 B.addApproval('code-review', 2)
1863 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001864
James E. Blair8b5408c2016-08-08 15:37:46 -07001865 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1866 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1867 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001868 self.waitUntilSettled()
1869
1870 B.addPatchset()
1871 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1872 self.waitUntilSettled()
1873
Paul Belanger174a8272017-03-14 13:20:10 -04001874 self.executor_server.hold_jobs_in_build = False
1875 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001876 self.waitUntilSettled()
1877
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001878 self.assertEqual(A.data['status'], 'MERGED')
1879 self.assertEqual(A.reported, 2)
1880 self.assertEqual(B.data['status'], 'NEW')
1881 self.assertEqual(B.reported, 2)
1882 self.assertEqual(C.data['status'], 'MERGED')
1883 self.assertEqual(C.reported, 2)
1884 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001885
1886 def test_new_patchset_dequeues_old_independent_queue(self):
1887 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001888 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001889 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1890 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1891 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1892 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1893 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1894 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1895 self.waitUntilSettled()
1896
1897 B.addPatchset()
1898 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1899 self.waitUntilSettled()
1900
Paul Belanger174a8272017-03-14 13:20:10 -04001901 self.executor_server.hold_jobs_in_build = False
1902 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001903 self.waitUntilSettled()
1904
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001905 self.assertEqual(A.data['status'], 'NEW')
1906 self.assertEqual(A.reported, 1)
1907 self.assertEqual(B.data['status'], 'NEW')
1908 self.assertEqual(B.reported, 1)
1909 self.assertEqual(C.data['status'], 'NEW')
1910 self.assertEqual(C.reported, 1)
1911 self.assertEqual(len(self.history), 10)
1912 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001913
James E. Blaircc020532017-04-19 13:04:44 -07001914 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001915 def test_noop_job(self):
1916 "Test that the internal noop job works"
1917 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001918 A.addApproval('code-review', 2)
1919 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001920 self.waitUntilSettled()
1921
1922 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1923 self.assertTrue(self.sched._areAllBuildsComplete())
1924 self.assertEqual(len(self.history), 0)
1925 self.assertEqual(A.data['status'], 'MERGED')
1926 self.assertEqual(A.reported, 2)
1927
James E. Blair06cc3922017-04-19 10:08:10 -07001928 @simple_layout('layouts/no-jobs-project.yaml')
Clint Byrum8897dca2017-05-10 19:14:19 -07001929 @skipIf(sys.version_info.major > 2, 'Fails on py3')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001930 def test_no_job_project(self):
1931 "Test that reports with no jobs don't get sent"
1932 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1933 'master', 'A')
1934 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1935 self.waitUntilSettled()
1936
1937 # Change wasn't reported to
1938 self.assertEqual(A.reported, False)
1939
1940 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001941 tenant = self.sched.abide.tenants.get('tenant-one')
1942 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001943 items = check_pipeline.getAllItems()
1944 self.assertEqual(len(items), 0)
1945
1946 self.assertEqual(len(self.history), 0)
1947
James E. Blair7d0dedc2013-02-21 17:26:09 -08001948 def test_zuul_refs(self):
1949 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001950 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001951 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1952 M1.setMerged()
1953 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1954 M2.setMerged()
1955
1956 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1957 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1958 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1959 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001960 A.addApproval('code-review', 2)
1961 B.addApproval('code-review', 2)
1962 C.addApproval('code-review', 2)
1963 D.addApproval('code-review', 2)
1964 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1965 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1966 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1967 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001968
1969 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001970 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001971 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001972 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001973 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001974 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001975 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001976 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977 self.waitUntilSettled()
1978
James E. Blair7d0dedc2013-02-21 17:26:09 -08001979 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001980 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001981 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001982 if x.parameters['ZUUL_CHANGE'] == '3':
1983 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001984 a_build = x
1985 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001986 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001987 b_build = x
1988 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001989 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001990 c_build = x
1991 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001992 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001993 d_build = x
1994 if a_build and b_build and c_build and d_build:
1995 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08001996
1997 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001998 self.assertIsNotNone(a_zref)
1999 self.assertIsNotNone(b_zref)
2000 self.assertIsNotNone(c_zref)
2001 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002002
2003 # And they should all be different
2004 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002005 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002006
Clint Byrum3343e3e2016-11-15 16:05:03 -08002007 # should have a, not b, and should not be in project2
2008 self.assertTrue(a_build.hasChanges(A))
2009 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002010
Clint Byrum3343e3e2016-11-15 16:05:03 -08002011 # should have a and b, and should not be in project2
2012 self.assertTrue(b_build.hasChanges(A, B))
2013 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002014
Clint Byrum3343e3e2016-11-15 16:05:03 -08002015 # should have a and b in 1, c in 2
2016 self.assertTrue(c_build.hasChanges(A, B, C))
2017 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002018
Clint Byrum3343e3e2016-11-15 16:05:03 -08002019 # should have a and b in 1, c and d in 2
2020 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002021
Paul Belanger174a8272017-03-14 13:20:10 -04002022 self.executor_server.hold_jobs_in_build = False
2023 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002024 self.waitUntilSettled()
2025
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002026 self.assertEqual(A.data['status'], 'MERGED')
2027 self.assertEqual(A.reported, 2)
2028 self.assertEqual(B.data['status'], 'MERGED')
2029 self.assertEqual(B.reported, 2)
2030 self.assertEqual(C.data['status'], 'MERGED')
2031 self.assertEqual(C.reported, 2)
2032 self.assertEqual(D.data['status'], 'MERGED')
2033 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002034
James E. Blair4a28a882013-08-23 15:17:33 -07002035 def test_rerun_on_error(self):
2036 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002037 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002039 A.addApproval('code-review', 2)
2040 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002041 self.waitUntilSettled()
2042
James E. Blair412fba82017-01-26 15:00:50 -08002043 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002044 self.executor_server.hold_jobs_in_build = False
2045 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002046 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002047 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002048 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2049
James E. Blair412e5582013-04-22 15:50:12 -07002050 def test_statsd(self):
2051 "Test each of the statsd methods used in the scheduler"
2052 import extras
2053 statsd = extras.try_import('statsd.statsd')
2054 statsd.incr('test-incr')
2055 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002056 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002057 self.assertReportedStat('test-incr', '1|c')
2058 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002059 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002060
Clint Byrum8897dca2017-05-10 19:14:19 -07002061 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blairdad52252014-02-07 16:59:17 -08002062 def test_stuck_job_cleanup(self):
2063 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002064
2065 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002066 self.gearman_server.hold_jobs_in_queue = True
2067 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002068 A.addApproval('code-review', 2)
2069 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002070 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002071 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002072 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2073
James E. Blair31cb3e82017-04-20 13:08:56 -07002074 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002075 self.sched.reconfigure(self.config)
2076 self.waitUntilSettled()
2077
James E. Blair18c64442014-03-18 10:14:45 -07002078 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002079 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002080 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002081 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2082 self.assertTrue(self.sched._areAllBuildsComplete())
2083
2084 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002085 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002086 self.assertEqual(self.history[0].result, 'SUCCESS')
2087
James E. Blair879dafb2015-07-17 14:04:49 -07002088 def test_file_head(self):
2089 # This is a regression test for an observed bug. A change
2090 # with a file named "HEAD" in the root directory of the repo
2091 # was processed by a merger. It then was unable to reset the
2092 # repo because of:
2093 # GitCommandError: 'git reset --hard HEAD' returned
2094 # with exit code 128
2095 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2096 # and filename
2097 # Use '--' to separate filenames from revisions'
2098
2099 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002100 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002101 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2102
2103 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2104 self.waitUntilSettled()
2105
2106 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2107 self.waitUntilSettled()
2108
2109 self.assertIn('Build succeeded', A.messages[0])
2110 self.assertIn('Build succeeded', B.messages[0])
2111
James E. Blair70c71582013-03-06 08:50:50 -08002112 def test_file_jobs(self):
2113 "Test that file jobs run only when appropriate"
2114 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002115 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002116 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002117 A.addApproval('code-review', 2)
2118 B.addApproval('code-review', 2)
2119 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2120 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002121 self.waitUntilSettled()
2122
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002123 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002124 if x.name == 'project-testfile']
2125
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002126 self.assertEqual(len(testfile_jobs), 1)
2127 self.assertEqual(testfile_jobs[0].changes, '1,2')
2128 self.assertEqual(A.data['status'], 'MERGED')
2129 self.assertEqual(A.reported, 2)
2130 self.assertEqual(B.data['status'], 'MERGED')
2131 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002132
Clint Byrum3ee39f32016-11-17 23:45:07 -08002133 def _test_irrelevant_files_jobs(self, should_skip):
2134 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002135 if should_skip:
2136 files = {'ignoreme': 'ignored\n'}
2137 else:
2138 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002139
2140 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002141 'master',
2142 'test irrelevant-files',
2143 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002144 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2145 self.waitUntilSettled()
2146
2147 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002148 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002149
2150 if should_skip:
2151 self.assertEqual([], tested_change_ids)
2152 else:
2153 self.assertIn(change.data['number'], tested_change_ids)
2154
James E. Blaircc324192017-04-19 14:36:08 -07002155 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002156 def test_irrelevant_files_match_skips_job(self):
2157 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002158
James E. Blaircc324192017-04-19 14:36:08 -07002159 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002160 def test_irrelevant_files_no_match_runs_job(self):
2161 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002162
James E. Blaircc324192017-04-19 14:36:08 -07002163 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002164 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002165 files = {'ignoreme': 'ignored\n'}
2166
2167 change = self.fake_gerrit.addFakeChange('org/project',
2168 'master',
2169 'test irrelevant-files',
2170 files=files)
2171 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2172 self.waitUntilSettled()
2173
2174 run_jobs = set([build.name for build in self.history])
2175
2176 self.assertEqual(set(['project-test-nomatch-starts-empty',
2177 'project-test-nomatch-starts-full']), run_jobs)
2178
James E. Blairc8a1e052014-02-25 09:29:26 -08002179 def test_queue_names(self):
2180 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002181 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002182 (trusted, project1) = tenant.getProject('org/project1')
2183 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002184 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2185 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2186 self.assertEqual(q1.name, 'integrated')
2187 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002188
Clint Byrum8897dca2017-05-10 19:14:19 -07002189 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blair64ed6f22013-07-10 14:07:23 -07002190 def test_queue_precedence(self):
2191 "Test that queue precedence works"
2192
2193 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002194 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002195 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2196 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002197 A.addApproval('code-review', 2)
2198 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002199
2200 self.waitUntilSettled()
2201 self.gearman_server.hold_jobs_in_queue = False
2202 self.gearman_server.release()
2203 self.waitUntilSettled()
2204
James E. Blair8de58bd2013-07-18 16:23:33 -07002205 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002206 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002207 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002208 self.waitUntilSettled()
2209
James E. Blair64ed6f22013-07-10 14:07:23 -07002210 self.log.debug(self.history)
2211 self.assertEqual(self.history[0].pipeline, 'gate')
2212 self.assertEqual(self.history[1].pipeline, 'check')
2213 self.assertEqual(self.history[2].pipeline, 'gate')
2214 self.assertEqual(self.history[3].pipeline, 'gate')
2215 self.assertEqual(self.history[4].pipeline, 'check')
2216 self.assertEqual(self.history[5].pipeline, 'check')
2217
Clark Boylana5edbe42014-06-03 16:39:10 -07002218 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002219 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002220 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002221 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002222 A.addApproval('code-review', 2)
2223 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002224 self.waitUntilSettled()
2225
Paul Belanger174a8272017-03-14 13:20:10 -04002226 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002227 self.waitUntilSettled()
2228
James E. Blair1843a552013-07-03 14:19:52 -07002229 port = self.webapp.server.socket.getsockname()[1]
2230
Adam Gandelman77a12c72017-02-03 17:43:43 -08002231 req = urllib.request.Request(
2232 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002233 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002234 headers = f.info()
2235 self.assertIn('Content-Length', headers)
2236 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002237 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2238 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002239 self.assertIn('Access-Control-Allow-Origin', headers)
2240 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002241 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002242 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002243 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002244
Paul Belanger174a8272017-03-14 13:20:10 -04002245 self.executor_server.hold_jobs_in_build = False
2246 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002247 self.waitUntilSettled()
2248
2249 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002250 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002251 for p in data['pipelines']:
2252 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002253 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002254 self.assertEqual(q['window'], 20)
2255 else:
2256 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002257 for head in q['heads']:
2258 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002259 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002260 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002261 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002262 status_jobs.append(job)
2263 self.assertEqual('project-merge', status_jobs[0]['name'])
2264 self.assertEqual('https://server/job/project-merge/0/',
2265 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002266 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002267 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002268 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002269 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002270 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002271 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002272 status_jobs[1]['report_url'])
2273
2274 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002275 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002276 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002277 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002278 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002279
James E. Blaircdccd972013-07-01 12:10:22 -07002280 def test_live_reconfiguration(self):
2281 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002282 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002283 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002284 A.addApproval('code-review', 2)
2285 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002286 self.waitUntilSettled()
2287
2288 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002289 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002290
Paul Belanger174a8272017-03-14 13:20:10 -04002291 self.executor_server.hold_jobs_in_build = False
2292 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002293 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002294 self.assertEqual(self.getJobFromHistory('project-merge').result,
2295 'SUCCESS')
2296 self.assertEqual(self.getJobFromHistory('project-test1').result,
2297 'SUCCESS')
2298 self.assertEqual(self.getJobFromHistory('project-test2').result,
2299 'SUCCESS')
2300 self.assertEqual(A.data['status'], 'MERGED')
2301 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002302
James E. Blair6bc782d2015-07-17 16:20:21 -07002303 def test_live_reconfiguration_merge_conflict(self):
2304 # A real-world bug: a change in a gate queue has a merge
2305 # conflict and a job is added to its project while it's
2306 # sitting in the queue. The job gets added to the change and
2307 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002308 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002309
2310 # This change is fine. It's here to stop the queue long
2311 # enough for the next change to be subject to the
2312 # reconfiguration, as well as to provide a conflict for the
2313 # next change. This change will succeed and merge.
2314 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002315 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002316 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002317
2318 # This change will be in merge conflict. During the
2319 # reconfiguration, we will add a job. We want to make sure
2320 # that doesn't cause it to get stuck.
2321 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002322 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002323 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002324
James E. Blair8b5408c2016-08-08 15:37:46 -07002325 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2326 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002327
2328 self.waitUntilSettled()
2329
2330 # No jobs have run yet
2331 self.assertEqual(A.data['status'], 'NEW')
2332 self.assertEqual(A.reported, 1)
2333 self.assertEqual(B.data['status'], 'NEW')
2334 self.assertEqual(B.reported, 1)
2335 self.assertEqual(len(self.history), 0)
2336
2337 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002338 self.commitConfigUpdate('common-config',
2339 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002340 self.sched.reconfigure(self.config)
2341 self.waitUntilSettled()
2342
Paul Belanger174a8272017-03-14 13:20:10 -04002343 self.executor_server.hold_jobs_in_build = False
2344 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002345 self.waitUntilSettled()
2346
2347 self.assertEqual(A.data['status'], 'MERGED')
2348 self.assertEqual(A.reported, 2)
2349 self.assertEqual(B.data['status'], 'NEW')
2350 self.assertEqual(B.reported, 2)
2351 self.assertEqual(self.getJobFromHistory('project-merge').result,
2352 'SUCCESS')
2353 self.assertEqual(self.getJobFromHistory('project-test1').result,
2354 'SUCCESS')
2355 self.assertEqual(self.getJobFromHistory('project-test2').result,
2356 'SUCCESS')
2357 self.assertEqual(self.getJobFromHistory('project-test3').result,
2358 'SUCCESS')
2359 self.assertEqual(len(self.history), 4)
2360
James E. Blair400e8fd2015-07-30 17:44:45 -07002361 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002362 # An extrapolation of test_live_reconfiguration_merge_conflict
2363 # that tests a job added to a job tree with a failed root does
2364 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002365 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002366
2367 # This change is fine. It's here to stop the queue long
2368 # enough for the next change to be subject to the
2369 # reconfiguration. This change will succeed and merge.
2370 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002371 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002372 A.addApproval('code-review', 2)
2373 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002374 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002375 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002376 self.waitUntilSettled()
2377
2378 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002379 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002380 B.addApproval('code-review', 2)
2381 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002382 self.waitUntilSettled()
2383
Paul Belanger174a8272017-03-14 13:20:10 -04002384 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002385 self.waitUntilSettled()
2386
2387 # Both -merge jobs have run, but no others.
2388 self.assertEqual(A.data['status'], 'NEW')
2389 self.assertEqual(A.reported, 1)
2390 self.assertEqual(B.data['status'], 'NEW')
2391 self.assertEqual(B.reported, 1)
2392 self.assertEqual(self.history[0].result, 'SUCCESS')
2393 self.assertEqual(self.history[0].name, 'project-merge')
2394 self.assertEqual(self.history[1].result, 'FAILURE')
2395 self.assertEqual(self.history[1].name, 'project-merge')
2396 self.assertEqual(len(self.history), 2)
2397
2398 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002399 self.commitConfigUpdate('common-config',
2400 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002401 self.sched.reconfigure(self.config)
2402 self.waitUntilSettled()
2403
Paul Belanger174a8272017-03-14 13:20:10 -04002404 self.executor_server.hold_jobs_in_build = False
2405 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002406 self.waitUntilSettled()
2407
2408 self.assertEqual(A.data['status'], 'MERGED')
2409 self.assertEqual(A.reported, 2)
2410 self.assertEqual(B.data['status'], 'NEW')
2411 self.assertEqual(B.reported, 2)
2412 self.assertEqual(self.history[0].result, 'SUCCESS')
2413 self.assertEqual(self.history[0].name, 'project-merge')
2414 self.assertEqual(self.history[1].result, 'FAILURE')
2415 self.assertEqual(self.history[1].name, 'project-merge')
2416 self.assertEqual(self.history[2].result, 'SUCCESS')
2417 self.assertEqual(self.history[3].result, 'SUCCESS')
2418 self.assertEqual(self.history[4].result, 'SUCCESS')
2419 self.assertEqual(len(self.history), 5)
2420
James E. Blair400e8fd2015-07-30 17:44:45 -07002421 def test_live_reconfiguration_failed_job(self):
2422 # Test that a change with a removed failing job does not
2423 # disrupt reconfiguration. If a change has a failed job and
2424 # that job is removed during a reconfiguration, we observed a
2425 # bug where the code to re-set build statuses would run on
2426 # that build and raise an exception because the job no longer
2427 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002428 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002429
2430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2431
2432 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002433 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002434
2435 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2436 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002437 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002438 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002439 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002440 self.waitUntilSettled()
2441
2442 self.assertEqual(A.data['status'], 'NEW')
2443 self.assertEqual(A.reported, 0)
2444
2445 self.assertEqual(self.getJobFromHistory('project-merge').result,
2446 'SUCCESS')
2447 self.assertEqual(self.getJobFromHistory('project-test1').result,
2448 'FAILURE')
2449 self.assertEqual(len(self.history), 2)
2450
2451 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002452 self.commitConfigUpdate('common-config',
2453 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002454 self.sched.reconfigure(self.config)
2455 self.waitUntilSettled()
2456
Paul Belanger174a8272017-03-14 13:20:10 -04002457 self.executor_server.hold_jobs_in_build = False
2458 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002459 self.waitUntilSettled()
2460
2461 self.assertEqual(self.getJobFromHistory('project-test2').result,
2462 'SUCCESS')
2463 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2464 'SUCCESS')
2465 self.assertEqual(len(self.history), 4)
2466
2467 self.assertEqual(A.data['status'], 'NEW')
2468 self.assertEqual(A.reported, 1)
2469 self.assertIn('Build succeeded', A.messages[0])
2470 # Ensure the removed job was not included in the report.
2471 self.assertNotIn('project-test1', A.messages[0])
2472
James E. Blairfe707d12015-08-05 15:18:15 -07002473 def test_live_reconfiguration_shared_queue(self):
2474 # Test that a change with a failing job which was removed from
2475 # this project but otherwise still exists in the system does
2476 # not disrupt reconfiguration.
2477
Paul Belanger174a8272017-03-14 13:20:10 -04002478 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002479
2480 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2481
Paul Belanger174a8272017-03-14 13:20:10 -04002482 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002483
2484 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2485 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002486 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002487 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002488 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002489 self.waitUntilSettled()
2490
2491 self.assertEqual(A.data['status'], 'NEW')
2492 self.assertEqual(A.reported, 0)
2493
Clint Byrum69663d42017-05-01 14:51:04 -07002494 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002495 'SUCCESS')
2496 self.assertEqual(self.getJobFromHistory(
2497 'project1-project2-integration').result, 'FAILURE')
2498 self.assertEqual(len(self.history), 2)
2499
2500 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002501 self.commitConfigUpdate(
2502 'common-config',
2503 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002504 self.sched.reconfigure(self.config)
2505 self.waitUntilSettled()
2506
Paul Belanger174a8272017-03-14 13:20:10 -04002507 self.executor_server.hold_jobs_in_build = False
2508 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002509 self.waitUntilSettled()
2510
Clint Byrum69663d42017-05-01 14:51:04 -07002511 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002512 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002513 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002514 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002515 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002516 'SUCCESS')
2517 self.assertEqual(self.getJobFromHistory(
2518 'project1-project2-integration').result, 'FAILURE')
2519 self.assertEqual(len(self.history), 4)
2520
2521 self.assertEqual(A.data['status'], 'NEW')
2522 self.assertEqual(A.reported, 1)
2523 self.assertIn('Build succeeded', A.messages[0])
2524 # Ensure the removed job was not included in the report.
2525 self.assertNotIn('project1-project2-integration', A.messages[0])
2526
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002527 def test_double_live_reconfiguration_shared_queue(self):
2528 # This was a real-world regression. A change is added to
2529 # gate; a reconfigure happens, a second change which depends
2530 # on the first is added, and a second reconfiguration happens.
2531 # Ensure that both changes merge.
2532
2533 # A failure may indicate incorrect caching or cleaning up of
2534 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002535 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002536
2537 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2538 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2539 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002540 A.addApproval('code-review', 2)
2541 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002542
2543 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002544 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002545 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002546 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002547 self.waitUntilSettled()
2548
2549 # Reconfigure (with only one change in the pipeline).
2550 self.sched.reconfigure(self.config)
2551 self.waitUntilSettled()
2552
2553 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002554 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002555 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002556 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002557 self.waitUntilSettled()
2558
2559 # Reconfigure (with both in the pipeline).
2560 self.sched.reconfigure(self.config)
2561 self.waitUntilSettled()
2562
Paul Belanger174a8272017-03-14 13:20:10 -04002563 self.executor_server.hold_jobs_in_build = False
2564 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002565 self.waitUntilSettled()
2566
2567 self.assertEqual(len(self.history), 8)
2568
2569 self.assertEqual(A.data['status'], 'MERGED')
2570 self.assertEqual(A.reported, 2)
2571 self.assertEqual(B.data['status'], 'MERGED')
2572 self.assertEqual(B.reported, 2)
2573
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002574 def test_live_reconfiguration_del_project(self):
2575 # Test project deletion from layout
2576 # while changes are enqueued
2577
Paul Belanger174a8272017-03-14 13:20:10 -04002578 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2580 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2581 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2582
2583 # A Depends-On: B
2584 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2585 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002586 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002587
2588 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2589 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2590 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002591 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002592 self.waitUntilSettled()
2593 self.assertEqual(len(self.builds), 5)
2594
2595 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002596 self.commitConfigUpdate(
2597 'common-config',
2598 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002599 self.sched.reconfigure(self.config)
2600 self.waitUntilSettled()
2601
2602 # Builds for C aborted, builds for A succeed,
2603 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002604 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002605 self.assertEqual(job_c.changes, '3,1')
2606 self.assertEqual(job_c.result, 'ABORTED')
2607
Paul Belanger174a8272017-03-14 13:20:10 -04002608 self.executor_server.hold_jobs_in_build = False
2609 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002610 self.waitUntilSettled()
2611
Clint Byrum0149ada2017-04-04 12:22:01 -07002612 self.assertEqual(
2613 self.getJobFromHistory('project-test1', 'org/project').changes,
2614 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002615
2616 self.assertEqual(A.data['status'], 'NEW')
2617 self.assertEqual(B.data['status'], 'NEW')
2618 self.assertEqual(C.data['status'], 'NEW')
2619 self.assertEqual(A.reported, 1)
2620 self.assertEqual(B.reported, 0)
2621 self.assertEqual(C.reported, 0)
2622
Clint Byrum0149ada2017-04-04 12:22:01 -07002623 tenant = self.sched.abide.tenants.get('tenant-one')
2624 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002625 self.assertIn('Build succeeded', A.messages[0])
2626
James E. Blairec056492016-07-22 09:45:56 -07002627 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002628 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002629 self.updateConfigLayout(
2630 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002631 self.sched.reconfigure(self.config)
2632
2633 self.init_repo("org/new-project")
2634 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2635
James E. Blair8b5408c2016-08-08 15:37:46 -07002636 A.addApproval('code-review', 2)
2637 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002638 self.waitUntilSettled()
2639 self.assertEqual(self.getJobFromHistory('project-merge').result,
2640 'SUCCESS')
2641 self.assertEqual(self.getJobFromHistory('project-test1').result,
2642 'SUCCESS')
2643 self.assertEqual(self.getJobFromHistory('project-test2').result,
2644 'SUCCESS')
2645 self.assertEqual(A.data['status'], 'MERGED')
2646 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002647
James E. Blair9ea70072017-04-19 16:05:30 -07002648 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002649 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002650 self.init_repo("org/delete-project")
2651 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2652
James E. Blair8b5408c2016-08-08 15:37:46 -07002653 A.addApproval('code-review', 2)
2654 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002655 self.waitUntilSettled()
2656 self.assertEqual(self.getJobFromHistory('project-merge').result,
2657 'SUCCESS')
2658 self.assertEqual(self.getJobFromHistory('project-test1').result,
2659 'SUCCESS')
2660 self.assertEqual(self.getJobFromHistory('project-test2').result,
2661 'SUCCESS')
2662 self.assertEqual(A.data['status'], 'MERGED')
2663 self.assertEqual(A.reported, 2)
2664
2665 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002666 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002667 if os.path.exists(os.path.join(self.merger_src_root, p)):
2668 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002669 if os.path.exists(os.path.join(self.executor_src_root, p)):
2670 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002671
2672 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2673
James E. Blair8b5408c2016-08-08 15:37:46 -07002674 B.addApproval('code-review', 2)
2675 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002676 self.waitUntilSettled()
2677 self.assertEqual(self.getJobFromHistory('project-merge').result,
2678 'SUCCESS')
2679 self.assertEqual(self.getJobFromHistory('project-test1').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory('project-test2').result,
2682 'SUCCESS')
2683 self.assertEqual(B.data['status'], 'MERGED')
2684 self.assertEqual(B.reported, 2)
2685
James E. Blaire68d43f2017-04-19 14:03:30 -07002686 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002687 def test_tags(self):
2688 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002689 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2690 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2691 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2692 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2693 self.waitUntilSettled()
2694
James E. Blaire68d43f2017-04-19 14:03:30 -07002695 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002696
2697 results = {self.getJobFromHistory('merge',
2698 project='org/project1').uuid: 'extratag merge',
2699 self.getJobFromHistory('merge',
2700 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002701
2702 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002703 self.assertEqual(results.get(build.uuid, ''),
2704 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002705
James E. Blair63bb0ef2013-07-29 17:14:51 -07002706 def test_timer(self):
2707 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002708 # This test can not use simple_layout because it must start
2709 # with a configuration which does not include a
2710 # timer-triggered job so that we have an opportunity to set
2711 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002712 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002713 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002714 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002715
Clark Boylan3ee090a2014-04-03 20:55:09 -07002716 # The pipeline triggers every second, so we should have seen
2717 # several by now.
2718 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002719 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002720
2721 self.assertEqual(len(self.builds), 2)
2722
James E. Blair63bb0ef2013-07-29 17:14:51 -07002723 port = self.webapp.server.socket.getsockname()[1]
2724
Clint Byrum5e729e92017-02-08 06:34:57 -08002725 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002726 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002727 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002728 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002729
Paul Belanger174a8272017-03-14 13:20:10 -04002730 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002731 # Stop queuing timer triggered jobs so that the assertions
2732 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002733 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002734 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002735 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002736 self.waitUntilSettled()
2737
2738 self.assertEqual(self.getJobFromHistory(
2739 'project-bitrot-stable-old').result, 'SUCCESS')
2740 self.assertEqual(self.getJobFromHistory(
2741 'project-bitrot-stable-older').result, 'SUCCESS')
2742
2743 data = json.loads(data)
2744 status_jobs = set()
2745 for p in data['pipelines']:
2746 for q in p['change_queues']:
2747 for head in q['heads']:
2748 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002749 for job in change['jobs']:
2750 status_jobs.add(job['name'])
2751 self.assertIn('project-bitrot-stable-old', status_jobs)
2752 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002753
James E. Blair4f6033c2014-03-27 15:49:09 -07002754 def test_idle(self):
2755 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002756 # This test can not use simple_layout because it must start
2757 # with a configuration which does not include a
2758 # timer-triggered job so that we have an opportunity to set
2759 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002760 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002761
Clark Boylan3ee090a2014-04-03 20:55:09 -07002762 for x in range(1, 3):
2763 # Test that timer triggers periodic jobs even across
2764 # layout config reloads.
2765 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002766 self.commitConfigUpdate('common-config',
2767 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002768 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002769 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002770
Clark Boylan3ee090a2014-04-03 20:55:09 -07002771 # The pipeline triggers every second, so we should have seen
2772 # several by now.
2773 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002774
2775 # Stop queuing timer triggered jobs so that the assertions
2776 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002777 self.commitConfigUpdate('common-config',
2778 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002779 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002780 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002781 self.assertEqual(len(self.builds), 2,
2782 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002783 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002784 self.waitUntilSettled()
2785 self.assertEqual(len(self.builds), 0)
2786 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002787
James E. Blaircc324192017-04-19 14:36:08 -07002788 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002789 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002790 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2791 self.waitUntilSettled()
2792
2793 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2794 self.waitUntilSettled()
2795
James E. Blairff80a2f2013-12-27 13:24:06 -08002796 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002797
2798 # A.messages only holds what FakeGerrit places in it. Thus we
2799 # work on the knowledge of what the first message should be as
2800 # it is only configured to go to SMTP.
2801
2802 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002803 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002804 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002805 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002806 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002807 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002808
2809 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002810 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002811 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002812 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002813 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002814 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002815
James E. Blaire5910202013-12-27 09:50:31 -08002816 def test_timer_smtp(self):
2817 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002818 # This test can not use simple_layout because it must start
2819 # with a configuration which does not include a
2820 # timer-triggered job so that we have an opportunity to set
2821 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002822 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002823 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002824 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002825
Clark Boylan3ee090a2014-04-03 20:55:09 -07002826 # The pipeline triggers every second, so we should have seen
2827 # several by now.
2828 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002829 self.waitUntilSettled()
2830
Clark Boylan3ee090a2014-04-03 20:55:09 -07002831 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002832 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002833 self.waitUntilSettled()
2834 self.assertEqual(len(self.history), 2)
2835
James E. Blaire5910202013-12-27 09:50:31 -08002836 self.assertEqual(self.getJobFromHistory(
2837 'project-bitrot-stable-old').result, 'SUCCESS')
2838 self.assertEqual(self.getJobFromHistory(
2839 'project-bitrot-stable-older').result, 'SUCCESS')
2840
James E. Blairff80a2f2013-12-27 13:24:06 -08002841 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002842
2843 # A.messages only holds what FakeGerrit places in it. Thus we
2844 # work on the knowledge of what the first message should be as
2845 # it is only configured to go to SMTP.
2846
2847 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002848 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002849 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002850 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002851 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002852 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002853
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854 # Stop queuing timer triggered jobs and let any that may have
2855 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002856 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002857 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002858 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002859 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002860 self.waitUntilSettled()
2861
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002862 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002863 def test_timer_sshkey(self):
2864 "Test that a periodic job can setup SSH key authentication"
2865 self.worker.hold_jobs_in_build = True
2866 self.config.set('zuul', 'layout_config',
2867 'tests/fixtures/layout-timer.yaml')
2868 self.sched.reconfigure(self.config)
2869 self.registerJobs()
2870
2871 # The pipeline triggers every second, so we should have seen
2872 # several by now.
2873 time.sleep(5)
2874 self.waitUntilSettled()
2875
2876 self.assertEqual(len(self.builds), 2)
2877
2878 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2879 self.assertTrue(os.path.isfile(ssh_wrapper))
2880 with open(ssh_wrapper) as f:
2881 ssh_wrapper_content = f.read()
2882 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2883 # In the unit tests Merger runs in the same process,
2884 # so we see its' environment variables
2885 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2886
2887 self.worker.release('.*')
2888 self.waitUntilSettled()
2889 self.assertEqual(len(self.history), 2)
2890
2891 self.assertEqual(self.getJobFromHistory(
2892 'project-bitrot-stable-old').result, 'SUCCESS')
2893 self.assertEqual(self.getJobFromHistory(
2894 'project-bitrot-stable-older').result, 'SUCCESS')
2895
2896 # Stop queuing timer triggered jobs and let any that may have
2897 # queued through so that end of test assertions pass.
2898 self.config.set('zuul', 'layout_config',
2899 'tests/fixtures/layout-no-timer.yaml')
2900 self.sched.reconfigure(self.config)
2901 self.registerJobs()
2902 self.waitUntilSettled()
2903 self.worker.release('.*')
2904 self.waitUntilSettled()
2905
James E. Blair91e34592015-07-31 16:45:59 -07002906 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002907 "Test that the RPC client can enqueue a change"
2908 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002909 A.addApproval('code-review', 2)
2910 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002911
2912 client = zuul.rpcclient.RPCClient('127.0.0.1',
2913 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002914 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002915 r = client.enqueue(tenant='tenant-one',
2916 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002917 project='org/project',
2918 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002919 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002920 self.waitUntilSettled()
2921 self.assertEqual(self.getJobFromHistory('project-merge').result,
2922 'SUCCESS')
2923 self.assertEqual(self.getJobFromHistory('project-test1').result,
2924 'SUCCESS')
2925 self.assertEqual(self.getJobFromHistory('project-test2').result,
2926 'SUCCESS')
2927 self.assertEqual(A.data['status'], 'MERGED')
2928 self.assertEqual(A.reported, 2)
2929 self.assertEqual(r, True)
2930
James E. Blair91e34592015-07-31 16:45:59 -07002931 def test_client_enqueue_ref(self):
2932 "Test that the RPC client can enqueue a ref"
2933
2934 client = zuul.rpcclient.RPCClient('127.0.0.1',
2935 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002936 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002937 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002938 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002939 pipeline='post',
2940 project='org/project',
2941 trigger='gerrit',
2942 ref='master',
2943 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2944 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2945 self.waitUntilSettled()
2946 job_names = [x.name for x in self.history]
2947 self.assertEqual(len(self.history), 1)
2948 self.assertIn('project-post', job_names)
2949 self.assertEqual(r, True)
2950
James E. Blairad28e912013-11-27 10:43:22 -08002951 def test_client_enqueue_negative(self):
2952 "Test that the RPC client returns errors"
2953 client = zuul.rpcclient.RPCClient('127.0.0.1',
2954 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002955 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002956 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002957 "Invalid tenant"):
2958 r = client.enqueue(tenant='tenant-foo',
2959 pipeline='gate',
2960 project='org/project',
2961 trigger='gerrit',
2962 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002963 self.assertEqual(r, False)
2964
2965 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002966 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002967 r = client.enqueue(tenant='tenant-one',
2968 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002969 project='project-does-not-exist',
2970 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002971 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002972 self.assertEqual(r, False)
2973
2974 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2975 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002976 r = client.enqueue(tenant='tenant-one',
2977 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002978 project='org/project',
2979 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002980 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002981 self.assertEqual(r, False)
2982
2983 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2984 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002985 r = client.enqueue(tenant='tenant-one',
2986 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002987 project='org/project',
2988 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002989 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002990 self.assertEqual(r, False)
2991
2992 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2993 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002994 r = client.enqueue(tenant='tenant-one',
2995 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002996 project='org/project',
2997 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002998 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002999 self.assertEqual(r, False)
3000
3001 self.waitUntilSettled()
3002 self.assertEqual(len(self.history), 0)
3003 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003004
3005 def test_client_promote(self):
3006 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003007 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003008 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3009 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3010 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003011 A.addApproval('code-review', 2)
3012 B.addApproval('code-review', 2)
3013 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003014
James E. Blair8b5408c2016-08-08 15:37:46 -07003015 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3016 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3017 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003018
3019 self.waitUntilSettled()
3020
Paul Belangerbaca3132016-11-04 12:49:54 -04003021 tenant = self.sched.abide.tenants.get('tenant-one')
3022 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003023 enqueue_times = {}
3024 for item in items:
3025 enqueue_times[str(item.change)] = item.enqueue_time
3026
James E. Blair36658cf2013-12-06 17:53:48 -08003027 client = zuul.rpcclient.RPCClient('127.0.0.1',
3028 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003029 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003030 r = client.promote(tenant='tenant-one',
3031 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003032 change_ids=['2,1', '3,1'])
3033
Sean Daguef39b9ca2014-01-10 21:34:35 -05003034 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003035 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003036 for item in items:
3037 self.assertEqual(
3038 enqueue_times[str(item.change)], item.enqueue_time)
3039
James E. Blair78acec92014-02-06 07:11:32 -08003040 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003041 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003042 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003043 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003044 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003045 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003046 self.waitUntilSettled()
3047
3048 self.assertEqual(len(self.builds), 6)
3049 self.assertEqual(self.builds[0].name, 'project-test1')
3050 self.assertEqual(self.builds[1].name, 'project-test2')
3051 self.assertEqual(self.builds[2].name, 'project-test1')
3052 self.assertEqual(self.builds[3].name, 'project-test2')
3053 self.assertEqual(self.builds[4].name, 'project-test1')
3054 self.assertEqual(self.builds[5].name, 'project-test2')
3055
Paul Belangerbaca3132016-11-04 12:49:54 -04003056 self.assertTrue(self.builds[0].hasChanges(B))
3057 self.assertFalse(self.builds[0].hasChanges(A))
3058 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003059
Paul Belangerbaca3132016-11-04 12:49:54 -04003060 self.assertTrue(self.builds[2].hasChanges(B))
3061 self.assertTrue(self.builds[2].hasChanges(C))
3062 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003063
Paul Belangerbaca3132016-11-04 12:49:54 -04003064 self.assertTrue(self.builds[4].hasChanges(B))
3065 self.assertTrue(self.builds[4].hasChanges(C))
3066 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003067
Paul Belanger174a8272017-03-14 13:20:10 -04003068 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003069 self.waitUntilSettled()
3070
3071 self.assertEqual(A.data['status'], 'MERGED')
3072 self.assertEqual(A.reported, 2)
3073 self.assertEqual(B.data['status'], 'MERGED')
3074 self.assertEqual(B.reported, 2)
3075 self.assertEqual(C.data['status'], 'MERGED')
3076 self.assertEqual(C.reported, 2)
3077
James E. Blair36658cf2013-12-06 17:53:48 -08003078 self.assertEqual(r, True)
3079
3080 def test_client_promote_dependent(self):
3081 "Test that the RPC client can promote a dependent change"
3082 # C (depends on B) -> B -> A ; then promote C to get:
3083 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003084 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003085 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3086 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3087 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3088
3089 C.setDependsOn(B, 1)
3090
James E. Blair8b5408c2016-08-08 15:37:46 -07003091 A.addApproval('code-review', 2)
3092 B.addApproval('code-review', 2)
3093 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003094
James E. Blair8b5408c2016-08-08 15:37:46 -07003095 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3096 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3097 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003098
3099 self.waitUntilSettled()
3100
3101 client = zuul.rpcclient.RPCClient('127.0.0.1',
3102 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003103 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003104 r = client.promote(tenant='tenant-one',
3105 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003106 change_ids=['3,1'])
3107
James E. Blair78acec92014-02-06 07:11:32 -08003108 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003109 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003110 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003111 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003112 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003113 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003114 self.waitUntilSettled()
3115
3116 self.assertEqual(len(self.builds), 6)
3117 self.assertEqual(self.builds[0].name, 'project-test1')
3118 self.assertEqual(self.builds[1].name, 'project-test2')
3119 self.assertEqual(self.builds[2].name, 'project-test1')
3120 self.assertEqual(self.builds[3].name, 'project-test2')
3121 self.assertEqual(self.builds[4].name, 'project-test1')
3122 self.assertEqual(self.builds[5].name, 'project-test2')
3123
Paul Belangerbaca3132016-11-04 12:49:54 -04003124 self.assertTrue(self.builds[0].hasChanges(B))
3125 self.assertFalse(self.builds[0].hasChanges(A))
3126 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003127
Paul Belangerbaca3132016-11-04 12:49:54 -04003128 self.assertTrue(self.builds[2].hasChanges(B))
3129 self.assertTrue(self.builds[2].hasChanges(C))
3130 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003131
Paul Belangerbaca3132016-11-04 12:49:54 -04003132 self.assertTrue(self.builds[4].hasChanges(B))
3133 self.assertTrue(self.builds[4].hasChanges(C))
3134 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003135
Paul Belanger174a8272017-03-14 13:20:10 -04003136 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003137 self.waitUntilSettled()
3138
3139 self.assertEqual(A.data['status'], 'MERGED')
3140 self.assertEqual(A.reported, 2)
3141 self.assertEqual(B.data['status'], 'MERGED')
3142 self.assertEqual(B.reported, 2)
3143 self.assertEqual(C.data['status'], 'MERGED')
3144 self.assertEqual(C.reported, 2)
3145
James E. Blair36658cf2013-12-06 17:53:48 -08003146 self.assertEqual(r, True)
3147
3148 def test_client_promote_negative(self):
3149 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003150 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003151 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003152 A.addApproval('code-review', 2)
3153 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003154 self.waitUntilSettled()
3155
3156 client = zuul.rpcclient.RPCClient('127.0.0.1',
3157 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003158 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003159
3160 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003161 r = client.promote(tenant='tenant-one',
3162 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003163 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003164 self.assertEqual(r, False)
3165
3166 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003167 r = client.promote(tenant='tenant-one',
3168 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003169 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003170 self.assertEqual(r, False)
3171
Paul Belanger174a8272017-03-14 13:20:10 -04003172 self.executor_server.hold_jobs_in_build = False
3173 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003174 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003175
James E. Blaircc324192017-04-19 14:36:08 -07003176 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003177 def test_queue_rate_limiting(self):
3178 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003179 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3181 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3182 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3183
3184 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003185 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003186
James E. Blair8b5408c2016-08-08 15:37:46 -07003187 A.addApproval('code-review', 2)
3188 B.addApproval('code-review', 2)
3189 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003190
James E. Blair8b5408c2016-08-08 15:37:46 -07003191 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3192 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3193 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003194 self.waitUntilSettled()
3195
3196 # Only A and B will have their merge jobs queued because
3197 # window is 2.
3198 self.assertEqual(len(self.builds), 2)
3199 self.assertEqual(self.builds[0].name, 'project-merge')
3200 self.assertEqual(self.builds[1].name, 'project-merge')
3201
James E. Blair732ee862017-04-21 07:49:26 -07003202 # Release the merge jobs one at a time.
3203 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003204 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003205 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003206 self.waitUntilSettled()
3207
3208 # Only A and B will have their test jobs queued because
3209 # window is 2.
3210 self.assertEqual(len(self.builds), 4)
3211 self.assertEqual(self.builds[0].name, 'project-test1')
3212 self.assertEqual(self.builds[1].name, 'project-test2')
3213 self.assertEqual(self.builds[2].name, 'project-test1')
3214 self.assertEqual(self.builds[3].name, 'project-test2')
3215
Paul Belanger174a8272017-03-14 13:20:10 -04003216 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003217 self.waitUntilSettled()
3218
James E. Blaircc324192017-04-19 14:36:08 -07003219 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003220 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003221 # A failed so window is reduced by 1 to 1.
3222 self.assertEqual(queue.window, 1)
3223 self.assertEqual(queue.window_floor, 1)
3224 self.assertEqual(A.data['status'], 'NEW')
3225
3226 # Gate is reset and only B's merge job is queued because
3227 # window shrunk to 1.
3228 self.assertEqual(len(self.builds), 1)
3229 self.assertEqual(self.builds[0].name, 'project-merge')
3230
Paul Belanger174a8272017-03-14 13:20:10 -04003231 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003232 self.waitUntilSettled()
3233
3234 # Only B's test jobs are queued because window is still 1.
3235 self.assertEqual(len(self.builds), 2)
3236 self.assertEqual(self.builds[0].name, 'project-test1')
3237 self.assertEqual(self.builds[1].name, 'project-test2')
3238
Paul Belanger174a8272017-03-14 13:20:10 -04003239 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003240 self.waitUntilSettled()
3241
3242 # B was successfully merged so window is increased to 2.
3243 self.assertEqual(queue.window, 2)
3244 self.assertEqual(queue.window_floor, 1)
3245 self.assertEqual(B.data['status'], 'MERGED')
3246
3247 # Only C is left and its merge job is queued.
3248 self.assertEqual(len(self.builds), 1)
3249 self.assertEqual(self.builds[0].name, 'project-merge')
3250
Paul Belanger174a8272017-03-14 13:20:10 -04003251 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003252 self.waitUntilSettled()
3253
3254 # After successful merge job the test jobs for C are queued.
3255 self.assertEqual(len(self.builds), 2)
3256 self.assertEqual(self.builds[0].name, 'project-test1')
3257 self.assertEqual(self.builds[1].name, 'project-test2')
3258
Paul Belanger174a8272017-03-14 13:20:10 -04003259 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003260 self.waitUntilSettled()
3261
3262 # C successfully merged so window is bumped to 3.
3263 self.assertEqual(queue.window, 3)
3264 self.assertEqual(queue.window_floor, 1)
3265 self.assertEqual(C.data['status'], 'MERGED')
3266
James E. Blaircc324192017-04-19 14:36:08 -07003267 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003268 def test_queue_rate_limiting_dependent(self):
3269 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003270 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003271 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3272 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3273 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3274
3275 B.setDependsOn(A, 1)
3276
Paul Belanger174a8272017-03-14 13:20:10 -04003277 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003278
James E. Blair8b5408c2016-08-08 15:37:46 -07003279 A.addApproval('code-review', 2)
3280 B.addApproval('code-review', 2)
3281 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003282
James E. Blair8b5408c2016-08-08 15:37:46 -07003283 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3284 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3285 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003286 self.waitUntilSettled()
3287
3288 # Only A and B will have their merge jobs queued because
3289 # window is 2.
3290 self.assertEqual(len(self.builds), 2)
3291 self.assertEqual(self.builds[0].name, 'project-merge')
3292 self.assertEqual(self.builds[1].name, 'project-merge')
3293
James E. Blairf4a5f022017-04-18 14:01:10 -07003294 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003295
3296 # Only A and B will have their test jobs queued because
3297 # window is 2.
3298 self.assertEqual(len(self.builds), 4)
3299 self.assertEqual(self.builds[0].name, 'project-test1')
3300 self.assertEqual(self.builds[1].name, 'project-test2')
3301 self.assertEqual(self.builds[2].name, 'project-test1')
3302 self.assertEqual(self.builds[3].name, 'project-test2')
3303
Paul Belanger174a8272017-03-14 13:20:10 -04003304 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003305 self.waitUntilSettled()
3306
James E. Blaircc324192017-04-19 14:36:08 -07003307 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003308 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003309 # A failed so window is reduced by 1 to 1.
3310 self.assertEqual(queue.window, 1)
3311 self.assertEqual(queue.window_floor, 1)
3312 self.assertEqual(A.data['status'], 'NEW')
3313 self.assertEqual(B.data['status'], 'NEW')
3314
3315 # Gate is reset and only C's merge job is queued because
3316 # window shrunk to 1 and A and B were dequeued.
3317 self.assertEqual(len(self.builds), 1)
3318 self.assertEqual(self.builds[0].name, 'project-merge')
3319
James E. Blairf4a5f022017-04-18 14:01:10 -07003320 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003321
3322 # Only C's test jobs are queued because window is still 1.
3323 self.assertEqual(len(self.builds), 2)
3324 self.assertEqual(self.builds[0].name, 'project-test1')
3325 self.assertEqual(self.builds[1].name, 'project-test2')
3326
Paul Belanger174a8272017-03-14 13:20:10 -04003327 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003328 self.waitUntilSettled()
3329
3330 # C was successfully merged so window is increased to 2.
3331 self.assertEqual(queue.window, 2)
3332 self.assertEqual(queue.window_floor, 1)
3333 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003334
James E. Blairec056492016-07-22 09:45:56 -07003335 @skip("Disabled for early v3 development")
Joshua Heskethba8776a2014-01-12 14:35:40 +08003336 def test_worker_update_metadata(self):
3337 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003338 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003339
3340 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003341 A.addApproval('code-review', 2)
3342 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003343 self.waitUntilSettled()
3344
Paul Belanger174a8272017-03-14 13:20:10 -04003345 self.assertEqual(len(self.executor.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003346
3347 self.log.debug('Current builds:')
Paul Belanger174a8272017-03-14 13:20:10 -04003348 self.log.debug(self.executor.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003349
3350 start = time.time()
3351 while True:
3352 if time.time() - start > 10:
3353 raise Exception("Timeout waiting for gearman server to report "
3354 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003355 build = list(self.executor.builds.values())[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003356 if build.worker.name == "My Worker":
3357 break
3358 else:
3359 time.sleep(0)
3360
3361 self.log.debug(build)
3362 self.assertEqual("My Worker", build.worker.name)
3363 self.assertEqual("localhost", build.worker.hostname)
3364 self.assertEqual(['127.0.0.1', '192.168.1.1'], build.worker.ips)
3365 self.assertEqual("zuul.example.org", build.worker.fqdn)
3366 self.assertEqual("FakeBuilder", build.worker.program)
3367 self.assertEqual("v1.1", build.worker.version)
3368 self.assertEqual({'something': 'else'}, build.worker.extra)
3369
Paul Belanger174a8272017-03-14 13:20:10 -04003370 self.executor_server.hold_jobs_in_build = False
3371 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003372 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003373
James E. Blaircc324192017-04-19 14:36:08 -07003374 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003375 def test_footer_message(self):
3376 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003377 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003378 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003379 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003380 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003381 self.waitUntilSettled()
3382
3383 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003384 B.addApproval('code-review', 2)
3385 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003386 self.waitUntilSettled()
3387
3388 self.assertEqual(2, len(self.smtp_messages))
3389
Adam Gandelman82258fd2017-02-07 16:51:58 -08003390 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003391Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003392http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003393
Adam Gandelman82258fd2017-02-07 16:51:58 -08003394 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003395For CI problems and help debugging, contact ci@example.org"""
3396
Adam Gandelman82258fd2017-02-07 16:51:58 -08003397 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3398 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3399 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3400 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003401
James E. Blairec056492016-07-22 09:45:56 -07003402 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003403 def test_merge_failure_reporters(self):
3404 """Check that the config is set up correctly"""
3405
James E. Blairf84026c2015-12-08 16:11:46 -08003406 self.updateConfigLayout(
3407 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003408 self.sched.reconfigure(self.config)
3409 self.registerJobs()
3410
3411 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003412 "Merge Failed.\n\nThis change or one of its cross-repo "
3413 "dependencies was unable to be automatically merged with the "
3414 "current state of its repository. Please rebase the change and "
3415 "upload a new patchset.",
Joshua Heskethb7179772014-01-30 23:30:46 +11003416 self.sched.layout.pipelines['check'].merge_failure_message)
3417 self.assertEqual(
3418 "The merge failed! For more information...",
3419 self.sched.layout.pipelines['gate'].merge_failure_message)
3420
3421 self.assertEqual(
3422 len(self.sched.layout.pipelines['check'].merge_failure_actions), 1)
3423 self.assertEqual(
3424 len(self.sched.layout.pipelines['gate'].merge_failure_actions), 2)
3425
3426 self.assertTrue(isinstance(
Joshua Heskethde958652015-11-10 19:19:50 +11003427 self.sched.layout.pipelines['check'].merge_failure_actions[0],
3428 zuul.reporter.gerrit.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003429
3430 self.assertTrue(
3431 (
3432 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003433 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003434 zuul.reporter.smtp.SMTPReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003435 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003436 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003437 zuul.reporter.gerrit.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003438 ) or (
3439 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003440 merge_failure_actions[0],
Joshua Heskethffe42062014-09-05 21:43:52 +10003441 zuul.reporter.gerrit.GerritReporter) and
Joshua Heskethb7179772014-01-30 23:30:46 +11003442 isinstance(self.sched.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003443 merge_failure_actions[1],
Joshua Heskethffe42062014-09-05 21:43:52 +10003444 zuul.reporter.smtp.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003445 )
3446 )
3447
James E. Blairec056492016-07-22 09:45:56 -07003448 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003449 def test_merge_failure_reports(self):
3450 """Check that when a change fails to merge the correct message is sent
3451 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003452 self.updateConfigLayout(
3453 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003454 self.sched.reconfigure(self.config)
3455 self.registerJobs()
3456
3457 # Check a test failure isn't reported to SMTP
3458 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003459 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003460 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003461 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003462 self.waitUntilSettled()
3463
3464 self.assertEqual(3, len(self.history)) # 3 jobs
3465 self.assertEqual(0, len(self.smtp_messages))
3466
3467 # Check a merge failure is reported to SMTP
3468 # B should be merged, but C will conflict with B
3469 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3470 B.addPatchset(['conflict'])
3471 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3472 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003473 B.addApproval('code-review', 2)
3474 C.addApproval('code-review', 2)
3475 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3476 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003477 self.waitUntilSettled()
3478
3479 self.assertEqual(6, len(self.history)) # A and B jobs
3480 self.assertEqual(1, len(self.smtp_messages))
3481 self.assertEqual('The merge failed! For more information...',
3482 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003483
James E. Blairec056492016-07-22 09:45:56 -07003484 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003485 def test_default_merge_failure_reports(self):
3486 """Check that the default merge failure reports are correct."""
3487
3488 # A should report success, B should report merge failure.
3489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3490 A.addPatchset(['conflict'])
3491 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3492 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003493 A.addApproval('code-review', 2)
3494 B.addApproval('code-review', 2)
3495 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3496 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003497 self.waitUntilSettled()
3498
3499 self.assertEqual(3, len(self.history)) # A jobs
3500 self.assertEqual(A.reported, 2)
3501 self.assertEqual(B.reported, 2)
3502 self.assertEqual(A.data['status'], 'MERGED')
3503 self.assertEqual(B.data['status'], 'NEW')
3504 self.assertIn('Build succeeded', A.messages[1])
3505 self.assertIn('Merge Failed', B.messages[1])
3506 self.assertIn('automatically merged', B.messages[1])
3507 self.assertNotIn('logs.example.com', B.messages[1])
3508 self.assertNotIn('SKIPPED', B.messages[1])
3509
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003510 def test_client_get_running_jobs(self):
3511 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003512 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003513 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003514 A.addApproval('code-review', 2)
3515 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003516 self.waitUntilSettled()
3517
3518 client = zuul.rpcclient.RPCClient('127.0.0.1',
3519 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003520 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003521
3522 # Wait for gearman server to send the initial workData back to zuul
3523 start = time.time()
3524 while True:
3525 if time.time() - start > 10:
3526 raise Exception("Timeout waiting for gearman server to report "
3527 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003528 build = list(self.executor_client.builds.values())[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003529 if build.worker.name == "My Worker":
3530 break
3531 else:
3532 time.sleep(0)
3533
3534 running_items = client.get_running_jobs()
3535
3536 self.assertEqual(1, len(running_items))
3537 running_item = running_items[0]
3538 self.assertEqual([], running_item['failing_reasons'])
3539 self.assertEqual([], running_item['items_behind'])
3540 self.assertEqual('https://hostname/1', running_item['url'])
3541 self.assertEqual(None, running_item['item_ahead'])
3542 self.assertEqual('org/project', running_item['project'])
3543 self.assertEqual(None, running_item['remaining_time'])
3544 self.assertEqual(True, running_item['active'])
3545 self.assertEqual('1,1', running_item['id'])
3546
3547 self.assertEqual(3, len(running_item['jobs']))
3548 for job in running_item['jobs']:
3549 if job['name'] == 'project-merge':
3550 self.assertEqual('project-merge', job['name'])
3551 self.assertEqual('gate', job['pipeline'])
3552 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003553 self.assertEqual('https://server/job/project-merge/0/',
3554 job['url'])
3555 self.assertEqual(7, len(job['worker']))
3556 self.assertEqual(False, job['canceled'])
3557 self.assertEqual(True, job['voting'])
3558 self.assertEqual(None, job['result'])
3559 self.assertEqual('gate', job['pipeline'])
3560 break
3561
Paul Belanger174a8272017-03-14 13:20:10 -04003562 self.executor_server.hold_jobs_in_build = False
3563 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003564 self.waitUntilSettled()
3565
3566 running_items = client.get_running_jobs()
3567 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003568
James E. Blairb70e55a2017-04-19 12:57:02 -07003569 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003570 def test_nonvoting_pipeline(self):
3571 "Test that a nonvoting pipeline (experimental) can still report"
3572
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003573 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3574 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003575 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3576 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003577
3578 self.assertEqual(self.getJobFromHistory('project-merge').result,
3579 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003580 self.assertEqual(
3581 self.getJobFromHistory('experimental-project-test').result,
3582 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003583 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003584
3585 def test_crd_gate(self):
3586 "Test cross-repo dependencies"
3587 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3588 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003589 A.addApproval('code-review', 2)
3590 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003591
3592 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3593 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3594 AM2.setMerged()
3595 AM1.setMerged()
3596
3597 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3598 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3599 BM2.setMerged()
3600 BM1.setMerged()
3601
3602 # A -> AM1 -> AM2
3603 # B -> BM1 -> BM2
3604 # A Depends-On: B
3605 # M2 is here to make sure it is never queried. If it is, it
3606 # means zuul is walking down the entire history of merged
3607 # changes.
3608
3609 B.setDependsOn(BM1, 1)
3610 BM1.setDependsOn(BM2, 1)
3611
3612 A.setDependsOn(AM1, 1)
3613 AM1.setDependsOn(AM2, 1)
3614
3615 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3616 A.subject, B.data['id'])
3617
James E. Blair8b5408c2016-08-08 15:37:46 -07003618 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003619 self.waitUntilSettled()
3620
3621 self.assertEqual(A.data['status'], 'NEW')
3622 self.assertEqual(B.data['status'], 'NEW')
3623
Paul Belanger5dccbe72016-11-14 11:17:37 -05003624 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003625 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003626
Paul Belanger174a8272017-03-14 13:20:10 -04003627 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003628 B.addApproval('approved', 1)
3629 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003630 self.waitUntilSettled()
3631
Paul Belanger174a8272017-03-14 13:20:10 -04003632 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003633 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003634 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003635 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003636 self.executor_server.hold_jobs_in_build = False
3637 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003638 self.waitUntilSettled()
3639
3640 self.assertEqual(AM2.queried, 0)
3641 self.assertEqual(BM2.queried, 0)
3642 self.assertEqual(A.data['status'], 'MERGED')
3643 self.assertEqual(B.data['status'], 'MERGED')
3644 self.assertEqual(A.reported, 2)
3645 self.assertEqual(B.reported, 2)
3646
Paul Belanger5dccbe72016-11-14 11:17:37 -05003647 changes = self.getJobFromHistory(
3648 'project-merge', 'org/project1').changes
3649 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003650
3651 def test_crd_branch(self):
3652 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003653
3654 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003655 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3656 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3657 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3658 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003659 A.addApproval('code-review', 2)
3660 B.addApproval('code-review', 2)
3661 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003662
3663 # A Depends-On: B+C
3664 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3665 A.subject, B.data['id'])
3666
Paul Belanger174a8272017-03-14 13:20:10 -04003667 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003668 B.addApproval('approved', 1)
3669 C.addApproval('approved', 1)
3670 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003671 self.waitUntilSettled()
3672
Paul Belanger174a8272017-03-14 13:20:10 -04003673 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003674 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003675 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003676 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003677 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003678 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003679 self.executor_server.hold_jobs_in_build = False
3680 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003681 self.waitUntilSettled()
3682
3683 self.assertEqual(A.data['status'], 'MERGED')
3684 self.assertEqual(B.data['status'], 'MERGED')
3685 self.assertEqual(C.data['status'], 'MERGED')
3686 self.assertEqual(A.reported, 2)
3687 self.assertEqual(B.reported, 2)
3688 self.assertEqual(C.reported, 2)
3689
Paul Belanger6379db12016-11-14 13:57:54 -05003690 changes = self.getJobFromHistory(
3691 'project-merge', 'org/project1').changes
3692 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003693
3694 def test_crd_multiline(self):
3695 "Test multiple depends-on lines in commit"
3696 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3697 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3698 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003699 A.addApproval('code-review', 2)
3700 B.addApproval('code-review', 2)
3701 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003702
3703 # A Depends-On: B+C
3704 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3705 A.subject, B.data['id'], C.data['id'])
3706
Paul Belanger174a8272017-03-14 13:20:10 -04003707 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003708 B.addApproval('approved', 1)
3709 C.addApproval('approved', 1)
3710 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003711 self.waitUntilSettled()
3712
Paul Belanger174a8272017-03-14 13:20:10 -04003713 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003714 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003715 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003716 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003717 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003718 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003719 self.executor_server.hold_jobs_in_build = False
3720 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003721 self.waitUntilSettled()
3722
3723 self.assertEqual(A.data['status'], 'MERGED')
3724 self.assertEqual(B.data['status'], 'MERGED')
3725 self.assertEqual(C.data['status'], 'MERGED')
3726 self.assertEqual(A.reported, 2)
3727 self.assertEqual(B.reported, 2)
3728 self.assertEqual(C.reported, 2)
3729
Paul Belanger5dccbe72016-11-14 11:17:37 -05003730 changes = self.getJobFromHistory(
3731 'project-merge', 'org/project1').changes
3732 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003733
3734 def test_crd_unshared_gate(self):
3735 "Test cross-repo dependencies in unshared gate queues"
3736 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3737 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003738 A.addApproval('code-review', 2)
3739 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003740
3741 # A Depends-On: B
3742 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3743 A.subject, B.data['id'])
3744
3745 # A and B do not share a queue, make sure that A is unable to
3746 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003747 B.addApproval('approved', 1)
3748 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003749 self.waitUntilSettled()
3750
3751 self.assertEqual(A.data['status'], 'NEW')
3752 self.assertEqual(B.data['status'], 'NEW')
3753 self.assertEqual(A.reported, 0)
3754 self.assertEqual(B.reported, 0)
3755 self.assertEqual(len(self.history), 0)
3756
3757 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003758 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003759 self.waitUntilSettled()
3760
3761 self.assertEqual(B.data['status'], 'MERGED')
3762 self.assertEqual(B.reported, 2)
3763
3764 # Now that B is merged, A should be able to be enqueued and
3765 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003766 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003767 self.waitUntilSettled()
3768
3769 self.assertEqual(A.data['status'], 'MERGED')
3770 self.assertEqual(A.reported, 2)
3771
James E. Blair96698e22015-04-02 07:48:21 -07003772 def test_crd_gate_reverse(self):
3773 "Test reverse cross-repo dependencies"
3774 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3775 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003776 A.addApproval('code-review', 2)
3777 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003778
3779 # A Depends-On: B
3780
3781 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3782 A.subject, B.data['id'])
3783
James E. Blair8b5408c2016-08-08 15:37:46 -07003784 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003785 self.waitUntilSettled()
3786
3787 self.assertEqual(A.data['status'], 'NEW')
3788 self.assertEqual(B.data['status'], 'NEW')
3789
Paul Belanger174a8272017-03-14 13:20:10 -04003790 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003791 A.addApproval('approved', 1)
3792 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003793 self.waitUntilSettled()
3794
Paul Belanger174a8272017-03-14 13:20:10 -04003795 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003796 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003797 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003798 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003799 self.executor_server.hold_jobs_in_build = False
3800 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003801 self.waitUntilSettled()
3802
3803 self.assertEqual(A.data['status'], 'MERGED')
3804 self.assertEqual(B.data['status'], 'MERGED')
3805 self.assertEqual(A.reported, 2)
3806 self.assertEqual(B.reported, 2)
3807
Paul Belanger5dccbe72016-11-14 11:17:37 -05003808 changes = self.getJobFromHistory(
3809 'project-merge', 'org/project1').changes
3810 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003811
James E. Blair5ee24252014-12-30 10:12:29 -08003812 def test_crd_cycle(self):
3813 "Test cross-repo dependency cycles"
3814 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3815 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003816 A.addApproval('code-review', 2)
3817 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003818
3819 # A -> B -> A (via commit-depends)
3820
3821 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3822 A.subject, B.data['id'])
3823 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3824 B.subject, A.data['id'])
3825
James E. Blair8b5408c2016-08-08 15:37:46 -07003826 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003827 self.waitUntilSettled()
3828
3829 self.assertEqual(A.reported, 0)
3830 self.assertEqual(B.reported, 0)
3831 self.assertEqual(A.data['status'], 'NEW')
3832 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003833
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003834 def test_crd_gate_unknown(self):
3835 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003836 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003837 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3838 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003839 A.addApproval('code-review', 2)
3840 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003841
3842 # A Depends-On: B
3843 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3844 A.subject, B.data['id'])
3845
James E. Blair8b5408c2016-08-08 15:37:46 -07003846 B.addApproval('approved', 1)
3847 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003848 self.waitUntilSettled()
3849
3850 # Unknown projects cannot share a queue with any other
3851 # since they don't have common jobs with any other (they have no jobs).
3852 # Changes which depend on unknown project changes
3853 # should not be processed in dependent pipeline
3854 self.assertEqual(A.data['status'], 'NEW')
3855 self.assertEqual(B.data['status'], 'NEW')
3856 self.assertEqual(A.reported, 0)
3857 self.assertEqual(B.reported, 0)
3858 self.assertEqual(len(self.history), 0)
3859
3860 # Simulate change B being gated outside this layout
James E. Blair8b5408c2016-08-08 15:37:46 -07003861 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003862 B.setMerged()
3863 self.waitUntilSettled()
3864 self.assertEqual(len(self.history), 0)
3865
3866 # Now that B is merged, A should be able to be enqueued and
3867 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003869 self.waitUntilSettled()
3870
3871 self.assertEqual(A.data['status'], 'MERGED')
3872 self.assertEqual(A.reported, 2)
3873 self.assertEqual(B.data['status'], 'MERGED')
3874 self.assertEqual(B.reported, 0)
3875
Clint Byrum8897dca2017-05-10 19:14:19 -07003876 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blairbfb8e042014-12-30 17:01:44 -08003877 def test_crd_check(self):
3878 "Test cross-repo dependencies in independent pipelines"
3879
Paul Belanger174a8272017-03-14 13:20:10 -04003880 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003881 self.gearman_server.hold_jobs_in_queue = True
3882 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3883 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3884
3885 # A Depends-On: B
3886 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3887 A.subject, B.data['id'])
3888
3889 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3890 self.waitUntilSettled()
3891
James E. Blairbfb8e042014-12-30 17:01:44 -08003892 self.gearman_server.hold_jobs_in_queue = False
3893 self.gearman_server.release()
3894 self.waitUntilSettled()
3895
Paul Belanger174a8272017-03-14 13:20:10 -04003896 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003897 self.waitUntilSettled()
3898
James E. Blair2a535672017-04-27 12:03:15 -07003899 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003900
Paul Belanger174a8272017-03-14 13:20:10 -04003901 self.executor_server.hold_jobs_in_build = False
3902 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003903 self.waitUntilSettled()
3904
James E. Blairbfb8e042014-12-30 17:01:44 -08003905 self.assertEqual(A.data['status'], 'NEW')
3906 self.assertEqual(B.data['status'], 'NEW')
3907 self.assertEqual(A.reported, 1)
3908 self.assertEqual(B.reported, 0)
3909
3910 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003911 tenant = self.sched.abide.tenants.get('tenant-one')
3912 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003913
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003914 def test_crd_check_git_depends(self):
3915 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003916 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003917 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3918 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3919
3920 # Add two git-dependent changes and make sure they both report
3921 # success.
3922 B.setDependsOn(A, 1)
3923 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3924 self.waitUntilSettled()
3925 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3926 self.waitUntilSettled()
3927
James E. Blairb8c16472015-05-05 14:55:26 -07003928 self.orderedRelease()
3929 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003930 self.waitUntilSettled()
3931
3932 self.assertEqual(A.data['status'], 'NEW')
3933 self.assertEqual(B.data['status'], 'NEW')
3934 self.assertEqual(A.reported, 1)
3935 self.assertEqual(B.reported, 1)
3936
3937 self.assertEqual(self.history[0].changes, '1,1')
3938 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003939 tenant = self.sched.abide.tenants.get('tenant-one')
3940 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003941
3942 self.assertIn('Build succeeded', A.messages[0])
3943 self.assertIn('Build succeeded', B.messages[0])
3944
3945 def test_crd_check_duplicate(self):
3946 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003947 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003948 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3949 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003950 tenant = self.sched.abide.tenants.get('tenant-one')
3951 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003952
3953 # Add two git-dependent changes...
3954 B.setDependsOn(A, 1)
3955 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3956 self.waitUntilSettled()
3957 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3958
3959 # ...make sure the live one is not duplicated...
3960 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3961 self.waitUntilSettled()
3962 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3963
3964 # ...but the non-live one is able to be.
3965 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3966 self.waitUntilSettled()
3967 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3968
Clark Boylandd849822015-03-02 12:38:14 -08003969 # Release jobs in order to avoid races with change A jobs
3970 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003971 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003972 self.executor_server.hold_jobs_in_build = False
3973 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003974 self.waitUntilSettled()
3975
3976 self.assertEqual(A.data['status'], 'NEW')
3977 self.assertEqual(B.data['status'], 'NEW')
3978 self.assertEqual(A.reported, 1)
3979 self.assertEqual(B.reported, 1)
3980
3981 self.assertEqual(self.history[0].changes, '1,1 2,1')
3982 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003983 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003984
3985 self.assertIn('Build succeeded', A.messages[0])
3986 self.assertIn('Build succeeded', B.messages[0])
3987
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003988 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003989 "Test cross-repo dependencies re-enqueued in independent pipelines"
3990
3991 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003992 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3993 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003994
3995 # A Depends-On: B
3996 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3997 A.subject, B.data['id'])
3998
3999 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4000 self.waitUntilSettled()
4001
4002 self.sched.reconfigure(self.config)
4003
4004 # Make sure the items still share a change queue, and the
4005 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004006 tenant = self.sched.abide.tenants.get('tenant-one')
4007 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4008 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004009 first_item = queue.queue[0]
4010 for item in queue.queue:
4011 self.assertEqual(item.queue, first_item.queue)
4012 self.assertFalse(first_item.live)
4013 self.assertTrue(queue.queue[1].live)
4014
4015 self.gearman_server.hold_jobs_in_queue = False
4016 self.gearman_server.release()
4017 self.waitUntilSettled()
4018
4019 self.assertEqual(A.data['status'], 'NEW')
4020 self.assertEqual(B.data['status'], 'NEW')
4021 self.assertEqual(A.reported, 1)
4022 self.assertEqual(B.reported, 0)
4023
4024 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004025 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004026
Clint Byrum8897dca2017-05-10 19:14:19 -07004027 @skipIf(sys.version_info.major > 2, 'Fails on py3')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004028 def test_crd_check_reconfiguration(self):
4029 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4030
Clint Byrum8897dca2017-05-10 19:14:19 -07004031 @skipIf(sys.version_info.major > 2, 'Fails on py3')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004032 def test_crd_undefined_project(self):
4033 """Test that undefined projects in dependencies are handled for
4034 independent pipelines"""
4035 # It's a hack for fake gerrit,
4036 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004037 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004038 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4039
Jesse Keatingea68ab42017-04-19 15:03:30 -07004040 @simple_layout('layouts/ignore-dependencies.yaml')
Clint Byrum8897dca2017-05-10 19:14:19 -07004041 @skipIf(sys.version_info.major > 2, 'Fails on py3')
James E. Blair17dd6772015-02-09 14:45:18 -08004042 def test_crd_check_ignore_dependencies(self):
4043 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004044
4045 self.gearman_server.hold_jobs_in_queue = True
4046 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4047 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4048 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4049
4050 # A Depends-On: B
4051 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4052 A.subject, B.data['id'])
4053 # C git-depends on B
4054 C.setDependsOn(B, 1)
4055 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4056 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4057 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4058 self.waitUntilSettled()
4059
4060 # Make sure none of the items share a change queue, and all
4061 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004062 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004063 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004064 self.assertEqual(len(check_pipeline.queues), 3)
4065 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4066 for item in check_pipeline.getAllItems():
4067 self.assertTrue(item.live)
4068
4069 self.gearman_server.hold_jobs_in_queue = False
4070 self.gearman_server.release()
4071 self.waitUntilSettled()
4072
4073 self.assertEqual(A.data['status'], 'NEW')
4074 self.assertEqual(B.data['status'], 'NEW')
4075 self.assertEqual(C.data['status'], 'NEW')
4076 self.assertEqual(A.reported, 1)
4077 self.assertEqual(B.reported, 1)
4078 self.assertEqual(C.reported, 1)
4079
4080 # Each job should have tested exactly one change
4081 for job in self.history:
4082 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004083
James E. Blair7cb84542017-04-19 13:35:05 -07004084 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004085 def test_crd_check_transitive(self):
4086 "Test transitive cross-repo dependencies"
4087 # Specifically, if A -> B -> C, and C gets a new patchset and
4088 # A gets a new patchset, ensure the test of A,2 includes B,1
4089 # and C,2 (not C,1 which would indicate stale data in the
4090 # cache for B).
4091 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4092 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4093 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4094
4095 # A Depends-On: B
4096 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4097 A.subject, B.data['id'])
4098
4099 # B Depends-On: C
4100 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4101 B.subject, C.data['id'])
4102
4103 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4104 self.waitUntilSettled()
4105 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4106
4107 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4108 self.waitUntilSettled()
4109 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4110
4111 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4112 self.waitUntilSettled()
4113 self.assertEqual(self.history[-1].changes, '3,1')
4114
4115 C.addPatchset()
4116 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4117 self.waitUntilSettled()
4118 self.assertEqual(self.history[-1].changes, '3,2')
4119
4120 A.addPatchset()
4121 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4122 self.waitUntilSettled()
4123 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004124
James E. Blaira8b90b32016-08-24 15:18:50 -07004125 def test_crd_check_unknown(self):
4126 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004127 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004128 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4129 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4130 # A Depends-On: B
4131 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4132 A.subject, B.data['id'])
4133
4134 # Make sure zuul has seen an event on B.
4135 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4136 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4137 self.waitUntilSettled()
4138
4139 self.assertEqual(A.data['status'], 'NEW')
4140 self.assertEqual(A.reported, 1)
4141 self.assertEqual(B.data['status'], 'NEW')
4142 self.assertEqual(B.reported, 0)
4143
James E. Blair92464a22016-04-05 10:21:26 -07004144 def test_crd_cycle_join(self):
4145 "Test an updated change creates a cycle"
4146 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4147
4148 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4149 self.waitUntilSettled()
4150
4151 # Create B->A
4152 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4153 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4154 B.subject, A.data['id'])
4155 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4156 self.waitUntilSettled()
4157
4158 # Update A to add A->B (a cycle).
4159 A.addPatchset()
4160 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4161 A.subject, B.data['id'])
4162 # Normally we would submit the patchset-created event for
4163 # processing here, however, we have no way of noting whether
4164 # the dependency cycle detection correctly raised an
4165 # exception, so instead, we reach into the source driver and
4166 # call the method that would ultimately be called by the event
4167 # processing.
4168
Paul Belanger0e155e22016-11-14 14:12:23 -05004169 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004170 (trusted, project) = tenant.getProject('org/project')
4171 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004172
4173 # TODO(pabelanger): As we add more source / trigger APIs we should make
4174 # it easier for users to create events for testing.
4175 event = zuul.model.TriggerEvent()
4176 event.trigger_name = 'gerrit'
4177 event.change_number = '1'
4178 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004179 with testtools.ExpectedException(
4180 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004181 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004182 self.log.debug("Got expected dependency cycle exception")
4183
4184 # Now if we update B to remove the depends-on, everything
4185 # should be okay. B; A->B
4186
4187 B.addPatchset()
4188 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004189
4190 source.getChange(event, True)
4191 event.change_number = '2'
4192 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004193
Jesse Keatingcc67b122017-04-19 14:54:57 -07004194 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004195 def test_disable_at(self):
4196 "Test a pipeline will only report to the disabled trigger when failing"
4197
Jesse Keatingcc67b122017-04-19 14:54:57 -07004198 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004199 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004200 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004201 0, tenant.layout.pipelines['check']._consecutive_failures)
4202 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004203
4204 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4205 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4206 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4207 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4208 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4209 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4210 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4211 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4212 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4213 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4214 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4215
Paul Belanger174a8272017-03-14 13:20:10 -04004216 self.executor_server.failJob('project-test1', A)
4217 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004218 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004219 self.executor_server.failJob('project-test1', D)
4220 self.executor_server.failJob('project-test1', E)
4221 self.executor_server.failJob('project-test1', F)
4222 self.executor_server.failJob('project-test1', G)
4223 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004224 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004225 self.executor_server.failJob('project-test1', J)
4226 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004227
4228 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4229 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4230 self.waitUntilSettled()
4231
4232 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004233 2, tenant.layout.pipelines['check']._consecutive_failures)
4234 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004235
4236 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4237 self.waitUntilSettled()
4238
4239 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004240 0, tenant.layout.pipelines['check']._consecutive_failures)
4241 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004242
4243 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4244 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4245 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4246 self.waitUntilSettled()
4247
4248 # We should be disabled now
4249 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004250 3, tenant.layout.pipelines['check']._consecutive_failures)
4251 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004252
4253 # We need to wait between each of these patches to make sure the
4254 # smtp messages come back in an expected order
4255 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4256 self.waitUntilSettled()
4257 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4258 self.waitUntilSettled()
4259 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4260 self.waitUntilSettled()
4261
4262 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4263 # leaving a message on each change
4264 self.assertEqual(1, len(A.messages))
4265 self.assertIn('Build failed.', A.messages[0])
4266 self.assertEqual(1, len(B.messages))
4267 self.assertIn('Build failed.', B.messages[0])
4268 self.assertEqual(1, len(C.messages))
4269 self.assertIn('Build succeeded.', C.messages[0])
4270 self.assertEqual(1, len(D.messages))
4271 self.assertIn('Build failed.', D.messages[0])
4272 self.assertEqual(1, len(E.messages))
4273 self.assertIn('Build failed.', E.messages[0])
4274 self.assertEqual(1, len(F.messages))
4275 self.assertIn('Build failed.', F.messages[0])
4276
4277 # The last 3 (GHI) would have only reported via smtp.
4278 self.assertEqual(3, len(self.smtp_messages))
4279 self.assertEqual(0, len(G.messages))
4280 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004281 self.assertIn(
4282 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004283 self.assertEqual(0, len(H.messages))
4284 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004285 self.assertIn(
4286 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004287 self.assertEqual(0, len(I.messages))
4288 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004289 self.assertIn(
4290 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004291
4292 # Now reload the configuration (simulate a HUP) to check the pipeline
4293 # comes out of disabled
4294 self.sched.reconfigure(self.config)
4295
Jesse Keatingcc67b122017-04-19 14:54:57 -07004296 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004297
4298 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004299 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004300 0, tenant.layout.pipelines['check']._consecutive_failures)
4301 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004302
4303 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4304 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4305 self.waitUntilSettled()
4306
4307 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004308 2, tenant.layout.pipelines['check']._consecutive_failures)
4309 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004310
4311 # J and K went back to gerrit
4312 self.assertEqual(1, len(J.messages))
4313 self.assertIn('Build failed.', J.messages[0])
4314 self.assertEqual(1, len(K.messages))
4315 self.assertIn('Build failed.', K.messages[0])
4316 # No more messages reported via smtp
4317 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004318
James E. Blaircc020532017-04-19 13:04:44 -07004319 @simple_layout('layouts/one-job-project.yaml')
4320 def test_one_job_project(self):
4321 "Test that queueing works with one job"
4322 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4323 'master', 'A')
4324 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4325 'master', 'B')
4326 A.addApproval('code-review', 2)
4327 B.addApproval('code-review', 2)
4328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4329 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4330 self.waitUntilSettled()
4331
4332 self.assertEqual(A.data['status'], 'MERGED')
4333 self.assertEqual(A.reported, 2)
4334 self.assertEqual(B.data['status'], 'MERGED')
4335 self.assertEqual(B.reported, 2)
4336
Paul Belanger71d98172016-11-08 10:56:31 -05004337 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004338 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004339
Paul Belanger174a8272017-03-14 13:20:10 -04004340 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4342 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4343 self.waitUntilSettled()
4344
Paul Belanger174a8272017-03-14 13:20:10 -04004345 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004346 self.waitUntilSettled()
4347
4348 self.assertEqual(len(self.builds), 2)
4349 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004350 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004351 self.waitUntilSettled()
4352
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004353 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004354 self.assertEqual(len(self.builds), 1,
4355 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004356 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004357 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004358 self.waitUntilSettled()
4359
Paul Belanger174a8272017-03-14 13:20:10 -04004360 self.executor_server.hold_jobs_in_build = False
4361 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004362 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004363 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004364 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4365 self.assertEqual(A.reported, 1)
4366 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004367
James E. Blair15be0e12017-01-03 13:45:20 -08004368 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004369 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004370
4371 self.fake_nodepool.paused = True
4372 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4373 A.addApproval('code-review', 2)
4374 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4375 self.waitUntilSettled()
4376
4377 self.zk.client.stop()
4378 self.zk.client.start()
4379 self.fake_nodepool.paused = False
4380 self.waitUntilSettled()
4381
4382 self.assertEqual(A.data['status'], 'MERGED')
4383 self.assertEqual(A.reported, 2)
4384
James E. Blair6ab79e02017-01-06 10:10:17 -08004385 def test_nodepool_failure(self):
4386 "Test that jobs are reported after a nodepool failure"
4387
4388 self.fake_nodepool.paused = True
4389 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4390 A.addApproval('code-review', 2)
4391 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4392 self.waitUntilSettled()
4393
4394 req = self.fake_nodepool.getNodeRequests()[0]
4395 self.fake_nodepool.addFailRequest(req)
4396
4397 self.fake_nodepool.paused = False
4398 self.waitUntilSettled()
4399
4400 self.assertEqual(A.data['status'], 'NEW')
4401 self.assertEqual(A.reported, 2)
4402 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4403 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4404 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4405
Arieb6f068c2016-10-09 13:11:06 +03004406
James E. Blaira002b032017-04-18 10:35:48 -07004407class TestExecutor(ZuulTestCase):
4408 tenant_config_file = 'config/single-tenant/main.yaml'
4409
4410 def assertFinalState(self):
4411 # In this test, we expect to shut down in a non-final state,
4412 # so skip these checks.
4413 pass
4414
4415 def assertCleanShutdown(self):
4416 self.log.debug("Assert clean shutdown")
4417
4418 # After shutdown, make sure no jobs are running
4419 self.assertEqual({}, self.executor_server.job_workers)
4420
4421 # Make sure that git.Repo objects have been garbage collected.
4422 repos = []
4423 gc.collect()
4424 for obj in gc.get_objects():
4425 if isinstance(obj, git.Repo):
4426 self.log.debug("Leaked git repo object: %s" % repr(obj))
4427 repos.append(obj)
4428 self.assertEqual(len(repos), 0)
4429
4430 def test_executor_shutdown(self):
4431 "Test that the executor can shut down with jobs running"
4432
4433 self.executor_server.hold_jobs_in_build = True
4434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4435 A.addApproval('code-review', 2)
4436 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4437 self.waitUntilSettled()
4438
4439
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004440class TestDependencyGraph(ZuulTestCase):
4441 tenant_config_file = 'config/dependency-graph/main.yaml'
4442
4443 def test_dependeny_graph_dispatch_jobs_once(self):
4444 "Test a job in a dependency graph is queued only once"
4445 # Job dependencies, starting with A
4446 # A
4447 # / \
4448 # B C
4449 # / \ / \
4450 # D F E
4451 # |
4452 # G
4453
4454 self.executor_server.hold_jobs_in_build = True
4455 change = self.fake_gerrit.addFakeChange(
4456 'org/project', 'master', 'change')
4457 change.addApproval('code-review', 2)
4458 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4459
4460 self.waitUntilSettled()
4461 self.assertEqual([b.name for b in self.builds], ['A'])
4462
4463 self.executor_server.release('A')
4464 self.waitUntilSettled()
4465 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4466
4467 self.executor_server.release('B')
4468 self.waitUntilSettled()
4469 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4470
4471 self.executor_server.release('D')
4472 self.waitUntilSettled()
4473 self.assertEqual([b.name for b in self.builds], ['C'])
4474
4475 self.executor_server.release('C')
4476 self.waitUntilSettled()
4477 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4478
4479 self.executor_server.release('F')
4480 self.waitUntilSettled()
4481 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4482
4483 self.executor_server.release('G')
4484 self.waitUntilSettled()
4485 self.assertEqual([b.name for b in self.builds], ['E'])
4486
4487 self.executor_server.release('E')
4488 self.waitUntilSettled()
4489 self.assertEqual(len(self.builds), 0)
4490
4491 self.executor_server.hold_jobs_in_build = False
4492 self.executor_server.release()
4493 self.waitUntilSettled()
4494
4495 self.assertEqual(len(self.builds), 0)
4496 self.assertEqual(len(self.history), 7)
4497
4498 self.assertEqual(change.data['status'], 'MERGED')
4499 self.assertEqual(change.reported, 2)
4500
4501 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4502 "Test that a job waits till all dependencies are successful"
4503 # Job dependencies, starting with A
4504 # A
4505 # / \
4506 # B C*
4507 # / \ / \
4508 # D F E
4509 # |
4510 # G
4511
4512 self.executor_server.hold_jobs_in_build = True
4513 change = self.fake_gerrit.addFakeChange(
4514 'org/project', 'master', 'change')
4515 change.addApproval('code-review', 2)
4516
4517 self.executor_server.failJob('C', change)
4518
4519 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4520
4521 self.waitUntilSettled()
4522 self.assertEqual([b.name for b in self.builds], ['A'])
4523
4524 self.executor_server.release('A')
4525 self.waitUntilSettled()
4526 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4527
4528 self.executor_server.release('B')
4529 self.waitUntilSettled()
4530 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4531
4532 self.executor_server.release('D')
4533 self.waitUntilSettled()
4534 self.assertEqual([b.name for b in self.builds], ['C'])
4535
4536 self.executor_server.release('C')
4537 self.waitUntilSettled()
4538 self.assertEqual(len(self.builds), 0)
4539
4540 self.executor_server.hold_jobs_in_build = False
4541 self.executor_server.release()
4542 self.waitUntilSettled()
4543
4544 self.assertEqual(len(self.builds), 0)
4545 self.assertEqual(len(self.history), 4)
4546
4547 self.assertEqual(change.data['status'], 'NEW')
4548 self.assertEqual(change.reported, 2)
4549
4550
Arieb6f068c2016-10-09 13:11:06 +03004551class TestDuplicatePipeline(ZuulTestCase):
4552 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4553
4554 def test_duplicate_pipelines(self):
4555 "Test that a change matching multiple pipelines works"
4556
4557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4558 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4559 self.waitUntilSettled()
4560
4561 self.assertHistory([
4562 dict(name='project-test1', result='SUCCESS', changes='1,1',
4563 pipeline='dup1'),
4564 dict(name='project-test1', result='SUCCESS', changes='1,1',
4565 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004566 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004567
4568 self.assertEqual(len(A.messages), 2)
4569
Paul Belangera46a3742016-11-09 14:23:03 -05004570 if 'dup1' in A.messages[0]:
4571 self.assertIn('dup1', A.messages[0])
4572 self.assertNotIn('dup2', A.messages[0])
4573 self.assertIn('project-test1', A.messages[0])
4574 self.assertIn('dup2', A.messages[1])
4575 self.assertNotIn('dup1', A.messages[1])
4576 self.assertIn('project-test1', A.messages[1])
4577 else:
4578 self.assertIn('dup1', A.messages[1])
4579 self.assertNotIn('dup2', A.messages[1])
4580 self.assertIn('project-test1', A.messages[1])
4581 self.assertIn('dup2', A.messages[0])
4582 self.assertNotIn('dup1', A.messages[0])
4583 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004584
4585
Paul Belanger86085b32016-11-03 12:48:57 -04004586class TestSchedulerTemplatedProject(ZuulTestCase):
4587 tenant_config_file = 'config/templated-project/main.yaml'
4588
Paul Belanger174a8272017-03-14 13:20:10 -04004589 def test_job_from_templates_executed(self):
4590 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004591
4592 A = self.fake_gerrit.addFakeChange(
4593 'org/templated-project', 'master', 'A')
4594 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4595 self.waitUntilSettled()
4596
4597 self.assertEqual(self.getJobFromHistory('project-test1').result,
4598 'SUCCESS')
4599 self.assertEqual(self.getJobFromHistory('project-test2').result,
4600 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004601
4602 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004603 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004604
4605 A = self.fake_gerrit.addFakeChange(
4606 'org/layered-project', 'master', 'A')
4607 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4608 self.waitUntilSettled()
4609
4610 self.assertEqual(self.getJobFromHistory('project-test1').result,
4611 'SUCCESS')
4612 self.assertEqual(self.getJobFromHistory('project-test2').result,
4613 'SUCCESS')
4614 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4615 ).result, 'SUCCESS')
4616 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4617 ).result, 'SUCCESS')
4618 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4619 ).result, 'SUCCESS')
4620 self.assertEqual(self.getJobFromHistory('project-test6').result,
4621 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004622
4623
4624class TestSchedulerSuccessURL(ZuulTestCase):
4625 tenant_config_file = 'config/success-url/main.yaml'
4626
4627 def test_success_url(self):
4628 "Ensure bad build params are ignored"
4629 self.sched.reconfigure(self.config)
4630 self.init_repo('org/docs')
4631
4632 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4633 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4634 self.waitUntilSettled()
4635
4636 # Both builds ran: docs-draft-test + docs-draft-test2
4637 self.assertEqual(len(self.history), 2)
4638
4639 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004640 for build in self.history:
4641 if build.name == 'docs-draft-test':
4642 uuid = build.uuid[:7]
4643 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004644
4645 # Two msgs: 'Starting...' + results
4646 self.assertEqual(len(self.smtp_messages), 2)
4647 body = self.smtp_messages[1]['body'].splitlines()
4648 self.assertEqual('Build succeeded.', body[0])
4649
4650 self.assertIn(
4651 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4652 'docs-draft-test/%s/publish-docs/' % uuid,
4653 body[2])
4654
Paul Belanger174a8272017-03-14 13:20:10 -04004655 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004656 self.assertIn(
4657 '- docs-draft-test2 https://server/job',
4658 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004659
4660
Adam Gandelman4da00f62016-12-09 15:47:33 -08004661class TestSchedulerMerges(ZuulTestCase):
4662 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004663
4664 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004665 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004666 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004667 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004668 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4669 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4670 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4671 A.addApproval('code-review', 2)
4672 B.addApproval('code-review', 2)
4673 C.addApproval('code-review', 2)
4674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4675 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4676 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4677 self.waitUntilSettled()
4678
4679 build = self.builds[-1]
4680 ref = self.getParameter(build, 'ZUUL_REF')
4681
James E. Blair2a535672017-04-27 12:03:15 -07004682 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4683 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004684 repo = git.Repo(path)
4685 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4686 repo_messages.reverse()
4687
Paul Belanger174a8272017-03-14 13:20:10 -04004688 self.executor_server.hold_jobs_in_build = False
4689 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004690 self.waitUntilSettled()
4691
4692 return repo_messages
4693
4694 def _test_merge(self, mode):
4695 us_path = os.path.join(
4696 self.upstream_root, 'org/project-%s' % mode)
4697 expected_messages = [
4698 'initial commit',
4699 'add content from fixture',
4700 # the intermediate commits order is nondeterministic
4701 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4702 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4703 ]
4704 result = self._test_project_merge_mode(mode)
4705 self.assertEqual(result[:2], expected_messages[:2])
4706 self.assertEqual(result[-2:], expected_messages[-2:])
4707
4708 def test_project_merge_mode_merge(self):
4709 self._test_merge('merge')
4710
4711 def test_project_merge_mode_merge_resolve(self):
4712 self._test_merge('merge-resolve')
4713
4714 def test_project_merge_mode_cherrypick(self):
4715 expected_messages = [
4716 'initial commit',
4717 'add content from fixture',
4718 'A-1',
4719 'B-1',
4720 'C-1']
4721 result = self._test_project_merge_mode('cherry-pick')
4722 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004723
4724 def test_merge_branch(self):
4725 "Test that the right commits are on alternate branches"
4726 self.create_branch('org/project-merge-branches', 'mp')
4727
Paul Belanger174a8272017-03-14 13:20:10 -04004728 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004729 A = self.fake_gerrit.addFakeChange(
4730 'org/project-merge-branches', 'mp', 'A')
4731 B = self.fake_gerrit.addFakeChange(
4732 'org/project-merge-branches', 'mp', 'B')
4733 C = self.fake_gerrit.addFakeChange(
4734 'org/project-merge-branches', 'mp', 'C')
4735 A.addApproval('code-review', 2)
4736 B.addApproval('code-review', 2)
4737 C.addApproval('code-review', 2)
4738 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4739 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4740 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4741 self.waitUntilSettled()
4742
Paul Belanger174a8272017-03-14 13:20:10 -04004743 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004744 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004745 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004746 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004747 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004748 self.waitUntilSettled()
4749
4750 build = self.builds[-1]
4751 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4752 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004753 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4754 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004755 repo = git.Repo(path)
4756
4757 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4758 repo_messages.reverse()
4759 correct_messages = [
4760 'initial commit',
4761 'add content from fixture',
4762 'mp commit',
4763 'A-1', 'B-1', 'C-1']
4764 self.assertEqual(repo_messages, correct_messages)
4765
Paul Belanger174a8272017-03-14 13:20:10 -04004766 self.executor_server.hold_jobs_in_build = False
4767 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004768 self.waitUntilSettled()
4769
4770 def test_merge_multi_branch(self):
4771 "Test that dependent changes on multiple branches are merged"
4772 self.create_branch('org/project-merge-branches', 'mp')
4773
Paul Belanger174a8272017-03-14 13:20:10 -04004774 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004775 A = self.fake_gerrit.addFakeChange(
4776 'org/project-merge-branches', 'master', 'A')
4777 B = self.fake_gerrit.addFakeChange(
4778 'org/project-merge-branches', 'mp', 'B')
4779 C = self.fake_gerrit.addFakeChange(
4780 'org/project-merge-branches', 'master', 'C')
4781 A.addApproval('code-review', 2)
4782 B.addApproval('code-review', 2)
4783 C.addApproval('code-review', 2)
4784 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4785 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4786 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4787 self.waitUntilSettled()
4788
4789 job_A = None
4790 for job in self.builds:
4791 if 'project-merge' in job.name:
4792 job_A = job
4793 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4794 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4795 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4796 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4797
James E. Blair2a535672017-04-27 12:03:15 -07004798 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4799 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004800 repo = git.Repo(path)
4801 repo_messages = [c.message.strip()
4802 for c in repo.iter_commits(ref_A)]
4803 repo_messages.reverse()
4804 correct_messages = [
4805 'initial commit', 'add content from fixture', 'A-1']
4806 self.assertEqual(repo_messages, correct_messages)
4807
Paul Belanger174a8272017-03-14 13:20:10 -04004808 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004809 self.waitUntilSettled()
4810
4811 job_B = None
4812 for job in self.builds:
4813 if 'project-merge' in job.name:
4814 job_B = job
4815 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4816 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4817 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4818 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4819
James E. Blair2a535672017-04-27 12:03:15 -07004820 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4821 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004822 repo = git.Repo(path)
4823 repo_messages = [c.message.strip()
4824 for c in repo.iter_commits(ref_B)]
4825 repo_messages.reverse()
4826 correct_messages = [
4827 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4828 self.assertEqual(repo_messages, correct_messages)
4829
Paul Belanger174a8272017-03-14 13:20:10 -04004830 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004831 self.waitUntilSettled()
4832
4833 job_C = None
4834 for job in self.builds:
4835 if 'project-merge' in job.name:
4836 job_C = job
4837 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4838 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4839 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4840 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004841 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4842 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004843 repo = git.Repo(path)
4844 repo_messages = [c.message.strip()
4845 for c in repo.iter_commits(ref_C)]
4846
4847 repo_messages.reverse()
4848 correct_messages = [
4849 'initial commit', 'add content from fixture',
4850 'A-1', 'C-1']
4851 # Ensure the right commits are in the history for this ref
4852 self.assertEqual(repo_messages, correct_messages)
4853
Paul Belanger174a8272017-03-14 13:20:10 -04004854 self.executor_server.hold_jobs_in_build = False
4855 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004856 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004857
4858
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004859class TestSemaphore(ZuulTestCase):
4860 tenant_config_file = 'config/semaphore/main.yaml'
4861
4862 def test_semaphore_one(self):
4863 "Test semaphores with max=1 (mutex)"
4864 tenant = self.sched.abide.tenants.get('tenant-one')
4865
4866 self.executor_server.hold_jobs_in_build = True
4867
4868 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4869 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4870 self.assertFalse('test-semaphore' in
4871 tenant.semaphore_handler.semaphores)
4872
4873 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4874 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4875 self.waitUntilSettled()
4876
4877 self.assertEqual(len(self.builds), 3)
4878 self.assertEqual(self.builds[0].name, 'project-test1')
4879 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4880 self.assertEqual(self.builds[2].name, 'project-test1')
4881
4882 self.executor_server.release('semaphore-one-test1')
4883 self.waitUntilSettled()
4884
4885 self.assertEqual(len(self.builds), 3)
4886 self.assertEqual(self.builds[0].name, 'project-test1')
4887 self.assertEqual(self.builds[1].name, 'project-test1')
4888 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4889 self.assertTrue('test-semaphore' in
4890 tenant.semaphore_handler.semaphores)
4891
4892 self.executor_server.release('semaphore-one-test2')
4893 self.waitUntilSettled()
4894
4895 self.assertEqual(len(self.builds), 3)
4896 self.assertEqual(self.builds[0].name, 'project-test1')
4897 self.assertEqual(self.builds[1].name, 'project-test1')
4898 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4899 self.assertTrue('test-semaphore' in
4900 tenant.semaphore_handler.semaphores)
4901
4902 self.executor_server.release('semaphore-one-test1')
4903 self.waitUntilSettled()
4904
4905 self.assertEqual(len(self.builds), 3)
4906 self.assertEqual(self.builds[0].name, 'project-test1')
4907 self.assertEqual(self.builds[1].name, 'project-test1')
4908 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4909 self.assertTrue('test-semaphore' in
4910 tenant.semaphore_handler.semaphores)
4911
4912 self.executor_server.release('semaphore-one-test2')
4913 self.waitUntilSettled()
4914
4915 self.assertEqual(len(self.builds), 2)
4916 self.assertEqual(self.builds[0].name, 'project-test1')
4917 self.assertEqual(self.builds[1].name, 'project-test1')
4918 self.assertFalse('test-semaphore' in
4919 tenant.semaphore_handler.semaphores)
4920
4921 self.executor_server.hold_jobs_in_build = False
4922 self.executor_server.release()
4923
4924 self.waitUntilSettled()
4925 self.assertEqual(len(self.builds), 0)
4926
4927 self.assertEqual(A.reported, 1)
4928 self.assertEqual(B.reported, 1)
4929 self.assertFalse('test-semaphore' in
4930 tenant.semaphore_handler.semaphores)
4931
4932 def test_semaphore_two(self):
4933 "Test semaphores with max>1"
4934 tenant = self.sched.abide.tenants.get('tenant-one')
4935
4936 self.executor_server.hold_jobs_in_build = True
4937 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4938 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4939 self.assertFalse('test-semaphore-two' in
4940 tenant.semaphore_handler.semaphores)
4941
4942 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4943 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4944 self.waitUntilSettled()
4945
4946 self.assertEqual(len(self.builds), 4)
4947 self.assertEqual(self.builds[0].name, 'project-test1')
4948 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4949 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4950 self.assertEqual(self.builds[3].name, 'project-test1')
4951 self.assertTrue('test-semaphore-two' in
4952 tenant.semaphore_handler.semaphores)
4953 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4954 'test-semaphore-two', [])), 2)
4955
4956 self.executor_server.release('semaphore-two-test1')
4957 self.waitUntilSettled()
4958
4959 self.assertEqual(len(self.builds), 4)
4960 self.assertEqual(self.builds[0].name, 'project-test1')
4961 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4962 self.assertEqual(self.builds[2].name, 'project-test1')
4963 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4964 self.assertTrue('test-semaphore-two' in
4965 tenant.semaphore_handler.semaphores)
4966 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4967 'test-semaphore-two', [])), 2)
4968
4969 self.executor_server.release('semaphore-two-test2')
4970 self.waitUntilSettled()
4971
4972 self.assertEqual(len(self.builds), 4)
4973 self.assertEqual(self.builds[0].name, 'project-test1')
4974 self.assertEqual(self.builds[1].name, 'project-test1')
4975 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4976 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4977 self.assertTrue('test-semaphore-two' in
4978 tenant.semaphore_handler.semaphores)
4979 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4980 'test-semaphore-two', [])), 2)
4981
4982 self.executor_server.release('semaphore-two-test1')
4983 self.waitUntilSettled()
4984
4985 self.assertEqual(len(self.builds), 3)
4986 self.assertEqual(self.builds[0].name, 'project-test1')
4987 self.assertEqual(self.builds[1].name, 'project-test1')
4988 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4989 self.assertTrue('test-semaphore-two' in
4990 tenant.semaphore_handler.semaphores)
4991 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4992 'test-semaphore-two', [])), 1)
4993
4994 self.executor_server.release('semaphore-two-test2')
4995 self.waitUntilSettled()
4996
4997 self.assertEqual(len(self.builds), 2)
4998 self.assertEqual(self.builds[0].name, 'project-test1')
4999 self.assertEqual(self.builds[1].name, 'project-test1')
5000 self.assertFalse('test-semaphore-two' in
5001 tenant.semaphore_handler.semaphores)
5002
5003 self.executor_server.hold_jobs_in_build = False
5004 self.executor_server.release()
5005
5006 self.waitUntilSettled()
5007 self.assertEqual(len(self.builds), 0)
5008
5009 self.assertEqual(A.reported, 1)
5010 self.assertEqual(B.reported, 1)
5011
5012 def test_semaphore_abandon(self):
5013 "Test abandon with job semaphores"
5014 self.executor_server.hold_jobs_in_build = True
5015 tenant = self.sched.abide.tenants.get('tenant-one')
5016 check_pipeline = tenant.layout.pipelines['check']
5017
5018 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5019 self.assertFalse('test-semaphore' in
5020 tenant.semaphore_handler.semaphores)
5021
5022 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5023 self.waitUntilSettled()
5024
5025 self.assertTrue('test-semaphore' in
5026 tenant.semaphore_handler.semaphores)
5027
5028 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5029 self.waitUntilSettled()
5030
5031 # The check pipeline should be empty
5032 items = check_pipeline.getAllItems()
5033 self.assertEqual(len(items), 0)
5034
5035 # The semaphore should be released
5036 self.assertFalse('test-semaphore' in
5037 tenant.semaphore_handler.semaphores)
5038
5039 self.executor_server.hold_jobs_in_build = False
5040 self.executor_server.release()
5041 self.waitUntilSettled()
5042
5043 def test_semaphore_reconfigure(self):
5044 "Test reconfigure with job semaphores"
5045 self.executor_server.hold_jobs_in_build = True
5046 tenant = self.sched.abide.tenants.get('tenant-one')
5047 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5048 self.assertFalse('test-semaphore' in
5049 tenant.semaphore_handler.semaphores)
5050
5051 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5052 self.waitUntilSettled()
5053
5054 self.assertTrue('test-semaphore' in
5055 tenant.semaphore_handler.semaphores)
5056
5057 # reconfigure without layout change
5058 self.sched.reconfigure(self.config)
5059 self.waitUntilSettled()
5060 tenant = self.sched.abide.tenants.get('tenant-one')
5061
5062 # semaphore still must be held
5063 self.assertTrue('test-semaphore' in
5064 tenant.semaphore_handler.semaphores)
5065
5066 self.commitConfigUpdate(
5067 'common-config',
5068 'config/semaphore/zuul-reconfiguration.yaml')
5069 self.sched.reconfigure(self.config)
5070 self.waitUntilSettled()
5071 tenant = self.sched.abide.tenants.get('tenant-one')
5072
5073 self.executor_server.release('project-test1')
5074 self.waitUntilSettled()
5075
5076 # There should be no builds anymore
5077 self.assertEqual(len(self.builds), 0)
5078
5079 # The semaphore should be released
5080 self.assertFalse('test-semaphore' in
5081 tenant.semaphore_handler.semaphores)
5082
5083
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005084class TestSemaphoreMultiTenant(ZuulTestCase):
5085 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5086
5087 def test_semaphore_tenant_isolation(self):
5088 "Test semaphores in multiple tenants"
5089
5090 self.waitUntilSettled()
5091 tenant_one = self.sched.abide.tenants.get('tenant-one')
5092 tenant_two = self.sched.abide.tenants.get('tenant-two')
5093
5094 self.executor_server.hold_jobs_in_build = True
5095 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5096 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5097 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5098 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5099 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5100 self.assertFalse('test-semaphore' in
5101 tenant_one.semaphore_handler.semaphores)
5102 self.assertFalse('test-semaphore' in
5103 tenant_two.semaphore_handler.semaphores)
5104
5105 # add patches to project1 of tenant-one
5106 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5107 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5108 self.waitUntilSettled()
5109
5110 # one build of project1-test1 must run
5111 # semaphore of tenant-one must be acquired once
5112 # semaphore of tenant-two must not be acquired
5113 self.assertEqual(len(self.builds), 1)
5114 self.assertEqual(self.builds[0].name, 'project1-test1')
5115 self.assertTrue('test-semaphore' in
5116 tenant_one.semaphore_handler.semaphores)
5117 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5118 'test-semaphore', [])), 1)
5119 self.assertFalse('test-semaphore' in
5120 tenant_two.semaphore_handler.semaphores)
5121
5122 # add patches to project2 of tenant-two
5123 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5124 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5125 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5126 self.waitUntilSettled()
5127
5128 # one build of project1-test1 must run
5129 # two builds of project2-test1 must run
5130 # semaphore of tenant-one must be acquired once
5131 # semaphore of tenant-two must be acquired twice
5132 self.assertEqual(len(self.builds), 3)
5133 self.assertEqual(self.builds[0].name, 'project1-test1')
5134 self.assertEqual(self.builds[1].name, 'project2-test1')
5135 self.assertEqual(self.builds[2].name, 'project2-test1')
5136 self.assertTrue('test-semaphore' in
5137 tenant_one.semaphore_handler.semaphores)
5138 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5139 'test-semaphore', [])), 1)
5140 self.assertTrue('test-semaphore' in
5141 tenant_two.semaphore_handler.semaphores)
5142 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5143 'test-semaphore', [])), 2)
5144
5145 self.executor_server.release('project1-test1')
5146 self.waitUntilSettled()
5147
5148 # one build of project1-test1 must run
5149 # two builds of project2-test1 must run
5150 # semaphore of tenant-one must be acquired once
5151 # semaphore of tenant-two must be acquired twice
5152 self.assertEqual(len(self.builds), 3)
5153 self.assertEqual(self.builds[0].name, 'project2-test1')
5154 self.assertEqual(self.builds[1].name, 'project2-test1')
5155 self.assertEqual(self.builds[2].name, 'project1-test1')
5156 self.assertTrue('test-semaphore' in
5157 tenant_one.semaphore_handler.semaphores)
5158 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5159 'test-semaphore', [])), 1)
5160 self.assertTrue('test-semaphore' in
5161 tenant_two.semaphore_handler.semaphores)
5162 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5163 'test-semaphore', [])), 2)
5164
5165 self.executor_server.release('project2-test1')
5166 self.waitUntilSettled()
5167
5168 # one build of project1-test1 must run
5169 # one build of project2-test1 must run
5170 # semaphore of tenant-one must be acquired once
5171 # semaphore of tenant-two must be acquired once
5172 self.assertEqual(len(self.builds), 2)
5173 self.assertTrue('test-semaphore' in
5174 tenant_one.semaphore_handler.semaphores)
5175 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5176 'test-semaphore', [])), 1)
5177 self.assertTrue('test-semaphore' in
5178 tenant_two.semaphore_handler.semaphores)
5179 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5180 'test-semaphore', [])), 1)
5181
5182 self.executor_server.hold_jobs_in_build = False
5183 self.executor_server.release()
5184
5185 self.waitUntilSettled()
5186
5187 # no build must run
5188 # semaphore of tenant-one must not be acquired
5189 # semaphore of tenant-two must not be acquired
5190 self.assertEqual(len(self.builds), 0)
5191 self.assertFalse('test-semaphore' in
5192 tenant_one.semaphore_handler.semaphores)
5193 self.assertFalse('test-semaphore' in
5194 tenant_two.semaphore_handler.semaphores)
5195
5196 self.assertEqual(A.reported, 1)
5197 self.assertEqual(B.reported, 1)
5198
5199
5200class TestSemaphoreInRepo(ZuulTestCase):
5201 tenant_config_file = 'config/in-repo/main.yaml'
5202
5203 def test_semaphore_in_repo(self):
5204 "Test semaphores in repo config"
5205
5206 # This tests dynamic semaphore handling in project repos. The semaphore
5207 # max value should not be evaluated dynamically but must be updated
5208 # after the change lands.
5209
5210 self.waitUntilSettled()
5211 tenant = self.sched.abide.tenants.get('tenant-one')
5212
5213 in_repo_conf = textwrap.dedent(
5214 """
5215 - job:
5216 name: project-test2
5217 semaphore: test-semaphore
5218
5219 - project:
5220 name: org/project
5221 tenant-one-gate:
5222 jobs:
5223 - project-test2
5224
5225 # the max value in dynamic layout must be ignored
5226 - semaphore:
5227 name: test-semaphore
5228 max: 2
5229 """)
5230
5231 in_repo_playbook = textwrap.dedent(
5232 """
5233 - hosts: all
5234 tasks: []
5235 """)
5236
5237 file_dict = {'.zuul.yaml': in_repo_conf,
5238 'playbooks/project-test2.yaml': in_repo_playbook}
5239 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5240 files=file_dict)
5241 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5242 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5243 B.setDependsOn(A, 1)
5244 C.setDependsOn(A, 1)
5245
5246 self.executor_server.hold_jobs_in_build = True
5247
5248 A.addApproval('code-review', 2)
5249 B.addApproval('code-review', 2)
5250 C.addApproval('code-review', 2)
5251 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5252 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5253 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5254 self.waitUntilSettled()
5255
5256 # check that the layout in a queue item still has max value of 1
5257 # for test-semaphore
5258 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5259 queue = None
5260 for queue_candidate in pipeline.queues:
5261 if queue_candidate.name == 'org/project':
5262 queue = queue_candidate
5263 break
5264 queue_item = queue.queue[0]
5265 item_dynamic_layout = queue_item.current_build_set.layout
5266 dynamic_test_semaphore = \
5267 item_dynamic_layout.semaphores.get('test-semaphore')
5268 self.assertEqual(dynamic_test_semaphore.max, 1)
5269
5270 # one build must be in queue, one semaphores acquired
5271 self.assertEqual(len(self.builds), 1)
5272 self.assertEqual(self.builds[0].name, 'project-test2')
5273 self.assertTrue('test-semaphore' in
5274 tenant.semaphore_handler.semaphores)
5275 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5276 'test-semaphore', [])), 1)
5277
5278 self.executor_server.release('project-test2')
5279 self.waitUntilSettled()
5280
5281 # change A must be merged
5282 self.assertEqual(A.data['status'], 'MERGED')
5283 self.assertEqual(A.reported, 2)
5284
5285 # send change-merged event as the gerrit mock doesn't send it
5286 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5287 self.waitUntilSettled()
5288
5289 # now that change A was merged, the new semaphore max must be effective
5290 tenant = self.sched.abide.tenants.get('tenant-one')
5291 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5292
5293 # two builds must be in queue, two semaphores acquired
5294 self.assertEqual(len(self.builds), 2)
5295 self.assertEqual(self.builds[0].name, 'project-test2')
5296 self.assertEqual(self.builds[1].name, 'project-test2')
5297 self.assertTrue('test-semaphore' in
5298 tenant.semaphore_handler.semaphores)
5299 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5300 'test-semaphore', [])), 2)
5301
5302 self.executor_server.release('project-test2')
5303 self.waitUntilSettled()
5304
5305 self.assertEqual(len(self.builds), 0)
5306 self.assertFalse('test-semaphore' in
5307 tenant.semaphore_handler.semaphores)
5308
5309 self.executor_server.hold_jobs_in_build = False
5310 self.executor_server.release()
5311
5312 self.waitUntilSettled()
5313 self.assertEqual(len(self.builds), 0)
5314
5315 self.assertEqual(A.reported, 2)
5316 self.assertEqual(B.reported, 2)
5317 self.assertEqual(C.reported, 2)