blob: f33d9640627c3d9ec0e4248a7147c1d1738334ed [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')
James E. Blairfcbf95d2017-07-25 16:38:09 -070065 self.assertEqual(self.getJobFromHistory('project-test2').node,
66 'label1')
Paul Belanger0a21f0a2017-06-13 13:14:42 -040067
68
Clark Boylanb640e052014-04-03 16:41:46 -070069class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070070 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010071
Paul Belanger174a8272017-03-14 13:20:10 -040072 def test_jobs_executed(self):
73 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070074
James E. Blairb0fcae42012-07-17 11:12:10 -070075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020076 A.addApproval('Code-Review', 2)
77 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070078 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040079 self.assertEqual(self.getJobFromHistory('project-merge').result,
80 'SUCCESS')
81 self.assertEqual(self.getJobFromHistory('project-test1').result,
82 'SUCCESS')
83 self.assertEqual(self.getJobFromHistory('project-test2').result,
84 'SUCCESS')
85 self.assertEqual(A.data['status'], 'MERGED')
86 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070087 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070088 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070089 self.assertEqual(self.getJobFromHistory('project-test2').node,
90 'label1')
James E. Blairb0fcae42012-07-17 11:12:10 -070091
James E. Blair552b54f2016-07-22 13:55:32 -070092 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050093 # Per-driver
94 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
95 # Per-driver per-connection
96 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
97 value='1|c')
James E. Blair66eeebf2013-07-27 17:44:32 -070098 self.assertReportedStat('zuul.pipeline.gate.current_changes',
99 value='1|g')
100 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
101 kind='ms')
102 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
103 value='1|c')
104 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
105 self.assertReportedStat('zuul.pipeline.gate.total_changes',
106 value='1|c')
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.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -0700109 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700110 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700111
James E. Blair2ef29e92017-07-21 15:25:05 -0700112 for build in self.history:
113 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700114
James E. Blair3cb10702013-08-24 08:56:03 -0700115 def test_initial_pipeline_gauges(self):
116 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700117 self.assertReportedStat('zuul.pipeline.gate.current_changes',
118 value='0|g')
119 self.assertReportedStat('zuul.pipeline.check.current_changes',
120 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700121
James E. Blair34776ee2016-08-25 13:53:54 -0700122 def test_job_branch(self):
123 "Test the correct variant of a job runs on a branch"
124 self.create_branch('org/project', 'stable')
125 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200126 A.addApproval('Code-Review', 2)
127 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700128 self.waitUntilSettled()
129 self.assertEqual(self.getJobFromHistory('project-test1').result,
130 'SUCCESS')
131 self.assertEqual(self.getJobFromHistory('project-test2').result,
132 'SUCCESS')
133 self.assertEqual(A.data['status'], 'MERGED')
134 self.assertEqual(A.reported, 2,
135 "A should report start and success")
136 self.assertIn('gate', A.messages[1],
137 "A should transit gate")
138 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700139 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700140
James E. Blairb0fcae42012-07-17 11:12:10 -0700141 def test_parallel_changes(self):
142 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700143
Paul Belanger174a8272017-03-14 13:20:10 -0400144 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700145 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
146 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
147 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200148 A.addApproval('Code-Review', 2)
149 B.addApproval('Code-Review', 2)
150 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700151
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200152 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
153 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
154 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700155
156 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400157 self.assertEqual(len(self.builds), 1)
158 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700159 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700160
Paul Belanger174a8272017-03-14 13:20:10 -0400161 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700162 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400163 self.assertEqual(len(self.builds), 3)
164 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400168 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700169 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700170
Paul Belanger174a8272017-03-14 13:20:10 -0400171 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400173 self.assertEqual(len(self.builds), 5)
174 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700175 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700177 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700180 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400181 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700182 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700183
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400184 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700185 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700186
Paul Belanger174a8272017-03-14 13:20:10 -0400187 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700188 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400189 self.assertEqual(len(self.builds), 6)
190 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700191 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400192 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700193 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700194
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400197 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700198 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700199
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700201 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400202 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700203 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700204
Paul Belanger174a8272017-03-14 13:20:10 -0400205 self.executor_server.hold_jobs_in_build = False
206 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700207 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700209
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400210 self.assertEqual(len(self.history), 9)
211 self.assertEqual(A.data['status'], 'MERGED')
212 self.assertEqual(B.data['status'], 'MERGED')
213 self.assertEqual(C.data['status'], 'MERGED')
214 self.assertEqual(A.reported, 2)
215 self.assertEqual(B.reported, 2)
216 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700217
218 def test_failed_changes(self):
219 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400220 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700221
James E. Blairb02a3bb2012-07-30 17:49:55 -0700222 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
223 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200224 A.addApproval('Code-Review', 2)
225 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700226
Paul Belanger174a8272017-03-14 13:20:10 -0400227 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700228
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200229 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
230 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700231 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700232 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400233
Paul Belanger174a8272017-03-14 13:20:10 -0400234 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400235 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700236 # A/project-merge is complete
237 self.assertBuilds([
238 dict(name='project-test1', changes='1,1'),
239 dict(name='project-test2', changes='1,1'),
240 dict(name='project-merge', changes='1,1 2,1'),
241 ])
James E. Blaire2819012013-06-28 17:17:26 -0400242
Paul Belanger174a8272017-03-14 13:20:10 -0400243 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400244 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700245 # A/project-merge is complete
246 # B/project-merge is complete
247 self.assertBuilds([
248 dict(name='project-test1', changes='1,1'),
249 dict(name='project-test2', changes='1,1'),
250 dict(name='project-test1', changes='1,1 2,1'),
251 dict(name='project-test2', changes='1,1 2,1'),
252 ])
253
254 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400255 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700256 self.builds[0].release()
257 self.waitUntilSettled()
258
259 self.orderedRelease()
260 self.assertHistory([
261 dict(name='project-merge', result='SUCCESS', changes='1,1'),
262 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
263 dict(name='project-test1', result='FAILURE', changes='1,1'),
264 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
265 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
266 dict(name='project-test2', result='SUCCESS', changes='1,1'),
267 dict(name='project-merge', result='SUCCESS', changes='2,1'),
268 dict(name='project-test1', result='SUCCESS', changes='2,1'),
269 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700270 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700271
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400272 self.assertEqual(A.data['status'], 'NEW')
273 self.assertEqual(B.data['status'], 'MERGED')
274 self.assertEqual(A.reported, 2)
275 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276
277 def test_independent_queues(self):
278 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700279
Paul Belanger174a8272017-03-14 13:20:10 -0400280 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900281 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700282 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
283 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200284 A.addApproval('Code-Review', 2)
285 B.addApproval('Code-Review', 2)
286 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700287
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200288 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800289 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700290
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200291 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800292 self.waitUntilSettled()
293
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200294 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700295 self.waitUntilSettled()
296
297 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700298 self.assertBuilds([
299 dict(name='project-merge', changes='1,1'),
300 dict(name='project-merge', changes='2,1'),
301 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700302
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700303 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700304 self.builds[0].release()
305 self.waitUntilSettled()
306 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700307 self.waitUntilSettled()
308 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400309 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700310 self.waitUntilSettled()
311
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700312 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700313 self.assertBuilds([
314 dict(name='project-test1', changes='1,1'),
315 dict(name='project-test2', changes='1,1'),
316 dict(name='project-test1', changes='2,1'),
317 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500318 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700319 dict(name='project-test1', changes='2,1 3,1'),
320 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800321 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700322 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700323
James E. Blair5f0d49e2016-08-19 09:36:50 -0700324 self.orderedRelease()
325 self.assertHistory([
326 dict(name='project-merge', result='SUCCESS', changes='1,1'),
327 dict(name='project-merge', result='SUCCESS', changes='2,1'),
328 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
329 dict(name='project-test1', result='SUCCESS', changes='1,1'),
330 dict(name='project-test2', result='SUCCESS', changes='1,1'),
331 dict(name='project-test1', result='SUCCESS', changes='2,1'),
332 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500333 dict(
334 name='project1-project2-integration',
335 result='SUCCESS',
336 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700337 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
338 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800339 dict(name='project1-project2-integration',
340 result='SUCCESS',
341 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700342 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700343
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400344 self.assertEqual(A.data['status'], 'MERGED')
345 self.assertEqual(B.data['status'], 'MERGED')
346 self.assertEqual(C.data['status'], 'MERGED')
347 self.assertEqual(A.reported, 2)
348 self.assertEqual(B.reported, 2)
349 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700350
351 def test_failed_change_at_head(self):
352 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700353
Paul Belanger174a8272017-03-14 13:20:10 -0400354 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700355 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
356 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
357 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200358 A.addApproval('Code-Review', 2)
359 B.addApproval('Code-Review', 2)
360 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700361
Paul Belanger174a8272017-03-14 13:20:10 -0400362 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700363
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200364 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
365 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
366 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700367
368 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700369
James E. Blairb536ecc2016-08-31 10:11:42 -0700370 self.assertBuilds([
371 dict(name='project-merge', changes='1,1'),
372 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700373
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()
Paul Belanger174a8272017-03-14 13:20:10 -0400378 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700379 self.waitUntilSettled()
380
James E. Blairb536ecc2016-08-31 10:11:42 -0700381 self.assertBuilds([
382 dict(name='project-test1', changes='1,1'),
383 dict(name='project-test2', changes='1,1'),
384 dict(name='project-test1', changes='1,1 2,1'),
385 dict(name='project-test2', changes='1,1 2,1'),
386 dict(name='project-test1', changes='1,1 2,1 3,1'),
387 dict(name='project-test2', changes='1,1 2,1 3,1'),
388 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700389
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400390 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700391 self.waitUntilSettled()
392
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400393 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700394 self.assertBuilds([
395 dict(name='project-test2', changes='1,1'),
396 dict(name='project-merge', changes='2,1'),
397 ])
398 # Unordered history comparison because the aborts can finish
399 # in any order.
400 self.assertHistory([
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1'),
403 dict(name='project-merge', result='SUCCESS',
404 changes='1,1 2,1'),
405 dict(name='project-merge', result='SUCCESS',
406 changes='1,1 2,1 3,1'),
407 dict(name='project-test1', result='FAILURE',
408 changes='1,1'),
409 dict(name='project-test1', result='ABORTED',
410 changes='1,1 2,1'),
411 dict(name='project-test2', result='ABORTED',
412 changes='1,1 2,1'),
413 dict(name='project-test1', result='ABORTED',
414 changes='1,1 2,1 3,1'),
415 dict(name='project-test2', result='ABORTED',
416 changes='1,1 2,1 3,1'),
417 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700418
Paul Belanger174a8272017-03-14 13:20:10 -0400419 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700420 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400421 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700422 self.waitUntilSettled()
423 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700424
James E. Blairb536ecc2016-08-31 10:11:42 -0700425 self.assertBuilds([])
426 self.assertHistory([
427 dict(name='project-merge', result='SUCCESS',
428 changes='1,1'),
429 dict(name='project-merge', result='SUCCESS',
430 changes='1,1 2,1'),
431 dict(name='project-merge', result='SUCCESS',
432 changes='1,1 2,1 3,1'),
433 dict(name='project-test1', result='FAILURE',
434 changes='1,1'),
435 dict(name='project-test1', result='ABORTED',
436 changes='1,1 2,1'),
437 dict(name='project-test2', result='ABORTED',
438 changes='1,1 2,1'),
439 dict(name='project-test1', result='ABORTED',
440 changes='1,1 2,1 3,1'),
441 dict(name='project-test2', result='ABORTED',
442 changes='1,1 2,1 3,1'),
443 dict(name='project-merge', result='SUCCESS',
444 changes='2,1'),
445 dict(name='project-merge', result='SUCCESS',
446 changes='2,1 3,1'),
447 dict(name='project-test2', result='SUCCESS',
448 changes='1,1'),
449 dict(name='project-test1', result='SUCCESS',
450 changes='2,1'),
451 dict(name='project-test2', result='SUCCESS',
452 changes='2,1'),
453 dict(name='project-test1', result='SUCCESS',
454 changes='2,1 3,1'),
455 dict(name='project-test2', result='SUCCESS',
456 changes='2,1 3,1'),
457 ], ordered=False)
458
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400459 self.assertEqual(A.data['status'], 'NEW')
460 self.assertEqual(B.data['status'], 'MERGED')
461 self.assertEqual(C.data['status'], 'MERGED')
462 self.assertEqual(A.reported, 2)
463 self.assertEqual(B.reported, 2)
464 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700465
James E. Blair0aac4872013-08-23 14:02:38 -0700466 def test_failed_change_in_middle(self):
467 "Test a failed change in the middle of the queue"
468
Paul Belanger174a8272017-03-14 13:20:10 -0400469 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
471 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
472 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200473 A.addApproval('Code-Review', 2)
474 B.addApproval('Code-Review', 2)
475 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700476
Paul Belanger174a8272017-03-14 13:20:10 -0400477 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700478
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200479 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
480 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
481 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700482
483 self.waitUntilSettled()
484
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()
Paul Belanger174a8272017-03-14 13:20:10 -0400489 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700490 self.waitUntilSettled()
491
492 self.assertEqual(len(self.builds), 6)
493 self.assertEqual(self.builds[0].name, 'project-test1')
494 self.assertEqual(self.builds[1].name, 'project-test2')
495 self.assertEqual(self.builds[2].name, 'project-test1')
496 self.assertEqual(self.builds[3].name, 'project-test2')
497 self.assertEqual(self.builds[4].name, 'project-test1')
498 self.assertEqual(self.builds[5].name, 'project-test2')
499
500 self.release(self.builds[2])
501 self.waitUntilSettled()
502
James E. Blair972e3c72013-08-29 12:04:55 -0700503 # project-test1 and project-test2 for A
504 # project-test2 for B
505 # project-merge for C (without B)
506 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700507 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
508
Paul Belanger174a8272017-03-14 13:20:10 -0400509 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700510 self.waitUntilSettled()
511
512 # project-test1 and project-test2 for A
513 # project-test2 for B
514 # project-test1 and project-test2 for C
515 self.assertEqual(len(self.builds), 5)
516
Paul Belanger2e2a0492016-10-30 18:33:37 -0400517 tenant = self.sched.abide.tenants.get('tenant-one')
518 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700519 builds = items[0].current_build_set.getBuilds()
520 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
521 self.assertEqual(self.countJobResults(builds, None), 2)
522 builds = items[1].current_build_set.getBuilds()
523 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
524 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
525 self.assertEqual(self.countJobResults(builds, None), 1)
526 builds = items[2].current_build_set.getBuilds()
527 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700528 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700529
Paul Belanger174a8272017-03-14 13:20:10 -0400530 self.executor_server.hold_jobs_in_build = False
531 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700532 self.waitUntilSettled()
533
534 self.assertEqual(len(self.builds), 0)
535 self.assertEqual(len(self.history), 12)
536 self.assertEqual(A.data['status'], 'MERGED')
537 self.assertEqual(B.data['status'], 'NEW')
538 self.assertEqual(C.data['status'], 'MERGED')
539 self.assertEqual(A.reported, 2)
540 self.assertEqual(B.reported, 2)
541 self.assertEqual(C.reported, 2)
542
James E. Blaird466dc42012-07-31 10:42:56 -0700543 def test_failed_change_at_head_with_queue(self):
544 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700545
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700546 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
548 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
549 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200550 A.addApproval('Code-Review', 2)
551 B.addApproval('Code-Review', 2)
552 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700553
Paul Belanger174a8272017-03-14 13:20:10 -0400554 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700555
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200556 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
557 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
558 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700559
560 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700561 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400562 self.assertEqual(len(self.builds), 0)
563 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700564 self.assertEqual(queue[0].name, b'executor:execute')
565 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800566 self.assertEqual(job_args['job'], 'project-merge')
567 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700568
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. Blair1f4c2bb2013-04-26 08:40:46 -0700573 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700574 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700575 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700576
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400577 self.assertEqual(len(self.builds), 0)
578 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800579
580 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700581 json.loads(queue[0].arguments.decode('utf8'))['job'],
582 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800583 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700584 json.loads(queue[1].arguments.decode('utf8'))['job'],
585 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800586 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700587 json.loads(queue[2].arguments.decode('utf8'))['job'],
588 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800589 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700590 json.loads(queue[3].arguments.decode('utf8'))['job'],
591 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800592 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700593 json.loads(queue[4].arguments.decode('utf8'))['job'],
594 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800595 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700596 json.loads(queue[5].arguments.decode('utf8'))['job'],
597 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700598
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700599 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700600 self.waitUntilSettled()
601
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400602 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700603 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400604 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
605 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700606
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700607 self.gearman_server.hold_jobs_in_queue = False
608 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700609 self.waitUntilSettled()
610
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400611 self.assertEqual(len(self.builds), 0)
612 self.assertEqual(len(self.history), 11)
613 self.assertEqual(A.data['status'], 'NEW')
614 self.assertEqual(B.data['status'], 'MERGED')
615 self.assertEqual(C.data['status'], 'MERGED')
616 self.assertEqual(A.reported, 2)
617 self.assertEqual(B.reported, 2)
618 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700619
James E. Blairec056492016-07-22 09:45:56 -0700620 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700621 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400622 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200624 A.addApproval('Code-Review', 2)
625 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700626 self.waitUntilSettled()
627 time.sleep(2)
628
629 data = json.loads(self.sched.formatStatusJSON())
630 found_job = None
631 for pipeline in data['pipelines']:
632 if pipeline['name'] != 'gate':
633 continue
634 for queue in pipeline['change_queues']:
635 for head in queue['heads']:
636 for item in head:
637 for job in item['jobs']:
638 if job['name'] == 'project-merge':
639 found_job = job
640 break
641
642 self.assertIsNotNone(found_job)
643 if iteration == 1:
644 self.assertIsNotNone(found_job['estimated_time'])
645 self.assertIsNone(found_job['remaining_time'])
646 else:
647 self.assertIsNotNone(found_job['estimated_time'])
648 self.assertTrue(found_job['estimated_time'] >= 2)
649 self.assertIsNotNone(found_job['remaining_time'])
650
Paul Belanger174a8272017-03-14 13:20:10 -0400651 self.executor_server.hold_jobs_in_build = False
652 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700653 self.waitUntilSettled()
654
James E. Blairec056492016-07-22 09:45:56 -0700655 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700656 def test_time_database(self):
657 "Test the time database"
658
659 self._test_time_database(1)
660 self._test_time_database(2)
661
James E. Blairfef71632013-09-23 11:15:47 -0700662 def test_two_failed_changes_at_head(self):
663 "Test that changes are reparented correctly if 2 fail at head"
664
Paul Belanger174a8272017-03-14 13:20:10 -0400665 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
667 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
668 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200669 A.addApproval('Code-Review', 2)
670 B.addApproval('Code-Review', 2)
671 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700672
Paul Belanger174a8272017-03-14 13:20:10 -0400673 self.executor_server.failJob('project-test1', A)
674 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700675
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200676 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
677 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
678 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700679 self.waitUntilSettled()
680
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()
Paul Belanger174a8272017-03-14 13:20:10 -0400685 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700686 self.waitUntilSettled()
687
688 self.assertEqual(len(self.builds), 6)
689 self.assertEqual(self.builds[0].name, 'project-test1')
690 self.assertEqual(self.builds[1].name, 'project-test2')
691 self.assertEqual(self.builds[2].name, 'project-test1')
692 self.assertEqual(self.builds[3].name, 'project-test2')
693 self.assertEqual(self.builds[4].name, 'project-test1')
694 self.assertEqual(self.builds[5].name, 'project-test2')
695
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400696 self.assertTrue(self.builds[0].hasChanges(A))
697 self.assertTrue(self.builds[2].hasChanges(A))
698 self.assertTrue(self.builds[2].hasChanges(B))
699 self.assertTrue(self.builds[4].hasChanges(A))
700 self.assertTrue(self.builds[4].hasChanges(B))
701 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700702
703 # Fail change B first
704 self.release(self.builds[2])
705 self.waitUntilSettled()
706
707 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400708 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700709 self.waitUntilSettled()
710
711 self.assertEqual(len(self.builds), 5)
712 self.assertEqual(self.builds[0].name, 'project-test1')
713 self.assertEqual(self.builds[1].name, 'project-test2')
714 self.assertEqual(self.builds[2].name, 'project-test2')
715 self.assertEqual(self.builds[3].name, 'project-test1')
716 self.assertEqual(self.builds[4].name, 'project-test2')
717
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400718 self.assertTrue(self.builds[1].hasChanges(A))
719 self.assertTrue(self.builds[2].hasChanges(A))
720 self.assertTrue(self.builds[2].hasChanges(B))
721 self.assertTrue(self.builds[4].hasChanges(A))
722 self.assertFalse(self.builds[4].hasChanges(B))
723 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700724
725 # Finish running all passing jobs for change A
726 self.release(self.builds[1])
727 self.waitUntilSettled()
728 # Fail and report change A
729 self.release(self.builds[0])
730 self.waitUntilSettled()
731
732 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400733 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700734 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400735 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700736 self.waitUntilSettled()
737
738 self.assertEqual(len(self.builds), 4)
739 self.assertEqual(self.builds[0].name, 'project-test1') # B
740 self.assertEqual(self.builds[1].name, 'project-test2') # B
741 self.assertEqual(self.builds[2].name, 'project-test1') # C
742 self.assertEqual(self.builds[3].name, 'project-test2') # C
743
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400744 self.assertFalse(self.builds[1].hasChanges(A))
745 self.assertTrue(self.builds[1].hasChanges(B))
746 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700747
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400748 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700749 # After A failed and B and C restarted, B should be back in
750 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400751 self.assertTrue(self.builds[2].hasChanges(B))
752 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700753
Paul Belanger174a8272017-03-14 13:20:10 -0400754 self.executor_server.hold_jobs_in_build = False
755 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700756 self.waitUntilSettled()
757
758 self.assertEqual(len(self.builds), 0)
759 self.assertEqual(len(self.history), 21)
760 self.assertEqual(A.data['status'], 'NEW')
761 self.assertEqual(B.data['status'], 'NEW')
762 self.assertEqual(C.data['status'], 'MERGED')
763 self.assertEqual(A.reported, 2)
764 self.assertEqual(B.reported, 2)
765 self.assertEqual(C.reported, 2)
766
James E. Blair8c803f82012-07-31 16:25:42 -0700767 def test_patch_order(self):
768 "Test that dependent patches are tested in the right order"
769 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
770 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
771 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200772 A.addApproval('Code-Review', 2)
773 B.addApproval('Code-Review', 2)
774 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700775
776 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
777 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
778 M2.setMerged()
779 M1.setMerged()
780
781 # C -> B -> A -> M1 -> M2
782 # M2 is here to make sure it is never queried. If it is, it
783 # means zuul is walking down the entire history of merged
784 # changes.
785
786 C.setDependsOn(B, 1)
787 B.setDependsOn(A, 1)
788 A.setDependsOn(M1, 1)
789 M1.setDependsOn(M2, 1)
790
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200791 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700792
793 self.waitUntilSettled()
794
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400795 self.assertEqual(A.data['status'], 'NEW')
796 self.assertEqual(B.data['status'], 'NEW')
797 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700798
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200799 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
800 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700801
802 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400803 self.assertEqual(M2.queried, 0)
804 self.assertEqual(A.data['status'], 'MERGED')
805 self.assertEqual(B.data['status'], 'MERGED')
806 self.assertEqual(C.data['status'], 'MERGED')
807 self.assertEqual(A.reported, 2)
808 self.assertEqual(B.reported, 2)
809 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700810
James E. Blair063672f2015-01-29 13:09:12 -0800811 def test_needed_changes_enqueue(self):
812 "Test that a needed change is enqueued ahead"
813 # A Given a git tree like this, if we enqueue
814 # / \ change C, we should walk up and down the tree
815 # B G and enqueue changes in the order ABCDEFG.
816 # /|\ This is also the order that you would get if
817 # *C E F you enqueued changes in the order ABCDEFG, so
818 # / the ordering is stable across re-enqueue events.
819 # D
820
821 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
822 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
823 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
824 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
825 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
826 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
827 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
828 B.setDependsOn(A, 1)
829 C.setDependsOn(B, 1)
830 D.setDependsOn(C, 1)
831 E.setDependsOn(B, 1)
832 F.setDependsOn(B, 1)
833 G.setDependsOn(A, 1)
834
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200835 A.addApproval('Code-Review', 2)
836 B.addApproval('Code-Review', 2)
837 C.addApproval('Code-Review', 2)
838 D.addApproval('Code-Review', 2)
839 E.addApproval('Code-Review', 2)
840 F.addApproval('Code-Review', 2)
841 G.addApproval('Code-Review', 2)
842 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800843
844 self.waitUntilSettled()
845
846 self.assertEqual(A.data['status'], 'NEW')
847 self.assertEqual(B.data['status'], 'NEW')
848 self.assertEqual(C.data['status'], 'NEW')
849 self.assertEqual(D.data['status'], 'NEW')
850 self.assertEqual(E.data['status'], 'NEW')
851 self.assertEqual(F.data['status'], 'NEW')
852 self.assertEqual(G.data['status'], 'NEW')
853
854 # We're about to add approvals to changes without adding the
855 # triggering events to Zuul, so that we can be sure that it is
856 # enqueing the changes based on dependencies, not because of
857 # triggering events. Since it will have the changes cached
858 # already (without approvals), we need to clear the cache
859 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400860 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100861 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800862
Paul Belanger174a8272017-03-14 13:20:10 -0400863 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200864 A.addApproval('Approved', 1)
865 B.addApproval('Approved', 1)
866 D.addApproval('Approved', 1)
867 E.addApproval('Approved', 1)
868 F.addApproval('Approved', 1)
869 G.addApproval('Approved', 1)
870 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800871
872 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400873 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800874 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400875 self.executor_server.hold_jobs_in_build = False
876 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800877 self.waitUntilSettled()
878
879 self.assertEqual(A.data['status'], 'MERGED')
880 self.assertEqual(B.data['status'], 'MERGED')
881 self.assertEqual(C.data['status'], 'MERGED')
882 self.assertEqual(D.data['status'], 'MERGED')
883 self.assertEqual(E.data['status'], 'MERGED')
884 self.assertEqual(F.data['status'], 'MERGED')
885 self.assertEqual(G.data['status'], 'MERGED')
886 self.assertEqual(A.reported, 2)
887 self.assertEqual(B.reported, 2)
888 self.assertEqual(C.reported, 2)
889 self.assertEqual(D.reported, 2)
890 self.assertEqual(E.reported, 2)
891 self.assertEqual(F.reported, 2)
892 self.assertEqual(G.reported, 2)
893 self.assertEqual(self.history[6].changes,
894 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
895
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100896 def test_source_cache(self):
897 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400898 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700899
900 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
901 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
902 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200903 A.addApproval('Code-Review', 2)
904 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700905
906 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
907 M1.setMerged()
908
909 B.setDependsOn(A, 1)
910 A.setDependsOn(M1, 1)
911
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200912 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700913 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
914
915 self.waitUntilSettled()
916
917 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700918 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700919 build.release()
920 self.waitUntilSettled()
921 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700922 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700923 build.release()
924 self.waitUntilSettled()
925
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200926 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700927 self.waitUntilSettled()
928
Joshua Hesketh352264b2015-08-11 23:42:08 +1000929 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700930 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000931 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700932
Paul Belanger174a8272017-03-14 13:20:10 -0400933 self.executor_server.hold_jobs_in_build = False
934 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700935 self.waitUntilSettled()
936
937 self.assertEqual(A.data['status'], 'MERGED')
938 self.assertEqual(B.data['status'], 'MERGED')
939 self.assertEqual(A.queried, 2) # Initial and isMerged
940 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
941
James E. Blair8c803f82012-07-31 16:25:42 -0700942 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700943 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700944 # TODO: move to test_gerrit (this is a unit test!)
945 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400946 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700947 (trusted, project) = tenant.getProject('org/project')
948 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400949
950 # TODO(pabelanger): As we add more source / trigger APIs we should make
951 # it easier for users to create events for testing.
952 event = zuul.model.TriggerEvent()
953 event.trigger_name = 'gerrit'
954 event.change_number = '1'
955 event.patch_number = '2'
956
957 a = source.getChange(event)
958 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700959 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700960
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200961 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400962 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700963 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700964
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200965 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400966 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700967 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700968
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700969 def test_project_merge_conflict(self):
970 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700971
972 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700973 A = self.fake_gerrit.addFakeChange('org/project',
974 'master', 'A',
975 files={'conflict': 'foo'})
976 B = self.fake_gerrit.addFakeChange('org/project',
977 'master', 'B',
978 files={'conflict': 'bar'})
979 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700980 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200981 A.addApproval('Code-Review', 2)
982 B.addApproval('Code-Review', 2)
983 C.addApproval('Code-Review', 2)
984 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
985 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
986 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700987 self.waitUntilSettled()
988
James E. Blair6736beb2013-07-11 15:18:15 -0700989 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700990 self.assertEqual(C.reported, 1)
991
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()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700996 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700997 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700998
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700999 self.gearman_server.hold_jobs_in_queue = False
1000 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001001 self.waitUntilSettled()
1002
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001003 self.assertEqual(A.data['status'], 'MERGED')
1004 self.assertEqual(B.data['status'], 'NEW')
1005 self.assertEqual(C.data['status'], 'MERGED')
1006 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001007 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001008 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001009
1010 self.assertHistory([
1011 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1012 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1013 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1014 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1015 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1016 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1017 ], ordered=False)
1018
1019 def test_delayed_merge_conflict(self):
1020 "Test that delayed check merge conflicts are handled properly"
1021
1022 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001023 # the executor sucesfully merges a change based on an old
1024 # repo state (frozen by the scheduler) which would otherwise
1025 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001026 self.gearman_server.hold_jobs_in_queue = True
1027 A = self.fake_gerrit.addFakeChange('org/project',
1028 'master', 'A',
1029 files={'conflict': 'foo'})
1030 B = self.fake_gerrit.addFakeChange('org/project',
1031 'master', 'B',
1032 files={'conflict': 'bar'})
1033 C = self.fake_gerrit.addFakeChange('org/project',
1034 'master', 'C')
1035 C.setDependsOn(B, 1)
1036
1037 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001038 A.addApproval('Code-Review', 2)
1039 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001040 self.waitUntilSettled()
1041 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001042 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001043 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1044 self.waitUntilSettled()
1045
1046 self.assertEqual(A.reported, 1)
1047 self.assertEqual(B.reported, 0) # Check does not report start
1048 self.assertEqual(C.reported, 0) # Check does not report start
1049
1050 # A merges while B and C are queued in check
1051 # Release A project-merge
1052 queue = self.gearman_server.getQueue()
1053 self.release(queue[0])
1054 self.waitUntilSettled()
1055
1056 # Release A project-test*
1057 # gate has higher precedence, so A's test jobs are added in
1058 # front of the merge jobs for B and C
1059 queue = self.gearman_server.getQueue()
1060 self.release(queue[0])
1061 self.release(queue[1])
1062 self.waitUntilSettled()
1063
1064 self.assertEqual(A.data['status'], 'MERGED')
1065 self.assertEqual(B.data['status'], 'NEW')
1066 self.assertEqual(C.data['status'], 'NEW')
1067 self.assertEqual(A.reported, 2)
1068 self.assertEqual(B.reported, 0)
1069 self.assertEqual(C.reported, 0)
1070 self.assertHistory([
1071 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1072 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1073 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1074 ], ordered=False)
1075
1076 # B and C report merge conflicts
1077 # Release B project-merge
1078 queue = self.gearman_server.getQueue()
1079 self.release(queue[0])
1080 self.waitUntilSettled()
1081
1082 # Release C
1083 self.gearman_server.hold_jobs_in_queue = False
1084 self.gearman_server.release()
1085 self.waitUntilSettled()
1086
1087 self.assertEqual(A.data['status'], 'MERGED')
1088 self.assertEqual(B.data['status'], 'NEW')
1089 self.assertEqual(C.data['status'], 'NEW')
1090 self.assertEqual(A.reported, 2)
1091 self.assertEqual(B.reported, 1)
1092 self.assertEqual(C.reported, 1)
1093
1094 self.assertHistory([
1095 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1096 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1097 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001098 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1099 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1100 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1101 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1102 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1103 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001104 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001105
James E. Blairdaabed22012-08-15 15:38:57 -07001106 def test_post(self):
1107 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001108 p = "review.example.com/org/project"
1109 upstream = self.getUpstreamRepos([p])
1110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1111 A.setMerged()
1112 A_commit = str(upstream[p].commit('master'))
1113 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001114
Zhongyue Luo5d556072012-09-21 02:00:47 +09001115 e = {
1116 "type": "ref-updated",
1117 "submitter": {
1118 "name": "User Name",
1119 },
1120 "refUpdate": {
1121 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001122 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001123 "refName": "master",
1124 "project": "org/project",
1125 }
1126 }
James E. Blairdaabed22012-08-15 15:38:57 -07001127 self.fake_gerrit.addEvent(e)
1128 self.waitUntilSettled()
1129
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001130 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001131 self.assertEqual(len(self.history), 1)
1132 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001133
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001134 def test_post_ignore_deletes(self):
1135 "Test that deleting refs does not trigger post jobs"
1136
1137 e = {
1138 "type": "ref-updated",
1139 "submitter": {
1140 "name": "User Name",
1141 },
1142 "refUpdate": {
1143 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1144 "newRev": "0000000000000000000000000000000000000000",
1145 "refName": "master",
1146 "project": "org/project",
1147 }
1148 }
1149 self.fake_gerrit.addEvent(e)
1150 self.waitUntilSettled()
1151
1152 job_names = [x.name for x in self.history]
1153 self.assertEqual(len(self.history), 0)
1154 self.assertNotIn('project-post', job_names)
1155
James E. Blaircc324192017-04-19 14:36:08 -07001156 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001157 def test_post_ignore_deletes_negative(self):
1158 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001159 e = {
1160 "type": "ref-updated",
1161 "submitter": {
1162 "name": "User Name",
1163 },
1164 "refUpdate": {
1165 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1166 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001167 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001168 "project": "org/project",
1169 }
1170 }
1171 self.fake_gerrit.addEvent(e)
1172 self.waitUntilSettled()
1173
1174 job_names = [x.name for x in self.history]
1175 self.assertEqual(len(self.history), 1)
1176 self.assertIn('project-post', job_names)
1177
James E. Blairec056492016-07-22 09:45:56 -07001178 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001179 def test_build_configuration_branch_interaction(self):
1180 "Test that switching between branches works"
1181 self.test_build_configuration()
1182 self.test_build_configuration_branch()
1183 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001184 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001185 repo = git.Repo(path)
1186 repo.heads.master.commit = repo.commit('init')
1187 self.test_build_configuration()
1188
James E. Blairdb93b302017-07-19 15:33:11 -07001189 def test_dependent_changes_rebase(self):
1190 # Test that no errors occur when we walk a dependency tree
1191 # with an unused leaf node due to a rebase.
1192 # Start by constructing: C -> B -> A
1193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1194 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1195 B.setDependsOn(A, 1)
1196
1197 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1198 C.setDependsOn(B, 1)
1199
1200 # Then rebase to form: D -> C -> A
1201 C.addPatchset() # C,2
1202 C.setDependsOn(A, 1)
1203
1204 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1205 D.setDependsOn(C, 2)
1206
1207 # Walk the entire tree
1208 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1209 self.waitUntilSettled()
1210 self.assertEqual(len(self.history), 3)
1211
1212 # Verify that walking just part of the tree still works
1213 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1214 self.waitUntilSettled()
1215 self.assertEqual(len(self.history), 6)
1216
James E. Blaircaec0c52012-08-22 14:52:22 -07001217 def test_dependent_changes_dequeue(self):
1218 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001219
James E. Blaircaec0c52012-08-22 14:52:22 -07001220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1222 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001223 A.addApproval('Code-Review', 2)
1224 B.addApproval('Code-Review', 2)
1225 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001226
1227 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1228 M1.setMerged()
1229
1230 # C -> B -> A -> M1
1231
1232 C.setDependsOn(B, 1)
1233 B.setDependsOn(A, 1)
1234 A.setDependsOn(M1, 1)
1235
Paul Belanger174a8272017-03-14 13:20:10 -04001236 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001237
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001238 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1239 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1240 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001241
1242 self.waitUntilSettled()
1243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertEqual(A.data['status'], 'NEW')
1245 self.assertEqual(A.reported, 2)
1246 self.assertEqual(B.data['status'], 'NEW')
1247 self.assertEqual(B.reported, 2)
1248 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001249 self.assertIn('This change depends on a change that failed to merge.',
1250 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001251 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001252
James E. Blair972e3c72013-08-29 12:04:55 -07001253 def test_failing_dependent_changes(self):
1254 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001255 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001256 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1257 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1258 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1259 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1260 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001261 A.addApproval('Code-Review', 2)
1262 B.addApproval('Code-Review', 2)
1263 C.addApproval('Code-Review', 2)
1264 D.addApproval('Code-Review', 2)
1265 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001266
1267 # E, D -> C -> B, A
1268
1269 D.setDependsOn(C, 1)
1270 C.setDependsOn(B, 1)
1271
Paul Belanger174a8272017-03-14 13:20:10 -04001272 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001273
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001274 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1275 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1276 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1277 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1278 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001279
1280 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()
Paul Belanger174a8272017-03-14 13:20:10 -04001283 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001285 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001288 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001289 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001290 self.waitUntilSettled()
1291
Paul Belanger174a8272017-03-14 13:20:10 -04001292 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001293 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001294 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001295 build.release()
1296 self.waitUntilSettled()
1297
Paul Belanger174a8272017-03-14 13:20:10 -04001298 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.waitUntilSettled()
1300
1301 self.assertEqual(A.data['status'], 'MERGED')
1302 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001303 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001304 self.assertEqual(B.data['status'], 'NEW')
1305 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001306 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001307 self.assertEqual(C.data['status'], 'NEW')
1308 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001309 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001310 self.assertEqual(D.data['status'], 'NEW')
1311 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001312 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001313 self.assertEqual(E.data['status'], 'MERGED')
1314 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001315 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001316 self.assertEqual(len(self.history), 18)
1317
James E. Blairec590122012-08-22 15:19:31 -07001318 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001319 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001320 # If it's dequeued more than once, we should see extra
1321 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001322
Paul Belanger174a8272017-03-14 13:20:10 -04001323 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1325 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1326 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001327 A.addApproval('Code-Review', 2)
1328 B.addApproval('Code-Review', 2)
1329 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001330
Paul Belanger174a8272017-03-14 13:20:10 -04001331 self.executor_server.failJob('project-test1', A)
1332 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001333
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001334 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1335 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1336 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001337
1338 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001339
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001340 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001341 self.assertEqual(self.builds[0].name, 'project-merge')
1342 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001343
Paul Belanger174a8272017-03-14 13:20:10 -04001344 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001346 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001347 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001348 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001349 self.waitUntilSettled()
1350
James E. Blair6df67132017-04-19 13:53:20 -07001351 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001352 self.assertEqual(self.builds[0].name, 'project-test1')
1353 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001354 self.assertEqual(self.builds[2].name, 'project-test1')
1355 self.assertEqual(self.builds[3].name, 'project-test2')
1356 self.assertEqual(self.builds[4].name, 'project-test1')
1357 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001358
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001359 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001360 self.waitUntilSettled()
1361
James E. Blair6df67132017-04-19 13:53:20 -07001362 self.assertEqual(len(self.builds), 2) # test2, merge for B
1363 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001364
Paul Belanger174a8272017-03-14 13:20:10 -04001365 self.executor_server.hold_jobs_in_build = False
1366 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001367 self.waitUntilSettled()
1368
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001369 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001370 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001371
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001372 self.assertEqual(A.data['status'], 'NEW')
1373 self.assertEqual(B.data['status'], 'MERGED')
1374 self.assertEqual(C.data['status'], 'MERGED')
1375 self.assertEqual(A.reported, 2)
1376 self.assertEqual(B.reported, 2)
1377 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001378
James E. Blaircc020532017-04-19 13:04:44 -07001379 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001380 def test_nonvoting_job(self):
1381 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001382
James E. Blair4ec821f2012-08-23 15:28:28 -07001383 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1384 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001385 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001386 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001387 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001388
1389 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001390
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001391 self.assertEqual(A.data['status'], 'MERGED')
1392 self.assertEqual(A.reported, 2)
1393 self.assertEqual(
1394 self.getJobFromHistory('nonvoting-project-merge').result,
1395 'SUCCESS')
1396 self.assertEqual(
1397 self.getJobFromHistory('nonvoting-project-test1').result,
1398 'SUCCESS')
1399 self.assertEqual(
1400 self.getJobFromHistory('nonvoting-project-test2').result,
1401 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001402
James E. Blair2ef29e92017-07-21 15:25:05 -07001403 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1404 parameters['zuul']['voting'])
1405 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1406 parameters['zuul']['voting'])
1407 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1408 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001409
James E. Blaire0487072012-08-29 17:38:31 -07001410 def test_check_queue_success(self):
1411 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001412
James E. Blaire0487072012-08-29 17:38:31 -07001413 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1414 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1415
1416 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001417
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 self.assertEqual(A.data['status'], 'NEW')
1419 self.assertEqual(A.reported, 1)
1420 self.assertEqual(self.getJobFromHistory('project-merge').result,
1421 'SUCCESS')
1422 self.assertEqual(self.getJobFromHistory('project-test1').result,
1423 'SUCCESS')
1424 self.assertEqual(self.getJobFromHistory('project-test2').result,
1425 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001426
1427 def test_check_queue_failure(self):
1428 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429
James E. Blaire0487072012-08-29 17:38:31 -07001430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001431 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001432 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1433
1434 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001435
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001436 self.assertEqual(A.data['status'], 'NEW')
1437 self.assertEqual(A.reported, 1)
1438 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001439 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001440 self.assertEqual(self.getJobFromHistory('project-test1').result,
1441 'SUCCESS')
1442 self.assertEqual(self.getJobFromHistory('project-test2').result,
1443 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001444
David Shrewsburyffab07a2017-07-24 12:45:07 -04001445 @simple_layout('layouts/autohold.yaml')
1446 def test_autohold(self):
1447 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1448
1449 client = zuul.rpcclient.RPCClient('127.0.0.1',
1450 self.gearman_server.port)
1451 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001452 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1453 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001454 self.assertTrue(r)
1455
1456 self.executor_server.failJob('project-test2', A)
1457 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1458
1459 self.waitUntilSettled()
1460
1461 self.assertEqual(A.data['status'], 'NEW')
1462 self.assertEqual(A.reported, 1)
1463 self.assertEqual(self.getJobFromHistory('project-test2').result,
1464 'FAILURE')
1465
1466 # Check nodepool for a held node
1467 held_node = None
1468 for node in self.fake_nodepool.getNodes():
1469 if node['state'] == zuul.model.STATE_HOLD:
1470 held_node = node
1471 break
1472 self.assertIsNotNone(held_node)
1473
1474 # Validate node has recorded the failed job
1475 self.assertEqual(
1476 held_node['hold_job'],
1477 " ".join(['tenant-one',
1478 'review.example.com/org/project',
1479 'project-test2'])
1480 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001481 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001482
1483 # Another failed change should not hold any more nodes
1484 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1485 self.executor_server.failJob('project-test2', B)
1486 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1487 self.waitUntilSettled()
1488 self.assertEqual(B.data['status'], 'NEW')
1489 self.assertEqual(B.reported, 1)
1490 self.assertEqual(self.getJobFromHistory('project-test2').result,
1491 'FAILURE')
1492
1493 held_nodes = 0
1494 for node in self.fake_nodepool.getNodes():
1495 if node['state'] == zuul.model.STATE_HOLD:
1496 held_nodes += 1
1497 self.assertEqual(held_nodes, 1)
1498
James E. Blair7cb84542017-04-19 13:35:05 -07001499 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001500 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001501 # This particular test does a large amount of merges and needs a little
1502 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001503 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001504 "test that dependent changes behind dequeued changes work"
1505 # This complicated test is a reproduction of a real life bug
1506 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001507
Paul Belanger174a8272017-03-14 13:20:10 -04001508 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001509 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1510 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1511 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1512 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1513 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1514 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1515 D.setDependsOn(C, 1)
1516 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001517 A.addApproval('Code-Review', 2)
1518 B.addApproval('Code-Review', 2)
1519 C.addApproval('Code-Review', 2)
1520 D.addApproval('Code-Review', 2)
1521 E.addApproval('Code-Review', 2)
1522 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001523
1524 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001525
1526 # Change object re-use in the gerrit trigger is hidden if
1527 # changes are added in quick succession; waiting makes it more
1528 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001529 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001530 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001531 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001532 self.waitUntilSettled()
1533
Paul Belanger174a8272017-03-14 13:20:10 -04001534 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001535 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001536 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001537 self.waitUntilSettled()
1538
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001539 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001540 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001541 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001542 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001543 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001544 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001545 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001546 self.waitUntilSettled()
1547
Paul Belanger174a8272017-03-14 13:20:10 -04001548 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001550 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001551 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001552 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001553 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001554 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001555 self.waitUntilSettled()
1556
1557 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001558
1559 # Grab pointers to the jobs we want to release before
1560 # releasing any, because list indexes may change as
1561 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001562 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001563 a.release()
1564 b.release()
1565 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001566 self.waitUntilSettled()
1567
Paul Belanger174a8272017-03-14 13:20:10 -04001568 self.executor_server.hold_jobs_in_build = False
1569 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001570 self.waitUntilSettled()
1571
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001572 self.assertEqual(A.data['status'], 'NEW')
1573 self.assertEqual(B.data['status'], 'MERGED')
1574 self.assertEqual(C.data['status'], 'MERGED')
1575 self.assertEqual(D.data['status'], 'MERGED')
1576 self.assertEqual(E.data['status'], 'MERGED')
1577 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001578
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001579 self.assertEqual(A.reported, 2)
1580 self.assertEqual(B.reported, 2)
1581 self.assertEqual(C.reported, 2)
1582 self.assertEqual(D.reported, 2)
1583 self.assertEqual(E.reported, 2)
1584 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001585
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001586 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1587 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001588
1589 def test_merger_repack(self):
1590 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001591
James E. Blair05fed602012-09-07 12:45:24 -07001592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001593 A.addApproval('Code-Review', 2)
1594 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001595 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001596 self.assertEqual(self.getJobFromHistory('project-merge').result,
1597 'SUCCESS')
1598 self.assertEqual(self.getJobFromHistory('project-test1').result,
1599 'SUCCESS')
1600 self.assertEqual(self.getJobFromHistory('project-test2').result,
1601 'SUCCESS')
1602 self.assertEqual(A.data['status'], 'MERGED')
1603 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001604 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001605 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001606
James E. Blair2a535672017-04-27 12:03:15 -07001607 path = os.path.join(self.merger_src_root, "review.example.com",
1608 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001609 if os.path.exists(path):
1610 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001611 path = os.path.join(self.executor_src_root, "review.example.com",
1612 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001613 if os.path.exists(path):
1614 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001615
1616 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001617 A.addApproval('Code-Review', 2)
1618 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001619 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001620 self.assertEqual(self.getJobFromHistory('project-merge').result,
1621 'SUCCESS')
1622 self.assertEqual(self.getJobFromHistory('project-test1').result,
1623 'SUCCESS')
1624 self.assertEqual(self.getJobFromHistory('project-test2').result,
1625 'SUCCESS')
1626 self.assertEqual(A.data['status'], 'MERGED')
1627 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001628
James E. Blair4886f282012-11-15 09:27:33 -08001629 def test_merger_repack_large_change(self):
1630 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001631 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001632 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001633 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001634 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001635 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001636 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001637 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001638 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001639 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001640 # TODOv3(jeblair): add hostname to upstream root
1641 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001642 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001643 path = os.path.join(self.merger_src_root, 'review.example.com',
1644 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001645 if os.path.exists(path):
1646 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001647 path = os.path.join(self.executor_src_root, 'review.example.com',
1648 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001649 if os.path.exists(path):
1650 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001651
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001652 A.addApproval('Code-Review', 2)
1653 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001654 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001655 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001656 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001657 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001658 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001659 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001660 'SUCCESS')
1661 self.assertEqual(A.data['status'], 'MERGED')
1662 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001663
James E. Blair2fa50962013-01-30 21:50:41 -08001664 def test_new_patchset_dequeues_old(self):
1665 "Test that a new patchset causes the old to be dequeued"
1666 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001667 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001668 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1669 M.setMerged()
1670
1671 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1672 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1673 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1674 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001675 A.addApproval('Code-Review', 2)
1676 B.addApproval('Code-Review', 2)
1677 C.addApproval('Code-Review', 2)
1678 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001679
1680 C.setDependsOn(B, 1)
1681 B.setDependsOn(A, 1)
1682 A.setDependsOn(M, 1)
1683
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001684 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1685 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1686 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1687 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001688 self.waitUntilSettled()
1689
1690 B.addPatchset()
1691 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1692 self.waitUntilSettled()
1693
Paul Belanger174a8272017-03-14 13:20:10 -04001694 self.executor_server.hold_jobs_in_build = False
1695 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001696 self.waitUntilSettled()
1697
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001698 self.assertEqual(A.data['status'], 'MERGED')
1699 self.assertEqual(A.reported, 2)
1700 self.assertEqual(B.data['status'], 'NEW')
1701 self.assertEqual(B.reported, 2)
1702 self.assertEqual(C.data['status'], 'NEW')
1703 self.assertEqual(C.reported, 2)
1704 self.assertEqual(D.data['status'], 'MERGED')
1705 self.assertEqual(D.reported, 2)
1706 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001707
James E. Blairba437362015-02-07 11:41:52 -08001708 def test_new_patchset_check(self):
1709 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001710
Paul Belanger174a8272017-03-14 13:20:10 -04001711 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001712
1713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001714 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001715 tenant = self.sched.abide.tenants.get('tenant-one')
1716 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001717
1718 # Add two git-dependent changes
1719 B.setDependsOn(A, 1)
1720 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1721 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001722 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1723 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001724
1725 # A live item, and a non-live/live pair
1726 items = check_pipeline.getAllItems()
1727 self.assertEqual(len(items), 3)
1728
1729 self.assertEqual(items[0].change.number, '1')
1730 self.assertEqual(items[0].change.patchset, '1')
1731 self.assertFalse(items[0].live)
1732
1733 self.assertEqual(items[1].change.number, '2')
1734 self.assertEqual(items[1].change.patchset, '1')
1735 self.assertTrue(items[1].live)
1736
1737 self.assertEqual(items[2].change.number, '1')
1738 self.assertEqual(items[2].change.patchset, '1')
1739 self.assertTrue(items[2].live)
1740
1741 # Add a new patchset to A
1742 A.addPatchset()
1743 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1744 self.waitUntilSettled()
1745
1746 # The live copy of A,1 should be gone, but the non-live and B
1747 # should continue, and we should have a new A,2
1748 items = check_pipeline.getAllItems()
1749 self.assertEqual(len(items), 3)
1750
1751 self.assertEqual(items[0].change.number, '1')
1752 self.assertEqual(items[0].change.patchset, '1')
1753 self.assertFalse(items[0].live)
1754
1755 self.assertEqual(items[1].change.number, '2')
1756 self.assertEqual(items[1].change.patchset, '1')
1757 self.assertTrue(items[1].live)
1758
1759 self.assertEqual(items[2].change.number, '1')
1760 self.assertEqual(items[2].change.patchset, '2')
1761 self.assertTrue(items[2].live)
1762
1763 # Add a new patchset to B
1764 B.addPatchset()
1765 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1766 self.waitUntilSettled()
1767
1768 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1769 # but we should have a new B,2 (still based on A,1)
1770 items = check_pipeline.getAllItems()
1771 self.assertEqual(len(items), 3)
1772
1773 self.assertEqual(items[0].change.number, '1')
1774 self.assertEqual(items[0].change.patchset, '2')
1775 self.assertTrue(items[0].live)
1776
1777 self.assertEqual(items[1].change.number, '1')
1778 self.assertEqual(items[1].change.patchset, '1')
1779 self.assertFalse(items[1].live)
1780
1781 self.assertEqual(items[2].change.number, '2')
1782 self.assertEqual(items[2].change.patchset, '2')
1783 self.assertTrue(items[2].live)
1784
1785 self.builds[0].release()
1786 self.waitUntilSettled()
1787 self.builds[0].release()
1788 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001789 self.executor_server.hold_jobs_in_build = False
1790 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001791 self.waitUntilSettled()
1792
1793 self.assertEqual(A.reported, 1)
1794 self.assertEqual(B.reported, 1)
1795 self.assertEqual(self.history[0].result, 'ABORTED')
1796 self.assertEqual(self.history[0].changes, '1,1')
1797 self.assertEqual(self.history[1].result, 'ABORTED')
1798 self.assertEqual(self.history[1].changes, '1,1 2,1')
1799 self.assertEqual(self.history[2].result, 'SUCCESS')
1800 self.assertEqual(self.history[2].changes, '1,2')
1801 self.assertEqual(self.history[3].result, 'SUCCESS')
1802 self.assertEqual(self.history[3].changes, '1,1 2,2')
1803
1804 def test_abandoned_gate(self):
1805 "Test that an abandoned change is dequeued from gate"
1806
Paul Belanger174a8272017-03-14 13:20:10 -04001807 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001808
1809 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001810 A.addApproval('Code-Review', 2)
1811 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001812 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001813 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1814 self.assertEqual(self.builds[0].name, 'project-merge')
1815
1816 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1817 self.waitUntilSettled()
1818
Paul Belanger174a8272017-03-14 13:20:10 -04001819 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001820 self.waitUntilSettled()
1821
Arie5b4048c2016-10-05 16:12:06 +03001822 self.assertBuilds([])
1823 self.assertHistory([
1824 dict(name='project-merge', result='ABORTED', changes='1,1')],
1825 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001826 self.assertEqual(A.reported, 1,
1827 "Abandoned gate change should report only start")
1828
1829 def test_abandoned_check(self):
1830 "Test that an abandoned change is dequeued from check"
1831
Paul Belanger174a8272017-03-14 13:20:10 -04001832 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001833
1834 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1835 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001836 tenant = self.sched.abide.tenants.get('tenant-one')
1837 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001838
1839 # Add two git-dependent changes
1840 B.setDependsOn(A, 1)
1841 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1842 self.waitUntilSettled()
1843 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1844 self.waitUntilSettled()
1845 # A live item, and a non-live/live pair
1846 items = check_pipeline.getAllItems()
1847 self.assertEqual(len(items), 3)
1848
1849 self.assertEqual(items[0].change.number, '1')
1850 self.assertFalse(items[0].live)
1851
1852 self.assertEqual(items[1].change.number, '2')
1853 self.assertTrue(items[1].live)
1854
1855 self.assertEqual(items[2].change.number, '1')
1856 self.assertTrue(items[2].live)
1857
1858 # Abandon A
1859 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1860 self.waitUntilSettled()
1861
1862 # The live copy of A should be gone, but the non-live and B
1863 # should continue
1864 items = check_pipeline.getAllItems()
1865 self.assertEqual(len(items), 2)
1866
1867 self.assertEqual(items[0].change.number, '1')
1868 self.assertFalse(items[0].live)
1869
1870 self.assertEqual(items[1].change.number, '2')
1871 self.assertTrue(items[1].live)
1872
Paul Belanger174a8272017-03-14 13:20:10 -04001873 self.executor_server.hold_jobs_in_build = False
1874 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001875 self.waitUntilSettled()
1876
1877 self.assertEqual(len(self.history), 4)
1878 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001879 'Build should have been aborted')
1880 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001881 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001882
Steve Varnau7b78b312015-04-03 14:49:46 -07001883 def test_abandoned_not_timer(self):
1884 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001885 # This test can not use simple_layout because it must start
1886 # with a configuration which does not include a
1887 # timer-triggered job so that we have an opportunity to set
1888 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001889 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001890 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001891 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001892 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001893 # The pipeline triggers every second, so we should have seen
1894 # several by now.
1895 time.sleep(5)
1896 self.waitUntilSettled()
1897 # Stop queuing timer triggered jobs so that the assertions
1898 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001899 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001900 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001901 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001902 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001903 # If APScheduler is in mid-event when we remove the job, we
1904 # can end up with one more event firing, so give it an extra
1905 # second to settle.
1906 time.sleep(1)
1907 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001908
James E. Blair21037782017-07-19 11:56:55 -07001909 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001910
1911 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1912 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1913 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001914 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001915
1916 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1917 self.waitUntilSettled()
1918
James E. Blair21037782017-07-19 11:56:55 -07001919 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001920
Paul Belanger174a8272017-03-14 13:20:10 -04001921 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001922 self.waitUntilSettled()
1923
James E. Blair2fa50962013-01-30 21:50:41 -08001924 def test_new_patchset_dequeues_old_on_head(self):
1925 "Test that a new patchset causes the old to be dequeued (at head)"
1926 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001927 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001928 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1929 M.setMerged()
1930 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1931 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1932 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1933 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001934 A.addApproval('Code-Review', 2)
1935 B.addApproval('Code-Review', 2)
1936 C.addApproval('Code-Review', 2)
1937 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001938
1939 C.setDependsOn(B, 1)
1940 B.setDependsOn(A, 1)
1941 A.setDependsOn(M, 1)
1942
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001943 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1944 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1945 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1946 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001947 self.waitUntilSettled()
1948
1949 A.addPatchset()
1950 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1951 self.waitUntilSettled()
1952
Paul Belanger174a8272017-03-14 13:20:10 -04001953 self.executor_server.hold_jobs_in_build = False
1954 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001955 self.waitUntilSettled()
1956
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001957 self.assertEqual(A.data['status'], 'NEW')
1958 self.assertEqual(A.reported, 2)
1959 self.assertEqual(B.data['status'], 'NEW')
1960 self.assertEqual(B.reported, 2)
1961 self.assertEqual(C.data['status'], 'NEW')
1962 self.assertEqual(C.reported, 2)
1963 self.assertEqual(D.data['status'], 'MERGED')
1964 self.assertEqual(D.reported, 2)
1965 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001966
1967 def test_new_patchset_dequeues_old_without_dependents(self):
1968 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001969 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001970 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1971 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1972 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001973 A.addApproval('Code-Review', 2)
1974 B.addApproval('Code-Review', 2)
1975 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001976
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001977 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1978 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1979 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001980 self.waitUntilSettled()
1981
1982 B.addPatchset()
1983 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1984 self.waitUntilSettled()
1985
Paul Belanger174a8272017-03-14 13:20:10 -04001986 self.executor_server.hold_jobs_in_build = False
1987 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001988 self.waitUntilSettled()
1989
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001990 self.assertEqual(A.data['status'], 'MERGED')
1991 self.assertEqual(A.reported, 2)
1992 self.assertEqual(B.data['status'], 'NEW')
1993 self.assertEqual(B.reported, 2)
1994 self.assertEqual(C.data['status'], 'MERGED')
1995 self.assertEqual(C.reported, 2)
1996 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001997
1998 def test_new_patchset_dequeues_old_independent_queue(self):
1999 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002000 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002001 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2002 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2003 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2004 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2005 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2006 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2007 self.waitUntilSettled()
2008
2009 B.addPatchset()
2010 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2011 self.waitUntilSettled()
2012
Paul Belanger174a8272017-03-14 13:20:10 -04002013 self.executor_server.hold_jobs_in_build = False
2014 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002015 self.waitUntilSettled()
2016
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002017 self.assertEqual(A.data['status'], 'NEW')
2018 self.assertEqual(A.reported, 1)
2019 self.assertEqual(B.data['status'], 'NEW')
2020 self.assertEqual(B.reported, 1)
2021 self.assertEqual(C.data['status'], 'NEW')
2022 self.assertEqual(C.reported, 1)
2023 self.assertEqual(len(self.history), 10)
2024 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
James E. Blaircc020532017-04-19 13:04:44 -07002026 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002027 def test_noop_job(self):
2028 "Test that the internal noop job works"
2029 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002030 A.addApproval('Code-Review', 2)
2031 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002032 self.waitUntilSettled()
2033
2034 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2035 self.assertTrue(self.sched._areAllBuildsComplete())
2036 self.assertEqual(len(self.history), 0)
2037 self.assertEqual(A.data['status'], 'MERGED')
2038 self.assertEqual(A.reported, 2)
2039
James E. Blair06cc3922017-04-19 10:08:10 -07002040 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002041 def test_no_job_project(self):
2042 "Test that reports with no jobs don't get sent"
2043 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2044 'master', 'A')
2045 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2046 self.waitUntilSettled()
2047
2048 # Change wasn't reported to
2049 self.assertEqual(A.reported, False)
2050
2051 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002052 tenant = self.sched.abide.tenants.get('tenant-one')
2053 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002054 items = check_pipeline.getAllItems()
2055 self.assertEqual(len(items), 0)
2056
2057 self.assertEqual(len(self.history), 0)
2058
James E. Blair7d0dedc2013-02-21 17:26:09 -08002059 def test_zuul_refs(self):
2060 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002061 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002062 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2063 M1.setMerged()
2064 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2065 M2.setMerged()
2066
2067 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2068 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2069 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2070 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002071 A.addApproval('Code-Review', 2)
2072 B.addApproval('Code-Review', 2)
2073 C.addApproval('Code-Review', 2)
2074 D.addApproval('Code-Review', 2)
2075 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2076 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2077 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2078 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002079
2080 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002081 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002083 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002084 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002085 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002087 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002088 self.waitUntilSettled()
2089
Clint Byrum3343e3e2016-11-15 16:05:03 -08002090 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002091 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002092 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002093 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002094 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002096 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002097 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002098 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002099 d_build = x
2100 if a_build and b_build and c_build and d_build:
2101 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002102
Clint Byrum3343e3e2016-11-15 16:05:03 -08002103 # should have a, not b, and should not be in project2
2104 self.assertTrue(a_build.hasChanges(A))
2105 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002106
Clint Byrum3343e3e2016-11-15 16:05:03 -08002107 # should have a and b, and should not be in project2
2108 self.assertTrue(b_build.hasChanges(A, B))
2109 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002110
Clint Byrum3343e3e2016-11-15 16:05:03 -08002111 # should have a and b in 1, c in 2
2112 self.assertTrue(c_build.hasChanges(A, B, C))
2113 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002114
Clint Byrum3343e3e2016-11-15 16:05:03 -08002115 # should have a and b in 1, c and d in 2
2116 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002117
Paul Belanger174a8272017-03-14 13:20:10 -04002118 self.executor_server.hold_jobs_in_build = False
2119 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002120 self.waitUntilSettled()
2121
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002122 self.assertEqual(A.data['status'], 'MERGED')
2123 self.assertEqual(A.reported, 2)
2124 self.assertEqual(B.data['status'], 'MERGED')
2125 self.assertEqual(B.reported, 2)
2126 self.assertEqual(C.data['status'], 'MERGED')
2127 self.assertEqual(C.reported, 2)
2128 self.assertEqual(D.data['status'], 'MERGED')
2129 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002130
James E. Blair4a28a882013-08-23 15:17:33 -07002131 def test_rerun_on_error(self):
2132 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002133 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002135 A.addApproval('Code-Review', 2)
2136 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002137 self.waitUntilSettled()
2138
James E. Blair412fba82017-01-26 15:00:50 -08002139 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002140 self.executor_server.hold_jobs_in_build = False
2141 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002142 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002143 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002144 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2145
James E. Blair412e5582013-04-22 15:50:12 -07002146 def test_statsd(self):
2147 "Test each of the statsd methods used in the scheduler"
2148 import extras
2149 statsd = extras.try_import('statsd.statsd')
2150 statsd.incr('test-incr')
2151 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002152 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002153 self.assertReportedStat('test-incr', '1|c')
2154 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002155 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002156
James E. Blairdad52252014-02-07 16:59:17 -08002157 def test_stuck_job_cleanup(self):
2158 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002159
2160 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002161 self.gearman_server.hold_jobs_in_queue = True
2162 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002163 A.addApproval('Code-Review', 2)
2164 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002165 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002166 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002167 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2168
James E. Blair31cb3e82017-04-20 13:08:56 -07002169 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002170 self.sched.reconfigure(self.config)
2171 self.waitUntilSettled()
2172
James E. Blair18c64442014-03-18 10:14:45 -07002173 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002174 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002175 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002176 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2177 self.assertTrue(self.sched._areAllBuildsComplete())
2178
2179 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002180 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002181 self.assertEqual(self.history[0].result, 'SUCCESS')
2182
James E. Blair879dafb2015-07-17 14:04:49 -07002183 def test_file_head(self):
2184 # This is a regression test for an observed bug. A change
2185 # with a file named "HEAD" in the root directory of the repo
2186 # was processed by a merger. It then was unable to reset the
2187 # repo because of:
2188 # GitCommandError: 'git reset --hard HEAD' returned
2189 # with exit code 128
2190 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2191 # and filename
2192 # Use '--' to separate filenames from revisions'
2193
2194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002195 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2197
2198 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2199 self.waitUntilSettled()
2200
2201 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2202 self.waitUntilSettled()
2203
2204 self.assertIn('Build succeeded', A.messages[0])
2205 self.assertIn('Build succeeded', B.messages[0])
2206
James E. Blair70c71582013-03-06 08:50:50 -08002207 def test_file_jobs(self):
2208 "Test that file jobs run only when appropriate"
2209 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002210 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002211 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002212 A.addApproval('Code-Review', 2)
2213 B.addApproval('Code-Review', 2)
2214 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2215 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002216 self.waitUntilSettled()
2217
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002218 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002219 if x.name == 'project-testfile']
2220
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002221 self.assertEqual(len(testfile_jobs), 1)
2222 self.assertEqual(testfile_jobs[0].changes, '1,2')
2223 self.assertEqual(A.data['status'], 'MERGED')
2224 self.assertEqual(A.reported, 2)
2225 self.assertEqual(B.data['status'], 'MERGED')
2226 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002227
Clint Byrum3ee39f32016-11-17 23:45:07 -08002228 def _test_irrelevant_files_jobs(self, should_skip):
2229 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002230 if should_skip:
2231 files = {'ignoreme': 'ignored\n'}
2232 else:
2233 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002234
2235 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002236 'master',
2237 'test irrelevant-files',
2238 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002239 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2240 self.waitUntilSettled()
2241
2242 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002243 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002244
2245 if should_skip:
2246 self.assertEqual([], tested_change_ids)
2247 else:
2248 self.assertIn(change.data['number'], tested_change_ids)
2249
James E. Blaircc324192017-04-19 14:36:08 -07002250 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002251 def test_irrelevant_files_match_skips_job(self):
2252 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002253
James E. Blaircc324192017-04-19 14:36:08 -07002254 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002255 def test_irrelevant_files_no_match_runs_job(self):
2256 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002257
James E. Blaircc324192017-04-19 14:36:08 -07002258 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002259 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002260 files = {'ignoreme': 'ignored\n'}
2261
2262 change = self.fake_gerrit.addFakeChange('org/project',
2263 'master',
2264 'test irrelevant-files',
2265 files=files)
2266 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2267 self.waitUntilSettled()
2268
2269 run_jobs = set([build.name for build in self.history])
2270
2271 self.assertEqual(set(['project-test-nomatch-starts-empty',
2272 'project-test-nomatch-starts-full']), run_jobs)
2273
James E. Blairc8a1e052014-02-25 09:29:26 -08002274 def test_queue_names(self):
2275 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002276 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002277 (trusted, project1) = tenant.getProject('org/project1')
2278 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002279 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2280 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2281 self.assertEqual(q1.name, 'integrated')
2282 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002283
James E. Blair64ed6f22013-07-10 14:07:23 -07002284 def test_queue_precedence(self):
2285 "Test that queue precedence works"
2286
2287 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002288 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002289 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2290 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002291 A.addApproval('Code-Review', 2)
2292 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002293
2294 self.waitUntilSettled()
2295 self.gearman_server.hold_jobs_in_queue = False
2296 self.gearman_server.release()
2297 self.waitUntilSettled()
2298
James E. Blair8de58bd2013-07-18 16:23:33 -07002299 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002300 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002301 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002302 self.waitUntilSettled()
2303
James E. Blair64ed6f22013-07-10 14:07:23 -07002304 self.log.debug(self.history)
2305 self.assertEqual(self.history[0].pipeline, 'gate')
2306 self.assertEqual(self.history[1].pipeline, 'check')
2307 self.assertEqual(self.history[2].pipeline, 'gate')
2308 self.assertEqual(self.history[3].pipeline, 'gate')
2309 self.assertEqual(self.history[4].pipeline, 'check')
2310 self.assertEqual(self.history[5].pipeline, 'check')
2311
Clark Boylana5edbe42014-06-03 16:39:10 -07002312 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002313 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002314 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002316 A.addApproval('Code-Review', 2)
2317 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002318 self.waitUntilSettled()
2319
Paul Belanger174a8272017-03-14 13:20:10 -04002320 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002321 self.waitUntilSettled()
2322
James E. Blair1843a552013-07-03 14:19:52 -07002323 port = self.webapp.server.socket.getsockname()[1]
2324
Adam Gandelman77a12c72017-02-03 17:43:43 -08002325 req = urllib.request.Request(
2326 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002327 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002328 headers = f.info()
2329 self.assertIn('Content-Length', headers)
2330 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002331 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2332 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002333 self.assertIn('Access-Control-Allow-Origin', headers)
2334 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002335 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002336 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002337 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002338
Paul Belanger174a8272017-03-14 13:20:10 -04002339 self.executor_server.hold_jobs_in_build = False
2340 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002341 self.waitUntilSettled()
2342
2343 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002344 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002345 for p in data['pipelines']:
2346 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002347 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002348 self.assertEqual(q['window'], 20)
2349 else:
2350 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002351 for head in q['heads']:
2352 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002353 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002354 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002355 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002356 status_jobs.append(job)
2357 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002358 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002359 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002360 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2361 uuid=status_jobs[0]['uuid']),
2362 status_jobs[0]['url'])
2363 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002364 'finger://{hostname}/{uuid}'.format(
2365 hostname=self.executor_server.hostname,
2366 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002367 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002368 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002369 self.assertEqual(
2370 'finger://{hostname}/{uuid}'.format(
2371 hostname=self.executor_server.hostname,
2372 uuid=status_jobs[0]['uuid']),
2373 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002374 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002375 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002376 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2377 uuid=status_jobs[1]['uuid']),
2378 status_jobs[1]['url'])
2379 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002380 'finger://{hostname}/{uuid}'.format(
2381 hostname=self.executor_server.hostname,
2382 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002383 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002384 self.assertEqual(
2385 'finger://{hostname}/{uuid}'.format(
2386 hostname=self.executor_server.hostname,
2387 uuid=status_jobs[1]['uuid']),
2388 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002389
2390 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002391 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002392 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2393 uuid=status_jobs[2]['uuid']),
2394 status_jobs[2]['url'])
2395 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002396 'finger://{hostname}/{uuid}'.format(
2397 hostname=self.executor_server.hostname,
2398 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002399 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002400 self.assertEqual(
2401 'finger://{hostname}/{uuid}'.format(
2402 hostname=self.executor_server.hostname,
2403 uuid=status_jobs[2]['uuid']),
2404 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002405
Tobias Henkel65639f82017-07-10 10:25:42 +02002406 # check job dependencies
2407 self.assertIsNotNone(status_jobs[0]['dependencies'])
2408 self.assertIsNotNone(status_jobs[1]['dependencies'])
2409 self.assertIsNotNone(status_jobs[2]['dependencies'])
2410 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2411 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2412 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2413 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2414 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2415
James E. Blaircdccd972013-07-01 12:10:22 -07002416 def test_live_reconfiguration(self):
2417 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002418 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002419 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002420 A.addApproval('Code-Review', 2)
2421 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002422 self.waitUntilSettled()
2423
2424 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002425 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002426
Paul Belanger174a8272017-03-14 13:20:10 -04002427 self.executor_server.hold_jobs_in_build = False
2428 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002429 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002430 self.assertEqual(self.getJobFromHistory('project-merge').result,
2431 'SUCCESS')
2432 self.assertEqual(self.getJobFromHistory('project-test1').result,
2433 'SUCCESS')
2434 self.assertEqual(self.getJobFromHistory('project-test2').result,
2435 'SUCCESS')
2436 self.assertEqual(A.data['status'], 'MERGED')
2437 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002438
James E. Blair6bc782d2015-07-17 16:20:21 -07002439 def test_live_reconfiguration_merge_conflict(self):
2440 # A real-world bug: a change in a gate queue has a merge
2441 # conflict and a job is added to its project while it's
2442 # sitting in the queue. The job gets added to the change and
2443 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002444 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002445
2446 # This change is fine. It's here to stop the queue long
2447 # enough for the next change to be subject to the
2448 # reconfiguration, as well as to provide a conflict for the
2449 # next change. This change will succeed and merge.
2450 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002451 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002452 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002453
2454 # This change will be in merge conflict. During the
2455 # reconfiguration, we will add a job. We want to make sure
2456 # that doesn't cause it to get stuck.
2457 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002458 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002459 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002460
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002461 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2462 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002463
2464 self.waitUntilSettled()
2465
2466 # No jobs have run yet
2467 self.assertEqual(A.data['status'], 'NEW')
2468 self.assertEqual(A.reported, 1)
2469 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002470 self.assertEqual(len(self.history), 0)
2471
2472 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002473 self.commitConfigUpdate('common-config',
2474 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002475 self.sched.reconfigure(self.config)
2476 self.waitUntilSettled()
2477
Paul Belanger174a8272017-03-14 13:20:10 -04002478 self.executor_server.hold_jobs_in_build = False
2479 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002480 self.waitUntilSettled()
2481
2482 self.assertEqual(A.data['status'], 'MERGED')
2483 self.assertEqual(A.reported, 2)
2484 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002485 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002486 self.assertEqual(self.getJobFromHistory('project-merge').result,
2487 'SUCCESS')
2488 self.assertEqual(self.getJobFromHistory('project-test1').result,
2489 'SUCCESS')
2490 self.assertEqual(self.getJobFromHistory('project-test2').result,
2491 'SUCCESS')
2492 self.assertEqual(self.getJobFromHistory('project-test3').result,
2493 'SUCCESS')
2494 self.assertEqual(len(self.history), 4)
2495
James E. Blair400e8fd2015-07-30 17:44:45 -07002496 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002497 # An extrapolation of test_live_reconfiguration_merge_conflict
2498 # that tests a job added to a job tree with a failed root does
2499 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002500 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002501
2502 # This change is fine. It's here to stop the queue long
2503 # enough for the next change to be subject to the
2504 # reconfiguration. This change will succeed and merge.
2505 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002506 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002507 A.addApproval('Code-Review', 2)
2508 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002509 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002510 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002511 self.waitUntilSettled()
2512
2513 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002514 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002515 B.addApproval('Code-Review', 2)
2516 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002517 self.waitUntilSettled()
2518
Paul Belanger174a8272017-03-14 13:20:10 -04002519 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002520 self.waitUntilSettled()
2521
2522 # Both -merge jobs have run, but no others.
2523 self.assertEqual(A.data['status'], 'NEW')
2524 self.assertEqual(A.reported, 1)
2525 self.assertEqual(B.data['status'], 'NEW')
2526 self.assertEqual(B.reported, 1)
2527 self.assertEqual(self.history[0].result, 'SUCCESS')
2528 self.assertEqual(self.history[0].name, 'project-merge')
2529 self.assertEqual(self.history[1].result, 'FAILURE')
2530 self.assertEqual(self.history[1].name, 'project-merge')
2531 self.assertEqual(len(self.history), 2)
2532
2533 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002534 self.commitConfigUpdate('common-config',
2535 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002536 self.sched.reconfigure(self.config)
2537 self.waitUntilSettled()
2538
Paul Belanger174a8272017-03-14 13:20:10 -04002539 self.executor_server.hold_jobs_in_build = False
2540 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002541 self.waitUntilSettled()
2542
2543 self.assertEqual(A.data['status'], 'MERGED')
2544 self.assertEqual(A.reported, 2)
2545 self.assertEqual(B.data['status'], 'NEW')
2546 self.assertEqual(B.reported, 2)
2547 self.assertEqual(self.history[0].result, 'SUCCESS')
2548 self.assertEqual(self.history[0].name, 'project-merge')
2549 self.assertEqual(self.history[1].result, 'FAILURE')
2550 self.assertEqual(self.history[1].name, 'project-merge')
2551 self.assertEqual(self.history[2].result, 'SUCCESS')
2552 self.assertEqual(self.history[3].result, 'SUCCESS')
2553 self.assertEqual(self.history[4].result, 'SUCCESS')
2554 self.assertEqual(len(self.history), 5)
2555
James E. Blair400e8fd2015-07-30 17:44:45 -07002556 def test_live_reconfiguration_failed_job(self):
2557 # Test that a change with a removed failing job does not
2558 # disrupt reconfiguration. If a change has a failed job and
2559 # that job is removed during a reconfiguration, we observed a
2560 # bug where the code to re-set build statuses would run on
2561 # that build and raise an exception because the job no longer
2562 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002563 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002564
2565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2566
2567 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002568 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002569
2570 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2571 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002572 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002573 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002574 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002575 self.waitUntilSettled()
2576
2577 self.assertEqual(A.data['status'], 'NEW')
2578 self.assertEqual(A.reported, 0)
2579
2580 self.assertEqual(self.getJobFromHistory('project-merge').result,
2581 'SUCCESS')
2582 self.assertEqual(self.getJobFromHistory('project-test1').result,
2583 'FAILURE')
2584 self.assertEqual(len(self.history), 2)
2585
2586 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002587 self.commitConfigUpdate('common-config',
2588 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002589 self.sched.reconfigure(self.config)
2590 self.waitUntilSettled()
2591
Paul Belanger174a8272017-03-14 13:20:10 -04002592 self.executor_server.hold_jobs_in_build = False
2593 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002594 self.waitUntilSettled()
2595
2596 self.assertEqual(self.getJobFromHistory('project-test2').result,
2597 'SUCCESS')
2598 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2599 'SUCCESS')
2600 self.assertEqual(len(self.history), 4)
2601
2602 self.assertEqual(A.data['status'], 'NEW')
2603 self.assertEqual(A.reported, 1)
2604 self.assertIn('Build succeeded', A.messages[0])
2605 # Ensure the removed job was not included in the report.
2606 self.assertNotIn('project-test1', A.messages[0])
2607
James E. Blairfe707d12015-08-05 15:18:15 -07002608 def test_live_reconfiguration_shared_queue(self):
2609 # Test that a change with a failing job which was removed from
2610 # this project but otherwise still exists in the system does
2611 # not disrupt reconfiguration.
2612
Paul Belanger174a8272017-03-14 13:20:10 -04002613 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002614
2615 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2616
Paul Belanger174a8272017-03-14 13:20:10 -04002617 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002618
2619 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2620 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002621 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002622 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002623 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002624 self.waitUntilSettled()
2625
2626 self.assertEqual(A.data['status'], 'NEW')
2627 self.assertEqual(A.reported, 0)
2628
Clint Byrum69663d42017-05-01 14:51:04 -07002629 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002630 'SUCCESS')
2631 self.assertEqual(self.getJobFromHistory(
2632 'project1-project2-integration').result, 'FAILURE')
2633 self.assertEqual(len(self.history), 2)
2634
2635 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002636 self.commitConfigUpdate(
2637 'common-config',
2638 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002639 self.sched.reconfigure(self.config)
2640 self.waitUntilSettled()
2641
Paul Belanger174a8272017-03-14 13:20:10 -04002642 self.executor_server.hold_jobs_in_build = False
2643 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002644 self.waitUntilSettled()
2645
Clint Byrum69663d42017-05-01 14:51:04 -07002646 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002647 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002648 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002649 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002650 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002651 'SUCCESS')
2652 self.assertEqual(self.getJobFromHistory(
2653 'project1-project2-integration').result, 'FAILURE')
2654 self.assertEqual(len(self.history), 4)
2655
2656 self.assertEqual(A.data['status'], 'NEW')
2657 self.assertEqual(A.reported, 1)
2658 self.assertIn('Build succeeded', A.messages[0])
2659 # Ensure the removed job was not included in the report.
2660 self.assertNotIn('project1-project2-integration', A.messages[0])
2661
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002662 def test_double_live_reconfiguration_shared_queue(self):
2663 # This was a real-world regression. A change is added to
2664 # gate; a reconfigure happens, a second change which depends
2665 # on the first is added, and a second reconfiguration happens.
2666 # Ensure that both changes merge.
2667
2668 # A failure may indicate incorrect caching or cleaning up of
2669 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002670 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002671
2672 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2673 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2674 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002675 A.addApproval('Code-Review', 2)
2676 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002677
2678 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002679 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002680 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002681 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002682 self.waitUntilSettled()
2683
2684 # Reconfigure (with only one change in the pipeline).
2685 self.sched.reconfigure(self.config)
2686 self.waitUntilSettled()
2687
2688 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002689 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002690 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002691 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002692 self.waitUntilSettled()
2693
2694 # Reconfigure (with both in the pipeline).
2695 self.sched.reconfigure(self.config)
2696 self.waitUntilSettled()
2697
Paul Belanger174a8272017-03-14 13:20:10 -04002698 self.executor_server.hold_jobs_in_build = False
2699 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002700 self.waitUntilSettled()
2701
2702 self.assertEqual(len(self.history), 8)
2703
2704 self.assertEqual(A.data['status'], 'MERGED')
2705 self.assertEqual(A.reported, 2)
2706 self.assertEqual(B.data['status'], 'MERGED')
2707 self.assertEqual(B.reported, 2)
2708
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002709 def test_live_reconfiguration_del_project(self):
2710 # Test project deletion from layout
2711 # while changes are enqueued
2712
Paul Belanger174a8272017-03-14 13:20:10 -04002713 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002714 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2715 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2716 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2717
2718 # A Depends-On: B
2719 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2720 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002721 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002722
2723 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2724 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2725 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002726 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002727 self.waitUntilSettled()
2728 self.assertEqual(len(self.builds), 5)
2729
2730 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002731 self.commitConfigUpdate(
2732 'common-config',
2733 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002734 self.sched.reconfigure(self.config)
2735 self.waitUntilSettled()
2736
2737 # Builds for C aborted, builds for A succeed,
2738 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002739 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002740 self.assertEqual(job_c.changes, '3,1')
2741 self.assertEqual(job_c.result, 'ABORTED')
2742
Paul Belanger174a8272017-03-14 13:20:10 -04002743 self.executor_server.hold_jobs_in_build = False
2744 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002745 self.waitUntilSettled()
2746
Clint Byrum0149ada2017-04-04 12:22:01 -07002747 self.assertEqual(
2748 self.getJobFromHistory('project-test1', 'org/project').changes,
2749 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002750
2751 self.assertEqual(A.data['status'], 'NEW')
2752 self.assertEqual(B.data['status'], 'NEW')
2753 self.assertEqual(C.data['status'], 'NEW')
2754 self.assertEqual(A.reported, 1)
2755 self.assertEqual(B.reported, 0)
2756 self.assertEqual(C.reported, 0)
2757
Clint Byrum0149ada2017-04-04 12:22:01 -07002758 tenant = self.sched.abide.tenants.get('tenant-one')
2759 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002760 self.assertIn('Build succeeded', A.messages[0])
2761
James E. Blair287c06d2013-07-24 10:39:30 -07002762 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002763 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002764 files = {'README': ''}
2765 self.addCommitToRepo("org/new-project", 'Initial commit',
2766 files=files, tag='init')
2767 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2768 self.commitConfigUpdate(
2769 'common-config',
2770 'layouts/delayed-repo-init.yaml')
2771 self.sched.reconfigure(self.config)
2772 self.waitUntilSettled()
2773
James E. Blair287c06d2013-07-24 10:39:30 -07002774 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2775
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002776 A.addApproval('Code-Review', 2)
2777 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002778 self.waitUntilSettled()
2779 self.assertEqual(self.getJobFromHistory('project-merge').result,
2780 'SUCCESS')
2781 self.assertEqual(self.getJobFromHistory('project-test1').result,
2782 'SUCCESS')
2783 self.assertEqual(self.getJobFromHistory('project-test2').result,
2784 'SUCCESS')
2785 self.assertEqual(A.data['status'], 'MERGED')
2786 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002787
James E. Blair9ea70072017-04-19 16:05:30 -07002788 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002789 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002790 self.init_repo("org/delete-project")
2791 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2792
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002793 A.addApproval('Code-Review', 2)
2794 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002795 self.waitUntilSettled()
2796 self.assertEqual(self.getJobFromHistory('project-merge').result,
2797 'SUCCESS')
2798 self.assertEqual(self.getJobFromHistory('project-test1').result,
2799 'SUCCESS')
2800 self.assertEqual(self.getJobFromHistory('project-test2').result,
2801 'SUCCESS')
2802 self.assertEqual(A.data['status'], 'MERGED')
2803 self.assertEqual(A.reported, 2)
2804
2805 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002806 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002807 if os.path.exists(os.path.join(self.merger_src_root, p)):
2808 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002809 if os.path.exists(os.path.join(self.executor_src_root, p)):
2810 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002811
2812 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2813
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002814 B.addApproval('Code-Review', 2)
2815 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002816 self.waitUntilSettled()
2817 self.assertEqual(self.getJobFromHistory('project-merge').result,
2818 'SUCCESS')
2819 self.assertEqual(self.getJobFromHistory('project-test1').result,
2820 'SUCCESS')
2821 self.assertEqual(self.getJobFromHistory('project-test2').result,
2822 'SUCCESS')
2823 self.assertEqual(B.data['status'], 'MERGED')
2824 self.assertEqual(B.reported, 2)
2825
James E. Blair892cca62017-08-09 11:36:58 -07002826 @simple_layout('layouts/untrusted-secrets.yaml')
2827 def test_untrusted_secrets(self):
2828 "Test untrusted secrets"
2829 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2830 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2831 self.waitUntilSettled()
2832
2833 self.assertHistory([])
2834 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002835 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002836 A.messages[0])
2837
James E. Blaire68d43f2017-04-19 14:03:30 -07002838 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002839 def test_tags(self):
2840 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002841 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2842 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2843 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2844 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2845 self.waitUntilSettled()
2846
James E. Blaire68d43f2017-04-19 14:03:30 -07002847 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002848
2849 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002850 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002851 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002852 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002853
2854 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002855 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002856 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002857
James E. Blair63bb0ef2013-07-29 17:14:51 -07002858 def test_timer(self):
2859 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002860 # This test can not use simple_layout because it must start
2861 # with a configuration which does not include a
2862 # timer-triggered job so that we have an opportunity to set
2863 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002864 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002865 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002866 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002867 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002868
Clark Boylan3ee090a2014-04-03 20:55:09 -07002869 # The pipeline triggers every second, so we should have seen
2870 # several by now.
2871 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002872 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002873
2874 self.assertEqual(len(self.builds), 2)
2875
James E. Blair63bb0ef2013-07-29 17:14:51 -07002876 port = self.webapp.server.socket.getsockname()[1]
2877
Clint Byrum5e729e92017-02-08 06:34:57 -08002878 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002879 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002880 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002881 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002882
Paul Belanger174a8272017-03-14 13:20:10 -04002883 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002884 # Stop queuing timer triggered jobs so that the assertions
2885 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002886 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002887 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002888 self.waitUntilSettled()
2889 # If APScheduler is in mid-event when we remove the job, we
2890 # can end up with one more event firing, so give it an extra
2891 # second to settle.
2892 time.sleep(1)
2893 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002894 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002895 self.waitUntilSettled()
2896
James E. Blair21037782017-07-19 11:56:55 -07002897 self.assertHistory([
2898 dict(name='project-bitrot', result='SUCCESS',
2899 ref='refs/heads/master'),
2900 dict(name='project-bitrot', result='SUCCESS',
2901 ref='refs/heads/stable'),
2902 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002903
2904 data = json.loads(data)
2905 status_jobs = set()
2906 for p in data['pipelines']:
2907 for q in p['change_queues']:
2908 for head in q['heads']:
2909 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002910 for job in change['jobs']:
2911 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002912 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002913
James E. Blair4f6033c2014-03-27 15:49:09 -07002914 def test_idle(self):
2915 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002916 # This test can not use simple_layout because it must start
2917 # with a configuration which does not include a
2918 # timer-triggered job so that we have an opportunity to set
2919 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002920 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002921
Clark Boylan3ee090a2014-04-03 20:55:09 -07002922 for x in range(1, 3):
2923 # Test that timer triggers periodic jobs even across
2924 # layout config reloads.
2925 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002926 self.commitConfigUpdate('common-config',
2927 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002928 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002929 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002930
Clark Boylan3ee090a2014-04-03 20:55:09 -07002931 # The pipeline triggers every second, so we should have seen
2932 # several by now.
2933 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002934
2935 # Stop queuing timer triggered jobs so that the assertions
2936 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002937 self.commitConfigUpdate('common-config',
2938 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002939 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002940 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002941 # If APScheduler is in mid-event when we remove the job,
2942 # we can end up with one more event firing, so give it an
2943 # extra second to settle.
2944 time.sleep(1)
2945 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002946 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002947 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002948 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002949 self.waitUntilSettled()
2950 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002951 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002952
James E. Blaircc324192017-04-19 14:36:08 -07002953 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002954 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002955 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2956 self.waitUntilSettled()
2957
2958 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2959 self.waitUntilSettled()
2960
James E. Blairff80a2f2013-12-27 13:24:06 -08002961 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002962
2963 # A.messages only holds what FakeGerrit places in it. Thus we
2964 # work on the knowledge of what the first message should be as
2965 # it is only configured to go to SMTP.
2966
2967 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002968 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002969 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002970 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002971 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002972 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002973
2974 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002975 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002976 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002977 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002978 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002979 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002980
James E. Blaire5910202013-12-27 09:50:31 -08002981 def test_timer_smtp(self):
2982 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002983 # This test can not use simple_layout because it must start
2984 # with a configuration which does not include a
2985 # timer-triggered job so that we have an opportunity to set
2986 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002987 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002988 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002989 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002990
Clark Boylan3ee090a2014-04-03 20:55:09 -07002991 # The pipeline triggers every second, so we should have seen
2992 # several by now.
2993 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002994 self.waitUntilSettled()
2995
Clark Boylan3ee090a2014-04-03 20:55:09 -07002996 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002997 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002998 self.waitUntilSettled()
2999 self.assertEqual(len(self.history), 2)
3000
James E. Blaire5910202013-12-27 09:50:31 -08003001 self.assertEqual(self.getJobFromHistory(
3002 'project-bitrot-stable-old').result, 'SUCCESS')
3003 self.assertEqual(self.getJobFromHistory(
3004 'project-bitrot-stable-older').result, 'SUCCESS')
3005
James E. Blairff80a2f2013-12-27 13:24:06 -08003006 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003007
3008 # A.messages only holds what FakeGerrit places in it. Thus we
3009 # work on the knowledge of what the first message should be as
3010 # it is only configured to go to SMTP.
3011
3012 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003013 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003014 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003015 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003016 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003017 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003018
Clark Boylan3ee090a2014-04-03 20:55:09 -07003019 # Stop queuing timer triggered jobs and let any that may have
3020 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003021 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003022 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003023 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003024 # If APScheduler is in mid-event when we remove the job, we
3025 # can end up with one more event firing, so give it an extra
3026 # second to settle.
3027 time.sleep(1)
3028 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003029 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003030 self.waitUntilSettled()
3031
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003032 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003033 def test_timer_sshkey(self):
3034 "Test that a periodic job can setup SSH key authentication"
3035 self.worker.hold_jobs_in_build = True
3036 self.config.set('zuul', 'layout_config',
3037 'tests/fixtures/layout-timer.yaml')
3038 self.sched.reconfigure(self.config)
3039 self.registerJobs()
3040
3041 # The pipeline triggers every second, so we should have seen
3042 # several by now.
3043 time.sleep(5)
3044 self.waitUntilSettled()
3045
3046 self.assertEqual(len(self.builds), 2)
3047
3048 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3049 self.assertTrue(os.path.isfile(ssh_wrapper))
3050 with open(ssh_wrapper) as f:
3051 ssh_wrapper_content = f.read()
3052 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3053 # In the unit tests Merger runs in the same process,
3054 # so we see its' environment variables
3055 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3056
3057 self.worker.release('.*')
3058 self.waitUntilSettled()
3059 self.assertEqual(len(self.history), 2)
3060
3061 self.assertEqual(self.getJobFromHistory(
3062 'project-bitrot-stable-old').result, 'SUCCESS')
3063 self.assertEqual(self.getJobFromHistory(
3064 'project-bitrot-stable-older').result, 'SUCCESS')
3065
3066 # Stop queuing timer triggered jobs and let any that may have
3067 # queued through so that end of test assertions pass.
3068 self.config.set('zuul', 'layout_config',
3069 'tests/fixtures/layout-no-timer.yaml')
3070 self.sched.reconfigure(self.config)
3071 self.registerJobs()
3072 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003073 # If APScheduler is in mid-event when we remove the job, we
3074 # can end up with one more event firing, so give it an extra
3075 # second to settle.
3076 time.sleep(1)
3077 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003078 self.worker.release('.*')
3079 self.waitUntilSettled()
3080
James E. Blair91e34592015-07-31 16:45:59 -07003081 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003082 "Test that the RPC client can enqueue a change"
3083 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003084 A.addApproval('Code-Review', 2)
3085 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003086
3087 client = zuul.rpcclient.RPCClient('127.0.0.1',
3088 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003089 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003090 r = client.enqueue(tenant='tenant-one',
3091 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003092 project='org/project',
3093 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003094 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003095 self.waitUntilSettled()
3096 self.assertEqual(self.getJobFromHistory('project-merge').result,
3097 'SUCCESS')
3098 self.assertEqual(self.getJobFromHistory('project-test1').result,
3099 'SUCCESS')
3100 self.assertEqual(self.getJobFromHistory('project-test2').result,
3101 'SUCCESS')
3102 self.assertEqual(A.data['status'], 'MERGED')
3103 self.assertEqual(A.reported, 2)
3104 self.assertEqual(r, True)
3105
James E. Blair91e34592015-07-31 16:45:59 -07003106 def test_client_enqueue_ref(self):
3107 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003108 p = "review.example.com/org/project"
3109 upstream = self.getUpstreamRepos([p])
3110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3111 A.setMerged()
3112 A_commit = str(upstream[p].commit('master'))
3113 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003114
3115 client = zuul.rpcclient.RPCClient('127.0.0.1',
3116 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003117 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003118 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003119 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003120 pipeline='post',
3121 project='org/project',
3122 trigger='gerrit',
3123 ref='master',
3124 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003125 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003126 self.waitUntilSettled()
3127 job_names = [x.name for x in self.history]
3128 self.assertEqual(len(self.history), 1)
3129 self.assertIn('project-post', job_names)
3130 self.assertEqual(r, True)
3131
James E. Blairad28e912013-11-27 10:43:22 -08003132 def test_client_enqueue_negative(self):
3133 "Test that the RPC client returns errors"
3134 client = zuul.rpcclient.RPCClient('127.0.0.1',
3135 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003136 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003137 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003138 "Invalid tenant"):
3139 r = client.enqueue(tenant='tenant-foo',
3140 pipeline='gate',
3141 project='org/project',
3142 trigger='gerrit',
3143 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003144 self.assertEqual(r, False)
3145
3146 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003147 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003148 r = client.enqueue(tenant='tenant-one',
3149 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003150 project='project-does-not-exist',
3151 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003152 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003153 self.assertEqual(r, False)
3154
3155 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3156 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003157 r = client.enqueue(tenant='tenant-one',
3158 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003159 project='org/project',
3160 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003161 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003162 self.assertEqual(r, False)
3163
3164 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3165 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003166 r = client.enqueue(tenant='tenant-one',
3167 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003168 project='org/project',
3169 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003170 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003171 self.assertEqual(r, False)
3172
3173 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3174 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003175 r = client.enqueue(tenant='tenant-one',
3176 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003177 project='org/project',
3178 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003179 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003180 self.assertEqual(r, False)
3181
3182 self.waitUntilSettled()
3183 self.assertEqual(len(self.history), 0)
3184 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003185
3186 def test_client_promote(self):
3187 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003188 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003189 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3190 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3191 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003192 A.addApproval('Code-Review', 2)
3193 B.addApproval('Code-Review', 2)
3194 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003195
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003196 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3197 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3198 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003199
3200 self.waitUntilSettled()
3201
Paul Belangerbaca3132016-11-04 12:49:54 -04003202 tenant = self.sched.abide.tenants.get('tenant-one')
3203 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003204 enqueue_times = {}
3205 for item in items:
3206 enqueue_times[str(item.change)] = item.enqueue_time
3207
James E. Blair36658cf2013-12-06 17:53:48 -08003208 client = zuul.rpcclient.RPCClient('127.0.0.1',
3209 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003210 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003211 r = client.promote(tenant='tenant-one',
3212 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003213 change_ids=['2,1', '3,1'])
3214
Sean Daguef39b9ca2014-01-10 21:34:35 -05003215 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003216 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003217 for item in items:
3218 self.assertEqual(
3219 enqueue_times[str(item.change)], item.enqueue_time)
3220
James E. Blair78acec92014-02-06 07:11:32 -08003221 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003222 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003223 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003224 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003225 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003226 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003227 self.waitUntilSettled()
3228
3229 self.assertEqual(len(self.builds), 6)
3230 self.assertEqual(self.builds[0].name, 'project-test1')
3231 self.assertEqual(self.builds[1].name, 'project-test2')
3232 self.assertEqual(self.builds[2].name, 'project-test1')
3233 self.assertEqual(self.builds[3].name, 'project-test2')
3234 self.assertEqual(self.builds[4].name, 'project-test1')
3235 self.assertEqual(self.builds[5].name, 'project-test2')
3236
Paul Belangerbaca3132016-11-04 12:49:54 -04003237 self.assertTrue(self.builds[0].hasChanges(B))
3238 self.assertFalse(self.builds[0].hasChanges(A))
3239 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003240
Paul Belangerbaca3132016-11-04 12:49:54 -04003241 self.assertTrue(self.builds[2].hasChanges(B))
3242 self.assertTrue(self.builds[2].hasChanges(C))
3243 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003244
Paul Belangerbaca3132016-11-04 12:49:54 -04003245 self.assertTrue(self.builds[4].hasChanges(B))
3246 self.assertTrue(self.builds[4].hasChanges(C))
3247 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003248
Paul Belanger174a8272017-03-14 13:20:10 -04003249 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003250 self.waitUntilSettled()
3251
3252 self.assertEqual(A.data['status'], 'MERGED')
3253 self.assertEqual(A.reported, 2)
3254 self.assertEqual(B.data['status'], 'MERGED')
3255 self.assertEqual(B.reported, 2)
3256 self.assertEqual(C.data['status'], 'MERGED')
3257 self.assertEqual(C.reported, 2)
3258
James E. Blair36658cf2013-12-06 17:53:48 -08003259 self.assertEqual(r, True)
3260
3261 def test_client_promote_dependent(self):
3262 "Test that the RPC client can promote a dependent change"
3263 # C (depends on B) -> B -> A ; then promote C to get:
3264 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003265 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003266 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3267 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3268 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3269
3270 C.setDependsOn(B, 1)
3271
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003272 A.addApproval('Code-Review', 2)
3273 B.addApproval('Code-Review', 2)
3274 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003275
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003276 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3277 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3278 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003279
3280 self.waitUntilSettled()
3281
3282 client = zuul.rpcclient.RPCClient('127.0.0.1',
3283 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003284 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003285 r = client.promote(tenant='tenant-one',
3286 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003287 change_ids=['3,1'])
3288
James E. Blair78acec92014-02-06 07:11:32 -08003289 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003290 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003291 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003292 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003293 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003294 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003295 self.waitUntilSettled()
3296
3297 self.assertEqual(len(self.builds), 6)
3298 self.assertEqual(self.builds[0].name, 'project-test1')
3299 self.assertEqual(self.builds[1].name, 'project-test2')
3300 self.assertEqual(self.builds[2].name, 'project-test1')
3301 self.assertEqual(self.builds[3].name, 'project-test2')
3302 self.assertEqual(self.builds[4].name, 'project-test1')
3303 self.assertEqual(self.builds[5].name, 'project-test2')
3304
Paul Belangerbaca3132016-11-04 12:49:54 -04003305 self.assertTrue(self.builds[0].hasChanges(B))
3306 self.assertFalse(self.builds[0].hasChanges(A))
3307 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003308
Paul Belangerbaca3132016-11-04 12:49:54 -04003309 self.assertTrue(self.builds[2].hasChanges(B))
3310 self.assertTrue(self.builds[2].hasChanges(C))
3311 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003312
Paul Belangerbaca3132016-11-04 12:49:54 -04003313 self.assertTrue(self.builds[4].hasChanges(B))
3314 self.assertTrue(self.builds[4].hasChanges(C))
3315 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003316
Paul Belanger174a8272017-03-14 13:20:10 -04003317 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003318 self.waitUntilSettled()
3319
3320 self.assertEqual(A.data['status'], 'MERGED')
3321 self.assertEqual(A.reported, 2)
3322 self.assertEqual(B.data['status'], 'MERGED')
3323 self.assertEqual(B.reported, 2)
3324 self.assertEqual(C.data['status'], 'MERGED')
3325 self.assertEqual(C.reported, 2)
3326
James E. Blair36658cf2013-12-06 17:53:48 -08003327 self.assertEqual(r, True)
3328
3329 def test_client_promote_negative(self):
3330 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003331 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003332 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003333 A.addApproval('Code-Review', 2)
3334 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003335 self.waitUntilSettled()
3336
3337 client = zuul.rpcclient.RPCClient('127.0.0.1',
3338 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003339 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003340
3341 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003342 r = client.promote(tenant='tenant-one',
3343 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003344 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003345 self.assertEqual(r, False)
3346
3347 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003348 r = client.promote(tenant='tenant-one',
3349 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003350 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003351 self.assertEqual(r, False)
3352
Paul Belanger174a8272017-03-14 13:20:10 -04003353 self.executor_server.hold_jobs_in_build = False
3354 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003355 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003356
James E. Blaircc324192017-04-19 14:36:08 -07003357 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003358 def test_queue_rate_limiting(self):
3359 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003360 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003361 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3362 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3363 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3364
3365 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003366 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003367
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003368 A.addApproval('Code-Review', 2)
3369 B.addApproval('Code-Review', 2)
3370 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003371
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003372 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3373 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3374 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003375 self.waitUntilSettled()
3376
3377 # Only A and B will have their merge jobs queued because
3378 # window is 2.
3379 self.assertEqual(len(self.builds), 2)
3380 self.assertEqual(self.builds[0].name, 'project-merge')
3381 self.assertEqual(self.builds[1].name, 'project-merge')
3382
James E. Blair732ee862017-04-21 07:49:26 -07003383 # Release the merge jobs one at a time.
3384 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003385 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003386 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003387 self.waitUntilSettled()
3388
3389 # Only A and B will have their test jobs queued because
3390 # window is 2.
3391 self.assertEqual(len(self.builds), 4)
3392 self.assertEqual(self.builds[0].name, 'project-test1')
3393 self.assertEqual(self.builds[1].name, 'project-test2')
3394 self.assertEqual(self.builds[2].name, 'project-test1')
3395 self.assertEqual(self.builds[3].name, 'project-test2')
3396
Paul Belanger174a8272017-03-14 13:20:10 -04003397 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003398 self.waitUntilSettled()
3399
James E. Blaircc324192017-04-19 14:36:08 -07003400 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003401 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003402 # A failed so window is reduced by 1 to 1.
3403 self.assertEqual(queue.window, 1)
3404 self.assertEqual(queue.window_floor, 1)
3405 self.assertEqual(A.data['status'], 'NEW')
3406
3407 # Gate is reset and only B's merge job is queued because
3408 # window shrunk to 1.
3409 self.assertEqual(len(self.builds), 1)
3410 self.assertEqual(self.builds[0].name, 'project-merge')
3411
Paul Belanger174a8272017-03-14 13:20:10 -04003412 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003413 self.waitUntilSettled()
3414
3415 # Only B's test jobs are queued because window is still 1.
3416 self.assertEqual(len(self.builds), 2)
3417 self.assertEqual(self.builds[0].name, 'project-test1')
3418 self.assertEqual(self.builds[1].name, 'project-test2')
3419
Paul Belanger174a8272017-03-14 13:20:10 -04003420 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003421 self.waitUntilSettled()
3422
3423 # B was successfully merged so window is increased to 2.
3424 self.assertEqual(queue.window, 2)
3425 self.assertEqual(queue.window_floor, 1)
3426 self.assertEqual(B.data['status'], 'MERGED')
3427
3428 # Only C is left and its merge job is queued.
3429 self.assertEqual(len(self.builds), 1)
3430 self.assertEqual(self.builds[0].name, 'project-merge')
3431
Paul Belanger174a8272017-03-14 13:20:10 -04003432 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003433 self.waitUntilSettled()
3434
3435 # After successful merge job the test jobs for C are queued.
3436 self.assertEqual(len(self.builds), 2)
3437 self.assertEqual(self.builds[0].name, 'project-test1')
3438 self.assertEqual(self.builds[1].name, 'project-test2')
3439
Paul Belanger174a8272017-03-14 13:20:10 -04003440 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003441 self.waitUntilSettled()
3442
3443 # C successfully merged so window is bumped to 3.
3444 self.assertEqual(queue.window, 3)
3445 self.assertEqual(queue.window_floor, 1)
3446 self.assertEqual(C.data['status'], 'MERGED')
3447
James E. Blaircc324192017-04-19 14:36:08 -07003448 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003449 def test_queue_rate_limiting_dependent(self):
3450 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003451 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003452 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3453 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3454 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3455
3456 B.setDependsOn(A, 1)
3457
Paul Belanger174a8272017-03-14 13:20:10 -04003458 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003459
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003460 A.addApproval('Code-Review', 2)
3461 B.addApproval('Code-Review', 2)
3462 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003463
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003464 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3465 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3466 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003467 self.waitUntilSettled()
3468
3469 # Only A and B will have their merge jobs queued because
3470 # window is 2.
3471 self.assertEqual(len(self.builds), 2)
3472 self.assertEqual(self.builds[0].name, 'project-merge')
3473 self.assertEqual(self.builds[1].name, 'project-merge')
3474
James E. Blairf4a5f022017-04-18 14:01:10 -07003475 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003476
3477 # Only A and B will have their test jobs queued because
3478 # window is 2.
3479 self.assertEqual(len(self.builds), 4)
3480 self.assertEqual(self.builds[0].name, 'project-test1')
3481 self.assertEqual(self.builds[1].name, 'project-test2')
3482 self.assertEqual(self.builds[2].name, 'project-test1')
3483 self.assertEqual(self.builds[3].name, 'project-test2')
3484
Paul Belanger174a8272017-03-14 13:20:10 -04003485 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
James E. Blaircc324192017-04-19 14:36:08 -07003488 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003489 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003490 # A failed so window is reduced by 1 to 1.
3491 self.assertEqual(queue.window, 1)
3492 self.assertEqual(queue.window_floor, 1)
3493 self.assertEqual(A.data['status'], 'NEW')
3494 self.assertEqual(B.data['status'], 'NEW')
3495
3496 # Gate is reset and only C's merge job is queued because
3497 # window shrunk to 1 and A and B were dequeued.
3498 self.assertEqual(len(self.builds), 1)
3499 self.assertEqual(self.builds[0].name, 'project-merge')
3500
James E. Blairf4a5f022017-04-18 14:01:10 -07003501 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003502
3503 # Only C's test jobs are queued because window is still 1.
3504 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003505 builds = self.getSortedBuilds()
3506 self.assertEqual(builds[0].name, 'project-test1')
3507 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003508
Paul Belanger174a8272017-03-14 13:20:10 -04003509 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003510 self.waitUntilSettled()
3511
3512 # C was successfully merged so window is increased to 2.
3513 self.assertEqual(queue.window, 2)
3514 self.assertEqual(queue.window_floor, 1)
3515 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003516
3517 def test_worker_update_metadata(self):
3518 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003519 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003520
3521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003522 A.addApproval('Code-Review', 2)
3523 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003524 self.waitUntilSettled()
3525
Clint Byrumd4de79a2017-05-27 11:39:13 -07003526 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003527
3528 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003529 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003530
3531 start = time.time()
3532 while True:
3533 if time.time() - start > 10:
3534 raise Exception("Timeout waiting for gearman server to report "
3535 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003536 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003537 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003538 break
3539 else:
3540 time.sleep(0)
3541
3542 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003543 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003544
Paul Belanger174a8272017-03-14 13:20:10 -04003545 self.executor_server.hold_jobs_in_build = False
3546 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003547 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003548
James E. Blaircc324192017-04-19 14:36:08 -07003549 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003550 def test_footer_message(self):
3551 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003552 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003553 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003554 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003555 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003556 self.waitUntilSettled()
3557
3558 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003559 B.addApproval('Code-Review', 2)
3560 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003561 self.waitUntilSettled()
3562
3563 self.assertEqual(2, len(self.smtp_messages))
3564
Adam Gandelman82258fd2017-02-07 16:51:58 -08003565 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003566Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003567http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003568
Adam Gandelman82258fd2017-02-07 16:51:58 -08003569 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003570For CI problems and help debugging, contact ci@example.org"""
3571
Adam Gandelman82258fd2017-02-07 16:51:58 -08003572 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3573 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3574 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3575 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003576
Tobias Henkel9842bd72017-05-16 13:40:03 +02003577 @simple_layout('layouts/unmanaged-project.yaml')
3578 def test_unmanaged_project_start_message(self):
3579 "Test start reporting is not done for unmanaged projects."
3580 self.init_repo("org/project", tag='init')
3581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3582 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3583 self.waitUntilSettled()
3584
3585 self.assertEqual(0, len(A.messages))
3586
Clint Byrumb9de8be2017-05-27 13:49:19 -07003587 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003588 def test_merge_failure_reporters(self):
3589 """Check that the config is set up correctly"""
3590
Clint Byrumb9de8be2017-05-27 13:49:19 -07003591 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003592 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003593 "Merge Failed.\n\nThis change or one of its cross-repo "
3594 "dependencies was unable to be automatically merged with the "
3595 "current state of its repository. Please rebase the change and "
3596 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003597 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003598 self.assertEqual(
3599 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003600 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003601
3602 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003603 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003604 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003605 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003606
3607 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003608 tenant.layout.pipelines['check'].merge_failure_actions[0],
3609 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003610
3611 self.assertTrue(
3612 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003613 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003614 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003615 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3616 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003617 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003618 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003619 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003620 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003621 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003622 gerritreporter.GerritReporter) and
3623 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003624 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003625 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003626 )
3627 )
3628
James E. Blairec056492016-07-22 09:45:56 -07003629 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003630 def test_merge_failure_reports(self):
3631 """Check that when a change fails to merge the correct message is sent
3632 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003633 self.updateConfigLayout(
3634 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003635 self.sched.reconfigure(self.config)
3636 self.registerJobs()
3637
3638 # Check a test failure isn't reported to SMTP
3639 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003640 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003641 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003642 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003643 self.waitUntilSettled()
3644
3645 self.assertEqual(3, len(self.history)) # 3 jobs
3646 self.assertEqual(0, len(self.smtp_messages))
3647
3648 # Check a merge failure is reported to SMTP
3649 # B should be merged, but C will conflict with B
3650 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3651 B.addPatchset(['conflict'])
3652 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3653 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003654 B.addApproval('Code-Review', 2)
3655 C.addApproval('Code-Review', 2)
3656 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3657 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003658 self.waitUntilSettled()
3659
3660 self.assertEqual(6, len(self.history)) # A and B jobs
3661 self.assertEqual(1, len(self.smtp_messages))
3662 self.assertEqual('The merge failed! For more information...',
3663 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003664
James E. Blairec056492016-07-22 09:45:56 -07003665 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003666 def test_default_merge_failure_reports(self):
3667 """Check that the default merge failure reports are correct."""
3668
3669 # A should report success, B should report merge failure.
3670 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3671 A.addPatchset(['conflict'])
3672 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3673 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003674 A.addApproval('Code-Review', 2)
3675 B.addApproval('Code-Review', 2)
3676 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3677 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003678 self.waitUntilSettled()
3679
3680 self.assertEqual(3, len(self.history)) # A jobs
3681 self.assertEqual(A.reported, 2)
3682 self.assertEqual(B.reported, 2)
3683 self.assertEqual(A.data['status'], 'MERGED')
3684 self.assertEqual(B.data['status'], 'NEW')
3685 self.assertIn('Build succeeded', A.messages[1])
3686 self.assertIn('Merge Failed', B.messages[1])
3687 self.assertIn('automatically merged', B.messages[1])
3688 self.assertNotIn('logs.example.com', B.messages[1])
3689 self.assertNotIn('SKIPPED', B.messages[1])
3690
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003691 def test_client_get_running_jobs(self):
3692 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003693 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003695 A.addApproval('Code-Review', 2)
3696 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003697 self.waitUntilSettled()
3698
3699 client = zuul.rpcclient.RPCClient('127.0.0.1',
3700 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003701 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003702
3703 # Wait for gearman server to send the initial workData back to zuul
3704 start = time.time()
3705 while True:
3706 if time.time() - start > 10:
3707 raise Exception("Timeout waiting for gearman server to report "
3708 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003709 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003710 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003711 break
3712 else:
3713 time.sleep(0)
3714
3715 running_items = client.get_running_jobs()
3716
3717 self.assertEqual(1, len(running_items))
3718 running_item = running_items[0]
3719 self.assertEqual([], running_item['failing_reasons'])
3720 self.assertEqual([], running_item['items_behind'])
3721 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003722 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003723 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003724 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003725 self.assertEqual(True, running_item['active'])
3726 self.assertEqual('1,1', running_item['id'])
3727
3728 self.assertEqual(3, len(running_item['jobs']))
3729 for job in running_item['jobs']:
3730 if job['name'] == 'project-merge':
3731 self.assertEqual('project-merge', job['name'])
3732 self.assertEqual('gate', job['pipeline'])
3733 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003734 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003735 'static/stream.html?uuid={uuid}&logfile=console.log'
3736 .format(uuid=job['uuid']), job['url'])
3737 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003738 'finger://{hostname}/{uuid}'.format(
3739 hostname=self.executor_server.hostname,
3740 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003741 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003742 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003743 self.assertEqual(False, job['canceled'])
3744 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003745 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003746 self.assertEqual('gate', job['pipeline'])
3747 break
3748
Paul Belanger174a8272017-03-14 13:20:10 -04003749 self.executor_server.hold_jobs_in_build = False
3750 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003751 self.waitUntilSettled()
3752
3753 running_items = client.get_running_jobs()
3754 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003755
James E. Blairb70e55a2017-04-19 12:57:02 -07003756 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003757 def test_nonvoting_pipeline(self):
3758 "Test that a nonvoting pipeline (experimental) can still report"
3759
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003760 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3761 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003762 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3763 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003764
3765 self.assertEqual(self.getJobFromHistory('project-merge').result,
3766 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003767 self.assertEqual(
3768 self.getJobFromHistory('experimental-project-test').result,
3769 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003770 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003771
3772 def test_crd_gate(self):
3773 "Test cross-repo dependencies"
3774 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3775 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003776 A.addApproval('Code-Review', 2)
3777 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003778
3779 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3780 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3781 AM2.setMerged()
3782 AM1.setMerged()
3783
3784 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3785 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3786 BM2.setMerged()
3787 BM1.setMerged()
3788
3789 # A -> AM1 -> AM2
3790 # B -> BM1 -> BM2
3791 # A Depends-On: B
3792 # M2 is here to make sure it is never queried. If it is, it
3793 # means zuul is walking down the entire history of merged
3794 # changes.
3795
3796 B.setDependsOn(BM1, 1)
3797 BM1.setDependsOn(BM2, 1)
3798
3799 A.setDependsOn(AM1, 1)
3800 AM1.setDependsOn(AM2, 1)
3801
3802 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3803 A.subject, B.data['id'])
3804
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003805 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003806 self.waitUntilSettled()
3807
3808 self.assertEqual(A.data['status'], 'NEW')
3809 self.assertEqual(B.data['status'], 'NEW')
3810
Paul Belanger5dccbe72016-11-14 11:17:37 -05003811 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003812 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003813
Paul Belanger174a8272017-03-14 13:20:10 -04003814 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003815 B.addApproval('Approved', 1)
3816 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003817 self.waitUntilSettled()
3818
Paul Belanger174a8272017-03-14 13:20:10 -04003819 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003820 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003821 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003822 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003823 self.executor_server.hold_jobs_in_build = False
3824 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003825 self.waitUntilSettled()
3826
3827 self.assertEqual(AM2.queried, 0)
3828 self.assertEqual(BM2.queried, 0)
3829 self.assertEqual(A.data['status'], 'MERGED')
3830 self.assertEqual(B.data['status'], 'MERGED')
3831 self.assertEqual(A.reported, 2)
3832 self.assertEqual(B.reported, 2)
3833
Paul Belanger5dccbe72016-11-14 11:17:37 -05003834 changes = self.getJobFromHistory(
3835 'project-merge', 'org/project1').changes
3836 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003837
3838 def test_crd_branch(self):
3839 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003840
3841 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003842 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3843 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003844 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3845 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3846 status='ABANDONED')
3847 C1.data['id'] = B.data['id']
3848 C2.data['id'] = B.data['id']
3849
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003850 A.addApproval('Code-Review', 2)
3851 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003852 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003853
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003854 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003855 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3856 A.subject, B.data['id'])
3857
Paul Belanger174a8272017-03-14 13:20:10 -04003858 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003859 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003860 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003861 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003862 self.waitUntilSettled()
3863
Paul Belanger174a8272017-03-14 13:20:10 -04003864 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003865 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003866 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003867 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003868 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003869 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003870 self.executor_server.hold_jobs_in_build = False
3871 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003872 self.waitUntilSettled()
3873
3874 self.assertEqual(A.data['status'], 'MERGED')
3875 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003876 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08003877 self.assertEqual(A.reported, 2)
3878 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003879 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003880
Paul Belanger6379db12016-11-14 13:57:54 -05003881 changes = self.getJobFromHistory(
3882 'project-merge', 'org/project1').changes
3883 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003884
3885 def test_crd_multiline(self):
3886 "Test multiple depends-on lines in commit"
3887 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3888 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3889 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003890 A.addApproval('Code-Review', 2)
3891 B.addApproval('Code-Review', 2)
3892 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003893
3894 # A Depends-On: B+C
3895 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3896 A.subject, B.data['id'], C.data['id'])
3897
Paul Belanger174a8272017-03-14 13:20:10 -04003898 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003899 B.addApproval('Approved', 1)
3900 C.addApproval('Approved', 1)
3901 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003902 self.waitUntilSettled()
3903
Paul Belanger174a8272017-03-14 13:20:10 -04003904 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003905 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003906 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003907 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003908 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003909 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003910 self.executor_server.hold_jobs_in_build = False
3911 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003912 self.waitUntilSettled()
3913
3914 self.assertEqual(A.data['status'], 'MERGED')
3915 self.assertEqual(B.data['status'], 'MERGED')
3916 self.assertEqual(C.data['status'], 'MERGED')
3917 self.assertEqual(A.reported, 2)
3918 self.assertEqual(B.reported, 2)
3919 self.assertEqual(C.reported, 2)
3920
Paul Belanger5dccbe72016-11-14 11:17:37 -05003921 changes = self.getJobFromHistory(
3922 'project-merge', 'org/project1').changes
3923 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003924
3925 def test_crd_unshared_gate(self):
3926 "Test cross-repo dependencies in unshared gate queues"
3927 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3928 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003929 A.addApproval('Code-Review', 2)
3930 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003931
3932 # A Depends-On: B
3933 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3934 A.subject, B.data['id'])
3935
3936 # A and B do not share a queue, make sure that A is unable to
3937 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003938 B.addApproval('Approved', 1)
3939 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003940 self.waitUntilSettled()
3941
3942 self.assertEqual(A.data['status'], 'NEW')
3943 self.assertEqual(B.data['status'], 'NEW')
3944 self.assertEqual(A.reported, 0)
3945 self.assertEqual(B.reported, 0)
3946 self.assertEqual(len(self.history), 0)
3947
3948 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003949 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003950 self.waitUntilSettled()
3951
3952 self.assertEqual(B.data['status'], 'MERGED')
3953 self.assertEqual(B.reported, 2)
3954
3955 # Now that B is merged, A should be able to be enqueued and
3956 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003957 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003958 self.waitUntilSettled()
3959
3960 self.assertEqual(A.data['status'], 'MERGED')
3961 self.assertEqual(A.reported, 2)
3962
James E. Blair96698e22015-04-02 07:48:21 -07003963 def test_crd_gate_reverse(self):
3964 "Test reverse cross-repo dependencies"
3965 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3966 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003967 A.addApproval('Code-Review', 2)
3968 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003969
3970 # A Depends-On: B
3971
3972 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3973 A.subject, B.data['id'])
3974
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003975 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003976 self.waitUntilSettled()
3977
3978 self.assertEqual(A.data['status'], 'NEW')
3979 self.assertEqual(B.data['status'], 'NEW')
3980
Paul Belanger174a8272017-03-14 13:20:10 -04003981 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003982 A.addApproval('Approved', 1)
3983 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003984 self.waitUntilSettled()
3985
Paul Belanger174a8272017-03-14 13:20:10 -04003986 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003987 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003988 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003989 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003990 self.executor_server.hold_jobs_in_build = False
3991 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003992 self.waitUntilSettled()
3993
3994 self.assertEqual(A.data['status'], 'MERGED')
3995 self.assertEqual(B.data['status'], 'MERGED')
3996 self.assertEqual(A.reported, 2)
3997 self.assertEqual(B.reported, 2)
3998
Paul Belanger5dccbe72016-11-14 11:17:37 -05003999 changes = self.getJobFromHistory(
4000 'project-merge', 'org/project1').changes
4001 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004002
James E. Blair5ee24252014-12-30 10:12:29 -08004003 def test_crd_cycle(self):
4004 "Test cross-repo dependency cycles"
4005 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4006 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004007 A.addApproval('Code-Review', 2)
4008 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004009
4010 # A -> B -> A (via commit-depends)
4011
4012 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4013 A.subject, B.data['id'])
4014 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4015 B.subject, A.data['id'])
4016
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004017 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004018 self.waitUntilSettled()
4019
4020 self.assertEqual(A.reported, 0)
4021 self.assertEqual(B.reported, 0)
4022 self.assertEqual(A.data['status'], 'NEW')
4023 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004024
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004025 def test_crd_gate_unknown(self):
4026 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004027 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004028 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4029 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004030 A.addApproval('Code-Review', 2)
4031 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004032
4033 # A Depends-On: B
4034 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4035 A.subject, B.data['id'])
4036
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004037 B.addApproval('Approved', 1)
4038 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004039 self.waitUntilSettled()
4040
4041 # Unknown projects cannot share a queue with any other
4042 # since they don't have common jobs with any other (they have no jobs).
4043 # Changes which depend on unknown project changes
4044 # should not be processed in dependent pipeline
4045 self.assertEqual(A.data['status'], 'NEW')
4046 self.assertEqual(B.data['status'], 'NEW')
4047 self.assertEqual(A.reported, 0)
4048 self.assertEqual(B.reported, 0)
4049 self.assertEqual(len(self.history), 0)
4050
James E. Blair7720b402017-06-07 08:51:49 -07004051 # Simulate change B being gated outside this layout Set the
4052 # change merged before submitting the event so that when the
4053 # event triggers a gerrit query to update the change, we get
4054 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004055 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004056 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004057 self.waitUntilSettled()
4058 self.assertEqual(len(self.history), 0)
4059
4060 # Now that B is merged, A should be able to be enqueued and
4061 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004062 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004063 self.waitUntilSettled()
4064
4065 self.assertEqual(A.data['status'], 'MERGED')
4066 self.assertEqual(A.reported, 2)
4067 self.assertEqual(B.data['status'], 'MERGED')
4068 self.assertEqual(B.reported, 0)
4069
James E. Blairbfb8e042014-12-30 17:01:44 -08004070 def test_crd_check(self):
4071 "Test cross-repo dependencies in independent pipelines"
4072
Paul Belanger174a8272017-03-14 13:20:10 -04004073 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004074 self.gearman_server.hold_jobs_in_queue = True
4075 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4076 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4077
4078 # A Depends-On: B
4079 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4080 A.subject, B.data['id'])
4081
4082 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4083 self.waitUntilSettled()
4084
James E. Blairbfb8e042014-12-30 17:01:44 -08004085 self.gearman_server.hold_jobs_in_queue = False
4086 self.gearman_server.release()
4087 self.waitUntilSettled()
4088
Paul Belanger174a8272017-03-14 13:20:10 -04004089 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004090 self.waitUntilSettled()
4091
James E. Blair2a535672017-04-27 12:03:15 -07004092 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004093
Paul Belanger174a8272017-03-14 13:20:10 -04004094 self.executor_server.hold_jobs_in_build = False
4095 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004096 self.waitUntilSettled()
4097
James E. Blairbfb8e042014-12-30 17:01:44 -08004098 self.assertEqual(A.data['status'], 'NEW')
4099 self.assertEqual(B.data['status'], 'NEW')
4100 self.assertEqual(A.reported, 1)
4101 self.assertEqual(B.reported, 0)
4102
4103 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004104 tenant = self.sched.abide.tenants.get('tenant-one')
4105 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004106
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004107 def test_crd_check_git_depends(self):
4108 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004109 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004110 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4111 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4112
4113 # Add two git-dependent changes and make sure they both report
4114 # success.
4115 B.setDependsOn(A, 1)
4116 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4117 self.waitUntilSettled()
4118 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4119 self.waitUntilSettled()
4120
James E. Blairb8c16472015-05-05 14:55:26 -07004121 self.orderedRelease()
4122 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004123 self.waitUntilSettled()
4124
4125 self.assertEqual(A.data['status'], 'NEW')
4126 self.assertEqual(B.data['status'], 'NEW')
4127 self.assertEqual(A.reported, 1)
4128 self.assertEqual(B.reported, 1)
4129
4130 self.assertEqual(self.history[0].changes, '1,1')
4131 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004132 tenant = self.sched.abide.tenants.get('tenant-one')
4133 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004134
4135 self.assertIn('Build succeeded', A.messages[0])
4136 self.assertIn('Build succeeded', B.messages[0])
4137
4138 def test_crd_check_duplicate(self):
4139 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004140 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004141 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4142 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004143 tenant = self.sched.abide.tenants.get('tenant-one')
4144 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004145
4146 # Add two git-dependent changes...
4147 B.setDependsOn(A, 1)
4148 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4149 self.waitUntilSettled()
4150 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4151
4152 # ...make sure the live one is not duplicated...
4153 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4154 self.waitUntilSettled()
4155 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4156
4157 # ...but the non-live one is able to be.
4158 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4159 self.waitUntilSettled()
4160 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4161
Clark Boylandd849822015-03-02 12:38:14 -08004162 # Release jobs in order to avoid races with change A jobs
4163 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004164 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004165 self.executor_server.hold_jobs_in_build = False
4166 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004167 self.waitUntilSettled()
4168
4169 self.assertEqual(A.data['status'], 'NEW')
4170 self.assertEqual(B.data['status'], 'NEW')
4171 self.assertEqual(A.reported, 1)
4172 self.assertEqual(B.reported, 1)
4173
4174 self.assertEqual(self.history[0].changes, '1,1 2,1')
4175 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004176 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004177
4178 self.assertIn('Build succeeded', A.messages[0])
4179 self.assertIn('Build succeeded', B.messages[0])
4180
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004181 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004182 "Test cross-repo dependencies re-enqueued in independent pipelines"
4183
4184 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004185 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4186 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004187
4188 # A Depends-On: B
4189 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4190 A.subject, B.data['id'])
4191
4192 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4193 self.waitUntilSettled()
4194
4195 self.sched.reconfigure(self.config)
4196
4197 # Make sure the items still share a change queue, and the
4198 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004199 tenant = self.sched.abide.tenants.get('tenant-one')
4200 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4201 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004202 first_item = queue.queue[0]
4203 for item in queue.queue:
4204 self.assertEqual(item.queue, first_item.queue)
4205 self.assertFalse(first_item.live)
4206 self.assertTrue(queue.queue[1].live)
4207
4208 self.gearman_server.hold_jobs_in_queue = False
4209 self.gearman_server.release()
4210 self.waitUntilSettled()
4211
4212 self.assertEqual(A.data['status'], 'NEW')
4213 self.assertEqual(B.data['status'], 'NEW')
4214 self.assertEqual(A.reported, 1)
4215 self.assertEqual(B.reported, 0)
4216
4217 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004218 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004219
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004220 def test_crd_check_reconfiguration(self):
4221 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4222
4223 def test_crd_undefined_project(self):
4224 """Test that undefined projects in dependencies are handled for
4225 independent pipelines"""
4226 # It's a hack for fake gerrit,
4227 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004228 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004229 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4230
Jesse Keatingea68ab42017-04-19 15:03:30 -07004231 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004232 def test_crd_check_ignore_dependencies(self):
4233 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004234
4235 self.gearman_server.hold_jobs_in_queue = True
4236 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4237 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4238 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4239
4240 # A Depends-On: B
4241 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4242 A.subject, B.data['id'])
4243 # C git-depends on B
4244 C.setDependsOn(B, 1)
4245 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4246 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4247 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4248 self.waitUntilSettled()
4249
4250 # Make sure none of the items share a change queue, and all
4251 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004252 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004253 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004254 self.assertEqual(len(check_pipeline.queues), 3)
4255 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4256 for item in check_pipeline.getAllItems():
4257 self.assertTrue(item.live)
4258
4259 self.gearman_server.hold_jobs_in_queue = False
4260 self.gearman_server.release()
4261 self.waitUntilSettled()
4262
4263 self.assertEqual(A.data['status'], 'NEW')
4264 self.assertEqual(B.data['status'], 'NEW')
4265 self.assertEqual(C.data['status'], 'NEW')
4266 self.assertEqual(A.reported, 1)
4267 self.assertEqual(B.reported, 1)
4268 self.assertEqual(C.reported, 1)
4269
4270 # Each job should have tested exactly one change
4271 for job in self.history:
4272 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004273
James E. Blair7cb84542017-04-19 13:35:05 -07004274 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004275 def test_crd_check_transitive(self):
4276 "Test transitive cross-repo dependencies"
4277 # Specifically, if A -> B -> C, and C gets a new patchset and
4278 # A gets a new patchset, ensure the test of A,2 includes B,1
4279 # and C,2 (not C,1 which would indicate stale data in the
4280 # cache for B).
4281 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4282 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4283 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4284
4285 # A Depends-On: B
4286 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4287 A.subject, B.data['id'])
4288
4289 # B Depends-On: C
4290 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4291 B.subject, C.data['id'])
4292
4293 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4294 self.waitUntilSettled()
4295 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4296
4297 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4298 self.waitUntilSettled()
4299 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4300
4301 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4302 self.waitUntilSettled()
4303 self.assertEqual(self.history[-1].changes, '3,1')
4304
4305 C.addPatchset()
4306 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4307 self.waitUntilSettled()
4308 self.assertEqual(self.history[-1].changes, '3,2')
4309
4310 A.addPatchset()
4311 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4312 self.waitUntilSettled()
4313 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004314
James E. Blaira8b90b32016-08-24 15:18:50 -07004315 def test_crd_check_unknown(self):
4316 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004317 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004318 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4319 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4320 # A Depends-On: B
4321 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4322 A.subject, B.data['id'])
4323
4324 # Make sure zuul has seen an event on B.
4325 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4326 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4327 self.waitUntilSettled()
4328
4329 self.assertEqual(A.data['status'], 'NEW')
4330 self.assertEqual(A.reported, 1)
4331 self.assertEqual(B.data['status'], 'NEW')
4332 self.assertEqual(B.reported, 0)
4333
James E. Blair92464a22016-04-05 10:21:26 -07004334 def test_crd_cycle_join(self):
4335 "Test an updated change creates a cycle"
4336 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4337
4338 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4339 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004340 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004341
4342 # Create B->A
4343 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4344 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4345 B.subject, A.data['id'])
4346 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4347 self.waitUntilSettled()
4348
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004349 # Dep is there so zuul should have reported on B
4350 self.assertEqual(B.reported, 1)
4351
James E. Blair92464a22016-04-05 10:21:26 -07004352 # Update A to add A->B (a cycle).
4353 A.addPatchset()
4354 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4355 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004356 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4357 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004358
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004359 # Dependency cycle injected so zuul should not have reported again on A
4360 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004361
4362 # Now if we update B to remove the depends-on, everything
4363 # should be okay. B; A->B
4364
4365 B.addPatchset()
4366 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004367 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4368 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004369
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004370 # Cycle was removed so now zuul should have reported again on A
4371 self.assertEqual(A.reported, 2)
4372
4373 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4374 self.waitUntilSettled()
4375 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004376
Jesse Keatingcc67b122017-04-19 14:54:57 -07004377 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004378 def test_disable_at(self):
4379 "Test a pipeline will only report to the disabled trigger when failing"
4380
Jesse Keatingcc67b122017-04-19 14:54:57 -07004381 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004382 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004383 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004384 0, tenant.layout.pipelines['check']._consecutive_failures)
4385 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004386
4387 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4388 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4389 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4390 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4391 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4392 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4393 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4394 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4395 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4396 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4397 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4398
Paul Belanger174a8272017-03-14 13:20:10 -04004399 self.executor_server.failJob('project-test1', A)
4400 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004401 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004402 self.executor_server.failJob('project-test1', D)
4403 self.executor_server.failJob('project-test1', E)
4404 self.executor_server.failJob('project-test1', F)
4405 self.executor_server.failJob('project-test1', G)
4406 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004407 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004408 self.executor_server.failJob('project-test1', J)
4409 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004410
4411 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4412 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4413 self.waitUntilSettled()
4414
4415 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004416 2, tenant.layout.pipelines['check']._consecutive_failures)
4417 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004418
4419 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4420 self.waitUntilSettled()
4421
4422 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004423 0, tenant.layout.pipelines['check']._consecutive_failures)
4424 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004425
4426 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4427 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4428 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4429 self.waitUntilSettled()
4430
4431 # We should be disabled now
4432 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004433 3, tenant.layout.pipelines['check']._consecutive_failures)
4434 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004435
4436 # We need to wait between each of these patches to make sure the
4437 # smtp messages come back in an expected order
4438 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4439 self.waitUntilSettled()
4440 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4441 self.waitUntilSettled()
4442 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4443 self.waitUntilSettled()
4444
4445 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4446 # leaving a message on each change
4447 self.assertEqual(1, len(A.messages))
4448 self.assertIn('Build failed.', A.messages[0])
4449 self.assertEqual(1, len(B.messages))
4450 self.assertIn('Build failed.', B.messages[0])
4451 self.assertEqual(1, len(C.messages))
4452 self.assertIn('Build succeeded.', C.messages[0])
4453 self.assertEqual(1, len(D.messages))
4454 self.assertIn('Build failed.', D.messages[0])
4455 self.assertEqual(1, len(E.messages))
4456 self.assertIn('Build failed.', E.messages[0])
4457 self.assertEqual(1, len(F.messages))
4458 self.assertIn('Build failed.', F.messages[0])
4459
4460 # The last 3 (GHI) would have only reported via smtp.
4461 self.assertEqual(3, len(self.smtp_messages))
4462 self.assertEqual(0, len(G.messages))
4463 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004464 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004465 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004466 self.assertEqual(0, len(H.messages))
4467 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004468 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004469 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004470 self.assertEqual(0, len(I.messages))
4471 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004472 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004473 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004474
4475 # Now reload the configuration (simulate a HUP) to check the pipeline
4476 # comes out of disabled
4477 self.sched.reconfigure(self.config)
4478
Jesse Keatingcc67b122017-04-19 14:54:57 -07004479 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004480
4481 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004482 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004483 0, tenant.layout.pipelines['check']._consecutive_failures)
4484 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004485
4486 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4487 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4488 self.waitUntilSettled()
4489
4490 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004491 2, tenant.layout.pipelines['check']._consecutive_failures)
4492 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004493
4494 # J and K went back to gerrit
4495 self.assertEqual(1, len(J.messages))
4496 self.assertIn('Build failed.', J.messages[0])
4497 self.assertEqual(1, len(K.messages))
4498 self.assertIn('Build failed.', K.messages[0])
4499 # No more messages reported via smtp
4500 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004501
James E. Blaircc020532017-04-19 13:04:44 -07004502 @simple_layout('layouts/one-job-project.yaml')
4503 def test_one_job_project(self):
4504 "Test that queueing works with one job"
4505 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4506 'master', 'A')
4507 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4508 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004509 A.addApproval('Code-Review', 2)
4510 B.addApproval('Code-Review', 2)
4511 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4512 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004513 self.waitUntilSettled()
4514
4515 self.assertEqual(A.data['status'], 'MERGED')
4516 self.assertEqual(A.reported, 2)
4517 self.assertEqual(B.data['status'], 'MERGED')
4518 self.assertEqual(B.reported, 2)
4519
Paul Belanger71d98172016-11-08 10:56:31 -05004520 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004521 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004522
Paul Belanger174a8272017-03-14 13:20:10 -04004523 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004524 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4525 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4526 self.waitUntilSettled()
4527
Paul Belanger174a8272017-03-14 13:20:10 -04004528 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004529 self.waitUntilSettled()
4530
4531 self.assertEqual(len(self.builds), 2)
4532 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004533 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004534 self.waitUntilSettled()
4535
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004536 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004537 self.assertEqual(len(self.builds), 1,
4538 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004539 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004540 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004541 self.waitUntilSettled()
4542
Paul Belanger174a8272017-03-14 13:20:10 -04004543 self.executor_server.hold_jobs_in_build = False
4544 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004545 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004546 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004547 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4548 self.assertEqual(A.reported, 1)
4549 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004550
James E. Blair15be0e12017-01-03 13:45:20 -08004551 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004552 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004553
4554 self.fake_nodepool.paused = True
4555 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004556 A.addApproval('Code-Review', 2)
4557 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004558 self.waitUntilSettled()
4559
4560 self.zk.client.stop()
4561 self.zk.client.start()
4562 self.fake_nodepool.paused = False
4563 self.waitUntilSettled()
4564
4565 self.assertEqual(A.data['status'], 'MERGED')
4566 self.assertEqual(A.reported, 2)
4567
James E. Blair6ab79e02017-01-06 10:10:17 -08004568 def test_nodepool_failure(self):
4569 "Test that jobs are reported after a nodepool failure"
4570
4571 self.fake_nodepool.paused = True
4572 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004573 A.addApproval('Code-Review', 2)
4574 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004575 self.waitUntilSettled()
4576
4577 req = self.fake_nodepool.getNodeRequests()[0]
4578 self.fake_nodepool.addFailRequest(req)
4579
4580 self.fake_nodepool.paused = False
4581 self.waitUntilSettled()
4582
4583 self.assertEqual(A.data['status'], 'NEW')
4584 self.assertEqual(A.reported, 2)
4585 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4586 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4587 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4588
Arieb6f068c2016-10-09 13:11:06 +03004589
James E. Blaira002b032017-04-18 10:35:48 -07004590class TestExecutor(ZuulTestCase):
4591 tenant_config_file = 'config/single-tenant/main.yaml'
4592
4593 def assertFinalState(self):
4594 # In this test, we expect to shut down in a non-final state,
4595 # so skip these checks.
4596 pass
4597
4598 def assertCleanShutdown(self):
4599 self.log.debug("Assert clean shutdown")
4600
4601 # After shutdown, make sure no jobs are running
4602 self.assertEqual({}, self.executor_server.job_workers)
4603
4604 # Make sure that git.Repo objects have been garbage collected.
4605 repos = []
4606 gc.collect()
4607 for obj in gc.get_objects():
4608 if isinstance(obj, git.Repo):
4609 self.log.debug("Leaked git repo object: %s" % repr(obj))
4610 repos.append(obj)
4611 self.assertEqual(len(repos), 0)
4612
4613 def test_executor_shutdown(self):
4614 "Test that the executor can shut down with jobs running"
4615
4616 self.executor_server.hold_jobs_in_build = True
4617 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004618 A.addApproval('Code-Review', 2)
4619 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004620 self.waitUntilSettled()
4621
4622
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004623class TestDependencyGraph(ZuulTestCase):
4624 tenant_config_file = 'config/dependency-graph/main.yaml'
4625
4626 def test_dependeny_graph_dispatch_jobs_once(self):
4627 "Test a job in a dependency graph is queued only once"
4628 # Job dependencies, starting with A
4629 # A
4630 # / \
4631 # B C
4632 # / \ / \
4633 # D F E
4634 # |
4635 # G
4636
4637 self.executor_server.hold_jobs_in_build = True
4638 change = self.fake_gerrit.addFakeChange(
4639 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004640 change.addApproval('Code-Review', 2)
4641 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004642
4643 self.waitUntilSettled()
4644 self.assertEqual([b.name for b in self.builds], ['A'])
4645
4646 self.executor_server.release('A')
4647 self.waitUntilSettled()
4648 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4649
4650 self.executor_server.release('B')
4651 self.waitUntilSettled()
4652 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4653
4654 self.executor_server.release('D')
4655 self.waitUntilSettled()
4656 self.assertEqual([b.name for b in self.builds], ['C'])
4657
4658 self.executor_server.release('C')
4659 self.waitUntilSettled()
4660 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4661
4662 self.executor_server.release('F')
4663 self.waitUntilSettled()
4664 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4665
4666 self.executor_server.release('G')
4667 self.waitUntilSettled()
4668 self.assertEqual([b.name for b in self.builds], ['E'])
4669
4670 self.executor_server.release('E')
4671 self.waitUntilSettled()
4672 self.assertEqual(len(self.builds), 0)
4673
4674 self.executor_server.hold_jobs_in_build = False
4675 self.executor_server.release()
4676 self.waitUntilSettled()
4677
4678 self.assertEqual(len(self.builds), 0)
4679 self.assertEqual(len(self.history), 7)
4680
4681 self.assertEqual(change.data['status'], 'MERGED')
4682 self.assertEqual(change.reported, 2)
4683
4684 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4685 "Test that a job waits till all dependencies are successful"
4686 # Job dependencies, starting with A
4687 # A
4688 # / \
4689 # B C*
4690 # / \ / \
4691 # D F E
4692 # |
4693 # G
4694
4695 self.executor_server.hold_jobs_in_build = True
4696 change = self.fake_gerrit.addFakeChange(
4697 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004698 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004699
4700 self.executor_server.failJob('C', change)
4701
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004702 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004703
4704 self.waitUntilSettled()
4705 self.assertEqual([b.name for b in self.builds], ['A'])
4706
4707 self.executor_server.release('A')
4708 self.waitUntilSettled()
4709 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4710
4711 self.executor_server.release('B')
4712 self.waitUntilSettled()
4713 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4714
4715 self.executor_server.release('D')
4716 self.waitUntilSettled()
4717 self.assertEqual([b.name for b in self.builds], ['C'])
4718
4719 self.executor_server.release('C')
4720 self.waitUntilSettled()
4721 self.assertEqual(len(self.builds), 0)
4722
4723 self.executor_server.hold_jobs_in_build = False
4724 self.executor_server.release()
4725 self.waitUntilSettled()
4726
4727 self.assertEqual(len(self.builds), 0)
4728 self.assertEqual(len(self.history), 4)
4729
4730 self.assertEqual(change.data['status'], 'NEW')
4731 self.assertEqual(change.reported, 2)
4732
4733
Arieb6f068c2016-10-09 13:11:06 +03004734class TestDuplicatePipeline(ZuulTestCase):
4735 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4736
4737 def test_duplicate_pipelines(self):
4738 "Test that a change matching multiple pipelines works"
4739
4740 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4741 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4742 self.waitUntilSettled()
4743
4744 self.assertHistory([
4745 dict(name='project-test1', result='SUCCESS', changes='1,1',
4746 pipeline='dup1'),
4747 dict(name='project-test1', result='SUCCESS', changes='1,1',
4748 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004749 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004750
4751 self.assertEqual(len(A.messages), 2)
4752
Paul Belangera46a3742016-11-09 14:23:03 -05004753 if 'dup1' in A.messages[0]:
4754 self.assertIn('dup1', A.messages[0])
4755 self.assertNotIn('dup2', A.messages[0])
4756 self.assertIn('project-test1', A.messages[0])
4757 self.assertIn('dup2', A.messages[1])
4758 self.assertNotIn('dup1', A.messages[1])
4759 self.assertIn('project-test1', A.messages[1])
4760 else:
4761 self.assertIn('dup1', A.messages[1])
4762 self.assertNotIn('dup2', A.messages[1])
4763 self.assertIn('project-test1', A.messages[1])
4764 self.assertIn('dup2', A.messages[0])
4765 self.assertNotIn('dup1', A.messages[0])
4766 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004767
4768
Paul Belanger86085b32016-11-03 12:48:57 -04004769class TestSchedulerTemplatedProject(ZuulTestCase):
4770 tenant_config_file = 'config/templated-project/main.yaml'
4771
Paul Belanger174a8272017-03-14 13:20:10 -04004772 def test_job_from_templates_executed(self):
4773 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004774
4775 A = self.fake_gerrit.addFakeChange(
4776 'org/templated-project', 'master', 'A')
4777 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4778 self.waitUntilSettled()
4779
4780 self.assertEqual(self.getJobFromHistory('project-test1').result,
4781 'SUCCESS')
4782 self.assertEqual(self.getJobFromHistory('project-test2').result,
4783 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004784
4785 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004786 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004787
4788 A = self.fake_gerrit.addFakeChange(
4789 'org/layered-project', 'master', 'A')
4790 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4791 self.waitUntilSettled()
4792
4793 self.assertEqual(self.getJobFromHistory('project-test1').result,
4794 'SUCCESS')
4795 self.assertEqual(self.getJobFromHistory('project-test2').result,
4796 'SUCCESS')
4797 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4798 ).result, 'SUCCESS')
4799 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4800 ).result, 'SUCCESS')
4801 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4802 ).result, 'SUCCESS')
4803 self.assertEqual(self.getJobFromHistory('project-test6').result,
4804 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004805
4806
4807class TestSchedulerSuccessURL(ZuulTestCase):
4808 tenant_config_file = 'config/success-url/main.yaml'
4809
4810 def test_success_url(self):
4811 "Ensure bad build params are ignored"
4812 self.sched.reconfigure(self.config)
4813 self.init_repo('org/docs')
4814
4815 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4816 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4817 self.waitUntilSettled()
4818
4819 # Both builds ran: docs-draft-test + docs-draft-test2
4820 self.assertEqual(len(self.history), 2)
4821
4822 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004823 for build in self.history:
4824 if build.name == 'docs-draft-test':
4825 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004826 elif build.name == 'docs-draft-test2':
4827 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004828
4829 # Two msgs: 'Starting...' + results
4830 self.assertEqual(len(self.smtp_messages), 2)
4831 body = self.smtp_messages[1]['body'].splitlines()
4832 self.assertEqual('Build succeeded.', body[0])
4833
4834 self.assertIn(
4835 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4836 'docs-draft-test/%s/publish-docs/' % uuid,
4837 body[2])
4838
Paul Belanger174a8272017-03-14 13:20:10 -04004839 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004840 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004841 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4842 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004843 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004844 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004845
4846
Adam Gandelman4da00f62016-12-09 15:47:33 -08004847class TestSchedulerMerges(ZuulTestCase):
4848 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004849
4850 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004851 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004852 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004853 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004854 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4855 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4856 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004857 A.addApproval('Code-Review', 2)
4858 B.addApproval('Code-Review', 2)
4859 C.addApproval('Code-Review', 2)
4860 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4861 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4862 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004863 self.waitUntilSettled()
4864
4865 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004866 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4867 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004868 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004869 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004870 repo_messages.reverse()
4871
Paul Belanger174a8272017-03-14 13:20:10 -04004872 self.executor_server.hold_jobs_in_build = False
4873 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004874 self.waitUntilSettled()
4875
4876 return repo_messages
4877
4878 def _test_merge(self, mode):
4879 us_path = os.path.join(
4880 self.upstream_root, 'org/project-%s' % mode)
4881 expected_messages = [
4882 'initial commit',
4883 'add content from fixture',
4884 # the intermediate commits order is nondeterministic
4885 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4886 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4887 ]
4888 result = self._test_project_merge_mode(mode)
4889 self.assertEqual(result[:2], expected_messages[:2])
4890 self.assertEqual(result[-2:], expected_messages[-2:])
4891
4892 def test_project_merge_mode_merge(self):
4893 self._test_merge('merge')
4894
4895 def test_project_merge_mode_merge_resolve(self):
4896 self._test_merge('merge-resolve')
4897
4898 def test_project_merge_mode_cherrypick(self):
4899 expected_messages = [
4900 'initial commit',
4901 'add content from fixture',
4902 'A-1',
4903 'B-1',
4904 'C-1']
4905 result = self._test_project_merge_mode('cherry-pick')
4906 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004907
4908 def test_merge_branch(self):
4909 "Test that the right commits are on alternate branches"
4910 self.create_branch('org/project-merge-branches', 'mp')
4911
Paul Belanger174a8272017-03-14 13:20:10 -04004912 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004913 A = self.fake_gerrit.addFakeChange(
4914 'org/project-merge-branches', 'mp', 'A')
4915 B = self.fake_gerrit.addFakeChange(
4916 'org/project-merge-branches', 'mp', 'B')
4917 C = self.fake_gerrit.addFakeChange(
4918 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004919 A.addApproval('Code-Review', 2)
4920 B.addApproval('Code-Review', 2)
4921 C.addApproval('Code-Review', 2)
4922 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4923 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4924 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004925 self.waitUntilSettled()
4926
Paul Belanger174a8272017-03-14 13:20:10 -04004927 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004928 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004929 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004930 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004931 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004932 self.waitUntilSettled()
4933
4934 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004935 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004936 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4937 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004938 repo = git.Repo(path)
4939
James E. Blair247cab72017-07-20 16:52:36 -07004940 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004941 repo_messages.reverse()
4942 correct_messages = [
4943 'initial commit',
4944 'add content from fixture',
4945 'mp commit',
4946 'A-1', 'B-1', 'C-1']
4947 self.assertEqual(repo_messages, correct_messages)
4948
Paul Belanger174a8272017-03-14 13:20:10 -04004949 self.executor_server.hold_jobs_in_build = False
4950 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004951 self.waitUntilSettled()
4952
4953 def test_merge_multi_branch(self):
4954 "Test that dependent changes on multiple branches are merged"
4955 self.create_branch('org/project-merge-branches', 'mp')
4956
Paul Belanger174a8272017-03-14 13:20:10 -04004957 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004958 A = self.fake_gerrit.addFakeChange(
4959 'org/project-merge-branches', 'master', 'A')
4960 B = self.fake_gerrit.addFakeChange(
4961 'org/project-merge-branches', 'mp', 'B')
4962 C = self.fake_gerrit.addFakeChange(
4963 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004964 A.addApproval('Code-Review', 2)
4965 B.addApproval('Code-Review', 2)
4966 C.addApproval('Code-Review', 2)
4967 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4968 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4969 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004970 self.waitUntilSettled()
4971
4972 job_A = None
4973 for job in self.builds:
4974 if 'project-merge' in job.name:
4975 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004976
James E. Blair2a535672017-04-27 12:03:15 -07004977 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4978 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004979 repo = git.Repo(path)
4980 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004981 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004982 repo_messages.reverse()
4983 correct_messages = [
4984 'initial commit', 'add content from fixture', 'A-1']
4985 self.assertEqual(repo_messages, correct_messages)
4986
Paul Belanger174a8272017-03-14 13:20:10 -04004987 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004988 self.waitUntilSettled()
4989
4990 job_B = None
4991 for job in self.builds:
4992 if 'project-merge' in job.name:
4993 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004994
James E. Blair2a535672017-04-27 12:03:15 -07004995 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4996 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004997 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004998 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004999 repo_messages.reverse()
5000 correct_messages = [
5001 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5002 self.assertEqual(repo_messages, correct_messages)
5003
Paul Belanger174a8272017-03-14 13:20:10 -04005004 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005005 self.waitUntilSettled()
5006
5007 job_C = None
5008 for job in self.builds:
5009 if 'project-merge' in job.name:
5010 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005011
James E. Blair2a535672017-04-27 12:03:15 -07005012 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5013 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005014 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005015 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005016
5017 repo_messages.reverse()
5018 correct_messages = [
5019 'initial commit', 'add content from fixture',
5020 'A-1', 'C-1']
5021 # Ensure the right commits are in the history for this ref
5022 self.assertEqual(repo_messages, correct_messages)
5023
Paul Belanger174a8272017-03-14 13:20:10 -04005024 self.executor_server.hold_jobs_in_build = False
5025 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005026 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005027
5028
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005029class TestSemaphore(ZuulTestCase):
5030 tenant_config_file = 'config/semaphore/main.yaml'
5031
5032 def test_semaphore_one(self):
5033 "Test semaphores with max=1 (mutex)"
5034 tenant = self.sched.abide.tenants.get('tenant-one')
5035
5036 self.executor_server.hold_jobs_in_build = True
5037
5038 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5039 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5040 self.assertFalse('test-semaphore' in
5041 tenant.semaphore_handler.semaphores)
5042
5043 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5044 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5045 self.waitUntilSettled()
5046
5047 self.assertEqual(len(self.builds), 3)
5048 self.assertEqual(self.builds[0].name, 'project-test1')
5049 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5050 self.assertEqual(self.builds[2].name, 'project-test1')
5051
5052 self.executor_server.release('semaphore-one-test1')
5053 self.waitUntilSettled()
5054
5055 self.assertEqual(len(self.builds), 3)
5056 self.assertEqual(self.builds[0].name, 'project-test1')
5057 self.assertEqual(self.builds[1].name, 'project-test1')
5058 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5059 self.assertTrue('test-semaphore' in
5060 tenant.semaphore_handler.semaphores)
5061
5062 self.executor_server.release('semaphore-one-test2')
5063 self.waitUntilSettled()
5064
5065 self.assertEqual(len(self.builds), 3)
5066 self.assertEqual(self.builds[0].name, 'project-test1')
5067 self.assertEqual(self.builds[1].name, 'project-test1')
5068 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5069 self.assertTrue('test-semaphore' in
5070 tenant.semaphore_handler.semaphores)
5071
5072 self.executor_server.release('semaphore-one-test1')
5073 self.waitUntilSettled()
5074
5075 self.assertEqual(len(self.builds), 3)
5076 self.assertEqual(self.builds[0].name, 'project-test1')
5077 self.assertEqual(self.builds[1].name, 'project-test1')
5078 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5079 self.assertTrue('test-semaphore' in
5080 tenant.semaphore_handler.semaphores)
5081
5082 self.executor_server.release('semaphore-one-test2')
5083 self.waitUntilSettled()
5084
5085 self.assertEqual(len(self.builds), 2)
5086 self.assertEqual(self.builds[0].name, 'project-test1')
5087 self.assertEqual(self.builds[1].name, 'project-test1')
5088 self.assertFalse('test-semaphore' in
5089 tenant.semaphore_handler.semaphores)
5090
5091 self.executor_server.hold_jobs_in_build = False
5092 self.executor_server.release()
5093
5094 self.waitUntilSettled()
5095 self.assertEqual(len(self.builds), 0)
5096
5097 self.assertEqual(A.reported, 1)
5098 self.assertEqual(B.reported, 1)
5099 self.assertFalse('test-semaphore' in
5100 tenant.semaphore_handler.semaphores)
5101
5102 def test_semaphore_two(self):
5103 "Test semaphores with max>1"
5104 tenant = self.sched.abide.tenants.get('tenant-one')
5105
5106 self.executor_server.hold_jobs_in_build = True
5107 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5108 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5109 self.assertFalse('test-semaphore-two' in
5110 tenant.semaphore_handler.semaphores)
5111
5112 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5113 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5114 self.waitUntilSettled()
5115
5116 self.assertEqual(len(self.builds), 4)
5117 self.assertEqual(self.builds[0].name, 'project-test1')
5118 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5119 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5120 self.assertEqual(self.builds[3].name, 'project-test1')
5121 self.assertTrue('test-semaphore-two' in
5122 tenant.semaphore_handler.semaphores)
5123 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5124 'test-semaphore-two', [])), 2)
5125
5126 self.executor_server.release('semaphore-two-test1')
5127 self.waitUntilSettled()
5128
5129 self.assertEqual(len(self.builds), 4)
5130 self.assertEqual(self.builds[0].name, 'project-test1')
5131 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5132 self.assertEqual(self.builds[2].name, 'project-test1')
5133 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5134 self.assertTrue('test-semaphore-two' in
5135 tenant.semaphore_handler.semaphores)
5136 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5137 'test-semaphore-two', [])), 2)
5138
5139 self.executor_server.release('semaphore-two-test2')
5140 self.waitUntilSettled()
5141
5142 self.assertEqual(len(self.builds), 4)
5143 self.assertEqual(self.builds[0].name, 'project-test1')
5144 self.assertEqual(self.builds[1].name, 'project-test1')
5145 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5146 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5147 self.assertTrue('test-semaphore-two' in
5148 tenant.semaphore_handler.semaphores)
5149 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5150 'test-semaphore-two', [])), 2)
5151
5152 self.executor_server.release('semaphore-two-test1')
5153 self.waitUntilSettled()
5154
5155 self.assertEqual(len(self.builds), 3)
5156 self.assertEqual(self.builds[0].name, 'project-test1')
5157 self.assertEqual(self.builds[1].name, 'project-test1')
5158 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5159 self.assertTrue('test-semaphore-two' in
5160 tenant.semaphore_handler.semaphores)
5161 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5162 'test-semaphore-two', [])), 1)
5163
5164 self.executor_server.release('semaphore-two-test2')
5165 self.waitUntilSettled()
5166
5167 self.assertEqual(len(self.builds), 2)
5168 self.assertEqual(self.builds[0].name, 'project-test1')
5169 self.assertEqual(self.builds[1].name, 'project-test1')
5170 self.assertFalse('test-semaphore-two' in
5171 tenant.semaphore_handler.semaphores)
5172
5173 self.executor_server.hold_jobs_in_build = False
5174 self.executor_server.release()
5175
5176 self.waitUntilSettled()
5177 self.assertEqual(len(self.builds), 0)
5178
5179 self.assertEqual(A.reported, 1)
5180 self.assertEqual(B.reported, 1)
5181
5182 def test_semaphore_abandon(self):
5183 "Test abandon with job semaphores"
5184 self.executor_server.hold_jobs_in_build = True
5185 tenant = self.sched.abide.tenants.get('tenant-one')
5186 check_pipeline = tenant.layout.pipelines['check']
5187
5188 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5189 self.assertFalse('test-semaphore' in
5190 tenant.semaphore_handler.semaphores)
5191
5192 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5193 self.waitUntilSettled()
5194
5195 self.assertTrue('test-semaphore' in
5196 tenant.semaphore_handler.semaphores)
5197
5198 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5199 self.waitUntilSettled()
5200
5201 # The check pipeline should be empty
5202 items = check_pipeline.getAllItems()
5203 self.assertEqual(len(items), 0)
5204
5205 # The semaphore should be released
5206 self.assertFalse('test-semaphore' in
5207 tenant.semaphore_handler.semaphores)
5208
5209 self.executor_server.hold_jobs_in_build = False
5210 self.executor_server.release()
5211 self.waitUntilSettled()
5212
Tobias Henkel0f714002017-06-30 23:30:52 +02005213 def test_semaphore_new_patchset(self):
5214 "Test new patchset with job semaphores"
5215 self.executor_server.hold_jobs_in_build = True
5216 tenant = self.sched.abide.tenants.get('tenant-one')
5217 check_pipeline = tenant.layout.pipelines['check']
5218
5219 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5220 self.assertFalse('test-semaphore' in
5221 tenant.semaphore_handler.semaphores)
5222
5223 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5224 self.waitUntilSettled()
5225
5226 self.assertTrue('test-semaphore' in
5227 tenant.semaphore_handler.semaphores)
5228 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5229 self.assertEqual(len(semaphore), 1)
5230
5231 A.addPatchset()
5232 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5233 self.waitUntilSettled()
5234
5235 self.assertTrue('test-semaphore' in
5236 tenant.semaphore_handler.semaphores)
5237 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5238 self.assertEqual(len(semaphore), 1)
5239
5240 items = check_pipeline.getAllItems()
5241 self.assertEqual(items[0].change.number, '1')
5242 self.assertEqual(items[0].change.patchset, '2')
5243 self.assertTrue(items[0].live)
5244
5245 self.executor_server.hold_jobs_in_build = False
5246 self.executor_server.release()
5247 self.waitUntilSettled()
5248
5249 # The semaphore should be released
5250 self.assertFalse('test-semaphore' in
5251 tenant.semaphore_handler.semaphores)
5252
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005253 def test_semaphore_reconfigure(self):
5254 "Test reconfigure with job semaphores"
5255 self.executor_server.hold_jobs_in_build = True
5256 tenant = self.sched.abide.tenants.get('tenant-one')
5257 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5258 self.assertFalse('test-semaphore' in
5259 tenant.semaphore_handler.semaphores)
5260
5261 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5262 self.waitUntilSettled()
5263
5264 self.assertTrue('test-semaphore' in
5265 tenant.semaphore_handler.semaphores)
5266
5267 # reconfigure without layout change
5268 self.sched.reconfigure(self.config)
5269 self.waitUntilSettled()
5270 tenant = self.sched.abide.tenants.get('tenant-one')
5271
5272 # semaphore still must be held
5273 self.assertTrue('test-semaphore' in
5274 tenant.semaphore_handler.semaphores)
5275
5276 self.commitConfigUpdate(
5277 'common-config',
5278 'config/semaphore/zuul-reconfiguration.yaml')
5279 self.sched.reconfigure(self.config)
5280 self.waitUntilSettled()
5281 tenant = self.sched.abide.tenants.get('tenant-one')
5282
5283 self.executor_server.release('project-test1')
5284 self.waitUntilSettled()
5285
5286 # There should be no builds anymore
5287 self.assertEqual(len(self.builds), 0)
5288
5289 # The semaphore should be released
5290 self.assertFalse('test-semaphore' in
5291 tenant.semaphore_handler.semaphores)
5292
5293
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005294class TestSemaphoreMultiTenant(ZuulTestCase):
5295 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5296
5297 def test_semaphore_tenant_isolation(self):
5298 "Test semaphores in multiple tenants"
5299
5300 self.waitUntilSettled()
5301 tenant_one = self.sched.abide.tenants.get('tenant-one')
5302 tenant_two = self.sched.abide.tenants.get('tenant-two')
5303
5304 self.executor_server.hold_jobs_in_build = True
5305 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5306 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5307 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5308 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5309 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5310 self.assertFalse('test-semaphore' in
5311 tenant_one.semaphore_handler.semaphores)
5312 self.assertFalse('test-semaphore' in
5313 tenant_two.semaphore_handler.semaphores)
5314
5315 # add patches to project1 of tenant-one
5316 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5317 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5318 self.waitUntilSettled()
5319
5320 # one build of project1-test1 must run
5321 # semaphore of tenant-one must be acquired once
5322 # semaphore of tenant-two must not be acquired
5323 self.assertEqual(len(self.builds), 1)
5324 self.assertEqual(self.builds[0].name, 'project1-test1')
5325 self.assertTrue('test-semaphore' in
5326 tenant_one.semaphore_handler.semaphores)
5327 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5328 'test-semaphore', [])), 1)
5329 self.assertFalse('test-semaphore' in
5330 tenant_two.semaphore_handler.semaphores)
5331
5332 # add patches to project2 of tenant-two
5333 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5334 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5335 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5336 self.waitUntilSettled()
5337
5338 # one build of project1-test1 must run
5339 # two builds of project2-test1 must run
5340 # semaphore of tenant-one must be acquired once
5341 # semaphore of tenant-two must be acquired twice
5342 self.assertEqual(len(self.builds), 3)
5343 self.assertEqual(self.builds[0].name, 'project1-test1')
5344 self.assertEqual(self.builds[1].name, 'project2-test1')
5345 self.assertEqual(self.builds[2].name, 'project2-test1')
5346 self.assertTrue('test-semaphore' in
5347 tenant_one.semaphore_handler.semaphores)
5348 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5349 'test-semaphore', [])), 1)
5350 self.assertTrue('test-semaphore' in
5351 tenant_two.semaphore_handler.semaphores)
5352 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5353 'test-semaphore', [])), 2)
5354
5355 self.executor_server.release('project1-test1')
5356 self.waitUntilSettled()
5357
5358 # one build of project1-test1 must run
5359 # two builds of project2-test1 must run
5360 # semaphore of tenant-one must be acquired once
5361 # semaphore of tenant-two must be acquired twice
5362 self.assertEqual(len(self.builds), 3)
5363 self.assertEqual(self.builds[0].name, 'project2-test1')
5364 self.assertEqual(self.builds[1].name, 'project2-test1')
5365 self.assertEqual(self.builds[2].name, 'project1-test1')
5366 self.assertTrue('test-semaphore' in
5367 tenant_one.semaphore_handler.semaphores)
5368 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5369 'test-semaphore', [])), 1)
5370 self.assertTrue('test-semaphore' in
5371 tenant_two.semaphore_handler.semaphores)
5372 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5373 'test-semaphore', [])), 2)
5374
5375 self.executor_server.release('project2-test1')
5376 self.waitUntilSettled()
5377
5378 # one build of project1-test1 must run
5379 # one build of project2-test1 must run
5380 # semaphore of tenant-one must be acquired once
5381 # semaphore of tenant-two must be acquired once
5382 self.assertEqual(len(self.builds), 2)
5383 self.assertTrue('test-semaphore' in
5384 tenant_one.semaphore_handler.semaphores)
5385 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5386 'test-semaphore', [])), 1)
5387 self.assertTrue('test-semaphore' in
5388 tenant_two.semaphore_handler.semaphores)
5389 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5390 'test-semaphore', [])), 1)
5391
5392 self.executor_server.hold_jobs_in_build = False
5393 self.executor_server.release()
5394
5395 self.waitUntilSettled()
5396
5397 # no build must run
5398 # semaphore of tenant-one must not be acquired
5399 # semaphore of tenant-two must not be acquired
5400 self.assertEqual(len(self.builds), 0)
5401 self.assertFalse('test-semaphore' in
5402 tenant_one.semaphore_handler.semaphores)
5403 self.assertFalse('test-semaphore' in
5404 tenant_two.semaphore_handler.semaphores)
5405
5406 self.assertEqual(A.reported, 1)
5407 self.assertEqual(B.reported, 1)
5408
5409
5410class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005411 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005412 tenant_config_file = 'config/in-repo/main.yaml'
5413
5414 def test_semaphore_in_repo(self):
5415 "Test semaphores in repo config"
5416
5417 # This tests dynamic semaphore handling in project repos. The semaphore
5418 # max value should not be evaluated dynamically but must be updated
5419 # after the change lands.
5420
5421 self.waitUntilSettled()
5422 tenant = self.sched.abide.tenants.get('tenant-one')
5423
5424 in_repo_conf = textwrap.dedent(
5425 """
5426 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005427 name: project-test1
5428
5429 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005430 name: project-test2
5431 semaphore: test-semaphore
5432
5433 - project:
5434 name: org/project
5435 tenant-one-gate:
5436 jobs:
5437 - project-test2
5438
5439 # the max value in dynamic layout must be ignored
5440 - semaphore:
5441 name: test-semaphore
5442 max: 2
5443 """)
5444
5445 in_repo_playbook = textwrap.dedent(
5446 """
5447 - hosts: all
5448 tasks: []
5449 """)
5450
5451 file_dict = {'.zuul.yaml': in_repo_conf,
5452 'playbooks/project-test2.yaml': in_repo_playbook}
5453 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5454 files=file_dict)
5455 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5456 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5457 B.setDependsOn(A, 1)
5458 C.setDependsOn(A, 1)
5459
5460 self.executor_server.hold_jobs_in_build = True
5461
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005462 A.addApproval('Code-Review', 2)
5463 B.addApproval('Code-Review', 2)
5464 C.addApproval('Code-Review', 2)
5465 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5466 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5467 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005468 self.waitUntilSettled()
5469
5470 # check that the layout in a queue item still has max value of 1
5471 # for test-semaphore
5472 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5473 queue = None
5474 for queue_candidate in pipeline.queues:
5475 if queue_candidate.name == 'org/project':
5476 queue = queue_candidate
5477 break
5478 queue_item = queue.queue[0]
5479 item_dynamic_layout = queue_item.current_build_set.layout
5480 dynamic_test_semaphore = \
5481 item_dynamic_layout.semaphores.get('test-semaphore')
5482 self.assertEqual(dynamic_test_semaphore.max, 1)
5483
5484 # one build must be in queue, one semaphores acquired
5485 self.assertEqual(len(self.builds), 1)
5486 self.assertEqual(self.builds[0].name, 'project-test2')
5487 self.assertTrue('test-semaphore' in
5488 tenant.semaphore_handler.semaphores)
5489 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5490 'test-semaphore', [])), 1)
5491
5492 self.executor_server.release('project-test2')
5493 self.waitUntilSettled()
5494
5495 # change A must be merged
5496 self.assertEqual(A.data['status'], 'MERGED')
5497 self.assertEqual(A.reported, 2)
5498
5499 # send change-merged event as the gerrit mock doesn't send it
5500 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5501 self.waitUntilSettled()
5502
5503 # now that change A was merged, the new semaphore max must be effective
5504 tenant = self.sched.abide.tenants.get('tenant-one')
5505 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5506
5507 # two builds must be in queue, two semaphores acquired
5508 self.assertEqual(len(self.builds), 2)
5509 self.assertEqual(self.builds[0].name, 'project-test2')
5510 self.assertEqual(self.builds[1].name, 'project-test2')
5511 self.assertTrue('test-semaphore' in
5512 tenant.semaphore_handler.semaphores)
5513 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5514 'test-semaphore', [])), 2)
5515
5516 self.executor_server.release('project-test2')
5517 self.waitUntilSettled()
5518
5519 self.assertEqual(len(self.builds), 0)
5520 self.assertFalse('test-semaphore' in
5521 tenant.semaphore_handler.semaphores)
5522
5523 self.executor_server.hold_jobs_in_build = False
5524 self.executor_server.release()
5525
5526 self.waitUntilSettled()
5527 self.assertEqual(len(self.builds), 0)
5528
5529 self.assertEqual(A.reported, 2)
5530 self.assertEqual(B.reported, 2)
5531 self.assertEqual(C.reported, 2)