blob: f2d7c962c515d0079d18ed38c218a6660bf960e5 [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. Blair80ac1582017-10-09 07:02:40 -070092 for stat in self.statsd.stats:
93 k, v = stat.decode('utf-8').split(':')
94 self.log.debug('stat %s:%s', k, v)
James E. Blair552b54f2016-07-22 13:55:32 -070095 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050096 # Per-driver
97 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
98 # Per-driver per-connection
99 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
100 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -0700101 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700102 'zuul.tenant.tenant-one.pipeline.gate.current_changes',
103 value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700104 self.assertReportedStat(
James E. Blair80ac1582017-10-09 07:02:40 -0700105 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
106 'org_project.master.job.project-merge.SUCCESS', kind='ms')
107 self.assertReportedStat(
108 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
109 'org_project.master.job.project-merge.SUCCESS', value='1|c')
110 self.assertReportedStat(
111 'zuul.tenant.tenant-one.pipeline.gate.resident_time', kind='ms')
112 self.assertReportedStat(
113 'zuul.tenant.tenant-one.pipeline.gate.total_changes', value='1|c')
114 self.assertReportedStat(
115 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
116 'org_project.master.resident_time', kind='ms')
117 self.assertReportedStat(
118 'zuul.tenant.tenant-one.pipeline.gate.project.review_example_com.'
119 'org_project.master.total_changes', value='1|c')
James E. Blairfaf81982017-10-10 15:42:26 -0700120 exec_key = 'zuul.executor.%s' % self.executor_server.hostname
121 self.assertReportedStat(exec_key + '.builds', value='1|c')
James E. Blair4f1731b2017-10-10 18:11:42 -0700122 self.assertReportedStat('zuul.nodepool.requested', value='1|c')
123 self.assertReportedStat('zuul.nodepool.requested.label.label1',
124 value='1|c')
125 self.assertReportedStat('zuul.nodepool.fulfilled.label.label1',
126 value='1|c')
127 self.assertReportedStat('zuul.nodepool.requested.size.1', value='1|c')
128 self.assertReportedStat('zuul.nodepool.fulfilled.size.1', value='1|c')
129 self.assertReportedStat('zuul.nodepool.current_requests', value='1|g')
James E. Blair412e5582013-04-22 15:50:12 -0700130
James E. Blair2ef29e92017-07-21 15:25:05 -0700131 for build in self.history:
132 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700133
James E. Blair3cb10702013-08-24 08:56:03 -0700134 def test_initial_pipeline_gauges(self):
135 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700136 self.assertReportedStat('zuul.pipeline.gate.current_changes',
137 value='0|g')
138 self.assertReportedStat('zuul.pipeline.check.current_changes',
139 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700140
James E. Blair34776ee2016-08-25 13:53:54 -0700141 def test_job_branch(self):
142 "Test the correct variant of a job runs on a branch"
143 self.create_branch('org/project', 'stable')
144 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200145 A.addApproval('Code-Review', 2)
146 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700147 self.waitUntilSettled()
148 self.assertEqual(self.getJobFromHistory('project-test1').result,
149 'SUCCESS')
150 self.assertEqual(self.getJobFromHistory('project-test2').result,
151 'SUCCESS')
152 self.assertEqual(A.data['status'], 'MERGED')
153 self.assertEqual(A.reported, 2,
154 "A should report start and success")
155 self.assertIn('gate', A.messages[1],
156 "A should transit gate")
157 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700158 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700159
James E. Blairb0fcae42012-07-17 11:12:10 -0700160 def test_parallel_changes(self):
161 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700162
Paul Belanger174a8272017-03-14 13:20:10 -0400163 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700164 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
165 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
166 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200167 A.addApproval('Code-Review', 2)
168 B.addApproval('Code-Review', 2)
169 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700170
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200171 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
172 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
173 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700174
175 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(len(self.builds), 1)
177 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700178 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700179
Paul Belanger174a8272017-03-14 13:20:10 -0400180 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400182 self.assertEqual(len(self.builds), 3)
183 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700184 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400185 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700186 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400187 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700188 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700189
Paul Belanger174a8272017-03-14 13:20:10 -0400190 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700191 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400192 self.assertEqual(len(self.builds), 5)
193 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700194 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400198 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700199 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700201 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700202
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400203 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700204 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700205
Paul Belanger174a8272017-03-14 13:20:10 -0400206 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700207 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(len(self.builds), 6)
209 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700210 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400211 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700212 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700213
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400214 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700215 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400216 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700217 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700218
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400219 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700220 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400221 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700222 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700223
Paul Belanger174a8272017-03-14 13:20:10 -0400224 self.executor_server.hold_jobs_in_build = False
225 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700226 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400227 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700228
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400229 self.assertEqual(len(self.history), 9)
230 self.assertEqual(A.data['status'], 'MERGED')
231 self.assertEqual(B.data['status'], 'MERGED')
232 self.assertEqual(C.data['status'], 'MERGED')
233 self.assertEqual(A.reported, 2)
234 self.assertEqual(B.reported, 2)
235 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700236
237 def test_failed_changes(self):
238 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400239 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700240
James E. Blairb02a3bb2012-07-30 17:49:55 -0700241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
242 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200243 A.addApproval('Code-Review', 2)
244 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700245
Paul Belanger174a8272017-03-14 13:20:10 -0400246 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700247
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200248 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
249 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700250 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700251 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400252
Paul Belanger174a8272017-03-14 13:20:10 -0400253 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400254 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700255 # A/project-merge is complete
256 self.assertBuilds([
257 dict(name='project-test1', changes='1,1'),
258 dict(name='project-test2', changes='1,1'),
259 dict(name='project-merge', changes='1,1 2,1'),
260 ])
James E. Blaire2819012013-06-28 17:17:26 -0400261
Paul Belanger174a8272017-03-14 13:20:10 -0400262 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400263 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700264 # A/project-merge is complete
265 # B/project-merge is complete
266 self.assertBuilds([
267 dict(name='project-test1', changes='1,1'),
268 dict(name='project-test2', changes='1,1'),
269 dict(name='project-test1', changes='1,1 2,1'),
270 dict(name='project-test2', changes='1,1 2,1'),
271 ])
272
273 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400274 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700275 self.builds[0].release()
276 self.waitUntilSettled()
277
278 self.orderedRelease()
279 self.assertHistory([
280 dict(name='project-merge', result='SUCCESS', changes='1,1'),
281 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
282 dict(name='project-test1', result='FAILURE', changes='1,1'),
283 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
284 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
285 dict(name='project-test2', result='SUCCESS', changes='1,1'),
286 dict(name='project-merge', result='SUCCESS', changes='2,1'),
287 dict(name='project-test1', result='SUCCESS', changes='2,1'),
288 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700289 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700290
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400291 self.assertEqual(A.data['status'], 'NEW')
292 self.assertEqual(B.data['status'], 'MERGED')
293 self.assertEqual(A.reported, 2)
294 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700295
296 def test_independent_queues(self):
297 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700298
Paul Belanger174a8272017-03-14 13:20:10 -0400299 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900300 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700301 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
302 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200303 A.addApproval('Code-Review', 2)
304 B.addApproval('Code-Review', 2)
305 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700306
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200307 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800308 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700309
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200310 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800311 self.waitUntilSettled()
312
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200313 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700314 self.waitUntilSettled()
315
316 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700317 self.assertBuilds([
318 dict(name='project-merge', changes='1,1'),
319 dict(name='project-merge', changes='2,1'),
320 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700321
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700322 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700323 self.builds[0].release()
324 self.waitUntilSettled()
325 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700326 self.waitUntilSettled()
327 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400328 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700329 self.waitUntilSettled()
330
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700331 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700332 self.assertBuilds([
333 dict(name='project-test1', changes='1,1'),
334 dict(name='project-test2', changes='1,1'),
335 dict(name='project-test1', changes='2,1'),
336 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500337 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700338 dict(name='project-test1', changes='2,1 3,1'),
339 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800340 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700341 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700342
James E. Blair5f0d49e2016-08-19 09:36:50 -0700343 self.orderedRelease()
344 self.assertHistory([
345 dict(name='project-merge', result='SUCCESS', changes='1,1'),
346 dict(name='project-merge', result='SUCCESS', changes='2,1'),
347 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
348 dict(name='project-test1', result='SUCCESS', changes='1,1'),
349 dict(name='project-test2', result='SUCCESS', changes='1,1'),
350 dict(name='project-test1', result='SUCCESS', changes='2,1'),
351 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500352 dict(
353 name='project1-project2-integration',
354 result='SUCCESS',
355 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700356 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
357 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800358 dict(name='project1-project2-integration',
359 result='SUCCESS',
360 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700361 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700362
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400363 self.assertEqual(A.data['status'], 'MERGED')
364 self.assertEqual(B.data['status'], 'MERGED')
365 self.assertEqual(C.data['status'], 'MERGED')
366 self.assertEqual(A.reported, 2)
367 self.assertEqual(B.reported, 2)
368 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700369
370 def test_failed_change_at_head(self):
371 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700372
Paul Belanger174a8272017-03-14 13:20:10 -0400373 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700374 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
375 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
376 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200377 A.addApproval('Code-Review', 2)
378 B.addApproval('Code-Review', 2)
379 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700380
Paul Belanger174a8272017-03-14 13:20:10 -0400381 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700382
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200383 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
384 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
385 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700386
387 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700388
James E. Blairb536ecc2016-08-31 10:11:42 -0700389 self.assertBuilds([
390 dict(name='project-merge', changes='1,1'),
391 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700392
Paul Belanger174a8272017-03-14 13:20:10 -0400393 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700394 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400395 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700396 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400397 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700398 self.waitUntilSettled()
399
James E. Blairb536ecc2016-08-31 10:11:42 -0700400 self.assertBuilds([
401 dict(name='project-test1', changes='1,1'),
402 dict(name='project-test2', changes='1,1'),
403 dict(name='project-test1', changes='1,1 2,1'),
404 dict(name='project-test2', changes='1,1 2,1'),
405 dict(name='project-test1', changes='1,1 2,1 3,1'),
406 dict(name='project-test2', changes='1,1 2,1 3,1'),
407 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700408
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400409 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700410 self.waitUntilSettled()
411
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400412 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700413 self.assertBuilds([
414 dict(name='project-test2', changes='1,1'),
415 dict(name='project-merge', changes='2,1'),
416 ])
417 # Unordered history comparison because the aborts can finish
418 # in any order.
419 self.assertHistory([
420 dict(name='project-merge', result='SUCCESS',
421 changes='1,1'),
422 dict(name='project-merge', result='SUCCESS',
423 changes='1,1 2,1'),
424 dict(name='project-merge', result='SUCCESS',
425 changes='1,1 2,1 3,1'),
426 dict(name='project-test1', result='FAILURE',
427 changes='1,1'),
428 dict(name='project-test1', result='ABORTED',
429 changes='1,1 2,1'),
430 dict(name='project-test2', result='ABORTED',
431 changes='1,1 2,1'),
432 dict(name='project-test1', result='ABORTED',
433 changes='1,1 2,1 3,1'),
434 dict(name='project-test2', result='ABORTED',
435 changes='1,1 2,1 3,1'),
436 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700437
Paul Belanger174a8272017-03-14 13:20:10 -0400438 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700439 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400440 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700441 self.waitUntilSettled()
442 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700443
James E. Blairb536ecc2016-08-31 10:11:42 -0700444 self.assertBuilds([])
445 self.assertHistory([
446 dict(name='project-merge', result='SUCCESS',
447 changes='1,1'),
448 dict(name='project-merge', result='SUCCESS',
449 changes='1,1 2,1'),
450 dict(name='project-merge', result='SUCCESS',
451 changes='1,1 2,1 3,1'),
452 dict(name='project-test1', result='FAILURE',
453 changes='1,1'),
454 dict(name='project-test1', result='ABORTED',
455 changes='1,1 2,1'),
456 dict(name='project-test2', result='ABORTED',
457 changes='1,1 2,1'),
458 dict(name='project-test1', result='ABORTED',
459 changes='1,1 2,1 3,1'),
460 dict(name='project-test2', result='ABORTED',
461 changes='1,1 2,1 3,1'),
462 dict(name='project-merge', result='SUCCESS',
463 changes='2,1'),
464 dict(name='project-merge', result='SUCCESS',
465 changes='2,1 3,1'),
466 dict(name='project-test2', result='SUCCESS',
467 changes='1,1'),
468 dict(name='project-test1', result='SUCCESS',
469 changes='2,1'),
470 dict(name='project-test2', result='SUCCESS',
471 changes='2,1'),
472 dict(name='project-test1', result='SUCCESS',
473 changes='2,1 3,1'),
474 dict(name='project-test2', result='SUCCESS',
475 changes='2,1 3,1'),
476 ], ordered=False)
477
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400478 self.assertEqual(A.data['status'], 'NEW')
479 self.assertEqual(B.data['status'], 'MERGED')
480 self.assertEqual(C.data['status'], 'MERGED')
481 self.assertEqual(A.reported, 2)
482 self.assertEqual(B.reported, 2)
483 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700484
James E. Blair0aac4872013-08-23 14:02:38 -0700485 def test_failed_change_in_middle(self):
486 "Test a failed change in the middle of the queue"
487
Paul Belanger174a8272017-03-14 13:20:10 -0400488 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
490 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
491 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200492 A.addApproval('Code-Review', 2)
493 B.addApproval('Code-Review', 2)
494 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700495
Paul Belanger174a8272017-03-14 13:20:10 -0400496 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700497
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200498 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
499 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
500 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700501
502 self.waitUntilSettled()
503
Paul Belanger174a8272017-03-14 13:20:10 -0400504 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700505 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400506 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700507 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400508 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700509 self.waitUntilSettled()
510
511 self.assertEqual(len(self.builds), 6)
512 self.assertEqual(self.builds[0].name, 'project-test1')
513 self.assertEqual(self.builds[1].name, 'project-test2')
514 self.assertEqual(self.builds[2].name, 'project-test1')
515 self.assertEqual(self.builds[3].name, 'project-test2')
516 self.assertEqual(self.builds[4].name, 'project-test1')
517 self.assertEqual(self.builds[5].name, 'project-test2')
518
519 self.release(self.builds[2])
520 self.waitUntilSettled()
521
James E. Blair972e3c72013-08-29 12:04:55 -0700522 # project-test1 and project-test2 for A
523 # project-test2 for B
524 # project-merge for C (without B)
525 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700526 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
527
Paul Belanger174a8272017-03-14 13:20:10 -0400528 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700529 self.waitUntilSettled()
530
531 # project-test1 and project-test2 for A
532 # project-test2 for B
533 # project-test1 and project-test2 for C
534 self.assertEqual(len(self.builds), 5)
535
Paul Belanger2e2a0492016-10-30 18:33:37 -0400536 tenant = self.sched.abide.tenants.get('tenant-one')
537 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700538 builds = items[0].current_build_set.getBuilds()
539 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
540 self.assertEqual(self.countJobResults(builds, None), 2)
541 builds = items[1].current_build_set.getBuilds()
542 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
543 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
544 self.assertEqual(self.countJobResults(builds, None), 1)
545 builds = items[2].current_build_set.getBuilds()
546 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700547 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700548
Paul Belanger174a8272017-03-14 13:20:10 -0400549 self.executor_server.hold_jobs_in_build = False
550 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700551 self.waitUntilSettled()
552
553 self.assertEqual(len(self.builds), 0)
554 self.assertEqual(len(self.history), 12)
555 self.assertEqual(A.data['status'], 'MERGED')
556 self.assertEqual(B.data['status'], 'NEW')
557 self.assertEqual(C.data['status'], 'MERGED')
558 self.assertEqual(A.reported, 2)
559 self.assertEqual(B.reported, 2)
560 self.assertEqual(C.reported, 2)
561
James E. Blaird466dc42012-07-31 10:42:56 -0700562 def test_failed_change_at_head_with_queue(self):
563 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700564
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700565 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700566 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
567 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
568 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200569 A.addApproval('Code-Review', 2)
570 B.addApproval('Code-Review', 2)
571 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700572
Paul Belanger174a8272017-03-14 13:20:10 -0400573 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700574
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200575 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
576 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
577 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700578
579 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700580 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400581 self.assertEqual(len(self.builds), 0)
582 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700583 self.assertEqual(queue[0].name, b'executor:execute')
584 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800585 self.assertEqual(job_args['job'], 'project-merge')
586 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700587
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700588 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700589 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700590 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700591 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700592 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700593 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700594 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700595
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400596 self.assertEqual(len(self.builds), 0)
597 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800598
599 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700600 json.loads(queue[0].arguments.decode('utf8'))['job'],
601 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800602 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700603 json.loads(queue[1].arguments.decode('utf8'))['job'],
604 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800605 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700606 json.loads(queue[2].arguments.decode('utf8'))['job'],
607 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800608 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700609 json.loads(queue[3].arguments.decode('utf8'))['job'],
610 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800611 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700612 json.loads(queue[4].arguments.decode('utf8'))['job'],
613 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800614 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700615 json.loads(queue[5].arguments.decode('utf8'))['job'],
616 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700617
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700618 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700619 self.waitUntilSettled()
620
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400621 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700622 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400623 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
624 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700625
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700626 self.gearman_server.hold_jobs_in_queue = False
627 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700628 self.waitUntilSettled()
629
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400630 self.assertEqual(len(self.builds), 0)
631 self.assertEqual(len(self.history), 11)
632 self.assertEqual(A.data['status'], 'NEW')
633 self.assertEqual(B.data['status'], 'MERGED')
634 self.assertEqual(C.data['status'], 'MERGED')
635 self.assertEqual(A.reported, 2)
636 self.assertEqual(B.reported, 2)
637 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700638
James E. Blairce8a2132016-05-19 15:21:52 -0700639 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400640 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200642 A.addApproval('Code-Review', 2)
643 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700644 self.waitUntilSettled()
645 time.sleep(2)
646
James E. Blairae0f23c2017-09-13 10:55:15 -0600647 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700648 found_job = None
649 for pipeline in data['pipelines']:
650 if pipeline['name'] != 'gate':
651 continue
652 for queue in pipeline['change_queues']:
653 for head in queue['heads']:
654 for item in head:
655 for job in item['jobs']:
656 if job['name'] == 'project-merge':
657 found_job = job
658 break
659
660 self.assertIsNotNone(found_job)
661 if iteration == 1:
662 self.assertIsNotNone(found_job['estimated_time'])
663 self.assertIsNone(found_job['remaining_time'])
664 else:
665 self.assertIsNotNone(found_job['estimated_time'])
666 self.assertTrue(found_job['estimated_time'] >= 2)
667 self.assertIsNotNone(found_job['remaining_time'])
668
Paul Belanger174a8272017-03-14 13:20:10 -0400669 self.executor_server.hold_jobs_in_build = False
670 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700671 self.waitUntilSettled()
672
673 def test_time_database(self):
674 "Test the time database"
675
676 self._test_time_database(1)
677 self._test_time_database(2)
678
James E. Blairfef71632013-09-23 11:15:47 -0700679 def test_two_failed_changes_at_head(self):
680 "Test that changes are reparented correctly if 2 fail at head"
681
Paul Belanger174a8272017-03-14 13:20:10 -0400682 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700683 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
684 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
685 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200686 A.addApproval('Code-Review', 2)
687 B.addApproval('Code-Review', 2)
688 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700689
Paul Belanger174a8272017-03-14 13:20:10 -0400690 self.executor_server.failJob('project-test1', A)
691 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700692
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200693 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
694 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
695 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700696 self.waitUntilSettled()
697
Paul Belanger174a8272017-03-14 13:20:10 -0400698 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700699 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400700 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700701 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400702 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700703 self.waitUntilSettled()
704
705 self.assertEqual(len(self.builds), 6)
706 self.assertEqual(self.builds[0].name, 'project-test1')
707 self.assertEqual(self.builds[1].name, 'project-test2')
708 self.assertEqual(self.builds[2].name, 'project-test1')
709 self.assertEqual(self.builds[3].name, 'project-test2')
710 self.assertEqual(self.builds[4].name, 'project-test1')
711 self.assertEqual(self.builds[5].name, 'project-test2')
712
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400713 self.assertTrue(self.builds[0].hasChanges(A))
714 self.assertTrue(self.builds[2].hasChanges(A))
715 self.assertTrue(self.builds[2].hasChanges(B))
716 self.assertTrue(self.builds[4].hasChanges(A))
717 self.assertTrue(self.builds[4].hasChanges(B))
718 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700719
720 # Fail change B first
721 self.release(self.builds[2])
722 self.waitUntilSettled()
723
724 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400725 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700726 self.waitUntilSettled()
727
728 self.assertEqual(len(self.builds), 5)
729 self.assertEqual(self.builds[0].name, 'project-test1')
730 self.assertEqual(self.builds[1].name, 'project-test2')
731 self.assertEqual(self.builds[2].name, 'project-test2')
732 self.assertEqual(self.builds[3].name, 'project-test1')
733 self.assertEqual(self.builds[4].name, 'project-test2')
734
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400735 self.assertTrue(self.builds[1].hasChanges(A))
736 self.assertTrue(self.builds[2].hasChanges(A))
737 self.assertTrue(self.builds[2].hasChanges(B))
738 self.assertTrue(self.builds[4].hasChanges(A))
739 self.assertFalse(self.builds[4].hasChanges(B))
740 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700741
742 # Finish running all passing jobs for change A
743 self.release(self.builds[1])
744 self.waitUntilSettled()
745 # Fail and report change A
746 self.release(self.builds[0])
747 self.waitUntilSettled()
748
749 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400750 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700751 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400752 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700753 self.waitUntilSettled()
754
755 self.assertEqual(len(self.builds), 4)
756 self.assertEqual(self.builds[0].name, 'project-test1') # B
757 self.assertEqual(self.builds[1].name, 'project-test2') # B
758 self.assertEqual(self.builds[2].name, 'project-test1') # C
759 self.assertEqual(self.builds[3].name, 'project-test2') # C
760
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400761 self.assertFalse(self.builds[1].hasChanges(A))
762 self.assertTrue(self.builds[1].hasChanges(B))
763 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700764
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400765 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700766 # After A failed and B and C restarted, B should be back in
767 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400768 self.assertTrue(self.builds[2].hasChanges(B))
769 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700770
Paul Belanger174a8272017-03-14 13:20:10 -0400771 self.executor_server.hold_jobs_in_build = False
772 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700773 self.waitUntilSettled()
774
775 self.assertEqual(len(self.builds), 0)
776 self.assertEqual(len(self.history), 21)
777 self.assertEqual(A.data['status'], 'NEW')
778 self.assertEqual(B.data['status'], 'NEW')
779 self.assertEqual(C.data['status'], 'MERGED')
780 self.assertEqual(A.reported, 2)
781 self.assertEqual(B.reported, 2)
782 self.assertEqual(C.reported, 2)
783
James E. Blair8c803f82012-07-31 16:25:42 -0700784 def test_patch_order(self):
785 "Test that dependent patches are tested in the right order"
786 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
787 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
788 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200789 A.addApproval('Code-Review', 2)
790 B.addApproval('Code-Review', 2)
791 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700792
793 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
794 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
795 M2.setMerged()
796 M1.setMerged()
797
798 # C -> B -> A -> M1 -> M2
799 # M2 is here to make sure it is never queried. If it is, it
800 # means zuul is walking down the entire history of merged
801 # changes.
802
803 C.setDependsOn(B, 1)
804 B.setDependsOn(A, 1)
805 A.setDependsOn(M1, 1)
806 M1.setDependsOn(M2, 1)
807
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200808 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700809
810 self.waitUntilSettled()
811
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400812 self.assertEqual(A.data['status'], 'NEW')
813 self.assertEqual(B.data['status'], 'NEW')
814 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700815
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200816 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
817 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700818
819 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400820 self.assertEqual(M2.queried, 0)
821 self.assertEqual(A.data['status'], 'MERGED')
822 self.assertEqual(B.data['status'], 'MERGED')
823 self.assertEqual(C.data['status'], 'MERGED')
824 self.assertEqual(A.reported, 2)
825 self.assertEqual(B.reported, 2)
826 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700827
James E. Blair063672f2015-01-29 13:09:12 -0800828 def test_needed_changes_enqueue(self):
829 "Test that a needed change is enqueued ahead"
830 # A Given a git tree like this, if we enqueue
831 # / \ change C, we should walk up and down the tree
832 # B G and enqueue changes in the order ABCDEFG.
833 # /|\ This is also the order that you would get if
834 # *C E F you enqueued changes in the order ABCDEFG, so
835 # / the ordering is stable across re-enqueue events.
836 # D
837
838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
839 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
840 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
841 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
842 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
843 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
844 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
845 B.setDependsOn(A, 1)
846 C.setDependsOn(B, 1)
847 D.setDependsOn(C, 1)
848 E.setDependsOn(B, 1)
849 F.setDependsOn(B, 1)
850 G.setDependsOn(A, 1)
851
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200852 A.addApproval('Code-Review', 2)
853 B.addApproval('Code-Review', 2)
854 C.addApproval('Code-Review', 2)
855 D.addApproval('Code-Review', 2)
856 E.addApproval('Code-Review', 2)
857 F.addApproval('Code-Review', 2)
858 G.addApproval('Code-Review', 2)
859 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800860
861 self.waitUntilSettled()
862
863 self.assertEqual(A.data['status'], 'NEW')
864 self.assertEqual(B.data['status'], 'NEW')
865 self.assertEqual(C.data['status'], 'NEW')
866 self.assertEqual(D.data['status'], 'NEW')
867 self.assertEqual(E.data['status'], 'NEW')
868 self.assertEqual(F.data['status'], 'NEW')
869 self.assertEqual(G.data['status'], 'NEW')
870
871 # We're about to add approvals to changes without adding the
872 # triggering events to Zuul, so that we can be sure that it is
873 # enqueing the changes based on dependencies, not because of
874 # triggering events. Since it will have the changes cached
875 # already (without approvals), we need to clear the cache
876 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400877 for connection in self.connections.connections.values():
James E. Blairb9704302017-10-11 16:42:18 -0700878 if hasattr(connection, '_change_cache'):
879 connection._change_cache.clear()
James E. Blair063672f2015-01-29 13:09:12 -0800880
Paul Belanger174a8272017-03-14 13:20:10 -0400881 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200882 A.addApproval('Approved', 1)
883 B.addApproval('Approved', 1)
884 D.addApproval('Approved', 1)
885 E.addApproval('Approved', 1)
886 F.addApproval('Approved', 1)
887 G.addApproval('Approved', 1)
888 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800889
890 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400891 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800892 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400893 self.executor_server.hold_jobs_in_build = False
894 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800895 self.waitUntilSettled()
896
897 self.assertEqual(A.data['status'], 'MERGED')
898 self.assertEqual(B.data['status'], 'MERGED')
899 self.assertEqual(C.data['status'], 'MERGED')
900 self.assertEqual(D.data['status'], 'MERGED')
901 self.assertEqual(E.data['status'], 'MERGED')
902 self.assertEqual(F.data['status'], 'MERGED')
903 self.assertEqual(G.data['status'], 'MERGED')
904 self.assertEqual(A.reported, 2)
905 self.assertEqual(B.reported, 2)
906 self.assertEqual(C.reported, 2)
907 self.assertEqual(D.reported, 2)
908 self.assertEqual(E.reported, 2)
909 self.assertEqual(F.reported, 2)
910 self.assertEqual(G.reported, 2)
911 self.assertEqual(self.history[6].changes,
912 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
913
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100914 def test_source_cache(self):
915 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400916 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700917
918 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
919 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
920 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200921 A.addApproval('Code-Review', 2)
922 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700923
924 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
925 M1.setMerged()
926
927 B.setDependsOn(A, 1)
928 A.setDependsOn(M1, 1)
929
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200930 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700931 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
932
933 self.waitUntilSettled()
934
935 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700936 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700937 build.release()
938 self.waitUntilSettled()
939 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700940 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700941 build.release()
942 self.waitUntilSettled()
943
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200944 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700945 self.waitUntilSettled()
946
Joshua Hesketh352264b2015-08-11 23:42:08 +1000947 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700948 # there should still be changes in the cache
James E. Blairb9704302017-10-11 16:42:18 -0700949 self.assertNotEqual(len(list(self.fake_gerrit._change_cache.keys())),
950 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700951
Paul Belanger174a8272017-03-14 13:20:10 -0400952 self.executor_server.hold_jobs_in_build = False
953 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700954 self.waitUntilSettled()
955
956 self.assertEqual(A.data['status'], 'MERGED')
957 self.assertEqual(B.data['status'], 'MERGED')
958 self.assertEqual(A.queried, 2) # Initial and isMerged
959 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
960
James E. Blair8c803f82012-07-31 16:25:42 -0700961 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700962 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700963 # TODO: move to test_gerrit (this is a unit test!)
964 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400965 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700966 (trusted, project) = tenant.getProject('org/project')
967 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400968
969 # TODO(pabelanger): As we add more source / trigger APIs we should make
970 # it easier for users to create events for testing.
971 event = zuul.model.TriggerEvent()
972 event.trigger_name = 'gerrit'
973 event.change_number = '1'
974 event.patch_number = '2'
975
976 a = source.getChange(event)
977 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700978 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700979
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200980 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400981 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700982 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700983
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200984 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400985 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700986 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700987
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700988 def test_project_merge_conflict(self):
989 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700990
991 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700992 A = self.fake_gerrit.addFakeChange('org/project',
993 'master', 'A',
994 files={'conflict': 'foo'})
995 B = self.fake_gerrit.addFakeChange('org/project',
996 'master', 'B',
997 files={'conflict': 'bar'})
998 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700999 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001000 A.addApproval('Code-Review', 2)
1001 B.addApproval('Code-Review', 2)
1002 C.addApproval('Code-Review', 2)
1003 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1004 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1005 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -07001006 self.waitUntilSettled()
1007
James E. Blair6736beb2013-07-11 15:18:15 -07001008 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -07001009 self.assertEqual(C.reported, 1)
1010
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001011 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001012 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001013 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001014 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001015 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001016 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001017
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001018 self.gearman_server.hold_jobs_in_queue = False
1019 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001020 self.waitUntilSettled()
1021
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001022 self.assertEqual(A.data['status'], 'MERGED')
1023 self.assertEqual(B.data['status'], 'NEW')
1024 self.assertEqual(C.data['status'], 'MERGED')
1025 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001026 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001027 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001028
1029 self.assertHistory([
1030 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1031 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1032 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1033 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1034 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1035 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1036 ], ordered=False)
1037
1038 def test_delayed_merge_conflict(self):
1039 "Test that delayed check merge conflicts are handled properly"
1040
1041 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001042 # the executor sucesfully merges a change based on an old
1043 # repo state (frozen by the scheduler) which would otherwise
1044 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001045 self.gearman_server.hold_jobs_in_queue = True
1046 A = self.fake_gerrit.addFakeChange('org/project',
1047 'master', 'A',
1048 files={'conflict': 'foo'})
1049 B = self.fake_gerrit.addFakeChange('org/project',
1050 'master', 'B',
1051 files={'conflict': 'bar'})
1052 C = self.fake_gerrit.addFakeChange('org/project',
1053 'master', 'C')
1054 C.setDependsOn(B, 1)
1055
1056 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001057 A.addApproval('Code-Review', 2)
1058 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001059 self.waitUntilSettled()
1060 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001061 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001062 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1063 self.waitUntilSettled()
1064
1065 self.assertEqual(A.reported, 1)
1066 self.assertEqual(B.reported, 0) # Check does not report start
1067 self.assertEqual(C.reported, 0) # Check does not report start
1068
1069 # A merges while B and C are queued in check
1070 # Release A project-merge
1071 queue = self.gearman_server.getQueue()
1072 self.release(queue[0])
1073 self.waitUntilSettled()
1074
1075 # Release A project-test*
1076 # gate has higher precedence, so A's test jobs are added in
1077 # front of the merge jobs for B and C
1078 queue = self.gearman_server.getQueue()
1079 self.release(queue[0])
1080 self.release(queue[1])
1081 self.waitUntilSettled()
1082
1083 self.assertEqual(A.data['status'], 'MERGED')
1084 self.assertEqual(B.data['status'], 'NEW')
1085 self.assertEqual(C.data['status'], 'NEW')
1086 self.assertEqual(A.reported, 2)
1087 self.assertEqual(B.reported, 0)
1088 self.assertEqual(C.reported, 0)
1089 self.assertHistory([
1090 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1091 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1092 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1093 ], ordered=False)
1094
1095 # B and C report merge conflicts
1096 # Release B project-merge
1097 queue = self.gearman_server.getQueue()
1098 self.release(queue[0])
1099 self.waitUntilSettled()
1100
1101 # Release C
1102 self.gearman_server.hold_jobs_in_queue = False
1103 self.gearman_server.release()
1104 self.waitUntilSettled()
1105
1106 self.assertEqual(A.data['status'], 'MERGED')
1107 self.assertEqual(B.data['status'], 'NEW')
1108 self.assertEqual(C.data['status'], 'NEW')
1109 self.assertEqual(A.reported, 2)
1110 self.assertEqual(B.reported, 1)
1111 self.assertEqual(C.reported, 1)
1112
1113 self.assertHistory([
1114 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1115 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1116 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001117 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1118 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1119 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1120 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1121 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1122 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001123 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001124
James E. Blairdaabed22012-08-15 15:38:57 -07001125 def test_post(self):
1126 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001127 p = "review.example.com/org/project"
1128 upstream = self.getUpstreamRepos([p])
1129 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1130 A.setMerged()
1131 A_commit = str(upstream[p].commit('master'))
1132 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001133
Zhongyue Luo5d556072012-09-21 02:00:47 +09001134 e = {
1135 "type": "ref-updated",
1136 "submitter": {
1137 "name": "User Name",
1138 },
1139 "refUpdate": {
1140 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001141 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001142 "refName": "master",
1143 "project": "org/project",
1144 }
1145 }
James E. Blairdaabed22012-08-15 15:38:57 -07001146 self.fake_gerrit.addEvent(e)
1147 self.waitUntilSettled()
1148
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001149 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001150 self.assertEqual(len(self.history), 1)
1151 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001152
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001153 def test_post_ignore_deletes(self):
1154 "Test that deleting refs does not trigger post jobs"
1155
1156 e = {
1157 "type": "ref-updated",
1158 "submitter": {
1159 "name": "User Name",
1160 },
1161 "refUpdate": {
1162 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1163 "newRev": "0000000000000000000000000000000000000000",
1164 "refName": "master",
1165 "project": "org/project",
1166 }
1167 }
1168 self.fake_gerrit.addEvent(e)
1169 self.waitUntilSettled()
1170
1171 job_names = [x.name for x in self.history]
1172 self.assertEqual(len(self.history), 0)
1173 self.assertNotIn('project-post', job_names)
1174
James E. Blaircc324192017-04-19 14:36:08 -07001175 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001176 def test_post_ignore_deletes_negative(self):
1177 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001178 e = {
1179 "type": "ref-updated",
1180 "submitter": {
1181 "name": "User Name",
1182 },
1183 "refUpdate": {
1184 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1185 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001186 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001187 "project": "org/project",
1188 }
1189 }
1190 self.fake_gerrit.addEvent(e)
1191 self.waitUntilSettled()
1192
1193 job_names = [x.name for x in self.history]
1194 self.assertEqual(len(self.history), 1)
1195 self.assertIn('project-post', job_names)
1196
James E. Blairec056492016-07-22 09:45:56 -07001197 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001198 def test_build_configuration_branch_interaction(self):
1199 "Test that switching between branches works"
1200 self.test_build_configuration()
1201 self.test_build_configuration_branch()
1202 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001203 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001204 repo = git.Repo(path)
1205 repo.heads.master.commit = repo.commit('init')
1206 self.test_build_configuration()
1207
James E. Blairdb93b302017-07-19 15:33:11 -07001208 def test_dependent_changes_rebase(self):
1209 # Test that no errors occur when we walk a dependency tree
1210 # with an unused leaf node due to a rebase.
1211 # Start by constructing: C -> B -> A
1212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1213 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1214 B.setDependsOn(A, 1)
1215
1216 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1217 C.setDependsOn(B, 1)
1218
1219 # Then rebase to form: D -> C -> A
1220 C.addPatchset() # C,2
1221 C.setDependsOn(A, 1)
1222
1223 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1224 D.setDependsOn(C, 2)
1225
1226 # Walk the entire tree
1227 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1228 self.waitUntilSettled()
1229 self.assertEqual(len(self.history), 3)
1230
1231 # Verify that walking just part of the tree still works
1232 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1233 self.waitUntilSettled()
1234 self.assertEqual(len(self.history), 6)
1235
James E. Blaircaec0c52012-08-22 14:52:22 -07001236 def test_dependent_changes_dequeue(self):
1237 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001238
James E. Blaircaec0c52012-08-22 14:52:22 -07001239 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1240 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1241 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001242 A.addApproval('Code-Review', 2)
1243 B.addApproval('Code-Review', 2)
1244 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001245
1246 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1247 M1.setMerged()
1248
1249 # C -> B -> A -> M1
1250
1251 C.setDependsOn(B, 1)
1252 B.setDependsOn(A, 1)
1253 A.setDependsOn(M1, 1)
1254
Paul Belanger174a8272017-03-14 13:20:10 -04001255 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001256
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001257 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1258 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1259 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001260
1261 self.waitUntilSettled()
1262
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001263 self.assertEqual(A.data['status'], 'NEW')
1264 self.assertEqual(A.reported, 2)
1265 self.assertEqual(B.data['status'], 'NEW')
1266 self.assertEqual(B.reported, 2)
1267 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001268 self.assertIn('This change depends on a change that failed to merge.',
1269 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001270 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001271
James E. Blair972e3c72013-08-29 12:04:55 -07001272 def test_failing_dependent_changes(self):
1273 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001274 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001275 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1276 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1277 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1278 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1279 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001280 A.addApproval('Code-Review', 2)
1281 B.addApproval('Code-Review', 2)
1282 C.addApproval('Code-Review', 2)
1283 D.addApproval('Code-Review', 2)
1284 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001285
1286 # E, D -> C -> B, A
1287
1288 D.setDependsOn(C, 1)
1289 C.setDependsOn(B, 1)
1290
Paul Belanger174a8272017-03-14 13:20:10 -04001291 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001292
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001293 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1294 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1295 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1296 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1297 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001298
1299 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001300 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001301 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001302 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001303 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001304 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001305 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001306 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001307 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001308 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001309 self.waitUntilSettled()
1310
Paul Belanger174a8272017-03-14 13:20:10 -04001311 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001312 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001313 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001314 build.release()
1315 self.waitUntilSettled()
1316
Paul Belanger174a8272017-03-14 13:20:10 -04001317 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001318 self.waitUntilSettled()
1319
1320 self.assertEqual(A.data['status'], 'MERGED')
1321 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001322 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001323 self.assertEqual(B.data['status'], 'NEW')
1324 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001325 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001326 self.assertEqual(C.data['status'], 'NEW')
1327 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001328 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001329 self.assertEqual(D.data['status'], 'NEW')
1330 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001331 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001332 self.assertEqual(E.data['status'], 'MERGED')
1333 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001334 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001335 self.assertEqual(len(self.history), 18)
1336
James E. Blairec590122012-08-22 15:19:31 -07001337 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001338 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001339 # If it's dequeued more than once, we should see extra
1340 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001341
Paul Belanger174a8272017-03-14 13:20:10 -04001342 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001343 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1344 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1345 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001346 A.addApproval('Code-Review', 2)
1347 B.addApproval('Code-Review', 2)
1348 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001349
Paul Belanger174a8272017-03-14 13:20:10 -04001350 self.executor_server.failJob('project-test1', A)
1351 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001352
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001353 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1354 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1355 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001356
1357 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001358
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001359 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001360 self.assertEqual(self.builds[0].name, 'project-merge')
1361 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001362
Paul Belanger174a8272017-03-14 13:20:10 -04001363 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001364 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001365 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001366 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001367 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001368 self.waitUntilSettled()
1369
James E. Blair6df67132017-04-19 13:53:20 -07001370 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001371 self.assertEqual(self.builds[0].name, 'project-test1')
1372 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001373 self.assertEqual(self.builds[2].name, 'project-test1')
1374 self.assertEqual(self.builds[3].name, 'project-test2')
1375 self.assertEqual(self.builds[4].name, 'project-test1')
1376 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001377
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001378 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001379 self.waitUntilSettled()
1380
James E. Blair6df67132017-04-19 13:53:20 -07001381 self.assertEqual(len(self.builds), 2) # test2, merge for B
1382 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001383
Paul Belanger174a8272017-03-14 13:20:10 -04001384 self.executor_server.hold_jobs_in_build = False
1385 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001386 self.waitUntilSettled()
1387
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001388 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001389 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001390
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001391 self.assertEqual(A.data['status'], 'NEW')
1392 self.assertEqual(B.data['status'], 'MERGED')
1393 self.assertEqual(C.data['status'], 'MERGED')
1394 self.assertEqual(A.reported, 2)
1395 self.assertEqual(B.reported, 2)
1396 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001397
James E. Blaircc020532017-04-19 13:04:44 -07001398 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001399 def test_nonvoting_job(self):
1400 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001401
James E. Blair4ec821f2012-08-23 15:28:28 -07001402 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1403 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001404 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001405 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001406 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001407
1408 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001409
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001410 self.assertEqual(A.data['status'], 'MERGED')
1411 self.assertEqual(A.reported, 2)
1412 self.assertEqual(
1413 self.getJobFromHistory('nonvoting-project-merge').result,
1414 'SUCCESS')
1415 self.assertEqual(
1416 self.getJobFromHistory('nonvoting-project-test1').result,
1417 'SUCCESS')
1418 self.assertEqual(
1419 self.getJobFromHistory('nonvoting-project-test2').result,
1420 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001421
James E. Blair2ef29e92017-07-21 15:25:05 -07001422 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1423 parameters['zuul']['voting'])
1424 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1425 parameters['zuul']['voting'])
1426 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1427 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001428
James E. Blaire0487072012-08-29 17:38:31 -07001429 def test_check_queue_success(self):
1430 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001431
James E. Blaire0487072012-08-29 17:38:31 -07001432 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1433 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1434
1435 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001436
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001437 self.assertEqual(A.data['status'], 'NEW')
1438 self.assertEqual(A.reported, 1)
1439 self.assertEqual(self.getJobFromHistory('project-merge').result,
1440 'SUCCESS')
1441 self.assertEqual(self.getJobFromHistory('project-test1').result,
1442 'SUCCESS')
1443 self.assertEqual(self.getJobFromHistory('project-test2').result,
1444 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001445
1446 def test_check_queue_failure(self):
1447 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001448
James E. Blaire0487072012-08-29 17:38:31 -07001449 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001450 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001451 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1452
1453 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001454
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001455 self.assertEqual(A.data['status'], 'NEW')
1456 self.assertEqual(A.reported, 1)
1457 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001458 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001459 self.assertEqual(self.getJobFromHistory('project-test1').result,
1460 'SUCCESS')
1461 self.assertEqual(self.getJobFromHistory('project-test2').result,
1462 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001463
David Shrewsburyffab07a2017-07-24 12:45:07 -04001464 @simple_layout('layouts/autohold.yaml')
1465 def test_autohold(self):
1466 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1467
1468 client = zuul.rpcclient.RPCClient('127.0.0.1',
1469 self.gearman_server.port)
1470 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001471 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1472 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001473 self.assertTrue(r)
1474
1475 self.executor_server.failJob('project-test2', A)
1476 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1477
1478 self.waitUntilSettled()
1479
1480 self.assertEqual(A.data['status'], 'NEW')
1481 self.assertEqual(A.reported, 1)
1482 self.assertEqual(self.getJobFromHistory('project-test2').result,
1483 'FAILURE')
1484
1485 # Check nodepool for a held node
1486 held_node = None
1487 for node in self.fake_nodepool.getNodes():
1488 if node['state'] == zuul.model.STATE_HOLD:
1489 held_node = node
1490 break
1491 self.assertIsNotNone(held_node)
1492
1493 # Validate node has recorded the failed job
1494 self.assertEqual(
1495 held_node['hold_job'],
1496 " ".join(['tenant-one',
1497 'review.example.com/org/project',
1498 'project-test2'])
1499 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001500 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001501
1502 # Another failed change should not hold any more nodes
1503 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1504 self.executor_server.failJob('project-test2', B)
1505 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1506 self.waitUntilSettled()
1507 self.assertEqual(B.data['status'], 'NEW')
1508 self.assertEqual(B.reported, 1)
1509 self.assertEqual(self.getJobFromHistory('project-test2').result,
1510 'FAILURE')
1511
1512 held_nodes = 0
1513 for node in self.fake_nodepool.getNodes():
1514 if node['state'] == zuul.model.STATE_HOLD:
1515 held_nodes += 1
1516 self.assertEqual(held_nodes, 1)
1517
James E. Blair7cb84542017-04-19 13:35:05 -07001518 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001519 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001520 # This particular test does a large amount of merges and needs a little
1521 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001522 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001523 "test that dependent changes behind dequeued changes work"
1524 # This complicated test is a reproduction of a real life bug
1525 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001526
Paul Belanger174a8272017-03-14 13:20:10 -04001527 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001528 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1529 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1530 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1531 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1532 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1533 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1534 D.setDependsOn(C, 1)
1535 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001536 A.addApproval('Code-Review', 2)
1537 B.addApproval('Code-Review', 2)
1538 C.addApproval('Code-Review', 2)
1539 D.addApproval('Code-Review', 2)
1540 E.addApproval('Code-Review', 2)
1541 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001542
1543 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001544
1545 # Change object re-use in the gerrit trigger is hidden if
1546 # changes are added in quick succession; waiting makes it more
1547 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001548 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001550 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001551 self.waitUntilSettled()
1552
Paul Belanger174a8272017-03-14 13:20:10 -04001553 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001554 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001555 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001556 self.waitUntilSettled()
1557
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001558 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001559 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001560 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001561 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001562 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001563 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001564 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001565 self.waitUntilSettled()
1566
Paul Belanger174a8272017-03-14 13:20:10 -04001567 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001568 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001569 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001570 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001571 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001572 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001573 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001574 self.waitUntilSettled()
1575
1576 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001577
1578 # Grab pointers to the jobs we want to release before
1579 # releasing any, because list indexes may change as
1580 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001581 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001582 a.release()
1583 b.release()
1584 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001585 self.waitUntilSettled()
1586
Paul Belanger174a8272017-03-14 13:20:10 -04001587 self.executor_server.hold_jobs_in_build = False
1588 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001589 self.waitUntilSettled()
1590
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001591 self.assertEqual(A.data['status'], 'NEW')
1592 self.assertEqual(B.data['status'], 'MERGED')
1593 self.assertEqual(C.data['status'], 'MERGED')
1594 self.assertEqual(D.data['status'], 'MERGED')
1595 self.assertEqual(E.data['status'], 'MERGED')
1596 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001597
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001598 self.assertEqual(A.reported, 2)
1599 self.assertEqual(B.reported, 2)
1600 self.assertEqual(C.reported, 2)
1601 self.assertEqual(D.reported, 2)
1602 self.assertEqual(E.reported, 2)
1603 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001604
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001605 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1606 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001607
1608 def test_merger_repack(self):
1609 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001610
James E. Blair05fed602012-09-07 12:45:24 -07001611 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001612 A.addApproval('Code-Review', 2)
1613 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001614 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001615 self.assertEqual(self.getJobFromHistory('project-merge').result,
1616 'SUCCESS')
1617 self.assertEqual(self.getJobFromHistory('project-test1').result,
1618 'SUCCESS')
1619 self.assertEqual(self.getJobFromHistory('project-test2').result,
1620 'SUCCESS')
1621 self.assertEqual(A.data['status'], 'MERGED')
1622 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001623 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001624 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001625
James E. Blair2a535672017-04-27 12:03:15 -07001626 path = os.path.join(self.merger_src_root, "review.example.com",
1627 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001628 if os.path.exists(path):
1629 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001630 path = os.path.join(self.executor_src_root, "review.example.com",
1631 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001632 if os.path.exists(path):
1633 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001634
1635 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001636 A.addApproval('Code-Review', 2)
1637 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001638 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001639 self.assertEqual(self.getJobFromHistory('project-merge').result,
1640 'SUCCESS')
1641 self.assertEqual(self.getJobFromHistory('project-test1').result,
1642 'SUCCESS')
1643 self.assertEqual(self.getJobFromHistory('project-test2').result,
1644 'SUCCESS')
1645 self.assertEqual(A.data['status'], 'MERGED')
1646 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001647
James E. Blair4886f282012-11-15 09:27:33 -08001648 def test_merger_repack_large_change(self):
1649 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001650 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001651 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001652 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001653 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001654 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001655 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001656 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001657 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001658 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001659 # TODOv3(jeblair): add hostname to upstream root
1660 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001661 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001662 path = os.path.join(self.merger_src_root, 'review.example.com',
1663 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001664 if os.path.exists(path):
1665 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001666 path = os.path.join(self.executor_src_root, 'review.example.com',
1667 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001668 if os.path.exists(path):
1669 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001670
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001671 A.addApproval('Code-Review', 2)
1672 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001673 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001674 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001675 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001676 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001677 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001678 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001679 'SUCCESS')
1680 self.assertEqual(A.data['status'], 'MERGED')
1681 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001682
James E. Blair2fa50962013-01-30 21:50:41 -08001683 def test_new_patchset_dequeues_old(self):
1684 "Test that a new patchset causes the old to be dequeued"
1685 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001686 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001687 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1688 M.setMerged()
1689
1690 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1691 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1692 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1693 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001694 A.addApproval('Code-Review', 2)
1695 B.addApproval('Code-Review', 2)
1696 C.addApproval('Code-Review', 2)
1697 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001698
1699 C.setDependsOn(B, 1)
1700 B.setDependsOn(A, 1)
1701 A.setDependsOn(M, 1)
1702
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001703 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1704 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1705 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1706 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001707 self.waitUntilSettled()
1708
1709 B.addPatchset()
1710 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1711 self.waitUntilSettled()
1712
Paul Belanger174a8272017-03-14 13:20:10 -04001713 self.executor_server.hold_jobs_in_build = False
1714 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001715 self.waitUntilSettled()
1716
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001717 self.assertEqual(A.data['status'], 'MERGED')
1718 self.assertEqual(A.reported, 2)
1719 self.assertEqual(B.data['status'], 'NEW')
1720 self.assertEqual(B.reported, 2)
1721 self.assertEqual(C.data['status'], 'NEW')
1722 self.assertEqual(C.reported, 2)
1723 self.assertEqual(D.data['status'], 'MERGED')
1724 self.assertEqual(D.reported, 2)
1725 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001726
James E. Blairba437362015-02-07 11:41:52 -08001727 def test_new_patchset_check(self):
1728 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001729
Paul Belanger174a8272017-03-14 13:20:10 -04001730 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001731
1732 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001733 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001734 tenant = self.sched.abide.tenants.get('tenant-one')
1735 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001736
1737 # Add two git-dependent changes
1738 B.setDependsOn(A, 1)
1739 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1740 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001741 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1742 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001743
1744 # A live item, and a non-live/live pair
1745 items = check_pipeline.getAllItems()
1746 self.assertEqual(len(items), 3)
1747
1748 self.assertEqual(items[0].change.number, '1')
1749 self.assertEqual(items[0].change.patchset, '1')
1750 self.assertFalse(items[0].live)
1751
1752 self.assertEqual(items[1].change.number, '2')
1753 self.assertEqual(items[1].change.patchset, '1')
1754 self.assertTrue(items[1].live)
1755
1756 self.assertEqual(items[2].change.number, '1')
1757 self.assertEqual(items[2].change.patchset, '1')
1758 self.assertTrue(items[2].live)
1759
1760 # Add a new patchset to A
1761 A.addPatchset()
1762 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1763 self.waitUntilSettled()
1764
1765 # The live copy of A,1 should be gone, but the non-live and B
1766 # should continue, and we should have a new A,2
1767 items = check_pipeline.getAllItems()
1768 self.assertEqual(len(items), 3)
1769
1770 self.assertEqual(items[0].change.number, '1')
1771 self.assertEqual(items[0].change.patchset, '1')
1772 self.assertFalse(items[0].live)
1773
1774 self.assertEqual(items[1].change.number, '2')
1775 self.assertEqual(items[1].change.patchset, '1')
1776 self.assertTrue(items[1].live)
1777
1778 self.assertEqual(items[2].change.number, '1')
1779 self.assertEqual(items[2].change.patchset, '2')
1780 self.assertTrue(items[2].live)
1781
1782 # Add a new patchset to B
1783 B.addPatchset()
1784 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1785 self.waitUntilSettled()
1786
1787 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1788 # but we should have a new B,2 (still based on A,1)
1789 items = check_pipeline.getAllItems()
1790 self.assertEqual(len(items), 3)
1791
1792 self.assertEqual(items[0].change.number, '1')
1793 self.assertEqual(items[0].change.patchset, '2')
1794 self.assertTrue(items[0].live)
1795
1796 self.assertEqual(items[1].change.number, '1')
1797 self.assertEqual(items[1].change.patchset, '1')
1798 self.assertFalse(items[1].live)
1799
1800 self.assertEqual(items[2].change.number, '2')
1801 self.assertEqual(items[2].change.patchset, '2')
1802 self.assertTrue(items[2].live)
1803
1804 self.builds[0].release()
1805 self.waitUntilSettled()
1806 self.builds[0].release()
1807 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001808 self.executor_server.hold_jobs_in_build = False
1809 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001810 self.waitUntilSettled()
1811
1812 self.assertEqual(A.reported, 1)
1813 self.assertEqual(B.reported, 1)
1814 self.assertEqual(self.history[0].result, 'ABORTED')
1815 self.assertEqual(self.history[0].changes, '1,1')
1816 self.assertEqual(self.history[1].result, 'ABORTED')
1817 self.assertEqual(self.history[1].changes, '1,1 2,1')
1818 self.assertEqual(self.history[2].result, 'SUCCESS')
1819 self.assertEqual(self.history[2].changes, '1,2')
1820 self.assertEqual(self.history[3].result, 'SUCCESS')
1821 self.assertEqual(self.history[3].changes, '1,1 2,2')
1822
1823 def test_abandoned_gate(self):
1824 "Test that an abandoned change is dequeued from gate"
1825
Paul Belanger174a8272017-03-14 13:20:10 -04001826 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001827
1828 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001829 A.addApproval('Code-Review', 2)
1830 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001831 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001832 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1833 self.assertEqual(self.builds[0].name, 'project-merge')
1834
1835 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1836 self.waitUntilSettled()
1837
Paul Belanger174a8272017-03-14 13:20:10 -04001838 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001839 self.waitUntilSettled()
1840
Arie5b4048c2016-10-05 16:12:06 +03001841 self.assertBuilds([])
1842 self.assertHistory([
1843 dict(name='project-merge', result='ABORTED', changes='1,1')],
1844 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001845 self.assertEqual(A.reported, 1,
1846 "Abandoned gate change should report only start")
1847
1848 def test_abandoned_check(self):
1849 "Test that an abandoned change is dequeued from check"
1850
Paul Belanger174a8272017-03-14 13:20:10 -04001851 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001852
1853 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1854 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001855 tenant = self.sched.abide.tenants.get('tenant-one')
1856 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001857
1858 # Add two git-dependent changes
1859 B.setDependsOn(A, 1)
1860 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1861 self.waitUntilSettled()
1862 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1863 self.waitUntilSettled()
1864 # A live item, and a non-live/live pair
1865 items = check_pipeline.getAllItems()
1866 self.assertEqual(len(items), 3)
1867
1868 self.assertEqual(items[0].change.number, '1')
1869 self.assertFalse(items[0].live)
1870
1871 self.assertEqual(items[1].change.number, '2')
1872 self.assertTrue(items[1].live)
1873
1874 self.assertEqual(items[2].change.number, '1')
1875 self.assertTrue(items[2].live)
1876
1877 # Abandon A
1878 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1879 self.waitUntilSettled()
1880
1881 # The live copy of A should be gone, but the non-live and B
1882 # should continue
1883 items = check_pipeline.getAllItems()
1884 self.assertEqual(len(items), 2)
1885
1886 self.assertEqual(items[0].change.number, '1')
1887 self.assertFalse(items[0].live)
1888
1889 self.assertEqual(items[1].change.number, '2')
1890 self.assertTrue(items[1].live)
1891
Paul Belanger174a8272017-03-14 13:20:10 -04001892 self.executor_server.hold_jobs_in_build = False
1893 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001894 self.waitUntilSettled()
1895
1896 self.assertEqual(len(self.history), 4)
1897 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001898 'Build should have been aborted')
1899 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001900 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001901
Steve Varnau7b78b312015-04-03 14:49:46 -07001902 def test_abandoned_not_timer(self):
1903 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001904 # This test can not use simple_layout because it must start
1905 # with a configuration which does not include a
1906 # timer-triggered job so that we have an opportunity to set
1907 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001908 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001909 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001910 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001911 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001912 # The pipeline triggers every second, so we should have seen
1913 # several by now.
1914 time.sleep(5)
1915 self.waitUntilSettled()
1916 # Stop queuing timer triggered jobs so that the assertions
1917 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001918 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001919 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001920 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001921 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001922 # If APScheduler is in mid-event when we remove the job, we
1923 # can end up with one more event firing, so give it an extra
1924 # second to settle.
1925 time.sleep(1)
1926 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001927
James E. Blair21037782017-07-19 11:56:55 -07001928 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001929
1930 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1931 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1932 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001933 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001934
1935 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1936 self.waitUntilSettled()
1937
James E. Blair21037782017-07-19 11:56:55 -07001938 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001939
Paul Belanger174a8272017-03-14 13:20:10 -04001940 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001941 self.waitUntilSettled()
1942
James E. Blair2fa50962013-01-30 21:50:41 -08001943 def test_new_patchset_dequeues_old_on_head(self):
1944 "Test that a new patchset causes the old to be dequeued (at head)"
1945 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001946 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001947 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1948 M.setMerged()
1949 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1950 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1951 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1952 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001953 A.addApproval('Code-Review', 2)
1954 B.addApproval('Code-Review', 2)
1955 C.addApproval('Code-Review', 2)
1956 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001957
1958 C.setDependsOn(B, 1)
1959 B.setDependsOn(A, 1)
1960 A.setDependsOn(M, 1)
1961
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001962 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1963 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1964 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1965 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001966 self.waitUntilSettled()
1967
1968 A.addPatchset()
1969 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1970 self.waitUntilSettled()
1971
Paul Belanger174a8272017-03-14 13:20:10 -04001972 self.executor_server.hold_jobs_in_build = False
1973 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001974 self.waitUntilSettled()
1975
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001976 self.assertEqual(A.data['status'], 'NEW')
1977 self.assertEqual(A.reported, 2)
1978 self.assertEqual(B.data['status'], 'NEW')
1979 self.assertEqual(B.reported, 2)
1980 self.assertEqual(C.data['status'], 'NEW')
1981 self.assertEqual(C.reported, 2)
1982 self.assertEqual(D.data['status'], 'MERGED')
1983 self.assertEqual(D.reported, 2)
1984 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001985
1986 def test_new_patchset_dequeues_old_without_dependents(self):
1987 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001988 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001989 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1990 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1991 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001992 A.addApproval('Code-Review', 2)
1993 B.addApproval('Code-Review', 2)
1994 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001995
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001996 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1997 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1998 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001999 self.waitUntilSettled()
2000
2001 B.addPatchset()
2002 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2003 self.waitUntilSettled()
2004
Paul Belanger174a8272017-03-14 13:20:10 -04002005 self.executor_server.hold_jobs_in_build = False
2006 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002007 self.waitUntilSettled()
2008
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002009 self.assertEqual(A.data['status'], 'MERGED')
2010 self.assertEqual(A.reported, 2)
2011 self.assertEqual(B.data['status'], 'NEW')
2012 self.assertEqual(B.reported, 2)
2013 self.assertEqual(C.data['status'], 'MERGED')
2014 self.assertEqual(C.reported, 2)
2015 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002016
2017 def test_new_patchset_dequeues_old_independent_queue(self):
2018 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002019 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002020 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2021 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2022 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2023 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2024 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2025 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2026 self.waitUntilSettled()
2027
2028 B.addPatchset()
2029 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2030 self.waitUntilSettled()
2031
Paul Belanger174a8272017-03-14 13:20:10 -04002032 self.executor_server.hold_jobs_in_build = False
2033 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002034 self.waitUntilSettled()
2035
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002036 self.assertEqual(A.data['status'], 'NEW')
2037 self.assertEqual(A.reported, 1)
2038 self.assertEqual(B.data['status'], 'NEW')
2039 self.assertEqual(B.reported, 1)
2040 self.assertEqual(C.data['status'], 'NEW')
2041 self.assertEqual(C.reported, 1)
2042 self.assertEqual(len(self.history), 10)
2043 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002044
James E. Blaircc020532017-04-19 13:04:44 -07002045 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002046 def test_noop_job(self):
2047 "Test that the internal noop job works"
2048 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002049 A.addApproval('Code-Review', 2)
2050 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002051 self.waitUntilSettled()
2052
2053 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2054 self.assertTrue(self.sched._areAllBuildsComplete())
2055 self.assertEqual(len(self.history), 0)
2056 self.assertEqual(A.data['status'], 'MERGED')
2057 self.assertEqual(A.reported, 2)
2058
James E. Blair06cc3922017-04-19 10:08:10 -07002059 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002060 def test_no_job_project(self):
2061 "Test that reports with no jobs don't get sent"
2062 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2063 'master', 'A')
2064 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2065 self.waitUntilSettled()
2066
2067 # Change wasn't reported to
2068 self.assertEqual(A.reported, False)
2069
2070 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002071 tenant = self.sched.abide.tenants.get('tenant-one')
2072 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002073 items = check_pipeline.getAllItems()
2074 self.assertEqual(len(items), 0)
2075
2076 self.assertEqual(len(self.history), 0)
2077
James E. Blair7d0dedc2013-02-21 17:26:09 -08002078 def test_zuul_refs(self):
2079 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002080 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002081 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2082 M1.setMerged()
2083 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2084 M2.setMerged()
2085
2086 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2087 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2088 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2089 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002090 A.addApproval('Code-Review', 2)
2091 B.addApproval('Code-Review', 2)
2092 C.addApproval('Code-Review', 2)
2093 D.addApproval('Code-Review', 2)
2094 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2095 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2096 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2097 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002098
2099 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002100 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002101 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002102 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002103 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002104 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002105 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002106 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002107 self.waitUntilSettled()
2108
Clint Byrum3343e3e2016-11-15 16:05:03 -08002109 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002110 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002111 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002112 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002113 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002114 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002115 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002116 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002117 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002118 d_build = x
2119 if a_build and b_build and c_build and d_build:
2120 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002121
Clint Byrum3343e3e2016-11-15 16:05:03 -08002122 # should have a, not b, and should not be in project2
2123 self.assertTrue(a_build.hasChanges(A))
2124 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002125
Clint Byrum3343e3e2016-11-15 16:05:03 -08002126 # should have a and b, and should not be in project2
2127 self.assertTrue(b_build.hasChanges(A, B))
2128 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002129
Clint Byrum3343e3e2016-11-15 16:05:03 -08002130 # should have a and b in 1, c in 2
2131 self.assertTrue(c_build.hasChanges(A, B, C))
2132 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002133
Clint Byrum3343e3e2016-11-15 16:05:03 -08002134 # should have a and b in 1, c and d in 2
2135 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002136
Paul Belanger174a8272017-03-14 13:20:10 -04002137 self.executor_server.hold_jobs_in_build = False
2138 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002139 self.waitUntilSettled()
2140
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002141 self.assertEqual(A.data['status'], 'MERGED')
2142 self.assertEqual(A.reported, 2)
2143 self.assertEqual(B.data['status'], 'MERGED')
2144 self.assertEqual(B.reported, 2)
2145 self.assertEqual(C.data['status'], 'MERGED')
2146 self.assertEqual(C.reported, 2)
2147 self.assertEqual(D.data['status'], 'MERGED')
2148 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002149
James E. Blair4a28a882013-08-23 15:17:33 -07002150 def test_rerun_on_error(self):
2151 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002152 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002154 A.addApproval('Code-Review', 2)
2155 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002156 self.waitUntilSettled()
2157
James E. Blair412fba82017-01-26 15:00:50 -08002158 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002159 self.executor_server.hold_jobs_in_build = False
2160 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002161 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002162 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002163 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2164
James E. Blair412e5582013-04-22 15:50:12 -07002165 def test_statsd(self):
2166 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002167 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002168 statsd.incr('test-incr')
2169 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002170 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002171 self.assertReportedStat('test-incr', '1|c')
2172 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002173 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002174
James E. Blairdad52252014-02-07 16:59:17 -08002175 def test_stuck_job_cleanup(self):
2176 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002177
2178 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002179 self.gearman_server.hold_jobs_in_queue = True
2180 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002181 A.addApproval('Code-Review', 2)
2182 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002183 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002184 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002185 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2186
James E. Blair31cb3e82017-04-20 13:08:56 -07002187 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002188 self.sched.reconfigure(self.config)
2189 self.waitUntilSettled()
2190
James E. Blair18c64442014-03-18 10:14:45 -07002191 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002192 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002193 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002194 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2195 self.assertTrue(self.sched._areAllBuildsComplete())
2196
2197 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002198 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002199 self.assertEqual(self.history[0].result, 'SUCCESS')
2200
James E. Blair879dafb2015-07-17 14:04:49 -07002201 def test_file_head(self):
2202 # This is a regression test for an observed bug. A change
2203 # with a file named "HEAD" in the root directory of the repo
2204 # was processed by a merger. It then was unable to reset the
2205 # repo because of:
2206 # GitCommandError: 'git reset --hard HEAD' returned
2207 # with exit code 128
2208 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2209 # and filename
2210 # Use '--' to separate filenames from revisions'
2211
2212 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002213 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002214 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2215
2216 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2217 self.waitUntilSettled()
2218
2219 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2220 self.waitUntilSettled()
2221
2222 self.assertIn('Build succeeded', A.messages[0])
2223 self.assertIn('Build succeeded', B.messages[0])
2224
James E. Blair70c71582013-03-06 08:50:50 -08002225 def test_file_jobs(self):
2226 "Test that file jobs run only when appropriate"
2227 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002228 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002229 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002230 A.addApproval('Code-Review', 2)
2231 B.addApproval('Code-Review', 2)
2232 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2233 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002234 self.waitUntilSettled()
2235
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002236 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002237 if x.name == 'project-testfile']
2238
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002239 self.assertEqual(len(testfile_jobs), 1)
2240 self.assertEqual(testfile_jobs[0].changes, '1,2')
2241 self.assertEqual(A.data['status'], 'MERGED')
2242 self.assertEqual(A.reported, 2)
2243 self.assertEqual(B.data['status'], 'MERGED')
2244 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002245
Clint Byrum3ee39f32016-11-17 23:45:07 -08002246 def _test_irrelevant_files_jobs(self, should_skip):
2247 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002248 if should_skip:
2249 files = {'ignoreme': 'ignored\n'}
2250 else:
2251 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002252
2253 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002254 'master',
2255 'test irrelevant-files',
2256 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002257 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2258 self.waitUntilSettled()
2259
2260 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002261 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002262
2263 if should_skip:
2264 self.assertEqual([], tested_change_ids)
2265 else:
2266 self.assertIn(change.data['number'], tested_change_ids)
2267
James E. Blaircc324192017-04-19 14:36:08 -07002268 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002269 def test_irrelevant_files_match_skips_job(self):
2270 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002271
James E. Blaircc324192017-04-19 14:36:08 -07002272 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002273 def test_irrelevant_files_no_match_runs_job(self):
2274 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002275
James E. Blaircc324192017-04-19 14:36:08 -07002276 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002277 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002278 files = {'ignoreme': 'ignored\n'}
2279
2280 change = self.fake_gerrit.addFakeChange('org/project',
2281 'master',
2282 'test irrelevant-files',
2283 files=files)
2284 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2285 self.waitUntilSettled()
2286
2287 run_jobs = set([build.name for build in self.history])
2288
2289 self.assertEqual(set(['project-test-nomatch-starts-empty',
2290 'project-test-nomatch-starts-full']), run_jobs)
2291
James E. Blairaf8b2082017-10-03 15:38:27 -07002292 @simple_layout('layouts/job-vars.yaml')
2293 def test_inherited_job_variables(self):
2294 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2296 self.waitUntilSettled()
2297 self.assertHistory([
2298 dict(name='parentjob', result='SUCCESS'),
2299 dict(name='child1', result='SUCCESS'),
2300 dict(name='child2', result='SUCCESS'),
2301 dict(name='child3', result='SUCCESS'),
2302 ], ordered=False)
2303 j = self.getJobFromHistory('parentjob')
2304 rp = set([p['name'] for p in j.parameters['projects']])
2305 self.assertEqual(j.parameters['vars']['override'], 0)
2306 self.assertEqual(j.parameters['vars']['child1override'], 0)
2307 self.assertEqual(j.parameters['vars']['parent'], 0)
2308 self.assertFalse('child1' in j.parameters['vars'])
2309 self.assertFalse('child2' in j.parameters['vars'])
2310 self.assertFalse('child3' in j.parameters['vars'])
2311 self.assertEqual(rp, set(['org/project', 'org/project0',
2312 'org/project0']))
2313 j = self.getJobFromHistory('child1')
2314 rp = set([p['name'] for p in j.parameters['projects']])
2315 self.assertEqual(j.parameters['vars']['override'], 1)
2316 self.assertEqual(j.parameters['vars']['child1override'], 1)
2317 self.assertEqual(j.parameters['vars']['parent'], 0)
2318 self.assertEqual(j.parameters['vars']['child1'], 1)
2319 self.assertFalse('child2' in j.parameters['vars'])
2320 self.assertFalse('child3' in j.parameters['vars'])
2321 self.assertEqual(rp, set(['org/project', 'org/project0',
2322 'org/project1']))
2323 j = self.getJobFromHistory('child2')
2324 rp = set([p['name'] for p in j.parameters['projects']])
2325 self.assertEqual(j.parameters['vars']['override'], 2)
2326 self.assertEqual(j.parameters['vars']['child1override'], 0)
2327 self.assertEqual(j.parameters['vars']['parent'], 0)
2328 self.assertFalse('child1' in j.parameters['vars'])
2329 self.assertEqual(j.parameters['vars']['child2'], 2)
2330 self.assertFalse('child3' in j.parameters['vars'])
2331 self.assertEqual(rp, set(['org/project', 'org/project0',
2332 'org/project2']))
2333 j = self.getJobFromHistory('child3')
2334 rp = set([p['name'] for p in j.parameters['projects']])
2335 self.assertEqual(j.parameters['vars']['override'], 3)
2336 self.assertEqual(j.parameters['vars']['child1override'], 0)
2337 self.assertEqual(j.parameters['vars']['parent'], 0)
2338 self.assertFalse('child1' in j.parameters['vars'])
2339 self.assertFalse('child2' in j.parameters['vars'])
2340 self.assertEqual(j.parameters['vars']['child3'], 3)
2341 self.assertEqual(rp, set(['org/project', 'org/project0',
2342 'org/project3']))
2343
James E. Blairc8a1e052014-02-25 09:29:26 -08002344 def test_queue_names(self):
2345 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002346 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002347 (trusted, project1) = tenant.getProject('org/project1')
2348 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002349 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2350 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2351 self.assertEqual(q1.name, 'integrated')
2352 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002353
James E. Blair64ed6f22013-07-10 14:07:23 -07002354 def test_queue_precedence(self):
2355 "Test that queue precedence works"
2356
2357 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002358 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002359 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2360 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002361 A.addApproval('Code-Review', 2)
2362 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002363
2364 self.waitUntilSettled()
2365 self.gearman_server.hold_jobs_in_queue = False
2366 self.gearman_server.release()
2367 self.waitUntilSettled()
2368
James E. Blair8de58bd2013-07-18 16:23:33 -07002369 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002370 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002371 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002372 self.waitUntilSettled()
2373
James E. Blair64ed6f22013-07-10 14:07:23 -07002374 self.log.debug(self.history)
2375 self.assertEqual(self.history[0].pipeline, 'gate')
2376 self.assertEqual(self.history[1].pipeline, 'check')
2377 self.assertEqual(self.history[2].pipeline, 'gate')
2378 self.assertEqual(self.history[3].pipeline, 'gate')
2379 self.assertEqual(self.history[4].pipeline, 'check')
2380 self.assertEqual(self.history[5].pipeline, 'check')
2381
Clark Boylana5edbe42014-06-03 16:39:10 -07002382 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002383 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002384 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002385 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002386 A.addApproval('Code-Review', 2)
2387 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002388 self.waitUntilSettled()
2389
Paul Belanger174a8272017-03-14 13:20:10 -04002390 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002391 self.waitUntilSettled()
2392
James E. Blair1843a552013-07-03 14:19:52 -07002393 port = self.webapp.server.socket.getsockname()[1]
2394
Adam Gandelman77a12c72017-02-03 17:43:43 -08002395 req = urllib.request.Request(
2396 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002397 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002398 headers = f.info()
2399 self.assertIn('Content-Length', headers)
2400 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002401 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2402 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002403 self.assertIn('Access-Control-Allow-Origin', headers)
2404 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002405 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002406 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002407 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002408
Paul Belanger174a8272017-03-14 13:20:10 -04002409 self.executor_server.hold_jobs_in_build = False
2410 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002411 self.waitUntilSettled()
2412
2413 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002414 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002415 for p in data['pipelines']:
2416 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002417 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002418 self.assertEqual(q['window'], 20)
2419 else:
2420 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002421 for head in q['heads']:
2422 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002423 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002424 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002425 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002426 status_jobs.append(job)
2427 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002428 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002429 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002430 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2431 uuid=status_jobs[0]['uuid']),
2432 status_jobs[0]['url'])
2433 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002434 'finger://{hostname}/{uuid}'.format(
2435 hostname=self.executor_server.hostname,
2436 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002437 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002438 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002439 self.assertEqual(
2440 'finger://{hostname}/{uuid}'.format(
2441 hostname=self.executor_server.hostname,
2442 uuid=status_jobs[0]['uuid']),
2443 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002444 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002445 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002446 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2447 uuid=status_jobs[1]['uuid']),
2448 status_jobs[1]['url'])
2449 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002450 'finger://{hostname}/{uuid}'.format(
2451 hostname=self.executor_server.hostname,
2452 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002453 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002454 self.assertEqual(
2455 'finger://{hostname}/{uuid}'.format(
2456 hostname=self.executor_server.hostname,
2457 uuid=status_jobs[1]['uuid']),
2458 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002459
2460 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002461 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002462 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2463 uuid=status_jobs[2]['uuid']),
2464 status_jobs[2]['url'])
2465 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002466 'finger://{hostname}/{uuid}'.format(
2467 hostname=self.executor_server.hostname,
2468 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002469 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002470 self.assertEqual(
2471 'finger://{hostname}/{uuid}'.format(
2472 hostname=self.executor_server.hostname,
2473 uuid=status_jobs[2]['uuid']),
2474 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002475
Tobias Henkel65639f82017-07-10 10:25:42 +02002476 # check job dependencies
2477 self.assertIsNotNone(status_jobs[0]['dependencies'])
2478 self.assertIsNotNone(status_jobs[1]['dependencies'])
2479 self.assertIsNotNone(status_jobs[2]['dependencies'])
2480 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2481 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2482 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2483 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2484 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2485
James E. Blaircdccd972013-07-01 12:10:22 -07002486 def test_live_reconfiguration(self):
2487 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002488 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002489 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002490 A.addApproval('Code-Review', 2)
2491 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002492 self.waitUntilSettled()
2493
2494 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002495 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002496
Paul Belanger174a8272017-03-14 13:20:10 -04002497 self.executor_server.hold_jobs_in_build = False
2498 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002499 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002500 self.assertEqual(self.getJobFromHistory('project-merge').result,
2501 'SUCCESS')
2502 self.assertEqual(self.getJobFromHistory('project-test1').result,
2503 'SUCCESS')
2504 self.assertEqual(self.getJobFromHistory('project-test2').result,
2505 'SUCCESS')
2506 self.assertEqual(A.data['status'], 'MERGED')
2507 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002508
James E. Blair027ba992017-09-20 13:48:32 -07002509 def test_live_reconfiguration_abort(self):
2510 # Raise an exception during reconfiguration and verify we
2511 # still function.
2512 self.executor_server.hold_jobs_in_build = True
2513 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2514 A.addApproval('Code-Review', 2)
2515 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2516 self.waitUntilSettled()
2517
2518 tenant = self.sched.abide.tenants.get('tenant-one')
2519 pipeline = tenant.layout.pipelines['gate']
2520 change = pipeline.getAllItems()[0].change
2521 # Set this to an invalid value to cause an exception during
2522 # reconfiguration.
2523 change.branch = None
2524
2525 self.sched.reconfigure(self.config)
2526 self.waitUntilSettled()
2527
2528 self.executor_server.hold_jobs_in_build = False
2529 self.executor_server.release()
2530
2531 self.waitUntilSettled()
2532 self.assertEqual(self.getJobFromHistory('project-merge').result,
2533 'ABORTED')
2534 self.assertEqual(A.data['status'], 'NEW')
2535 # The final report fails because of the invalid value set above.
2536 self.assertEqual(A.reported, 1)
2537
James E. Blair6bc782d2015-07-17 16:20:21 -07002538 def test_live_reconfiguration_merge_conflict(self):
2539 # A real-world bug: a change in a gate queue has a merge
2540 # conflict and a job is added to its project while it's
2541 # sitting in the queue. The job gets added to the change and
2542 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002543 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002544
2545 # This change is fine. It's here to stop the queue long
2546 # enough for the next change to be subject to the
2547 # reconfiguration, as well as to provide a conflict for the
2548 # next change. This change will succeed and merge.
2549 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002550 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002551 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002552
2553 # This change will be in merge conflict. During the
2554 # reconfiguration, we will add a job. We want to make sure
2555 # that doesn't cause it to get stuck.
2556 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002557 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002558 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002559
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002560 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2561 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002562
2563 self.waitUntilSettled()
2564
2565 # No jobs have run yet
2566 self.assertEqual(A.data['status'], 'NEW')
2567 self.assertEqual(A.reported, 1)
2568 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002569 self.assertEqual(len(self.history), 0)
2570
2571 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002572 self.commitConfigUpdate('common-config',
2573 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002574 self.sched.reconfigure(self.config)
2575 self.waitUntilSettled()
2576
Paul Belanger174a8272017-03-14 13:20:10 -04002577 self.executor_server.hold_jobs_in_build = False
2578 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002579 self.waitUntilSettled()
2580
2581 self.assertEqual(A.data['status'], 'MERGED')
2582 self.assertEqual(A.reported, 2)
2583 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002584 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002585 self.assertEqual(self.getJobFromHistory('project-merge').result,
2586 'SUCCESS')
2587 self.assertEqual(self.getJobFromHistory('project-test1').result,
2588 'SUCCESS')
2589 self.assertEqual(self.getJobFromHistory('project-test2').result,
2590 'SUCCESS')
2591 self.assertEqual(self.getJobFromHistory('project-test3').result,
2592 'SUCCESS')
2593 self.assertEqual(len(self.history), 4)
2594
James E. Blair400e8fd2015-07-30 17:44:45 -07002595 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002596 # An extrapolation of test_live_reconfiguration_merge_conflict
2597 # that tests a job added to a job tree with a failed root does
2598 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002599 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002600
2601 # This change is fine. It's here to stop the queue long
2602 # enough for the next change to be subject to the
2603 # reconfiguration. This change will succeed and merge.
2604 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002605 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002606 A.addApproval('Code-Review', 2)
2607 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002608 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002609 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002610 self.waitUntilSettled()
2611
2612 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002613 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002614 B.addApproval('Code-Review', 2)
2615 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002616 self.waitUntilSettled()
2617
Paul Belanger174a8272017-03-14 13:20:10 -04002618 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002619 self.waitUntilSettled()
2620
2621 # Both -merge jobs have run, but no others.
2622 self.assertEqual(A.data['status'], 'NEW')
2623 self.assertEqual(A.reported, 1)
2624 self.assertEqual(B.data['status'], 'NEW')
2625 self.assertEqual(B.reported, 1)
2626 self.assertEqual(self.history[0].result, 'SUCCESS')
2627 self.assertEqual(self.history[0].name, 'project-merge')
2628 self.assertEqual(self.history[1].result, 'FAILURE')
2629 self.assertEqual(self.history[1].name, 'project-merge')
2630 self.assertEqual(len(self.history), 2)
2631
2632 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002633 self.commitConfigUpdate('common-config',
2634 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002635 self.sched.reconfigure(self.config)
2636 self.waitUntilSettled()
2637
Paul Belanger174a8272017-03-14 13:20:10 -04002638 self.executor_server.hold_jobs_in_build = False
2639 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002640 self.waitUntilSettled()
2641
2642 self.assertEqual(A.data['status'], 'MERGED')
2643 self.assertEqual(A.reported, 2)
2644 self.assertEqual(B.data['status'], 'NEW')
2645 self.assertEqual(B.reported, 2)
2646 self.assertEqual(self.history[0].result, 'SUCCESS')
2647 self.assertEqual(self.history[0].name, 'project-merge')
2648 self.assertEqual(self.history[1].result, 'FAILURE')
2649 self.assertEqual(self.history[1].name, 'project-merge')
2650 self.assertEqual(self.history[2].result, 'SUCCESS')
2651 self.assertEqual(self.history[3].result, 'SUCCESS')
2652 self.assertEqual(self.history[4].result, 'SUCCESS')
2653 self.assertEqual(len(self.history), 5)
2654
James E. Blair400e8fd2015-07-30 17:44:45 -07002655 def test_live_reconfiguration_failed_job(self):
2656 # Test that a change with a removed failing job does not
2657 # disrupt reconfiguration. If a change has a failed job and
2658 # that job is removed during a reconfiguration, we observed a
2659 # bug where the code to re-set build statuses would run on
2660 # that build and raise an exception because the job no longer
2661 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002662 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002663
2664 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2665
2666 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002667 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002668
2669 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2670 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002671 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002672 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002673 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002674 self.waitUntilSettled()
2675
2676 self.assertEqual(A.data['status'], 'NEW')
2677 self.assertEqual(A.reported, 0)
2678
2679 self.assertEqual(self.getJobFromHistory('project-merge').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory('project-test1').result,
2682 'FAILURE')
2683 self.assertEqual(len(self.history), 2)
2684
2685 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002686 self.commitConfigUpdate('common-config',
2687 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002688 self.sched.reconfigure(self.config)
2689 self.waitUntilSettled()
2690
Paul Belanger174a8272017-03-14 13:20:10 -04002691 self.executor_server.hold_jobs_in_build = False
2692 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002693 self.waitUntilSettled()
2694
2695 self.assertEqual(self.getJobFromHistory('project-test2').result,
2696 'SUCCESS')
2697 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2698 'SUCCESS')
2699 self.assertEqual(len(self.history), 4)
2700
2701 self.assertEqual(A.data['status'], 'NEW')
2702 self.assertEqual(A.reported, 1)
2703 self.assertIn('Build succeeded', A.messages[0])
2704 # Ensure the removed job was not included in the report.
2705 self.assertNotIn('project-test1', A.messages[0])
2706
James E. Blairfe707d12015-08-05 15:18:15 -07002707 def test_live_reconfiguration_shared_queue(self):
2708 # Test that a change with a failing job which was removed from
2709 # this project but otherwise still exists in the system does
2710 # not disrupt reconfiguration.
2711
Paul Belanger174a8272017-03-14 13:20:10 -04002712 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002713
2714 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2715
Paul Belanger174a8272017-03-14 13:20:10 -04002716 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002717
2718 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2719 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002720 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002721 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002722 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002723 self.waitUntilSettled()
2724
2725 self.assertEqual(A.data['status'], 'NEW')
2726 self.assertEqual(A.reported, 0)
2727
Clint Byrum69663d42017-05-01 14:51:04 -07002728 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002729 'SUCCESS')
2730 self.assertEqual(self.getJobFromHistory(
2731 'project1-project2-integration').result, 'FAILURE')
2732 self.assertEqual(len(self.history), 2)
2733
2734 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002735 self.commitConfigUpdate(
2736 'common-config',
2737 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002738 self.sched.reconfigure(self.config)
2739 self.waitUntilSettled()
2740
Paul Belanger174a8272017-03-14 13:20:10 -04002741 self.executor_server.hold_jobs_in_build = False
2742 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002743 self.waitUntilSettled()
2744
Clint Byrum69663d42017-05-01 14:51:04 -07002745 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002746 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002747 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002748 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002749 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002750 'SUCCESS')
2751 self.assertEqual(self.getJobFromHistory(
2752 'project1-project2-integration').result, 'FAILURE')
2753 self.assertEqual(len(self.history), 4)
2754
2755 self.assertEqual(A.data['status'], 'NEW')
2756 self.assertEqual(A.reported, 1)
2757 self.assertIn('Build succeeded', A.messages[0])
2758 # Ensure the removed job was not included in the report.
2759 self.assertNotIn('project1-project2-integration', A.messages[0])
2760
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002761 def test_double_live_reconfiguration_shared_queue(self):
2762 # This was a real-world regression. A change is added to
2763 # gate; a reconfigure happens, a second change which depends
2764 # on the first is added, and a second reconfiguration happens.
2765 # Ensure that both changes merge.
2766
2767 # A failure may indicate incorrect caching or cleaning up of
2768 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002769 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002770
2771 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2772 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2773 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002774 A.addApproval('Code-Review', 2)
2775 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002776
2777 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002778 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002779 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002780 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002781 self.waitUntilSettled()
2782
2783 # Reconfigure (with only one change in the pipeline).
2784 self.sched.reconfigure(self.config)
2785 self.waitUntilSettled()
2786
2787 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002788 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002789 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002790 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002791 self.waitUntilSettled()
2792
2793 # Reconfigure (with both in the pipeline).
2794 self.sched.reconfigure(self.config)
2795 self.waitUntilSettled()
2796
Paul Belanger174a8272017-03-14 13:20:10 -04002797 self.executor_server.hold_jobs_in_build = False
2798 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002799 self.waitUntilSettled()
2800
2801 self.assertEqual(len(self.history), 8)
2802
2803 self.assertEqual(A.data['status'], 'MERGED')
2804 self.assertEqual(A.reported, 2)
2805 self.assertEqual(B.data['status'], 'MERGED')
2806 self.assertEqual(B.reported, 2)
2807
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002808 def test_live_reconfiguration_del_project(self):
2809 # Test project deletion from layout
2810 # while changes are enqueued
2811
Paul Belanger174a8272017-03-14 13:20:10 -04002812 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002813 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2814 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2815 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2816
2817 # A Depends-On: B
2818 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2819 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002820 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002821
2822 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2823 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2824 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002825 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002826 self.waitUntilSettled()
2827 self.assertEqual(len(self.builds), 5)
2828
2829 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002830 self.commitConfigUpdate(
2831 'common-config',
2832 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002833 self.sched.reconfigure(self.config)
2834 self.waitUntilSettled()
2835
2836 # Builds for C aborted, builds for A succeed,
2837 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002838 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002839 self.assertEqual(job_c.changes, '3,1')
2840 self.assertEqual(job_c.result, 'ABORTED')
2841
Paul Belanger174a8272017-03-14 13:20:10 -04002842 self.executor_server.hold_jobs_in_build = False
2843 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002844 self.waitUntilSettled()
2845
Clint Byrum0149ada2017-04-04 12:22:01 -07002846 self.assertEqual(
2847 self.getJobFromHistory('project-test1', 'org/project').changes,
2848 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002849
2850 self.assertEqual(A.data['status'], 'NEW')
2851 self.assertEqual(B.data['status'], 'NEW')
2852 self.assertEqual(C.data['status'], 'NEW')
2853 self.assertEqual(A.reported, 1)
2854 self.assertEqual(B.reported, 0)
2855 self.assertEqual(C.reported, 0)
2856
Clint Byrum0149ada2017-04-04 12:22:01 -07002857 tenant = self.sched.abide.tenants.get('tenant-one')
2858 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002859 self.assertIn('Build succeeded', A.messages[0])
2860
James E. Blair287c06d2013-07-24 10:39:30 -07002861 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002862 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002863 files = {'README': ''}
2864 self.addCommitToRepo("org/new-project", 'Initial commit',
2865 files=files, tag='init')
2866 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2867 self.commitConfigUpdate(
2868 'common-config',
2869 'layouts/delayed-repo-init.yaml')
2870 self.sched.reconfigure(self.config)
2871 self.waitUntilSettled()
2872
James E. Blair287c06d2013-07-24 10:39:30 -07002873 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2874
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002875 A.addApproval('Code-Review', 2)
2876 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002877 self.waitUntilSettled()
2878 self.assertEqual(self.getJobFromHistory('project-merge').result,
2879 'SUCCESS')
2880 self.assertEqual(self.getJobFromHistory('project-test1').result,
2881 'SUCCESS')
2882 self.assertEqual(self.getJobFromHistory('project-test2').result,
2883 'SUCCESS')
2884 self.assertEqual(A.data['status'], 'MERGED')
2885 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002886
James E. Blair9ea70072017-04-19 16:05:30 -07002887 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002888 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002889 self.init_repo("org/delete-project")
2890 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2891
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002892 A.addApproval('Code-Review', 2)
2893 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002894 self.waitUntilSettled()
2895 self.assertEqual(self.getJobFromHistory('project-merge').result,
2896 'SUCCESS')
2897 self.assertEqual(self.getJobFromHistory('project-test1').result,
2898 'SUCCESS')
2899 self.assertEqual(self.getJobFromHistory('project-test2').result,
2900 'SUCCESS')
2901 self.assertEqual(A.data['status'], 'MERGED')
2902 self.assertEqual(A.reported, 2)
2903
2904 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002905 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002906 if os.path.exists(os.path.join(self.merger_src_root, p)):
2907 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002908 if os.path.exists(os.path.join(self.executor_src_root, p)):
2909 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002910
2911 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2912
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002913 B.addApproval('Code-Review', 2)
2914 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002915 self.waitUntilSettled()
2916 self.assertEqual(self.getJobFromHistory('project-merge').result,
2917 'SUCCESS')
2918 self.assertEqual(self.getJobFromHistory('project-test1').result,
2919 'SUCCESS')
2920 self.assertEqual(self.getJobFromHistory('project-test2').result,
2921 'SUCCESS')
2922 self.assertEqual(B.data['status'], 'MERGED')
2923 self.assertEqual(B.reported, 2)
2924
James E. Blair892cca62017-08-09 11:36:58 -07002925 @simple_layout('layouts/untrusted-secrets.yaml')
2926 def test_untrusted_secrets(self):
2927 "Test untrusted secrets"
2928 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2929 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2930 self.waitUntilSettled()
2931
2932 self.assertHistory([])
2933 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002934 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002935 A.messages[0])
2936
James E. Blaire68d43f2017-04-19 14:03:30 -07002937 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002938 def test_tags(self):
2939 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002940 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2941 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2942 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2943 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2944 self.waitUntilSettled()
2945
James E. Blaire68d43f2017-04-19 14:03:30 -07002946 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002947
2948 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002949 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002950 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002951 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002952
2953 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002954 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002955 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002956
James E. Blair63bb0ef2013-07-29 17:14:51 -07002957 def test_timer(self):
2958 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002959 # This test can not use simple_layout because it must start
2960 # with a configuration which does not include a
2961 # timer-triggered job so that we have an opportunity to set
2962 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002963 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002964 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002965 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002966 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002967
Clark Boylan3ee090a2014-04-03 20:55:09 -07002968 # The pipeline triggers every second, so we should have seen
2969 # several by now.
2970 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002971 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002972
2973 self.assertEqual(len(self.builds), 2)
2974
James E. Blair63bb0ef2013-07-29 17:14:51 -07002975 port = self.webapp.server.socket.getsockname()[1]
2976
Clint Byrum5e729e92017-02-08 06:34:57 -08002977 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002978 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002979 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002980 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002981
Paul Belanger174a8272017-03-14 13:20:10 -04002982 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002983 # Stop queuing timer triggered jobs so that the assertions
2984 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002985 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002986 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002987 self.waitUntilSettled()
2988 # If APScheduler is in mid-event when we remove the job, we
2989 # can end up with one more event firing, so give it an extra
2990 # second to settle.
2991 time.sleep(1)
2992 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002993 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002994 self.waitUntilSettled()
2995
James E. Blair21037782017-07-19 11:56:55 -07002996 self.assertHistory([
2997 dict(name='project-bitrot', result='SUCCESS',
2998 ref='refs/heads/master'),
2999 dict(name='project-bitrot', result='SUCCESS',
3000 ref='refs/heads/stable'),
3001 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003002
3003 data = json.loads(data)
3004 status_jobs = set()
3005 for p in data['pipelines']:
3006 for q in p['change_queues']:
3007 for head in q['heads']:
3008 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07003009 for job in change['jobs']:
3010 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07003011 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003012
James E. Blair4f6033c2014-03-27 15:49:09 -07003013 def test_idle(self):
3014 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003015 # This test can not use simple_layout because it must start
3016 # with a configuration which does not include a
3017 # timer-triggered job so that we have an opportunity to set
3018 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003019 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003020
Clark Boylan3ee090a2014-04-03 20:55:09 -07003021 for x in range(1, 3):
3022 # Test that timer triggers periodic jobs even across
3023 # layout config reloads.
3024 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003025 self.commitConfigUpdate('common-config',
3026 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003027 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003028 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003029
Clark Boylan3ee090a2014-04-03 20:55:09 -07003030 # The pipeline triggers every second, so we should have seen
3031 # several by now.
3032 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003033
3034 # Stop queuing timer triggered jobs so that the assertions
3035 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003036 self.commitConfigUpdate('common-config',
3037 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003038 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003039 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003040 # If APScheduler is in mid-event when we remove the job,
3041 # we can end up with one more event firing, so give it an
3042 # extra second to settle.
3043 time.sleep(1)
3044 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003045 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003046 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003047 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003048 self.waitUntilSettled()
3049 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003050 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003051
James E. Blaircc324192017-04-19 14:36:08 -07003052 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003053 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003054 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3055 self.waitUntilSettled()
3056
3057 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3058 self.waitUntilSettled()
3059
James E. Blairff80a2f2013-12-27 13:24:06 -08003060 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003061
3062 # A.messages only holds what FakeGerrit places in it. Thus we
3063 # work on the knowledge of what the first message should be as
3064 # it is only configured to go to SMTP.
3065
3066 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003067 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003068 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003069 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003070 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003071 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003072
3073 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003074 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003075 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003076 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003077 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003078 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003079
James E. Blaire5910202013-12-27 09:50:31 -08003080 def test_timer_smtp(self):
3081 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003082 # This test can not use simple_layout because it must start
3083 # with a configuration which does not include a
3084 # timer-triggered job so that we have an opportunity to set
3085 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003086 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003087 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003088 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003089
Clark Boylan3ee090a2014-04-03 20:55:09 -07003090 # The pipeline triggers every second, so we should have seen
3091 # several by now.
3092 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003093 self.waitUntilSettled()
3094
Clark Boylan3ee090a2014-04-03 20:55:09 -07003095 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003096 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003097 self.waitUntilSettled()
3098 self.assertEqual(len(self.history), 2)
3099
James E. Blaire5910202013-12-27 09:50:31 -08003100 self.assertEqual(self.getJobFromHistory(
3101 'project-bitrot-stable-old').result, 'SUCCESS')
3102 self.assertEqual(self.getJobFromHistory(
3103 'project-bitrot-stable-older').result, 'SUCCESS')
3104
James E. Blairff80a2f2013-12-27 13:24:06 -08003105 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003106
3107 # A.messages only holds what FakeGerrit places in it. Thus we
3108 # work on the knowledge of what the first message should be as
3109 # it is only configured to go to SMTP.
3110
3111 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003112 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003113 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003114 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003115 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003116 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003117
Clark Boylan3ee090a2014-04-03 20:55:09 -07003118 # Stop queuing timer triggered jobs and let any that may have
3119 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003120 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003121 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003122 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003123 # If APScheduler is in mid-event when we remove the job, we
3124 # can end up with one more event firing, so give it an extra
3125 # second to settle.
3126 time.sleep(1)
3127 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003128 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003129 self.waitUntilSettled()
3130
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003131 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003132 def test_timer_sshkey(self):
3133 "Test that a periodic job can setup SSH key authentication"
3134 self.worker.hold_jobs_in_build = True
3135 self.config.set('zuul', 'layout_config',
3136 'tests/fixtures/layout-timer.yaml')
3137 self.sched.reconfigure(self.config)
3138 self.registerJobs()
3139
3140 # The pipeline triggers every second, so we should have seen
3141 # several by now.
3142 time.sleep(5)
3143 self.waitUntilSettled()
3144
3145 self.assertEqual(len(self.builds), 2)
3146
3147 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3148 self.assertTrue(os.path.isfile(ssh_wrapper))
3149 with open(ssh_wrapper) as f:
3150 ssh_wrapper_content = f.read()
3151 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3152 # In the unit tests Merger runs in the same process,
3153 # so we see its' environment variables
3154 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3155
3156 self.worker.release('.*')
3157 self.waitUntilSettled()
3158 self.assertEqual(len(self.history), 2)
3159
3160 self.assertEqual(self.getJobFromHistory(
3161 'project-bitrot-stable-old').result, 'SUCCESS')
3162 self.assertEqual(self.getJobFromHistory(
3163 'project-bitrot-stable-older').result, 'SUCCESS')
3164
3165 # Stop queuing timer triggered jobs and let any that may have
3166 # queued through so that end of test assertions pass.
3167 self.config.set('zuul', 'layout_config',
3168 'tests/fixtures/layout-no-timer.yaml')
3169 self.sched.reconfigure(self.config)
3170 self.registerJobs()
3171 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003172 # If APScheduler is in mid-event when we remove the job, we
3173 # can end up with one more event firing, so give it an extra
3174 # second to settle.
3175 time.sleep(1)
3176 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003177 self.worker.release('.*')
3178 self.waitUntilSettled()
3179
James E. Blair91e34592015-07-31 16:45:59 -07003180 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003181 "Test that the RPC client can enqueue a change"
3182 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003183 A.addApproval('Code-Review', 2)
3184 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003185
3186 client = zuul.rpcclient.RPCClient('127.0.0.1',
3187 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003188 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003189 r = client.enqueue(tenant='tenant-one',
3190 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003191 project='org/project',
3192 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003193 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003194 self.waitUntilSettled()
3195 self.assertEqual(self.getJobFromHistory('project-merge').result,
3196 'SUCCESS')
3197 self.assertEqual(self.getJobFromHistory('project-test1').result,
3198 'SUCCESS')
3199 self.assertEqual(self.getJobFromHistory('project-test2').result,
3200 'SUCCESS')
3201 self.assertEqual(A.data['status'], 'MERGED')
3202 self.assertEqual(A.reported, 2)
3203 self.assertEqual(r, True)
3204
James E. Blair91e34592015-07-31 16:45:59 -07003205 def test_client_enqueue_ref(self):
3206 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003207 p = "review.example.com/org/project"
3208 upstream = self.getUpstreamRepos([p])
3209 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3210 A.setMerged()
3211 A_commit = str(upstream[p].commit('master'))
3212 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003213
3214 client = zuul.rpcclient.RPCClient('127.0.0.1',
3215 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003216 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003217 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003218 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003219 pipeline='post',
3220 project='org/project',
3221 trigger='gerrit',
3222 ref='master',
3223 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003224 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003225 self.waitUntilSettled()
3226 job_names = [x.name for x in self.history]
3227 self.assertEqual(len(self.history), 1)
3228 self.assertIn('project-post', job_names)
3229 self.assertEqual(r, True)
3230
James E. Blairad28e912013-11-27 10:43:22 -08003231 def test_client_enqueue_negative(self):
3232 "Test that the RPC client returns errors"
3233 client = zuul.rpcclient.RPCClient('127.0.0.1',
3234 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003235 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003236 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003237 "Invalid tenant"):
3238 r = client.enqueue(tenant='tenant-foo',
3239 pipeline='gate',
3240 project='org/project',
3241 trigger='gerrit',
3242 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003243 self.assertEqual(r, False)
3244
3245 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003246 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003247 r = client.enqueue(tenant='tenant-one',
3248 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003249 project='project-does-not-exist',
3250 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003251 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003252 self.assertEqual(r, False)
3253
3254 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3255 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003256 r = client.enqueue(tenant='tenant-one',
3257 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003258 project='org/project',
3259 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003260 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003261 self.assertEqual(r, False)
3262
3263 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3264 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003265 r = client.enqueue(tenant='tenant-one',
3266 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003267 project='org/project',
3268 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003269 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003270 self.assertEqual(r, False)
3271
3272 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3273 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003274 r = client.enqueue(tenant='tenant-one',
3275 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003276 project='org/project',
3277 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003278 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003279 self.assertEqual(r, False)
3280
3281 self.waitUntilSettled()
3282 self.assertEqual(len(self.history), 0)
3283 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003284
3285 def test_client_promote(self):
3286 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003287 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003288 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3289 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3290 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003291 A.addApproval('Code-Review', 2)
3292 B.addApproval('Code-Review', 2)
3293 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003294
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003295 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3296 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3297 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003298
3299 self.waitUntilSettled()
3300
Paul Belangerbaca3132016-11-04 12:49:54 -04003301 tenant = self.sched.abide.tenants.get('tenant-one')
3302 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003303 enqueue_times = {}
3304 for item in items:
3305 enqueue_times[str(item.change)] = item.enqueue_time
3306
James E. Blair36658cf2013-12-06 17:53:48 -08003307 client = zuul.rpcclient.RPCClient('127.0.0.1',
3308 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003309 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003310 r = client.promote(tenant='tenant-one',
3311 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003312 change_ids=['2,1', '3,1'])
3313
Sean Daguef39b9ca2014-01-10 21:34:35 -05003314 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003315 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003316 for item in items:
3317 self.assertEqual(
3318 enqueue_times[str(item.change)], item.enqueue_time)
3319
James E. Blair78acec92014-02-06 07:11:32 -08003320 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003321 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003322 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003323 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003324 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003325 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003326 self.waitUntilSettled()
3327
3328 self.assertEqual(len(self.builds), 6)
3329 self.assertEqual(self.builds[0].name, 'project-test1')
3330 self.assertEqual(self.builds[1].name, 'project-test2')
3331 self.assertEqual(self.builds[2].name, 'project-test1')
3332 self.assertEqual(self.builds[3].name, 'project-test2')
3333 self.assertEqual(self.builds[4].name, 'project-test1')
3334 self.assertEqual(self.builds[5].name, 'project-test2')
3335
Paul Belangerbaca3132016-11-04 12:49:54 -04003336 self.assertTrue(self.builds[0].hasChanges(B))
3337 self.assertFalse(self.builds[0].hasChanges(A))
3338 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003339
Paul Belangerbaca3132016-11-04 12:49:54 -04003340 self.assertTrue(self.builds[2].hasChanges(B))
3341 self.assertTrue(self.builds[2].hasChanges(C))
3342 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003343
Paul Belangerbaca3132016-11-04 12:49:54 -04003344 self.assertTrue(self.builds[4].hasChanges(B))
3345 self.assertTrue(self.builds[4].hasChanges(C))
3346 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003347
Paul Belanger174a8272017-03-14 13:20:10 -04003348 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003349 self.waitUntilSettled()
3350
3351 self.assertEqual(A.data['status'], 'MERGED')
3352 self.assertEqual(A.reported, 2)
3353 self.assertEqual(B.data['status'], 'MERGED')
3354 self.assertEqual(B.reported, 2)
3355 self.assertEqual(C.data['status'], 'MERGED')
3356 self.assertEqual(C.reported, 2)
3357
James E. Blair36658cf2013-12-06 17:53:48 -08003358 self.assertEqual(r, True)
3359
3360 def test_client_promote_dependent(self):
3361 "Test that the RPC client can promote a dependent change"
3362 # C (depends on B) -> B -> A ; then promote C to get:
3363 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003364 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003365 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3366 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3367 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3368
3369 C.setDependsOn(B, 1)
3370
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003371 A.addApproval('Code-Review', 2)
3372 B.addApproval('Code-Review', 2)
3373 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003374
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003375 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3376 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3377 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003378
3379 self.waitUntilSettled()
3380
3381 client = zuul.rpcclient.RPCClient('127.0.0.1',
3382 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003383 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003384 r = client.promote(tenant='tenant-one',
3385 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003386 change_ids=['3,1'])
3387
James E. Blair78acec92014-02-06 07:11:32 -08003388 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003389 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003390 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003391 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003392 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003393 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003394 self.waitUntilSettled()
3395
3396 self.assertEqual(len(self.builds), 6)
3397 self.assertEqual(self.builds[0].name, 'project-test1')
3398 self.assertEqual(self.builds[1].name, 'project-test2')
3399 self.assertEqual(self.builds[2].name, 'project-test1')
3400 self.assertEqual(self.builds[3].name, 'project-test2')
3401 self.assertEqual(self.builds[4].name, 'project-test1')
3402 self.assertEqual(self.builds[5].name, 'project-test2')
3403
Paul Belangerbaca3132016-11-04 12:49:54 -04003404 self.assertTrue(self.builds[0].hasChanges(B))
3405 self.assertFalse(self.builds[0].hasChanges(A))
3406 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003407
Paul Belangerbaca3132016-11-04 12:49:54 -04003408 self.assertTrue(self.builds[2].hasChanges(B))
3409 self.assertTrue(self.builds[2].hasChanges(C))
3410 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003411
Paul Belangerbaca3132016-11-04 12:49:54 -04003412 self.assertTrue(self.builds[4].hasChanges(B))
3413 self.assertTrue(self.builds[4].hasChanges(C))
3414 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003415
Paul Belanger174a8272017-03-14 13:20:10 -04003416 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003417 self.waitUntilSettled()
3418
3419 self.assertEqual(A.data['status'], 'MERGED')
3420 self.assertEqual(A.reported, 2)
3421 self.assertEqual(B.data['status'], 'MERGED')
3422 self.assertEqual(B.reported, 2)
3423 self.assertEqual(C.data['status'], 'MERGED')
3424 self.assertEqual(C.reported, 2)
3425
James E. Blair36658cf2013-12-06 17:53:48 -08003426 self.assertEqual(r, True)
3427
3428 def test_client_promote_negative(self):
3429 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003430 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003431 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003432 A.addApproval('Code-Review', 2)
3433 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003434 self.waitUntilSettled()
3435
3436 client = zuul.rpcclient.RPCClient('127.0.0.1',
3437 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003438 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003439
3440 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003441 r = client.promote(tenant='tenant-one',
3442 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003443 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003444 self.assertEqual(r, False)
3445
3446 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003447 r = client.promote(tenant='tenant-one',
3448 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003449 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003450 self.assertEqual(r, False)
3451
Paul Belanger174a8272017-03-14 13:20:10 -04003452 self.executor_server.hold_jobs_in_build = False
3453 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003454 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003455
James E. Blaircc324192017-04-19 14:36:08 -07003456 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003457 def test_queue_rate_limiting(self):
3458 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003459 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003460 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3461 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3462 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3463
3464 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003465 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003466
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003467 A.addApproval('Code-Review', 2)
3468 B.addApproval('Code-Review', 2)
3469 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003470
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003471 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3472 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3473 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003474 self.waitUntilSettled()
3475
3476 # Only A and B will have their merge jobs queued because
3477 # window is 2.
3478 self.assertEqual(len(self.builds), 2)
3479 self.assertEqual(self.builds[0].name, 'project-merge')
3480 self.assertEqual(self.builds[1].name, 'project-merge')
3481
James E. Blair732ee862017-04-21 07:49:26 -07003482 # Release the merge jobs one at a time.
3483 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003484 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003485 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003486 self.waitUntilSettled()
3487
3488 # Only A and B will have their test jobs queued because
3489 # window is 2.
3490 self.assertEqual(len(self.builds), 4)
3491 self.assertEqual(self.builds[0].name, 'project-test1')
3492 self.assertEqual(self.builds[1].name, 'project-test2')
3493 self.assertEqual(self.builds[2].name, 'project-test1')
3494 self.assertEqual(self.builds[3].name, 'project-test2')
3495
Paul Belanger174a8272017-03-14 13:20:10 -04003496 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003497 self.waitUntilSettled()
3498
James E. Blaircc324192017-04-19 14:36:08 -07003499 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003500 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003501 # A failed so window is reduced by 1 to 1.
3502 self.assertEqual(queue.window, 1)
3503 self.assertEqual(queue.window_floor, 1)
3504 self.assertEqual(A.data['status'], 'NEW')
3505
3506 # Gate is reset and only B's merge job is queued because
3507 # window shrunk to 1.
3508 self.assertEqual(len(self.builds), 1)
3509 self.assertEqual(self.builds[0].name, 'project-merge')
3510
Paul Belanger174a8272017-03-14 13:20:10 -04003511 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003512 self.waitUntilSettled()
3513
3514 # Only B's test jobs are queued because window is still 1.
3515 self.assertEqual(len(self.builds), 2)
3516 self.assertEqual(self.builds[0].name, 'project-test1')
3517 self.assertEqual(self.builds[1].name, 'project-test2')
3518
Paul Belanger174a8272017-03-14 13:20:10 -04003519 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003520 self.waitUntilSettled()
3521
3522 # B was successfully merged so window is increased to 2.
3523 self.assertEqual(queue.window, 2)
3524 self.assertEqual(queue.window_floor, 1)
3525 self.assertEqual(B.data['status'], 'MERGED')
3526
3527 # Only C is left and its merge job is queued.
3528 self.assertEqual(len(self.builds), 1)
3529 self.assertEqual(self.builds[0].name, 'project-merge')
3530
Paul Belanger174a8272017-03-14 13:20:10 -04003531 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003532 self.waitUntilSettled()
3533
3534 # After successful merge job the test jobs for C are queued.
3535 self.assertEqual(len(self.builds), 2)
3536 self.assertEqual(self.builds[0].name, 'project-test1')
3537 self.assertEqual(self.builds[1].name, 'project-test2')
3538
Paul Belanger174a8272017-03-14 13:20:10 -04003539 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003540 self.waitUntilSettled()
3541
3542 # C successfully merged so window is bumped to 3.
3543 self.assertEqual(queue.window, 3)
3544 self.assertEqual(queue.window_floor, 1)
3545 self.assertEqual(C.data['status'], 'MERGED')
3546
James E. Blaircc324192017-04-19 14:36:08 -07003547 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003548 def test_queue_rate_limiting_dependent(self):
3549 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003550 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003551 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3552 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3553 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3554
3555 B.setDependsOn(A, 1)
3556
Paul Belanger174a8272017-03-14 13:20:10 -04003557 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003558
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003559 A.addApproval('Code-Review', 2)
3560 B.addApproval('Code-Review', 2)
3561 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003562
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003563 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3564 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3565 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003566 self.waitUntilSettled()
3567
3568 # Only A and B will have their merge jobs queued because
3569 # window is 2.
3570 self.assertEqual(len(self.builds), 2)
3571 self.assertEqual(self.builds[0].name, 'project-merge')
3572 self.assertEqual(self.builds[1].name, 'project-merge')
3573
James E. Blairf4a5f022017-04-18 14:01:10 -07003574 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003575
3576 # Only A and B will have their test jobs queued because
3577 # window is 2.
3578 self.assertEqual(len(self.builds), 4)
3579 self.assertEqual(self.builds[0].name, 'project-test1')
3580 self.assertEqual(self.builds[1].name, 'project-test2')
3581 self.assertEqual(self.builds[2].name, 'project-test1')
3582 self.assertEqual(self.builds[3].name, 'project-test2')
3583
Paul Belanger174a8272017-03-14 13:20:10 -04003584 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003585 self.waitUntilSettled()
3586
James E. Blaircc324192017-04-19 14:36:08 -07003587 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003588 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003589 # A failed so window is reduced by 1 to 1.
3590 self.assertEqual(queue.window, 1)
3591 self.assertEqual(queue.window_floor, 1)
3592 self.assertEqual(A.data['status'], 'NEW')
3593 self.assertEqual(B.data['status'], 'NEW')
3594
3595 # Gate is reset and only C's merge job is queued because
3596 # window shrunk to 1 and A and B were dequeued.
3597 self.assertEqual(len(self.builds), 1)
3598 self.assertEqual(self.builds[0].name, 'project-merge')
3599
James E. Blairf4a5f022017-04-18 14:01:10 -07003600 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003601
3602 # Only C's test jobs are queued because window is still 1.
3603 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003604 builds = self.getSortedBuilds()
3605 self.assertEqual(builds[0].name, 'project-test1')
3606 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003607
Paul Belanger174a8272017-03-14 13:20:10 -04003608 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003609 self.waitUntilSettled()
3610
3611 # C was successfully merged so window is increased to 2.
3612 self.assertEqual(queue.window, 2)
3613 self.assertEqual(queue.window_floor, 1)
3614 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003615
3616 def test_worker_update_metadata(self):
3617 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003618 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003619
3620 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003621 A.addApproval('Code-Review', 2)
3622 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003623 self.waitUntilSettled()
3624
Clint Byrumd4de79a2017-05-27 11:39:13 -07003625 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003626
3627 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003628 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003629
3630 start = time.time()
3631 while True:
3632 if time.time() - start > 10:
3633 raise Exception("Timeout waiting for gearman server to report "
3634 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003635 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003636 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003637 break
3638 else:
3639 time.sleep(0)
3640
3641 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003642 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003643
Paul Belanger174a8272017-03-14 13:20:10 -04003644 self.executor_server.hold_jobs_in_build = False
3645 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003646 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003647
James E. Blaircc324192017-04-19 14:36:08 -07003648 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003649 def test_footer_message(self):
3650 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003651 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003652 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003653 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003654 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003655 self.waitUntilSettled()
3656
3657 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003658 B.addApproval('Code-Review', 2)
3659 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003660 self.waitUntilSettled()
3661
3662 self.assertEqual(2, len(self.smtp_messages))
3663
Adam Gandelman82258fd2017-02-07 16:51:58 -08003664 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003665Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003666http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003667
Adam Gandelman82258fd2017-02-07 16:51:58 -08003668 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003669For CI problems and help debugging, contact ci@example.org"""
3670
Adam Gandelman82258fd2017-02-07 16:51:58 -08003671 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3672 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3673 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3674 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003675
Tobias Henkel9842bd72017-05-16 13:40:03 +02003676 @simple_layout('layouts/unmanaged-project.yaml')
3677 def test_unmanaged_project_start_message(self):
3678 "Test start reporting is not done for unmanaged projects."
3679 self.init_repo("org/project", tag='init')
3680 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3681 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3682 self.waitUntilSettled()
3683
3684 self.assertEqual(0, len(A.messages))
3685
Clint Byrumb9de8be2017-05-27 13:49:19 -07003686 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003687 def test_merge_failure_reporters(self):
3688 """Check that the config is set up correctly"""
3689
Clint Byrumb9de8be2017-05-27 13:49:19 -07003690 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003691 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003692 "Merge Failed.\n\nThis change or one of its cross-repo "
3693 "dependencies was unable to be automatically merged with the "
3694 "current state of its repository. Please rebase the change and "
3695 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003696 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003697 self.assertEqual(
3698 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003699 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003700
3701 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003702 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003703 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003704 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003705
3706 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003707 tenant.layout.pipelines['check'].merge_failure_actions[0],
3708 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003709
3710 self.assertTrue(
3711 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003712 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003713 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003714 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3715 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003716 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003717 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003718 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003719 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003720 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003721 gerritreporter.GerritReporter) and
3722 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003723 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003724 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003725 )
3726 )
3727
James E. Blairec056492016-07-22 09:45:56 -07003728 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003729 def test_merge_failure_reports(self):
3730 """Check that when a change fails to merge the correct message is sent
3731 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003732 self.updateConfigLayout(
3733 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003734 self.sched.reconfigure(self.config)
3735 self.registerJobs()
3736
3737 # Check a test failure isn't reported to SMTP
3738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003739 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003740 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003741 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003742 self.waitUntilSettled()
3743
3744 self.assertEqual(3, len(self.history)) # 3 jobs
3745 self.assertEqual(0, len(self.smtp_messages))
3746
3747 # Check a merge failure is reported to SMTP
3748 # B should be merged, but C will conflict with B
3749 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3750 B.addPatchset(['conflict'])
3751 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3752 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003753 B.addApproval('Code-Review', 2)
3754 C.addApproval('Code-Review', 2)
3755 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3756 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003757 self.waitUntilSettled()
3758
3759 self.assertEqual(6, len(self.history)) # A and B jobs
3760 self.assertEqual(1, len(self.smtp_messages))
3761 self.assertEqual('The merge failed! For more information...',
3762 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003763
James E. Blairec056492016-07-22 09:45:56 -07003764 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003765 def test_default_merge_failure_reports(self):
3766 """Check that the default merge failure reports are correct."""
3767
3768 # A should report success, B should report merge failure.
3769 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3770 A.addPatchset(['conflict'])
3771 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3772 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003773 A.addApproval('Code-Review', 2)
3774 B.addApproval('Code-Review', 2)
3775 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3776 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003777 self.waitUntilSettled()
3778
3779 self.assertEqual(3, len(self.history)) # A jobs
3780 self.assertEqual(A.reported, 2)
3781 self.assertEqual(B.reported, 2)
3782 self.assertEqual(A.data['status'], 'MERGED')
3783 self.assertEqual(B.data['status'], 'NEW')
3784 self.assertIn('Build succeeded', A.messages[1])
3785 self.assertIn('Merge Failed', B.messages[1])
3786 self.assertIn('automatically merged', B.messages[1])
3787 self.assertNotIn('logs.example.com', B.messages[1])
3788 self.assertNotIn('SKIPPED', B.messages[1])
3789
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003790 def test_client_get_running_jobs(self):
3791 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003792 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003793 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003794 A.addApproval('Code-Review', 2)
3795 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003796 self.waitUntilSettled()
3797
3798 client = zuul.rpcclient.RPCClient('127.0.0.1',
3799 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003800 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003801
3802 # Wait for gearman server to send the initial workData back to zuul
3803 start = time.time()
3804 while True:
3805 if time.time() - start > 10:
3806 raise Exception("Timeout waiting for gearman server to report "
3807 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003808 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003809 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003810 break
3811 else:
3812 time.sleep(0)
3813
3814 running_items = client.get_running_jobs()
3815
3816 self.assertEqual(1, len(running_items))
3817 running_item = running_items[0]
3818 self.assertEqual([], running_item['failing_reasons'])
3819 self.assertEqual([], running_item['items_behind'])
3820 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003821 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003822 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003823 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003824 self.assertEqual(True, running_item['active'])
3825 self.assertEqual('1,1', running_item['id'])
3826
3827 self.assertEqual(3, len(running_item['jobs']))
3828 for job in running_item['jobs']:
3829 if job['name'] == 'project-merge':
3830 self.assertEqual('project-merge', job['name'])
3831 self.assertEqual('gate', job['pipeline'])
3832 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003833 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003834 'static/stream.html?uuid={uuid}&logfile=console.log'
3835 .format(uuid=job['uuid']), job['url'])
3836 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003837 'finger://{hostname}/{uuid}'.format(
3838 hostname=self.executor_server.hostname,
3839 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003840 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003841 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003842 self.assertEqual(False, job['canceled'])
3843 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003844 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003845 self.assertEqual('gate', job['pipeline'])
3846 break
3847
Paul Belanger174a8272017-03-14 13:20:10 -04003848 self.executor_server.hold_jobs_in_build = False
3849 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003850 self.waitUntilSettled()
3851
3852 running_items = client.get_running_jobs()
3853 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003854
James E. Blairb70e55a2017-04-19 12:57:02 -07003855 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003856 def test_nonvoting_pipeline(self):
3857 "Test that a nonvoting pipeline (experimental) can still report"
3858
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003859 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3860 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003861 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3862 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003863
3864 self.assertEqual(self.getJobFromHistory('project-merge').result,
3865 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003866 self.assertEqual(
3867 self.getJobFromHistory('experimental-project-test').result,
3868 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003869 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003870
3871 def test_crd_gate(self):
3872 "Test cross-repo dependencies"
3873 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3874 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003875 A.addApproval('Code-Review', 2)
3876 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003877
3878 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3879 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3880 AM2.setMerged()
3881 AM1.setMerged()
3882
3883 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3884 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3885 BM2.setMerged()
3886 BM1.setMerged()
3887
3888 # A -> AM1 -> AM2
3889 # B -> BM1 -> BM2
3890 # A Depends-On: B
3891 # M2 is here to make sure it is never queried. If it is, it
3892 # means zuul is walking down the entire history of merged
3893 # changes.
3894
3895 B.setDependsOn(BM1, 1)
3896 BM1.setDependsOn(BM2, 1)
3897
3898 A.setDependsOn(AM1, 1)
3899 AM1.setDependsOn(AM2, 1)
3900
3901 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3902 A.subject, B.data['id'])
3903
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003904 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003905 self.waitUntilSettled()
3906
3907 self.assertEqual(A.data['status'], 'NEW')
3908 self.assertEqual(B.data['status'], 'NEW')
3909
Paul Belanger5dccbe72016-11-14 11:17:37 -05003910 for connection in self.connections.connections.values():
James E. Blairb9704302017-10-11 16:42:18 -07003911 if hasattr(connection, '_change_cache'):
3912 connection._change_cache.clear()
James E. Blair5ee24252014-12-30 10:12:29 -08003913
Paul Belanger174a8272017-03-14 13:20:10 -04003914 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003915 B.addApproval('Approved', 1)
3916 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003917 self.waitUntilSettled()
3918
Paul Belanger174a8272017-03-14 13:20:10 -04003919 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003920 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003921 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003922 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003923 self.executor_server.hold_jobs_in_build = False
3924 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003925 self.waitUntilSettled()
3926
3927 self.assertEqual(AM2.queried, 0)
3928 self.assertEqual(BM2.queried, 0)
3929 self.assertEqual(A.data['status'], 'MERGED')
3930 self.assertEqual(B.data['status'], 'MERGED')
3931 self.assertEqual(A.reported, 2)
3932 self.assertEqual(B.reported, 2)
3933
Paul Belanger5dccbe72016-11-14 11:17:37 -05003934 changes = self.getJobFromHistory(
3935 'project-merge', 'org/project1').changes
3936 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003937
3938 def test_crd_branch(self):
3939 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003940
3941 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003942 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3943 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003944 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3945 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3946 status='ABANDONED')
3947 C1.data['id'] = B.data['id']
3948 C2.data['id'] = B.data['id']
3949
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003950 A.addApproval('Code-Review', 2)
3951 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003952 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003953
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003954 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003955 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3956 A.subject, B.data['id'])
3957
Paul Belanger174a8272017-03-14 13:20:10 -04003958 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003959 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003960 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003961 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003962 self.waitUntilSettled()
3963
Paul Belanger174a8272017-03-14 13:20:10 -04003964 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003965 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003966 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003967 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003968 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003969 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003970 self.executor_server.hold_jobs_in_build = False
3971 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003972 self.waitUntilSettled()
3973
3974 self.assertEqual(A.data['status'], 'MERGED')
3975 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003976 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08003977 self.assertEqual(A.reported, 2)
3978 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003979 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003980
Paul Belanger6379db12016-11-14 13:57:54 -05003981 changes = self.getJobFromHistory(
3982 'project-merge', 'org/project1').changes
3983 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003984
3985 def test_crd_multiline(self):
3986 "Test multiple depends-on lines in commit"
3987 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3988 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3989 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003990 A.addApproval('Code-Review', 2)
3991 B.addApproval('Code-Review', 2)
3992 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003993
3994 # A Depends-On: B+C
3995 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3996 A.subject, B.data['id'], C.data['id'])
3997
Paul Belanger174a8272017-03-14 13:20:10 -04003998 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003999 B.addApproval('Approved', 1)
4000 C.addApproval('Approved', 1)
4001 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08004002 self.waitUntilSettled()
4003
Paul Belanger174a8272017-03-14 13:20:10 -04004004 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004005 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004006 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004007 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004008 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004009 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004010 self.executor_server.hold_jobs_in_build = False
4011 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08004012 self.waitUntilSettled()
4013
4014 self.assertEqual(A.data['status'], 'MERGED')
4015 self.assertEqual(B.data['status'], 'MERGED')
4016 self.assertEqual(C.data['status'], 'MERGED')
4017 self.assertEqual(A.reported, 2)
4018 self.assertEqual(B.reported, 2)
4019 self.assertEqual(C.reported, 2)
4020
Paul Belanger5dccbe72016-11-14 11:17:37 -05004021 changes = self.getJobFromHistory(
4022 'project-merge', 'org/project1').changes
4023 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08004024
4025 def test_crd_unshared_gate(self):
4026 "Test cross-repo dependencies in unshared gate queues"
4027 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4028 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004029 A.addApproval('Code-Review', 2)
4030 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004031
4032 # A Depends-On: B
4033 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4034 A.subject, B.data['id'])
4035
4036 # A and B do not share a queue, make sure that A is unable to
4037 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004038 B.addApproval('Approved', 1)
4039 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004040 self.waitUntilSettled()
4041
4042 self.assertEqual(A.data['status'], 'NEW')
4043 self.assertEqual(B.data['status'], 'NEW')
4044 self.assertEqual(A.reported, 0)
4045 self.assertEqual(B.reported, 0)
4046 self.assertEqual(len(self.history), 0)
4047
4048 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004049 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004050 self.waitUntilSettled()
4051
4052 self.assertEqual(B.data['status'], 'MERGED')
4053 self.assertEqual(B.reported, 2)
4054
4055 # Now that B is merged, A should be able to be enqueued and
4056 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004057 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004058 self.waitUntilSettled()
4059
4060 self.assertEqual(A.data['status'], 'MERGED')
4061 self.assertEqual(A.reported, 2)
4062
James E. Blair96698e22015-04-02 07:48:21 -07004063 def test_crd_gate_reverse(self):
4064 "Test reverse cross-repo dependencies"
4065 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4066 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004067 A.addApproval('Code-Review', 2)
4068 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004069
4070 # A Depends-On: B
4071
4072 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4073 A.subject, B.data['id'])
4074
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004075 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004076 self.waitUntilSettled()
4077
4078 self.assertEqual(A.data['status'], 'NEW')
4079 self.assertEqual(B.data['status'], 'NEW')
4080
Paul Belanger174a8272017-03-14 13:20:10 -04004081 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004082 A.addApproval('Approved', 1)
4083 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004084 self.waitUntilSettled()
4085
Paul Belanger174a8272017-03-14 13:20:10 -04004086 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004087 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004088 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004089 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004090 self.executor_server.hold_jobs_in_build = False
4091 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004092 self.waitUntilSettled()
4093
4094 self.assertEqual(A.data['status'], 'MERGED')
4095 self.assertEqual(B.data['status'], 'MERGED')
4096 self.assertEqual(A.reported, 2)
4097 self.assertEqual(B.reported, 2)
4098
Paul Belanger5dccbe72016-11-14 11:17:37 -05004099 changes = self.getJobFromHistory(
4100 'project-merge', 'org/project1').changes
4101 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004102
James E. Blair5ee24252014-12-30 10:12:29 -08004103 def test_crd_cycle(self):
4104 "Test cross-repo dependency cycles"
4105 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4106 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004107 A.addApproval('Code-Review', 2)
4108 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004109
4110 # A -> B -> A (via commit-depends)
4111
4112 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4113 A.subject, B.data['id'])
4114 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4115 B.subject, A.data['id'])
4116
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004117 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004118 self.waitUntilSettled()
4119
4120 self.assertEqual(A.reported, 0)
4121 self.assertEqual(B.reported, 0)
4122 self.assertEqual(A.data['status'], 'NEW')
4123 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004124
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004125 def test_crd_gate_unknown(self):
4126 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004127 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004128 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4129 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004130 A.addApproval('Code-Review', 2)
4131 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004132
4133 # A Depends-On: B
4134 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4135 A.subject, B.data['id'])
4136
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004137 B.addApproval('Approved', 1)
4138 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004139 self.waitUntilSettled()
4140
4141 # Unknown projects cannot share a queue with any other
4142 # since they don't have common jobs with any other (they have no jobs).
4143 # Changes which depend on unknown project changes
4144 # should not be processed in dependent pipeline
4145 self.assertEqual(A.data['status'], 'NEW')
4146 self.assertEqual(B.data['status'], 'NEW')
4147 self.assertEqual(A.reported, 0)
4148 self.assertEqual(B.reported, 0)
4149 self.assertEqual(len(self.history), 0)
4150
James E. Blair7720b402017-06-07 08:51:49 -07004151 # Simulate change B being gated outside this layout Set the
4152 # change merged before submitting the event so that when the
4153 # event triggers a gerrit query to update the change, we get
4154 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004155 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004156 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004157 self.waitUntilSettled()
4158 self.assertEqual(len(self.history), 0)
4159
4160 # Now that B is merged, A should be able to be enqueued and
4161 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004162 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004163 self.waitUntilSettled()
4164
4165 self.assertEqual(A.data['status'], 'MERGED')
4166 self.assertEqual(A.reported, 2)
4167 self.assertEqual(B.data['status'], 'MERGED')
4168 self.assertEqual(B.reported, 0)
4169
James E. Blairbfb8e042014-12-30 17:01:44 -08004170 def test_crd_check(self):
4171 "Test cross-repo dependencies in independent pipelines"
4172
Paul Belanger174a8272017-03-14 13:20:10 -04004173 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004174 self.gearman_server.hold_jobs_in_queue = True
4175 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4176 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4177
4178 # A Depends-On: B
4179 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4180 A.subject, B.data['id'])
4181
4182 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4183 self.waitUntilSettled()
4184
James E. Blairbfb8e042014-12-30 17:01:44 -08004185 self.gearman_server.hold_jobs_in_queue = False
4186 self.gearman_server.release()
4187 self.waitUntilSettled()
4188
Paul Belanger174a8272017-03-14 13:20:10 -04004189 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004190 self.waitUntilSettled()
4191
James E. Blair2a535672017-04-27 12:03:15 -07004192 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004193
Paul Belanger174a8272017-03-14 13:20:10 -04004194 self.executor_server.hold_jobs_in_build = False
4195 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004196 self.waitUntilSettled()
4197
James E. Blairbfb8e042014-12-30 17:01:44 -08004198 self.assertEqual(A.data['status'], 'NEW')
4199 self.assertEqual(B.data['status'], 'NEW')
4200 self.assertEqual(A.reported, 1)
4201 self.assertEqual(B.reported, 0)
4202
4203 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004204 tenant = self.sched.abide.tenants.get('tenant-one')
4205 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004206
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004207 def test_crd_check_git_depends(self):
4208 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004209 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004210 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4211 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4212
4213 # Add two git-dependent changes and make sure they both report
4214 # success.
4215 B.setDependsOn(A, 1)
4216 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4217 self.waitUntilSettled()
4218 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4219 self.waitUntilSettled()
4220
James E. Blairb8c16472015-05-05 14:55:26 -07004221 self.orderedRelease()
4222 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004223 self.waitUntilSettled()
4224
4225 self.assertEqual(A.data['status'], 'NEW')
4226 self.assertEqual(B.data['status'], 'NEW')
4227 self.assertEqual(A.reported, 1)
4228 self.assertEqual(B.reported, 1)
4229
4230 self.assertEqual(self.history[0].changes, '1,1')
4231 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004232 tenant = self.sched.abide.tenants.get('tenant-one')
4233 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004234
4235 self.assertIn('Build succeeded', A.messages[0])
4236 self.assertIn('Build succeeded', B.messages[0])
4237
4238 def test_crd_check_duplicate(self):
4239 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004240 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004241 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4242 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004243 tenant = self.sched.abide.tenants.get('tenant-one')
4244 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004245
4246 # Add two git-dependent changes...
4247 B.setDependsOn(A, 1)
4248 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4249 self.waitUntilSettled()
4250 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4251
4252 # ...make sure the live one is not duplicated...
4253 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4254 self.waitUntilSettled()
4255 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4256
4257 # ...but the non-live one is able to be.
4258 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4259 self.waitUntilSettled()
4260 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4261
Clark Boylandd849822015-03-02 12:38:14 -08004262 # Release jobs in order to avoid races with change A jobs
4263 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004264 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004265 self.executor_server.hold_jobs_in_build = False
4266 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004267 self.waitUntilSettled()
4268
4269 self.assertEqual(A.data['status'], 'NEW')
4270 self.assertEqual(B.data['status'], 'NEW')
4271 self.assertEqual(A.reported, 1)
4272 self.assertEqual(B.reported, 1)
4273
4274 self.assertEqual(self.history[0].changes, '1,1 2,1')
4275 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004276 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004277
4278 self.assertIn('Build succeeded', A.messages[0])
4279 self.assertIn('Build succeeded', B.messages[0])
4280
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004281 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004282 "Test cross-repo dependencies re-enqueued in independent pipelines"
4283
4284 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004285 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4286 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004287
4288 # A Depends-On: B
4289 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4290 A.subject, B.data['id'])
4291
4292 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4293 self.waitUntilSettled()
4294
4295 self.sched.reconfigure(self.config)
4296
4297 # Make sure the items still share a change queue, and the
4298 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004299 tenant = self.sched.abide.tenants.get('tenant-one')
4300 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4301 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004302 first_item = queue.queue[0]
4303 for item in queue.queue:
4304 self.assertEqual(item.queue, first_item.queue)
4305 self.assertFalse(first_item.live)
4306 self.assertTrue(queue.queue[1].live)
4307
4308 self.gearman_server.hold_jobs_in_queue = False
4309 self.gearman_server.release()
4310 self.waitUntilSettled()
4311
4312 self.assertEqual(A.data['status'], 'NEW')
4313 self.assertEqual(B.data['status'], 'NEW')
4314 self.assertEqual(A.reported, 1)
4315 self.assertEqual(B.reported, 0)
4316
4317 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004318 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004319
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004320 def test_crd_check_reconfiguration(self):
4321 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4322
4323 def test_crd_undefined_project(self):
4324 """Test that undefined projects in dependencies are handled for
4325 independent pipelines"""
4326 # It's a hack for fake gerrit,
4327 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004328 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004329 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4330
Jesse Keatingea68ab42017-04-19 15:03:30 -07004331 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004332 def test_crd_check_ignore_dependencies(self):
4333 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004334
4335 self.gearman_server.hold_jobs_in_queue = True
4336 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4337 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4338 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4339
4340 # A Depends-On: B
4341 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4342 A.subject, B.data['id'])
4343 # C git-depends on B
4344 C.setDependsOn(B, 1)
4345 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4346 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4347 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4348 self.waitUntilSettled()
4349
4350 # Make sure none of the items share a change queue, and all
4351 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004352 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004353 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004354 self.assertEqual(len(check_pipeline.queues), 3)
4355 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4356 for item in check_pipeline.getAllItems():
4357 self.assertTrue(item.live)
4358
4359 self.gearman_server.hold_jobs_in_queue = False
4360 self.gearman_server.release()
4361 self.waitUntilSettled()
4362
4363 self.assertEqual(A.data['status'], 'NEW')
4364 self.assertEqual(B.data['status'], 'NEW')
4365 self.assertEqual(C.data['status'], 'NEW')
4366 self.assertEqual(A.reported, 1)
4367 self.assertEqual(B.reported, 1)
4368 self.assertEqual(C.reported, 1)
4369
4370 # Each job should have tested exactly one change
4371 for job in self.history:
4372 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004373
James E. Blair7cb84542017-04-19 13:35:05 -07004374 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004375 def test_crd_check_transitive(self):
4376 "Test transitive cross-repo dependencies"
4377 # Specifically, if A -> B -> C, and C gets a new patchset and
4378 # A gets a new patchset, ensure the test of A,2 includes B,1
4379 # and C,2 (not C,1 which would indicate stale data in the
4380 # cache for B).
4381 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4382 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4383 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4384
4385 # A Depends-On: B
4386 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4387 A.subject, B.data['id'])
4388
4389 # B Depends-On: C
4390 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4391 B.subject, C.data['id'])
4392
4393 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4394 self.waitUntilSettled()
4395 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4396
4397 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4398 self.waitUntilSettled()
4399 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4400
4401 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4402 self.waitUntilSettled()
4403 self.assertEqual(self.history[-1].changes, '3,1')
4404
4405 C.addPatchset()
4406 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4407 self.waitUntilSettled()
4408 self.assertEqual(self.history[-1].changes, '3,2')
4409
4410 A.addPatchset()
4411 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4412 self.waitUntilSettled()
4413 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004414
James E. Blaira8b90b32016-08-24 15:18:50 -07004415 def test_crd_check_unknown(self):
4416 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004417 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004418 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4419 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4420 # A Depends-On: B
4421 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4422 A.subject, B.data['id'])
4423
4424 # Make sure zuul has seen an event on B.
4425 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4426 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4427 self.waitUntilSettled()
4428
4429 self.assertEqual(A.data['status'], 'NEW')
4430 self.assertEqual(A.reported, 1)
4431 self.assertEqual(B.data['status'], 'NEW')
4432 self.assertEqual(B.reported, 0)
4433
James E. Blair92464a22016-04-05 10:21:26 -07004434 def test_crd_cycle_join(self):
4435 "Test an updated change creates a cycle"
4436 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4437
4438 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4439 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004440 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004441
4442 # Create B->A
4443 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4444 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4445 B.subject, A.data['id'])
4446 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4447 self.waitUntilSettled()
4448
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004449 # Dep is there so zuul should have reported on B
4450 self.assertEqual(B.reported, 1)
4451
James E. Blair92464a22016-04-05 10:21:26 -07004452 # Update A to add A->B (a cycle).
4453 A.addPatchset()
4454 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4455 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004456 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4457 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004458
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004459 # Dependency cycle injected so zuul should not have reported again on A
4460 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004461
4462 # Now if we update B to remove the depends-on, everything
4463 # should be okay. B; A->B
4464
4465 B.addPatchset()
4466 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004467 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4468 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004469
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004470 # Cycle was removed so now zuul should have reported again on A
4471 self.assertEqual(A.reported, 2)
4472
4473 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4474 self.waitUntilSettled()
4475 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004476
Jesse Keatingcc67b122017-04-19 14:54:57 -07004477 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004478 def test_disable_at(self):
4479 "Test a pipeline will only report to the disabled trigger when failing"
4480
Jesse Keatingcc67b122017-04-19 14:54:57 -07004481 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004482 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004483 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004484 0, tenant.layout.pipelines['check']._consecutive_failures)
4485 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004486
4487 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4488 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4489 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4490 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4491 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4492 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4493 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4494 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4495 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4496 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4497 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4498
Paul Belanger174a8272017-03-14 13:20:10 -04004499 self.executor_server.failJob('project-test1', A)
4500 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004501 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004502 self.executor_server.failJob('project-test1', D)
4503 self.executor_server.failJob('project-test1', E)
4504 self.executor_server.failJob('project-test1', F)
4505 self.executor_server.failJob('project-test1', G)
4506 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004507 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004508 self.executor_server.failJob('project-test1', J)
4509 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004510
4511 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4512 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4513 self.waitUntilSettled()
4514
4515 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004516 2, tenant.layout.pipelines['check']._consecutive_failures)
4517 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004518
4519 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4520 self.waitUntilSettled()
4521
4522 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004523 0, tenant.layout.pipelines['check']._consecutive_failures)
4524 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004525
4526 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4527 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4528 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4529 self.waitUntilSettled()
4530
4531 # We should be disabled now
4532 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004533 3, tenant.layout.pipelines['check']._consecutive_failures)
4534 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004535
4536 # We need to wait between each of these patches to make sure the
4537 # smtp messages come back in an expected order
4538 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4539 self.waitUntilSettled()
4540 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4541 self.waitUntilSettled()
4542 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4543 self.waitUntilSettled()
4544
4545 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4546 # leaving a message on each change
4547 self.assertEqual(1, len(A.messages))
4548 self.assertIn('Build failed.', A.messages[0])
4549 self.assertEqual(1, len(B.messages))
4550 self.assertIn('Build failed.', B.messages[0])
4551 self.assertEqual(1, len(C.messages))
4552 self.assertIn('Build succeeded.', C.messages[0])
4553 self.assertEqual(1, len(D.messages))
4554 self.assertIn('Build failed.', D.messages[0])
4555 self.assertEqual(1, len(E.messages))
4556 self.assertIn('Build failed.', E.messages[0])
4557 self.assertEqual(1, len(F.messages))
4558 self.assertIn('Build failed.', F.messages[0])
4559
4560 # The last 3 (GHI) would have only reported via smtp.
4561 self.assertEqual(3, len(self.smtp_messages))
4562 self.assertEqual(0, len(G.messages))
4563 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004564 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004565 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004566 self.assertEqual(0, len(H.messages))
4567 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004568 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004569 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004570 self.assertEqual(0, len(I.messages))
4571 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004572 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004573 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004574
4575 # Now reload the configuration (simulate a HUP) to check the pipeline
4576 # comes out of disabled
4577 self.sched.reconfigure(self.config)
4578
Jesse Keatingcc67b122017-04-19 14:54:57 -07004579 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004580
4581 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004582 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004583 0, tenant.layout.pipelines['check']._consecutive_failures)
4584 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004585
4586 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4587 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4588 self.waitUntilSettled()
4589
4590 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004591 2, tenant.layout.pipelines['check']._consecutive_failures)
4592 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004593
4594 # J and K went back to gerrit
4595 self.assertEqual(1, len(J.messages))
4596 self.assertIn('Build failed.', J.messages[0])
4597 self.assertEqual(1, len(K.messages))
4598 self.assertIn('Build failed.', K.messages[0])
4599 # No more messages reported via smtp
4600 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004601
James E. Blaircc020532017-04-19 13:04:44 -07004602 @simple_layout('layouts/one-job-project.yaml')
4603 def test_one_job_project(self):
4604 "Test that queueing works with one job"
4605 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4606 'master', 'A')
4607 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4608 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004609 A.addApproval('Code-Review', 2)
4610 B.addApproval('Code-Review', 2)
4611 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4612 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004613 self.waitUntilSettled()
4614
4615 self.assertEqual(A.data['status'], 'MERGED')
4616 self.assertEqual(A.reported, 2)
4617 self.assertEqual(B.data['status'], 'MERGED')
4618 self.assertEqual(B.reported, 2)
4619
Paul Belanger32023542017-10-06 14:48:50 -04004620 def test_job_aborted(self):
4621 "Test that if a execute server aborts a job, it is run again"
4622 self.executor_server.hold_jobs_in_build = True
4623 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4624 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4625 self.waitUntilSettled()
4626
4627 self.executor_server.release('.*-merge')
4628 self.waitUntilSettled()
4629
4630 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004631
4632 # first abort
4633 self.builds[0].aborted = True
4634 self.executor_server.release('.*-test*')
4635 self.waitUntilSettled()
4636 self.assertEqual(len(self.builds), 1)
4637
4638 # second abort
4639 self.builds[0].aborted = True
4640 self.executor_server.release('.*-test*')
4641 self.waitUntilSettled()
4642 self.assertEqual(len(self.builds), 1)
4643
4644 # third abort
4645 self.builds[0].aborted = True
4646 self.executor_server.release('.*-test*')
4647 self.waitUntilSettled()
4648 self.assertEqual(len(self.builds), 1)
4649
4650 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004651 self.builds[0].aborted = True
4652 self.executor_server.release('.*-test*')
4653 self.waitUntilSettled()
4654 self.assertEqual(len(self.builds), 1)
4655
4656 self.executor_server.hold_jobs_in_build = False
4657 self.executor_server.release()
4658 self.waitUntilSettled()
4659
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004660 self.assertEqual(len(self.history), 7)
4661 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004662 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4663
Paul Belanger71d98172016-11-08 10:56:31 -05004664 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004665 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004666
Paul Belanger174a8272017-03-14 13:20:10 -04004667 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004668 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4669 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4670 self.waitUntilSettled()
4671
Paul Belanger174a8272017-03-14 13:20:10 -04004672 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004673 self.waitUntilSettled()
4674
4675 self.assertEqual(len(self.builds), 2)
4676 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004677 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004678 self.waitUntilSettled()
4679
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004680 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004681 self.assertEqual(len(self.builds), 1,
4682 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004683 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004684 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004685 self.waitUntilSettled()
4686
Paul Belanger174a8272017-03-14 13:20:10 -04004687 self.executor_server.hold_jobs_in_build = False
4688 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004689 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004690 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004691 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4692 self.assertEqual(A.reported, 1)
4693 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004694
James E. Blair15be0e12017-01-03 13:45:20 -08004695 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004696 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004697
4698 self.fake_nodepool.paused = True
4699 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004700 A.addApproval('Code-Review', 2)
4701 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004702 self.waitUntilSettled()
4703
4704 self.zk.client.stop()
4705 self.zk.client.start()
4706 self.fake_nodepool.paused = False
4707 self.waitUntilSettled()
4708
4709 self.assertEqual(A.data['status'], 'MERGED')
4710 self.assertEqual(A.reported, 2)
4711
James E. Blair6ab79e02017-01-06 10:10:17 -08004712 def test_nodepool_failure(self):
4713 "Test that jobs are reported after a nodepool failure"
4714
4715 self.fake_nodepool.paused = True
4716 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004717 A.addApproval('Code-Review', 2)
4718 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004719 self.waitUntilSettled()
4720
4721 req = self.fake_nodepool.getNodeRequests()[0]
4722 self.fake_nodepool.addFailRequest(req)
4723
4724 self.fake_nodepool.paused = False
4725 self.waitUntilSettled()
4726
4727 self.assertEqual(A.data['status'], 'NEW')
4728 self.assertEqual(A.reported, 2)
4729 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4730 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4731 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4732
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004733 def test_nodepool_priority(self):
4734 "Test that nodes are requested at the correct priority"
4735
4736 self.fake_nodepool.paused = True
4737
4738 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4739 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4740
4741 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4742 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4743
4744 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4745 C.addApproval('Code-Review', 2)
4746 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4747
4748 self.waitUntilSettled()
4749
4750 reqs = self.fake_nodepool.getNodeRequests()
4751
4752 # The requests come back sorted by oid. Since we have three requests
4753 # for the three changes each with a different priority.
4754 # Also they get a serial number based on order they were received
4755 # so the number on the endof the oid should map to order submitted.
4756
4757 # * gate first - high priority - change C
4758 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4759 self.assertEqual(reqs[0]['node_types'], ['label1'])
4760 # * check second - normal priority - change B
4761 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4762 self.assertEqual(reqs[1]['node_types'], ['label1'])
4763 # * post third - low priority - change A
4764 # additionally, the post job defined uses an ubuntu-xenial node,
4765 # so we include that check just as an extra verification
4766 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4767 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4768
4769 self.fake_nodepool.paused = False
4770 self.waitUntilSettled()
4771
James E. Blairfceaf412017-09-29 13:44:57 -07004772 @simple_layout('layouts/multiple-templates.yaml')
4773 def test_multiple_project_templates(self):
4774 # Test that applying multiple project templates to a project
4775 # doesn't alter them when used for a second project.
4776 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4777 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4778 self.waitUntilSettled()
4779
4780 build = self.getJobFromHistory('py27')
4781 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4782
James E. Blair29c77002017-10-05 14:56:35 -07004783 def test_pending_merge_in_reconfig(self):
4784 # Test that if we are waiting for an outstanding merge on
4785 # reconfiguration that we continue to do so.
4786 self.gearman_server.hold_merge_jobs_in_queue = True
4787 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4788 A.setMerged()
4789 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4790 self.waitUntilSettled()
4791 # Reconfigure while we still have an outstanding merge job
4792 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4793 None)
4794 self.waitUntilSettled()
4795 # Verify the merge job is still running and that the item is
4796 # in the pipeline
4797 self.assertEqual(len(self.sched.merger.jobs), 1)
4798 tenant = self.sched.abide.tenants.get('tenant-one')
4799 pipeline = tenant.layout.pipelines['post']
4800 self.assertEqual(len(pipeline.getAllItems()), 1)
4801 self.gearman_server.hold_merge_jobs_in_queue = False
4802 self.gearman_server.release()
4803 self.waitUntilSettled()
4804
Arieb6f068c2016-10-09 13:11:06 +03004805
James E. Blaira002b032017-04-18 10:35:48 -07004806class TestExecutor(ZuulTestCase):
4807 tenant_config_file = 'config/single-tenant/main.yaml'
4808
4809 def assertFinalState(self):
4810 # In this test, we expect to shut down in a non-final state,
4811 # so skip these checks.
4812 pass
4813
4814 def assertCleanShutdown(self):
4815 self.log.debug("Assert clean shutdown")
4816
4817 # After shutdown, make sure no jobs are running
4818 self.assertEqual({}, self.executor_server.job_workers)
4819
4820 # Make sure that git.Repo objects have been garbage collected.
4821 repos = []
4822 gc.collect()
4823 for obj in gc.get_objects():
4824 if isinstance(obj, git.Repo):
4825 self.log.debug("Leaked git repo object: %s" % repr(obj))
4826 repos.append(obj)
4827 self.assertEqual(len(repos), 0)
4828
4829 def test_executor_shutdown(self):
4830 "Test that the executor can shut down with jobs running"
4831
4832 self.executor_server.hold_jobs_in_build = True
4833 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004834 A.addApproval('Code-Review', 2)
4835 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004836 self.waitUntilSettled()
4837
4838
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004839class TestDependencyGraph(ZuulTestCase):
4840 tenant_config_file = 'config/dependency-graph/main.yaml'
4841
4842 def test_dependeny_graph_dispatch_jobs_once(self):
4843 "Test a job in a dependency graph is queued only once"
4844 # Job dependencies, starting with A
4845 # A
4846 # / \
4847 # B C
4848 # / \ / \
4849 # D F E
4850 # |
4851 # G
4852
4853 self.executor_server.hold_jobs_in_build = True
4854 change = self.fake_gerrit.addFakeChange(
4855 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004856 change.addApproval('Code-Review', 2)
4857 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004858
4859 self.waitUntilSettled()
4860 self.assertEqual([b.name for b in self.builds], ['A'])
4861
4862 self.executor_server.release('A')
4863 self.waitUntilSettled()
4864 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4865
4866 self.executor_server.release('B')
4867 self.waitUntilSettled()
4868 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4869
4870 self.executor_server.release('D')
4871 self.waitUntilSettled()
4872 self.assertEqual([b.name for b in self.builds], ['C'])
4873
4874 self.executor_server.release('C')
4875 self.waitUntilSettled()
4876 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4877
4878 self.executor_server.release('F')
4879 self.waitUntilSettled()
4880 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4881
4882 self.executor_server.release('G')
4883 self.waitUntilSettled()
4884 self.assertEqual([b.name for b in self.builds], ['E'])
4885
4886 self.executor_server.release('E')
4887 self.waitUntilSettled()
4888 self.assertEqual(len(self.builds), 0)
4889
4890 self.executor_server.hold_jobs_in_build = False
4891 self.executor_server.release()
4892 self.waitUntilSettled()
4893
4894 self.assertEqual(len(self.builds), 0)
4895 self.assertEqual(len(self.history), 7)
4896
4897 self.assertEqual(change.data['status'], 'MERGED')
4898 self.assertEqual(change.reported, 2)
4899
4900 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4901 "Test that a job waits till all dependencies are successful"
4902 # Job dependencies, starting with A
4903 # A
4904 # / \
4905 # B C*
4906 # / \ / \
4907 # D F E
4908 # |
4909 # G
4910
4911 self.executor_server.hold_jobs_in_build = True
4912 change = self.fake_gerrit.addFakeChange(
4913 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004914 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004915
4916 self.executor_server.failJob('C', change)
4917
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004918 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004919
4920 self.waitUntilSettled()
4921 self.assertEqual([b.name for b in self.builds], ['A'])
4922
4923 self.executor_server.release('A')
4924 self.waitUntilSettled()
4925 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4926
4927 self.executor_server.release('B')
4928 self.waitUntilSettled()
4929 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4930
4931 self.executor_server.release('D')
4932 self.waitUntilSettled()
4933 self.assertEqual([b.name for b in self.builds], ['C'])
4934
4935 self.executor_server.release('C')
4936 self.waitUntilSettled()
4937 self.assertEqual(len(self.builds), 0)
4938
4939 self.executor_server.hold_jobs_in_build = False
4940 self.executor_server.release()
4941 self.waitUntilSettled()
4942
4943 self.assertEqual(len(self.builds), 0)
4944 self.assertEqual(len(self.history), 4)
4945
4946 self.assertEqual(change.data['status'], 'NEW')
4947 self.assertEqual(change.reported, 2)
4948
4949
Arieb6f068c2016-10-09 13:11:06 +03004950class TestDuplicatePipeline(ZuulTestCase):
4951 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4952
4953 def test_duplicate_pipelines(self):
4954 "Test that a change matching multiple pipelines works"
4955
4956 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4957 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4958 self.waitUntilSettled()
4959
4960 self.assertHistory([
4961 dict(name='project-test1', result='SUCCESS', changes='1,1',
4962 pipeline='dup1'),
4963 dict(name='project-test1', result='SUCCESS', changes='1,1',
4964 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004965 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004966
4967 self.assertEqual(len(A.messages), 2)
4968
Paul Belangera46a3742016-11-09 14:23:03 -05004969 if 'dup1' in A.messages[0]:
4970 self.assertIn('dup1', A.messages[0])
4971 self.assertNotIn('dup2', A.messages[0])
4972 self.assertIn('project-test1', A.messages[0])
4973 self.assertIn('dup2', A.messages[1])
4974 self.assertNotIn('dup1', A.messages[1])
4975 self.assertIn('project-test1', A.messages[1])
4976 else:
4977 self.assertIn('dup1', A.messages[1])
4978 self.assertNotIn('dup2', A.messages[1])
4979 self.assertIn('project-test1', A.messages[1])
4980 self.assertIn('dup2', A.messages[0])
4981 self.assertNotIn('dup1', A.messages[0])
4982 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004983
4984
Paul Belanger86085b32016-11-03 12:48:57 -04004985class TestSchedulerTemplatedProject(ZuulTestCase):
4986 tenant_config_file = 'config/templated-project/main.yaml'
4987
Paul Belanger174a8272017-03-14 13:20:10 -04004988 def test_job_from_templates_executed(self):
4989 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004990
4991 A = self.fake_gerrit.addFakeChange(
4992 'org/templated-project', 'master', 'A')
4993 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4994 self.waitUntilSettled()
4995
4996 self.assertEqual(self.getJobFromHistory('project-test1').result,
4997 'SUCCESS')
4998 self.assertEqual(self.getJobFromHistory('project-test2').result,
4999 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04005000
5001 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04005002 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04005003
5004 A = self.fake_gerrit.addFakeChange(
5005 'org/layered-project', 'master', 'A')
5006 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5007 self.waitUntilSettled()
5008
5009 self.assertEqual(self.getJobFromHistory('project-test1').result,
5010 'SUCCESS')
5011 self.assertEqual(self.getJobFromHistory('project-test2').result,
5012 'SUCCESS')
5013 self.assertEqual(self.getJobFromHistory('layered-project-test3'
5014 ).result, 'SUCCESS')
5015 self.assertEqual(self.getJobFromHistory('layered-project-test4'
5016 ).result, 'SUCCESS')
5017 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
5018 ).result, 'SUCCESS')
5019 self.assertEqual(self.getJobFromHistory('project-test6').result,
5020 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08005021
James E. Blaire74f5712017-09-29 15:14:31 -07005022 def test_unimplied_branch_matchers(self):
5023 # This tests that there are no implied branch matchers added
5024 # by project templates.
5025 self.create_branch('org/layered-project', 'stable')
5026
5027 A = self.fake_gerrit.addFakeChange(
5028 'org/layered-project', 'stable', 'A')
5029
5030 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5031 self.waitUntilSettled()
5032
5033 self.assertEqual(self.getJobFromHistory('project-test1').result,
5034 'SUCCESS')
5035 print(self.getJobFromHistory('project-test1').
5036 parameters['zuul']['_inheritance_path'])
5037
5038 def test_implied_branch_matchers(self):
5039 # This tests that there is an implied branch matcher when a
5040 # template is used on an in-repo project pipeline definition.
5041 self.create_branch('untrusted-config', 'stable')
5042 self.fake_gerrit.addEvent(
5043 self.fake_gerrit.getFakeBranchCreatedEvent(
5044 'untrusted-config', 'stable'))
5045 self.waitUntilSettled()
5046
5047 A = self.fake_gerrit.addFakeChange(
5048 'untrusted-config', 'stable', 'A')
5049
5050 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5051 self.waitUntilSettled()
5052
5053 self.assertEqual(self.getJobFromHistory('project-test1').result,
5054 'SUCCESS')
5055 print(self.getJobFromHistory('project-test1').
5056 parameters['zuul']['_inheritance_path'])
5057
Adam Gandelman94a60062016-11-21 16:43:14 -08005058
5059class TestSchedulerSuccessURL(ZuulTestCase):
5060 tenant_config_file = 'config/success-url/main.yaml'
5061
5062 def test_success_url(self):
5063 "Ensure bad build params are ignored"
5064 self.sched.reconfigure(self.config)
5065 self.init_repo('org/docs')
5066
5067 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
5068 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5069 self.waitUntilSettled()
5070
5071 # Both builds ran: docs-draft-test + docs-draft-test2
5072 self.assertEqual(len(self.history), 2)
5073
5074 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08005075 for build in self.history:
5076 if build.name == 'docs-draft-test':
5077 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06005078 elif build.name == 'docs-draft-test2':
5079 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08005080
5081 # Two msgs: 'Starting...' + results
5082 self.assertEqual(len(self.smtp_messages), 2)
5083 body = self.smtp_messages[1]['body'].splitlines()
5084 self.assertEqual('Build succeeded.', body[0])
5085
5086 self.assertIn(
5087 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
5088 'docs-draft-test/%s/publish-docs/' % uuid,
5089 body[2])
5090
Paul Belanger174a8272017-03-14 13:20:10 -04005091 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08005092 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05005093 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
5094 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06005095 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08005096 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08005097
5098
Adam Gandelman4da00f62016-12-09 15:47:33 -08005099class TestSchedulerMerges(ZuulTestCase):
5100 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08005101
5102 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04005103 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08005104 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04005105 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08005106 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
5107 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
5108 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005109 A.addApproval('Code-Review', 2)
5110 B.addApproval('Code-Review', 2)
5111 C.addApproval('Code-Review', 2)
5112 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5113 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5114 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08005115 self.waitUntilSettled()
5116
5117 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07005118 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5119 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08005120 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005121 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08005122 repo_messages.reverse()
5123
Paul Belanger174a8272017-03-14 13:20:10 -04005124 self.executor_server.hold_jobs_in_build = False
5125 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08005126 self.waitUntilSettled()
5127
5128 return repo_messages
5129
5130 def _test_merge(self, mode):
5131 us_path = os.path.join(
5132 self.upstream_root, 'org/project-%s' % mode)
5133 expected_messages = [
5134 'initial commit',
5135 'add content from fixture',
5136 # the intermediate commits order is nondeterministic
5137 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
5138 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
5139 ]
5140 result = self._test_project_merge_mode(mode)
5141 self.assertEqual(result[:2], expected_messages[:2])
5142 self.assertEqual(result[-2:], expected_messages[-2:])
5143
5144 def test_project_merge_mode_merge(self):
5145 self._test_merge('merge')
5146
5147 def test_project_merge_mode_merge_resolve(self):
5148 self._test_merge('merge-resolve')
5149
5150 def test_project_merge_mode_cherrypick(self):
5151 expected_messages = [
5152 'initial commit',
5153 'add content from fixture',
5154 'A-1',
5155 'B-1',
5156 'C-1']
5157 result = self._test_project_merge_mode('cherry-pick')
5158 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005159
5160 def test_merge_branch(self):
5161 "Test that the right commits are on alternate branches"
5162 self.create_branch('org/project-merge-branches', 'mp')
5163
Paul Belanger174a8272017-03-14 13:20:10 -04005164 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005165 A = self.fake_gerrit.addFakeChange(
5166 'org/project-merge-branches', 'mp', 'A')
5167 B = self.fake_gerrit.addFakeChange(
5168 'org/project-merge-branches', 'mp', 'B')
5169 C = self.fake_gerrit.addFakeChange(
5170 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005171 A.addApproval('Code-Review', 2)
5172 B.addApproval('Code-Review', 2)
5173 C.addApproval('Code-Review', 2)
5174 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5175 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5176 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005177 self.waitUntilSettled()
5178
Paul Belanger174a8272017-03-14 13:20:10 -04005179 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005180 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005181 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005182 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005183 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005184 self.waitUntilSettled()
5185
5186 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005187 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005188 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5189 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005190 repo = git.Repo(path)
5191
James E. Blair247cab72017-07-20 16:52:36 -07005192 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005193 repo_messages.reverse()
5194 correct_messages = [
5195 'initial commit',
5196 'add content from fixture',
5197 'mp commit',
5198 'A-1', 'B-1', 'C-1']
5199 self.assertEqual(repo_messages, correct_messages)
5200
Paul Belanger174a8272017-03-14 13:20:10 -04005201 self.executor_server.hold_jobs_in_build = False
5202 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005203 self.waitUntilSettled()
5204
5205 def test_merge_multi_branch(self):
5206 "Test that dependent changes on multiple branches are merged"
5207 self.create_branch('org/project-merge-branches', 'mp')
5208
Paul Belanger174a8272017-03-14 13:20:10 -04005209 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005210 A = self.fake_gerrit.addFakeChange(
5211 'org/project-merge-branches', 'master', 'A')
5212 B = self.fake_gerrit.addFakeChange(
5213 'org/project-merge-branches', 'mp', 'B')
5214 C = self.fake_gerrit.addFakeChange(
5215 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005216 A.addApproval('Code-Review', 2)
5217 B.addApproval('Code-Review', 2)
5218 C.addApproval('Code-Review', 2)
5219 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5220 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5221 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005222 self.waitUntilSettled()
5223
5224 job_A = None
5225 for job in self.builds:
5226 if 'project-merge' in job.name:
5227 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005228
James E. Blair2a535672017-04-27 12:03:15 -07005229 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5230 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005231 repo = git.Repo(path)
5232 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005233 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005234 repo_messages.reverse()
5235 correct_messages = [
5236 'initial commit', 'add content from fixture', 'A-1']
5237 self.assertEqual(repo_messages, correct_messages)
5238
Paul Belanger174a8272017-03-14 13:20:10 -04005239 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005240 self.waitUntilSettled()
5241
5242 job_B = None
5243 for job in self.builds:
5244 if 'project-merge' in job.name:
5245 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005246
James E. Blair2a535672017-04-27 12:03:15 -07005247 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5248 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005249 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005250 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005251 repo_messages.reverse()
5252 correct_messages = [
5253 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5254 self.assertEqual(repo_messages, correct_messages)
5255
Paul Belanger174a8272017-03-14 13:20:10 -04005256 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005257 self.waitUntilSettled()
5258
5259 job_C = None
5260 for job in self.builds:
5261 if 'project-merge' in job.name:
5262 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005263
James E. Blair2a535672017-04-27 12:03:15 -07005264 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5265 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005266 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005267 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005268
5269 repo_messages.reverse()
5270 correct_messages = [
5271 'initial commit', 'add content from fixture',
5272 'A-1', 'C-1']
5273 # Ensure the right commits are in the history for this ref
5274 self.assertEqual(repo_messages, correct_messages)
5275
Paul Belanger174a8272017-03-14 13:20:10 -04005276 self.executor_server.hold_jobs_in_build = False
5277 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005278 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005279
5280
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005281class TestSemaphore(ZuulTestCase):
5282 tenant_config_file = 'config/semaphore/main.yaml'
5283
5284 def test_semaphore_one(self):
5285 "Test semaphores with max=1 (mutex)"
5286 tenant = self.sched.abide.tenants.get('tenant-one')
5287
5288 self.executor_server.hold_jobs_in_build = True
5289
5290 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5291 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5292 self.assertFalse('test-semaphore' in
5293 tenant.semaphore_handler.semaphores)
5294
5295 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5296 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5297 self.waitUntilSettled()
5298
5299 self.assertEqual(len(self.builds), 3)
5300 self.assertEqual(self.builds[0].name, 'project-test1')
5301 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5302 self.assertEqual(self.builds[2].name, 'project-test1')
5303
5304 self.executor_server.release('semaphore-one-test1')
5305 self.waitUntilSettled()
5306
5307 self.assertEqual(len(self.builds), 3)
5308 self.assertEqual(self.builds[0].name, 'project-test1')
5309 self.assertEqual(self.builds[1].name, 'project-test1')
5310 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5311 self.assertTrue('test-semaphore' in
5312 tenant.semaphore_handler.semaphores)
5313
5314 self.executor_server.release('semaphore-one-test2')
5315 self.waitUntilSettled()
5316
5317 self.assertEqual(len(self.builds), 3)
5318 self.assertEqual(self.builds[0].name, 'project-test1')
5319 self.assertEqual(self.builds[1].name, 'project-test1')
5320 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5321 self.assertTrue('test-semaphore' in
5322 tenant.semaphore_handler.semaphores)
5323
5324 self.executor_server.release('semaphore-one-test1')
5325 self.waitUntilSettled()
5326
5327 self.assertEqual(len(self.builds), 3)
5328 self.assertEqual(self.builds[0].name, 'project-test1')
5329 self.assertEqual(self.builds[1].name, 'project-test1')
5330 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5331 self.assertTrue('test-semaphore' in
5332 tenant.semaphore_handler.semaphores)
5333
5334 self.executor_server.release('semaphore-one-test2')
5335 self.waitUntilSettled()
5336
5337 self.assertEqual(len(self.builds), 2)
5338 self.assertEqual(self.builds[0].name, 'project-test1')
5339 self.assertEqual(self.builds[1].name, 'project-test1')
5340 self.assertFalse('test-semaphore' in
5341 tenant.semaphore_handler.semaphores)
5342
5343 self.executor_server.hold_jobs_in_build = False
5344 self.executor_server.release()
5345
5346 self.waitUntilSettled()
5347 self.assertEqual(len(self.builds), 0)
5348
5349 self.assertEqual(A.reported, 1)
5350 self.assertEqual(B.reported, 1)
5351 self.assertFalse('test-semaphore' in
5352 tenant.semaphore_handler.semaphores)
5353
5354 def test_semaphore_two(self):
5355 "Test semaphores with max>1"
5356 tenant = self.sched.abide.tenants.get('tenant-one')
5357
5358 self.executor_server.hold_jobs_in_build = True
5359 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5360 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5361 self.assertFalse('test-semaphore-two' in
5362 tenant.semaphore_handler.semaphores)
5363
5364 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5365 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5366 self.waitUntilSettled()
5367
5368 self.assertEqual(len(self.builds), 4)
5369 self.assertEqual(self.builds[0].name, 'project-test1')
5370 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5371 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5372 self.assertEqual(self.builds[3].name, 'project-test1')
5373 self.assertTrue('test-semaphore-two' in
5374 tenant.semaphore_handler.semaphores)
5375 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5376 'test-semaphore-two', [])), 2)
5377
5378 self.executor_server.release('semaphore-two-test1')
5379 self.waitUntilSettled()
5380
5381 self.assertEqual(len(self.builds), 4)
5382 self.assertEqual(self.builds[0].name, 'project-test1')
5383 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5384 self.assertEqual(self.builds[2].name, 'project-test1')
5385 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5386 self.assertTrue('test-semaphore-two' in
5387 tenant.semaphore_handler.semaphores)
5388 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5389 'test-semaphore-two', [])), 2)
5390
5391 self.executor_server.release('semaphore-two-test2')
5392 self.waitUntilSettled()
5393
5394 self.assertEqual(len(self.builds), 4)
5395 self.assertEqual(self.builds[0].name, 'project-test1')
5396 self.assertEqual(self.builds[1].name, 'project-test1')
5397 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5398 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5399 self.assertTrue('test-semaphore-two' in
5400 tenant.semaphore_handler.semaphores)
5401 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5402 'test-semaphore-two', [])), 2)
5403
5404 self.executor_server.release('semaphore-two-test1')
5405 self.waitUntilSettled()
5406
5407 self.assertEqual(len(self.builds), 3)
5408 self.assertEqual(self.builds[0].name, 'project-test1')
5409 self.assertEqual(self.builds[1].name, 'project-test1')
5410 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5411 self.assertTrue('test-semaphore-two' in
5412 tenant.semaphore_handler.semaphores)
5413 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5414 'test-semaphore-two', [])), 1)
5415
5416 self.executor_server.release('semaphore-two-test2')
5417 self.waitUntilSettled()
5418
5419 self.assertEqual(len(self.builds), 2)
5420 self.assertEqual(self.builds[0].name, 'project-test1')
5421 self.assertEqual(self.builds[1].name, 'project-test1')
5422 self.assertFalse('test-semaphore-two' in
5423 tenant.semaphore_handler.semaphores)
5424
5425 self.executor_server.hold_jobs_in_build = False
5426 self.executor_server.release()
5427
5428 self.waitUntilSettled()
5429 self.assertEqual(len(self.builds), 0)
5430
5431 self.assertEqual(A.reported, 1)
5432 self.assertEqual(B.reported, 1)
5433
5434 def test_semaphore_abandon(self):
5435 "Test abandon with job semaphores"
5436 self.executor_server.hold_jobs_in_build = True
5437 tenant = self.sched.abide.tenants.get('tenant-one')
5438 check_pipeline = tenant.layout.pipelines['check']
5439
5440 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5441 self.assertFalse('test-semaphore' in
5442 tenant.semaphore_handler.semaphores)
5443
5444 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5445 self.waitUntilSettled()
5446
5447 self.assertTrue('test-semaphore' in
5448 tenant.semaphore_handler.semaphores)
5449
5450 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5451 self.waitUntilSettled()
5452
5453 # The check pipeline should be empty
5454 items = check_pipeline.getAllItems()
5455 self.assertEqual(len(items), 0)
5456
5457 # The semaphore should be released
5458 self.assertFalse('test-semaphore' in
5459 tenant.semaphore_handler.semaphores)
5460
5461 self.executor_server.hold_jobs_in_build = False
5462 self.executor_server.release()
5463 self.waitUntilSettled()
5464
Tobias Henkel0f714002017-06-30 23:30:52 +02005465 def test_semaphore_new_patchset(self):
5466 "Test new patchset with job semaphores"
5467 self.executor_server.hold_jobs_in_build = True
5468 tenant = self.sched.abide.tenants.get('tenant-one')
5469 check_pipeline = tenant.layout.pipelines['check']
5470
5471 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5472 self.assertFalse('test-semaphore' in
5473 tenant.semaphore_handler.semaphores)
5474
5475 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5476 self.waitUntilSettled()
5477
5478 self.assertTrue('test-semaphore' in
5479 tenant.semaphore_handler.semaphores)
5480 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5481 self.assertEqual(len(semaphore), 1)
5482
5483 A.addPatchset()
5484 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5485 self.waitUntilSettled()
5486
5487 self.assertTrue('test-semaphore' in
5488 tenant.semaphore_handler.semaphores)
5489 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5490 self.assertEqual(len(semaphore), 1)
5491
5492 items = check_pipeline.getAllItems()
5493 self.assertEqual(items[0].change.number, '1')
5494 self.assertEqual(items[0].change.patchset, '2')
5495 self.assertTrue(items[0].live)
5496
5497 self.executor_server.hold_jobs_in_build = False
5498 self.executor_server.release()
5499 self.waitUntilSettled()
5500
5501 # The semaphore should be released
5502 self.assertFalse('test-semaphore' in
5503 tenant.semaphore_handler.semaphores)
5504
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005505 def test_semaphore_reconfigure(self):
5506 "Test reconfigure with job semaphores"
5507 self.executor_server.hold_jobs_in_build = True
5508 tenant = self.sched.abide.tenants.get('tenant-one')
5509 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5510 self.assertFalse('test-semaphore' in
5511 tenant.semaphore_handler.semaphores)
5512
5513 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5514 self.waitUntilSettled()
5515
5516 self.assertTrue('test-semaphore' in
5517 tenant.semaphore_handler.semaphores)
5518
5519 # reconfigure without layout change
5520 self.sched.reconfigure(self.config)
5521 self.waitUntilSettled()
5522 tenant = self.sched.abide.tenants.get('tenant-one')
5523
5524 # semaphore still must be held
5525 self.assertTrue('test-semaphore' in
5526 tenant.semaphore_handler.semaphores)
5527
5528 self.commitConfigUpdate(
5529 'common-config',
5530 'config/semaphore/zuul-reconfiguration.yaml')
5531 self.sched.reconfigure(self.config)
5532 self.waitUntilSettled()
5533 tenant = self.sched.abide.tenants.get('tenant-one')
5534
5535 self.executor_server.release('project-test1')
5536 self.waitUntilSettled()
5537
5538 # There should be no builds anymore
5539 self.assertEqual(len(self.builds), 0)
5540
5541 # The semaphore should be released
5542 self.assertFalse('test-semaphore' in
5543 tenant.semaphore_handler.semaphores)
5544
5545
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005546class TestSemaphoreMultiTenant(ZuulTestCase):
5547 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5548
5549 def test_semaphore_tenant_isolation(self):
5550 "Test semaphores in multiple tenants"
5551
5552 self.waitUntilSettled()
5553 tenant_one = self.sched.abide.tenants.get('tenant-one')
5554 tenant_two = self.sched.abide.tenants.get('tenant-two')
5555
5556 self.executor_server.hold_jobs_in_build = True
5557 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5558 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5559 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5560 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5561 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5562 self.assertFalse('test-semaphore' in
5563 tenant_one.semaphore_handler.semaphores)
5564 self.assertFalse('test-semaphore' in
5565 tenant_two.semaphore_handler.semaphores)
5566
5567 # add patches to project1 of tenant-one
5568 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5569 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5570 self.waitUntilSettled()
5571
5572 # one build of project1-test1 must run
5573 # semaphore of tenant-one must be acquired once
5574 # semaphore of tenant-two must not be acquired
5575 self.assertEqual(len(self.builds), 1)
5576 self.assertEqual(self.builds[0].name, 'project1-test1')
5577 self.assertTrue('test-semaphore' in
5578 tenant_one.semaphore_handler.semaphores)
5579 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5580 'test-semaphore', [])), 1)
5581 self.assertFalse('test-semaphore' in
5582 tenant_two.semaphore_handler.semaphores)
5583
5584 # add patches to project2 of tenant-two
5585 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5586 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5587 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5588 self.waitUntilSettled()
5589
5590 # one build of project1-test1 must run
5591 # two builds of project2-test1 must run
5592 # semaphore of tenant-one must be acquired once
5593 # semaphore of tenant-two must be acquired twice
5594 self.assertEqual(len(self.builds), 3)
5595 self.assertEqual(self.builds[0].name, 'project1-test1')
5596 self.assertEqual(self.builds[1].name, 'project2-test1')
5597 self.assertEqual(self.builds[2].name, 'project2-test1')
5598 self.assertTrue('test-semaphore' in
5599 tenant_one.semaphore_handler.semaphores)
5600 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5601 'test-semaphore', [])), 1)
5602 self.assertTrue('test-semaphore' in
5603 tenant_two.semaphore_handler.semaphores)
5604 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5605 'test-semaphore', [])), 2)
5606
5607 self.executor_server.release('project1-test1')
5608 self.waitUntilSettled()
5609
5610 # one build of project1-test1 must run
5611 # two builds of project2-test1 must run
5612 # semaphore of tenant-one must be acquired once
5613 # semaphore of tenant-two must be acquired twice
5614 self.assertEqual(len(self.builds), 3)
5615 self.assertEqual(self.builds[0].name, 'project2-test1')
5616 self.assertEqual(self.builds[1].name, 'project2-test1')
5617 self.assertEqual(self.builds[2].name, 'project1-test1')
5618 self.assertTrue('test-semaphore' in
5619 tenant_one.semaphore_handler.semaphores)
5620 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5621 'test-semaphore', [])), 1)
5622 self.assertTrue('test-semaphore' in
5623 tenant_two.semaphore_handler.semaphores)
5624 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5625 'test-semaphore', [])), 2)
5626
5627 self.executor_server.release('project2-test1')
5628 self.waitUntilSettled()
5629
5630 # one build of project1-test1 must run
5631 # one build of project2-test1 must run
5632 # semaphore of tenant-one must be acquired once
5633 # semaphore of tenant-two must be acquired once
5634 self.assertEqual(len(self.builds), 2)
5635 self.assertTrue('test-semaphore' in
5636 tenant_one.semaphore_handler.semaphores)
5637 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5638 'test-semaphore', [])), 1)
5639 self.assertTrue('test-semaphore' in
5640 tenant_two.semaphore_handler.semaphores)
5641 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5642 'test-semaphore', [])), 1)
5643
5644 self.executor_server.hold_jobs_in_build = False
5645 self.executor_server.release()
5646
5647 self.waitUntilSettled()
5648
5649 # no build must run
5650 # semaphore of tenant-one must not be acquired
5651 # semaphore of tenant-two must not be acquired
5652 self.assertEqual(len(self.builds), 0)
5653 self.assertFalse('test-semaphore' in
5654 tenant_one.semaphore_handler.semaphores)
5655 self.assertFalse('test-semaphore' in
5656 tenant_two.semaphore_handler.semaphores)
5657
5658 self.assertEqual(A.reported, 1)
5659 self.assertEqual(B.reported, 1)
5660
5661
5662class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005663 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005664 tenant_config_file = 'config/in-repo/main.yaml'
5665
5666 def test_semaphore_in_repo(self):
5667 "Test semaphores in repo config"
5668
5669 # This tests dynamic semaphore handling in project repos. The semaphore
5670 # max value should not be evaluated dynamically but must be updated
5671 # after the change lands.
5672
5673 self.waitUntilSettled()
5674 tenant = self.sched.abide.tenants.get('tenant-one')
5675
5676 in_repo_conf = textwrap.dedent(
5677 """
5678 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005679 name: project-test1
5680
5681 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005682 name: project-test2
5683 semaphore: test-semaphore
5684
5685 - project:
5686 name: org/project
5687 tenant-one-gate:
5688 jobs:
5689 - project-test2
5690
5691 # the max value in dynamic layout must be ignored
5692 - semaphore:
5693 name: test-semaphore
5694 max: 2
5695 """)
5696
5697 in_repo_playbook = textwrap.dedent(
5698 """
5699 - hosts: all
5700 tasks: []
5701 """)
5702
5703 file_dict = {'.zuul.yaml': in_repo_conf,
5704 'playbooks/project-test2.yaml': in_repo_playbook}
5705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5706 files=file_dict)
5707 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5708 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5709 B.setDependsOn(A, 1)
5710 C.setDependsOn(A, 1)
5711
5712 self.executor_server.hold_jobs_in_build = True
5713
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005714 A.addApproval('Code-Review', 2)
5715 B.addApproval('Code-Review', 2)
5716 C.addApproval('Code-Review', 2)
5717 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5718 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5719 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005720 self.waitUntilSettled()
5721
5722 # check that the layout in a queue item still has max value of 1
5723 # for test-semaphore
5724 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5725 queue = None
5726 for queue_candidate in pipeline.queues:
5727 if queue_candidate.name == 'org/project':
5728 queue = queue_candidate
5729 break
5730 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005731 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005732 dynamic_test_semaphore = \
5733 item_dynamic_layout.semaphores.get('test-semaphore')
5734 self.assertEqual(dynamic_test_semaphore.max, 1)
5735
5736 # one build must be in queue, one semaphores acquired
5737 self.assertEqual(len(self.builds), 1)
5738 self.assertEqual(self.builds[0].name, 'project-test2')
5739 self.assertTrue('test-semaphore' in
5740 tenant.semaphore_handler.semaphores)
5741 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5742 'test-semaphore', [])), 1)
5743
5744 self.executor_server.release('project-test2')
5745 self.waitUntilSettled()
5746
5747 # change A must be merged
5748 self.assertEqual(A.data['status'], 'MERGED')
5749 self.assertEqual(A.reported, 2)
5750
5751 # send change-merged event as the gerrit mock doesn't send it
5752 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5753 self.waitUntilSettled()
5754
5755 # now that change A was merged, the new semaphore max must be effective
5756 tenant = self.sched.abide.tenants.get('tenant-one')
5757 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5758
5759 # two builds must be in queue, two semaphores acquired
5760 self.assertEqual(len(self.builds), 2)
5761 self.assertEqual(self.builds[0].name, 'project-test2')
5762 self.assertEqual(self.builds[1].name, 'project-test2')
5763 self.assertTrue('test-semaphore' in
5764 tenant.semaphore_handler.semaphores)
5765 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5766 'test-semaphore', [])), 2)
5767
5768 self.executor_server.release('project-test2')
5769 self.waitUntilSettled()
5770
5771 self.assertEqual(len(self.builds), 0)
5772 self.assertFalse('test-semaphore' in
5773 tenant.semaphore_handler.semaphores)
5774
5775 self.executor_server.hold_jobs_in_build = False
5776 self.executor_server.release()
5777
5778 self.waitUntilSettled()
5779 self.assertEqual(len(self.builds), 0)
5780
5781 self.assertEqual(A.reported, 2)
5782 self.assertEqual(B.reported, 2)
5783 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005784
5785
5786class TestSchedulerBranchMatcher(ZuulTestCase):
5787
5788 @simple_layout('layouts/matcher-test.yaml')
5789 def test_job_branch_ignored(self):
5790 '''
5791 Test that branch matching logic works.
5792
5793 The 'ignore-branch' job has a branch matcher that is supposed to
5794 match every branch except for the 'featureA' branch, so it should
5795 not be run on a change to that branch.
5796 '''
5797 self.create_branch('org/project', 'featureA')
5798 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5799 A.addApproval('Code-Review', 2)
5800 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5801 self.waitUntilSettled()
5802 self.printHistory()
5803 self.assertEqual(self.getJobFromHistory('project-test1').result,
5804 'SUCCESS')
5805 self.assertJobNotInHistory('ignore-branch')
5806 self.assertEqual(A.data['status'], 'MERGED')
5807 self.assertEqual(A.reported, 2,
5808 "A should report start and success")
5809 self.assertIn('gate', A.messages[1],
5810 "A should transit gate")