blob: 2dcd9bf0f9b0e33271b342489d73469d95fbae7d [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. Blairce8a2132016-05-19 15:21:52 -0700620 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400621 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200623 A.addApproval('Code-Review', 2)
624 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700625 self.waitUntilSettled()
626 time.sleep(2)
627
James E. Blairae0f23c2017-09-13 10:55:15 -0600628 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700629 found_job = None
630 for pipeline in data['pipelines']:
631 if pipeline['name'] != 'gate':
632 continue
633 for queue in pipeline['change_queues']:
634 for head in queue['heads']:
635 for item in head:
636 for job in item['jobs']:
637 if job['name'] == 'project-merge':
638 found_job = job
639 break
640
641 self.assertIsNotNone(found_job)
642 if iteration == 1:
643 self.assertIsNotNone(found_job['estimated_time'])
644 self.assertIsNone(found_job['remaining_time'])
645 else:
646 self.assertIsNotNone(found_job['estimated_time'])
647 self.assertTrue(found_job['estimated_time'] >= 2)
648 self.assertIsNotNone(found_job['remaining_time'])
649
Paul Belanger174a8272017-03-14 13:20:10 -0400650 self.executor_server.hold_jobs_in_build = False
651 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700652 self.waitUntilSettled()
653
654 def test_time_database(self):
655 "Test the time database"
656
657 self._test_time_database(1)
658 self._test_time_database(2)
659
James E. Blairfef71632013-09-23 11:15:47 -0700660 def test_two_failed_changes_at_head(self):
661 "Test that changes are reparented correctly if 2 fail at head"
662
Paul Belanger174a8272017-03-14 13:20:10 -0400663 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
665 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
666 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200667 A.addApproval('Code-Review', 2)
668 B.addApproval('Code-Review', 2)
669 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700670
Paul Belanger174a8272017-03-14 13:20:10 -0400671 self.executor_server.failJob('project-test1', A)
672 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700673
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200674 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
675 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
676 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700677 self.waitUntilSettled()
678
Paul Belanger174a8272017-03-14 13:20:10 -0400679 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700680 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400681 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700682 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400683 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700684 self.waitUntilSettled()
685
686 self.assertEqual(len(self.builds), 6)
687 self.assertEqual(self.builds[0].name, 'project-test1')
688 self.assertEqual(self.builds[1].name, 'project-test2')
689 self.assertEqual(self.builds[2].name, 'project-test1')
690 self.assertEqual(self.builds[3].name, 'project-test2')
691 self.assertEqual(self.builds[4].name, 'project-test1')
692 self.assertEqual(self.builds[5].name, 'project-test2')
693
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400694 self.assertTrue(self.builds[0].hasChanges(A))
695 self.assertTrue(self.builds[2].hasChanges(A))
696 self.assertTrue(self.builds[2].hasChanges(B))
697 self.assertTrue(self.builds[4].hasChanges(A))
698 self.assertTrue(self.builds[4].hasChanges(B))
699 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700700
701 # Fail change B first
702 self.release(self.builds[2])
703 self.waitUntilSettled()
704
705 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400706 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700707 self.waitUntilSettled()
708
709 self.assertEqual(len(self.builds), 5)
710 self.assertEqual(self.builds[0].name, 'project-test1')
711 self.assertEqual(self.builds[1].name, 'project-test2')
712 self.assertEqual(self.builds[2].name, 'project-test2')
713 self.assertEqual(self.builds[3].name, 'project-test1')
714 self.assertEqual(self.builds[4].name, 'project-test2')
715
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400716 self.assertTrue(self.builds[1].hasChanges(A))
717 self.assertTrue(self.builds[2].hasChanges(A))
718 self.assertTrue(self.builds[2].hasChanges(B))
719 self.assertTrue(self.builds[4].hasChanges(A))
720 self.assertFalse(self.builds[4].hasChanges(B))
721 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700722
723 # Finish running all passing jobs for change A
724 self.release(self.builds[1])
725 self.waitUntilSettled()
726 # Fail and report change A
727 self.release(self.builds[0])
728 self.waitUntilSettled()
729
730 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400731 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700732 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400733 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700734 self.waitUntilSettled()
735
736 self.assertEqual(len(self.builds), 4)
737 self.assertEqual(self.builds[0].name, 'project-test1') # B
738 self.assertEqual(self.builds[1].name, 'project-test2') # B
739 self.assertEqual(self.builds[2].name, 'project-test1') # C
740 self.assertEqual(self.builds[3].name, 'project-test2') # C
741
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400742 self.assertFalse(self.builds[1].hasChanges(A))
743 self.assertTrue(self.builds[1].hasChanges(B))
744 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700745
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400746 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700747 # After A failed and B and C restarted, B should be back in
748 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400749 self.assertTrue(self.builds[2].hasChanges(B))
750 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700751
Paul Belanger174a8272017-03-14 13:20:10 -0400752 self.executor_server.hold_jobs_in_build = False
753 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700754 self.waitUntilSettled()
755
756 self.assertEqual(len(self.builds), 0)
757 self.assertEqual(len(self.history), 21)
758 self.assertEqual(A.data['status'], 'NEW')
759 self.assertEqual(B.data['status'], 'NEW')
760 self.assertEqual(C.data['status'], 'MERGED')
761 self.assertEqual(A.reported, 2)
762 self.assertEqual(B.reported, 2)
763 self.assertEqual(C.reported, 2)
764
James E. Blair8c803f82012-07-31 16:25:42 -0700765 def test_patch_order(self):
766 "Test that dependent patches are tested in the right order"
767 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
768 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
769 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200770 A.addApproval('Code-Review', 2)
771 B.addApproval('Code-Review', 2)
772 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700773
774 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
775 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
776 M2.setMerged()
777 M1.setMerged()
778
779 # C -> B -> A -> M1 -> M2
780 # M2 is here to make sure it is never queried. If it is, it
781 # means zuul is walking down the entire history of merged
782 # changes.
783
784 C.setDependsOn(B, 1)
785 B.setDependsOn(A, 1)
786 A.setDependsOn(M1, 1)
787 M1.setDependsOn(M2, 1)
788
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200789 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700790
791 self.waitUntilSettled()
792
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400793 self.assertEqual(A.data['status'], 'NEW')
794 self.assertEqual(B.data['status'], 'NEW')
795 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700796
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200797 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
798 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700799
800 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400801 self.assertEqual(M2.queried, 0)
802 self.assertEqual(A.data['status'], 'MERGED')
803 self.assertEqual(B.data['status'], 'MERGED')
804 self.assertEqual(C.data['status'], 'MERGED')
805 self.assertEqual(A.reported, 2)
806 self.assertEqual(B.reported, 2)
807 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700808
James E. Blair063672f2015-01-29 13:09:12 -0800809 def test_needed_changes_enqueue(self):
810 "Test that a needed change is enqueued ahead"
811 # A Given a git tree like this, if we enqueue
812 # / \ change C, we should walk up and down the tree
813 # B G and enqueue changes in the order ABCDEFG.
814 # /|\ This is also the order that you would get if
815 # *C E F you enqueued changes in the order ABCDEFG, so
816 # / the ordering is stable across re-enqueue events.
817 # D
818
819 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
820 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
821 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
822 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
823 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
824 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
825 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
826 B.setDependsOn(A, 1)
827 C.setDependsOn(B, 1)
828 D.setDependsOn(C, 1)
829 E.setDependsOn(B, 1)
830 F.setDependsOn(B, 1)
831 G.setDependsOn(A, 1)
832
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200833 A.addApproval('Code-Review', 2)
834 B.addApproval('Code-Review', 2)
835 C.addApproval('Code-Review', 2)
836 D.addApproval('Code-Review', 2)
837 E.addApproval('Code-Review', 2)
838 F.addApproval('Code-Review', 2)
839 G.addApproval('Code-Review', 2)
840 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800841
842 self.waitUntilSettled()
843
844 self.assertEqual(A.data['status'], 'NEW')
845 self.assertEqual(B.data['status'], 'NEW')
846 self.assertEqual(C.data['status'], 'NEW')
847 self.assertEqual(D.data['status'], 'NEW')
848 self.assertEqual(E.data['status'], 'NEW')
849 self.assertEqual(F.data['status'], 'NEW')
850 self.assertEqual(G.data['status'], 'NEW')
851
852 # We're about to add approvals to changes without adding the
853 # triggering events to Zuul, so that we can be sure that it is
854 # enqueing the changes based on dependencies, not because of
855 # triggering events. Since it will have the changes cached
856 # already (without approvals), we need to clear the cache
857 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400858 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100859 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800860
Paul Belanger174a8272017-03-14 13:20:10 -0400861 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200862 A.addApproval('Approved', 1)
863 B.addApproval('Approved', 1)
864 D.addApproval('Approved', 1)
865 E.addApproval('Approved', 1)
866 F.addApproval('Approved', 1)
867 G.addApproval('Approved', 1)
868 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800869
870 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400871 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800872 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400873 self.executor_server.hold_jobs_in_build = False
874 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800875 self.waitUntilSettled()
876
877 self.assertEqual(A.data['status'], 'MERGED')
878 self.assertEqual(B.data['status'], 'MERGED')
879 self.assertEqual(C.data['status'], 'MERGED')
880 self.assertEqual(D.data['status'], 'MERGED')
881 self.assertEqual(E.data['status'], 'MERGED')
882 self.assertEqual(F.data['status'], 'MERGED')
883 self.assertEqual(G.data['status'], 'MERGED')
884 self.assertEqual(A.reported, 2)
885 self.assertEqual(B.reported, 2)
886 self.assertEqual(C.reported, 2)
887 self.assertEqual(D.reported, 2)
888 self.assertEqual(E.reported, 2)
889 self.assertEqual(F.reported, 2)
890 self.assertEqual(G.reported, 2)
891 self.assertEqual(self.history[6].changes,
892 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
893
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100894 def test_source_cache(self):
895 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400896 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700897
898 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
899 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
900 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200901 A.addApproval('Code-Review', 2)
902 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700903
904 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
905 M1.setMerged()
906
907 B.setDependsOn(A, 1)
908 A.setDependsOn(M1, 1)
909
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200910 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700911 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
912
913 self.waitUntilSettled()
914
915 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700916 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700917 build.release()
918 self.waitUntilSettled()
919 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700920 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700921 build.release()
922 self.waitUntilSettled()
923
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200924 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700925 self.waitUntilSettled()
926
Joshua Hesketh352264b2015-08-11 23:42:08 +1000927 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700928 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000929 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700930
Paul Belanger174a8272017-03-14 13:20:10 -0400931 self.executor_server.hold_jobs_in_build = False
932 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700933 self.waitUntilSettled()
934
935 self.assertEqual(A.data['status'], 'MERGED')
936 self.assertEqual(B.data['status'], 'MERGED')
937 self.assertEqual(A.queried, 2) # Initial and isMerged
938 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
939
James E. Blair8c803f82012-07-31 16:25:42 -0700940 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700941 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700942 # TODO: move to test_gerrit (this is a unit test!)
943 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400944 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700945 (trusted, project) = tenant.getProject('org/project')
946 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400947
948 # TODO(pabelanger): As we add more source / trigger APIs we should make
949 # it easier for users to create events for testing.
950 event = zuul.model.TriggerEvent()
951 event.trigger_name = 'gerrit'
952 event.change_number = '1'
953 event.patch_number = '2'
954
955 a = source.getChange(event)
956 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700957 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700958
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200959 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400960 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700961 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700962
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200963 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400964 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700965 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700966
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700967 def test_project_merge_conflict(self):
968 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700969
970 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700971 A = self.fake_gerrit.addFakeChange('org/project',
972 'master', 'A',
973 files={'conflict': 'foo'})
974 B = self.fake_gerrit.addFakeChange('org/project',
975 'master', 'B',
976 files={'conflict': 'bar'})
977 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700978 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200979 A.addApproval('Code-Review', 2)
980 B.addApproval('Code-Review', 2)
981 C.addApproval('Code-Review', 2)
982 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
983 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
984 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700985 self.waitUntilSettled()
986
James E. Blair6736beb2013-07-11 15:18:15 -0700987 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700988 self.assertEqual(C.reported, 1)
989
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700990 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700991 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700992 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700993 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700994 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700995 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700996
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700997 self.gearman_server.hold_jobs_in_queue = False
998 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700999 self.waitUntilSettled()
1000
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001001 self.assertEqual(A.data['status'], 'MERGED')
1002 self.assertEqual(B.data['status'], 'NEW')
1003 self.assertEqual(C.data['status'], 'MERGED')
1004 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001005 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001006 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001007
1008 self.assertHistory([
1009 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1010 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1011 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1012 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1013 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1014 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1015 ], ordered=False)
1016
1017 def test_delayed_merge_conflict(self):
1018 "Test that delayed check merge conflicts are handled properly"
1019
1020 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001021 # the executor sucesfully merges a change based on an old
1022 # repo state (frozen by the scheduler) which would otherwise
1023 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001024 self.gearman_server.hold_jobs_in_queue = True
1025 A = self.fake_gerrit.addFakeChange('org/project',
1026 'master', 'A',
1027 files={'conflict': 'foo'})
1028 B = self.fake_gerrit.addFakeChange('org/project',
1029 'master', 'B',
1030 files={'conflict': 'bar'})
1031 C = self.fake_gerrit.addFakeChange('org/project',
1032 'master', 'C')
1033 C.setDependsOn(B, 1)
1034
1035 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001036 A.addApproval('Code-Review', 2)
1037 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001038 self.waitUntilSettled()
1039 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001040 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001041 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1042 self.waitUntilSettled()
1043
1044 self.assertEqual(A.reported, 1)
1045 self.assertEqual(B.reported, 0) # Check does not report start
1046 self.assertEqual(C.reported, 0) # Check does not report start
1047
1048 # A merges while B and C are queued in check
1049 # Release A project-merge
1050 queue = self.gearman_server.getQueue()
1051 self.release(queue[0])
1052 self.waitUntilSettled()
1053
1054 # Release A project-test*
1055 # gate has higher precedence, so A's test jobs are added in
1056 # front of the merge jobs for B and C
1057 queue = self.gearman_server.getQueue()
1058 self.release(queue[0])
1059 self.release(queue[1])
1060 self.waitUntilSettled()
1061
1062 self.assertEqual(A.data['status'], 'MERGED')
1063 self.assertEqual(B.data['status'], 'NEW')
1064 self.assertEqual(C.data['status'], 'NEW')
1065 self.assertEqual(A.reported, 2)
1066 self.assertEqual(B.reported, 0)
1067 self.assertEqual(C.reported, 0)
1068 self.assertHistory([
1069 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1072 ], ordered=False)
1073
1074 # B and C report merge conflicts
1075 # Release B project-merge
1076 queue = self.gearman_server.getQueue()
1077 self.release(queue[0])
1078 self.waitUntilSettled()
1079
1080 # Release C
1081 self.gearman_server.hold_jobs_in_queue = False
1082 self.gearman_server.release()
1083 self.waitUntilSettled()
1084
1085 self.assertEqual(A.data['status'], 'MERGED')
1086 self.assertEqual(B.data['status'], 'NEW')
1087 self.assertEqual(C.data['status'], 'NEW')
1088 self.assertEqual(A.reported, 2)
1089 self.assertEqual(B.reported, 1)
1090 self.assertEqual(C.reported, 1)
1091
1092 self.assertHistory([
1093 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1094 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1095 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001096 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1097 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1098 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1099 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1100 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1101 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001102 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001103
James E. Blairdaabed22012-08-15 15:38:57 -07001104 def test_post(self):
1105 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001106 p = "review.example.com/org/project"
1107 upstream = self.getUpstreamRepos([p])
1108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1109 A.setMerged()
1110 A_commit = str(upstream[p].commit('master'))
1111 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001112
Zhongyue Luo5d556072012-09-21 02:00:47 +09001113 e = {
1114 "type": "ref-updated",
1115 "submitter": {
1116 "name": "User Name",
1117 },
1118 "refUpdate": {
1119 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001120 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001121 "refName": "master",
1122 "project": "org/project",
1123 }
1124 }
James E. Blairdaabed22012-08-15 15:38:57 -07001125 self.fake_gerrit.addEvent(e)
1126 self.waitUntilSettled()
1127
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001128 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001129 self.assertEqual(len(self.history), 1)
1130 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001131
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001132 def test_post_ignore_deletes(self):
1133 "Test that deleting refs does not trigger post jobs"
1134
1135 e = {
1136 "type": "ref-updated",
1137 "submitter": {
1138 "name": "User Name",
1139 },
1140 "refUpdate": {
1141 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1142 "newRev": "0000000000000000000000000000000000000000",
1143 "refName": "master",
1144 "project": "org/project",
1145 }
1146 }
1147 self.fake_gerrit.addEvent(e)
1148 self.waitUntilSettled()
1149
1150 job_names = [x.name for x in self.history]
1151 self.assertEqual(len(self.history), 0)
1152 self.assertNotIn('project-post', job_names)
1153
James E. Blaircc324192017-04-19 14:36:08 -07001154 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001155 def test_post_ignore_deletes_negative(self):
1156 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001157 e = {
1158 "type": "ref-updated",
1159 "submitter": {
1160 "name": "User Name",
1161 },
1162 "refUpdate": {
1163 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1164 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001165 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001166 "project": "org/project",
1167 }
1168 }
1169 self.fake_gerrit.addEvent(e)
1170 self.waitUntilSettled()
1171
1172 job_names = [x.name for x in self.history]
1173 self.assertEqual(len(self.history), 1)
1174 self.assertIn('project-post', job_names)
1175
James E. Blairec056492016-07-22 09:45:56 -07001176 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001177 def test_build_configuration_branch_interaction(self):
1178 "Test that switching between branches works"
1179 self.test_build_configuration()
1180 self.test_build_configuration_branch()
1181 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001182 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001183 repo = git.Repo(path)
1184 repo.heads.master.commit = repo.commit('init')
1185 self.test_build_configuration()
1186
James E. Blairdb93b302017-07-19 15:33:11 -07001187 def test_dependent_changes_rebase(self):
1188 # Test that no errors occur when we walk a dependency tree
1189 # with an unused leaf node due to a rebase.
1190 # Start by constructing: C -> B -> A
1191 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1192 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1193 B.setDependsOn(A, 1)
1194
1195 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1196 C.setDependsOn(B, 1)
1197
1198 # Then rebase to form: D -> C -> A
1199 C.addPatchset() # C,2
1200 C.setDependsOn(A, 1)
1201
1202 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1203 D.setDependsOn(C, 2)
1204
1205 # Walk the entire tree
1206 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1207 self.waitUntilSettled()
1208 self.assertEqual(len(self.history), 3)
1209
1210 # Verify that walking just part of the tree still works
1211 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1212 self.waitUntilSettled()
1213 self.assertEqual(len(self.history), 6)
1214
James E. Blaircaec0c52012-08-22 14:52:22 -07001215 def test_dependent_changes_dequeue(self):
1216 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001217
James E. Blaircaec0c52012-08-22 14:52:22 -07001218 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1219 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1220 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001221 A.addApproval('Code-Review', 2)
1222 B.addApproval('Code-Review', 2)
1223 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001224
1225 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1226 M1.setMerged()
1227
1228 # C -> B -> A -> M1
1229
1230 C.setDependsOn(B, 1)
1231 B.setDependsOn(A, 1)
1232 A.setDependsOn(M1, 1)
1233
Paul Belanger174a8272017-03-14 13:20:10 -04001234 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001235
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001236 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1237 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1238 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001239
1240 self.waitUntilSettled()
1241
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001242 self.assertEqual(A.data['status'], 'NEW')
1243 self.assertEqual(A.reported, 2)
1244 self.assertEqual(B.data['status'], 'NEW')
1245 self.assertEqual(B.reported, 2)
1246 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001247 self.assertIn('This change depends on a change that failed to merge.',
1248 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001249 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001250
James E. Blair972e3c72013-08-29 12:04:55 -07001251 def test_failing_dependent_changes(self):
1252 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001253 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001254 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1255 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1256 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1257 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1258 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001259 A.addApproval('Code-Review', 2)
1260 B.addApproval('Code-Review', 2)
1261 C.addApproval('Code-Review', 2)
1262 D.addApproval('Code-Review', 2)
1263 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001264
1265 # E, D -> C -> B, A
1266
1267 D.setDependsOn(C, 1)
1268 C.setDependsOn(B, 1)
1269
Paul Belanger174a8272017-03-14 13:20:10 -04001270 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001271
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001272 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1273 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1274 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1275 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1276 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001277
1278 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001279 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001280 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001281 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001282 self.waitUntilSettled()
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()
1289
Paul Belanger174a8272017-03-14 13:20:10 -04001290 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001291 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001292 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001293 build.release()
1294 self.waitUntilSettled()
1295
Paul Belanger174a8272017-03-14 13:20:10 -04001296 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001297 self.waitUntilSettled()
1298
1299 self.assertEqual(A.data['status'], 'MERGED')
1300 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001301 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001302 self.assertEqual(B.data['status'], 'NEW')
1303 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001304 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.assertEqual(C.data['status'], 'NEW')
1306 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001307 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001308 self.assertEqual(D.data['status'], 'NEW')
1309 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001310 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001311 self.assertEqual(E.data['status'], 'MERGED')
1312 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001313 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001314 self.assertEqual(len(self.history), 18)
1315
James E. Blairec590122012-08-22 15:19:31 -07001316 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001317 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001318 # If it's dequeued more than once, we should see extra
1319 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001320
Paul Belanger174a8272017-03-14 13:20:10 -04001321 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001322 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1323 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1324 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001325 A.addApproval('Code-Review', 2)
1326 B.addApproval('Code-Review', 2)
1327 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001328
Paul Belanger174a8272017-03-14 13:20:10 -04001329 self.executor_server.failJob('project-test1', A)
1330 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001331
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001332 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1333 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1334 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001335
1336 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001337
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001338 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001339 self.assertEqual(self.builds[0].name, 'project-merge')
1340 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001341
Paul Belanger174a8272017-03-14 13:20:10 -04001342 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001343 self.waitUntilSettled()
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()
1348
James E. Blair6df67132017-04-19 13:53:20 -07001349 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001350 self.assertEqual(self.builds[0].name, 'project-test1')
1351 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001352 self.assertEqual(self.builds[2].name, 'project-test1')
1353 self.assertEqual(self.builds[3].name, 'project-test2')
1354 self.assertEqual(self.builds[4].name, 'project-test1')
1355 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001356
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001357 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001358 self.waitUntilSettled()
1359
James E. Blair6df67132017-04-19 13:53:20 -07001360 self.assertEqual(len(self.builds), 2) # test2, merge for B
1361 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001362
Paul Belanger174a8272017-03-14 13:20:10 -04001363 self.executor_server.hold_jobs_in_build = False
1364 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001365 self.waitUntilSettled()
1366
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001367 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001368 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001369
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001370 self.assertEqual(A.data['status'], 'NEW')
1371 self.assertEqual(B.data['status'], 'MERGED')
1372 self.assertEqual(C.data['status'], 'MERGED')
1373 self.assertEqual(A.reported, 2)
1374 self.assertEqual(B.reported, 2)
1375 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001376
James E. Blaircc020532017-04-19 13:04:44 -07001377 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001378 def test_nonvoting_job(self):
1379 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001380
James E. Blair4ec821f2012-08-23 15:28:28 -07001381 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1382 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001383 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001384 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001385 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001386
1387 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001388
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001389 self.assertEqual(A.data['status'], 'MERGED')
1390 self.assertEqual(A.reported, 2)
1391 self.assertEqual(
1392 self.getJobFromHistory('nonvoting-project-merge').result,
1393 'SUCCESS')
1394 self.assertEqual(
1395 self.getJobFromHistory('nonvoting-project-test1').result,
1396 'SUCCESS')
1397 self.assertEqual(
1398 self.getJobFromHistory('nonvoting-project-test2').result,
1399 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001400
James E. Blair2ef29e92017-07-21 15:25:05 -07001401 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1402 parameters['zuul']['voting'])
1403 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1404 parameters['zuul']['voting'])
1405 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1406 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001407
James E. Blaire0487072012-08-29 17:38:31 -07001408 def test_check_queue_success(self):
1409 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001410
James E. Blaire0487072012-08-29 17:38:31 -07001411 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1412 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1413
1414 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001415
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001416 self.assertEqual(A.data['status'], 'NEW')
1417 self.assertEqual(A.reported, 1)
1418 self.assertEqual(self.getJobFromHistory('project-merge').result,
1419 'SUCCESS')
1420 self.assertEqual(self.getJobFromHistory('project-test1').result,
1421 'SUCCESS')
1422 self.assertEqual(self.getJobFromHistory('project-test2').result,
1423 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001424
1425 def test_check_queue_failure(self):
1426 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001427
James E. Blaire0487072012-08-29 17:38:31 -07001428 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001429 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001430 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1431
1432 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001433
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001434 self.assertEqual(A.data['status'], 'NEW')
1435 self.assertEqual(A.reported, 1)
1436 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001437 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001438 self.assertEqual(self.getJobFromHistory('project-test1').result,
1439 'SUCCESS')
1440 self.assertEqual(self.getJobFromHistory('project-test2').result,
1441 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001442
David Shrewsburyffab07a2017-07-24 12:45:07 -04001443 @simple_layout('layouts/autohold.yaml')
1444 def test_autohold(self):
1445 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1446
1447 client = zuul.rpcclient.RPCClient('127.0.0.1',
1448 self.gearman_server.port)
1449 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001450 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1451 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001452 self.assertTrue(r)
1453
1454 self.executor_server.failJob('project-test2', A)
1455 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1456
1457 self.waitUntilSettled()
1458
1459 self.assertEqual(A.data['status'], 'NEW')
1460 self.assertEqual(A.reported, 1)
1461 self.assertEqual(self.getJobFromHistory('project-test2').result,
1462 'FAILURE')
1463
1464 # Check nodepool for a held node
1465 held_node = None
1466 for node in self.fake_nodepool.getNodes():
1467 if node['state'] == zuul.model.STATE_HOLD:
1468 held_node = node
1469 break
1470 self.assertIsNotNone(held_node)
1471
1472 # Validate node has recorded the failed job
1473 self.assertEqual(
1474 held_node['hold_job'],
1475 " ".join(['tenant-one',
1476 'review.example.com/org/project',
1477 'project-test2'])
1478 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001479 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001480
1481 # Another failed change should not hold any more nodes
1482 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1483 self.executor_server.failJob('project-test2', B)
1484 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1485 self.waitUntilSettled()
1486 self.assertEqual(B.data['status'], 'NEW')
1487 self.assertEqual(B.reported, 1)
1488 self.assertEqual(self.getJobFromHistory('project-test2').result,
1489 'FAILURE')
1490
1491 held_nodes = 0
1492 for node in self.fake_nodepool.getNodes():
1493 if node['state'] == zuul.model.STATE_HOLD:
1494 held_nodes += 1
1495 self.assertEqual(held_nodes, 1)
1496
James E. Blair7cb84542017-04-19 13:35:05 -07001497 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001498 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001499 # This particular test does a large amount of merges and needs a little
1500 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001501 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001502 "test that dependent changes behind dequeued changes work"
1503 # This complicated test is a reproduction of a real life bug
1504 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001505
Paul Belanger174a8272017-03-14 13:20:10 -04001506 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001507 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1508 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1509 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1510 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1511 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1512 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1513 D.setDependsOn(C, 1)
1514 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001515 A.addApproval('Code-Review', 2)
1516 B.addApproval('Code-Review', 2)
1517 C.addApproval('Code-Review', 2)
1518 D.addApproval('Code-Review', 2)
1519 E.addApproval('Code-Review', 2)
1520 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001521
1522 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001523
1524 # Change object re-use in the gerrit trigger is hidden if
1525 # changes are added in quick succession; waiting makes it more
1526 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001527 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001528 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001529 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001530 self.waitUntilSettled()
1531
Paul Belanger174a8272017-03-14 13:20:10 -04001532 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001533 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001534 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001535 self.waitUntilSettled()
1536
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001537 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001538 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001539 self.fake_gerrit.addEvent(D.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(E.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(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001544 self.waitUntilSettled()
1545
Paul Belanger174a8272017-03-14 13:20:10 -04001546 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001547 self.waitUntilSettled()
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()
1554
1555 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001556
1557 # Grab pointers to the jobs we want to release before
1558 # releasing any, because list indexes may change as
1559 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001560 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001561 a.release()
1562 b.release()
1563 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001564 self.waitUntilSettled()
1565
Paul Belanger174a8272017-03-14 13:20:10 -04001566 self.executor_server.hold_jobs_in_build = False
1567 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001568 self.waitUntilSettled()
1569
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001570 self.assertEqual(A.data['status'], 'NEW')
1571 self.assertEqual(B.data['status'], 'MERGED')
1572 self.assertEqual(C.data['status'], 'MERGED')
1573 self.assertEqual(D.data['status'], 'MERGED')
1574 self.assertEqual(E.data['status'], 'MERGED')
1575 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001576
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001577 self.assertEqual(A.reported, 2)
1578 self.assertEqual(B.reported, 2)
1579 self.assertEqual(C.reported, 2)
1580 self.assertEqual(D.reported, 2)
1581 self.assertEqual(E.reported, 2)
1582 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001583
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001584 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1585 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001586
1587 def test_merger_repack(self):
1588 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001589
James E. Blair05fed602012-09-07 12:45:24 -07001590 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001591 A.addApproval('Code-Review', 2)
1592 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001593 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001594 self.assertEqual(self.getJobFromHistory('project-merge').result,
1595 'SUCCESS')
1596 self.assertEqual(self.getJobFromHistory('project-test1').result,
1597 'SUCCESS')
1598 self.assertEqual(self.getJobFromHistory('project-test2').result,
1599 'SUCCESS')
1600 self.assertEqual(A.data['status'], 'MERGED')
1601 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001602 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001603 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001604
James E. Blair2a535672017-04-27 12:03:15 -07001605 path = os.path.join(self.merger_src_root, "review.example.com",
1606 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001607 if os.path.exists(path):
1608 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001609 path = os.path.join(self.executor_src_root, "review.example.com",
1610 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001611 if os.path.exists(path):
1612 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001613
1614 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001615 A.addApproval('Code-Review', 2)
1616 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001617 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001618 self.assertEqual(self.getJobFromHistory('project-merge').result,
1619 'SUCCESS')
1620 self.assertEqual(self.getJobFromHistory('project-test1').result,
1621 'SUCCESS')
1622 self.assertEqual(self.getJobFromHistory('project-test2').result,
1623 'SUCCESS')
1624 self.assertEqual(A.data['status'], 'MERGED')
1625 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001626
James E. Blair4886f282012-11-15 09:27:33 -08001627 def test_merger_repack_large_change(self):
1628 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001629 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001630 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001631 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001632 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001633 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001634 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001635 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001637 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001638 # TODOv3(jeblair): add hostname to upstream root
1639 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001640 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001641 path = os.path.join(self.merger_src_root, 'review.example.com',
1642 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001643 if os.path.exists(path):
1644 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001645 path = os.path.join(self.executor_src_root, 'review.example.com',
1646 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001647 if os.path.exists(path):
1648 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001649
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001650 A.addApproval('Code-Review', 2)
1651 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001652 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001653 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001654 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001655 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001656 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001657 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001658 'SUCCESS')
1659 self.assertEqual(A.data['status'], 'MERGED')
1660 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001661
James E. Blair2fa50962013-01-30 21:50:41 -08001662 def test_new_patchset_dequeues_old(self):
1663 "Test that a new patchset causes the old to be dequeued"
1664 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001665 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001666 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1667 M.setMerged()
1668
1669 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1670 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1671 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1672 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001673 A.addApproval('Code-Review', 2)
1674 B.addApproval('Code-Review', 2)
1675 C.addApproval('Code-Review', 2)
1676 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001677
1678 C.setDependsOn(B, 1)
1679 B.setDependsOn(A, 1)
1680 A.setDependsOn(M, 1)
1681
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001682 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1683 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1684 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1685 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001686 self.waitUntilSettled()
1687
1688 B.addPatchset()
1689 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1690 self.waitUntilSettled()
1691
Paul Belanger174a8272017-03-14 13:20:10 -04001692 self.executor_server.hold_jobs_in_build = False
1693 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001694 self.waitUntilSettled()
1695
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001696 self.assertEqual(A.data['status'], 'MERGED')
1697 self.assertEqual(A.reported, 2)
1698 self.assertEqual(B.data['status'], 'NEW')
1699 self.assertEqual(B.reported, 2)
1700 self.assertEqual(C.data['status'], 'NEW')
1701 self.assertEqual(C.reported, 2)
1702 self.assertEqual(D.data['status'], 'MERGED')
1703 self.assertEqual(D.reported, 2)
1704 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001705
James E. Blairba437362015-02-07 11:41:52 -08001706 def test_new_patchset_check(self):
1707 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001708
Paul Belanger174a8272017-03-14 13:20:10 -04001709 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001710
1711 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001712 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001713 tenant = self.sched.abide.tenants.get('tenant-one')
1714 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001715
1716 # Add two git-dependent changes
1717 B.setDependsOn(A, 1)
1718 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1719 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001720 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1721 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001722
1723 # A live item, and a non-live/live pair
1724 items = check_pipeline.getAllItems()
1725 self.assertEqual(len(items), 3)
1726
1727 self.assertEqual(items[0].change.number, '1')
1728 self.assertEqual(items[0].change.patchset, '1')
1729 self.assertFalse(items[0].live)
1730
1731 self.assertEqual(items[1].change.number, '2')
1732 self.assertEqual(items[1].change.patchset, '1')
1733 self.assertTrue(items[1].live)
1734
1735 self.assertEqual(items[2].change.number, '1')
1736 self.assertEqual(items[2].change.patchset, '1')
1737 self.assertTrue(items[2].live)
1738
1739 # Add a new patchset to A
1740 A.addPatchset()
1741 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1742 self.waitUntilSettled()
1743
1744 # The live copy of A,1 should be gone, but the non-live and B
1745 # should continue, and we should have a new A,2
1746 items = check_pipeline.getAllItems()
1747 self.assertEqual(len(items), 3)
1748
1749 self.assertEqual(items[0].change.number, '1')
1750 self.assertEqual(items[0].change.patchset, '1')
1751 self.assertFalse(items[0].live)
1752
1753 self.assertEqual(items[1].change.number, '2')
1754 self.assertEqual(items[1].change.patchset, '1')
1755 self.assertTrue(items[1].live)
1756
1757 self.assertEqual(items[2].change.number, '1')
1758 self.assertEqual(items[2].change.patchset, '2')
1759 self.assertTrue(items[2].live)
1760
1761 # Add a new patchset to B
1762 B.addPatchset()
1763 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1764 self.waitUntilSettled()
1765
1766 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1767 # but we should have a new B,2 (still based on A,1)
1768 items = check_pipeline.getAllItems()
1769 self.assertEqual(len(items), 3)
1770
1771 self.assertEqual(items[0].change.number, '1')
1772 self.assertEqual(items[0].change.patchset, '2')
1773 self.assertTrue(items[0].live)
1774
1775 self.assertEqual(items[1].change.number, '1')
1776 self.assertEqual(items[1].change.patchset, '1')
1777 self.assertFalse(items[1].live)
1778
1779 self.assertEqual(items[2].change.number, '2')
1780 self.assertEqual(items[2].change.patchset, '2')
1781 self.assertTrue(items[2].live)
1782
1783 self.builds[0].release()
1784 self.waitUntilSettled()
1785 self.builds[0].release()
1786 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001787 self.executor_server.hold_jobs_in_build = False
1788 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001789 self.waitUntilSettled()
1790
1791 self.assertEqual(A.reported, 1)
1792 self.assertEqual(B.reported, 1)
1793 self.assertEqual(self.history[0].result, 'ABORTED')
1794 self.assertEqual(self.history[0].changes, '1,1')
1795 self.assertEqual(self.history[1].result, 'ABORTED')
1796 self.assertEqual(self.history[1].changes, '1,1 2,1')
1797 self.assertEqual(self.history[2].result, 'SUCCESS')
1798 self.assertEqual(self.history[2].changes, '1,2')
1799 self.assertEqual(self.history[3].result, 'SUCCESS')
1800 self.assertEqual(self.history[3].changes, '1,1 2,2')
1801
1802 def test_abandoned_gate(self):
1803 "Test that an abandoned change is dequeued from gate"
1804
Paul Belanger174a8272017-03-14 13:20:10 -04001805 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001806
1807 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001808 A.addApproval('Code-Review', 2)
1809 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001810 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001811 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1812 self.assertEqual(self.builds[0].name, 'project-merge')
1813
1814 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1815 self.waitUntilSettled()
1816
Paul Belanger174a8272017-03-14 13:20:10 -04001817 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001818 self.waitUntilSettled()
1819
Arie5b4048c2016-10-05 16:12:06 +03001820 self.assertBuilds([])
1821 self.assertHistory([
1822 dict(name='project-merge', result='ABORTED', changes='1,1')],
1823 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001824 self.assertEqual(A.reported, 1,
1825 "Abandoned gate change should report only start")
1826
1827 def test_abandoned_check(self):
1828 "Test that an abandoned change is dequeued from check"
1829
Paul Belanger174a8272017-03-14 13:20:10 -04001830 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001831
1832 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1833 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001834 tenant = self.sched.abide.tenants.get('tenant-one')
1835 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001836
1837 # Add two git-dependent changes
1838 B.setDependsOn(A, 1)
1839 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1840 self.waitUntilSettled()
1841 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1842 self.waitUntilSettled()
1843 # A live item, and a non-live/live pair
1844 items = check_pipeline.getAllItems()
1845 self.assertEqual(len(items), 3)
1846
1847 self.assertEqual(items[0].change.number, '1')
1848 self.assertFalse(items[0].live)
1849
1850 self.assertEqual(items[1].change.number, '2')
1851 self.assertTrue(items[1].live)
1852
1853 self.assertEqual(items[2].change.number, '1')
1854 self.assertTrue(items[2].live)
1855
1856 # Abandon A
1857 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1858 self.waitUntilSettled()
1859
1860 # The live copy of A should be gone, but the non-live and B
1861 # should continue
1862 items = check_pipeline.getAllItems()
1863 self.assertEqual(len(items), 2)
1864
1865 self.assertEqual(items[0].change.number, '1')
1866 self.assertFalse(items[0].live)
1867
1868 self.assertEqual(items[1].change.number, '2')
1869 self.assertTrue(items[1].live)
1870
Paul Belanger174a8272017-03-14 13:20:10 -04001871 self.executor_server.hold_jobs_in_build = False
1872 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001873 self.waitUntilSettled()
1874
1875 self.assertEqual(len(self.history), 4)
1876 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001877 'Build should have been aborted')
1878 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001879 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001880
Steve Varnau7b78b312015-04-03 14:49:46 -07001881 def test_abandoned_not_timer(self):
1882 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001883 # This test can not use simple_layout because it must start
1884 # with a configuration which does not include a
1885 # timer-triggered job so that we have an opportunity to set
1886 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001887 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001888 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001889 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001890 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001891 # The pipeline triggers every second, so we should have seen
1892 # several by now.
1893 time.sleep(5)
1894 self.waitUntilSettled()
1895 # Stop queuing timer triggered jobs so that the assertions
1896 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001897 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001898 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001899 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001900 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001901 # If APScheduler is in mid-event when we remove the job, we
1902 # can end up with one more event firing, so give it an extra
1903 # second to settle.
1904 time.sleep(1)
1905 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001906
James E. Blair21037782017-07-19 11:56:55 -07001907 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001908
1909 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1910 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1911 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001912 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001913
1914 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1915 self.waitUntilSettled()
1916
James E. Blair21037782017-07-19 11:56:55 -07001917 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001918
Paul Belanger174a8272017-03-14 13:20:10 -04001919 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001920 self.waitUntilSettled()
1921
James E. Blair2fa50962013-01-30 21:50:41 -08001922 def test_new_patchset_dequeues_old_on_head(self):
1923 "Test that a new patchset causes the old to be dequeued (at head)"
1924 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001925 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001926 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1927 M.setMerged()
1928 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1929 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1930 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1931 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001932 A.addApproval('Code-Review', 2)
1933 B.addApproval('Code-Review', 2)
1934 C.addApproval('Code-Review', 2)
1935 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001936
1937 C.setDependsOn(B, 1)
1938 B.setDependsOn(A, 1)
1939 A.setDependsOn(M, 1)
1940
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001941 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1942 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1943 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1944 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001945 self.waitUntilSettled()
1946
1947 A.addPatchset()
1948 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1949 self.waitUntilSettled()
1950
Paul Belanger174a8272017-03-14 13:20:10 -04001951 self.executor_server.hold_jobs_in_build = False
1952 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001953 self.waitUntilSettled()
1954
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001955 self.assertEqual(A.data['status'], 'NEW')
1956 self.assertEqual(A.reported, 2)
1957 self.assertEqual(B.data['status'], 'NEW')
1958 self.assertEqual(B.reported, 2)
1959 self.assertEqual(C.data['status'], 'NEW')
1960 self.assertEqual(C.reported, 2)
1961 self.assertEqual(D.data['status'], 'MERGED')
1962 self.assertEqual(D.reported, 2)
1963 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001964
1965 def test_new_patchset_dequeues_old_without_dependents(self):
1966 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001967 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001968 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1969 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1970 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001971 A.addApproval('Code-Review', 2)
1972 B.addApproval('Code-Review', 2)
1973 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001974
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001975 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1976 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1977 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001978 self.waitUntilSettled()
1979
1980 B.addPatchset()
1981 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1982 self.waitUntilSettled()
1983
Paul Belanger174a8272017-03-14 13:20:10 -04001984 self.executor_server.hold_jobs_in_build = False
1985 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001986 self.waitUntilSettled()
1987
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001988 self.assertEqual(A.data['status'], 'MERGED')
1989 self.assertEqual(A.reported, 2)
1990 self.assertEqual(B.data['status'], 'NEW')
1991 self.assertEqual(B.reported, 2)
1992 self.assertEqual(C.data['status'], 'MERGED')
1993 self.assertEqual(C.reported, 2)
1994 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001995
1996 def test_new_patchset_dequeues_old_independent_queue(self):
1997 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001998 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001999 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2000 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2001 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2002 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2003 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2004 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2005 self.waitUntilSettled()
2006
2007 B.addPatchset()
2008 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2009 self.waitUntilSettled()
2010
Paul Belanger174a8272017-03-14 13:20:10 -04002011 self.executor_server.hold_jobs_in_build = False
2012 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002013 self.waitUntilSettled()
2014
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002015 self.assertEqual(A.data['status'], 'NEW')
2016 self.assertEqual(A.reported, 1)
2017 self.assertEqual(B.data['status'], 'NEW')
2018 self.assertEqual(B.reported, 1)
2019 self.assertEqual(C.data['status'], 'NEW')
2020 self.assertEqual(C.reported, 1)
2021 self.assertEqual(len(self.history), 10)
2022 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002023
James E. Blaircc020532017-04-19 13:04:44 -07002024 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002025 def test_noop_job(self):
2026 "Test that the internal noop job works"
2027 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002028 A.addApproval('Code-Review', 2)
2029 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002030 self.waitUntilSettled()
2031
2032 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2033 self.assertTrue(self.sched._areAllBuildsComplete())
2034 self.assertEqual(len(self.history), 0)
2035 self.assertEqual(A.data['status'], 'MERGED')
2036 self.assertEqual(A.reported, 2)
2037
James E. Blair06cc3922017-04-19 10:08:10 -07002038 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002039 def test_no_job_project(self):
2040 "Test that reports with no jobs don't get sent"
2041 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2042 'master', 'A')
2043 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2044 self.waitUntilSettled()
2045
2046 # Change wasn't reported to
2047 self.assertEqual(A.reported, False)
2048
2049 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002050 tenant = self.sched.abide.tenants.get('tenant-one')
2051 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002052 items = check_pipeline.getAllItems()
2053 self.assertEqual(len(items), 0)
2054
2055 self.assertEqual(len(self.history), 0)
2056
James E. Blair7d0dedc2013-02-21 17:26:09 -08002057 def test_zuul_refs(self):
2058 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002059 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002060 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2061 M1.setMerged()
2062 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2063 M2.setMerged()
2064
2065 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2066 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2067 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2068 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002069 A.addApproval('Code-Review', 2)
2070 B.addApproval('Code-Review', 2)
2071 C.addApproval('Code-Review', 2)
2072 D.addApproval('Code-Review', 2)
2073 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2074 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2075 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2076 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002077
2078 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002079 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002080 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()
2087
Clint Byrum3343e3e2016-11-15 16:05:03 -08002088 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002089 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002090 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002091 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002092 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002093 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002094 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002096 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002097 d_build = x
2098 if a_build and b_build and c_build and d_build:
2099 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002100
Clint Byrum3343e3e2016-11-15 16:05:03 -08002101 # should have a, not b, and should not be in project2
2102 self.assertTrue(a_build.hasChanges(A))
2103 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002104
Clint Byrum3343e3e2016-11-15 16:05:03 -08002105 # should have a and b, and should not be in project2
2106 self.assertTrue(b_build.hasChanges(A, B))
2107 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002108
Clint Byrum3343e3e2016-11-15 16:05:03 -08002109 # should have a and b in 1, c in 2
2110 self.assertTrue(c_build.hasChanges(A, B, C))
2111 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002112
Clint Byrum3343e3e2016-11-15 16:05:03 -08002113 # should have a and b in 1, c and d in 2
2114 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002115
Paul Belanger174a8272017-03-14 13:20:10 -04002116 self.executor_server.hold_jobs_in_build = False
2117 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002118 self.waitUntilSettled()
2119
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002120 self.assertEqual(A.data['status'], 'MERGED')
2121 self.assertEqual(A.reported, 2)
2122 self.assertEqual(B.data['status'], 'MERGED')
2123 self.assertEqual(B.reported, 2)
2124 self.assertEqual(C.data['status'], 'MERGED')
2125 self.assertEqual(C.reported, 2)
2126 self.assertEqual(D.data['status'], 'MERGED')
2127 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002128
James E. Blair4a28a882013-08-23 15:17:33 -07002129 def test_rerun_on_error(self):
2130 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002131 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002132 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002133 A.addApproval('Code-Review', 2)
2134 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002135 self.waitUntilSettled()
2136
James E. Blair412fba82017-01-26 15:00:50 -08002137 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002138 self.executor_server.hold_jobs_in_build = False
2139 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002140 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002141 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002142 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2143
James E. Blair412e5582013-04-22 15:50:12 -07002144 def test_statsd(self):
2145 "Test each of the statsd methods used in the scheduler"
2146 import extras
2147 statsd = extras.try_import('statsd.statsd')
2148 statsd.incr('test-incr')
2149 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002150 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002151 self.assertReportedStat('test-incr', '1|c')
2152 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002153 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002154
James E. Blairdad52252014-02-07 16:59:17 -08002155 def test_stuck_job_cleanup(self):
2156 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002157
2158 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002159 self.gearman_server.hold_jobs_in_queue = True
2160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002161 A.addApproval('Code-Review', 2)
2162 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002163 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002164 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002165 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2166
James E. Blair31cb3e82017-04-20 13:08:56 -07002167 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002168 self.sched.reconfigure(self.config)
2169 self.waitUntilSettled()
2170
James E. Blair18c64442014-03-18 10:14:45 -07002171 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002172 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002173 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002174 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2175 self.assertTrue(self.sched._areAllBuildsComplete())
2176
2177 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002178 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002179 self.assertEqual(self.history[0].result, 'SUCCESS')
2180
James E. Blair879dafb2015-07-17 14:04:49 -07002181 def test_file_head(self):
2182 # This is a regression test for an observed bug. A change
2183 # with a file named "HEAD" in the root directory of the repo
2184 # was processed by a merger. It then was unable to reset the
2185 # repo because of:
2186 # GitCommandError: 'git reset --hard HEAD' returned
2187 # with exit code 128
2188 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2189 # and filename
2190 # Use '--' to separate filenames from revisions'
2191
2192 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002193 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002194 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2195
2196 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2197 self.waitUntilSettled()
2198
2199 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2200 self.waitUntilSettled()
2201
2202 self.assertIn('Build succeeded', A.messages[0])
2203 self.assertIn('Build succeeded', B.messages[0])
2204
James E. Blair70c71582013-03-06 08:50:50 -08002205 def test_file_jobs(self):
2206 "Test that file jobs run only when appropriate"
2207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002208 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002209 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002210 A.addApproval('Code-Review', 2)
2211 B.addApproval('Code-Review', 2)
2212 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2213 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002214 self.waitUntilSettled()
2215
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002216 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002217 if x.name == 'project-testfile']
2218
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002219 self.assertEqual(len(testfile_jobs), 1)
2220 self.assertEqual(testfile_jobs[0].changes, '1,2')
2221 self.assertEqual(A.data['status'], 'MERGED')
2222 self.assertEqual(A.reported, 2)
2223 self.assertEqual(B.data['status'], 'MERGED')
2224 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002225
Clint Byrum3ee39f32016-11-17 23:45:07 -08002226 def _test_irrelevant_files_jobs(self, should_skip):
2227 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002228 if should_skip:
2229 files = {'ignoreme': 'ignored\n'}
2230 else:
2231 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002232
2233 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002234 'master',
2235 'test irrelevant-files',
2236 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002237 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2238 self.waitUntilSettled()
2239
2240 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002241 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002242
2243 if should_skip:
2244 self.assertEqual([], tested_change_ids)
2245 else:
2246 self.assertIn(change.data['number'], tested_change_ids)
2247
James E. Blaircc324192017-04-19 14:36:08 -07002248 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002249 def test_irrelevant_files_match_skips_job(self):
2250 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002251
James E. Blaircc324192017-04-19 14:36:08 -07002252 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002253 def test_irrelevant_files_no_match_runs_job(self):
2254 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002255
James E. Blaircc324192017-04-19 14:36:08 -07002256 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002257 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002258 files = {'ignoreme': 'ignored\n'}
2259
2260 change = self.fake_gerrit.addFakeChange('org/project',
2261 'master',
2262 'test irrelevant-files',
2263 files=files)
2264 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2265 self.waitUntilSettled()
2266
2267 run_jobs = set([build.name for build in self.history])
2268
2269 self.assertEqual(set(['project-test-nomatch-starts-empty',
2270 'project-test-nomatch-starts-full']), run_jobs)
2271
James E. Blairc8a1e052014-02-25 09:29:26 -08002272 def test_queue_names(self):
2273 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002274 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002275 (trusted, project1) = tenant.getProject('org/project1')
2276 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002277 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2278 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2279 self.assertEqual(q1.name, 'integrated')
2280 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002281
James E. Blair64ed6f22013-07-10 14:07:23 -07002282 def test_queue_precedence(self):
2283 "Test that queue precedence works"
2284
2285 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002286 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002287 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002289 A.addApproval('Code-Review', 2)
2290 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002291
2292 self.waitUntilSettled()
2293 self.gearman_server.hold_jobs_in_queue = False
2294 self.gearman_server.release()
2295 self.waitUntilSettled()
2296
James E. Blair8de58bd2013-07-18 16:23:33 -07002297 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002298 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002299 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002300 self.waitUntilSettled()
2301
James E. Blair64ed6f22013-07-10 14:07:23 -07002302 self.log.debug(self.history)
2303 self.assertEqual(self.history[0].pipeline, 'gate')
2304 self.assertEqual(self.history[1].pipeline, 'check')
2305 self.assertEqual(self.history[2].pipeline, 'gate')
2306 self.assertEqual(self.history[3].pipeline, 'gate')
2307 self.assertEqual(self.history[4].pipeline, 'check')
2308 self.assertEqual(self.history[5].pipeline, 'check')
2309
Clark Boylana5edbe42014-06-03 16:39:10 -07002310 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002311 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002312 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002313 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002314 A.addApproval('Code-Review', 2)
2315 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002316 self.waitUntilSettled()
2317
Paul Belanger174a8272017-03-14 13:20:10 -04002318 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002319 self.waitUntilSettled()
2320
James E. Blair1843a552013-07-03 14:19:52 -07002321 port = self.webapp.server.socket.getsockname()[1]
2322
Adam Gandelman77a12c72017-02-03 17:43:43 -08002323 req = urllib.request.Request(
2324 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002325 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002326 headers = f.info()
2327 self.assertIn('Content-Length', headers)
2328 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002329 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2330 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002331 self.assertIn('Access-Control-Allow-Origin', headers)
2332 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002333 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002334 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002335 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002336
Paul Belanger174a8272017-03-14 13:20:10 -04002337 self.executor_server.hold_jobs_in_build = False
2338 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002339 self.waitUntilSettled()
2340
2341 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002342 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002343 for p in data['pipelines']:
2344 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002345 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002346 self.assertEqual(q['window'], 20)
2347 else:
2348 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002349 for head in q['heads']:
2350 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002351 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002352 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002353 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002354 status_jobs.append(job)
2355 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002356 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002357 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002358 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2359 uuid=status_jobs[0]['uuid']),
2360 status_jobs[0]['url'])
2361 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002362 'finger://{hostname}/{uuid}'.format(
2363 hostname=self.executor_server.hostname,
2364 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002365 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002366 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002367 self.assertEqual(
2368 'finger://{hostname}/{uuid}'.format(
2369 hostname=self.executor_server.hostname,
2370 uuid=status_jobs[0]['uuid']),
2371 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002372 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002373 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002374 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2375 uuid=status_jobs[1]['uuid']),
2376 status_jobs[1]['url'])
2377 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002378 'finger://{hostname}/{uuid}'.format(
2379 hostname=self.executor_server.hostname,
2380 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002381 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002382 self.assertEqual(
2383 'finger://{hostname}/{uuid}'.format(
2384 hostname=self.executor_server.hostname,
2385 uuid=status_jobs[1]['uuid']),
2386 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002387
2388 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002389 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002390 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2391 uuid=status_jobs[2]['uuid']),
2392 status_jobs[2]['url'])
2393 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002394 'finger://{hostname}/{uuid}'.format(
2395 hostname=self.executor_server.hostname,
2396 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002397 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002398 self.assertEqual(
2399 'finger://{hostname}/{uuid}'.format(
2400 hostname=self.executor_server.hostname,
2401 uuid=status_jobs[2]['uuid']),
2402 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002403
Tobias Henkel65639f82017-07-10 10:25:42 +02002404 # check job dependencies
2405 self.assertIsNotNone(status_jobs[0]['dependencies'])
2406 self.assertIsNotNone(status_jobs[1]['dependencies'])
2407 self.assertIsNotNone(status_jobs[2]['dependencies'])
2408 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2409 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2410 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2411 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2412 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2413
James E. Blaircdccd972013-07-01 12:10:22 -07002414 def test_live_reconfiguration(self):
2415 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002416 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002417 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002418 A.addApproval('Code-Review', 2)
2419 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002420 self.waitUntilSettled()
2421
2422 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002423 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002424
Paul Belanger174a8272017-03-14 13:20:10 -04002425 self.executor_server.hold_jobs_in_build = False
2426 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002427 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002428 self.assertEqual(self.getJobFromHistory('project-merge').result,
2429 'SUCCESS')
2430 self.assertEqual(self.getJobFromHistory('project-test1').result,
2431 'SUCCESS')
2432 self.assertEqual(self.getJobFromHistory('project-test2').result,
2433 'SUCCESS')
2434 self.assertEqual(A.data['status'], 'MERGED')
2435 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002436
James E. Blair027ba992017-09-20 13:48:32 -07002437 def test_live_reconfiguration_abort(self):
2438 # Raise an exception during reconfiguration and verify we
2439 # still function.
2440 self.executor_server.hold_jobs_in_build = True
2441 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2442 A.addApproval('Code-Review', 2)
2443 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2444 self.waitUntilSettled()
2445
2446 tenant = self.sched.abide.tenants.get('tenant-one')
2447 pipeline = tenant.layout.pipelines['gate']
2448 change = pipeline.getAllItems()[0].change
2449 # Set this to an invalid value to cause an exception during
2450 # reconfiguration.
2451 change.branch = None
2452
2453 self.sched.reconfigure(self.config)
2454 self.waitUntilSettled()
2455
2456 self.executor_server.hold_jobs_in_build = False
2457 self.executor_server.release()
2458
2459 self.waitUntilSettled()
2460 self.assertEqual(self.getJobFromHistory('project-merge').result,
2461 'ABORTED')
2462 self.assertEqual(A.data['status'], 'NEW')
2463 # The final report fails because of the invalid value set above.
2464 self.assertEqual(A.reported, 1)
2465
James E. Blair6bc782d2015-07-17 16:20:21 -07002466 def test_live_reconfiguration_merge_conflict(self):
2467 # A real-world bug: a change in a gate queue has a merge
2468 # conflict and a job is added to its project while it's
2469 # sitting in the queue. The job gets added to the change and
2470 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002471 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002472
2473 # This change is fine. It's here to stop the queue long
2474 # enough for the next change to be subject to the
2475 # reconfiguration, as well as to provide a conflict for the
2476 # next change. This change will succeed and merge.
2477 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002478 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002479 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002480
2481 # This change will be in merge conflict. During the
2482 # reconfiguration, we will add a job. We want to make sure
2483 # that doesn't cause it to get stuck.
2484 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002485 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002486 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002487
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002488 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2489 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002490
2491 self.waitUntilSettled()
2492
2493 # No jobs have run yet
2494 self.assertEqual(A.data['status'], 'NEW')
2495 self.assertEqual(A.reported, 1)
2496 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002497 self.assertEqual(len(self.history), 0)
2498
2499 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002500 self.commitConfigUpdate('common-config',
2501 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002502 self.sched.reconfigure(self.config)
2503 self.waitUntilSettled()
2504
Paul Belanger174a8272017-03-14 13:20:10 -04002505 self.executor_server.hold_jobs_in_build = False
2506 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002507 self.waitUntilSettled()
2508
2509 self.assertEqual(A.data['status'], 'MERGED')
2510 self.assertEqual(A.reported, 2)
2511 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002512 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002513 self.assertEqual(self.getJobFromHistory('project-merge').result,
2514 'SUCCESS')
2515 self.assertEqual(self.getJobFromHistory('project-test1').result,
2516 'SUCCESS')
2517 self.assertEqual(self.getJobFromHistory('project-test2').result,
2518 'SUCCESS')
2519 self.assertEqual(self.getJobFromHistory('project-test3').result,
2520 'SUCCESS')
2521 self.assertEqual(len(self.history), 4)
2522
James E. Blair400e8fd2015-07-30 17:44:45 -07002523 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002524 # An extrapolation of test_live_reconfiguration_merge_conflict
2525 # that tests a job added to a job tree with a failed root does
2526 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002527 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002528
2529 # This change is fine. It's here to stop the queue long
2530 # enough for the next change to be subject to the
2531 # reconfiguration. This change will succeed and merge.
2532 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002533 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002534 A.addApproval('Code-Review', 2)
2535 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002536 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002537 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002538 self.waitUntilSettled()
2539
2540 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002541 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002542 B.addApproval('Code-Review', 2)
2543 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002544 self.waitUntilSettled()
2545
Paul Belanger174a8272017-03-14 13:20:10 -04002546 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002547 self.waitUntilSettled()
2548
2549 # Both -merge jobs have run, but no others.
2550 self.assertEqual(A.data['status'], 'NEW')
2551 self.assertEqual(A.reported, 1)
2552 self.assertEqual(B.data['status'], 'NEW')
2553 self.assertEqual(B.reported, 1)
2554 self.assertEqual(self.history[0].result, 'SUCCESS')
2555 self.assertEqual(self.history[0].name, 'project-merge')
2556 self.assertEqual(self.history[1].result, 'FAILURE')
2557 self.assertEqual(self.history[1].name, 'project-merge')
2558 self.assertEqual(len(self.history), 2)
2559
2560 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002561 self.commitConfigUpdate('common-config',
2562 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002563 self.sched.reconfigure(self.config)
2564 self.waitUntilSettled()
2565
Paul Belanger174a8272017-03-14 13:20:10 -04002566 self.executor_server.hold_jobs_in_build = False
2567 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002568 self.waitUntilSettled()
2569
2570 self.assertEqual(A.data['status'], 'MERGED')
2571 self.assertEqual(A.reported, 2)
2572 self.assertEqual(B.data['status'], 'NEW')
2573 self.assertEqual(B.reported, 2)
2574 self.assertEqual(self.history[0].result, 'SUCCESS')
2575 self.assertEqual(self.history[0].name, 'project-merge')
2576 self.assertEqual(self.history[1].result, 'FAILURE')
2577 self.assertEqual(self.history[1].name, 'project-merge')
2578 self.assertEqual(self.history[2].result, 'SUCCESS')
2579 self.assertEqual(self.history[3].result, 'SUCCESS')
2580 self.assertEqual(self.history[4].result, 'SUCCESS')
2581 self.assertEqual(len(self.history), 5)
2582
James E. Blair400e8fd2015-07-30 17:44:45 -07002583 def test_live_reconfiguration_failed_job(self):
2584 # Test that a change with a removed failing job does not
2585 # disrupt reconfiguration. If a change has a failed job and
2586 # that job is removed during a reconfiguration, we observed a
2587 # bug where the code to re-set build statuses would run on
2588 # that build and raise an exception because the job no longer
2589 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002590 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002591
2592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2593
2594 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002595 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002596
2597 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2598 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002599 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002600 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002601 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002602 self.waitUntilSettled()
2603
2604 self.assertEqual(A.data['status'], 'NEW')
2605 self.assertEqual(A.reported, 0)
2606
2607 self.assertEqual(self.getJobFromHistory('project-merge').result,
2608 'SUCCESS')
2609 self.assertEqual(self.getJobFromHistory('project-test1').result,
2610 'FAILURE')
2611 self.assertEqual(len(self.history), 2)
2612
2613 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002614 self.commitConfigUpdate('common-config',
2615 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002616 self.sched.reconfigure(self.config)
2617 self.waitUntilSettled()
2618
Paul Belanger174a8272017-03-14 13:20:10 -04002619 self.executor_server.hold_jobs_in_build = False
2620 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002621 self.waitUntilSettled()
2622
2623 self.assertEqual(self.getJobFromHistory('project-test2').result,
2624 'SUCCESS')
2625 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2626 'SUCCESS')
2627 self.assertEqual(len(self.history), 4)
2628
2629 self.assertEqual(A.data['status'], 'NEW')
2630 self.assertEqual(A.reported, 1)
2631 self.assertIn('Build succeeded', A.messages[0])
2632 # Ensure the removed job was not included in the report.
2633 self.assertNotIn('project-test1', A.messages[0])
2634
James E. Blairfe707d12015-08-05 15:18:15 -07002635 def test_live_reconfiguration_shared_queue(self):
2636 # Test that a change with a failing job which was removed from
2637 # this project but otherwise still exists in the system does
2638 # not disrupt reconfiguration.
2639
Paul Belanger174a8272017-03-14 13:20:10 -04002640 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002641
2642 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2643
Paul Belanger174a8272017-03-14 13:20:10 -04002644 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002645
2646 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2647 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002648 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002649 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002650 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002651 self.waitUntilSettled()
2652
2653 self.assertEqual(A.data['status'], 'NEW')
2654 self.assertEqual(A.reported, 0)
2655
Clint Byrum69663d42017-05-01 14:51:04 -07002656 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002657 'SUCCESS')
2658 self.assertEqual(self.getJobFromHistory(
2659 'project1-project2-integration').result, 'FAILURE')
2660 self.assertEqual(len(self.history), 2)
2661
2662 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002663 self.commitConfigUpdate(
2664 'common-config',
2665 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002666 self.sched.reconfigure(self.config)
2667 self.waitUntilSettled()
2668
Paul Belanger174a8272017-03-14 13:20:10 -04002669 self.executor_server.hold_jobs_in_build = False
2670 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002671 self.waitUntilSettled()
2672
Clint Byrum69663d42017-05-01 14:51:04 -07002673 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002674 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002675 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002676 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002677 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002678 'SUCCESS')
2679 self.assertEqual(self.getJobFromHistory(
2680 'project1-project2-integration').result, 'FAILURE')
2681 self.assertEqual(len(self.history), 4)
2682
2683 self.assertEqual(A.data['status'], 'NEW')
2684 self.assertEqual(A.reported, 1)
2685 self.assertIn('Build succeeded', A.messages[0])
2686 # Ensure the removed job was not included in the report.
2687 self.assertNotIn('project1-project2-integration', A.messages[0])
2688
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002689 def test_double_live_reconfiguration_shared_queue(self):
2690 # This was a real-world regression. A change is added to
2691 # gate; a reconfigure happens, a second change which depends
2692 # on the first is added, and a second reconfiguration happens.
2693 # Ensure that both changes merge.
2694
2695 # A failure may indicate incorrect caching or cleaning up of
2696 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002697 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002698
2699 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2700 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2701 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002702 A.addApproval('Code-Review', 2)
2703 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002704
2705 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002706 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002707 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002708 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002709 self.waitUntilSettled()
2710
2711 # Reconfigure (with only one change in the pipeline).
2712 self.sched.reconfigure(self.config)
2713 self.waitUntilSettled()
2714
2715 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002716 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002717 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002718 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002719 self.waitUntilSettled()
2720
2721 # Reconfigure (with both in the pipeline).
2722 self.sched.reconfigure(self.config)
2723 self.waitUntilSettled()
2724
Paul Belanger174a8272017-03-14 13:20:10 -04002725 self.executor_server.hold_jobs_in_build = False
2726 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002727 self.waitUntilSettled()
2728
2729 self.assertEqual(len(self.history), 8)
2730
2731 self.assertEqual(A.data['status'], 'MERGED')
2732 self.assertEqual(A.reported, 2)
2733 self.assertEqual(B.data['status'], 'MERGED')
2734 self.assertEqual(B.reported, 2)
2735
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002736 def test_live_reconfiguration_del_project(self):
2737 # Test project deletion from layout
2738 # while changes are enqueued
2739
Paul Belanger174a8272017-03-14 13:20:10 -04002740 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002741 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2742 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2743 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2744
2745 # A Depends-On: B
2746 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2747 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002748 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002749
2750 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2751 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2752 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002753 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002754 self.waitUntilSettled()
2755 self.assertEqual(len(self.builds), 5)
2756
2757 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002758 self.commitConfigUpdate(
2759 'common-config',
2760 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002761 self.sched.reconfigure(self.config)
2762 self.waitUntilSettled()
2763
2764 # Builds for C aborted, builds for A succeed,
2765 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002766 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002767 self.assertEqual(job_c.changes, '3,1')
2768 self.assertEqual(job_c.result, 'ABORTED')
2769
Paul Belanger174a8272017-03-14 13:20:10 -04002770 self.executor_server.hold_jobs_in_build = False
2771 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002772 self.waitUntilSettled()
2773
Clint Byrum0149ada2017-04-04 12:22:01 -07002774 self.assertEqual(
2775 self.getJobFromHistory('project-test1', 'org/project').changes,
2776 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002777
2778 self.assertEqual(A.data['status'], 'NEW')
2779 self.assertEqual(B.data['status'], 'NEW')
2780 self.assertEqual(C.data['status'], 'NEW')
2781 self.assertEqual(A.reported, 1)
2782 self.assertEqual(B.reported, 0)
2783 self.assertEqual(C.reported, 0)
2784
Clint Byrum0149ada2017-04-04 12:22:01 -07002785 tenant = self.sched.abide.tenants.get('tenant-one')
2786 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002787 self.assertIn('Build succeeded', A.messages[0])
2788
James E. Blair287c06d2013-07-24 10:39:30 -07002789 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002790 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002791 files = {'README': ''}
2792 self.addCommitToRepo("org/new-project", 'Initial commit',
2793 files=files, tag='init')
2794 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2795 self.commitConfigUpdate(
2796 'common-config',
2797 'layouts/delayed-repo-init.yaml')
2798 self.sched.reconfigure(self.config)
2799 self.waitUntilSettled()
2800
James E. Blair287c06d2013-07-24 10:39:30 -07002801 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2802
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002803 A.addApproval('Code-Review', 2)
2804 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002805 self.waitUntilSettled()
2806 self.assertEqual(self.getJobFromHistory('project-merge').result,
2807 'SUCCESS')
2808 self.assertEqual(self.getJobFromHistory('project-test1').result,
2809 'SUCCESS')
2810 self.assertEqual(self.getJobFromHistory('project-test2').result,
2811 'SUCCESS')
2812 self.assertEqual(A.data['status'], 'MERGED')
2813 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002814
James E. Blair9ea70072017-04-19 16:05:30 -07002815 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002816 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002817 self.init_repo("org/delete-project")
2818 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2819
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002820 A.addApproval('Code-Review', 2)
2821 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002822 self.waitUntilSettled()
2823 self.assertEqual(self.getJobFromHistory('project-merge').result,
2824 'SUCCESS')
2825 self.assertEqual(self.getJobFromHistory('project-test1').result,
2826 'SUCCESS')
2827 self.assertEqual(self.getJobFromHistory('project-test2').result,
2828 'SUCCESS')
2829 self.assertEqual(A.data['status'], 'MERGED')
2830 self.assertEqual(A.reported, 2)
2831
2832 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002833 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002834 if os.path.exists(os.path.join(self.merger_src_root, p)):
2835 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002836 if os.path.exists(os.path.join(self.executor_src_root, p)):
2837 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002838
2839 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2840
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002841 B.addApproval('Code-Review', 2)
2842 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002843 self.waitUntilSettled()
2844 self.assertEqual(self.getJobFromHistory('project-merge').result,
2845 'SUCCESS')
2846 self.assertEqual(self.getJobFromHistory('project-test1').result,
2847 'SUCCESS')
2848 self.assertEqual(self.getJobFromHistory('project-test2').result,
2849 'SUCCESS')
2850 self.assertEqual(B.data['status'], 'MERGED')
2851 self.assertEqual(B.reported, 2)
2852
James E. Blair892cca62017-08-09 11:36:58 -07002853 @simple_layout('layouts/untrusted-secrets.yaml')
2854 def test_untrusted_secrets(self):
2855 "Test untrusted secrets"
2856 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2857 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2858 self.waitUntilSettled()
2859
2860 self.assertHistory([])
2861 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002862 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002863 A.messages[0])
2864
James E. Blaire68d43f2017-04-19 14:03:30 -07002865 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002866 def test_tags(self):
2867 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002868 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2869 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2870 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2871 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2872 self.waitUntilSettled()
2873
James E. Blaire68d43f2017-04-19 14:03:30 -07002874 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002875
2876 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002877 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002878 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002879 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002880
2881 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002882 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002883 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002884
James E. Blair63bb0ef2013-07-29 17:14:51 -07002885 def test_timer(self):
2886 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002887 # This test can not use simple_layout because it must start
2888 # with a configuration which does not include a
2889 # timer-triggered job so that we have an opportunity to set
2890 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002891 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002892 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002893 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002894 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002895
Clark Boylan3ee090a2014-04-03 20:55:09 -07002896 # The pipeline triggers every second, so we should have seen
2897 # several by now.
2898 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002899 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002900
2901 self.assertEqual(len(self.builds), 2)
2902
James E. Blair63bb0ef2013-07-29 17:14:51 -07002903 port = self.webapp.server.socket.getsockname()[1]
2904
Clint Byrum5e729e92017-02-08 06:34:57 -08002905 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002906 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002907 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002908 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002909
Paul Belanger174a8272017-03-14 13:20:10 -04002910 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002911 # Stop queuing timer triggered jobs so that the assertions
2912 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002913 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002914 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002915 self.waitUntilSettled()
2916 # If APScheduler is in mid-event when we remove the job, we
2917 # can end up with one more event firing, so give it an extra
2918 # second to settle.
2919 time.sleep(1)
2920 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002921 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002922 self.waitUntilSettled()
2923
James E. Blair21037782017-07-19 11:56:55 -07002924 self.assertHistory([
2925 dict(name='project-bitrot', result='SUCCESS',
2926 ref='refs/heads/master'),
2927 dict(name='project-bitrot', result='SUCCESS',
2928 ref='refs/heads/stable'),
2929 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002930
2931 data = json.loads(data)
2932 status_jobs = set()
2933 for p in data['pipelines']:
2934 for q in p['change_queues']:
2935 for head in q['heads']:
2936 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002937 for job in change['jobs']:
2938 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002939 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002940
James E. Blair4f6033c2014-03-27 15:49:09 -07002941 def test_idle(self):
2942 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002943 # This test can not use simple_layout because it must start
2944 # with a configuration which does not include a
2945 # timer-triggered job so that we have an opportunity to set
2946 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002947 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002948
Clark Boylan3ee090a2014-04-03 20:55:09 -07002949 for x in range(1, 3):
2950 # Test that timer triggers periodic jobs even across
2951 # layout config reloads.
2952 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002953 self.commitConfigUpdate('common-config',
2954 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002955 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002956 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002957
Clark Boylan3ee090a2014-04-03 20:55:09 -07002958 # The pipeline triggers every second, so we should have seen
2959 # several by now.
2960 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002961
2962 # Stop queuing timer triggered jobs so that the assertions
2963 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002964 self.commitConfigUpdate('common-config',
2965 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002966 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002967 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002968 # If APScheduler is in mid-event when we remove the job,
2969 # we can end up with one more event firing, so give it an
2970 # extra second to settle.
2971 time.sleep(1)
2972 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002973 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002974 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002975 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002976 self.waitUntilSettled()
2977 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002978 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002979
James E. Blaircc324192017-04-19 14:36:08 -07002980 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002981 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002982 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2983 self.waitUntilSettled()
2984
2985 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2986 self.waitUntilSettled()
2987
James E. Blairff80a2f2013-12-27 13:24:06 -08002988 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002989
2990 # A.messages only holds what FakeGerrit places in it. Thus we
2991 # work on the knowledge of what the first message should be as
2992 # it is only configured to go to SMTP.
2993
2994 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002995 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002996 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002997 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002998 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002999 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003000
3001 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003002 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003003 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003004 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003005 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003006 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003007
James E. Blaire5910202013-12-27 09:50:31 -08003008 def test_timer_smtp(self):
3009 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003010 # This test can not use simple_layout because it must start
3011 # with a configuration which does not include a
3012 # timer-triggered job so that we have an opportunity to set
3013 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003014 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003015 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003016 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003017
Clark Boylan3ee090a2014-04-03 20:55:09 -07003018 # The pipeline triggers every second, so we should have seen
3019 # several by now.
3020 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003021 self.waitUntilSettled()
3022
Clark Boylan3ee090a2014-04-03 20:55:09 -07003023 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003024 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003025 self.waitUntilSettled()
3026 self.assertEqual(len(self.history), 2)
3027
James E. Blaire5910202013-12-27 09:50:31 -08003028 self.assertEqual(self.getJobFromHistory(
3029 'project-bitrot-stable-old').result, 'SUCCESS')
3030 self.assertEqual(self.getJobFromHistory(
3031 'project-bitrot-stable-older').result, 'SUCCESS')
3032
James E. Blairff80a2f2013-12-27 13:24:06 -08003033 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003034
3035 # A.messages only holds what FakeGerrit places in it. Thus we
3036 # work on the knowledge of what the first message should be as
3037 # it is only configured to go to SMTP.
3038
3039 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003040 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003041 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003042 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003043 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003044 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003045
Clark Boylan3ee090a2014-04-03 20:55:09 -07003046 # Stop queuing timer triggered jobs and let any that may have
3047 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003048 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003049 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003050 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003051 # If APScheduler is in mid-event when we remove the job, we
3052 # can end up with one more event firing, so give it an extra
3053 # second to settle.
3054 time.sleep(1)
3055 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003056 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003057 self.waitUntilSettled()
3058
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003059 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003060 def test_timer_sshkey(self):
3061 "Test that a periodic job can setup SSH key authentication"
3062 self.worker.hold_jobs_in_build = True
3063 self.config.set('zuul', 'layout_config',
3064 'tests/fixtures/layout-timer.yaml')
3065 self.sched.reconfigure(self.config)
3066 self.registerJobs()
3067
3068 # The pipeline triggers every second, so we should have seen
3069 # several by now.
3070 time.sleep(5)
3071 self.waitUntilSettled()
3072
3073 self.assertEqual(len(self.builds), 2)
3074
3075 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3076 self.assertTrue(os.path.isfile(ssh_wrapper))
3077 with open(ssh_wrapper) as f:
3078 ssh_wrapper_content = f.read()
3079 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3080 # In the unit tests Merger runs in the same process,
3081 # so we see its' environment variables
3082 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3083
3084 self.worker.release('.*')
3085 self.waitUntilSettled()
3086 self.assertEqual(len(self.history), 2)
3087
3088 self.assertEqual(self.getJobFromHistory(
3089 'project-bitrot-stable-old').result, 'SUCCESS')
3090 self.assertEqual(self.getJobFromHistory(
3091 'project-bitrot-stable-older').result, 'SUCCESS')
3092
3093 # Stop queuing timer triggered jobs and let any that may have
3094 # queued through so that end of test assertions pass.
3095 self.config.set('zuul', 'layout_config',
3096 'tests/fixtures/layout-no-timer.yaml')
3097 self.sched.reconfigure(self.config)
3098 self.registerJobs()
3099 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003100 # If APScheduler is in mid-event when we remove the job, we
3101 # can end up with one more event firing, so give it an extra
3102 # second to settle.
3103 time.sleep(1)
3104 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003105 self.worker.release('.*')
3106 self.waitUntilSettled()
3107
James E. Blair91e34592015-07-31 16:45:59 -07003108 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003109 "Test that the RPC client can enqueue a change"
3110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003111 A.addApproval('Code-Review', 2)
3112 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003113
3114 client = zuul.rpcclient.RPCClient('127.0.0.1',
3115 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003116 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003117 r = client.enqueue(tenant='tenant-one',
3118 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003119 project='org/project',
3120 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003121 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003122 self.waitUntilSettled()
3123 self.assertEqual(self.getJobFromHistory('project-merge').result,
3124 'SUCCESS')
3125 self.assertEqual(self.getJobFromHistory('project-test1').result,
3126 'SUCCESS')
3127 self.assertEqual(self.getJobFromHistory('project-test2').result,
3128 'SUCCESS')
3129 self.assertEqual(A.data['status'], 'MERGED')
3130 self.assertEqual(A.reported, 2)
3131 self.assertEqual(r, True)
3132
James E. Blair91e34592015-07-31 16:45:59 -07003133 def test_client_enqueue_ref(self):
3134 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003135 p = "review.example.com/org/project"
3136 upstream = self.getUpstreamRepos([p])
3137 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3138 A.setMerged()
3139 A_commit = str(upstream[p].commit('master'))
3140 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003141
3142 client = zuul.rpcclient.RPCClient('127.0.0.1',
3143 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003144 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003145 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003146 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003147 pipeline='post',
3148 project='org/project',
3149 trigger='gerrit',
3150 ref='master',
3151 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003152 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003153 self.waitUntilSettled()
3154 job_names = [x.name for x in self.history]
3155 self.assertEqual(len(self.history), 1)
3156 self.assertIn('project-post', job_names)
3157 self.assertEqual(r, True)
3158
James E. Blairad28e912013-11-27 10:43:22 -08003159 def test_client_enqueue_negative(self):
3160 "Test that the RPC client returns errors"
3161 client = zuul.rpcclient.RPCClient('127.0.0.1',
3162 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003163 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003164 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003165 "Invalid tenant"):
3166 r = client.enqueue(tenant='tenant-foo',
3167 pipeline='gate',
3168 project='org/project',
3169 trigger='gerrit',
3170 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003171 self.assertEqual(r, False)
3172
3173 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003174 "Invalid project"):
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='project-does-not-exist',
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 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3183 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003184 r = client.enqueue(tenant='tenant-one',
3185 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003186 project='org/project',
3187 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003188 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003189 self.assertEqual(r, False)
3190
3191 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3192 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003193 r = client.enqueue(tenant='tenant-one',
3194 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003195 project='org/project',
3196 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003197 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003198 self.assertEqual(r, False)
3199
3200 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3201 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003202 r = client.enqueue(tenant='tenant-one',
3203 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003204 project='org/project',
3205 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003206 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003207 self.assertEqual(r, False)
3208
3209 self.waitUntilSettled()
3210 self.assertEqual(len(self.history), 0)
3211 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003212
3213 def test_client_promote(self):
3214 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003215 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003216 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3217 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3218 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003219 A.addApproval('Code-Review', 2)
3220 B.addApproval('Code-Review', 2)
3221 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003222
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003223 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3224 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3225 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003226
3227 self.waitUntilSettled()
3228
Paul Belangerbaca3132016-11-04 12:49:54 -04003229 tenant = self.sched.abide.tenants.get('tenant-one')
3230 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003231 enqueue_times = {}
3232 for item in items:
3233 enqueue_times[str(item.change)] = item.enqueue_time
3234
James E. Blair36658cf2013-12-06 17:53:48 -08003235 client = zuul.rpcclient.RPCClient('127.0.0.1',
3236 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003237 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003238 r = client.promote(tenant='tenant-one',
3239 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003240 change_ids=['2,1', '3,1'])
3241
Sean Daguef39b9ca2014-01-10 21:34:35 -05003242 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003243 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003244 for item in items:
3245 self.assertEqual(
3246 enqueue_times[str(item.change)], item.enqueue_time)
3247
James E. Blair78acec92014-02-06 07:11:32 -08003248 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003249 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003250 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003251 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003252 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003253 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003254 self.waitUntilSettled()
3255
3256 self.assertEqual(len(self.builds), 6)
3257 self.assertEqual(self.builds[0].name, 'project-test1')
3258 self.assertEqual(self.builds[1].name, 'project-test2')
3259 self.assertEqual(self.builds[2].name, 'project-test1')
3260 self.assertEqual(self.builds[3].name, 'project-test2')
3261 self.assertEqual(self.builds[4].name, 'project-test1')
3262 self.assertEqual(self.builds[5].name, 'project-test2')
3263
Paul Belangerbaca3132016-11-04 12:49:54 -04003264 self.assertTrue(self.builds[0].hasChanges(B))
3265 self.assertFalse(self.builds[0].hasChanges(A))
3266 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003267
Paul Belangerbaca3132016-11-04 12:49:54 -04003268 self.assertTrue(self.builds[2].hasChanges(B))
3269 self.assertTrue(self.builds[2].hasChanges(C))
3270 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003271
Paul Belangerbaca3132016-11-04 12:49:54 -04003272 self.assertTrue(self.builds[4].hasChanges(B))
3273 self.assertTrue(self.builds[4].hasChanges(C))
3274 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003275
Paul Belanger174a8272017-03-14 13:20:10 -04003276 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003277 self.waitUntilSettled()
3278
3279 self.assertEqual(A.data['status'], 'MERGED')
3280 self.assertEqual(A.reported, 2)
3281 self.assertEqual(B.data['status'], 'MERGED')
3282 self.assertEqual(B.reported, 2)
3283 self.assertEqual(C.data['status'], 'MERGED')
3284 self.assertEqual(C.reported, 2)
3285
James E. Blair36658cf2013-12-06 17:53:48 -08003286 self.assertEqual(r, True)
3287
3288 def test_client_promote_dependent(self):
3289 "Test that the RPC client can promote a dependent change"
3290 # C (depends on B) -> B -> A ; then promote C to get:
3291 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003292 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003293 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3294 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3295 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3296
3297 C.setDependsOn(B, 1)
3298
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003299 A.addApproval('Code-Review', 2)
3300 B.addApproval('Code-Review', 2)
3301 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003302
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003303 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3304 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3305 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003306
3307 self.waitUntilSettled()
3308
3309 client = zuul.rpcclient.RPCClient('127.0.0.1',
3310 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003311 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003312 r = client.promote(tenant='tenant-one',
3313 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003314 change_ids=['3,1'])
3315
James E. Blair78acec92014-02-06 07:11:32 -08003316 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003317 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003318 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003319 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003320 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003321 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003322 self.waitUntilSettled()
3323
3324 self.assertEqual(len(self.builds), 6)
3325 self.assertEqual(self.builds[0].name, 'project-test1')
3326 self.assertEqual(self.builds[1].name, 'project-test2')
3327 self.assertEqual(self.builds[2].name, 'project-test1')
3328 self.assertEqual(self.builds[3].name, 'project-test2')
3329 self.assertEqual(self.builds[4].name, 'project-test1')
3330 self.assertEqual(self.builds[5].name, 'project-test2')
3331
Paul Belangerbaca3132016-11-04 12:49:54 -04003332 self.assertTrue(self.builds[0].hasChanges(B))
3333 self.assertFalse(self.builds[0].hasChanges(A))
3334 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003335
Paul Belangerbaca3132016-11-04 12:49:54 -04003336 self.assertTrue(self.builds[2].hasChanges(B))
3337 self.assertTrue(self.builds[2].hasChanges(C))
3338 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003339
Paul Belangerbaca3132016-11-04 12:49:54 -04003340 self.assertTrue(self.builds[4].hasChanges(B))
3341 self.assertTrue(self.builds[4].hasChanges(C))
3342 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003343
Paul Belanger174a8272017-03-14 13:20:10 -04003344 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003345 self.waitUntilSettled()
3346
3347 self.assertEqual(A.data['status'], 'MERGED')
3348 self.assertEqual(A.reported, 2)
3349 self.assertEqual(B.data['status'], 'MERGED')
3350 self.assertEqual(B.reported, 2)
3351 self.assertEqual(C.data['status'], 'MERGED')
3352 self.assertEqual(C.reported, 2)
3353
James E. Blair36658cf2013-12-06 17:53:48 -08003354 self.assertEqual(r, True)
3355
3356 def test_client_promote_negative(self):
3357 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003358 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003359 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003360 A.addApproval('Code-Review', 2)
3361 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003362 self.waitUntilSettled()
3363
3364 client = zuul.rpcclient.RPCClient('127.0.0.1',
3365 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003366 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003367
3368 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003369 r = client.promote(tenant='tenant-one',
3370 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003371 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003372 self.assertEqual(r, False)
3373
3374 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003375 r = client.promote(tenant='tenant-one',
3376 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003377 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003378 self.assertEqual(r, False)
3379
Paul Belanger174a8272017-03-14 13:20:10 -04003380 self.executor_server.hold_jobs_in_build = False
3381 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003382 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003383
James E. Blaircc324192017-04-19 14:36:08 -07003384 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003385 def test_queue_rate_limiting(self):
3386 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003387 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003388 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3389 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3390 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3391
3392 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003393 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003394
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003395 A.addApproval('Code-Review', 2)
3396 B.addApproval('Code-Review', 2)
3397 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003398
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003399 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3400 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3401 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003402 self.waitUntilSettled()
3403
3404 # Only A and B will have their merge jobs queued because
3405 # window is 2.
3406 self.assertEqual(len(self.builds), 2)
3407 self.assertEqual(self.builds[0].name, 'project-merge')
3408 self.assertEqual(self.builds[1].name, 'project-merge')
3409
James E. Blair732ee862017-04-21 07:49:26 -07003410 # Release the merge jobs one at a time.
3411 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003412 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003413 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003414 self.waitUntilSettled()
3415
3416 # Only A and B will have their test jobs queued because
3417 # window is 2.
3418 self.assertEqual(len(self.builds), 4)
3419 self.assertEqual(self.builds[0].name, 'project-test1')
3420 self.assertEqual(self.builds[1].name, 'project-test2')
3421 self.assertEqual(self.builds[2].name, 'project-test1')
3422 self.assertEqual(self.builds[3].name, 'project-test2')
3423
Paul Belanger174a8272017-03-14 13:20:10 -04003424 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003425 self.waitUntilSettled()
3426
James E. Blaircc324192017-04-19 14:36:08 -07003427 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003428 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003429 # A failed so window is reduced by 1 to 1.
3430 self.assertEqual(queue.window, 1)
3431 self.assertEqual(queue.window_floor, 1)
3432 self.assertEqual(A.data['status'], 'NEW')
3433
3434 # Gate is reset and only B's merge job is queued because
3435 # window shrunk to 1.
3436 self.assertEqual(len(self.builds), 1)
3437 self.assertEqual(self.builds[0].name, 'project-merge')
3438
Paul Belanger174a8272017-03-14 13:20:10 -04003439 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003440 self.waitUntilSettled()
3441
3442 # Only B's test jobs are queued because window is still 1.
3443 self.assertEqual(len(self.builds), 2)
3444 self.assertEqual(self.builds[0].name, 'project-test1')
3445 self.assertEqual(self.builds[1].name, 'project-test2')
3446
Paul Belanger174a8272017-03-14 13:20:10 -04003447 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003448 self.waitUntilSettled()
3449
3450 # B was successfully merged so window is increased to 2.
3451 self.assertEqual(queue.window, 2)
3452 self.assertEqual(queue.window_floor, 1)
3453 self.assertEqual(B.data['status'], 'MERGED')
3454
3455 # Only C is left and its merge job is queued.
3456 self.assertEqual(len(self.builds), 1)
3457 self.assertEqual(self.builds[0].name, 'project-merge')
3458
Paul Belanger174a8272017-03-14 13:20:10 -04003459 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003460 self.waitUntilSettled()
3461
3462 # After successful merge job the test jobs for C are queued.
3463 self.assertEqual(len(self.builds), 2)
3464 self.assertEqual(self.builds[0].name, 'project-test1')
3465 self.assertEqual(self.builds[1].name, 'project-test2')
3466
Paul Belanger174a8272017-03-14 13:20:10 -04003467 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003468 self.waitUntilSettled()
3469
3470 # C successfully merged so window is bumped to 3.
3471 self.assertEqual(queue.window, 3)
3472 self.assertEqual(queue.window_floor, 1)
3473 self.assertEqual(C.data['status'], 'MERGED')
3474
James E. Blaircc324192017-04-19 14:36:08 -07003475 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003476 def test_queue_rate_limiting_dependent(self):
3477 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003478 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003479 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3480 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3481 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3482
3483 B.setDependsOn(A, 1)
3484
Paul Belanger174a8272017-03-14 13:20:10 -04003485 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003486
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003487 A.addApproval('Code-Review', 2)
3488 B.addApproval('Code-Review', 2)
3489 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003490
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003491 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3492 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3493 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003494 self.waitUntilSettled()
3495
3496 # Only A and B will have their merge jobs queued because
3497 # window is 2.
3498 self.assertEqual(len(self.builds), 2)
3499 self.assertEqual(self.builds[0].name, 'project-merge')
3500 self.assertEqual(self.builds[1].name, 'project-merge')
3501
James E. Blairf4a5f022017-04-18 14:01:10 -07003502 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003503
3504 # Only A and B will have their test jobs queued because
3505 # window is 2.
3506 self.assertEqual(len(self.builds), 4)
3507 self.assertEqual(self.builds[0].name, 'project-test1')
3508 self.assertEqual(self.builds[1].name, 'project-test2')
3509 self.assertEqual(self.builds[2].name, 'project-test1')
3510 self.assertEqual(self.builds[3].name, 'project-test2')
3511
Paul Belanger174a8272017-03-14 13:20:10 -04003512 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003513 self.waitUntilSettled()
3514
James E. Blaircc324192017-04-19 14:36:08 -07003515 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003516 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003517 # A failed so window is reduced by 1 to 1.
3518 self.assertEqual(queue.window, 1)
3519 self.assertEqual(queue.window_floor, 1)
3520 self.assertEqual(A.data['status'], 'NEW')
3521 self.assertEqual(B.data['status'], 'NEW')
3522
3523 # Gate is reset and only C's merge job is queued because
3524 # window shrunk to 1 and A and B were dequeued.
3525 self.assertEqual(len(self.builds), 1)
3526 self.assertEqual(self.builds[0].name, 'project-merge')
3527
James E. Blairf4a5f022017-04-18 14:01:10 -07003528 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003529
3530 # Only C's test jobs are queued because window is still 1.
3531 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003532 builds = self.getSortedBuilds()
3533 self.assertEqual(builds[0].name, 'project-test1')
3534 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003535
Paul Belanger174a8272017-03-14 13:20:10 -04003536 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003537 self.waitUntilSettled()
3538
3539 # C was successfully merged so window is increased to 2.
3540 self.assertEqual(queue.window, 2)
3541 self.assertEqual(queue.window_floor, 1)
3542 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003543
3544 def test_worker_update_metadata(self):
3545 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003546 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003547
3548 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003549 A.addApproval('Code-Review', 2)
3550 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003551 self.waitUntilSettled()
3552
Clint Byrumd4de79a2017-05-27 11:39:13 -07003553 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003554
3555 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003556 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003557
3558 start = time.time()
3559 while True:
3560 if time.time() - start > 10:
3561 raise Exception("Timeout waiting for gearman server to report "
3562 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003563 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003564 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003565 break
3566 else:
3567 time.sleep(0)
3568
3569 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003570 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003571
Paul Belanger174a8272017-03-14 13:20:10 -04003572 self.executor_server.hold_jobs_in_build = False
3573 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003574 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003575
James E. Blaircc324192017-04-19 14:36:08 -07003576 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003577 def test_footer_message(self):
3578 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003579 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003580 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003581 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003582 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003583 self.waitUntilSettled()
3584
3585 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003586 B.addApproval('Code-Review', 2)
3587 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003588 self.waitUntilSettled()
3589
3590 self.assertEqual(2, len(self.smtp_messages))
3591
Adam Gandelman82258fd2017-02-07 16:51:58 -08003592 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003593Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003594http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003595
Adam Gandelman82258fd2017-02-07 16:51:58 -08003596 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003597For CI problems and help debugging, contact ci@example.org"""
3598
Adam Gandelman82258fd2017-02-07 16:51:58 -08003599 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3600 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3601 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3602 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003603
Tobias Henkel9842bd72017-05-16 13:40:03 +02003604 @simple_layout('layouts/unmanaged-project.yaml')
3605 def test_unmanaged_project_start_message(self):
3606 "Test start reporting is not done for unmanaged projects."
3607 self.init_repo("org/project", tag='init')
3608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3609 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3610 self.waitUntilSettled()
3611
3612 self.assertEqual(0, len(A.messages))
3613
Clint Byrumb9de8be2017-05-27 13:49:19 -07003614 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003615 def test_merge_failure_reporters(self):
3616 """Check that the config is set up correctly"""
3617
Clint Byrumb9de8be2017-05-27 13:49:19 -07003618 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003619 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003620 "Merge Failed.\n\nThis change or one of its cross-repo "
3621 "dependencies was unable to be automatically merged with the "
3622 "current state of its repository. Please rebase the change and "
3623 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003624 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003625 self.assertEqual(
3626 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003627 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003628
3629 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003630 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003631 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003632 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003633
3634 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003635 tenant.layout.pipelines['check'].merge_failure_actions[0],
3636 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003637
3638 self.assertTrue(
3639 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003640 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003641 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003642 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3643 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003644 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003645 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003646 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003647 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003648 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003649 gerritreporter.GerritReporter) and
3650 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003651 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003652 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003653 )
3654 )
3655
James E. Blairec056492016-07-22 09:45:56 -07003656 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003657 def test_merge_failure_reports(self):
3658 """Check that when a change fails to merge the correct message is sent
3659 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003660 self.updateConfigLayout(
3661 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003662 self.sched.reconfigure(self.config)
3663 self.registerJobs()
3664
3665 # Check a test failure isn't reported to SMTP
3666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003667 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003668 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003669 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003670 self.waitUntilSettled()
3671
3672 self.assertEqual(3, len(self.history)) # 3 jobs
3673 self.assertEqual(0, len(self.smtp_messages))
3674
3675 # Check a merge failure is reported to SMTP
3676 # B should be merged, but C will conflict with B
3677 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3678 B.addPatchset(['conflict'])
3679 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3680 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003681 B.addApproval('Code-Review', 2)
3682 C.addApproval('Code-Review', 2)
3683 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3684 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003685 self.waitUntilSettled()
3686
3687 self.assertEqual(6, len(self.history)) # A and B jobs
3688 self.assertEqual(1, len(self.smtp_messages))
3689 self.assertEqual('The merge failed! For more information...',
3690 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003691
James E. Blairec056492016-07-22 09:45:56 -07003692 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003693 def test_default_merge_failure_reports(self):
3694 """Check that the default merge failure reports are correct."""
3695
3696 # A should report success, B should report merge failure.
3697 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3698 A.addPatchset(['conflict'])
3699 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3700 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003701 A.addApproval('Code-Review', 2)
3702 B.addApproval('Code-Review', 2)
3703 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3704 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003705 self.waitUntilSettled()
3706
3707 self.assertEqual(3, len(self.history)) # A jobs
3708 self.assertEqual(A.reported, 2)
3709 self.assertEqual(B.reported, 2)
3710 self.assertEqual(A.data['status'], 'MERGED')
3711 self.assertEqual(B.data['status'], 'NEW')
3712 self.assertIn('Build succeeded', A.messages[1])
3713 self.assertIn('Merge Failed', B.messages[1])
3714 self.assertIn('automatically merged', B.messages[1])
3715 self.assertNotIn('logs.example.com', B.messages[1])
3716 self.assertNotIn('SKIPPED', B.messages[1])
3717
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003718 def test_client_get_running_jobs(self):
3719 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003720 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003721 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003722 A.addApproval('Code-Review', 2)
3723 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003724 self.waitUntilSettled()
3725
3726 client = zuul.rpcclient.RPCClient('127.0.0.1',
3727 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003728 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003729
3730 # Wait for gearman server to send the initial workData back to zuul
3731 start = time.time()
3732 while True:
3733 if time.time() - start > 10:
3734 raise Exception("Timeout waiting for gearman server to report "
3735 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003736 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003737 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003738 break
3739 else:
3740 time.sleep(0)
3741
3742 running_items = client.get_running_jobs()
3743
3744 self.assertEqual(1, len(running_items))
3745 running_item = running_items[0]
3746 self.assertEqual([], running_item['failing_reasons'])
3747 self.assertEqual([], running_item['items_behind'])
3748 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003749 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003750 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003751 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003752 self.assertEqual(True, running_item['active'])
3753 self.assertEqual('1,1', running_item['id'])
3754
3755 self.assertEqual(3, len(running_item['jobs']))
3756 for job in running_item['jobs']:
3757 if job['name'] == 'project-merge':
3758 self.assertEqual('project-merge', job['name'])
3759 self.assertEqual('gate', job['pipeline'])
3760 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003761 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003762 'static/stream.html?uuid={uuid}&logfile=console.log'
3763 .format(uuid=job['uuid']), job['url'])
3764 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003765 'finger://{hostname}/{uuid}'.format(
3766 hostname=self.executor_server.hostname,
3767 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003768 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003769 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003770 self.assertEqual(False, job['canceled'])
3771 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003772 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003773 self.assertEqual('gate', job['pipeline'])
3774 break
3775
Paul Belanger174a8272017-03-14 13:20:10 -04003776 self.executor_server.hold_jobs_in_build = False
3777 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003778 self.waitUntilSettled()
3779
3780 running_items = client.get_running_jobs()
3781 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003782
James E. Blairb70e55a2017-04-19 12:57:02 -07003783 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003784 def test_nonvoting_pipeline(self):
3785 "Test that a nonvoting pipeline (experimental) can still report"
3786
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003787 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3788 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003789 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3790 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003791
3792 self.assertEqual(self.getJobFromHistory('project-merge').result,
3793 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003794 self.assertEqual(
3795 self.getJobFromHistory('experimental-project-test').result,
3796 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003797 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003798
3799 def test_crd_gate(self):
3800 "Test cross-repo dependencies"
3801 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3802 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003803 A.addApproval('Code-Review', 2)
3804 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003805
3806 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3807 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3808 AM2.setMerged()
3809 AM1.setMerged()
3810
3811 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3812 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3813 BM2.setMerged()
3814 BM1.setMerged()
3815
3816 # A -> AM1 -> AM2
3817 # B -> BM1 -> BM2
3818 # A Depends-On: B
3819 # M2 is here to make sure it is never queried. If it is, it
3820 # means zuul is walking down the entire history of merged
3821 # changes.
3822
3823 B.setDependsOn(BM1, 1)
3824 BM1.setDependsOn(BM2, 1)
3825
3826 A.setDependsOn(AM1, 1)
3827 AM1.setDependsOn(AM2, 1)
3828
3829 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3830 A.subject, B.data['id'])
3831
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003832 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003833 self.waitUntilSettled()
3834
3835 self.assertEqual(A.data['status'], 'NEW')
3836 self.assertEqual(B.data['status'], 'NEW')
3837
Paul Belanger5dccbe72016-11-14 11:17:37 -05003838 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003839 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003840
Paul Belanger174a8272017-03-14 13:20:10 -04003841 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003842 B.addApproval('Approved', 1)
3843 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003844 self.waitUntilSettled()
3845
Paul Belanger174a8272017-03-14 13:20:10 -04003846 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003847 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003848 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003849 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003850 self.executor_server.hold_jobs_in_build = False
3851 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003852 self.waitUntilSettled()
3853
3854 self.assertEqual(AM2.queried, 0)
3855 self.assertEqual(BM2.queried, 0)
3856 self.assertEqual(A.data['status'], 'MERGED')
3857 self.assertEqual(B.data['status'], 'MERGED')
3858 self.assertEqual(A.reported, 2)
3859 self.assertEqual(B.reported, 2)
3860
Paul Belanger5dccbe72016-11-14 11:17:37 -05003861 changes = self.getJobFromHistory(
3862 'project-merge', 'org/project1').changes
3863 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003864
3865 def test_crd_branch(self):
3866 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003867
3868 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003869 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3870 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003871 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3872 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3873 status='ABANDONED')
3874 C1.data['id'] = B.data['id']
3875 C2.data['id'] = B.data['id']
3876
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003877 A.addApproval('Code-Review', 2)
3878 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003879 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003880
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003881 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003882 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3883 A.subject, B.data['id'])
3884
Paul Belanger174a8272017-03-14 13:20:10 -04003885 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003886 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003887 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003888 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003889 self.waitUntilSettled()
3890
Paul Belanger174a8272017-03-14 13:20:10 -04003891 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003892 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003893 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003894 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003895 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003896 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003897 self.executor_server.hold_jobs_in_build = False
3898 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003899 self.waitUntilSettled()
3900
3901 self.assertEqual(A.data['status'], 'MERGED')
3902 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003903 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08003904 self.assertEqual(A.reported, 2)
3905 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003906 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003907
Paul Belanger6379db12016-11-14 13:57:54 -05003908 changes = self.getJobFromHistory(
3909 'project-merge', 'org/project1').changes
3910 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003911
3912 def test_crd_multiline(self):
3913 "Test multiple depends-on lines in commit"
3914 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3915 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3916 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003917 A.addApproval('Code-Review', 2)
3918 B.addApproval('Code-Review', 2)
3919 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003920
3921 # A Depends-On: B+C
3922 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3923 A.subject, B.data['id'], C.data['id'])
3924
Paul Belanger174a8272017-03-14 13:20:10 -04003925 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003926 B.addApproval('Approved', 1)
3927 C.addApproval('Approved', 1)
3928 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003929 self.waitUntilSettled()
3930
Paul Belanger174a8272017-03-14 13:20:10 -04003931 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003932 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003933 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003934 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003935 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003936 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003937 self.executor_server.hold_jobs_in_build = False
3938 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003939 self.waitUntilSettled()
3940
3941 self.assertEqual(A.data['status'], 'MERGED')
3942 self.assertEqual(B.data['status'], 'MERGED')
3943 self.assertEqual(C.data['status'], 'MERGED')
3944 self.assertEqual(A.reported, 2)
3945 self.assertEqual(B.reported, 2)
3946 self.assertEqual(C.reported, 2)
3947
Paul Belanger5dccbe72016-11-14 11:17:37 -05003948 changes = self.getJobFromHistory(
3949 'project-merge', 'org/project1').changes
3950 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003951
3952 def test_crd_unshared_gate(self):
3953 "Test cross-repo dependencies in unshared gate queues"
3954 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3955 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003956 A.addApproval('Code-Review', 2)
3957 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003958
3959 # A Depends-On: B
3960 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3961 A.subject, B.data['id'])
3962
3963 # A and B do not share a queue, make sure that A is unable to
3964 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003965 B.addApproval('Approved', 1)
3966 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003967 self.waitUntilSettled()
3968
3969 self.assertEqual(A.data['status'], 'NEW')
3970 self.assertEqual(B.data['status'], 'NEW')
3971 self.assertEqual(A.reported, 0)
3972 self.assertEqual(B.reported, 0)
3973 self.assertEqual(len(self.history), 0)
3974
3975 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003976 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003977 self.waitUntilSettled()
3978
3979 self.assertEqual(B.data['status'], 'MERGED')
3980 self.assertEqual(B.reported, 2)
3981
3982 # Now that B is merged, A should be able to be enqueued and
3983 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003984 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003985 self.waitUntilSettled()
3986
3987 self.assertEqual(A.data['status'], 'MERGED')
3988 self.assertEqual(A.reported, 2)
3989
James E. Blair96698e22015-04-02 07:48:21 -07003990 def test_crd_gate_reverse(self):
3991 "Test reverse cross-repo dependencies"
3992 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3993 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003994 A.addApproval('Code-Review', 2)
3995 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003996
3997 # A Depends-On: B
3998
3999 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4000 A.subject, B.data['id'])
4001
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004002 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004003 self.waitUntilSettled()
4004
4005 self.assertEqual(A.data['status'], 'NEW')
4006 self.assertEqual(B.data['status'], 'NEW')
4007
Paul Belanger174a8272017-03-14 13:20:10 -04004008 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004009 A.addApproval('Approved', 1)
4010 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004011 self.waitUntilSettled()
4012
Paul Belanger174a8272017-03-14 13:20:10 -04004013 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004014 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004015 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004016 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004017 self.executor_server.hold_jobs_in_build = False
4018 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004019 self.waitUntilSettled()
4020
4021 self.assertEqual(A.data['status'], 'MERGED')
4022 self.assertEqual(B.data['status'], 'MERGED')
4023 self.assertEqual(A.reported, 2)
4024 self.assertEqual(B.reported, 2)
4025
Paul Belanger5dccbe72016-11-14 11:17:37 -05004026 changes = self.getJobFromHistory(
4027 'project-merge', 'org/project1').changes
4028 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004029
James E. Blair5ee24252014-12-30 10:12:29 -08004030 def test_crd_cycle(self):
4031 "Test cross-repo dependency cycles"
4032 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4033 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004034 A.addApproval('Code-Review', 2)
4035 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004036
4037 # A -> B -> A (via commit-depends)
4038
4039 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4040 A.subject, B.data['id'])
4041 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4042 B.subject, A.data['id'])
4043
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004044 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004045 self.waitUntilSettled()
4046
4047 self.assertEqual(A.reported, 0)
4048 self.assertEqual(B.reported, 0)
4049 self.assertEqual(A.data['status'], 'NEW')
4050 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004051
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004052 def test_crd_gate_unknown(self):
4053 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004054 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004055 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4056 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004057 A.addApproval('Code-Review', 2)
4058 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004059
4060 # A Depends-On: B
4061 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4062 A.subject, B.data['id'])
4063
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004064 B.addApproval('Approved', 1)
4065 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004066 self.waitUntilSettled()
4067
4068 # Unknown projects cannot share a queue with any other
4069 # since they don't have common jobs with any other (they have no jobs).
4070 # Changes which depend on unknown project changes
4071 # should not be processed in dependent pipeline
4072 self.assertEqual(A.data['status'], 'NEW')
4073 self.assertEqual(B.data['status'], 'NEW')
4074 self.assertEqual(A.reported, 0)
4075 self.assertEqual(B.reported, 0)
4076 self.assertEqual(len(self.history), 0)
4077
James E. Blair7720b402017-06-07 08:51:49 -07004078 # Simulate change B being gated outside this layout Set the
4079 # change merged before submitting the event so that when the
4080 # event triggers a gerrit query to update the change, we get
4081 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004082 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004083 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004084 self.waitUntilSettled()
4085 self.assertEqual(len(self.history), 0)
4086
4087 # Now that B is merged, A should be able to be enqueued and
4088 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004089 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004090 self.waitUntilSettled()
4091
4092 self.assertEqual(A.data['status'], 'MERGED')
4093 self.assertEqual(A.reported, 2)
4094 self.assertEqual(B.data['status'], 'MERGED')
4095 self.assertEqual(B.reported, 0)
4096
James E. Blairbfb8e042014-12-30 17:01:44 -08004097 def test_crd_check(self):
4098 "Test cross-repo dependencies in independent pipelines"
4099
Paul Belanger174a8272017-03-14 13:20:10 -04004100 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004101 self.gearman_server.hold_jobs_in_queue = True
4102 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4103 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4104
4105 # A Depends-On: B
4106 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4107 A.subject, B.data['id'])
4108
4109 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4110 self.waitUntilSettled()
4111
James E. Blairbfb8e042014-12-30 17:01:44 -08004112 self.gearman_server.hold_jobs_in_queue = False
4113 self.gearman_server.release()
4114 self.waitUntilSettled()
4115
Paul Belanger174a8272017-03-14 13:20:10 -04004116 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004117 self.waitUntilSettled()
4118
James E. Blair2a535672017-04-27 12:03:15 -07004119 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004120
Paul Belanger174a8272017-03-14 13:20:10 -04004121 self.executor_server.hold_jobs_in_build = False
4122 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004123 self.waitUntilSettled()
4124
James E. Blairbfb8e042014-12-30 17:01:44 -08004125 self.assertEqual(A.data['status'], 'NEW')
4126 self.assertEqual(B.data['status'], 'NEW')
4127 self.assertEqual(A.reported, 1)
4128 self.assertEqual(B.reported, 0)
4129
4130 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004131 tenant = self.sched.abide.tenants.get('tenant-one')
4132 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004133
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004134 def test_crd_check_git_depends(self):
4135 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004136 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004137 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4138 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4139
4140 # Add two git-dependent changes and make sure they both report
4141 # success.
4142 B.setDependsOn(A, 1)
4143 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4144 self.waitUntilSettled()
4145 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4146 self.waitUntilSettled()
4147
James E. Blairb8c16472015-05-05 14:55:26 -07004148 self.orderedRelease()
4149 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004150 self.waitUntilSettled()
4151
4152 self.assertEqual(A.data['status'], 'NEW')
4153 self.assertEqual(B.data['status'], 'NEW')
4154 self.assertEqual(A.reported, 1)
4155 self.assertEqual(B.reported, 1)
4156
4157 self.assertEqual(self.history[0].changes, '1,1')
4158 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004159 tenant = self.sched.abide.tenants.get('tenant-one')
4160 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004161
4162 self.assertIn('Build succeeded', A.messages[0])
4163 self.assertIn('Build succeeded', B.messages[0])
4164
4165 def test_crd_check_duplicate(self):
4166 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004167 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004168 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4169 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004170 tenant = self.sched.abide.tenants.get('tenant-one')
4171 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004172
4173 # Add two git-dependent changes...
4174 B.setDependsOn(A, 1)
4175 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4176 self.waitUntilSettled()
4177 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4178
4179 # ...make sure the live one is not duplicated...
4180 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4181 self.waitUntilSettled()
4182 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4183
4184 # ...but the non-live one is able to be.
4185 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4186 self.waitUntilSettled()
4187 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4188
Clark Boylandd849822015-03-02 12:38:14 -08004189 # Release jobs in order to avoid races with change A jobs
4190 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004191 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004192 self.executor_server.hold_jobs_in_build = False
4193 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004194 self.waitUntilSettled()
4195
4196 self.assertEqual(A.data['status'], 'NEW')
4197 self.assertEqual(B.data['status'], 'NEW')
4198 self.assertEqual(A.reported, 1)
4199 self.assertEqual(B.reported, 1)
4200
4201 self.assertEqual(self.history[0].changes, '1,1 2,1')
4202 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004203 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004204
4205 self.assertIn('Build succeeded', A.messages[0])
4206 self.assertIn('Build succeeded', B.messages[0])
4207
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004208 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004209 "Test cross-repo dependencies re-enqueued in independent pipelines"
4210
4211 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004212 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4213 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004214
4215 # A Depends-On: B
4216 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4217 A.subject, B.data['id'])
4218
4219 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4220 self.waitUntilSettled()
4221
4222 self.sched.reconfigure(self.config)
4223
4224 # Make sure the items still share a change queue, and the
4225 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004226 tenant = self.sched.abide.tenants.get('tenant-one')
4227 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4228 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004229 first_item = queue.queue[0]
4230 for item in queue.queue:
4231 self.assertEqual(item.queue, first_item.queue)
4232 self.assertFalse(first_item.live)
4233 self.assertTrue(queue.queue[1].live)
4234
4235 self.gearman_server.hold_jobs_in_queue = False
4236 self.gearman_server.release()
4237 self.waitUntilSettled()
4238
4239 self.assertEqual(A.data['status'], 'NEW')
4240 self.assertEqual(B.data['status'], 'NEW')
4241 self.assertEqual(A.reported, 1)
4242 self.assertEqual(B.reported, 0)
4243
4244 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004245 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004246
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004247 def test_crd_check_reconfiguration(self):
4248 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4249
4250 def test_crd_undefined_project(self):
4251 """Test that undefined projects in dependencies are handled for
4252 independent pipelines"""
4253 # It's a hack for fake gerrit,
4254 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004255 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004256 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4257
Jesse Keatingea68ab42017-04-19 15:03:30 -07004258 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004259 def test_crd_check_ignore_dependencies(self):
4260 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004261
4262 self.gearman_server.hold_jobs_in_queue = True
4263 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4264 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4265 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4266
4267 # A Depends-On: B
4268 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4269 A.subject, B.data['id'])
4270 # C git-depends on B
4271 C.setDependsOn(B, 1)
4272 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4273 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4274 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4275 self.waitUntilSettled()
4276
4277 # Make sure none of the items share a change queue, and all
4278 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004279 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004280 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004281 self.assertEqual(len(check_pipeline.queues), 3)
4282 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4283 for item in check_pipeline.getAllItems():
4284 self.assertTrue(item.live)
4285
4286 self.gearman_server.hold_jobs_in_queue = False
4287 self.gearman_server.release()
4288 self.waitUntilSettled()
4289
4290 self.assertEqual(A.data['status'], 'NEW')
4291 self.assertEqual(B.data['status'], 'NEW')
4292 self.assertEqual(C.data['status'], 'NEW')
4293 self.assertEqual(A.reported, 1)
4294 self.assertEqual(B.reported, 1)
4295 self.assertEqual(C.reported, 1)
4296
4297 # Each job should have tested exactly one change
4298 for job in self.history:
4299 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004300
James E. Blair7cb84542017-04-19 13:35:05 -07004301 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004302 def test_crd_check_transitive(self):
4303 "Test transitive cross-repo dependencies"
4304 # Specifically, if A -> B -> C, and C gets a new patchset and
4305 # A gets a new patchset, ensure the test of A,2 includes B,1
4306 # and C,2 (not C,1 which would indicate stale data in the
4307 # cache for B).
4308 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4309 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4310 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4311
4312 # A Depends-On: B
4313 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4314 A.subject, B.data['id'])
4315
4316 # B Depends-On: C
4317 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4318 B.subject, C.data['id'])
4319
4320 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4321 self.waitUntilSettled()
4322 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4323
4324 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4325 self.waitUntilSettled()
4326 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4327
4328 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4329 self.waitUntilSettled()
4330 self.assertEqual(self.history[-1].changes, '3,1')
4331
4332 C.addPatchset()
4333 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4334 self.waitUntilSettled()
4335 self.assertEqual(self.history[-1].changes, '3,2')
4336
4337 A.addPatchset()
4338 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4339 self.waitUntilSettled()
4340 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004341
James E. Blaira8b90b32016-08-24 15:18:50 -07004342 def test_crd_check_unknown(self):
4343 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004344 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004345 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4346 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4347 # A Depends-On: B
4348 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4349 A.subject, B.data['id'])
4350
4351 # Make sure zuul has seen an event on B.
4352 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4353 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4354 self.waitUntilSettled()
4355
4356 self.assertEqual(A.data['status'], 'NEW')
4357 self.assertEqual(A.reported, 1)
4358 self.assertEqual(B.data['status'], 'NEW')
4359 self.assertEqual(B.reported, 0)
4360
James E. Blair92464a22016-04-05 10:21:26 -07004361 def test_crd_cycle_join(self):
4362 "Test an updated change creates a cycle"
4363 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4364
4365 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4366 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004367 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004368
4369 # Create B->A
4370 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4371 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4372 B.subject, A.data['id'])
4373 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4374 self.waitUntilSettled()
4375
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004376 # Dep is there so zuul should have reported on B
4377 self.assertEqual(B.reported, 1)
4378
James E. Blair92464a22016-04-05 10:21:26 -07004379 # Update A to add A->B (a cycle).
4380 A.addPatchset()
4381 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4382 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004383 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4384 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004385
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004386 # Dependency cycle injected so zuul should not have reported again on A
4387 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004388
4389 # Now if we update B to remove the depends-on, everything
4390 # should be okay. B; A->B
4391
4392 B.addPatchset()
4393 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004394 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4395 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004396
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004397 # Cycle was removed so now zuul should have reported again on A
4398 self.assertEqual(A.reported, 2)
4399
4400 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4401 self.waitUntilSettled()
4402 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004403
Jesse Keatingcc67b122017-04-19 14:54:57 -07004404 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004405 def test_disable_at(self):
4406 "Test a pipeline will only report to the disabled trigger when failing"
4407
Jesse Keatingcc67b122017-04-19 14:54:57 -07004408 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004409 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004410 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004411 0, tenant.layout.pipelines['check']._consecutive_failures)
4412 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004413
4414 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4415 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4416 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4417 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4418 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4419 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4420 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4421 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4422 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4423 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4424 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4425
Paul Belanger174a8272017-03-14 13:20:10 -04004426 self.executor_server.failJob('project-test1', A)
4427 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004428 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004429 self.executor_server.failJob('project-test1', D)
4430 self.executor_server.failJob('project-test1', E)
4431 self.executor_server.failJob('project-test1', F)
4432 self.executor_server.failJob('project-test1', G)
4433 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004434 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004435 self.executor_server.failJob('project-test1', J)
4436 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004437
4438 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4439 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4440 self.waitUntilSettled()
4441
4442 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004443 2, tenant.layout.pipelines['check']._consecutive_failures)
4444 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004445
4446 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4447 self.waitUntilSettled()
4448
4449 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004450 0, tenant.layout.pipelines['check']._consecutive_failures)
4451 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004452
4453 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4454 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4455 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4456 self.waitUntilSettled()
4457
4458 # We should be disabled now
4459 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004460 3, tenant.layout.pipelines['check']._consecutive_failures)
4461 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004462
4463 # We need to wait between each of these patches to make sure the
4464 # smtp messages come back in an expected order
4465 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4466 self.waitUntilSettled()
4467 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4468 self.waitUntilSettled()
4469 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4470 self.waitUntilSettled()
4471
4472 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4473 # leaving a message on each change
4474 self.assertEqual(1, len(A.messages))
4475 self.assertIn('Build failed.', A.messages[0])
4476 self.assertEqual(1, len(B.messages))
4477 self.assertIn('Build failed.', B.messages[0])
4478 self.assertEqual(1, len(C.messages))
4479 self.assertIn('Build succeeded.', C.messages[0])
4480 self.assertEqual(1, len(D.messages))
4481 self.assertIn('Build failed.', D.messages[0])
4482 self.assertEqual(1, len(E.messages))
4483 self.assertIn('Build failed.', E.messages[0])
4484 self.assertEqual(1, len(F.messages))
4485 self.assertIn('Build failed.', F.messages[0])
4486
4487 # The last 3 (GHI) would have only reported via smtp.
4488 self.assertEqual(3, len(self.smtp_messages))
4489 self.assertEqual(0, len(G.messages))
4490 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004491 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004492 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004493 self.assertEqual(0, len(H.messages))
4494 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004495 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004496 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004497 self.assertEqual(0, len(I.messages))
4498 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004499 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004500 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004501
4502 # Now reload the configuration (simulate a HUP) to check the pipeline
4503 # comes out of disabled
4504 self.sched.reconfigure(self.config)
4505
Jesse Keatingcc67b122017-04-19 14:54:57 -07004506 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004507
4508 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004509 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004510 0, tenant.layout.pipelines['check']._consecutive_failures)
4511 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004512
4513 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4514 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4515 self.waitUntilSettled()
4516
4517 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004518 2, tenant.layout.pipelines['check']._consecutive_failures)
4519 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004520
4521 # J and K went back to gerrit
4522 self.assertEqual(1, len(J.messages))
4523 self.assertIn('Build failed.', J.messages[0])
4524 self.assertEqual(1, len(K.messages))
4525 self.assertIn('Build failed.', K.messages[0])
4526 # No more messages reported via smtp
4527 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004528
James E. Blaircc020532017-04-19 13:04:44 -07004529 @simple_layout('layouts/one-job-project.yaml')
4530 def test_one_job_project(self):
4531 "Test that queueing works with one job"
4532 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4533 'master', 'A')
4534 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4535 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004536 A.addApproval('Code-Review', 2)
4537 B.addApproval('Code-Review', 2)
4538 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4539 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004540 self.waitUntilSettled()
4541
4542 self.assertEqual(A.data['status'], 'MERGED')
4543 self.assertEqual(A.reported, 2)
4544 self.assertEqual(B.data['status'], 'MERGED')
4545 self.assertEqual(B.reported, 2)
4546
Paul Belanger71d98172016-11-08 10:56:31 -05004547 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004548 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004549
Paul Belanger174a8272017-03-14 13:20:10 -04004550 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004551 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4552 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4553 self.waitUntilSettled()
4554
Paul Belanger174a8272017-03-14 13:20:10 -04004555 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004556 self.waitUntilSettled()
4557
4558 self.assertEqual(len(self.builds), 2)
4559 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004560 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004561 self.waitUntilSettled()
4562
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004563 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004564 self.assertEqual(len(self.builds), 1,
4565 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004566 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004567 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004568 self.waitUntilSettled()
4569
Paul Belanger174a8272017-03-14 13:20:10 -04004570 self.executor_server.hold_jobs_in_build = False
4571 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004572 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004573 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004574 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4575 self.assertEqual(A.reported, 1)
4576 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004577
James E. Blair15be0e12017-01-03 13:45:20 -08004578 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004579 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004580
4581 self.fake_nodepool.paused = True
4582 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004583 A.addApproval('Code-Review', 2)
4584 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004585 self.waitUntilSettled()
4586
4587 self.zk.client.stop()
4588 self.zk.client.start()
4589 self.fake_nodepool.paused = False
4590 self.waitUntilSettled()
4591
4592 self.assertEqual(A.data['status'], 'MERGED')
4593 self.assertEqual(A.reported, 2)
4594
James E. Blair6ab79e02017-01-06 10:10:17 -08004595 def test_nodepool_failure(self):
4596 "Test that jobs are reported after a nodepool failure"
4597
4598 self.fake_nodepool.paused = True
4599 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004600 A.addApproval('Code-Review', 2)
4601 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004602 self.waitUntilSettled()
4603
4604 req = self.fake_nodepool.getNodeRequests()[0]
4605 self.fake_nodepool.addFailRequest(req)
4606
4607 self.fake_nodepool.paused = False
4608 self.waitUntilSettled()
4609
4610 self.assertEqual(A.data['status'], 'NEW')
4611 self.assertEqual(A.reported, 2)
4612 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4613 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4614 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4615
Arieb6f068c2016-10-09 13:11:06 +03004616
James E. Blaira002b032017-04-18 10:35:48 -07004617class TestExecutor(ZuulTestCase):
4618 tenant_config_file = 'config/single-tenant/main.yaml'
4619
4620 def assertFinalState(self):
4621 # In this test, we expect to shut down in a non-final state,
4622 # so skip these checks.
4623 pass
4624
4625 def assertCleanShutdown(self):
4626 self.log.debug("Assert clean shutdown")
4627
4628 # After shutdown, make sure no jobs are running
4629 self.assertEqual({}, self.executor_server.job_workers)
4630
4631 # Make sure that git.Repo objects have been garbage collected.
4632 repos = []
4633 gc.collect()
4634 for obj in gc.get_objects():
4635 if isinstance(obj, git.Repo):
4636 self.log.debug("Leaked git repo object: %s" % repr(obj))
4637 repos.append(obj)
4638 self.assertEqual(len(repos), 0)
4639
4640 def test_executor_shutdown(self):
4641 "Test that the executor can shut down with jobs running"
4642
4643 self.executor_server.hold_jobs_in_build = True
4644 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004645 A.addApproval('Code-Review', 2)
4646 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004647 self.waitUntilSettled()
4648
4649
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004650class TestDependencyGraph(ZuulTestCase):
4651 tenant_config_file = 'config/dependency-graph/main.yaml'
4652
4653 def test_dependeny_graph_dispatch_jobs_once(self):
4654 "Test a job in a dependency graph is queued only once"
4655 # Job dependencies, starting with A
4656 # A
4657 # / \
4658 # B C
4659 # / \ / \
4660 # D F E
4661 # |
4662 # G
4663
4664 self.executor_server.hold_jobs_in_build = True
4665 change = self.fake_gerrit.addFakeChange(
4666 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004667 change.addApproval('Code-Review', 2)
4668 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004669
4670 self.waitUntilSettled()
4671 self.assertEqual([b.name for b in self.builds], ['A'])
4672
4673 self.executor_server.release('A')
4674 self.waitUntilSettled()
4675 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4676
4677 self.executor_server.release('B')
4678 self.waitUntilSettled()
4679 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4680
4681 self.executor_server.release('D')
4682 self.waitUntilSettled()
4683 self.assertEqual([b.name for b in self.builds], ['C'])
4684
4685 self.executor_server.release('C')
4686 self.waitUntilSettled()
4687 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4688
4689 self.executor_server.release('F')
4690 self.waitUntilSettled()
4691 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4692
4693 self.executor_server.release('G')
4694 self.waitUntilSettled()
4695 self.assertEqual([b.name for b in self.builds], ['E'])
4696
4697 self.executor_server.release('E')
4698 self.waitUntilSettled()
4699 self.assertEqual(len(self.builds), 0)
4700
4701 self.executor_server.hold_jobs_in_build = False
4702 self.executor_server.release()
4703 self.waitUntilSettled()
4704
4705 self.assertEqual(len(self.builds), 0)
4706 self.assertEqual(len(self.history), 7)
4707
4708 self.assertEqual(change.data['status'], 'MERGED')
4709 self.assertEqual(change.reported, 2)
4710
4711 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4712 "Test that a job waits till all dependencies are successful"
4713 # Job dependencies, starting with A
4714 # A
4715 # / \
4716 # B C*
4717 # / \ / \
4718 # D F E
4719 # |
4720 # G
4721
4722 self.executor_server.hold_jobs_in_build = True
4723 change = self.fake_gerrit.addFakeChange(
4724 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004725 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004726
4727 self.executor_server.failJob('C', change)
4728
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004729 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004730
4731 self.waitUntilSettled()
4732 self.assertEqual([b.name for b in self.builds], ['A'])
4733
4734 self.executor_server.release('A')
4735 self.waitUntilSettled()
4736 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4737
4738 self.executor_server.release('B')
4739 self.waitUntilSettled()
4740 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4741
4742 self.executor_server.release('D')
4743 self.waitUntilSettled()
4744 self.assertEqual([b.name for b in self.builds], ['C'])
4745
4746 self.executor_server.release('C')
4747 self.waitUntilSettled()
4748 self.assertEqual(len(self.builds), 0)
4749
4750 self.executor_server.hold_jobs_in_build = False
4751 self.executor_server.release()
4752 self.waitUntilSettled()
4753
4754 self.assertEqual(len(self.builds), 0)
4755 self.assertEqual(len(self.history), 4)
4756
4757 self.assertEqual(change.data['status'], 'NEW')
4758 self.assertEqual(change.reported, 2)
4759
4760
Arieb6f068c2016-10-09 13:11:06 +03004761class TestDuplicatePipeline(ZuulTestCase):
4762 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4763
4764 def test_duplicate_pipelines(self):
4765 "Test that a change matching multiple pipelines works"
4766
4767 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4768 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4769 self.waitUntilSettled()
4770
4771 self.assertHistory([
4772 dict(name='project-test1', result='SUCCESS', changes='1,1',
4773 pipeline='dup1'),
4774 dict(name='project-test1', result='SUCCESS', changes='1,1',
4775 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004776 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004777
4778 self.assertEqual(len(A.messages), 2)
4779
Paul Belangera46a3742016-11-09 14:23:03 -05004780 if 'dup1' in A.messages[0]:
4781 self.assertIn('dup1', A.messages[0])
4782 self.assertNotIn('dup2', A.messages[0])
4783 self.assertIn('project-test1', A.messages[0])
4784 self.assertIn('dup2', A.messages[1])
4785 self.assertNotIn('dup1', A.messages[1])
4786 self.assertIn('project-test1', A.messages[1])
4787 else:
4788 self.assertIn('dup1', A.messages[1])
4789 self.assertNotIn('dup2', A.messages[1])
4790 self.assertIn('project-test1', A.messages[1])
4791 self.assertIn('dup2', A.messages[0])
4792 self.assertNotIn('dup1', A.messages[0])
4793 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004794
4795
Paul Belanger86085b32016-11-03 12:48:57 -04004796class TestSchedulerTemplatedProject(ZuulTestCase):
4797 tenant_config_file = 'config/templated-project/main.yaml'
4798
Paul Belanger174a8272017-03-14 13:20:10 -04004799 def test_job_from_templates_executed(self):
4800 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004801
4802 A = self.fake_gerrit.addFakeChange(
4803 'org/templated-project', 'master', 'A')
4804 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4805 self.waitUntilSettled()
4806
4807 self.assertEqual(self.getJobFromHistory('project-test1').result,
4808 'SUCCESS')
4809 self.assertEqual(self.getJobFromHistory('project-test2').result,
4810 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004811
4812 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004813 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004814
4815 A = self.fake_gerrit.addFakeChange(
4816 'org/layered-project', 'master', 'A')
4817 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4818 self.waitUntilSettled()
4819
4820 self.assertEqual(self.getJobFromHistory('project-test1').result,
4821 'SUCCESS')
4822 self.assertEqual(self.getJobFromHistory('project-test2').result,
4823 'SUCCESS')
4824 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4825 ).result, 'SUCCESS')
4826 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4827 ).result, 'SUCCESS')
4828 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4829 ).result, 'SUCCESS')
4830 self.assertEqual(self.getJobFromHistory('project-test6').result,
4831 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004832
4833
4834class TestSchedulerSuccessURL(ZuulTestCase):
4835 tenant_config_file = 'config/success-url/main.yaml'
4836
4837 def test_success_url(self):
4838 "Ensure bad build params are ignored"
4839 self.sched.reconfigure(self.config)
4840 self.init_repo('org/docs')
4841
4842 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4843 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4844 self.waitUntilSettled()
4845
4846 # Both builds ran: docs-draft-test + docs-draft-test2
4847 self.assertEqual(len(self.history), 2)
4848
4849 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004850 for build in self.history:
4851 if build.name == 'docs-draft-test':
4852 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004853 elif build.name == 'docs-draft-test2':
4854 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004855
4856 # Two msgs: 'Starting...' + results
4857 self.assertEqual(len(self.smtp_messages), 2)
4858 body = self.smtp_messages[1]['body'].splitlines()
4859 self.assertEqual('Build succeeded.', body[0])
4860
4861 self.assertIn(
4862 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4863 'docs-draft-test/%s/publish-docs/' % uuid,
4864 body[2])
4865
Paul Belanger174a8272017-03-14 13:20:10 -04004866 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004867 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004868 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4869 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004870 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004871 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004872
4873
Adam Gandelman4da00f62016-12-09 15:47:33 -08004874class TestSchedulerMerges(ZuulTestCase):
4875 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004876
4877 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004878 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004879 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004880 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004881 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4882 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4883 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004884 A.addApproval('Code-Review', 2)
4885 B.addApproval('Code-Review', 2)
4886 C.addApproval('Code-Review', 2)
4887 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4888 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4889 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004890 self.waitUntilSettled()
4891
4892 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004893 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4894 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004895 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004896 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004897 repo_messages.reverse()
4898
Paul Belanger174a8272017-03-14 13:20:10 -04004899 self.executor_server.hold_jobs_in_build = False
4900 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004901 self.waitUntilSettled()
4902
4903 return repo_messages
4904
4905 def _test_merge(self, mode):
4906 us_path = os.path.join(
4907 self.upstream_root, 'org/project-%s' % mode)
4908 expected_messages = [
4909 'initial commit',
4910 'add content from fixture',
4911 # the intermediate commits order is nondeterministic
4912 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4913 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4914 ]
4915 result = self._test_project_merge_mode(mode)
4916 self.assertEqual(result[:2], expected_messages[:2])
4917 self.assertEqual(result[-2:], expected_messages[-2:])
4918
4919 def test_project_merge_mode_merge(self):
4920 self._test_merge('merge')
4921
4922 def test_project_merge_mode_merge_resolve(self):
4923 self._test_merge('merge-resolve')
4924
4925 def test_project_merge_mode_cherrypick(self):
4926 expected_messages = [
4927 'initial commit',
4928 'add content from fixture',
4929 'A-1',
4930 'B-1',
4931 'C-1']
4932 result = self._test_project_merge_mode('cherry-pick')
4933 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004934
4935 def test_merge_branch(self):
4936 "Test that the right commits are on alternate branches"
4937 self.create_branch('org/project-merge-branches', 'mp')
4938
Paul Belanger174a8272017-03-14 13:20:10 -04004939 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004940 A = self.fake_gerrit.addFakeChange(
4941 'org/project-merge-branches', 'mp', 'A')
4942 B = self.fake_gerrit.addFakeChange(
4943 'org/project-merge-branches', 'mp', 'B')
4944 C = self.fake_gerrit.addFakeChange(
4945 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004946 A.addApproval('Code-Review', 2)
4947 B.addApproval('Code-Review', 2)
4948 C.addApproval('Code-Review', 2)
4949 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4950 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4951 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004952 self.waitUntilSettled()
4953
Paul Belanger174a8272017-03-14 13:20:10 -04004954 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004955 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004956 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004957 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004958 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004959 self.waitUntilSettled()
4960
4961 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004962 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004963 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4964 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004965 repo = git.Repo(path)
4966
James E. Blair247cab72017-07-20 16:52:36 -07004967 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004968 repo_messages.reverse()
4969 correct_messages = [
4970 'initial commit',
4971 'add content from fixture',
4972 'mp commit',
4973 'A-1', 'B-1', 'C-1']
4974 self.assertEqual(repo_messages, correct_messages)
4975
Paul Belanger174a8272017-03-14 13:20:10 -04004976 self.executor_server.hold_jobs_in_build = False
4977 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004978 self.waitUntilSettled()
4979
4980 def test_merge_multi_branch(self):
4981 "Test that dependent changes on multiple branches are merged"
4982 self.create_branch('org/project-merge-branches', 'mp')
4983
Paul Belanger174a8272017-03-14 13:20:10 -04004984 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004985 A = self.fake_gerrit.addFakeChange(
4986 'org/project-merge-branches', 'master', 'A')
4987 B = self.fake_gerrit.addFakeChange(
4988 'org/project-merge-branches', 'mp', 'B')
4989 C = self.fake_gerrit.addFakeChange(
4990 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004991 A.addApproval('Code-Review', 2)
4992 B.addApproval('Code-Review', 2)
4993 C.addApproval('Code-Review', 2)
4994 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4995 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4996 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004997 self.waitUntilSettled()
4998
4999 job_A = None
5000 for job in self.builds:
5001 if 'project-merge' in job.name:
5002 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005003
James E. Blair2a535672017-04-27 12:03:15 -07005004 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5005 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005006 repo = git.Repo(path)
5007 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005008 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005009 repo_messages.reverse()
5010 correct_messages = [
5011 'initial commit', 'add content from fixture', 'A-1']
5012 self.assertEqual(repo_messages, correct_messages)
5013
Paul Belanger174a8272017-03-14 13:20:10 -04005014 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005015 self.waitUntilSettled()
5016
5017 job_B = None
5018 for job in self.builds:
5019 if 'project-merge' in job.name:
5020 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005021
James E. Blair2a535672017-04-27 12:03:15 -07005022 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5023 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005024 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005025 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005026 repo_messages.reverse()
5027 correct_messages = [
5028 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5029 self.assertEqual(repo_messages, correct_messages)
5030
Paul Belanger174a8272017-03-14 13:20:10 -04005031 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005032 self.waitUntilSettled()
5033
5034 job_C = None
5035 for job in self.builds:
5036 if 'project-merge' in job.name:
5037 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005038
James E. Blair2a535672017-04-27 12:03:15 -07005039 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5040 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005041 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005042 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005043
5044 repo_messages.reverse()
5045 correct_messages = [
5046 'initial commit', 'add content from fixture',
5047 'A-1', 'C-1']
5048 # Ensure the right commits are in the history for this ref
5049 self.assertEqual(repo_messages, correct_messages)
5050
Paul Belanger174a8272017-03-14 13:20:10 -04005051 self.executor_server.hold_jobs_in_build = False
5052 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005053 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005054
5055
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005056class TestSemaphore(ZuulTestCase):
5057 tenant_config_file = 'config/semaphore/main.yaml'
5058
5059 def test_semaphore_one(self):
5060 "Test semaphores with max=1 (mutex)"
5061 tenant = self.sched.abide.tenants.get('tenant-one')
5062
5063 self.executor_server.hold_jobs_in_build = True
5064
5065 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5066 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5067 self.assertFalse('test-semaphore' in
5068 tenant.semaphore_handler.semaphores)
5069
5070 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5071 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5072 self.waitUntilSettled()
5073
5074 self.assertEqual(len(self.builds), 3)
5075 self.assertEqual(self.builds[0].name, 'project-test1')
5076 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5077 self.assertEqual(self.builds[2].name, 'project-test1')
5078
5079 self.executor_server.release('semaphore-one-test1')
5080 self.waitUntilSettled()
5081
5082 self.assertEqual(len(self.builds), 3)
5083 self.assertEqual(self.builds[0].name, 'project-test1')
5084 self.assertEqual(self.builds[1].name, 'project-test1')
5085 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5086 self.assertTrue('test-semaphore' in
5087 tenant.semaphore_handler.semaphores)
5088
5089 self.executor_server.release('semaphore-one-test2')
5090 self.waitUntilSettled()
5091
5092 self.assertEqual(len(self.builds), 3)
5093 self.assertEqual(self.builds[0].name, 'project-test1')
5094 self.assertEqual(self.builds[1].name, 'project-test1')
5095 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5096 self.assertTrue('test-semaphore' in
5097 tenant.semaphore_handler.semaphores)
5098
5099 self.executor_server.release('semaphore-one-test1')
5100 self.waitUntilSettled()
5101
5102 self.assertEqual(len(self.builds), 3)
5103 self.assertEqual(self.builds[0].name, 'project-test1')
5104 self.assertEqual(self.builds[1].name, 'project-test1')
5105 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5106 self.assertTrue('test-semaphore' in
5107 tenant.semaphore_handler.semaphores)
5108
5109 self.executor_server.release('semaphore-one-test2')
5110 self.waitUntilSettled()
5111
5112 self.assertEqual(len(self.builds), 2)
5113 self.assertEqual(self.builds[0].name, 'project-test1')
5114 self.assertEqual(self.builds[1].name, 'project-test1')
5115 self.assertFalse('test-semaphore' in
5116 tenant.semaphore_handler.semaphores)
5117
5118 self.executor_server.hold_jobs_in_build = False
5119 self.executor_server.release()
5120
5121 self.waitUntilSettled()
5122 self.assertEqual(len(self.builds), 0)
5123
5124 self.assertEqual(A.reported, 1)
5125 self.assertEqual(B.reported, 1)
5126 self.assertFalse('test-semaphore' in
5127 tenant.semaphore_handler.semaphores)
5128
5129 def test_semaphore_two(self):
5130 "Test semaphores with max>1"
5131 tenant = self.sched.abide.tenants.get('tenant-one')
5132
5133 self.executor_server.hold_jobs_in_build = True
5134 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5135 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5136 self.assertFalse('test-semaphore-two' in
5137 tenant.semaphore_handler.semaphores)
5138
5139 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5140 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5141 self.waitUntilSettled()
5142
5143 self.assertEqual(len(self.builds), 4)
5144 self.assertEqual(self.builds[0].name, 'project-test1')
5145 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5146 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5147 self.assertEqual(self.builds[3].name, 'project-test1')
5148 self.assertTrue('test-semaphore-two' in
5149 tenant.semaphore_handler.semaphores)
5150 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5151 'test-semaphore-two', [])), 2)
5152
5153 self.executor_server.release('semaphore-two-test1')
5154 self.waitUntilSettled()
5155
5156 self.assertEqual(len(self.builds), 4)
5157 self.assertEqual(self.builds[0].name, 'project-test1')
5158 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5159 self.assertEqual(self.builds[2].name, 'project-test1')
5160 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5161 self.assertTrue('test-semaphore-two' in
5162 tenant.semaphore_handler.semaphores)
5163 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5164 'test-semaphore-two', [])), 2)
5165
5166 self.executor_server.release('semaphore-two-test2')
5167 self.waitUntilSettled()
5168
5169 self.assertEqual(len(self.builds), 4)
5170 self.assertEqual(self.builds[0].name, 'project-test1')
5171 self.assertEqual(self.builds[1].name, 'project-test1')
5172 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5173 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5174 self.assertTrue('test-semaphore-two' in
5175 tenant.semaphore_handler.semaphores)
5176 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5177 'test-semaphore-two', [])), 2)
5178
5179 self.executor_server.release('semaphore-two-test1')
5180 self.waitUntilSettled()
5181
5182 self.assertEqual(len(self.builds), 3)
5183 self.assertEqual(self.builds[0].name, 'project-test1')
5184 self.assertEqual(self.builds[1].name, 'project-test1')
5185 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5186 self.assertTrue('test-semaphore-two' in
5187 tenant.semaphore_handler.semaphores)
5188 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5189 'test-semaphore-two', [])), 1)
5190
5191 self.executor_server.release('semaphore-two-test2')
5192 self.waitUntilSettled()
5193
5194 self.assertEqual(len(self.builds), 2)
5195 self.assertEqual(self.builds[0].name, 'project-test1')
5196 self.assertEqual(self.builds[1].name, 'project-test1')
5197 self.assertFalse('test-semaphore-two' in
5198 tenant.semaphore_handler.semaphores)
5199
5200 self.executor_server.hold_jobs_in_build = False
5201 self.executor_server.release()
5202
5203 self.waitUntilSettled()
5204 self.assertEqual(len(self.builds), 0)
5205
5206 self.assertEqual(A.reported, 1)
5207 self.assertEqual(B.reported, 1)
5208
5209 def test_semaphore_abandon(self):
5210 "Test abandon with job semaphores"
5211 self.executor_server.hold_jobs_in_build = True
5212 tenant = self.sched.abide.tenants.get('tenant-one')
5213 check_pipeline = tenant.layout.pipelines['check']
5214
5215 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5216 self.assertFalse('test-semaphore' in
5217 tenant.semaphore_handler.semaphores)
5218
5219 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5220 self.waitUntilSettled()
5221
5222 self.assertTrue('test-semaphore' in
5223 tenant.semaphore_handler.semaphores)
5224
5225 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5226 self.waitUntilSettled()
5227
5228 # The check pipeline should be empty
5229 items = check_pipeline.getAllItems()
5230 self.assertEqual(len(items), 0)
5231
5232 # The semaphore should be released
5233 self.assertFalse('test-semaphore' in
5234 tenant.semaphore_handler.semaphores)
5235
5236 self.executor_server.hold_jobs_in_build = False
5237 self.executor_server.release()
5238 self.waitUntilSettled()
5239
Tobias Henkel0f714002017-06-30 23:30:52 +02005240 def test_semaphore_new_patchset(self):
5241 "Test new patchset with job semaphores"
5242 self.executor_server.hold_jobs_in_build = True
5243 tenant = self.sched.abide.tenants.get('tenant-one')
5244 check_pipeline = tenant.layout.pipelines['check']
5245
5246 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5247 self.assertFalse('test-semaphore' in
5248 tenant.semaphore_handler.semaphores)
5249
5250 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5251 self.waitUntilSettled()
5252
5253 self.assertTrue('test-semaphore' in
5254 tenant.semaphore_handler.semaphores)
5255 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5256 self.assertEqual(len(semaphore), 1)
5257
5258 A.addPatchset()
5259 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5260 self.waitUntilSettled()
5261
5262 self.assertTrue('test-semaphore' in
5263 tenant.semaphore_handler.semaphores)
5264 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5265 self.assertEqual(len(semaphore), 1)
5266
5267 items = check_pipeline.getAllItems()
5268 self.assertEqual(items[0].change.number, '1')
5269 self.assertEqual(items[0].change.patchset, '2')
5270 self.assertTrue(items[0].live)
5271
5272 self.executor_server.hold_jobs_in_build = False
5273 self.executor_server.release()
5274 self.waitUntilSettled()
5275
5276 # The semaphore should be released
5277 self.assertFalse('test-semaphore' in
5278 tenant.semaphore_handler.semaphores)
5279
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005280 def test_semaphore_reconfigure(self):
5281 "Test reconfigure with job semaphores"
5282 self.executor_server.hold_jobs_in_build = True
5283 tenant = self.sched.abide.tenants.get('tenant-one')
5284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5285 self.assertFalse('test-semaphore' in
5286 tenant.semaphore_handler.semaphores)
5287
5288 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5289 self.waitUntilSettled()
5290
5291 self.assertTrue('test-semaphore' in
5292 tenant.semaphore_handler.semaphores)
5293
5294 # reconfigure without layout change
5295 self.sched.reconfigure(self.config)
5296 self.waitUntilSettled()
5297 tenant = self.sched.abide.tenants.get('tenant-one')
5298
5299 # semaphore still must be held
5300 self.assertTrue('test-semaphore' in
5301 tenant.semaphore_handler.semaphores)
5302
5303 self.commitConfigUpdate(
5304 'common-config',
5305 'config/semaphore/zuul-reconfiguration.yaml')
5306 self.sched.reconfigure(self.config)
5307 self.waitUntilSettled()
5308 tenant = self.sched.abide.tenants.get('tenant-one')
5309
5310 self.executor_server.release('project-test1')
5311 self.waitUntilSettled()
5312
5313 # There should be no builds anymore
5314 self.assertEqual(len(self.builds), 0)
5315
5316 # The semaphore should be released
5317 self.assertFalse('test-semaphore' in
5318 tenant.semaphore_handler.semaphores)
5319
5320
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005321class TestSemaphoreMultiTenant(ZuulTestCase):
5322 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5323
5324 def test_semaphore_tenant_isolation(self):
5325 "Test semaphores in multiple tenants"
5326
5327 self.waitUntilSettled()
5328 tenant_one = self.sched.abide.tenants.get('tenant-one')
5329 tenant_two = self.sched.abide.tenants.get('tenant-two')
5330
5331 self.executor_server.hold_jobs_in_build = True
5332 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5333 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5334 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5335 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5336 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5337 self.assertFalse('test-semaphore' in
5338 tenant_one.semaphore_handler.semaphores)
5339 self.assertFalse('test-semaphore' in
5340 tenant_two.semaphore_handler.semaphores)
5341
5342 # add patches to project1 of tenant-one
5343 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5344 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5345 self.waitUntilSettled()
5346
5347 # one build of project1-test1 must run
5348 # semaphore of tenant-one must be acquired once
5349 # semaphore of tenant-two must not be acquired
5350 self.assertEqual(len(self.builds), 1)
5351 self.assertEqual(self.builds[0].name, 'project1-test1')
5352 self.assertTrue('test-semaphore' in
5353 tenant_one.semaphore_handler.semaphores)
5354 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5355 'test-semaphore', [])), 1)
5356 self.assertFalse('test-semaphore' in
5357 tenant_two.semaphore_handler.semaphores)
5358
5359 # add patches to project2 of tenant-two
5360 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5361 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5362 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5363 self.waitUntilSettled()
5364
5365 # one build of project1-test1 must run
5366 # two builds of project2-test1 must run
5367 # semaphore of tenant-one must be acquired once
5368 # semaphore of tenant-two must be acquired twice
5369 self.assertEqual(len(self.builds), 3)
5370 self.assertEqual(self.builds[0].name, 'project1-test1')
5371 self.assertEqual(self.builds[1].name, 'project2-test1')
5372 self.assertEqual(self.builds[2].name, 'project2-test1')
5373 self.assertTrue('test-semaphore' in
5374 tenant_one.semaphore_handler.semaphores)
5375 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5376 'test-semaphore', [])), 1)
5377 self.assertTrue('test-semaphore' in
5378 tenant_two.semaphore_handler.semaphores)
5379 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5380 'test-semaphore', [])), 2)
5381
5382 self.executor_server.release('project1-test1')
5383 self.waitUntilSettled()
5384
5385 # one build of project1-test1 must run
5386 # two builds of project2-test1 must run
5387 # semaphore of tenant-one must be acquired once
5388 # semaphore of tenant-two must be acquired twice
5389 self.assertEqual(len(self.builds), 3)
5390 self.assertEqual(self.builds[0].name, 'project2-test1')
5391 self.assertEqual(self.builds[1].name, 'project2-test1')
5392 self.assertEqual(self.builds[2].name, 'project1-test1')
5393 self.assertTrue('test-semaphore' in
5394 tenant_one.semaphore_handler.semaphores)
5395 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5396 'test-semaphore', [])), 1)
5397 self.assertTrue('test-semaphore' in
5398 tenant_two.semaphore_handler.semaphores)
5399 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5400 'test-semaphore', [])), 2)
5401
5402 self.executor_server.release('project2-test1')
5403 self.waitUntilSettled()
5404
5405 # one build of project1-test1 must run
5406 # one build of project2-test1 must run
5407 # semaphore of tenant-one must be acquired once
5408 # semaphore of tenant-two must be acquired once
5409 self.assertEqual(len(self.builds), 2)
5410 self.assertTrue('test-semaphore' in
5411 tenant_one.semaphore_handler.semaphores)
5412 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5413 'test-semaphore', [])), 1)
5414 self.assertTrue('test-semaphore' in
5415 tenant_two.semaphore_handler.semaphores)
5416 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5417 'test-semaphore', [])), 1)
5418
5419 self.executor_server.hold_jobs_in_build = False
5420 self.executor_server.release()
5421
5422 self.waitUntilSettled()
5423
5424 # no build must run
5425 # semaphore of tenant-one must not be acquired
5426 # semaphore of tenant-two must not be acquired
5427 self.assertEqual(len(self.builds), 0)
5428 self.assertFalse('test-semaphore' in
5429 tenant_one.semaphore_handler.semaphores)
5430 self.assertFalse('test-semaphore' in
5431 tenant_two.semaphore_handler.semaphores)
5432
5433 self.assertEqual(A.reported, 1)
5434 self.assertEqual(B.reported, 1)
5435
5436
5437class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005438 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005439 tenant_config_file = 'config/in-repo/main.yaml'
5440
5441 def test_semaphore_in_repo(self):
5442 "Test semaphores in repo config"
5443
5444 # This tests dynamic semaphore handling in project repos. The semaphore
5445 # max value should not be evaluated dynamically but must be updated
5446 # after the change lands.
5447
5448 self.waitUntilSettled()
5449 tenant = self.sched.abide.tenants.get('tenant-one')
5450
5451 in_repo_conf = textwrap.dedent(
5452 """
5453 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005454 name: project-test1
5455
5456 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005457 name: project-test2
5458 semaphore: test-semaphore
5459
5460 - project:
5461 name: org/project
5462 tenant-one-gate:
5463 jobs:
5464 - project-test2
5465
5466 # the max value in dynamic layout must be ignored
5467 - semaphore:
5468 name: test-semaphore
5469 max: 2
5470 """)
5471
5472 in_repo_playbook = textwrap.dedent(
5473 """
5474 - hosts: all
5475 tasks: []
5476 """)
5477
5478 file_dict = {'.zuul.yaml': in_repo_conf,
5479 'playbooks/project-test2.yaml': in_repo_playbook}
5480 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5481 files=file_dict)
5482 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5483 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5484 B.setDependsOn(A, 1)
5485 C.setDependsOn(A, 1)
5486
5487 self.executor_server.hold_jobs_in_build = True
5488
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005489 A.addApproval('Code-Review', 2)
5490 B.addApproval('Code-Review', 2)
5491 C.addApproval('Code-Review', 2)
5492 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5493 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5494 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005495 self.waitUntilSettled()
5496
5497 # check that the layout in a queue item still has max value of 1
5498 # for test-semaphore
5499 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5500 queue = None
5501 for queue_candidate in pipeline.queues:
5502 if queue_candidate.name == 'org/project':
5503 queue = queue_candidate
5504 break
5505 queue_item = queue.queue[0]
5506 item_dynamic_layout = queue_item.current_build_set.layout
5507 dynamic_test_semaphore = \
5508 item_dynamic_layout.semaphores.get('test-semaphore')
5509 self.assertEqual(dynamic_test_semaphore.max, 1)
5510
5511 # one build must be in queue, one semaphores acquired
5512 self.assertEqual(len(self.builds), 1)
5513 self.assertEqual(self.builds[0].name, 'project-test2')
5514 self.assertTrue('test-semaphore' in
5515 tenant.semaphore_handler.semaphores)
5516 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5517 'test-semaphore', [])), 1)
5518
5519 self.executor_server.release('project-test2')
5520 self.waitUntilSettled()
5521
5522 # change A must be merged
5523 self.assertEqual(A.data['status'], 'MERGED')
5524 self.assertEqual(A.reported, 2)
5525
5526 # send change-merged event as the gerrit mock doesn't send it
5527 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5528 self.waitUntilSettled()
5529
5530 # now that change A was merged, the new semaphore max must be effective
5531 tenant = self.sched.abide.tenants.get('tenant-one')
5532 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5533
5534 # two builds must be in queue, two semaphores acquired
5535 self.assertEqual(len(self.builds), 2)
5536 self.assertEqual(self.builds[0].name, 'project-test2')
5537 self.assertEqual(self.builds[1].name, 'project-test2')
5538 self.assertTrue('test-semaphore' in
5539 tenant.semaphore_handler.semaphores)
5540 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5541 'test-semaphore', [])), 2)
5542
5543 self.executor_server.release('project-test2')
5544 self.waitUntilSettled()
5545
5546 self.assertEqual(len(self.builds), 0)
5547 self.assertFalse('test-semaphore' in
5548 tenant.semaphore_handler.semaphores)
5549
5550 self.executor_server.hold_jobs_in_build = False
5551 self.executor_server.release()
5552
5553 self.waitUntilSettled()
5554 self.assertEqual(len(self.builds), 0)
5555
5556 self.assertEqual(A.reported, 2)
5557 self.assertEqual(B.reported, 2)
5558 self.assertEqual(C.reported, 2)