blob: 1e0affc4fabc23967d6ec48f55b86fe6393c7ffa [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:
James E. Blaire3db2952017-07-21 15:03:36 -07001286 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001287 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
James E. Blair21037782017-07-19 11:56:55 -07001837 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001838
1839 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1840 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1841 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001842 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001843
1844 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1845 self.waitUntilSettled()
1846
James E. Blair21037782017-07-19 11:56:55 -07001847 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001848
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
Clint Byrum3343e3e2016-11-15 16:05:03 -08002036 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002037 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002038 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002039 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002040 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002041 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002042 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002043 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002044 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002045 d_build = x
2046 if a_build and b_build and c_build and d_build:
2047 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002048
Clint Byrum3343e3e2016-11-15 16:05:03 -08002049 # should have a, not b, and should not be in project2
2050 self.assertTrue(a_build.hasChanges(A))
2051 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002052
Clint Byrum3343e3e2016-11-15 16:05:03 -08002053 # should have a and b, and should not be in project2
2054 self.assertTrue(b_build.hasChanges(A, B))
2055 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002056
Clint Byrum3343e3e2016-11-15 16:05:03 -08002057 # should have a and b in 1, c in 2
2058 self.assertTrue(c_build.hasChanges(A, B, C))
2059 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002060
Clint Byrum3343e3e2016-11-15 16:05:03 -08002061 # should have a and b in 1, c and d in 2
2062 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002063
Paul Belanger174a8272017-03-14 13:20:10 -04002064 self.executor_server.hold_jobs_in_build = False
2065 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002066 self.waitUntilSettled()
2067
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002068 self.assertEqual(A.data['status'], 'MERGED')
2069 self.assertEqual(A.reported, 2)
2070 self.assertEqual(B.data['status'], 'MERGED')
2071 self.assertEqual(B.reported, 2)
2072 self.assertEqual(C.data['status'], 'MERGED')
2073 self.assertEqual(C.reported, 2)
2074 self.assertEqual(D.data['status'], 'MERGED')
2075 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002076
James E. Blair4a28a882013-08-23 15:17:33 -07002077 def test_rerun_on_error(self):
2078 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002079 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002080 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002081 A.addApproval('code-review', 2)
2082 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002083 self.waitUntilSettled()
2084
James E. Blair412fba82017-01-26 15:00:50 -08002085 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002086 self.executor_server.hold_jobs_in_build = False
2087 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002088 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002089 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002090 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2091
James E. Blair412e5582013-04-22 15:50:12 -07002092 def test_statsd(self):
2093 "Test each of the statsd methods used in the scheduler"
2094 import extras
2095 statsd = extras.try_import('statsd.statsd')
2096 statsd.incr('test-incr')
2097 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002098 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002099 self.assertReportedStat('test-incr', '1|c')
2100 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002101 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002102
James E. Blairdad52252014-02-07 16:59:17 -08002103 def test_stuck_job_cleanup(self):
2104 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002105
2106 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002107 self.gearman_server.hold_jobs_in_queue = True
2108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002109 A.addApproval('code-review', 2)
2110 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002111 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002112 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002113 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2114
James E. Blair31cb3e82017-04-20 13:08:56 -07002115 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002116 self.sched.reconfigure(self.config)
2117 self.waitUntilSettled()
2118
James E. Blair18c64442014-03-18 10:14:45 -07002119 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002120 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002121 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002122 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2123 self.assertTrue(self.sched._areAllBuildsComplete())
2124
2125 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002126 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002127 self.assertEqual(self.history[0].result, 'SUCCESS')
2128
James E. Blair879dafb2015-07-17 14:04:49 -07002129 def test_file_head(self):
2130 # This is a regression test for an observed bug. A change
2131 # with a file named "HEAD" in the root directory of the repo
2132 # was processed by a merger. It then was unable to reset the
2133 # repo because of:
2134 # GitCommandError: 'git reset --hard HEAD' returned
2135 # with exit code 128
2136 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2137 # and filename
2138 # Use '--' to separate filenames from revisions'
2139
2140 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002141 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002142 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2143
2144 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2145 self.waitUntilSettled()
2146
2147 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2148 self.waitUntilSettled()
2149
2150 self.assertIn('Build succeeded', A.messages[0])
2151 self.assertIn('Build succeeded', B.messages[0])
2152
James E. Blair70c71582013-03-06 08:50:50 -08002153 def test_file_jobs(self):
2154 "Test that file jobs run only when appropriate"
2155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002156 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002157 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002158 A.addApproval('code-review', 2)
2159 B.addApproval('code-review', 2)
2160 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2161 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002162 self.waitUntilSettled()
2163
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002164 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002165 if x.name == 'project-testfile']
2166
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002167 self.assertEqual(len(testfile_jobs), 1)
2168 self.assertEqual(testfile_jobs[0].changes, '1,2')
2169 self.assertEqual(A.data['status'], 'MERGED')
2170 self.assertEqual(A.reported, 2)
2171 self.assertEqual(B.data['status'], 'MERGED')
2172 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002173
Clint Byrum3ee39f32016-11-17 23:45:07 -08002174 def _test_irrelevant_files_jobs(self, should_skip):
2175 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002176 if should_skip:
2177 files = {'ignoreme': 'ignored\n'}
2178 else:
2179 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002180
2181 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002182 'master',
2183 'test irrelevant-files',
2184 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002185 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2186 self.waitUntilSettled()
2187
2188 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002189 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002190
2191 if should_skip:
2192 self.assertEqual([], tested_change_ids)
2193 else:
2194 self.assertIn(change.data['number'], tested_change_ids)
2195
James E. Blaircc324192017-04-19 14:36:08 -07002196 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002197 def test_irrelevant_files_match_skips_job(self):
2198 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002199
James E. Blaircc324192017-04-19 14:36:08 -07002200 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002201 def test_irrelevant_files_no_match_runs_job(self):
2202 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002203
James E. Blaircc324192017-04-19 14:36:08 -07002204 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002205 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002206 files = {'ignoreme': 'ignored\n'}
2207
2208 change = self.fake_gerrit.addFakeChange('org/project',
2209 'master',
2210 'test irrelevant-files',
2211 files=files)
2212 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2213 self.waitUntilSettled()
2214
2215 run_jobs = set([build.name for build in self.history])
2216
2217 self.assertEqual(set(['project-test-nomatch-starts-empty',
2218 'project-test-nomatch-starts-full']), run_jobs)
2219
James E. Blairc8a1e052014-02-25 09:29:26 -08002220 def test_queue_names(self):
2221 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002222 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002223 (trusted, project1) = tenant.getProject('org/project1')
2224 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002225 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2226 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2227 self.assertEqual(q1.name, 'integrated')
2228 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002229
James E. Blair64ed6f22013-07-10 14:07:23 -07002230 def test_queue_precedence(self):
2231 "Test that queue precedence works"
2232
2233 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002234 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002235 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2236 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002237 A.addApproval('code-review', 2)
2238 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002239
2240 self.waitUntilSettled()
2241 self.gearman_server.hold_jobs_in_queue = False
2242 self.gearman_server.release()
2243 self.waitUntilSettled()
2244
James E. Blair8de58bd2013-07-18 16:23:33 -07002245 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002246 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002247 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002248 self.waitUntilSettled()
2249
James E. Blair64ed6f22013-07-10 14:07:23 -07002250 self.log.debug(self.history)
2251 self.assertEqual(self.history[0].pipeline, 'gate')
2252 self.assertEqual(self.history[1].pipeline, 'check')
2253 self.assertEqual(self.history[2].pipeline, 'gate')
2254 self.assertEqual(self.history[3].pipeline, 'gate')
2255 self.assertEqual(self.history[4].pipeline, 'check')
2256 self.assertEqual(self.history[5].pipeline, 'check')
2257
Clark Boylana5edbe42014-06-03 16:39:10 -07002258 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002259 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002260 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002262 A.addApproval('code-review', 2)
2263 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002264 self.waitUntilSettled()
2265
Paul Belanger174a8272017-03-14 13:20:10 -04002266 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002267 self.waitUntilSettled()
2268
James E. Blair1843a552013-07-03 14:19:52 -07002269 port = self.webapp.server.socket.getsockname()[1]
2270
Adam Gandelman77a12c72017-02-03 17:43:43 -08002271 req = urllib.request.Request(
2272 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002273 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002274 headers = f.info()
2275 self.assertIn('Content-Length', headers)
2276 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002277 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2278 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002279 self.assertIn('Access-Control-Allow-Origin', headers)
2280 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002281 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002282 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002283 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002284
Paul Belanger174a8272017-03-14 13:20:10 -04002285 self.executor_server.hold_jobs_in_build = False
2286 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002287 self.waitUntilSettled()
2288
2289 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002290 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002291 for p in data['pipelines']:
2292 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002293 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002294 self.assertEqual(q['window'], 20)
2295 else:
2296 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002297 for head in q['heads']:
2298 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002299 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002300 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002301 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002302 status_jobs.append(job)
2303 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002304 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002305 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002306 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2307 uuid=status_jobs[0]['uuid']),
2308 status_jobs[0]['url'])
2309 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002310 'finger://{hostname}/{uuid}'.format(
2311 hostname=self.executor_server.hostname,
2312 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002313 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002314 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002315 self.assertEqual(
2316 'finger://{hostname}/{uuid}'.format(
2317 hostname=self.executor_server.hostname,
2318 uuid=status_jobs[0]['uuid']),
2319 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002320 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002321 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002322 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2323 uuid=status_jobs[1]['uuid']),
2324 status_jobs[1]['url'])
2325 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002326 'finger://{hostname}/{uuid}'.format(
2327 hostname=self.executor_server.hostname,
2328 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002329 status_jobs[1]['finger_url'])
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[1]['uuid']),
2334 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002335
2336 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002337 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002338 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2339 uuid=status_jobs[2]['uuid']),
2340 status_jobs[2]['url'])
2341 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002342 'finger://{hostname}/{uuid}'.format(
2343 hostname=self.executor_server.hostname,
2344 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002345 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002346 self.assertEqual(
2347 'finger://{hostname}/{uuid}'.format(
2348 hostname=self.executor_server.hostname,
2349 uuid=status_jobs[2]['uuid']),
2350 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002351
Tobias Henkel65639f82017-07-10 10:25:42 +02002352 # check job dependencies
2353 self.assertIsNotNone(status_jobs[0]['dependencies'])
2354 self.assertIsNotNone(status_jobs[1]['dependencies'])
2355 self.assertIsNotNone(status_jobs[2]['dependencies'])
2356 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2357 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2358 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2359 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2360 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2361
James E. Blaircdccd972013-07-01 12:10:22 -07002362 def test_live_reconfiguration(self):
2363 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002364 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002366 A.addApproval('code-review', 2)
2367 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002368 self.waitUntilSettled()
2369
2370 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002371 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002372
Paul Belanger174a8272017-03-14 13:20:10 -04002373 self.executor_server.hold_jobs_in_build = False
2374 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002375 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002376 self.assertEqual(self.getJobFromHistory('project-merge').result,
2377 'SUCCESS')
2378 self.assertEqual(self.getJobFromHistory('project-test1').result,
2379 'SUCCESS')
2380 self.assertEqual(self.getJobFromHistory('project-test2').result,
2381 'SUCCESS')
2382 self.assertEqual(A.data['status'], 'MERGED')
2383 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002384
James E. Blair6bc782d2015-07-17 16:20:21 -07002385 def test_live_reconfiguration_merge_conflict(self):
2386 # A real-world bug: a change in a gate queue has a merge
2387 # conflict and a job is added to its project while it's
2388 # sitting in the queue. The job gets added to the change and
2389 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002390 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002391
2392 # This change is fine. It's here to stop the queue long
2393 # enough for the next change to be subject to the
2394 # reconfiguration, as well as to provide a conflict for the
2395 # next change. This change will succeed and merge.
2396 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002397 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002398 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002399
2400 # This change will be in merge conflict. During the
2401 # reconfiguration, we will add a job. We want to make sure
2402 # that doesn't cause it to get stuck.
2403 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002404 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002405 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002406
James E. Blair8b5408c2016-08-08 15:37:46 -07002407 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2408 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002409
2410 self.waitUntilSettled()
2411
2412 # No jobs have run yet
2413 self.assertEqual(A.data['status'], 'NEW')
2414 self.assertEqual(A.reported, 1)
2415 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002416 self.assertEqual(len(self.history), 0)
2417
2418 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002419 self.commitConfigUpdate('common-config',
2420 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002421 self.sched.reconfigure(self.config)
2422 self.waitUntilSettled()
2423
Paul Belanger174a8272017-03-14 13:20:10 -04002424 self.executor_server.hold_jobs_in_build = False
2425 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002426 self.waitUntilSettled()
2427
2428 self.assertEqual(A.data['status'], 'MERGED')
2429 self.assertEqual(A.reported, 2)
2430 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002431 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002432 self.assertEqual(self.getJobFromHistory('project-merge').result,
2433 'SUCCESS')
2434 self.assertEqual(self.getJobFromHistory('project-test1').result,
2435 'SUCCESS')
2436 self.assertEqual(self.getJobFromHistory('project-test2').result,
2437 'SUCCESS')
2438 self.assertEqual(self.getJobFromHistory('project-test3').result,
2439 'SUCCESS')
2440 self.assertEqual(len(self.history), 4)
2441
James E. Blair400e8fd2015-07-30 17:44:45 -07002442 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002443 # An extrapolation of test_live_reconfiguration_merge_conflict
2444 # that tests a job added to a job tree with a failed root does
2445 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002446 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002447
2448 # This change is fine. It's here to stop the queue long
2449 # enough for the next change to be subject to the
2450 # reconfiguration. This change will succeed and merge.
2451 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002452 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002453 A.addApproval('code-review', 2)
2454 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002455 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002456 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002457 self.waitUntilSettled()
2458
2459 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002460 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002461 B.addApproval('code-review', 2)
2462 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002463 self.waitUntilSettled()
2464
Paul Belanger174a8272017-03-14 13:20:10 -04002465 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002466 self.waitUntilSettled()
2467
2468 # Both -merge jobs have run, but no others.
2469 self.assertEqual(A.data['status'], 'NEW')
2470 self.assertEqual(A.reported, 1)
2471 self.assertEqual(B.data['status'], 'NEW')
2472 self.assertEqual(B.reported, 1)
2473 self.assertEqual(self.history[0].result, 'SUCCESS')
2474 self.assertEqual(self.history[0].name, 'project-merge')
2475 self.assertEqual(self.history[1].result, 'FAILURE')
2476 self.assertEqual(self.history[1].name, 'project-merge')
2477 self.assertEqual(len(self.history), 2)
2478
2479 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002480 self.commitConfigUpdate('common-config',
2481 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002482 self.sched.reconfigure(self.config)
2483 self.waitUntilSettled()
2484
Paul Belanger174a8272017-03-14 13:20:10 -04002485 self.executor_server.hold_jobs_in_build = False
2486 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002487 self.waitUntilSettled()
2488
2489 self.assertEqual(A.data['status'], 'MERGED')
2490 self.assertEqual(A.reported, 2)
2491 self.assertEqual(B.data['status'], 'NEW')
2492 self.assertEqual(B.reported, 2)
2493 self.assertEqual(self.history[0].result, 'SUCCESS')
2494 self.assertEqual(self.history[0].name, 'project-merge')
2495 self.assertEqual(self.history[1].result, 'FAILURE')
2496 self.assertEqual(self.history[1].name, 'project-merge')
2497 self.assertEqual(self.history[2].result, 'SUCCESS')
2498 self.assertEqual(self.history[3].result, 'SUCCESS')
2499 self.assertEqual(self.history[4].result, 'SUCCESS')
2500 self.assertEqual(len(self.history), 5)
2501
James E. Blair400e8fd2015-07-30 17:44:45 -07002502 def test_live_reconfiguration_failed_job(self):
2503 # Test that a change with a removed failing job does not
2504 # disrupt reconfiguration. If a change has a failed job and
2505 # that job is removed during a reconfiguration, we observed a
2506 # bug where the code to re-set build statuses would run on
2507 # that build and raise an exception because the job no longer
2508 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002509 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002510
2511 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2512
2513 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002514 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002515
2516 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2517 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002518 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002519 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002520 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002521 self.waitUntilSettled()
2522
2523 self.assertEqual(A.data['status'], 'NEW')
2524 self.assertEqual(A.reported, 0)
2525
2526 self.assertEqual(self.getJobFromHistory('project-merge').result,
2527 'SUCCESS')
2528 self.assertEqual(self.getJobFromHistory('project-test1').result,
2529 'FAILURE')
2530 self.assertEqual(len(self.history), 2)
2531
2532 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002533 self.commitConfigUpdate('common-config',
2534 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002535 self.sched.reconfigure(self.config)
2536 self.waitUntilSettled()
2537
Paul Belanger174a8272017-03-14 13:20:10 -04002538 self.executor_server.hold_jobs_in_build = False
2539 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002540 self.waitUntilSettled()
2541
2542 self.assertEqual(self.getJobFromHistory('project-test2').result,
2543 'SUCCESS')
2544 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2545 'SUCCESS')
2546 self.assertEqual(len(self.history), 4)
2547
2548 self.assertEqual(A.data['status'], 'NEW')
2549 self.assertEqual(A.reported, 1)
2550 self.assertIn('Build succeeded', A.messages[0])
2551 # Ensure the removed job was not included in the report.
2552 self.assertNotIn('project-test1', A.messages[0])
2553
James E. Blairfe707d12015-08-05 15:18:15 -07002554 def test_live_reconfiguration_shared_queue(self):
2555 # Test that a change with a failing job which was removed from
2556 # this project but otherwise still exists in the system does
2557 # not disrupt reconfiguration.
2558
Paul Belanger174a8272017-03-14 13:20:10 -04002559 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002560
2561 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2562
Paul Belanger174a8272017-03-14 13:20:10 -04002563 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002564
2565 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2566 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002567 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002568 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002569 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002570 self.waitUntilSettled()
2571
2572 self.assertEqual(A.data['status'], 'NEW')
2573 self.assertEqual(A.reported, 0)
2574
Clint Byrum69663d42017-05-01 14:51:04 -07002575 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002576 'SUCCESS')
2577 self.assertEqual(self.getJobFromHistory(
2578 'project1-project2-integration').result, 'FAILURE')
2579 self.assertEqual(len(self.history), 2)
2580
2581 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002582 self.commitConfigUpdate(
2583 'common-config',
2584 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002585 self.sched.reconfigure(self.config)
2586 self.waitUntilSettled()
2587
Paul Belanger174a8272017-03-14 13:20:10 -04002588 self.executor_server.hold_jobs_in_build = False
2589 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002590 self.waitUntilSettled()
2591
Clint Byrum69663d42017-05-01 14:51:04 -07002592 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002593 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002594 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002595 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002596 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002597 'SUCCESS')
2598 self.assertEqual(self.getJobFromHistory(
2599 'project1-project2-integration').result, 'FAILURE')
2600 self.assertEqual(len(self.history), 4)
2601
2602 self.assertEqual(A.data['status'], 'NEW')
2603 self.assertEqual(A.reported, 1)
2604 self.assertIn('Build succeeded', A.messages[0])
2605 # Ensure the removed job was not included in the report.
2606 self.assertNotIn('project1-project2-integration', A.messages[0])
2607
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002608 def test_double_live_reconfiguration_shared_queue(self):
2609 # This was a real-world regression. A change is added to
2610 # gate; a reconfigure happens, a second change which depends
2611 # on the first is added, and a second reconfiguration happens.
2612 # Ensure that both changes merge.
2613
2614 # A failure may indicate incorrect caching or cleaning up of
2615 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002616 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002617
2618 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2619 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2620 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002621 A.addApproval('code-review', 2)
2622 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002623
2624 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002625 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002626 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002627 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002628 self.waitUntilSettled()
2629
2630 # Reconfigure (with only one change in the pipeline).
2631 self.sched.reconfigure(self.config)
2632 self.waitUntilSettled()
2633
2634 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002635 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002636 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002637 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002638 self.waitUntilSettled()
2639
2640 # Reconfigure (with both in the pipeline).
2641 self.sched.reconfigure(self.config)
2642 self.waitUntilSettled()
2643
Paul Belanger174a8272017-03-14 13:20:10 -04002644 self.executor_server.hold_jobs_in_build = False
2645 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002646 self.waitUntilSettled()
2647
2648 self.assertEqual(len(self.history), 8)
2649
2650 self.assertEqual(A.data['status'], 'MERGED')
2651 self.assertEqual(A.reported, 2)
2652 self.assertEqual(B.data['status'], 'MERGED')
2653 self.assertEqual(B.reported, 2)
2654
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002655 def test_live_reconfiguration_del_project(self):
2656 # Test project deletion from layout
2657 # while changes are enqueued
2658
Paul Belanger174a8272017-03-14 13:20:10 -04002659 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002660 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2661 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2662 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2663
2664 # A Depends-On: B
2665 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2666 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002667 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002668
2669 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2670 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2671 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002672 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002673 self.waitUntilSettled()
2674 self.assertEqual(len(self.builds), 5)
2675
2676 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002677 self.commitConfigUpdate(
2678 'common-config',
2679 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002680 self.sched.reconfigure(self.config)
2681 self.waitUntilSettled()
2682
2683 # Builds for C aborted, builds for A succeed,
2684 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002685 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002686 self.assertEqual(job_c.changes, '3,1')
2687 self.assertEqual(job_c.result, 'ABORTED')
2688
Paul Belanger174a8272017-03-14 13:20:10 -04002689 self.executor_server.hold_jobs_in_build = False
2690 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002691 self.waitUntilSettled()
2692
Clint Byrum0149ada2017-04-04 12:22:01 -07002693 self.assertEqual(
2694 self.getJobFromHistory('project-test1', 'org/project').changes,
2695 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002696
2697 self.assertEqual(A.data['status'], 'NEW')
2698 self.assertEqual(B.data['status'], 'NEW')
2699 self.assertEqual(C.data['status'], 'NEW')
2700 self.assertEqual(A.reported, 1)
2701 self.assertEqual(B.reported, 0)
2702 self.assertEqual(C.reported, 0)
2703
Clint Byrum0149ada2017-04-04 12:22:01 -07002704 tenant = self.sched.abide.tenants.get('tenant-one')
2705 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002706 self.assertIn('Build succeeded', A.messages[0])
2707
James E. Blairec056492016-07-22 09:45:56 -07002708 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002709 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002710 self.updateConfigLayout(
2711 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002712 self.sched.reconfigure(self.config)
2713
2714 self.init_repo("org/new-project")
2715 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2716
James E. Blair8b5408c2016-08-08 15:37:46 -07002717 A.addApproval('code-review', 2)
2718 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002719 self.waitUntilSettled()
2720 self.assertEqual(self.getJobFromHistory('project-merge').result,
2721 'SUCCESS')
2722 self.assertEqual(self.getJobFromHistory('project-test1').result,
2723 'SUCCESS')
2724 self.assertEqual(self.getJobFromHistory('project-test2').result,
2725 'SUCCESS')
2726 self.assertEqual(A.data['status'], 'MERGED')
2727 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002728
James E. Blair9ea70072017-04-19 16:05:30 -07002729 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002730 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002731 self.init_repo("org/delete-project")
2732 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2733
James E. Blair8b5408c2016-08-08 15:37:46 -07002734 A.addApproval('code-review', 2)
2735 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002736 self.waitUntilSettled()
2737 self.assertEqual(self.getJobFromHistory('project-merge').result,
2738 'SUCCESS')
2739 self.assertEqual(self.getJobFromHistory('project-test1').result,
2740 'SUCCESS')
2741 self.assertEqual(self.getJobFromHistory('project-test2').result,
2742 'SUCCESS')
2743 self.assertEqual(A.data['status'], 'MERGED')
2744 self.assertEqual(A.reported, 2)
2745
2746 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002747 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002748 if os.path.exists(os.path.join(self.merger_src_root, p)):
2749 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002750 if os.path.exists(os.path.join(self.executor_src_root, p)):
2751 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002752
2753 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2754
James E. Blair8b5408c2016-08-08 15:37:46 -07002755 B.addApproval('code-review', 2)
2756 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002757 self.waitUntilSettled()
2758 self.assertEqual(self.getJobFromHistory('project-merge').result,
2759 'SUCCESS')
2760 self.assertEqual(self.getJobFromHistory('project-test1').result,
2761 'SUCCESS')
2762 self.assertEqual(self.getJobFromHistory('project-test2').result,
2763 'SUCCESS')
2764 self.assertEqual(B.data['status'], 'MERGED')
2765 self.assertEqual(B.reported, 2)
2766
James E. Blaire68d43f2017-04-19 14:03:30 -07002767 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002768 def test_tags(self):
2769 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002770 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2771 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2772 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2773 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2774 self.waitUntilSettled()
2775
James E. Blaire68d43f2017-04-19 14:03:30 -07002776 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002777
2778 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002779 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002780 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002781 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002782
2783 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002784 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002785 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002786
James E. Blair63bb0ef2013-07-29 17:14:51 -07002787 def test_timer(self):
2788 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002789 # This test can not use simple_layout because it must start
2790 # with a configuration which does not include a
2791 # timer-triggered job so that we have an opportunity to set
2792 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002793 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002794 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002795 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002796 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002797
Clark Boylan3ee090a2014-04-03 20:55:09 -07002798 # The pipeline triggers every second, so we should have seen
2799 # several by now.
2800 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002801 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002802
2803 self.assertEqual(len(self.builds), 2)
2804
James E. Blair63bb0ef2013-07-29 17:14:51 -07002805 port = self.webapp.server.socket.getsockname()[1]
2806
Clint Byrum5e729e92017-02-08 06:34:57 -08002807 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002808 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002809 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002810 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002811
Paul Belanger174a8272017-03-14 13:20:10 -04002812 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002813 # Stop queuing timer triggered jobs so that the assertions
2814 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002815 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002816 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002817 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002818 self.waitUntilSettled()
2819
James E. Blair21037782017-07-19 11:56:55 -07002820 self.assertHistory([
2821 dict(name='project-bitrot', result='SUCCESS',
2822 ref='refs/heads/master'),
2823 dict(name='project-bitrot', result='SUCCESS',
2824 ref='refs/heads/stable'),
2825 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002826
2827 data = json.loads(data)
2828 status_jobs = set()
2829 for p in data['pipelines']:
2830 for q in p['change_queues']:
2831 for head in q['heads']:
2832 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002833 for job in change['jobs']:
2834 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002835 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002836
James E. Blair4f6033c2014-03-27 15:49:09 -07002837 def test_idle(self):
2838 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002839 # This test can not use simple_layout because it must start
2840 # with a configuration which does not include a
2841 # timer-triggered job so that we have an opportunity to set
2842 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002843 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002844
Clark Boylan3ee090a2014-04-03 20:55:09 -07002845 for x in range(1, 3):
2846 # Test that timer triggers periodic jobs even across
2847 # layout config reloads.
2848 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002849 self.commitConfigUpdate('common-config',
2850 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002851 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002852 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002853
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854 # The pipeline triggers every second, so we should have seen
2855 # several by now.
2856 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002857
2858 # Stop queuing timer triggered jobs so that the assertions
2859 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002860 self.commitConfigUpdate('common-config',
2861 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002862 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002863 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002864 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002865 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002866 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002867 self.waitUntilSettled()
2868 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002869 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002870
James E. Blaircc324192017-04-19 14:36:08 -07002871 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002872 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002873 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2874 self.waitUntilSettled()
2875
2876 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2877 self.waitUntilSettled()
2878
James E. Blairff80a2f2013-12-27 13:24:06 -08002879 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002880
2881 # A.messages only holds what FakeGerrit places in it. Thus we
2882 # work on the knowledge of what the first message should be as
2883 # it is only configured to go to SMTP.
2884
2885 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002886 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002887 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002888 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002889 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002890 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002891
2892 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002893 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002894 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002895 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002896 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002897 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002898
James E. Blaire5910202013-12-27 09:50:31 -08002899 def test_timer_smtp(self):
2900 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002901 # This test can not use simple_layout because it must start
2902 # with a configuration which does not include a
2903 # timer-triggered job so that we have an opportunity to set
2904 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002905 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002906 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002907 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002908
Clark Boylan3ee090a2014-04-03 20:55:09 -07002909 # The pipeline triggers every second, so we should have seen
2910 # several by now.
2911 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002912 self.waitUntilSettled()
2913
Clark Boylan3ee090a2014-04-03 20:55:09 -07002914 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002915 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002916 self.waitUntilSettled()
2917 self.assertEqual(len(self.history), 2)
2918
James E. Blaire5910202013-12-27 09:50:31 -08002919 self.assertEqual(self.getJobFromHistory(
2920 'project-bitrot-stable-old').result, 'SUCCESS')
2921 self.assertEqual(self.getJobFromHistory(
2922 'project-bitrot-stable-older').result, 'SUCCESS')
2923
James E. Blairff80a2f2013-12-27 13:24:06 -08002924 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002925
2926 # A.messages only holds what FakeGerrit places in it. Thus we
2927 # work on the knowledge of what the first message should be as
2928 # it is only configured to go to SMTP.
2929
2930 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002931 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002932 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002933 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002934 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002935 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002936
Clark Boylan3ee090a2014-04-03 20:55:09 -07002937 # Stop queuing timer triggered jobs and let any that may have
2938 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002939 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002940 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002941 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002942 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002943 self.waitUntilSettled()
2944
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002945 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002946 def test_timer_sshkey(self):
2947 "Test that a periodic job can setup SSH key authentication"
2948 self.worker.hold_jobs_in_build = True
2949 self.config.set('zuul', 'layout_config',
2950 'tests/fixtures/layout-timer.yaml')
2951 self.sched.reconfigure(self.config)
2952 self.registerJobs()
2953
2954 # The pipeline triggers every second, so we should have seen
2955 # several by now.
2956 time.sleep(5)
2957 self.waitUntilSettled()
2958
2959 self.assertEqual(len(self.builds), 2)
2960
2961 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2962 self.assertTrue(os.path.isfile(ssh_wrapper))
2963 with open(ssh_wrapper) as f:
2964 ssh_wrapper_content = f.read()
2965 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2966 # In the unit tests Merger runs in the same process,
2967 # so we see its' environment variables
2968 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2969
2970 self.worker.release('.*')
2971 self.waitUntilSettled()
2972 self.assertEqual(len(self.history), 2)
2973
2974 self.assertEqual(self.getJobFromHistory(
2975 'project-bitrot-stable-old').result, 'SUCCESS')
2976 self.assertEqual(self.getJobFromHistory(
2977 'project-bitrot-stable-older').result, 'SUCCESS')
2978
2979 # Stop queuing timer triggered jobs and let any that may have
2980 # queued through so that end of test assertions pass.
2981 self.config.set('zuul', 'layout_config',
2982 'tests/fixtures/layout-no-timer.yaml')
2983 self.sched.reconfigure(self.config)
2984 self.registerJobs()
2985 self.waitUntilSettled()
2986 self.worker.release('.*')
2987 self.waitUntilSettled()
2988
James E. Blair91e34592015-07-31 16:45:59 -07002989 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002990 "Test that the RPC client can enqueue a change"
2991 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002992 A.addApproval('code-review', 2)
2993 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002994
2995 client = zuul.rpcclient.RPCClient('127.0.0.1',
2996 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002997 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002998 r = client.enqueue(tenant='tenant-one',
2999 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003000 project='org/project',
3001 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003002 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003003 self.waitUntilSettled()
3004 self.assertEqual(self.getJobFromHistory('project-merge').result,
3005 'SUCCESS')
3006 self.assertEqual(self.getJobFromHistory('project-test1').result,
3007 'SUCCESS')
3008 self.assertEqual(self.getJobFromHistory('project-test2').result,
3009 'SUCCESS')
3010 self.assertEqual(A.data['status'], 'MERGED')
3011 self.assertEqual(A.reported, 2)
3012 self.assertEqual(r, True)
3013
James E. Blair91e34592015-07-31 16:45:59 -07003014 def test_client_enqueue_ref(self):
3015 "Test that the RPC client can enqueue a ref"
3016
3017 client = zuul.rpcclient.RPCClient('127.0.0.1',
3018 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003019 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003020 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003021 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003022 pipeline='post',
3023 project='org/project',
3024 trigger='gerrit',
3025 ref='master',
3026 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3027 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3028 self.waitUntilSettled()
3029 job_names = [x.name for x in self.history]
3030 self.assertEqual(len(self.history), 1)
3031 self.assertIn('project-post', job_names)
3032 self.assertEqual(r, True)
3033
James E. Blairad28e912013-11-27 10:43:22 -08003034 def test_client_enqueue_negative(self):
3035 "Test that the RPC client returns errors"
3036 client = zuul.rpcclient.RPCClient('127.0.0.1',
3037 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003038 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003039 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003040 "Invalid tenant"):
3041 r = client.enqueue(tenant='tenant-foo',
3042 pipeline='gate',
3043 project='org/project',
3044 trigger='gerrit',
3045 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003046 self.assertEqual(r, False)
3047
3048 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003049 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003050 r = client.enqueue(tenant='tenant-one',
3051 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003052 project='project-does-not-exist',
3053 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003054 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003055 self.assertEqual(r, False)
3056
3057 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3058 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003059 r = client.enqueue(tenant='tenant-one',
3060 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003061 project='org/project',
3062 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003063 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003064 self.assertEqual(r, False)
3065
3066 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3067 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003068 r = client.enqueue(tenant='tenant-one',
3069 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003070 project='org/project',
3071 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003072 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003073 self.assertEqual(r, False)
3074
3075 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3076 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003077 r = client.enqueue(tenant='tenant-one',
3078 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003079 project='org/project',
3080 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003081 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003082 self.assertEqual(r, False)
3083
3084 self.waitUntilSettled()
3085 self.assertEqual(len(self.history), 0)
3086 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003087
3088 def test_client_promote(self):
3089 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003090 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003091 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3092 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3093 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003094 A.addApproval('code-review', 2)
3095 B.addApproval('code-review', 2)
3096 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003097
James E. Blair8b5408c2016-08-08 15:37:46 -07003098 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3099 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3100 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003101
3102 self.waitUntilSettled()
3103
Paul Belangerbaca3132016-11-04 12:49:54 -04003104 tenant = self.sched.abide.tenants.get('tenant-one')
3105 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003106 enqueue_times = {}
3107 for item in items:
3108 enqueue_times[str(item.change)] = item.enqueue_time
3109
James E. Blair36658cf2013-12-06 17:53:48 -08003110 client = zuul.rpcclient.RPCClient('127.0.0.1',
3111 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003112 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003113 r = client.promote(tenant='tenant-one',
3114 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003115 change_ids=['2,1', '3,1'])
3116
Sean Daguef39b9ca2014-01-10 21:34:35 -05003117 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003118 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003119 for item in items:
3120 self.assertEqual(
3121 enqueue_times[str(item.change)], item.enqueue_time)
3122
James E. Blair78acec92014-02-06 07:11:32 -08003123 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003124 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003125 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003126 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003127 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003128 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003129 self.waitUntilSettled()
3130
3131 self.assertEqual(len(self.builds), 6)
3132 self.assertEqual(self.builds[0].name, 'project-test1')
3133 self.assertEqual(self.builds[1].name, 'project-test2')
3134 self.assertEqual(self.builds[2].name, 'project-test1')
3135 self.assertEqual(self.builds[3].name, 'project-test2')
3136 self.assertEqual(self.builds[4].name, 'project-test1')
3137 self.assertEqual(self.builds[5].name, 'project-test2')
3138
Paul Belangerbaca3132016-11-04 12:49:54 -04003139 self.assertTrue(self.builds[0].hasChanges(B))
3140 self.assertFalse(self.builds[0].hasChanges(A))
3141 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003142
Paul Belangerbaca3132016-11-04 12:49:54 -04003143 self.assertTrue(self.builds[2].hasChanges(B))
3144 self.assertTrue(self.builds[2].hasChanges(C))
3145 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003146
Paul Belangerbaca3132016-11-04 12:49:54 -04003147 self.assertTrue(self.builds[4].hasChanges(B))
3148 self.assertTrue(self.builds[4].hasChanges(C))
3149 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003150
Paul Belanger174a8272017-03-14 13:20:10 -04003151 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003152 self.waitUntilSettled()
3153
3154 self.assertEqual(A.data['status'], 'MERGED')
3155 self.assertEqual(A.reported, 2)
3156 self.assertEqual(B.data['status'], 'MERGED')
3157 self.assertEqual(B.reported, 2)
3158 self.assertEqual(C.data['status'], 'MERGED')
3159 self.assertEqual(C.reported, 2)
3160
James E. Blair36658cf2013-12-06 17:53:48 -08003161 self.assertEqual(r, True)
3162
3163 def test_client_promote_dependent(self):
3164 "Test that the RPC client can promote a dependent change"
3165 # C (depends on B) -> B -> A ; then promote C to get:
3166 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003167 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003168 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3169 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3170 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3171
3172 C.setDependsOn(B, 1)
3173
James E. Blair8b5408c2016-08-08 15:37:46 -07003174 A.addApproval('code-review', 2)
3175 B.addApproval('code-review', 2)
3176 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003177
James E. Blair8b5408c2016-08-08 15:37:46 -07003178 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3179 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3180 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003181
3182 self.waitUntilSettled()
3183
3184 client = zuul.rpcclient.RPCClient('127.0.0.1',
3185 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003186 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003187 r = client.promote(tenant='tenant-one',
3188 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003189 change_ids=['3,1'])
3190
James E. Blair78acec92014-02-06 07:11:32 -08003191 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003192 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003193 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003194 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003195 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003196 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003197 self.waitUntilSettled()
3198
3199 self.assertEqual(len(self.builds), 6)
3200 self.assertEqual(self.builds[0].name, 'project-test1')
3201 self.assertEqual(self.builds[1].name, 'project-test2')
3202 self.assertEqual(self.builds[2].name, 'project-test1')
3203 self.assertEqual(self.builds[3].name, 'project-test2')
3204 self.assertEqual(self.builds[4].name, 'project-test1')
3205 self.assertEqual(self.builds[5].name, 'project-test2')
3206
Paul Belangerbaca3132016-11-04 12:49:54 -04003207 self.assertTrue(self.builds[0].hasChanges(B))
3208 self.assertFalse(self.builds[0].hasChanges(A))
3209 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003210
Paul Belangerbaca3132016-11-04 12:49:54 -04003211 self.assertTrue(self.builds[2].hasChanges(B))
3212 self.assertTrue(self.builds[2].hasChanges(C))
3213 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003214
Paul Belangerbaca3132016-11-04 12:49:54 -04003215 self.assertTrue(self.builds[4].hasChanges(B))
3216 self.assertTrue(self.builds[4].hasChanges(C))
3217 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003218
Paul Belanger174a8272017-03-14 13:20:10 -04003219 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003220 self.waitUntilSettled()
3221
3222 self.assertEqual(A.data['status'], 'MERGED')
3223 self.assertEqual(A.reported, 2)
3224 self.assertEqual(B.data['status'], 'MERGED')
3225 self.assertEqual(B.reported, 2)
3226 self.assertEqual(C.data['status'], 'MERGED')
3227 self.assertEqual(C.reported, 2)
3228
James E. Blair36658cf2013-12-06 17:53:48 -08003229 self.assertEqual(r, True)
3230
3231 def test_client_promote_negative(self):
3232 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003233 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003234 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003235 A.addApproval('code-review', 2)
3236 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003237 self.waitUntilSettled()
3238
3239 client = zuul.rpcclient.RPCClient('127.0.0.1',
3240 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003241 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003242
3243 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003244 r = client.promote(tenant='tenant-one',
3245 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003246 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003247 self.assertEqual(r, False)
3248
3249 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003250 r = client.promote(tenant='tenant-one',
3251 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003252 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003253 self.assertEqual(r, False)
3254
Paul Belanger174a8272017-03-14 13:20:10 -04003255 self.executor_server.hold_jobs_in_build = False
3256 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003257 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003258
James E. Blaircc324192017-04-19 14:36:08 -07003259 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003260 def test_queue_rate_limiting(self):
3261 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003262 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003263 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3264 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3265 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3266
3267 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003268 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003269
James E. Blair8b5408c2016-08-08 15:37:46 -07003270 A.addApproval('code-review', 2)
3271 B.addApproval('code-review', 2)
3272 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003273
James E. Blair8b5408c2016-08-08 15:37:46 -07003274 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3275 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3276 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003277 self.waitUntilSettled()
3278
3279 # Only A and B will have their merge jobs queued because
3280 # window is 2.
3281 self.assertEqual(len(self.builds), 2)
3282 self.assertEqual(self.builds[0].name, 'project-merge')
3283 self.assertEqual(self.builds[1].name, 'project-merge')
3284
James E. Blair732ee862017-04-21 07:49:26 -07003285 # Release the merge jobs one at a time.
3286 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003287 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003288 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003289 self.waitUntilSettled()
3290
3291 # Only A and B will have their test jobs queued because
3292 # window is 2.
3293 self.assertEqual(len(self.builds), 4)
3294 self.assertEqual(self.builds[0].name, 'project-test1')
3295 self.assertEqual(self.builds[1].name, 'project-test2')
3296 self.assertEqual(self.builds[2].name, 'project-test1')
3297 self.assertEqual(self.builds[3].name, 'project-test2')
3298
Paul Belanger174a8272017-03-14 13:20:10 -04003299 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003300 self.waitUntilSettled()
3301
James E. Blaircc324192017-04-19 14:36:08 -07003302 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003303 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003304 # A failed so window is reduced by 1 to 1.
3305 self.assertEqual(queue.window, 1)
3306 self.assertEqual(queue.window_floor, 1)
3307 self.assertEqual(A.data['status'], 'NEW')
3308
3309 # Gate is reset and only B's merge job is queued because
3310 # window shrunk to 1.
3311 self.assertEqual(len(self.builds), 1)
3312 self.assertEqual(self.builds[0].name, 'project-merge')
3313
Paul Belanger174a8272017-03-14 13:20:10 -04003314 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003315 self.waitUntilSettled()
3316
3317 # Only B's test jobs are queued because window is still 1.
3318 self.assertEqual(len(self.builds), 2)
3319 self.assertEqual(self.builds[0].name, 'project-test1')
3320 self.assertEqual(self.builds[1].name, 'project-test2')
3321
Paul Belanger174a8272017-03-14 13:20:10 -04003322 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003323 self.waitUntilSettled()
3324
3325 # B was successfully merged so window is increased to 2.
3326 self.assertEqual(queue.window, 2)
3327 self.assertEqual(queue.window_floor, 1)
3328 self.assertEqual(B.data['status'], 'MERGED')
3329
3330 # Only C is left and its merge job is queued.
3331 self.assertEqual(len(self.builds), 1)
3332 self.assertEqual(self.builds[0].name, 'project-merge')
3333
Paul Belanger174a8272017-03-14 13:20:10 -04003334 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003335 self.waitUntilSettled()
3336
3337 # After successful merge job the test jobs for C are queued.
3338 self.assertEqual(len(self.builds), 2)
3339 self.assertEqual(self.builds[0].name, 'project-test1')
3340 self.assertEqual(self.builds[1].name, 'project-test2')
3341
Paul Belanger174a8272017-03-14 13:20:10 -04003342 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003343 self.waitUntilSettled()
3344
3345 # C successfully merged so window is bumped to 3.
3346 self.assertEqual(queue.window, 3)
3347 self.assertEqual(queue.window_floor, 1)
3348 self.assertEqual(C.data['status'], 'MERGED')
3349
James E. Blaircc324192017-04-19 14:36:08 -07003350 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003351 def test_queue_rate_limiting_dependent(self):
3352 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003353 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003354 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3355 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3356 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3357
3358 B.setDependsOn(A, 1)
3359
Paul Belanger174a8272017-03-14 13:20:10 -04003360 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003361
James E. Blair8b5408c2016-08-08 15:37:46 -07003362 A.addApproval('code-review', 2)
3363 B.addApproval('code-review', 2)
3364 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003365
James E. Blair8b5408c2016-08-08 15:37:46 -07003366 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3367 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3368 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003369 self.waitUntilSettled()
3370
3371 # Only A and B will have their merge jobs queued because
3372 # window is 2.
3373 self.assertEqual(len(self.builds), 2)
3374 self.assertEqual(self.builds[0].name, 'project-merge')
3375 self.assertEqual(self.builds[1].name, 'project-merge')
3376
James E. Blairf4a5f022017-04-18 14:01:10 -07003377 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003378
3379 # Only A and B will have their test jobs queued because
3380 # window is 2.
3381 self.assertEqual(len(self.builds), 4)
3382 self.assertEqual(self.builds[0].name, 'project-test1')
3383 self.assertEqual(self.builds[1].name, 'project-test2')
3384 self.assertEqual(self.builds[2].name, 'project-test1')
3385 self.assertEqual(self.builds[3].name, 'project-test2')
3386
Paul Belanger174a8272017-03-14 13:20:10 -04003387 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003388 self.waitUntilSettled()
3389
James E. Blaircc324192017-04-19 14:36:08 -07003390 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003391 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003392 # A failed so window is reduced by 1 to 1.
3393 self.assertEqual(queue.window, 1)
3394 self.assertEqual(queue.window_floor, 1)
3395 self.assertEqual(A.data['status'], 'NEW')
3396 self.assertEqual(B.data['status'], 'NEW')
3397
3398 # Gate is reset and only C's merge job is queued because
3399 # window shrunk to 1 and A and B were dequeued.
3400 self.assertEqual(len(self.builds), 1)
3401 self.assertEqual(self.builds[0].name, 'project-merge')
3402
James E. Blairf4a5f022017-04-18 14:01:10 -07003403 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003404
3405 # Only C's test jobs are queued because window is still 1.
3406 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003407 builds = self.getSortedBuilds()
3408 self.assertEqual(builds[0].name, 'project-test1')
3409 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003410
Paul Belanger174a8272017-03-14 13:20:10 -04003411 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003412 self.waitUntilSettled()
3413
3414 # C was successfully merged so window is increased to 2.
3415 self.assertEqual(queue.window, 2)
3416 self.assertEqual(queue.window_floor, 1)
3417 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003418
3419 def test_worker_update_metadata(self):
3420 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003421 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003422
3423 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003424 A.addApproval('code-review', 2)
3425 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003426 self.waitUntilSettled()
3427
Clint Byrumd4de79a2017-05-27 11:39:13 -07003428 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003429
3430 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003431 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003432
3433 start = time.time()
3434 while True:
3435 if time.time() - start > 10:
3436 raise Exception("Timeout waiting for gearman server to report "
3437 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003438 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003439 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003440 break
3441 else:
3442 time.sleep(0)
3443
3444 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003445 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003446
Paul Belanger174a8272017-03-14 13:20:10 -04003447 self.executor_server.hold_jobs_in_build = False
3448 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003449 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003450
James E. Blaircc324192017-04-19 14:36:08 -07003451 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003452 def test_footer_message(self):
3453 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003454 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003455 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003456 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003457 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003458 self.waitUntilSettled()
3459
3460 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003461 B.addApproval('code-review', 2)
3462 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003463 self.waitUntilSettled()
3464
3465 self.assertEqual(2, len(self.smtp_messages))
3466
Adam Gandelman82258fd2017-02-07 16:51:58 -08003467 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003468Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003469http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003470
Adam Gandelman82258fd2017-02-07 16:51:58 -08003471 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003472For CI problems and help debugging, contact ci@example.org"""
3473
Adam Gandelman82258fd2017-02-07 16:51:58 -08003474 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3475 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3476 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3477 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003478
Tobias Henkel9842bd72017-05-16 13:40:03 +02003479 @simple_layout('layouts/unmanaged-project.yaml')
3480 def test_unmanaged_project_start_message(self):
3481 "Test start reporting is not done for unmanaged projects."
3482 self.init_repo("org/project", tag='init')
3483 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3484 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3485 self.waitUntilSettled()
3486
3487 self.assertEqual(0, len(A.messages))
3488
Clint Byrumb9de8be2017-05-27 13:49:19 -07003489 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003490 def test_merge_failure_reporters(self):
3491 """Check that the config is set up correctly"""
3492
Clint Byrumb9de8be2017-05-27 13:49:19 -07003493 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003494 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003495 "Merge Failed.\n\nThis change or one of its cross-repo "
3496 "dependencies was unable to be automatically merged with the "
3497 "current state of its repository. Please rebase the change and "
3498 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003499 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003500 self.assertEqual(
3501 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003502 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003503
3504 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003505 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003506 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003507 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003508
3509 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003510 tenant.layout.pipelines['check'].merge_failure_actions[0],
3511 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003512
3513 self.assertTrue(
3514 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003515 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003516 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003517 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3518 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003519 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003520 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003521 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003522 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003523 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003524 gerritreporter.GerritReporter) and
3525 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003526 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003527 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003528 )
3529 )
3530
James E. Blairec056492016-07-22 09:45:56 -07003531 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003532 def test_merge_failure_reports(self):
3533 """Check that when a change fails to merge the correct message is sent
3534 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003535 self.updateConfigLayout(
3536 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003537 self.sched.reconfigure(self.config)
3538 self.registerJobs()
3539
3540 # Check a test failure isn't reported to SMTP
3541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003542 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003543 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003544 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003545 self.waitUntilSettled()
3546
3547 self.assertEqual(3, len(self.history)) # 3 jobs
3548 self.assertEqual(0, len(self.smtp_messages))
3549
3550 # Check a merge failure is reported to SMTP
3551 # B should be merged, but C will conflict with B
3552 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3553 B.addPatchset(['conflict'])
3554 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3555 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003556 B.addApproval('code-review', 2)
3557 C.addApproval('code-review', 2)
3558 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3559 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003560 self.waitUntilSettled()
3561
3562 self.assertEqual(6, len(self.history)) # A and B jobs
3563 self.assertEqual(1, len(self.smtp_messages))
3564 self.assertEqual('The merge failed! For more information...',
3565 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003566
James E. Blairec056492016-07-22 09:45:56 -07003567 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003568 def test_default_merge_failure_reports(self):
3569 """Check that the default merge failure reports are correct."""
3570
3571 # A should report success, B should report merge failure.
3572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3573 A.addPatchset(['conflict'])
3574 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3575 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003576 A.addApproval('code-review', 2)
3577 B.addApproval('code-review', 2)
3578 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3579 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003580 self.waitUntilSettled()
3581
3582 self.assertEqual(3, len(self.history)) # A jobs
3583 self.assertEqual(A.reported, 2)
3584 self.assertEqual(B.reported, 2)
3585 self.assertEqual(A.data['status'], 'MERGED')
3586 self.assertEqual(B.data['status'], 'NEW')
3587 self.assertIn('Build succeeded', A.messages[1])
3588 self.assertIn('Merge Failed', B.messages[1])
3589 self.assertIn('automatically merged', B.messages[1])
3590 self.assertNotIn('logs.example.com', B.messages[1])
3591 self.assertNotIn('SKIPPED', B.messages[1])
3592
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003593 def test_client_get_running_jobs(self):
3594 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003595 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003596 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003597 A.addApproval('code-review', 2)
3598 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003599 self.waitUntilSettled()
3600
3601 client = zuul.rpcclient.RPCClient('127.0.0.1',
3602 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003603 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003604
3605 # Wait for gearman server to send the initial workData back to zuul
3606 start = time.time()
3607 while True:
3608 if time.time() - start > 10:
3609 raise Exception("Timeout waiting for gearman server to report "
3610 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003611 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003612 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003613 break
3614 else:
3615 time.sleep(0)
3616
3617 running_items = client.get_running_jobs()
3618
3619 self.assertEqual(1, len(running_items))
3620 running_item = running_items[0]
3621 self.assertEqual([], running_item['failing_reasons'])
3622 self.assertEqual([], running_item['items_behind'])
3623 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003624 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003625 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003626 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003627 self.assertEqual(True, running_item['active'])
3628 self.assertEqual('1,1', running_item['id'])
3629
3630 self.assertEqual(3, len(running_item['jobs']))
3631 for job in running_item['jobs']:
3632 if job['name'] == 'project-merge':
3633 self.assertEqual('project-merge', job['name'])
3634 self.assertEqual('gate', job['pipeline'])
3635 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003636 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003637 'static/stream.html?uuid={uuid}&logfile=console.log'
3638 .format(uuid=job['uuid']), job['url'])
3639 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003640 'finger://{hostname}/{uuid}'.format(
3641 hostname=self.executor_server.hostname,
3642 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003643 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003644 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003645 self.assertEqual(False, job['canceled'])
3646 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003647 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003648 self.assertEqual('gate', job['pipeline'])
3649 break
3650
Paul Belanger174a8272017-03-14 13:20:10 -04003651 self.executor_server.hold_jobs_in_build = False
3652 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003653 self.waitUntilSettled()
3654
3655 running_items = client.get_running_jobs()
3656 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003657
James E. Blairb70e55a2017-04-19 12:57:02 -07003658 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003659 def test_nonvoting_pipeline(self):
3660 "Test that a nonvoting pipeline (experimental) can still report"
3661
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003662 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3663 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003664 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3665 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003666
3667 self.assertEqual(self.getJobFromHistory('project-merge').result,
3668 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003669 self.assertEqual(
3670 self.getJobFromHistory('experimental-project-test').result,
3671 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003672 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003673
3674 def test_crd_gate(self):
3675 "Test cross-repo dependencies"
3676 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3677 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003678 A.addApproval('code-review', 2)
3679 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003680
3681 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3682 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3683 AM2.setMerged()
3684 AM1.setMerged()
3685
3686 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3687 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3688 BM2.setMerged()
3689 BM1.setMerged()
3690
3691 # A -> AM1 -> AM2
3692 # B -> BM1 -> BM2
3693 # A Depends-On: B
3694 # M2 is here to make sure it is never queried. If it is, it
3695 # means zuul is walking down the entire history of merged
3696 # changes.
3697
3698 B.setDependsOn(BM1, 1)
3699 BM1.setDependsOn(BM2, 1)
3700
3701 A.setDependsOn(AM1, 1)
3702 AM1.setDependsOn(AM2, 1)
3703
3704 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3705 A.subject, B.data['id'])
3706
James E. Blair8b5408c2016-08-08 15:37:46 -07003707 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003708 self.waitUntilSettled()
3709
3710 self.assertEqual(A.data['status'], 'NEW')
3711 self.assertEqual(B.data['status'], 'NEW')
3712
Paul Belanger5dccbe72016-11-14 11:17:37 -05003713 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003714 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003715
Paul Belanger174a8272017-03-14 13:20:10 -04003716 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003717 B.addApproval('approved', 1)
3718 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003719 self.waitUntilSettled()
3720
Paul Belanger174a8272017-03-14 13:20:10 -04003721 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003722 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003723 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003724 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003725 self.executor_server.hold_jobs_in_build = False
3726 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003727 self.waitUntilSettled()
3728
3729 self.assertEqual(AM2.queried, 0)
3730 self.assertEqual(BM2.queried, 0)
3731 self.assertEqual(A.data['status'], 'MERGED')
3732 self.assertEqual(B.data['status'], 'MERGED')
3733 self.assertEqual(A.reported, 2)
3734 self.assertEqual(B.reported, 2)
3735
Paul Belanger5dccbe72016-11-14 11:17:37 -05003736 changes = self.getJobFromHistory(
3737 'project-merge', 'org/project1').changes
3738 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003739
3740 def test_crd_branch(self):
3741 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003742
3743 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003744 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3745 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3746 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3747 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003748 A.addApproval('code-review', 2)
3749 B.addApproval('code-review', 2)
3750 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003751
3752 # A Depends-On: B+C
3753 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3754 A.subject, B.data['id'])
3755
Paul Belanger174a8272017-03-14 13:20:10 -04003756 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003757 B.addApproval('approved', 1)
3758 C.addApproval('approved', 1)
3759 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003760 self.waitUntilSettled()
3761
Paul Belanger174a8272017-03-14 13:20:10 -04003762 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003763 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003764 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003765 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003766 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003767 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003768 self.executor_server.hold_jobs_in_build = False
3769 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003770 self.waitUntilSettled()
3771
3772 self.assertEqual(A.data['status'], 'MERGED')
3773 self.assertEqual(B.data['status'], 'MERGED')
3774 self.assertEqual(C.data['status'], 'MERGED')
3775 self.assertEqual(A.reported, 2)
3776 self.assertEqual(B.reported, 2)
3777 self.assertEqual(C.reported, 2)
3778
Paul Belanger6379db12016-11-14 13:57:54 -05003779 changes = self.getJobFromHistory(
3780 'project-merge', 'org/project1').changes
3781 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003782
3783 def test_crd_multiline(self):
3784 "Test multiple depends-on lines in commit"
3785 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3786 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3787 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003788 A.addApproval('code-review', 2)
3789 B.addApproval('code-review', 2)
3790 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003791
3792 # A Depends-On: B+C
3793 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3794 A.subject, B.data['id'], C.data['id'])
3795
Paul Belanger174a8272017-03-14 13:20:10 -04003796 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003797 B.addApproval('approved', 1)
3798 C.addApproval('approved', 1)
3799 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003800 self.waitUntilSettled()
3801
Paul Belanger174a8272017-03-14 13:20:10 -04003802 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003803 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003804 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003805 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003806 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003807 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003808 self.executor_server.hold_jobs_in_build = False
3809 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003810 self.waitUntilSettled()
3811
3812 self.assertEqual(A.data['status'], 'MERGED')
3813 self.assertEqual(B.data['status'], 'MERGED')
3814 self.assertEqual(C.data['status'], 'MERGED')
3815 self.assertEqual(A.reported, 2)
3816 self.assertEqual(B.reported, 2)
3817 self.assertEqual(C.reported, 2)
3818
Paul Belanger5dccbe72016-11-14 11:17:37 -05003819 changes = self.getJobFromHistory(
3820 'project-merge', 'org/project1').changes
3821 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003822
3823 def test_crd_unshared_gate(self):
3824 "Test cross-repo dependencies in unshared gate queues"
3825 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3826 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003827 A.addApproval('code-review', 2)
3828 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003829
3830 # A Depends-On: B
3831 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3832 A.subject, B.data['id'])
3833
3834 # A and B do not share a queue, make sure that A is unable to
3835 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003836 B.addApproval('approved', 1)
3837 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003838 self.waitUntilSettled()
3839
3840 self.assertEqual(A.data['status'], 'NEW')
3841 self.assertEqual(B.data['status'], 'NEW')
3842 self.assertEqual(A.reported, 0)
3843 self.assertEqual(B.reported, 0)
3844 self.assertEqual(len(self.history), 0)
3845
3846 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003847 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003848 self.waitUntilSettled()
3849
3850 self.assertEqual(B.data['status'], 'MERGED')
3851 self.assertEqual(B.reported, 2)
3852
3853 # Now that B is merged, A should be able to be enqueued and
3854 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003855 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003856 self.waitUntilSettled()
3857
3858 self.assertEqual(A.data['status'], 'MERGED')
3859 self.assertEqual(A.reported, 2)
3860
James E. Blair96698e22015-04-02 07:48:21 -07003861 def test_crd_gate_reverse(self):
3862 "Test reverse cross-repo dependencies"
3863 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3864 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003865 A.addApproval('code-review', 2)
3866 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003867
3868 # A Depends-On: B
3869
3870 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3871 A.subject, B.data['id'])
3872
James E. Blair8b5408c2016-08-08 15:37:46 -07003873 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003874 self.waitUntilSettled()
3875
3876 self.assertEqual(A.data['status'], 'NEW')
3877 self.assertEqual(B.data['status'], 'NEW')
3878
Paul Belanger174a8272017-03-14 13:20:10 -04003879 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003880 A.addApproval('approved', 1)
3881 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003882 self.waitUntilSettled()
3883
Paul Belanger174a8272017-03-14 13:20:10 -04003884 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003885 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003886 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003887 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003888 self.executor_server.hold_jobs_in_build = False
3889 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003890 self.waitUntilSettled()
3891
3892 self.assertEqual(A.data['status'], 'MERGED')
3893 self.assertEqual(B.data['status'], 'MERGED')
3894 self.assertEqual(A.reported, 2)
3895 self.assertEqual(B.reported, 2)
3896
Paul Belanger5dccbe72016-11-14 11:17:37 -05003897 changes = self.getJobFromHistory(
3898 'project-merge', 'org/project1').changes
3899 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003900
James E. Blair5ee24252014-12-30 10:12:29 -08003901 def test_crd_cycle(self):
3902 "Test cross-repo dependency cycles"
3903 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3904 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003905 A.addApproval('code-review', 2)
3906 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003907
3908 # A -> B -> A (via commit-depends)
3909
3910 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3911 A.subject, B.data['id'])
3912 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3913 B.subject, A.data['id'])
3914
James E. Blair8b5408c2016-08-08 15:37:46 -07003915 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003916 self.waitUntilSettled()
3917
3918 self.assertEqual(A.reported, 0)
3919 self.assertEqual(B.reported, 0)
3920 self.assertEqual(A.data['status'], 'NEW')
3921 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003922
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003923 def test_crd_gate_unknown(self):
3924 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003925 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003926 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3927 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003928 A.addApproval('code-review', 2)
3929 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003930
3931 # A Depends-On: B
3932 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3933 A.subject, B.data['id'])
3934
James E. Blair8b5408c2016-08-08 15:37:46 -07003935 B.addApproval('approved', 1)
3936 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003937 self.waitUntilSettled()
3938
3939 # Unknown projects cannot share a queue with any other
3940 # since they don't have common jobs with any other (they have no jobs).
3941 # Changes which depend on unknown project changes
3942 # should not be processed in dependent pipeline
3943 self.assertEqual(A.data['status'], 'NEW')
3944 self.assertEqual(B.data['status'], 'NEW')
3945 self.assertEqual(A.reported, 0)
3946 self.assertEqual(B.reported, 0)
3947 self.assertEqual(len(self.history), 0)
3948
James E. Blair7720b402017-06-07 08:51:49 -07003949 # Simulate change B being gated outside this layout Set the
3950 # change merged before submitting the event so that when the
3951 # event triggers a gerrit query to update the change, we get
3952 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003953 B.setMerged()
James E. Blair7720b402017-06-07 08:51:49 -07003954 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003955 self.waitUntilSettled()
3956 self.assertEqual(len(self.history), 0)
3957
3958 # Now that B is merged, A should be able to be enqueued and
3959 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003960 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003961 self.waitUntilSettled()
3962
3963 self.assertEqual(A.data['status'], 'MERGED')
3964 self.assertEqual(A.reported, 2)
3965 self.assertEqual(B.data['status'], 'MERGED')
3966 self.assertEqual(B.reported, 0)
3967
James E. Blairbfb8e042014-12-30 17:01:44 -08003968 def test_crd_check(self):
3969 "Test cross-repo dependencies in independent pipelines"
3970
Paul Belanger174a8272017-03-14 13:20:10 -04003971 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003972 self.gearman_server.hold_jobs_in_queue = True
3973 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3974 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3975
3976 # A Depends-On: B
3977 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3978 A.subject, B.data['id'])
3979
3980 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3981 self.waitUntilSettled()
3982
James E. Blairbfb8e042014-12-30 17:01:44 -08003983 self.gearman_server.hold_jobs_in_queue = False
3984 self.gearman_server.release()
3985 self.waitUntilSettled()
3986
Paul Belanger174a8272017-03-14 13:20:10 -04003987 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003988 self.waitUntilSettled()
3989
James E. Blair2a535672017-04-27 12:03:15 -07003990 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003991
Paul Belanger174a8272017-03-14 13:20:10 -04003992 self.executor_server.hold_jobs_in_build = False
3993 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003994 self.waitUntilSettled()
3995
James E. Blairbfb8e042014-12-30 17:01:44 -08003996 self.assertEqual(A.data['status'], 'NEW')
3997 self.assertEqual(B.data['status'], 'NEW')
3998 self.assertEqual(A.reported, 1)
3999 self.assertEqual(B.reported, 0)
4000
4001 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004002 tenant = self.sched.abide.tenants.get('tenant-one')
4003 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004004
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004005 def test_crd_check_git_depends(self):
4006 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004007 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004008 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4009 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4010
4011 # Add two git-dependent changes and make sure they both report
4012 # success.
4013 B.setDependsOn(A, 1)
4014 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4015 self.waitUntilSettled()
4016 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4017 self.waitUntilSettled()
4018
James E. Blairb8c16472015-05-05 14:55:26 -07004019 self.orderedRelease()
4020 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004021 self.waitUntilSettled()
4022
4023 self.assertEqual(A.data['status'], 'NEW')
4024 self.assertEqual(B.data['status'], 'NEW')
4025 self.assertEqual(A.reported, 1)
4026 self.assertEqual(B.reported, 1)
4027
4028 self.assertEqual(self.history[0].changes, '1,1')
4029 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004030 tenant = self.sched.abide.tenants.get('tenant-one')
4031 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004032
4033 self.assertIn('Build succeeded', A.messages[0])
4034 self.assertIn('Build succeeded', B.messages[0])
4035
4036 def test_crd_check_duplicate(self):
4037 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004038 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004039 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4040 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004041 tenant = self.sched.abide.tenants.get('tenant-one')
4042 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004043
4044 # Add two git-dependent changes...
4045 B.setDependsOn(A, 1)
4046 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4047 self.waitUntilSettled()
4048 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4049
4050 # ...make sure the live one is not duplicated...
4051 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4052 self.waitUntilSettled()
4053 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4054
4055 # ...but the non-live one is able to be.
4056 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4057 self.waitUntilSettled()
4058 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4059
Clark Boylandd849822015-03-02 12:38:14 -08004060 # Release jobs in order to avoid races with change A jobs
4061 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004062 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004063 self.executor_server.hold_jobs_in_build = False
4064 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004065 self.waitUntilSettled()
4066
4067 self.assertEqual(A.data['status'], 'NEW')
4068 self.assertEqual(B.data['status'], 'NEW')
4069 self.assertEqual(A.reported, 1)
4070 self.assertEqual(B.reported, 1)
4071
4072 self.assertEqual(self.history[0].changes, '1,1 2,1')
4073 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004074 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004075
4076 self.assertIn('Build succeeded', A.messages[0])
4077 self.assertIn('Build succeeded', B.messages[0])
4078
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004079 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004080 "Test cross-repo dependencies re-enqueued in independent pipelines"
4081
4082 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004083 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4084 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004085
4086 # A Depends-On: B
4087 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4088 A.subject, B.data['id'])
4089
4090 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4091 self.waitUntilSettled()
4092
4093 self.sched.reconfigure(self.config)
4094
4095 # Make sure the items still share a change queue, and the
4096 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004097 tenant = self.sched.abide.tenants.get('tenant-one')
4098 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4099 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004100 first_item = queue.queue[0]
4101 for item in queue.queue:
4102 self.assertEqual(item.queue, first_item.queue)
4103 self.assertFalse(first_item.live)
4104 self.assertTrue(queue.queue[1].live)
4105
4106 self.gearman_server.hold_jobs_in_queue = False
4107 self.gearman_server.release()
4108 self.waitUntilSettled()
4109
4110 self.assertEqual(A.data['status'], 'NEW')
4111 self.assertEqual(B.data['status'], 'NEW')
4112 self.assertEqual(A.reported, 1)
4113 self.assertEqual(B.reported, 0)
4114
4115 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004116 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004117
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004118 def test_crd_check_reconfiguration(self):
4119 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4120
4121 def test_crd_undefined_project(self):
4122 """Test that undefined projects in dependencies are handled for
4123 independent pipelines"""
4124 # It's a hack for fake gerrit,
4125 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004126 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004127 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4128
Jesse Keatingea68ab42017-04-19 15:03:30 -07004129 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004130 def test_crd_check_ignore_dependencies(self):
4131 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004132
4133 self.gearman_server.hold_jobs_in_queue = True
4134 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4135 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4136 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4137
4138 # A Depends-On: B
4139 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4140 A.subject, B.data['id'])
4141 # C git-depends on B
4142 C.setDependsOn(B, 1)
4143 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4144 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4145 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4146 self.waitUntilSettled()
4147
4148 # Make sure none of the items share a change queue, and all
4149 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004150 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004151 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004152 self.assertEqual(len(check_pipeline.queues), 3)
4153 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4154 for item in check_pipeline.getAllItems():
4155 self.assertTrue(item.live)
4156
4157 self.gearman_server.hold_jobs_in_queue = False
4158 self.gearman_server.release()
4159 self.waitUntilSettled()
4160
4161 self.assertEqual(A.data['status'], 'NEW')
4162 self.assertEqual(B.data['status'], 'NEW')
4163 self.assertEqual(C.data['status'], 'NEW')
4164 self.assertEqual(A.reported, 1)
4165 self.assertEqual(B.reported, 1)
4166 self.assertEqual(C.reported, 1)
4167
4168 # Each job should have tested exactly one change
4169 for job in self.history:
4170 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004171
James E. Blair7cb84542017-04-19 13:35:05 -07004172 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004173 def test_crd_check_transitive(self):
4174 "Test transitive cross-repo dependencies"
4175 # Specifically, if A -> B -> C, and C gets a new patchset and
4176 # A gets a new patchset, ensure the test of A,2 includes B,1
4177 # and C,2 (not C,1 which would indicate stale data in the
4178 # cache for B).
4179 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4180 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4181 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4182
4183 # A Depends-On: B
4184 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4185 A.subject, B.data['id'])
4186
4187 # B Depends-On: C
4188 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4189 B.subject, C.data['id'])
4190
4191 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4192 self.waitUntilSettled()
4193 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4194
4195 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4196 self.waitUntilSettled()
4197 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4198
4199 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4200 self.waitUntilSettled()
4201 self.assertEqual(self.history[-1].changes, '3,1')
4202
4203 C.addPatchset()
4204 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4205 self.waitUntilSettled()
4206 self.assertEqual(self.history[-1].changes, '3,2')
4207
4208 A.addPatchset()
4209 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4210 self.waitUntilSettled()
4211 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004212
James E. Blaira8b90b32016-08-24 15:18:50 -07004213 def test_crd_check_unknown(self):
4214 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004215 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004216 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4217 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4218 # A Depends-On: B
4219 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4220 A.subject, B.data['id'])
4221
4222 # Make sure zuul has seen an event on B.
4223 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4224 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4225 self.waitUntilSettled()
4226
4227 self.assertEqual(A.data['status'], 'NEW')
4228 self.assertEqual(A.reported, 1)
4229 self.assertEqual(B.data['status'], 'NEW')
4230 self.assertEqual(B.reported, 0)
4231
James E. Blair92464a22016-04-05 10:21:26 -07004232 def test_crd_cycle_join(self):
4233 "Test an updated change creates a cycle"
4234 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4235
4236 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4237 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004238 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004239
4240 # Create B->A
4241 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4242 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4243 B.subject, A.data['id'])
4244 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4245 self.waitUntilSettled()
4246
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004247 # Dep is there so zuul should have reported on B
4248 self.assertEqual(B.reported, 1)
4249
James E. Blair92464a22016-04-05 10:21:26 -07004250 # Update A to add A->B (a cycle).
4251 A.addPatchset()
4252 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4253 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004254 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4255 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004256
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004257 # Dependency cycle injected so zuul should not have reported again on A
4258 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004259
4260 # Now if we update B to remove the depends-on, everything
4261 # should be okay. B; A->B
4262
4263 B.addPatchset()
4264 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004265 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4266 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004267
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004268 # Cycle was removed so now zuul should have reported again on A
4269 self.assertEqual(A.reported, 2)
4270
4271 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4272 self.waitUntilSettled()
4273 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004274
Jesse Keatingcc67b122017-04-19 14:54:57 -07004275 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004276 def test_disable_at(self):
4277 "Test a pipeline will only report to the disabled trigger when failing"
4278
Jesse Keatingcc67b122017-04-19 14:54:57 -07004279 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004280 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004281 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004282 0, tenant.layout.pipelines['check']._consecutive_failures)
4283 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004284
4285 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4286 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4287 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4288 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4289 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4290 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4291 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4292 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4293 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4294 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4295 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4296
Paul Belanger174a8272017-03-14 13:20:10 -04004297 self.executor_server.failJob('project-test1', A)
4298 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004299 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004300 self.executor_server.failJob('project-test1', D)
4301 self.executor_server.failJob('project-test1', E)
4302 self.executor_server.failJob('project-test1', F)
4303 self.executor_server.failJob('project-test1', G)
4304 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004305 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004306 self.executor_server.failJob('project-test1', J)
4307 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004308
4309 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4310 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4311 self.waitUntilSettled()
4312
4313 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004314 2, tenant.layout.pipelines['check']._consecutive_failures)
4315 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004316
4317 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4318 self.waitUntilSettled()
4319
4320 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004321 0, tenant.layout.pipelines['check']._consecutive_failures)
4322 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004323
4324 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4325 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4326 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4327 self.waitUntilSettled()
4328
4329 # We should be disabled now
4330 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004331 3, tenant.layout.pipelines['check']._consecutive_failures)
4332 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004333
4334 # We need to wait between each of these patches to make sure the
4335 # smtp messages come back in an expected order
4336 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4337 self.waitUntilSettled()
4338 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4339 self.waitUntilSettled()
4340 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4341 self.waitUntilSettled()
4342
4343 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4344 # leaving a message on each change
4345 self.assertEqual(1, len(A.messages))
4346 self.assertIn('Build failed.', A.messages[0])
4347 self.assertEqual(1, len(B.messages))
4348 self.assertIn('Build failed.', B.messages[0])
4349 self.assertEqual(1, len(C.messages))
4350 self.assertIn('Build succeeded.', C.messages[0])
4351 self.assertEqual(1, len(D.messages))
4352 self.assertIn('Build failed.', D.messages[0])
4353 self.assertEqual(1, len(E.messages))
4354 self.assertIn('Build failed.', E.messages[0])
4355 self.assertEqual(1, len(F.messages))
4356 self.assertIn('Build failed.', F.messages[0])
4357
4358 # The last 3 (GHI) would have only reported via smtp.
4359 self.assertEqual(3, len(self.smtp_messages))
4360 self.assertEqual(0, len(G.messages))
4361 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004362 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004363 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004364 self.assertEqual(0, len(H.messages))
4365 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004366 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004367 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004368 self.assertEqual(0, len(I.messages))
4369 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004370 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004371 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004372
4373 # Now reload the configuration (simulate a HUP) to check the pipeline
4374 # comes out of disabled
4375 self.sched.reconfigure(self.config)
4376
Jesse Keatingcc67b122017-04-19 14:54:57 -07004377 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004378
4379 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004380 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004381 0, tenant.layout.pipelines['check']._consecutive_failures)
4382 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004383
4384 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4385 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4386 self.waitUntilSettled()
4387
4388 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004389 2, tenant.layout.pipelines['check']._consecutive_failures)
4390 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004391
4392 # J and K went back to gerrit
4393 self.assertEqual(1, len(J.messages))
4394 self.assertIn('Build failed.', J.messages[0])
4395 self.assertEqual(1, len(K.messages))
4396 self.assertIn('Build failed.', K.messages[0])
4397 # No more messages reported via smtp
4398 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004399
James E. Blaircc020532017-04-19 13:04:44 -07004400 @simple_layout('layouts/one-job-project.yaml')
4401 def test_one_job_project(self):
4402 "Test that queueing works with one job"
4403 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4404 'master', 'A')
4405 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4406 'master', 'B')
4407 A.addApproval('code-review', 2)
4408 B.addApproval('code-review', 2)
4409 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4410 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4411 self.waitUntilSettled()
4412
4413 self.assertEqual(A.data['status'], 'MERGED')
4414 self.assertEqual(A.reported, 2)
4415 self.assertEqual(B.data['status'], 'MERGED')
4416 self.assertEqual(B.reported, 2)
4417
Paul Belanger71d98172016-11-08 10:56:31 -05004418 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004419 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004420
Paul Belanger174a8272017-03-14 13:20:10 -04004421 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004422 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4423 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4424 self.waitUntilSettled()
4425
Paul Belanger174a8272017-03-14 13:20:10 -04004426 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004427 self.waitUntilSettled()
4428
4429 self.assertEqual(len(self.builds), 2)
4430 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004431 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004432 self.waitUntilSettled()
4433
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004434 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004435 self.assertEqual(len(self.builds), 1,
4436 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004437 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004438 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004439 self.waitUntilSettled()
4440
Paul Belanger174a8272017-03-14 13:20:10 -04004441 self.executor_server.hold_jobs_in_build = False
4442 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004443 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004444 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004445 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4446 self.assertEqual(A.reported, 1)
4447 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004448
James E. Blair15be0e12017-01-03 13:45:20 -08004449 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004450 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004451
4452 self.fake_nodepool.paused = True
4453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4454 A.addApproval('code-review', 2)
4455 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4456 self.waitUntilSettled()
4457
4458 self.zk.client.stop()
4459 self.zk.client.start()
4460 self.fake_nodepool.paused = False
4461 self.waitUntilSettled()
4462
4463 self.assertEqual(A.data['status'], 'MERGED')
4464 self.assertEqual(A.reported, 2)
4465
James E. Blair6ab79e02017-01-06 10:10:17 -08004466 def test_nodepool_failure(self):
4467 "Test that jobs are reported after a nodepool failure"
4468
4469 self.fake_nodepool.paused = True
4470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4471 A.addApproval('code-review', 2)
4472 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4473 self.waitUntilSettled()
4474
4475 req = self.fake_nodepool.getNodeRequests()[0]
4476 self.fake_nodepool.addFailRequest(req)
4477
4478 self.fake_nodepool.paused = False
4479 self.waitUntilSettled()
4480
4481 self.assertEqual(A.data['status'], 'NEW')
4482 self.assertEqual(A.reported, 2)
4483 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4484 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4485 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4486
Arieb6f068c2016-10-09 13:11:06 +03004487
James E. Blaira002b032017-04-18 10:35:48 -07004488class TestExecutor(ZuulTestCase):
4489 tenant_config_file = 'config/single-tenant/main.yaml'
4490
4491 def assertFinalState(self):
4492 # In this test, we expect to shut down in a non-final state,
4493 # so skip these checks.
4494 pass
4495
4496 def assertCleanShutdown(self):
4497 self.log.debug("Assert clean shutdown")
4498
4499 # After shutdown, make sure no jobs are running
4500 self.assertEqual({}, self.executor_server.job_workers)
4501
4502 # Make sure that git.Repo objects have been garbage collected.
4503 repos = []
4504 gc.collect()
4505 for obj in gc.get_objects():
4506 if isinstance(obj, git.Repo):
4507 self.log.debug("Leaked git repo object: %s" % repr(obj))
4508 repos.append(obj)
4509 self.assertEqual(len(repos), 0)
4510
4511 def test_executor_shutdown(self):
4512 "Test that the executor can shut down with jobs running"
4513
4514 self.executor_server.hold_jobs_in_build = True
4515 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4516 A.addApproval('code-review', 2)
4517 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4518 self.waitUntilSettled()
4519
4520
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004521class TestDependencyGraph(ZuulTestCase):
4522 tenant_config_file = 'config/dependency-graph/main.yaml'
4523
4524 def test_dependeny_graph_dispatch_jobs_once(self):
4525 "Test a job in a dependency graph is queued only once"
4526 # Job dependencies, starting with A
4527 # A
4528 # / \
4529 # B C
4530 # / \ / \
4531 # D F E
4532 # |
4533 # G
4534
4535 self.executor_server.hold_jobs_in_build = True
4536 change = self.fake_gerrit.addFakeChange(
4537 'org/project', 'master', 'change')
4538 change.addApproval('code-review', 2)
4539 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4540
4541 self.waitUntilSettled()
4542 self.assertEqual([b.name for b in self.builds], ['A'])
4543
4544 self.executor_server.release('A')
4545 self.waitUntilSettled()
4546 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4547
4548 self.executor_server.release('B')
4549 self.waitUntilSettled()
4550 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4551
4552 self.executor_server.release('D')
4553 self.waitUntilSettled()
4554 self.assertEqual([b.name for b in self.builds], ['C'])
4555
4556 self.executor_server.release('C')
4557 self.waitUntilSettled()
4558 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4559
4560 self.executor_server.release('F')
4561 self.waitUntilSettled()
4562 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4563
4564 self.executor_server.release('G')
4565 self.waitUntilSettled()
4566 self.assertEqual([b.name for b in self.builds], ['E'])
4567
4568 self.executor_server.release('E')
4569 self.waitUntilSettled()
4570 self.assertEqual(len(self.builds), 0)
4571
4572 self.executor_server.hold_jobs_in_build = False
4573 self.executor_server.release()
4574 self.waitUntilSettled()
4575
4576 self.assertEqual(len(self.builds), 0)
4577 self.assertEqual(len(self.history), 7)
4578
4579 self.assertEqual(change.data['status'], 'MERGED')
4580 self.assertEqual(change.reported, 2)
4581
4582 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4583 "Test that a job waits till all dependencies are successful"
4584 # Job dependencies, starting with A
4585 # A
4586 # / \
4587 # B C*
4588 # / \ / \
4589 # D F E
4590 # |
4591 # G
4592
4593 self.executor_server.hold_jobs_in_build = True
4594 change = self.fake_gerrit.addFakeChange(
4595 'org/project', 'master', 'change')
4596 change.addApproval('code-review', 2)
4597
4598 self.executor_server.failJob('C', change)
4599
4600 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4601
4602 self.waitUntilSettled()
4603 self.assertEqual([b.name for b in self.builds], ['A'])
4604
4605 self.executor_server.release('A')
4606 self.waitUntilSettled()
4607 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4608
4609 self.executor_server.release('B')
4610 self.waitUntilSettled()
4611 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4612
4613 self.executor_server.release('D')
4614 self.waitUntilSettled()
4615 self.assertEqual([b.name for b in self.builds], ['C'])
4616
4617 self.executor_server.release('C')
4618 self.waitUntilSettled()
4619 self.assertEqual(len(self.builds), 0)
4620
4621 self.executor_server.hold_jobs_in_build = False
4622 self.executor_server.release()
4623 self.waitUntilSettled()
4624
4625 self.assertEqual(len(self.builds), 0)
4626 self.assertEqual(len(self.history), 4)
4627
4628 self.assertEqual(change.data['status'], 'NEW')
4629 self.assertEqual(change.reported, 2)
4630
4631
Arieb6f068c2016-10-09 13:11:06 +03004632class TestDuplicatePipeline(ZuulTestCase):
4633 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4634
4635 def test_duplicate_pipelines(self):
4636 "Test that a change matching multiple pipelines works"
4637
4638 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4639 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4640 self.waitUntilSettled()
4641
4642 self.assertHistory([
4643 dict(name='project-test1', result='SUCCESS', changes='1,1',
4644 pipeline='dup1'),
4645 dict(name='project-test1', result='SUCCESS', changes='1,1',
4646 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004647 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004648
4649 self.assertEqual(len(A.messages), 2)
4650
Paul Belangera46a3742016-11-09 14:23:03 -05004651 if 'dup1' in A.messages[0]:
4652 self.assertIn('dup1', A.messages[0])
4653 self.assertNotIn('dup2', A.messages[0])
4654 self.assertIn('project-test1', A.messages[0])
4655 self.assertIn('dup2', A.messages[1])
4656 self.assertNotIn('dup1', A.messages[1])
4657 self.assertIn('project-test1', A.messages[1])
4658 else:
4659 self.assertIn('dup1', A.messages[1])
4660 self.assertNotIn('dup2', A.messages[1])
4661 self.assertIn('project-test1', A.messages[1])
4662 self.assertIn('dup2', A.messages[0])
4663 self.assertNotIn('dup1', A.messages[0])
4664 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004665
4666
Paul Belanger86085b32016-11-03 12:48:57 -04004667class TestSchedulerTemplatedProject(ZuulTestCase):
4668 tenant_config_file = 'config/templated-project/main.yaml'
4669
Paul Belanger174a8272017-03-14 13:20:10 -04004670 def test_job_from_templates_executed(self):
4671 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004672
4673 A = self.fake_gerrit.addFakeChange(
4674 'org/templated-project', 'master', 'A')
4675 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4676 self.waitUntilSettled()
4677
4678 self.assertEqual(self.getJobFromHistory('project-test1').result,
4679 'SUCCESS')
4680 self.assertEqual(self.getJobFromHistory('project-test2').result,
4681 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004682
4683 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004684 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004685
4686 A = self.fake_gerrit.addFakeChange(
4687 'org/layered-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')
4695 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4696 ).result, 'SUCCESS')
4697 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4698 ).result, 'SUCCESS')
4699 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4700 ).result, 'SUCCESS')
4701 self.assertEqual(self.getJobFromHistory('project-test6').result,
4702 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004703
4704
4705class TestSchedulerSuccessURL(ZuulTestCase):
4706 tenant_config_file = 'config/success-url/main.yaml'
4707
4708 def test_success_url(self):
4709 "Ensure bad build params are ignored"
4710 self.sched.reconfigure(self.config)
4711 self.init_repo('org/docs')
4712
4713 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4714 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4715 self.waitUntilSettled()
4716
4717 # Both builds ran: docs-draft-test + docs-draft-test2
4718 self.assertEqual(len(self.history), 2)
4719
4720 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004721 for build in self.history:
4722 if build.name == 'docs-draft-test':
4723 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004724 elif build.name == 'docs-draft-test2':
4725 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004726
4727 # Two msgs: 'Starting...' + results
4728 self.assertEqual(len(self.smtp_messages), 2)
4729 body = self.smtp_messages[1]['body'].splitlines()
4730 self.assertEqual('Build succeeded.', body[0])
4731
4732 self.assertIn(
4733 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4734 'docs-draft-test/%s/publish-docs/' % uuid,
4735 body[2])
4736
Paul Belanger174a8272017-03-14 13:20:10 -04004737 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004738 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004739 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4740 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004741 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004742 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004743
4744
Adam Gandelman4da00f62016-12-09 15:47:33 -08004745class TestSchedulerMerges(ZuulTestCase):
4746 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004747
4748 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004749 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004750 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004751 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004752 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4753 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4754 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4755 A.addApproval('code-review', 2)
4756 B.addApproval('code-review', 2)
4757 C.addApproval('code-review', 2)
4758 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4759 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4760 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4761 self.waitUntilSettled()
4762
4763 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004764 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4765 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004766 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004767 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004768 repo_messages.reverse()
4769
Paul Belanger174a8272017-03-14 13:20:10 -04004770 self.executor_server.hold_jobs_in_build = False
4771 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004772 self.waitUntilSettled()
4773
4774 return repo_messages
4775
4776 def _test_merge(self, mode):
4777 us_path = os.path.join(
4778 self.upstream_root, 'org/project-%s' % mode)
4779 expected_messages = [
4780 'initial commit',
4781 'add content from fixture',
4782 # the intermediate commits order is nondeterministic
4783 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4784 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4785 ]
4786 result = self._test_project_merge_mode(mode)
4787 self.assertEqual(result[:2], expected_messages[:2])
4788 self.assertEqual(result[-2:], expected_messages[-2:])
4789
4790 def test_project_merge_mode_merge(self):
4791 self._test_merge('merge')
4792
4793 def test_project_merge_mode_merge_resolve(self):
4794 self._test_merge('merge-resolve')
4795
4796 def test_project_merge_mode_cherrypick(self):
4797 expected_messages = [
4798 'initial commit',
4799 'add content from fixture',
4800 'A-1',
4801 'B-1',
4802 'C-1']
4803 result = self._test_project_merge_mode('cherry-pick')
4804 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004805
4806 def test_merge_branch(self):
4807 "Test that the right commits are on alternate branches"
4808 self.create_branch('org/project-merge-branches', 'mp')
4809
Paul Belanger174a8272017-03-14 13:20:10 -04004810 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004811 A = self.fake_gerrit.addFakeChange(
4812 'org/project-merge-branches', 'mp', 'A')
4813 B = self.fake_gerrit.addFakeChange(
4814 'org/project-merge-branches', 'mp', 'B')
4815 C = self.fake_gerrit.addFakeChange(
4816 'org/project-merge-branches', 'mp', 'C')
4817 A.addApproval('code-review', 2)
4818 B.addApproval('code-review', 2)
4819 C.addApproval('code-review', 2)
4820 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4821 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4822 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4823 self.waitUntilSettled()
4824
Paul Belanger174a8272017-03-14 13:20:10 -04004825 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004826 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004827 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004828 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004829 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004830 self.waitUntilSettled()
4831
4832 build = self.builds[-1]
4833 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004834 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4835 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004836 repo = git.Repo(path)
4837
James E. Blair247cab72017-07-20 16:52:36 -07004838 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004839 repo_messages.reverse()
4840 correct_messages = [
4841 'initial commit',
4842 'add content from fixture',
4843 'mp commit',
4844 'A-1', 'B-1', 'C-1']
4845 self.assertEqual(repo_messages, correct_messages)
4846
Paul Belanger174a8272017-03-14 13:20:10 -04004847 self.executor_server.hold_jobs_in_build = False
4848 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004849 self.waitUntilSettled()
4850
4851 def test_merge_multi_branch(self):
4852 "Test that dependent changes on multiple branches are merged"
4853 self.create_branch('org/project-merge-branches', 'mp')
4854
Paul Belanger174a8272017-03-14 13:20:10 -04004855 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004856 A = self.fake_gerrit.addFakeChange(
4857 'org/project-merge-branches', 'master', 'A')
4858 B = self.fake_gerrit.addFakeChange(
4859 'org/project-merge-branches', 'mp', 'B')
4860 C = self.fake_gerrit.addFakeChange(
4861 'org/project-merge-branches', 'master', 'C')
4862 A.addApproval('code-review', 2)
4863 B.addApproval('code-review', 2)
4864 C.addApproval('code-review', 2)
4865 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4866 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4867 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4868 self.waitUntilSettled()
4869
4870 job_A = None
4871 for job in self.builds:
4872 if 'project-merge' in job.name:
4873 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004874
James E. Blair2a535672017-04-27 12:03:15 -07004875 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4876 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004877 repo = git.Repo(path)
4878 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004879 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004880 repo_messages.reverse()
4881 correct_messages = [
4882 'initial commit', 'add content from fixture', 'A-1']
4883 self.assertEqual(repo_messages, correct_messages)
4884
Paul Belanger174a8272017-03-14 13:20:10 -04004885 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004886 self.waitUntilSettled()
4887
4888 job_B = None
4889 for job in self.builds:
4890 if 'project-merge' in job.name:
4891 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004892
James E. Blair2a535672017-04-27 12:03:15 -07004893 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4894 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004895 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004896 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004897 repo_messages.reverse()
4898 correct_messages = [
4899 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4900 self.assertEqual(repo_messages, correct_messages)
4901
Paul Belanger174a8272017-03-14 13:20:10 -04004902 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004903 self.waitUntilSettled()
4904
4905 job_C = None
4906 for job in self.builds:
4907 if 'project-merge' in job.name:
4908 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07004909
James E. Blair2a535672017-04-27 12:03:15 -07004910 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4911 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004912 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004913 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004914
4915 repo_messages.reverse()
4916 correct_messages = [
4917 'initial commit', 'add content from fixture',
4918 'A-1', 'C-1']
4919 # Ensure the right commits are in the history for this ref
4920 self.assertEqual(repo_messages, correct_messages)
4921
Paul Belanger174a8272017-03-14 13:20:10 -04004922 self.executor_server.hold_jobs_in_build = False
4923 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004924 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004925
4926
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004927class TestSemaphore(ZuulTestCase):
4928 tenant_config_file = 'config/semaphore/main.yaml'
4929
4930 def test_semaphore_one(self):
4931 "Test semaphores with max=1 (mutex)"
4932 tenant = self.sched.abide.tenants.get('tenant-one')
4933
4934 self.executor_server.hold_jobs_in_build = True
4935
4936 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4937 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4938 self.assertFalse('test-semaphore' in
4939 tenant.semaphore_handler.semaphores)
4940
4941 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4942 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4943 self.waitUntilSettled()
4944
4945 self.assertEqual(len(self.builds), 3)
4946 self.assertEqual(self.builds[0].name, 'project-test1')
4947 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4948 self.assertEqual(self.builds[2].name, 'project-test1')
4949
4950 self.executor_server.release('semaphore-one-test1')
4951 self.waitUntilSettled()
4952
4953 self.assertEqual(len(self.builds), 3)
4954 self.assertEqual(self.builds[0].name, 'project-test1')
4955 self.assertEqual(self.builds[1].name, 'project-test1')
4956 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4957 self.assertTrue('test-semaphore' in
4958 tenant.semaphore_handler.semaphores)
4959
4960 self.executor_server.release('semaphore-one-test2')
4961 self.waitUntilSettled()
4962
4963 self.assertEqual(len(self.builds), 3)
4964 self.assertEqual(self.builds[0].name, 'project-test1')
4965 self.assertEqual(self.builds[1].name, 'project-test1')
4966 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4967 self.assertTrue('test-semaphore' in
4968 tenant.semaphore_handler.semaphores)
4969
4970 self.executor_server.release('semaphore-one-test1')
4971 self.waitUntilSettled()
4972
4973 self.assertEqual(len(self.builds), 3)
4974 self.assertEqual(self.builds[0].name, 'project-test1')
4975 self.assertEqual(self.builds[1].name, 'project-test1')
4976 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4977 self.assertTrue('test-semaphore' in
4978 tenant.semaphore_handler.semaphores)
4979
4980 self.executor_server.release('semaphore-one-test2')
4981 self.waitUntilSettled()
4982
4983 self.assertEqual(len(self.builds), 2)
4984 self.assertEqual(self.builds[0].name, 'project-test1')
4985 self.assertEqual(self.builds[1].name, 'project-test1')
4986 self.assertFalse('test-semaphore' in
4987 tenant.semaphore_handler.semaphores)
4988
4989 self.executor_server.hold_jobs_in_build = False
4990 self.executor_server.release()
4991
4992 self.waitUntilSettled()
4993 self.assertEqual(len(self.builds), 0)
4994
4995 self.assertEqual(A.reported, 1)
4996 self.assertEqual(B.reported, 1)
4997 self.assertFalse('test-semaphore' in
4998 tenant.semaphore_handler.semaphores)
4999
5000 def test_semaphore_two(self):
5001 "Test semaphores with max>1"
5002 tenant = self.sched.abide.tenants.get('tenant-one')
5003
5004 self.executor_server.hold_jobs_in_build = True
5005 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5006 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5007 self.assertFalse('test-semaphore-two' in
5008 tenant.semaphore_handler.semaphores)
5009
5010 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5011 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5012 self.waitUntilSettled()
5013
5014 self.assertEqual(len(self.builds), 4)
5015 self.assertEqual(self.builds[0].name, 'project-test1')
5016 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5017 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5018 self.assertEqual(self.builds[3].name, 'project-test1')
5019 self.assertTrue('test-semaphore-two' in
5020 tenant.semaphore_handler.semaphores)
5021 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5022 'test-semaphore-two', [])), 2)
5023
5024 self.executor_server.release('semaphore-two-test1')
5025 self.waitUntilSettled()
5026
5027 self.assertEqual(len(self.builds), 4)
5028 self.assertEqual(self.builds[0].name, 'project-test1')
5029 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5030 self.assertEqual(self.builds[2].name, 'project-test1')
5031 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5032 self.assertTrue('test-semaphore-two' in
5033 tenant.semaphore_handler.semaphores)
5034 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5035 'test-semaphore-two', [])), 2)
5036
5037 self.executor_server.release('semaphore-two-test2')
5038 self.waitUntilSettled()
5039
5040 self.assertEqual(len(self.builds), 4)
5041 self.assertEqual(self.builds[0].name, 'project-test1')
5042 self.assertEqual(self.builds[1].name, 'project-test1')
5043 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5044 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5045 self.assertTrue('test-semaphore-two' in
5046 tenant.semaphore_handler.semaphores)
5047 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5048 'test-semaphore-two', [])), 2)
5049
5050 self.executor_server.release('semaphore-two-test1')
5051 self.waitUntilSettled()
5052
5053 self.assertEqual(len(self.builds), 3)
5054 self.assertEqual(self.builds[0].name, 'project-test1')
5055 self.assertEqual(self.builds[1].name, 'project-test1')
5056 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5057 self.assertTrue('test-semaphore-two' in
5058 tenant.semaphore_handler.semaphores)
5059 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5060 'test-semaphore-two', [])), 1)
5061
5062 self.executor_server.release('semaphore-two-test2')
5063 self.waitUntilSettled()
5064
5065 self.assertEqual(len(self.builds), 2)
5066 self.assertEqual(self.builds[0].name, 'project-test1')
5067 self.assertEqual(self.builds[1].name, 'project-test1')
5068 self.assertFalse('test-semaphore-two' in
5069 tenant.semaphore_handler.semaphores)
5070
5071 self.executor_server.hold_jobs_in_build = False
5072 self.executor_server.release()
5073
5074 self.waitUntilSettled()
5075 self.assertEqual(len(self.builds), 0)
5076
5077 self.assertEqual(A.reported, 1)
5078 self.assertEqual(B.reported, 1)
5079
5080 def test_semaphore_abandon(self):
5081 "Test abandon with job semaphores"
5082 self.executor_server.hold_jobs_in_build = True
5083 tenant = self.sched.abide.tenants.get('tenant-one')
5084 check_pipeline = tenant.layout.pipelines['check']
5085
5086 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5087 self.assertFalse('test-semaphore' in
5088 tenant.semaphore_handler.semaphores)
5089
5090 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5091 self.waitUntilSettled()
5092
5093 self.assertTrue('test-semaphore' in
5094 tenant.semaphore_handler.semaphores)
5095
5096 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5097 self.waitUntilSettled()
5098
5099 # The check pipeline should be empty
5100 items = check_pipeline.getAllItems()
5101 self.assertEqual(len(items), 0)
5102
5103 # The semaphore should be released
5104 self.assertFalse('test-semaphore' in
5105 tenant.semaphore_handler.semaphores)
5106
5107 self.executor_server.hold_jobs_in_build = False
5108 self.executor_server.release()
5109 self.waitUntilSettled()
5110
Tobias Henkel0f714002017-06-30 23:30:52 +02005111 def test_semaphore_new_patchset(self):
5112 "Test new patchset with job semaphores"
5113 self.executor_server.hold_jobs_in_build = True
5114 tenant = self.sched.abide.tenants.get('tenant-one')
5115 check_pipeline = tenant.layout.pipelines['check']
5116
5117 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5118 self.assertFalse('test-semaphore' in
5119 tenant.semaphore_handler.semaphores)
5120
5121 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5122 self.waitUntilSettled()
5123
5124 self.assertTrue('test-semaphore' in
5125 tenant.semaphore_handler.semaphores)
5126 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5127 self.assertEqual(len(semaphore), 1)
5128
5129 A.addPatchset()
5130 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5131 self.waitUntilSettled()
5132
5133 self.assertTrue('test-semaphore' in
5134 tenant.semaphore_handler.semaphores)
5135 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5136 self.assertEqual(len(semaphore), 1)
5137
5138 items = check_pipeline.getAllItems()
5139 self.assertEqual(items[0].change.number, '1')
5140 self.assertEqual(items[0].change.patchset, '2')
5141 self.assertTrue(items[0].live)
5142
5143 self.executor_server.hold_jobs_in_build = False
5144 self.executor_server.release()
5145 self.waitUntilSettled()
5146
5147 # The semaphore should be released
5148 self.assertFalse('test-semaphore' in
5149 tenant.semaphore_handler.semaphores)
5150
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005151 def test_semaphore_reconfigure(self):
5152 "Test reconfigure with job semaphores"
5153 self.executor_server.hold_jobs_in_build = True
5154 tenant = self.sched.abide.tenants.get('tenant-one')
5155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5156 self.assertFalse('test-semaphore' in
5157 tenant.semaphore_handler.semaphores)
5158
5159 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5160 self.waitUntilSettled()
5161
5162 self.assertTrue('test-semaphore' in
5163 tenant.semaphore_handler.semaphores)
5164
5165 # reconfigure without layout change
5166 self.sched.reconfigure(self.config)
5167 self.waitUntilSettled()
5168 tenant = self.sched.abide.tenants.get('tenant-one')
5169
5170 # semaphore still must be held
5171 self.assertTrue('test-semaphore' in
5172 tenant.semaphore_handler.semaphores)
5173
5174 self.commitConfigUpdate(
5175 'common-config',
5176 'config/semaphore/zuul-reconfiguration.yaml')
5177 self.sched.reconfigure(self.config)
5178 self.waitUntilSettled()
5179 tenant = self.sched.abide.tenants.get('tenant-one')
5180
5181 self.executor_server.release('project-test1')
5182 self.waitUntilSettled()
5183
5184 # There should be no builds anymore
5185 self.assertEqual(len(self.builds), 0)
5186
5187 # The semaphore should be released
5188 self.assertFalse('test-semaphore' in
5189 tenant.semaphore_handler.semaphores)
5190
5191
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005192class TestSemaphoreMultiTenant(ZuulTestCase):
5193 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5194
5195 def test_semaphore_tenant_isolation(self):
5196 "Test semaphores in multiple tenants"
5197
5198 self.waitUntilSettled()
5199 tenant_one = self.sched.abide.tenants.get('tenant-one')
5200 tenant_two = self.sched.abide.tenants.get('tenant-two')
5201
5202 self.executor_server.hold_jobs_in_build = True
5203 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5204 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5205 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5206 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5207 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5208 self.assertFalse('test-semaphore' in
5209 tenant_one.semaphore_handler.semaphores)
5210 self.assertFalse('test-semaphore' in
5211 tenant_two.semaphore_handler.semaphores)
5212
5213 # add patches to project1 of tenant-one
5214 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5215 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5216 self.waitUntilSettled()
5217
5218 # one build of project1-test1 must run
5219 # semaphore of tenant-one must be acquired once
5220 # semaphore of tenant-two must not be acquired
5221 self.assertEqual(len(self.builds), 1)
5222 self.assertEqual(self.builds[0].name, 'project1-test1')
5223 self.assertTrue('test-semaphore' in
5224 tenant_one.semaphore_handler.semaphores)
5225 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5226 'test-semaphore', [])), 1)
5227 self.assertFalse('test-semaphore' in
5228 tenant_two.semaphore_handler.semaphores)
5229
5230 # add patches to project2 of tenant-two
5231 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5232 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5233 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5234 self.waitUntilSettled()
5235
5236 # one build of project1-test1 must run
5237 # two builds of project2-test1 must run
5238 # semaphore of tenant-one must be acquired once
5239 # semaphore of tenant-two must be acquired twice
5240 self.assertEqual(len(self.builds), 3)
5241 self.assertEqual(self.builds[0].name, 'project1-test1')
5242 self.assertEqual(self.builds[1].name, 'project2-test1')
5243 self.assertEqual(self.builds[2].name, 'project2-test1')
5244 self.assertTrue('test-semaphore' in
5245 tenant_one.semaphore_handler.semaphores)
5246 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5247 'test-semaphore', [])), 1)
5248 self.assertTrue('test-semaphore' in
5249 tenant_two.semaphore_handler.semaphores)
5250 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5251 'test-semaphore', [])), 2)
5252
5253 self.executor_server.release('project1-test1')
5254 self.waitUntilSettled()
5255
5256 # one build of project1-test1 must run
5257 # two builds of project2-test1 must run
5258 # semaphore of tenant-one must be acquired once
5259 # semaphore of tenant-two must be acquired twice
5260 self.assertEqual(len(self.builds), 3)
5261 self.assertEqual(self.builds[0].name, 'project2-test1')
5262 self.assertEqual(self.builds[1].name, 'project2-test1')
5263 self.assertEqual(self.builds[2].name, 'project1-test1')
5264 self.assertTrue('test-semaphore' in
5265 tenant_one.semaphore_handler.semaphores)
5266 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5267 'test-semaphore', [])), 1)
5268 self.assertTrue('test-semaphore' in
5269 tenant_two.semaphore_handler.semaphores)
5270 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5271 'test-semaphore', [])), 2)
5272
5273 self.executor_server.release('project2-test1')
5274 self.waitUntilSettled()
5275
5276 # one build of project1-test1 must run
5277 # one build of project2-test1 must run
5278 # semaphore of tenant-one must be acquired once
5279 # semaphore of tenant-two must be acquired once
5280 self.assertEqual(len(self.builds), 2)
5281 self.assertTrue('test-semaphore' in
5282 tenant_one.semaphore_handler.semaphores)
5283 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5284 'test-semaphore', [])), 1)
5285 self.assertTrue('test-semaphore' in
5286 tenant_two.semaphore_handler.semaphores)
5287 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5288 'test-semaphore', [])), 1)
5289
5290 self.executor_server.hold_jobs_in_build = False
5291 self.executor_server.release()
5292
5293 self.waitUntilSettled()
5294
5295 # no build must run
5296 # semaphore of tenant-one must not be acquired
5297 # semaphore of tenant-two must not be acquired
5298 self.assertEqual(len(self.builds), 0)
5299 self.assertFalse('test-semaphore' in
5300 tenant_one.semaphore_handler.semaphores)
5301 self.assertFalse('test-semaphore' in
5302 tenant_two.semaphore_handler.semaphores)
5303
5304 self.assertEqual(A.reported, 1)
5305 self.assertEqual(B.reported, 1)
5306
5307
5308class TestSemaphoreInRepo(ZuulTestCase):
5309 tenant_config_file = 'config/in-repo/main.yaml'
5310
5311 def test_semaphore_in_repo(self):
5312 "Test semaphores in repo config"
5313
5314 # This tests dynamic semaphore handling in project repos. The semaphore
5315 # max value should not be evaluated dynamically but must be updated
5316 # after the change lands.
5317
5318 self.waitUntilSettled()
5319 tenant = self.sched.abide.tenants.get('tenant-one')
5320
5321 in_repo_conf = textwrap.dedent(
5322 """
5323 - job:
5324 name: project-test2
5325 semaphore: test-semaphore
5326
5327 - project:
5328 name: org/project
5329 tenant-one-gate:
5330 jobs:
5331 - project-test2
5332
5333 # the max value in dynamic layout must be ignored
5334 - semaphore:
5335 name: test-semaphore
5336 max: 2
5337 """)
5338
5339 in_repo_playbook = textwrap.dedent(
5340 """
5341 - hosts: all
5342 tasks: []
5343 """)
5344
5345 file_dict = {'.zuul.yaml': in_repo_conf,
5346 'playbooks/project-test2.yaml': in_repo_playbook}
5347 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5348 files=file_dict)
5349 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5350 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5351 B.setDependsOn(A, 1)
5352 C.setDependsOn(A, 1)
5353
5354 self.executor_server.hold_jobs_in_build = True
5355
5356 A.addApproval('code-review', 2)
5357 B.addApproval('code-review', 2)
5358 C.addApproval('code-review', 2)
5359 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5360 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5361 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5362 self.waitUntilSettled()
5363
5364 # check that the layout in a queue item still has max value of 1
5365 # for test-semaphore
5366 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5367 queue = None
5368 for queue_candidate in pipeline.queues:
5369 if queue_candidate.name == 'org/project':
5370 queue = queue_candidate
5371 break
5372 queue_item = queue.queue[0]
5373 item_dynamic_layout = queue_item.current_build_set.layout
5374 dynamic_test_semaphore = \
5375 item_dynamic_layout.semaphores.get('test-semaphore')
5376 self.assertEqual(dynamic_test_semaphore.max, 1)
5377
5378 # one build must be in queue, one semaphores acquired
5379 self.assertEqual(len(self.builds), 1)
5380 self.assertEqual(self.builds[0].name, 'project-test2')
5381 self.assertTrue('test-semaphore' in
5382 tenant.semaphore_handler.semaphores)
5383 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5384 'test-semaphore', [])), 1)
5385
5386 self.executor_server.release('project-test2')
5387 self.waitUntilSettled()
5388
5389 # change A must be merged
5390 self.assertEqual(A.data['status'], 'MERGED')
5391 self.assertEqual(A.reported, 2)
5392
5393 # send change-merged event as the gerrit mock doesn't send it
5394 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5395 self.waitUntilSettled()
5396
5397 # now that change A was merged, the new semaphore max must be effective
5398 tenant = self.sched.abide.tenants.get('tenant-one')
5399 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5400
5401 # two builds must be in queue, two semaphores acquired
5402 self.assertEqual(len(self.builds), 2)
5403 self.assertEqual(self.builds[0].name, 'project-test2')
5404 self.assertEqual(self.builds[1].name, 'project-test2')
5405 self.assertTrue('test-semaphore' in
5406 tenant.semaphore_handler.semaphores)
5407 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5408 'test-semaphore', [])), 2)
5409
5410 self.executor_server.release('project-test2')
5411 self.waitUntilSettled()
5412
5413 self.assertEqual(len(self.builds), 0)
5414 self.assertFalse('test-semaphore' in
5415 tenant.semaphore_handler.semaphores)
5416
5417 self.executor_server.hold_jobs_in_build = False
5418 self.executor_server.release()
5419
5420 self.waitUntilSettled()
5421 self.assertEqual(len(self.builds), 0)
5422
5423 self.assertEqual(A.reported, 2)
5424 self.assertEqual(B.reported, 2)
5425 self.assertEqual(C.reported, 2)