blob: 0229d655ee201b25179526e291cac7d124ab73a6 [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. Blairdb93b302017-07-19 15:33:11 -07001181 def test_dependent_changes_rebase(self):
1182 # Test that no errors occur when we walk a dependency tree
1183 # with an unused leaf node due to a rebase.
1184 # Start by constructing: C -> B -> A
1185 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1186 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1187 B.setDependsOn(A, 1)
1188
1189 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1190 C.setDependsOn(B, 1)
1191
1192 # Then rebase to form: D -> C -> A
1193 C.addPatchset() # C,2
1194 C.setDependsOn(A, 1)
1195
1196 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1197 D.setDependsOn(C, 2)
1198
1199 # Walk the entire tree
1200 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1201 self.waitUntilSettled()
1202 self.assertEqual(len(self.history), 3)
1203
1204 # Verify that walking just part of the tree still works
1205 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1206 self.waitUntilSettled()
1207 self.assertEqual(len(self.history), 6)
1208
James E. Blaircaec0c52012-08-22 14:52:22 -07001209 def test_dependent_changes_dequeue(self):
1210 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001211
James E. Blaircaec0c52012-08-22 14:52:22 -07001212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1214 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001215 A.addApproval('code-review', 2)
1216 B.addApproval('code-review', 2)
1217 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001218
1219 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1220 M1.setMerged()
1221
1222 # C -> B -> A -> M1
1223
1224 C.setDependsOn(B, 1)
1225 B.setDependsOn(A, 1)
1226 A.setDependsOn(M1, 1)
1227
Paul Belanger174a8272017-03-14 13:20:10 -04001228 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001229
James E. Blair8b5408c2016-08-08 15:37:46 -07001230 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1231 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1232 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001233
1234 self.waitUntilSettled()
1235
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001236 self.assertEqual(A.data['status'], 'NEW')
1237 self.assertEqual(A.reported, 2)
1238 self.assertEqual(B.data['status'], 'NEW')
1239 self.assertEqual(B.reported, 2)
1240 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001241 self.assertIn('This change depends on a change that failed to merge.',
1242 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001243 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001244
James E. Blair972e3c72013-08-29 12:04:55 -07001245 def test_failing_dependent_changes(self):
1246 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001247 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1249 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1250 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1251 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1252 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001253 A.addApproval('code-review', 2)
1254 B.addApproval('code-review', 2)
1255 C.addApproval('code-review', 2)
1256 D.addApproval('code-review', 2)
1257 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001258
1259 # E, D -> C -> B, A
1260
1261 D.setDependsOn(C, 1)
1262 C.setDependsOn(B, 1)
1263
Paul Belanger174a8272017-03-14 13:20:10 -04001264 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001265
James E. Blair8b5408c2016-08-08 15:37:46 -07001266 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1267 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1268 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1269 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1270 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001271
1272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001273 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001274 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001275 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001276 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001277 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001278 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001279 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001280 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001281 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001282 self.waitUntilSettled()
1283
Paul Belanger174a8272017-03-14 13:20:10 -04001284 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001285 for build in self.builds:
1286 if build.parameters['ZUUL_CHANGE'] != '1':
1287 build.release()
1288 self.waitUntilSettled()
1289
Paul Belanger174a8272017-03-14 13:20:10 -04001290 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001291 self.waitUntilSettled()
1292
1293 self.assertEqual(A.data['status'], 'MERGED')
1294 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001295 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001296 self.assertEqual(B.data['status'], 'NEW')
1297 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001298 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.assertEqual(C.data['status'], 'NEW')
1300 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001301 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001302 self.assertEqual(D.data['status'], 'NEW')
1303 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001304 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.assertEqual(E.data['status'], 'MERGED')
1306 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001307 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001308 self.assertEqual(len(self.history), 18)
1309
James E. Blairec590122012-08-22 15:19:31 -07001310 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001311 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001312 # If it's dequeued more than once, we should see extra
1313 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001314
Paul Belanger174a8272017-03-14 13:20:10 -04001315 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001316 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1317 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1318 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001319 A.addApproval('code-review', 2)
1320 B.addApproval('code-review', 2)
1321 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001322
Paul Belanger174a8272017-03-14 13:20:10 -04001323 self.executor_server.failJob('project-test1', A)
1324 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001325
James E. Blair8b5408c2016-08-08 15:37:46 -07001326 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1327 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1328 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001329
1330 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001331
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001332 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001333 self.assertEqual(self.builds[0].name, 'project-merge')
1334 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001335
Paul Belanger174a8272017-03-14 13:20:10 -04001336 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001337 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001338 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001339 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001340 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001341 self.waitUntilSettled()
1342
James E. Blair6df67132017-04-19 13:53:20 -07001343 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001344 self.assertEqual(self.builds[0].name, 'project-test1')
1345 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001346 self.assertEqual(self.builds[2].name, 'project-test1')
1347 self.assertEqual(self.builds[3].name, 'project-test2')
1348 self.assertEqual(self.builds[4].name, 'project-test1')
1349 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001350
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001351 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001352 self.waitUntilSettled()
1353
James E. Blair6df67132017-04-19 13:53:20 -07001354 self.assertEqual(len(self.builds), 2) # test2, merge for B
1355 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001356
Paul Belanger174a8272017-03-14 13:20:10 -04001357 self.executor_server.hold_jobs_in_build = False
1358 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001359 self.waitUntilSettled()
1360
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001361 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001362 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001363
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001364 self.assertEqual(A.data['status'], 'NEW')
1365 self.assertEqual(B.data['status'], 'MERGED')
1366 self.assertEqual(C.data['status'], 'MERGED')
1367 self.assertEqual(A.reported, 2)
1368 self.assertEqual(B.reported, 2)
1369 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001370
James E. Blaircc020532017-04-19 13:04:44 -07001371 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001372 def test_nonvoting_job(self):
1373 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001374
James E. Blair4ec821f2012-08-23 15:28:28 -07001375 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1376 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001377 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001378 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001379 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001380
1381 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001382
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001383 self.assertEqual(A.data['status'], 'MERGED')
1384 self.assertEqual(A.reported, 2)
1385 self.assertEqual(
1386 self.getJobFromHistory('nonvoting-project-merge').result,
1387 'SUCCESS')
1388 self.assertEqual(
1389 self.getJobFromHistory('nonvoting-project-test1').result,
1390 'SUCCESS')
1391 self.assertEqual(
1392 self.getJobFromHistory('nonvoting-project-test2').result,
1393 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001394
James E. Blair5821bd92015-09-16 08:48:15 -07001395 for build in self.builds:
1396 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1397
James E. Blaire0487072012-08-29 17:38:31 -07001398 def test_check_queue_success(self):
1399 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001400
James E. Blaire0487072012-08-29 17:38:31 -07001401 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1402 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1403
1404 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001405
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001406 self.assertEqual(A.data['status'], 'NEW')
1407 self.assertEqual(A.reported, 1)
1408 self.assertEqual(self.getJobFromHistory('project-merge').result,
1409 'SUCCESS')
1410 self.assertEqual(self.getJobFromHistory('project-test1').result,
1411 'SUCCESS')
1412 self.assertEqual(self.getJobFromHistory('project-test2').result,
1413 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001414
1415 def test_check_queue_failure(self):
1416 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001417
James E. Blaire0487072012-08-29 17:38:31 -07001418 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001419 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001420 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1421
1422 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001423
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001424 self.assertEqual(A.data['status'], 'NEW')
1425 self.assertEqual(A.reported, 1)
1426 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001427 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001428 self.assertEqual(self.getJobFromHistory('project-test1').result,
1429 'SUCCESS')
1430 self.assertEqual(self.getJobFromHistory('project-test2').result,
1431 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001432
James E. Blair7cb84542017-04-19 13:35:05 -07001433 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001434 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001435 # This particular test does a large amount of merges and needs a little
1436 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001437 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001438 "test that dependent changes behind dequeued changes work"
1439 # This complicated test is a reproduction of a real life bug
1440 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001441
Paul Belanger174a8272017-03-14 13:20:10 -04001442 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001443 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1444 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1445 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1446 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1447 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1448 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1449 D.setDependsOn(C, 1)
1450 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001451 A.addApproval('code-review', 2)
1452 B.addApproval('code-review', 2)
1453 C.addApproval('code-review', 2)
1454 D.addApproval('code-review', 2)
1455 E.addApproval('code-review', 2)
1456 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001457
1458 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001459
1460 # Change object re-use in the gerrit trigger is hidden if
1461 # changes are added in quick succession; waiting makes it more
1462 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001463 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001464 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001465 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001466 self.waitUntilSettled()
1467
Paul Belanger174a8272017-03-14 13:20:10 -04001468 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001469 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001470 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001471 self.waitUntilSettled()
1472
James E. Blair8b5408c2016-08-08 15:37:46 -07001473 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001474 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001475 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001476 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001477 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001478 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001479 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001480 self.waitUntilSettled()
1481
Paul Belanger174a8272017-03-14 13:20:10 -04001482 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001483 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001484 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001485 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001486 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001487 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001488 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001489 self.waitUntilSettled()
1490
1491 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001492
1493 # Grab pointers to the jobs we want to release before
1494 # releasing any, because list indexes may change as
1495 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001496 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001497 a.release()
1498 b.release()
1499 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001500 self.waitUntilSettled()
1501
Paul Belanger174a8272017-03-14 13:20:10 -04001502 self.executor_server.hold_jobs_in_build = False
1503 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001504 self.waitUntilSettled()
1505
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001506 self.assertEqual(A.data['status'], 'NEW')
1507 self.assertEqual(B.data['status'], 'MERGED')
1508 self.assertEqual(C.data['status'], 'MERGED')
1509 self.assertEqual(D.data['status'], 'MERGED')
1510 self.assertEqual(E.data['status'], 'MERGED')
1511 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001512
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001513 self.assertEqual(A.reported, 2)
1514 self.assertEqual(B.reported, 2)
1515 self.assertEqual(C.reported, 2)
1516 self.assertEqual(D.reported, 2)
1517 self.assertEqual(E.reported, 2)
1518 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001519
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001520 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1521 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001522
1523 def test_merger_repack(self):
1524 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001525
James E. Blair05fed602012-09-07 12:45:24 -07001526 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001527 A.addApproval('code-review', 2)
1528 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001529 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001530 self.assertEqual(self.getJobFromHistory('project-merge').result,
1531 'SUCCESS')
1532 self.assertEqual(self.getJobFromHistory('project-test1').result,
1533 'SUCCESS')
1534 self.assertEqual(self.getJobFromHistory('project-test2').result,
1535 'SUCCESS')
1536 self.assertEqual(A.data['status'], 'MERGED')
1537 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001538 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001539 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001540
James E. Blair2a535672017-04-27 12:03:15 -07001541 path = os.path.join(self.merger_src_root, "review.example.com",
1542 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001543 if os.path.exists(path):
1544 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001545 path = os.path.join(self.executor_src_root, "review.example.com",
1546 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001547 if os.path.exists(path):
1548 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001549
1550 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001551 A.addApproval('code-review', 2)
1552 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001553 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001554 self.assertEqual(self.getJobFromHistory('project-merge').result,
1555 'SUCCESS')
1556 self.assertEqual(self.getJobFromHistory('project-test1').result,
1557 'SUCCESS')
1558 self.assertEqual(self.getJobFromHistory('project-test2').result,
1559 'SUCCESS')
1560 self.assertEqual(A.data['status'], 'MERGED')
1561 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001562
James E. Blair4886f282012-11-15 09:27:33 -08001563 def test_merger_repack_large_change(self):
1564 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001565 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001566 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001567 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001568 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001569 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001570 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001571 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001573 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001574 # TODOv3(jeblair): add hostname to upstream root
1575 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001576 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001577 path = os.path.join(self.merger_src_root, 'review.example.com',
1578 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001579 if os.path.exists(path):
1580 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001581 path = os.path.join(self.executor_src_root, 'review.example.com',
1582 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001583 if os.path.exists(path):
1584 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001585
James E. Blair8b5408c2016-08-08 15:37:46 -07001586 A.addApproval('code-review', 2)
1587 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001588 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001589 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001590 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001591 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001592 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001593 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001594 'SUCCESS')
1595 self.assertEqual(A.data['status'], 'MERGED')
1596 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001597
James E. Blair2fa50962013-01-30 21:50:41 -08001598 def test_new_patchset_dequeues_old(self):
1599 "Test that a new patchset causes the old to be dequeued"
1600 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001601 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001602 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1603 M.setMerged()
1604
1605 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1606 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1607 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1608 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001609 A.addApproval('code-review', 2)
1610 B.addApproval('code-review', 2)
1611 C.addApproval('code-review', 2)
1612 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001613
1614 C.setDependsOn(B, 1)
1615 B.setDependsOn(A, 1)
1616 A.setDependsOn(M, 1)
1617
James E. Blair8b5408c2016-08-08 15:37:46 -07001618 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1619 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1620 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1621 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001622 self.waitUntilSettled()
1623
1624 B.addPatchset()
1625 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1626 self.waitUntilSettled()
1627
Paul Belanger174a8272017-03-14 13:20:10 -04001628 self.executor_server.hold_jobs_in_build = False
1629 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001630 self.waitUntilSettled()
1631
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001632 self.assertEqual(A.data['status'], 'MERGED')
1633 self.assertEqual(A.reported, 2)
1634 self.assertEqual(B.data['status'], 'NEW')
1635 self.assertEqual(B.reported, 2)
1636 self.assertEqual(C.data['status'], 'NEW')
1637 self.assertEqual(C.reported, 2)
1638 self.assertEqual(D.data['status'], 'MERGED')
1639 self.assertEqual(D.reported, 2)
1640 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001641
James E. Blairba437362015-02-07 11:41:52 -08001642 def test_new_patchset_check(self):
1643 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001644
Paul Belanger174a8272017-03-14 13:20:10 -04001645 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001646
1647 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001648 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001649 tenant = self.sched.abide.tenants.get('tenant-one')
1650 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001651
1652 # Add two git-dependent changes
1653 B.setDependsOn(A, 1)
1654 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1655 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001656 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1657 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001658
1659 # A live item, and a non-live/live pair
1660 items = check_pipeline.getAllItems()
1661 self.assertEqual(len(items), 3)
1662
1663 self.assertEqual(items[0].change.number, '1')
1664 self.assertEqual(items[0].change.patchset, '1')
1665 self.assertFalse(items[0].live)
1666
1667 self.assertEqual(items[1].change.number, '2')
1668 self.assertEqual(items[1].change.patchset, '1')
1669 self.assertTrue(items[1].live)
1670
1671 self.assertEqual(items[2].change.number, '1')
1672 self.assertEqual(items[2].change.patchset, '1')
1673 self.assertTrue(items[2].live)
1674
1675 # Add a new patchset to A
1676 A.addPatchset()
1677 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1678 self.waitUntilSettled()
1679
1680 # The live copy of A,1 should be gone, but the non-live and B
1681 # should continue, and we should have a new A,2
1682 items = check_pipeline.getAllItems()
1683 self.assertEqual(len(items), 3)
1684
1685 self.assertEqual(items[0].change.number, '1')
1686 self.assertEqual(items[0].change.patchset, '1')
1687 self.assertFalse(items[0].live)
1688
1689 self.assertEqual(items[1].change.number, '2')
1690 self.assertEqual(items[1].change.patchset, '1')
1691 self.assertTrue(items[1].live)
1692
1693 self.assertEqual(items[2].change.number, '1')
1694 self.assertEqual(items[2].change.patchset, '2')
1695 self.assertTrue(items[2].live)
1696
1697 # Add a new patchset to B
1698 B.addPatchset()
1699 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1700 self.waitUntilSettled()
1701
1702 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1703 # but we should have a new B,2 (still based on A,1)
1704 items = check_pipeline.getAllItems()
1705 self.assertEqual(len(items), 3)
1706
1707 self.assertEqual(items[0].change.number, '1')
1708 self.assertEqual(items[0].change.patchset, '2')
1709 self.assertTrue(items[0].live)
1710
1711 self.assertEqual(items[1].change.number, '1')
1712 self.assertEqual(items[1].change.patchset, '1')
1713 self.assertFalse(items[1].live)
1714
1715 self.assertEqual(items[2].change.number, '2')
1716 self.assertEqual(items[2].change.patchset, '2')
1717 self.assertTrue(items[2].live)
1718
1719 self.builds[0].release()
1720 self.waitUntilSettled()
1721 self.builds[0].release()
1722 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001723 self.executor_server.hold_jobs_in_build = False
1724 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001725 self.waitUntilSettled()
1726
1727 self.assertEqual(A.reported, 1)
1728 self.assertEqual(B.reported, 1)
1729 self.assertEqual(self.history[0].result, 'ABORTED')
1730 self.assertEqual(self.history[0].changes, '1,1')
1731 self.assertEqual(self.history[1].result, 'ABORTED')
1732 self.assertEqual(self.history[1].changes, '1,1 2,1')
1733 self.assertEqual(self.history[2].result, 'SUCCESS')
1734 self.assertEqual(self.history[2].changes, '1,2')
1735 self.assertEqual(self.history[3].result, 'SUCCESS')
1736 self.assertEqual(self.history[3].changes, '1,1 2,2')
1737
1738 def test_abandoned_gate(self):
1739 "Test that an abandoned change is dequeued from gate"
1740
Paul Belanger174a8272017-03-14 13:20:10 -04001741 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001742
1743 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001744 A.addApproval('code-review', 2)
1745 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001746 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001747 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1748 self.assertEqual(self.builds[0].name, 'project-merge')
1749
1750 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1751 self.waitUntilSettled()
1752
Paul Belanger174a8272017-03-14 13:20:10 -04001753 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001754 self.waitUntilSettled()
1755
Arie5b4048c2016-10-05 16:12:06 +03001756 self.assertBuilds([])
1757 self.assertHistory([
1758 dict(name='project-merge', result='ABORTED', changes='1,1')],
1759 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001760 self.assertEqual(A.reported, 1,
1761 "Abandoned gate change should report only start")
1762
1763 def test_abandoned_check(self):
1764 "Test that an abandoned change is dequeued from check"
1765
Paul Belanger174a8272017-03-14 13:20:10 -04001766 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001767
1768 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1769 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001770 tenant = self.sched.abide.tenants.get('tenant-one')
1771 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001772
1773 # Add two git-dependent changes
1774 B.setDependsOn(A, 1)
1775 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1776 self.waitUntilSettled()
1777 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1778 self.waitUntilSettled()
1779 # A live item, and a non-live/live pair
1780 items = check_pipeline.getAllItems()
1781 self.assertEqual(len(items), 3)
1782
1783 self.assertEqual(items[0].change.number, '1')
1784 self.assertFalse(items[0].live)
1785
1786 self.assertEqual(items[1].change.number, '2')
1787 self.assertTrue(items[1].live)
1788
1789 self.assertEqual(items[2].change.number, '1')
1790 self.assertTrue(items[2].live)
1791
1792 # Abandon A
1793 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1794 self.waitUntilSettled()
1795
1796 # The live copy of A should be gone, but the non-live and B
1797 # should continue
1798 items = check_pipeline.getAllItems()
1799 self.assertEqual(len(items), 2)
1800
1801 self.assertEqual(items[0].change.number, '1')
1802 self.assertFalse(items[0].live)
1803
1804 self.assertEqual(items[1].change.number, '2')
1805 self.assertTrue(items[1].live)
1806
Paul Belanger174a8272017-03-14 13:20:10 -04001807 self.executor_server.hold_jobs_in_build = False
1808 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001809 self.waitUntilSettled()
1810
1811 self.assertEqual(len(self.history), 4)
1812 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001813 'Build should have been aborted')
1814 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001815 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001816
Steve Varnau7b78b312015-04-03 14:49:46 -07001817 def test_abandoned_not_timer(self):
1818 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001819 # This test can not use simple_layout because it must start
1820 # with a configuration which does not include a
1821 # timer-triggered job so that we have an opportunity to set
1822 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001823 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001824 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001825 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001826 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001827 # The pipeline triggers every second, so we should have seen
1828 # several by now.
1829 time.sleep(5)
1830 self.waitUntilSettled()
1831 # Stop queuing timer triggered jobs so that the assertions
1832 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001833 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001834 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001835 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001836
Steve Varnau7b78b312015-04-03 14:49:46 -07001837 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1838
1839 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1840 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1841 self.waitUntilSettled()
1842 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1843
1844 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1845 self.waitUntilSettled()
1846
1847 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1848
Paul Belanger174a8272017-03-14 13:20:10 -04001849 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001850 self.waitUntilSettled()
1851
Arx Cruzb1b010d2013-10-28 19:49:59 -02001852 def test_zuul_url_return(self):
1853 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001854 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001855 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001856
1857 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001858 A.addApproval('code-review', 2)
1859 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001860 self.waitUntilSettled()
1861
1862 self.assertEqual(len(self.builds), 1)
1863 for build in self.builds:
1864 self.assertTrue('ZUUL_URL' in build.parameters)
1865
Paul Belanger174a8272017-03-14 13:20:10 -04001866 self.executor_server.hold_jobs_in_build = False
1867 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001868 self.waitUntilSettled()
1869
James E. Blair2fa50962013-01-30 21:50:41 -08001870 def test_new_patchset_dequeues_old_on_head(self):
1871 "Test that a new patchset causes the old to be dequeued (at head)"
1872 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001873 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001874 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1875 M.setMerged()
1876 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1877 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1878 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1879 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001880 A.addApproval('code-review', 2)
1881 B.addApproval('code-review', 2)
1882 C.addApproval('code-review', 2)
1883 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001884
1885 C.setDependsOn(B, 1)
1886 B.setDependsOn(A, 1)
1887 A.setDependsOn(M, 1)
1888
James E. Blair8b5408c2016-08-08 15:37:46 -07001889 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1890 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1891 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1892 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001893 self.waitUntilSettled()
1894
1895 A.addPatchset()
1896 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1897 self.waitUntilSettled()
1898
Paul Belanger174a8272017-03-14 13:20:10 -04001899 self.executor_server.hold_jobs_in_build = False
1900 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001901 self.waitUntilSettled()
1902
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001903 self.assertEqual(A.data['status'], 'NEW')
1904 self.assertEqual(A.reported, 2)
1905 self.assertEqual(B.data['status'], 'NEW')
1906 self.assertEqual(B.reported, 2)
1907 self.assertEqual(C.data['status'], 'NEW')
1908 self.assertEqual(C.reported, 2)
1909 self.assertEqual(D.data['status'], 'MERGED')
1910 self.assertEqual(D.reported, 2)
1911 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001912
1913 def test_new_patchset_dequeues_old_without_dependents(self):
1914 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001915 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001916 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1917 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1918 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001919 A.addApproval('code-review', 2)
1920 B.addApproval('code-review', 2)
1921 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001922
James E. Blair8b5408c2016-08-08 15:37:46 -07001923 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1924 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1925 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001926 self.waitUntilSettled()
1927
1928 B.addPatchset()
1929 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1930 self.waitUntilSettled()
1931
Paul Belanger174a8272017-03-14 13:20:10 -04001932 self.executor_server.hold_jobs_in_build = False
1933 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001934 self.waitUntilSettled()
1935
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001936 self.assertEqual(A.data['status'], 'MERGED')
1937 self.assertEqual(A.reported, 2)
1938 self.assertEqual(B.data['status'], 'NEW')
1939 self.assertEqual(B.reported, 2)
1940 self.assertEqual(C.data['status'], 'MERGED')
1941 self.assertEqual(C.reported, 2)
1942 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001943
1944 def test_new_patchset_dequeues_old_independent_queue(self):
1945 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001946 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001947 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1948 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1949 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1950 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1951 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1952 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1953 self.waitUntilSettled()
1954
1955 B.addPatchset()
1956 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1957 self.waitUntilSettled()
1958
Paul Belanger174a8272017-03-14 13:20:10 -04001959 self.executor_server.hold_jobs_in_build = False
1960 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001961 self.waitUntilSettled()
1962
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001963 self.assertEqual(A.data['status'], 'NEW')
1964 self.assertEqual(A.reported, 1)
1965 self.assertEqual(B.data['status'], 'NEW')
1966 self.assertEqual(B.reported, 1)
1967 self.assertEqual(C.data['status'], 'NEW')
1968 self.assertEqual(C.reported, 1)
1969 self.assertEqual(len(self.history), 10)
1970 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001971
James E. Blaircc020532017-04-19 13:04:44 -07001972 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001973 def test_noop_job(self):
1974 "Test that the internal noop job works"
1975 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001976 A.addApproval('code-review', 2)
1977 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001978 self.waitUntilSettled()
1979
1980 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1981 self.assertTrue(self.sched._areAllBuildsComplete())
1982 self.assertEqual(len(self.history), 0)
1983 self.assertEqual(A.data['status'], 'MERGED')
1984 self.assertEqual(A.reported, 2)
1985
James E. Blair06cc3922017-04-19 10:08:10 -07001986 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001987 def test_no_job_project(self):
1988 "Test that reports with no jobs don't get sent"
1989 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1990 'master', 'A')
1991 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1992 self.waitUntilSettled()
1993
1994 # Change wasn't reported to
1995 self.assertEqual(A.reported, False)
1996
1997 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001998 tenant = self.sched.abide.tenants.get('tenant-one')
1999 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002000 items = check_pipeline.getAllItems()
2001 self.assertEqual(len(items), 0)
2002
2003 self.assertEqual(len(self.history), 0)
2004
James E. Blair7d0dedc2013-02-21 17:26:09 -08002005 def test_zuul_refs(self):
2006 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002007 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002008 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2009 M1.setMerged()
2010 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2011 M2.setMerged()
2012
2013 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2014 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2015 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2016 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07002017 A.addApproval('code-review', 2)
2018 B.addApproval('code-review', 2)
2019 C.addApproval('code-review', 2)
2020 D.addApproval('code-review', 2)
2021 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2022 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
2023 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
2024 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
2026 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002027 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002028 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002029 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002030 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002031 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002032 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002033 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002034 self.waitUntilSettled()
2035
James E. Blair7d0dedc2013-02-21 17:26:09 -08002036 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08002037 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002038 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08002039 if x.parameters['ZUUL_CHANGE'] == '3':
2040 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002041 a_build = x
2042 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002043 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002044 b_build = x
2045 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002046 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002047 c_build = x
2048 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08002049 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08002050 d_build = x
2051 if a_build and b_build and c_build and d_build:
2052 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002053
2054 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002055 self.assertIsNotNone(a_zref)
2056 self.assertIsNotNone(b_zref)
2057 self.assertIsNotNone(c_zref)
2058 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002059
2060 # And they should all be different
2061 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002062 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002063
Clint Byrum3343e3e2016-11-15 16:05:03 -08002064 # should have a, not b, and should not be in project2
2065 self.assertTrue(a_build.hasChanges(A))
2066 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002067
Clint Byrum3343e3e2016-11-15 16:05:03 -08002068 # should have a and b, and should not be in project2
2069 self.assertTrue(b_build.hasChanges(A, B))
2070 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002071
Clint Byrum3343e3e2016-11-15 16:05:03 -08002072 # should have a and b in 1, c in 2
2073 self.assertTrue(c_build.hasChanges(A, B, C))
2074 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002075
Clint Byrum3343e3e2016-11-15 16:05:03 -08002076 # should have a and b in 1, c and d in 2
2077 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002078
Paul Belanger174a8272017-03-14 13:20:10 -04002079 self.executor_server.hold_jobs_in_build = False
2080 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002081 self.waitUntilSettled()
2082
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002083 self.assertEqual(A.data['status'], 'MERGED')
2084 self.assertEqual(A.reported, 2)
2085 self.assertEqual(B.data['status'], 'MERGED')
2086 self.assertEqual(B.reported, 2)
2087 self.assertEqual(C.data['status'], 'MERGED')
2088 self.assertEqual(C.reported, 2)
2089 self.assertEqual(D.data['status'], 'MERGED')
2090 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002091
James E. Blair4a28a882013-08-23 15:17:33 -07002092 def test_rerun_on_error(self):
2093 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002094 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002095 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. Blair4a28a882013-08-23 15:17:33 -07002098 self.waitUntilSettled()
2099
James E. Blair412fba82017-01-26 15:00:50 -08002100 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002101 self.executor_server.hold_jobs_in_build = False
2102 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002103 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002104 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002105 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2106
James E. Blair412e5582013-04-22 15:50:12 -07002107 def test_statsd(self):
2108 "Test each of the statsd methods used in the scheduler"
2109 import extras
2110 statsd = extras.try_import('statsd.statsd')
2111 statsd.incr('test-incr')
2112 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002113 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002114 self.assertReportedStat('test-incr', '1|c')
2115 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002116 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002117
James E. Blairdad52252014-02-07 16:59:17 -08002118 def test_stuck_job_cleanup(self):
2119 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002120
2121 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002122 self.gearman_server.hold_jobs_in_queue = True
2123 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002124 A.addApproval('code-review', 2)
2125 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002126 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002127 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002128 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2129
James E. Blair31cb3e82017-04-20 13:08:56 -07002130 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002131 self.sched.reconfigure(self.config)
2132 self.waitUntilSettled()
2133
James E. Blair18c64442014-03-18 10:14:45 -07002134 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002135 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002136 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002137 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2138 self.assertTrue(self.sched._areAllBuildsComplete())
2139
2140 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002141 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002142 self.assertEqual(self.history[0].result, 'SUCCESS')
2143
James E. Blair879dafb2015-07-17 14:04:49 -07002144 def test_file_head(self):
2145 # This is a regression test for an observed bug. A change
2146 # with a file named "HEAD" in the root directory of the repo
2147 # was processed by a merger. It then was unable to reset the
2148 # repo because of:
2149 # GitCommandError: 'git reset --hard HEAD' returned
2150 # with exit code 128
2151 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2152 # and filename
2153 # Use '--' to separate filenames from revisions'
2154
2155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002156 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002157 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2158
2159 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2160 self.waitUntilSettled()
2161
2162 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2163 self.waitUntilSettled()
2164
2165 self.assertIn('Build succeeded', A.messages[0])
2166 self.assertIn('Build succeeded', B.messages[0])
2167
James E. Blair70c71582013-03-06 08:50:50 -08002168 def test_file_jobs(self):
2169 "Test that file jobs run only when appropriate"
2170 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002171 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002172 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002173 A.addApproval('code-review', 2)
2174 B.addApproval('code-review', 2)
2175 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2176 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002177 self.waitUntilSettled()
2178
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002179 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002180 if x.name == 'project-testfile']
2181
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002182 self.assertEqual(len(testfile_jobs), 1)
2183 self.assertEqual(testfile_jobs[0].changes, '1,2')
2184 self.assertEqual(A.data['status'], 'MERGED')
2185 self.assertEqual(A.reported, 2)
2186 self.assertEqual(B.data['status'], 'MERGED')
2187 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002188
Clint Byrum3ee39f32016-11-17 23:45:07 -08002189 def _test_irrelevant_files_jobs(self, should_skip):
2190 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002191 if should_skip:
2192 files = {'ignoreme': 'ignored\n'}
2193 else:
2194 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002195
2196 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002197 'master',
2198 'test irrelevant-files',
2199 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002200 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2201 self.waitUntilSettled()
2202
2203 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002204 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002205
2206 if should_skip:
2207 self.assertEqual([], tested_change_ids)
2208 else:
2209 self.assertIn(change.data['number'], tested_change_ids)
2210
James E. Blaircc324192017-04-19 14:36:08 -07002211 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002212 def test_irrelevant_files_match_skips_job(self):
2213 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002214
James E. Blaircc324192017-04-19 14:36:08 -07002215 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002216 def test_irrelevant_files_no_match_runs_job(self):
2217 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002218
James E. Blaircc324192017-04-19 14:36:08 -07002219 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002220 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002221 files = {'ignoreme': 'ignored\n'}
2222
2223 change = self.fake_gerrit.addFakeChange('org/project',
2224 'master',
2225 'test irrelevant-files',
2226 files=files)
2227 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2228 self.waitUntilSettled()
2229
2230 run_jobs = set([build.name for build in self.history])
2231
2232 self.assertEqual(set(['project-test-nomatch-starts-empty',
2233 'project-test-nomatch-starts-full']), run_jobs)
2234
James E. Blairc8a1e052014-02-25 09:29:26 -08002235 def test_queue_names(self):
2236 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002237 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002238 (trusted, project1) = tenant.getProject('org/project1')
2239 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002240 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2241 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2242 self.assertEqual(q1.name, 'integrated')
2243 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002244
James E. Blair64ed6f22013-07-10 14:07:23 -07002245 def test_queue_precedence(self):
2246 "Test that queue precedence works"
2247
2248 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002249 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2251 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002252 A.addApproval('code-review', 2)
2253 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002254
2255 self.waitUntilSettled()
2256 self.gearman_server.hold_jobs_in_queue = False
2257 self.gearman_server.release()
2258 self.waitUntilSettled()
2259
James E. Blair8de58bd2013-07-18 16:23:33 -07002260 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002261 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002262 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002263 self.waitUntilSettled()
2264
James E. Blair64ed6f22013-07-10 14:07:23 -07002265 self.log.debug(self.history)
2266 self.assertEqual(self.history[0].pipeline, 'gate')
2267 self.assertEqual(self.history[1].pipeline, 'check')
2268 self.assertEqual(self.history[2].pipeline, 'gate')
2269 self.assertEqual(self.history[3].pipeline, 'gate')
2270 self.assertEqual(self.history[4].pipeline, 'check')
2271 self.assertEqual(self.history[5].pipeline, 'check')
2272
Clark Boylana5edbe42014-06-03 16:39:10 -07002273 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002274 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002275 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002277 A.addApproval('code-review', 2)
2278 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002279 self.waitUntilSettled()
2280
Paul Belanger174a8272017-03-14 13:20:10 -04002281 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002282 self.waitUntilSettled()
2283
James E. Blair1843a552013-07-03 14:19:52 -07002284 port = self.webapp.server.socket.getsockname()[1]
2285
Adam Gandelman77a12c72017-02-03 17:43:43 -08002286 req = urllib.request.Request(
2287 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002288 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002289 headers = f.info()
2290 self.assertIn('Content-Length', headers)
2291 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002292 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2293 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002294 self.assertIn('Access-Control-Allow-Origin', headers)
2295 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002296 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002297 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002298 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002299
Paul Belanger174a8272017-03-14 13:20:10 -04002300 self.executor_server.hold_jobs_in_build = False
2301 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002302 self.waitUntilSettled()
2303
2304 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002305 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002306 for p in data['pipelines']:
2307 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002308 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002309 self.assertEqual(q['window'], 20)
2310 else:
2311 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002312 for head in q['heads']:
2313 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002314 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002315 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002316 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002317 status_jobs.append(job)
2318 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002319 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002320 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002321 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2322 uuid=status_jobs[0]['uuid']),
2323 status_jobs[0]['url'])
2324 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002325 'finger://{hostname}/{uuid}'.format(
2326 hostname=self.executor_server.hostname,
2327 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002328 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002329 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002330 self.assertEqual(
2331 'finger://{hostname}/{uuid}'.format(
2332 hostname=self.executor_server.hostname,
2333 uuid=status_jobs[0]['uuid']),
2334 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002335 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002336 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002337 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2338 uuid=status_jobs[1]['uuid']),
2339 status_jobs[1]['url'])
2340 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002341 'finger://{hostname}/{uuid}'.format(
2342 hostname=self.executor_server.hostname,
2343 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002344 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002345 self.assertEqual(
2346 'finger://{hostname}/{uuid}'.format(
2347 hostname=self.executor_server.hostname,
2348 uuid=status_jobs[1]['uuid']),
2349 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002350
2351 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002352 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002353 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2354 uuid=status_jobs[2]['uuid']),
2355 status_jobs[2]['url'])
2356 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002357 'finger://{hostname}/{uuid}'.format(
2358 hostname=self.executor_server.hostname,
2359 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002360 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002361 self.assertEqual(
2362 'finger://{hostname}/{uuid}'.format(
2363 hostname=self.executor_server.hostname,
2364 uuid=status_jobs[2]['uuid']),
2365 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002366
Tobias Henkel65639f82017-07-10 10:25:42 +02002367 # check job dependencies
2368 self.assertIsNotNone(status_jobs[0]['dependencies'])
2369 self.assertIsNotNone(status_jobs[1]['dependencies'])
2370 self.assertIsNotNone(status_jobs[2]['dependencies'])
2371 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2372 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2373 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2374 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2375 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2376
James E. Blaircdccd972013-07-01 12:10:22 -07002377 def test_live_reconfiguration(self):
2378 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002379 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002380 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002381 A.addApproval('code-review', 2)
2382 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002383 self.waitUntilSettled()
2384
2385 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002386 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002387
Paul Belanger174a8272017-03-14 13:20:10 -04002388 self.executor_server.hold_jobs_in_build = False
2389 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002390 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002391 self.assertEqual(self.getJobFromHistory('project-merge').result,
2392 'SUCCESS')
2393 self.assertEqual(self.getJobFromHistory('project-test1').result,
2394 'SUCCESS')
2395 self.assertEqual(self.getJobFromHistory('project-test2').result,
2396 'SUCCESS')
2397 self.assertEqual(A.data['status'], 'MERGED')
2398 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002399
James E. Blair6bc782d2015-07-17 16:20:21 -07002400 def test_live_reconfiguration_merge_conflict(self):
2401 # A real-world bug: a change in a gate queue has a merge
2402 # conflict and a job is added to its project while it's
2403 # sitting in the queue. The job gets added to the change and
2404 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002405 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002406
2407 # This change is fine. It's here to stop the queue long
2408 # enough for the next change to be subject to the
2409 # reconfiguration, as well as to provide a conflict for the
2410 # next change. This change will succeed and merge.
2411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002412 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002413 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002414
2415 # This change will be in merge conflict. During the
2416 # reconfiguration, we will add a job. We want to make sure
2417 # that doesn't cause it to get stuck.
2418 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002419 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002420 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002421
James E. Blair8b5408c2016-08-08 15:37:46 -07002422 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2423 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002424
2425 self.waitUntilSettled()
2426
2427 # No jobs have run yet
2428 self.assertEqual(A.data['status'], 'NEW')
2429 self.assertEqual(A.reported, 1)
2430 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002431 self.assertEqual(len(self.history), 0)
2432
2433 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002434 self.commitConfigUpdate('common-config',
2435 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002436 self.sched.reconfigure(self.config)
2437 self.waitUntilSettled()
2438
Paul Belanger174a8272017-03-14 13:20:10 -04002439 self.executor_server.hold_jobs_in_build = False
2440 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002441 self.waitUntilSettled()
2442
2443 self.assertEqual(A.data['status'], 'MERGED')
2444 self.assertEqual(A.reported, 2)
2445 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002446 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002447 self.assertEqual(self.getJobFromHistory('project-merge').result,
2448 'SUCCESS')
2449 self.assertEqual(self.getJobFromHistory('project-test1').result,
2450 'SUCCESS')
2451 self.assertEqual(self.getJobFromHistory('project-test2').result,
2452 'SUCCESS')
2453 self.assertEqual(self.getJobFromHistory('project-test3').result,
2454 'SUCCESS')
2455 self.assertEqual(len(self.history), 4)
2456
James E. Blair400e8fd2015-07-30 17:44:45 -07002457 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002458 # An extrapolation of test_live_reconfiguration_merge_conflict
2459 # that tests a job added to a job tree with a failed root does
2460 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002461 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002462
2463 # This change is fine. It's here to stop the queue long
2464 # enough for the next change to be subject to the
2465 # reconfiguration. This change will succeed and merge.
2466 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002467 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002468 A.addApproval('code-review', 2)
2469 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002470 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002471 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002472 self.waitUntilSettled()
2473
2474 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002475 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002476 B.addApproval('code-review', 2)
2477 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002478 self.waitUntilSettled()
2479
Paul Belanger174a8272017-03-14 13:20:10 -04002480 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002481 self.waitUntilSettled()
2482
2483 # Both -merge jobs have run, but no others.
2484 self.assertEqual(A.data['status'], 'NEW')
2485 self.assertEqual(A.reported, 1)
2486 self.assertEqual(B.data['status'], 'NEW')
2487 self.assertEqual(B.reported, 1)
2488 self.assertEqual(self.history[0].result, 'SUCCESS')
2489 self.assertEqual(self.history[0].name, 'project-merge')
2490 self.assertEqual(self.history[1].result, 'FAILURE')
2491 self.assertEqual(self.history[1].name, 'project-merge')
2492 self.assertEqual(len(self.history), 2)
2493
2494 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002495 self.commitConfigUpdate('common-config',
2496 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002497 self.sched.reconfigure(self.config)
2498 self.waitUntilSettled()
2499
Paul Belanger174a8272017-03-14 13:20:10 -04002500 self.executor_server.hold_jobs_in_build = False
2501 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002502 self.waitUntilSettled()
2503
2504 self.assertEqual(A.data['status'], 'MERGED')
2505 self.assertEqual(A.reported, 2)
2506 self.assertEqual(B.data['status'], 'NEW')
2507 self.assertEqual(B.reported, 2)
2508 self.assertEqual(self.history[0].result, 'SUCCESS')
2509 self.assertEqual(self.history[0].name, 'project-merge')
2510 self.assertEqual(self.history[1].result, 'FAILURE')
2511 self.assertEqual(self.history[1].name, 'project-merge')
2512 self.assertEqual(self.history[2].result, 'SUCCESS')
2513 self.assertEqual(self.history[3].result, 'SUCCESS')
2514 self.assertEqual(self.history[4].result, 'SUCCESS')
2515 self.assertEqual(len(self.history), 5)
2516
James E. Blair400e8fd2015-07-30 17:44:45 -07002517 def test_live_reconfiguration_failed_job(self):
2518 # Test that a change with a removed failing job does not
2519 # disrupt reconfiguration. If a change has a failed job and
2520 # that job is removed during a reconfiguration, we observed a
2521 # bug where the code to re-set build statuses would run on
2522 # that build and raise an exception because the job no longer
2523 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002524 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002525
2526 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2527
2528 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002529 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002530
2531 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2532 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002533 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002534 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002535 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002536 self.waitUntilSettled()
2537
2538 self.assertEqual(A.data['status'], 'NEW')
2539 self.assertEqual(A.reported, 0)
2540
2541 self.assertEqual(self.getJobFromHistory('project-merge').result,
2542 'SUCCESS')
2543 self.assertEqual(self.getJobFromHistory('project-test1').result,
2544 'FAILURE')
2545 self.assertEqual(len(self.history), 2)
2546
2547 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002548 self.commitConfigUpdate('common-config',
2549 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -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. Blair400e8fd2015-07-30 17:44:45 -07002555 self.waitUntilSettled()
2556
2557 self.assertEqual(self.getJobFromHistory('project-test2').result,
2558 'SUCCESS')
2559 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2560 'SUCCESS')
2561 self.assertEqual(len(self.history), 4)
2562
2563 self.assertEqual(A.data['status'], 'NEW')
2564 self.assertEqual(A.reported, 1)
2565 self.assertIn('Build succeeded', A.messages[0])
2566 # Ensure the removed job was not included in the report.
2567 self.assertNotIn('project-test1', A.messages[0])
2568
James E. Blairfe707d12015-08-05 15:18:15 -07002569 def test_live_reconfiguration_shared_queue(self):
2570 # Test that a change with a failing job which was removed from
2571 # this project but otherwise still exists in the system does
2572 # not disrupt reconfiguration.
2573
Paul Belanger174a8272017-03-14 13:20:10 -04002574 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002575
2576 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2577
Paul Belanger174a8272017-03-14 13:20:10 -04002578 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002579
2580 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2581 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002582 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002583 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002584 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002585 self.waitUntilSettled()
2586
2587 self.assertEqual(A.data['status'], 'NEW')
2588 self.assertEqual(A.reported, 0)
2589
Clint Byrum69663d42017-05-01 14:51:04 -07002590 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002591 'SUCCESS')
2592 self.assertEqual(self.getJobFromHistory(
2593 'project1-project2-integration').result, 'FAILURE')
2594 self.assertEqual(len(self.history), 2)
2595
2596 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002597 self.commitConfigUpdate(
2598 'common-config',
2599 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002600 self.sched.reconfigure(self.config)
2601 self.waitUntilSettled()
2602
Paul Belanger174a8272017-03-14 13:20:10 -04002603 self.executor_server.hold_jobs_in_build = False
2604 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002605 self.waitUntilSettled()
2606
Clint Byrum69663d42017-05-01 14:51:04 -07002607 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002608 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002609 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002610 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002611 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002612 'SUCCESS')
2613 self.assertEqual(self.getJobFromHistory(
2614 'project1-project2-integration').result, 'FAILURE')
2615 self.assertEqual(len(self.history), 4)
2616
2617 self.assertEqual(A.data['status'], 'NEW')
2618 self.assertEqual(A.reported, 1)
2619 self.assertIn('Build succeeded', A.messages[0])
2620 # Ensure the removed job was not included in the report.
2621 self.assertNotIn('project1-project2-integration', A.messages[0])
2622
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002623 def test_double_live_reconfiguration_shared_queue(self):
2624 # This was a real-world regression. A change is added to
2625 # gate; a reconfigure happens, a second change which depends
2626 # on the first is added, and a second reconfiguration happens.
2627 # Ensure that both changes merge.
2628
2629 # A failure may indicate incorrect caching or cleaning up of
2630 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002631 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002632
2633 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2634 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2635 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002636 A.addApproval('code-review', 2)
2637 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002638
2639 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002640 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002641 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002642 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002643 self.waitUntilSettled()
2644
2645 # Reconfigure (with only one change in the pipeline).
2646 self.sched.reconfigure(self.config)
2647 self.waitUntilSettled()
2648
2649 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002650 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002651 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002652 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002653 self.waitUntilSettled()
2654
2655 # Reconfigure (with both in the pipeline).
2656 self.sched.reconfigure(self.config)
2657 self.waitUntilSettled()
2658
Paul Belanger174a8272017-03-14 13:20:10 -04002659 self.executor_server.hold_jobs_in_build = False
2660 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002661 self.waitUntilSettled()
2662
2663 self.assertEqual(len(self.history), 8)
2664
2665 self.assertEqual(A.data['status'], 'MERGED')
2666 self.assertEqual(A.reported, 2)
2667 self.assertEqual(B.data['status'], 'MERGED')
2668 self.assertEqual(B.reported, 2)
2669
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002670 def test_live_reconfiguration_del_project(self):
2671 # Test project deletion from layout
2672 # while changes are enqueued
2673
Paul Belanger174a8272017-03-14 13:20:10 -04002674 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2676 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2677 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2678
2679 # A Depends-On: B
2680 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2681 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002682 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002683
2684 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2685 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2686 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002687 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002688 self.waitUntilSettled()
2689 self.assertEqual(len(self.builds), 5)
2690
2691 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002692 self.commitConfigUpdate(
2693 'common-config',
2694 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002695 self.sched.reconfigure(self.config)
2696 self.waitUntilSettled()
2697
2698 # Builds for C aborted, builds for A succeed,
2699 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002700 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002701 self.assertEqual(job_c.changes, '3,1')
2702 self.assertEqual(job_c.result, 'ABORTED')
2703
Paul Belanger174a8272017-03-14 13:20:10 -04002704 self.executor_server.hold_jobs_in_build = False
2705 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002706 self.waitUntilSettled()
2707
Clint Byrum0149ada2017-04-04 12:22:01 -07002708 self.assertEqual(
2709 self.getJobFromHistory('project-test1', 'org/project').changes,
2710 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002711
2712 self.assertEqual(A.data['status'], 'NEW')
2713 self.assertEqual(B.data['status'], 'NEW')
2714 self.assertEqual(C.data['status'], 'NEW')
2715 self.assertEqual(A.reported, 1)
2716 self.assertEqual(B.reported, 0)
2717 self.assertEqual(C.reported, 0)
2718
Clint Byrum0149ada2017-04-04 12:22:01 -07002719 tenant = self.sched.abide.tenants.get('tenant-one')
2720 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002721 self.assertIn('Build succeeded', A.messages[0])
2722
James E. Blairec056492016-07-22 09:45:56 -07002723 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002724 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002725 self.updateConfigLayout(
2726 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002727 self.sched.reconfigure(self.config)
2728
2729 self.init_repo("org/new-project")
2730 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2731
James E. Blair8b5408c2016-08-08 15:37:46 -07002732 A.addApproval('code-review', 2)
2733 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002734 self.waitUntilSettled()
2735 self.assertEqual(self.getJobFromHistory('project-merge').result,
2736 'SUCCESS')
2737 self.assertEqual(self.getJobFromHistory('project-test1').result,
2738 'SUCCESS')
2739 self.assertEqual(self.getJobFromHistory('project-test2').result,
2740 'SUCCESS')
2741 self.assertEqual(A.data['status'], 'MERGED')
2742 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002743
James E. Blair9ea70072017-04-19 16:05:30 -07002744 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002745 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002746 self.init_repo("org/delete-project")
2747 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2748
James E. Blair8b5408c2016-08-08 15:37:46 -07002749 A.addApproval('code-review', 2)
2750 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002751 self.waitUntilSettled()
2752 self.assertEqual(self.getJobFromHistory('project-merge').result,
2753 'SUCCESS')
2754 self.assertEqual(self.getJobFromHistory('project-test1').result,
2755 'SUCCESS')
2756 self.assertEqual(self.getJobFromHistory('project-test2').result,
2757 'SUCCESS')
2758 self.assertEqual(A.data['status'], 'MERGED')
2759 self.assertEqual(A.reported, 2)
2760
2761 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002762 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002763 if os.path.exists(os.path.join(self.merger_src_root, p)):
2764 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002765 if os.path.exists(os.path.join(self.executor_src_root, p)):
2766 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002767
2768 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2769
James E. Blair8b5408c2016-08-08 15:37:46 -07002770 B.addApproval('code-review', 2)
2771 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002772 self.waitUntilSettled()
2773 self.assertEqual(self.getJobFromHistory('project-merge').result,
2774 'SUCCESS')
2775 self.assertEqual(self.getJobFromHistory('project-test1').result,
2776 'SUCCESS')
2777 self.assertEqual(self.getJobFromHistory('project-test2').result,
2778 'SUCCESS')
2779 self.assertEqual(B.data['status'], 'MERGED')
2780 self.assertEqual(B.reported, 2)
2781
James E. Blaire68d43f2017-04-19 14:03:30 -07002782 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002783 def test_tags(self):
2784 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002785 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2786 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2788 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2789 self.waitUntilSettled()
2790
James E. Blaire68d43f2017-04-19 14:03:30 -07002791 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002792
2793 results = {self.getJobFromHistory('merge',
2794 project='org/project1').uuid: 'extratag merge',
2795 self.getJobFromHistory('merge',
2796 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002797
2798 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002799 self.assertEqual(results.get(build.uuid, ''),
Monty Taylord13bc362017-06-30 13:11:37 -05002800 build.parameters['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002801
James E. Blair63bb0ef2013-07-29 17:14:51 -07002802 def test_timer(self):
2803 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002804 # This test can not use simple_layout because it must start
2805 # with a configuration which does not include a
2806 # timer-triggered job so that we have an opportunity to set
2807 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002808 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002809 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002810 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002811
Clark Boylan3ee090a2014-04-03 20:55:09 -07002812 # The pipeline triggers every second, so we should have seen
2813 # several by now.
2814 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002815 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002816
2817 self.assertEqual(len(self.builds), 2)
2818
James E. Blair63bb0ef2013-07-29 17:14:51 -07002819 port = self.webapp.server.socket.getsockname()[1]
2820
Clint Byrum5e729e92017-02-08 06:34:57 -08002821 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002822 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002823 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002824 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002825
Paul Belanger174a8272017-03-14 13:20:10 -04002826 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002827 # Stop queuing timer triggered jobs so that the assertions
2828 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002829 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002830 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002831 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002832 self.waitUntilSettled()
2833
2834 self.assertEqual(self.getJobFromHistory(
2835 'project-bitrot-stable-old').result, 'SUCCESS')
2836 self.assertEqual(self.getJobFromHistory(
2837 'project-bitrot-stable-older').result, 'SUCCESS')
2838
2839 data = json.loads(data)
2840 status_jobs = set()
2841 for p in data['pipelines']:
2842 for q in p['change_queues']:
2843 for head in q['heads']:
2844 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002845 for job in change['jobs']:
2846 status_jobs.add(job['name'])
2847 self.assertIn('project-bitrot-stable-old', status_jobs)
2848 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002849
James E. Blair4f6033c2014-03-27 15:49:09 -07002850 def test_idle(self):
2851 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002852 # This test can not use simple_layout because it must start
2853 # with a configuration which does not include a
2854 # timer-triggered job so that we have an opportunity to set
2855 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002856 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002857
Clark Boylan3ee090a2014-04-03 20:55:09 -07002858 for x in range(1, 3):
2859 # Test that timer triggers periodic jobs even across
2860 # layout config reloads.
2861 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002862 self.commitConfigUpdate('common-config',
2863 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002864 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002865 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002866
Clark Boylan3ee090a2014-04-03 20:55:09 -07002867 # The pipeline triggers every second, so we should have seen
2868 # several by now.
2869 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002870
2871 # Stop queuing timer triggered jobs so that the assertions
2872 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002873 self.commitConfigUpdate('common-config',
2874 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002875 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002876 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002877 self.assertEqual(len(self.builds), 2,
2878 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002879 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002880 self.waitUntilSettled()
2881 self.assertEqual(len(self.builds), 0)
2882 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002883
James E. Blaircc324192017-04-19 14:36:08 -07002884 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002885 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002886 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2887 self.waitUntilSettled()
2888
2889 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2890 self.waitUntilSettled()
2891
James E. Blairff80a2f2013-12-27 13:24:06 -08002892 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002893
2894 # A.messages only holds what FakeGerrit places in it. Thus we
2895 # work on the knowledge of what the first message should be as
2896 # it is only configured to go to SMTP.
2897
2898 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002899 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002900 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002901 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002902 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002903 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002904
2905 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002906 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002907 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002908 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002909 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002910 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002911
James E. Blaire5910202013-12-27 09:50:31 -08002912 def test_timer_smtp(self):
2913 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002914 # This test can not use simple_layout because it must start
2915 # with a configuration which does not include a
2916 # timer-triggered job so that we have an opportunity to set
2917 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002918 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002919 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002920 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002921
Clark Boylan3ee090a2014-04-03 20:55:09 -07002922 # The pipeline triggers every second, so we should have seen
2923 # several by now.
2924 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002925 self.waitUntilSettled()
2926
Clark Boylan3ee090a2014-04-03 20:55:09 -07002927 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002928 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002929 self.waitUntilSettled()
2930 self.assertEqual(len(self.history), 2)
2931
James E. Blaire5910202013-12-27 09:50:31 -08002932 self.assertEqual(self.getJobFromHistory(
2933 'project-bitrot-stable-old').result, 'SUCCESS')
2934 self.assertEqual(self.getJobFromHistory(
2935 'project-bitrot-stable-older').result, 'SUCCESS')
2936
James E. Blairff80a2f2013-12-27 13:24:06 -08002937 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002938
2939 # A.messages only holds what FakeGerrit places in it. Thus we
2940 # work on the knowledge of what the first message should be as
2941 # it is only configured to go to SMTP.
2942
2943 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002944 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002945 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002946 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002947 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002948 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002949
Clark Boylan3ee090a2014-04-03 20:55:09 -07002950 # Stop queuing timer triggered jobs and let any that may have
2951 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002952 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002953 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002954 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002955 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002956 self.waitUntilSettled()
2957
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002958 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002959 def test_timer_sshkey(self):
2960 "Test that a periodic job can setup SSH key authentication"
2961 self.worker.hold_jobs_in_build = True
2962 self.config.set('zuul', 'layout_config',
2963 'tests/fixtures/layout-timer.yaml')
2964 self.sched.reconfigure(self.config)
2965 self.registerJobs()
2966
2967 # The pipeline triggers every second, so we should have seen
2968 # several by now.
2969 time.sleep(5)
2970 self.waitUntilSettled()
2971
2972 self.assertEqual(len(self.builds), 2)
2973
2974 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2975 self.assertTrue(os.path.isfile(ssh_wrapper))
2976 with open(ssh_wrapper) as f:
2977 ssh_wrapper_content = f.read()
2978 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2979 # In the unit tests Merger runs in the same process,
2980 # so we see its' environment variables
2981 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2982
2983 self.worker.release('.*')
2984 self.waitUntilSettled()
2985 self.assertEqual(len(self.history), 2)
2986
2987 self.assertEqual(self.getJobFromHistory(
2988 'project-bitrot-stable-old').result, 'SUCCESS')
2989 self.assertEqual(self.getJobFromHistory(
2990 'project-bitrot-stable-older').result, 'SUCCESS')
2991
2992 # Stop queuing timer triggered jobs and let any that may have
2993 # queued through so that end of test assertions pass.
2994 self.config.set('zuul', 'layout_config',
2995 'tests/fixtures/layout-no-timer.yaml')
2996 self.sched.reconfigure(self.config)
2997 self.registerJobs()
2998 self.waitUntilSettled()
2999 self.worker.release('.*')
3000 self.waitUntilSettled()
3001
James E. Blair91e34592015-07-31 16:45:59 -07003002 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003003 "Test that the RPC client can enqueue a change"
3004 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003005 A.addApproval('code-review', 2)
3006 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003007
3008 client = zuul.rpcclient.RPCClient('127.0.0.1',
3009 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003010 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003011 r = client.enqueue(tenant='tenant-one',
3012 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003013 project='org/project',
3014 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003015 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003016 self.waitUntilSettled()
3017 self.assertEqual(self.getJobFromHistory('project-merge').result,
3018 'SUCCESS')
3019 self.assertEqual(self.getJobFromHistory('project-test1').result,
3020 'SUCCESS')
3021 self.assertEqual(self.getJobFromHistory('project-test2').result,
3022 'SUCCESS')
3023 self.assertEqual(A.data['status'], 'MERGED')
3024 self.assertEqual(A.reported, 2)
3025 self.assertEqual(r, True)
3026
James E. Blair91e34592015-07-31 16:45:59 -07003027 def test_client_enqueue_ref(self):
3028 "Test that the RPC client can enqueue a ref"
3029
3030 client = zuul.rpcclient.RPCClient('127.0.0.1',
3031 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003032 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003033 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003034 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003035 pipeline='post',
3036 project='org/project',
3037 trigger='gerrit',
3038 ref='master',
3039 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3040 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3041 self.waitUntilSettled()
3042 job_names = [x.name for x in self.history]
3043 self.assertEqual(len(self.history), 1)
3044 self.assertIn('project-post', job_names)
3045 self.assertEqual(r, True)
3046
James E. Blairad28e912013-11-27 10:43:22 -08003047 def test_client_enqueue_negative(self):
3048 "Test that the RPC client returns errors"
3049 client = zuul.rpcclient.RPCClient('127.0.0.1',
3050 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003051 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003052 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003053 "Invalid tenant"):
3054 r = client.enqueue(tenant='tenant-foo',
3055 pipeline='gate',
3056 project='org/project',
3057 trigger='gerrit',
3058 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003059 self.assertEqual(r, False)
3060
3061 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003062 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003063 r = client.enqueue(tenant='tenant-one',
3064 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003065 project='project-does-not-exist',
3066 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003067 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003068 self.assertEqual(r, False)
3069
3070 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3071 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003072 r = client.enqueue(tenant='tenant-one',
3073 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003074 project='org/project',
3075 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003076 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003077 self.assertEqual(r, False)
3078
3079 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3080 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003081 r = client.enqueue(tenant='tenant-one',
3082 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003083 project='org/project',
3084 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003085 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003086 self.assertEqual(r, False)
3087
3088 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3089 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003090 r = client.enqueue(tenant='tenant-one',
3091 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003092 project='org/project',
3093 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003094 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003095 self.assertEqual(r, False)
3096
3097 self.waitUntilSettled()
3098 self.assertEqual(len(self.history), 0)
3099 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003100
3101 def test_client_promote(self):
3102 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003103 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003104 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3105 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3106 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003107 A.addApproval('code-review', 2)
3108 B.addApproval('code-review', 2)
3109 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003110
James E. Blair8b5408c2016-08-08 15:37:46 -07003111 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3112 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3113 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003114
3115 self.waitUntilSettled()
3116
Paul Belangerbaca3132016-11-04 12:49:54 -04003117 tenant = self.sched.abide.tenants.get('tenant-one')
3118 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003119 enqueue_times = {}
3120 for item in items:
3121 enqueue_times[str(item.change)] = item.enqueue_time
3122
James E. Blair36658cf2013-12-06 17:53:48 -08003123 client = zuul.rpcclient.RPCClient('127.0.0.1',
3124 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003125 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003126 r = client.promote(tenant='tenant-one',
3127 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003128 change_ids=['2,1', '3,1'])
3129
Sean Daguef39b9ca2014-01-10 21:34:35 -05003130 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003131 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003132 for item in items:
3133 self.assertEqual(
3134 enqueue_times[str(item.change)], item.enqueue_time)
3135
James E. Blair78acec92014-02-06 07:11:32 -08003136 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003137 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003138 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003139 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003140 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003141 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003142 self.waitUntilSettled()
3143
3144 self.assertEqual(len(self.builds), 6)
3145 self.assertEqual(self.builds[0].name, 'project-test1')
3146 self.assertEqual(self.builds[1].name, 'project-test2')
3147 self.assertEqual(self.builds[2].name, 'project-test1')
3148 self.assertEqual(self.builds[3].name, 'project-test2')
3149 self.assertEqual(self.builds[4].name, 'project-test1')
3150 self.assertEqual(self.builds[5].name, 'project-test2')
3151
Paul Belangerbaca3132016-11-04 12:49:54 -04003152 self.assertTrue(self.builds[0].hasChanges(B))
3153 self.assertFalse(self.builds[0].hasChanges(A))
3154 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003155
Paul Belangerbaca3132016-11-04 12:49:54 -04003156 self.assertTrue(self.builds[2].hasChanges(B))
3157 self.assertTrue(self.builds[2].hasChanges(C))
3158 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003159
Paul Belangerbaca3132016-11-04 12:49:54 -04003160 self.assertTrue(self.builds[4].hasChanges(B))
3161 self.assertTrue(self.builds[4].hasChanges(C))
3162 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003163
Paul Belanger174a8272017-03-14 13:20:10 -04003164 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003165 self.waitUntilSettled()
3166
3167 self.assertEqual(A.data['status'], 'MERGED')
3168 self.assertEqual(A.reported, 2)
3169 self.assertEqual(B.data['status'], 'MERGED')
3170 self.assertEqual(B.reported, 2)
3171 self.assertEqual(C.data['status'], 'MERGED')
3172 self.assertEqual(C.reported, 2)
3173
James E. Blair36658cf2013-12-06 17:53:48 -08003174 self.assertEqual(r, True)
3175
3176 def test_client_promote_dependent(self):
3177 "Test that the RPC client can promote a dependent change"
3178 # C (depends on B) -> B -> A ; then promote C to get:
3179 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003180 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003181 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3182 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3183 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3184
3185 C.setDependsOn(B, 1)
3186
James E. Blair8b5408c2016-08-08 15:37:46 -07003187 A.addApproval('code-review', 2)
3188 B.addApproval('code-review', 2)
3189 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003190
James E. Blair8b5408c2016-08-08 15:37:46 -07003191 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3192 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3193 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003194
3195 self.waitUntilSettled()
3196
3197 client = zuul.rpcclient.RPCClient('127.0.0.1',
3198 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003199 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003200 r = client.promote(tenant='tenant-one',
3201 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003202 change_ids=['3,1'])
3203
James E. Blair78acec92014-02-06 07:11:32 -08003204 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003205 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003206 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003207 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003208 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003209 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003210 self.waitUntilSettled()
3211
3212 self.assertEqual(len(self.builds), 6)
3213 self.assertEqual(self.builds[0].name, 'project-test1')
3214 self.assertEqual(self.builds[1].name, 'project-test2')
3215 self.assertEqual(self.builds[2].name, 'project-test1')
3216 self.assertEqual(self.builds[3].name, 'project-test2')
3217 self.assertEqual(self.builds[4].name, 'project-test1')
3218 self.assertEqual(self.builds[5].name, 'project-test2')
3219
Paul Belangerbaca3132016-11-04 12:49:54 -04003220 self.assertTrue(self.builds[0].hasChanges(B))
3221 self.assertFalse(self.builds[0].hasChanges(A))
3222 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003223
Paul Belangerbaca3132016-11-04 12:49:54 -04003224 self.assertTrue(self.builds[2].hasChanges(B))
3225 self.assertTrue(self.builds[2].hasChanges(C))
3226 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003227
Paul Belangerbaca3132016-11-04 12:49:54 -04003228 self.assertTrue(self.builds[4].hasChanges(B))
3229 self.assertTrue(self.builds[4].hasChanges(C))
3230 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003231
Paul Belanger174a8272017-03-14 13:20:10 -04003232 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003233 self.waitUntilSettled()
3234
3235 self.assertEqual(A.data['status'], 'MERGED')
3236 self.assertEqual(A.reported, 2)
3237 self.assertEqual(B.data['status'], 'MERGED')
3238 self.assertEqual(B.reported, 2)
3239 self.assertEqual(C.data['status'], 'MERGED')
3240 self.assertEqual(C.reported, 2)
3241
James E. Blair36658cf2013-12-06 17:53:48 -08003242 self.assertEqual(r, True)
3243
3244 def test_client_promote_negative(self):
3245 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003246 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003247 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003248 A.addApproval('code-review', 2)
3249 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003250 self.waitUntilSettled()
3251
3252 client = zuul.rpcclient.RPCClient('127.0.0.1',
3253 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003254 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003255
3256 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003257 r = client.promote(tenant='tenant-one',
3258 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003259 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003260 self.assertEqual(r, False)
3261
3262 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003263 r = client.promote(tenant='tenant-one',
3264 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003265 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003266 self.assertEqual(r, False)
3267
Paul Belanger174a8272017-03-14 13:20:10 -04003268 self.executor_server.hold_jobs_in_build = False
3269 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003270 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003271
James E. Blaircc324192017-04-19 14:36:08 -07003272 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003273 def test_queue_rate_limiting(self):
3274 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003275 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003276 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3277 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3278 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3279
3280 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003281 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003282
James E. Blair8b5408c2016-08-08 15:37:46 -07003283 A.addApproval('code-review', 2)
3284 B.addApproval('code-review', 2)
3285 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003286
James E. Blair8b5408c2016-08-08 15:37:46 -07003287 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3288 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3289 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003290 self.waitUntilSettled()
3291
3292 # Only A and B will have their merge jobs queued because
3293 # window is 2.
3294 self.assertEqual(len(self.builds), 2)
3295 self.assertEqual(self.builds[0].name, 'project-merge')
3296 self.assertEqual(self.builds[1].name, 'project-merge')
3297
James E. Blair732ee862017-04-21 07:49:26 -07003298 # Release the merge jobs one at a time.
3299 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003300 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003301 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003302 self.waitUntilSettled()
3303
3304 # Only A and B will have their test jobs queued because
3305 # window is 2.
3306 self.assertEqual(len(self.builds), 4)
3307 self.assertEqual(self.builds[0].name, 'project-test1')
3308 self.assertEqual(self.builds[1].name, 'project-test2')
3309 self.assertEqual(self.builds[2].name, 'project-test1')
3310 self.assertEqual(self.builds[3].name, 'project-test2')
3311
Paul Belanger174a8272017-03-14 13:20:10 -04003312 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003313 self.waitUntilSettled()
3314
James E. Blaircc324192017-04-19 14:36:08 -07003315 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003316 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003317 # A failed so window is reduced by 1 to 1.
3318 self.assertEqual(queue.window, 1)
3319 self.assertEqual(queue.window_floor, 1)
3320 self.assertEqual(A.data['status'], 'NEW')
3321
3322 # Gate is reset and only B's merge job is queued because
3323 # window shrunk to 1.
3324 self.assertEqual(len(self.builds), 1)
3325 self.assertEqual(self.builds[0].name, 'project-merge')
3326
Paul Belanger174a8272017-03-14 13:20:10 -04003327 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003328 self.waitUntilSettled()
3329
3330 # Only B's test jobs are queued because window is still 1.
3331 self.assertEqual(len(self.builds), 2)
3332 self.assertEqual(self.builds[0].name, 'project-test1')
3333 self.assertEqual(self.builds[1].name, 'project-test2')
3334
Paul Belanger174a8272017-03-14 13:20:10 -04003335 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003336 self.waitUntilSettled()
3337
3338 # B was successfully merged so window is increased to 2.
3339 self.assertEqual(queue.window, 2)
3340 self.assertEqual(queue.window_floor, 1)
3341 self.assertEqual(B.data['status'], 'MERGED')
3342
3343 # Only C is left and its merge job is queued.
3344 self.assertEqual(len(self.builds), 1)
3345 self.assertEqual(self.builds[0].name, 'project-merge')
3346
Paul Belanger174a8272017-03-14 13:20:10 -04003347 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003348 self.waitUntilSettled()
3349
3350 # After successful merge job the test jobs for C are queued.
3351 self.assertEqual(len(self.builds), 2)
3352 self.assertEqual(self.builds[0].name, 'project-test1')
3353 self.assertEqual(self.builds[1].name, 'project-test2')
3354
Paul Belanger174a8272017-03-14 13:20:10 -04003355 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003356 self.waitUntilSettled()
3357
3358 # C successfully merged so window is bumped to 3.
3359 self.assertEqual(queue.window, 3)
3360 self.assertEqual(queue.window_floor, 1)
3361 self.assertEqual(C.data['status'], 'MERGED')
3362
James E. Blaircc324192017-04-19 14:36:08 -07003363 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003364 def test_queue_rate_limiting_dependent(self):
3365 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003366 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003367 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3368 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3369 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3370
3371 B.setDependsOn(A, 1)
3372
Paul Belanger174a8272017-03-14 13:20:10 -04003373 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003374
James E. Blair8b5408c2016-08-08 15:37:46 -07003375 A.addApproval('code-review', 2)
3376 B.addApproval('code-review', 2)
3377 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003378
James E. Blair8b5408c2016-08-08 15:37:46 -07003379 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3380 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3381 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003382 self.waitUntilSettled()
3383
3384 # Only A and B will have their merge jobs queued because
3385 # window is 2.
3386 self.assertEqual(len(self.builds), 2)
3387 self.assertEqual(self.builds[0].name, 'project-merge')
3388 self.assertEqual(self.builds[1].name, 'project-merge')
3389
James E. Blairf4a5f022017-04-18 14:01:10 -07003390 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003391
3392 # Only A and B will have their test jobs queued because
3393 # window is 2.
3394 self.assertEqual(len(self.builds), 4)
3395 self.assertEqual(self.builds[0].name, 'project-test1')
3396 self.assertEqual(self.builds[1].name, 'project-test2')
3397 self.assertEqual(self.builds[2].name, 'project-test1')
3398 self.assertEqual(self.builds[3].name, 'project-test2')
3399
Paul Belanger174a8272017-03-14 13:20:10 -04003400 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003401 self.waitUntilSettled()
3402
James E. Blaircc324192017-04-19 14:36:08 -07003403 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003404 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003405 # A failed so window is reduced by 1 to 1.
3406 self.assertEqual(queue.window, 1)
3407 self.assertEqual(queue.window_floor, 1)
3408 self.assertEqual(A.data['status'], 'NEW')
3409 self.assertEqual(B.data['status'], 'NEW')
3410
3411 # Gate is reset and only C's merge job is queued because
3412 # window shrunk to 1 and A and B were dequeued.
3413 self.assertEqual(len(self.builds), 1)
3414 self.assertEqual(self.builds[0].name, 'project-merge')
3415
James E. Blairf4a5f022017-04-18 14:01:10 -07003416 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003417
3418 # Only C's test jobs are queued because window is still 1.
3419 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003420 builds = self.getSortedBuilds()
3421 self.assertEqual(builds[0].name, 'project-test1')
3422 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003423
Paul Belanger174a8272017-03-14 13:20:10 -04003424 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003425 self.waitUntilSettled()
3426
3427 # C was successfully merged so window is increased to 2.
3428 self.assertEqual(queue.window, 2)
3429 self.assertEqual(queue.window_floor, 1)
3430 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003431
3432 def test_worker_update_metadata(self):
3433 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003434 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003435
3436 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003437 A.addApproval('code-review', 2)
3438 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003439 self.waitUntilSettled()
3440
Clint Byrumd4de79a2017-05-27 11:39:13 -07003441 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003442
3443 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003444 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003445
3446 start = time.time()
3447 while True:
3448 if time.time() - start > 10:
3449 raise Exception("Timeout waiting for gearman server to report "
3450 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003451 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003452 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003453 break
3454 else:
3455 time.sleep(0)
3456
3457 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003458 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003459
Paul Belanger174a8272017-03-14 13:20:10 -04003460 self.executor_server.hold_jobs_in_build = False
3461 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003462 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003463
James E. Blaircc324192017-04-19 14:36:08 -07003464 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003465 def test_footer_message(self):
3466 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003467 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003468 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003469 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003470 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003471 self.waitUntilSettled()
3472
3473 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003474 B.addApproval('code-review', 2)
3475 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003476 self.waitUntilSettled()
3477
3478 self.assertEqual(2, len(self.smtp_messages))
3479
Adam Gandelman82258fd2017-02-07 16:51:58 -08003480 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003481Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003482http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003483
Adam Gandelman82258fd2017-02-07 16:51:58 -08003484 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003485For CI problems and help debugging, contact ci@example.org"""
3486
Adam Gandelman82258fd2017-02-07 16:51:58 -08003487 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3488 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3489 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3490 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003491
Tobias Henkel9842bd72017-05-16 13:40:03 +02003492 @simple_layout('layouts/unmanaged-project.yaml')
3493 def test_unmanaged_project_start_message(self):
3494 "Test start reporting is not done for unmanaged projects."
3495 self.init_repo("org/project", tag='init')
3496 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3497 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3498 self.waitUntilSettled()
3499
3500 self.assertEqual(0, len(A.messages))
3501
Clint Byrumb9de8be2017-05-27 13:49:19 -07003502 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003503 def test_merge_failure_reporters(self):
3504 """Check that the config is set up correctly"""
3505
Clint Byrumb9de8be2017-05-27 13:49:19 -07003506 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003507 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003508 "Merge Failed.\n\nThis change or one of its cross-repo "
3509 "dependencies was unable to be automatically merged with the "
3510 "current state of its repository. Please rebase the change and "
3511 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003512 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003513 self.assertEqual(
3514 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003515 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003516
3517 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003518 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003519 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003520 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003521
3522 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003523 tenant.layout.pipelines['check'].merge_failure_actions[0],
3524 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003525
3526 self.assertTrue(
3527 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003528 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003529 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003530 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3531 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003532 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003533 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003534 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003535 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003536 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003537 gerritreporter.GerritReporter) and
3538 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003539 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003540 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003541 )
3542 )
3543
James E. Blairec056492016-07-22 09:45:56 -07003544 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003545 def test_merge_failure_reports(self):
3546 """Check that when a change fails to merge the correct message is sent
3547 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003548 self.updateConfigLayout(
3549 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003550 self.sched.reconfigure(self.config)
3551 self.registerJobs()
3552
3553 # Check a test failure isn't reported to SMTP
3554 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003555 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003556 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003557 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003558 self.waitUntilSettled()
3559
3560 self.assertEqual(3, len(self.history)) # 3 jobs
3561 self.assertEqual(0, len(self.smtp_messages))
3562
3563 # Check a merge failure is reported to SMTP
3564 # B should be merged, but C will conflict with B
3565 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3566 B.addPatchset(['conflict'])
3567 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3568 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003569 B.addApproval('code-review', 2)
3570 C.addApproval('code-review', 2)
3571 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3572 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003573 self.waitUntilSettled()
3574
3575 self.assertEqual(6, len(self.history)) # A and B jobs
3576 self.assertEqual(1, len(self.smtp_messages))
3577 self.assertEqual('The merge failed! For more information...',
3578 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003579
James E. Blairec056492016-07-22 09:45:56 -07003580 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003581 def test_default_merge_failure_reports(self):
3582 """Check that the default merge failure reports are correct."""
3583
3584 # A should report success, B should report merge failure.
3585 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3586 A.addPatchset(['conflict'])
3587 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3588 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003589 A.addApproval('code-review', 2)
3590 B.addApproval('code-review', 2)
3591 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3592 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003593 self.waitUntilSettled()
3594
3595 self.assertEqual(3, len(self.history)) # A jobs
3596 self.assertEqual(A.reported, 2)
3597 self.assertEqual(B.reported, 2)
3598 self.assertEqual(A.data['status'], 'MERGED')
3599 self.assertEqual(B.data['status'], 'NEW')
3600 self.assertIn('Build succeeded', A.messages[1])
3601 self.assertIn('Merge Failed', B.messages[1])
3602 self.assertIn('automatically merged', B.messages[1])
3603 self.assertNotIn('logs.example.com', B.messages[1])
3604 self.assertNotIn('SKIPPED', B.messages[1])
3605
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003606 def test_client_get_running_jobs(self):
3607 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003608 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003609 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003610 A.addApproval('code-review', 2)
3611 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003612 self.waitUntilSettled()
3613
3614 client = zuul.rpcclient.RPCClient('127.0.0.1',
3615 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003616 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003617
3618 # Wait for gearman server to send the initial workData back to zuul
3619 start = time.time()
3620 while True:
3621 if time.time() - start > 10:
3622 raise Exception("Timeout waiting for gearman server to report "
3623 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003624 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003625 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003626 break
3627 else:
3628 time.sleep(0)
3629
3630 running_items = client.get_running_jobs()
3631
3632 self.assertEqual(1, len(running_items))
3633 running_item = running_items[0]
3634 self.assertEqual([], running_item['failing_reasons'])
3635 self.assertEqual([], running_item['items_behind'])
3636 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003637 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003638 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003639 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003640 self.assertEqual(True, running_item['active'])
3641 self.assertEqual('1,1', running_item['id'])
3642
3643 self.assertEqual(3, len(running_item['jobs']))
3644 for job in running_item['jobs']:
3645 if job['name'] == 'project-merge':
3646 self.assertEqual('project-merge', job['name'])
3647 self.assertEqual('gate', job['pipeline'])
3648 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003649 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003650 'static/stream.html?uuid={uuid}&logfile=console.log'
3651 .format(uuid=job['uuid']), job['url'])
3652 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003653 'finger://{hostname}/{uuid}'.format(
3654 hostname=self.executor_server.hostname,
3655 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003656 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003657 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003658 self.assertEqual(False, job['canceled'])
3659 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003660 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003661 self.assertEqual('gate', job['pipeline'])
3662 break
3663
Paul Belanger174a8272017-03-14 13:20:10 -04003664 self.executor_server.hold_jobs_in_build = False
3665 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003666 self.waitUntilSettled()
3667
3668 running_items = client.get_running_jobs()
3669 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003670
James E. Blairb70e55a2017-04-19 12:57:02 -07003671 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003672 def test_nonvoting_pipeline(self):
3673 "Test that a nonvoting pipeline (experimental) can still report"
3674
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003675 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3676 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003677 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3678 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003679
3680 self.assertEqual(self.getJobFromHistory('project-merge').result,
3681 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003682 self.assertEqual(
3683 self.getJobFromHistory('experimental-project-test').result,
3684 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003685 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003686
3687 def test_crd_gate(self):
3688 "Test cross-repo dependencies"
3689 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3690 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003691 A.addApproval('code-review', 2)
3692 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003693
3694 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3695 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3696 AM2.setMerged()
3697 AM1.setMerged()
3698
3699 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3700 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3701 BM2.setMerged()
3702 BM1.setMerged()
3703
3704 # A -> AM1 -> AM2
3705 # B -> BM1 -> BM2
3706 # A Depends-On: B
3707 # M2 is here to make sure it is never queried. If it is, it
3708 # means zuul is walking down the entire history of merged
3709 # changes.
3710
3711 B.setDependsOn(BM1, 1)
3712 BM1.setDependsOn(BM2, 1)
3713
3714 A.setDependsOn(AM1, 1)
3715 AM1.setDependsOn(AM2, 1)
3716
3717 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3718 A.subject, B.data['id'])
3719
James E. Blair8b5408c2016-08-08 15:37:46 -07003720 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003721 self.waitUntilSettled()
3722
3723 self.assertEqual(A.data['status'], 'NEW')
3724 self.assertEqual(B.data['status'], 'NEW')
3725
Paul Belanger5dccbe72016-11-14 11:17:37 -05003726 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003727 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003728
Paul Belanger174a8272017-03-14 13:20:10 -04003729 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003730 B.addApproval('approved', 1)
3731 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003732 self.waitUntilSettled()
3733
Paul Belanger174a8272017-03-14 13:20:10 -04003734 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003735 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003736 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003737 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003738 self.executor_server.hold_jobs_in_build = False
3739 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003740 self.waitUntilSettled()
3741
3742 self.assertEqual(AM2.queried, 0)
3743 self.assertEqual(BM2.queried, 0)
3744 self.assertEqual(A.data['status'], 'MERGED')
3745 self.assertEqual(B.data['status'], 'MERGED')
3746 self.assertEqual(A.reported, 2)
3747 self.assertEqual(B.reported, 2)
3748
Paul Belanger5dccbe72016-11-14 11:17:37 -05003749 changes = self.getJobFromHistory(
3750 'project-merge', 'org/project1').changes
3751 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003752
3753 def test_crd_branch(self):
3754 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003755
3756 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003757 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3758 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3759 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3760 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003761 A.addApproval('code-review', 2)
3762 B.addApproval('code-review', 2)
3763 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003764
3765 # A Depends-On: B+C
3766 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3767 A.subject, B.data['id'])
3768
Paul Belanger174a8272017-03-14 13:20:10 -04003769 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003770 B.addApproval('approved', 1)
3771 C.addApproval('approved', 1)
3772 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003773 self.waitUntilSettled()
3774
Paul Belanger174a8272017-03-14 13:20:10 -04003775 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003776 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003777 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003778 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003779 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003780 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003781 self.executor_server.hold_jobs_in_build = False
3782 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003783 self.waitUntilSettled()
3784
3785 self.assertEqual(A.data['status'], 'MERGED')
3786 self.assertEqual(B.data['status'], 'MERGED')
3787 self.assertEqual(C.data['status'], 'MERGED')
3788 self.assertEqual(A.reported, 2)
3789 self.assertEqual(B.reported, 2)
3790 self.assertEqual(C.reported, 2)
3791
Paul Belanger6379db12016-11-14 13:57:54 -05003792 changes = self.getJobFromHistory(
3793 'project-merge', 'org/project1').changes
3794 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003795
3796 def test_crd_multiline(self):
3797 "Test multiple depends-on lines in commit"
3798 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3799 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3800 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003801 A.addApproval('code-review', 2)
3802 B.addApproval('code-review', 2)
3803 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003804
3805 # A Depends-On: B+C
3806 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3807 A.subject, B.data['id'], C.data['id'])
3808
Paul Belanger174a8272017-03-14 13:20:10 -04003809 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003810 B.addApproval('approved', 1)
3811 C.addApproval('approved', 1)
3812 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003813 self.waitUntilSettled()
3814
Paul Belanger174a8272017-03-14 13:20:10 -04003815 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003816 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003817 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003818 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003819 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003820 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003821 self.executor_server.hold_jobs_in_build = False
3822 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003823 self.waitUntilSettled()
3824
3825 self.assertEqual(A.data['status'], 'MERGED')
3826 self.assertEqual(B.data['status'], 'MERGED')
3827 self.assertEqual(C.data['status'], 'MERGED')
3828 self.assertEqual(A.reported, 2)
3829 self.assertEqual(B.reported, 2)
3830 self.assertEqual(C.reported, 2)
3831
Paul Belanger5dccbe72016-11-14 11:17:37 -05003832 changes = self.getJobFromHistory(
3833 'project-merge', 'org/project1').changes
3834 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003835
3836 def test_crd_unshared_gate(self):
3837 "Test cross-repo dependencies in unshared gate queues"
3838 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3839 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003840 A.addApproval('code-review', 2)
3841 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003842
3843 # A Depends-On: B
3844 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3845 A.subject, B.data['id'])
3846
3847 # A and B do not share a queue, make sure that A is unable to
3848 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003849 B.addApproval('approved', 1)
3850 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003851 self.waitUntilSettled()
3852
3853 self.assertEqual(A.data['status'], 'NEW')
3854 self.assertEqual(B.data['status'], 'NEW')
3855 self.assertEqual(A.reported, 0)
3856 self.assertEqual(B.reported, 0)
3857 self.assertEqual(len(self.history), 0)
3858
3859 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003860 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003861 self.waitUntilSettled()
3862
3863 self.assertEqual(B.data['status'], 'MERGED')
3864 self.assertEqual(B.reported, 2)
3865
3866 # Now that B is merged, A should be able to be enqueued and
3867 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003868 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003869 self.waitUntilSettled()
3870
3871 self.assertEqual(A.data['status'], 'MERGED')
3872 self.assertEqual(A.reported, 2)
3873
James E. Blair96698e22015-04-02 07:48:21 -07003874 def test_crd_gate_reverse(self):
3875 "Test reverse cross-repo dependencies"
3876 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3877 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003878 A.addApproval('code-review', 2)
3879 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003880
3881 # A Depends-On: B
3882
3883 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3884 A.subject, B.data['id'])
3885
James E. Blair8b5408c2016-08-08 15:37:46 -07003886 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003887 self.waitUntilSettled()
3888
3889 self.assertEqual(A.data['status'], 'NEW')
3890 self.assertEqual(B.data['status'], 'NEW')
3891
Paul Belanger174a8272017-03-14 13:20:10 -04003892 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003893 A.addApproval('approved', 1)
3894 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003895 self.waitUntilSettled()
3896
Paul Belanger174a8272017-03-14 13:20:10 -04003897 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003898 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003899 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003900 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003901 self.executor_server.hold_jobs_in_build = False
3902 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003903 self.waitUntilSettled()
3904
3905 self.assertEqual(A.data['status'], 'MERGED')
3906 self.assertEqual(B.data['status'], 'MERGED')
3907 self.assertEqual(A.reported, 2)
3908 self.assertEqual(B.reported, 2)
3909
Paul Belanger5dccbe72016-11-14 11:17:37 -05003910 changes = self.getJobFromHistory(
3911 'project-merge', 'org/project1').changes
3912 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003913
James E. Blair5ee24252014-12-30 10:12:29 -08003914 def test_crd_cycle(self):
3915 "Test cross-repo dependency cycles"
3916 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3917 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003918 A.addApproval('code-review', 2)
3919 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003920
3921 # A -> B -> A (via commit-depends)
3922
3923 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3924 A.subject, B.data['id'])
3925 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3926 B.subject, A.data['id'])
3927
James E. Blair8b5408c2016-08-08 15:37:46 -07003928 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003929 self.waitUntilSettled()
3930
3931 self.assertEqual(A.reported, 0)
3932 self.assertEqual(B.reported, 0)
3933 self.assertEqual(A.data['status'], 'NEW')
3934 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003935
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003936 def test_crd_gate_unknown(self):
3937 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003938 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003939 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3940 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003941 A.addApproval('code-review', 2)
3942 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003943
3944 # A Depends-On: B
3945 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3946 A.subject, B.data['id'])
3947
James E. Blair8b5408c2016-08-08 15:37:46 -07003948 B.addApproval('approved', 1)
3949 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003950 self.waitUntilSettled()
3951
3952 # Unknown projects cannot share a queue with any other
3953 # since they don't have common jobs with any other (they have no jobs).
3954 # Changes which depend on unknown project changes
3955 # should not be processed in dependent pipeline
3956 self.assertEqual(A.data['status'], 'NEW')
3957 self.assertEqual(B.data['status'], 'NEW')
3958 self.assertEqual(A.reported, 0)
3959 self.assertEqual(B.reported, 0)
3960 self.assertEqual(len(self.history), 0)
3961
James E. Blair7720b402017-06-07 08:51:49 -07003962 # Simulate change B being gated outside this layout Set the
3963 # change merged before submitting the event so that when the
3964 # event triggers a gerrit query to update the change, we get
3965 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003966 B.setMerged()
James E. Blair7720b402017-06-07 08:51:49 -07003967 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003968 self.waitUntilSettled()
3969 self.assertEqual(len(self.history), 0)
3970
3971 # Now that B is merged, A should be able to be enqueued and
3972 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003973 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003974 self.waitUntilSettled()
3975
3976 self.assertEqual(A.data['status'], 'MERGED')
3977 self.assertEqual(A.reported, 2)
3978 self.assertEqual(B.data['status'], 'MERGED')
3979 self.assertEqual(B.reported, 0)
3980
James E. Blairbfb8e042014-12-30 17:01:44 -08003981 def test_crd_check(self):
3982 "Test cross-repo dependencies in independent pipelines"
3983
Paul Belanger174a8272017-03-14 13:20:10 -04003984 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003985 self.gearman_server.hold_jobs_in_queue = True
3986 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3987 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3988
3989 # A Depends-On: B
3990 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3991 A.subject, B.data['id'])
3992
3993 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3994 self.waitUntilSettled()
3995
James E. Blairbfb8e042014-12-30 17:01:44 -08003996 self.gearman_server.hold_jobs_in_queue = False
3997 self.gearman_server.release()
3998 self.waitUntilSettled()
3999
Paul Belanger174a8272017-03-14 13:20:10 -04004000 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004001 self.waitUntilSettled()
4002
James E. Blair2a535672017-04-27 12:03:15 -07004003 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004004
Paul Belanger174a8272017-03-14 13:20:10 -04004005 self.executor_server.hold_jobs_in_build = False
4006 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004007 self.waitUntilSettled()
4008
James E. Blairbfb8e042014-12-30 17:01:44 -08004009 self.assertEqual(A.data['status'], 'NEW')
4010 self.assertEqual(B.data['status'], 'NEW')
4011 self.assertEqual(A.reported, 1)
4012 self.assertEqual(B.reported, 0)
4013
4014 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004015 tenant = self.sched.abide.tenants.get('tenant-one')
4016 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004017
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004018 def test_crd_check_git_depends(self):
4019 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004020 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004021 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4022 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4023
4024 # Add two git-dependent changes and make sure they both report
4025 # success.
4026 B.setDependsOn(A, 1)
4027 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4028 self.waitUntilSettled()
4029 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4030 self.waitUntilSettled()
4031
James E. Blairb8c16472015-05-05 14:55:26 -07004032 self.orderedRelease()
4033 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004034 self.waitUntilSettled()
4035
4036 self.assertEqual(A.data['status'], 'NEW')
4037 self.assertEqual(B.data['status'], 'NEW')
4038 self.assertEqual(A.reported, 1)
4039 self.assertEqual(B.reported, 1)
4040
4041 self.assertEqual(self.history[0].changes, '1,1')
4042 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004043 tenant = self.sched.abide.tenants.get('tenant-one')
4044 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004045
4046 self.assertIn('Build succeeded', A.messages[0])
4047 self.assertIn('Build succeeded', B.messages[0])
4048
4049 def test_crd_check_duplicate(self):
4050 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004051 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004052 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4053 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004054 tenant = self.sched.abide.tenants.get('tenant-one')
4055 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004056
4057 # Add two git-dependent changes...
4058 B.setDependsOn(A, 1)
4059 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4060 self.waitUntilSettled()
4061 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4062
4063 # ...make sure the live one is not duplicated...
4064 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4065 self.waitUntilSettled()
4066 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4067
4068 # ...but the non-live one is able to be.
4069 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4070 self.waitUntilSettled()
4071 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4072
Clark Boylandd849822015-03-02 12:38:14 -08004073 # Release jobs in order to avoid races with change A jobs
4074 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004075 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004076 self.executor_server.hold_jobs_in_build = False
4077 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004078 self.waitUntilSettled()
4079
4080 self.assertEqual(A.data['status'], 'NEW')
4081 self.assertEqual(B.data['status'], 'NEW')
4082 self.assertEqual(A.reported, 1)
4083 self.assertEqual(B.reported, 1)
4084
4085 self.assertEqual(self.history[0].changes, '1,1 2,1')
4086 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004087 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004088
4089 self.assertIn('Build succeeded', A.messages[0])
4090 self.assertIn('Build succeeded', B.messages[0])
4091
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004092 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004093 "Test cross-repo dependencies re-enqueued in independent pipelines"
4094
4095 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004096 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4097 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004098
4099 # A Depends-On: B
4100 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4101 A.subject, B.data['id'])
4102
4103 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4104 self.waitUntilSettled()
4105
4106 self.sched.reconfigure(self.config)
4107
4108 # Make sure the items still share a change queue, and the
4109 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004110 tenant = self.sched.abide.tenants.get('tenant-one')
4111 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4112 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004113 first_item = queue.queue[0]
4114 for item in queue.queue:
4115 self.assertEqual(item.queue, first_item.queue)
4116 self.assertFalse(first_item.live)
4117 self.assertTrue(queue.queue[1].live)
4118
4119 self.gearman_server.hold_jobs_in_queue = False
4120 self.gearman_server.release()
4121 self.waitUntilSettled()
4122
4123 self.assertEqual(A.data['status'], 'NEW')
4124 self.assertEqual(B.data['status'], 'NEW')
4125 self.assertEqual(A.reported, 1)
4126 self.assertEqual(B.reported, 0)
4127
4128 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004129 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004130
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004131 def test_crd_check_reconfiguration(self):
4132 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4133
4134 def test_crd_undefined_project(self):
4135 """Test that undefined projects in dependencies are handled for
4136 independent pipelines"""
4137 # It's a hack for fake gerrit,
4138 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004139 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004140 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4141
Jesse Keatingea68ab42017-04-19 15:03:30 -07004142 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004143 def test_crd_check_ignore_dependencies(self):
4144 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004145
4146 self.gearman_server.hold_jobs_in_queue = True
4147 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4148 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4149 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4150
4151 # A Depends-On: B
4152 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4153 A.subject, B.data['id'])
4154 # C git-depends on B
4155 C.setDependsOn(B, 1)
4156 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4157 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4158 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4159 self.waitUntilSettled()
4160
4161 # Make sure none of the items share a change queue, and all
4162 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004163 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004164 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004165 self.assertEqual(len(check_pipeline.queues), 3)
4166 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4167 for item in check_pipeline.getAllItems():
4168 self.assertTrue(item.live)
4169
4170 self.gearman_server.hold_jobs_in_queue = False
4171 self.gearman_server.release()
4172 self.waitUntilSettled()
4173
4174 self.assertEqual(A.data['status'], 'NEW')
4175 self.assertEqual(B.data['status'], 'NEW')
4176 self.assertEqual(C.data['status'], 'NEW')
4177 self.assertEqual(A.reported, 1)
4178 self.assertEqual(B.reported, 1)
4179 self.assertEqual(C.reported, 1)
4180
4181 # Each job should have tested exactly one change
4182 for job in self.history:
4183 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004184
James E. Blair7cb84542017-04-19 13:35:05 -07004185 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004186 def test_crd_check_transitive(self):
4187 "Test transitive cross-repo dependencies"
4188 # Specifically, if A -> B -> C, and C gets a new patchset and
4189 # A gets a new patchset, ensure the test of A,2 includes B,1
4190 # and C,2 (not C,1 which would indicate stale data in the
4191 # cache for B).
4192 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4193 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4194 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4195
4196 # A Depends-On: B
4197 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4198 A.subject, B.data['id'])
4199
4200 # B Depends-On: C
4201 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4202 B.subject, C.data['id'])
4203
4204 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4205 self.waitUntilSettled()
4206 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4207
4208 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4209 self.waitUntilSettled()
4210 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4211
4212 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4213 self.waitUntilSettled()
4214 self.assertEqual(self.history[-1].changes, '3,1')
4215
4216 C.addPatchset()
4217 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4218 self.waitUntilSettled()
4219 self.assertEqual(self.history[-1].changes, '3,2')
4220
4221 A.addPatchset()
4222 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4223 self.waitUntilSettled()
4224 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004225
James E. Blaira8b90b32016-08-24 15:18:50 -07004226 def test_crd_check_unknown(self):
4227 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004228 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004229 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4230 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4231 # A Depends-On: B
4232 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4233 A.subject, B.data['id'])
4234
4235 # Make sure zuul has seen an event on B.
4236 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4237 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4238 self.waitUntilSettled()
4239
4240 self.assertEqual(A.data['status'], 'NEW')
4241 self.assertEqual(A.reported, 1)
4242 self.assertEqual(B.data['status'], 'NEW')
4243 self.assertEqual(B.reported, 0)
4244
James E. Blair92464a22016-04-05 10:21:26 -07004245 def test_crd_cycle_join(self):
4246 "Test an updated change creates a cycle"
4247 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4248
4249 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4250 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004251 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004252
4253 # Create B->A
4254 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4255 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4256 B.subject, A.data['id'])
4257 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4258 self.waitUntilSettled()
4259
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004260 # Dep is there so zuul should have reported on B
4261 self.assertEqual(B.reported, 1)
4262
James E. Blair92464a22016-04-05 10:21:26 -07004263 # Update A to add A->B (a cycle).
4264 A.addPatchset()
4265 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4266 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004267 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4268 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004269
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004270 # Dependency cycle injected so zuul should not have reported again on A
4271 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004272
4273 # Now if we update B to remove the depends-on, everything
4274 # should be okay. B; A->B
4275
4276 B.addPatchset()
4277 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004278 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4279 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004280
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004281 # Cycle was removed so now zuul should have reported again on A
4282 self.assertEqual(A.reported, 2)
4283
4284 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4285 self.waitUntilSettled()
4286 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004287
Jesse Keatingcc67b122017-04-19 14:54:57 -07004288 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004289 def test_disable_at(self):
4290 "Test a pipeline will only report to the disabled trigger when failing"
4291
Jesse Keatingcc67b122017-04-19 14:54:57 -07004292 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004293 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004294 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004295 0, tenant.layout.pipelines['check']._consecutive_failures)
4296 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004297
4298 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4299 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4300 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4301 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4302 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4303 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4304 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4305 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4306 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4307 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4308 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4309
Paul Belanger174a8272017-03-14 13:20:10 -04004310 self.executor_server.failJob('project-test1', A)
4311 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004312 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004313 self.executor_server.failJob('project-test1', D)
4314 self.executor_server.failJob('project-test1', E)
4315 self.executor_server.failJob('project-test1', F)
4316 self.executor_server.failJob('project-test1', G)
4317 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004318 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004319 self.executor_server.failJob('project-test1', J)
4320 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004321
4322 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4323 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4324 self.waitUntilSettled()
4325
4326 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004327 2, tenant.layout.pipelines['check']._consecutive_failures)
4328 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004329
4330 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4331 self.waitUntilSettled()
4332
4333 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004334 0, tenant.layout.pipelines['check']._consecutive_failures)
4335 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004336
4337 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4338 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4339 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4340 self.waitUntilSettled()
4341
4342 # We should be disabled now
4343 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004344 3, tenant.layout.pipelines['check']._consecutive_failures)
4345 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004346
4347 # We need to wait between each of these patches to make sure the
4348 # smtp messages come back in an expected order
4349 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4350 self.waitUntilSettled()
4351 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4352 self.waitUntilSettled()
4353 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4354 self.waitUntilSettled()
4355
4356 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4357 # leaving a message on each change
4358 self.assertEqual(1, len(A.messages))
4359 self.assertIn('Build failed.', A.messages[0])
4360 self.assertEqual(1, len(B.messages))
4361 self.assertIn('Build failed.', B.messages[0])
4362 self.assertEqual(1, len(C.messages))
4363 self.assertIn('Build succeeded.', C.messages[0])
4364 self.assertEqual(1, len(D.messages))
4365 self.assertIn('Build failed.', D.messages[0])
4366 self.assertEqual(1, len(E.messages))
4367 self.assertIn('Build failed.', E.messages[0])
4368 self.assertEqual(1, len(F.messages))
4369 self.assertIn('Build failed.', F.messages[0])
4370
4371 # The last 3 (GHI) would have only reported via smtp.
4372 self.assertEqual(3, len(self.smtp_messages))
4373 self.assertEqual(0, len(G.messages))
4374 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004375 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004376 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004377 self.assertEqual(0, len(H.messages))
4378 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004379 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004380 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004381 self.assertEqual(0, len(I.messages))
4382 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004383 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004384 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004385
4386 # Now reload the configuration (simulate a HUP) to check the pipeline
4387 # comes out of disabled
4388 self.sched.reconfigure(self.config)
4389
Jesse Keatingcc67b122017-04-19 14:54:57 -07004390 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004391
4392 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004393 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004394 0, tenant.layout.pipelines['check']._consecutive_failures)
4395 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004396
4397 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4398 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4399 self.waitUntilSettled()
4400
4401 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004402 2, tenant.layout.pipelines['check']._consecutive_failures)
4403 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004404
4405 # J and K went back to gerrit
4406 self.assertEqual(1, len(J.messages))
4407 self.assertIn('Build failed.', J.messages[0])
4408 self.assertEqual(1, len(K.messages))
4409 self.assertIn('Build failed.', K.messages[0])
4410 # No more messages reported via smtp
4411 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004412
James E. Blaircc020532017-04-19 13:04:44 -07004413 @simple_layout('layouts/one-job-project.yaml')
4414 def test_one_job_project(self):
4415 "Test that queueing works with one job"
4416 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4417 'master', 'A')
4418 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4419 'master', 'B')
4420 A.addApproval('code-review', 2)
4421 B.addApproval('code-review', 2)
4422 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4423 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4424 self.waitUntilSettled()
4425
4426 self.assertEqual(A.data['status'], 'MERGED')
4427 self.assertEqual(A.reported, 2)
4428 self.assertEqual(B.data['status'], 'MERGED')
4429 self.assertEqual(B.reported, 2)
4430
Paul Belanger71d98172016-11-08 10:56:31 -05004431 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004432 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004433
Paul Belanger174a8272017-03-14 13:20:10 -04004434 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004435 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4436 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4437 self.waitUntilSettled()
4438
Paul Belanger174a8272017-03-14 13:20:10 -04004439 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004440 self.waitUntilSettled()
4441
4442 self.assertEqual(len(self.builds), 2)
4443 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004444 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004445 self.waitUntilSettled()
4446
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004447 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004448 self.assertEqual(len(self.builds), 1,
4449 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004450 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004451 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004452 self.waitUntilSettled()
4453
Paul Belanger174a8272017-03-14 13:20:10 -04004454 self.executor_server.hold_jobs_in_build = False
4455 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004456 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004457 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004458 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4459 self.assertEqual(A.reported, 1)
4460 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004461
James E. Blair15be0e12017-01-03 13:45:20 -08004462 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004463 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004464
4465 self.fake_nodepool.paused = True
4466 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4467 A.addApproval('code-review', 2)
4468 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4469 self.waitUntilSettled()
4470
4471 self.zk.client.stop()
4472 self.zk.client.start()
4473 self.fake_nodepool.paused = False
4474 self.waitUntilSettled()
4475
4476 self.assertEqual(A.data['status'], 'MERGED')
4477 self.assertEqual(A.reported, 2)
4478
James E. Blair6ab79e02017-01-06 10:10:17 -08004479 def test_nodepool_failure(self):
4480 "Test that jobs are reported after a nodepool failure"
4481
4482 self.fake_nodepool.paused = True
4483 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4484 A.addApproval('code-review', 2)
4485 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4486 self.waitUntilSettled()
4487
4488 req = self.fake_nodepool.getNodeRequests()[0]
4489 self.fake_nodepool.addFailRequest(req)
4490
4491 self.fake_nodepool.paused = False
4492 self.waitUntilSettled()
4493
4494 self.assertEqual(A.data['status'], 'NEW')
4495 self.assertEqual(A.reported, 2)
4496 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4497 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4498 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4499
Arieb6f068c2016-10-09 13:11:06 +03004500
James E. Blaira002b032017-04-18 10:35:48 -07004501class TestExecutor(ZuulTestCase):
4502 tenant_config_file = 'config/single-tenant/main.yaml'
4503
4504 def assertFinalState(self):
4505 # In this test, we expect to shut down in a non-final state,
4506 # so skip these checks.
4507 pass
4508
4509 def assertCleanShutdown(self):
4510 self.log.debug("Assert clean shutdown")
4511
4512 # After shutdown, make sure no jobs are running
4513 self.assertEqual({}, self.executor_server.job_workers)
4514
4515 # Make sure that git.Repo objects have been garbage collected.
4516 repos = []
4517 gc.collect()
4518 for obj in gc.get_objects():
4519 if isinstance(obj, git.Repo):
4520 self.log.debug("Leaked git repo object: %s" % repr(obj))
4521 repos.append(obj)
4522 self.assertEqual(len(repos), 0)
4523
4524 def test_executor_shutdown(self):
4525 "Test that the executor can shut down with jobs running"
4526
4527 self.executor_server.hold_jobs_in_build = True
4528 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4529 A.addApproval('code-review', 2)
4530 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4531 self.waitUntilSettled()
4532
4533
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004534class TestDependencyGraph(ZuulTestCase):
4535 tenant_config_file = 'config/dependency-graph/main.yaml'
4536
4537 def test_dependeny_graph_dispatch_jobs_once(self):
4538 "Test a job in a dependency graph is queued only once"
4539 # Job dependencies, starting with A
4540 # A
4541 # / \
4542 # B C
4543 # / \ / \
4544 # D F E
4545 # |
4546 # G
4547
4548 self.executor_server.hold_jobs_in_build = True
4549 change = self.fake_gerrit.addFakeChange(
4550 'org/project', 'master', 'change')
4551 change.addApproval('code-review', 2)
4552 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4553
4554 self.waitUntilSettled()
4555 self.assertEqual([b.name for b in self.builds], ['A'])
4556
4557 self.executor_server.release('A')
4558 self.waitUntilSettled()
4559 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4560
4561 self.executor_server.release('B')
4562 self.waitUntilSettled()
4563 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4564
4565 self.executor_server.release('D')
4566 self.waitUntilSettled()
4567 self.assertEqual([b.name for b in self.builds], ['C'])
4568
4569 self.executor_server.release('C')
4570 self.waitUntilSettled()
4571 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4572
4573 self.executor_server.release('F')
4574 self.waitUntilSettled()
4575 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4576
4577 self.executor_server.release('G')
4578 self.waitUntilSettled()
4579 self.assertEqual([b.name for b in self.builds], ['E'])
4580
4581 self.executor_server.release('E')
4582 self.waitUntilSettled()
4583 self.assertEqual(len(self.builds), 0)
4584
4585 self.executor_server.hold_jobs_in_build = False
4586 self.executor_server.release()
4587 self.waitUntilSettled()
4588
4589 self.assertEqual(len(self.builds), 0)
4590 self.assertEqual(len(self.history), 7)
4591
4592 self.assertEqual(change.data['status'], 'MERGED')
4593 self.assertEqual(change.reported, 2)
4594
4595 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4596 "Test that a job waits till all dependencies are successful"
4597 # Job dependencies, starting with A
4598 # A
4599 # / \
4600 # B C*
4601 # / \ / \
4602 # D F E
4603 # |
4604 # G
4605
4606 self.executor_server.hold_jobs_in_build = True
4607 change = self.fake_gerrit.addFakeChange(
4608 'org/project', 'master', 'change')
4609 change.addApproval('code-review', 2)
4610
4611 self.executor_server.failJob('C', change)
4612
4613 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4614
4615 self.waitUntilSettled()
4616 self.assertEqual([b.name for b in self.builds], ['A'])
4617
4618 self.executor_server.release('A')
4619 self.waitUntilSettled()
4620 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4621
4622 self.executor_server.release('B')
4623 self.waitUntilSettled()
4624 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4625
4626 self.executor_server.release('D')
4627 self.waitUntilSettled()
4628 self.assertEqual([b.name for b in self.builds], ['C'])
4629
4630 self.executor_server.release('C')
4631 self.waitUntilSettled()
4632 self.assertEqual(len(self.builds), 0)
4633
4634 self.executor_server.hold_jobs_in_build = False
4635 self.executor_server.release()
4636 self.waitUntilSettled()
4637
4638 self.assertEqual(len(self.builds), 0)
4639 self.assertEqual(len(self.history), 4)
4640
4641 self.assertEqual(change.data['status'], 'NEW')
4642 self.assertEqual(change.reported, 2)
4643
4644
Arieb6f068c2016-10-09 13:11:06 +03004645class TestDuplicatePipeline(ZuulTestCase):
4646 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4647
4648 def test_duplicate_pipelines(self):
4649 "Test that a change matching multiple pipelines works"
4650
4651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4652 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4653 self.waitUntilSettled()
4654
4655 self.assertHistory([
4656 dict(name='project-test1', result='SUCCESS', changes='1,1',
4657 pipeline='dup1'),
4658 dict(name='project-test1', result='SUCCESS', changes='1,1',
4659 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004660 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004661
4662 self.assertEqual(len(A.messages), 2)
4663
Paul Belangera46a3742016-11-09 14:23:03 -05004664 if 'dup1' in A.messages[0]:
4665 self.assertIn('dup1', A.messages[0])
4666 self.assertNotIn('dup2', A.messages[0])
4667 self.assertIn('project-test1', A.messages[0])
4668 self.assertIn('dup2', A.messages[1])
4669 self.assertNotIn('dup1', A.messages[1])
4670 self.assertIn('project-test1', A.messages[1])
4671 else:
4672 self.assertIn('dup1', A.messages[1])
4673 self.assertNotIn('dup2', A.messages[1])
4674 self.assertIn('project-test1', A.messages[1])
4675 self.assertIn('dup2', A.messages[0])
4676 self.assertNotIn('dup1', A.messages[0])
4677 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004678
4679
Paul Belanger86085b32016-11-03 12:48:57 -04004680class TestSchedulerTemplatedProject(ZuulTestCase):
4681 tenant_config_file = 'config/templated-project/main.yaml'
4682
Paul Belanger174a8272017-03-14 13:20:10 -04004683 def test_job_from_templates_executed(self):
4684 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004685
4686 A = self.fake_gerrit.addFakeChange(
4687 'org/templated-project', 'master', 'A')
4688 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4689 self.waitUntilSettled()
4690
4691 self.assertEqual(self.getJobFromHistory('project-test1').result,
4692 'SUCCESS')
4693 self.assertEqual(self.getJobFromHistory('project-test2').result,
4694 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004695
4696 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004697 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004698
4699 A = self.fake_gerrit.addFakeChange(
4700 'org/layered-project', 'master', 'A')
4701 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4702 self.waitUntilSettled()
4703
4704 self.assertEqual(self.getJobFromHistory('project-test1').result,
4705 'SUCCESS')
4706 self.assertEqual(self.getJobFromHistory('project-test2').result,
4707 'SUCCESS')
4708 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4709 ).result, 'SUCCESS')
4710 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4711 ).result, 'SUCCESS')
4712 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4713 ).result, 'SUCCESS')
4714 self.assertEqual(self.getJobFromHistory('project-test6').result,
4715 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004716
4717
4718class TestSchedulerSuccessURL(ZuulTestCase):
4719 tenant_config_file = 'config/success-url/main.yaml'
4720
4721 def test_success_url(self):
4722 "Ensure bad build params are ignored"
4723 self.sched.reconfigure(self.config)
4724 self.init_repo('org/docs')
4725
4726 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4727 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4728 self.waitUntilSettled()
4729
4730 # Both builds ran: docs-draft-test + docs-draft-test2
4731 self.assertEqual(len(self.history), 2)
4732
4733 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004734 for build in self.history:
4735 if build.name == 'docs-draft-test':
4736 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004737 elif build.name == 'docs-draft-test2':
4738 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004739
4740 # Two msgs: 'Starting...' + results
4741 self.assertEqual(len(self.smtp_messages), 2)
4742 body = self.smtp_messages[1]['body'].splitlines()
4743 self.assertEqual('Build succeeded.', body[0])
4744
4745 self.assertIn(
4746 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4747 'docs-draft-test/%s/publish-docs/' % uuid,
4748 body[2])
4749
Paul Belanger174a8272017-03-14 13:20:10 -04004750 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004751 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004752 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4753 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004754 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004755 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004756
4757
Adam Gandelman4da00f62016-12-09 15:47:33 -08004758class TestSchedulerMerges(ZuulTestCase):
4759 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004760
4761 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004762 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004763 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004764 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004765 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4766 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4767 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4768 A.addApproval('code-review', 2)
4769 B.addApproval('code-review', 2)
4770 C.addApproval('code-review', 2)
4771 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4772 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4773 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4774 self.waitUntilSettled()
4775
4776 build = self.builds[-1]
4777 ref = self.getParameter(build, 'ZUUL_REF')
4778
James E. Blair2a535672017-04-27 12:03:15 -07004779 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4780 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004781 repo = git.Repo(path)
4782 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4783 repo_messages.reverse()
4784
Paul Belanger174a8272017-03-14 13:20:10 -04004785 self.executor_server.hold_jobs_in_build = False
4786 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004787 self.waitUntilSettled()
4788
4789 return repo_messages
4790
4791 def _test_merge(self, mode):
4792 us_path = os.path.join(
4793 self.upstream_root, 'org/project-%s' % mode)
4794 expected_messages = [
4795 'initial commit',
4796 'add content from fixture',
4797 # the intermediate commits order is nondeterministic
4798 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4799 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4800 ]
4801 result = self._test_project_merge_mode(mode)
4802 self.assertEqual(result[:2], expected_messages[:2])
4803 self.assertEqual(result[-2:], expected_messages[-2:])
4804
4805 def test_project_merge_mode_merge(self):
4806 self._test_merge('merge')
4807
4808 def test_project_merge_mode_merge_resolve(self):
4809 self._test_merge('merge-resolve')
4810
4811 def test_project_merge_mode_cherrypick(self):
4812 expected_messages = [
4813 'initial commit',
4814 'add content from fixture',
4815 'A-1',
4816 'B-1',
4817 'C-1']
4818 result = self._test_project_merge_mode('cherry-pick')
4819 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004820
4821 def test_merge_branch(self):
4822 "Test that the right commits are on alternate branches"
4823 self.create_branch('org/project-merge-branches', 'mp')
4824
Paul Belanger174a8272017-03-14 13:20:10 -04004825 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004826 A = self.fake_gerrit.addFakeChange(
4827 'org/project-merge-branches', 'mp', 'A')
4828 B = self.fake_gerrit.addFakeChange(
4829 'org/project-merge-branches', 'mp', 'B')
4830 C = self.fake_gerrit.addFakeChange(
4831 'org/project-merge-branches', 'mp', 'C')
4832 A.addApproval('code-review', 2)
4833 B.addApproval('code-review', 2)
4834 C.addApproval('code-review', 2)
4835 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4836 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4837 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4838 self.waitUntilSettled()
4839
Paul Belanger174a8272017-03-14 13:20:10 -04004840 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004841 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004842 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004843 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004844 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004845 self.waitUntilSettled()
4846
4847 build = self.builds[-1]
4848 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4849 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004850 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4851 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004852 repo = git.Repo(path)
4853
4854 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4855 repo_messages.reverse()
4856 correct_messages = [
4857 'initial commit',
4858 'add content from fixture',
4859 'mp commit',
4860 'A-1', 'B-1', 'C-1']
4861 self.assertEqual(repo_messages, correct_messages)
4862
Paul Belanger174a8272017-03-14 13:20:10 -04004863 self.executor_server.hold_jobs_in_build = False
4864 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004865 self.waitUntilSettled()
4866
4867 def test_merge_multi_branch(self):
4868 "Test that dependent changes on multiple branches are merged"
4869 self.create_branch('org/project-merge-branches', 'mp')
4870
Paul Belanger174a8272017-03-14 13:20:10 -04004871 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004872 A = self.fake_gerrit.addFakeChange(
4873 'org/project-merge-branches', 'master', 'A')
4874 B = self.fake_gerrit.addFakeChange(
4875 'org/project-merge-branches', 'mp', 'B')
4876 C = self.fake_gerrit.addFakeChange(
4877 'org/project-merge-branches', 'master', 'C')
4878 A.addApproval('code-review', 2)
4879 B.addApproval('code-review', 2)
4880 C.addApproval('code-review', 2)
4881 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4882 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4883 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4884 self.waitUntilSettled()
4885
4886 job_A = None
4887 for job in self.builds:
4888 if 'project-merge' in job.name:
4889 job_A = job
4890 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4891 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4892 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4893 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4894
James E. Blair2a535672017-04-27 12:03:15 -07004895 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4896 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004897 repo = git.Repo(path)
4898 repo_messages = [c.message.strip()
4899 for c in repo.iter_commits(ref_A)]
4900 repo_messages.reverse()
4901 correct_messages = [
4902 'initial commit', 'add content from fixture', 'A-1']
4903 self.assertEqual(repo_messages, correct_messages)
4904
Paul Belanger174a8272017-03-14 13:20:10 -04004905 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004906 self.waitUntilSettled()
4907
4908 job_B = None
4909 for job in self.builds:
4910 if 'project-merge' in job.name:
4911 job_B = job
4912 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4913 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4914 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4915 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4916
James E. Blair2a535672017-04-27 12:03:15 -07004917 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4918 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004919 repo = git.Repo(path)
4920 repo_messages = [c.message.strip()
4921 for c in repo.iter_commits(ref_B)]
4922 repo_messages.reverse()
4923 correct_messages = [
4924 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4925 self.assertEqual(repo_messages, correct_messages)
4926
Paul Belanger174a8272017-03-14 13:20:10 -04004927 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004928 self.waitUntilSettled()
4929
4930 job_C = None
4931 for job in self.builds:
4932 if 'project-merge' in job.name:
4933 job_C = job
4934 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4935 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4936 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4937 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004938 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4939 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004940 repo = git.Repo(path)
4941 repo_messages = [c.message.strip()
4942 for c in repo.iter_commits(ref_C)]
4943
4944 repo_messages.reverse()
4945 correct_messages = [
4946 'initial commit', 'add content from fixture',
4947 'A-1', 'C-1']
4948 # Ensure the right commits are in the history for this ref
4949 self.assertEqual(repo_messages, correct_messages)
4950
Paul Belanger174a8272017-03-14 13:20:10 -04004951 self.executor_server.hold_jobs_in_build = False
4952 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004953 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004954
4955
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004956class TestSemaphore(ZuulTestCase):
4957 tenant_config_file = 'config/semaphore/main.yaml'
4958
4959 def test_semaphore_one(self):
4960 "Test semaphores with max=1 (mutex)"
4961 tenant = self.sched.abide.tenants.get('tenant-one')
4962
4963 self.executor_server.hold_jobs_in_build = True
4964
4965 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4966 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4967 self.assertFalse('test-semaphore' in
4968 tenant.semaphore_handler.semaphores)
4969
4970 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4971 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4972 self.waitUntilSettled()
4973
4974 self.assertEqual(len(self.builds), 3)
4975 self.assertEqual(self.builds[0].name, 'project-test1')
4976 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4977 self.assertEqual(self.builds[2].name, 'project-test1')
4978
4979 self.executor_server.release('semaphore-one-test1')
4980 self.waitUntilSettled()
4981
4982 self.assertEqual(len(self.builds), 3)
4983 self.assertEqual(self.builds[0].name, 'project-test1')
4984 self.assertEqual(self.builds[1].name, 'project-test1')
4985 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4986 self.assertTrue('test-semaphore' in
4987 tenant.semaphore_handler.semaphores)
4988
4989 self.executor_server.release('semaphore-one-test2')
4990 self.waitUntilSettled()
4991
4992 self.assertEqual(len(self.builds), 3)
4993 self.assertEqual(self.builds[0].name, 'project-test1')
4994 self.assertEqual(self.builds[1].name, 'project-test1')
4995 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4996 self.assertTrue('test-semaphore' in
4997 tenant.semaphore_handler.semaphores)
4998
4999 self.executor_server.release('semaphore-one-test1')
5000 self.waitUntilSettled()
5001
5002 self.assertEqual(len(self.builds), 3)
5003 self.assertEqual(self.builds[0].name, 'project-test1')
5004 self.assertEqual(self.builds[1].name, 'project-test1')
5005 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5006 self.assertTrue('test-semaphore' in
5007 tenant.semaphore_handler.semaphores)
5008
5009 self.executor_server.release('semaphore-one-test2')
5010 self.waitUntilSettled()
5011
5012 self.assertEqual(len(self.builds), 2)
5013 self.assertEqual(self.builds[0].name, 'project-test1')
5014 self.assertEqual(self.builds[1].name, 'project-test1')
5015 self.assertFalse('test-semaphore' in
5016 tenant.semaphore_handler.semaphores)
5017
5018 self.executor_server.hold_jobs_in_build = False
5019 self.executor_server.release()
5020
5021 self.waitUntilSettled()
5022 self.assertEqual(len(self.builds), 0)
5023
5024 self.assertEqual(A.reported, 1)
5025 self.assertEqual(B.reported, 1)
5026 self.assertFalse('test-semaphore' in
5027 tenant.semaphore_handler.semaphores)
5028
5029 def test_semaphore_two(self):
5030 "Test semaphores with max>1"
5031 tenant = self.sched.abide.tenants.get('tenant-one')
5032
5033 self.executor_server.hold_jobs_in_build = True
5034 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5035 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5036 self.assertFalse('test-semaphore-two' in
5037 tenant.semaphore_handler.semaphores)
5038
5039 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5040 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5041 self.waitUntilSettled()
5042
5043 self.assertEqual(len(self.builds), 4)
5044 self.assertEqual(self.builds[0].name, 'project-test1')
5045 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5046 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5047 self.assertEqual(self.builds[3].name, 'project-test1')
5048 self.assertTrue('test-semaphore-two' in
5049 tenant.semaphore_handler.semaphores)
5050 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5051 'test-semaphore-two', [])), 2)
5052
5053 self.executor_server.release('semaphore-two-test1')
5054 self.waitUntilSettled()
5055
5056 self.assertEqual(len(self.builds), 4)
5057 self.assertEqual(self.builds[0].name, 'project-test1')
5058 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5059 self.assertEqual(self.builds[2].name, 'project-test1')
5060 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5061 self.assertTrue('test-semaphore-two' in
5062 tenant.semaphore_handler.semaphores)
5063 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5064 'test-semaphore-two', [])), 2)
5065
5066 self.executor_server.release('semaphore-two-test2')
5067 self.waitUntilSettled()
5068
5069 self.assertEqual(len(self.builds), 4)
5070 self.assertEqual(self.builds[0].name, 'project-test1')
5071 self.assertEqual(self.builds[1].name, 'project-test1')
5072 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5073 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5074 self.assertTrue('test-semaphore-two' in
5075 tenant.semaphore_handler.semaphores)
5076 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5077 'test-semaphore-two', [])), 2)
5078
5079 self.executor_server.release('semaphore-two-test1')
5080 self.waitUntilSettled()
5081
5082 self.assertEqual(len(self.builds), 3)
5083 self.assertEqual(self.builds[0].name, 'project-test1')
5084 self.assertEqual(self.builds[1].name, 'project-test1')
5085 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5086 self.assertTrue('test-semaphore-two' in
5087 tenant.semaphore_handler.semaphores)
5088 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5089 'test-semaphore-two', [])), 1)
5090
5091 self.executor_server.release('semaphore-two-test2')
5092 self.waitUntilSettled()
5093
5094 self.assertEqual(len(self.builds), 2)
5095 self.assertEqual(self.builds[0].name, 'project-test1')
5096 self.assertEqual(self.builds[1].name, 'project-test1')
5097 self.assertFalse('test-semaphore-two' in
5098 tenant.semaphore_handler.semaphores)
5099
5100 self.executor_server.hold_jobs_in_build = False
5101 self.executor_server.release()
5102
5103 self.waitUntilSettled()
5104 self.assertEqual(len(self.builds), 0)
5105
5106 self.assertEqual(A.reported, 1)
5107 self.assertEqual(B.reported, 1)
5108
5109 def test_semaphore_abandon(self):
5110 "Test abandon with job semaphores"
5111 self.executor_server.hold_jobs_in_build = True
5112 tenant = self.sched.abide.tenants.get('tenant-one')
5113 check_pipeline = tenant.layout.pipelines['check']
5114
5115 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5116 self.assertFalse('test-semaphore' in
5117 tenant.semaphore_handler.semaphores)
5118
5119 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5120 self.waitUntilSettled()
5121
5122 self.assertTrue('test-semaphore' in
5123 tenant.semaphore_handler.semaphores)
5124
5125 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5126 self.waitUntilSettled()
5127
5128 # The check pipeline should be empty
5129 items = check_pipeline.getAllItems()
5130 self.assertEqual(len(items), 0)
5131
5132 # The semaphore should be released
5133 self.assertFalse('test-semaphore' in
5134 tenant.semaphore_handler.semaphores)
5135
5136 self.executor_server.hold_jobs_in_build = False
5137 self.executor_server.release()
5138 self.waitUntilSettled()
5139
Tobias Henkel0f714002017-06-30 23:30:52 +02005140 def test_semaphore_new_patchset(self):
5141 "Test new patchset with job semaphores"
5142 self.executor_server.hold_jobs_in_build = True
5143 tenant = self.sched.abide.tenants.get('tenant-one')
5144 check_pipeline = tenant.layout.pipelines['check']
5145
5146 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5147 self.assertFalse('test-semaphore' in
5148 tenant.semaphore_handler.semaphores)
5149
5150 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5151 self.waitUntilSettled()
5152
5153 self.assertTrue('test-semaphore' in
5154 tenant.semaphore_handler.semaphores)
5155 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5156 self.assertEqual(len(semaphore), 1)
5157
5158 A.addPatchset()
5159 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5160 self.waitUntilSettled()
5161
5162 self.assertTrue('test-semaphore' in
5163 tenant.semaphore_handler.semaphores)
5164 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5165 self.assertEqual(len(semaphore), 1)
5166
5167 items = check_pipeline.getAllItems()
5168 self.assertEqual(items[0].change.number, '1')
5169 self.assertEqual(items[0].change.patchset, '2')
5170 self.assertTrue(items[0].live)
5171
5172 self.executor_server.hold_jobs_in_build = False
5173 self.executor_server.release()
5174 self.waitUntilSettled()
5175
5176 # The semaphore should be released
5177 self.assertFalse('test-semaphore' in
5178 tenant.semaphore_handler.semaphores)
5179
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005180 def test_semaphore_reconfigure(self):
5181 "Test reconfigure with job semaphores"
5182 self.executor_server.hold_jobs_in_build = True
5183 tenant = self.sched.abide.tenants.get('tenant-one')
5184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5185 self.assertFalse('test-semaphore' in
5186 tenant.semaphore_handler.semaphores)
5187
5188 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5189 self.waitUntilSettled()
5190
5191 self.assertTrue('test-semaphore' in
5192 tenant.semaphore_handler.semaphores)
5193
5194 # reconfigure without layout change
5195 self.sched.reconfigure(self.config)
5196 self.waitUntilSettled()
5197 tenant = self.sched.abide.tenants.get('tenant-one')
5198
5199 # semaphore still must be held
5200 self.assertTrue('test-semaphore' in
5201 tenant.semaphore_handler.semaphores)
5202
5203 self.commitConfigUpdate(
5204 'common-config',
5205 'config/semaphore/zuul-reconfiguration.yaml')
5206 self.sched.reconfigure(self.config)
5207 self.waitUntilSettled()
5208 tenant = self.sched.abide.tenants.get('tenant-one')
5209
5210 self.executor_server.release('project-test1')
5211 self.waitUntilSettled()
5212
5213 # There should be no builds anymore
5214 self.assertEqual(len(self.builds), 0)
5215
5216 # The semaphore should be released
5217 self.assertFalse('test-semaphore' in
5218 tenant.semaphore_handler.semaphores)
5219
5220
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005221class TestSemaphoreMultiTenant(ZuulTestCase):
5222 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5223
5224 def test_semaphore_tenant_isolation(self):
5225 "Test semaphores in multiple tenants"
5226
5227 self.waitUntilSettled()
5228 tenant_one = self.sched.abide.tenants.get('tenant-one')
5229 tenant_two = self.sched.abide.tenants.get('tenant-two')
5230
5231 self.executor_server.hold_jobs_in_build = True
5232 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5233 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5234 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5235 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5236 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5237 self.assertFalse('test-semaphore' in
5238 tenant_one.semaphore_handler.semaphores)
5239 self.assertFalse('test-semaphore' in
5240 tenant_two.semaphore_handler.semaphores)
5241
5242 # add patches to project1 of tenant-one
5243 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5244 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5245 self.waitUntilSettled()
5246
5247 # one build of project1-test1 must run
5248 # semaphore of tenant-one must be acquired once
5249 # semaphore of tenant-two must not be acquired
5250 self.assertEqual(len(self.builds), 1)
5251 self.assertEqual(self.builds[0].name, 'project1-test1')
5252 self.assertTrue('test-semaphore' in
5253 tenant_one.semaphore_handler.semaphores)
5254 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5255 'test-semaphore', [])), 1)
5256 self.assertFalse('test-semaphore' in
5257 tenant_two.semaphore_handler.semaphores)
5258
5259 # add patches to project2 of tenant-two
5260 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5261 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5262 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5263 self.waitUntilSettled()
5264
5265 # one build of project1-test1 must run
5266 # two builds of project2-test1 must run
5267 # semaphore of tenant-one must be acquired once
5268 # semaphore of tenant-two must be acquired twice
5269 self.assertEqual(len(self.builds), 3)
5270 self.assertEqual(self.builds[0].name, 'project1-test1')
5271 self.assertEqual(self.builds[1].name, 'project2-test1')
5272 self.assertEqual(self.builds[2].name, 'project2-test1')
5273 self.assertTrue('test-semaphore' in
5274 tenant_one.semaphore_handler.semaphores)
5275 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5276 'test-semaphore', [])), 1)
5277 self.assertTrue('test-semaphore' in
5278 tenant_two.semaphore_handler.semaphores)
5279 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5280 'test-semaphore', [])), 2)
5281
5282 self.executor_server.release('project1-test1')
5283 self.waitUntilSettled()
5284
5285 # one build of project1-test1 must run
5286 # two builds of project2-test1 must run
5287 # semaphore of tenant-one must be acquired once
5288 # semaphore of tenant-two must be acquired twice
5289 self.assertEqual(len(self.builds), 3)
5290 self.assertEqual(self.builds[0].name, 'project2-test1')
5291 self.assertEqual(self.builds[1].name, 'project2-test1')
5292 self.assertEqual(self.builds[2].name, 'project1-test1')
5293 self.assertTrue('test-semaphore' in
5294 tenant_one.semaphore_handler.semaphores)
5295 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5296 'test-semaphore', [])), 1)
5297 self.assertTrue('test-semaphore' in
5298 tenant_two.semaphore_handler.semaphores)
5299 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5300 'test-semaphore', [])), 2)
5301
5302 self.executor_server.release('project2-test1')
5303 self.waitUntilSettled()
5304
5305 # one build of project1-test1 must run
5306 # one build of project2-test1 must run
5307 # semaphore of tenant-one must be acquired once
5308 # semaphore of tenant-two must be acquired once
5309 self.assertEqual(len(self.builds), 2)
5310 self.assertTrue('test-semaphore' in
5311 tenant_one.semaphore_handler.semaphores)
5312 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5313 'test-semaphore', [])), 1)
5314 self.assertTrue('test-semaphore' in
5315 tenant_two.semaphore_handler.semaphores)
5316 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5317 'test-semaphore', [])), 1)
5318
5319 self.executor_server.hold_jobs_in_build = False
5320 self.executor_server.release()
5321
5322 self.waitUntilSettled()
5323
5324 # no build must run
5325 # semaphore of tenant-one must not be acquired
5326 # semaphore of tenant-two must not be acquired
5327 self.assertEqual(len(self.builds), 0)
5328 self.assertFalse('test-semaphore' in
5329 tenant_one.semaphore_handler.semaphores)
5330 self.assertFalse('test-semaphore' in
5331 tenant_two.semaphore_handler.semaphores)
5332
5333 self.assertEqual(A.reported, 1)
5334 self.assertEqual(B.reported, 1)
5335
5336
5337class TestSemaphoreInRepo(ZuulTestCase):
5338 tenant_config_file = 'config/in-repo/main.yaml'
5339
5340 def test_semaphore_in_repo(self):
5341 "Test semaphores in repo config"
5342
5343 # This tests dynamic semaphore handling in project repos. The semaphore
5344 # max value should not be evaluated dynamically but must be updated
5345 # after the change lands.
5346
5347 self.waitUntilSettled()
5348 tenant = self.sched.abide.tenants.get('tenant-one')
5349
5350 in_repo_conf = textwrap.dedent(
5351 """
5352 - job:
5353 name: project-test2
5354 semaphore: test-semaphore
5355
5356 - project:
5357 name: org/project
5358 tenant-one-gate:
5359 jobs:
5360 - project-test2
5361
5362 # the max value in dynamic layout must be ignored
5363 - semaphore:
5364 name: test-semaphore
5365 max: 2
5366 """)
5367
5368 in_repo_playbook = textwrap.dedent(
5369 """
5370 - hosts: all
5371 tasks: []
5372 """)
5373
5374 file_dict = {'.zuul.yaml': in_repo_conf,
5375 'playbooks/project-test2.yaml': in_repo_playbook}
5376 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5377 files=file_dict)
5378 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5379 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5380 B.setDependsOn(A, 1)
5381 C.setDependsOn(A, 1)
5382
5383 self.executor_server.hold_jobs_in_build = True
5384
5385 A.addApproval('code-review', 2)
5386 B.addApproval('code-review', 2)
5387 C.addApproval('code-review', 2)
5388 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5389 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5390 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5391 self.waitUntilSettled()
5392
5393 # check that the layout in a queue item still has max value of 1
5394 # for test-semaphore
5395 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5396 queue = None
5397 for queue_candidate in pipeline.queues:
5398 if queue_candidate.name == 'org/project':
5399 queue = queue_candidate
5400 break
5401 queue_item = queue.queue[0]
5402 item_dynamic_layout = queue_item.current_build_set.layout
5403 dynamic_test_semaphore = \
5404 item_dynamic_layout.semaphores.get('test-semaphore')
5405 self.assertEqual(dynamic_test_semaphore.max, 1)
5406
5407 # one build must be in queue, one semaphores acquired
5408 self.assertEqual(len(self.builds), 1)
5409 self.assertEqual(self.builds[0].name, 'project-test2')
5410 self.assertTrue('test-semaphore' in
5411 tenant.semaphore_handler.semaphores)
5412 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5413 'test-semaphore', [])), 1)
5414
5415 self.executor_server.release('project-test2')
5416 self.waitUntilSettled()
5417
5418 # change A must be merged
5419 self.assertEqual(A.data['status'], 'MERGED')
5420 self.assertEqual(A.reported, 2)
5421
5422 # send change-merged event as the gerrit mock doesn't send it
5423 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5424 self.waitUntilSettled()
5425
5426 # now that change A was merged, the new semaphore max must be effective
5427 tenant = self.sched.abide.tenants.get('tenant-one')
5428 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5429
5430 # two builds must be in queue, two semaphores acquired
5431 self.assertEqual(len(self.builds), 2)
5432 self.assertEqual(self.builds[0].name, 'project-test2')
5433 self.assertEqual(self.builds[1].name, 'project-test2')
5434 self.assertTrue('test-semaphore' in
5435 tenant.semaphore_handler.semaphores)
5436 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5437 'test-semaphore', [])), 2)
5438
5439 self.executor_server.release('project-test2')
5440 self.waitUntilSettled()
5441
5442 self.assertEqual(len(self.builds), 0)
5443 self.assertFalse('test-semaphore' in
5444 tenant.semaphore_handler.semaphores)
5445
5446 self.executor_server.hold_jobs_in_build = False
5447 self.executor_server.release()
5448
5449 self.waitUntilSettled()
5450 self.assertEqual(len(self.builds), 0)
5451
5452 self.assertEqual(A.reported, 2)
5453 self.assertEqual(B.reported, 2)
5454 self.assertEqual(C.reported, 2)