blob: e80a30a7e5a5b582824599ed89f102542448e10e [file] [log] [blame]
James E. Blairb0fcae42012-07-17 11:12:10 -07001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira002b032017-04-18 10:35:48 -070017import gc
James E. Blairb0fcae42012-07-17 11:12:10 -070018import json
Tobias Henkel9a0e1942017-03-20 16:16:02 +010019import textwrap
20
Monty Taylorbc758832013-06-17 17:22:42 -040021import os
James E. Blairb0fcae42012-07-17 11:12:10 -070022import re
James E. Blair4886cc12012-07-18 15:39:41 -070023import shutil
Monty Taylorbc758832013-06-17 17:22:42 -040024import time
Clint Byrum03454a52017-05-26 17:14:02 -070025from unittest import skip
Monty Taylorbc758832013-06-17 17:22:42 -040026
James E. Blair4886cc12012-07-18 15:39:41 -070027import git
Monty Taylorbc758832013-06-17 17:22:42 -040028import testtools
Monty Taylorb934c1a2017-06-16 19:31:47 -050029import urllib
James E. Blairb0fcae42012-07-17 11:12:10 -070030
Maru Newby3fe5f852015-01-13 04:22:14 +000031import zuul.change_matcher
Clint Byrumb9de8be2017-05-27 13:49:19 -070032from zuul.driver.gerrit import gerritreporter
James E. Blairb0fcae42012-07-17 11:12:10 -070033import zuul.scheduler
James E. Blairad28e912013-11-27 10:43:22 -080034import zuul.rpcclient
Paul Belanger9bba4902016-11-02 16:07:33 -040035import zuul.model
James E. Blairb0fcae42012-07-17 11:12:10 -070036
Maru Newby3fe5f852015-01-13 04:22:14 +000037from tests.base import (
Paul Belanger0a21f0a2017-06-13 13:14:42 -040038 SSLZuulTestCase,
Maru Newby3fe5f852015-01-13 04:22:14 +000039 ZuulTestCase,
40 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070041 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000042)
James E. Blairb0fcae42012-07-17 11:12:10 -070043
James E. Blairb0fcae42012-07-17 11:12:10 -070044
Paul Belanger0a21f0a2017-06-13 13:14:42 -040045class TestSchedulerSSL(SSLZuulTestCase):
46 tenant_config_file = 'config/single-tenant/main.yaml'
47
48 def test_jobs_executed(self):
49 "Test that jobs are executed and a change is merged"
50
51 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020052 A.addApproval('Code-Review', 2)
53 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Paul Belanger0a21f0a2017-06-13 13:14:42 -040054 self.waitUntilSettled()
55 self.assertEqual(self.getJobFromHistory('project-merge').result,
56 'SUCCESS')
57 self.assertEqual(self.getJobFromHistory('project-test1').result,
58 'SUCCESS')
59 self.assertEqual(self.getJobFromHistory('project-test2').result,
60 'SUCCESS')
61 self.assertEqual(A.data['status'], 'MERGED')
62 self.assertEqual(A.reported, 2)
63 self.assertEqual(self.getJobFromHistory('project-test1').node,
64 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070065 self.assertEqual(self.getJobFromHistory('project-test2').node,
66 'label1')
Paul Belanger0a21f0a2017-06-13 13:14:42 -040067
68
Clark Boylanb640e052014-04-03 16:41:46 -070069class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070070 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010071
Paul Belanger174a8272017-03-14 13:20:10 -040072 def test_jobs_executed(self):
73 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070074
James E. Blairb0fcae42012-07-17 11:12:10 -070075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +020076 A.addApproval('Code-Review', 2)
77 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070078 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040079 self.assertEqual(self.getJobFromHistory('project-merge').result,
80 'SUCCESS')
81 self.assertEqual(self.getJobFromHistory('project-test1').result,
82 'SUCCESS')
83 self.assertEqual(self.getJobFromHistory('project-test2').result,
84 'SUCCESS')
85 self.assertEqual(A.data['status'], 'MERGED')
86 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070087 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070088 'label1')
James E. Blairfcbf95d2017-07-25 16:38:09 -070089 self.assertEqual(self.getJobFromHistory('project-test2').node,
90 'label1')
James E. Blairb0fcae42012-07-17 11:12:10 -070091
James E. Blair552b54f2016-07-22 13:55:32 -070092 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050093 # Per-driver
94 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
95 # Per-driver per-connection
96 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
97 value='1|c')
James E. Blair66eeebf2013-07-27 17:44:32 -070098 self.assertReportedStat('zuul.pipeline.gate.current_changes',
99 value='1|g')
100 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
101 kind='ms')
102 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
103 value='1|c')
104 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
105 self.assertReportedStat('zuul.pipeline.gate.total_changes',
106 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700107 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700108 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -0700109 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -0700110 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700111
James E. Blair2ef29e92017-07-21 15:25:05 -0700112 for build in self.history:
113 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700114
James E. Blair3cb10702013-08-24 08:56:03 -0700115 def test_initial_pipeline_gauges(self):
116 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700117 self.assertReportedStat('zuul.pipeline.gate.current_changes',
118 value='0|g')
119 self.assertReportedStat('zuul.pipeline.check.current_changes',
120 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700121
James E. Blair34776ee2016-08-25 13:53:54 -0700122 def test_job_branch(self):
123 "Test the correct variant of a job runs on a branch"
124 self.create_branch('org/project', 'stable')
125 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200126 A.addApproval('Code-Review', 2)
127 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700128 self.waitUntilSettled()
129 self.assertEqual(self.getJobFromHistory('project-test1').result,
130 'SUCCESS')
131 self.assertEqual(self.getJobFromHistory('project-test2').result,
132 'SUCCESS')
133 self.assertEqual(A.data['status'], 'MERGED')
134 self.assertEqual(A.reported, 2,
135 "A should report start and success")
136 self.assertIn('gate', A.messages[1],
137 "A should transit gate")
138 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700139 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700140
James E. Blairb0fcae42012-07-17 11:12:10 -0700141 def test_parallel_changes(self):
142 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700143
Paul Belanger174a8272017-03-14 13:20:10 -0400144 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700145 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
146 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
147 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200148 A.addApproval('Code-Review', 2)
149 B.addApproval('Code-Review', 2)
150 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700151
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200152 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
153 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
154 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700155
156 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400157 self.assertEqual(len(self.builds), 1)
158 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700159 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700160
Paul Belanger174a8272017-03-14 13:20:10 -0400161 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700162 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400163 self.assertEqual(len(self.builds), 3)
164 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400168 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700169 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700170
Paul Belanger174a8272017-03-14 13:20:10 -0400171 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700172 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400173 self.assertEqual(len(self.builds), 5)
174 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700175 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700177 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700180 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400181 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700182 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700183
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400184 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700185 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700186
Paul Belanger174a8272017-03-14 13:20:10 -0400187 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700188 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400189 self.assertEqual(len(self.builds), 6)
190 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700191 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400192 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700193 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700194
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400197 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700198 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700199
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700201 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400202 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700203 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700204
Paul Belanger174a8272017-03-14 13:20:10 -0400205 self.executor_server.hold_jobs_in_build = False
206 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700207 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700209
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400210 self.assertEqual(len(self.history), 9)
211 self.assertEqual(A.data['status'], 'MERGED')
212 self.assertEqual(B.data['status'], 'MERGED')
213 self.assertEqual(C.data['status'], 'MERGED')
214 self.assertEqual(A.reported, 2)
215 self.assertEqual(B.reported, 2)
216 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700217
218 def test_failed_changes(self):
219 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400220 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700221
James E. Blairb02a3bb2012-07-30 17:49:55 -0700222 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
223 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200224 A.addApproval('Code-Review', 2)
225 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700226
Paul Belanger174a8272017-03-14 13:20:10 -0400227 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700228
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200229 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
230 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700231 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700232 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400233
Paul Belanger174a8272017-03-14 13:20:10 -0400234 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400235 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700236 # A/project-merge is complete
237 self.assertBuilds([
238 dict(name='project-test1', changes='1,1'),
239 dict(name='project-test2', changes='1,1'),
240 dict(name='project-merge', changes='1,1 2,1'),
241 ])
James E. Blaire2819012013-06-28 17:17:26 -0400242
Paul Belanger174a8272017-03-14 13:20:10 -0400243 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400244 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700245 # A/project-merge is complete
246 # B/project-merge is complete
247 self.assertBuilds([
248 dict(name='project-test1', changes='1,1'),
249 dict(name='project-test2', changes='1,1'),
250 dict(name='project-test1', changes='1,1 2,1'),
251 dict(name='project-test2', changes='1,1 2,1'),
252 ])
253
254 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400255 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700256 self.builds[0].release()
257 self.waitUntilSettled()
258
259 self.orderedRelease()
260 self.assertHistory([
261 dict(name='project-merge', result='SUCCESS', changes='1,1'),
262 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
263 dict(name='project-test1', result='FAILURE', changes='1,1'),
264 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
265 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
266 dict(name='project-test2', result='SUCCESS', changes='1,1'),
267 dict(name='project-merge', result='SUCCESS', changes='2,1'),
268 dict(name='project-test1', result='SUCCESS', changes='2,1'),
269 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700270 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700271
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400272 self.assertEqual(A.data['status'], 'NEW')
273 self.assertEqual(B.data['status'], 'MERGED')
274 self.assertEqual(A.reported, 2)
275 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276
277 def test_independent_queues(self):
278 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700279
Paul Belanger174a8272017-03-14 13:20:10 -0400280 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900281 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700282 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
283 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200284 A.addApproval('Code-Review', 2)
285 B.addApproval('Code-Review', 2)
286 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700287
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200288 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800289 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700290
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200291 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800292 self.waitUntilSettled()
293
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200294 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700295 self.waitUntilSettled()
296
297 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700298 self.assertBuilds([
299 dict(name='project-merge', changes='1,1'),
300 dict(name='project-merge', changes='2,1'),
301 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700302
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700303 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700304 self.builds[0].release()
305 self.waitUntilSettled()
306 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700307 self.waitUntilSettled()
308 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400309 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700310 self.waitUntilSettled()
311
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700312 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700313 self.assertBuilds([
314 dict(name='project-test1', changes='1,1'),
315 dict(name='project-test2', changes='1,1'),
316 dict(name='project-test1', changes='2,1'),
317 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500318 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700319 dict(name='project-test1', changes='2,1 3,1'),
320 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800321 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700322 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700323
James E. Blair5f0d49e2016-08-19 09:36:50 -0700324 self.orderedRelease()
325 self.assertHistory([
326 dict(name='project-merge', result='SUCCESS', changes='1,1'),
327 dict(name='project-merge', result='SUCCESS', changes='2,1'),
328 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
329 dict(name='project-test1', result='SUCCESS', changes='1,1'),
330 dict(name='project-test2', result='SUCCESS', changes='1,1'),
331 dict(name='project-test1', result='SUCCESS', changes='2,1'),
332 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500333 dict(
334 name='project1-project2-integration',
335 result='SUCCESS',
336 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700337 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
338 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800339 dict(name='project1-project2-integration',
340 result='SUCCESS',
341 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700342 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700343
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400344 self.assertEqual(A.data['status'], 'MERGED')
345 self.assertEqual(B.data['status'], 'MERGED')
346 self.assertEqual(C.data['status'], 'MERGED')
347 self.assertEqual(A.reported, 2)
348 self.assertEqual(B.reported, 2)
349 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700350
351 def test_failed_change_at_head(self):
352 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700353
Paul Belanger174a8272017-03-14 13:20:10 -0400354 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700355 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
356 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
357 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200358 A.addApproval('Code-Review', 2)
359 B.addApproval('Code-Review', 2)
360 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700361
Paul Belanger174a8272017-03-14 13:20:10 -0400362 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700363
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200364 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
365 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
366 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700367
368 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700369
James E. Blairb536ecc2016-08-31 10:11:42 -0700370 self.assertBuilds([
371 dict(name='project-merge', changes='1,1'),
372 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700373
Paul Belanger174a8272017-03-14 13:20:10 -0400374 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700375 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400376 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700377 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400378 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700379 self.waitUntilSettled()
380
James E. Blairb536ecc2016-08-31 10:11:42 -0700381 self.assertBuilds([
382 dict(name='project-test1', changes='1,1'),
383 dict(name='project-test2', changes='1,1'),
384 dict(name='project-test1', changes='1,1 2,1'),
385 dict(name='project-test2', changes='1,1 2,1'),
386 dict(name='project-test1', changes='1,1 2,1 3,1'),
387 dict(name='project-test2', changes='1,1 2,1 3,1'),
388 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700389
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400390 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700391 self.waitUntilSettled()
392
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400393 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700394 self.assertBuilds([
395 dict(name='project-test2', changes='1,1'),
396 dict(name='project-merge', changes='2,1'),
397 ])
398 # Unordered history comparison because the aborts can finish
399 # in any order.
400 self.assertHistory([
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1'),
403 dict(name='project-merge', result='SUCCESS',
404 changes='1,1 2,1'),
405 dict(name='project-merge', result='SUCCESS',
406 changes='1,1 2,1 3,1'),
407 dict(name='project-test1', result='FAILURE',
408 changes='1,1'),
409 dict(name='project-test1', result='ABORTED',
410 changes='1,1 2,1'),
411 dict(name='project-test2', result='ABORTED',
412 changes='1,1 2,1'),
413 dict(name='project-test1', result='ABORTED',
414 changes='1,1 2,1 3,1'),
415 dict(name='project-test2', result='ABORTED',
416 changes='1,1 2,1 3,1'),
417 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700418
Paul Belanger174a8272017-03-14 13:20:10 -0400419 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700420 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400421 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700422 self.waitUntilSettled()
423 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700424
James E. Blairb536ecc2016-08-31 10:11:42 -0700425 self.assertBuilds([])
426 self.assertHistory([
427 dict(name='project-merge', result='SUCCESS',
428 changes='1,1'),
429 dict(name='project-merge', result='SUCCESS',
430 changes='1,1 2,1'),
431 dict(name='project-merge', result='SUCCESS',
432 changes='1,1 2,1 3,1'),
433 dict(name='project-test1', result='FAILURE',
434 changes='1,1'),
435 dict(name='project-test1', result='ABORTED',
436 changes='1,1 2,1'),
437 dict(name='project-test2', result='ABORTED',
438 changes='1,1 2,1'),
439 dict(name='project-test1', result='ABORTED',
440 changes='1,1 2,1 3,1'),
441 dict(name='project-test2', result='ABORTED',
442 changes='1,1 2,1 3,1'),
443 dict(name='project-merge', result='SUCCESS',
444 changes='2,1'),
445 dict(name='project-merge', result='SUCCESS',
446 changes='2,1 3,1'),
447 dict(name='project-test2', result='SUCCESS',
448 changes='1,1'),
449 dict(name='project-test1', result='SUCCESS',
450 changes='2,1'),
451 dict(name='project-test2', result='SUCCESS',
452 changes='2,1'),
453 dict(name='project-test1', result='SUCCESS',
454 changes='2,1 3,1'),
455 dict(name='project-test2', result='SUCCESS',
456 changes='2,1 3,1'),
457 ], ordered=False)
458
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400459 self.assertEqual(A.data['status'], 'NEW')
460 self.assertEqual(B.data['status'], 'MERGED')
461 self.assertEqual(C.data['status'], 'MERGED')
462 self.assertEqual(A.reported, 2)
463 self.assertEqual(B.reported, 2)
464 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700465
James E. Blair0aac4872013-08-23 14:02:38 -0700466 def test_failed_change_in_middle(self):
467 "Test a failed change in the middle of the queue"
468
Paul Belanger174a8272017-03-14 13:20:10 -0400469 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700470 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
471 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
472 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200473 A.addApproval('Code-Review', 2)
474 B.addApproval('Code-Review', 2)
475 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700476
Paul Belanger174a8272017-03-14 13:20:10 -0400477 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700478
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200479 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
480 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
481 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700482
483 self.waitUntilSettled()
484
Paul Belanger174a8272017-03-14 13:20:10 -0400485 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700486 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400487 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700488 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400489 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700490 self.waitUntilSettled()
491
492 self.assertEqual(len(self.builds), 6)
493 self.assertEqual(self.builds[0].name, 'project-test1')
494 self.assertEqual(self.builds[1].name, 'project-test2')
495 self.assertEqual(self.builds[2].name, 'project-test1')
496 self.assertEqual(self.builds[3].name, 'project-test2')
497 self.assertEqual(self.builds[4].name, 'project-test1')
498 self.assertEqual(self.builds[5].name, 'project-test2')
499
500 self.release(self.builds[2])
501 self.waitUntilSettled()
502
James E. Blair972e3c72013-08-29 12:04:55 -0700503 # project-test1 and project-test2 for A
504 # project-test2 for B
505 # project-merge for C (without B)
506 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700507 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
508
Paul Belanger174a8272017-03-14 13:20:10 -0400509 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700510 self.waitUntilSettled()
511
512 # project-test1 and project-test2 for A
513 # project-test2 for B
514 # project-test1 and project-test2 for C
515 self.assertEqual(len(self.builds), 5)
516
Paul Belanger2e2a0492016-10-30 18:33:37 -0400517 tenant = self.sched.abide.tenants.get('tenant-one')
518 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700519 builds = items[0].current_build_set.getBuilds()
520 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
521 self.assertEqual(self.countJobResults(builds, None), 2)
522 builds = items[1].current_build_set.getBuilds()
523 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
524 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
525 self.assertEqual(self.countJobResults(builds, None), 1)
526 builds = items[2].current_build_set.getBuilds()
527 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700528 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700529
Paul Belanger174a8272017-03-14 13:20:10 -0400530 self.executor_server.hold_jobs_in_build = False
531 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700532 self.waitUntilSettled()
533
534 self.assertEqual(len(self.builds), 0)
535 self.assertEqual(len(self.history), 12)
536 self.assertEqual(A.data['status'], 'MERGED')
537 self.assertEqual(B.data['status'], 'NEW')
538 self.assertEqual(C.data['status'], 'MERGED')
539 self.assertEqual(A.reported, 2)
540 self.assertEqual(B.reported, 2)
541 self.assertEqual(C.reported, 2)
542
James E. Blaird466dc42012-07-31 10:42:56 -0700543 def test_failed_change_at_head_with_queue(self):
544 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700545
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700546 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
548 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
549 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200550 A.addApproval('Code-Review', 2)
551 B.addApproval('Code-Review', 2)
552 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700553
Paul Belanger174a8272017-03-14 13:20:10 -0400554 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700555
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200556 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
557 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
558 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700559
560 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700561 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400562 self.assertEqual(len(self.builds), 0)
563 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700564 self.assertEqual(queue[0].name, b'executor:execute')
565 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800566 self.assertEqual(job_args['job'], 'project-merge')
567 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700568
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700569 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700570 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700571 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700572 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700573 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700574 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700575 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700576
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400577 self.assertEqual(len(self.builds), 0)
578 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800579
580 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700581 json.loads(queue[0].arguments.decode('utf8'))['job'],
582 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800583 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700584 json.loads(queue[1].arguments.decode('utf8'))['job'],
585 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800586 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700587 json.loads(queue[2].arguments.decode('utf8'))['job'],
588 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800589 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700590 json.loads(queue[3].arguments.decode('utf8'))['job'],
591 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800592 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700593 json.loads(queue[4].arguments.decode('utf8'))['job'],
594 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800595 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700596 json.loads(queue[5].arguments.decode('utf8'))['job'],
597 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700598
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700599 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700600 self.waitUntilSettled()
601
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400602 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700603 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400604 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
605 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700606
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700607 self.gearman_server.hold_jobs_in_queue = False
608 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700609 self.waitUntilSettled()
610
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400611 self.assertEqual(len(self.builds), 0)
612 self.assertEqual(len(self.history), 11)
613 self.assertEqual(A.data['status'], 'NEW')
614 self.assertEqual(B.data['status'], 'MERGED')
615 self.assertEqual(C.data['status'], 'MERGED')
616 self.assertEqual(A.reported, 2)
617 self.assertEqual(B.reported, 2)
618 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700619
James E. Blairec056492016-07-22 09:45:56 -0700620 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700621 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400622 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200624 A.addApproval('Code-Review', 2)
625 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700626 self.waitUntilSettled()
627 time.sleep(2)
628
629 data = json.loads(self.sched.formatStatusJSON())
630 found_job = None
631 for pipeline in data['pipelines']:
632 if pipeline['name'] != 'gate':
633 continue
634 for queue in pipeline['change_queues']:
635 for head in queue['heads']:
636 for item in head:
637 for job in item['jobs']:
638 if job['name'] == 'project-merge':
639 found_job = job
640 break
641
642 self.assertIsNotNone(found_job)
643 if iteration == 1:
644 self.assertIsNotNone(found_job['estimated_time'])
645 self.assertIsNone(found_job['remaining_time'])
646 else:
647 self.assertIsNotNone(found_job['estimated_time'])
648 self.assertTrue(found_job['estimated_time'] >= 2)
649 self.assertIsNotNone(found_job['remaining_time'])
650
Paul Belanger174a8272017-03-14 13:20:10 -0400651 self.executor_server.hold_jobs_in_build = False
652 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700653 self.waitUntilSettled()
654
James E. Blairec056492016-07-22 09:45:56 -0700655 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700656 def test_time_database(self):
657 "Test the time database"
658
659 self._test_time_database(1)
660 self._test_time_database(2)
661
James E. Blairfef71632013-09-23 11:15:47 -0700662 def test_two_failed_changes_at_head(self):
663 "Test that changes are reparented correctly if 2 fail at head"
664
Paul Belanger174a8272017-03-14 13:20:10 -0400665 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700666 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
667 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
668 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200669 A.addApproval('Code-Review', 2)
670 B.addApproval('Code-Review', 2)
671 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700672
Paul Belanger174a8272017-03-14 13:20:10 -0400673 self.executor_server.failJob('project-test1', A)
674 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700675
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200676 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
677 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
678 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700679 self.waitUntilSettled()
680
Paul Belanger174a8272017-03-14 13:20:10 -0400681 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700682 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400683 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700684 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400685 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700686 self.waitUntilSettled()
687
688 self.assertEqual(len(self.builds), 6)
689 self.assertEqual(self.builds[0].name, 'project-test1')
690 self.assertEqual(self.builds[1].name, 'project-test2')
691 self.assertEqual(self.builds[2].name, 'project-test1')
692 self.assertEqual(self.builds[3].name, 'project-test2')
693 self.assertEqual(self.builds[4].name, 'project-test1')
694 self.assertEqual(self.builds[5].name, 'project-test2')
695
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400696 self.assertTrue(self.builds[0].hasChanges(A))
697 self.assertTrue(self.builds[2].hasChanges(A))
698 self.assertTrue(self.builds[2].hasChanges(B))
699 self.assertTrue(self.builds[4].hasChanges(A))
700 self.assertTrue(self.builds[4].hasChanges(B))
701 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700702
703 # Fail change B first
704 self.release(self.builds[2])
705 self.waitUntilSettled()
706
707 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400708 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700709 self.waitUntilSettled()
710
711 self.assertEqual(len(self.builds), 5)
712 self.assertEqual(self.builds[0].name, 'project-test1')
713 self.assertEqual(self.builds[1].name, 'project-test2')
714 self.assertEqual(self.builds[2].name, 'project-test2')
715 self.assertEqual(self.builds[3].name, 'project-test1')
716 self.assertEqual(self.builds[4].name, 'project-test2')
717
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400718 self.assertTrue(self.builds[1].hasChanges(A))
719 self.assertTrue(self.builds[2].hasChanges(A))
720 self.assertTrue(self.builds[2].hasChanges(B))
721 self.assertTrue(self.builds[4].hasChanges(A))
722 self.assertFalse(self.builds[4].hasChanges(B))
723 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700724
725 # Finish running all passing jobs for change A
726 self.release(self.builds[1])
727 self.waitUntilSettled()
728 # Fail and report change A
729 self.release(self.builds[0])
730 self.waitUntilSettled()
731
732 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400733 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700734 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400735 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700736 self.waitUntilSettled()
737
738 self.assertEqual(len(self.builds), 4)
739 self.assertEqual(self.builds[0].name, 'project-test1') # B
740 self.assertEqual(self.builds[1].name, 'project-test2') # B
741 self.assertEqual(self.builds[2].name, 'project-test1') # C
742 self.assertEqual(self.builds[3].name, 'project-test2') # C
743
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400744 self.assertFalse(self.builds[1].hasChanges(A))
745 self.assertTrue(self.builds[1].hasChanges(B))
746 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700747
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400748 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700749 # After A failed and B and C restarted, B should be back in
750 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400751 self.assertTrue(self.builds[2].hasChanges(B))
752 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700753
Paul Belanger174a8272017-03-14 13:20:10 -0400754 self.executor_server.hold_jobs_in_build = False
755 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700756 self.waitUntilSettled()
757
758 self.assertEqual(len(self.builds), 0)
759 self.assertEqual(len(self.history), 21)
760 self.assertEqual(A.data['status'], 'NEW')
761 self.assertEqual(B.data['status'], 'NEW')
762 self.assertEqual(C.data['status'], 'MERGED')
763 self.assertEqual(A.reported, 2)
764 self.assertEqual(B.reported, 2)
765 self.assertEqual(C.reported, 2)
766
James E. Blair8c803f82012-07-31 16:25:42 -0700767 def test_patch_order(self):
768 "Test that dependent patches are tested in the right order"
769 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
770 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
771 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200772 A.addApproval('Code-Review', 2)
773 B.addApproval('Code-Review', 2)
774 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700775
776 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
777 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
778 M2.setMerged()
779 M1.setMerged()
780
781 # C -> B -> A -> M1 -> M2
782 # M2 is here to make sure it is never queried. If it is, it
783 # means zuul is walking down the entire history of merged
784 # changes.
785
786 C.setDependsOn(B, 1)
787 B.setDependsOn(A, 1)
788 A.setDependsOn(M1, 1)
789 M1.setDependsOn(M2, 1)
790
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200791 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700792
793 self.waitUntilSettled()
794
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400795 self.assertEqual(A.data['status'], 'NEW')
796 self.assertEqual(B.data['status'], 'NEW')
797 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700798
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200799 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
800 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700801
802 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400803 self.assertEqual(M2.queried, 0)
804 self.assertEqual(A.data['status'], 'MERGED')
805 self.assertEqual(B.data['status'], 'MERGED')
806 self.assertEqual(C.data['status'], 'MERGED')
807 self.assertEqual(A.reported, 2)
808 self.assertEqual(B.reported, 2)
809 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700810
James E. Blair063672f2015-01-29 13:09:12 -0800811 def test_needed_changes_enqueue(self):
812 "Test that a needed change is enqueued ahead"
813 # A Given a git tree like this, if we enqueue
814 # / \ change C, we should walk up and down the tree
815 # B G and enqueue changes in the order ABCDEFG.
816 # /|\ This is also the order that you would get if
817 # *C E F you enqueued changes in the order ABCDEFG, so
818 # / the ordering is stable across re-enqueue events.
819 # D
820
821 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
822 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
823 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
824 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
825 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
826 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
827 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
828 B.setDependsOn(A, 1)
829 C.setDependsOn(B, 1)
830 D.setDependsOn(C, 1)
831 E.setDependsOn(B, 1)
832 F.setDependsOn(B, 1)
833 G.setDependsOn(A, 1)
834
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200835 A.addApproval('Code-Review', 2)
836 B.addApproval('Code-Review', 2)
837 C.addApproval('Code-Review', 2)
838 D.addApproval('Code-Review', 2)
839 E.addApproval('Code-Review', 2)
840 F.addApproval('Code-Review', 2)
841 G.addApproval('Code-Review', 2)
842 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800843
844 self.waitUntilSettled()
845
846 self.assertEqual(A.data['status'], 'NEW')
847 self.assertEqual(B.data['status'], 'NEW')
848 self.assertEqual(C.data['status'], 'NEW')
849 self.assertEqual(D.data['status'], 'NEW')
850 self.assertEqual(E.data['status'], 'NEW')
851 self.assertEqual(F.data['status'], 'NEW')
852 self.assertEqual(G.data['status'], 'NEW')
853
854 # We're about to add approvals to changes without adding the
855 # triggering events to Zuul, so that we can be sure that it is
856 # enqueing the changes based on dependencies, not because of
857 # triggering events. Since it will have the changes cached
858 # already (without approvals), we need to clear the cache
859 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400860 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100861 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800862
Paul Belanger174a8272017-03-14 13:20:10 -0400863 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200864 A.addApproval('Approved', 1)
865 B.addApproval('Approved', 1)
866 D.addApproval('Approved', 1)
867 E.addApproval('Approved', 1)
868 F.addApproval('Approved', 1)
869 G.addApproval('Approved', 1)
870 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800871
872 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400873 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800874 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400875 self.executor_server.hold_jobs_in_build = False
876 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800877 self.waitUntilSettled()
878
879 self.assertEqual(A.data['status'], 'MERGED')
880 self.assertEqual(B.data['status'], 'MERGED')
881 self.assertEqual(C.data['status'], 'MERGED')
882 self.assertEqual(D.data['status'], 'MERGED')
883 self.assertEqual(E.data['status'], 'MERGED')
884 self.assertEqual(F.data['status'], 'MERGED')
885 self.assertEqual(G.data['status'], 'MERGED')
886 self.assertEqual(A.reported, 2)
887 self.assertEqual(B.reported, 2)
888 self.assertEqual(C.reported, 2)
889 self.assertEqual(D.reported, 2)
890 self.assertEqual(E.reported, 2)
891 self.assertEqual(F.reported, 2)
892 self.assertEqual(G.reported, 2)
893 self.assertEqual(self.history[6].changes,
894 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
895
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100896 def test_source_cache(self):
897 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400898 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700899
900 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
901 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
902 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200903 A.addApproval('Code-Review', 2)
904 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700905
906 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
907 M1.setMerged()
908
909 B.setDependsOn(A, 1)
910 A.setDependsOn(M1, 1)
911
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200912 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700913 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
914
915 self.waitUntilSettled()
916
917 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700918 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700919 build.release()
920 self.waitUntilSettled()
921 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700922 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700923 build.release()
924 self.waitUntilSettled()
925
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200926 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700927 self.waitUntilSettled()
928
Joshua Hesketh352264b2015-08-11 23:42:08 +1000929 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700930 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000931 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700932
Paul Belanger174a8272017-03-14 13:20:10 -0400933 self.executor_server.hold_jobs_in_build = False
934 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700935 self.waitUntilSettled()
936
937 self.assertEqual(A.data['status'], 'MERGED')
938 self.assertEqual(B.data['status'], 'MERGED')
939 self.assertEqual(A.queried, 2) # Initial and isMerged
940 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
941
James E. Blair8c803f82012-07-31 16:25:42 -0700942 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700943 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700944 # TODO: move to test_gerrit (this is a unit test!)
945 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400946 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700947 (trusted, project) = tenant.getProject('org/project')
948 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400949
950 # TODO(pabelanger): As we add more source / trigger APIs we should make
951 # it easier for users to create events for testing.
952 event = zuul.model.TriggerEvent()
953 event.trigger_name = 'gerrit'
954 event.change_number = '1'
955 event.patch_number = '2'
956
957 a = source.getChange(event)
958 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700959 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700960
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200961 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400962 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700963 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700964
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200965 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400966 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700967 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700968
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700969 def test_project_merge_conflict(self):
970 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700971
972 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700973 A = self.fake_gerrit.addFakeChange('org/project',
974 'master', 'A',
975 files={'conflict': 'foo'})
976 B = self.fake_gerrit.addFakeChange('org/project',
977 'master', 'B',
978 files={'conflict': 'bar'})
979 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700980 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200981 A.addApproval('Code-Review', 2)
982 B.addApproval('Code-Review', 2)
983 C.addApproval('Code-Review', 2)
984 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
985 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
986 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700987 self.waitUntilSettled()
988
James E. Blair6736beb2013-07-11 15:18:15 -0700989 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700990 self.assertEqual(C.reported, 1)
991
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700992 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700993 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700994 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700995 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700996 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700997 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700998
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700999 self.gearman_server.hold_jobs_in_queue = False
1000 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001001 self.waitUntilSettled()
1002
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001003 self.assertEqual(A.data['status'], 'MERGED')
1004 self.assertEqual(B.data['status'], 'NEW')
1005 self.assertEqual(C.data['status'], 'MERGED')
1006 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001007 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001008 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001009
1010 self.assertHistory([
1011 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1012 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1013 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1014 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1015 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1016 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1017 ], ordered=False)
1018
1019 def test_delayed_merge_conflict(self):
1020 "Test that delayed check merge conflicts are handled properly"
1021
1022 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001023 # the executor sucesfully merges a change based on an old
1024 # repo state (frozen by the scheduler) which would otherwise
1025 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001026 self.gearman_server.hold_jobs_in_queue = True
1027 A = self.fake_gerrit.addFakeChange('org/project',
1028 'master', 'A',
1029 files={'conflict': 'foo'})
1030 B = self.fake_gerrit.addFakeChange('org/project',
1031 'master', 'B',
1032 files={'conflict': 'bar'})
1033 C = self.fake_gerrit.addFakeChange('org/project',
1034 'master', 'C')
1035 C.setDependsOn(B, 1)
1036
1037 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001038 A.addApproval('Code-Review', 2)
1039 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001040 self.waitUntilSettled()
1041 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001042 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001043 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1044 self.waitUntilSettled()
1045
1046 self.assertEqual(A.reported, 1)
1047 self.assertEqual(B.reported, 0) # Check does not report start
1048 self.assertEqual(C.reported, 0) # Check does not report start
1049
1050 # A merges while B and C are queued in check
1051 # Release A project-merge
1052 queue = self.gearman_server.getQueue()
1053 self.release(queue[0])
1054 self.waitUntilSettled()
1055
1056 # Release A project-test*
1057 # gate has higher precedence, so A's test jobs are added in
1058 # front of the merge jobs for B and C
1059 queue = self.gearman_server.getQueue()
1060 self.release(queue[0])
1061 self.release(queue[1])
1062 self.waitUntilSettled()
1063
1064 self.assertEqual(A.data['status'], 'MERGED')
1065 self.assertEqual(B.data['status'], 'NEW')
1066 self.assertEqual(C.data['status'], 'NEW')
1067 self.assertEqual(A.reported, 2)
1068 self.assertEqual(B.reported, 0)
1069 self.assertEqual(C.reported, 0)
1070 self.assertHistory([
1071 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1072 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1073 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1074 ], ordered=False)
1075
1076 # B and C report merge conflicts
1077 # Release B project-merge
1078 queue = self.gearman_server.getQueue()
1079 self.release(queue[0])
1080 self.waitUntilSettled()
1081
1082 # Release C
1083 self.gearman_server.hold_jobs_in_queue = False
1084 self.gearman_server.release()
1085 self.waitUntilSettled()
1086
1087 self.assertEqual(A.data['status'], 'MERGED')
1088 self.assertEqual(B.data['status'], 'NEW')
1089 self.assertEqual(C.data['status'], 'NEW')
1090 self.assertEqual(A.reported, 2)
1091 self.assertEqual(B.reported, 1)
1092 self.assertEqual(C.reported, 1)
1093
1094 self.assertHistory([
1095 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1096 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1097 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001098 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1099 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1100 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1101 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1102 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1103 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001104 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001105
James E. Blairdaabed22012-08-15 15:38:57 -07001106 def test_post(self):
1107 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001108 p = "review.example.com/org/project"
1109 upstream = self.getUpstreamRepos([p])
1110 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1111 A.setMerged()
1112 A_commit = str(upstream[p].commit('master'))
1113 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001114
Zhongyue Luo5d556072012-09-21 02:00:47 +09001115 e = {
1116 "type": "ref-updated",
1117 "submitter": {
1118 "name": "User Name",
1119 },
1120 "refUpdate": {
1121 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001122 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001123 "refName": "master",
1124 "project": "org/project",
1125 }
1126 }
James E. Blairdaabed22012-08-15 15:38:57 -07001127 self.fake_gerrit.addEvent(e)
1128 self.waitUntilSettled()
1129
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001130 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001131 self.assertEqual(len(self.history), 1)
1132 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001133
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001134 def test_post_ignore_deletes(self):
1135 "Test that deleting refs does not trigger post jobs"
1136
1137 e = {
1138 "type": "ref-updated",
1139 "submitter": {
1140 "name": "User Name",
1141 },
1142 "refUpdate": {
1143 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1144 "newRev": "0000000000000000000000000000000000000000",
1145 "refName": "master",
1146 "project": "org/project",
1147 }
1148 }
1149 self.fake_gerrit.addEvent(e)
1150 self.waitUntilSettled()
1151
1152 job_names = [x.name for x in self.history]
1153 self.assertEqual(len(self.history), 0)
1154 self.assertNotIn('project-post', job_names)
1155
James E. Blaircc324192017-04-19 14:36:08 -07001156 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001157 def test_post_ignore_deletes_negative(self):
1158 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001159 e = {
1160 "type": "ref-updated",
1161 "submitter": {
1162 "name": "User Name",
1163 },
1164 "refUpdate": {
1165 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1166 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001167 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001168 "project": "org/project",
1169 }
1170 }
1171 self.fake_gerrit.addEvent(e)
1172 self.waitUntilSettled()
1173
1174 job_names = [x.name for x in self.history]
1175 self.assertEqual(len(self.history), 1)
1176 self.assertIn('project-post', job_names)
1177
James E. Blairec056492016-07-22 09:45:56 -07001178 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001179 def test_build_configuration_branch_interaction(self):
1180 "Test that switching between branches works"
1181 self.test_build_configuration()
1182 self.test_build_configuration_branch()
1183 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001184 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001185 repo = git.Repo(path)
1186 repo.heads.master.commit = repo.commit('init')
1187 self.test_build_configuration()
1188
James E. Blairdb93b302017-07-19 15:33:11 -07001189 def test_dependent_changes_rebase(self):
1190 # Test that no errors occur when we walk a dependency tree
1191 # with an unused leaf node due to a rebase.
1192 # Start by constructing: C -> B -> A
1193 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1194 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1195 B.setDependsOn(A, 1)
1196
1197 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1198 C.setDependsOn(B, 1)
1199
1200 # Then rebase to form: D -> C -> A
1201 C.addPatchset() # C,2
1202 C.setDependsOn(A, 1)
1203
1204 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1205 D.setDependsOn(C, 2)
1206
1207 # Walk the entire tree
1208 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1209 self.waitUntilSettled()
1210 self.assertEqual(len(self.history), 3)
1211
1212 # Verify that walking just part of the tree still works
1213 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1214 self.waitUntilSettled()
1215 self.assertEqual(len(self.history), 6)
1216
James E. Blaircaec0c52012-08-22 14:52:22 -07001217 def test_dependent_changes_dequeue(self):
1218 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001219
James E. Blaircaec0c52012-08-22 14:52:22 -07001220 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1221 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1222 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001223 A.addApproval('Code-Review', 2)
1224 B.addApproval('Code-Review', 2)
1225 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001226
1227 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1228 M1.setMerged()
1229
1230 # C -> B -> A -> M1
1231
1232 C.setDependsOn(B, 1)
1233 B.setDependsOn(A, 1)
1234 A.setDependsOn(M1, 1)
1235
Paul Belanger174a8272017-03-14 13:20:10 -04001236 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001237
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001238 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1239 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1240 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001241
1242 self.waitUntilSettled()
1243
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001244 self.assertEqual(A.data['status'], 'NEW')
1245 self.assertEqual(A.reported, 2)
1246 self.assertEqual(B.data['status'], 'NEW')
1247 self.assertEqual(B.reported, 2)
1248 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001249 self.assertIn('This change depends on a change that failed to merge.',
1250 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001251 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001252
James E. Blair972e3c72013-08-29 12:04:55 -07001253 def test_failing_dependent_changes(self):
1254 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001255 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001256 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1257 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1258 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1259 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1260 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001261 A.addApproval('Code-Review', 2)
1262 B.addApproval('Code-Review', 2)
1263 C.addApproval('Code-Review', 2)
1264 D.addApproval('Code-Review', 2)
1265 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001266
1267 # E, D -> C -> B, A
1268
1269 D.setDependsOn(C, 1)
1270 C.setDependsOn(B, 1)
1271
Paul Belanger174a8272017-03-14 13:20:10 -04001272 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001273
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001274 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1275 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1276 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1277 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1278 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001279
1280 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001281 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001282 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001283 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001285 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001287 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001288 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001289 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001290 self.waitUntilSettled()
1291
Paul Belanger174a8272017-03-14 13:20:10 -04001292 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001293 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001294 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001295 build.release()
1296 self.waitUntilSettled()
1297
Paul Belanger174a8272017-03-14 13:20:10 -04001298 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.waitUntilSettled()
1300
1301 self.assertEqual(A.data['status'], 'MERGED')
1302 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001303 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001304 self.assertEqual(B.data['status'], 'NEW')
1305 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001306 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001307 self.assertEqual(C.data['status'], 'NEW')
1308 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001309 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001310 self.assertEqual(D.data['status'], 'NEW')
1311 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001312 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001313 self.assertEqual(E.data['status'], 'MERGED')
1314 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001315 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001316 self.assertEqual(len(self.history), 18)
1317
James E. Blairec590122012-08-22 15:19:31 -07001318 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001319 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001320 # If it's dequeued more than once, we should see extra
1321 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001322
Paul Belanger174a8272017-03-14 13:20:10 -04001323 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001324 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1325 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1326 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001327 A.addApproval('Code-Review', 2)
1328 B.addApproval('Code-Review', 2)
1329 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001330
Paul Belanger174a8272017-03-14 13:20:10 -04001331 self.executor_server.failJob('project-test1', A)
1332 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001333
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001334 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1335 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1336 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001337
1338 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001339
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001340 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001341 self.assertEqual(self.builds[0].name, 'project-merge')
1342 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001343
Paul Belanger174a8272017-03-14 13:20:10 -04001344 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001346 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001347 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001348 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001349 self.waitUntilSettled()
1350
James E. Blair6df67132017-04-19 13:53:20 -07001351 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001352 self.assertEqual(self.builds[0].name, 'project-test1')
1353 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001354 self.assertEqual(self.builds[2].name, 'project-test1')
1355 self.assertEqual(self.builds[3].name, 'project-test2')
1356 self.assertEqual(self.builds[4].name, 'project-test1')
1357 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001358
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001359 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001360 self.waitUntilSettled()
1361
James E. Blair6df67132017-04-19 13:53:20 -07001362 self.assertEqual(len(self.builds), 2) # test2, merge for B
1363 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001364
Paul Belanger174a8272017-03-14 13:20:10 -04001365 self.executor_server.hold_jobs_in_build = False
1366 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001367 self.waitUntilSettled()
1368
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001369 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001370 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001371
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001372 self.assertEqual(A.data['status'], 'NEW')
1373 self.assertEqual(B.data['status'], 'MERGED')
1374 self.assertEqual(C.data['status'], 'MERGED')
1375 self.assertEqual(A.reported, 2)
1376 self.assertEqual(B.reported, 2)
1377 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001378
James E. Blaircc020532017-04-19 13:04:44 -07001379 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001380 def test_nonvoting_job(self):
1381 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001382
James E. Blair4ec821f2012-08-23 15:28:28 -07001383 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1384 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001385 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001386 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001387 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001388
1389 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001390
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001391 self.assertEqual(A.data['status'], 'MERGED')
1392 self.assertEqual(A.reported, 2)
1393 self.assertEqual(
1394 self.getJobFromHistory('nonvoting-project-merge').result,
1395 'SUCCESS')
1396 self.assertEqual(
1397 self.getJobFromHistory('nonvoting-project-test1').result,
1398 'SUCCESS')
1399 self.assertEqual(
1400 self.getJobFromHistory('nonvoting-project-test2').result,
1401 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001402
James E. Blair2ef29e92017-07-21 15:25:05 -07001403 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1404 parameters['zuul']['voting'])
1405 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1406 parameters['zuul']['voting'])
1407 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1408 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001409
James E. Blaire0487072012-08-29 17:38:31 -07001410 def test_check_queue_success(self):
1411 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001412
James E. Blaire0487072012-08-29 17:38:31 -07001413 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1414 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1415
1416 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001417
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001418 self.assertEqual(A.data['status'], 'NEW')
1419 self.assertEqual(A.reported, 1)
1420 self.assertEqual(self.getJobFromHistory('project-merge').result,
1421 'SUCCESS')
1422 self.assertEqual(self.getJobFromHistory('project-test1').result,
1423 'SUCCESS')
1424 self.assertEqual(self.getJobFromHistory('project-test2').result,
1425 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001426
1427 def test_check_queue_failure(self):
1428 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001429
James E. Blaire0487072012-08-29 17:38:31 -07001430 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001431 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001432 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1433
1434 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001435
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001436 self.assertEqual(A.data['status'], 'NEW')
1437 self.assertEqual(A.reported, 1)
1438 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001439 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001440 self.assertEqual(self.getJobFromHistory('project-test1').result,
1441 'SUCCESS')
1442 self.assertEqual(self.getJobFromHistory('project-test2').result,
1443 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001444
David Shrewsburyffab07a2017-07-24 12:45:07 -04001445 @simple_layout('layouts/autohold.yaml')
1446 def test_autohold(self):
1447 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1448
1449 client = zuul.rpcclient.RPCClient('127.0.0.1',
1450 self.gearman_server.port)
1451 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001452 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1453 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001454 self.assertTrue(r)
1455
1456 self.executor_server.failJob('project-test2', A)
1457 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1458
1459 self.waitUntilSettled()
1460
1461 self.assertEqual(A.data['status'], 'NEW')
1462 self.assertEqual(A.reported, 1)
1463 self.assertEqual(self.getJobFromHistory('project-test2').result,
1464 'FAILURE')
1465
1466 # Check nodepool for a held node
1467 held_node = None
1468 for node in self.fake_nodepool.getNodes():
1469 if node['state'] == zuul.model.STATE_HOLD:
1470 held_node = node
1471 break
1472 self.assertIsNotNone(held_node)
1473
1474 # Validate node has recorded the failed job
1475 self.assertEqual(
1476 held_node['hold_job'],
1477 " ".join(['tenant-one',
1478 'review.example.com/org/project',
1479 'project-test2'])
1480 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001481 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001482
1483 # Another failed change should not hold any more nodes
1484 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1485 self.executor_server.failJob('project-test2', B)
1486 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1487 self.waitUntilSettled()
1488 self.assertEqual(B.data['status'], 'NEW')
1489 self.assertEqual(B.reported, 1)
1490 self.assertEqual(self.getJobFromHistory('project-test2').result,
1491 'FAILURE')
1492
1493 held_nodes = 0
1494 for node in self.fake_nodepool.getNodes():
1495 if node['state'] == zuul.model.STATE_HOLD:
1496 held_nodes += 1
1497 self.assertEqual(held_nodes, 1)
1498
James E. Blair7cb84542017-04-19 13:35:05 -07001499 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001500 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001501 # This particular test does a large amount of merges and needs a little
1502 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001503 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001504 "test that dependent changes behind dequeued changes work"
1505 # This complicated test is a reproduction of a real life bug
1506 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001507
Paul Belanger174a8272017-03-14 13:20:10 -04001508 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001509 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1510 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1511 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1512 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1513 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1514 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1515 D.setDependsOn(C, 1)
1516 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001517 A.addApproval('Code-Review', 2)
1518 B.addApproval('Code-Review', 2)
1519 C.addApproval('Code-Review', 2)
1520 D.addApproval('Code-Review', 2)
1521 E.addApproval('Code-Review', 2)
1522 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001523
1524 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001525
1526 # Change object re-use in the gerrit trigger is hidden if
1527 # changes are added in quick succession; waiting makes it more
1528 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001529 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001530 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001531 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001532 self.waitUntilSettled()
1533
Paul Belanger174a8272017-03-14 13:20:10 -04001534 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001535 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001536 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001537 self.waitUntilSettled()
1538
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001539 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001540 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001541 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001542 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001543 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001544 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001545 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001546 self.waitUntilSettled()
1547
Paul Belanger174a8272017-03-14 13:20:10 -04001548 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001550 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001551 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001552 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001553 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001554 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001555 self.waitUntilSettled()
1556
1557 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001558
1559 # Grab pointers to the jobs we want to release before
1560 # releasing any, because list indexes may change as
1561 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001562 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001563 a.release()
1564 b.release()
1565 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001566 self.waitUntilSettled()
1567
Paul Belanger174a8272017-03-14 13:20:10 -04001568 self.executor_server.hold_jobs_in_build = False
1569 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001570 self.waitUntilSettled()
1571
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001572 self.assertEqual(A.data['status'], 'NEW')
1573 self.assertEqual(B.data['status'], 'MERGED')
1574 self.assertEqual(C.data['status'], 'MERGED')
1575 self.assertEqual(D.data['status'], 'MERGED')
1576 self.assertEqual(E.data['status'], 'MERGED')
1577 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001578
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001579 self.assertEqual(A.reported, 2)
1580 self.assertEqual(B.reported, 2)
1581 self.assertEqual(C.reported, 2)
1582 self.assertEqual(D.reported, 2)
1583 self.assertEqual(E.reported, 2)
1584 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001585
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001586 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1587 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001588
1589 def test_merger_repack(self):
1590 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001591
James E. Blair05fed602012-09-07 12:45:24 -07001592 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001593 A.addApproval('Code-Review', 2)
1594 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001595 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001596 self.assertEqual(self.getJobFromHistory('project-merge').result,
1597 'SUCCESS')
1598 self.assertEqual(self.getJobFromHistory('project-test1').result,
1599 'SUCCESS')
1600 self.assertEqual(self.getJobFromHistory('project-test2').result,
1601 'SUCCESS')
1602 self.assertEqual(A.data['status'], 'MERGED')
1603 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001604 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001605 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001606
James E. Blair2a535672017-04-27 12:03:15 -07001607 path = os.path.join(self.merger_src_root, "review.example.com",
1608 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001609 if os.path.exists(path):
1610 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001611 path = os.path.join(self.executor_src_root, "review.example.com",
1612 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001613 if os.path.exists(path):
1614 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001615
1616 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001617 A.addApproval('Code-Review', 2)
1618 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001619 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001620 self.assertEqual(self.getJobFromHistory('project-merge').result,
1621 'SUCCESS')
1622 self.assertEqual(self.getJobFromHistory('project-test1').result,
1623 'SUCCESS')
1624 self.assertEqual(self.getJobFromHistory('project-test2').result,
1625 'SUCCESS')
1626 self.assertEqual(A.data['status'], 'MERGED')
1627 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001628
James E. Blair4886f282012-11-15 09:27:33 -08001629 def test_merger_repack_large_change(self):
1630 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001631 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001632 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001633 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001634 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001635 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001636 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001637 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001638 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001639 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001640 # TODOv3(jeblair): add hostname to upstream root
1641 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001642 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001643 path = os.path.join(self.merger_src_root, 'review.example.com',
1644 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001645 if os.path.exists(path):
1646 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001647 path = os.path.join(self.executor_src_root, 'review.example.com',
1648 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001649 if os.path.exists(path):
1650 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001651
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001652 A.addApproval('Code-Review', 2)
1653 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001654 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001655 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001656 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001657 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001658 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001659 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001660 'SUCCESS')
1661 self.assertEqual(A.data['status'], 'MERGED')
1662 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001663
James E. Blair2fa50962013-01-30 21:50:41 -08001664 def test_new_patchset_dequeues_old(self):
1665 "Test that a new patchset causes the old to be dequeued"
1666 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001667 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001668 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1669 M.setMerged()
1670
1671 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1672 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1673 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1674 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001675 A.addApproval('Code-Review', 2)
1676 B.addApproval('Code-Review', 2)
1677 C.addApproval('Code-Review', 2)
1678 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001679
1680 C.setDependsOn(B, 1)
1681 B.setDependsOn(A, 1)
1682 A.setDependsOn(M, 1)
1683
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001684 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1685 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1686 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1687 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001688 self.waitUntilSettled()
1689
1690 B.addPatchset()
1691 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1692 self.waitUntilSettled()
1693
Paul Belanger174a8272017-03-14 13:20:10 -04001694 self.executor_server.hold_jobs_in_build = False
1695 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001696 self.waitUntilSettled()
1697
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001698 self.assertEqual(A.data['status'], 'MERGED')
1699 self.assertEqual(A.reported, 2)
1700 self.assertEqual(B.data['status'], 'NEW')
1701 self.assertEqual(B.reported, 2)
1702 self.assertEqual(C.data['status'], 'NEW')
1703 self.assertEqual(C.reported, 2)
1704 self.assertEqual(D.data['status'], 'MERGED')
1705 self.assertEqual(D.reported, 2)
1706 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001707
James E. Blairba437362015-02-07 11:41:52 -08001708 def test_new_patchset_check(self):
1709 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001710
Paul Belanger174a8272017-03-14 13:20:10 -04001711 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001712
1713 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001714 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001715 tenant = self.sched.abide.tenants.get('tenant-one')
1716 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001717
1718 # Add two git-dependent changes
1719 B.setDependsOn(A, 1)
1720 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1721 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001722 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1723 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001724
1725 # A live item, and a non-live/live pair
1726 items = check_pipeline.getAllItems()
1727 self.assertEqual(len(items), 3)
1728
1729 self.assertEqual(items[0].change.number, '1')
1730 self.assertEqual(items[0].change.patchset, '1')
1731 self.assertFalse(items[0].live)
1732
1733 self.assertEqual(items[1].change.number, '2')
1734 self.assertEqual(items[1].change.patchset, '1')
1735 self.assertTrue(items[1].live)
1736
1737 self.assertEqual(items[2].change.number, '1')
1738 self.assertEqual(items[2].change.patchset, '1')
1739 self.assertTrue(items[2].live)
1740
1741 # Add a new patchset to A
1742 A.addPatchset()
1743 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1744 self.waitUntilSettled()
1745
1746 # The live copy of A,1 should be gone, but the non-live and B
1747 # should continue, and we should have a new A,2
1748 items = check_pipeline.getAllItems()
1749 self.assertEqual(len(items), 3)
1750
1751 self.assertEqual(items[0].change.number, '1')
1752 self.assertEqual(items[0].change.patchset, '1')
1753 self.assertFalse(items[0].live)
1754
1755 self.assertEqual(items[1].change.number, '2')
1756 self.assertEqual(items[1].change.patchset, '1')
1757 self.assertTrue(items[1].live)
1758
1759 self.assertEqual(items[2].change.number, '1')
1760 self.assertEqual(items[2].change.patchset, '2')
1761 self.assertTrue(items[2].live)
1762
1763 # Add a new patchset to B
1764 B.addPatchset()
1765 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1766 self.waitUntilSettled()
1767
1768 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1769 # but we should have a new B,2 (still based on A,1)
1770 items = check_pipeline.getAllItems()
1771 self.assertEqual(len(items), 3)
1772
1773 self.assertEqual(items[0].change.number, '1')
1774 self.assertEqual(items[0].change.patchset, '2')
1775 self.assertTrue(items[0].live)
1776
1777 self.assertEqual(items[1].change.number, '1')
1778 self.assertEqual(items[1].change.patchset, '1')
1779 self.assertFalse(items[1].live)
1780
1781 self.assertEqual(items[2].change.number, '2')
1782 self.assertEqual(items[2].change.patchset, '2')
1783 self.assertTrue(items[2].live)
1784
1785 self.builds[0].release()
1786 self.waitUntilSettled()
1787 self.builds[0].release()
1788 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001789 self.executor_server.hold_jobs_in_build = False
1790 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001791 self.waitUntilSettled()
1792
1793 self.assertEqual(A.reported, 1)
1794 self.assertEqual(B.reported, 1)
1795 self.assertEqual(self.history[0].result, 'ABORTED')
1796 self.assertEqual(self.history[0].changes, '1,1')
1797 self.assertEqual(self.history[1].result, 'ABORTED')
1798 self.assertEqual(self.history[1].changes, '1,1 2,1')
1799 self.assertEqual(self.history[2].result, 'SUCCESS')
1800 self.assertEqual(self.history[2].changes, '1,2')
1801 self.assertEqual(self.history[3].result, 'SUCCESS')
1802 self.assertEqual(self.history[3].changes, '1,1 2,2')
1803
1804 def test_abandoned_gate(self):
1805 "Test that an abandoned change is dequeued from gate"
1806
Paul Belanger174a8272017-03-14 13:20:10 -04001807 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001808
1809 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001810 A.addApproval('Code-Review', 2)
1811 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001812 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001813 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1814 self.assertEqual(self.builds[0].name, 'project-merge')
1815
1816 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1817 self.waitUntilSettled()
1818
Paul Belanger174a8272017-03-14 13:20:10 -04001819 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001820 self.waitUntilSettled()
1821
Arie5b4048c2016-10-05 16:12:06 +03001822 self.assertBuilds([])
1823 self.assertHistory([
1824 dict(name='project-merge', result='ABORTED', changes='1,1')],
1825 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001826 self.assertEqual(A.reported, 1,
1827 "Abandoned gate change should report only start")
1828
1829 def test_abandoned_check(self):
1830 "Test that an abandoned change is dequeued from check"
1831
Paul Belanger174a8272017-03-14 13:20:10 -04001832 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001833
1834 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1835 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001836 tenant = self.sched.abide.tenants.get('tenant-one')
1837 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001838
1839 # Add two git-dependent changes
1840 B.setDependsOn(A, 1)
1841 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1842 self.waitUntilSettled()
1843 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1844 self.waitUntilSettled()
1845 # A live item, and a non-live/live pair
1846 items = check_pipeline.getAllItems()
1847 self.assertEqual(len(items), 3)
1848
1849 self.assertEqual(items[0].change.number, '1')
1850 self.assertFalse(items[0].live)
1851
1852 self.assertEqual(items[1].change.number, '2')
1853 self.assertTrue(items[1].live)
1854
1855 self.assertEqual(items[2].change.number, '1')
1856 self.assertTrue(items[2].live)
1857
1858 # Abandon A
1859 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1860 self.waitUntilSettled()
1861
1862 # The live copy of A should be gone, but the non-live and B
1863 # should continue
1864 items = check_pipeline.getAllItems()
1865 self.assertEqual(len(items), 2)
1866
1867 self.assertEqual(items[0].change.number, '1')
1868 self.assertFalse(items[0].live)
1869
1870 self.assertEqual(items[1].change.number, '2')
1871 self.assertTrue(items[1].live)
1872
Paul Belanger174a8272017-03-14 13:20:10 -04001873 self.executor_server.hold_jobs_in_build = False
1874 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001875 self.waitUntilSettled()
1876
1877 self.assertEqual(len(self.history), 4)
1878 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001879 'Build should have been aborted')
1880 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001881 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001882
Steve Varnau7b78b312015-04-03 14:49:46 -07001883 def test_abandoned_not_timer(self):
1884 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001885 # This test can not use simple_layout because it must start
1886 # with a configuration which does not include a
1887 # timer-triggered job so that we have an opportunity to set
1888 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001889 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001890 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001891 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001892 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001893 # The pipeline triggers every second, so we should have seen
1894 # several by now.
1895 time.sleep(5)
1896 self.waitUntilSettled()
1897 # Stop queuing timer triggered jobs so that the assertions
1898 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001899 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001900 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001901 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001902 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001903 # If APScheduler is in mid-event when we remove the job, we
1904 # can end up with one more event firing, so give it an extra
1905 # second to settle.
1906 time.sleep(1)
1907 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001908
James E. Blair21037782017-07-19 11:56:55 -07001909 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001910
1911 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1912 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1913 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001914 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001915
1916 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1917 self.waitUntilSettled()
1918
James E. Blair21037782017-07-19 11:56:55 -07001919 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001920
Paul Belanger174a8272017-03-14 13:20:10 -04001921 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001922 self.waitUntilSettled()
1923
James E. Blair2fa50962013-01-30 21:50:41 -08001924 def test_new_patchset_dequeues_old_on_head(self):
1925 "Test that a new patchset causes the old to be dequeued (at head)"
1926 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001927 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001928 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1929 M.setMerged()
1930 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1931 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1932 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1933 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001934 A.addApproval('Code-Review', 2)
1935 B.addApproval('Code-Review', 2)
1936 C.addApproval('Code-Review', 2)
1937 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001938
1939 C.setDependsOn(B, 1)
1940 B.setDependsOn(A, 1)
1941 A.setDependsOn(M, 1)
1942
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001943 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1944 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1945 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1946 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001947 self.waitUntilSettled()
1948
1949 A.addPatchset()
1950 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1951 self.waitUntilSettled()
1952
Paul Belanger174a8272017-03-14 13:20:10 -04001953 self.executor_server.hold_jobs_in_build = False
1954 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001955 self.waitUntilSettled()
1956
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001957 self.assertEqual(A.data['status'], 'NEW')
1958 self.assertEqual(A.reported, 2)
1959 self.assertEqual(B.data['status'], 'NEW')
1960 self.assertEqual(B.reported, 2)
1961 self.assertEqual(C.data['status'], 'NEW')
1962 self.assertEqual(C.reported, 2)
1963 self.assertEqual(D.data['status'], 'MERGED')
1964 self.assertEqual(D.reported, 2)
1965 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001966
1967 def test_new_patchset_dequeues_old_without_dependents(self):
1968 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001969 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001970 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1971 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1972 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001973 A.addApproval('Code-Review', 2)
1974 B.addApproval('Code-Review', 2)
1975 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001976
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001977 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1978 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1979 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001980 self.waitUntilSettled()
1981
1982 B.addPatchset()
1983 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1984 self.waitUntilSettled()
1985
Paul Belanger174a8272017-03-14 13:20:10 -04001986 self.executor_server.hold_jobs_in_build = False
1987 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001988 self.waitUntilSettled()
1989
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001990 self.assertEqual(A.data['status'], 'MERGED')
1991 self.assertEqual(A.reported, 2)
1992 self.assertEqual(B.data['status'], 'NEW')
1993 self.assertEqual(B.reported, 2)
1994 self.assertEqual(C.data['status'], 'MERGED')
1995 self.assertEqual(C.reported, 2)
1996 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001997
1998 def test_new_patchset_dequeues_old_independent_queue(self):
1999 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002000 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002001 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2002 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2003 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2004 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2005 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2006 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2007 self.waitUntilSettled()
2008
2009 B.addPatchset()
2010 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2011 self.waitUntilSettled()
2012
Paul Belanger174a8272017-03-14 13:20:10 -04002013 self.executor_server.hold_jobs_in_build = False
2014 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002015 self.waitUntilSettled()
2016
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002017 self.assertEqual(A.data['status'], 'NEW')
2018 self.assertEqual(A.reported, 1)
2019 self.assertEqual(B.data['status'], 'NEW')
2020 self.assertEqual(B.reported, 1)
2021 self.assertEqual(C.data['status'], 'NEW')
2022 self.assertEqual(C.reported, 1)
2023 self.assertEqual(len(self.history), 10)
2024 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002025
James E. Blaircc020532017-04-19 13:04:44 -07002026 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002027 def test_noop_job(self):
2028 "Test that the internal noop job works"
2029 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002030 A.addApproval('Code-Review', 2)
2031 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002032 self.waitUntilSettled()
2033
2034 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2035 self.assertTrue(self.sched._areAllBuildsComplete())
2036 self.assertEqual(len(self.history), 0)
2037 self.assertEqual(A.data['status'], 'MERGED')
2038 self.assertEqual(A.reported, 2)
2039
James E. Blair06cc3922017-04-19 10:08:10 -07002040 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002041 def test_no_job_project(self):
2042 "Test that reports with no jobs don't get sent"
2043 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2044 'master', 'A')
2045 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2046 self.waitUntilSettled()
2047
2048 # Change wasn't reported to
2049 self.assertEqual(A.reported, False)
2050
2051 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002052 tenant = self.sched.abide.tenants.get('tenant-one')
2053 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002054 items = check_pipeline.getAllItems()
2055 self.assertEqual(len(items), 0)
2056
2057 self.assertEqual(len(self.history), 0)
2058
James E. Blair7d0dedc2013-02-21 17:26:09 -08002059 def test_zuul_refs(self):
2060 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002061 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002062 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2063 M1.setMerged()
2064 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2065 M2.setMerged()
2066
2067 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2068 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2069 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2070 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002071 A.addApproval('Code-Review', 2)
2072 B.addApproval('Code-Review', 2)
2073 C.addApproval('Code-Review', 2)
2074 D.addApproval('Code-Review', 2)
2075 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2076 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2077 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2078 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002079
2080 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002081 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002082 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002083 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002084 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002085 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002086 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002087 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002088 self.waitUntilSettled()
2089
Clint Byrum3343e3e2016-11-15 16:05:03 -08002090 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002091 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002092 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002093 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002094 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002095 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002096 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002097 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002098 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002099 d_build = x
2100 if a_build and b_build and c_build and d_build:
2101 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002102
Clint Byrum3343e3e2016-11-15 16:05:03 -08002103 # should have a, not b, and should not be in project2
2104 self.assertTrue(a_build.hasChanges(A))
2105 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002106
Clint Byrum3343e3e2016-11-15 16:05:03 -08002107 # should have a and b, and should not be in project2
2108 self.assertTrue(b_build.hasChanges(A, B))
2109 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002110
Clint Byrum3343e3e2016-11-15 16:05:03 -08002111 # should have a and b in 1, c in 2
2112 self.assertTrue(c_build.hasChanges(A, B, C))
2113 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002114
Clint Byrum3343e3e2016-11-15 16:05:03 -08002115 # should have a and b in 1, c and d in 2
2116 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002117
Paul Belanger174a8272017-03-14 13:20:10 -04002118 self.executor_server.hold_jobs_in_build = False
2119 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002120 self.waitUntilSettled()
2121
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002122 self.assertEqual(A.data['status'], 'MERGED')
2123 self.assertEqual(A.reported, 2)
2124 self.assertEqual(B.data['status'], 'MERGED')
2125 self.assertEqual(B.reported, 2)
2126 self.assertEqual(C.data['status'], 'MERGED')
2127 self.assertEqual(C.reported, 2)
2128 self.assertEqual(D.data['status'], 'MERGED')
2129 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002130
James E. Blair4a28a882013-08-23 15:17:33 -07002131 def test_rerun_on_error(self):
2132 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002133 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002134 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002135 A.addApproval('Code-Review', 2)
2136 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002137 self.waitUntilSettled()
2138
James E. Blair412fba82017-01-26 15:00:50 -08002139 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002140 self.executor_server.hold_jobs_in_build = False
2141 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002142 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002143 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002144 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2145
James E. Blair412e5582013-04-22 15:50:12 -07002146 def test_statsd(self):
2147 "Test each of the statsd methods used in the scheduler"
2148 import extras
2149 statsd = extras.try_import('statsd.statsd')
2150 statsd.incr('test-incr')
2151 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002152 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002153 self.assertReportedStat('test-incr', '1|c')
2154 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002155 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002156
James E. Blairdad52252014-02-07 16:59:17 -08002157 def test_stuck_job_cleanup(self):
2158 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002159
2160 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002161 self.gearman_server.hold_jobs_in_queue = True
2162 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002163 A.addApproval('Code-Review', 2)
2164 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002165 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002166 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002167 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2168
James E. Blair31cb3e82017-04-20 13:08:56 -07002169 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002170 self.sched.reconfigure(self.config)
2171 self.waitUntilSettled()
2172
James E. Blair18c64442014-03-18 10:14:45 -07002173 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002174 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002175 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002176 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2177 self.assertTrue(self.sched._areAllBuildsComplete())
2178
2179 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002180 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002181 self.assertEqual(self.history[0].result, 'SUCCESS')
2182
James E. Blair879dafb2015-07-17 14:04:49 -07002183 def test_file_head(self):
2184 # This is a regression test for an observed bug. A change
2185 # with a file named "HEAD" in the root directory of the repo
2186 # was processed by a merger. It then was unable to reset the
2187 # repo because of:
2188 # GitCommandError: 'git reset --hard HEAD' returned
2189 # with exit code 128
2190 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2191 # and filename
2192 # Use '--' to separate filenames from revisions'
2193
2194 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002195 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002196 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2197
2198 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2199 self.waitUntilSettled()
2200
2201 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2202 self.waitUntilSettled()
2203
2204 self.assertIn('Build succeeded', A.messages[0])
2205 self.assertIn('Build succeeded', B.messages[0])
2206
James E. Blair70c71582013-03-06 08:50:50 -08002207 def test_file_jobs(self):
2208 "Test that file jobs run only when appropriate"
2209 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002210 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002211 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002212 A.addApproval('Code-Review', 2)
2213 B.addApproval('Code-Review', 2)
2214 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2215 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002216 self.waitUntilSettled()
2217
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002218 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002219 if x.name == 'project-testfile']
2220
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002221 self.assertEqual(len(testfile_jobs), 1)
2222 self.assertEqual(testfile_jobs[0].changes, '1,2')
2223 self.assertEqual(A.data['status'], 'MERGED')
2224 self.assertEqual(A.reported, 2)
2225 self.assertEqual(B.data['status'], 'MERGED')
2226 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002227
Clint Byrum3ee39f32016-11-17 23:45:07 -08002228 def _test_irrelevant_files_jobs(self, should_skip):
2229 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002230 if should_skip:
2231 files = {'ignoreme': 'ignored\n'}
2232 else:
2233 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002234
2235 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002236 'master',
2237 'test irrelevant-files',
2238 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002239 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2240 self.waitUntilSettled()
2241
2242 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002243 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002244
2245 if should_skip:
2246 self.assertEqual([], tested_change_ids)
2247 else:
2248 self.assertIn(change.data['number'], tested_change_ids)
2249
James E. Blaircc324192017-04-19 14:36:08 -07002250 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002251 def test_irrelevant_files_match_skips_job(self):
2252 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002253
James E. Blaircc324192017-04-19 14:36:08 -07002254 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002255 def test_irrelevant_files_no_match_runs_job(self):
2256 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002257
James E. Blaircc324192017-04-19 14:36:08 -07002258 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002259 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002260 files = {'ignoreme': 'ignored\n'}
2261
2262 change = self.fake_gerrit.addFakeChange('org/project',
2263 'master',
2264 'test irrelevant-files',
2265 files=files)
2266 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2267 self.waitUntilSettled()
2268
2269 run_jobs = set([build.name for build in self.history])
2270
2271 self.assertEqual(set(['project-test-nomatch-starts-empty',
2272 'project-test-nomatch-starts-full']), run_jobs)
2273
James E. Blairc8a1e052014-02-25 09:29:26 -08002274 def test_queue_names(self):
2275 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002276 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002277 (trusted, project1) = tenant.getProject('org/project1')
2278 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002279 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2280 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2281 self.assertEqual(q1.name, 'integrated')
2282 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002283
James E. Blair64ed6f22013-07-10 14:07:23 -07002284 def test_queue_precedence(self):
2285 "Test that queue precedence works"
2286
2287 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002288 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002289 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2290 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002291 A.addApproval('Code-Review', 2)
2292 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002293
2294 self.waitUntilSettled()
2295 self.gearman_server.hold_jobs_in_queue = False
2296 self.gearman_server.release()
2297 self.waitUntilSettled()
2298
James E. Blair8de58bd2013-07-18 16:23:33 -07002299 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002300 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002301 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002302 self.waitUntilSettled()
2303
James E. Blair64ed6f22013-07-10 14:07:23 -07002304 self.log.debug(self.history)
2305 self.assertEqual(self.history[0].pipeline, 'gate')
2306 self.assertEqual(self.history[1].pipeline, 'check')
2307 self.assertEqual(self.history[2].pipeline, 'gate')
2308 self.assertEqual(self.history[3].pipeline, 'gate')
2309 self.assertEqual(self.history[4].pipeline, 'check')
2310 self.assertEqual(self.history[5].pipeline, 'check')
2311
Clark Boylana5edbe42014-06-03 16:39:10 -07002312 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002313 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002314 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002316 A.addApproval('Code-Review', 2)
2317 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002318 self.waitUntilSettled()
2319
Paul Belanger174a8272017-03-14 13:20:10 -04002320 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002321 self.waitUntilSettled()
2322
James E. Blair1843a552013-07-03 14:19:52 -07002323 port = self.webapp.server.socket.getsockname()[1]
2324
Adam Gandelman77a12c72017-02-03 17:43:43 -08002325 req = urllib.request.Request(
2326 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002327 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002328 headers = f.info()
2329 self.assertIn('Content-Length', headers)
2330 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002331 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2332 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002333 self.assertIn('Access-Control-Allow-Origin', headers)
2334 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002335 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002336 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002337 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002338
Paul Belanger174a8272017-03-14 13:20:10 -04002339 self.executor_server.hold_jobs_in_build = False
2340 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002341 self.waitUntilSettled()
2342
2343 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002344 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002345 for p in data['pipelines']:
2346 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002347 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002348 self.assertEqual(q['window'], 20)
2349 else:
2350 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002351 for head in q['heads']:
2352 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002353 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002354 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002355 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002356 status_jobs.append(job)
2357 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002358 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002359 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002360 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2361 uuid=status_jobs[0]['uuid']),
2362 status_jobs[0]['url'])
2363 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002364 'finger://{hostname}/{uuid}'.format(
2365 hostname=self.executor_server.hostname,
2366 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002367 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002368 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002369 self.assertEqual(
2370 'finger://{hostname}/{uuid}'.format(
2371 hostname=self.executor_server.hostname,
2372 uuid=status_jobs[0]['uuid']),
2373 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002374 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002375 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002376 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2377 uuid=status_jobs[1]['uuid']),
2378 status_jobs[1]['url'])
2379 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002380 'finger://{hostname}/{uuid}'.format(
2381 hostname=self.executor_server.hostname,
2382 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002383 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002384 self.assertEqual(
2385 'finger://{hostname}/{uuid}'.format(
2386 hostname=self.executor_server.hostname,
2387 uuid=status_jobs[1]['uuid']),
2388 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002389
2390 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002391 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002392 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2393 uuid=status_jobs[2]['uuid']),
2394 status_jobs[2]['url'])
2395 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002396 'finger://{hostname}/{uuid}'.format(
2397 hostname=self.executor_server.hostname,
2398 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002399 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002400 self.assertEqual(
2401 'finger://{hostname}/{uuid}'.format(
2402 hostname=self.executor_server.hostname,
2403 uuid=status_jobs[2]['uuid']),
2404 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002405
Tobias Henkel65639f82017-07-10 10:25:42 +02002406 # check job dependencies
2407 self.assertIsNotNone(status_jobs[0]['dependencies'])
2408 self.assertIsNotNone(status_jobs[1]['dependencies'])
2409 self.assertIsNotNone(status_jobs[2]['dependencies'])
2410 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2411 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2412 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2413 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2414 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2415
James E. Blaircdccd972013-07-01 12:10:22 -07002416 def test_live_reconfiguration(self):
2417 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002418 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002419 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002420 A.addApproval('Code-Review', 2)
2421 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002422 self.waitUntilSettled()
2423
2424 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002425 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002426
Paul Belanger174a8272017-03-14 13:20:10 -04002427 self.executor_server.hold_jobs_in_build = False
2428 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002429 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002430 self.assertEqual(self.getJobFromHistory('project-merge').result,
2431 'SUCCESS')
2432 self.assertEqual(self.getJobFromHistory('project-test1').result,
2433 'SUCCESS')
2434 self.assertEqual(self.getJobFromHistory('project-test2').result,
2435 'SUCCESS')
2436 self.assertEqual(A.data['status'], 'MERGED')
2437 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002438
James E. Blair6bc782d2015-07-17 16:20:21 -07002439 def test_live_reconfiguration_merge_conflict(self):
2440 # A real-world bug: a change in a gate queue has a merge
2441 # conflict and a job is added to its project while it's
2442 # sitting in the queue. The job gets added to the change and
2443 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002444 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002445
2446 # This change is fine. It's here to stop the queue long
2447 # enough for the next change to be subject to the
2448 # reconfiguration, as well as to provide a conflict for the
2449 # next change. This change will succeed and merge.
2450 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002451 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002452 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002453
2454 # This change will be in merge conflict. During the
2455 # reconfiguration, we will add a job. We want to make sure
2456 # that doesn't cause it to get stuck.
2457 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002458 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002459 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002460
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002461 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2462 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002463
2464 self.waitUntilSettled()
2465
2466 # No jobs have run yet
2467 self.assertEqual(A.data['status'], 'NEW')
2468 self.assertEqual(A.reported, 1)
2469 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002470 self.assertEqual(len(self.history), 0)
2471
2472 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002473 self.commitConfigUpdate('common-config',
2474 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002475 self.sched.reconfigure(self.config)
2476 self.waitUntilSettled()
2477
Paul Belanger174a8272017-03-14 13:20:10 -04002478 self.executor_server.hold_jobs_in_build = False
2479 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002480 self.waitUntilSettled()
2481
2482 self.assertEqual(A.data['status'], 'MERGED')
2483 self.assertEqual(A.reported, 2)
2484 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002485 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002486 self.assertEqual(self.getJobFromHistory('project-merge').result,
2487 'SUCCESS')
2488 self.assertEqual(self.getJobFromHistory('project-test1').result,
2489 'SUCCESS')
2490 self.assertEqual(self.getJobFromHistory('project-test2').result,
2491 'SUCCESS')
2492 self.assertEqual(self.getJobFromHistory('project-test3').result,
2493 'SUCCESS')
2494 self.assertEqual(len(self.history), 4)
2495
James E. Blair400e8fd2015-07-30 17:44:45 -07002496 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002497 # An extrapolation of test_live_reconfiguration_merge_conflict
2498 # that tests a job added to a job tree with a failed root does
2499 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002500 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002501
2502 # This change is fine. It's here to stop the queue long
2503 # enough for the next change to be subject to the
2504 # reconfiguration. This change will succeed and merge.
2505 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002506 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002507 A.addApproval('Code-Review', 2)
2508 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002509 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002510 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002511 self.waitUntilSettled()
2512
2513 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002514 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002515 B.addApproval('Code-Review', 2)
2516 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002517 self.waitUntilSettled()
2518
Paul Belanger174a8272017-03-14 13:20:10 -04002519 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002520 self.waitUntilSettled()
2521
2522 # Both -merge jobs have run, but no others.
2523 self.assertEqual(A.data['status'], 'NEW')
2524 self.assertEqual(A.reported, 1)
2525 self.assertEqual(B.data['status'], 'NEW')
2526 self.assertEqual(B.reported, 1)
2527 self.assertEqual(self.history[0].result, 'SUCCESS')
2528 self.assertEqual(self.history[0].name, 'project-merge')
2529 self.assertEqual(self.history[1].result, 'FAILURE')
2530 self.assertEqual(self.history[1].name, 'project-merge')
2531 self.assertEqual(len(self.history), 2)
2532
2533 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002534 self.commitConfigUpdate('common-config',
2535 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002536 self.sched.reconfigure(self.config)
2537 self.waitUntilSettled()
2538
Paul Belanger174a8272017-03-14 13:20:10 -04002539 self.executor_server.hold_jobs_in_build = False
2540 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002541 self.waitUntilSettled()
2542
2543 self.assertEqual(A.data['status'], 'MERGED')
2544 self.assertEqual(A.reported, 2)
2545 self.assertEqual(B.data['status'], 'NEW')
2546 self.assertEqual(B.reported, 2)
2547 self.assertEqual(self.history[0].result, 'SUCCESS')
2548 self.assertEqual(self.history[0].name, 'project-merge')
2549 self.assertEqual(self.history[1].result, 'FAILURE')
2550 self.assertEqual(self.history[1].name, 'project-merge')
2551 self.assertEqual(self.history[2].result, 'SUCCESS')
2552 self.assertEqual(self.history[3].result, 'SUCCESS')
2553 self.assertEqual(self.history[4].result, 'SUCCESS')
2554 self.assertEqual(len(self.history), 5)
2555
James E. Blair400e8fd2015-07-30 17:44:45 -07002556 def test_live_reconfiguration_failed_job(self):
2557 # Test that a change with a removed failing job does not
2558 # disrupt reconfiguration. If a change has a failed job and
2559 # that job is removed during a reconfiguration, we observed a
2560 # bug where the code to re-set build statuses would run on
2561 # that build and raise an exception because the job no longer
2562 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002563 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002564
2565 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2566
2567 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002568 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002569
2570 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2571 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002572 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002573 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002574 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002575 self.waitUntilSettled()
2576
2577 self.assertEqual(A.data['status'], 'NEW')
2578 self.assertEqual(A.reported, 0)
2579
2580 self.assertEqual(self.getJobFromHistory('project-merge').result,
2581 'SUCCESS')
2582 self.assertEqual(self.getJobFromHistory('project-test1').result,
2583 'FAILURE')
2584 self.assertEqual(len(self.history), 2)
2585
2586 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002587 self.commitConfigUpdate('common-config',
2588 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002589 self.sched.reconfigure(self.config)
2590 self.waitUntilSettled()
2591
Paul Belanger174a8272017-03-14 13:20:10 -04002592 self.executor_server.hold_jobs_in_build = False
2593 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002594 self.waitUntilSettled()
2595
2596 self.assertEqual(self.getJobFromHistory('project-test2').result,
2597 'SUCCESS')
2598 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2599 'SUCCESS')
2600 self.assertEqual(len(self.history), 4)
2601
2602 self.assertEqual(A.data['status'], 'NEW')
2603 self.assertEqual(A.reported, 1)
2604 self.assertIn('Build succeeded', A.messages[0])
2605 # Ensure the removed job was not included in the report.
2606 self.assertNotIn('project-test1', A.messages[0])
2607
James E. Blairfe707d12015-08-05 15:18:15 -07002608 def test_live_reconfiguration_shared_queue(self):
2609 # Test that a change with a failing job which was removed from
2610 # this project but otherwise still exists in the system does
2611 # not disrupt reconfiguration.
2612
Paul Belanger174a8272017-03-14 13:20:10 -04002613 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002614
2615 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2616
Paul Belanger174a8272017-03-14 13:20:10 -04002617 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002618
2619 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2620 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002621 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002622 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002623 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002624 self.waitUntilSettled()
2625
2626 self.assertEqual(A.data['status'], 'NEW')
2627 self.assertEqual(A.reported, 0)
2628
Clint Byrum69663d42017-05-01 14:51:04 -07002629 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002630 'SUCCESS')
2631 self.assertEqual(self.getJobFromHistory(
2632 'project1-project2-integration').result, 'FAILURE')
2633 self.assertEqual(len(self.history), 2)
2634
2635 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002636 self.commitConfigUpdate(
2637 'common-config',
2638 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002639 self.sched.reconfigure(self.config)
2640 self.waitUntilSettled()
2641
Paul Belanger174a8272017-03-14 13:20:10 -04002642 self.executor_server.hold_jobs_in_build = False
2643 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002644 self.waitUntilSettled()
2645
Clint Byrum69663d42017-05-01 14:51:04 -07002646 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002647 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002648 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002649 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002650 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002651 'SUCCESS')
2652 self.assertEqual(self.getJobFromHistory(
2653 'project1-project2-integration').result, 'FAILURE')
2654 self.assertEqual(len(self.history), 4)
2655
2656 self.assertEqual(A.data['status'], 'NEW')
2657 self.assertEqual(A.reported, 1)
2658 self.assertIn('Build succeeded', A.messages[0])
2659 # Ensure the removed job was not included in the report.
2660 self.assertNotIn('project1-project2-integration', A.messages[0])
2661
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002662 def test_double_live_reconfiguration_shared_queue(self):
2663 # This was a real-world regression. A change is added to
2664 # gate; a reconfigure happens, a second change which depends
2665 # on the first is added, and a second reconfiguration happens.
2666 # Ensure that both changes merge.
2667
2668 # A failure may indicate incorrect caching or cleaning up of
2669 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002670 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002671
2672 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2673 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2674 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002675 A.addApproval('Code-Review', 2)
2676 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002677
2678 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002679 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002680 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002681 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002682 self.waitUntilSettled()
2683
2684 # Reconfigure (with only one change in the pipeline).
2685 self.sched.reconfigure(self.config)
2686 self.waitUntilSettled()
2687
2688 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002689 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002690 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002691 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002692 self.waitUntilSettled()
2693
2694 # Reconfigure (with both in the pipeline).
2695 self.sched.reconfigure(self.config)
2696 self.waitUntilSettled()
2697
Paul Belanger174a8272017-03-14 13:20:10 -04002698 self.executor_server.hold_jobs_in_build = False
2699 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002700 self.waitUntilSettled()
2701
2702 self.assertEqual(len(self.history), 8)
2703
2704 self.assertEqual(A.data['status'], 'MERGED')
2705 self.assertEqual(A.reported, 2)
2706 self.assertEqual(B.data['status'], 'MERGED')
2707 self.assertEqual(B.reported, 2)
2708
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002709 def test_live_reconfiguration_del_project(self):
2710 # Test project deletion from layout
2711 # while changes are enqueued
2712
Paul Belanger174a8272017-03-14 13:20:10 -04002713 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002714 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2715 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2716 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2717
2718 # A Depends-On: B
2719 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2720 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002721 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002722
2723 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2724 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2725 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002726 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002727 self.waitUntilSettled()
2728 self.assertEqual(len(self.builds), 5)
2729
2730 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002731 self.commitConfigUpdate(
2732 'common-config',
2733 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002734 self.sched.reconfigure(self.config)
2735 self.waitUntilSettled()
2736
2737 # Builds for C aborted, builds for A succeed,
2738 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002739 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002740 self.assertEqual(job_c.changes, '3,1')
2741 self.assertEqual(job_c.result, 'ABORTED')
2742
Paul Belanger174a8272017-03-14 13:20:10 -04002743 self.executor_server.hold_jobs_in_build = False
2744 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002745 self.waitUntilSettled()
2746
Clint Byrum0149ada2017-04-04 12:22:01 -07002747 self.assertEqual(
2748 self.getJobFromHistory('project-test1', 'org/project').changes,
2749 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002750
2751 self.assertEqual(A.data['status'], 'NEW')
2752 self.assertEqual(B.data['status'], 'NEW')
2753 self.assertEqual(C.data['status'], 'NEW')
2754 self.assertEqual(A.reported, 1)
2755 self.assertEqual(B.reported, 0)
2756 self.assertEqual(C.reported, 0)
2757
Clint Byrum0149ada2017-04-04 12:22:01 -07002758 tenant = self.sched.abide.tenants.get('tenant-one')
2759 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002760 self.assertIn('Build succeeded', A.messages[0])
2761
James E. Blairec056492016-07-22 09:45:56 -07002762 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002763 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002764 self.updateConfigLayout(
2765 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002766 self.sched.reconfigure(self.config)
2767
2768 self.init_repo("org/new-project")
2769 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2770
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002771 A.addApproval('Code-Review', 2)
2772 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002773 self.waitUntilSettled()
2774 self.assertEqual(self.getJobFromHistory('project-merge').result,
2775 'SUCCESS')
2776 self.assertEqual(self.getJobFromHistory('project-test1').result,
2777 'SUCCESS')
2778 self.assertEqual(self.getJobFromHistory('project-test2').result,
2779 'SUCCESS')
2780 self.assertEqual(A.data['status'], 'MERGED')
2781 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002782
James E. Blair9ea70072017-04-19 16:05:30 -07002783 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002784 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002785 self.init_repo("org/delete-project")
2786 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2787
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002788 A.addApproval('Code-Review', 2)
2789 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002790 self.waitUntilSettled()
2791 self.assertEqual(self.getJobFromHistory('project-merge').result,
2792 'SUCCESS')
2793 self.assertEqual(self.getJobFromHistory('project-test1').result,
2794 'SUCCESS')
2795 self.assertEqual(self.getJobFromHistory('project-test2').result,
2796 'SUCCESS')
2797 self.assertEqual(A.data['status'], 'MERGED')
2798 self.assertEqual(A.reported, 2)
2799
2800 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002801 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002802 if os.path.exists(os.path.join(self.merger_src_root, p)):
2803 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002804 if os.path.exists(os.path.join(self.executor_src_root, p)):
2805 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002806
2807 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2808
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002809 B.addApproval('Code-Review', 2)
2810 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002811 self.waitUntilSettled()
2812 self.assertEqual(self.getJobFromHistory('project-merge').result,
2813 'SUCCESS')
2814 self.assertEqual(self.getJobFromHistory('project-test1').result,
2815 'SUCCESS')
2816 self.assertEqual(self.getJobFromHistory('project-test2').result,
2817 'SUCCESS')
2818 self.assertEqual(B.data['status'], 'MERGED')
2819 self.assertEqual(B.reported, 2)
2820
James E. Blaire68d43f2017-04-19 14:03:30 -07002821 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002822 def test_tags(self):
2823 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002824 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2825 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2826 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2827 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2828 self.waitUntilSettled()
2829
James E. Blaire68d43f2017-04-19 14:03:30 -07002830 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002831
2832 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002833 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002834 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002835 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002836
2837 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002838 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002839 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002840
James E. Blair63bb0ef2013-07-29 17:14:51 -07002841 def test_timer(self):
2842 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002843 # This test can not use simple_layout because it must start
2844 # with a configuration which does not include a
2845 # timer-triggered job so that we have an opportunity to set
2846 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002847 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002848 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002849 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002850 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002851
Clark Boylan3ee090a2014-04-03 20:55:09 -07002852 # The pipeline triggers every second, so we should have seen
2853 # several by now.
2854 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002855 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002856
2857 self.assertEqual(len(self.builds), 2)
2858
James E. Blair63bb0ef2013-07-29 17:14:51 -07002859 port = self.webapp.server.socket.getsockname()[1]
2860
Clint Byrum5e729e92017-02-08 06:34:57 -08002861 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002862 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002863 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002864 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002865
Paul Belanger174a8272017-03-14 13:20:10 -04002866 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002867 # Stop queuing timer triggered jobs so that the assertions
2868 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002869 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002870 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002871 self.waitUntilSettled()
2872 # If APScheduler is in mid-event when we remove the job, we
2873 # can end up with one more event firing, so give it an extra
2874 # second to settle.
2875 time.sleep(1)
2876 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002877 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002878 self.waitUntilSettled()
2879
James E. Blair21037782017-07-19 11:56:55 -07002880 self.assertHistory([
2881 dict(name='project-bitrot', result='SUCCESS',
2882 ref='refs/heads/master'),
2883 dict(name='project-bitrot', result='SUCCESS',
2884 ref='refs/heads/stable'),
2885 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002886
2887 data = json.loads(data)
2888 status_jobs = set()
2889 for p in data['pipelines']:
2890 for q in p['change_queues']:
2891 for head in q['heads']:
2892 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002893 for job in change['jobs']:
2894 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002895 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002896
James E. Blair4f6033c2014-03-27 15:49:09 -07002897 def test_idle(self):
2898 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002899 # This test can not use simple_layout because it must start
2900 # with a configuration which does not include a
2901 # timer-triggered job so that we have an opportunity to set
2902 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002903 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002904
Clark Boylan3ee090a2014-04-03 20:55:09 -07002905 for x in range(1, 3):
2906 # Test that timer triggers periodic jobs even across
2907 # layout config reloads.
2908 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002909 self.commitConfigUpdate('common-config',
2910 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002911 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002912 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002913
Clark Boylan3ee090a2014-04-03 20:55:09 -07002914 # The pipeline triggers every second, so we should have seen
2915 # several by now.
2916 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002917
2918 # Stop queuing timer triggered jobs so that the assertions
2919 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002920 self.commitConfigUpdate('common-config',
2921 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002922 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002923 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002924 # If APScheduler is in mid-event when we remove the job,
2925 # we can end up with one more event firing, so give it an
2926 # extra second to settle.
2927 time.sleep(1)
2928 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002929 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002930 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002931 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002932 self.waitUntilSettled()
2933 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002934 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002935
James E. Blaircc324192017-04-19 14:36:08 -07002936 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002937 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002938 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2939 self.waitUntilSettled()
2940
2941 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2942 self.waitUntilSettled()
2943
James E. Blairff80a2f2013-12-27 13:24:06 -08002944 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002945
2946 # A.messages only holds what FakeGerrit places in it. Thus we
2947 # work on the knowledge of what the first message should be as
2948 # it is only configured to go to SMTP.
2949
2950 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002951 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002952 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002953 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002954 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002955 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002956
2957 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002958 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002959 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002960 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002961 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002962 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002963
James E. Blaire5910202013-12-27 09:50:31 -08002964 def test_timer_smtp(self):
2965 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002966 # This test can not use simple_layout because it must start
2967 # with a configuration which does not include a
2968 # timer-triggered job so that we have an opportunity to set
2969 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002970 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002971 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002972 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002973
Clark Boylan3ee090a2014-04-03 20:55:09 -07002974 # The pipeline triggers every second, so we should have seen
2975 # several by now.
2976 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002977 self.waitUntilSettled()
2978
Clark Boylan3ee090a2014-04-03 20:55:09 -07002979 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002980 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002981 self.waitUntilSettled()
2982 self.assertEqual(len(self.history), 2)
2983
James E. Blaire5910202013-12-27 09:50:31 -08002984 self.assertEqual(self.getJobFromHistory(
2985 'project-bitrot-stable-old').result, 'SUCCESS')
2986 self.assertEqual(self.getJobFromHistory(
2987 'project-bitrot-stable-older').result, 'SUCCESS')
2988
James E. Blairff80a2f2013-12-27 13:24:06 -08002989 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002990
2991 # A.messages only holds what FakeGerrit places in it. Thus we
2992 # work on the knowledge of what the first message should be as
2993 # it is only configured to go to SMTP.
2994
2995 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002996 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002997 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002998 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002999 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003000 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003001
Clark Boylan3ee090a2014-04-03 20:55:09 -07003002 # Stop queuing timer triggered jobs and let any that may have
3003 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003004 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003005 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003006 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003007 # If APScheduler is in mid-event when we remove the job, we
3008 # can end up with one more event firing, so give it an extra
3009 # second to settle.
3010 time.sleep(1)
3011 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003012 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003013 self.waitUntilSettled()
3014
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003015 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003016 def test_timer_sshkey(self):
3017 "Test that a periodic job can setup SSH key authentication"
3018 self.worker.hold_jobs_in_build = True
3019 self.config.set('zuul', 'layout_config',
3020 'tests/fixtures/layout-timer.yaml')
3021 self.sched.reconfigure(self.config)
3022 self.registerJobs()
3023
3024 # The pipeline triggers every second, so we should have seen
3025 # several by now.
3026 time.sleep(5)
3027 self.waitUntilSettled()
3028
3029 self.assertEqual(len(self.builds), 2)
3030
3031 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3032 self.assertTrue(os.path.isfile(ssh_wrapper))
3033 with open(ssh_wrapper) as f:
3034 ssh_wrapper_content = f.read()
3035 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3036 # In the unit tests Merger runs in the same process,
3037 # so we see its' environment variables
3038 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3039
3040 self.worker.release('.*')
3041 self.waitUntilSettled()
3042 self.assertEqual(len(self.history), 2)
3043
3044 self.assertEqual(self.getJobFromHistory(
3045 'project-bitrot-stable-old').result, 'SUCCESS')
3046 self.assertEqual(self.getJobFromHistory(
3047 'project-bitrot-stable-older').result, 'SUCCESS')
3048
3049 # Stop queuing timer triggered jobs and let any that may have
3050 # queued through so that end of test assertions pass.
3051 self.config.set('zuul', 'layout_config',
3052 'tests/fixtures/layout-no-timer.yaml')
3053 self.sched.reconfigure(self.config)
3054 self.registerJobs()
3055 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003056 # If APScheduler is in mid-event when we remove the job, we
3057 # can end up with one more event firing, so give it an extra
3058 # second to settle.
3059 time.sleep(1)
3060 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003061 self.worker.release('.*')
3062 self.waitUntilSettled()
3063
James E. Blair91e34592015-07-31 16:45:59 -07003064 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003065 "Test that the RPC client can enqueue a change"
3066 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003067 A.addApproval('Code-Review', 2)
3068 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003069
3070 client = zuul.rpcclient.RPCClient('127.0.0.1',
3071 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003072 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003073 r = client.enqueue(tenant='tenant-one',
3074 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003075 project='org/project',
3076 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003077 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003078 self.waitUntilSettled()
3079 self.assertEqual(self.getJobFromHistory('project-merge').result,
3080 'SUCCESS')
3081 self.assertEqual(self.getJobFromHistory('project-test1').result,
3082 'SUCCESS')
3083 self.assertEqual(self.getJobFromHistory('project-test2').result,
3084 'SUCCESS')
3085 self.assertEqual(A.data['status'], 'MERGED')
3086 self.assertEqual(A.reported, 2)
3087 self.assertEqual(r, True)
3088
James E. Blair91e34592015-07-31 16:45:59 -07003089 def test_client_enqueue_ref(self):
3090 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003091 p = "review.example.com/org/project"
3092 upstream = self.getUpstreamRepos([p])
3093 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3094 A.setMerged()
3095 A_commit = str(upstream[p].commit('master'))
3096 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003097
3098 client = zuul.rpcclient.RPCClient('127.0.0.1',
3099 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003100 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003101 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003102 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003103 pipeline='post',
3104 project='org/project',
3105 trigger='gerrit',
3106 ref='master',
3107 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003108 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003109 self.waitUntilSettled()
3110 job_names = [x.name for x in self.history]
3111 self.assertEqual(len(self.history), 1)
3112 self.assertIn('project-post', job_names)
3113 self.assertEqual(r, True)
3114
James E. Blairad28e912013-11-27 10:43:22 -08003115 def test_client_enqueue_negative(self):
3116 "Test that the RPC client returns errors"
3117 client = zuul.rpcclient.RPCClient('127.0.0.1',
3118 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003119 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003120 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003121 "Invalid tenant"):
3122 r = client.enqueue(tenant='tenant-foo',
3123 pipeline='gate',
3124 project='org/project',
3125 trigger='gerrit',
3126 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003127 self.assertEqual(r, False)
3128
3129 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003130 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003131 r = client.enqueue(tenant='tenant-one',
3132 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003133 project='project-does-not-exist',
3134 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003135 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003136 self.assertEqual(r, False)
3137
3138 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3139 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003140 r = client.enqueue(tenant='tenant-one',
3141 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003142 project='org/project',
3143 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003144 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003145 self.assertEqual(r, False)
3146
3147 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3148 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003149 r = client.enqueue(tenant='tenant-one',
3150 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003151 project='org/project',
3152 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003153 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003154 self.assertEqual(r, False)
3155
3156 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3157 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003158 r = client.enqueue(tenant='tenant-one',
3159 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003160 project='org/project',
3161 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003162 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003163 self.assertEqual(r, False)
3164
3165 self.waitUntilSettled()
3166 self.assertEqual(len(self.history), 0)
3167 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003168
3169 def test_client_promote(self):
3170 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003171 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003172 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3173 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3174 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003175 A.addApproval('Code-Review', 2)
3176 B.addApproval('Code-Review', 2)
3177 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003178
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003179 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3180 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3181 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003182
3183 self.waitUntilSettled()
3184
Paul Belangerbaca3132016-11-04 12:49:54 -04003185 tenant = self.sched.abide.tenants.get('tenant-one')
3186 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003187 enqueue_times = {}
3188 for item in items:
3189 enqueue_times[str(item.change)] = item.enqueue_time
3190
James E. Blair36658cf2013-12-06 17:53:48 -08003191 client = zuul.rpcclient.RPCClient('127.0.0.1',
3192 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003193 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003194 r = client.promote(tenant='tenant-one',
3195 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003196 change_ids=['2,1', '3,1'])
3197
Sean Daguef39b9ca2014-01-10 21:34:35 -05003198 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003199 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003200 for item in items:
3201 self.assertEqual(
3202 enqueue_times[str(item.change)], item.enqueue_time)
3203
James E. Blair78acec92014-02-06 07:11:32 -08003204 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003205 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003206 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003207 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003208 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003209 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003210 self.waitUntilSettled()
3211
3212 self.assertEqual(len(self.builds), 6)
3213 self.assertEqual(self.builds[0].name, 'project-test1')
3214 self.assertEqual(self.builds[1].name, 'project-test2')
3215 self.assertEqual(self.builds[2].name, 'project-test1')
3216 self.assertEqual(self.builds[3].name, 'project-test2')
3217 self.assertEqual(self.builds[4].name, 'project-test1')
3218 self.assertEqual(self.builds[5].name, 'project-test2')
3219
Paul Belangerbaca3132016-11-04 12:49:54 -04003220 self.assertTrue(self.builds[0].hasChanges(B))
3221 self.assertFalse(self.builds[0].hasChanges(A))
3222 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003223
Paul Belangerbaca3132016-11-04 12:49:54 -04003224 self.assertTrue(self.builds[2].hasChanges(B))
3225 self.assertTrue(self.builds[2].hasChanges(C))
3226 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003227
Paul Belangerbaca3132016-11-04 12:49:54 -04003228 self.assertTrue(self.builds[4].hasChanges(B))
3229 self.assertTrue(self.builds[4].hasChanges(C))
3230 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003231
Paul Belanger174a8272017-03-14 13:20:10 -04003232 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003233 self.waitUntilSettled()
3234
3235 self.assertEqual(A.data['status'], 'MERGED')
3236 self.assertEqual(A.reported, 2)
3237 self.assertEqual(B.data['status'], 'MERGED')
3238 self.assertEqual(B.reported, 2)
3239 self.assertEqual(C.data['status'], 'MERGED')
3240 self.assertEqual(C.reported, 2)
3241
James E. Blair36658cf2013-12-06 17:53:48 -08003242 self.assertEqual(r, True)
3243
3244 def test_client_promote_dependent(self):
3245 "Test that the RPC client can promote a dependent change"
3246 # C (depends on B) -> B -> A ; then promote C to get:
3247 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003248 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003249 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3250 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3251 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3252
3253 C.setDependsOn(B, 1)
3254
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003255 A.addApproval('Code-Review', 2)
3256 B.addApproval('Code-Review', 2)
3257 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003258
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003259 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3260 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3261 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003262
3263 self.waitUntilSettled()
3264
3265 client = zuul.rpcclient.RPCClient('127.0.0.1',
3266 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003267 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003268 r = client.promote(tenant='tenant-one',
3269 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003270 change_ids=['3,1'])
3271
James E. Blair78acec92014-02-06 07:11:32 -08003272 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003273 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003274 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003275 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003276 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003277 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003278 self.waitUntilSettled()
3279
3280 self.assertEqual(len(self.builds), 6)
3281 self.assertEqual(self.builds[0].name, 'project-test1')
3282 self.assertEqual(self.builds[1].name, 'project-test2')
3283 self.assertEqual(self.builds[2].name, 'project-test1')
3284 self.assertEqual(self.builds[3].name, 'project-test2')
3285 self.assertEqual(self.builds[4].name, 'project-test1')
3286 self.assertEqual(self.builds[5].name, 'project-test2')
3287
Paul Belangerbaca3132016-11-04 12:49:54 -04003288 self.assertTrue(self.builds[0].hasChanges(B))
3289 self.assertFalse(self.builds[0].hasChanges(A))
3290 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003291
Paul Belangerbaca3132016-11-04 12:49:54 -04003292 self.assertTrue(self.builds[2].hasChanges(B))
3293 self.assertTrue(self.builds[2].hasChanges(C))
3294 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003295
Paul Belangerbaca3132016-11-04 12:49:54 -04003296 self.assertTrue(self.builds[4].hasChanges(B))
3297 self.assertTrue(self.builds[4].hasChanges(C))
3298 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003299
Paul Belanger174a8272017-03-14 13:20:10 -04003300 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003301 self.waitUntilSettled()
3302
3303 self.assertEqual(A.data['status'], 'MERGED')
3304 self.assertEqual(A.reported, 2)
3305 self.assertEqual(B.data['status'], 'MERGED')
3306 self.assertEqual(B.reported, 2)
3307 self.assertEqual(C.data['status'], 'MERGED')
3308 self.assertEqual(C.reported, 2)
3309
James E. Blair36658cf2013-12-06 17:53:48 -08003310 self.assertEqual(r, True)
3311
3312 def test_client_promote_negative(self):
3313 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003314 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003316 A.addApproval('Code-Review', 2)
3317 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003318 self.waitUntilSettled()
3319
3320 client = zuul.rpcclient.RPCClient('127.0.0.1',
3321 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003322 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003323
3324 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003325 r = client.promote(tenant='tenant-one',
3326 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003327 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003328 self.assertEqual(r, False)
3329
3330 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003331 r = client.promote(tenant='tenant-one',
3332 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003333 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003334 self.assertEqual(r, False)
3335
Paul Belanger174a8272017-03-14 13:20:10 -04003336 self.executor_server.hold_jobs_in_build = False
3337 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003338 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003339
James E. Blaircc324192017-04-19 14:36:08 -07003340 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003341 def test_queue_rate_limiting(self):
3342 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003343 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003344 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3345 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3346 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3347
3348 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003349 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003350
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003351 A.addApproval('Code-Review', 2)
3352 B.addApproval('Code-Review', 2)
3353 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003354
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003355 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3356 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3357 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003358 self.waitUntilSettled()
3359
3360 # Only A and B will have their merge jobs queued because
3361 # window is 2.
3362 self.assertEqual(len(self.builds), 2)
3363 self.assertEqual(self.builds[0].name, 'project-merge')
3364 self.assertEqual(self.builds[1].name, 'project-merge')
3365
James E. Blair732ee862017-04-21 07:49:26 -07003366 # Release the merge jobs one at a time.
3367 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003368 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003369 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003370 self.waitUntilSettled()
3371
3372 # Only A and B will have their test jobs queued because
3373 # window is 2.
3374 self.assertEqual(len(self.builds), 4)
3375 self.assertEqual(self.builds[0].name, 'project-test1')
3376 self.assertEqual(self.builds[1].name, 'project-test2')
3377 self.assertEqual(self.builds[2].name, 'project-test1')
3378 self.assertEqual(self.builds[3].name, 'project-test2')
3379
Paul Belanger174a8272017-03-14 13:20:10 -04003380 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003381 self.waitUntilSettled()
3382
James E. Blaircc324192017-04-19 14:36:08 -07003383 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003384 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003385 # A failed so window is reduced by 1 to 1.
3386 self.assertEqual(queue.window, 1)
3387 self.assertEqual(queue.window_floor, 1)
3388 self.assertEqual(A.data['status'], 'NEW')
3389
3390 # Gate is reset and only B's merge job is queued because
3391 # window shrunk to 1.
3392 self.assertEqual(len(self.builds), 1)
3393 self.assertEqual(self.builds[0].name, 'project-merge')
3394
Paul Belanger174a8272017-03-14 13:20:10 -04003395 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003396 self.waitUntilSettled()
3397
3398 # Only B's test jobs are queued because window is still 1.
3399 self.assertEqual(len(self.builds), 2)
3400 self.assertEqual(self.builds[0].name, 'project-test1')
3401 self.assertEqual(self.builds[1].name, 'project-test2')
3402
Paul Belanger174a8272017-03-14 13:20:10 -04003403 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003404 self.waitUntilSettled()
3405
3406 # B was successfully merged so window is increased to 2.
3407 self.assertEqual(queue.window, 2)
3408 self.assertEqual(queue.window_floor, 1)
3409 self.assertEqual(B.data['status'], 'MERGED')
3410
3411 # Only C is left and its merge job is queued.
3412 self.assertEqual(len(self.builds), 1)
3413 self.assertEqual(self.builds[0].name, 'project-merge')
3414
Paul Belanger174a8272017-03-14 13:20:10 -04003415 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003416 self.waitUntilSettled()
3417
3418 # After successful merge job the test jobs for C are queued.
3419 self.assertEqual(len(self.builds), 2)
3420 self.assertEqual(self.builds[0].name, 'project-test1')
3421 self.assertEqual(self.builds[1].name, 'project-test2')
3422
Paul Belanger174a8272017-03-14 13:20:10 -04003423 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003424 self.waitUntilSettled()
3425
3426 # C successfully merged so window is bumped to 3.
3427 self.assertEqual(queue.window, 3)
3428 self.assertEqual(queue.window_floor, 1)
3429 self.assertEqual(C.data['status'], 'MERGED')
3430
James E. Blaircc324192017-04-19 14:36:08 -07003431 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003432 def test_queue_rate_limiting_dependent(self):
3433 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003434 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003435 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3436 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3437 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3438
3439 B.setDependsOn(A, 1)
3440
Paul Belanger174a8272017-03-14 13:20:10 -04003441 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003442
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003443 A.addApproval('Code-Review', 2)
3444 B.addApproval('Code-Review', 2)
3445 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003446
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003447 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3448 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3449 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003450 self.waitUntilSettled()
3451
3452 # Only A and B will have their merge jobs queued because
3453 # window is 2.
3454 self.assertEqual(len(self.builds), 2)
3455 self.assertEqual(self.builds[0].name, 'project-merge')
3456 self.assertEqual(self.builds[1].name, 'project-merge')
3457
James E. Blairf4a5f022017-04-18 14:01:10 -07003458 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003459
3460 # Only A and B will have their test jobs queued because
3461 # window is 2.
3462 self.assertEqual(len(self.builds), 4)
3463 self.assertEqual(self.builds[0].name, 'project-test1')
3464 self.assertEqual(self.builds[1].name, 'project-test2')
3465 self.assertEqual(self.builds[2].name, 'project-test1')
3466 self.assertEqual(self.builds[3].name, 'project-test2')
3467
Paul Belanger174a8272017-03-14 13:20:10 -04003468 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003469 self.waitUntilSettled()
3470
James E. Blaircc324192017-04-19 14:36:08 -07003471 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003472 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003473 # A failed so window is reduced by 1 to 1.
3474 self.assertEqual(queue.window, 1)
3475 self.assertEqual(queue.window_floor, 1)
3476 self.assertEqual(A.data['status'], 'NEW')
3477 self.assertEqual(B.data['status'], 'NEW')
3478
3479 # Gate is reset and only C's merge job is queued because
3480 # window shrunk to 1 and A and B were dequeued.
3481 self.assertEqual(len(self.builds), 1)
3482 self.assertEqual(self.builds[0].name, 'project-merge')
3483
James E. Blairf4a5f022017-04-18 14:01:10 -07003484 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003485
3486 # Only C's test jobs are queued because window is still 1.
3487 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003488 builds = self.getSortedBuilds()
3489 self.assertEqual(builds[0].name, 'project-test1')
3490 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003491
Paul Belanger174a8272017-03-14 13:20:10 -04003492 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003493 self.waitUntilSettled()
3494
3495 # C was successfully merged so window is increased to 2.
3496 self.assertEqual(queue.window, 2)
3497 self.assertEqual(queue.window_floor, 1)
3498 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003499
3500 def test_worker_update_metadata(self):
3501 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003502 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003503
3504 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003505 A.addApproval('Code-Review', 2)
3506 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003507 self.waitUntilSettled()
3508
Clint Byrumd4de79a2017-05-27 11:39:13 -07003509 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003510
3511 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003512 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003513
3514 start = time.time()
3515 while True:
3516 if time.time() - start > 10:
3517 raise Exception("Timeout waiting for gearman server to report "
3518 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003519 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003520 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003521 break
3522 else:
3523 time.sleep(0)
3524
3525 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003526 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003527
Paul Belanger174a8272017-03-14 13:20:10 -04003528 self.executor_server.hold_jobs_in_build = False
3529 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003530 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003531
James E. Blaircc324192017-04-19 14:36:08 -07003532 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003533 def test_footer_message(self):
3534 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003535 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003536 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003537 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003538 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003539 self.waitUntilSettled()
3540
3541 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003542 B.addApproval('Code-Review', 2)
3543 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003544 self.waitUntilSettled()
3545
3546 self.assertEqual(2, len(self.smtp_messages))
3547
Adam Gandelman82258fd2017-02-07 16:51:58 -08003548 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003549Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003550http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003551
Adam Gandelman82258fd2017-02-07 16:51:58 -08003552 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003553For CI problems and help debugging, contact ci@example.org"""
3554
Adam Gandelman82258fd2017-02-07 16:51:58 -08003555 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3556 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3557 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3558 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003559
Tobias Henkel9842bd72017-05-16 13:40:03 +02003560 @simple_layout('layouts/unmanaged-project.yaml')
3561 def test_unmanaged_project_start_message(self):
3562 "Test start reporting is not done for unmanaged projects."
3563 self.init_repo("org/project", tag='init')
3564 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3565 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3566 self.waitUntilSettled()
3567
3568 self.assertEqual(0, len(A.messages))
3569
Clint Byrumb9de8be2017-05-27 13:49:19 -07003570 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003571 def test_merge_failure_reporters(self):
3572 """Check that the config is set up correctly"""
3573
Clint Byrumb9de8be2017-05-27 13:49:19 -07003574 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003575 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003576 "Merge Failed.\n\nThis change or one of its cross-repo "
3577 "dependencies was unable to be automatically merged with the "
3578 "current state of its repository. Please rebase the change and "
3579 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003580 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003581 self.assertEqual(
3582 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003583 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003584
3585 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003586 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003587 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003588 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003589
3590 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003591 tenant.layout.pipelines['check'].merge_failure_actions[0],
3592 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003593
3594 self.assertTrue(
3595 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003596 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003597 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003598 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3599 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003600 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003601 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003602 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003603 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003604 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003605 gerritreporter.GerritReporter) and
3606 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003607 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003608 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003609 )
3610 )
3611
James E. Blairec056492016-07-22 09:45:56 -07003612 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003613 def test_merge_failure_reports(self):
3614 """Check that when a change fails to merge the correct message is sent
3615 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003616 self.updateConfigLayout(
3617 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003618 self.sched.reconfigure(self.config)
3619 self.registerJobs()
3620
3621 # Check a test failure isn't reported to SMTP
3622 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003623 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003624 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003625 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003626 self.waitUntilSettled()
3627
3628 self.assertEqual(3, len(self.history)) # 3 jobs
3629 self.assertEqual(0, len(self.smtp_messages))
3630
3631 # Check a merge failure is reported to SMTP
3632 # B should be merged, but C will conflict with B
3633 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3634 B.addPatchset(['conflict'])
3635 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3636 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003637 B.addApproval('Code-Review', 2)
3638 C.addApproval('Code-Review', 2)
3639 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3640 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003641 self.waitUntilSettled()
3642
3643 self.assertEqual(6, len(self.history)) # A and B jobs
3644 self.assertEqual(1, len(self.smtp_messages))
3645 self.assertEqual('The merge failed! For more information...',
3646 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003647
James E. Blairec056492016-07-22 09:45:56 -07003648 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003649 def test_default_merge_failure_reports(self):
3650 """Check that the default merge failure reports are correct."""
3651
3652 # A should report success, B should report merge failure.
3653 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3654 A.addPatchset(['conflict'])
3655 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3656 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003657 A.addApproval('Code-Review', 2)
3658 B.addApproval('Code-Review', 2)
3659 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3660 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003661 self.waitUntilSettled()
3662
3663 self.assertEqual(3, len(self.history)) # A jobs
3664 self.assertEqual(A.reported, 2)
3665 self.assertEqual(B.reported, 2)
3666 self.assertEqual(A.data['status'], 'MERGED')
3667 self.assertEqual(B.data['status'], 'NEW')
3668 self.assertIn('Build succeeded', A.messages[1])
3669 self.assertIn('Merge Failed', B.messages[1])
3670 self.assertIn('automatically merged', B.messages[1])
3671 self.assertNotIn('logs.example.com', B.messages[1])
3672 self.assertNotIn('SKIPPED', B.messages[1])
3673
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003674 def test_client_get_running_jobs(self):
3675 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003676 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003677 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003678 A.addApproval('Code-Review', 2)
3679 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003680 self.waitUntilSettled()
3681
3682 client = zuul.rpcclient.RPCClient('127.0.0.1',
3683 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003684 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003685
3686 # Wait for gearman server to send the initial workData back to zuul
3687 start = time.time()
3688 while True:
3689 if time.time() - start > 10:
3690 raise Exception("Timeout waiting for gearman server to report "
3691 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003692 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003693 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003694 break
3695 else:
3696 time.sleep(0)
3697
3698 running_items = client.get_running_jobs()
3699
3700 self.assertEqual(1, len(running_items))
3701 running_item = running_items[0]
3702 self.assertEqual([], running_item['failing_reasons'])
3703 self.assertEqual([], running_item['items_behind'])
3704 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003705 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003706 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003707 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003708 self.assertEqual(True, running_item['active'])
3709 self.assertEqual('1,1', running_item['id'])
3710
3711 self.assertEqual(3, len(running_item['jobs']))
3712 for job in running_item['jobs']:
3713 if job['name'] == 'project-merge':
3714 self.assertEqual('project-merge', job['name'])
3715 self.assertEqual('gate', job['pipeline'])
3716 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003717 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003718 'static/stream.html?uuid={uuid}&logfile=console.log'
3719 .format(uuid=job['uuid']), job['url'])
3720 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003721 'finger://{hostname}/{uuid}'.format(
3722 hostname=self.executor_server.hostname,
3723 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003724 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003725 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003726 self.assertEqual(False, job['canceled'])
3727 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003728 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003729 self.assertEqual('gate', job['pipeline'])
3730 break
3731
Paul Belanger174a8272017-03-14 13:20:10 -04003732 self.executor_server.hold_jobs_in_build = False
3733 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003734 self.waitUntilSettled()
3735
3736 running_items = client.get_running_jobs()
3737 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003738
James E. Blairb70e55a2017-04-19 12:57:02 -07003739 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003740 def test_nonvoting_pipeline(self):
3741 "Test that a nonvoting pipeline (experimental) can still report"
3742
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003743 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3744 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003745 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3746 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003747
3748 self.assertEqual(self.getJobFromHistory('project-merge').result,
3749 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003750 self.assertEqual(
3751 self.getJobFromHistory('experimental-project-test').result,
3752 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003753 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003754
3755 def test_crd_gate(self):
3756 "Test cross-repo dependencies"
3757 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3758 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003759 A.addApproval('Code-Review', 2)
3760 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003761
3762 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3763 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3764 AM2.setMerged()
3765 AM1.setMerged()
3766
3767 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3768 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3769 BM2.setMerged()
3770 BM1.setMerged()
3771
3772 # A -> AM1 -> AM2
3773 # B -> BM1 -> BM2
3774 # A Depends-On: B
3775 # M2 is here to make sure it is never queried. If it is, it
3776 # means zuul is walking down the entire history of merged
3777 # changes.
3778
3779 B.setDependsOn(BM1, 1)
3780 BM1.setDependsOn(BM2, 1)
3781
3782 A.setDependsOn(AM1, 1)
3783 AM1.setDependsOn(AM2, 1)
3784
3785 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3786 A.subject, B.data['id'])
3787
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003788 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003789 self.waitUntilSettled()
3790
3791 self.assertEqual(A.data['status'], 'NEW')
3792 self.assertEqual(B.data['status'], 'NEW')
3793
Paul Belanger5dccbe72016-11-14 11:17:37 -05003794 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003795 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003796
Paul Belanger174a8272017-03-14 13:20:10 -04003797 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003798 B.addApproval('Approved', 1)
3799 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003800 self.waitUntilSettled()
3801
Paul Belanger174a8272017-03-14 13:20:10 -04003802 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003803 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003804 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003805 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003806 self.executor_server.hold_jobs_in_build = False
3807 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003808 self.waitUntilSettled()
3809
3810 self.assertEqual(AM2.queried, 0)
3811 self.assertEqual(BM2.queried, 0)
3812 self.assertEqual(A.data['status'], 'MERGED')
3813 self.assertEqual(B.data['status'], 'MERGED')
3814 self.assertEqual(A.reported, 2)
3815 self.assertEqual(B.reported, 2)
3816
Paul Belanger5dccbe72016-11-14 11:17:37 -05003817 changes = self.getJobFromHistory(
3818 'project-merge', 'org/project1').changes
3819 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003820
3821 def test_crd_branch(self):
3822 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003823
3824 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003825 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3826 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3827 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3828 C.data['id'] = B.data['id']
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003829 A.addApproval('Code-Review', 2)
3830 B.addApproval('Code-Review', 2)
3831 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003832
3833 # A Depends-On: B+C
3834 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3835 A.subject, B.data['id'])
3836
Paul Belanger174a8272017-03-14 13:20:10 -04003837 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003838 B.addApproval('Approved', 1)
3839 C.addApproval('Approved', 1)
3840 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003841 self.waitUntilSettled()
3842
Paul Belanger174a8272017-03-14 13:20:10 -04003843 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003844 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003845 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003846 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003847 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003848 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003849 self.executor_server.hold_jobs_in_build = False
3850 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003851 self.waitUntilSettled()
3852
3853 self.assertEqual(A.data['status'], 'MERGED')
3854 self.assertEqual(B.data['status'], 'MERGED')
3855 self.assertEqual(C.data['status'], 'MERGED')
3856 self.assertEqual(A.reported, 2)
3857 self.assertEqual(B.reported, 2)
3858 self.assertEqual(C.reported, 2)
3859
Paul Belanger6379db12016-11-14 13:57:54 -05003860 changes = self.getJobFromHistory(
3861 'project-merge', 'org/project1').changes
3862 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003863
3864 def test_crd_multiline(self):
3865 "Test multiple depends-on lines in commit"
3866 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3867 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3868 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003869 A.addApproval('Code-Review', 2)
3870 B.addApproval('Code-Review', 2)
3871 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003872
3873 # A Depends-On: B+C
3874 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3875 A.subject, B.data['id'], C.data['id'])
3876
Paul Belanger174a8272017-03-14 13:20:10 -04003877 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003878 B.addApproval('Approved', 1)
3879 C.addApproval('Approved', 1)
3880 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003881 self.waitUntilSettled()
3882
Paul Belanger174a8272017-03-14 13:20:10 -04003883 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003884 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003885 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003886 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003887 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003888 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003889 self.executor_server.hold_jobs_in_build = False
3890 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003891 self.waitUntilSettled()
3892
3893 self.assertEqual(A.data['status'], 'MERGED')
3894 self.assertEqual(B.data['status'], 'MERGED')
3895 self.assertEqual(C.data['status'], 'MERGED')
3896 self.assertEqual(A.reported, 2)
3897 self.assertEqual(B.reported, 2)
3898 self.assertEqual(C.reported, 2)
3899
Paul Belanger5dccbe72016-11-14 11:17:37 -05003900 changes = self.getJobFromHistory(
3901 'project-merge', 'org/project1').changes
3902 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003903
3904 def test_crd_unshared_gate(self):
3905 "Test cross-repo dependencies in unshared gate queues"
3906 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3907 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003908 A.addApproval('Code-Review', 2)
3909 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003910
3911 # A Depends-On: B
3912 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3913 A.subject, B.data['id'])
3914
3915 # A and B do not share a queue, make sure that A is unable to
3916 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003917 B.addApproval('Approved', 1)
3918 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003919 self.waitUntilSettled()
3920
3921 self.assertEqual(A.data['status'], 'NEW')
3922 self.assertEqual(B.data['status'], 'NEW')
3923 self.assertEqual(A.reported, 0)
3924 self.assertEqual(B.reported, 0)
3925 self.assertEqual(len(self.history), 0)
3926
3927 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003928 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003929 self.waitUntilSettled()
3930
3931 self.assertEqual(B.data['status'], 'MERGED')
3932 self.assertEqual(B.reported, 2)
3933
3934 # Now that B is merged, A should be able to be enqueued and
3935 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003936 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003937 self.waitUntilSettled()
3938
3939 self.assertEqual(A.data['status'], 'MERGED')
3940 self.assertEqual(A.reported, 2)
3941
James E. Blair96698e22015-04-02 07:48:21 -07003942 def test_crd_gate_reverse(self):
3943 "Test reverse cross-repo dependencies"
3944 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3945 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003946 A.addApproval('Code-Review', 2)
3947 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003948
3949 # A Depends-On: B
3950
3951 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3952 A.subject, B.data['id'])
3953
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003954 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003955 self.waitUntilSettled()
3956
3957 self.assertEqual(A.data['status'], 'NEW')
3958 self.assertEqual(B.data['status'], 'NEW')
3959
Paul Belanger174a8272017-03-14 13:20:10 -04003960 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003961 A.addApproval('Approved', 1)
3962 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003963 self.waitUntilSettled()
3964
Paul Belanger174a8272017-03-14 13:20:10 -04003965 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003966 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003967 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003968 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003969 self.executor_server.hold_jobs_in_build = False
3970 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003971 self.waitUntilSettled()
3972
3973 self.assertEqual(A.data['status'], 'MERGED')
3974 self.assertEqual(B.data['status'], 'MERGED')
3975 self.assertEqual(A.reported, 2)
3976 self.assertEqual(B.reported, 2)
3977
Paul Belanger5dccbe72016-11-14 11:17:37 -05003978 changes = self.getJobFromHistory(
3979 'project-merge', 'org/project1').changes
3980 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003981
James E. Blair5ee24252014-12-30 10:12:29 -08003982 def test_crd_cycle(self):
3983 "Test cross-repo dependency cycles"
3984 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3985 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003986 A.addApproval('Code-Review', 2)
3987 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003988
3989 # A -> B -> A (via commit-depends)
3990
3991 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3992 A.subject, B.data['id'])
3993 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3994 B.subject, A.data['id'])
3995
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003996 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003997 self.waitUntilSettled()
3998
3999 self.assertEqual(A.reported, 0)
4000 self.assertEqual(B.reported, 0)
4001 self.assertEqual(A.data['status'], 'NEW')
4002 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004003
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004004 def test_crd_gate_unknown(self):
4005 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004006 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004007 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4008 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004009 A.addApproval('Code-Review', 2)
4010 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004011
4012 # A Depends-On: B
4013 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4014 A.subject, B.data['id'])
4015
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004016 B.addApproval('Approved', 1)
4017 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004018 self.waitUntilSettled()
4019
4020 # Unknown projects cannot share a queue with any other
4021 # since they don't have common jobs with any other (they have no jobs).
4022 # Changes which depend on unknown project changes
4023 # should not be processed in dependent pipeline
4024 self.assertEqual(A.data['status'], 'NEW')
4025 self.assertEqual(B.data['status'], 'NEW')
4026 self.assertEqual(A.reported, 0)
4027 self.assertEqual(B.reported, 0)
4028 self.assertEqual(len(self.history), 0)
4029
James E. Blair7720b402017-06-07 08:51:49 -07004030 # Simulate change B being gated outside this layout Set the
4031 # change merged before submitting the event so that when the
4032 # event triggers a gerrit query to update the change, we get
4033 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004034 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004035 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004036 self.waitUntilSettled()
4037 self.assertEqual(len(self.history), 0)
4038
4039 # Now that B is merged, A should be able to be enqueued and
4040 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004041 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004042 self.waitUntilSettled()
4043
4044 self.assertEqual(A.data['status'], 'MERGED')
4045 self.assertEqual(A.reported, 2)
4046 self.assertEqual(B.data['status'], 'MERGED')
4047 self.assertEqual(B.reported, 0)
4048
James E. Blairbfb8e042014-12-30 17:01:44 -08004049 def test_crd_check(self):
4050 "Test cross-repo dependencies in independent pipelines"
4051
Paul Belanger174a8272017-03-14 13:20:10 -04004052 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004053 self.gearman_server.hold_jobs_in_queue = True
4054 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4055 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4056
4057 # A Depends-On: B
4058 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4059 A.subject, B.data['id'])
4060
4061 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4062 self.waitUntilSettled()
4063
James E. Blairbfb8e042014-12-30 17:01:44 -08004064 self.gearman_server.hold_jobs_in_queue = False
4065 self.gearman_server.release()
4066 self.waitUntilSettled()
4067
Paul Belanger174a8272017-03-14 13:20:10 -04004068 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004069 self.waitUntilSettled()
4070
James E. Blair2a535672017-04-27 12:03:15 -07004071 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004072
Paul Belanger174a8272017-03-14 13:20:10 -04004073 self.executor_server.hold_jobs_in_build = False
4074 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004075 self.waitUntilSettled()
4076
James E. Blairbfb8e042014-12-30 17:01:44 -08004077 self.assertEqual(A.data['status'], 'NEW')
4078 self.assertEqual(B.data['status'], 'NEW')
4079 self.assertEqual(A.reported, 1)
4080 self.assertEqual(B.reported, 0)
4081
4082 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004083 tenant = self.sched.abide.tenants.get('tenant-one')
4084 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004085
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004086 def test_crd_check_git_depends(self):
4087 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004088 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004089 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4090 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4091
4092 # Add two git-dependent changes and make sure they both report
4093 # success.
4094 B.setDependsOn(A, 1)
4095 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4096 self.waitUntilSettled()
4097 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4098 self.waitUntilSettled()
4099
James E. Blairb8c16472015-05-05 14:55:26 -07004100 self.orderedRelease()
4101 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004102 self.waitUntilSettled()
4103
4104 self.assertEqual(A.data['status'], 'NEW')
4105 self.assertEqual(B.data['status'], 'NEW')
4106 self.assertEqual(A.reported, 1)
4107 self.assertEqual(B.reported, 1)
4108
4109 self.assertEqual(self.history[0].changes, '1,1')
4110 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004111 tenant = self.sched.abide.tenants.get('tenant-one')
4112 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004113
4114 self.assertIn('Build succeeded', A.messages[0])
4115 self.assertIn('Build succeeded', B.messages[0])
4116
4117 def test_crd_check_duplicate(self):
4118 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004119 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004120 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4121 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004122 tenant = self.sched.abide.tenants.get('tenant-one')
4123 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004124
4125 # Add two git-dependent changes...
4126 B.setDependsOn(A, 1)
4127 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4128 self.waitUntilSettled()
4129 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4130
4131 # ...make sure the live one is not duplicated...
4132 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4133 self.waitUntilSettled()
4134 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4135
4136 # ...but the non-live one is able to be.
4137 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4138 self.waitUntilSettled()
4139 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4140
Clark Boylandd849822015-03-02 12:38:14 -08004141 # Release jobs in order to avoid races with change A jobs
4142 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004143 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004144 self.executor_server.hold_jobs_in_build = False
4145 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004146 self.waitUntilSettled()
4147
4148 self.assertEqual(A.data['status'], 'NEW')
4149 self.assertEqual(B.data['status'], 'NEW')
4150 self.assertEqual(A.reported, 1)
4151 self.assertEqual(B.reported, 1)
4152
4153 self.assertEqual(self.history[0].changes, '1,1 2,1')
4154 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004155 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004156
4157 self.assertIn('Build succeeded', A.messages[0])
4158 self.assertIn('Build succeeded', B.messages[0])
4159
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004160 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004161 "Test cross-repo dependencies re-enqueued in independent pipelines"
4162
4163 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004164 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4165 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004166
4167 # A Depends-On: B
4168 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4169 A.subject, B.data['id'])
4170
4171 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4172 self.waitUntilSettled()
4173
4174 self.sched.reconfigure(self.config)
4175
4176 # Make sure the items still share a change queue, and the
4177 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004178 tenant = self.sched.abide.tenants.get('tenant-one')
4179 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4180 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004181 first_item = queue.queue[0]
4182 for item in queue.queue:
4183 self.assertEqual(item.queue, first_item.queue)
4184 self.assertFalse(first_item.live)
4185 self.assertTrue(queue.queue[1].live)
4186
4187 self.gearman_server.hold_jobs_in_queue = False
4188 self.gearman_server.release()
4189 self.waitUntilSettled()
4190
4191 self.assertEqual(A.data['status'], 'NEW')
4192 self.assertEqual(B.data['status'], 'NEW')
4193 self.assertEqual(A.reported, 1)
4194 self.assertEqual(B.reported, 0)
4195
4196 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004197 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004198
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004199 def test_crd_check_reconfiguration(self):
4200 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4201
4202 def test_crd_undefined_project(self):
4203 """Test that undefined projects in dependencies are handled for
4204 independent pipelines"""
4205 # It's a hack for fake gerrit,
4206 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004207 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004208 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4209
Jesse Keatingea68ab42017-04-19 15:03:30 -07004210 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004211 def test_crd_check_ignore_dependencies(self):
4212 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004213
4214 self.gearman_server.hold_jobs_in_queue = True
4215 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4216 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4217 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4218
4219 # A Depends-On: B
4220 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4221 A.subject, B.data['id'])
4222 # C git-depends on B
4223 C.setDependsOn(B, 1)
4224 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4225 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4226 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4227 self.waitUntilSettled()
4228
4229 # Make sure none of the items share a change queue, and all
4230 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004231 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004232 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004233 self.assertEqual(len(check_pipeline.queues), 3)
4234 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4235 for item in check_pipeline.getAllItems():
4236 self.assertTrue(item.live)
4237
4238 self.gearman_server.hold_jobs_in_queue = False
4239 self.gearman_server.release()
4240 self.waitUntilSettled()
4241
4242 self.assertEqual(A.data['status'], 'NEW')
4243 self.assertEqual(B.data['status'], 'NEW')
4244 self.assertEqual(C.data['status'], 'NEW')
4245 self.assertEqual(A.reported, 1)
4246 self.assertEqual(B.reported, 1)
4247 self.assertEqual(C.reported, 1)
4248
4249 # Each job should have tested exactly one change
4250 for job in self.history:
4251 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004252
James E. Blair7cb84542017-04-19 13:35:05 -07004253 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004254 def test_crd_check_transitive(self):
4255 "Test transitive cross-repo dependencies"
4256 # Specifically, if A -> B -> C, and C gets a new patchset and
4257 # A gets a new patchset, ensure the test of A,2 includes B,1
4258 # and C,2 (not C,1 which would indicate stale data in the
4259 # cache for B).
4260 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4261 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4262 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4263
4264 # A Depends-On: B
4265 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4266 A.subject, B.data['id'])
4267
4268 # B Depends-On: C
4269 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4270 B.subject, C.data['id'])
4271
4272 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4273 self.waitUntilSettled()
4274 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4275
4276 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4277 self.waitUntilSettled()
4278 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4279
4280 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4281 self.waitUntilSettled()
4282 self.assertEqual(self.history[-1].changes, '3,1')
4283
4284 C.addPatchset()
4285 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4286 self.waitUntilSettled()
4287 self.assertEqual(self.history[-1].changes, '3,2')
4288
4289 A.addPatchset()
4290 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4291 self.waitUntilSettled()
4292 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004293
James E. Blaira8b90b32016-08-24 15:18:50 -07004294 def test_crd_check_unknown(self):
4295 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004296 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004297 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4298 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4299 # A Depends-On: B
4300 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4301 A.subject, B.data['id'])
4302
4303 # Make sure zuul has seen an event on B.
4304 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4305 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4306 self.waitUntilSettled()
4307
4308 self.assertEqual(A.data['status'], 'NEW')
4309 self.assertEqual(A.reported, 1)
4310 self.assertEqual(B.data['status'], 'NEW')
4311 self.assertEqual(B.reported, 0)
4312
James E. Blair92464a22016-04-05 10:21:26 -07004313 def test_crd_cycle_join(self):
4314 "Test an updated change creates a cycle"
4315 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4316
4317 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4318 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004319 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004320
4321 # Create B->A
4322 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4323 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4324 B.subject, A.data['id'])
4325 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4326 self.waitUntilSettled()
4327
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004328 # Dep is there so zuul should have reported on B
4329 self.assertEqual(B.reported, 1)
4330
James E. Blair92464a22016-04-05 10:21:26 -07004331 # Update A to add A->B (a cycle).
4332 A.addPatchset()
4333 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4334 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004335 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4336 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004337
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004338 # Dependency cycle injected so zuul should not have reported again on A
4339 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004340
4341 # Now if we update B to remove the depends-on, everything
4342 # should be okay. B; A->B
4343
4344 B.addPatchset()
4345 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004346 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4347 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004348
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004349 # Cycle was removed so now zuul should have reported again on A
4350 self.assertEqual(A.reported, 2)
4351
4352 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4353 self.waitUntilSettled()
4354 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004355
Jesse Keatingcc67b122017-04-19 14:54:57 -07004356 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004357 def test_disable_at(self):
4358 "Test a pipeline will only report to the disabled trigger when failing"
4359
Jesse Keatingcc67b122017-04-19 14:54:57 -07004360 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004361 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004362 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004363 0, tenant.layout.pipelines['check']._consecutive_failures)
4364 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004365
4366 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4367 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4368 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4369 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4370 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4371 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4372 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4373 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4374 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4375 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4376 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4377
Paul Belanger174a8272017-03-14 13:20:10 -04004378 self.executor_server.failJob('project-test1', A)
4379 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004380 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004381 self.executor_server.failJob('project-test1', D)
4382 self.executor_server.failJob('project-test1', E)
4383 self.executor_server.failJob('project-test1', F)
4384 self.executor_server.failJob('project-test1', G)
4385 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004386 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004387 self.executor_server.failJob('project-test1', J)
4388 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004389
4390 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4391 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4392 self.waitUntilSettled()
4393
4394 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004395 2, tenant.layout.pipelines['check']._consecutive_failures)
4396 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004397
4398 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4399 self.waitUntilSettled()
4400
4401 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004402 0, tenant.layout.pipelines['check']._consecutive_failures)
4403 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004404
4405 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4406 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4407 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4408 self.waitUntilSettled()
4409
4410 # We should be disabled now
4411 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004412 3, tenant.layout.pipelines['check']._consecutive_failures)
4413 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004414
4415 # We need to wait between each of these patches to make sure the
4416 # smtp messages come back in an expected order
4417 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4418 self.waitUntilSettled()
4419 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4420 self.waitUntilSettled()
4421 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4422 self.waitUntilSettled()
4423
4424 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4425 # leaving a message on each change
4426 self.assertEqual(1, len(A.messages))
4427 self.assertIn('Build failed.', A.messages[0])
4428 self.assertEqual(1, len(B.messages))
4429 self.assertIn('Build failed.', B.messages[0])
4430 self.assertEqual(1, len(C.messages))
4431 self.assertIn('Build succeeded.', C.messages[0])
4432 self.assertEqual(1, len(D.messages))
4433 self.assertIn('Build failed.', D.messages[0])
4434 self.assertEqual(1, len(E.messages))
4435 self.assertIn('Build failed.', E.messages[0])
4436 self.assertEqual(1, len(F.messages))
4437 self.assertIn('Build failed.', F.messages[0])
4438
4439 # The last 3 (GHI) would have only reported via smtp.
4440 self.assertEqual(3, len(self.smtp_messages))
4441 self.assertEqual(0, len(G.messages))
4442 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004443 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004444 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004445 self.assertEqual(0, len(H.messages))
4446 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004447 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004448 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004449 self.assertEqual(0, len(I.messages))
4450 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004451 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004452 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004453
4454 # Now reload the configuration (simulate a HUP) to check the pipeline
4455 # comes out of disabled
4456 self.sched.reconfigure(self.config)
4457
Jesse Keatingcc67b122017-04-19 14:54:57 -07004458 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004459
4460 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004461 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004462 0, tenant.layout.pipelines['check']._consecutive_failures)
4463 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004464
4465 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4466 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4467 self.waitUntilSettled()
4468
4469 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004470 2, tenant.layout.pipelines['check']._consecutive_failures)
4471 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004472
4473 # J and K went back to gerrit
4474 self.assertEqual(1, len(J.messages))
4475 self.assertIn('Build failed.', J.messages[0])
4476 self.assertEqual(1, len(K.messages))
4477 self.assertIn('Build failed.', K.messages[0])
4478 # No more messages reported via smtp
4479 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004480
James E. Blaircc020532017-04-19 13:04:44 -07004481 @simple_layout('layouts/one-job-project.yaml')
4482 def test_one_job_project(self):
4483 "Test that queueing works with one job"
4484 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4485 'master', 'A')
4486 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4487 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004488 A.addApproval('Code-Review', 2)
4489 B.addApproval('Code-Review', 2)
4490 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4491 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004492 self.waitUntilSettled()
4493
4494 self.assertEqual(A.data['status'], 'MERGED')
4495 self.assertEqual(A.reported, 2)
4496 self.assertEqual(B.data['status'], 'MERGED')
4497 self.assertEqual(B.reported, 2)
4498
Paul Belanger71d98172016-11-08 10:56:31 -05004499 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004500 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004501
Paul Belanger174a8272017-03-14 13:20:10 -04004502 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004503 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4504 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4505 self.waitUntilSettled()
4506
Paul Belanger174a8272017-03-14 13:20:10 -04004507 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004508 self.waitUntilSettled()
4509
4510 self.assertEqual(len(self.builds), 2)
4511 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004512 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004513 self.waitUntilSettled()
4514
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004515 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004516 self.assertEqual(len(self.builds), 1,
4517 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004518 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004519 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004520 self.waitUntilSettled()
4521
Paul Belanger174a8272017-03-14 13:20:10 -04004522 self.executor_server.hold_jobs_in_build = False
4523 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004524 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004525 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004526 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4527 self.assertEqual(A.reported, 1)
4528 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004529
James E. Blair15be0e12017-01-03 13:45:20 -08004530 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004531 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004532
4533 self.fake_nodepool.paused = True
4534 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004535 A.addApproval('Code-Review', 2)
4536 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004537 self.waitUntilSettled()
4538
4539 self.zk.client.stop()
4540 self.zk.client.start()
4541 self.fake_nodepool.paused = False
4542 self.waitUntilSettled()
4543
4544 self.assertEqual(A.data['status'], 'MERGED')
4545 self.assertEqual(A.reported, 2)
4546
James E. Blair6ab79e02017-01-06 10:10:17 -08004547 def test_nodepool_failure(self):
4548 "Test that jobs are reported after a nodepool failure"
4549
4550 self.fake_nodepool.paused = True
4551 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004552 A.addApproval('Code-Review', 2)
4553 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004554 self.waitUntilSettled()
4555
4556 req = self.fake_nodepool.getNodeRequests()[0]
4557 self.fake_nodepool.addFailRequest(req)
4558
4559 self.fake_nodepool.paused = False
4560 self.waitUntilSettled()
4561
4562 self.assertEqual(A.data['status'], 'NEW')
4563 self.assertEqual(A.reported, 2)
4564 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4565 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4566 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4567
Arieb6f068c2016-10-09 13:11:06 +03004568
James E. Blaira002b032017-04-18 10:35:48 -07004569class TestExecutor(ZuulTestCase):
4570 tenant_config_file = 'config/single-tenant/main.yaml'
4571
4572 def assertFinalState(self):
4573 # In this test, we expect to shut down in a non-final state,
4574 # so skip these checks.
4575 pass
4576
4577 def assertCleanShutdown(self):
4578 self.log.debug("Assert clean shutdown")
4579
4580 # After shutdown, make sure no jobs are running
4581 self.assertEqual({}, self.executor_server.job_workers)
4582
4583 # Make sure that git.Repo objects have been garbage collected.
4584 repos = []
4585 gc.collect()
4586 for obj in gc.get_objects():
4587 if isinstance(obj, git.Repo):
4588 self.log.debug("Leaked git repo object: %s" % repr(obj))
4589 repos.append(obj)
4590 self.assertEqual(len(repos), 0)
4591
4592 def test_executor_shutdown(self):
4593 "Test that the executor can shut down with jobs running"
4594
4595 self.executor_server.hold_jobs_in_build = True
4596 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004597 A.addApproval('Code-Review', 2)
4598 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004599 self.waitUntilSettled()
4600
4601
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004602class TestDependencyGraph(ZuulTestCase):
4603 tenant_config_file = 'config/dependency-graph/main.yaml'
4604
4605 def test_dependeny_graph_dispatch_jobs_once(self):
4606 "Test a job in a dependency graph is queued only once"
4607 # Job dependencies, starting with A
4608 # A
4609 # / \
4610 # B C
4611 # / \ / \
4612 # D F E
4613 # |
4614 # G
4615
4616 self.executor_server.hold_jobs_in_build = True
4617 change = self.fake_gerrit.addFakeChange(
4618 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004619 change.addApproval('Code-Review', 2)
4620 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004621
4622 self.waitUntilSettled()
4623 self.assertEqual([b.name for b in self.builds], ['A'])
4624
4625 self.executor_server.release('A')
4626 self.waitUntilSettled()
4627 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4628
4629 self.executor_server.release('B')
4630 self.waitUntilSettled()
4631 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4632
4633 self.executor_server.release('D')
4634 self.waitUntilSettled()
4635 self.assertEqual([b.name for b in self.builds], ['C'])
4636
4637 self.executor_server.release('C')
4638 self.waitUntilSettled()
4639 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4640
4641 self.executor_server.release('F')
4642 self.waitUntilSettled()
4643 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4644
4645 self.executor_server.release('G')
4646 self.waitUntilSettled()
4647 self.assertEqual([b.name for b in self.builds], ['E'])
4648
4649 self.executor_server.release('E')
4650 self.waitUntilSettled()
4651 self.assertEqual(len(self.builds), 0)
4652
4653 self.executor_server.hold_jobs_in_build = False
4654 self.executor_server.release()
4655 self.waitUntilSettled()
4656
4657 self.assertEqual(len(self.builds), 0)
4658 self.assertEqual(len(self.history), 7)
4659
4660 self.assertEqual(change.data['status'], 'MERGED')
4661 self.assertEqual(change.reported, 2)
4662
4663 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4664 "Test that a job waits till all dependencies are successful"
4665 # Job dependencies, starting with A
4666 # A
4667 # / \
4668 # B C*
4669 # / \ / \
4670 # D F E
4671 # |
4672 # G
4673
4674 self.executor_server.hold_jobs_in_build = True
4675 change = self.fake_gerrit.addFakeChange(
4676 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004677 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004678
4679 self.executor_server.failJob('C', change)
4680
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004681 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004682
4683 self.waitUntilSettled()
4684 self.assertEqual([b.name for b in self.builds], ['A'])
4685
4686 self.executor_server.release('A')
4687 self.waitUntilSettled()
4688 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4689
4690 self.executor_server.release('B')
4691 self.waitUntilSettled()
4692 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4693
4694 self.executor_server.release('D')
4695 self.waitUntilSettled()
4696 self.assertEqual([b.name for b in self.builds], ['C'])
4697
4698 self.executor_server.release('C')
4699 self.waitUntilSettled()
4700 self.assertEqual(len(self.builds), 0)
4701
4702 self.executor_server.hold_jobs_in_build = False
4703 self.executor_server.release()
4704 self.waitUntilSettled()
4705
4706 self.assertEqual(len(self.builds), 0)
4707 self.assertEqual(len(self.history), 4)
4708
4709 self.assertEqual(change.data['status'], 'NEW')
4710 self.assertEqual(change.reported, 2)
4711
4712
Arieb6f068c2016-10-09 13:11:06 +03004713class TestDuplicatePipeline(ZuulTestCase):
4714 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4715
4716 def test_duplicate_pipelines(self):
4717 "Test that a change matching multiple pipelines works"
4718
4719 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4720 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4721 self.waitUntilSettled()
4722
4723 self.assertHistory([
4724 dict(name='project-test1', result='SUCCESS', changes='1,1',
4725 pipeline='dup1'),
4726 dict(name='project-test1', result='SUCCESS', changes='1,1',
4727 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004728 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004729
4730 self.assertEqual(len(A.messages), 2)
4731
Paul Belangera46a3742016-11-09 14:23:03 -05004732 if 'dup1' in A.messages[0]:
4733 self.assertIn('dup1', A.messages[0])
4734 self.assertNotIn('dup2', A.messages[0])
4735 self.assertIn('project-test1', A.messages[0])
4736 self.assertIn('dup2', A.messages[1])
4737 self.assertNotIn('dup1', A.messages[1])
4738 self.assertIn('project-test1', A.messages[1])
4739 else:
4740 self.assertIn('dup1', A.messages[1])
4741 self.assertNotIn('dup2', A.messages[1])
4742 self.assertIn('project-test1', A.messages[1])
4743 self.assertIn('dup2', A.messages[0])
4744 self.assertNotIn('dup1', A.messages[0])
4745 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004746
4747
Paul Belanger86085b32016-11-03 12:48:57 -04004748class TestSchedulerTemplatedProject(ZuulTestCase):
4749 tenant_config_file = 'config/templated-project/main.yaml'
4750
Paul Belanger174a8272017-03-14 13:20:10 -04004751 def test_job_from_templates_executed(self):
4752 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004753
4754 A = self.fake_gerrit.addFakeChange(
4755 'org/templated-project', 'master', 'A')
4756 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4757 self.waitUntilSettled()
4758
4759 self.assertEqual(self.getJobFromHistory('project-test1').result,
4760 'SUCCESS')
4761 self.assertEqual(self.getJobFromHistory('project-test2').result,
4762 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004763
4764 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004765 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004766
4767 A = self.fake_gerrit.addFakeChange(
4768 'org/layered-project', 'master', 'A')
4769 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4770 self.waitUntilSettled()
4771
4772 self.assertEqual(self.getJobFromHistory('project-test1').result,
4773 'SUCCESS')
4774 self.assertEqual(self.getJobFromHistory('project-test2').result,
4775 'SUCCESS')
4776 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4777 ).result, 'SUCCESS')
4778 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4779 ).result, 'SUCCESS')
4780 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4781 ).result, 'SUCCESS')
4782 self.assertEqual(self.getJobFromHistory('project-test6').result,
4783 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004784
4785
4786class TestSchedulerSuccessURL(ZuulTestCase):
4787 tenant_config_file = 'config/success-url/main.yaml'
4788
4789 def test_success_url(self):
4790 "Ensure bad build params are ignored"
4791 self.sched.reconfigure(self.config)
4792 self.init_repo('org/docs')
4793
4794 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4795 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4796 self.waitUntilSettled()
4797
4798 # Both builds ran: docs-draft-test + docs-draft-test2
4799 self.assertEqual(len(self.history), 2)
4800
4801 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004802 for build in self.history:
4803 if build.name == 'docs-draft-test':
4804 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004805 elif build.name == 'docs-draft-test2':
4806 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004807
4808 # Two msgs: 'Starting...' + results
4809 self.assertEqual(len(self.smtp_messages), 2)
4810 body = self.smtp_messages[1]['body'].splitlines()
4811 self.assertEqual('Build succeeded.', body[0])
4812
4813 self.assertIn(
4814 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4815 'docs-draft-test/%s/publish-docs/' % uuid,
4816 body[2])
4817
Paul Belanger174a8272017-03-14 13:20:10 -04004818 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004819 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004820 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4821 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004822 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004823 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004824
4825
Adam Gandelman4da00f62016-12-09 15:47:33 -08004826class TestSchedulerMerges(ZuulTestCase):
4827 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004828
4829 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004830 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004831 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004832 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004833 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4834 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4835 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004836 A.addApproval('Code-Review', 2)
4837 B.addApproval('Code-Review', 2)
4838 C.addApproval('Code-Review', 2)
4839 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4840 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4841 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004842 self.waitUntilSettled()
4843
4844 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004845 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4846 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004847 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004848 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004849 repo_messages.reverse()
4850
Paul Belanger174a8272017-03-14 13:20:10 -04004851 self.executor_server.hold_jobs_in_build = False
4852 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004853 self.waitUntilSettled()
4854
4855 return repo_messages
4856
4857 def _test_merge(self, mode):
4858 us_path = os.path.join(
4859 self.upstream_root, 'org/project-%s' % mode)
4860 expected_messages = [
4861 'initial commit',
4862 'add content from fixture',
4863 # the intermediate commits order is nondeterministic
4864 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4865 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4866 ]
4867 result = self._test_project_merge_mode(mode)
4868 self.assertEqual(result[:2], expected_messages[:2])
4869 self.assertEqual(result[-2:], expected_messages[-2:])
4870
4871 def test_project_merge_mode_merge(self):
4872 self._test_merge('merge')
4873
4874 def test_project_merge_mode_merge_resolve(self):
4875 self._test_merge('merge-resolve')
4876
4877 def test_project_merge_mode_cherrypick(self):
4878 expected_messages = [
4879 'initial commit',
4880 'add content from fixture',
4881 'A-1',
4882 'B-1',
4883 'C-1']
4884 result = self._test_project_merge_mode('cherry-pick')
4885 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004886
4887 def test_merge_branch(self):
4888 "Test that the right commits are on alternate branches"
4889 self.create_branch('org/project-merge-branches', 'mp')
4890
Paul Belanger174a8272017-03-14 13:20:10 -04004891 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004892 A = self.fake_gerrit.addFakeChange(
4893 'org/project-merge-branches', 'mp', 'A')
4894 B = self.fake_gerrit.addFakeChange(
4895 'org/project-merge-branches', 'mp', 'B')
4896 C = self.fake_gerrit.addFakeChange(
4897 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004898 A.addApproval('Code-Review', 2)
4899 B.addApproval('Code-Review', 2)
4900 C.addApproval('Code-Review', 2)
4901 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4902 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4903 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004904 self.waitUntilSettled()
4905
Paul Belanger174a8272017-03-14 13:20:10 -04004906 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004907 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004908 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004909 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004910 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004911 self.waitUntilSettled()
4912
4913 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004914 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004915 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4916 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004917 repo = git.Repo(path)
4918
James E. Blair247cab72017-07-20 16:52:36 -07004919 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004920 repo_messages.reverse()
4921 correct_messages = [
4922 'initial commit',
4923 'add content from fixture',
4924 'mp commit',
4925 'A-1', 'B-1', 'C-1']
4926 self.assertEqual(repo_messages, correct_messages)
4927
Paul Belanger174a8272017-03-14 13:20:10 -04004928 self.executor_server.hold_jobs_in_build = False
4929 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004930 self.waitUntilSettled()
4931
4932 def test_merge_multi_branch(self):
4933 "Test that dependent changes on multiple branches are merged"
4934 self.create_branch('org/project-merge-branches', 'mp')
4935
Paul Belanger174a8272017-03-14 13:20:10 -04004936 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004937 A = self.fake_gerrit.addFakeChange(
4938 'org/project-merge-branches', 'master', 'A')
4939 B = self.fake_gerrit.addFakeChange(
4940 'org/project-merge-branches', 'mp', 'B')
4941 C = self.fake_gerrit.addFakeChange(
4942 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004943 A.addApproval('Code-Review', 2)
4944 B.addApproval('Code-Review', 2)
4945 C.addApproval('Code-Review', 2)
4946 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4947 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4948 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004949 self.waitUntilSettled()
4950
4951 job_A = None
4952 for job in self.builds:
4953 if 'project-merge' in job.name:
4954 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004955
James E. Blair2a535672017-04-27 12:03:15 -07004956 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4957 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004958 repo = git.Repo(path)
4959 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004960 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004961 repo_messages.reverse()
4962 correct_messages = [
4963 'initial commit', 'add content from fixture', 'A-1']
4964 self.assertEqual(repo_messages, correct_messages)
4965
Paul Belanger174a8272017-03-14 13:20:10 -04004966 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004967 self.waitUntilSettled()
4968
4969 job_B = None
4970 for job in self.builds:
4971 if 'project-merge' in job.name:
4972 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004973
James E. Blair2a535672017-04-27 12:03:15 -07004974 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4975 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004976 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004977 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004978 repo_messages.reverse()
4979 correct_messages = [
4980 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4981 self.assertEqual(repo_messages, correct_messages)
4982
Paul Belanger174a8272017-03-14 13:20:10 -04004983 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004984 self.waitUntilSettled()
4985
4986 job_C = None
4987 for job in self.builds:
4988 if 'project-merge' in job.name:
4989 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07004990
James E. Blair2a535672017-04-27 12:03:15 -07004991 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4992 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004993 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004994 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004995
4996 repo_messages.reverse()
4997 correct_messages = [
4998 'initial commit', 'add content from fixture',
4999 'A-1', 'C-1']
5000 # Ensure the right commits are in the history for this ref
5001 self.assertEqual(repo_messages, correct_messages)
5002
Paul Belanger174a8272017-03-14 13:20:10 -04005003 self.executor_server.hold_jobs_in_build = False
5004 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005005 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005006
5007
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005008class TestSemaphore(ZuulTestCase):
5009 tenant_config_file = 'config/semaphore/main.yaml'
5010
5011 def test_semaphore_one(self):
5012 "Test semaphores with max=1 (mutex)"
5013 tenant = self.sched.abide.tenants.get('tenant-one')
5014
5015 self.executor_server.hold_jobs_in_build = True
5016
5017 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5018 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5019 self.assertFalse('test-semaphore' in
5020 tenant.semaphore_handler.semaphores)
5021
5022 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5023 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5024 self.waitUntilSettled()
5025
5026 self.assertEqual(len(self.builds), 3)
5027 self.assertEqual(self.builds[0].name, 'project-test1')
5028 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5029 self.assertEqual(self.builds[2].name, 'project-test1')
5030
5031 self.executor_server.release('semaphore-one-test1')
5032 self.waitUntilSettled()
5033
5034 self.assertEqual(len(self.builds), 3)
5035 self.assertEqual(self.builds[0].name, 'project-test1')
5036 self.assertEqual(self.builds[1].name, 'project-test1')
5037 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5038 self.assertTrue('test-semaphore' in
5039 tenant.semaphore_handler.semaphores)
5040
5041 self.executor_server.release('semaphore-one-test2')
5042 self.waitUntilSettled()
5043
5044 self.assertEqual(len(self.builds), 3)
5045 self.assertEqual(self.builds[0].name, 'project-test1')
5046 self.assertEqual(self.builds[1].name, 'project-test1')
5047 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5048 self.assertTrue('test-semaphore' in
5049 tenant.semaphore_handler.semaphores)
5050
5051 self.executor_server.release('semaphore-one-test1')
5052 self.waitUntilSettled()
5053
5054 self.assertEqual(len(self.builds), 3)
5055 self.assertEqual(self.builds[0].name, 'project-test1')
5056 self.assertEqual(self.builds[1].name, 'project-test1')
5057 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5058 self.assertTrue('test-semaphore' in
5059 tenant.semaphore_handler.semaphores)
5060
5061 self.executor_server.release('semaphore-one-test2')
5062 self.waitUntilSettled()
5063
5064 self.assertEqual(len(self.builds), 2)
5065 self.assertEqual(self.builds[0].name, 'project-test1')
5066 self.assertEqual(self.builds[1].name, 'project-test1')
5067 self.assertFalse('test-semaphore' in
5068 tenant.semaphore_handler.semaphores)
5069
5070 self.executor_server.hold_jobs_in_build = False
5071 self.executor_server.release()
5072
5073 self.waitUntilSettled()
5074 self.assertEqual(len(self.builds), 0)
5075
5076 self.assertEqual(A.reported, 1)
5077 self.assertEqual(B.reported, 1)
5078 self.assertFalse('test-semaphore' in
5079 tenant.semaphore_handler.semaphores)
5080
5081 def test_semaphore_two(self):
5082 "Test semaphores with max>1"
5083 tenant = self.sched.abide.tenants.get('tenant-one')
5084
5085 self.executor_server.hold_jobs_in_build = True
5086 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5087 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5088 self.assertFalse('test-semaphore-two' in
5089 tenant.semaphore_handler.semaphores)
5090
5091 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5092 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5093 self.waitUntilSettled()
5094
5095 self.assertEqual(len(self.builds), 4)
5096 self.assertEqual(self.builds[0].name, 'project-test1')
5097 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5098 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5099 self.assertEqual(self.builds[3].name, 'project-test1')
5100 self.assertTrue('test-semaphore-two' in
5101 tenant.semaphore_handler.semaphores)
5102 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5103 'test-semaphore-two', [])), 2)
5104
5105 self.executor_server.release('semaphore-two-test1')
5106 self.waitUntilSettled()
5107
5108 self.assertEqual(len(self.builds), 4)
5109 self.assertEqual(self.builds[0].name, 'project-test1')
5110 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5111 self.assertEqual(self.builds[2].name, 'project-test1')
5112 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5113 self.assertTrue('test-semaphore-two' in
5114 tenant.semaphore_handler.semaphores)
5115 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5116 'test-semaphore-two', [])), 2)
5117
5118 self.executor_server.release('semaphore-two-test2')
5119 self.waitUntilSettled()
5120
5121 self.assertEqual(len(self.builds), 4)
5122 self.assertEqual(self.builds[0].name, 'project-test1')
5123 self.assertEqual(self.builds[1].name, 'project-test1')
5124 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5125 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5126 self.assertTrue('test-semaphore-two' in
5127 tenant.semaphore_handler.semaphores)
5128 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5129 'test-semaphore-two', [])), 2)
5130
5131 self.executor_server.release('semaphore-two-test1')
5132 self.waitUntilSettled()
5133
5134 self.assertEqual(len(self.builds), 3)
5135 self.assertEqual(self.builds[0].name, 'project-test1')
5136 self.assertEqual(self.builds[1].name, 'project-test1')
5137 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5138 self.assertTrue('test-semaphore-two' in
5139 tenant.semaphore_handler.semaphores)
5140 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5141 'test-semaphore-two', [])), 1)
5142
5143 self.executor_server.release('semaphore-two-test2')
5144 self.waitUntilSettled()
5145
5146 self.assertEqual(len(self.builds), 2)
5147 self.assertEqual(self.builds[0].name, 'project-test1')
5148 self.assertEqual(self.builds[1].name, 'project-test1')
5149 self.assertFalse('test-semaphore-two' in
5150 tenant.semaphore_handler.semaphores)
5151
5152 self.executor_server.hold_jobs_in_build = False
5153 self.executor_server.release()
5154
5155 self.waitUntilSettled()
5156 self.assertEqual(len(self.builds), 0)
5157
5158 self.assertEqual(A.reported, 1)
5159 self.assertEqual(B.reported, 1)
5160
5161 def test_semaphore_abandon(self):
5162 "Test abandon with job semaphores"
5163 self.executor_server.hold_jobs_in_build = True
5164 tenant = self.sched.abide.tenants.get('tenant-one')
5165 check_pipeline = tenant.layout.pipelines['check']
5166
5167 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5168 self.assertFalse('test-semaphore' in
5169 tenant.semaphore_handler.semaphores)
5170
5171 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5172 self.waitUntilSettled()
5173
5174 self.assertTrue('test-semaphore' in
5175 tenant.semaphore_handler.semaphores)
5176
5177 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5178 self.waitUntilSettled()
5179
5180 # The check pipeline should be empty
5181 items = check_pipeline.getAllItems()
5182 self.assertEqual(len(items), 0)
5183
5184 # The semaphore should be released
5185 self.assertFalse('test-semaphore' in
5186 tenant.semaphore_handler.semaphores)
5187
5188 self.executor_server.hold_jobs_in_build = False
5189 self.executor_server.release()
5190 self.waitUntilSettled()
5191
Tobias Henkel0f714002017-06-30 23:30:52 +02005192 def test_semaphore_new_patchset(self):
5193 "Test new patchset with job semaphores"
5194 self.executor_server.hold_jobs_in_build = True
5195 tenant = self.sched.abide.tenants.get('tenant-one')
5196 check_pipeline = tenant.layout.pipelines['check']
5197
5198 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5199 self.assertFalse('test-semaphore' in
5200 tenant.semaphore_handler.semaphores)
5201
5202 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5203 self.waitUntilSettled()
5204
5205 self.assertTrue('test-semaphore' in
5206 tenant.semaphore_handler.semaphores)
5207 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5208 self.assertEqual(len(semaphore), 1)
5209
5210 A.addPatchset()
5211 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5212 self.waitUntilSettled()
5213
5214 self.assertTrue('test-semaphore' in
5215 tenant.semaphore_handler.semaphores)
5216 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5217 self.assertEqual(len(semaphore), 1)
5218
5219 items = check_pipeline.getAllItems()
5220 self.assertEqual(items[0].change.number, '1')
5221 self.assertEqual(items[0].change.patchset, '2')
5222 self.assertTrue(items[0].live)
5223
5224 self.executor_server.hold_jobs_in_build = False
5225 self.executor_server.release()
5226 self.waitUntilSettled()
5227
5228 # The semaphore should be released
5229 self.assertFalse('test-semaphore' in
5230 tenant.semaphore_handler.semaphores)
5231
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005232 def test_semaphore_reconfigure(self):
5233 "Test reconfigure with job semaphores"
5234 self.executor_server.hold_jobs_in_build = True
5235 tenant = self.sched.abide.tenants.get('tenant-one')
5236 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5237 self.assertFalse('test-semaphore' in
5238 tenant.semaphore_handler.semaphores)
5239
5240 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5241 self.waitUntilSettled()
5242
5243 self.assertTrue('test-semaphore' in
5244 tenant.semaphore_handler.semaphores)
5245
5246 # reconfigure without layout change
5247 self.sched.reconfigure(self.config)
5248 self.waitUntilSettled()
5249 tenant = self.sched.abide.tenants.get('tenant-one')
5250
5251 # semaphore still must be held
5252 self.assertTrue('test-semaphore' in
5253 tenant.semaphore_handler.semaphores)
5254
5255 self.commitConfigUpdate(
5256 'common-config',
5257 'config/semaphore/zuul-reconfiguration.yaml')
5258 self.sched.reconfigure(self.config)
5259 self.waitUntilSettled()
5260 tenant = self.sched.abide.tenants.get('tenant-one')
5261
5262 self.executor_server.release('project-test1')
5263 self.waitUntilSettled()
5264
5265 # There should be no builds anymore
5266 self.assertEqual(len(self.builds), 0)
5267
5268 # The semaphore should be released
5269 self.assertFalse('test-semaphore' in
5270 tenant.semaphore_handler.semaphores)
5271
5272
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005273class TestSemaphoreMultiTenant(ZuulTestCase):
5274 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5275
5276 def test_semaphore_tenant_isolation(self):
5277 "Test semaphores in multiple tenants"
5278
5279 self.waitUntilSettled()
5280 tenant_one = self.sched.abide.tenants.get('tenant-one')
5281 tenant_two = self.sched.abide.tenants.get('tenant-two')
5282
5283 self.executor_server.hold_jobs_in_build = True
5284 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5285 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5286 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5287 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5288 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5289 self.assertFalse('test-semaphore' in
5290 tenant_one.semaphore_handler.semaphores)
5291 self.assertFalse('test-semaphore' in
5292 tenant_two.semaphore_handler.semaphores)
5293
5294 # add patches to project1 of tenant-one
5295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5296 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5297 self.waitUntilSettled()
5298
5299 # one build of project1-test1 must run
5300 # semaphore of tenant-one must be acquired once
5301 # semaphore of tenant-two must not be acquired
5302 self.assertEqual(len(self.builds), 1)
5303 self.assertEqual(self.builds[0].name, 'project1-test1')
5304 self.assertTrue('test-semaphore' in
5305 tenant_one.semaphore_handler.semaphores)
5306 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5307 'test-semaphore', [])), 1)
5308 self.assertFalse('test-semaphore' in
5309 tenant_two.semaphore_handler.semaphores)
5310
5311 # add patches to project2 of tenant-two
5312 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5313 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5314 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5315 self.waitUntilSettled()
5316
5317 # one build of project1-test1 must run
5318 # two builds of project2-test1 must run
5319 # semaphore of tenant-one must be acquired once
5320 # semaphore of tenant-two must be acquired twice
5321 self.assertEqual(len(self.builds), 3)
5322 self.assertEqual(self.builds[0].name, 'project1-test1')
5323 self.assertEqual(self.builds[1].name, 'project2-test1')
5324 self.assertEqual(self.builds[2].name, 'project2-test1')
5325 self.assertTrue('test-semaphore' in
5326 tenant_one.semaphore_handler.semaphores)
5327 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5328 'test-semaphore', [])), 1)
5329 self.assertTrue('test-semaphore' in
5330 tenant_two.semaphore_handler.semaphores)
5331 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5332 'test-semaphore', [])), 2)
5333
5334 self.executor_server.release('project1-test1')
5335 self.waitUntilSettled()
5336
5337 # one build of project1-test1 must run
5338 # two builds of project2-test1 must run
5339 # semaphore of tenant-one must be acquired once
5340 # semaphore of tenant-two must be acquired twice
5341 self.assertEqual(len(self.builds), 3)
5342 self.assertEqual(self.builds[0].name, 'project2-test1')
5343 self.assertEqual(self.builds[1].name, 'project2-test1')
5344 self.assertEqual(self.builds[2].name, 'project1-test1')
5345 self.assertTrue('test-semaphore' in
5346 tenant_one.semaphore_handler.semaphores)
5347 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5348 'test-semaphore', [])), 1)
5349 self.assertTrue('test-semaphore' in
5350 tenant_two.semaphore_handler.semaphores)
5351 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5352 'test-semaphore', [])), 2)
5353
5354 self.executor_server.release('project2-test1')
5355 self.waitUntilSettled()
5356
5357 # one build of project1-test1 must run
5358 # one build of project2-test1 must run
5359 # semaphore of tenant-one must be acquired once
5360 # semaphore of tenant-two must be acquired once
5361 self.assertEqual(len(self.builds), 2)
5362 self.assertTrue('test-semaphore' in
5363 tenant_one.semaphore_handler.semaphores)
5364 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5365 'test-semaphore', [])), 1)
5366 self.assertTrue('test-semaphore' in
5367 tenant_two.semaphore_handler.semaphores)
5368 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5369 'test-semaphore', [])), 1)
5370
5371 self.executor_server.hold_jobs_in_build = False
5372 self.executor_server.release()
5373
5374 self.waitUntilSettled()
5375
5376 # no build must run
5377 # semaphore of tenant-one must not be acquired
5378 # semaphore of tenant-two must not be acquired
5379 self.assertEqual(len(self.builds), 0)
5380 self.assertFalse('test-semaphore' in
5381 tenant_one.semaphore_handler.semaphores)
5382 self.assertFalse('test-semaphore' in
5383 tenant_two.semaphore_handler.semaphores)
5384
5385 self.assertEqual(A.reported, 1)
5386 self.assertEqual(B.reported, 1)
5387
5388
5389class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005390 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005391 tenant_config_file = 'config/in-repo/main.yaml'
5392
5393 def test_semaphore_in_repo(self):
5394 "Test semaphores in repo config"
5395
5396 # This tests dynamic semaphore handling in project repos. The semaphore
5397 # max value should not be evaluated dynamically but must be updated
5398 # after the change lands.
5399
5400 self.waitUntilSettled()
5401 tenant = self.sched.abide.tenants.get('tenant-one')
5402
5403 in_repo_conf = textwrap.dedent(
5404 """
5405 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005406 name: project-test1
5407
5408 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005409 name: project-test2
5410 semaphore: test-semaphore
5411
5412 - project:
5413 name: org/project
5414 tenant-one-gate:
5415 jobs:
5416 - project-test2
5417
5418 # the max value in dynamic layout must be ignored
5419 - semaphore:
5420 name: test-semaphore
5421 max: 2
5422 """)
5423
5424 in_repo_playbook = textwrap.dedent(
5425 """
5426 - hosts: all
5427 tasks: []
5428 """)
5429
5430 file_dict = {'.zuul.yaml': in_repo_conf,
5431 'playbooks/project-test2.yaml': in_repo_playbook}
5432 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5433 files=file_dict)
5434 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5435 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5436 B.setDependsOn(A, 1)
5437 C.setDependsOn(A, 1)
5438
5439 self.executor_server.hold_jobs_in_build = True
5440
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005441 A.addApproval('Code-Review', 2)
5442 B.addApproval('Code-Review', 2)
5443 C.addApproval('Code-Review', 2)
5444 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5445 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5446 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005447 self.waitUntilSettled()
5448
5449 # check that the layout in a queue item still has max value of 1
5450 # for test-semaphore
5451 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5452 queue = None
5453 for queue_candidate in pipeline.queues:
5454 if queue_candidate.name == 'org/project':
5455 queue = queue_candidate
5456 break
5457 queue_item = queue.queue[0]
5458 item_dynamic_layout = queue_item.current_build_set.layout
5459 dynamic_test_semaphore = \
5460 item_dynamic_layout.semaphores.get('test-semaphore')
5461 self.assertEqual(dynamic_test_semaphore.max, 1)
5462
5463 # one build must be in queue, one semaphores acquired
5464 self.assertEqual(len(self.builds), 1)
5465 self.assertEqual(self.builds[0].name, 'project-test2')
5466 self.assertTrue('test-semaphore' in
5467 tenant.semaphore_handler.semaphores)
5468 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5469 'test-semaphore', [])), 1)
5470
5471 self.executor_server.release('project-test2')
5472 self.waitUntilSettled()
5473
5474 # change A must be merged
5475 self.assertEqual(A.data['status'], 'MERGED')
5476 self.assertEqual(A.reported, 2)
5477
5478 # send change-merged event as the gerrit mock doesn't send it
5479 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5480 self.waitUntilSettled()
5481
5482 # now that change A was merged, the new semaphore max must be effective
5483 tenant = self.sched.abide.tenants.get('tenant-one')
5484 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5485
5486 # two builds must be in queue, two semaphores acquired
5487 self.assertEqual(len(self.builds), 2)
5488 self.assertEqual(self.builds[0].name, 'project-test2')
5489 self.assertEqual(self.builds[1].name, 'project-test2')
5490 self.assertTrue('test-semaphore' in
5491 tenant.semaphore_handler.semaphores)
5492 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5493 'test-semaphore', [])), 2)
5494
5495 self.executor_server.release('project-test2')
5496 self.waitUntilSettled()
5497
5498 self.assertEqual(len(self.builds), 0)
5499 self.assertFalse('test-semaphore' in
5500 tenant.semaphore_handler.semaphores)
5501
5502 self.executor_server.hold_jobs_in_build = False
5503 self.executor_server.release()
5504
5505 self.waitUntilSettled()
5506 self.assertEqual(len(self.builds), 0)
5507
5508 self.assertEqual(A.reported, 2)
5509 self.assertEqual(B.reported, 2)
5510 self.assertEqual(C.reported, 2)