blob: 5dd3f4e63c57b440624900b42d1fd0d0d05f4c69 [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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020052 A.addApproval('Code-Review', 2)
53 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Paul Belanger0a21f0a2017-06-13 13:14:42 -040054 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020075 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. Blair2ef29e92017-07-21 15:25:05 -0700110 for build in self.history:
111 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700112
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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200124 A.addApproval('Code-Review', 2)
125 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700126 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200146 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200150 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200222 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200227 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200282 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200286 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200289 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800290 self.waitUntilSettled()
291
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200292 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200356 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200362 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200471 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200477 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200548 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200554 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200622 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200667 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200674 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200770 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200789 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200797 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200833 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200862 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200901 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200910 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:
James E. Blaire675d682017-07-21 15:29:35 -0700916 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700917 build.release()
918 self.waitUntilSettled()
919 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700920 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700921 build.release()
922 self.waitUntilSettled()
923
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200924 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200959 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200963 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200979 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001036 A.addApproval('Code-Review', 2)
1037 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001038 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001215 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001230 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001253 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001266 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001319 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
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001326 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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001377 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001378 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001379 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. Blair2ef29e92017-07-21 15:25:05 -07001395 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1396 parameters['zuul']['voting'])
1397 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1398 parameters['zuul']['voting'])
1399 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1400 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001401
James E. Blaire0487072012-08-29 17:38:31 -07001402 def test_check_queue_success(self):
1403 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001404
James E. Blaire0487072012-08-29 17:38:31 -07001405 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1406 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1407
1408 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001409
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001410 self.assertEqual(A.data['status'], 'NEW')
1411 self.assertEqual(A.reported, 1)
1412 self.assertEqual(self.getJobFromHistory('project-merge').result,
1413 'SUCCESS')
1414 self.assertEqual(self.getJobFromHistory('project-test1').result,
1415 'SUCCESS')
1416 self.assertEqual(self.getJobFromHistory('project-test2').result,
1417 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001418
1419 def test_check_queue_failure(self):
1420 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001421
James E. Blaire0487072012-08-29 17:38:31 -07001422 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001423 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001424 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1425
1426 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001427
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001428 self.assertEqual(A.data['status'], 'NEW')
1429 self.assertEqual(A.reported, 1)
1430 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001431 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001432 self.assertEqual(self.getJobFromHistory('project-test1').result,
1433 'SUCCESS')
1434 self.assertEqual(self.getJobFromHistory('project-test2').result,
1435 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001436
James E. Blair7cb84542017-04-19 13:35:05 -07001437 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001438 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001439 # This particular test does a large amount of merges and needs a little
1440 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001441 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001442 "test that dependent changes behind dequeued changes work"
1443 # This complicated test is a reproduction of a real life bug
1444 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001445
Paul Belanger174a8272017-03-14 13:20:10 -04001446 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001447 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1448 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1449 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1450 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1451 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1452 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1453 D.setDependsOn(C, 1)
1454 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001455 A.addApproval('Code-Review', 2)
1456 B.addApproval('Code-Review', 2)
1457 C.addApproval('Code-Review', 2)
1458 D.addApproval('Code-Review', 2)
1459 E.addApproval('Code-Review', 2)
1460 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001461
1462 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001463
1464 # Change object re-use in the gerrit trigger is hidden if
1465 # changes are added in quick succession; waiting makes it more
1466 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001467 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001468 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001469 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001470 self.waitUntilSettled()
1471
Paul Belanger174a8272017-03-14 13:20:10 -04001472 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001473 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001474 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001475 self.waitUntilSettled()
1476
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001477 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001478 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001479 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001480 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001481 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001482 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001483 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001484 self.waitUntilSettled()
1485
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()
Paul Belanger174a8272017-03-14 13:20:10 -04001490 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001491 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001492 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001493 self.waitUntilSettled()
1494
1495 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001496
1497 # Grab pointers to the jobs we want to release before
1498 # releasing any, because list indexes may change as
1499 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001500 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001501 a.release()
1502 b.release()
1503 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001504 self.waitUntilSettled()
1505
Paul Belanger174a8272017-03-14 13:20:10 -04001506 self.executor_server.hold_jobs_in_build = False
1507 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001508 self.waitUntilSettled()
1509
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001510 self.assertEqual(A.data['status'], 'NEW')
1511 self.assertEqual(B.data['status'], 'MERGED')
1512 self.assertEqual(C.data['status'], 'MERGED')
1513 self.assertEqual(D.data['status'], 'MERGED')
1514 self.assertEqual(E.data['status'], 'MERGED')
1515 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001516
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001517 self.assertEqual(A.reported, 2)
1518 self.assertEqual(B.reported, 2)
1519 self.assertEqual(C.reported, 2)
1520 self.assertEqual(D.reported, 2)
1521 self.assertEqual(E.reported, 2)
1522 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001523
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001524 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1525 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001526
1527 def test_merger_repack(self):
1528 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001529
James E. Blair05fed602012-09-07 12:45:24 -07001530 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001531 A.addApproval('Code-Review', 2)
1532 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001533 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001534 self.assertEqual(self.getJobFromHistory('project-merge').result,
1535 'SUCCESS')
1536 self.assertEqual(self.getJobFromHistory('project-test1').result,
1537 'SUCCESS')
1538 self.assertEqual(self.getJobFromHistory('project-test2').result,
1539 'SUCCESS')
1540 self.assertEqual(A.data['status'], 'MERGED')
1541 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001542 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001543 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001544
James E. Blair2a535672017-04-27 12:03:15 -07001545 path = os.path.join(self.merger_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. Blair2a535672017-04-27 12:03:15 -07001549 path = os.path.join(self.executor_src_root, "review.example.com",
1550 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001551 if os.path.exists(path):
1552 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001553
1554 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001555 A.addApproval('Code-Review', 2)
1556 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001557 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001558 self.assertEqual(self.getJobFromHistory('project-merge').result,
1559 'SUCCESS')
1560 self.assertEqual(self.getJobFromHistory('project-test1').result,
1561 'SUCCESS')
1562 self.assertEqual(self.getJobFromHistory('project-test2').result,
1563 'SUCCESS')
1564 self.assertEqual(A.data['status'], 'MERGED')
1565 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001566
James E. Blair4886f282012-11-15 09:27:33 -08001567 def test_merger_repack_large_change(self):
1568 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001569 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001570 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001571 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001572 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001573 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001574 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001575 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001577 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001578 # TODOv3(jeblair): add hostname to upstream root
1579 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001580 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001581 path = os.path.join(self.merger_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. Blair2a535672017-04-27 12:03:15 -07001585 path = os.path.join(self.executor_src_root, 'review.example.com',
1586 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001587 if os.path.exists(path):
1588 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001589
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001590 A.addApproval('Code-Review', 2)
1591 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001592 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001593 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001594 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001595 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001596 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001597 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001598 'SUCCESS')
1599 self.assertEqual(A.data['status'], 'MERGED')
1600 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001601
James E. Blair2fa50962013-01-30 21:50:41 -08001602 def test_new_patchset_dequeues_old(self):
1603 "Test that a new patchset causes the old to be dequeued"
1604 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001605 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001606 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1607 M.setMerged()
1608
1609 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1610 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1611 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1612 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001613 A.addApproval('Code-Review', 2)
1614 B.addApproval('Code-Review', 2)
1615 C.addApproval('Code-Review', 2)
1616 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001617
1618 C.setDependsOn(B, 1)
1619 B.setDependsOn(A, 1)
1620 A.setDependsOn(M, 1)
1621
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001622 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1623 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1624 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1625 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001626 self.waitUntilSettled()
1627
1628 B.addPatchset()
1629 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1630 self.waitUntilSettled()
1631
Paul Belanger174a8272017-03-14 13:20:10 -04001632 self.executor_server.hold_jobs_in_build = False
1633 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001634 self.waitUntilSettled()
1635
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001636 self.assertEqual(A.data['status'], 'MERGED')
1637 self.assertEqual(A.reported, 2)
1638 self.assertEqual(B.data['status'], 'NEW')
1639 self.assertEqual(B.reported, 2)
1640 self.assertEqual(C.data['status'], 'NEW')
1641 self.assertEqual(C.reported, 2)
1642 self.assertEqual(D.data['status'], 'MERGED')
1643 self.assertEqual(D.reported, 2)
1644 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001645
James E. Blairba437362015-02-07 11:41:52 -08001646 def test_new_patchset_check(self):
1647 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001648
Paul Belanger174a8272017-03-14 13:20:10 -04001649 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001650
1651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001652 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001653 tenant = self.sched.abide.tenants.get('tenant-one')
1654 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001655
1656 # Add two git-dependent changes
1657 B.setDependsOn(A, 1)
1658 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1659 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001660 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1661 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001662
1663 # A live item, and a non-live/live pair
1664 items = check_pipeline.getAllItems()
1665 self.assertEqual(len(items), 3)
1666
1667 self.assertEqual(items[0].change.number, '1')
1668 self.assertEqual(items[0].change.patchset, '1')
1669 self.assertFalse(items[0].live)
1670
1671 self.assertEqual(items[1].change.number, '2')
1672 self.assertEqual(items[1].change.patchset, '1')
1673 self.assertTrue(items[1].live)
1674
1675 self.assertEqual(items[2].change.number, '1')
1676 self.assertEqual(items[2].change.patchset, '1')
1677 self.assertTrue(items[2].live)
1678
1679 # Add a new patchset to A
1680 A.addPatchset()
1681 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1682 self.waitUntilSettled()
1683
1684 # The live copy of A,1 should be gone, but the non-live and B
1685 # should continue, and we should have a new A,2
1686 items = check_pipeline.getAllItems()
1687 self.assertEqual(len(items), 3)
1688
1689 self.assertEqual(items[0].change.number, '1')
1690 self.assertEqual(items[0].change.patchset, '1')
1691 self.assertFalse(items[0].live)
1692
1693 self.assertEqual(items[1].change.number, '2')
1694 self.assertEqual(items[1].change.patchset, '1')
1695 self.assertTrue(items[1].live)
1696
1697 self.assertEqual(items[2].change.number, '1')
1698 self.assertEqual(items[2].change.patchset, '2')
1699 self.assertTrue(items[2].live)
1700
1701 # Add a new patchset to B
1702 B.addPatchset()
1703 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1704 self.waitUntilSettled()
1705
1706 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1707 # but we should have a new B,2 (still based on A,1)
1708 items = check_pipeline.getAllItems()
1709 self.assertEqual(len(items), 3)
1710
1711 self.assertEqual(items[0].change.number, '1')
1712 self.assertEqual(items[0].change.patchset, '2')
1713 self.assertTrue(items[0].live)
1714
1715 self.assertEqual(items[1].change.number, '1')
1716 self.assertEqual(items[1].change.patchset, '1')
1717 self.assertFalse(items[1].live)
1718
1719 self.assertEqual(items[2].change.number, '2')
1720 self.assertEqual(items[2].change.patchset, '2')
1721 self.assertTrue(items[2].live)
1722
1723 self.builds[0].release()
1724 self.waitUntilSettled()
1725 self.builds[0].release()
1726 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001727 self.executor_server.hold_jobs_in_build = False
1728 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001729 self.waitUntilSettled()
1730
1731 self.assertEqual(A.reported, 1)
1732 self.assertEqual(B.reported, 1)
1733 self.assertEqual(self.history[0].result, 'ABORTED')
1734 self.assertEqual(self.history[0].changes, '1,1')
1735 self.assertEqual(self.history[1].result, 'ABORTED')
1736 self.assertEqual(self.history[1].changes, '1,1 2,1')
1737 self.assertEqual(self.history[2].result, 'SUCCESS')
1738 self.assertEqual(self.history[2].changes, '1,2')
1739 self.assertEqual(self.history[3].result, 'SUCCESS')
1740 self.assertEqual(self.history[3].changes, '1,1 2,2')
1741
1742 def test_abandoned_gate(self):
1743 "Test that an abandoned change is dequeued from gate"
1744
Paul Belanger174a8272017-03-14 13:20:10 -04001745 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001746
1747 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001748 A.addApproval('Code-Review', 2)
1749 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001750 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001751 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1752 self.assertEqual(self.builds[0].name, 'project-merge')
1753
1754 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1755 self.waitUntilSettled()
1756
Paul Belanger174a8272017-03-14 13:20:10 -04001757 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001758 self.waitUntilSettled()
1759
Arie5b4048c2016-10-05 16:12:06 +03001760 self.assertBuilds([])
1761 self.assertHistory([
1762 dict(name='project-merge', result='ABORTED', changes='1,1')],
1763 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001764 self.assertEqual(A.reported, 1,
1765 "Abandoned gate change should report only start")
1766
1767 def test_abandoned_check(self):
1768 "Test that an abandoned change is dequeued from check"
1769
Paul Belanger174a8272017-03-14 13:20:10 -04001770 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001771
1772 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1773 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001774 tenant = self.sched.abide.tenants.get('tenant-one')
1775 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001776
1777 # Add two git-dependent changes
1778 B.setDependsOn(A, 1)
1779 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1780 self.waitUntilSettled()
1781 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1782 self.waitUntilSettled()
1783 # A live item, and a non-live/live pair
1784 items = check_pipeline.getAllItems()
1785 self.assertEqual(len(items), 3)
1786
1787 self.assertEqual(items[0].change.number, '1')
1788 self.assertFalse(items[0].live)
1789
1790 self.assertEqual(items[1].change.number, '2')
1791 self.assertTrue(items[1].live)
1792
1793 self.assertEqual(items[2].change.number, '1')
1794 self.assertTrue(items[2].live)
1795
1796 # Abandon A
1797 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1798 self.waitUntilSettled()
1799
1800 # The live copy of A should be gone, but the non-live and B
1801 # should continue
1802 items = check_pipeline.getAllItems()
1803 self.assertEqual(len(items), 2)
1804
1805 self.assertEqual(items[0].change.number, '1')
1806 self.assertFalse(items[0].live)
1807
1808 self.assertEqual(items[1].change.number, '2')
1809 self.assertTrue(items[1].live)
1810
Paul Belanger174a8272017-03-14 13:20:10 -04001811 self.executor_server.hold_jobs_in_build = False
1812 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001813 self.waitUntilSettled()
1814
1815 self.assertEqual(len(self.history), 4)
1816 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001817 'Build should have been aborted')
1818 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001819 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001820
Steve Varnau7b78b312015-04-03 14:49:46 -07001821 def test_abandoned_not_timer(self):
1822 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001823 # This test can not use simple_layout because it must start
1824 # with a configuration which does not include a
1825 # timer-triggered job so that we have an opportunity to set
1826 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001827 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001828 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001829 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001830 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001831 # The pipeline triggers every second, so we should have seen
1832 # several by now.
1833 time.sleep(5)
1834 self.waitUntilSettled()
1835 # Stop queuing timer triggered jobs so that the assertions
1836 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001837 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001838 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001839 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001840 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001841
James E. Blair21037782017-07-19 11:56:55 -07001842 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001843
1844 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1845 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1846 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001847 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001848
1849 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1850 self.waitUntilSettled()
1851
James E. Blair21037782017-07-19 11:56:55 -07001852 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001853
Paul Belanger174a8272017-03-14 13:20:10 -04001854 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001855 self.waitUntilSettled()
1856
James E. Blair2fa50962013-01-30 21:50:41 -08001857 def test_new_patchset_dequeues_old_on_head(self):
1858 "Test that a new patchset causes the old to be dequeued (at head)"
1859 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001860 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001861 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1862 M.setMerged()
1863 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1864 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1865 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1866 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001867 A.addApproval('Code-Review', 2)
1868 B.addApproval('Code-Review', 2)
1869 C.addApproval('Code-Review', 2)
1870 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001871
1872 C.setDependsOn(B, 1)
1873 B.setDependsOn(A, 1)
1874 A.setDependsOn(M, 1)
1875
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001876 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1877 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1878 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1879 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001880 self.waitUntilSettled()
1881
1882 A.addPatchset()
1883 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1884 self.waitUntilSettled()
1885
Paul Belanger174a8272017-03-14 13:20:10 -04001886 self.executor_server.hold_jobs_in_build = False
1887 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001888 self.waitUntilSettled()
1889
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001890 self.assertEqual(A.data['status'], 'NEW')
1891 self.assertEqual(A.reported, 2)
1892 self.assertEqual(B.data['status'], 'NEW')
1893 self.assertEqual(B.reported, 2)
1894 self.assertEqual(C.data['status'], 'NEW')
1895 self.assertEqual(C.reported, 2)
1896 self.assertEqual(D.data['status'], 'MERGED')
1897 self.assertEqual(D.reported, 2)
1898 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001899
1900 def test_new_patchset_dequeues_old_without_dependents(self):
1901 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001902 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001903 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1904 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1905 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001906 A.addApproval('Code-Review', 2)
1907 B.addApproval('Code-Review', 2)
1908 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001909
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001910 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1911 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1912 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001913 self.waitUntilSettled()
1914
1915 B.addPatchset()
1916 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1917 self.waitUntilSettled()
1918
Paul Belanger174a8272017-03-14 13:20:10 -04001919 self.executor_server.hold_jobs_in_build = False
1920 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001921 self.waitUntilSettled()
1922
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001923 self.assertEqual(A.data['status'], 'MERGED')
1924 self.assertEqual(A.reported, 2)
1925 self.assertEqual(B.data['status'], 'NEW')
1926 self.assertEqual(B.reported, 2)
1927 self.assertEqual(C.data['status'], 'MERGED')
1928 self.assertEqual(C.reported, 2)
1929 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001930
1931 def test_new_patchset_dequeues_old_independent_queue(self):
1932 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001933 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001934 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1935 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1936 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1937 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1938 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1939 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1940 self.waitUntilSettled()
1941
1942 B.addPatchset()
1943 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1944 self.waitUntilSettled()
1945
Paul Belanger174a8272017-03-14 13:20:10 -04001946 self.executor_server.hold_jobs_in_build = False
1947 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001948 self.waitUntilSettled()
1949
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001950 self.assertEqual(A.data['status'], 'NEW')
1951 self.assertEqual(A.reported, 1)
1952 self.assertEqual(B.data['status'], 'NEW')
1953 self.assertEqual(B.reported, 1)
1954 self.assertEqual(C.data['status'], 'NEW')
1955 self.assertEqual(C.reported, 1)
1956 self.assertEqual(len(self.history), 10)
1957 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001958
James E. Blaircc020532017-04-19 13:04:44 -07001959 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001960 def test_noop_job(self):
1961 "Test that the internal noop job works"
1962 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001963 A.addApproval('Code-Review', 2)
1964 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001965 self.waitUntilSettled()
1966
1967 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1968 self.assertTrue(self.sched._areAllBuildsComplete())
1969 self.assertEqual(len(self.history), 0)
1970 self.assertEqual(A.data['status'], 'MERGED')
1971 self.assertEqual(A.reported, 2)
1972
James E. Blair06cc3922017-04-19 10:08:10 -07001973 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001974 def test_no_job_project(self):
1975 "Test that reports with no jobs don't get sent"
1976 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1977 'master', 'A')
1978 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1979 self.waitUntilSettled()
1980
1981 # Change wasn't reported to
1982 self.assertEqual(A.reported, False)
1983
1984 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001985 tenant = self.sched.abide.tenants.get('tenant-one')
1986 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001987 items = check_pipeline.getAllItems()
1988 self.assertEqual(len(items), 0)
1989
1990 self.assertEqual(len(self.history), 0)
1991
James E. Blair7d0dedc2013-02-21 17:26:09 -08001992 def test_zuul_refs(self):
1993 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001994 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001995 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1996 M1.setMerged()
1997 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1998 M2.setMerged()
1999
2000 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2001 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2002 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2003 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002004 A.addApproval('Code-Review', 2)
2005 B.addApproval('Code-Review', 2)
2006 C.addApproval('Code-Review', 2)
2007 D.addApproval('Code-Review', 2)
2008 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2009 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2010 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2011 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002012
2013 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002014 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002015 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002016 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002017 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002018 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002019 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002020 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002021 self.waitUntilSettled()
2022
Clint Byrum3343e3e2016-11-15 16:05:03 -08002023 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002024 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002025 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002026 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002027 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002028 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002029 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002030 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002031 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002032 d_build = x
2033 if a_build and b_build and c_build and d_build:
2034 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002035
Clint Byrum3343e3e2016-11-15 16:05:03 -08002036 # should have a, not b, and should not be in project2
2037 self.assertTrue(a_build.hasChanges(A))
2038 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002039
Clint Byrum3343e3e2016-11-15 16:05:03 -08002040 # should have a and b, and should not be in project2
2041 self.assertTrue(b_build.hasChanges(A, B))
2042 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002043
Clint Byrum3343e3e2016-11-15 16:05:03 -08002044 # should have a and b in 1, c in 2
2045 self.assertTrue(c_build.hasChanges(A, B, C))
2046 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002047
Clint Byrum3343e3e2016-11-15 16:05:03 -08002048 # should have a and b in 1, c and d in 2
2049 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002050
Paul Belanger174a8272017-03-14 13:20:10 -04002051 self.executor_server.hold_jobs_in_build = False
2052 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002053 self.waitUntilSettled()
2054
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002055 self.assertEqual(A.data['status'], 'MERGED')
2056 self.assertEqual(A.reported, 2)
2057 self.assertEqual(B.data['status'], 'MERGED')
2058 self.assertEqual(B.reported, 2)
2059 self.assertEqual(C.data['status'], 'MERGED')
2060 self.assertEqual(C.reported, 2)
2061 self.assertEqual(D.data['status'], 'MERGED')
2062 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002063
James E. Blair4a28a882013-08-23 15:17:33 -07002064 def test_rerun_on_error(self):
2065 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002066 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002067 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002068 A.addApproval('Code-Review', 2)
2069 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002070 self.waitUntilSettled()
2071
James E. Blair412fba82017-01-26 15:00:50 -08002072 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002073 self.executor_server.hold_jobs_in_build = False
2074 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002075 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002076 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002077 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2078
James E. Blair412e5582013-04-22 15:50:12 -07002079 def test_statsd(self):
2080 "Test each of the statsd methods used in the scheduler"
2081 import extras
2082 statsd = extras.try_import('statsd.statsd')
2083 statsd.incr('test-incr')
2084 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002085 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002086 self.assertReportedStat('test-incr', '1|c')
2087 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002088 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002089
James E. Blairdad52252014-02-07 16:59:17 -08002090 def test_stuck_job_cleanup(self):
2091 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002092
2093 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002094 self.gearman_server.hold_jobs_in_queue = True
2095 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002096 A.addApproval('Code-Review', 2)
2097 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002098 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002099 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002100 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2101
James E. Blair31cb3e82017-04-20 13:08:56 -07002102 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002103 self.sched.reconfigure(self.config)
2104 self.waitUntilSettled()
2105
James E. Blair18c64442014-03-18 10:14:45 -07002106 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002107 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002108 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002109 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2110 self.assertTrue(self.sched._areAllBuildsComplete())
2111
2112 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002113 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002114 self.assertEqual(self.history[0].result, 'SUCCESS')
2115
James E. Blair879dafb2015-07-17 14:04:49 -07002116 def test_file_head(self):
2117 # This is a regression test for an observed bug. A change
2118 # with a file named "HEAD" in the root directory of the repo
2119 # was processed by a merger. It then was unable to reset the
2120 # repo because of:
2121 # GitCommandError: 'git reset --hard HEAD' returned
2122 # with exit code 128
2123 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2124 # and filename
2125 # Use '--' to separate filenames from revisions'
2126
2127 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002128 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002129 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2130
2131 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2132 self.waitUntilSettled()
2133
2134 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2135 self.waitUntilSettled()
2136
2137 self.assertIn('Build succeeded', A.messages[0])
2138 self.assertIn('Build succeeded', B.messages[0])
2139
James E. Blair70c71582013-03-06 08:50:50 -08002140 def test_file_jobs(self):
2141 "Test that file jobs run only when appropriate"
2142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002143 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002144 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002145 A.addApproval('Code-Review', 2)
2146 B.addApproval('Code-Review', 2)
2147 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2148 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002149 self.waitUntilSettled()
2150
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002151 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002152 if x.name == 'project-testfile']
2153
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002154 self.assertEqual(len(testfile_jobs), 1)
2155 self.assertEqual(testfile_jobs[0].changes, '1,2')
2156 self.assertEqual(A.data['status'], 'MERGED')
2157 self.assertEqual(A.reported, 2)
2158 self.assertEqual(B.data['status'], 'MERGED')
2159 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002160
Clint Byrum3ee39f32016-11-17 23:45:07 -08002161 def _test_irrelevant_files_jobs(self, should_skip):
2162 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002163 if should_skip:
2164 files = {'ignoreme': 'ignored\n'}
2165 else:
2166 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002167
2168 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002169 'master',
2170 'test irrelevant-files',
2171 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002172 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2173 self.waitUntilSettled()
2174
2175 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002176 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002177
2178 if should_skip:
2179 self.assertEqual([], tested_change_ids)
2180 else:
2181 self.assertIn(change.data['number'], tested_change_ids)
2182
James E. Blaircc324192017-04-19 14:36:08 -07002183 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002184 def test_irrelevant_files_match_skips_job(self):
2185 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002186
James E. Blaircc324192017-04-19 14:36:08 -07002187 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002188 def test_irrelevant_files_no_match_runs_job(self):
2189 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002190
James E. Blaircc324192017-04-19 14:36:08 -07002191 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002192 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002193 files = {'ignoreme': 'ignored\n'}
2194
2195 change = self.fake_gerrit.addFakeChange('org/project',
2196 'master',
2197 'test irrelevant-files',
2198 files=files)
2199 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2200 self.waitUntilSettled()
2201
2202 run_jobs = set([build.name for build in self.history])
2203
2204 self.assertEqual(set(['project-test-nomatch-starts-empty',
2205 'project-test-nomatch-starts-full']), run_jobs)
2206
James E. Blairc8a1e052014-02-25 09:29:26 -08002207 def test_queue_names(self):
2208 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002209 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002210 (trusted, project1) = tenant.getProject('org/project1')
2211 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002212 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2213 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2214 self.assertEqual(q1.name, 'integrated')
2215 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002216
James E. Blair64ed6f22013-07-10 14:07:23 -07002217 def test_queue_precedence(self):
2218 "Test that queue precedence works"
2219
2220 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002221 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002222 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2223 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002224 A.addApproval('Code-Review', 2)
2225 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002226
2227 self.waitUntilSettled()
2228 self.gearman_server.hold_jobs_in_queue = False
2229 self.gearman_server.release()
2230 self.waitUntilSettled()
2231
James E. Blair8de58bd2013-07-18 16:23:33 -07002232 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002233 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002234 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002235 self.waitUntilSettled()
2236
James E. Blair64ed6f22013-07-10 14:07:23 -07002237 self.log.debug(self.history)
2238 self.assertEqual(self.history[0].pipeline, 'gate')
2239 self.assertEqual(self.history[1].pipeline, 'check')
2240 self.assertEqual(self.history[2].pipeline, 'gate')
2241 self.assertEqual(self.history[3].pipeline, 'gate')
2242 self.assertEqual(self.history[4].pipeline, 'check')
2243 self.assertEqual(self.history[5].pipeline, 'check')
2244
Clark Boylana5edbe42014-06-03 16:39:10 -07002245 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002246 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002247 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002249 A.addApproval('Code-Review', 2)
2250 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002251 self.waitUntilSettled()
2252
Paul Belanger174a8272017-03-14 13:20:10 -04002253 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002254 self.waitUntilSettled()
2255
James E. Blair1843a552013-07-03 14:19:52 -07002256 port = self.webapp.server.socket.getsockname()[1]
2257
Adam Gandelman77a12c72017-02-03 17:43:43 -08002258 req = urllib.request.Request(
2259 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002260 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002261 headers = f.info()
2262 self.assertIn('Content-Length', headers)
2263 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002264 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2265 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002266 self.assertIn('Access-Control-Allow-Origin', headers)
2267 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002268 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002269 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002270 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002271
Paul Belanger174a8272017-03-14 13:20:10 -04002272 self.executor_server.hold_jobs_in_build = False
2273 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002274 self.waitUntilSettled()
2275
2276 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002277 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002278 for p in data['pipelines']:
2279 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002280 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002281 self.assertEqual(q['window'], 20)
2282 else:
2283 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002284 for head in q['heads']:
2285 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002286 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002287 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002288 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002289 status_jobs.append(job)
2290 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002291 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002292 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002293 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2294 uuid=status_jobs[0]['uuid']),
2295 status_jobs[0]['url'])
2296 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002297 'finger://{hostname}/{uuid}'.format(
2298 hostname=self.executor_server.hostname,
2299 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002300 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002301 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002302 self.assertEqual(
2303 'finger://{hostname}/{uuid}'.format(
2304 hostname=self.executor_server.hostname,
2305 uuid=status_jobs[0]['uuid']),
2306 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002307 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002308 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002309 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2310 uuid=status_jobs[1]['uuid']),
2311 status_jobs[1]['url'])
2312 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002313 'finger://{hostname}/{uuid}'.format(
2314 hostname=self.executor_server.hostname,
2315 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002316 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002317 self.assertEqual(
2318 'finger://{hostname}/{uuid}'.format(
2319 hostname=self.executor_server.hostname,
2320 uuid=status_jobs[1]['uuid']),
2321 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002322
2323 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002324 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002325 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2326 uuid=status_jobs[2]['uuid']),
2327 status_jobs[2]['url'])
2328 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002329 'finger://{hostname}/{uuid}'.format(
2330 hostname=self.executor_server.hostname,
2331 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002332 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002333 self.assertEqual(
2334 'finger://{hostname}/{uuid}'.format(
2335 hostname=self.executor_server.hostname,
2336 uuid=status_jobs[2]['uuid']),
2337 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002338
Tobias Henkel65639f82017-07-10 10:25:42 +02002339 # check job dependencies
2340 self.assertIsNotNone(status_jobs[0]['dependencies'])
2341 self.assertIsNotNone(status_jobs[1]['dependencies'])
2342 self.assertIsNotNone(status_jobs[2]['dependencies'])
2343 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2344 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2345 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2346 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2347 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2348
James E. Blaircdccd972013-07-01 12:10:22 -07002349 def test_live_reconfiguration(self):
2350 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002351 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002352 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002353 A.addApproval('Code-Review', 2)
2354 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002355 self.waitUntilSettled()
2356
2357 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002358 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002359
Paul Belanger174a8272017-03-14 13:20:10 -04002360 self.executor_server.hold_jobs_in_build = False
2361 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002362 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002363 self.assertEqual(self.getJobFromHistory('project-merge').result,
2364 'SUCCESS')
2365 self.assertEqual(self.getJobFromHistory('project-test1').result,
2366 'SUCCESS')
2367 self.assertEqual(self.getJobFromHistory('project-test2').result,
2368 'SUCCESS')
2369 self.assertEqual(A.data['status'], 'MERGED')
2370 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002371
James E. Blair6bc782d2015-07-17 16:20:21 -07002372 def test_live_reconfiguration_merge_conflict(self):
2373 # A real-world bug: a change in a gate queue has a merge
2374 # conflict and a job is added to its project while it's
2375 # sitting in the queue. The job gets added to the change and
2376 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002377 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002378
2379 # This change is fine. It's here to stop the queue long
2380 # enough for the next change to be subject to the
2381 # reconfiguration, as well as to provide a conflict for the
2382 # next change. This change will succeed and merge.
2383 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002384 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002385 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002386
2387 # This change will be in merge conflict. During the
2388 # reconfiguration, we will add a job. We want to make sure
2389 # that doesn't cause it to get stuck.
2390 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002391 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002392 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002393
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002394 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2395 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002396
2397 self.waitUntilSettled()
2398
2399 # No jobs have run yet
2400 self.assertEqual(A.data['status'], 'NEW')
2401 self.assertEqual(A.reported, 1)
2402 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002403 self.assertEqual(len(self.history), 0)
2404
2405 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002406 self.commitConfigUpdate('common-config',
2407 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002408 self.sched.reconfigure(self.config)
2409 self.waitUntilSettled()
2410
Paul Belanger174a8272017-03-14 13:20:10 -04002411 self.executor_server.hold_jobs_in_build = False
2412 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002413 self.waitUntilSettled()
2414
2415 self.assertEqual(A.data['status'], 'MERGED')
2416 self.assertEqual(A.reported, 2)
2417 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002418 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002419 self.assertEqual(self.getJobFromHistory('project-merge').result,
2420 'SUCCESS')
2421 self.assertEqual(self.getJobFromHistory('project-test1').result,
2422 'SUCCESS')
2423 self.assertEqual(self.getJobFromHistory('project-test2').result,
2424 'SUCCESS')
2425 self.assertEqual(self.getJobFromHistory('project-test3').result,
2426 'SUCCESS')
2427 self.assertEqual(len(self.history), 4)
2428
James E. Blair400e8fd2015-07-30 17:44:45 -07002429 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002430 # An extrapolation of test_live_reconfiguration_merge_conflict
2431 # that tests a job added to a job tree with a failed root does
2432 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002433 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002434
2435 # This change is fine. It's here to stop the queue long
2436 # enough for the next change to be subject to the
2437 # reconfiguration. This change will succeed and merge.
2438 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002439 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002440 A.addApproval('Code-Review', 2)
2441 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002442 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002443 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002444 self.waitUntilSettled()
2445
2446 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002447 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002448 B.addApproval('Code-Review', 2)
2449 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002450 self.waitUntilSettled()
2451
Paul Belanger174a8272017-03-14 13:20:10 -04002452 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002453 self.waitUntilSettled()
2454
2455 # Both -merge jobs have run, but no others.
2456 self.assertEqual(A.data['status'], 'NEW')
2457 self.assertEqual(A.reported, 1)
2458 self.assertEqual(B.data['status'], 'NEW')
2459 self.assertEqual(B.reported, 1)
2460 self.assertEqual(self.history[0].result, 'SUCCESS')
2461 self.assertEqual(self.history[0].name, 'project-merge')
2462 self.assertEqual(self.history[1].result, 'FAILURE')
2463 self.assertEqual(self.history[1].name, 'project-merge')
2464 self.assertEqual(len(self.history), 2)
2465
2466 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002467 self.commitConfigUpdate('common-config',
2468 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002469 self.sched.reconfigure(self.config)
2470 self.waitUntilSettled()
2471
Paul Belanger174a8272017-03-14 13:20:10 -04002472 self.executor_server.hold_jobs_in_build = False
2473 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002474 self.waitUntilSettled()
2475
2476 self.assertEqual(A.data['status'], 'MERGED')
2477 self.assertEqual(A.reported, 2)
2478 self.assertEqual(B.data['status'], 'NEW')
2479 self.assertEqual(B.reported, 2)
2480 self.assertEqual(self.history[0].result, 'SUCCESS')
2481 self.assertEqual(self.history[0].name, 'project-merge')
2482 self.assertEqual(self.history[1].result, 'FAILURE')
2483 self.assertEqual(self.history[1].name, 'project-merge')
2484 self.assertEqual(self.history[2].result, 'SUCCESS')
2485 self.assertEqual(self.history[3].result, 'SUCCESS')
2486 self.assertEqual(self.history[4].result, 'SUCCESS')
2487 self.assertEqual(len(self.history), 5)
2488
James E. Blair400e8fd2015-07-30 17:44:45 -07002489 def test_live_reconfiguration_failed_job(self):
2490 # Test that a change with a removed failing job does not
2491 # disrupt reconfiguration. If a change has a failed job and
2492 # that job is removed during a reconfiguration, we observed a
2493 # bug where the code to re-set build statuses would run on
2494 # that build and raise an exception because the job no longer
2495 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002496 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002497
2498 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2499
2500 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002501 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002502
2503 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2504 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002505 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002506 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002507 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002508 self.waitUntilSettled()
2509
2510 self.assertEqual(A.data['status'], 'NEW')
2511 self.assertEqual(A.reported, 0)
2512
2513 self.assertEqual(self.getJobFromHistory('project-merge').result,
2514 'SUCCESS')
2515 self.assertEqual(self.getJobFromHistory('project-test1').result,
2516 'FAILURE')
2517 self.assertEqual(len(self.history), 2)
2518
2519 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002520 self.commitConfigUpdate('common-config',
2521 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002522 self.sched.reconfigure(self.config)
2523 self.waitUntilSettled()
2524
Paul Belanger174a8272017-03-14 13:20:10 -04002525 self.executor_server.hold_jobs_in_build = False
2526 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002527 self.waitUntilSettled()
2528
2529 self.assertEqual(self.getJobFromHistory('project-test2').result,
2530 'SUCCESS')
2531 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2532 'SUCCESS')
2533 self.assertEqual(len(self.history), 4)
2534
2535 self.assertEqual(A.data['status'], 'NEW')
2536 self.assertEqual(A.reported, 1)
2537 self.assertIn('Build succeeded', A.messages[0])
2538 # Ensure the removed job was not included in the report.
2539 self.assertNotIn('project-test1', A.messages[0])
2540
James E. Blairfe707d12015-08-05 15:18:15 -07002541 def test_live_reconfiguration_shared_queue(self):
2542 # Test that a change with a failing job which was removed from
2543 # this project but otherwise still exists in the system does
2544 # not disrupt reconfiguration.
2545
Paul Belanger174a8272017-03-14 13:20:10 -04002546 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002547
2548 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2549
Paul Belanger174a8272017-03-14 13:20:10 -04002550 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002551
2552 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2553 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002554 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002555 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002556 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002557 self.waitUntilSettled()
2558
2559 self.assertEqual(A.data['status'], 'NEW')
2560 self.assertEqual(A.reported, 0)
2561
Clint Byrum69663d42017-05-01 14:51:04 -07002562 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002563 'SUCCESS')
2564 self.assertEqual(self.getJobFromHistory(
2565 'project1-project2-integration').result, 'FAILURE')
2566 self.assertEqual(len(self.history), 2)
2567
2568 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002569 self.commitConfigUpdate(
2570 'common-config',
2571 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002572 self.sched.reconfigure(self.config)
2573 self.waitUntilSettled()
2574
Paul Belanger174a8272017-03-14 13:20:10 -04002575 self.executor_server.hold_jobs_in_build = False
2576 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002577 self.waitUntilSettled()
2578
Clint Byrum69663d42017-05-01 14:51:04 -07002579 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002580 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002581 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002582 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002583 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002584 'SUCCESS')
2585 self.assertEqual(self.getJobFromHistory(
2586 'project1-project2-integration').result, 'FAILURE')
2587 self.assertEqual(len(self.history), 4)
2588
2589 self.assertEqual(A.data['status'], 'NEW')
2590 self.assertEqual(A.reported, 1)
2591 self.assertIn('Build succeeded', A.messages[0])
2592 # Ensure the removed job was not included in the report.
2593 self.assertNotIn('project1-project2-integration', A.messages[0])
2594
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002595 def test_double_live_reconfiguration_shared_queue(self):
2596 # This was a real-world regression. A change is added to
2597 # gate; a reconfigure happens, a second change which depends
2598 # on the first is added, and a second reconfiguration happens.
2599 # Ensure that both changes merge.
2600
2601 # A failure may indicate incorrect caching or cleaning up of
2602 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002603 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002604
2605 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2606 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2607 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002608 A.addApproval('Code-Review', 2)
2609 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002610
2611 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002612 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002613 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002614 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002615 self.waitUntilSettled()
2616
2617 # Reconfigure (with only one change in the pipeline).
2618 self.sched.reconfigure(self.config)
2619 self.waitUntilSettled()
2620
2621 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002622 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002623 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002624 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002625 self.waitUntilSettled()
2626
2627 # Reconfigure (with both in the pipeline).
2628 self.sched.reconfigure(self.config)
2629 self.waitUntilSettled()
2630
Paul Belanger174a8272017-03-14 13:20:10 -04002631 self.executor_server.hold_jobs_in_build = False
2632 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002633 self.waitUntilSettled()
2634
2635 self.assertEqual(len(self.history), 8)
2636
2637 self.assertEqual(A.data['status'], 'MERGED')
2638 self.assertEqual(A.reported, 2)
2639 self.assertEqual(B.data['status'], 'MERGED')
2640 self.assertEqual(B.reported, 2)
2641
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002642 def test_live_reconfiguration_del_project(self):
2643 # Test project deletion from layout
2644 # while changes are enqueued
2645
Paul Belanger174a8272017-03-14 13:20:10 -04002646 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002647 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2648 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2649 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2650
2651 # A Depends-On: B
2652 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2653 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002654 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002655
2656 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2657 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2658 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002659 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002660 self.waitUntilSettled()
2661 self.assertEqual(len(self.builds), 5)
2662
2663 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002664 self.commitConfigUpdate(
2665 'common-config',
2666 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002667 self.sched.reconfigure(self.config)
2668 self.waitUntilSettled()
2669
2670 # Builds for C aborted, builds for A succeed,
2671 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002672 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002673 self.assertEqual(job_c.changes, '3,1')
2674 self.assertEqual(job_c.result, 'ABORTED')
2675
Paul Belanger174a8272017-03-14 13:20:10 -04002676 self.executor_server.hold_jobs_in_build = False
2677 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002678 self.waitUntilSettled()
2679
Clint Byrum0149ada2017-04-04 12:22:01 -07002680 self.assertEqual(
2681 self.getJobFromHistory('project-test1', 'org/project').changes,
2682 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002683
2684 self.assertEqual(A.data['status'], 'NEW')
2685 self.assertEqual(B.data['status'], 'NEW')
2686 self.assertEqual(C.data['status'], 'NEW')
2687 self.assertEqual(A.reported, 1)
2688 self.assertEqual(B.reported, 0)
2689 self.assertEqual(C.reported, 0)
2690
Clint Byrum0149ada2017-04-04 12:22:01 -07002691 tenant = self.sched.abide.tenants.get('tenant-one')
2692 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002693 self.assertIn('Build succeeded', A.messages[0])
2694
James E. Blairec056492016-07-22 09:45:56 -07002695 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002696 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002697 self.updateConfigLayout(
2698 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002699 self.sched.reconfigure(self.config)
2700
2701 self.init_repo("org/new-project")
2702 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2703
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002704 A.addApproval('Code-Review', 2)
2705 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002706 self.waitUntilSettled()
2707 self.assertEqual(self.getJobFromHistory('project-merge').result,
2708 'SUCCESS')
2709 self.assertEqual(self.getJobFromHistory('project-test1').result,
2710 'SUCCESS')
2711 self.assertEqual(self.getJobFromHistory('project-test2').result,
2712 'SUCCESS')
2713 self.assertEqual(A.data['status'], 'MERGED')
2714 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002715
James E. Blair9ea70072017-04-19 16:05:30 -07002716 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002717 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002718 self.init_repo("org/delete-project")
2719 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2720
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002721 A.addApproval('Code-Review', 2)
2722 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002723 self.waitUntilSettled()
2724 self.assertEqual(self.getJobFromHistory('project-merge').result,
2725 'SUCCESS')
2726 self.assertEqual(self.getJobFromHistory('project-test1').result,
2727 'SUCCESS')
2728 self.assertEqual(self.getJobFromHistory('project-test2').result,
2729 'SUCCESS')
2730 self.assertEqual(A.data['status'], 'MERGED')
2731 self.assertEqual(A.reported, 2)
2732
2733 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002734 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002735 if os.path.exists(os.path.join(self.merger_src_root, p)):
2736 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002737 if os.path.exists(os.path.join(self.executor_src_root, p)):
2738 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002739
2740 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2741
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002742 B.addApproval('Code-Review', 2)
2743 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002744 self.waitUntilSettled()
2745 self.assertEqual(self.getJobFromHistory('project-merge').result,
2746 'SUCCESS')
2747 self.assertEqual(self.getJobFromHistory('project-test1').result,
2748 'SUCCESS')
2749 self.assertEqual(self.getJobFromHistory('project-test2').result,
2750 'SUCCESS')
2751 self.assertEqual(B.data['status'], 'MERGED')
2752 self.assertEqual(B.reported, 2)
2753
James E. Blaire68d43f2017-04-19 14:03:30 -07002754 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002755 def test_tags(self):
2756 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002757 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2758 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2759 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2760 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2761 self.waitUntilSettled()
2762
James E. Blaire68d43f2017-04-19 14:03:30 -07002763 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002764
2765 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002766 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002767 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002768 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002769
2770 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002771 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002772 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002773
James E. Blair63bb0ef2013-07-29 17:14:51 -07002774 def test_timer(self):
2775 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002776 # This test can not use simple_layout because it must start
2777 # with a configuration which does not include a
2778 # timer-triggered job so that we have an opportunity to set
2779 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002780 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002781 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002782 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002783 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002784
Clark Boylan3ee090a2014-04-03 20:55:09 -07002785 # The pipeline triggers every second, so we should have seen
2786 # several by now.
2787 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002788 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002789
2790 self.assertEqual(len(self.builds), 2)
2791
James E. Blair63bb0ef2013-07-29 17:14:51 -07002792 port = self.webapp.server.socket.getsockname()[1]
2793
Clint Byrum5e729e92017-02-08 06:34:57 -08002794 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002795 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002796 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002797 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002798
Paul Belanger174a8272017-03-14 13:20:10 -04002799 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002800 # Stop queuing timer triggered jobs so that the assertions
2801 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002802 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002803 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002804 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002805 self.waitUntilSettled()
2806
James E. Blair21037782017-07-19 11:56:55 -07002807 self.assertHistory([
2808 dict(name='project-bitrot', result='SUCCESS',
2809 ref='refs/heads/master'),
2810 dict(name='project-bitrot', result='SUCCESS',
2811 ref='refs/heads/stable'),
2812 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002813
2814 data = json.loads(data)
2815 status_jobs = set()
2816 for p in data['pipelines']:
2817 for q in p['change_queues']:
2818 for head in q['heads']:
2819 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002820 for job in change['jobs']:
2821 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002822 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002823
James E. Blair4f6033c2014-03-27 15:49:09 -07002824 def test_idle(self):
2825 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002826 # This test can not use simple_layout because it must start
2827 # with a configuration which does not include a
2828 # timer-triggered job so that we have an opportunity to set
2829 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002830 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002831
Clark Boylan3ee090a2014-04-03 20:55:09 -07002832 for x in range(1, 3):
2833 # Test that timer triggers periodic jobs even across
2834 # layout config reloads.
2835 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002836 self.commitConfigUpdate('common-config',
2837 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002838 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002839 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002840
Clark Boylan3ee090a2014-04-03 20:55:09 -07002841 # The pipeline triggers every second, so we should have seen
2842 # several by now.
2843 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002844
2845 # Stop queuing timer triggered jobs so that the assertions
2846 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002847 self.commitConfigUpdate('common-config',
2848 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002849 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002850 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002851 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002852 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002853 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002854 self.waitUntilSettled()
2855 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002856 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002857
James E. Blaircc324192017-04-19 14:36:08 -07002858 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002859 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002860 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2861 self.waitUntilSettled()
2862
2863 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2864 self.waitUntilSettled()
2865
James E. Blairff80a2f2013-12-27 13:24:06 -08002866 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002867
2868 # A.messages only holds what FakeGerrit places in it. Thus we
2869 # work on the knowledge of what the first message should be as
2870 # it is only configured to go to SMTP.
2871
2872 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002873 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002874 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002875 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002876 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002877 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002878
2879 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002880 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002881 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002882 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002883 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002884 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002885
James E. Blaire5910202013-12-27 09:50:31 -08002886 def test_timer_smtp(self):
2887 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002888 # This test can not use simple_layout because it must start
2889 # with a configuration which does not include a
2890 # timer-triggered job so that we have an opportunity to set
2891 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002892 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002893 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002894 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002895
Clark Boylan3ee090a2014-04-03 20:55:09 -07002896 # The pipeline triggers every second, so we should have seen
2897 # several by now.
2898 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002899 self.waitUntilSettled()
2900
Clark Boylan3ee090a2014-04-03 20:55:09 -07002901 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002902 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002903 self.waitUntilSettled()
2904 self.assertEqual(len(self.history), 2)
2905
James E. Blaire5910202013-12-27 09:50:31 -08002906 self.assertEqual(self.getJobFromHistory(
2907 'project-bitrot-stable-old').result, 'SUCCESS')
2908 self.assertEqual(self.getJobFromHistory(
2909 'project-bitrot-stable-older').result, 'SUCCESS')
2910
James E. Blairff80a2f2013-12-27 13:24:06 -08002911 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002912
2913 # A.messages only holds what FakeGerrit places in it. Thus we
2914 # work on the knowledge of what the first message should be as
2915 # it is only configured to go to SMTP.
2916
2917 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002918 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002919 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002920 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002921 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002922 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002923
Clark Boylan3ee090a2014-04-03 20:55:09 -07002924 # Stop queuing timer triggered jobs and let any that may have
2925 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002926 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002927 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002928 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002929 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002930 self.waitUntilSettled()
2931
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002932 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002933 def test_timer_sshkey(self):
2934 "Test that a periodic job can setup SSH key authentication"
2935 self.worker.hold_jobs_in_build = True
2936 self.config.set('zuul', 'layout_config',
2937 'tests/fixtures/layout-timer.yaml')
2938 self.sched.reconfigure(self.config)
2939 self.registerJobs()
2940
2941 # The pipeline triggers every second, so we should have seen
2942 # several by now.
2943 time.sleep(5)
2944 self.waitUntilSettled()
2945
2946 self.assertEqual(len(self.builds), 2)
2947
2948 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2949 self.assertTrue(os.path.isfile(ssh_wrapper))
2950 with open(ssh_wrapper) as f:
2951 ssh_wrapper_content = f.read()
2952 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2953 # In the unit tests Merger runs in the same process,
2954 # so we see its' environment variables
2955 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2956
2957 self.worker.release('.*')
2958 self.waitUntilSettled()
2959 self.assertEqual(len(self.history), 2)
2960
2961 self.assertEqual(self.getJobFromHistory(
2962 'project-bitrot-stable-old').result, 'SUCCESS')
2963 self.assertEqual(self.getJobFromHistory(
2964 'project-bitrot-stable-older').result, 'SUCCESS')
2965
2966 # Stop queuing timer triggered jobs and let any that may have
2967 # queued through so that end of test assertions pass.
2968 self.config.set('zuul', 'layout_config',
2969 'tests/fixtures/layout-no-timer.yaml')
2970 self.sched.reconfigure(self.config)
2971 self.registerJobs()
2972 self.waitUntilSettled()
2973 self.worker.release('.*')
2974 self.waitUntilSettled()
2975
James E. Blair91e34592015-07-31 16:45:59 -07002976 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002977 "Test that the RPC client can enqueue a change"
2978 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002979 A.addApproval('Code-Review', 2)
2980 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002981
2982 client = zuul.rpcclient.RPCClient('127.0.0.1',
2983 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002984 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002985 r = client.enqueue(tenant='tenant-one',
2986 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002987 project='org/project',
2988 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002989 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002990 self.waitUntilSettled()
2991 self.assertEqual(self.getJobFromHistory('project-merge').result,
2992 'SUCCESS')
2993 self.assertEqual(self.getJobFromHistory('project-test1').result,
2994 'SUCCESS')
2995 self.assertEqual(self.getJobFromHistory('project-test2').result,
2996 'SUCCESS')
2997 self.assertEqual(A.data['status'], 'MERGED')
2998 self.assertEqual(A.reported, 2)
2999 self.assertEqual(r, True)
3000
James E. Blair91e34592015-07-31 16:45:59 -07003001 def test_client_enqueue_ref(self):
3002 "Test that the RPC client can enqueue a ref"
3003
3004 client = zuul.rpcclient.RPCClient('127.0.0.1',
3005 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003006 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003007 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003008 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003009 pipeline='post',
3010 project='org/project',
3011 trigger='gerrit',
3012 ref='master',
3013 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
3014 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
3015 self.waitUntilSettled()
3016 job_names = [x.name for x in self.history]
3017 self.assertEqual(len(self.history), 1)
3018 self.assertIn('project-post', job_names)
3019 self.assertEqual(r, True)
3020
James E. Blairad28e912013-11-27 10:43:22 -08003021 def test_client_enqueue_negative(self):
3022 "Test that the RPC client returns errors"
3023 client = zuul.rpcclient.RPCClient('127.0.0.1',
3024 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003025 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003026 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003027 "Invalid tenant"):
3028 r = client.enqueue(tenant='tenant-foo',
3029 pipeline='gate',
3030 project='org/project',
3031 trigger='gerrit',
3032 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003033 self.assertEqual(r, False)
3034
3035 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003036 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003037 r = client.enqueue(tenant='tenant-one',
3038 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003039 project='project-does-not-exist',
3040 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003041 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003042 self.assertEqual(r, False)
3043
3044 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3045 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003046 r = client.enqueue(tenant='tenant-one',
3047 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003048 project='org/project',
3049 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003050 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003051 self.assertEqual(r, False)
3052
3053 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3054 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003055 r = client.enqueue(tenant='tenant-one',
3056 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003057 project='org/project',
3058 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003059 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003060 self.assertEqual(r, False)
3061
3062 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3063 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003064 r = client.enqueue(tenant='tenant-one',
3065 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003066 project='org/project',
3067 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003068 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003069 self.assertEqual(r, False)
3070
3071 self.waitUntilSettled()
3072 self.assertEqual(len(self.history), 0)
3073 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003074
3075 def test_client_promote(self):
3076 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003077 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003078 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3079 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3080 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003081 A.addApproval('Code-Review', 2)
3082 B.addApproval('Code-Review', 2)
3083 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003084
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003085 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3086 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3087 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003088
3089 self.waitUntilSettled()
3090
Paul Belangerbaca3132016-11-04 12:49:54 -04003091 tenant = self.sched.abide.tenants.get('tenant-one')
3092 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003093 enqueue_times = {}
3094 for item in items:
3095 enqueue_times[str(item.change)] = item.enqueue_time
3096
James E. Blair36658cf2013-12-06 17:53:48 -08003097 client = zuul.rpcclient.RPCClient('127.0.0.1',
3098 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003099 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003100 r = client.promote(tenant='tenant-one',
3101 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003102 change_ids=['2,1', '3,1'])
3103
Sean Daguef39b9ca2014-01-10 21:34:35 -05003104 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003105 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003106 for item in items:
3107 self.assertEqual(
3108 enqueue_times[str(item.change)], item.enqueue_time)
3109
James E. Blair78acec92014-02-06 07:11:32 -08003110 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003111 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003112 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003113 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003114 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003115 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003116 self.waitUntilSettled()
3117
3118 self.assertEqual(len(self.builds), 6)
3119 self.assertEqual(self.builds[0].name, 'project-test1')
3120 self.assertEqual(self.builds[1].name, 'project-test2')
3121 self.assertEqual(self.builds[2].name, 'project-test1')
3122 self.assertEqual(self.builds[3].name, 'project-test2')
3123 self.assertEqual(self.builds[4].name, 'project-test1')
3124 self.assertEqual(self.builds[5].name, 'project-test2')
3125
Paul Belangerbaca3132016-11-04 12:49:54 -04003126 self.assertTrue(self.builds[0].hasChanges(B))
3127 self.assertFalse(self.builds[0].hasChanges(A))
3128 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003129
Paul Belangerbaca3132016-11-04 12:49:54 -04003130 self.assertTrue(self.builds[2].hasChanges(B))
3131 self.assertTrue(self.builds[2].hasChanges(C))
3132 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003133
Paul Belangerbaca3132016-11-04 12:49:54 -04003134 self.assertTrue(self.builds[4].hasChanges(B))
3135 self.assertTrue(self.builds[4].hasChanges(C))
3136 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003137
Paul Belanger174a8272017-03-14 13:20:10 -04003138 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003139 self.waitUntilSettled()
3140
3141 self.assertEqual(A.data['status'], 'MERGED')
3142 self.assertEqual(A.reported, 2)
3143 self.assertEqual(B.data['status'], 'MERGED')
3144 self.assertEqual(B.reported, 2)
3145 self.assertEqual(C.data['status'], 'MERGED')
3146 self.assertEqual(C.reported, 2)
3147
James E. Blair36658cf2013-12-06 17:53:48 -08003148 self.assertEqual(r, True)
3149
3150 def test_client_promote_dependent(self):
3151 "Test that the RPC client can promote a dependent change"
3152 # C (depends on B) -> B -> A ; then promote C to get:
3153 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003154 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003155 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3156 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3157 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3158
3159 C.setDependsOn(B, 1)
3160
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003161 A.addApproval('Code-Review', 2)
3162 B.addApproval('Code-Review', 2)
3163 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003164
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003165 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3166 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3167 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003168
3169 self.waitUntilSettled()
3170
3171 client = zuul.rpcclient.RPCClient('127.0.0.1',
3172 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003173 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003174 r = client.promote(tenant='tenant-one',
3175 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003176 change_ids=['3,1'])
3177
James E. Blair78acec92014-02-06 07:11:32 -08003178 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003179 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003180 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003181 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003182 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003183 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003184 self.waitUntilSettled()
3185
3186 self.assertEqual(len(self.builds), 6)
3187 self.assertEqual(self.builds[0].name, 'project-test1')
3188 self.assertEqual(self.builds[1].name, 'project-test2')
3189 self.assertEqual(self.builds[2].name, 'project-test1')
3190 self.assertEqual(self.builds[3].name, 'project-test2')
3191 self.assertEqual(self.builds[4].name, 'project-test1')
3192 self.assertEqual(self.builds[5].name, 'project-test2')
3193
Paul Belangerbaca3132016-11-04 12:49:54 -04003194 self.assertTrue(self.builds[0].hasChanges(B))
3195 self.assertFalse(self.builds[0].hasChanges(A))
3196 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003197
Paul Belangerbaca3132016-11-04 12:49:54 -04003198 self.assertTrue(self.builds[2].hasChanges(B))
3199 self.assertTrue(self.builds[2].hasChanges(C))
3200 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003201
Paul Belangerbaca3132016-11-04 12:49:54 -04003202 self.assertTrue(self.builds[4].hasChanges(B))
3203 self.assertTrue(self.builds[4].hasChanges(C))
3204 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003205
Paul Belanger174a8272017-03-14 13:20:10 -04003206 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003207 self.waitUntilSettled()
3208
3209 self.assertEqual(A.data['status'], 'MERGED')
3210 self.assertEqual(A.reported, 2)
3211 self.assertEqual(B.data['status'], 'MERGED')
3212 self.assertEqual(B.reported, 2)
3213 self.assertEqual(C.data['status'], 'MERGED')
3214 self.assertEqual(C.reported, 2)
3215
James E. Blair36658cf2013-12-06 17:53:48 -08003216 self.assertEqual(r, True)
3217
3218 def test_client_promote_negative(self):
3219 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003220 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003221 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003222 A.addApproval('Code-Review', 2)
3223 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003224 self.waitUntilSettled()
3225
3226 client = zuul.rpcclient.RPCClient('127.0.0.1',
3227 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003228 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003229
3230 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003231 r = client.promote(tenant='tenant-one',
3232 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003233 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003234 self.assertEqual(r, False)
3235
3236 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003237 r = client.promote(tenant='tenant-one',
3238 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003239 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003240 self.assertEqual(r, False)
3241
Paul Belanger174a8272017-03-14 13:20:10 -04003242 self.executor_server.hold_jobs_in_build = False
3243 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003244 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003245
James E. Blaircc324192017-04-19 14:36:08 -07003246 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003247 def test_queue_rate_limiting(self):
3248 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003249 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003250 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3251 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3252 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3253
3254 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003255 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003256
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003257 A.addApproval('Code-Review', 2)
3258 B.addApproval('Code-Review', 2)
3259 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003260
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003261 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3262 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3263 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003264 self.waitUntilSettled()
3265
3266 # Only A and B will have their merge jobs queued because
3267 # window is 2.
3268 self.assertEqual(len(self.builds), 2)
3269 self.assertEqual(self.builds[0].name, 'project-merge')
3270 self.assertEqual(self.builds[1].name, 'project-merge')
3271
James E. Blair732ee862017-04-21 07:49:26 -07003272 # Release the merge jobs one at a time.
3273 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003274 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003275 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003276 self.waitUntilSettled()
3277
3278 # Only A and B will have their test jobs queued because
3279 # window is 2.
3280 self.assertEqual(len(self.builds), 4)
3281 self.assertEqual(self.builds[0].name, 'project-test1')
3282 self.assertEqual(self.builds[1].name, 'project-test2')
3283 self.assertEqual(self.builds[2].name, 'project-test1')
3284 self.assertEqual(self.builds[3].name, 'project-test2')
3285
Paul Belanger174a8272017-03-14 13:20:10 -04003286 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003287 self.waitUntilSettled()
3288
James E. Blaircc324192017-04-19 14:36:08 -07003289 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003290 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003291 # A failed so window is reduced by 1 to 1.
3292 self.assertEqual(queue.window, 1)
3293 self.assertEqual(queue.window_floor, 1)
3294 self.assertEqual(A.data['status'], 'NEW')
3295
3296 # Gate is reset and only B's merge job is queued because
3297 # window shrunk to 1.
3298 self.assertEqual(len(self.builds), 1)
3299 self.assertEqual(self.builds[0].name, 'project-merge')
3300
Paul Belanger174a8272017-03-14 13:20:10 -04003301 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003302 self.waitUntilSettled()
3303
3304 # Only B's test jobs are queued because window is still 1.
3305 self.assertEqual(len(self.builds), 2)
3306 self.assertEqual(self.builds[0].name, 'project-test1')
3307 self.assertEqual(self.builds[1].name, 'project-test2')
3308
Paul Belanger174a8272017-03-14 13:20:10 -04003309 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003310 self.waitUntilSettled()
3311
3312 # B was successfully merged so window is increased to 2.
3313 self.assertEqual(queue.window, 2)
3314 self.assertEqual(queue.window_floor, 1)
3315 self.assertEqual(B.data['status'], 'MERGED')
3316
3317 # Only C is left and its merge job is queued.
3318 self.assertEqual(len(self.builds), 1)
3319 self.assertEqual(self.builds[0].name, 'project-merge')
3320
Paul Belanger174a8272017-03-14 13:20:10 -04003321 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003322 self.waitUntilSettled()
3323
3324 # After successful merge job the test jobs for C are queued.
3325 self.assertEqual(len(self.builds), 2)
3326 self.assertEqual(self.builds[0].name, 'project-test1')
3327 self.assertEqual(self.builds[1].name, 'project-test2')
3328
Paul Belanger174a8272017-03-14 13:20:10 -04003329 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003330 self.waitUntilSettled()
3331
3332 # C successfully merged so window is bumped to 3.
3333 self.assertEqual(queue.window, 3)
3334 self.assertEqual(queue.window_floor, 1)
3335 self.assertEqual(C.data['status'], 'MERGED')
3336
James E. Blaircc324192017-04-19 14:36:08 -07003337 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003338 def test_queue_rate_limiting_dependent(self):
3339 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003340 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3342 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3343 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3344
3345 B.setDependsOn(A, 1)
3346
Paul Belanger174a8272017-03-14 13:20:10 -04003347 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003348
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003349 A.addApproval('Code-Review', 2)
3350 B.addApproval('Code-Review', 2)
3351 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003352
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003353 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3354 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3355 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003356 self.waitUntilSettled()
3357
3358 # Only A and B will have their merge jobs queued because
3359 # window is 2.
3360 self.assertEqual(len(self.builds), 2)
3361 self.assertEqual(self.builds[0].name, 'project-merge')
3362 self.assertEqual(self.builds[1].name, 'project-merge')
3363
James E. Blairf4a5f022017-04-18 14:01:10 -07003364 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003365
3366 # Only A and B will have their test jobs queued because
3367 # window is 2.
3368 self.assertEqual(len(self.builds), 4)
3369 self.assertEqual(self.builds[0].name, 'project-test1')
3370 self.assertEqual(self.builds[1].name, 'project-test2')
3371 self.assertEqual(self.builds[2].name, 'project-test1')
3372 self.assertEqual(self.builds[3].name, 'project-test2')
3373
Paul Belanger174a8272017-03-14 13:20:10 -04003374 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003375 self.waitUntilSettled()
3376
James E. Blaircc324192017-04-19 14:36:08 -07003377 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003378 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003379 # A failed so window is reduced by 1 to 1.
3380 self.assertEqual(queue.window, 1)
3381 self.assertEqual(queue.window_floor, 1)
3382 self.assertEqual(A.data['status'], 'NEW')
3383 self.assertEqual(B.data['status'], 'NEW')
3384
3385 # Gate is reset and only C's merge job is queued because
3386 # window shrunk to 1 and A and B were dequeued.
3387 self.assertEqual(len(self.builds), 1)
3388 self.assertEqual(self.builds[0].name, 'project-merge')
3389
James E. Blairf4a5f022017-04-18 14:01:10 -07003390 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003391
3392 # Only C's test jobs are queued because window is still 1.
3393 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003394 builds = self.getSortedBuilds()
3395 self.assertEqual(builds[0].name, 'project-test1')
3396 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003397
Paul Belanger174a8272017-03-14 13:20:10 -04003398 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003399 self.waitUntilSettled()
3400
3401 # C was successfully merged so window is increased to 2.
3402 self.assertEqual(queue.window, 2)
3403 self.assertEqual(queue.window_floor, 1)
3404 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003405
3406 def test_worker_update_metadata(self):
3407 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003408 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003409
3410 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003411 A.addApproval('Code-Review', 2)
3412 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003413 self.waitUntilSettled()
3414
Clint Byrumd4de79a2017-05-27 11:39:13 -07003415 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003416
3417 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003418 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003419
3420 start = time.time()
3421 while True:
3422 if time.time() - start > 10:
3423 raise Exception("Timeout waiting for gearman server to report "
3424 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003425 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003426 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003427 break
3428 else:
3429 time.sleep(0)
3430
3431 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003432 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003433
Paul Belanger174a8272017-03-14 13:20:10 -04003434 self.executor_server.hold_jobs_in_build = False
3435 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003436 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003437
James E. Blaircc324192017-04-19 14:36:08 -07003438 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003439 def test_footer_message(self):
3440 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003441 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003442 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003443 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003444 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003445 self.waitUntilSettled()
3446
3447 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003448 B.addApproval('Code-Review', 2)
3449 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003450 self.waitUntilSettled()
3451
3452 self.assertEqual(2, len(self.smtp_messages))
3453
Adam Gandelman82258fd2017-02-07 16:51:58 -08003454 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003455Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003456http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003457
Adam Gandelman82258fd2017-02-07 16:51:58 -08003458 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003459For CI problems and help debugging, contact ci@example.org"""
3460
Adam Gandelman82258fd2017-02-07 16:51:58 -08003461 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3462 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3463 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3464 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003465
Tobias Henkel9842bd72017-05-16 13:40:03 +02003466 @simple_layout('layouts/unmanaged-project.yaml')
3467 def test_unmanaged_project_start_message(self):
3468 "Test start reporting is not done for unmanaged projects."
3469 self.init_repo("org/project", tag='init')
3470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3471 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3472 self.waitUntilSettled()
3473
3474 self.assertEqual(0, len(A.messages))
3475
Clint Byrumb9de8be2017-05-27 13:49:19 -07003476 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003477 def test_merge_failure_reporters(self):
3478 """Check that the config is set up correctly"""
3479
Clint Byrumb9de8be2017-05-27 13:49:19 -07003480 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003481 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003482 "Merge Failed.\n\nThis change or one of its cross-repo "
3483 "dependencies was unable to be automatically merged with the "
3484 "current state of its repository. Please rebase the change and "
3485 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003486 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003487 self.assertEqual(
3488 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003489 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003490
3491 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003492 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003493 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003494 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003495
3496 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003497 tenant.layout.pipelines['check'].merge_failure_actions[0],
3498 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003499
3500 self.assertTrue(
3501 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003502 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003503 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003504 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3505 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003506 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003507 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003508 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003509 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003510 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003511 gerritreporter.GerritReporter) and
3512 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003513 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003514 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003515 )
3516 )
3517
James E. Blairec056492016-07-22 09:45:56 -07003518 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003519 def test_merge_failure_reports(self):
3520 """Check that when a change fails to merge the correct message is sent
3521 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003522 self.updateConfigLayout(
3523 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003524 self.sched.reconfigure(self.config)
3525 self.registerJobs()
3526
3527 # Check a test failure isn't reported to SMTP
3528 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003529 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003530 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003531 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003532 self.waitUntilSettled()
3533
3534 self.assertEqual(3, len(self.history)) # 3 jobs
3535 self.assertEqual(0, len(self.smtp_messages))
3536
3537 # Check a merge failure is reported to SMTP
3538 # B should be merged, but C will conflict with B
3539 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3540 B.addPatchset(['conflict'])
3541 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3542 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003543 B.addApproval('Code-Review', 2)
3544 C.addApproval('Code-Review', 2)
3545 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3546 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003547 self.waitUntilSettled()
3548
3549 self.assertEqual(6, len(self.history)) # A and B jobs
3550 self.assertEqual(1, len(self.smtp_messages))
3551 self.assertEqual('The merge failed! For more information...',
3552 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003553
James E. Blairec056492016-07-22 09:45:56 -07003554 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003555 def test_default_merge_failure_reports(self):
3556 """Check that the default merge failure reports are correct."""
3557
3558 # A should report success, B should report merge failure.
3559 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3560 A.addPatchset(['conflict'])
3561 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3562 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003563 A.addApproval('Code-Review', 2)
3564 B.addApproval('Code-Review', 2)
3565 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3566 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003567 self.waitUntilSettled()
3568
3569 self.assertEqual(3, len(self.history)) # A jobs
3570 self.assertEqual(A.reported, 2)
3571 self.assertEqual(B.reported, 2)
3572 self.assertEqual(A.data['status'], 'MERGED')
3573 self.assertEqual(B.data['status'], 'NEW')
3574 self.assertIn('Build succeeded', A.messages[1])
3575 self.assertIn('Merge Failed', B.messages[1])
3576 self.assertIn('automatically merged', B.messages[1])
3577 self.assertNotIn('logs.example.com', B.messages[1])
3578 self.assertNotIn('SKIPPED', B.messages[1])
3579
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003580 def test_client_get_running_jobs(self):
3581 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003582 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003583 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003584 A.addApproval('Code-Review', 2)
3585 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003586 self.waitUntilSettled()
3587
3588 client = zuul.rpcclient.RPCClient('127.0.0.1',
3589 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003590 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003591
3592 # Wait for gearman server to send the initial workData back to zuul
3593 start = time.time()
3594 while True:
3595 if time.time() - start > 10:
3596 raise Exception("Timeout waiting for gearman server to report "
3597 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003598 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003599 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003600 break
3601 else:
3602 time.sleep(0)
3603
3604 running_items = client.get_running_jobs()
3605
3606 self.assertEqual(1, len(running_items))
3607 running_item = running_items[0]
3608 self.assertEqual([], running_item['failing_reasons'])
3609 self.assertEqual([], running_item['items_behind'])
3610 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003611 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003612 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003613 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003614 self.assertEqual(True, running_item['active'])
3615 self.assertEqual('1,1', running_item['id'])
3616
3617 self.assertEqual(3, len(running_item['jobs']))
3618 for job in running_item['jobs']:
3619 if job['name'] == 'project-merge':
3620 self.assertEqual('project-merge', job['name'])
3621 self.assertEqual('gate', job['pipeline'])
3622 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003623 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003624 'static/stream.html?uuid={uuid}&logfile=console.log'
3625 .format(uuid=job['uuid']), job['url'])
3626 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003627 'finger://{hostname}/{uuid}'.format(
3628 hostname=self.executor_server.hostname,
3629 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003630 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003631 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003632 self.assertEqual(False, job['canceled'])
3633 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003634 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003635 self.assertEqual('gate', job['pipeline'])
3636 break
3637
Paul Belanger174a8272017-03-14 13:20:10 -04003638 self.executor_server.hold_jobs_in_build = False
3639 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003640 self.waitUntilSettled()
3641
3642 running_items = client.get_running_jobs()
3643 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003644
James E. Blairb70e55a2017-04-19 12:57:02 -07003645 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003646 def test_nonvoting_pipeline(self):
3647 "Test that a nonvoting pipeline (experimental) can still report"
3648
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003649 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3650 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003651 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3652 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003653
3654 self.assertEqual(self.getJobFromHistory('project-merge').result,
3655 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003656 self.assertEqual(
3657 self.getJobFromHistory('experimental-project-test').result,
3658 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003659 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003660
3661 def test_crd_gate(self):
3662 "Test cross-repo dependencies"
3663 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3664 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003665 A.addApproval('Code-Review', 2)
3666 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003667
3668 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3669 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3670 AM2.setMerged()
3671 AM1.setMerged()
3672
3673 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3674 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3675 BM2.setMerged()
3676 BM1.setMerged()
3677
3678 # A -> AM1 -> AM2
3679 # B -> BM1 -> BM2
3680 # A Depends-On: B
3681 # M2 is here to make sure it is never queried. If it is, it
3682 # means zuul is walking down the entire history of merged
3683 # changes.
3684
3685 B.setDependsOn(BM1, 1)
3686 BM1.setDependsOn(BM2, 1)
3687
3688 A.setDependsOn(AM1, 1)
3689 AM1.setDependsOn(AM2, 1)
3690
3691 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3692 A.subject, B.data['id'])
3693
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003694 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003695 self.waitUntilSettled()
3696
3697 self.assertEqual(A.data['status'], 'NEW')
3698 self.assertEqual(B.data['status'], 'NEW')
3699
Paul Belanger5dccbe72016-11-14 11:17:37 -05003700 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003701 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003702
Paul Belanger174a8272017-03-14 13:20:10 -04003703 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003704 B.addApproval('Approved', 1)
3705 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003706 self.waitUntilSettled()
3707
Paul Belanger174a8272017-03-14 13:20:10 -04003708 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003709 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003710 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003711 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003712 self.executor_server.hold_jobs_in_build = False
3713 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003714 self.waitUntilSettled()
3715
3716 self.assertEqual(AM2.queried, 0)
3717 self.assertEqual(BM2.queried, 0)
3718 self.assertEqual(A.data['status'], 'MERGED')
3719 self.assertEqual(B.data['status'], 'MERGED')
3720 self.assertEqual(A.reported, 2)
3721 self.assertEqual(B.reported, 2)
3722
Paul Belanger5dccbe72016-11-14 11:17:37 -05003723 changes = self.getJobFromHistory(
3724 'project-merge', 'org/project1').changes
3725 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003726
3727 def test_crd_branch(self):
3728 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003729
3730 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003731 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3732 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3733 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3734 C.data['id'] = B.data['id']
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003735 A.addApproval('Code-Review', 2)
3736 B.addApproval('Code-Review', 2)
3737 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003738
3739 # A Depends-On: B+C
3740 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3741 A.subject, B.data['id'])
3742
Paul Belanger174a8272017-03-14 13:20:10 -04003743 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003744 B.addApproval('Approved', 1)
3745 C.addApproval('Approved', 1)
3746 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003747 self.waitUntilSettled()
3748
Paul Belanger174a8272017-03-14 13:20:10 -04003749 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003750 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003751 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003752 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003753 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003754 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003755 self.executor_server.hold_jobs_in_build = False
3756 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003757 self.waitUntilSettled()
3758
3759 self.assertEqual(A.data['status'], 'MERGED')
3760 self.assertEqual(B.data['status'], 'MERGED')
3761 self.assertEqual(C.data['status'], 'MERGED')
3762 self.assertEqual(A.reported, 2)
3763 self.assertEqual(B.reported, 2)
3764 self.assertEqual(C.reported, 2)
3765
Paul Belanger6379db12016-11-14 13:57:54 -05003766 changes = self.getJobFromHistory(
3767 'project-merge', 'org/project1').changes
3768 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003769
3770 def test_crd_multiline(self):
3771 "Test multiple depends-on lines in commit"
3772 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3773 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3774 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003775 A.addApproval('Code-Review', 2)
3776 B.addApproval('Code-Review', 2)
3777 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003778
3779 # A Depends-On: B+C
3780 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3781 A.subject, B.data['id'], C.data['id'])
3782
Paul Belanger174a8272017-03-14 13:20:10 -04003783 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003784 B.addApproval('Approved', 1)
3785 C.addApproval('Approved', 1)
3786 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003787 self.waitUntilSettled()
3788
Paul Belanger174a8272017-03-14 13:20:10 -04003789 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003790 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003791 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003792 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003793 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003794 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003795 self.executor_server.hold_jobs_in_build = False
3796 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003797 self.waitUntilSettled()
3798
3799 self.assertEqual(A.data['status'], 'MERGED')
3800 self.assertEqual(B.data['status'], 'MERGED')
3801 self.assertEqual(C.data['status'], 'MERGED')
3802 self.assertEqual(A.reported, 2)
3803 self.assertEqual(B.reported, 2)
3804 self.assertEqual(C.reported, 2)
3805
Paul Belanger5dccbe72016-11-14 11:17:37 -05003806 changes = self.getJobFromHistory(
3807 'project-merge', 'org/project1').changes
3808 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003809
3810 def test_crd_unshared_gate(self):
3811 "Test cross-repo dependencies in unshared gate queues"
3812 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3813 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003814 A.addApproval('Code-Review', 2)
3815 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003816
3817 # A Depends-On: B
3818 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3819 A.subject, B.data['id'])
3820
3821 # A and B do not share a queue, make sure that A is unable to
3822 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003823 B.addApproval('Approved', 1)
3824 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003825 self.waitUntilSettled()
3826
3827 self.assertEqual(A.data['status'], 'NEW')
3828 self.assertEqual(B.data['status'], 'NEW')
3829 self.assertEqual(A.reported, 0)
3830 self.assertEqual(B.reported, 0)
3831 self.assertEqual(len(self.history), 0)
3832
3833 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003834 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003835 self.waitUntilSettled()
3836
3837 self.assertEqual(B.data['status'], 'MERGED')
3838 self.assertEqual(B.reported, 2)
3839
3840 # Now that B is merged, A should be able to be enqueued and
3841 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003842 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003843 self.waitUntilSettled()
3844
3845 self.assertEqual(A.data['status'], 'MERGED')
3846 self.assertEqual(A.reported, 2)
3847
James E. Blair96698e22015-04-02 07:48:21 -07003848 def test_crd_gate_reverse(self):
3849 "Test reverse cross-repo dependencies"
3850 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3851 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003852 A.addApproval('Code-Review', 2)
3853 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003854
3855 # A Depends-On: B
3856
3857 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3858 A.subject, B.data['id'])
3859
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003860 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003861 self.waitUntilSettled()
3862
3863 self.assertEqual(A.data['status'], 'NEW')
3864 self.assertEqual(B.data['status'], 'NEW')
3865
Paul Belanger174a8272017-03-14 13:20:10 -04003866 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003867 A.addApproval('Approved', 1)
3868 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003869 self.waitUntilSettled()
3870
Paul Belanger174a8272017-03-14 13:20:10 -04003871 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003872 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003873 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003874 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003875 self.executor_server.hold_jobs_in_build = False
3876 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003877 self.waitUntilSettled()
3878
3879 self.assertEqual(A.data['status'], 'MERGED')
3880 self.assertEqual(B.data['status'], 'MERGED')
3881 self.assertEqual(A.reported, 2)
3882 self.assertEqual(B.reported, 2)
3883
Paul Belanger5dccbe72016-11-14 11:17:37 -05003884 changes = self.getJobFromHistory(
3885 'project-merge', 'org/project1').changes
3886 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003887
James E. Blair5ee24252014-12-30 10:12:29 -08003888 def test_crd_cycle(self):
3889 "Test cross-repo dependency cycles"
3890 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3891 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003892 A.addApproval('Code-Review', 2)
3893 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003894
3895 # A -> B -> A (via commit-depends)
3896
3897 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3898 A.subject, B.data['id'])
3899 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3900 B.subject, A.data['id'])
3901
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003902 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003903 self.waitUntilSettled()
3904
3905 self.assertEqual(A.reported, 0)
3906 self.assertEqual(B.reported, 0)
3907 self.assertEqual(A.data['status'], 'NEW')
3908 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003909
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003910 def test_crd_gate_unknown(self):
3911 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003912 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003913 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3914 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003915 A.addApproval('Code-Review', 2)
3916 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003917
3918 # A Depends-On: B
3919 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3920 A.subject, B.data['id'])
3921
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003922 B.addApproval('Approved', 1)
3923 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003924 self.waitUntilSettled()
3925
3926 # Unknown projects cannot share a queue with any other
3927 # since they don't have common jobs with any other (they have no jobs).
3928 # Changes which depend on unknown project changes
3929 # should not be processed in dependent pipeline
3930 self.assertEqual(A.data['status'], 'NEW')
3931 self.assertEqual(B.data['status'], 'NEW')
3932 self.assertEqual(A.reported, 0)
3933 self.assertEqual(B.reported, 0)
3934 self.assertEqual(len(self.history), 0)
3935
James E. Blair7720b402017-06-07 08:51:49 -07003936 # Simulate change B being gated outside this layout Set the
3937 # change merged before submitting the event so that when the
3938 # event triggers a gerrit query to update the change, we get
3939 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003940 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003941 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003942 self.waitUntilSettled()
3943 self.assertEqual(len(self.history), 0)
3944
3945 # Now that B is merged, A should be able to be enqueued and
3946 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003947 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003948 self.waitUntilSettled()
3949
3950 self.assertEqual(A.data['status'], 'MERGED')
3951 self.assertEqual(A.reported, 2)
3952 self.assertEqual(B.data['status'], 'MERGED')
3953 self.assertEqual(B.reported, 0)
3954
James E. Blairbfb8e042014-12-30 17:01:44 -08003955 def test_crd_check(self):
3956 "Test cross-repo dependencies in independent pipelines"
3957
Paul Belanger174a8272017-03-14 13:20:10 -04003958 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003959 self.gearman_server.hold_jobs_in_queue = True
3960 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3961 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3962
3963 # A Depends-On: B
3964 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3965 A.subject, B.data['id'])
3966
3967 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3968 self.waitUntilSettled()
3969
James E. Blairbfb8e042014-12-30 17:01:44 -08003970 self.gearman_server.hold_jobs_in_queue = False
3971 self.gearman_server.release()
3972 self.waitUntilSettled()
3973
Paul Belanger174a8272017-03-14 13:20:10 -04003974 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003975 self.waitUntilSettled()
3976
James E. Blair2a535672017-04-27 12:03:15 -07003977 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003978
Paul Belanger174a8272017-03-14 13:20:10 -04003979 self.executor_server.hold_jobs_in_build = False
3980 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003981 self.waitUntilSettled()
3982
James E. Blairbfb8e042014-12-30 17:01:44 -08003983 self.assertEqual(A.data['status'], 'NEW')
3984 self.assertEqual(B.data['status'], 'NEW')
3985 self.assertEqual(A.reported, 1)
3986 self.assertEqual(B.reported, 0)
3987
3988 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003989 tenant = self.sched.abide.tenants.get('tenant-one')
3990 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003991
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003992 def test_crd_check_git_depends(self):
3993 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003994 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003995 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3996 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3997
3998 # Add two git-dependent changes and make sure they both report
3999 # success.
4000 B.setDependsOn(A, 1)
4001 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4002 self.waitUntilSettled()
4003 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4004 self.waitUntilSettled()
4005
James E. Blairb8c16472015-05-05 14:55:26 -07004006 self.orderedRelease()
4007 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004008 self.waitUntilSettled()
4009
4010 self.assertEqual(A.data['status'], 'NEW')
4011 self.assertEqual(B.data['status'], 'NEW')
4012 self.assertEqual(A.reported, 1)
4013 self.assertEqual(B.reported, 1)
4014
4015 self.assertEqual(self.history[0].changes, '1,1')
4016 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004017 tenant = self.sched.abide.tenants.get('tenant-one')
4018 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004019
4020 self.assertIn('Build succeeded', A.messages[0])
4021 self.assertIn('Build succeeded', B.messages[0])
4022
4023 def test_crd_check_duplicate(self):
4024 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004025 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004026 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4027 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004028 tenant = self.sched.abide.tenants.get('tenant-one')
4029 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004030
4031 # Add two git-dependent changes...
4032 B.setDependsOn(A, 1)
4033 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4034 self.waitUntilSettled()
4035 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4036
4037 # ...make sure the live one is not duplicated...
4038 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4039 self.waitUntilSettled()
4040 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4041
4042 # ...but the non-live one is able to be.
4043 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4044 self.waitUntilSettled()
4045 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4046
Clark Boylandd849822015-03-02 12:38:14 -08004047 # Release jobs in order to avoid races with change A jobs
4048 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004049 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004050 self.executor_server.hold_jobs_in_build = False
4051 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004052 self.waitUntilSettled()
4053
4054 self.assertEqual(A.data['status'], 'NEW')
4055 self.assertEqual(B.data['status'], 'NEW')
4056 self.assertEqual(A.reported, 1)
4057 self.assertEqual(B.reported, 1)
4058
4059 self.assertEqual(self.history[0].changes, '1,1 2,1')
4060 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004061 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004062
4063 self.assertIn('Build succeeded', A.messages[0])
4064 self.assertIn('Build succeeded', B.messages[0])
4065
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004066 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004067 "Test cross-repo dependencies re-enqueued in independent pipelines"
4068
4069 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004070 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4071 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004072
4073 # A Depends-On: B
4074 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4075 A.subject, B.data['id'])
4076
4077 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4078 self.waitUntilSettled()
4079
4080 self.sched.reconfigure(self.config)
4081
4082 # Make sure the items still share a change queue, and the
4083 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004084 tenant = self.sched.abide.tenants.get('tenant-one')
4085 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4086 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004087 first_item = queue.queue[0]
4088 for item in queue.queue:
4089 self.assertEqual(item.queue, first_item.queue)
4090 self.assertFalse(first_item.live)
4091 self.assertTrue(queue.queue[1].live)
4092
4093 self.gearman_server.hold_jobs_in_queue = False
4094 self.gearman_server.release()
4095 self.waitUntilSettled()
4096
4097 self.assertEqual(A.data['status'], 'NEW')
4098 self.assertEqual(B.data['status'], 'NEW')
4099 self.assertEqual(A.reported, 1)
4100 self.assertEqual(B.reported, 0)
4101
4102 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004103 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004104
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004105 def test_crd_check_reconfiguration(self):
4106 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4107
4108 def test_crd_undefined_project(self):
4109 """Test that undefined projects in dependencies are handled for
4110 independent pipelines"""
4111 # It's a hack for fake gerrit,
4112 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004113 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004114 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4115
Jesse Keatingea68ab42017-04-19 15:03:30 -07004116 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004117 def test_crd_check_ignore_dependencies(self):
4118 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004119
4120 self.gearman_server.hold_jobs_in_queue = True
4121 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4122 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4123 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4124
4125 # A Depends-On: B
4126 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4127 A.subject, B.data['id'])
4128 # C git-depends on B
4129 C.setDependsOn(B, 1)
4130 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4131 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4132 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4133 self.waitUntilSettled()
4134
4135 # Make sure none of the items share a change queue, and all
4136 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004137 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004138 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004139 self.assertEqual(len(check_pipeline.queues), 3)
4140 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4141 for item in check_pipeline.getAllItems():
4142 self.assertTrue(item.live)
4143
4144 self.gearman_server.hold_jobs_in_queue = False
4145 self.gearman_server.release()
4146 self.waitUntilSettled()
4147
4148 self.assertEqual(A.data['status'], 'NEW')
4149 self.assertEqual(B.data['status'], 'NEW')
4150 self.assertEqual(C.data['status'], 'NEW')
4151 self.assertEqual(A.reported, 1)
4152 self.assertEqual(B.reported, 1)
4153 self.assertEqual(C.reported, 1)
4154
4155 # Each job should have tested exactly one change
4156 for job in self.history:
4157 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004158
James E. Blair7cb84542017-04-19 13:35:05 -07004159 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004160 def test_crd_check_transitive(self):
4161 "Test transitive cross-repo dependencies"
4162 # Specifically, if A -> B -> C, and C gets a new patchset and
4163 # A gets a new patchset, ensure the test of A,2 includes B,1
4164 # and C,2 (not C,1 which would indicate stale data in the
4165 # cache for B).
4166 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4167 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4168 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4169
4170 # A Depends-On: B
4171 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4172 A.subject, B.data['id'])
4173
4174 # B Depends-On: C
4175 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4176 B.subject, C.data['id'])
4177
4178 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4179 self.waitUntilSettled()
4180 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4181
4182 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4183 self.waitUntilSettled()
4184 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4185
4186 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4187 self.waitUntilSettled()
4188 self.assertEqual(self.history[-1].changes, '3,1')
4189
4190 C.addPatchset()
4191 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4192 self.waitUntilSettled()
4193 self.assertEqual(self.history[-1].changes, '3,2')
4194
4195 A.addPatchset()
4196 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4197 self.waitUntilSettled()
4198 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004199
James E. Blaira8b90b32016-08-24 15:18:50 -07004200 def test_crd_check_unknown(self):
4201 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004202 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004203 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4204 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4205 # A Depends-On: B
4206 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4207 A.subject, B.data['id'])
4208
4209 # Make sure zuul has seen an event on B.
4210 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4211 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4212 self.waitUntilSettled()
4213
4214 self.assertEqual(A.data['status'], 'NEW')
4215 self.assertEqual(A.reported, 1)
4216 self.assertEqual(B.data['status'], 'NEW')
4217 self.assertEqual(B.reported, 0)
4218
James E. Blair92464a22016-04-05 10:21:26 -07004219 def test_crd_cycle_join(self):
4220 "Test an updated change creates a cycle"
4221 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4222
4223 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4224 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004225 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004226
4227 # Create B->A
4228 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4229 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4230 B.subject, A.data['id'])
4231 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4232 self.waitUntilSettled()
4233
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004234 # Dep is there so zuul should have reported on B
4235 self.assertEqual(B.reported, 1)
4236
James E. Blair92464a22016-04-05 10:21:26 -07004237 # Update A to add A->B (a cycle).
4238 A.addPatchset()
4239 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4240 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004241 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4242 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004243
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004244 # Dependency cycle injected so zuul should not have reported again on A
4245 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004246
4247 # Now if we update B to remove the depends-on, everything
4248 # should be okay. B; A->B
4249
4250 B.addPatchset()
4251 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004252 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4253 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004254
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004255 # Cycle was removed so now zuul should have reported again on A
4256 self.assertEqual(A.reported, 2)
4257
4258 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4259 self.waitUntilSettled()
4260 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004261
Jesse Keatingcc67b122017-04-19 14:54:57 -07004262 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004263 def test_disable_at(self):
4264 "Test a pipeline will only report to the disabled trigger when failing"
4265
Jesse Keatingcc67b122017-04-19 14:54:57 -07004266 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004267 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004268 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004269 0, tenant.layout.pipelines['check']._consecutive_failures)
4270 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004271
4272 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4273 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4274 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4275 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4276 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4277 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4278 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4279 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4280 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4281 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4282 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4283
Paul Belanger174a8272017-03-14 13:20:10 -04004284 self.executor_server.failJob('project-test1', A)
4285 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004286 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004287 self.executor_server.failJob('project-test1', D)
4288 self.executor_server.failJob('project-test1', E)
4289 self.executor_server.failJob('project-test1', F)
4290 self.executor_server.failJob('project-test1', G)
4291 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004292 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004293 self.executor_server.failJob('project-test1', J)
4294 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004295
4296 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4297 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4298 self.waitUntilSettled()
4299
4300 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004301 2, tenant.layout.pipelines['check']._consecutive_failures)
4302 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004303
4304 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4305 self.waitUntilSettled()
4306
4307 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004308 0, tenant.layout.pipelines['check']._consecutive_failures)
4309 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004310
4311 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4312 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4313 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4314 self.waitUntilSettled()
4315
4316 # We should be disabled now
4317 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004318 3, tenant.layout.pipelines['check']._consecutive_failures)
4319 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004320
4321 # We need to wait between each of these patches to make sure the
4322 # smtp messages come back in an expected order
4323 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4324 self.waitUntilSettled()
4325 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4326 self.waitUntilSettled()
4327 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4328 self.waitUntilSettled()
4329
4330 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4331 # leaving a message on each change
4332 self.assertEqual(1, len(A.messages))
4333 self.assertIn('Build failed.', A.messages[0])
4334 self.assertEqual(1, len(B.messages))
4335 self.assertIn('Build failed.', B.messages[0])
4336 self.assertEqual(1, len(C.messages))
4337 self.assertIn('Build succeeded.', C.messages[0])
4338 self.assertEqual(1, len(D.messages))
4339 self.assertIn('Build failed.', D.messages[0])
4340 self.assertEqual(1, len(E.messages))
4341 self.assertIn('Build failed.', E.messages[0])
4342 self.assertEqual(1, len(F.messages))
4343 self.assertIn('Build failed.', F.messages[0])
4344
4345 # The last 3 (GHI) would have only reported via smtp.
4346 self.assertEqual(3, len(self.smtp_messages))
4347 self.assertEqual(0, len(G.messages))
4348 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004349 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004350 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004351 self.assertEqual(0, len(H.messages))
4352 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004353 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004354 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004355 self.assertEqual(0, len(I.messages))
4356 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004357 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004358 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004359
4360 # Now reload the configuration (simulate a HUP) to check the pipeline
4361 # comes out of disabled
4362 self.sched.reconfigure(self.config)
4363
Jesse Keatingcc67b122017-04-19 14:54:57 -07004364 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004365
4366 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004367 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004368 0, tenant.layout.pipelines['check']._consecutive_failures)
4369 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004370
4371 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4372 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4373 self.waitUntilSettled()
4374
4375 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004376 2, tenant.layout.pipelines['check']._consecutive_failures)
4377 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004378
4379 # J and K went back to gerrit
4380 self.assertEqual(1, len(J.messages))
4381 self.assertIn('Build failed.', J.messages[0])
4382 self.assertEqual(1, len(K.messages))
4383 self.assertIn('Build failed.', K.messages[0])
4384 # No more messages reported via smtp
4385 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004386
James E. Blaircc020532017-04-19 13:04:44 -07004387 @simple_layout('layouts/one-job-project.yaml')
4388 def test_one_job_project(self):
4389 "Test that queueing works with one job"
4390 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4391 'master', 'A')
4392 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4393 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004394 A.addApproval('Code-Review', 2)
4395 B.addApproval('Code-Review', 2)
4396 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4397 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004398 self.waitUntilSettled()
4399
4400 self.assertEqual(A.data['status'], 'MERGED')
4401 self.assertEqual(A.reported, 2)
4402 self.assertEqual(B.data['status'], 'MERGED')
4403 self.assertEqual(B.reported, 2)
4404
Paul Belanger71d98172016-11-08 10:56:31 -05004405 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004406 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004407
Paul Belanger174a8272017-03-14 13:20:10 -04004408 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004409 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4410 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4411 self.waitUntilSettled()
4412
Paul Belanger174a8272017-03-14 13:20:10 -04004413 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004414 self.waitUntilSettled()
4415
4416 self.assertEqual(len(self.builds), 2)
4417 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004418 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004419 self.waitUntilSettled()
4420
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004421 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004422 self.assertEqual(len(self.builds), 1,
4423 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004424 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004425 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004426 self.waitUntilSettled()
4427
Paul Belanger174a8272017-03-14 13:20:10 -04004428 self.executor_server.hold_jobs_in_build = False
4429 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004430 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004431 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004432 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4433 self.assertEqual(A.reported, 1)
4434 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004435
James E. Blair15be0e12017-01-03 13:45:20 -08004436 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004437 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004438
4439 self.fake_nodepool.paused = True
4440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004441 A.addApproval('Code-Review', 2)
4442 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004443 self.waitUntilSettled()
4444
4445 self.zk.client.stop()
4446 self.zk.client.start()
4447 self.fake_nodepool.paused = False
4448 self.waitUntilSettled()
4449
4450 self.assertEqual(A.data['status'], 'MERGED')
4451 self.assertEqual(A.reported, 2)
4452
James E. Blair6ab79e02017-01-06 10:10:17 -08004453 def test_nodepool_failure(self):
4454 "Test that jobs are reported after a nodepool failure"
4455
4456 self.fake_nodepool.paused = True
4457 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004458 A.addApproval('Code-Review', 2)
4459 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004460 self.waitUntilSettled()
4461
4462 req = self.fake_nodepool.getNodeRequests()[0]
4463 self.fake_nodepool.addFailRequest(req)
4464
4465 self.fake_nodepool.paused = False
4466 self.waitUntilSettled()
4467
4468 self.assertEqual(A.data['status'], 'NEW')
4469 self.assertEqual(A.reported, 2)
4470 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4471 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4472 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4473
Arieb6f068c2016-10-09 13:11:06 +03004474
James E. Blaira002b032017-04-18 10:35:48 -07004475class TestExecutor(ZuulTestCase):
4476 tenant_config_file = 'config/single-tenant/main.yaml'
4477
4478 def assertFinalState(self):
4479 # In this test, we expect to shut down in a non-final state,
4480 # so skip these checks.
4481 pass
4482
4483 def assertCleanShutdown(self):
4484 self.log.debug("Assert clean shutdown")
4485
4486 # After shutdown, make sure no jobs are running
4487 self.assertEqual({}, self.executor_server.job_workers)
4488
4489 # Make sure that git.Repo objects have been garbage collected.
4490 repos = []
4491 gc.collect()
4492 for obj in gc.get_objects():
4493 if isinstance(obj, git.Repo):
4494 self.log.debug("Leaked git repo object: %s" % repr(obj))
4495 repos.append(obj)
4496 self.assertEqual(len(repos), 0)
4497
4498 def test_executor_shutdown(self):
4499 "Test that the executor can shut down with jobs running"
4500
4501 self.executor_server.hold_jobs_in_build = True
4502 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004503 A.addApproval('Code-Review', 2)
4504 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004505 self.waitUntilSettled()
4506
4507
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004508class TestDependencyGraph(ZuulTestCase):
4509 tenant_config_file = 'config/dependency-graph/main.yaml'
4510
4511 def test_dependeny_graph_dispatch_jobs_once(self):
4512 "Test a job in a dependency graph is queued only once"
4513 # Job dependencies, starting with A
4514 # A
4515 # / \
4516 # B C
4517 # / \ / \
4518 # D F E
4519 # |
4520 # G
4521
4522 self.executor_server.hold_jobs_in_build = True
4523 change = self.fake_gerrit.addFakeChange(
4524 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004525 change.addApproval('Code-Review', 2)
4526 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004527
4528 self.waitUntilSettled()
4529 self.assertEqual([b.name for b in self.builds], ['A'])
4530
4531 self.executor_server.release('A')
4532 self.waitUntilSettled()
4533 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4534
4535 self.executor_server.release('B')
4536 self.waitUntilSettled()
4537 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4538
4539 self.executor_server.release('D')
4540 self.waitUntilSettled()
4541 self.assertEqual([b.name for b in self.builds], ['C'])
4542
4543 self.executor_server.release('C')
4544 self.waitUntilSettled()
4545 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4546
4547 self.executor_server.release('F')
4548 self.waitUntilSettled()
4549 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4550
4551 self.executor_server.release('G')
4552 self.waitUntilSettled()
4553 self.assertEqual([b.name for b in self.builds], ['E'])
4554
4555 self.executor_server.release('E')
4556 self.waitUntilSettled()
4557 self.assertEqual(len(self.builds), 0)
4558
4559 self.executor_server.hold_jobs_in_build = False
4560 self.executor_server.release()
4561 self.waitUntilSettled()
4562
4563 self.assertEqual(len(self.builds), 0)
4564 self.assertEqual(len(self.history), 7)
4565
4566 self.assertEqual(change.data['status'], 'MERGED')
4567 self.assertEqual(change.reported, 2)
4568
4569 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4570 "Test that a job waits till all dependencies are successful"
4571 # Job dependencies, starting with A
4572 # A
4573 # / \
4574 # B C*
4575 # / \ / \
4576 # D F E
4577 # |
4578 # G
4579
4580 self.executor_server.hold_jobs_in_build = True
4581 change = self.fake_gerrit.addFakeChange(
4582 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004583 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004584
4585 self.executor_server.failJob('C', change)
4586
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004587 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004588
4589 self.waitUntilSettled()
4590 self.assertEqual([b.name for b in self.builds], ['A'])
4591
4592 self.executor_server.release('A')
4593 self.waitUntilSettled()
4594 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4595
4596 self.executor_server.release('B')
4597 self.waitUntilSettled()
4598 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4599
4600 self.executor_server.release('D')
4601 self.waitUntilSettled()
4602 self.assertEqual([b.name for b in self.builds], ['C'])
4603
4604 self.executor_server.release('C')
4605 self.waitUntilSettled()
4606 self.assertEqual(len(self.builds), 0)
4607
4608 self.executor_server.hold_jobs_in_build = False
4609 self.executor_server.release()
4610 self.waitUntilSettled()
4611
4612 self.assertEqual(len(self.builds), 0)
4613 self.assertEqual(len(self.history), 4)
4614
4615 self.assertEqual(change.data['status'], 'NEW')
4616 self.assertEqual(change.reported, 2)
4617
4618
Arieb6f068c2016-10-09 13:11:06 +03004619class TestDuplicatePipeline(ZuulTestCase):
4620 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4621
4622 def test_duplicate_pipelines(self):
4623 "Test that a change matching multiple pipelines works"
4624
4625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4626 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4627 self.waitUntilSettled()
4628
4629 self.assertHistory([
4630 dict(name='project-test1', result='SUCCESS', changes='1,1',
4631 pipeline='dup1'),
4632 dict(name='project-test1', result='SUCCESS', changes='1,1',
4633 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004634 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004635
4636 self.assertEqual(len(A.messages), 2)
4637
Paul Belangera46a3742016-11-09 14:23:03 -05004638 if 'dup1' in A.messages[0]:
4639 self.assertIn('dup1', A.messages[0])
4640 self.assertNotIn('dup2', A.messages[0])
4641 self.assertIn('project-test1', A.messages[0])
4642 self.assertIn('dup2', A.messages[1])
4643 self.assertNotIn('dup1', A.messages[1])
4644 self.assertIn('project-test1', A.messages[1])
4645 else:
4646 self.assertIn('dup1', A.messages[1])
4647 self.assertNotIn('dup2', A.messages[1])
4648 self.assertIn('project-test1', A.messages[1])
4649 self.assertIn('dup2', A.messages[0])
4650 self.assertNotIn('dup1', A.messages[0])
4651 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004652
4653
Paul Belanger86085b32016-11-03 12:48:57 -04004654class TestSchedulerTemplatedProject(ZuulTestCase):
4655 tenant_config_file = 'config/templated-project/main.yaml'
4656
Paul Belanger174a8272017-03-14 13:20:10 -04004657 def test_job_from_templates_executed(self):
4658 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004659
4660 A = self.fake_gerrit.addFakeChange(
4661 'org/templated-project', 'master', 'A')
4662 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4663 self.waitUntilSettled()
4664
4665 self.assertEqual(self.getJobFromHistory('project-test1').result,
4666 'SUCCESS')
4667 self.assertEqual(self.getJobFromHistory('project-test2').result,
4668 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004669
4670 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004671 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004672
4673 A = self.fake_gerrit.addFakeChange(
4674 'org/layered-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')
4682 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4683 ).result, 'SUCCESS')
4684 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4685 ).result, 'SUCCESS')
4686 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4687 ).result, 'SUCCESS')
4688 self.assertEqual(self.getJobFromHistory('project-test6').result,
4689 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004690
4691
4692class TestSchedulerSuccessURL(ZuulTestCase):
4693 tenant_config_file = 'config/success-url/main.yaml'
4694
4695 def test_success_url(self):
4696 "Ensure bad build params are ignored"
4697 self.sched.reconfigure(self.config)
4698 self.init_repo('org/docs')
4699
4700 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4701 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4702 self.waitUntilSettled()
4703
4704 # Both builds ran: docs-draft-test + docs-draft-test2
4705 self.assertEqual(len(self.history), 2)
4706
4707 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004708 for build in self.history:
4709 if build.name == 'docs-draft-test':
4710 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004711 elif build.name == 'docs-draft-test2':
4712 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004713
4714 # Two msgs: 'Starting...' + results
4715 self.assertEqual(len(self.smtp_messages), 2)
4716 body = self.smtp_messages[1]['body'].splitlines()
4717 self.assertEqual('Build succeeded.', body[0])
4718
4719 self.assertIn(
4720 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4721 'docs-draft-test/%s/publish-docs/' % uuid,
4722 body[2])
4723
Paul Belanger174a8272017-03-14 13:20:10 -04004724 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004725 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004726 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4727 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004728 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004729 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004730
4731
Adam Gandelman4da00f62016-12-09 15:47:33 -08004732class TestSchedulerMerges(ZuulTestCase):
4733 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004734
4735 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004736 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004737 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004738 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004739 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4740 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4741 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004742 A.addApproval('Code-Review', 2)
4743 B.addApproval('Code-Review', 2)
4744 C.addApproval('Code-Review', 2)
4745 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4746 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4747 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004748 self.waitUntilSettled()
4749
4750 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004751 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4752 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004753 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004754 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004755 repo_messages.reverse()
4756
Paul Belanger174a8272017-03-14 13:20:10 -04004757 self.executor_server.hold_jobs_in_build = False
4758 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004759 self.waitUntilSettled()
4760
4761 return repo_messages
4762
4763 def _test_merge(self, mode):
4764 us_path = os.path.join(
4765 self.upstream_root, 'org/project-%s' % mode)
4766 expected_messages = [
4767 'initial commit',
4768 'add content from fixture',
4769 # the intermediate commits order is nondeterministic
4770 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4771 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4772 ]
4773 result = self._test_project_merge_mode(mode)
4774 self.assertEqual(result[:2], expected_messages[:2])
4775 self.assertEqual(result[-2:], expected_messages[-2:])
4776
4777 def test_project_merge_mode_merge(self):
4778 self._test_merge('merge')
4779
4780 def test_project_merge_mode_merge_resolve(self):
4781 self._test_merge('merge-resolve')
4782
4783 def test_project_merge_mode_cherrypick(self):
4784 expected_messages = [
4785 'initial commit',
4786 'add content from fixture',
4787 'A-1',
4788 'B-1',
4789 'C-1']
4790 result = self._test_project_merge_mode('cherry-pick')
4791 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004792
4793 def test_merge_branch(self):
4794 "Test that the right commits are on alternate branches"
4795 self.create_branch('org/project-merge-branches', 'mp')
4796
Paul Belanger174a8272017-03-14 13:20:10 -04004797 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004798 A = self.fake_gerrit.addFakeChange(
4799 'org/project-merge-branches', 'mp', 'A')
4800 B = self.fake_gerrit.addFakeChange(
4801 'org/project-merge-branches', 'mp', 'B')
4802 C = self.fake_gerrit.addFakeChange(
4803 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004804 A.addApproval('Code-Review', 2)
4805 B.addApproval('Code-Review', 2)
4806 C.addApproval('Code-Review', 2)
4807 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4808 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4809 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004810 self.waitUntilSettled()
4811
Paul Belanger174a8272017-03-14 13:20:10 -04004812 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004813 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004814 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004815 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004816 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004817 self.waitUntilSettled()
4818
4819 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004820 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004821 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4822 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004823 repo = git.Repo(path)
4824
James E. Blair247cab72017-07-20 16:52:36 -07004825 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004826 repo_messages.reverse()
4827 correct_messages = [
4828 'initial commit',
4829 'add content from fixture',
4830 'mp commit',
4831 'A-1', 'B-1', 'C-1']
4832 self.assertEqual(repo_messages, correct_messages)
4833
Paul Belanger174a8272017-03-14 13:20:10 -04004834 self.executor_server.hold_jobs_in_build = False
4835 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004836 self.waitUntilSettled()
4837
4838 def test_merge_multi_branch(self):
4839 "Test that dependent changes on multiple branches are merged"
4840 self.create_branch('org/project-merge-branches', 'mp')
4841
Paul Belanger174a8272017-03-14 13:20:10 -04004842 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004843 A = self.fake_gerrit.addFakeChange(
4844 'org/project-merge-branches', 'master', 'A')
4845 B = self.fake_gerrit.addFakeChange(
4846 'org/project-merge-branches', 'mp', 'B')
4847 C = self.fake_gerrit.addFakeChange(
4848 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004849 A.addApproval('Code-Review', 2)
4850 B.addApproval('Code-Review', 2)
4851 C.addApproval('Code-Review', 2)
4852 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4853 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4854 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004855 self.waitUntilSettled()
4856
4857 job_A = None
4858 for job in self.builds:
4859 if 'project-merge' in job.name:
4860 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004861
James E. Blair2a535672017-04-27 12:03:15 -07004862 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4863 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004864 repo = git.Repo(path)
4865 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004866 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004867 repo_messages.reverse()
4868 correct_messages = [
4869 'initial commit', 'add content from fixture', 'A-1']
4870 self.assertEqual(repo_messages, correct_messages)
4871
Paul Belanger174a8272017-03-14 13:20:10 -04004872 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004873 self.waitUntilSettled()
4874
4875 job_B = None
4876 for job in self.builds:
4877 if 'project-merge' in job.name:
4878 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004879
James E. Blair2a535672017-04-27 12:03:15 -07004880 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4881 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004882 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004883 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004884 repo_messages.reverse()
4885 correct_messages = [
4886 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4887 self.assertEqual(repo_messages, correct_messages)
4888
Paul Belanger174a8272017-03-14 13:20:10 -04004889 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004890 self.waitUntilSettled()
4891
4892 job_C = None
4893 for job in self.builds:
4894 if 'project-merge' in job.name:
4895 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07004896
James E. Blair2a535672017-04-27 12:03:15 -07004897 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4898 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004899 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004900 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004901
4902 repo_messages.reverse()
4903 correct_messages = [
4904 'initial commit', 'add content from fixture',
4905 'A-1', 'C-1']
4906 # Ensure the right commits are in the history for this ref
4907 self.assertEqual(repo_messages, correct_messages)
4908
Paul Belanger174a8272017-03-14 13:20:10 -04004909 self.executor_server.hold_jobs_in_build = False
4910 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004911 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004912
4913
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004914class TestSemaphore(ZuulTestCase):
4915 tenant_config_file = 'config/semaphore/main.yaml'
4916
4917 def test_semaphore_one(self):
4918 "Test semaphores with max=1 (mutex)"
4919 tenant = self.sched.abide.tenants.get('tenant-one')
4920
4921 self.executor_server.hold_jobs_in_build = True
4922
4923 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4924 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4925 self.assertFalse('test-semaphore' in
4926 tenant.semaphore_handler.semaphores)
4927
4928 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4929 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4930 self.waitUntilSettled()
4931
4932 self.assertEqual(len(self.builds), 3)
4933 self.assertEqual(self.builds[0].name, 'project-test1')
4934 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4935 self.assertEqual(self.builds[2].name, 'project-test1')
4936
4937 self.executor_server.release('semaphore-one-test1')
4938 self.waitUntilSettled()
4939
4940 self.assertEqual(len(self.builds), 3)
4941 self.assertEqual(self.builds[0].name, 'project-test1')
4942 self.assertEqual(self.builds[1].name, 'project-test1')
4943 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4944 self.assertTrue('test-semaphore' in
4945 tenant.semaphore_handler.semaphores)
4946
4947 self.executor_server.release('semaphore-one-test2')
4948 self.waitUntilSettled()
4949
4950 self.assertEqual(len(self.builds), 3)
4951 self.assertEqual(self.builds[0].name, 'project-test1')
4952 self.assertEqual(self.builds[1].name, 'project-test1')
4953 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4954 self.assertTrue('test-semaphore' in
4955 tenant.semaphore_handler.semaphores)
4956
4957 self.executor_server.release('semaphore-one-test1')
4958 self.waitUntilSettled()
4959
4960 self.assertEqual(len(self.builds), 3)
4961 self.assertEqual(self.builds[0].name, 'project-test1')
4962 self.assertEqual(self.builds[1].name, 'project-test1')
4963 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4964 self.assertTrue('test-semaphore' in
4965 tenant.semaphore_handler.semaphores)
4966
4967 self.executor_server.release('semaphore-one-test2')
4968 self.waitUntilSettled()
4969
4970 self.assertEqual(len(self.builds), 2)
4971 self.assertEqual(self.builds[0].name, 'project-test1')
4972 self.assertEqual(self.builds[1].name, 'project-test1')
4973 self.assertFalse('test-semaphore' in
4974 tenant.semaphore_handler.semaphores)
4975
4976 self.executor_server.hold_jobs_in_build = False
4977 self.executor_server.release()
4978
4979 self.waitUntilSettled()
4980 self.assertEqual(len(self.builds), 0)
4981
4982 self.assertEqual(A.reported, 1)
4983 self.assertEqual(B.reported, 1)
4984 self.assertFalse('test-semaphore' in
4985 tenant.semaphore_handler.semaphores)
4986
4987 def test_semaphore_two(self):
4988 "Test semaphores with max>1"
4989 tenant = self.sched.abide.tenants.get('tenant-one')
4990
4991 self.executor_server.hold_jobs_in_build = True
4992 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4993 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4994 self.assertFalse('test-semaphore-two' in
4995 tenant.semaphore_handler.semaphores)
4996
4997 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4998 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4999 self.waitUntilSettled()
5000
5001 self.assertEqual(len(self.builds), 4)
5002 self.assertEqual(self.builds[0].name, 'project-test1')
5003 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5004 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5005 self.assertEqual(self.builds[3].name, 'project-test1')
5006 self.assertTrue('test-semaphore-two' in
5007 tenant.semaphore_handler.semaphores)
5008 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5009 'test-semaphore-two', [])), 2)
5010
5011 self.executor_server.release('semaphore-two-test1')
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-test2')
5017 self.assertEqual(self.builds[2].name, 'project-test1')
5018 self.assertEqual(self.builds[3].name, 'semaphore-two-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-test2')
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, 'project-test1')
5030 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5031 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5032 self.assertTrue('test-semaphore-two' in
5033 tenant.semaphore_handler.semaphores)
5034 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5035 'test-semaphore-two', [])), 2)
5036
5037 self.executor_server.release('semaphore-two-test1')
5038 self.waitUntilSettled()
5039
5040 self.assertEqual(len(self.builds), 3)
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-test2')
5044 self.assertTrue('test-semaphore-two' in
5045 tenant.semaphore_handler.semaphores)
5046 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5047 'test-semaphore-two', [])), 1)
5048
5049 self.executor_server.release('semaphore-two-test2')
5050 self.waitUntilSettled()
5051
5052 self.assertEqual(len(self.builds), 2)
5053 self.assertEqual(self.builds[0].name, 'project-test1')
5054 self.assertEqual(self.builds[1].name, 'project-test1')
5055 self.assertFalse('test-semaphore-two' in
5056 tenant.semaphore_handler.semaphores)
5057
5058 self.executor_server.hold_jobs_in_build = False
5059 self.executor_server.release()
5060
5061 self.waitUntilSettled()
5062 self.assertEqual(len(self.builds), 0)
5063
5064 self.assertEqual(A.reported, 1)
5065 self.assertEqual(B.reported, 1)
5066
5067 def test_semaphore_abandon(self):
5068 "Test abandon with job semaphores"
5069 self.executor_server.hold_jobs_in_build = True
5070 tenant = self.sched.abide.tenants.get('tenant-one')
5071 check_pipeline = tenant.layout.pipelines['check']
5072
5073 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5074 self.assertFalse('test-semaphore' in
5075 tenant.semaphore_handler.semaphores)
5076
5077 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5078 self.waitUntilSettled()
5079
5080 self.assertTrue('test-semaphore' in
5081 tenant.semaphore_handler.semaphores)
5082
5083 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5084 self.waitUntilSettled()
5085
5086 # The check pipeline should be empty
5087 items = check_pipeline.getAllItems()
5088 self.assertEqual(len(items), 0)
5089
5090 # The semaphore should be released
5091 self.assertFalse('test-semaphore' in
5092 tenant.semaphore_handler.semaphores)
5093
5094 self.executor_server.hold_jobs_in_build = False
5095 self.executor_server.release()
5096 self.waitUntilSettled()
5097
Tobias Henkel0f714002017-06-30 23:30:52 +02005098 def test_semaphore_new_patchset(self):
5099 "Test new patchset with job semaphores"
5100 self.executor_server.hold_jobs_in_build = True
5101 tenant = self.sched.abide.tenants.get('tenant-one')
5102 check_pipeline = tenant.layout.pipelines['check']
5103
5104 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5105 self.assertFalse('test-semaphore' in
5106 tenant.semaphore_handler.semaphores)
5107
5108 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5109 self.waitUntilSettled()
5110
5111 self.assertTrue('test-semaphore' in
5112 tenant.semaphore_handler.semaphores)
5113 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5114 self.assertEqual(len(semaphore), 1)
5115
5116 A.addPatchset()
5117 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5118 self.waitUntilSettled()
5119
5120 self.assertTrue('test-semaphore' in
5121 tenant.semaphore_handler.semaphores)
5122 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5123 self.assertEqual(len(semaphore), 1)
5124
5125 items = check_pipeline.getAllItems()
5126 self.assertEqual(items[0].change.number, '1')
5127 self.assertEqual(items[0].change.patchset, '2')
5128 self.assertTrue(items[0].live)
5129
5130 self.executor_server.hold_jobs_in_build = False
5131 self.executor_server.release()
5132 self.waitUntilSettled()
5133
5134 # The semaphore should be released
5135 self.assertFalse('test-semaphore' in
5136 tenant.semaphore_handler.semaphores)
5137
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005138 def test_semaphore_reconfigure(self):
5139 "Test reconfigure with job semaphores"
5140 self.executor_server.hold_jobs_in_build = True
5141 tenant = self.sched.abide.tenants.get('tenant-one')
5142 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5143 self.assertFalse('test-semaphore' in
5144 tenant.semaphore_handler.semaphores)
5145
5146 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5147 self.waitUntilSettled()
5148
5149 self.assertTrue('test-semaphore' in
5150 tenant.semaphore_handler.semaphores)
5151
5152 # reconfigure without layout change
5153 self.sched.reconfigure(self.config)
5154 self.waitUntilSettled()
5155 tenant = self.sched.abide.tenants.get('tenant-one')
5156
5157 # semaphore still must be held
5158 self.assertTrue('test-semaphore' in
5159 tenant.semaphore_handler.semaphores)
5160
5161 self.commitConfigUpdate(
5162 'common-config',
5163 'config/semaphore/zuul-reconfiguration.yaml')
5164 self.sched.reconfigure(self.config)
5165 self.waitUntilSettled()
5166 tenant = self.sched.abide.tenants.get('tenant-one')
5167
5168 self.executor_server.release('project-test1')
5169 self.waitUntilSettled()
5170
5171 # There should be no builds anymore
5172 self.assertEqual(len(self.builds), 0)
5173
5174 # The semaphore should be released
5175 self.assertFalse('test-semaphore' in
5176 tenant.semaphore_handler.semaphores)
5177
5178
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005179class TestSemaphoreMultiTenant(ZuulTestCase):
5180 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5181
5182 def test_semaphore_tenant_isolation(self):
5183 "Test semaphores in multiple tenants"
5184
5185 self.waitUntilSettled()
5186 tenant_one = self.sched.abide.tenants.get('tenant-one')
5187 tenant_two = self.sched.abide.tenants.get('tenant-two')
5188
5189 self.executor_server.hold_jobs_in_build = True
5190 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5191 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5192 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5193 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5194 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5195 self.assertFalse('test-semaphore' in
5196 tenant_one.semaphore_handler.semaphores)
5197 self.assertFalse('test-semaphore' in
5198 tenant_two.semaphore_handler.semaphores)
5199
5200 # add patches to project1 of tenant-one
5201 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5202 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5203 self.waitUntilSettled()
5204
5205 # one build of project1-test1 must run
5206 # semaphore of tenant-one must be acquired once
5207 # semaphore of tenant-two must not be acquired
5208 self.assertEqual(len(self.builds), 1)
5209 self.assertEqual(self.builds[0].name, 'project1-test1')
5210 self.assertTrue('test-semaphore' in
5211 tenant_one.semaphore_handler.semaphores)
5212 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5213 'test-semaphore', [])), 1)
5214 self.assertFalse('test-semaphore' in
5215 tenant_two.semaphore_handler.semaphores)
5216
5217 # add patches to project2 of tenant-two
5218 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5219 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5220 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5221 self.waitUntilSettled()
5222
5223 # one build of project1-test1 must run
5224 # two builds of project2-test1 must run
5225 # semaphore of tenant-one must be acquired once
5226 # semaphore of tenant-two must be acquired twice
5227 self.assertEqual(len(self.builds), 3)
5228 self.assertEqual(self.builds[0].name, 'project1-test1')
5229 self.assertEqual(self.builds[1].name, 'project2-test1')
5230 self.assertEqual(self.builds[2].name, 'project2-test1')
5231 self.assertTrue('test-semaphore' in
5232 tenant_one.semaphore_handler.semaphores)
5233 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5234 'test-semaphore', [])), 1)
5235 self.assertTrue('test-semaphore' in
5236 tenant_two.semaphore_handler.semaphores)
5237 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5238 'test-semaphore', [])), 2)
5239
5240 self.executor_server.release('project1-test1')
5241 self.waitUntilSettled()
5242
5243 # one build of project1-test1 must run
5244 # two builds of project2-test1 must run
5245 # semaphore of tenant-one must be acquired once
5246 # semaphore of tenant-two must be acquired twice
5247 self.assertEqual(len(self.builds), 3)
5248 self.assertEqual(self.builds[0].name, 'project2-test1')
5249 self.assertEqual(self.builds[1].name, 'project2-test1')
5250 self.assertEqual(self.builds[2].name, 'project1-test1')
5251 self.assertTrue('test-semaphore' in
5252 tenant_one.semaphore_handler.semaphores)
5253 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5254 'test-semaphore', [])), 1)
5255 self.assertTrue('test-semaphore' in
5256 tenant_two.semaphore_handler.semaphores)
5257 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5258 'test-semaphore', [])), 2)
5259
5260 self.executor_server.release('project2-test1')
5261 self.waitUntilSettled()
5262
5263 # one build of project1-test1 must run
5264 # one build of project2-test1 must run
5265 # semaphore of tenant-one must be acquired once
5266 # semaphore of tenant-two must be acquired once
5267 self.assertEqual(len(self.builds), 2)
5268 self.assertTrue('test-semaphore' in
5269 tenant_one.semaphore_handler.semaphores)
5270 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5271 'test-semaphore', [])), 1)
5272 self.assertTrue('test-semaphore' in
5273 tenant_two.semaphore_handler.semaphores)
5274 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5275 'test-semaphore', [])), 1)
5276
5277 self.executor_server.hold_jobs_in_build = False
5278 self.executor_server.release()
5279
5280 self.waitUntilSettled()
5281
5282 # no build must run
5283 # semaphore of tenant-one must not be acquired
5284 # semaphore of tenant-two must not be acquired
5285 self.assertEqual(len(self.builds), 0)
5286 self.assertFalse('test-semaphore' in
5287 tenant_one.semaphore_handler.semaphores)
5288 self.assertFalse('test-semaphore' in
5289 tenant_two.semaphore_handler.semaphores)
5290
5291 self.assertEqual(A.reported, 1)
5292 self.assertEqual(B.reported, 1)
5293
5294
5295class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005296 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005297 tenant_config_file = 'config/in-repo/main.yaml'
5298
5299 def test_semaphore_in_repo(self):
5300 "Test semaphores in repo config"
5301
5302 # This tests dynamic semaphore handling in project repos. The semaphore
5303 # max value should not be evaluated dynamically but must be updated
5304 # after the change lands.
5305
5306 self.waitUntilSettled()
5307 tenant = self.sched.abide.tenants.get('tenant-one')
5308
5309 in_repo_conf = textwrap.dedent(
5310 """
5311 - job:
5312 name: project-test2
5313 semaphore: test-semaphore
5314
5315 - project:
5316 name: org/project
5317 tenant-one-gate:
5318 jobs:
5319 - project-test2
5320
5321 # the max value in dynamic layout must be ignored
5322 - semaphore:
5323 name: test-semaphore
5324 max: 2
5325 """)
5326
5327 in_repo_playbook = textwrap.dedent(
5328 """
5329 - hosts: all
5330 tasks: []
5331 """)
5332
5333 file_dict = {'.zuul.yaml': in_repo_conf,
5334 'playbooks/project-test2.yaml': in_repo_playbook}
5335 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5336 files=file_dict)
5337 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5338 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5339 B.setDependsOn(A, 1)
5340 C.setDependsOn(A, 1)
5341
5342 self.executor_server.hold_jobs_in_build = True
5343
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005344 A.addApproval('Code-Review', 2)
5345 B.addApproval('Code-Review', 2)
5346 C.addApproval('Code-Review', 2)
5347 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5348 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5349 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005350 self.waitUntilSettled()
5351
5352 # check that the layout in a queue item still has max value of 1
5353 # for test-semaphore
5354 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5355 queue = None
5356 for queue_candidate in pipeline.queues:
5357 if queue_candidate.name == 'org/project':
5358 queue = queue_candidate
5359 break
5360 queue_item = queue.queue[0]
5361 item_dynamic_layout = queue_item.current_build_set.layout
5362 dynamic_test_semaphore = \
5363 item_dynamic_layout.semaphores.get('test-semaphore')
5364 self.assertEqual(dynamic_test_semaphore.max, 1)
5365
5366 # one build must be in queue, one semaphores acquired
5367 self.assertEqual(len(self.builds), 1)
5368 self.assertEqual(self.builds[0].name, 'project-test2')
5369 self.assertTrue('test-semaphore' in
5370 tenant.semaphore_handler.semaphores)
5371 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5372 'test-semaphore', [])), 1)
5373
5374 self.executor_server.release('project-test2')
5375 self.waitUntilSettled()
5376
5377 # change A must be merged
5378 self.assertEqual(A.data['status'], 'MERGED')
5379 self.assertEqual(A.reported, 2)
5380
5381 # send change-merged event as the gerrit mock doesn't send it
5382 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5383 self.waitUntilSettled()
5384
5385 # now that change A was merged, the new semaphore max must be effective
5386 tenant = self.sched.abide.tenants.get('tenant-one')
5387 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5388
5389 # two builds must be in queue, two semaphores acquired
5390 self.assertEqual(len(self.builds), 2)
5391 self.assertEqual(self.builds[0].name, 'project-test2')
5392 self.assertEqual(self.builds[1].name, 'project-test2')
5393 self.assertTrue('test-semaphore' in
5394 tenant.semaphore_handler.semaphores)
5395 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5396 'test-semaphore', [])), 2)
5397
5398 self.executor_server.release('project-test2')
5399 self.waitUntilSettled()
5400
5401 self.assertEqual(len(self.builds), 0)
5402 self.assertFalse('test-semaphore' in
5403 tenant.semaphore_handler.semaphores)
5404
5405 self.executor_server.hold_jobs_in_build = False
5406 self.executor_server.release()
5407
5408 self.waitUntilSettled()
5409 self.assertEqual(len(self.builds), 0)
5410
5411 self.assertEqual(A.reported, 2)
5412 self.assertEqual(B.reported, 2)
5413 self.assertEqual(C.reported, 2)