blob: 97d53e0b70e7f05ff6dab01cca5c24f9a081bce5 [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. Blair892cca62017-08-09 11:36:58 -07002821 @simple_layout('layouts/untrusted-secrets.yaml')
2822 def test_untrusted_secrets(self):
2823 "Test untrusted secrets"
2824 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2825 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2826 self.waitUntilSettled()
2827
2828 self.assertHistory([])
2829 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002830 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002831 A.messages[0])
2832
James E. Blaire68d43f2017-04-19 14:03:30 -07002833 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002834 def test_tags(self):
2835 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002836 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2837 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2838 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2839 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2840 self.waitUntilSettled()
2841
James E. Blaire68d43f2017-04-19 14:03:30 -07002842 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002843
2844 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002845 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002846 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002847 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002848
2849 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002850 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002851 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002852
James E. Blair63bb0ef2013-07-29 17:14:51 -07002853 def test_timer(self):
2854 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002855 # This test can not use simple_layout because it must start
2856 # with a configuration which does not include a
2857 # timer-triggered job so that we have an opportunity to set
2858 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002859 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002860 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002861 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002862 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002863
Clark Boylan3ee090a2014-04-03 20:55:09 -07002864 # The pipeline triggers every second, so we should have seen
2865 # several by now.
2866 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002867 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002868
2869 self.assertEqual(len(self.builds), 2)
2870
James E. Blair63bb0ef2013-07-29 17:14:51 -07002871 port = self.webapp.server.socket.getsockname()[1]
2872
Clint Byrum5e729e92017-02-08 06:34:57 -08002873 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002874 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002875 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002876 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002877
Paul Belanger174a8272017-03-14 13:20:10 -04002878 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002879 # Stop queuing timer triggered jobs so that the assertions
2880 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002881 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002882 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002883 self.waitUntilSettled()
2884 # If APScheduler is in mid-event when we remove the job, we
2885 # can end up with one more event firing, so give it an extra
2886 # second to settle.
2887 time.sleep(1)
2888 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002889 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002890 self.waitUntilSettled()
2891
James E. Blair21037782017-07-19 11:56:55 -07002892 self.assertHistory([
2893 dict(name='project-bitrot', result='SUCCESS',
2894 ref='refs/heads/master'),
2895 dict(name='project-bitrot', result='SUCCESS',
2896 ref='refs/heads/stable'),
2897 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002898
2899 data = json.loads(data)
2900 status_jobs = set()
2901 for p in data['pipelines']:
2902 for q in p['change_queues']:
2903 for head in q['heads']:
2904 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002905 for job in change['jobs']:
2906 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07002907 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002908
James E. Blair4f6033c2014-03-27 15:49:09 -07002909 def test_idle(self):
2910 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002911 # This test can not use simple_layout because it must start
2912 # with a configuration which does not include a
2913 # timer-triggered job so that we have an opportunity to set
2914 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002915 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002916
Clark Boylan3ee090a2014-04-03 20:55:09 -07002917 for x in range(1, 3):
2918 # Test that timer triggers periodic jobs even across
2919 # layout config reloads.
2920 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002921 self.commitConfigUpdate('common-config',
2922 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002923 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002924 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002925
Clark Boylan3ee090a2014-04-03 20:55:09 -07002926 # The pipeline triggers every second, so we should have seen
2927 # several by now.
2928 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002929
2930 # Stop queuing timer triggered jobs so that the assertions
2931 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002932 self.commitConfigUpdate('common-config',
2933 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002934 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002935 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07002936 # If APScheduler is in mid-event when we remove the job,
2937 # we can end up with one more event firing, so give it an
2938 # extra second to settle.
2939 time.sleep(1)
2940 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07002941 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08002942 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002943 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002944 self.waitUntilSettled()
2945 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07002946 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07002947
James E. Blaircc324192017-04-19 14:36:08 -07002948 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002949 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002950 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2951 self.waitUntilSettled()
2952
2953 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2954 self.waitUntilSettled()
2955
James E. Blairff80a2f2013-12-27 13:24:06 -08002956 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002957
2958 # A.messages only holds what FakeGerrit places in it. Thus we
2959 # work on the knowledge of what the first message should be as
2960 # it is only configured to go to SMTP.
2961
2962 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002963 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002964 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002965 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002966 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002967 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002968
2969 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002970 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002971 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002972 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002973 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002974 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002975
James E. Blaire5910202013-12-27 09:50:31 -08002976 def test_timer_smtp(self):
2977 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002978 # This test can not use simple_layout because it must start
2979 # with a configuration which does not include a
2980 # timer-triggered job so that we have an opportunity to set
2981 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002982 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002983 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002984 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002985
Clark Boylan3ee090a2014-04-03 20:55:09 -07002986 # The pipeline triggers every second, so we should have seen
2987 # several by now.
2988 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002989 self.waitUntilSettled()
2990
Clark Boylan3ee090a2014-04-03 20:55:09 -07002991 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002992 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002993 self.waitUntilSettled()
2994 self.assertEqual(len(self.history), 2)
2995
James E. Blaire5910202013-12-27 09:50:31 -08002996 self.assertEqual(self.getJobFromHistory(
2997 'project-bitrot-stable-old').result, 'SUCCESS')
2998 self.assertEqual(self.getJobFromHistory(
2999 'project-bitrot-stable-older').result, 'SUCCESS')
3000
James E. Blairff80a2f2013-12-27 13:24:06 -08003001 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003002
3003 # A.messages only holds what FakeGerrit places in it. Thus we
3004 # work on the knowledge of what the first message should be as
3005 # it is only configured to go to SMTP.
3006
3007 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003008 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003009 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003010 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003011 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003012 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003013
Clark Boylan3ee090a2014-04-03 20:55:09 -07003014 # Stop queuing timer triggered jobs and let any that may have
3015 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003016 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003017 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003018 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003019 # If APScheduler is in mid-event when we remove the job, we
3020 # can end up with one more event firing, so give it an extra
3021 # second to settle.
3022 time.sleep(1)
3023 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003024 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003025 self.waitUntilSettled()
3026
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003027 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003028 def test_timer_sshkey(self):
3029 "Test that a periodic job can setup SSH key authentication"
3030 self.worker.hold_jobs_in_build = True
3031 self.config.set('zuul', 'layout_config',
3032 'tests/fixtures/layout-timer.yaml')
3033 self.sched.reconfigure(self.config)
3034 self.registerJobs()
3035
3036 # The pipeline triggers every second, so we should have seen
3037 # several by now.
3038 time.sleep(5)
3039 self.waitUntilSettled()
3040
3041 self.assertEqual(len(self.builds), 2)
3042
3043 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3044 self.assertTrue(os.path.isfile(ssh_wrapper))
3045 with open(ssh_wrapper) as f:
3046 ssh_wrapper_content = f.read()
3047 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3048 # In the unit tests Merger runs in the same process,
3049 # so we see its' environment variables
3050 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3051
3052 self.worker.release('.*')
3053 self.waitUntilSettled()
3054 self.assertEqual(len(self.history), 2)
3055
3056 self.assertEqual(self.getJobFromHistory(
3057 'project-bitrot-stable-old').result, 'SUCCESS')
3058 self.assertEqual(self.getJobFromHistory(
3059 'project-bitrot-stable-older').result, 'SUCCESS')
3060
3061 # Stop queuing timer triggered jobs and let any that may have
3062 # queued through so that end of test assertions pass.
3063 self.config.set('zuul', 'layout_config',
3064 'tests/fixtures/layout-no-timer.yaml')
3065 self.sched.reconfigure(self.config)
3066 self.registerJobs()
3067 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003068 # If APScheduler is in mid-event when we remove the job, we
3069 # can end up with one more event firing, so give it an extra
3070 # second to settle.
3071 time.sleep(1)
3072 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003073 self.worker.release('.*')
3074 self.waitUntilSettled()
3075
James E. Blair91e34592015-07-31 16:45:59 -07003076 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003077 "Test that the RPC client can enqueue a change"
3078 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003079 A.addApproval('Code-Review', 2)
3080 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003081
3082 client = zuul.rpcclient.RPCClient('127.0.0.1',
3083 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003084 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003085 r = client.enqueue(tenant='tenant-one',
3086 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003087 project='org/project',
3088 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003089 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003090 self.waitUntilSettled()
3091 self.assertEqual(self.getJobFromHistory('project-merge').result,
3092 'SUCCESS')
3093 self.assertEqual(self.getJobFromHistory('project-test1').result,
3094 'SUCCESS')
3095 self.assertEqual(self.getJobFromHistory('project-test2').result,
3096 'SUCCESS')
3097 self.assertEqual(A.data['status'], 'MERGED')
3098 self.assertEqual(A.reported, 2)
3099 self.assertEqual(r, True)
3100
James E. Blair91e34592015-07-31 16:45:59 -07003101 def test_client_enqueue_ref(self):
3102 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003103 p = "review.example.com/org/project"
3104 upstream = self.getUpstreamRepos([p])
3105 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3106 A.setMerged()
3107 A_commit = str(upstream[p].commit('master'))
3108 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003109
3110 client = zuul.rpcclient.RPCClient('127.0.0.1',
3111 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003112 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003113 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003114 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003115 pipeline='post',
3116 project='org/project',
3117 trigger='gerrit',
3118 ref='master',
3119 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003120 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003121 self.waitUntilSettled()
3122 job_names = [x.name for x in self.history]
3123 self.assertEqual(len(self.history), 1)
3124 self.assertIn('project-post', job_names)
3125 self.assertEqual(r, True)
3126
James E. Blairad28e912013-11-27 10:43:22 -08003127 def test_client_enqueue_negative(self):
3128 "Test that the RPC client returns errors"
3129 client = zuul.rpcclient.RPCClient('127.0.0.1',
3130 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003131 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003132 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003133 "Invalid tenant"):
3134 r = client.enqueue(tenant='tenant-foo',
3135 pipeline='gate',
3136 project='org/project',
3137 trigger='gerrit',
3138 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003139 self.assertEqual(r, False)
3140
3141 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003142 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003143 r = client.enqueue(tenant='tenant-one',
3144 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003145 project='project-does-not-exist',
3146 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003147 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003148 self.assertEqual(r, False)
3149
3150 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3151 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003152 r = client.enqueue(tenant='tenant-one',
3153 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003154 project='org/project',
3155 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003156 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003157 self.assertEqual(r, False)
3158
3159 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3160 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003161 r = client.enqueue(tenant='tenant-one',
3162 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003163 project='org/project',
3164 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003165 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003166 self.assertEqual(r, False)
3167
3168 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3169 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003170 r = client.enqueue(tenant='tenant-one',
3171 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003172 project='org/project',
3173 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003174 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003175 self.assertEqual(r, False)
3176
3177 self.waitUntilSettled()
3178 self.assertEqual(len(self.history), 0)
3179 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003180
3181 def test_client_promote(self):
3182 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003183 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003184 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3185 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3186 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003187 A.addApproval('Code-Review', 2)
3188 B.addApproval('Code-Review', 2)
3189 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003190
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003191 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3192 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3193 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003194
3195 self.waitUntilSettled()
3196
Paul Belangerbaca3132016-11-04 12:49:54 -04003197 tenant = self.sched.abide.tenants.get('tenant-one')
3198 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003199 enqueue_times = {}
3200 for item in items:
3201 enqueue_times[str(item.change)] = item.enqueue_time
3202
James E. Blair36658cf2013-12-06 17:53:48 -08003203 client = zuul.rpcclient.RPCClient('127.0.0.1',
3204 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003205 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003206 r = client.promote(tenant='tenant-one',
3207 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003208 change_ids=['2,1', '3,1'])
3209
Sean Daguef39b9ca2014-01-10 21:34:35 -05003210 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003211 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003212 for item in items:
3213 self.assertEqual(
3214 enqueue_times[str(item.change)], item.enqueue_time)
3215
James E. Blair78acec92014-02-06 07:11:32 -08003216 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003217 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003218 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003219 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003220 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003221 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003222 self.waitUntilSettled()
3223
3224 self.assertEqual(len(self.builds), 6)
3225 self.assertEqual(self.builds[0].name, 'project-test1')
3226 self.assertEqual(self.builds[1].name, 'project-test2')
3227 self.assertEqual(self.builds[2].name, 'project-test1')
3228 self.assertEqual(self.builds[3].name, 'project-test2')
3229 self.assertEqual(self.builds[4].name, 'project-test1')
3230 self.assertEqual(self.builds[5].name, 'project-test2')
3231
Paul Belangerbaca3132016-11-04 12:49:54 -04003232 self.assertTrue(self.builds[0].hasChanges(B))
3233 self.assertFalse(self.builds[0].hasChanges(A))
3234 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003235
Paul Belangerbaca3132016-11-04 12:49:54 -04003236 self.assertTrue(self.builds[2].hasChanges(B))
3237 self.assertTrue(self.builds[2].hasChanges(C))
3238 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003239
Paul Belangerbaca3132016-11-04 12:49:54 -04003240 self.assertTrue(self.builds[4].hasChanges(B))
3241 self.assertTrue(self.builds[4].hasChanges(C))
3242 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003243
Paul Belanger174a8272017-03-14 13:20:10 -04003244 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003245 self.waitUntilSettled()
3246
3247 self.assertEqual(A.data['status'], 'MERGED')
3248 self.assertEqual(A.reported, 2)
3249 self.assertEqual(B.data['status'], 'MERGED')
3250 self.assertEqual(B.reported, 2)
3251 self.assertEqual(C.data['status'], 'MERGED')
3252 self.assertEqual(C.reported, 2)
3253
James E. Blair36658cf2013-12-06 17:53:48 -08003254 self.assertEqual(r, True)
3255
3256 def test_client_promote_dependent(self):
3257 "Test that the RPC client can promote a dependent change"
3258 # C (depends on B) -> B -> A ; then promote C to get:
3259 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003260 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003261 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3262 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3263 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3264
3265 C.setDependsOn(B, 1)
3266
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003267 A.addApproval('Code-Review', 2)
3268 B.addApproval('Code-Review', 2)
3269 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003270
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003271 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3272 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3273 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003274
3275 self.waitUntilSettled()
3276
3277 client = zuul.rpcclient.RPCClient('127.0.0.1',
3278 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003279 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003280 r = client.promote(tenant='tenant-one',
3281 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003282 change_ids=['3,1'])
3283
James E. Blair78acec92014-02-06 07:11:32 -08003284 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003285 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003286 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003287 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003288 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003289 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003290 self.waitUntilSettled()
3291
3292 self.assertEqual(len(self.builds), 6)
3293 self.assertEqual(self.builds[0].name, 'project-test1')
3294 self.assertEqual(self.builds[1].name, 'project-test2')
3295 self.assertEqual(self.builds[2].name, 'project-test1')
3296 self.assertEqual(self.builds[3].name, 'project-test2')
3297 self.assertEqual(self.builds[4].name, 'project-test1')
3298 self.assertEqual(self.builds[5].name, 'project-test2')
3299
Paul Belangerbaca3132016-11-04 12:49:54 -04003300 self.assertTrue(self.builds[0].hasChanges(B))
3301 self.assertFalse(self.builds[0].hasChanges(A))
3302 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003303
Paul Belangerbaca3132016-11-04 12:49:54 -04003304 self.assertTrue(self.builds[2].hasChanges(B))
3305 self.assertTrue(self.builds[2].hasChanges(C))
3306 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003307
Paul Belangerbaca3132016-11-04 12:49:54 -04003308 self.assertTrue(self.builds[4].hasChanges(B))
3309 self.assertTrue(self.builds[4].hasChanges(C))
3310 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003311
Paul Belanger174a8272017-03-14 13:20:10 -04003312 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003313 self.waitUntilSettled()
3314
3315 self.assertEqual(A.data['status'], 'MERGED')
3316 self.assertEqual(A.reported, 2)
3317 self.assertEqual(B.data['status'], 'MERGED')
3318 self.assertEqual(B.reported, 2)
3319 self.assertEqual(C.data['status'], 'MERGED')
3320 self.assertEqual(C.reported, 2)
3321
James E. Blair36658cf2013-12-06 17:53:48 -08003322 self.assertEqual(r, True)
3323
3324 def test_client_promote_negative(self):
3325 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003326 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003327 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003328 A.addApproval('Code-Review', 2)
3329 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003330 self.waitUntilSettled()
3331
3332 client = zuul.rpcclient.RPCClient('127.0.0.1',
3333 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003334 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003335
3336 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003337 r = client.promote(tenant='tenant-one',
3338 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003339 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003340 self.assertEqual(r, False)
3341
3342 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003343 r = client.promote(tenant='tenant-one',
3344 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003345 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003346 self.assertEqual(r, False)
3347
Paul Belanger174a8272017-03-14 13:20:10 -04003348 self.executor_server.hold_jobs_in_build = False
3349 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003350 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003351
James E. Blaircc324192017-04-19 14:36:08 -07003352 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003353 def test_queue_rate_limiting(self):
3354 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003355 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003356 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3357 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3358 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3359
3360 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003361 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003362
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003363 A.addApproval('Code-Review', 2)
3364 B.addApproval('Code-Review', 2)
3365 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003366
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003367 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3368 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3369 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003370 self.waitUntilSettled()
3371
3372 # Only A and B will have their merge jobs queued because
3373 # window is 2.
3374 self.assertEqual(len(self.builds), 2)
3375 self.assertEqual(self.builds[0].name, 'project-merge')
3376 self.assertEqual(self.builds[1].name, 'project-merge')
3377
James E. Blair732ee862017-04-21 07:49:26 -07003378 # Release the merge jobs one at a time.
3379 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003380 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003381 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003382 self.waitUntilSettled()
3383
3384 # Only A and B will have their test jobs queued because
3385 # window is 2.
3386 self.assertEqual(len(self.builds), 4)
3387 self.assertEqual(self.builds[0].name, 'project-test1')
3388 self.assertEqual(self.builds[1].name, 'project-test2')
3389 self.assertEqual(self.builds[2].name, 'project-test1')
3390 self.assertEqual(self.builds[3].name, 'project-test2')
3391
Paul Belanger174a8272017-03-14 13:20:10 -04003392 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003393 self.waitUntilSettled()
3394
James E. Blaircc324192017-04-19 14:36:08 -07003395 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003396 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003397 # A failed so window is reduced by 1 to 1.
3398 self.assertEqual(queue.window, 1)
3399 self.assertEqual(queue.window_floor, 1)
3400 self.assertEqual(A.data['status'], 'NEW')
3401
3402 # Gate is reset and only B's merge job is queued because
3403 # window shrunk to 1.
3404 self.assertEqual(len(self.builds), 1)
3405 self.assertEqual(self.builds[0].name, 'project-merge')
3406
Paul Belanger174a8272017-03-14 13:20:10 -04003407 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003408 self.waitUntilSettled()
3409
3410 # Only B's test jobs are queued because window is still 1.
3411 self.assertEqual(len(self.builds), 2)
3412 self.assertEqual(self.builds[0].name, 'project-test1')
3413 self.assertEqual(self.builds[1].name, 'project-test2')
3414
Paul Belanger174a8272017-03-14 13:20:10 -04003415 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003416 self.waitUntilSettled()
3417
3418 # B was successfully merged so window is increased to 2.
3419 self.assertEqual(queue.window, 2)
3420 self.assertEqual(queue.window_floor, 1)
3421 self.assertEqual(B.data['status'], 'MERGED')
3422
3423 # Only C is left and its merge job is queued.
3424 self.assertEqual(len(self.builds), 1)
3425 self.assertEqual(self.builds[0].name, 'project-merge')
3426
Paul Belanger174a8272017-03-14 13:20:10 -04003427 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003428 self.waitUntilSettled()
3429
3430 # After successful merge job the test jobs for C are queued.
3431 self.assertEqual(len(self.builds), 2)
3432 self.assertEqual(self.builds[0].name, 'project-test1')
3433 self.assertEqual(self.builds[1].name, 'project-test2')
3434
Paul Belanger174a8272017-03-14 13:20:10 -04003435 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003436 self.waitUntilSettled()
3437
3438 # C successfully merged so window is bumped to 3.
3439 self.assertEqual(queue.window, 3)
3440 self.assertEqual(queue.window_floor, 1)
3441 self.assertEqual(C.data['status'], 'MERGED')
3442
James E. Blaircc324192017-04-19 14:36:08 -07003443 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003444 def test_queue_rate_limiting_dependent(self):
3445 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003446 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003447 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3448 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3449 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3450
3451 B.setDependsOn(A, 1)
3452
Paul Belanger174a8272017-03-14 13:20:10 -04003453 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003454
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003455 A.addApproval('Code-Review', 2)
3456 B.addApproval('Code-Review', 2)
3457 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003458
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003459 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3460 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3461 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003462 self.waitUntilSettled()
3463
3464 # Only A and B will have their merge jobs queued because
3465 # window is 2.
3466 self.assertEqual(len(self.builds), 2)
3467 self.assertEqual(self.builds[0].name, 'project-merge')
3468 self.assertEqual(self.builds[1].name, 'project-merge')
3469
James E. Blairf4a5f022017-04-18 14:01:10 -07003470 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003471
3472 # Only A and B will have their test jobs queued because
3473 # window is 2.
3474 self.assertEqual(len(self.builds), 4)
3475 self.assertEqual(self.builds[0].name, 'project-test1')
3476 self.assertEqual(self.builds[1].name, 'project-test2')
3477 self.assertEqual(self.builds[2].name, 'project-test1')
3478 self.assertEqual(self.builds[3].name, 'project-test2')
3479
Paul Belanger174a8272017-03-14 13:20:10 -04003480 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003481 self.waitUntilSettled()
3482
James E. Blaircc324192017-04-19 14:36:08 -07003483 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003484 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003485 # A failed so window is reduced by 1 to 1.
3486 self.assertEqual(queue.window, 1)
3487 self.assertEqual(queue.window_floor, 1)
3488 self.assertEqual(A.data['status'], 'NEW')
3489 self.assertEqual(B.data['status'], 'NEW')
3490
3491 # Gate is reset and only C's merge job is queued because
3492 # window shrunk to 1 and A and B were dequeued.
3493 self.assertEqual(len(self.builds), 1)
3494 self.assertEqual(self.builds[0].name, 'project-merge')
3495
James E. Blairf4a5f022017-04-18 14:01:10 -07003496 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003497
3498 # Only C's test jobs are queued because window is still 1.
3499 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003500 builds = self.getSortedBuilds()
3501 self.assertEqual(builds[0].name, 'project-test1')
3502 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003503
Paul Belanger174a8272017-03-14 13:20:10 -04003504 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003505 self.waitUntilSettled()
3506
3507 # C was successfully merged so window is increased to 2.
3508 self.assertEqual(queue.window, 2)
3509 self.assertEqual(queue.window_floor, 1)
3510 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003511
3512 def test_worker_update_metadata(self):
3513 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003514 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003515
3516 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003517 A.addApproval('Code-Review', 2)
3518 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003519 self.waitUntilSettled()
3520
Clint Byrumd4de79a2017-05-27 11:39:13 -07003521 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003522
3523 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003524 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003525
3526 start = time.time()
3527 while True:
3528 if time.time() - start > 10:
3529 raise Exception("Timeout waiting for gearman server to report "
3530 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003531 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003532 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003533 break
3534 else:
3535 time.sleep(0)
3536
3537 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003538 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003539
Paul Belanger174a8272017-03-14 13:20:10 -04003540 self.executor_server.hold_jobs_in_build = False
3541 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003542 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003543
James E. Blaircc324192017-04-19 14:36:08 -07003544 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003545 def test_footer_message(self):
3546 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003547 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003548 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003549 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003550 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003551 self.waitUntilSettled()
3552
3553 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003554 B.addApproval('Code-Review', 2)
3555 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003556 self.waitUntilSettled()
3557
3558 self.assertEqual(2, len(self.smtp_messages))
3559
Adam Gandelman82258fd2017-02-07 16:51:58 -08003560 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003561Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003562http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003563
Adam Gandelman82258fd2017-02-07 16:51:58 -08003564 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003565For CI problems and help debugging, contact ci@example.org"""
3566
Adam Gandelman82258fd2017-02-07 16:51:58 -08003567 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3568 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3569 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3570 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003571
Tobias Henkel9842bd72017-05-16 13:40:03 +02003572 @simple_layout('layouts/unmanaged-project.yaml')
3573 def test_unmanaged_project_start_message(self):
3574 "Test start reporting is not done for unmanaged projects."
3575 self.init_repo("org/project", tag='init')
3576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3577 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3578 self.waitUntilSettled()
3579
3580 self.assertEqual(0, len(A.messages))
3581
Clint Byrumb9de8be2017-05-27 13:49:19 -07003582 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003583 def test_merge_failure_reporters(self):
3584 """Check that the config is set up correctly"""
3585
Clint Byrumb9de8be2017-05-27 13:49:19 -07003586 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003587 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003588 "Merge Failed.\n\nThis change or one of its cross-repo "
3589 "dependencies was unable to be automatically merged with the "
3590 "current state of its repository. Please rebase the change and "
3591 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003592 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003593 self.assertEqual(
3594 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003595 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003596
3597 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003598 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003599 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003600 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003601
3602 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003603 tenant.layout.pipelines['check'].merge_failure_actions[0],
3604 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003605
3606 self.assertTrue(
3607 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003608 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003609 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003610 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3611 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003612 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003613 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003614 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003615 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003616 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003617 gerritreporter.GerritReporter) and
3618 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003619 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003620 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003621 )
3622 )
3623
James E. Blairec056492016-07-22 09:45:56 -07003624 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003625 def test_merge_failure_reports(self):
3626 """Check that when a change fails to merge the correct message is sent
3627 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003628 self.updateConfigLayout(
3629 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003630 self.sched.reconfigure(self.config)
3631 self.registerJobs()
3632
3633 # Check a test failure isn't reported to SMTP
3634 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003635 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003636 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003637 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003638 self.waitUntilSettled()
3639
3640 self.assertEqual(3, len(self.history)) # 3 jobs
3641 self.assertEqual(0, len(self.smtp_messages))
3642
3643 # Check a merge failure is reported to SMTP
3644 # B should be merged, but C will conflict with B
3645 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3646 B.addPatchset(['conflict'])
3647 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3648 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003649 B.addApproval('Code-Review', 2)
3650 C.addApproval('Code-Review', 2)
3651 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3652 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003653 self.waitUntilSettled()
3654
3655 self.assertEqual(6, len(self.history)) # A and B jobs
3656 self.assertEqual(1, len(self.smtp_messages))
3657 self.assertEqual('The merge failed! For more information...',
3658 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003659
James E. Blairec056492016-07-22 09:45:56 -07003660 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003661 def test_default_merge_failure_reports(self):
3662 """Check that the default merge failure reports are correct."""
3663
3664 # A should report success, B should report merge failure.
3665 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3666 A.addPatchset(['conflict'])
3667 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3668 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003669 A.addApproval('Code-Review', 2)
3670 B.addApproval('Code-Review', 2)
3671 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3672 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003673 self.waitUntilSettled()
3674
3675 self.assertEqual(3, len(self.history)) # A jobs
3676 self.assertEqual(A.reported, 2)
3677 self.assertEqual(B.reported, 2)
3678 self.assertEqual(A.data['status'], 'MERGED')
3679 self.assertEqual(B.data['status'], 'NEW')
3680 self.assertIn('Build succeeded', A.messages[1])
3681 self.assertIn('Merge Failed', B.messages[1])
3682 self.assertIn('automatically merged', B.messages[1])
3683 self.assertNotIn('logs.example.com', B.messages[1])
3684 self.assertNotIn('SKIPPED', B.messages[1])
3685
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003686 def test_client_get_running_jobs(self):
3687 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003688 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003689 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003690 A.addApproval('Code-Review', 2)
3691 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003692 self.waitUntilSettled()
3693
3694 client = zuul.rpcclient.RPCClient('127.0.0.1',
3695 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003696 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003697
3698 # Wait for gearman server to send the initial workData back to zuul
3699 start = time.time()
3700 while True:
3701 if time.time() - start > 10:
3702 raise Exception("Timeout waiting for gearman server to report "
3703 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003704 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003705 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003706 break
3707 else:
3708 time.sleep(0)
3709
3710 running_items = client.get_running_jobs()
3711
3712 self.assertEqual(1, len(running_items))
3713 running_item = running_items[0]
3714 self.assertEqual([], running_item['failing_reasons'])
3715 self.assertEqual([], running_item['items_behind'])
3716 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003717 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003718 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003719 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003720 self.assertEqual(True, running_item['active'])
3721 self.assertEqual('1,1', running_item['id'])
3722
3723 self.assertEqual(3, len(running_item['jobs']))
3724 for job in running_item['jobs']:
3725 if job['name'] == 'project-merge':
3726 self.assertEqual('project-merge', job['name'])
3727 self.assertEqual('gate', job['pipeline'])
3728 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003729 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003730 'static/stream.html?uuid={uuid}&logfile=console.log'
3731 .format(uuid=job['uuid']), job['url'])
3732 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003733 'finger://{hostname}/{uuid}'.format(
3734 hostname=self.executor_server.hostname,
3735 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003736 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003737 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003738 self.assertEqual(False, job['canceled'])
3739 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003740 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003741 self.assertEqual('gate', job['pipeline'])
3742 break
3743
Paul Belanger174a8272017-03-14 13:20:10 -04003744 self.executor_server.hold_jobs_in_build = False
3745 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003746 self.waitUntilSettled()
3747
3748 running_items = client.get_running_jobs()
3749 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003750
James E. Blairb70e55a2017-04-19 12:57:02 -07003751 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003752 def test_nonvoting_pipeline(self):
3753 "Test that a nonvoting pipeline (experimental) can still report"
3754
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003755 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3756 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003757 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3758 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003759
3760 self.assertEqual(self.getJobFromHistory('project-merge').result,
3761 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003762 self.assertEqual(
3763 self.getJobFromHistory('experimental-project-test').result,
3764 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003765 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003766
3767 def test_crd_gate(self):
3768 "Test cross-repo dependencies"
3769 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3770 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003771 A.addApproval('Code-Review', 2)
3772 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003773
3774 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3775 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3776 AM2.setMerged()
3777 AM1.setMerged()
3778
3779 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3780 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3781 BM2.setMerged()
3782 BM1.setMerged()
3783
3784 # A -> AM1 -> AM2
3785 # B -> BM1 -> BM2
3786 # A Depends-On: B
3787 # M2 is here to make sure it is never queried. If it is, it
3788 # means zuul is walking down the entire history of merged
3789 # changes.
3790
3791 B.setDependsOn(BM1, 1)
3792 BM1.setDependsOn(BM2, 1)
3793
3794 A.setDependsOn(AM1, 1)
3795 AM1.setDependsOn(AM2, 1)
3796
3797 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3798 A.subject, B.data['id'])
3799
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003800 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003801 self.waitUntilSettled()
3802
3803 self.assertEqual(A.data['status'], 'NEW')
3804 self.assertEqual(B.data['status'], 'NEW')
3805
Paul Belanger5dccbe72016-11-14 11:17:37 -05003806 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003807 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003808
Paul Belanger174a8272017-03-14 13:20:10 -04003809 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003810 B.addApproval('Approved', 1)
3811 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003812 self.waitUntilSettled()
3813
Paul Belanger174a8272017-03-14 13:20:10 -04003814 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003815 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003816 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003817 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003818 self.executor_server.hold_jobs_in_build = False
3819 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003820 self.waitUntilSettled()
3821
3822 self.assertEqual(AM2.queried, 0)
3823 self.assertEqual(BM2.queried, 0)
3824 self.assertEqual(A.data['status'], 'MERGED')
3825 self.assertEqual(B.data['status'], 'MERGED')
3826 self.assertEqual(A.reported, 2)
3827 self.assertEqual(B.reported, 2)
3828
Paul Belanger5dccbe72016-11-14 11:17:37 -05003829 changes = self.getJobFromHistory(
3830 'project-merge', 'org/project1').changes
3831 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003832
3833 def test_crd_branch(self):
3834 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003835
3836 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003837 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3838 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3839 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3840 C.data['id'] = B.data['id']
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003841 A.addApproval('Code-Review', 2)
3842 B.addApproval('Code-Review', 2)
3843 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003844
3845 # A Depends-On: B+C
3846 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3847 A.subject, B.data['id'])
3848
Paul Belanger174a8272017-03-14 13:20:10 -04003849 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003850 B.addApproval('Approved', 1)
3851 C.addApproval('Approved', 1)
3852 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003853 self.waitUntilSettled()
3854
Paul Belanger174a8272017-03-14 13:20:10 -04003855 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003856 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003857 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003858 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003859 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003860 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003861 self.executor_server.hold_jobs_in_build = False
3862 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003863 self.waitUntilSettled()
3864
3865 self.assertEqual(A.data['status'], 'MERGED')
3866 self.assertEqual(B.data['status'], 'MERGED')
3867 self.assertEqual(C.data['status'], 'MERGED')
3868 self.assertEqual(A.reported, 2)
3869 self.assertEqual(B.reported, 2)
3870 self.assertEqual(C.reported, 2)
3871
Paul Belanger6379db12016-11-14 13:57:54 -05003872 changes = self.getJobFromHistory(
3873 'project-merge', 'org/project1').changes
3874 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003875
3876 def test_crd_multiline(self):
3877 "Test multiple depends-on lines in commit"
3878 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3879 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3880 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003881 A.addApproval('Code-Review', 2)
3882 B.addApproval('Code-Review', 2)
3883 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003884
3885 # A Depends-On: B+C
3886 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3887 A.subject, B.data['id'], C.data['id'])
3888
Paul Belanger174a8272017-03-14 13:20:10 -04003889 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003890 B.addApproval('Approved', 1)
3891 C.addApproval('Approved', 1)
3892 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003893 self.waitUntilSettled()
3894
Paul Belanger174a8272017-03-14 13:20:10 -04003895 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003896 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003897 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003898 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003899 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003900 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003901 self.executor_server.hold_jobs_in_build = False
3902 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003903 self.waitUntilSettled()
3904
3905 self.assertEqual(A.data['status'], 'MERGED')
3906 self.assertEqual(B.data['status'], 'MERGED')
3907 self.assertEqual(C.data['status'], 'MERGED')
3908 self.assertEqual(A.reported, 2)
3909 self.assertEqual(B.reported, 2)
3910 self.assertEqual(C.reported, 2)
3911
Paul Belanger5dccbe72016-11-14 11:17:37 -05003912 changes = self.getJobFromHistory(
3913 'project-merge', 'org/project1').changes
3914 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003915
3916 def test_crd_unshared_gate(self):
3917 "Test cross-repo dependencies in unshared gate queues"
3918 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3919 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003920 A.addApproval('Code-Review', 2)
3921 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003922
3923 # A Depends-On: B
3924 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3925 A.subject, B.data['id'])
3926
3927 # A and B do not share a queue, make sure that A is unable to
3928 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003929 B.addApproval('Approved', 1)
3930 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003931 self.waitUntilSettled()
3932
3933 self.assertEqual(A.data['status'], 'NEW')
3934 self.assertEqual(B.data['status'], 'NEW')
3935 self.assertEqual(A.reported, 0)
3936 self.assertEqual(B.reported, 0)
3937 self.assertEqual(len(self.history), 0)
3938
3939 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003940 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003941 self.waitUntilSettled()
3942
3943 self.assertEqual(B.data['status'], 'MERGED')
3944 self.assertEqual(B.reported, 2)
3945
3946 # Now that B is merged, A should be able to be enqueued and
3947 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003948 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003949 self.waitUntilSettled()
3950
3951 self.assertEqual(A.data['status'], 'MERGED')
3952 self.assertEqual(A.reported, 2)
3953
James E. Blair96698e22015-04-02 07:48:21 -07003954 def test_crd_gate_reverse(self):
3955 "Test reverse cross-repo dependencies"
3956 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3957 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003958 A.addApproval('Code-Review', 2)
3959 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003960
3961 # A Depends-On: B
3962
3963 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3964 A.subject, B.data['id'])
3965
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003966 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003967 self.waitUntilSettled()
3968
3969 self.assertEqual(A.data['status'], 'NEW')
3970 self.assertEqual(B.data['status'], 'NEW')
3971
Paul Belanger174a8272017-03-14 13:20:10 -04003972 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003973 A.addApproval('Approved', 1)
3974 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003975 self.waitUntilSettled()
3976
Paul Belanger174a8272017-03-14 13:20:10 -04003977 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003978 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003979 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003980 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003981 self.executor_server.hold_jobs_in_build = False
3982 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003983 self.waitUntilSettled()
3984
3985 self.assertEqual(A.data['status'], 'MERGED')
3986 self.assertEqual(B.data['status'], 'MERGED')
3987 self.assertEqual(A.reported, 2)
3988 self.assertEqual(B.reported, 2)
3989
Paul Belanger5dccbe72016-11-14 11:17:37 -05003990 changes = self.getJobFromHistory(
3991 'project-merge', 'org/project1').changes
3992 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003993
James E. Blair5ee24252014-12-30 10:12:29 -08003994 def test_crd_cycle(self):
3995 "Test cross-repo dependency cycles"
3996 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3997 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003998 A.addApproval('Code-Review', 2)
3999 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004000
4001 # A -> B -> A (via commit-depends)
4002
4003 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4004 A.subject, B.data['id'])
4005 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4006 B.subject, A.data['id'])
4007
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004008 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004009 self.waitUntilSettled()
4010
4011 self.assertEqual(A.reported, 0)
4012 self.assertEqual(B.reported, 0)
4013 self.assertEqual(A.data['status'], 'NEW')
4014 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004015
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004016 def test_crd_gate_unknown(self):
4017 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004018 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004019 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4020 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004021 A.addApproval('Code-Review', 2)
4022 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004023
4024 # A Depends-On: B
4025 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4026 A.subject, B.data['id'])
4027
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004028 B.addApproval('Approved', 1)
4029 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004030 self.waitUntilSettled()
4031
4032 # Unknown projects cannot share a queue with any other
4033 # since they don't have common jobs with any other (they have no jobs).
4034 # Changes which depend on unknown project changes
4035 # should not be processed in dependent pipeline
4036 self.assertEqual(A.data['status'], 'NEW')
4037 self.assertEqual(B.data['status'], 'NEW')
4038 self.assertEqual(A.reported, 0)
4039 self.assertEqual(B.reported, 0)
4040 self.assertEqual(len(self.history), 0)
4041
James E. Blair7720b402017-06-07 08:51:49 -07004042 # Simulate change B being gated outside this layout Set the
4043 # change merged before submitting the event so that when the
4044 # event triggers a gerrit query to update the change, we get
4045 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004046 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004047 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004048 self.waitUntilSettled()
4049 self.assertEqual(len(self.history), 0)
4050
4051 # Now that B is merged, A should be able to be enqueued and
4052 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004053 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004054 self.waitUntilSettled()
4055
4056 self.assertEqual(A.data['status'], 'MERGED')
4057 self.assertEqual(A.reported, 2)
4058 self.assertEqual(B.data['status'], 'MERGED')
4059 self.assertEqual(B.reported, 0)
4060
James E. Blairbfb8e042014-12-30 17:01:44 -08004061 def test_crd_check(self):
4062 "Test cross-repo dependencies in independent pipelines"
4063
Paul Belanger174a8272017-03-14 13:20:10 -04004064 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004065 self.gearman_server.hold_jobs_in_queue = True
4066 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4067 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4068
4069 # A Depends-On: B
4070 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4071 A.subject, B.data['id'])
4072
4073 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4074 self.waitUntilSettled()
4075
James E. Blairbfb8e042014-12-30 17:01:44 -08004076 self.gearman_server.hold_jobs_in_queue = False
4077 self.gearman_server.release()
4078 self.waitUntilSettled()
4079
Paul Belanger174a8272017-03-14 13:20:10 -04004080 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004081 self.waitUntilSettled()
4082
James E. Blair2a535672017-04-27 12:03:15 -07004083 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004084
Paul Belanger174a8272017-03-14 13:20:10 -04004085 self.executor_server.hold_jobs_in_build = False
4086 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004087 self.waitUntilSettled()
4088
James E. Blairbfb8e042014-12-30 17:01:44 -08004089 self.assertEqual(A.data['status'], 'NEW')
4090 self.assertEqual(B.data['status'], 'NEW')
4091 self.assertEqual(A.reported, 1)
4092 self.assertEqual(B.reported, 0)
4093
4094 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004095 tenant = self.sched.abide.tenants.get('tenant-one')
4096 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004097
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004098 def test_crd_check_git_depends(self):
4099 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004100 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004101 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4102 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4103
4104 # Add two git-dependent changes and make sure they both report
4105 # success.
4106 B.setDependsOn(A, 1)
4107 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4108 self.waitUntilSettled()
4109 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4110 self.waitUntilSettled()
4111
James E. Blairb8c16472015-05-05 14:55:26 -07004112 self.orderedRelease()
4113 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004114 self.waitUntilSettled()
4115
4116 self.assertEqual(A.data['status'], 'NEW')
4117 self.assertEqual(B.data['status'], 'NEW')
4118 self.assertEqual(A.reported, 1)
4119 self.assertEqual(B.reported, 1)
4120
4121 self.assertEqual(self.history[0].changes, '1,1')
4122 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004123 tenant = self.sched.abide.tenants.get('tenant-one')
4124 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004125
4126 self.assertIn('Build succeeded', A.messages[0])
4127 self.assertIn('Build succeeded', B.messages[0])
4128
4129 def test_crd_check_duplicate(self):
4130 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004131 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004132 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4133 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004134 tenant = self.sched.abide.tenants.get('tenant-one')
4135 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004136
4137 # Add two git-dependent changes...
4138 B.setDependsOn(A, 1)
4139 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4140 self.waitUntilSettled()
4141 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4142
4143 # ...make sure the live one is not duplicated...
4144 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4145 self.waitUntilSettled()
4146 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4147
4148 # ...but the non-live one is able to be.
4149 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4150 self.waitUntilSettled()
4151 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4152
Clark Boylandd849822015-03-02 12:38:14 -08004153 # Release jobs in order to avoid races with change A jobs
4154 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004155 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004156 self.executor_server.hold_jobs_in_build = False
4157 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004158 self.waitUntilSettled()
4159
4160 self.assertEqual(A.data['status'], 'NEW')
4161 self.assertEqual(B.data['status'], 'NEW')
4162 self.assertEqual(A.reported, 1)
4163 self.assertEqual(B.reported, 1)
4164
4165 self.assertEqual(self.history[0].changes, '1,1 2,1')
4166 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004167 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004168
4169 self.assertIn('Build succeeded', A.messages[0])
4170 self.assertIn('Build succeeded', B.messages[0])
4171
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004172 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004173 "Test cross-repo dependencies re-enqueued in independent pipelines"
4174
4175 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004176 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4177 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004178
4179 # A Depends-On: B
4180 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4181 A.subject, B.data['id'])
4182
4183 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4184 self.waitUntilSettled()
4185
4186 self.sched.reconfigure(self.config)
4187
4188 # Make sure the items still share a change queue, and the
4189 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004190 tenant = self.sched.abide.tenants.get('tenant-one')
4191 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4192 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004193 first_item = queue.queue[0]
4194 for item in queue.queue:
4195 self.assertEqual(item.queue, first_item.queue)
4196 self.assertFalse(first_item.live)
4197 self.assertTrue(queue.queue[1].live)
4198
4199 self.gearman_server.hold_jobs_in_queue = False
4200 self.gearman_server.release()
4201 self.waitUntilSettled()
4202
4203 self.assertEqual(A.data['status'], 'NEW')
4204 self.assertEqual(B.data['status'], 'NEW')
4205 self.assertEqual(A.reported, 1)
4206 self.assertEqual(B.reported, 0)
4207
4208 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004209 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004210
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004211 def test_crd_check_reconfiguration(self):
4212 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4213
4214 def test_crd_undefined_project(self):
4215 """Test that undefined projects in dependencies are handled for
4216 independent pipelines"""
4217 # It's a hack for fake gerrit,
4218 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004219 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004220 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4221
Jesse Keatingea68ab42017-04-19 15:03:30 -07004222 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004223 def test_crd_check_ignore_dependencies(self):
4224 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004225
4226 self.gearman_server.hold_jobs_in_queue = True
4227 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4228 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4229 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4230
4231 # A Depends-On: B
4232 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4233 A.subject, B.data['id'])
4234 # C git-depends on B
4235 C.setDependsOn(B, 1)
4236 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4237 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4238 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4239 self.waitUntilSettled()
4240
4241 # Make sure none of the items share a change queue, and all
4242 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004243 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004244 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004245 self.assertEqual(len(check_pipeline.queues), 3)
4246 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4247 for item in check_pipeline.getAllItems():
4248 self.assertTrue(item.live)
4249
4250 self.gearman_server.hold_jobs_in_queue = False
4251 self.gearman_server.release()
4252 self.waitUntilSettled()
4253
4254 self.assertEqual(A.data['status'], 'NEW')
4255 self.assertEqual(B.data['status'], 'NEW')
4256 self.assertEqual(C.data['status'], 'NEW')
4257 self.assertEqual(A.reported, 1)
4258 self.assertEqual(B.reported, 1)
4259 self.assertEqual(C.reported, 1)
4260
4261 # Each job should have tested exactly one change
4262 for job in self.history:
4263 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004264
James E. Blair7cb84542017-04-19 13:35:05 -07004265 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004266 def test_crd_check_transitive(self):
4267 "Test transitive cross-repo dependencies"
4268 # Specifically, if A -> B -> C, and C gets a new patchset and
4269 # A gets a new patchset, ensure the test of A,2 includes B,1
4270 # and C,2 (not C,1 which would indicate stale data in the
4271 # cache for B).
4272 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4273 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4274 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4275
4276 # A Depends-On: B
4277 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4278 A.subject, B.data['id'])
4279
4280 # B Depends-On: C
4281 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4282 B.subject, C.data['id'])
4283
4284 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4285 self.waitUntilSettled()
4286 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4287
4288 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4289 self.waitUntilSettled()
4290 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4291
4292 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4293 self.waitUntilSettled()
4294 self.assertEqual(self.history[-1].changes, '3,1')
4295
4296 C.addPatchset()
4297 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4298 self.waitUntilSettled()
4299 self.assertEqual(self.history[-1].changes, '3,2')
4300
4301 A.addPatchset()
4302 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4303 self.waitUntilSettled()
4304 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004305
James E. Blaira8b90b32016-08-24 15:18:50 -07004306 def test_crd_check_unknown(self):
4307 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004308 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004309 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4310 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4311 # A Depends-On: B
4312 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4313 A.subject, B.data['id'])
4314
4315 # Make sure zuul has seen an event on B.
4316 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4317 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4318 self.waitUntilSettled()
4319
4320 self.assertEqual(A.data['status'], 'NEW')
4321 self.assertEqual(A.reported, 1)
4322 self.assertEqual(B.data['status'], 'NEW')
4323 self.assertEqual(B.reported, 0)
4324
James E. Blair92464a22016-04-05 10:21:26 -07004325 def test_crd_cycle_join(self):
4326 "Test an updated change creates a cycle"
4327 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4328
4329 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4330 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004331 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004332
4333 # Create B->A
4334 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4335 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4336 B.subject, A.data['id'])
4337 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4338 self.waitUntilSettled()
4339
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004340 # Dep is there so zuul should have reported on B
4341 self.assertEqual(B.reported, 1)
4342
James E. Blair92464a22016-04-05 10:21:26 -07004343 # Update A to add A->B (a cycle).
4344 A.addPatchset()
4345 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4346 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004347 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4348 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004349
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004350 # Dependency cycle injected so zuul should not have reported again on A
4351 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004352
4353 # Now if we update B to remove the depends-on, everything
4354 # should be okay. B; A->B
4355
4356 B.addPatchset()
4357 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004358 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4359 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004360
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004361 # Cycle was removed so now zuul should have reported again on A
4362 self.assertEqual(A.reported, 2)
4363
4364 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4365 self.waitUntilSettled()
4366 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004367
Jesse Keatingcc67b122017-04-19 14:54:57 -07004368 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004369 def test_disable_at(self):
4370 "Test a pipeline will only report to the disabled trigger when failing"
4371
Jesse Keatingcc67b122017-04-19 14:54:57 -07004372 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004373 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004374 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004375 0, tenant.layout.pipelines['check']._consecutive_failures)
4376 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004377
4378 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4379 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4380 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4381 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4382 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4383 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4384 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4385 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4386 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4387 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4388 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4389
Paul Belanger174a8272017-03-14 13:20:10 -04004390 self.executor_server.failJob('project-test1', A)
4391 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004392 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004393 self.executor_server.failJob('project-test1', D)
4394 self.executor_server.failJob('project-test1', E)
4395 self.executor_server.failJob('project-test1', F)
4396 self.executor_server.failJob('project-test1', G)
4397 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004398 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004399 self.executor_server.failJob('project-test1', J)
4400 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004401
4402 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4403 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4404 self.waitUntilSettled()
4405
4406 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004407 2, tenant.layout.pipelines['check']._consecutive_failures)
4408 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004409
4410 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4411 self.waitUntilSettled()
4412
4413 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004414 0, tenant.layout.pipelines['check']._consecutive_failures)
4415 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004416
4417 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4418 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4419 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4420 self.waitUntilSettled()
4421
4422 # We should be disabled now
4423 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004424 3, tenant.layout.pipelines['check']._consecutive_failures)
4425 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004426
4427 # We need to wait between each of these patches to make sure the
4428 # smtp messages come back in an expected order
4429 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4430 self.waitUntilSettled()
4431 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4432 self.waitUntilSettled()
4433 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4434 self.waitUntilSettled()
4435
4436 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4437 # leaving a message on each change
4438 self.assertEqual(1, len(A.messages))
4439 self.assertIn('Build failed.', A.messages[0])
4440 self.assertEqual(1, len(B.messages))
4441 self.assertIn('Build failed.', B.messages[0])
4442 self.assertEqual(1, len(C.messages))
4443 self.assertIn('Build succeeded.', C.messages[0])
4444 self.assertEqual(1, len(D.messages))
4445 self.assertIn('Build failed.', D.messages[0])
4446 self.assertEqual(1, len(E.messages))
4447 self.assertIn('Build failed.', E.messages[0])
4448 self.assertEqual(1, len(F.messages))
4449 self.assertIn('Build failed.', F.messages[0])
4450
4451 # The last 3 (GHI) would have only reported via smtp.
4452 self.assertEqual(3, len(self.smtp_messages))
4453 self.assertEqual(0, len(G.messages))
4454 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004455 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004456 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004457 self.assertEqual(0, len(H.messages))
4458 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004459 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004460 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004461 self.assertEqual(0, len(I.messages))
4462 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004463 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004464 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004465
4466 # Now reload the configuration (simulate a HUP) to check the pipeline
4467 # comes out of disabled
4468 self.sched.reconfigure(self.config)
4469
Jesse Keatingcc67b122017-04-19 14:54:57 -07004470 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004471
4472 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004473 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004474 0, tenant.layout.pipelines['check']._consecutive_failures)
4475 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004476
4477 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4478 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4479 self.waitUntilSettled()
4480
4481 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004482 2, tenant.layout.pipelines['check']._consecutive_failures)
4483 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004484
4485 # J and K went back to gerrit
4486 self.assertEqual(1, len(J.messages))
4487 self.assertIn('Build failed.', J.messages[0])
4488 self.assertEqual(1, len(K.messages))
4489 self.assertIn('Build failed.', K.messages[0])
4490 # No more messages reported via smtp
4491 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004492
James E. Blaircc020532017-04-19 13:04:44 -07004493 @simple_layout('layouts/one-job-project.yaml')
4494 def test_one_job_project(self):
4495 "Test that queueing works with one job"
4496 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4497 'master', 'A')
4498 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4499 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004500 A.addApproval('Code-Review', 2)
4501 B.addApproval('Code-Review', 2)
4502 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4503 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004504 self.waitUntilSettled()
4505
4506 self.assertEqual(A.data['status'], 'MERGED')
4507 self.assertEqual(A.reported, 2)
4508 self.assertEqual(B.data['status'], 'MERGED')
4509 self.assertEqual(B.reported, 2)
4510
Paul Belanger71d98172016-11-08 10:56:31 -05004511 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004512 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004513
Paul Belanger174a8272017-03-14 13:20:10 -04004514 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004515 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4516 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4517 self.waitUntilSettled()
4518
Paul Belanger174a8272017-03-14 13:20:10 -04004519 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004520 self.waitUntilSettled()
4521
4522 self.assertEqual(len(self.builds), 2)
4523 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004524 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004525 self.waitUntilSettled()
4526
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004527 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004528 self.assertEqual(len(self.builds), 1,
4529 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004530 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004531 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004532 self.waitUntilSettled()
4533
Paul Belanger174a8272017-03-14 13:20:10 -04004534 self.executor_server.hold_jobs_in_build = False
4535 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004536 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004537 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004538 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4539 self.assertEqual(A.reported, 1)
4540 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004541
James E. Blair15be0e12017-01-03 13:45:20 -08004542 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004543 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004544
4545 self.fake_nodepool.paused = True
4546 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004547 A.addApproval('Code-Review', 2)
4548 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004549 self.waitUntilSettled()
4550
4551 self.zk.client.stop()
4552 self.zk.client.start()
4553 self.fake_nodepool.paused = False
4554 self.waitUntilSettled()
4555
4556 self.assertEqual(A.data['status'], 'MERGED')
4557 self.assertEqual(A.reported, 2)
4558
James E. Blair6ab79e02017-01-06 10:10:17 -08004559 def test_nodepool_failure(self):
4560 "Test that jobs are reported after a nodepool failure"
4561
4562 self.fake_nodepool.paused = True
4563 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004564 A.addApproval('Code-Review', 2)
4565 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004566 self.waitUntilSettled()
4567
4568 req = self.fake_nodepool.getNodeRequests()[0]
4569 self.fake_nodepool.addFailRequest(req)
4570
4571 self.fake_nodepool.paused = False
4572 self.waitUntilSettled()
4573
4574 self.assertEqual(A.data['status'], 'NEW')
4575 self.assertEqual(A.reported, 2)
4576 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4577 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4578 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4579
Arieb6f068c2016-10-09 13:11:06 +03004580
James E. Blaira002b032017-04-18 10:35:48 -07004581class TestExecutor(ZuulTestCase):
4582 tenant_config_file = 'config/single-tenant/main.yaml'
4583
4584 def assertFinalState(self):
4585 # In this test, we expect to shut down in a non-final state,
4586 # so skip these checks.
4587 pass
4588
4589 def assertCleanShutdown(self):
4590 self.log.debug("Assert clean shutdown")
4591
4592 # After shutdown, make sure no jobs are running
4593 self.assertEqual({}, self.executor_server.job_workers)
4594
4595 # Make sure that git.Repo objects have been garbage collected.
4596 repos = []
4597 gc.collect()
4598 for obj in gc.get_objects():
4599 if isinstance(obj, git.Repo):
4600 self.log.debug("Leaked git repo object: %s" % repr(obj))
4601 repos.append(obj)
4602 self.assertEqual(len(repos), 0)
4603
4604 def test_executor_shutdown(self):
4605 "Test that the executor can shut down with jobs running"
4606
4607 self.executor_server.hold_jobs_in_build = True
4608 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004609 A.addApproval('Code-Review', 2)
4610 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004611 self.waitUntilSettled()
4612
4613
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004614class TestDependencyGraph(ZuulTestCase):
4615 tenant_config_file = 'config/dependency-graph/main.yaml'
4616
4617 def test_dependeny_graph_dispatch_jobs_once(self):
4618 "Test a job in a dependency graph is queued only once"
4619 # Job dependencies, starting with A
4620 # A
4621 # / \
4622 # B C
4623 # / \ / \
4624 # D F E
4625 # |
4626 # G
4627
4628 self.executor_server.hold_jobs_in_build = True
4629 change = self.fake_gerrit.addFakeChange(
4630 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004631 change.addApproval('Code-Review', 2)
4632 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004633
4634 self.waitUntilSettled()
4635 self.assertEqual([b.name for b in self.builds], ['A'])
4636
4637 self.executor_server.release('A')
4638 self.waitUntilSettled()
4639 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4640
4641 self.executor_server.release('B')
4642 self.waitUntilSettled()
4643 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4644
4645 self.executor_server.release('D')
4646 self.waitUntilSettled()
4647 self.assertEqual([b.name for b in self.builds], ['C'])
4648
4649 self.executor_server.release('C')
4650 self.waitUntilSettled()
4651 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4652
4653 self.executor_server.release('F')
4654 self.waitUntilSettled()
4655 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4656
4657 self.executor_server.release('G')
4658 self.waitUntilSettled()
4659 self.assertEqual([b.name for b in self.builds], ['E'])
4660
4661 self.executor_server.release('E')
4662 self.waitUntilSettled()
4663 self.assertEqual(len(self.builds), 0)
4664
4665 self.executor_server.hold_jobs_in_build = False
4666 self.executor_server.release()
4667 self.waitUntilSettled()
4668
4669 self.assertEqual(len(self.builds), 0)
4670 self.assertEqual(len(self.history), 7)
4671
4672 self.assertEqual(change.data['status'], 'MERGED')
4673 self.assertEqual(change.reported, 2)
4674
4675 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4676 "Test that a job waits till all dependencies are successful"
4677 # Job dependencies, starting with A
4678 # A
4679 # / \
4680 # B C*
4681 # / \ / \
4682 # D F E
4683 # |
4684 # G
4685
4686 self.executor_server.hold_jobs_in_build = True
4687 change = self.fake_gerrit.addFakeChange(
4688 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004689 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004690
4691 self.executor_server.failJob('C', change)
4692
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004693 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004694
4695 self.waitUntilSettled()
4696 self.assertEqual([b.name for b in self.builds], ['A'])
4697
4698 self.executor_server.release('A')
4699 self.waitUntilSettled()
4700 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4701
4702 self.executor_server.release('B')
4703 self.waitUntilSettled()
4704 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4705
4706 self.executor_server.release('D')
4707 self.waitUntilSettled()
4708 self.assertEqual([b.name for b in self.builds], ['C'])
4709
4710 self.executor_server.release('C')
4711 self.waitUntilSettled()
4712 self.assertEqual(len(self.builds), 0)
4713
4714 self.executor_server.hold_jobs_in_build = False
4715 self.executor_server.release()
4716 self.waitUntilSettled()
4717
4718 self.assertEqual(len(self.builds), 0)
4719 self.assertEqual(len(self.history), 4)
4720
4721 self.assertEqual(change.data['status'], 'NEW')
4722 self.assertEqual(change.reported, 2)
4723
4724
Arieb6f068c2016-10-09 13:11:06 +03004725class TestDuplicatePipeline(ZuulTestCase):
4726 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4727
4728 def test_duplicate_pipelines(self):
4729 "Test that a change matching multiple pipelines works"
4730
4731 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4732 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4733 self.waitUntilSettled()
4734
4735 self.assertHistory([
4736 dict(name='project-test1', result='SUCCESS', changes='1,1',
4737 pipeline='dup1'),
4738 dict(name='project-test1', result='SUCCESS', changes='1,1',
4739 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004740 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004741
4742 self.assertEqual(len(A.messages), 2)
4743
Paul Belangera46a3742016-11-09 14:23:03 -05004744 if 'dup1' in A.messages[0]:
4745 self.assertIn('dup1', A.messages[0])
4746 self.assertNotIn('dup2', A.messages[0])
4747 self.assertIn('project-test1', A.messages[0])
4748 self.assertIn('dup2', A.messages[1])
4749 self.assertNotIn('dup1', A.messages[1])
4750 self.assertIn('project-test1', A.messages[1])
4751 else:
4752 self.assertIn('dup1', A.messages[1])
4753 self.assertNotIn('dup2', A.messages[1])
4754 self.assertIn('project-test1', A.messages[1])
4755 self.assertIn('dup2', A.messages[0])
4756 self.assertNotIn('dup1', A.messages[0])
4757 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004758
4759
Paul Belanger86085b32016-11-03 12:48:57 -04004760class TestSchedulerTemplatedProject(ZuulTestCase):
4761 tenant_config_file = 'config/templated-project/main.yaml'
4762
Paul Belanger174a8272017-03-14 13:20:10 -04004763 def test_job_from_templates_executed(self):
4764 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004765
4766 A = self.fake_gerrit.addFakeChange(
4767 'org/templated-project', 'master', 'A')
4768 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4769 self.waitUntilSettled()
4770
4771 self.assertEqual(self.getJobFromHistory('project-test1').result,
4772 'SUCCESS')
4773 self.assertEqual(self.getJobFromHistory('project-test2').result,
4774 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004775
4776 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004777 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004778
4779 A = self.fake_gerrit.addFakeChange(
4780 'org/layered-project', 'master', 'A')
4781 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4782 self.waitUntilSettled()
4783
4784 self.assertEqual(self.getJobFromHistory('project-test1').result,
4785 'SUCCESS')
4786 self.assertEqual(self.getJobFromHistory('project-test2').result,
4787 'SUCCESS')
4788 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4789 ).result, 'SUCCESS')
4790 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4791 ).result, 'SUCCESS')
4792 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4793 ).result, 'SUCCESS')
4794 self.assertEqual(self.getJobFromHistory('project-test6').result,
4795 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004796
4797
4798class TestSchedulerSuccessURL(ZuulTestCase):
4799 tenant_config_file = 'config/success-url/main.yaml'
4800
4801 def test_success_url(self):
4802 "Ensure bad build params are ignored"
4803 self.sched.reconfigure(self.config)
4804 self.init_repo('org/docs')
4805
4806 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4807 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4808 self.waitUntilSettled()
4809
4810 # Both builds ran: docs-draft-test + docs-draft-test2
4811 self.assertEqual(len(self.history), 2)
4812
4813 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004814 for build in self.history:
4815 if build.name == 'docs-draft-test':
4816 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06004817 elif build.name == 'docs-draft-test2':
4818 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08004819
4820 # Two msgs: 'Starting...' + results
4821 self.assertEqual(len(self.smtp_messages), 2)
4822 body = self.smtp_messages[1]['body'].splitlines()
4823 self.assertEqual('Build succeeded.', body[0])
4824
4825 self.assertIn(
4826 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4827 'docs-draft-test/%s/publish-docs/' % uuid,
4828 body[2])
4829
Paul Belanger174a8272017-03-14 13:20:10 -04004830 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004831 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05004832 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
4833 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06004834 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08004835 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004836
4837
Adam Gandelman4da00f62016-12-09 15:47:33 -08004838class TestSchedulerMerges(ZuulTestCase):
4839 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004840
4841 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004842 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004843 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004844 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004845 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4846 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4847 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004848 A.addApproval('Code-Review', 2)
4849 B.addApproval('Code-Review', 2)
4850 C.addApproval('Code-Review', 2)
4851 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4852 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4853 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08004854 self.waitUntilSettled()
4855
4856 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07004857 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4858 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004859 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004860 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08004861 repo_messages.reverse()
4862
Paul Belanger174a8272017-03-14 13:20:10 -04004863 self.executor_server.hold_jobs_in_build = False
4864 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004865 self.waitUntilSettled()
4866
4867 return repo_messages
4868
4869 def _test_merge(self, mode):
4870 us_path = os.path.join(
4871 self.upstream_root, 'org/project-%s' % mode)
4872 expected_messages = [
4873 'initial commit',
4874 'add content from fixture',
4875 # the intermediate commits order is nondeterministic
4876 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4877 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4878 ]
4879 result = self._test_project_merge_mode(mode)
4880 self.assertEqual(result[:2], expected_messages[:2])
4881 self.assertEqual(result[-2:], expected_messages[-2:])
4882
4883 def test_project_merge_mode_merge(self):
4884 self._test_merge('merge')
4885
4886 def test_project_merge_mode_merge_resolve(self):
4887 self._test_merge('merge-resolve')
4888
4889 def test_project_merge_mode_cherrypick(self):
4890 expected_messages = [
4891 'initial commit',
4892 'add content from fixture',
4893 'A-1',
4894 'B-1',
4895 'C-1']
4896 result = self._test_project_merge_mode('cherry-pick')
4897 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004898
4899 def test_merge_branch(self):
4900 "Test that the right commits are on alternate branches"
4901 self.create_branch('org/project-merge-branches', 'mp')
4902
Paul Belanger174a8272017-03-14 13:20:10 -04004903 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004904 A = self.fake_gerrit.addFakeChange(
4905 'org/project-merge-branches', 'mp', 'A')
4906 B = self.fake_gerrit.addFakeChange(
4907 'org/project-merge-branches', 'mp', 'B')
4908 C = self.fake_gerrit.addFakeChange(
4909 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004910 A.addApproval('Code-Review', 2)
4911 B.addApproval('Code-Review', 2)
4912 C.addApproval('Code-Review', 2)
4913 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4914 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4915 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004916 self.waitUntilSettled()
4917
Paul Belanger174a8272017-03-14 13:20:10 -04004918 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004919 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004920 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004921 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004922 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004923 self.waitUntilSettled()
4924
4925 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07004926 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07004927 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4928 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004929 repo = git.Repo(path)
4930
James E. Blair247cab72017-07-20 16:52:36 -07004931 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004932 repo_messages.reverse()
4933 correct_messages = [
4934 'initial commit',
4935 'add content from fixture',
4936 'mp commit',
4937 'A-1', 'B-1', 'C-1']
4938 self.assertEqual(repo_messages, correct_messages)
4939
Paul Belanger174a8272017-03-14 13:20:10 -04004940 self.executor_server.hold_jobs_in_build = False
4941 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004942 self.waitUntilSettled()
4943
4944 def test_merge_multi_branch(self):
4945 "Test that dependent changes on multiple branches are merged"
4946 self.create_branch('org/project-merge-branches', 'mp')
4947
Paul Belanger174a8272017-03-14 13:20:10 -04004948 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004949 A = self.fake_gerrit.addFakeChange(
4950 'org/project-merge-branches', 'master', 'A')
4951 B = self.fake_gerrit.addFakeChange(
4952 'org/project-merge-branches', 'mp', 'B')
4953 C = self.fake_gerrit.addFakeChange(
4954 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004955 A.addApproval('Code-Review', 2)
4956 B.addApproval('Code-Review', 2)
4957 C.addApproval('Code-Review', 2)
4958 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4959 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
4960 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08004961 self.waitUntilSettled()
4962
4963 job_A = None
4964 for job in self.builds:
4965 if 'project-merge' in job.name:
4966 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004967
James E. Blair2a535672017-04-27 12:03:15 -07004968 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4969 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004970 repo = git.Repo(path)
4971 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07004972 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004973 repo_messages.reverse()
4974 correct_messages = [
4975 'initial commit', 'add content from fixture', 'A-1']
4976 self.assertEqual(repo_messages, correct_messages)
4977
Paul Belanger174a8272017-03-14 13:20:10 -04004978 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004979 self.waitUntilSettled()
4980
4981 job_B = None
4982 for job in self.builds:
4983 if 'project-merge' in job.name:
4984 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08004985
James E. Blair2a535672017-04-27 12:03:15 -07004986 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4987 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004988 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07004989 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08004990 repo_messages.reverse()
4991 correct_messages = [
4992 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4993 self.assertEqual(repo_messages, correct_messages)
4994
Paul Belanger174a8272017-03-14 13:20:10 -04004995 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004996 self.waitUntilSettled()
4997
4998 job_C = None
4999 for job in self.builds:
5000 if 'project-merge' in job.name:
5001 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005002
James E. Blair2a535672017-04-27 12:03:15 -07005003 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5004 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005005 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005006 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005007
5008 repo_messages.reverse()
5009 correct_messages = [
5010 'initial commit', 'add content from fixture',
5011 'A-1', 'C-1']
5012 # Ensure the right commits are in the history for this ref
5013 self.assertEqual(repo_messages, correct_messages)
5014
Paul Belanger174a8272017-03-14 13:20:10 -04005015 self.executor_server.hold_jobs_in_build = False
5016 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005017 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005018
5019
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005020class TestSemaphore(ZuulTestCase):
5021 tenant_config_file = 'config/semaphore/main.yaml'
5022
5023 def test_semaphore_one(self):
5024 "Test semaphores with max=1 (mutex)"
5025 tenant = self.sched.abide.tenants.get('tenant-one')
5026
5027 self.executor_server.hold_jobs_in_build = True
5028
5029 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5030 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5031 self.assertFalse('test-semaphore' in
5032 tenant.semaphore_handler.semaphores)
5033
5034 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5035 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5036 self.waitUntilSettled()
5037
5038 self.assertEqual(len(self.builds), 3)
5039 self.assertEqual(self.builds[0].name, 'project-test1')
5040 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5041 self.assertEqual(self.builds[2].name, 'project-test1')
5042
5043 self.executor_server.release('semaphore-one-test1')
5044 self.waitUntilSettled()
5045
5046 self.assertEqual(len(self.builds), 3)
5047 self.assertEqual(self.builds[0].name, 'project-test1')
5048 self.assertEqual(self.builds[1].name, 'project-test1')
5049 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5050 self.assertTrue('test-semaphore' in
5051 tenant.semaphore_handler.semaphores)
5052
5053 self.executor_server.release('semaphore-one-test2')
5054 self.waitUntilSettled()
5055
5056 self.assertEqual(len(self.builds), 3)
5057 self.assertEqual(self.builds[0].name, 'project-test1')
5058 self.assertEqual(self.builds[1].name, 'project-test1')
5059 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5060 self.assertTrue('test-semaphore' in
5061 tenant.semaphore_handler.semaphores)
5062
5063 self.executor_server.release('semaphore-one-test1')
5064 self.waitUntilSettled()
5065
5066 self.assertEqual(len(self.builds), 3)
5067 self.assertEqual(self.builds[0].name, 'project-test1')
5068 self.assertEqual(self.builds[1].name, 'project-test1')
5069 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5070 self.assertTrue('test-semaphore' in
5071 tenant.semaphore_handler.semaphores)
5072
5073 self.executor_server.release('semaphore-one-test2')
5074 self.waitUntilSettled()
5075
5076 self.assertEqual(len(self.builds), 2)
5077 self.assertEqual(self.builds[0].name, 'project-test1')
5078 self.assertEqual(self.builds[1].name, 'project-test1')
5079 self.assertFalse('test-semaphore' in
5080 tenant.semaphore_handler.semaphores)
5081
5082 self.executor_server.hold_jobs_in_build = False
5083 self.executor_server.release()
5084
5085 self.waitUntilSettled()
5086 self.assertEqual(len(self.builds), 0)
5087
5088 self.assertEqual(A.reported, 1)
5089 self.assertEqual(B.reported, 1)
5090 self.assertFalse('test-semaphore' in
5091 tenant.semaphore_handler.semaphores)
5092
5093 def test_semaphore_two(self):
5094 "Test semaphores with max>1"
5095 tenant = self.sched.abide.tenants.get('tenant-one')
5096
5097 self.executor_server.hold_jobs_in_build = True
5098 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5099 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5100 self.assertFalse('test-semaphore-two' in
5101 tenant.semaphore_handler.semaphores)
5102
5103 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5104 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5105 self.waitUntilSettled()
5106
5107 self.assertEqual(len(self.builds), 4)
5108 self.assertEqual(self.builds[0].name, 'project-test1')
5109 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5110 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5111 self.assertEqual(self.builds[3].name, 'project-test1')
5112 self.assertTrue('test-semaphore-two' in
5113 tenant.semaphore_handler.semaphores)
5114 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5115 'test-semaphore-two', [])), 2)
5116
5117 self.executor_server.release('semaphore-two-test1')
5118 self.waitUntilSettled()
5119
5120 self.assertEqual(len(self.builds), 4)
5121 self.assertEqual(self.builds[0].name, 'project-test1')
5122 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5123 self.assertEqual(self.builds[2].name, 'project-test1')
5124 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5125 self.assertTrue('test-semaphore-two' in
5126 tenant.semaphore_handler.semaphores)
5127 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5128 'test-semaphore-two', [])), 2)
5129
5130 self.executor_server.release('semaphore-two-test2')
5131 self.waitUntilSettled()
5132
5133 self.assertEqual(len(self.builds), 4)
5134 self.assertEqual(self.builds[0].name, 'project-test1')
5135 self.assertEqual(self.builds[1].name, 'project-test1')
5136 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5137 self.assertEqual(self.builds[3].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', [])), 2)
5142
5143 self.executor_server.release('semaphore-two-test1')
5144 self.waitUntilSettled()
5145
5146 self.assertEqual(len(self.builds), 3)
5147 self.assertEqual(self.builds[0].name, 'project-test1')
5148 self.assertEqual(self.builds[1].name, 'project-test1')
5149 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5150 self.assertTrue('test-semaphore-two' in
5151 tenant.semaphore_handler.semaphores)
5152 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5153 'test-semaphore-two', [])), 1)
5154
5155 self.executor_server.release('semaphore-two-test2')
5156 self.waitUntilSettled()
5157
5158 self.assertEqual(len(self.builds), 2)
5159 self.assertEqual(self.builds[0].name, 'project-test1')
5160 self.assertEqual(self.builds[1].name, 'project-test1')
5161 self.assertFalse('test-semaphore-two' in
5162 tenant.semaphore_handler.semaphores)
5163
5164 self.executor_server.hold_jobs_in_build = False
5165 self.executor_server.release()
5166
5167 self.waitUntilSettled()
5168 self.assertEqual(len(self.builds), 0)
5169
5170 self.assertEqual(A.reported, 1)
5171 self.assertEqual(B.reported, 1)
5172
5173 def test_semaphore_abandon(self):
5174 "Test abandon with job semaphores"
5175 self.executor_server.hold_jobs_in_build = True
5176 tenant = self.sched.abide.tenants.get('tenant-one')
5177 check_pipeline = tenant.layout.pipelines['check']
5178
5179 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5180 self.assertFalse('test-semaphore' in
5181 tenant.semaphore_handler.semaphores)
5182
5183 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5184 self.waitUntilSettled()
5185
5186 self.assertTrue('test-semaphore' in
5187 tenant.semaphore_handler.semaphores)
5188
5189 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5190 self.waitUntilSettled()
5191
5192 # The check pipeline should be empty
5193 items = check_pipeline.getAllItems()
5194 self.assertEqual(len(items), 0)
5195
5196 # The semaphore should be released
5197 self.assertFalse('test-semaphore' in
5198 tenant.semaphore_handler.semaphores)
5199
5200 self.executor_server.hold_jobs_in_build = False
5201 self.executor_server.release()
5202 self.waitUntilSettled()
5203
Tobias Henkel0f714002017-06-30 23:30:52 +02005204 def test_semaphore_new_patchset(self):
5205 "Test new patchset with job semaphores"
5206 self.executor_server.hold_jobs_in_build = True
5207 tenant = self.sched.abide.tenants.get('tenant-one')
5208 check_pipeline = tenant.layout.pipelines['check']
5209
5210 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5211 self.assertFalse('test-semaphore' in
5212 tenant.semaphore_handler.semaphores)
5213
5214 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5215 self.waitUntilSettled()
5216
5217 self.assertTrue('test-semaphore' in
5218 tenant.semaphore_handler.semaphores)
5219 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5220 self.assertEqual(len(semaphore), 1)
5221
5222 A.addPatchset()
5223 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5224 self.waitUntilSettled()
5225
5226 self.assertTrue('test-semaphore' in
5227 tenant.semaphore_handler.semaphores)
5228 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5229 self.assertEqual(len(semaphore), 1)
5230
5231 items = check_pipeline.getAllItems()
5232 self.assertEqual(items[0].change.number, '1')
5233 self.assertEqual(items[0].change.patchset, '2')
5234 self.assertTrue(items[0].live)
5235
5236 self.executor_server.hold_jobs_in_build = False
5237 self.executor_server.release()
5238 self.waitUntilSettled()
5239
5240 # The semaphore should be released
5241 self.assertFalse('test-semaphore' in
5242 tenant.semaphore_handler.semaphores)
5243
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005244 def test_semaphore_reconfigure(self):
5245 "Test reconfigure with job semaphores"
5246 self.executor_server.hold_jobs_in_build = True
5247 tenant = self.sched.abide.tenants.get('tenant-one')
5248 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5249 self.assertFalse('test-semaphore' in
5250 tenant.semaphore_handler.semaphores)
5251
5252 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5253 self.waitUntilSettled()
5254
5255 self.assertTrue('test-semaphore' in
5256 tenant.semaphore_handler.semaphores)
5257
5258 # reconfigure without layout change
5259 self.sched.reconfigure(self.config)
5260 self.waitUntilSettled()
5261 tenant = self.sched.abide.tenants.get('tenant-one')
5262
5263 # semaphore still must be held
5264 self.assertTrue('test-semaphore' in
5265 tenant.semaphore_handler.semaphores)
5266
5267 self.commitConfigUpdate(
5268 'common-config',
5269 'config/semaphore/zuul-reconfiguration.yaml')
5270 self.sched.reconfigure(self.config)
5271 self.waitUntilSettled()
5272 tenant = self.sched.abide.tenants.get('tenant-one')
5273
5274 self.executor_server.release('project-test1')
5275 self.waitUntilSettled()
5276
5277 # There should be no builds anymore
5278 self.assertEqual(len(self.builds), 0)
5279
5280 # The semaphore should be released
5281 self.assertFalse('test-semaphore' in
5282 tenant.semaphore_handler.semaphores)
5283
5284
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005285class TestSemaphoreMultiTenant(ZuulTestCase):
5286 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5287
5288 def test_semaphore_tenant_isolation(self):
5289 "Test semaphores in multiple tenants"
5290
5291 self.waitUntilSettled()
5292 tenant_one = self.sched.abide.tenants.get('tenant-one')
5293 tenant_two = self.sched.abide.tenants.get('tenant-two')
5294
5295 self.executor_server.hold_jobs_in_build = True
5296 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5297 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5298 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5299 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5300 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5301 self.assertFalse('test-semaphore' in
5302 tenant_one.semaphore_handler.semaphores)
5303 self.assertFalse('test-semaphore' in
5304 tenant_two.semaphore_handler.semaphores)
5305
5306 # add patches to project1 of tenant-one
5307 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5308 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5309 self.waitUntilSettled()
5310
5311 # one build of project1-test1 must run
5312 # semaphore of tenant-one must be acquired once
5313 # semaphore of tenant-two must not be acquired
5314 self.assertEqual(len(self.builds), 1)
5315 self.assertEqual(self.builds[0].name, 'project1-test1')
5316 self.assertTrue('test-semaphore' in
5317 tenant_one.semaphore_handler.semaphores)
5318 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5319 'test-semaphore', [])), 1)
5320 self.assertFalse('test-semaphore' in
5321 tenant_two.semaphore_handler.semaphores)
5322
5323 # add patches to project2 of tenant-two
5324 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5325 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5326 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5327 self.waitUntilSettled()
5328
5329 # one build of project1-test1 must run
5330 # two builds of project2-test1 must run
5331 # semaphore of tenant-one must be acquired once
5332 # semaphore of tenant-two must be acquired twice
5333 self.assertEqual(len(self.builds), 3)
5334 self.assertEqual(self.builds[0].name, 'project1-test1')
5335 self.assertEqual(self.builds[1].name, 'project2-test1')
5336 self.assertEqual(self.builds[2].name, 'project2-test1')
5337 self.assertTrue('test-semaphore' in
5338 tenant_one.semaphore_handler.semaphores)
5339 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5340 'test-semaphore', [])), 1)
5341 self.assertTrue('test-semaphore' in
5342 tenant_two.semaphore_handler.semaphores)
5343 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5344 'test-semaphore', [])), 2)
5345
5346 self.executor_server.release('project1-test1')
5347 self.waitUntilSettled()
5348
5349 # one build of project1-test1 must run
5350 # two builds of project2-test1 must run
5351 # semaphore of tenant-one must be acquired once
5352 # semaphore of tenant-two must be acquired twice
5353 self.assertEqual(len(self.builds), 3)
5354 self.assertEqual(self.builds[0].name, 'project2-test1')
5355 self.assertEqual(self.builds[1].name, 'project2-test1')
5356 self.assertEqual(self.builds[2].name, 'project1-test1')
5357 self.assertTrue('test-semaphore' in
5358 tenant_one.semaphore_handler.semaphores)
5359 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5360 'test-semaphore', [])), 1)
5361 self.assertTrue('test-semaphore' in
5362 tenant_two.semaphore_handler.semaphores)
5363 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5364 'test-semaphore', [])), 2)
5365
5366 self.executor_server.release('project2-test1')
5367 self.waitUntilSettled()
5368
5369 # one build of project1-test1 must run
5370 # one build of project2-test1 must run
5371 # semaphore of tenant-one must be acquired once
5372 # semaphore of tenant-two must be acquired once
5373 self.assertEqual(len(self.builds), 2)
5374 self.assertTrue('test-semaphore' in
5375 tenant_one.semaphore_handler.semaphores)
5376 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5377 'test-semaphore', [])), 1)
5378 self.assertTrue('test-semaphore' in
5379 tenant_two.semaphore_handler.semaphores)
5380 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5381 'test-semaphore', [])), 1)
5382
5383 self.executor_server.hold_jobs_in_build = False
5384 self.executor_server.release()
5385
5386 self.waitUntilSettled()
5387
5388 # no build must run
5389 # semaphore of tenant-one must not be acquired
5390 # semaphore of tenant-two must not be acquired
5391 self.assertEqual(len(self.builds), 0)
5392 self.assertFalse('test-semaphore' in
5393 tenant_one.semaphore_handler.semaphores)
5394 self.assertFalse('test-semaphore' in
5395 tenant_two.semaphore_handler.semaphores)
5396
5397 self.assertEqual(A.reported, 1)
5398 self.assertEqual(B.reported, 1)
5399
5400
5401class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005402 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005403 tenant_config_file = 'config/in-repo/main.yaml'
5404
5405 def test_semaphore_in_repo(self):
5406 "Test semaphores in repo config"
5407
5408 # This tests dynamic semaphore handling in project repos. The semaphore
5409 # max value should not be evaluated dynamically but must be updated
5410 # after the change lands.
5411
5412 self.waitUntilSettled()
5413 tenant = self.sched.abide.tenants.get('tenant-one')
5414
5415 in_repo_conf = textwrap.dedent(
5416 """
5417 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005418 name: project-test1
5419
5420 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005421 name: project-test2
5422 semaphore: test-semaphore
5423
5424 - project:
5425 name: org/project
5426 tenant-one-gate:
5427 jobs:
5428 - project-test2
5429
5430 # the max value in dynamic layout must be ignored
5431 - semaphore:
5432 name: test-semaphore
5433 max: 2
5434 """)
5435
5436 in_repo_playbook = textwrap.dedent(
5437 """
5438 - hosts: all
5439 tasks: []
5440 """)
5441
5442 file_dict = {'.zuul.yaml': in_repo_conf,
5443 'playbooks/project-test2.yaml': in_repo_playbook}
5444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5445 files=file_dict)
5446 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5447 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5448 B.setDependsOn(A, 1)
5449 C.setDependsOn(A, 1)
5450
5451 self.executor_server.hold_jobs_in_build = True
5452
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005453 A.addApproval('Code-Review', 2)
5454 B.addApproval('Code-Review', 2)
5455 C.addApproval('Code-Review', 2)
5456 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5457 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5458 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005459 self.waitUntilSettled()
5460
5461 # check that the layout in a queue item still has max value of 1
5462 # for test-semaphore
5463 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5464 queue = None
5465 for queue_candidate in pipeline.queues:
5466 if queue_candidate.name == 'org/project':
5467 queue = queue_candidate
5468 break
5469 queue_item = queue.queue[0]
5470 item_dynamic_layout = queue_item.current_build_set.layout
5471 dynamic_test_semaphore = \
5472 item_dynamic_layout.semaphores.get('test-semaphore')
5473 self.assertEqual(dynamic_test_semaphore.max, 1)
5474
5475 # one build must be in queue, one semaphores acquired
5476 self.assertEqual(len(self.builds), 1)
5477 self.assertEqual(self.builds[0].name, 'project-test2')
5478 self.assertTrue('test-semaphore' in
5479 tenant.semaphore_handler.semaphores)
5480 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5481 'test-semaphore', [])), 1)
5482
5483 self.executor_server.release('project-test2')
5484 self.waitUntilSettled()
5485
5486 # change A must be merged
5487 self.assertEqual(A.data['status'], 'MERGED')
5488 self.assertEqual(A.reported, 2)
5489
5490 # send change-merged event as the gerrit mock doesn't send it
5491 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5492 self.waitUntilSettled()
5493
5494 # now that change A was merged, the new semaphore max must be effective
5495 tenant = self.sched.abide.tenants.get('tenant-one')
5496 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5497
5498 # two builds must be in queue, two semaphores acquired
5499 self.assertEqual(len(self.builds), 2)
5500 self.assertEqual(self.builds[0].name, 'project-test2')
5501 self.assertEqual(self.builds[1].name, 'project-test2')
5502 self.assertTrue('test-semaphore' in
5503 tenant.semaphore_handler.semaphores)
5504 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5505 'test-semaphore', [])), 2)
5506
5507 self.executor_server.release('project-test2')
5508 self.waitUntilSettled()
5509
5510 self.assertEqual(len(self.builds), 0)
5511 self.assertFalse('test-semaphore' in
5512 tenant.semaphore_handler.semaphores)
5513
5514 self.executor_server.hold_jobs_in_build = False
5515 self.executor_server.release()
5516
5517 self.waitUntilSettled()
5518 self.assertEqual(len(self.builds), 0)
5519
5520 self.assertEqual(A.reported, 2)
5521 self.assertEqual(B.reported, 2)
5522 self.assertEqual(C.reported, 2)