blob: c3cbf6d736fa54f6cd25b85f8d3440a0b2d7acd3 [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
Monty Taylorbc758832013-06-17 17:22:42 -040024import time
Clint Byrum03454a52017-05-26 17:14:02 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
Monty Taylorb934c1a2017-06-16 19:31:47 -050029import urllib
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
Clint Byrumb9de8be2017-05-27 13:49:19 -070032from zuul.driver.gerrit import gerritreporter
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 (
Paul Belanger0a21f0a2017-06-13 13:14:42 -040038 SSLZuulTestCase,
Maru Newby3fe5f852015-01-13 04:22:14 +000039 ZuulTestCase,
40 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070041 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000042)
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044
Paul Belanger0a21f0a2017-06-13 13:14:42 -040045class TestSchedulerSSL(SSLZuulTestCase):
46 tenant_config_file = 'config/single-tenant/main.yaml'
47
48 def test_jobs_executed(self):
49 "Test that jobs are executed and a change is merged"
50
51 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
52 A.addApproval('code-review', 2)
53 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
54 self.waitUntilSettled()
55 self.assertEqual(self.getJobFromHistory('project-merge').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test1').result,
58 'SUCCESS')
59 self.assertEqual(self.getJobFromHistory('project-test2').result,
60 'SUCCESS')
61 self.assertEqual(A.data['status'], 'MERGED')
62 self.assertEqual(A.reported, 2)
63 self.assertEqual(self.getJobFromHistory('project-test1').node,
64 'label1')
65 self.assertIsNone(self.getJobFromHistory('project-test2').node)
66
67
Clark Boylanb640e052014-04-03 16:41:46 -070068class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070069 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010070
Paul Belanger174a8272017-03-14 13:20:10 -040071 def test_jobs_executed(self):
72 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070073
James E. Blairb0fcae42012-07-17 11:12:10 -070074 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070075 A.addApproval('code-review', 2)
76 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070077 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040078 self.assertEqual(self.getJobFromHistory('project-merge').result,
79 'SUCCESS')
80 self.assertEqual(self.getJobFromHistory('project-test1').result,
81 'SUCCESS')
82 self.assertEqual(self.getJobFromHistory('project-test2').result,
83 'SUCCESS')
84 self.assertEqual(A.data['status'], 'MERGED')
85 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070086 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070087 'label1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080088 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070089
James E. Blair552b54f2016-07-22 13:55:32 -070090 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050091 # Per-driver
92 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
93 # Per-driver per-connection
94 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
95 value='1|c')
James E. Blair66eeebf2013-07-27 17:44:32 -070096 self.assertReportedStat('zuul.pipeline.gate.current_changes',
97 value='1|g')
98 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
99 kind='ms')
100 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
101 value='1|c')
102 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
103 self.assertReportedStat('zuul.pipeline.gate.total_changes',
104 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700105 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700106 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -0700107 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700108 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700109
James E. Blair5821bd92015-09-16 08:48:15 -0700110 for build in self.builds:
111 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
112
James E. Blair3cb10702013-08-24 08:56:03 -0700113 def test_initial_pipeline_gauges(self):
114 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700115 self.assertReportedStat('zuul.pipeline.gate.current_changes',
116 value='0|g')
117 self.assertReportedStat('zuul.pipeline.check.current_changes',
118 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700119
James E. Blair34776ee2016-08-25 13:53:54 -0700120 def test_job_branch(self):
121 "Test the correct variant of a job runs on a branch"
122 self.create_branch('org/project', 'stable')
123 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
124 A.addApproval('code-review', 2)
125 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
126 self.waitUntilSettled()
127 self.assertEqual(self.getJobFromHistory('project-test1').result,
128 'SUCCESS')
129 self.assertEqual(self.getJobFromHistory('project-test2').result,
130 'SUCCESS')
131 self.assertEqual(A.data['status'], 'MERGED')
132 self.assertEqual(A.reported, 2,
133 "A should report start and success")
134 self.assertIn('gate', A.messages[1],
135 "A should transit gate")
136 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700137 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700138
James E. Blairb0fcae42012-07-17 11:12:10 -0700139 def test_parallel_changes(self):
140 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700141
Paul Belanger174a8272017-03-14 13:20:10 -0400142 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700143 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
144 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
145 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700146 A.addApproval('code-review', 2)
147 B.addApproval('code-review', 2)
148 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700149
James E. Blair8b5408c2016-08-08 15:37:46 -0700150 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
151 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
152 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700153
154 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400155 self.assertEqual(len(self.builds), 1)
156 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700157 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700158
Paul Belanger174a8272017-03-14 13:20:10 -0400159 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700160 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400161 self.assertEqual(len(self.builds), 3)
162 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700163 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400164 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
Paul Belanger174a8272017-03-14 13:20:10 -0400169 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700170 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(len(self.builds), 5)
172 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700173 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700175 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700176
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700178 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700180 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700181
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400182 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700183 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700184
Paul Belanger174a8272017-03-14 13:20:10 -0400185 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700186 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400187 self.assertEqual(len(self.builds), 6)
188 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700189 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400190 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700191 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700192
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400193 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700194 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400198 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700199 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700201 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700202
Paul Belanger174a8272017-03-14 13:20:10 -0400203 self.executor_server.hold_jobs_in_build = False
204 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700205 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400206 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700207
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(len(self.history), 9)
209 self.assertEqual(A.data['status'], 'MERGED')
210 self.assertEqual(B.data['status'], 'MERGED')
211 self.assertEqual(C.data['status'], 'MERGED')
212 self.assertEqual(A.reported, 2)
213 self.assertEqual(B.reported, 2)
214 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700215
216 def test_failed_changes(self):
217 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400218 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700219
James E. Blairb02a3bb2012-07-30 17:49:55 -0700220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700222 A.addApproval('code-review', 2)
223 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700224
Paul Belanger174a8272017-03-14 13:20:10 -0400225 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700226
James E. Blair8b5408c2016-08-08 15:37:46 -0700227 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
228 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700229 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700230 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400231
Paul Belanger174a8272017-03-14 13:20:10 -0400232 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400233 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700234 # A/project-merge is complete
235 self.assertBuilds([
236 dict(name='project-test1', changes='1,1'),
237 dict(name='project-test2', changes='1,1'),
238 dict(name='project-merge', changes='1,1 2,1'),
239 ])
James E. Blaire2819012013-06-28 17:17:26 -0400240
Paul Belanger174a8272017-03-14 13:20:10 -0400241 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400242 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700243 # A/project-merge is complete
244 # B/project-merge is complete
245 self.assertBuilds([
246 dict(name='project-test1', changes='1,1'),
247 dict(name='project-test2', changes='1,1'),
248 dict(name='project-test1', changes='1,1 2,1'),
249 dict(name='project-test2', changes='1,1 2,1'),
250 ])
251
252 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400253 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700254 self.builds[0].release()
255 self.waitUntilSettled()
256
257 self.orderedRelease()
258 self.assertHistory([
259 dict(name='project-merge', result='SUCCESS', changes='1,1'),
260 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
261 dict(name='project-test1', result='FAILURE', changes='1,1'),
262 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
263 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
264 dict(name='project-test2', result='SUCCESS', changes='1,1'),
265 dict(name='project-merge', result='SUCCESS', changes='2,1'),
266 dict(name='project-test1', result='SUCCESS', changes='2,1'),
267 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700268 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700269
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400270 self.assertEqual(A.data['status'], 'NEW')
271 self.assertEqual(B.data['status'], 'MERGED')
272 self.assertEqual(A.reported, 2)
273 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700274
275 def test_independent_queues(self):
276 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700277
Paul Belanger174a8272017-03-14 13:20:10 -0400278 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700280 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
281 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700282 A.addApproval('code-review', 2)
283 B.addApproval('code-review', 2)
284 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700285
James E. Blair8b5408c2016-08-08 15:37:46 -0700286 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800287 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700288
James E. Blairf59f3cf2017-02-19 14:50:26 -0800289 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
290 self.waitUntilSettled()
291
292 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293 self.waitUntilSettled()
294
295 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700296 self.assertBuilds([
297 dict(name='project-merge', changes='1,1'),
298 dict(name='project-merge', changes='2,1'),
299 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700300
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700301 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700302 self.builds[0].release()
303 self.waitUntilSettled()
304 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700305 self.waitUntilSettled()
306 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400307 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700308 self.waitUntilSettled()
309
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700310 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700311 self.assertBuilds([
312 dict(name='project-test1', changes='1,1'),
313 dict(name='project-test2', changes='1,1'),
314 dict(name='project-test1', changes='2,1'),
315 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500316 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700317 dict(name='project-test1', changes='2,1 3,1'),
318 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800319 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700320 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700321
James E. Blair5f0d49e2016-08-19 09:36:50 -0700322 self.orderedRelease()
323 self.assertHistory([
324 dict(name='project-merge', result='SUCCESS', changes='1,1'),
325 dict(name='project-merge', result='SUCCESS', changes='2,1'),
326 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
327 dict(name='project-test1', result='SUCCESS', changes='1,1'),
328 dict(name='project-test2', result='SUCCESS', changes='1,1'),
329 dict(name='project-test1', result='SUCCESS', changes='2,1'),
330 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500331 dict(
332 name='project1-project2-integration',
333 result='SUCCESS',
334 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700335 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
336 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800337 dict(name='project1-project2-integration',
338 result='SUCCESS',
339 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700340 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700341
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400342 self.assertEqual(A.data['status'], 'MERGED')
343 self.assertEqual(B.data['status'], 'MERGED')
344 self.assertEqual(C.data['status'], 'MERGED')
345 self.assertEqual(A.reported, 2)
346 self.assertEqual(B.reported, 2)
347 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700348
349 def test_failed_change_at_head(self):
350 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700351
Paul Belanger174a8272017-03-14 13:20:10 -0400352 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700353 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
354 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
355 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700356 A.addApproval('code-review', 2)
357 B.addApproval('code-review', 2)
358 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700359
Paul Belanger174a8272017-03-14 13:20:10 -0400360 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700361
James E. Blair8b5408c2016-08-08 15:37:46 -0700362 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
363 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
364 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700365
366 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700367
James E. Blairb536ecc2016-08-31 10:11:42 -0700368 self.assertBuilds([
369 dict(name='project-merge', changes='1,1'),
370 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700371
Paul Belanger174a8272017-03-14 13:20:10 -0400372 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700373 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400374 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700375 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400376 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700377 self.waitUntilSettled()
378
James E. Blairb536ecc2016-08-31 10:11:42 -0700379 self.assertBuilds([
380 dict(name='project-test1', changes='1,1'),
381 dict(name='project-test2', changes='1,1'),
382 dict(name='project-test1', changes='1,1 2,1'),
383 dict(name='project-test2', changes='1,1 2,1'),
384 dict(name='project-test1', changes='1,1 2,1 3,1'),
385 dict(name='project-test2', changes='1,1 2,1 3,1'),
386 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700387
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400388 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700389 self.waitUntilSettled()
390
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400391 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700392 self.assertBuilds([
393 dict(name='project-test2', changes='1,1'),
394 dict(name='project-merge', changes='2,1'),
395 ])
396 # Unordered history comparison because the aborts can finish
397 # in any order.
398 self.assertHistory([
399 dict(name='project-merge', result='SUCCESS',
400 changes='1,1'),
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1 2,1'),
403 dict(name='project-merge', result='SUCCESS',
404 changes='1,1 2,1 3,1'),
405 dict(name='project-test1', result='FAILURE',
406 changes='1,1'),
407 dict(name='project-test1', result='ABORTED',
408 changes='1,1 2,1'),
409 dict(name='project-test2', result='ABORTED',
410 changes='1,1 2,1'),
411 dict(name='project-test1', result='ABORTED',
412 changes='1,1 2,1 3,1'),
413 dict(name='project-test2', result='ABORTED',
414 changes='1,1 2,1 3,1'),
415 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700416
Paul Belanger174a8272017-03-14 13:20:10 -0400417 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700418 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400419 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700420 self.waitUntilSettled()
421 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700422
James E. Blairb536ecc2016-08-31 10:11:42 -0700423 self.assertBuilds([])
424 self.assertHistory([
425 dict(name='project-merge', result='SUCCESS',
426 changes='1,1'),
427 dict(name='project-merge', result='SUCCESS',
428 changes='1,1 2,1'),
429 dict(name='project-merge', result='SUCCESS',
430 changes='1,1 2,1 3,1'),
431 dict(name='project-test1', result='FAILURE',
432 changes='1,1'),
433 dict(name='project-test1', result='ABORTED',
434 changes='1,1 2,1'),
435 dict(name='project-test2', result='ABORTED',
436 changes='1,1 2,1'),
437 dict(name='project-test1', result='ABORTED',
438 changes='1,1 2,1 3,1'),
439 dict(name='project-test2', result='ABORTED',
440 changes='1,1 2,1 3,1'),
441 dict(name='project-merge', result='SUCCESS',
442 changes='2,1'),
443 dict(name='project-merge', result='SUCCESS',
444 changes='2,1 3,1'),
445 dict(name='project-test2', result='SUCCESS',
446 changes='1,1'),
447 dict(name='project-test1', result='SUCCESS',
448 changes='2,1'),
449 dict(name='project-test2', result='SUCCESS',
450 changes='2,1'),
451 dict(name='project-test1', result='SUCCESS',
452 changes='2,1 3,1'),
453 dict(name='project-test2', result='SUCCESS',
454 changes='2,1 3,1'),
455 ], ordered=False)
456
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400457 self.assertEqual(A.data['status'], 'NEW')
458 self.assertEqual(B.data['status'], 'MERGED')
459 self.assertEqual(C.data['status'], 'MERGED')
460 self.assertEqual(A.reported, 2)
461 self.assertEqual(B.reported, 2)
462 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700463
James E. Blair0aac4872013-08-23 14:02:38 -0700464 def test_failed_change_in_middle(self):
465 "Test a failed change in the middle of the queue"
466
Paul Belanger174a8272017-03-14 13:20:10 -0400467 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700468 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
469 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
470 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700471 A.addApproval('code-review', 2)
472 B.addApproval('code-review', 2)
473 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700474
Paul Belanger174a8272017-03-14 13:20:10 -0400475 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700476
James E. Blair8b5408c2016-08-08 15:37:46 -0700477 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
478 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
479 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700480
481 self.waitUntilSettled()
482
Paul Belanger174a8272017-03-14 13:20:10 -0400483 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700484 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400485 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700486 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400487 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700488 self.waitUntilSettled()
489
490 self.assertEqual(len(self.builds), 6)
491 self.assertEqual(self.builds[0].name, 'project-test1')
492 self.assertEqual(self.builds[1].name, 'project-test2')
493 self.assertEqual(self.builds[2].name, 'project-test1')
494 self.assertEqual(self.builds[3].name, 'project-test2')
495 self.assertEqual(self.builds[4].name, 'project-test1')
496 self.assertEqual(self.builds[5].name, 'project-test2')
497
498 self.release(self.builds[2])
499 self.waitUntilSettled()
500
James E. Blair972e3c72013-08-29 12:04:55 -0700501 # project-test1 and project-test2 for A
502 # project-test2 for B
503 # project-merge for C (without B)
504 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700505 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
506
Paul Belanger174a8272017-03-14 13:20:10 -0400507 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700508 self.waitUntilSettled()
509
510 # project-test1 and project-test2 for A
511 # project-test2 for B
512 # project-test1 and project-test2 for C
513 self.assertEqual(len(self.builds), 5)
514
Paul Belanger2e2a0492016-10-30 18:33:37 -0400515 tenant = self.sched.abide.tenants.get('tenant-one')
516 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700517 builds = items[0].current_build_set.getBuilds()
518 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
519 self.assertEqual(self.countJobResults(builds, None), 2)
520 builds = items[1].current_build_set.getBuilds()
521 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
522 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
523 self.assertEqual(self.countJobResults(builds, None), 1)
524 builds = items[2].current_build_set.getBuilds()
525 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700526 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700527
Paul Belanger174a8272017-03-14 13:20:10 -0400528 self.executor_server.hold_jobs_in_build = False
529 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700530 self.waitUntilSettled()
531
532 self.assertEqual(len(self.builds), 0)
533 self.assertEqual(len(self.history), 12)
534 self.assertEqual(A.data['status'], 'MERGED')
535 self.assertEqual(B.data['status'], 'NEW')
536 self.assertEqual(C.data['status'], 'MERGED')
537 self.assertEqual(A.reported, 2)
538 self.assertEqual(B.reported, 2)
539 self.assertEqual(C.reported, 2)
540
James E. Blaird466dc42012-07-31 10:42:56 -0700541 def test_failed_change_at_head_with_queue(self):
542 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700543
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700544 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700545 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
546 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
547 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700548 A.addApproval('code-review', 2)
549 B.addApproval('code-review', 2)
550 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700551
Paul Belanger174a8272017-03-14 13:20:10 -0400552 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700553
James E. Blair8b5408c2016-08-08 15:37:46 -0700554 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
555 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
556 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700557
558 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700559 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400560 self.assertEqual(len(self.builds), 0)
561 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700562 self.assertEqual(queue[0].name, b'executor:execute')
563 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800564 self.assertEqual(job_args['job'], 'project-merge')
565 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700566
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700567 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700568 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700570 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700572 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700573 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700574
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400575 self.assertEqual(len(self.builds), 0)
576 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800577
578 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700579 json.loads(queue[0].arguments.decode('utf8'))['job'],
580 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800581 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700582 json.loads(queue[1].arguments.decode('utf8'))['job'],
583 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800584 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700585 json.loads(queue[2].arguments.decode('utf8'))['job'],
586 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800587 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700588 json.loads(queue[3].arguments.decode('utf8'))['job'],
589 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800590 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700591 json.loads(queue[4].arguments.decode('utf8'))['job'],
592 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800593 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700594 json.loads(queue[5].arguments.decode('utf8'))['job'],
595 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700596
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700597 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700598 self.waitUntilSettled()
599
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400600 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700601 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400602 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
603 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700604
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700605 self.gearman_server.hold_jobs_in_queue = False
606 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700607 self.waitUntilSettled()
608
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400609 self.assertEqual(len(self.builds), 0)
610 self.assertEqual(len(self.history), 11)
611 self.assertEqual(A.data['status'], 'NEW')
612 self.assertEqual(B.data['status'], 'MERGED')
613 self.assertEqual(C.data['status'], 'MERGED')
614 self.assertEqual(A.reported, 2)
615 self.assertEqual(B.reported, 2)
616 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700617
James E. Blairec056492016-07-22 09:45:56 -0700618 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700619 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400620 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700621 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700622 A.addApproval('code-review', 2)
623 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700624 self.waitUntilSettled()
625 time.sleep(2)
626
627 data = json.loads(self.sched.formatStatusJSON())
628 found_job = None
629 for pipeline in data['pipelines']:
630 if pipeline['name'] != 'gate':
631 continue
632 for queue in pipeline['change_queues']:
633 for head in queue['heads']:
634 for item in head:
635 for job in item['jobs']:
636 if job['name'] == 'project-merge':
637 found_job = job
638 break
639
640 self.assertIsNotNone(found_job)
641 if iteration == 1:
642 self.assertIsNotNone(found_job['estimated_time'])
643 self.assertIsNone(found_job['remaining_time'])
644 else:
645 self.assertIsNotNone(found_job['estimated_time'])
646 self.assertTrue(found_job['estimated_time'] >= 2)
647 self.assertIsNotNone(found_job['remaining_time'])
648
Paul Belanger174a8272017-03-14 13:20:10 -0400649 self.executor_server.hold_jobs_in_build = False
650 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700651 self.waitUntilSettled()
652
James E. Blairec056492016-07-22 09:45:56 -0700653 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700654 def test_time_database(self):
655 "Test the time database"
656
657 self._test_time_database(1)
658 self._test_time_database(2)
659
James E. Blairfef71632013-09-23 11:15:47 -0700660 def test_two_failed_changes_at_head(self):
661 "Test that changes are reparented correctly if 2 fail at head"
662
Paul Belanger174a8272017-03-14 13:20:10 -0400663 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
665 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
666 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700667 A.addApproval('code-review', 2)
668 B.addApproval('code-review', 2)
669 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700670
Paul Belanger174a8272017-03-14 13:20:10 -0400671 self.executor_server.failJob('project-test1', A)
672 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700673
James E. Blair8b5408c2016-08-08 15:37:46 -0700674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
675 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
676 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700677 self.waitUntilSettled()
678
Paul Belanger174a8272017-03-14 13:20:10 -0400679 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700680 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400681 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700682 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400683 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700684 self.waitUntilSettled()
685
686 self.assertEqual(len(self.builds), 6)
687 self.assertEqual(self.builds[0].name, 'project-test1')
688 self.assertEqual(self.builds[1].name, 'project-test2')
689 self.assertEqual(self.builds[2].name, 'project-test1')
690 self.assertEqual(self.builds[3].name, 'project-test2')
691 self.assertEqual(self.builds[4].name, 'project-test1')
692 self.assertEqual(self.builds[5].name, 'project-test2')
693
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400694 self.assertTrue(self.builds[0].hasChanges(A))
695 self.assertTrue(self.builds[2].hasChanges(A))
696 self.assertTrue(self.builds[2].hasChanges(B))
697 self.assertTrue(self.builds[4].hasChanges(A))
698 self.assertTrue(self.builds[4].hasChanges(B))
699 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700700
701 # Fail change B first
702 self.release(self.builds[2])
703 self.waitUntilSettled()
704
705 # restart of C after B failure
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), 5)
710 self.assertEqual(self.builds[0].name, 'project-test1')
711 self.assertEqual(self.builds[1].name, 'project-test2')
712 self.assertEqual(self.builds[2].name, 'project-test2')
713 self.assertEqual(self.builds[3].name, 'project-test1')
714 self.assertEqual(self.builds[4].name, 'project-test2')
715
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400716 self.assertTrue(self.builds[1].hasChanges(A))
717 self.assertTrue(self.builds[2].hasChanges(A))
718 self.assertTrue(self.builds[2].hasChanges(B))
719 self.assertTrue(self.builds[4].hasChanges(A))
720 self.assertFalse(self.builds[4].hasChanges(B))
721 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700722
723 # Finish running all passing jobs for change A
724 self.release(self.builds[1])
725 self.waitUntilSettled()
726 # Fail and report change A
727 self.release(self.builds[0])
728 self.waitUntilSettled()
729
730 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400731 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700732 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400733 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700734 self.waitUntilSettled()
735
736 self.assertEqual(len(self.builds), 4)
737 self.assertEqual(self.builds[0].name, 'project-test1') # B
738 self.assertEqual(self.builds[1].name, 'project-test2') # B
739 self.assertEqual(self.builds[2].name, 'project-test1') # C
740 self.assertEqual(self.builds[3].name, 'project-test2') # C
741
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400742 self.assertFalse(self.builds[1].hasChanges(A))
743 self.assertTrue(self.builds[1].hasChanges(B))
744 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700745
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400746 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700747 # After A failed and B and C restarted, B should be back in
748 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400749 self.assertTrue(self.builds[2].hasChanges(B))
750 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700751
Paul Belanger174a8272017-03-14 13:20:10 -0400752 self.executor_server.hold_jobs_in_build = False
753 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700754 self.waitUntilSettled()
755
756 self.assertEqual(len(self.builds), 0)
757 self.assertEqual(len(self.history), 21)
758 self.assertEqual(A.data['status'], 'NEW')
759 self.assertEqual(B.data['status'], 'NEW')
760 self.assertEqual(C.data['status'], 'MERGED')
761 self.assertEqual(A.reported, 2)
762 self.assertEqual(B.reported, 2)
763 self.assertEqual(C.reported, 2)
764
James E. Blair8c803f82012-07-31 16:25:42 -0700765 def test_patch_order(self):
766 "Test that dependent patches are tested in the right order"
767 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
768 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
769 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700770 A.addApproval('code-review', 2)
771 B.addApproval('code-review', 2)
772 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700773
774 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
775 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
776 M2.setMerged()
777 M1.setMerged()
778
779 # C -> B -> A -> M1 -> M2
780 # M2 is here to make sure it is never queried. If it is, it
781 # means zuul is walking down the entire history of merged
782 # changes.
783
784 C.setDependsOn(B, 1)
785 B.setDependsOn(A, 1)
786 A.setDependsOn(M1, 1)
787 M1.setDependsOn(M2, 1)
788
James E. Blair8b5408c2016-08-08 15:37:46 -0700789 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700790
791 self.waitUntilSettled()
792
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400793 self.assertEqual(A.data['status'], 'NEW')
794 self.assertEqual(B.data['status'], 'NEW')
795 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700796
James E. Blair8b5408c2016-08-08 15:37:46 -0700797 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
798 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700799
800 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400801 self.assertEqual(M2.queried, 0)
802 self.assertEqual(A.data['status'], 'MERGED')
803 self.assertEqual(B.data['status'], 'MERGED')
804 self.assertEqual(C.data['status'], 'MERGED')
805 self.assertEqual(A.reported, 2)
806 self.assertEqual(B.reported, 2)
807 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700808
James E. Blair063672f2015-01-29 13:09:12 -0800809 def test_needed_changes_enqueue(self):
810 "Test that a needed change is enqueued ahead"
811 # A Given a git tree like this, if we enqueue
812 # / \ change C, we should walk up and down the tree
813 # B G and enqueue changes in the order ABCDEFG.
814 # /|\ This is also the order that you would get if
815 # *C E F you enqueued changes in the order ABCDEFG, so
816 # / the ordering is stable across re-enqueue events.
817 # D
818
819 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
820 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
821 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
822 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
823 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
824 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
825 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
826 B.setDependsOn(A, 1)
827 C.setDependsOn(B, 1)
828 D.setDependsOn(C, 1)
829 E.setDependsOn(B, 1)
830 F.setDependsOn(B, 1)
831 G.setDependsOn(A, 1)
832
James E. Blair8b5408c2016-08-08 15:37:46 -0700833 A.addApproval('code-review', 2)
834 B.addApproval('code-review', 2)
835 C.addApproval('code-review', 2)
836 D.addApproval('code-review', 2)
837 E.addApproval('code-review', 2)
838 F.addApproval('code-review', 2)
839 G.addApproval('code-review', 2)
840 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800841
842 self.waitUntilSettled()
843
844 self.assertEqual(A.data['status'], 'NEW')
845 self.assertEqual(B.data['status'], 'NEW')
846 self.assertEqual(C.data['status'], 'NEW')
847 self.assertEqual(D.data['status'], 'NEW')
848 self.assertEqual(E.data['status'], 'NEW')
849 self.assertEqual(F.data['status'], 'NEW')
850 self.assertEqual(G.data['status'], 'NEW')
851
852 # We're about to add approvals to changes without adding the
853 # triggering events to Zuul, so that we can be sure that it is
854 # enqueing the changes based on dependencies, not because of
855 # triggering events. Since it will have the changes cached
856 # already (without approvals), we need to clear the cache
857 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400858 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100859 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800860
Paul Belanger174a8272017-03-14 13:20:10 -0400861 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700862 A.addApproval('approved', 1)
863 B.addApproval('approved', 1)
864 D.addApproval('approved', 1)
865 E.addApproval('approved', 1)
866 F.addApproval('approved', 1)
867 G.addApproval('approved', 1)
868 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800869
870 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400871 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800872 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400873 self.executor_server.hold_jobs_in_build = False
874 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800875 self.waitUntilSettled()
876
877 self.assertEqual(A.data['status'], 'MERGED')
878 self.assertEqual(B.data['status'], 'MERGED')
879 self.assertEqual(C.data['status'], 'MERGED')
880 self.assertEqual(D.data['status'], 'MERGED')
881 self.assertEqual(E.data['status'], 'MERGED')
882 self.assertEqual(F.data['status'], 'MERGED')
883 self.assertEqual(G.data['status'], 'MERGED')
884 self.assertEqual(A.reported, 2)
885 self.assertEqual(B.reported, 2)
886 self.assertEqual(C.reported, 2)
887 self.assertEqual(D.reported, 2)
888 self.assertEqual(E.reported, 2)
889 self.assertEqual(F.reported, 2)
890 self.assertEqual(G.reported, 2)
891 self.assertEqual(self.history[6].changes,
892 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
893
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100894 def test_source_cache(self):
895 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400896 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700897
898 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
899 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
900 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700901 A.addApproval('code-review', 2)
902 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700903
904 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
905 M1.setMerged()
906
907 B.setDependsOn(A, 1)
908 A.setDependsOn(M1, 1)
909
James E. Blair8b5408c2016-08-08 15:37:46 -0700910 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700911 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
912
913 self.waitUntilSettled()
914
915 for build in self.builds:
916 if build.parameters['ZUUL_PIPELINE'] == 'check':
917 build.release()
918 self.waitUntilSettled()
919 for build in self.builds:
920 if build.parameters['ZUUL_PIPELINE'] == 'check':
921 build.release()
922 self.waitUntilSettled()
923
James E. Blair8b5408c2016-08-08 15:37:46 -0700924 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700925 self.waitUntilSettled()
926
Joshua Hesketh352264b2015-08-11 23:42:08 +1000927 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700928 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000929 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700930
Paul Belanger174a8272017-03-14 13:20:10 -0400931 self.executor_server.hold_jobs_in_build = False
932 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700933 self.waitUntilSettled()
934
935 self.assertEqual(A.data['status'], 'MERGED')
936 self.assertEqual(B.data['status'], 'MERGED')
937 self.assertEqual(A.queried, 2) # Initial and isMerged
938 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
939
James E. Blair8c803f82012-07-31 16:25:42 -0700940 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700941 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700942 # TODO: move to test_gerrit (this is a unit test!)
943 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400944 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700945 (trusted, project) = tenant.getProject('org/project')
946 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400947
948 # TODO(pabelanger): As we add more source / trigger APIs we should make
949 # it easier for users to create events for testing.
950 event = zuul.model.TriggerEvent()
951 event.trigger_name = 'gerrit'
952 event.change_number = '1'
953 event.patch_number = '2'
954
955 a = source.getChange(event)
956 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700957 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700958
James E. Blair8b5408c2016-08-08 15:37:46 -0700959 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400960 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700961 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700962
James E. Blair8b5408c2016-08-08 15:37:46 -0700963 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400964 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700965 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700966
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700967 def test_project_merge_conflict(self):
968 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700969
970 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700971 A = self.fake_gerrit.addFakeChange('org/project',
972 'master', 'A',
973 files={'conflict': 'foo'})
974 B = self.fake_gerrit.addFakeChange('org/project',
975 'master', 'B',
976 files={'conflict': 'bar'})
977 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700978 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700979 A.addApproval('code-review', 2)
980 B.addApproval('code-review', 2)
981 C.addApproval('code-review', 2)
982 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
983 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
984 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700985 self.waitUntilSettled()
986
James E. Blair6736beb2013-07-11 15:18:15 -0700987 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700988 self.assertEqual(C.reported, 1)
989
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700990 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700991 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700992 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700993 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700994 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700995 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700996
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700997 self.gearman_server.hold_jobs_in_queue = False
998 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700999 self.waitUntilSettled()
1000
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001001 self.assertEqual(A.data['status'], 'MERGED')
1002 self.assertEqual(B.data['status'], 'NEW')
1003 self.assertEqual(C.data['status'], 'MERGED')
1004 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001005 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001006 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001007
1008 self.assertHistory([
1009 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1010 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1011 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1012 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1013 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1014 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1015 ], ordered=False)
1016
1017 def test_delayed_merge_conflict(self):
1018 "Test that delayed check merge conflicts are handled properly"
1019
1020 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001021 # the executor sucesfully merges a change based on an old
1022 # repo state (frozen by the scheduler) which would otherwise
1023 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001024 self.gearman_server.hold_jobs_in_queue = True
1025 A = self.fake_gerrit.addFakeChange('org/project',
1026 'master', 'A',
1027 files={'conflict': 'foo'})
1028 B = self.fake_gerrit.addFakeChange('org/project',
1029 'master', 'B',
1030 files={'conflict': 'bar'})
1031 C = self.fake_gerrit.addFakeChange('org/project',
1032 'master', 'C')
1033 C.setDependsOn(B, 1)
1034
1035 # A enters the gate queue; B and C enter the check queue
1036 A.addApproval('code-review', 2)
1037 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1038 self.waitUntilSettled()
1039 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001040 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001041 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1042 self.waitUntilSettled()
1043
1044 self.assertEqual(A.reported, 1)
1045 self.assertEqual(B.reported, 0) # Check does not report start
1046 self.assertEqual(C.reported, 0) # Check does not report start
1047
1048 # A merges while B and C are queued in check
1049 # Release A project-merge
1050 queue = self.gearman_server.getQueue()
1051 self.release(queue[0])
1052 self.waitUntilSettled()
1053
1054 # Release A project-test*
1055 # gate has higher precedence, so A's test jobs are added in
1056 # front of the merge jobs for B and C
1057 queue = self.gearman_server.getQueue()
1058 self.release(queue[0])
1059 self.release(queue[1])
1060 self.waitUntilSettled()
1061
1062 self.assertEqual(A.data['status'], 'MERGED')
1063 self.assertEqual(B.data['status'], 'NEW')
1064 self.assertEqual(C.data['status'], 'NEW')
1065 self.assertEqual(A.reported, 2)
1066 self.assertEqual(B.reported, 0)
1067 self.assertEqual(C.reported, 0)
1068 self.assertHistory([
1069 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1072 ], ordered=False)
1073
1074 # B and C report merge conflicts
1075 # Release B project-merge
1076 queue = self.gearman_server.getQueue()
1077 self.release(queue[0])
1078 self.waitUntilSettled()
1079
1080 # Release C
1081 self.gearman_server.hold_jobs_in_queue = False
1082 self.gearman_server.release()
1083 self.waitUntilSettled()
1084
1085 self.assertEqual(A.data['status'], 'MERGED')
1086 self.assertEqual(B.data['status'], 'NEW')
1087 self.assertEqual(C.data['status'], 'NEW')
1088 self.assertEqual(A.reported, 2)
1089 self.assertEqual(B.reported, 1)
1090 self.assertEqual(C.reported, 1)
1091
1092 self.assertHistory([
1093 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1094 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1095 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001096 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1097 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1098 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1099 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1100 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1101 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001102 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001103
James E. Blairdaabed22012-08-15 15:38:57 -07001104 def test_post(self):
1105 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001106
Zhongyue Luo5d556072012-09-21 02:00:47 +09001107 e = {
1108 "type": "ref-updated",
1109 "submitter": {
1110 "name": "User Name",
1111 },
1112 "refUpdate": {
1113 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1114 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1115 "refName": "master",
1116 "project": "org/project",
1117 }
1118 }
James E. Blairdaabed22012-08-15 15:38:57 -07001119 self.fake_gerrit.addEvent(e)
1120 self.waitUntilSettled()
1121
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001122 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001123 self.assertEqual(len(self.history), 1)
1124 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001125
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001126 def test_post_ignore_deletes(self):
1127 "Test that deleting refs does not trigger post jobs"
1128
1129 e = {
1130 "type": "ref-updated",
1131 "submitter": {
1132 "name": "User Name",
1133 },
1134 "refUpdate": {
1135 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1136 "newRev": "0000000000000000000000000000000000000000",
1137 "refName": "master",
1138 "project": "org/project",
1139 }
1140 }
1141 self.fake_gerrit.addEvent(e)
1142 self.waitUntilSettled()
1143
1144 job_names = [x.name for x in self.history]
1145 self.assertEqual(len(self.history), 0)
1146 self.assertNotIn('project-post', job_names)
1147
James E. Blaircc324192017-04-19 14:36:08 -07001148 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001149 def test_post_ignore_deletes_negative(self):
1150 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001151 e = {
1152 "type": "ref-updated",
1153 "submitter": {
1154 "name": "User Name",
1155 },
1156 "refUpdate": {
1157 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1158 "newRev": "0000000000000000000000000000000000000000",
1159 "refName": "master",
1160 "project": "org/project",
1161 }
1162 }
1163 self.fake_gerrit.addEvent(e)
1164 self.waitUntilSettled()
1165
1166 job_names = [x.name for x in self.history]
1167 self.assertEqual(len(self.history), 1)
1168 self.assertIn('project-post', job_names)
1169
James E. Blairec056492016-07-22 09:45:56 -07001170 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001171 def test_build_configuration_branch_interaction(self):
1172 "Test that switching between branches works"
1173 self.test_build_configuration()
1174 self.test_build_configuration_branch()
1175 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001176 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001177 repo = git.Repo(path)
1178 repo.heads.master.commit = repo.commit('init')
1179 self.test_build_configuration()
1180
James E. Blaircaec0c52012-08-22 14:52:22 -07001181 def test_dependent_changes_dequeue(self):
1182 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001183
James E. Blaircaec0c52012-08-22 14:52:22 -07001184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1185 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1186 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001187 A.addApproval('code-review', 2)
1188 B.addApproval('code-review', 2)
1189 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001190
1191 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1192 M1.setMerged()
1193
1194 # C -> B -> A -> M1
1195
1196 C.setDependsOn(B, 1)
1197 B.setDependsOn(A, 1)
1198 A.setDependsOn(M1, 1)
1199
Paul Belanger174a8272017-03-14 13:20:10 -04001200 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001201
James E. Blair8b5408c2016-08-08 15:37:46 -07001202 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1203 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1204 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001205
1206 self.waitUntilSettled()
1207
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001208 self.assertEqual(A.data['status'], 'NEW')
1209 self.assertEqual(A.reported, 2)
1210 self.assertEqual(B.data['status'], 'NEW')
1211 self.assertEqual(B.reported, 2)
1212 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001213 self.assertIn('This change depends on a change that failed to merge.',
1214 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001215 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001216
James E. Blair972e3c72013-08-29 12:04:55 -07001217 def test_failing_dependent_changes(self):
1218 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001219 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1222 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1223 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1224 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001225 A.addApproval('code-review', 2)
1226 B.addApproval('code-review', 2)
1227 C.addApproval('code-review', 2)
1228 D.addApproval('code-review', 2)
1229 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001230
1231 # E, D -> C -> B, A
1232
1233 D.setDependsOn(C, 1)
1234 C.setDependsOn(B, 1)
1235
Paul Belanger174a8272017-03-14 13:20:10 -04001236 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001237
James E. Blair8b5408c2016-08-08 15:37:46 -07001238 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1239 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1240 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1241 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1242 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001243
1244 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001245 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001246 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001247 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001248 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001249 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001250 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001251 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001252 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001253 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001254 self.waitUntilSettled()
1255
Paul Belanger174a8272017-03-14 13:20:10 -04001256 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001257 for build in self.builds:
1258 if build.parameters['ZUUL_CHANGE'] != '1':
1259 build.release()
1260 self.waitUntilSettled()
1261
Paul Belanger174a8272017-03-14 13:20:10 -04001262 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001263 self.waitUntilSettled()
1264
1265 self.assertEqual(A.data['status'], 'MERGED')
1266 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001267 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001268 self.assertEqual(B.data['status'], 'NEW')
1269 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001270 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001271 self.assertEqual(C.data['status'], 'NEW')
1272 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001273 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001274 self.assertEqual(D.data['status'], 'NEW')
1275 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001276 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001277 self.assertEqual(E.data['status'], 'MERGED')
1278 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001279 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001280 self.assertEqual(len(self.history), 18)
1281
James E. Blairec590122012-08-22 15:19:31 -07001282 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001283 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001284 # If it's dequeued more than once, we should see extra
1285 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001286
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001288 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1289 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1290 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001291 A.addApproval('code-review', 2)
1292 B.addApproval('code-review', 2)
1293 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001294
Paul Belanger174a8272017-03-14 13:20:10 -04001295 self.executor_server.failJob('project-test1', A)
1296 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001297
James E. Blair8b5408c2016-08-08 15:37:46 -07001298 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1299 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1300 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001301
1302 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001303
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001304 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001305 self.assertEqual(self.builds[0].name, 'project-merge')
1306 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001307
Paul Belanger174a8272017-03-14 13:20:10 -04001308 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001309 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001310 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001311 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001312 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001313 self.waitUntilSettled()
1314
James E. Blair6df67132017-04-19 13:53:20 -07001315 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001316 self.assertEqual(self.builds[0].name, 'project-test1')
1317 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001318 self.assertEqual(self.builds[2].name, 'project-test1')
1319 self.assertEqual(self.builds[3].name, 'project-test2')
1320 self.assertEqual(self.builds[4].name, 'project-test1')
1321 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001322
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001323 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001324 self.waitUntilSettled()
1325
James E. Blair6df67132017-04-19 13:53:20 -07001326 self.assertEqual(len(self.builds), 2) # test2, merge for B
1327 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001328
Paul Belanger174a8272017-03-14 13:20:10 -04001329 self.executor_server.hold_jobs_in_build = False
1330 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001331 self.waitUntilSettled()
1332
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001333 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001334 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001335
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001336 self.assertEqual(A.data['status'], 'NEW')
1337 self.assertEqual(B.data['status'], 'MERGED')
1338 self.assertEqual(C.data['status'], 'MERGED')
1339 self.assertEqual(A.reported, 2)
1340 self.assertEqual(B.reported, 2)
1341 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001342
James E. Blaircc020532017-04-19 13:04:44 -07001343 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001344 def test_nonvoting_job(self):
1345 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001346
James E. Blair4ec821f2012-08-23 15:28:28 -07001347 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1348 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001349 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001350 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001351 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001352
1353 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001354
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001355 self.assertEqual(A.data['status'], 'MERGED')
1356 self.assertEqual(A.reported, 2)
1357 self.assertEqual(
1358 self.getJobFromHistory('nonvoting-project-merge').result,
1359 'SUCCESS')
1360 self.assertEqual(
1361 self.getJobFromHistory('nonvoting-project-test1').result,
1362 'SUCCESS')
1363 self.assertEqual(
1364 self.getJobFromHistory('nonvoting-project-test2').result,
1365 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001366
James E. Blair5821bd92015-09-16 08:48:15 -07001367 for build in self.builds:
1368 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1369
James E. Blaire0487072012-08-29 17:38:31 -07001370 def test_check_queue_success(self):
1371 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001372
James E. Blaire0487072012-08-29 17:38:31 -07001373 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1374 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1375
1376 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001377
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001378 self.assertEqual(A.data['status'], 'NEW')
1379 self.assertEqual(A.reported, 1)
1380 self.assertEqual(self.getJobFromHistory('project-merge').result,
1381 'SUCCESS')
1382 self.assertEqual(self.getJobFromHistory('project-test1').result,
1383 'SUCCESS')
1384 self.assertEqual(self.getJobFromHistory('project-test2').result,
1385 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001386
1387 def test_check_queue_failure(self):
1388 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001389
James E. Blaire0487072012-08-29 17:38:31 -07001390 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001391 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001392 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1393
1394 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001395
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001396 self.assertEqual(A.data['status'], 'NEW')
1397 self.assertEqual(A.reported, 1)
1398 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001399 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001400 self.assertEqual(self.getJobFromHistory('project-test1').result,
1401 'SUCCESS')
1402 self.assertEqual(self.getJobFromHistory('project-test2').result,
1403 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001404
James E. Blair7cb84542017-04-19 13:35:05 -07001405 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001406 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001407 # This particular test does a large amount of merges and needs a little
1408 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001409 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001410 "test that dependent changes behind dequeued changes work"
1411 # This complicated test is a reproduction of a real life bug
1412 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001413
Paul Belanger174a8272017-03-14 13:20:10 -04001414 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001415 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1416 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1417 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1418 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1419 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1420 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1421 D.setDependsOn(C, 1)
1422 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001423 A.addApproval('code-review', 2)
1424 B.addApproval('code-review', 2)
1425 C.addApproval('code-review', 2)
1426 D.addApproval('code-review', 2)
1427 E.addApproval('code-review', 2)
1428 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001429
1430 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001431
1432 # Change object re-use in the gerrit trigger is hidden if
1433 # changes are added in quick succession; waiting makes it more
1434 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001435 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001436 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001437 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001438 self.waitUntilSettled()
1439
Paul Belanger174a8272017-03-14 13:20:10 -04001440 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001441 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001442 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001443 self.waitUntilSettled()
1444
James E. Blair8b5408c2016-08-08 15:37:46 -07001445 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001446 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001447 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001448 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001449 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001450 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001451 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001452 self.waitUntilSettled()
1453
Paul Belanger174a8272017-03-14 13:20:10 -04001454 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001455 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001456 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001457 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001458 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001459 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001460 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001461 self.waitUntilSettled()
1462
1463 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001464
1465 # Grab pointers to the jobs we want to release before
1466 # releasing any, because list indexes may change as
1467 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001468 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001469 a.release()
1470 b.release()
1471 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001472 self.waitUntilSettled()
1473
Paul Belanger174a8272017-03-14 13:20:10 -04001474 self.executor_server.hold_jobs_in_build = False
1475 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001476 self.waitUntilSettled()
1477
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001478 self.assertEqual(A.data['status'], 'NEW')
1479 self.assertEqual(B.data['status'], 'MERGED')
1480 self.assertEqual(C.data['status'], 'MERGED')
1481 self.assertEqual(D.data['status'], 'MERGED')
1482 self.assertEqual(E.data['status'], 'MERGED')
1483 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001484
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001485 self.assertEqual(A.reported, 2)
1486 self.assertEqual(B.reported, 2)
1487 self.assertEqual(C.reported, 2)
1488 self.assertEqual(D.reported, 2)
1489 self.assertEqual(E.reported, 2)
1490 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001491
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001492 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1493 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001494
1495 def test_merger_repack(self):
1496 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001497
James E. Blair05fed602012-09-07 12:45:24 -07001498 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001499 A.addApproval('code-review', 2)
1500 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001501 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001502 self.assertEqual(self.getJobFromHistory('project-merge').result,
1503 'SUCCESS')
1504 self.assertEqual(self.getJobFromHistory('project-test1').result,
1505 'SUCCESS')
1506 self.assertEqual(self.getJobFromHistory('project-test2').result,
1507 'SUCCESS')
1508 self.assertEqual(A.data['status'], 'MERGED')
1509 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001510 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001511 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001512
James E. Blair2a535672017-04-27 12:03:15 -07001513 path = os.path.join(self.merger_src_root, "review.example.com",
1514 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001515 if os.path.exists(path):
1516 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001517 path = os.path.join(self.executor_src_root, "review.example.com",
1518 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001519 if os.path.exists(path):
1520 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001521
1522 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001523 A.addApproval('code-review', 2)
1524 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001525 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001526 self.assertEqual(self.getJobFromHistory('project-merge').result,
1527 'SUCCESS')
1528 self.assertEqual(self.getJobFromHistory('project-test1').result,
1529 'SUCCESS')
1530 self.assertEqual(self.getJobFromHistory('project-test2').result,
1531 'SUCCESS')
1532 self.assertEqual(A.data['status'], 'MERGED')
1533 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001534
James E. Blair4886f282012-11-15 09:27:33 -08001535 def test_merger_repack_large_change(self):
1536 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001537 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001538 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001539 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001540 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001541 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001542 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001543 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001544 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001545 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001546 # TODOv3(jeblair): add hostname to upstream root
1547 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001548 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001549 path = os.path.join(self.merger_src_root, 'review.example.com',
1550 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001551 if os.path.exists(path):
1552 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001553 path = os.path.join(self.executor_src_root, 'review.example.com',
1554 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001555 if os.path.exists(path):
1556 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001557
James E. Blair8b5408c2016-08-08 15:37:46 -07001558 A.addApproval('code-review', 2)
1559 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001560 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001561 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001562 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001563 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001564 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001565 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001566 'SUCCESS')
1567 self.assertEqual(A.data['status'], 'MERGED')
1568 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001569
James E. Blair2fa50962013-01-30 21:50:41 -08001570 def test_new_patchset_dequeues_old(self):
1571 "Test that a new patchset causes the old to be dequeued"
1572 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001573 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001574 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1575 M.setMerged()
1576
1577 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1578 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1579 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1580 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001581 A.addApproval('code-review', 2)
1582 B.addApproval('code-review', 2)
1583 C.addApproval('code-review', 2)
1584 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001585
1586 C.setDependsOn(B, 1)
1587 B.setDependsOn(A, 1)
1588 A.setDependsOn(M, 1)
1589
James E. Blair8b5408c2016-08-08 15:37:46 -07001590 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1591 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1592 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1593 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001594 self.waitUntilSettled()
1595
1596 B.addPatchset()
1597 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1598 self.waitUntilSettled()
1599
Paul Belanger174a8272017-03-14 13:20:10 -04001600 self.executor_server.hold_jobs_in_build = False
1601 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001602 self.waitUntilSettled()
1603
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001604 self.assertEqual(A.data['status'], 'MERGED')
1605 self.assertEqual(A.reported, 2)
1606 self.assertEqual(B.data['status'], 'NEW')
1607 self.assertEqual(B.reported, 2)
1608 self.assertEqual(C.data['status'], 'NEW')
1609 self.assertEqual(C.reported, 2)
1610 self.assertEqual(D.data['status'], 'MERGED')
1611 self.assertEqual(D.reported, 2)
1612 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001613
James E. Blairba437362015-02-07 11:41:52 -08001614 def test_new_patchset_check(self):
1615 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001616
Paul Belanger174a8272017-03-14 13:20:10 -04001617 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001618
1619 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001620 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001621 tenant = self.sched.abide.tenants.get('tenant-one')
1622 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001623
1624 # Add two git-dependent changes
1625 B.setDependsOn(A, 1)
1626 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1627 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001628 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1629 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001630
1631 # A live item, and a non-live/live pair
1632 items = check_pipeline.getAllItems()
1633 self.assertEqual(len(items), 3)
1634
1635 self.assertEqual(items[0].change.number, '1')
1636 self.assertEqual(items[0].change.patchset, '1')
1637 self.assertFalse(items[0].live)
1638
1639 self.assertEqual(items[1].change.number, '2')
1640 self.assertEqual(items[1].change.patchset, '1')
1641 self.assertTrue(items[1].live)
1642
1643 self.assertEqual(items[2].change.number, '1')
1644 self.assertEqual(items[2].change.patchset, '1')
1645 self.assertTrue(items[2].live)
1646
1647 # Add a new patchset to A
1648 A.addPatchset()
1649 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1650 self.waitUntilSettled()
1651
1652 # The live copy of A,1 should be gone, but the non-live and B
1653 # should continue, and we should have a new A,2
1654 items = check_pipeline.getAllItems()
1655 self.assertEqual(len(items), 3)
1656
1657 self.assertEqual(items[0].change.number, '1')
1658 self.assertEqual(items[0].change.patchset, '1')
1659 self.assertFalse(items[0].live)
1660
1661 self.assertEqual(items[1].change.number, '2')
1662 self.assertEqual(items[1].change.patchset, '1')
1663 self.assertTrue(items[1].live)
1664
1665 self.assertEqual(items[2].change.number, '1')
1666 self.assertEqual(items[2].change.patchset, '2')
1667 self.assertTrue(items[2].live)
1668
1669 # Add a new patchset to B
1670 B.addPatchset()
1671 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1672 self.waitUntilSettled()
1673
1674 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1675 # but we should have a new B,2 (still based on A,1)
1676 items = check_pipeline.getAllItems()
1677 self.assertEqual(len(items), 3)
1678
1679 self.assertEqual(items[0].change.number, '1')
1680 self.assertEqual(items[0].change.patchset, '2')
1681 self.assertTrue(items[0].live)
1682
1683 self.assertEqual(items[1].change.number, '1')
1684 self.assertEqual(items[1].change.patchset, '1')
1685 self.assertFalse(items[1].live)
1686
1687 self.assertEqual(items[2].change.number, '2')
1688 self.assertEqual(items[2].change.patchset, '2')
1689 self.assertTrue(items[2].live)
1690
1691 self.builds[0].release()
1692 self.waitUntilSettled()
1693 self.builds[0].release()
1694 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001695 self.executor_server.hold_jobs_in_build = False
1696 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001697 self.waitUntilSettled()
1698
1699 self.assertEqual(A.reported, 1)
1700 self.assertEqual(B.reported, 1)
1701 self.assertEqual(self.history[0].result, 'ABORTED')
1702 self.assertEqual(self.history[0].changes, '1,1')
1703 self.assertEqual(self.history[1].result, 'ABORTED')
1704 self.assertEqual(self.history[1].changes, '1,1 2,1')
1705 self.assertEqual(self.history[2].result, 'SUCCESS')
1706 self.assertEqual(self.history[2].changes, '1,2')
1707 self.assertEqual(self.history[3].result, 'SUCCESS')
1708 self.assertEqual(self.history[3].changes, '1,1 2,2')
1709
1710 def test_abandoned_gate(self):
1711 "Test that an abandoned change is dequeued from gate"
1712
Paul Belanger174a8272017-03-14 13:20:10 -04001713 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001714
1715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001716 A.addApproval('code-review', 2)
1717 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001718 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001719 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1720 self.assertEqual(self.builds[0].name, 'project-merge')
1721
1722 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1723 self.waitUntilSettled()
1724
Paul Belanger174a8272017-03-14 13:20:10 -04001725 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001726 self.waitUntilSettled()
1727
Arie5b4048c2016-10-05 16:12:06 +03001728 self.assertBuilds([])
1729 self.assertHistory([
1730 dict(name='project-merge', result='ABORTED', changes='1,1')],
1731 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001732 self.assertEqual(A.reported, 1,
1733 "Abandoned gate change should report only start")
1734
1735 def test_abandoned_check(self):
1736 "Test that an abandoned change is dequeued from check"
1737
Paul Belanger174a8272017-03-14 13:20:10 -04001738 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001739
1740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1741 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001742 tenant = self.sched.abide.tenants.get('tenant-one')
1743 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001744
1745 # Add two git-dependent changes
1746 B.setDependsOn(A, 1)
1747 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1748 self.waitUntilSettled()
1749 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1750 self.waitUntilSettled()
1751 # A live item, and a non-live/live pair
1752 items = check_pipeline.getAllItems()
1753 self.assertEqual(len(items), 3)
1754
1755 self.assertEqual(items[0].change.number, '1')
1756 self.assertFalse(items[0].live)
1757
1758 self.assertEqual(items[1].change.number, '2')
1759 self.assertTrue(items[1].live)
1760
1761 self.assertEqual(items[2].change.number, '1')
1762 self.assertTrue(items[2].live)
1763
1764 # Abandon A
1765 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1766 self.waitUntilSettled()
1767
1768 # The live copy of A should be gone, but the non-live and B
1769 # should continue
1770 items = check_pipeline.getAllItems()
1771 self.assertEqual(len(items), 2)
1772
1773 self.assertEqual(items[0].change.number, '1')
1774 self.assertFalse(items[0].live)
1775
1776 self.assertEqual(items[1].change.number, '2')
1777 self.assertTrue(items[1].live)
1778
Paul Belanger174a8272017-03-14 13:20:10 -04001779 self.executor_server.hold_jobs_in_build = False
1780 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001781 self.waitUntilSettled()
1782
1783 self.assertEqual(len(self.history), 4)
1784 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001785 'Build should have been aborted')
1786 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001787 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001788
Steve Varnau7b78b312015-04-03 14:49:46 -07001789 def test_abandoned_not_timer(self):
1790 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001791 # This test can not use simple_layout because it must start
1792 # with a configuration which does not include a
1793 # timer-triggered job so that we have an opportunity to set
1794 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001795 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001796 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001797 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001798 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001799 # The pipeline triggers every second, so we should have seen
1800 # several by now.
1801 time.sleep(5)
1802 self.waitUntilSettled()
1803 # Stop queuing timer triggered jobs so that the assertions
1804 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001805 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001806 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001807 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001808
Steve Varnau7b78b312015-04-03 14:49:46 -07001809 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1810
1811 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1812 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1813 self.waitUntilSettled()
1814 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1815
1816 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1817 self.waitUntilSettled()
1818
1819 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1820
Paul Belanger174a8272017-03-14 13:20:10 -04001821 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001822 self.waitUntilSettled()
1823
Arx Cruzb1b010d2013-10-28 19:49:59 -02001824 def test_zuul_url_return(self):
1825 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001826 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001827 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001828
1829 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001830 A.addApproval('code-review', 2)
1831 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001832 self.waitUntilSettled()
1833
1834 self.assertEqual(len(self.builds), 1)
1835 for build in self.builds:
1836 self.assertTrue('ZUUL_URL' in build.parameters)
1837
Paul Belanger174a8272017-03-14 13:20:10 -04001838 self.executor_server.hold_jobs_in_build = False
1839 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001840 self.waitUntilSettled()
1841
James E. Blair2fa50962013-01-30 21:50:41 -08001842 def test_new_patchset_dequeues_old_on_head(self):
1843 "Test that a new patchset causes the old to be dequeued (at head)"
1844 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001845 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001846 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1847 M.setMerged()
1848 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1849 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1850 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1851 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001852 A.addApproval('code-review', 2)
1853 B.addApproval('code-review', 2)
1854 C.addApproval('code-review', 2)
1855 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001856
1857 C.setDependsOn(B, 1)
1858 B.setDependsOn(A, 1)
1859 A.setDependsOn(M, 1)
1860
James E. Blair8b5408c2016-08-08 15:37:46 -07001861 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1862 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1863 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1864 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001865 self.waitUntilSettled()
1866
1867 A.addPatchset()
1868 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1869 self.waitUntilSettled()
1870
Paul Belanger174a8272017-03-14 13:20:10 -04001871 self.executor_server.hold_jobs_in_build = False
1872 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001873 self.waitUntilSettled()
1874
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001875 self.assertEqual(A.data['status'], 'NEW')
1876 self.assertEqual(A.reported, 2)
1877 self.assertEqual(B.data['status'], 'NEW')
1878 self.assertEqual(B.reported, 2)
1879 self.assertEqual(C.data['status'], 'NEW')
1880 self.assertEqual(C.reported, 2)
1881 self.assertEqual(D.data['status'], 'MERGED')
1882 self.assertEqual(D.reported, 2)
1883 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001884
1885 def test_new_patchset_dequeues_old_without_dependents(self):
1886 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001887 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001888 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1889 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1890 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001891 A.addApproval('code-review', 2)
1892 B.addApproval('code-review', 2)
1893 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001894
James E. Blair8b5408c2016-08-08 15:37:46 -07001895 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1896 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1897 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001898 self.waitUntilSettled()
1899
1900 B.addPatchset()
1901 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1902 self.waitUntilSettled()
1903
Paul Belanger174a8272017-03-14 13:20:10 -04001904 self.executor_server.hold_jobs_in_build = False
1905 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001906 self.waitUntilSettled()
1907
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001908 self.assertEqual(A.data['status'], 'MERGED')
1909 self.assertEqual(A.reported, 2)
1910 self.assertEqual(B.data['status'], 'NEW')
1911 self.assertEqual(B.reported, 2)
1912 self.assertEqual(C.data['status'], 'MERGED')
1913 self.assertEqual(C.reported, 2)
1914 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001915
1916 def test_new_patchset_dequeues_old_independent_queue(self):
1917 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001918 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001919 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1920 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1921 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1922 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1923 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1924 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1925 self.waitUntilSettled()
1926
1927 B.addPatchset()
1928 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1929 self.waitUntilSettled()
1930
Paul Belanger174a8272017-03-14 13:20:10 -04001931 self.executor_server.hold_jobs_in_build = False
1932 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001933 self.waitUntilSettled()
1934
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001935 self.assertEqual(A.data['status'], 'NEW')
1936 self.assertEqual(A.reported, 1)
1937 self.assertEqual(B.data['status'], 'NEW')
1938 self.assertEqual(B.reported, 1)
1939 self.assertEqual(C.data['status'], 'NEW')
1940 self.assertEqual(C.reported, 1)
1941 self.assertEqual(len(self.history), 10)
1942 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001943
James E. Blaircc020532017-04-19 13:04:44 -07001944 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001945 def test_noop_job(self):
1946 "Test that the internal noop job works"
1947 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001948 A.addApproval('code-review', 2)
1949 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001950 self.waitUntilSettled()
1951
1952 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1953 self.assertTrue(self.sched._areAllBuildsComplete())
1954 self.assertEqual(len(self.history), 0)
1955 self.assertEqual(A.data['status'], 'MERGED')
1956 self.assertEqual(A.reported, 2)
1957
James E. Blair06cc3922017-04-19 10:08:10 -07001958 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001959 def test_no_job_project(self):
1960 "Test that reports with no jobs don't get sent"
1961 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1962 'master', 'A')
1963 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1964 self.waitUntilSettled()
1965
1966 # Change wasn't reported to
1967 self.assertEqual(A.reported, False)
1968
1969 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001970 tenant = self.sched.abide.tenants.get('tenant-one')
1971 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001972 items = check_pipeline.getAllItems()
1973 self.assertEqual(len(items), 0)
1974
1975 self.assertEqual(len(self.history), 0)
1976
James E. Blair7d0dedc2013-02-21 17:26:09 -08001977 def test_zuul_refs(self):
1978 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001979 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001980 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1981 M1.setMerged()
1982 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1983 M2.setMerged()
1984
1985 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1986 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1987 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1988 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001989 A.addApproval('code-review', 2)
1990 B.addApproval('code-review', 2)
1991 C.addApproval('code-review', 2)
1992 D.addApproval('code-review', 2)
1993 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1994 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1995 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1996 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001997
1998 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001999 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002000 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002001 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002002 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002003 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002004 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002005 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002006 self.waitUntilSettled()
2007
James E. Blair7d0dedc2013-02-21 17:26:09 -08002008 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002009 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002010 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002011 if x.parameters['ZUUL_CHANGE'] == '3':
2012 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002013 a_build = x
2014 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002015 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002016 b_build = x
2017 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002018 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002019 c_build = x
2020 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002021 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002022 d_build = x
2023 if a_build and b_build and c_build and d_build:
2024 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
2026 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002027 self.assertIsNotNone(a_zref)
2028 self.assertIsNotNone(b_zref)
2029 self.assertIsNotNone(c_zref)
2030 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002031
2032 # And they should all be different
2033 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002034 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002035
Clint Byrum3343e3e2016-11-15 16:05:03 -08002036 # should have a, not b, and should not be in project2
2037 self.assertTrue(a_build.hasChanges(A))
2038 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002039
Clint Byrum3343e3e2016-11-15 16:05:03 -08002040 # should have a and b, and should not be in project2
2041 self.assertTrue(b_build.hasChanges(A, B))
2042 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002043
Clint Byrum3343e3e2016-11-15 16:05:03 -08002044 # should have a and b in 1, c in 2
2045 self.assertTrue(c_build.hasChanges(A, B, C))
2046 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002047
Clint Byrum3343e3e2016-11-15 16:05:03 -08002048 # should have a and b in 1, c and d in 2
2049 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002050
Paul Belanger174a8272017-03-14 13:20:10 -04002051 self.executor_server.hold_jobs_in_build = False
2052 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002053 self.waitUntilSettled()
2054
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002055 self.assertEqual(A.data['status'], 'MERGED')
2056 self.assertEqual(A.reported, 2)
2057 self.assertEqual(B.data['status'], 'MERGED')
2058 self.assertEqual(B.reported, 2)
2059 self.assertEqual(C.data['status'], 'MERGED')
2060 self.assertEqual(C.reported, 2)
2061 self.assertEqual(D.data['status'], 'MERGED')
2062 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002063
James E. Blair4a28a882013-08-23 15:17:33 -07002064 def test_rerun_on_error(self):
2065 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002066 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002067 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. Blair4a28a882013-08-23 15:17:33 -07002070 self.waitUntilSettled()
2071
James E. Blair412fba82017-01-26 15:00:50 -08002072 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002073 self.executor_server.hold_jobs_in_build = False
2074 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002075 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002076 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002077 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2078
James E. Blair412e5582013-04-22 15:50:12 -07002079 def test_statsd(self):
2080 "Test each of the statsd methods used in the scheduler"
2081 import extras
2082 statsd = extras.try_import('statsd.statsd')
2083 statsd.incr('test-incr')
2084 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002085 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002086 self.assertReportedStat('test-incr', '1|c')
2087 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002088 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002089
James E. Blairdad52252014-02-07 16:59:17 -08002090 def test_stuck_job_cleanup(self):
2091 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002092
2093 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002094 self.gearman_server.hold_jobs_in_queue = True
2095 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002096 A.addApproval('code-review', 2)
2097 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002098 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002099 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002100 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2101
James E. Blair31cb3e82017-04-20 13:08:56 -07002102 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002103 self.sched.reconfigure(self.config)
2104 self.waitUntilSettled()
2105
James E. Blair18c64442014-03-18 10:14:45 -07002106 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002107 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002108 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002109 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2110 self.assertTrue(self.sched._areAllBuildsComplete())
2111
2112 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002113 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002114 self.assertEqual(self.history[0].result, 'SUCCESS')
2115
James E. Blair879dafb2015-07-17 14:04:49 -07002116 def test_file_head(self):
2117 # This is a regression test for an observed bug. A change
2118 # with a file named "HEAD" in the root directory of the repo
2119 # was processed by a merger. It then was unable to reset the
2120 # repo because of:
2121 # GitCommandError: 'git reset --hard HEAD' returned
2122 # with exit code 128
2123 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2124 # and filename
2125 # Use '--' to separate filenames from revisions'
2126
2127 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002128 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002129 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2130
2131 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2132 self.waitUntilSettled()
2133
2134 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2135 self.waitUntilSettled()
2136
2137 self.assertIn('Build succeeded', A.messages[0])
2138 self.assertIn('Build succeeded', B.messages[0])
2139
James E. Blair70c71582013-03-06 08:50:50 -08002140 def test_file_jobs(self):
2141 "Test that file jobs run only when appropriate"
2142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002143 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002144 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002145 A.addApproval('code-review', 2)
2146 B.addApproval('code-review', 2)
2147 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2148 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002149 self.waitUntilSettled()
2150
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002151 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002152 if x.name == 'project-testfile']
2153
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002154 self.assertEqual(len(testfile_jobs), 1)
2155 self.assertEqual(testfile_jobs[0].changes, '1,2')
2156 self.assertEqual(A.data['status'], 'MERGED')
2157 self.assertEqual(A.reported, 2)
2158 self.assertEqual(B.data['status'], 'MERGED')
2159 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002160
Clint Byrum3ee39f32016-11-17 23:45:07 -08002161 def _test_irrelevant_files_jobs(self, should_skip):
2162 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002163 if should_skip:
2164 files = {'ignoreme': 'ignored\n'}
2165 else:
2166 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002167
2168 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002169 'master',
2170 'test irrelevant-files',
2171 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002172 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2173 self.waitUntilSettled()
2174
2175 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002176 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002177
2178 if should_skip:
2179 self.assertEqual([], tested_change_ids)
2180 else:
2181 self.assertIn(change.data['number'], tested_change_ids)
2182
James E. Blaircc324192017-04-19 14:36:08 -07002183 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002184 def test_irrelevant_files_match_skips_job(self):
2185 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002186
James E. Blaircc324192017-04-19 14:36:08 -07002187 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002188 def test_irrelevant_files_no_match_runs_job(self):
2189 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002190
James E. Blaircc324192017-04-19 14:36:08 -07002191 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002192 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002193 files = {'ignoreme': 'ignored\n'}
2194
2195 change = self.fake_gerrit.addFakeChange('org/project',
2196 'master',
2197 'test irrelevant-files',
2198 files=files)
2199 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2200 self.waitUntilSettled()
2201
2202 run_jobs = set([build.name for build in self.history])
2203
2204 self.assertEqual(set(['project-test-nomatch-starts-empty',
2205 'project-test-nomatch-starts-full']), run_jobs)
2206
James E. Blairc8a1e052014-02-25 09:29:26 -08002207 def test_queue_names(self):
2208 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002209 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002210 (trusted, project1) = tenant.getProject('org/project1')
2211 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002212 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2213 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2214 self.assertEqual(q1.name, 'integrated')
2215 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002216
James E. Blair64ed6f22013-07-10 14:07:23 -07002217 def test_queue_precedence(self):
2218 "Test that queue precedence works"
2219
2220 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002221 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002222 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2223 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002224 A.addApproval('code-review', 2)
2225 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002226
2227 self.waitUntilSettled()
2228 self.gearman_server.hold_jobs_in_queue = False
2229 self.gearman_server.release()
2230 self.waitUntilSettled()
2231
James E. Blair8de58bd2013-07-18 16:23:33 -07002232 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002233 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002234 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002235 self.waitUntilSettled()
2236
James E. Blair64ed6f22013-07-10 14:07:23 -07002237 self.log.debug(self.history)
2238 self.assertEqual(self.history[0].pipeline, 'gate')
2239 self.assertEqual(self.history[1].pipeline, 'check')
2240 self.assertEqual(self.history[2].pipeline, 'gate')
2241 self.assertEqual(self.history[3].pipeline, 'gate')
2242 self.assertEqual(self.history[4].pipeline, 'check')
2243 self.assertEqual(self.history[5].pipeline, 'check')
2244
Clark Boylana5edbe42014-06-03 16:39:10 -07002245 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002246 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002247 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002249 A.addApproval('code-review', 2)
2250 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002251 self.waitUntilSettled()
2252
Paul Belanger174a8272017-03-14 13:20:10 -04002253 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002254 self.waitUntilSettled()
2255
James E. Blair1843a552013-07-03 14:19:52 -07002256 port = self.webapp.server.socket.getsockname()[1]
2257
Adam Gandelman77a12c72017-02-03 17:43:43 -08002258 req = urllib.request.Request(
2259 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002260 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002261 headers = f.info()
2262 self.assertIn('Content-Length', headers)
2263 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002264 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2265 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002266 self.assertIn('Access-Control-Allow-Origin', headers)
2267 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002268 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002269 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002270 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002271
Paul Belanger174a8272017-03-14 13:20:10 -04002272 self.executor_server.hold_jobs_in_build = False
2273 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002274 self.waitUntilSettled()
2275
2276 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002277 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002278 for p in data['pipelines']:
2279 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002280 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002281 self.assertEqual(q['window'], 20)
2282 else:
2283 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002284 for head in q['heads']:
2285 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002286 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002287 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002288 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002289 status_jobs.append(job)
2290 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002291 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002292 self.assertEqual(
2293 'finger://{hostname}/{uuid}'.format(
2294 hostname=self.executor_server.hostname,
2295 uuid=status_jobs[0]['uuid']),
2296 status_jobs[0]['url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002297 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002298 self.assertEqual(
2299 'finger://{hostname}/{uuid}'.format(
2300 hostname=self.executor_server.hostname,
2301 uuid=status_jobs[0]['uuid']),
2302 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002303 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002304 self.assertEqual(
2305 'finger://{hostname}/{uuid}'.format(
2306 hostname=self.executor_server.hostname,
2307 uuid=status_jobs[1]['uuid']),
2308 status_jobs[1]['url'])
2309 self.assertEqual(
2310 'finger://{hostname}/{uuid}'.format(
2311 hostname=self.executor_server.hostname,
2312 uuid=status_jobs[1]['uuid']),
2313 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002314
2315 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002316 self.assertEqual(
2317 'finger://{hostname}/{uuid}'.format(
2318 hostname=self.executor_server.hostname,
2319 uuid=status_jobs[2]['uuid']),
2320 status_jobs[2]['url'])
2321 self.assertEqual(
2322 'finger://{hostname}/{uuid}'.format(
2323 hostname=self.executor_server.hostname,
2324 uuid=status_jobs[2]['uuid']),
2325 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002326
James E. Blaircdccd972013-07-01 12:10:22 -07002327 def test_live_reconfiguration(self):
2328 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002329 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002330 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002331 A.addApproval('code-review', 2)
2332 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002333 self.waitUntilSettled()
2334
2335 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002336 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002337
Paul Belanger174a8272017-03-14 13:20:10 -04002338 self.executor_server.hold_jobs_in_build = False
2339 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002340 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002341 self.assertEqual(self.getJobFromHistory('project-merge').result,
2342 'SUCCESS')
2343 self.assertEqual(self.getJobFromHistory('project-test1').result,
2344 'SUCCESS')
2345 self.assertEqual(self.getJobFromHistory('project-test2').result,
2346 'SUCCESS')
2347 self.assertEqual(A.data['status'], 'MERGED')
2348 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002349
James E. Blair6bc782d2015-07-17 16:20:21 -07002350 def test_live_reconfiguration_merge_conflict(self):
2351 # A real-world bug: a change in a gate queue has a merge
2352 # conflict and a job is added to its project while it's
2353 # sitting in the queue. The job gets added to the change and
2354 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002355 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002356
2357 # This change is fine. It's here to stop the queue long
2358 # enough for the next change to be subject to the
2359 # reconfiguration, as well as to provide a conflict for the
2360 # next change. This change will succeed and merge.
2361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002362 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002363 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002364
2365 # This change will be in merge conflict. During the
2366 # reconfiguration, we will add a job. We want to make sure
2367 # that doesn't cause it to get stuck.
2368 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002369 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002370 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002371
James E. Blair8b5408c2016-08-08 15:37:46 -07002372 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2373 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002374
2375 self.waitUntilSettled()
2376
2377 # No jobs have run yet
2378 self.assertEqual(A.data['status'], 'NEW')
2379 self.assertEqual(A.reported, 1)
2380 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002381 self.assertEqual(len(self.history), 0)
2382
2383 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002384 self.commitConfigUpdate('common-config',
2385 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002386 self.sched.reconfigure(self.config)
2387 self.waitUntilSettled()
2388
Paul Belanger174a8272017-03-14 13:20:10 -04002389 self.executor_server.hold_jobs_in_build = False
2390 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002391 self.waitUntilSettled()
2392
2393 self.assertEqual(A.data['status'], 'MERGED')
2394 self.assertEqual(A.reported, 2)
2395 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002396 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002397 self.assertEqual(self.getJobFromHistory('project-merge').result,
2398 'SUCCESS')
2399 self.assertEqual(self.getJobFromHistory('project-test1').result,
2400 'SUCCESS')
2401 self.assertEqual(self.getJobFromHistory('project-test2').result,
2402 'SUCCESS')
2403 self.assertEqual(self.getJobFromHistory('project-test3').result,
2404 'SUCCESS')
2405 self.assertEqual(len(self.history), 4)
2406
James E. Blair400e8fd2015-07-30 17:44:45 -07002407 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002408 # An extrapolation of test_live_reconfiguration_merge_conflict
2409 # that tests a job added to a job tree with a failed root does
2410 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002411 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002412
2413 # This change is fine. It's here to stop the queue long
2414 # enough for the next change to be subject to the
2415 # reconfiguration. This change will succeed and merge.
2416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002417 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002418 A.addApproval('code-review', 2)
2419 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002420 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002421 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002422 self.waitUntilSettled()
2423
2424 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002425 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002426 B.addApproval('code-review', 2)
2427 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002428 self.waitUntilSettled()
2429
Paul Belanger174a8272017-03-14 13:20:10 -04002430 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002431 self.waitUntilSettled()
2432
2433 # Both -merge jobs have run, but no others.
2434 self.assertEqual(A.data['status'], 'NEW')
2435 self.assertEqual(A.reported, 1)
2436 self.assertEqual(B.data['status'], 'NEW')
2437 self.assertEqual(B.reported, 1)
2438 self.assertEqual(self.history[0].result, 'SUCCESS')
2439 self.assertEqual(self.history[0].name, 'project-merge')
2440 self.assertEqual(self.history[1].result, 'FAILURE')
2441 self.assertEqual(self.history[1].name, 'project-merge')
2442 self.assertEqual(len(self.history), 2)
2443
2444 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002445 self.commitConfigUpdate('common-config',
2446 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002447 self.sched.reconfigure(self.config)
2448 self.waitUntilSettled()
2449
Paul Belanger174a8272017-03-14 13:20:10 -04002450 self.executor_server.hold_jobs_in_build = False
2451 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002452 self.waitUntilSettled()
2453
2454 self.assertEqual(A.data['status'], 'MERGED')
2455 self.assertEqual(A.reported, 2)
2456 self.assertEqual(B.data['status'], 'NEW')
2457 self.assertEqual(B.reported, 2)
2458 self.assertEqual(self.history[0].result, 'SUCCESS')
2459 self.assertEqual(self.history[0].name, 'project-merge')
2460 self.assertEqual(self.history[1].result, 'FAILURE')
2461 self.assertEqual(self.history[1].name, 'project-merge')
2462 self.assertEqual(self.history[2].result, 'SUCCESS')
2463 self.assertEqual(self.history[3].result, 'SUCCESS')
2464 self.assertEqual(self.history[4].result, 'SUCCESS')
2465 self.assertEqual(len(self.history), 5)
2466
James E. Blair400e8fd2015-07-30 17:44:45 -07002467 def test_live_reconfiguration_failed_job(self):
2468 # Test that a change with a removed failing job does not
2469 # disrupt reconfiguration. If a change has a failed job and
2470 # that job is removed during a reconfiguration, we observed a
2471 # bug where the code to re-set build statuses would run on
2472 # that build and raise an exception because the job no longer
2473 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002474 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002475
2476 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2477
2478 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002479 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002480
2481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2482 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002483 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002484 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002485 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002486 self.waitUntilSettled()
2487
2488 self.assertEqual(A.data['status'], 'NEW')
2489 self.assertEqual(A.reported, 0)
2490
2491 self.assertEqual(self.getJobFromHistory('project-merge').result,
2492 'SUCCESS')
2493 self.assertEqual(self.getJobFromHistory('project-test1').result,
2494 'FAILURE')
2495 self.assertEqual(len(self.history), 2)
2496
2497 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002498 self.commitConfigUpdate('common-config',
2499 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002500 self.sched.reconfigure(self.config)
2501 self.waitUntilSettled()
2502
Paul Belanger174a8272017-03-14 13:20:10 -04002503 self.executor_server.hold_jobs_in_build = False
2504 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002505 self.waitUntilSettled()
2506
2507 self.assertEqual(self.getJobFromHistory('project-test2').result,
2508 'SUCCESS')
2509 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2510 'SUCCESS')
2511 self.assertEqual(len(self.history), 4)
2512
2513 self.assertEqual(A.data['status'], 'NEW')
2514 self.assertEqual(A.reported, 1)
2515 self.assertIn('Build succeeded', A.messages[0])
2516 # Ensure the removed job was not included in the report.
2517 self.assertNotIn('project-test1', A.messages[0])
2518
James E. Blairfe707d12015-08-05 15:18:15 -07002519 def test_live_reconfiguration_shared_queue(self):
2520 # Test that a change with a failing job which was removed from
2521 # this project but otherwise still exists in the system does
2522 # not disrupt reconfiguration.
2523
Paul Belanger174a8272017-03-14 13:20:10 -04002524 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002525
2526 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2527
Paul Belanger174a8272017-03-14 13:20:10 -04002528 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002529
2530 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2531 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002532 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002533 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002534 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002535 self.waitUntilSettled()
2536
2537 self.assertEqual(A.data['status'], 'NEW')
2538 self.assertEqual(A.reported, 0)
2539
Clint Byrum69663d42017-05-01 14:51:04 -07002540 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002541 'SUCCESS')
2542 self.assertEqual(self.getJobFromHistory(
2543 'project1-project2-integration').result, 'FAILURE')
2544 self.assertEqual(len(self.history), 2)
2545
2546 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002547 self.commitConfigUpdate(
2548 'common-config',
2549 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002550 self.sched.reconfigure(self.config)
2551 self.waitUntilSettled()
2552
Paul Belanger174a8272017-03-14 13:20:10 -04002553 self.executor_server.hold_jobs_in_build = False
2554 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002555 self.waitUntilSettled()
2556
Clint Byrum69663d42017-05-01 14:51:04 -07002557 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002558 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002559 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002560 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002561 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002562 'SUCCESS')
2563 self.assertEqual(self.getJobFromHistory(
2564 'project1-project2-integration').result, 'FAILURE')
2565 self.assertEqual(len(self.history), 4)
2566
2567 self.assertEqual(A.data['status'], 'NEW')
2568 self.assertEqual(A.reported, 1)
2569 self.assertIn('Build succeeded', A.messages[0])
2570 # Ensure the removed job was not included in the report.
2571 self.assertNotIn('project1-project2-integration', A.messages[0])
2572
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002573 def test_double_live_reconfiguration_shared_queue(self):
2574 # This was a real-world regression. A change is added to
2575 # gate; a reconfigure happens, a second change which depends
2576 # on the first is added, and a second reconfiguration happens.
2577 # Ensure that both changes merge.
2578
2579 # A failure may indicate incorrect caching or cleaning up of
2580 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002581 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002582
2583 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2584 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2585 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002586 A.addApproval('code-review', 2)
2587 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002588
2589 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002590 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002591 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002592 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002593 self.waitUntilSettled()
2594
2595 # Reconfigure (with only one change in the pipeline).
2596 self.sched.reconfigure(self.config)
2597 self.waitUntilSettled()
2598
2599 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002600 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002601 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002602 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002603 self.waitUntilSettled()
2604
2605 # Reconfigure (with both in the pipeline).
2606 self.sched.reconfigure(self.config)
2607 self.waitUntilSettled()
2608
Paul Belanger174a8272017-03-14 13:20:10 -04002609 self.executor_server.hold_jobs_in_build = False
2610 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002611 self.waitUntilSettled()
2612
2613 self.assertEqual(len(self.history), 8)
2614
2615 self.assertEqual(A.data['status'], 'MERGED')
2616 self.assertEqual(A.reported, 2)
2617 self.assertEqual(B.data['status'], 'MERGED')
2618 self.assertEqual(B.reported, 2)
2619
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002620 def test_live_reconfiguration_del_project(self):
2621 # Test project deletion from layout
2622 # while changes are enqueued
2623
Paul Belanger174a8272017-03-14 13:20:10 -04002624 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2626 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2627 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2628
2629 # A Depends-On: B
2630 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2631 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002632 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002633
2634 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2635 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2636 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002637 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002638 self.waitUntilSettled()
2639 self.assertEqual(len(self.builds), 5)
2640
2641 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002642 self.commitConfigUpdate(
2643 'common-config',
2644 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002645 self.sched.reconfigure(self.config)
2646 self.waitUntilSettled()
2647
2648 # Builds for C aborted, builds for A succeed,
2649 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002650 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002651 self.assertEqual(job_c.changes, '3,1')
2652 self.assertEqual(job_c.result, 'ABORTED')
2653
Paul Belanger174a8272017-03-14 13:20:10 -04002654 self.executor_server.hold_jobs_in_build = False
2655 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002656 self.waitUntilSettled()
2657
Clint Byrum0149ada2017-04-04 12:22:01 -07002658 self.assertEqual(
2659 self.getJobFromHistory('project-test1', 'org/project').changes,
2660 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002661
2662 self.assertEqual(A.data['status'], 'NEW')
2663 self.assertEqual(B.data['status'], 'NEW')
2664 self.assertEqual(C.data['status'], 'NEW')
2665 self.assertEqual(A.reported, 1)
2666 self.assertEqual(B.reported, 0)
2667 self.assertEqual(C.reported, 0)
2668
Clint Byrum0149ada2017-04-04 12:22:01 -07002669 tenant = self.sched.abide.tenants.get('tenant-one')
2670 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002671 self.assertIn('Build succeeded', A.messages[0])
2672
James E. Blairec056492016-07-22 09:45:56 -07002673 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002674 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002675 self.updateConfigLayout(
2676 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002677 self.sched.reconfigure(self.config)
2678
2679 self.init_repo("org/new-project")
2680 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2681
James E. Blair8b5408c2016-08-08 15:37:46 -07002682 A.addApproval('code-review', 2)
2683 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002684 self.waitUntilSettled()
2685 self.assertEqual(self.getJobFromHistory('project-merge').result,
2686 'SUCCESS')
2687 self.assertEqual(self.getJobFromHistory('project-test1').result,
2688 'SUCCESS')
2689 self.assertEqual(self.getJobFromHistory('project-test2').result,
2690 'SUCCESS')
2691 self.assertEqual(A.data['status'], 'MERGED')
2692 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002693
James E. Blair9ea70072017-04-19 16:05:30 -07002694 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002695 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002696 self.init_repo("org/delete-project")
2697 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2698
James E. Blair8b5408c2016-08-08 15:37:46 -07002699 A.addApproval('code-review', 2)
2700 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002701 self.waitUntilSettled()
2702 self.assertEqual(self.getJobFromHistory('project-merge').result,
2703 'SUCCESS')
2704 self.assertEqual(self.getJobFromHistory('project-test1').result,
2705 'SUCCESS')
2706 self.assertEqual(self.getJobFromHistory('project-test2').result,
2707 'SUCCESS')
2708 self.assertEqual(A.data['status'], 'MERGED')
2709 self.assertEqual(A.reported, 2)
2710
2711 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002712 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002713 if os.path.exists(os.path.join(self.merger_src_root, p)):
2714 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002715 if os.path.exists(os.path.join(self.executor_src_root, p)):
2716 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002717
2718 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2719
James E. Blair8b5408c2016-08-08 15:37:46 -07002720 B.addApproval('code-review', 2)
2721 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002722 self.waitUntilSettled()
2723 self.assertEqual(self.getJobFromHistory('project-merge').result,
2724 'SUCCESS')
2725 self.assertEqual(self.getJobFromHistory('project-test1').result,
2726 'SUCCESS')
2727 self.assertEqual(self.getJobFromHistory('project-test2').result,
2728 'SUCCESS')
2729 self.assertEqual(B.data['status'], 'MERGED')
2730 self.assertEqual(B.reported, 2)
2731
James E. Blaire68d43f2017-04-19 14:03:30 -07002732 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002733 def test_tags(self):
2734 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002735 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2736 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2737 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2738 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2739 self.waitUntilSettled()
2740
James E. Blaire68d43f2017-04-19 14:03:30 -07002741 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002742
2743 results = {self.getJobFromHistory('merge',
2744 project='org/project1').uuid: 'extratag merge',
2745 self.getJobFromHistory('merge',
2746 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002747
2748 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002749 self.assertEqual(results.get(build.uuid, ''),
2750 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002751
James E. Blair63bb0ef2013-07-29 17:14:51 -07002752 def test_timer(self):
2753 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002754 # This test can not use simple_layout because it must start
2755 # with a configuration which does not include a
2756 # timer-triggered job so that we have an opportunity to set
2757 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002758 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002759 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002760 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002761
Clark Boylan3ee090a2014-04-03 20:55:09 -07002762 # The pipeline triggers every second, so we should have seen
2763 # several by now.
2764 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002765 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002766
2767 self.assertEqual(len(self.builds), 2)
2768
James E. Blair63bb0ef2013-07-29 17:14:51 -07002769 port = self.webapp.server.socket.getsockname()[1]
2770
Clint Byrum5e729e92017-02-08 06:34:57 -08002771 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002772 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002773 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002774 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002775
Paul Belanger174a8272017-03-14 13:20:10 -04002776 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002777 # Stop queuing timer triggered jobs so that the assertions
2778 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002779 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002780 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002781 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002782 self.waitUntilSettled()
2783
2784 self.assertEqual(self.getJobFromHistory(
2785 'project-bitrot-stable-old').result, 'SUCCESS')
2786 self.assertEqual(self.getJobFromHistory(
2787 'project-bitrot-stable-older').result, 'SUCCESS')
2788
2789 data = json.loads(data)
2790 status_jobs = set()
2791 for p in data['pipelines']:
2792 for q in p['change_queues']:
2793 for head in q['heads']:
2794 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002795 for job in change['jobs']:
2796 status_jobs.add(job['name'])
2797 self.assertIn('project-bitrot-stable-old', status_jobs)
2798 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002799
James E. Blair4f6033c2014-03-27 15:49:09 -07002800 def test_idle(self):
2801 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002802 # This test can not use simple_layout because it must start
2803 # with a configuration which does not include a
2804 # timer-triggered job so that we have an opportunity to set
2805 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002806 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002807
Clark Boylan3ee090a2014-04-03 20:55:09 -07002808 for x in range(1, 3):
2809 # Test that timer triggers periodic jobs even across
2810 # layout config reloads.
2811 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002812 self.commitConfigUpdate('common-config',
2813 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002814 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002815 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002816
Clark Boylan3ee090a2014-04-03 20:55:09 -07002817 # The pipeline triggers every second, so we should have seen
2818 # several by now.
2819 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002820
2821 # Stop queuing timer triggered jobs so that the assertions
2822 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002823 self.commitConfigUpdate('common-config',
2824 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002825 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002826 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002827 self.assertEqual(len(self.builds), 2,
2828 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002829 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002830 self.waitUntilSettled()
2831 self.assertEqual(len(self.builds), 0)
2832 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002833
James E. Blaircc324192017-04-19 14:36:08 -07002834 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002835 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002836 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2837 self.waitUntilSettled()
2838
2839 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2840 self.waitUntilSettled()
2841
James E. Blairff80a2f2013-12-27 13:24:06 -08002842 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002843
2844 # A.messages only holds what FakeGerrit places in it. Thus we
2845 # work on the knowledge of what the first message should be as
2846 # it is only configured to go to SMTP.
2847
2848 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002849 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002850 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002851 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002852 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002853 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002854
2855 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002856 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002857 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002858 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002859 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002860 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002861
James E. Blaire5910202013-12-27 09:50:31 -08002862 def test_timer_smtp(self):
2863 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002864 # This test can not use simple_layout because it must start
2865 # with a configuration which does not include a
2866 # timer-triggered job so that we have an opportunity to set
2867 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002868 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002869 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002870 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002871
Clark Boylan3ee090a2014-04-03 20:55:09 -07002872 # The pipeline triggers every second, so we should have seen
2873 # several by now.
2874 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002875 self.waitUntilSettled()
2876
Clark Boylan3ee090a2014-04-03 20:55:09 -07002877 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002878 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002879 self.waitUntilSettled()
2880 self.assertEqual(len(self.history), 2)
2881
James E. Blaire5910202013-12-27 09:50:31 -08002882 self.assertEqual(self.getJobFromHistory(
2883 'project-bitrot-stable-old').result, 'SUCCESS')
2884 self.assertEqual(self.getJobFromHistory(
2885 'project-bitrot-stable-older').result, 'SUCCESS')
2886
James E. Blairff80a2f2013-12-27 13:24:06 -08002887 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002888
2889 # A.messages only holds what FakeGerrit places in it. Thus we
2890 # work on the knowledge of what the first message should be as
2891 # it is only configured to go to SMTP.
2892
2893 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002894 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002895 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002896 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002897 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002898 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002899
Clark Boylan3ee090a2014-04-03 20:55:09 -07002900 # Stop queuing timer triggered jobs and let any that may have
2901 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002902 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002903 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002904 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002905 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002906 self.waitUntilSettled()
2907
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002908 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002909 def test_timer_sshkey(self):
2910 "Test that a periodic job can setup SSH key authentication"
2911 self.worker.hold_jobs_in_build = True
2912 self.config.set('zuul', 'layout_config',
2913 'tests/fixtures/layout-timer.yaml')
2914 self.sched.reconfigure(self.config)
2915 self.registerJobs()
2916
2917 # The pipeline triggers every second, so we should have seen
2918 # several by now.
2919 time.sleep(5)
2920 self.waitUntilSettled()
2921
2922 self.assertEqual(len(self.builds), 2)
2923
2924 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2925 self.assertTrue(os.path.isfile(ssh_wrapper))
2926 with open(ssh_wrapper) as f:
2927 ssh_wrapper_content = f.read()
2928 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2929 # In the unit tests Merger runs in the same process,
2930 # so we see its' environment variables
2931 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2932
2933 self.worker.release('.*')
2934 self.waitUntilSettled()
2935 self.assertEqual(len(self.history), 2)
2936
2937 self.assertEqual(self.getJobFromHistory(
2938 'project-bitrot-stable-old').result, 'SUCCESS')
2939 self.assertEqual(self.getJobFromHistory(
2940 'project-bitrot-stable-older').result, 'SUCCESS')
2941
2942 # Stop queuing timer triggered jobs and let any that may have
2943 # queued through so that end of test assertions pass.
2944 self.config.set('zuul', 'layout_config',
2945 'tests/fixtures/layout-no-timer.yaml')
2946 self.sched.reconfigure(self.config)
2947 self.registerJobs()
2948 self.waitUntilSettled()
2949 self.worker.release('.*')
2950 self.waitUntilSettled()
2951
James E. Blair91e34592015-07-31 16:45:59 -07002952 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002953 "Test that the RPC client can enqueue a change"
2954 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002955 A.addApproval('code-review', 2)
2956 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002957
2958 client = zuul.rpcclient.RPCClient('127.0.0.1',
2959 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002960 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002961 r = client.enqueue(tenant='tenant-one',
2962 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002963 project='org/project',
2964 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002965 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002966 self.waitUntilSettled()
2967 self.assertEqual(self.getJobFromHistory('project-merge').result,
2968 'SUCCESS')
2969 self.assertEqual(self.getJobFromHistory('project-test1').result,
2970 'SUCCESS')
2971 self.assertEqual(self.getJobFromHistory('project-test2').result,
2972 'SUCCESS')
2973 self.assertEqual(A.data['status'], 'MERGED')
2974 self.assertEqual(A.reported, 2)
2975 self.assertEqual(r, True)
2976
James E. Blair91e34592015-07-31 16:45:59 -07002977 def test_client_enqueue_ref(self):
2978 "Test that the RPC client can enqueue a ref"
2979
2980 client = zuul.rpcclient.RPCClient('127.0.0.1',
2981 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002982 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002983 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002984 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002985 pipeline='post',
2986 project='org/project',
2987 trigger='gerrit',
2988 ref='master',
2989 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2990 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2991 self.waitUntilSettled()
2992 job_names = [x.name for x in self.history]
2993 self.assertEqual(len(self.history), 1)
2994 self.assertIn('project-post', job_names)
2995 self.assertEqual(r, True)
2996
James E. Blairad28e912013-11-27 10:43:22 -08002997 def test_client_enqueue_negative(self):
2998 "Test that the RPC client returns errors"
2999 client = zuul.rpcclient.RPCClient('127.0.0.1',
3000 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003001 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003002 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003003 "Invalid tenant"):
3004 r = client.enqueue(tenant='tenant-foo',
3005 pipeline='gate',
3006 project='org/project',
3007 trigger='gerrit',
3008 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003009 self.assertEqual(r, False)
3010
3011 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003012 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003013 r = client.enqueue(tenant='tenant-one',
3014 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003015 project='project-does-not-exist',
3016 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003017 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003018 self.assertEqual(r, False)
3019
3020 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3021 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003022 r = client.enqueue(tenant='tenant-one',
3023 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003024 project='org/project',
3025 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003026 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003027 self.assertEqual(r, False)
3028
3029 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3030 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003031 r = client.enqueue(tenant='tenant-one',
3032 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003033 project='org/project',
3034 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003035 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003036 self.assertEqual(r, False)
3037
3038 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3039 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003040 r = client.enqueue(tenant='tenant-one',
3041 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003042 project='org/project',
3043 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003044 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003045 self.assertEqual(r, False)
3046
3047 self.waitUntilSettled()
3048 self.assertEqual(len(self.history), 0)
3049 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003050
3051 def test_client_promote(self):
3052 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003053 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3055 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3056 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003057 A.addApproval('code-review', 2)
3058 B.addApproval('code-review', 2)
3059 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003060
James E. Blair8b5408c2016-08-08 15:37:46 -07003061 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3062 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3063 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003064
3065 self.waitUntilSettled()
3066
Paul Belangerbaca3132016-11-04 12:49:54 -04003067 tenant = self.sched.abide.tenants.get('tenant-one')
3068 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003069 enqueue_times = {}
3070 for item in items:
3071 enqueue_times[str(item.change)] = item.enqueue_time
3072
James E. Blair36658cf2013-12-06 17:53:48 -08003073 client = zuul.rpcclient.RPCClient('127.0.0.1',
3074 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003075 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003076 r = client.promote(tenant='tenant-one',
3077 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003078 change_ids=['2,1', '3,1'])
3079
Sean Daguef39b9ca2014-01-10 21:34:35 -05003080 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003081 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003082 for item in items:
3083 self.assertEqual(
3084 enqueue_times[str(item.change)], item.enqueue_time)
3085
James E. Blair78acec92014-02-06 07:11:32 -08003086 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003087 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003088 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003089 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003090 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003091 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003092 self.waitUntilSettled()
3093
3094 self.assertEqual(len(self.builds), 6)
3095 self.assertEqual(self.builds[0].name, 'project-test1')
3096 self.assertEqual(self.builds[1].name, 'project-test2')
3097 self.assertEqual(self.builds[2].name, 'project-test1')
3098 self.assertEqual(self.builds[3].name, 'project-test2')
3099 self.assertEqual(self.builds[4].name, 'project-test1')
3100 self.assertEqual(self.builds[5].name, 'project-test2')
3101
Paul Belangerbaca3132016-11-04 12:49:54 -04003102 self.assertTrue(self.builds[0].hasChanges(B))
3103 self.assertFalse(self.builds[0].hasChanges(A))
3104 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003105
Paul Belangerbaca3132016-11-04 12:49:54 -04003106 self.assertTrue(self.builds[2].hasChanges(B))
3107 self.assertTrue(self.builds[2].hasChanges(C))
3108 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003109
Paul Belangerbaca3132016-11-04 12:49:54 -04003110 self.assertTrue(self.builds[4].hasChanges(B))
3111 self.assertTrue(self.builds[4].hasChanges(C))
3112 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003113
Paul Belanger174a8272017-03-14 13:20:10 -04003114 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003115 self.waitUntilSettled()
3116
3117 self.assertEqual(A.data['status'], 'MERGED')
3118 self.assertEqual(A.reported, 2)
3119 self.assertEqual(B.data['status'], 'MERGED')
3120 self.assertEqual(B.reported, 2)
3121 self.assertEqual(C.data['status'], 'MERGED')
3122 self.assertEqual(C.reported, 2)
3123
James E. Blair36658cf2013-12-06 17:53:48 -08003124 self.assertEqual(r, True)
3125
3126 def test_client_promote_dependent(self):
3127 "Test that the RPC client can promote a dependent change"
3128 # C (depends on B) -> B -> A ; then promote C to get:
3129 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003130 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003131 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3132 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3133 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3134
3135 C.setDependsOn(B, 1)
3136
James E. Blair8b5408c2016-08-08 15:37:46 -07003137 A.addApproval('code-review', 2)
3138 B.addApproval('code-review', 2)
3139 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003140
James E. Blair8b5408c2016-08-08 15:37:46 -07003141 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3142 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3143 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003144
3145 self.waitUntilSettled()
3146
3147 client = zuul.rpcclient.RPCClient('127.0.0.1',
3148 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003149 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003150 r = client.promote(tenant='tenant-one',
3151 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003152 change_ids=['3,1'])
3153
James E. Blair78acec92014-02-06 07:11:32 -08003154 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003155 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003156 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003157 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003158 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003159 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003160 self.waitUntilSettled()
3161
3162 self.assertEqual(len(self.builds), 6)
3163 self.assertEqual(self.builds[0].name, 'project-test1')
3164 self.assertEqual(self.builds[1].name, 'project-test2')
3165 self.assertEqual(self.builds[2].name, 'project-test1')
3166 self.assertEqual(self.builds[3].name, 'project-test2')
3167 self.assertEqual(self.builds[4].name, 'project-test1')
3168 self.assertEqual(self.builds[5].name, 'project-test2')
3169
Paul Belangerbaca3132016-11-04 12:49:54 -04003170 self.assertTrue(self.builds[0].hasChanges(B))
3171 self.assertFalse(self.builds[0].hasChanges(A))
3172 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003173
Paul Belangerbaca3132016-11-04 12:49:54 -04003174 self.assertTrue(self.builds[2].hasChanges(B))
3175 self.assertTrue(self.builds[2].hasChanges(C))
3176 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003177
Paul Belangerbaca3132016-11-04 12:49:54 -04003178 self.assertTrue(self.builds[4].hasChanges(B))
3179 self.assertTrue(self.builds[4].hasChanges(C))
3180 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003181
Paul Belanger174a8272017-03-14 13:20:10 -04003182 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003183 self.waitUntilSettled()
3184
3185 self.assertEqual(A.data['status'], 'MERGED')
3186 self.assertEqual(A.reported, 2)
3187 self.assertEqual(B.data['status'], 'MERGED')
3188 self.assertEqual(B.reported, 2)
3189 self.assertEqual(C.data['status'], 'MERGED')
3190 self.assertEqual(C.reported, 2)
3191
James E. Blair36658cf2013-12-06 17:53:48 -08003192 self.assertEqual(r, True)
3193
3194 def test_client_promote_negative(self):
3195 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003196 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003198 A.addApproval('code-review', 2)
3199 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003200 self.waitUntilSettled()
3201
3202 client = zuul.rpcclient.RPCClient('127.0.0.1',
3203 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003204 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003205
3206 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003207 r = client.promote(tenant='tenant-one',
3208 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003209 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003210 self.assertEqual(r, False)
3211
3212 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003213 r = client.promote(tenant='tenant-one',
3214 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003215 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003216 self.assertEqual(r, False)
3217
Paul Belanger174a8272017-03-14 13:20:10 -04003218 self.executor_server.hold_jobs_in_build = False
3219 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003220 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003221
James E. Blaircc324192017-04-19 14:36:08 -07003222 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003223 def test_queue_rate_limiting(self):
3224 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003225 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003226 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3227 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3228 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3229
3230 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003231 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003232
James E. Blair8b5408c2016-08-08 15:37:46 -07003233 A.addApproval('code-review', 2)
3234 B.addApproval('code-review', 2)
3235 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003236
James E. Blair8b5408c2016-08-08 15:37:46 -07003237 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3238 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3239 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003240 self.waitUntilSettled()
3241
3242 # Only A and B will have their merge jobs queued because
3243 # window is 2.
3244 self.assertEqual(len(self.builds), 2)
3245 self.assertEqual(self.builds[0].name, 'project-merge')
3246 self.assertEqual(self.builds[1].name, 'project-merge')
3247
James E. Blair732ee862017-04-21 07:49:26 -07003248 # Release the merge jobs one at a time.
3249 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003250 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003251 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003252 self.waitUntilSettled()
3253
3254 # Only A and B will have their test jobs queued because
3255 # window is 2.
3256 self.assertEqual(len(self.builds), 4)
3257 self.assertEqual(self.builds[0].name, 'project-test1')
3258 self.assertEqual(self.builds[1].name, 'project-test2')
3259 self.assertEqual(self.builds[2].name, 'project-test1')
3260 self.assertEqual(self.builds[3].name, 'project-test2')
3261
Paul Belanger174a8272017-03-14 13:20:10 -04003262 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003263 self.waitUntilSettled()
3264
James E. Blaircc324192017-04-19 14:36:08 -07003265 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003266 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003267 # A failed so window is reduced by 1 to 1.
3268 self.assertEqual(queue.window, 1)
3269 self.assertEqual(queue.window_floor, 1)
3270 self.assertEqual(A.data['status'], 'NEW')
3271
3272 # Gate is reset and only B's merge job is queued because
3273 # window shrunk to 1.
3274 self.assertEqual(len(self.builds), 1)
3275 self.assertEqual(self.builds[0].name, 'project-merge')
3276
Paul Belanger174a8272017-03-14 13:20:10 -04003277 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003278 self.waitUntilSettled()
3279
3280 # Only B's test jobs are queued because window is still 1.
3281 self.assertEqual(len(self.builds), 2)
3282 self.assertEqual(self.builds[0].name, 'project-test1')
3283 self.assertEqual(self.builds[1].name, 'project-test2')
3284
Paul Belanger174a8272017-03-14 13:20:10 -04003285 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003286 self.waitUntilSettled()
3287
3288 # B was successfully merged so window is increased to 2.
3289 self.assertEqual(queue.window, 2)
3290 self.assertEqual(queue.window_floor, 1)
3291 self.assertEqual(B.data['status'], 'MERGED')
3292
3293 # Only C is left and its merge job is queued.
3294 self.assertEqual(len(self.builds), 1)
3295 self.assertEqual(self.builds[0].name, 'project-merge')
3296
Paul Belanger174a8272017-03-14 13:20:10 -04003297 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003298 self.waitUntilSettled()
3299
3300 # After successful merge job the test jobs for C are queued.
3301 self.assertEqual(len(self.builds), 2)
3302 self.assertEqual(self.builds[0].name, 'project-test1')
3303 self.assertEqual(self.builds[1].name, 'project-test2')
3304
Paul Belanger174a8272017-03-14 13:20:10 -04003305 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003306 self.waitUntilSettled()
3307
3308 # C successfully merged so window is bumped to 3.
3309 self.assertEqual(queue.window, 3)
3310 self.assertEqual(queue.window_floor, 1)
3311 self.assertEqual(C.data['status'], 'MERGED')
3312
James E. Blaircc324192017-04-19 14:36:08 -07003313 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003314 def test_queue_rate_limiting_dependent(self):
3315 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003316 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3318 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3319 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3320
3321 B.setDependsOn(A, 1)
3322
Paul Belanger174a8272017-03-14 13:20:10 -04003323 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003324
James E. Blair8b5408c2016-08-08 15:37:46 -07003325 A.addApproval('code-review', 2)
3326 B.addApproval('code-review', 2)
3327 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003328
James E. Blair8b5408c2016-08-08 15:37:46 -07003329 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3330 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3331 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003332 self.waitUntilSettled()
3333
3334 # Only A and B will have their merge jobs queued because
3335 # window is 2.
3336 self.assertEqual(len(self.builds), 2)
3337 self.assertEqual(self.builds[0].name, 'project-merge')
3338 self.assertEqual(self.builds[1].name, 'project-merge')
3339
James E. Blairf4a5f022017-04-18 14:01:10 -07003340 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003341
3342 # Only A and B will have their test jobs queued because
3343 # window is 2.
3344 self.assertEqual(len(self.builds), 4)
3345 self.assertEqual(self.builds[0].name, 'project-test1')
3346 self.assertEqual(self.builds[1].name, 'project-test2')
3347 self.assertEqual(self.builds[2].name, 'project-test1')
3348 self.assertEqual(self.builds[3].name, 'project-test2')
3349
Paul Belanger174a8272017-03-14 13:20:10 -04003350 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003351 self.waitUntilSettled()
3352
James E. Blaircc324192017-04-19 14:36:08 -07003353 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003354 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003355 # A failed so window is reduced by 1 to 1.
3356 self.assertEqual(queue.window, 1)
3357 self.assertEqual(queue.window_floor, 1)
3358 self.assertEqual(A.data['status'], 'NEW')
3359 self.assertEqual(B.data['status'], 'NEW')
3360
3361 # Gate is reset and only C's merge job is queued because
3362 # window shrunk to 1 and A and B were dequeued.
3363 self.assertEqual(len(self.builds), 1)
3364 self.assertEqual(self.builds[0].name, 'project-merge')
3365
James E. Blairf4a5f022017-04-18 14:01:10 -07003366 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003367
3368 # Only C's test jobs are queued because window is still 1.
3369 self.assertEqual(len(self.builds), 2)
3370 self.assertEqual(self.builds[0].name, 'project-test1')
3371 self.assertEqual(self.builds[1].name, 'project-test2')
3372
Paul Belanger174a8272017-03-14 13:20:10 -04003373 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003374 self.waitUntilSettled()
3375
3376 # C was successfully merged so window is increased to 2.
3377 self.assertEqual(queue.window, 2)
3378 self.assertEqual(queue.window_floor, 1)
3379 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003380
3381 def test_worker_update_metadata(self):
3382 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003383 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003384
3385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003386 A.addApproval('code-review', 2)
3387 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003388 self.waitUntilSettled()
3389
Clint Byrumd4de79a2017-05-27 11:39:13 -07003390 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003391
3392 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003393 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003394
3395 start = time.time()
3396 while True:
3397 if time.time() - start > 10:
3398 raise Exception("Timeout waiting for gearman server to report "
3399 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003400 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003401 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003402 break
3403 else:
3404 time.sleep(0)
3405
3406 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003407 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003408
Paul Belanger174a8272017-03-14 13:20:10 -04003409 self.executor_server.hold_jobs_in_build = False
3410 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003411 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003412
James E. Blaircc324192017-04-19 14:36:08 -07003413 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003414 def test_footer_message(self):
3415 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003416 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003417 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003418 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003419 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003420 self.waitUntilSettled()
3421
3422 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003423 B.addApproval('code-review', 2)
3424 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003425 self.waitUntilSettled()
3426
3427 self.assertEqual(2, len(self.smtp_messages))
3428
Adam Gandelman82258fd2017-02-07 16:51:58 -08003429 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003430Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003431http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003432
Adam Gandelman82258fd2017-02-07 16:51:58 -08003433 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003434For CI problems and help debugging, contact ci@example.org"""
3435
Adam Gandelman82258fd2017-02-07 16:51:58 -08003436 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3437 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3438 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3439 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003440
Tobias Henkel9842bd72017-05-16 13:40:03 +02003441 @simple_layout('layouts/unmanaged-project.yaml')
3442 def test_unmanaged_project_start_message(self):
3443 "Test start reporting is not done for unmanaged projects."
3444 self.init_repo("org/project", tag='init')
3445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3446 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3447 self.waitUntilSettled()
3448
3449 self.assertEqual(0, len(A.messages))
3450
Clint Byrumb9de8be2017-05-27 13:49:19 -07003451 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003452 def test_merge_failure_reporters(self):
3453 """Check that the config is set up correctly"""
3454
Clint Byrumb9de8be2017-05-27 13:49:19 -07003455 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003456 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003457 "Merge Failed.\n\nThis change or one of its cross-repo "
3458 "dependencies was unable to be automatically merged with the "
3459 "current state of its repository. Please rebase the change and "
3460 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003461 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003462 self.assertEqual(
3463 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003464 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003465
3466 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003467 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003468 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003469 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003470
3471 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003472 tenant.layout.pipelines['check'].merge_failure_actions[0],
3473 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003474
3475 self.assertTrue(
3476 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003477 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003478 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003479 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3480 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003481 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003482 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003483 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003484 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003485 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003486 gerritreporter.GerritReporter) and
3487 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003488 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003489 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003490 )
3491 )
3492
James E. Blairec056492016-07-22 09:45:56 -07003493 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003494 def test_merge_failure_reports(self):
3495 """Check that when a change fails to merge the correct message is sent
3496 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003497 self.updateConfigLayout(
3498 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003499 self.sched.reconfigure(self.config)
3500 self.registerJobs()
3501
3502 # Check a test failure isn't reported to SMTP
3503 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003504 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003505 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003506 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003507 self.waitUntilSettled()
3508
3509 self.assertEqual(3, len(self.history)) # 3 jobs
3510 self.assertEqual(0, len(self.smtp_messages))
3511
3512 # Check a merge failure is reported to SMTP
3513 # B should be merged, but C will conflict with B
3514 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3515 B.addPatchset(['conflict'])
3516 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3517 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003518 B.addApproval('code-review', 2)
3519 C.addApproval('code-review', 2)
3520 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3521 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003522 self.waitUntilSettled()
3523
3524 self.assertEqual(6, len(self.history)) # A and B jobs
3525 self.assertEqual(1, len(self.smtp_messages))
3526 self.assertEqual('The merge failed! For more information...',
3527 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003528
James E. Blairec056492016-07-22 09:45:56 -07003529 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003530 def test_default_merge_failure_reports(self):
3531 """Check that the default merge failure reports are correct."""
3532
3533 # A should report success, B should report merge failure.
3534 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3535 A.addPatchset(['conflict'])
3536 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3537 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003538 A.addApproval('code-review', 2)
3539 B.addApproval('code-review', 2)
3540 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3541 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003542 self.waitUntilSettled()
3543
3544 self.assertEqual(3, len(self.history)) # A jobs
3545 self.assertEqual(A.reported, 2)
3546 self.assertEqual(B.reported, 2)
3547 self.assertEqual(A.data['status'], 'MERGED')
3548 self.assertEqual(B.data['status'], 'NEW')
3549 self.assertIn('Build succeeded', A.messages[1])
3550 self.assertIn('Merge Failed', B.messages[1])
3551 self.assertIn('automatically merged', B.messages[1])
3552 self.assertNotIn('logs.example.com', B.messages[1])
3553 self.assertNotIn('SKIPPED', B.messages[1])
3554
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003555 def test_client_get_running_jobs(self):
3556 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003557 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003558 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003559 A.addApproval('code-review', 2)
3560 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003561 self.waitUntilSettled()
3562
3563 client = zuul.rpcclient.RPCClient('127.0.0.1',
3564 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003565 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003566
3567 # Wait for gearman server to send the initial workData back to zuul
3568 start = time.time()
3569 while True:
3570 if time.time() - start > 10:
3571 raise Exception("Timeout waiting for gearman server to report "
3572 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003573 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003574 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003575 break
3576 else:
3577 time.sleep(0)
3578
3579 running_items = client.get_running_jobs()
3580
3581 self.assertEqual(1, len(running_items))
3582 running_item = running_items[0]
3583 self.assertEqual([], running_item['failing_reasons'])
3584 self.assertEqual([], running_item['items_behind'])
3585 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003586 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003587 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003588 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003589 self.assertEqual(True, running_item['active'])
3590 self.assertEqual('1,1', running_item['id'])
3591
3592 self.assertEqual(3, len(running_item['jobs']))
3593 for job in running_item['jobs']:
3594 if job['name'] == 'project-merge':
3595 self.assertEqual('project-merge', job['name'])
3596 self.assertEqual('gate', job['pipeline'])
3597 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003598 self.assertEqual(
3599 'finger://{hostname}/{uuid}'.format(
3600 hostname=self.executor_server.hostname,
3601 uuid=job['uuid']),
3602 job['url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003603 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003604 self.assertEqual(False, job['canceled'])
3605 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003606 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003607 self.assertEqual('gate', job['pipeline'])
3608 break
3609
Paul Belanger174a8272017-03-14 13:20:10 -04003610 self.executor_server.hold_jobs_in_build = False
3611 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003612 self.waitUntilSettled()
3613
3614 running_items = client.get_running_jobs()
3615 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003616
James E. Blairb70e55a2017-04-19 12:57:02 -07003617 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003618 def test_nonvoting_pipeline(self):
3619 "Test that a nonvoting pipeline (experimental) can still report"
3620
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003621 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3622 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003623 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3624 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003625
3626 self.assertEqual(self.getJobFromHistory('project-merge').result,
3627 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003628 self.assertEqual(
3629 self.getJobFromHistory('experimental-project-test').result,
3630 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003631 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003632
3633 def test_crd_gate(self):
3634 "Test cross-repo dependencies"
3635 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3636 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003637 A.addApproval('code-review', 2)
3638 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003639
3640 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3641 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3642 AM2.setMerged()
3643 AM1.setMerged()
3644
3645 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3646 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3647 BM2.setMerged()
3648 BM1.setMerged()
3649
3650 # A -> AM1 -> AM2
3651 # B -> BM1 -> BM2
3652 # A Depends-On: B
3653 # M2 is here to make sure it is never queried. If it is, it
3654 # means zuul is walking down the entire history of merged
3655 # changes.
3656
3657 B.setDependsOn(BM1, 1)
3658 BM1.setDependsOn(BM2, 1)
3659
3660 A.setDependsOn(AM1, 1)
3661 AM1.setDependsOn(AM2, 1)
3662
3663 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3664 A.subject, B.data['id'])
3665
James E. Blair8b5408c2016-08-08 15:37:46 -07003666 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003667 self.waitUntilSettled()
3668
3669 self.assertEqual(A.data['status'], 'NEW')
3670 self.assertEqual(B.data['status'], 'NEW')
3671
Paul Belanger5dccbe72016-11-14 11:17:37 -05003672 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003673 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003674
Paul Belanger174a8272017-03-14 13:20:10 -04003675 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003676 B.addApproval('approved', 1)
3677 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003678 self.waitUntilSettled()
3679
Paul Belanger174a8272017-03-14 13:20:10 -04003680 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003681 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003682 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003683 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003684 self.executor_server.hold_jobs_in_build = False
3685 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003686 self.waitUntilSettled()
3687
3688 self.assertEqual(AM2.queried, 0)
3689 self.assertEqual(BM2.queried, 0)
3690 self.assertEqual(A.data['status'], 'MERGED')
3691 self.assertEqual(B.data['status'], 'MERGED')
3692 self.assertEqual(A.reported, 2)
3693 self.assertEqual(B.reported, 2)
3694
Paul Belanger5dccbe72016-11-14 11:17:37 -05003695 changes = self.getJobFromHistory(
3696 'project-merge', 'org/project1').changes
3697 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003698
3699 def test_crd_branch(self):
3700 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003701
3702 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003703 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3704 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3705 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3706 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003707 A.addApproval('code-review', 2)
3708 B.addApproval('code-review', 2)
3709 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003710
3711 # A Depends-On: B+C
3712 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3713 A.subject, B.data['id'])
3714
Paul Belanger174a8272017-03-14 13:20:10 -04003715 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003716 B.addApproval('approved', 1)
3717 C.addApproval('approved', 1)
3718 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003719 self.waitUntilSettled()
3720
Paul Belanger174a8272017-03-14 13:20:10 -04003721 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003722 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003723 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003724 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003725 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003726 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003727 self.executor_server.hold_jobs_in_build = False
3728 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003729 self.waitUntilSettled()
3730
3731 self.assertEqual(A.data['status'], 'MERGED')
3732 self.assertEqual(B.data['status'], 'MERGED')
3733 self.assertEqual(C.data['status'], 'MERGED')
3734 self.assertEqual(A.reported, 2)
3735 self.assertEqual(B.reported, 2)
3736 self.assertEqual(C.reported, 2)
3737
Paul Belanger6379db12016-11-14 13:57:54 -05003738 changes = self.getJobFromHistory(
3739 'project-merge', 'org/project1').changes
3740 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003741
3742 def test_crd_multiline(self):
3743 "Test multiple depends-on lines in commit"
3744 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3745 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3746 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003747 A.addApproval('code-review', 2)
3748 B.addApproval('code-review', 2)
3749 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003750
3751 # A Depends-On: B+C
3752 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3753 A.subject, B.data['id'], C.data['id'])
3754
Paul Belanger174a8272017-03-14 13:20:10 -04003755 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003756 B.addApproval('approved', 1)
3757 C.addApproval('approved', 1)
3758 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003759 self.waitUntilSettled()
3760
Paul Belanger174a8272017-03-14 13:20:10 -04003761 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003762 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003763 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003764 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003765 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003766 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003767 self.executor_server.hold_jobs_in_build = False
3768 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003769 self.waitUntilSettled()
3770
3771 self.assertEqual(A.data['status'], 'MERGED')
3772 self.assertEqual(B.data['status'], 'MERGED')
3773 self.assertEqual(C.data['status'], 'MERGED')
3774 self.assertEqual(A.reported, 2)
3775 self.assertEqual(B.reported, 2)
3776 self.assertEqual(C.reported, 2)
3777
Paul Belanger5dccbe72016-11-14 11:17:37 -05003778 changes = self.getJobFromHistory(
3779 'project-merge', 'org/project1').changes
3780 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003781
3782 def test_crd_unshared_gate(self):
3783 "Test cross-repo dependencies in unshared gate queues"
3784 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3785 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003786 A.addApproval('code-review', 2)
3787 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003788
3789 # A Depends-On: B
3790 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3791 A.subject, B.data['id'])
3792
3793 # A and B do not share a queue, make sure that A is unable to
3794 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003795 B.addApproval('approved', 1)
3796 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003797 self.waitUntilSettled()
3798
3799 self.assertEqual(A.data['status'], 'NEW')
3800 self.assertEqual(B.data['status'], 'NEW')
3801 self.assertEqual(A.reported, 0)
3802 self.assertEqual(B.reported, 0)
3803 self.assertEqual(len(self.history), 0)
3804
3805 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003806 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003807 self.waitUntilSettled()
3808
3809 self.assertEqual(B.data['status'], 'MERGED')
3810 self.assertEqual(B.reported, 2)
3811
3812 # Now that B is merged, A should be able to be enqueued and
3813 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003814 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003815 self.waitUntilSettled()
3816
3817 self.assertEqual(A.data['status'], 'MERGED')
3818 self.assertEqual(A.reported, 2)
3819
James E. Blair96698e22015-04-02 07:48:21 -07003820 def test_crd_gate_reverse(self):
3821 "Test reverse cross-repo dependencies"
3822 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3823 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003824 A.addApproval('code-review', 2)
3825 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003826
3827 # A Depends-On: B
3828
3829 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3830 A.subject, B.data['id'])
3831
James E. Blair8b5408c2016-08-08 15:37:46 -07003832 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003833 self.waitUntilSettled()
3834
3835 self.assertEqual(A.data['status'], 'NEW')
3836 self.assertEqual(B.data['status'], 'NEW')
3837
Paul Belanger174a8272017-03-14 13:20:10 -04003838 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003839 A.addApproval('approved', 1)
3840 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003841 self.waitUntilSettled()
3842
Paul Belanger174a8272017-03-14 13:20:10 -04003843 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003844 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003845 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003846 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003847 self.executor_server.hold_jobs_in_build = False
3848 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003849 self.waitUntilSettled()
3850
3851 self.assertEqual(A.data['status'], 'MERGED')
3852 self.assertEqual(B.data['status'], 'MERGED')
3853 self.assertEqual(A.reported, 2)
3854 self.assertEqual(B.reported, 2)
3855
Paul Belanger5dccbe72016-11-14 11:17:37 -05003856 changes = self.getJobFromHistory(
3857 'project-merge', 'org/project1').changes
3858 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003859
James E. Blair5ee24252014-12-30 10:12:29 -08003860 def test_crd_cycle(self):
3861 "Test cross-repo dependency cycles"
3862 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3863 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003864 A.addApproval('code-review', 2)
3865 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003866
3867 # A -> B -> A (via commit-depends)
3868
3869 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3870 A.subject, B.data['id'])
3871 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3872 B.subject, A.data['id'])
3873
James E. Blair8b5408c2016-08-08 15:37:46 -07003874 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003875 self.waitUntilSettled()
3876
3877 self.assertEqual(A.reported, 0)
3878 self.assertEqual(B.reported, 0)
3879 self.assertEqual(A.data['status'], 'NEW')
3880 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003881
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003882 def test_crd_gate_unknown(self):
3883 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003884 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003885 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3886 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003887 A.addApproval('code-review', 2)
3888 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003889
3890 # A Depends-On: B
3891 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3892 A.subject, B.data['id'])
3893
James E. Blair8b5408c2016-08-08 15:37:46 -07003894 B.addApproval('approved', 1)
3895 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003896 self.waitUntilSettled()
3897
3898 # Unknown projects cannot share a queue with any other
3899 # since they don't have common jobs with any other (they have no jobs).
3900 # Changes which depend on unknown project changes
3901 # should not be processed in dependent pipeline
3902 self.assertEqual(A.data['status'], 'NEW')
3903 self.assertEqual(B.data['status'], 'NEW')
3904 self.assertEqual(A.reported, 0)
3905 self.assertEqual(B.reported, 0)
3906 self.assertEqual(len(self.history), 0)
3907
James E. Blair7720b402017-06-07 08:51:49 -07003908 # Simulate change B being gated outside this layout Set the
3909 # change merged before submitting the event so that when the
3910 # event triggers a gerrit query to update the change, we get
3911 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003912 B.setMerged()
James E. Blair7720b402017-06-07 08:51:49 -07003913 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003914 self.waitUntilSettled()
3915 self.assertEqual(len(self.history), 0)
3916
3917 # Now that B is merged, A should be able to be enqueued and
3918 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003919 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003920 self.waitUntilSettled()
3921
3922 self.assertEqual(A.data['status'], 'MERGED')
3923 self.assertEqual(A.reported, 2)
3924 self.assertEqual(B.data['status'], 'MERGED')
3925 self.assertEqual(B.reported, 0)
3926
James E. Blairbfb8e042014-12-30 17:01:44 -08003927 def test_crd_check(self):
3928 "Test cross-repo dependencies in independent pipelines"
3929
Paul Belanger174a8272017-03-14 13:20:10 -04003930 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003931 self.gearman_server.hold_jobs_in_queue = True
3932 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3933 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3934
3935 # A Depends-On: B
3936 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3937 A.subject, B.data['id'])
3938
3939 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3940 self.waitUntilSettled()
3941
James E. Blairbfb8e042014-12-30 17:01:44 -08003942 self.gearman_server.hold_jobs_in_queue = False
3943 self.gearman_server.release()
3944 self.waitUntilSettled()
3945
Paul Belanger174a8272017-03-14 13:20:10 -04003946 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003947 self.waitUntilSettled()
3948
James E. Blair2a535672017-04-27 12:03:15 -07003949 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003950
Paul Belanger174a8272017-03-14 13:20:10 -04003951 self.executor_server.hold_jobs_in_build = False
3952 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003953 self.waitUntilSettled()
3954
James E. Blairbfb8e042014-12-30 17:01:44 -08003955 self.assertEqual(A.data['status'], 'NEW')
3956 self.assertEqual(B.data['status'], 'NEW')
3957 self.assertEqual(A.reported, 1)
3958 self.assertEqual(B.reported, 0)
3959
3960 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003961 tenant = self.sched.abide.tenants.get('tenant-one')
3962 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003963
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003964 def test_crd_check_git_depends(self):
3965 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003966 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003967 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3968 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3969
3970 # Add two git-dependent changes and make sure they both report
3971 # success.
3972 B.setDependsOn(A, 1)
3973 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3974 self.waitUntilSettled()
3975 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3976 self.waitUntilSettled()
3977
James E. Blairb8c16472015-05-05 14:55:26 -07003978 self.orderedRelease()
3979 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003980 self.waitUntilSettled()
3981
3982 self.assertEqual(A.data['status'], 'NEW')
3983 self.assertEqual(B.data['status'], 'NEW')
3984 self.assertEqual(A.reported, 1)
3985 self.assertEqual(B.reported, 1)
3986
3987 self.assertEqual(self.history[0].changes, '1,1')
3988 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003989 tenant = self.sched.abide.tenants.get('tenant-one')
3990 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003991
3992 self.assertIn('Build succeeded', A.messages[0])
3993 self.assertIn('Build succeeded', B.messages[0])
3994
3995 def test_crd_check_duplicate(self):
3996 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003997 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003998 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3999 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004000 tenant = self.sched.abide.tenants.get('tenant-one')
4001 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004002
4003 # Add two git-dependent changes...
4004 B.setDependsOn(A, 1)
4005 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4006 self.waitUntilSettled()
4007 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4008
4009 # ...make sure the live one is not duplicated...
4010 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4011 self.waitUntilSettled()
4012 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4013
4014 # ...but the non-live one is able to be.
4015 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4016 self.waitUntilSettled()
4017 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4018
Clark Boylandd849822015-03-02 12:38:14 -08004019 # Release jobs in order to avoid races with change A jobs
4020 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004021 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004022 self.executor_server.hold_jobs_in_build = False
4023 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004024 self.waitUntilSettled()
4025
4026 self.assertEqual(A.data['status'], 'NEW')
4027 self.assertEqual(B.data['status'], 'NEW')
4028 self.assertEqual(A.reported, 1)
4029 self.assertEqual(B.reported, 1)
4030
4031 self.assertEqual(self.history[0].changes, '1,1 2,1')
4032 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004033 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004034
4035 self.assertIn('Build succeeded', A.messages[0])
4036 self.assertIn('Build succeeded', B.messages[0])
4037
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004038 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004039 "Test cross-repo dependencies re-enqueued in independent pipelines"
4040
4041 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004042 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4043 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004044
4045 # A Depends-On: B
4046 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4047 A.subject, B.data['id'])
4048
4049 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4050 self.waitUntilSettled()
4051
4052 self.sched.reconfigure(self.config)
4053
4054 # Make sure the items still share a change queue, and the
4055 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004056 tenant = self.sched.abide.tenants.get('tenant-one')
4057 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4058 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004059 first_item = queue.queue[0]
4060 for item in queue.queue:
4061 self.assertEqual(item.queue, first_item.queue)
4062 self.assertFalse(first_item.live)
4063 self.assertTrue(queue.queue[1].live)
4064
4065 self.gearman_server.hold_jobs_in_queue = False
4066 self.gearman_server.release()
4067 self.waitUntilSettled()
4068
4069 self.assertEqual(A.data['status'], 'NEW')
4070 self.assertEqual(B.data['status'], 'NEW')
4071 self.assertEqual(A.reported, 1)
4072 self.assertEqual(B.reported, 0)
4073
4074 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004075 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004076
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004077 def test_crd_check_reconfiguration(self):
4078 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4079
4080 def test_crd_undefined_project(self):
4081 """Test that undefined projects in dependencies are handled for
4082 independent pipelines"""
4083 # It's a hack for fake gerrit,
4084 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004085 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004086 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4087
Jesse Keatingea68ab42017-04-19 15:03:30 -07004088 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004089 def test_crd_check_ignore_dependencies(self):
4090 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004091
4092 self.gearman_server.hold_jobs_in_queue = True
4093 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4094 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4095 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4096
4097 # A Depends-On: B
4098 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4099 A.subject, B.data['id'])
4100 # C git-depends on B
4101 C.setDependsOn(B, 1)
4102 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4103 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4104 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4105 self.waitUntilSettled()
4106
4107 # Make sure none of the items share a change queue, and all
4108 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004109 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004110 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004111 self.assertEqual(len(check_pipeline.queues), 3)
4112 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4113 for item in check_pipeline.getAllItems():
4114 self.assertTrue(item.live)
4115
4116 self.gearman_server.hold_jobs_in_queue = False
4117 self.gearman_server.release()
4118 self.waitUntilSettled()
4119
4120 self.assertEqual(A.data['status'], 'NEW')
4121 self.assertEqual(B.data['status'], 'NEW')
4122 self.assertEqual(C.data['status'], 'NEW')
4123 self.assertEqual(A.reported, 1)
4124 self.assertEqual(B.reported, 1)
4125 self.assertEqual(C.reported, 1)
4126
4127 # Each job should have tested exactly one change
4128 for job in self.history:
4129 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004130
James E. Blair7cb84542017-04-19 13:35:05 -07004131 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004132 def test_crd_check_transitive(self):
4133 "Test transitive cross-repo dependencies"
4134 # Specifically, if A -> B -> C, and C gets a new patchset and
4135 # A gets a new patchset, ensure the test of A,2 includes B,1
4136 # and C,2 (not C,1 which would indicate stale data in the
4137 # cache for B).
4138 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4139 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4140 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4141
4142 # A Depends-On: B
4143 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4144 A.subject, B.data['id'])
4145
4146 # B Depends-On: C
4147 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4148 B.subject, C.data['id'])
4149
4150 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4151 self.waitUntilSettled()
4152 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4153
4154 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4155 self.waitUntilSettled()
4156 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4157
4158 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4159 self.waitUntilSettled()
4160 self.assertEqual(self.history[-1].changes, '3,1')
4161
4162 C.addPatchset()
4163 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4164 self.waitUntilSettled()
4165 self.assertEqual(self.history[-1].changes, '3,2')
4166
4167 A.addPatchset()
4168 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4169 self.waitUntilSettled()
4170 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004171
James E. Blaira8b90b32016-08-24 15:18:50 -07004172 def test_crd_check_unknown(self):
4173 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004174 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004175 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4176 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4177 # A Depends-On: B
4178 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4179 A.subject, B.data['id'])
4180
4181 # Make sure zuul has seen an event on B.
4182 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4183 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4184 self.waitUntilSettled()
4185
4186 self.assertEqual(A.data['status'], 'NEW')
4187 self.assertEqual(A.reported, 1)
4188 self.assertEqual(B.data['status'], 'NEW')
4189 self.assertEqual(B.reported, 0)
4190
James E. Blair92464a22016-04-05 10:21:26 -07004191 def test_crd_cycle_join(self):
4192 "Test an updated change creates a cycle"
4193 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4194
4195 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4196 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004197 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004198
4199 # Create B->A
4200 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4201 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4202 B.subject, A.data['id'])
4203 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4204 self.waitUntilSettled()
4205
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004206 # Dep is there so zuul should have reported on B
4207 self.assertEqual(B.reported, 1)
4208
James E. Blair92464a22016-04-05 10:21:26 -07004209 # Update A to add A->B (a cycle).
4210 A.addPatchset()
4211 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4212 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004213 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4214 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004215
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004216 # Dependency cycle injected so zuul should not have reported again on A
4217 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004218
4219 # Now if we update B to remove the depends-on, everything
4220 # should be okay. B; A->B
4221
4222 B.addPatchset()
4223 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004224 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4225 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004226
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004227 # Cycle was removed so now zuul should have reported again on A
4228 self.assertEqual(A.reported, 2)
4229
4230 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4231 self.waitUntilSettled()
4232 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004233
Jesse Keatingcc67b122017-04-19 14:54:57 -07004234 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004235 def test_disable_at(self):
4236 "Test a pipeline will only report to the disabled trigger when failing"
4237
Jesse Keatingcc67b122017-04-19 14:54:57 -07004238 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004239 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004240 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004241 0, tenant.layout.pipelines['check']._consecutive_failures)
4242 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004243
4244 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4245 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4246 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4247 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4248 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4249 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4250 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4251 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4252 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4253 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4254 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4255
Paul Belanger174a8272017-03-14 13:20:10 -04004256 self.executor_server.failJob('project-test1', A)
4257 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004258 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004259 self.executor_server.failJob('project-test1', D)
4260 self.executor_server.failJob('project-test1', E)
4261 self.executor_server.failJob('project-test1', F)
4262 self.executor_server.failJob('project-test1', G)
4263 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004264 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004265 self.executor_server.failJob('project-test1', J)
4266 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004267
4268 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4269 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4270 self.waitUntilSettled()
4271
4272 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004273 2, tenant.layout.pipelines['check']._consecutive_failures)
4274 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004275
4276 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4277 self.waitUntilSettled()
4278
4279 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004280 0, tenant.layout.pipelines['check']._consecutive_failures)
4281 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004282
4283 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4284 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4285 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4286 self.waitUntilSettled()
4287
4288 # We should be disabled now
4289 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004290 3, tenant.layout.pipelines['check']._consecutive_failures)
4291 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004292
4293 # We need to wait between each of these patches to make sure the
4294 # smtp messages come back in an expected order
4295 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4296 self.waitUntilSettled()
4297 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4298 self.waitUntilSettled()
4299 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4300 self.waitUntilSettled()
4301
4302 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4303 # leaving a message on each change
4304 self.assertEqual(1, len(A.messages))
4305 self.assertIn('Build failed.', A.messages[0])
4306 self.assertEqual(1, len(B.messages))
4307 self.assertIn('Build failed.', B.messages[0])
4308 self.assertEqual(1, len(C.messages))
4309 self.assertIn('Build succeeded.', C.messages[0])
4310 self.assertEqual(1, len(D.messages))
4311 self.assertIn('Build failed.', D.messages[0])
4312 self.assertEqual(1, len(E.messages))
4313 self.assertIn('Build failed.', E.messages[0])
4314 self.assertEqual(1, len(F.messages))
4315 self.assertIn('Build failed.', F.messages[0])
4316
4317 # The last 3 (GHI) would have only reported via smtp.
4318 self.assertEqual(3, len(self.smtp_messages))
4319 self.assertEqual(0, len(G.messages))
4320 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004321 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004322 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004323 self.assertEqual(0, len(H.messages))
4324 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004325 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004326 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004327 self.assertEqual(0, len(I.messages))
4328 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004329 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004330 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004331
4332 # Now reload the configuration (simulate a HUP) to check the pipeline
4333 # comes out of disabled
4334 self.sched.reconfigure(self.config)
4335
Jesse Keatingcc67b122017-04-19 14:54:57 -07004336 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004337
4338 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004339 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004340 0, tenant.layout.pipelines['check']._consecutive_failures)
4341 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004342
4343 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4344 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4345 self.waitUntilSettled()
4346
4347 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004348 2, tenant.layout.pipelines['check']._consecutive_failures)
4349 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004350
4351 # J and K went back to gerrit
4352 self.assertEqual(1, len(J.messages))
4353 self.assertIn('Build failed.', J.messages[0])
4354 self.assertEqual(1, len(K.messages))
4355 self.assertIn('Build failed.', K.messages[0])
4356 # No more messages reported via smtp
4357 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004358
James E. Blaircc020532017-04-19 13:04:44 -07004359 @simple_layout('layouts/one-job-project.yaml')
4360 def test_one_job_project(self):
4361 "Test that queueing works with one job"
4362 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4363 'master', 'A')
4364 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4365 'master', 'B')
4366 A.addApproval('code-review', 2)
4367 B.addApproval('code-review', 2)
4368 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4369 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4370 self.waitUntilSettled()
4371
4372 self.assertEqual(A.data['status'], 'MERGED')
4373 self.assertEqual(A.reported, 2)
4374 self.assertEqual(B.data['status'], 'MERGED')
4375 self.assertEqual(B.reported, 2)
4376
Paul Belanger71d98172016-11-08 10:56:31 -05004377 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004378 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004379
Paul Belanger174a8272017-03-14 13:20:10 -04004380 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004381 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4382 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4383 self.waitUntilSettled()
4384
Paul Belanger174a8272017-03-14 13:20:10 -04004385 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004386 self.waitUntilSettled()
4387
4388 self.assertEqual(len(self.builds), 2)
4389 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004390 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004391 self.waitUntilSettled()
4392
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004393 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004394 self.assertEqual(len(self.builds), 1,
4395 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004396 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004397 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004398 self.waitUntilSettled()
4399
Paul Belanger174a8272017-03-14 13:20:10 -04004400 self.executor_server.hold_jobs_in_build = False
4401 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004402 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004403 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004404 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4405 self.assertEqual(A.reported, 1)
4406 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004407
James E. Blair15be0e12017-01-03 13:45:20 -08004408 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004409 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004410
4411 self.fake_nodepool.paused = True
4412 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4413 A.addApproval('code-review', 2)
4414 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4415 self.waitUntilSettled()
4416
4417 self.zk.client.stop()
4418 self.zk.client.start()
4419 self.fake_nodepool.paused = False
4420 self.waitUntilSettled()
4421
4422 self.assertEqual(A.data['status'], 'MERGED')
4423 self.assertEqual(A.reported, 2)
4424
James E. Blair6ab79e02017-01-06 10:10:17 -08004425 def test_nodepool_failure(self):
4426 "Test that jobs are reported after a nodepool failure"
4427
4428 self.fake_nodepool.paused = True
4429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4430 A.addApproval('code-review', 2)
4431 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4432 self.waitUntilSettled()
4433
4434 req = self.fake_nodepool.getNodeRequests()[0]
4435 self.fake_nodepool.addFailRequest(req)
4436
4437 self.fake_nodepool.paused = False
4438 self.waitUntilSettled()
4439
4440 self.assertEqual(A.data['status'], 'NEW')
4441 self.assertEqual(A.reported, 2)
4442 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4443 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4444 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4445
Arieb6f068c2016-10-09 13:11:06 +03004446
James E. Blaira002b032017-04-18 10:35:48 -07004447class TestExecutor(ZuulTestCase):
4448 tenant_config_file = 'config/single-tenant/main.yaml'
4449
4450 def assertFinalState(self):
4451 # In this test, we expect to shut down in a non-final state,
4452 # so skip these checks.
4453 pass
4454
4455 def assertCleanShutdown(self):
4456 self.log.debug("Assert clean shutdown")
4457
4458 # After shutdown, make sure no jobs are running
4459 self.assertEqual({}, self.executor_server.job_workers)
4460
4461 # Make sure that git.Repo objects have been garbage collected.
4462 repos = []
4463 gc.collect()
4464 for obj in gc.get_objects():
4465 if isinstance(obj, git.Repo):
4466 self.log.debug("Leaked git repo object: %s" % repr(obj))
4467 repos.append(obj)
4468 self.assertEqual(len(repos), 0)
4469
4470 def test_executor_shutdown(self):
4471 "Test that the executor can shut down with jobs running"
4472
4473 self.executor_server.hold_jobs_in_build = True
4474 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4475 A.addApproval('code-review', 2)
4476 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4477 self.waitUntilSettled()
4478
4479
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004480class TestDependencyGraph(ZuulTestCase):
4481 tenant_config_file = 'config/dependency-graph/main.yaml'
4482
4483 def test_dependeny_graph_dispatch_jobs_once(self):
4484 "Test a job in a dependency graph is queued only once"
4485 # Job dependencies, starting with A
4486 # A
4487 # / \
4488 # B C
4489 # / \ / \
4490 # D F E
4491 # |
4492 # G
4493
4494 self.executor_server.hold_jobs_in_build = True
4495 change = self.fake_gerrit.addFakeChange(
4496 'org/project', 'master', 'change')
4497 change.addApproval('code-review', 2)
4498 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4499
4500 self.waitUntilSettled()
4501 self.assertEqual([b.name for b in self.builds], ['A'])
4502
4503 self.executor_server.release('A')
4504 self.waitUntilSettled()
4505 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4506
4507 self.executor_server.release('B')
4508 self.waitUntilSettled()
4509 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4510
4511 self.executor_server.release('D')
4512 self.waitUntilSettled()
4513 self.assertEqual([b.name for b in self.builds], ['C'])
4514
4515 self.executor_server.release('C')
4516 self.waitUntilSettled()
4517 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4518
4519 self.executor_server.release('F')
4520 self.waitUntilSettled()
4521 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4522
4523 self.executor_server.release('G')
4524 self.waitUntilSettled()
4525 self.assertEqual([b.name for b in self.builds], ['E'])
4526
4527 self.executor_server.release('E')
4528 self.waitUntilSettled()
4529 self.assertEqual(len(self.builds), 0)
4530
4531 self.executor_server.hold_jobs_in_build = False
4532 self.executor_server.release()
4533 self.waitUntilSettled()
4534
4535 self.assertEqual(len(self.builds), 0)
4536 self.assertEqual(len(self.history), 7)
4537
4538 self.assertEqual(change.data['status'], 'MERGED')
4539 self.assertEqual(change.reported, 2)
4540
4541 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4542 "Test that a job waits till all dependencies are successful"
4543 # Job dependencies, starting with A
4544 # A
4545 # / \
4546 # B C*
4547 # / \ / \
4548 # D F E
4549 # |
4550 # G
4551
4552 self.executor_server.hold_jobs_in_build = True
4553 change = self.fake_gerrit.addFakeChange(
4554 'org/project', 'master', 'change')
4555 change.addApproval('code-review', 2)
4556
4557 self.executor_server.failJob('C', change)
4558
4559 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4560
4561 self.waitUntilSettled()
4562 self.assertEqual([b.name for b in self.builds], ['A'])
4563
4564 self.executor_server.release('A')
4565 self.waitUntilSettled()
4566 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4567
4568 self.executor_server.release('B')
4569 self.waitUntilSettled()
4570 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4571
4572 self.executor_server.release('D')
4573 self.waitUntilSettled()
4574 self.assertEqual([b.name for b in self.builds], ['C'])
4575
4576 self.executor_server.release('C')
4577 self.waitUntilSettled()
4578 self.assertEqual(len(self.builds), 0)
4579
4580 self.executor_server.hold_jobs_in_build = False
4581 self.executor_server.release()
4582 self.waitUntilSettled()
4583
4584 self.assertEqual(len(self.builds), 0)
4585 self.assertEqual(len(self.history), 4)
4586
4587 self.assertEqual(change.data['status'], 'NEW')
4588 self.assertEqual(change.reported, 2)
4589
4590
Arieb6f068c2016-10-09 13:11:06 +03004591class TestDuplicatePipeline(ZuulTestCase):
4592 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4593
4594 def test_duplicate_pipelines(self):
4595 "Test that a change matching multiple pipelines works"
4596
4597 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4598 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4599 self.waitUntilSettled()
4600
4601 self.assertHistory([
4602 dict(name='project-test1', result='SUCCESS', changes='1,1',
4603 pipeline='dup1'),
4604 dict(name='project-test1', result='SUCCESS', changes='1,1',
4605 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004606 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004607
4608 self.assertEqual(len(A.messages), 2)
4609
Paul Belangera46a3742016-11-09 14:23:03 -05004610 if 'dup1' in A.messages[0]:
4611 self.assertIn('dup1', A.messages[0])
4612 self.assertNotIn('dup2', A.messages[0])
4613 self.assertIn('project-test1', A.messages[0])
4614 self.assertIn('dup2', A.messages[1])
4615 self.assertNotIn('dup1', A.messages[1])
4616 self.assertIn('project-test1', A.messages[1])
4617 else:
4618 self.assertIn('dup1', A.messages[1])
4619 self.assertNotIn('dup2', A.messages[1])
4620 self.assertIn('project-test1', A.messages[1])
4621 self.assertIn('dup2', A.messages[0])
4622 self.assertNotIn('dup1', A.messages[0])
4623 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004624
4625
Paul Belanger86085b32016-11-03 12:48:57 -04004626class TestSchedulerTemplatedProject(ZuulTestCase):
4627 tenant_config_file = 'config/templated-project/main.yaml'
4628
Paul Belanger174a8272017-03-14 13:20:10 -04004629 def test_job_from_templates_executed(self):
4630 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004631
4632 A = self.fake_gerrit.addFakeChange(
4633 'org/templated-project', 'master', 'A')
4634 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4635 self.waitUntilSettled()
4636
4637 self.assertEqual(self.getJobFromHistory('project-test1').result,
4638 'SUCCESS')
4639 self.assertEqual(self.getJobFromHistory('project-test2').result,
4640 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004641
4642 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004643 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004644
4645 A = self.fake_gerrit.addFakeChange(
4646 'org/layered-project', 'master', 'A')
4647 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4648 self.waitUntilSettled()
4649
4650 self.assertEqual(self.getJobFromHistory('project-test1').result,
4651 'SUCCESS')
4652 self.assertEqual(self.getJobFromHistory('project-test2').result,
4653 'SUCCESS')
4654 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4655 ).result, 'SUCCESS')
4656 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4657 ).result, 'SUCCESS')
4658 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4659 ).result, 'SUCCESS')
4660 self.assertEqual(self.getJobFromHistory('project-test6').result,
4661 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004662
4663
4664class TestSchedulerSuccessURL(ZuulTestCase):
4665 tenant_config_file = 'config/success-url/main.yaml'
4666
4667 def test_success_url(self):
4668 "Ensure bad build params are ignored"
4669 self.sched.reconfigure(self.config)
4670 self.init_repo('org/docs')
4671
4672 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4673 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4674 self.waitUntilSettled()
4675
4676 # Both builds ran: docs-draft-test + docs-draft-test2
4677 self.assertEqual(len(self.history), 2)
4678
4679 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004680 for build in self.history:
4681 if build.name == 'docs-draft-test':
4682 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004683 elif build.name == 'docs-draft-test2':
4684 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004685
4686 # Two msgs: 'Starting...' + results
4687 self.assertEqual(len(self.smtp_messages), 2)
4688 body = self.smtp_messages[1]['body'].splitlines()
4689 self.assertEqual('Build succeeded.', body[0])
4690
4691 self.assertIn(
4692 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4693 'docs-draft-test/%s/publish-docs/' % uuid,
4694 body[2])
4695
Paul Belanger174a8272017-03-14 13:20:10 -04004696 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004697 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004698 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4699 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004700 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004701 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004702
4703
Adam Gandelman4da00f62016-12-09 15:47:33 -08004704class TestSchedulerMerges(ZuulTestCase):
4705 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004706
4707 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004708 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004709 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004710 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004711 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4712 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4713 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4714 A.addApproval('code-review', 2)
4715 B.addApproval('code-review', 2)
4716 C.addApproval('code-review', 2)
4717 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4718 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4719 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4720 self.waitUntilSettled()
4721
4722 build = self.builds[-1]
4723 ref = self.getParameter(build, 'ZUUL_REF')
4724
James E. Blair2a535672017-04-27 12:03:15 -07004725 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4726 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004727 repo = git.Repo(path)
4728 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4729 repo_messages.reverse()
4730
Paul Belanger174a8272017-03-14 13:20:10 -04004731 self.executor_server.hold_jobs_in_build = False
4732 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004733 self.waitUntilSettled()
4734
4735 return repo_messages
4736
4737 def _test_merge(self, mode):
4738 us_path = os.path.join(
4739 self.upstream_root, 'org/project-%s' % mode)
4740 expected_messages = [
4741 'initial commit',
4742 'add content from fixture',
4743 # the intermediate commits order is nondeterministic
4744 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4745 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4746 ]
4747 result = self._test_project_merge_mode(mode)
4748 self.assertEqual(result[:2], expected_messages[:2])
4749 self.assertEqual(result[-2:], expected_messages[-2:])
4750
4751 def test_project_merge_mode_merge(self):
4752 self._test_merge('merge')
4753
4754 def test_project_merge_mode_merge_resolve(self):
4755 self._test_merge('merge-resolve')
4756
4757 def test_project_merge_mode_cherrypick(self):
4758 expected_messages = [
4759 'initial commit',
4760 'add content from fixture',
4761 'A-1',
4762 'B-1',
4763 'C-1']
4764 result = self._test_project_merge_mode('cherry-pick')
4765 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004766
4767 def test_merge_branch(self):
4768 "Test that the right commits are on alternate branches"
4769 self.create_branch('org/project-merge-branches', 'mp')
4770
Paul Belanger174a8272017-03-14 13:20:10 -04004771 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004772 A = self.fake_gerrit.addFakeChange(
4773 'org/project-merge-branches', 'mp', 'A')
4774 B = self.fake_gerrit.addFakeChange(
4775 'org/project-merge-branches', 'mp', 'B')
4776 C = self.fake_gerrit.addFakeChange(
4777 'org/project-merge-branches', 'mp', 'C')
4778 A.addApproval('code-review', 2)
4779 B.addApproval('code-review', 2)
4780 C.addApproval('code-review', 2)
4781 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4782 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4783 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4784 self.waitUntilSettled()
4785
Paul Belanger174a8272017-03-14 13:20:10 -04004786 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004787 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004788 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004789 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004790 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004791 self.waitUntilSettled()
4792
4793 build = self.builds[-1]
4794 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4795 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004796 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4797 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004798 repo = git.Repo(path)
4799
4800 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4801 repo_messages.reverse()
4802 correct_messages = [
4803 'initial commit',
4804 'add content from fixture',
4805 'mp commit',
4806 'A-1', 'B-1', 'C-1']
4807 self.assertEqual(repo_messages, correct_messages)
4808
Paul Belanger174a8272017-03-14 13:20:10 -04004809 self.executor_server.hold_jobs_in_build = False
4810 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004811 self.waitUntilSettled()
4812
4813 def test_merge_multi_branch(self):
4814 "Test that dependent changes on multiple branches are merged"
4815 self.create_branch('org/project-merge-branches', 'mp')
4816
Paul Belanger174a8272017-03-14 13:20:10 -04004817 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004818 A = self.fake_gerrit.addFakeChange(
4819 'org/project-merge-branches', 'master', 'A')
4820 B = self.fake_gerrit.addFakeChange(
4821 'org/project-merge-branches', 'mp', 'B')
4822 C = self.fake_gerrit.addFakeChange(
4823 'org/project-merge-branches', 'master', 'C')
4824 A.addApproval('code-review', 2)
4825 B.addApproval('code-review', 2)
4826 C.addApproval('code-review', 2)
4827 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4828 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4829 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4830 self.waitUntilSettled()
4831
4832 job_A = None
4833 for job in self.builds:
4834 if 'project-merge' in job.name:
4835 job_A = job
4836 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4837 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4838 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4839 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4840
James E. Blair2a535672017-04-27 12:03:15 -07004841 path = os.path.join(job_A.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_A)]
4846 repo_messages.reverse()
4847 correct_messages = [
4848 'initial commit', 'add content from fixture', 'A-1']
4849 self.assertEqual(repo_messages, correct_messages)
4850
Paul Belanger174a8272017-03-14 13:20:10 -04004851 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004852 self.waitUntilSettled()
4853
4854 job_B = None
4855 for job in self.builds:
4856 if 'project-merge' in job.name:
4857 job_B = job
4858 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4859 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4860 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4861 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4862
James E. Blair2a535672017-04-27 12:03:15 -07004863 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4864 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004865 repo = git.Repo(path)
4866 repo_messages = [c.message.strip()
4867 for c in repo.iter_commits(ref_B)]
4868 repo_messages.reverse()
4869 correct_messages = [
4870 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4871 self.assertEqual(repo_messages, correct_messages)
4872
Paul Belanger174a8272017-03-14 13:20:10 -04004873 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004874 self.waitUntilSettled()
4875
4876 job_C = None
4877 for job in self.builds:
4878 if 'project-merge' in job.name:
4879 job_C = job
4880 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4881 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4882 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4883 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004884 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4885 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004886 repo = git.Repo(path)
4887 repo_messages = [c.message.strip()
4888 for c in repo.iter_commits(ref_C)]
4889
4890 repo_messages.reverse()
4891 correct_messages = [
4892 'initial commit', 'add content from fixture',
4893 'A-1', 'C-1']
4894 # Ensure the right commits are in the history for this ref
4895 self.assertEqual(repo_messages, correct_messages)
4896
Paul Belanger174a8272017-03-14 13:20:10 -04004897 self.executor_server.hold_jobs_in_build = False
4898 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004899 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004900
4901
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004902class TestSemaphore(ZuulTestCase):
4903 tenant_config_file = 'config/semaphore/main.yaml'
4904
4905 def test_semaphore_one(self):
4906 "Test semaphores with max=1 (mutex)"
4907 tenant = self.sched.abide.tenants.get('tenant-one')
4908
4909 self.executor_server.hold_jobs_in_build = True
4910
4911 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4912 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4913 self.assertFalse('test-semaphore' in
4914 tenant.semaphore_handler.semaphores)
4915
4916 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4917 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4918 self.waitUntilSettled()
4919
4920 self.assertEqual(len(self.builds), 3)
4921 self.assertEqual(self.builds[0].name, 'project-test1')
4922 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4923 self.assertEqual(self.builds[2].name, 'project-test1')
4924
4925 self.executor_server.release('semaphore-one-test1')
4926 self.waitUntilSettled()
4927
4928 self.assertEqual(len(self.builds), 3)
4929 self.assertEqual(self.builds[0].name, 'project-test1')
4930 self.assertEqual(self.builds[1].name, 'project-test1')
4931 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4932 self.assertTrue('test-semaphore' in
4933 tenant.semaphore_handler.semaphores)
4934
4935 self.executor_server.release('semaphore-one-test2')
4936 self.waitUntilSettled()
4937
4938 self.assertEqual(len(self.builds), 3)
4939 self.assertEqual(self.builds[0].name, 'project-test1')
4940 self.assertEqual(self.builds[1].name, 'project-test1')
4941 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4942 self.assertTrue('test-semaphore' in
4943 tenant.semaphore_handler.semaphores)
4944
4945 self.executor_server.release('semaphore-one-test1')
4946 self.waitUntilSettled()
4947
4948 self.assertEqual(len(self.builds), 3)
4949 self.assertEqual(self.builds[0].name, 'project-test1')
4950 self.assertEqual(self.builds[1].name, 'project-test1')
4951 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4952 self.assertTrue('test-semaphore' in
4953 tenant.semaphore_handler.semaphores)
4954
4955 self.executor_server.release('semaphore-one-test2')
4956 self.waitUntilSettled()
4957
4958 self.assertEqual(len(self.builds), 2)
4959 self.assertEqual(self.builds[0].name, 'project-test1')
4960 self.assertEqual(self.builds[1].name, 'project-test1')
4961 self.assertFalse('test-semaphore' in
4962 tenant.semaphore_handler.semaphores)
4963
4964 self.executor_server.hold_jobs_in_build = False
4965 self.executor_server.release()
4966
4967 self.waitUntilSettled()
4968 self.assertEqual(len(self.builds), 0)
4969
4970 self.assertEqual(A.reported, 1)
4971 self.assertEqual(B.reported, 1)
4972 self.assertFalse('test-semaphore' in
4973 tenant.semaphore_handler.semaphores)
4974
4975 def test_semaphore_two(self):
4976 "Test semaphores with max>1"
4977 tenant = self.sched.abide.tenants.get('tenant-one')
4978
4979 self.executor_server.hold_jobs_in_build = True
4980 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4981 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4982 self.assertFalse('test-semaphore-two' in
4983 tenant.semaphore_handler.semaphores)
4984
4985 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4986 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4987 self.waitUntilSettled()
4988
4989 self.assertEqual(len(self.builds), 4)
4990 self.assertEqual(self.builds[0].name, 'project-test1')
4991 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4992 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4993 self.assertEqual(self.builds[3].name, 'project-test1')
4994 self.assertTrue('test-semaphore-two' in
4995 tenant.semaphore_handler.semaphores)
4996 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4997 'test-semaphore-two', [])), 2)
4998
4999 self.executor_server.release('semaphore-two-test1')
5000 self.waitUntilSettled()
5001
5002 self.assertEqual(len(self.builds), 4)
5003 self.assertEqual(self.builds[0].name, 'project-test1')
5004 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5005 self.assertEqual(self.builds[2].name, 'project-test1')
5006 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5007 self.assertTrue('test-semaphore-two' in
5008 tenant.semaphore_handler.semaphores)
5009 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5010 'test-semaphore-two', [])), 2)
5011
5012 self.executor_server.release('semaphore-two-test2')
5013 self.waitUntilSettled()
5014
5015 self.assertEqual(len(self.builds), 4)
5016 self.assertEqual(self.builds[0].name, 'project-test1')
5017 self.assertEqual(self.builds[1].name, 'project-test1')
5018 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5019 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5020 self.assertTrue('test-semaphore-two' in
5021 tenant.semaphore_handler.semaphores)
5022 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5023 'test-semaphore-two', [])), 2)
5024
5025 self.executor_server.release('semaphore-two-test1')
5026 self.waitUntilSettled()
5027
5028 self.assertEqual(len(self.builds), 3)
5029 self.assertEqual(self.builds[0].name, 'project-test1')
5030 self.assertEqual(self.builds[1].name, 'project-test1')
5031 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5032 self.assertTrue('test-semaphore-two' in
5033 tenant.semaphore_handler.semaphores)
5034 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5035 'test-semaphore-two', [])), 1)
5036
5037 self.executor_server.release('semaphore-two-test2')
5038 self.waitUntilSettled()
5039
5040 self.assertEqual(len(self.builds), 2)
5041 self.assertEqual(self.builds[0].name, 'project-test1')
5042 self.assertEqual(self.builds[1].name, 'project-test1')
5043 self.assertFalse('test-semaphore-two' in
5044 tenant.semaphore_handler.semaphores)
5045
5046 self.executor_server.hold_jobs_in_build = False
5047 self.executor_server.release()
5048
5049 self.waitUntilSettled()
5050 self.assertEqual(len(self.builds), 0)
5051
5052 self.assertEqual(A.reported, 1)
5053 self.assertEqual(B.reported, 1)
5054
5055 def test_semaphore_abandon(self):
5056 "Test abandon with job semaphores"
5057 self.executor_server.hold_jobs_in_build = True
5058 tenant = self.sched.abide.tenants.get('tenant-one')
5059 check_pipeline = tenant.layout.pipelines['check']
5060
5061 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5062 self.assertFalse('test-semaphore' in
5063 tenant.semaphore_handler.semaphores)
5064
5065 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5066 self.waitUntilSettled()
5067
5068 self.assertTrue('test-semaphore' in
5069 tenant.semaphore_handler.semaphores)
5070
5071 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5072 self.waitUntilSettled()
5073
5074 # The check pipeline should be empty
5075 items = check_pipeline.getAllItems()
5076 self.assertEqual(len(items), 0)
5077
5078 # The semaphore should be released
5079 self.assertFalse('test-semaphore' in
5080 tenant.semaphore_handler.semaphores)
5081
5082 self.executor_server.hold_jobs_in_build = False
5083 self.executor_server.release()
5084 self.waitUntilSettled()
5085
5086 def test_semaphore_reconfigure(self):
5087 "Test reconfigure with job semaphores"
5088 self.executor_server.hold_jobs_in_build = True
5089 tenant = self.sched.abide.tenants.get('tenant-one')
5090 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5091 self.assertFalse('test-semaphore' in
5092 tenant.semaphore_handler.semaphores)
5093
5094 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5095 self.waitUntilSettled()
5096
5097 self.assertTrue('test-semaphore' in
5098 tenant.semaphore_handler.semaphores)
5099
5100 # reconfigure without layout change
5101 self.sched.reconfigure(self.config)
5102 self.waitUntilSettled()
5103 tenant = self.sched.abide.tenants.get('tenant-one')
5104
5105 # semaphore still must be held
5106 self.assertTrue('test-semaphore' in
5107 tenant.semaphore_handler.semaphores)
5108
5109 self.commitConfigUpdate(
5110 'common-config',
5111 'config/semaphore/zuul-reconfiguration.yaml')
5112 self.sched.reconfigure(self.config)
5113 self.waitUntilSettled()
5114 tenant = self.sched.abide.tenants.get('tenant-one')
5115
5116 self.executor_server.release('project-test1')
5117 self.waitUntilSettled()
5118
5119 # There should be no builds anymore
5120 self.assertEqual(len(self.builds), 0)
5121
5122 # The semaphore should be released
5123 self.assertFalse('test-semaphore' in
5124 tenant.semaphore_handler.semaphores)
5125
5126
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005127class TestSemaphoreMultiTenant(ZuulTestCase):
5128 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5129
5130 def test_semaphore_tenant_isolation(self):
5131 "Test semaphores in multiple tenants"
5132
5133 self.waitUntilSettled()
5134 tenant_one = self.sched.abide.tenants.get('tenant-one')
5135 tenant_two = self.sched.abide.tenants.get('tenant-two')
5136
5137 self.executor_server.hold_jobs_in_build = True
5138 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5139 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5140 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5141 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5142 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5143 self.assertFalse('test-semaphore' in
5144 tenant_one.semaphore_handler.semaphores)
5145 self.assertFalse('test-semaphore' in
5146 tenant_two.semaphore_handler.semaphores)
5147
5148 # add patches to project1 of tenant-one
5149 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5150 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5151 self.waitUntilSettled()
5152
5153 # one build of project1-test1 must run
5154 # semaphore of tenant-one must be acquired once
5155 # semaphore of tenant-two must not be acquired
5156 self.assertEqual(len(self.builds), 1)
5157 self.assertEqual(self.builds[0].name, 'project1-test1')
5158 self.assertTrue('test-semaphore' in
5159 tenant_one.semaphore_handler.semaphores)
5160 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5161 'test-semaphore', [])), 1)
5162 self.assertFalse('test-semaphore' in
5163 tenant_two.semaphore_handler.semaphores)
5164
5165 # add patches to project2 of tenant-two
5166 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5167 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5168 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5169 self.waitUntilSettled()
5170
5171 # one build of project1-test1 must run
5172 # two builds of project2-test1 must run
5173 # semaphore of tenant-one must be acquired once
5174 # semaphore of tenant-two must be acquired twice
5175 self.assertEqual(len(self.builds), 3)
5176 self.assertEqual(self.builds[0].name, 'project1-test1')
5177 self.assertEqual(self.builds[1].name, 'project2-test1')
5178 self.assertEqual(self.builds[2].name, 'project2-test1')
5179 self.assertTrue('test-semaphore' in
5180 tenant_one.semaphore_handler.semaphores)
5181 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5182 'test-semaphore', [])), 1)
5183 self.assertTrue('test-semaphore' in
5184 tenant_two.semaphore_handler.semaphores)
5185 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5186 'test-semaphore', [])), 2)
5187
5188 self.executor_server.release('project1-test1')
5189 self.waitUntilSettled()
5190
5191 # one build of project1-test1 must run
5192 # two builds of project2-test1 must run
5193 # semaphore of tenant-one must be acquired once
5194 # semaphore of tenant-two must be acquired twice
5195 self.assertEqual(len(self.builds), 3)
5196 self.assertEqual(self.builds[0].name, 'project2-test1')
5197 self.assertEqual(self.builds[1].name, 'project2-test1')
5198 self.assertEqual(self.builds[2].name, 'project1-test1')
5199 self.assertTrue('test-semaphore' in
5200 tenant_one.semaphore_handler.semaphores)
5201 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5202 'test-semaphore', [])), 1)
5203 self.assertTrue('test-semaphore' in
5204 tenant_two.semaphore_handler.semaphores)
5205 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5206 'test-semaphore', [])), 2)
5207
5208 self.executor_server.release('project2-test1')
5209 self.waitUntilSettled()
5210
5211 # one build of project1-test1 must run
5212 # one build of project2-test1 must run
5213 # semaphore of tenant-one must be acquired once
5214 # semaphore of tenant-two must be acquired once
5215 self.assertEqual(len(self.builds), 2)
5216 self.assertTrue('test-semaphore' in
5217 tenant_one.semaphore_handler.semaphores)
5218 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5219 'test-semaphore', [])), 1)
5220 self.assertTrue('test-semaphore' in
5221 tenant_two.semaphore_handler.semaphores)
5222 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5223 'test-semaphore', [])), 1)
5224
5225 self.executor_server.hold_jobs_in_build = False
5226 self.executor_server.release()
5227
5228 self.waitUntilSettled()
5229
5230 # no build must run
5231 # semaphore of tenant-one must not be acquired
5232 # semaphore of tenant-two must not be acquired
5233 self.assertEqual(len(self.builds), 0)
5234 self.assertFalse('test-semaphore' in
5235 tenant_one.semaphore_handler.semaphores)
5236 self.assertFalse('test-semaphore' in
5237 tenant_two.semaphore_handler.semaphores)
5238
5239 self.assertEqual(A.reported, 1)
5240 self.assertEqual(B.reported, 1)
5241
5242
5243class TestSemaphoreInRepo(ZuulTestCase):
5244 tenant_config_file = 'config/in-repo/main.yaml'
5245
5246 def test_semaphore_in_repo(self):
5247 "Test semaphores in repo config"
5248
5249 # This tests dynamic semaphore handling in project repos. The semaphore
5250 # max value should not be evaluated dynamically but must be updated
5251 # after the change lands.
5252
5253 self.waitUntilSettled()
5254 tenant = self.sched.abide.tenants.get('tenant-one')
5255
5256 in_repo_conf = textwrap.dedent(
5257 """
5258 - job:
5259 name: project-test2
5260 semaphore: test-semaphore
5261
5262 - project:
5263 name: org/project
5264 tenant-one-gate:
5265 jobs:
5266 - project-test2
5267
5268 # the max value in dynamic layout must be ignored
5269 - semaphore:
5270 name: test-semaphore
5271 max: 2
5272 """)
5273
5274 in_repo_playbook = textwrap.dedent(
5275 """
5276 - hosts: all
5277 tasks: []
5278 """)
5279
5280 file_dict = {'.zuul.yaml': in_repo_conf,
5281 'playbooks/project-test2.yaml': in_repo_playbook}
5282 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5283 files=file_dict)
5284 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5285 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5286 B.setDependsOn(A, 1)
5287 C.setDependsOn(A, 1)
5288
5289 self.executor_server.hold_jobs_in_build = True
5290
5291 A.addApproval('code-review', 2)
5292 B.addApproval('code-review', 2)
5293 C.addApproval('code-review', 2)
5294 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5295 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5296 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5297 self.waitUntilSettled()
5298
5299 # check that the layout in a queue item still has max value of 1
5300 # for test-semaphore
5301 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5302 queue = None
5303 for queue_candidate in pipeline.queues:
5304 if queue_candidate.name == 'org/project':
5305 queue = queue_candidate
5306 break
5307 queue_item = queue.queue[0]
5308 item_dynamic_layout = queue_item.current_build_set.layout
5309 dynamic_test_semaphore = \
5310 item_dynamic_layout.semaphores.get('test-semaphore')
5311 self.assertEqual(dynamic_test_semaphore.max, 1)
5312
5313 # one build must be in queue, one semaphores acquired
5314 self.assertEqual(len(self.builds), 1)
5315 self.assertEqual(self.builds[0].name, 'project-test2')
5316 self.assertTrue('test-semaphore' in
5317 tenant.semaphore_handler.semaphores)
5318 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5319 'test-semaphore', [])), 1)
5320
5321 self.executor_server.release('project-test2')
5322 self.waitUntilSettled()
5323
5324 # change A must be merged
5325 self.assertEqual(A.data['status'], 'MERGED')
5326 self.assertEqual(A.reported, 2)
5327
5328 # send change-merged event as the gerrit mock doesn't send it
5329 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5330 self.waitUntilSettled()
5331
5332 # now that change A was merged, the new semaphore max must be effective
5333 tenant = self.sched.abide.tenants.get('tenant-one')
5334 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5335
5336 # two builds must be in queue, two semaphores acquired
5337 self.assertEqual(len(self.builds), 2)
5338 self.assertEqual(self.builds[0].name, 'project-test2')
5339 self.assertEqual(self.builds[1].name, 'project-test2')
5340 self.assertTrue('test-semaphore' in
5341 tenant.semaphore_handler.semaphores)
5342 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5343 'test-semaphore', [])), 2)
5344
5345 self.executor_server.release('project-test2')
5346 self.waitUntilSettled()
5347
5348 self.assertEqual(len(self.builds), 0)
5349 self.assertFalse('test-semaphore' in
5350 tenant.semaphore_handler.semaphores)
5351
5352 self.executor_server.hold_jobs_in_build = False
5353 self.executor_server.release()
5354
5355 self.waitUntilSettled()
5356 self.assertEqual(len(self.builds), 0)
5357
5358 self.assertEqual(A.reported, 2)
5359 self.assertEqual(B.reported, 2)
5360 self.assertEqual(C.reported, 2)