blob: 90904213f32aed9eb9e8d7351be2a170bf98ffe5 [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
David Shrewsburyc4676132017-10-16 12:41:57 -04001518 @simple_layout('layouts/autohold.yaml')
1519 def test_autohold_list(self):
1520 client = zuul.rpcclient.RPCClient('127.0.0.1',
1521 self.gearman_server.port)
1522 self.addCleanup(client.shutdown)
1523
1524 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1525 "reason text", 1)
1526 self.assertTrue(r)
1527
1528 autohold_requests = client.autohold_list()
1529 self.assertNotEqual({}, autohold_requests)
1530 self.assertEqual(1, len(autohold_requests.keys()))
1531
1532 # The single dict key should be a CSV string value
1533 key = list(autohold_requests.keys())[0]
1534 tenant, project, job = key.split(',')
1535
1536 self.assertEqual('tenant-one', tenant)
1537 self.assertIn('org/project', project)
1538 self.assertEqual('project-test2', job)
1539
1540 # Note: the value is converted from set to list by json.
1541 self.assertEqual([1, "reason text"], autohold_requests[key])
1542
James E. Blair7cb84542017-04-19 13:35:05 -07001543 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001544 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001545 # This particular test does a large amount of merges and needs a little
1546 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001547 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001548 "test that dependent changes behind dequeued changes work"
1549 # This complicated test is a reproduction of a real life bug
1550 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001551
Paul Belanger174a8272017-03-14 13:20:10 -04001552 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001553 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1554 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1555 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1556 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1557 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1558 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1559 D.setDependsOn(C, 1)
1560 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001561 A.addApproval('Code-Review', 2)
1562 B.addApproval('Code-Review', 2)
1563 C.addApproval('Code-Review', 2)
1564 D.addApproval('Code-Review', 2)
1565 E.addApproval('Code-Review', 2)
1566 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001567
1568 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001569
1570 # Change object re-use in the gerrit trigger is hidden if
1571 # changes are added in quick succession; waiting makes it more
1572 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001573 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001574 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001575 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001576 self.waitUntilSettled()
1577
Paul Belanger174a8272017-03-14 13:20:10 -04001578 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001579 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001580 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001581 self.waitUntilSettled()
1582
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001583 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001584 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001585 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001586 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001587 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001588 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001589 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001590 self.waitUntilSettled()
1591
Paul Belanger174a8272017-03-14 13:20:10 -04001592 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001593 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001594 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001595 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001596 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001597 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001598 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001599 self.waitUntilSettled()
1600
1601 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001602
1603 # Grab pointers to the jobs we want to release before
1604 # releasing any, because list indexes may change as
1605 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001606 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001607 a.release()
1608 b.release()
1609 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001610 self.waitUntilSettled()
1611
Paul Belanger174a8272017-03-14 13:20:10 -04001612 self.executor_server.hold_jobs_in_build = False
1613 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001614 self.waitUntilSettled()
1615
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001616 self.assertEqual(A.data['status'], 'NEW')
1617 self.assertEqual(B.data['status'], 'MERGED')
1618 self.assertEqual(C.data['status'], 'MERGED')
1619 self.assertEqual(D.data['status'], 'MERGED')
1620 self.assertEqual(E.data['status'], 'MERGED')
1621 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001622
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001623 self.assertEqual(A.reported, 2)
1624 self.assertEqual(B.reported, 2)
1625 self.assertEqual(C.reported, 2)
1626 self.assertEqual(D.reported, 2)
1627 self.assertEqual(E.reported, 2)
1628 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001629
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001630 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1631 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001632
1633 def test_merger_repack(self):
1634 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001635
James E. Blair05fed602012-09-07 12:45:24 -07001636 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001637 A.addApproval('Code-Review', 2)
1638 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001639 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001640 self.assertEqual(self.getJobFromHistory('project-merge').result,
1641 'SUCCESS')
1642 self.assertEqual(self.getJobFromHistory('project-test1').result,
1643 'SUCCESS')
1644 self.assertEqual(self.getJobFromHistory('project-test2').result,
1645 'SUCCESS')
1646 self.assertEqual(A.data['status'], 'MERGED')
1647 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001648 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001649 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001650
James E. Blair2a535672017-04-27 12:03:15 -07001651 path = os.path.join(self.merger_src_root, "review.example.com",
1652 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001653 if os.path.exists(path):
1654 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001655 path = os.path.join(self.executor_src_root, "review.example.com",
1656 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001657 if os.path.exists(path):
1658 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001659
1660 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001661 A.addApproval('Code-Review', 2)
1662 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001663 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001664 self.assertEqual(self.getJobFromHistory('project-merge').result,
1665 'SUCCESS')
1666 self.assertEqual(self.getJobFromHistory('project-test1').result,
1667 'SUCCESS')
1668 self.assertEqual(self.getJobFromHistory('project-test2').result,
1669 'SUCCESS')
1670 self.assertEqual(A.data['status'], 'MERGED')
1671 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001672
James E. Blair4886f282012-11-15 09:27:33 -08001673 def test_merger_repack_large_change(self):
1674 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001675 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001676 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001677 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001678 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001679 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001680 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001681 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001682 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001683 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001684 # TODOv3(jeblair): add hostname to upstream root
1685 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001686 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001687 path = os.path.join(self.merger_src_root, 'review.example.com',
1688 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001689 if os.path.exists(path):
1690 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001691 path = os.path.join(self.executor_src_root, 'review.example.com',
1692 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001693 if os.path.exists(path):
1694 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001695
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001696 A.addApproval('Code-Review', 2)
1697 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001698 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001699 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001700 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001701 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001702 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001703 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001704 'SUCCESS')
1705 self.assertEqual(A.data['status'], 'MERGED')
1706 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001707
James E. Blair2fa50962013-01-30 21:50:41 -08001708 def test_new_patchset_dequeues_old(self):
1709 "Test that a new patchset causes the old to be dequeued"
1710 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001711 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001712 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1713 M.setMerged()
1714
1715 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1716 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1717 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1718 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001719 A.addApproval('Code-Review', 2)
1720 B.addApproval('Code-Review', 2)
1721 C.addApproval('Code-Review', 2)
1722 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001723
1724 C.setDependsOn(B, 1)
1725 B.setDependsOn(A, 1)
1726 A.setDependsOn(M, 1)
1727
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001728 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1729 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1730 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1731 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001732 self.waitUntilSettled()
1733
1734 B.addPatchset()
1735 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1736 self.waitUntilSettled()
1737
Paul Belanger174a8272017-03-14 13:20:10 -04001738 self.executor_server.hold_jobs_in_build = False
1739 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001740 self.waitUntilSettled()
1741
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001742 self.assertEqual(A.data['status'], 'MERGED')
1743 self.assertEqual(A.reported, 2)
1744 self.assertEqual(B.data['status'], 'NEW')
1745 self.assertEqual(B.reported, 2)
1746 self.assertEqual(C.data['status'], 'NEW')
1747 self.assertEqual(C.reported, 2)
1748 self.assertEqual(D.data['status'], 'MERGED')
1749 self.assertEqual(D.reported, 2)
1750 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001751
James E. Blairba437362015-02-07 11:41:52 -08001752 def test_new_patchset_check(self):
1753 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001754
Paul Belanger174a8272017-03-14 13:20:10 -04001755 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001756
1757 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001758 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001759 tenant = self.sched.abide.tenants.get('tenant-one')
1760 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001761
1762 # Add two git-dependent changes
1763 B.setDependsOn(A, 1)
1764 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1765 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001766 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1767 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001768
1769 # A live item, and a non-live/live pair
1770 items = check_pipeline.getAllItems()
1771 self.assertEqual(len(items), 3)
1772
1773 self.assertEqual(items[0].change.number, '1')
1774 self.assertEqual(items[0].change.patchset, '1')
1775 self.assertFalse(items[0].live)
1776
1777 self.assertEqual(items[1].change.number, '2')
1778 self.assertEqual(items[1].change.patchset, '1')
1779 self.assertTrue(items[1].live)
1780
1781 self.assertEqual(items[2].change.number, '1')
1782 self.assertEqual(items[2].change.patchset, '1')
1783 self.assertTrue(items[2].live)
1784
1785 # Add a new patchset to A
1786 A.addPatchset()
1787 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1788 self.waitUntilSettled()
1789
1790 # The live copy of A,1 should be gone, but the non-live and B
1791 # should continue, and we should have a new A,2
1792 items = check_pipeline.getAllItems()
1793 self.assertEqual(len(items), 3)
1794
1795 self.assertEqual(items[0].change.number, '1')
1796 self.assertEqual(items[0].change.patchset, '1')
1797 self.assertFalse(items[0].live)
1798
1799 self.assertEqual(items[1].change.number, '2')
1800 self.assertEqual(items[1].change.patchset, '1')
1801 self.assertTrue(items[1].live)
1802
1803 self.assertEqual(items[2].change.number, '1')
1804 self.assertEqual(items[2].change.patchset, '2')
1805 self.assertTrue(items[2].live)
1806
1807 # Add a new patchset to B
1808 B.addPatchset()
1809 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1810 self.waitUntilSettled()
1811
1812 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1813 # but we should have a new B,2 (still based on A,1)
1814 items = check_pipeline.getAllItems()
1815 self.assertEqual(len(items), 3)
1816
1817 self.assertEqual(items[0].change.number, '1')
1818 self.assertEqual(items[0].change.patchset, '2')
1819 self.assertTrue(items[0].live)
1820
1821 self.assertEqual(items[1].change.number, '1')
1822 self.assertEqual(items[1].change.patchset, '1')
1823 self.assertFalse(items[1].live)
1824
1825 self.assertEqual(items[2].change.number, '2')
1826 self.assertEqual(items[2].change.patchset, '2')
1827 self.assertTrue(items[2].live)
1828
1829 self.builds[0].release()
1830 self.waitUntilSettled()
1831 self.builds[0].release()
1832 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001833 self.executor_server.hold_jobs_in_build = False
1834 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001835 self.waitUntilSettled()
1836
1837 self.assertEqual(A.reported, 1)
1838 self.assertEqual(B.reported, 1)
1839 self.assertEqual(self.history[0].result, 'ABORTED')
1840 self.assertEqual(self.history[0].changes, '1,1')
1841 self.assertEqual(self.history[1].result, 'ABORTED')
1842 self.assertEqual(self.history[1].changes, '1,1 2,1')
1843 self.assertEqual(self.history[2].result, 'SUCCESS')
1844 self.assertEqual(self.history[2].changes, '1,2')
1845 self.assertEqual(self.history[3].result, 'SUCCESS')
1846 self.assertEqual(self.history[3].changes, '1,1 2,2')
1847
1848 def test_abandoned_gate(self):
1849 "Test that an abandoned change is dequeued from gate"
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')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001854 A.addApproval('Code-Review', 2)
1855 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001856 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001857 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1858 self.assertEqual(self.builds[0].name, 'project-merge')
1859
1860 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1861 self.waitUntilSettled()
1862
Paul Belanger174a8272017-03-14 13:20:10 -04001863 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001864 self.waitUntilSettled()
1865
Arie5b4048c2016-10-05 16:12:06 +03001866 self.assertBuilds([])
1867 self.assertHistory([
1868 dict(name='project-merge', result='ABORTED', changes='1,1')],
1869 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001870 self.assertEqual(A.reported, 1,
1871 "Abandoned gate change should report only start")
1872
1873 def test_abandoned_check(self):
1874 "Test that an abandoned change is dequeued from check"
1875
Paul Belanger174a8272017-03-14 13:20:10 -04001876 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001877
1878 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1879 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001880 tenant = self.sched.abide.tenants.get('tenant-one')
1881 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001882
1883 # Add two git-dependent changes
1884 B.setDependsOn(A, 1)
1885 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1886 self.waitUntilSettled()
1887 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1888 self.waitUntilSettled()
1889 # A live item, and a non-live/live pair
1890 items = check_pipeline.getAllItems()
1891 self.assertEqual(len(items), 3)
1892
1893 self.assertEqual(items[0].change.number, '1')
1894 self.assertFalse(items[0].live)
1895
1896 self.assertEqual(items[1].change.number, '2')
1897 self.assertTrue(items[1].live)
1898
1899 self.assertEqual(items[2].change.number, '1')
1900 self.assertTrue(items[2].live)
1901
1902 # Abandon A
1903 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1904 self.waitUntilSettled()
1905
1906 # The live copy of A should be gone, but the non-live and B
1907 # should continue
1908 items = check_pipeline.getAllItems()
1909 self.assertEqual(len(items), 2)
1910
1911 self.assertEqual(items[0].change.number, '1')
1912 self.assertFalse(items[0].live)
1913
1914 self.assertEqual(items[1].change.number, '2')
1915 self.assertTrue(items[1].live)
1916
Paul Belanger174a8272017-03-14 13:20:10 -04001917 self.executor_server.hold_jobs_in_build = False
1918 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001919 self.waitUntilSettled()
1920
1921 self.assertEqual(len(self.history), 4)
1922 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001923 'Build should have been aborted')
1924 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001925 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001926
Steve Varnau7b78b312015-04-03 14:49:46 -07001927 def test_abandoned_not_timer(self):
1928 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001929 # This test can not use simple_layout because it must start
1930 # with a configuration which does not include a
1931 # timer-triggered job so that we have an opportunity to set
1932 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001933 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001934 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001935 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001936 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001937 # The pipeline triggers every second, so we should have seen
1938 # several by now.
1939 time.sleep(5)
1940 self.waitUntilSettled()
1941 # Stop queuing timer triggered jobs so that the assertions
1942 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001943 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001944 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001945 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001946 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001947 # If APScheduler is in mid-event when we remove the job, we
1948 # can end up with one more event firing, so give it an extra
1949 # second to settle.
1950 time.sleep(1)
1951 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001952
James E. Blair21037782017-07-19 11:56:55 -07001953 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001954
1955 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1956 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1957 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001958 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001959
1960 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1961 self.waitUntilSettled()
1962
James E. Blair21037782017-07-19 11:56:55 -07001963 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001964
Paul Belanger174a8272017-03-14 13:20:10 -04001965 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001966 self.waitUntilSettled()
1967
James E. Blair2fa50962013-01-30 21:50:41 -08001968 def test_new_patchset_dequeues_old_on_head(self):
1969 "Test that a new patchset causes the old to be dequeued (at head)"
1970 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001971 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001972 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1973 M.setMerged()
1974 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1975 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1976 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1977 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001978 A.addApproval('Code-Review', 2)
1979 B.addApproval('Code-Review', 2)
1980 C.addApproval('Code-Review', 2)
1981 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001982
1983 C.setDependsOn(B, 1)
1984 B.setDependsOn(A, 1)
1985 A.setDependsOn(M, 1)
1986
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001987 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1988 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1989 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1990 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001991 self.waitUntilSettled()
1992
1993 A.addPatchset()
1994 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1995 self.waitUntilSettled()
1996
Paul Belanger174a8272017-03-14 13:20:10 -04001997 self.executor_server.hold_jobs_in_build = False
1998 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001999 self.waitUntilSettled()
2000
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002001 self.assertEqual(A.data['status'], 'NEW')
2002 self.assertEqual(A.reported, 2)
2003 self.assertEqual(B.data['status'], 'NEW')
2004 self.assertEqual(B.reported, 2)
2005 self.assertEqual(C.data['status'], 'NEW')
2006 self.assertEqual(C.reported, 2)
2007 self.assertEqual(D.data['status'], 'MERGED')
2008 self.assertEqual(D.reported, 2)
2009 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08002010
2011 def test_new_patchset_dequeues_old_without_dependents(self):
2012 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04002013 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002014 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2015 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2016 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002017 A.addApproval('Code-Review', 2)
2018 B.addApproval('Code-Review', 2)
2019 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08002020
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002021 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2022 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2023 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08002024 self.waitUntilSettled()
2025
2026 B.addPatchset()
2027 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2028 self.waitUntilSettled()
2029
Paul Belanger174a8272017-03-14 13:20:10 -04002030 self.executor_server.hold_jobs_in_build = False
2031 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002032 self.waitUntilSettled()
2033
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002034 self.assertEqual(A.data['status'], 'MERGED')
2035 self.assertEqual(A.reported, 2)
2036 self.assertEqual(B.data['status'], 'NEW')
2037 self.assertEqual(B.reported, 2)
2038 self.assertEqual(C.data['status'], 'MERGED')
2039 self.assertEqual(C.reported, 2)
2040 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002041
2042 def test_new_patchset_dequeues_old_independent_queue(self):
2043 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002044 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002045 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2046 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2047 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2048 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2049 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2050 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2051 self.waitUntilSettled()
2052
2053 B.addPatchset()
2054 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2055 self.waitUntilSettled()
2056
Paul Belanger174a8272017-03-14 13:20:10 -04002057 self.executor_server.hold_jobs_in_build = False
2058 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002059 self.waitUntilSettled()
2060
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002061 self.assertEqual(A.data['status'], 'NEW')
2062 self.assertEqual(A.reported, 1)
2063 self.assertEqual(B.data['status'], 'NEW')
2064 self.assertEqual(B.reported, 1)
2065 self.assertEqual(C.data['status'], 'NEW')
2066 self.assertEqual(C.reported, 1)
2067 self.assertEqual(len(self.history), 10)
2068 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002069
James E. Blaircc020532017-04-19 13:04:44 -07002070 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002071 def test_noop_job(self):
2072 "Test that the internal noop job works"
2073 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002074 A.addApproval('Code-Review', 2)
2075 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002076 self.waitUntilSettled()
2077
2078 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2079 self.assertTrue(self.sched._areAllBuildsComplete())
2080 self.assertEqual(len(self.history), 0)
2081 self.assertEqual(A.data['status'], 'MERGED')
2082 self.assertEqual(A.reported, 2)
2083
James E. Blair06cc3922017-04-19 10:08:10 -07002084 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002085 def test_no_job_project(self):
2086 "Test that reports with no jobs don't get sent"
2087 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2088 'master', 'A')
2089 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2090 self.waitUntilSettled()
2091
2092 # Change wasn't reported to
2093 self.assertEqual(A.reported, False)
2094
2095 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002096 tenant = self.sched.abide.tenants.get('tenant-one')
2097 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002098 items = check_pipeline.getAllItems()
2099 self.assertEqual(len(items), 0)
2100
2101 self.assertEqual(len(self.history), 0)
2102
James E. Blair7d0dedc2013-02-21 17:26:09 -08002103 def test_zuul_refs(self):
2104 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002105 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002106 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2107 M1.setMerged()
2108 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2109 M2.setMerged()
2110
2111 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2112 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2113 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2114 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002115 A.addApproval('Code-Review', 2)
2116 B.addApproval('Code-Review', 2)
2117 C.addApproval('Code-Review', 2)
2118 D.addApproval('Code-Review', 2)
2119 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2120 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2121 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2122 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002123
2124 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002125 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002126 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002127 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002128 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002129 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002130 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002131 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002132 self.waitUntilSettled()
2133
Clint Byrum3343e3e2016-11-15 16:05:03 -08002134 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002135 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002136 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002137 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002138 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002139 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002140 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002141 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002142 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002143 d_build = x
2144 if a_build and b_build and c_build and d_build:
2145 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002146
Clint Byrum3343e3e2016-11-15 16:05:03 -08002147 # should have a, not b, and should not be in project2
2148 self.assertTrue(a_build.hasChanges(A))
2149 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002150
Clint Byrum3343e3e2016-11-15 16:05:03 -08002151 # should have a and b, and should not be in project2
2152 self.assertTrue(b_build.hasChanges(A, B))
2153 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002154
Clint Byrum3343e3e2016-11-15 16:05:03 -08002155 # should have a and b in 1, c in 2
2156 self.assertTrue(c_build.hasChanges(A, B, C))
2157 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002158
Clint Byrum3343e3e2016-11-15 16:05:03 -08002159 # should have a and b in 1, c and d in 2
2160 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002161
Paul Belanger174a8272017-03-14 13:20:10 -04002162 self.executor_server.hold_jobs_in_build = False
2163 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002164 self.waitUntilSettled()
2165
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002166 self.assertEqual(A.data['status'], 'MERGED')
2167 self.assertEqual(A.reported, 2)
2168 self.assertEqual(B.data['status'], 'MERGED')
2169 self.assertEqual(B.reported, 2)
2170 self.assertEqual(C.data['status'], 'MERGED')
2171 self.assertEqual(C.reported, 2)
2172 self.assertEqual(D.data['status'], 'MERGED')
2173 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002174
James E. Blair4a28a882013-08-23 15:17:33 -07002175 def test_rerun_on_error(self):
2176 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002177 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002178 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002179 A.addApproval('Code-Review', 2)
2180 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002181 self.waitUntilSettled()
2182
James E. Blair412fba82017-01-26 15:00:50 -08002183 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002184 self.executor_server.hold_jobs_in_build = False
2185 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002186 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002187 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002188 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2189
James E. Blair412e5582013-04-22 15:50:12 -07002190 def test_statsd(self):
2191 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002192 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002193 statsd.incr('test-incr')
2194 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002195 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002196 self.assertReportedStat('test-incr', '1|c')
2197 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002198 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002199
James E. Blairdad52252014-02-07 16:59:17 -08002200 def test_stuck_job_cleanup(self):
2201 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002202
2203 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002204 self.gearman_server.hold_jobs_in_queue = True
2205 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002206 A.addApproval('Code-Review', 2)
2207 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002208 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002209 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002210 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2211
James E. Blair31cb3e82017-04-20 13:08:56 -07002212 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002213 self.sched.reconfigure(self.config)
2214 self.waitUntilSettled()
2215
James E. Blair18c64442014-03-18 10:14:45 -07002216 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002217 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002218 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002219 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2220 self.assertTrue(self.sched._areAllBuildsComplete())
2221
2222 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002223 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002224 self.assertEqual(self.history[0].result, 'SUCCESS')
2225
James E. Blair879dafb2015-07-17 14:04:49 -07002226 def test_file_head(self):
2227 # This is a regression test for an observed bug. A change
2228 # with a file named "HEAD" in the root directory of the repo
2229 # was processed by a merger. It then was unable to reset the
2230 # repo because of:
2231 # GitCommandError: 'git reset --hard HEAD' returned
2232 # with exit code 128
2233 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2234 # and filename
2235 # Use '--' to separate filenames from revisions'
2236
2237 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002238 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002239 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2240
2241 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2242 self.waitUntilSettled()
2243
2244 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2245 self.waitUntilSettled()
2246
2247 self.assertIn('Build succeeded', A.messages[0])
2248 self.assertIn('Build succeeded', B.messages[0])
2249
James E. Blair70c71582013-03-06 08:50:50 -08002250 def test_file_jobs(self):
2251 "Test that file jobs run only when appropriate"
2252 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002253 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002254 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002255 A.addApproval('Code-Review', 2)
2256 B.addApproval('Code-Review', 2)
2257 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2258 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002259 self.waitUntilSettled()
2260
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002261 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002262 if x.name == 'project-testfile']
2263
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002264 self.assertEqual(len(testfile_jobs), 1)
2265 self.assertEqual(testfile_jobs[0].changes, '1,2')
2266 self.assertEqual(A.data['status'], 'MERGED')
2267 self.assertEqual(A.reported, 2)
2268 self.assertEqual(B.data['status'], 'MERGED')
2269 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002270
Clint Byrum3ee39f32016-11-17 23:45:07 -08002271 def _test_irrelevant_files_jobs(self, should_skip):
2272 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002273 if should_skip:
2274 files = {'ignoreme': 'ignored\n'}
2275 else:
2276 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002277
2278 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002279 'master',
2280 'test irrelevant-files',
2281 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002282 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2283 self.waitUntilSettled()
2284
2285 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002286 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002287
2288 if should_skip:
2289 self.assertEqual([], tested_change_ids)
2290 else:
2291 self.assertIn(change.data['number'], tested_change_ids)
2292
James E. Blaircc324192017-04-19 14:36:08 -07002293 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002294 def test_irrelevant_files_match_skips_job(self):
2295 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002296
James E. Blaircc324192017-04-19 14:36:08 -07002297 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002298 def test_irrelevant_files_no_match_runs_job(self):
2299 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002300
James E. Blaircc324192017-04-19 14:36:08 -07002301 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002302 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002303 files = {'ignoreme': 'ignored\n'}
2304
2305 change = self.fake_gerrit.addFakeChange('org/project',
2306 'master',
2307 'test irrelevant-files',
2308 files=files)
2309 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2310 self.waitUntilSettled()
2311
2312 run_jobs = set([build.name for build in self.history])
2313
2314 self.assertEqual(set(['project-test-nomatch-starts-empty',
2315 'project-test-nomatch-starts-full']), run_jobs)
2316
James E. Blairaf8b2082017-10-03 15:38:27 -07002317 @simple_layout('layouts/job-vars.yaml')
2318 def test_inherited_job_variables(self):
2319 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2320 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2321 self.waitUntilSettled()
2322 self.assertHistory([
2323 dict(name='parentjob', result='SUCCESS'),
2324 dict(name='child1', result='SUCCESS'),
2325 dict(name='child2', result='SUCCESS'),
2326 dict(name='child3', result='SUCCESS'),
2327 ], ordered=False)
2328 j = self.getJobFromHistory('parentjob')
2329 rp = set([p['name'] for p in j.parameters['projects']])
2330 self.assertEqual(j.parameters['vars']['override'], 0)
2331 self.assertEqual(j.parameters['vars']['child1override'], 0)
2332 self.assertEqual(j.parameters['vars']['parent'], 0)
2333 self.assertFalse('child1' in j.parameters['vars'])
2334 self.assertFalse('child2' in j.parameters['vars'])
2335 self.assertFalse('child3' in j.parameters['vars'])
2336 self.assertEqual(rp, set(['org/project', 'org/project0',
2337 'org/project0']))
2338 j = self.getJobFromHistory('child1')
2339 rp = set([p['name'] for p in j.parameters['projects']])
2340 self.assertEqual(j.parameters['vars']['override'], 1)
2341 self.assertEqual(j.parameters['vars']['child1override'], 1)
2342 self.assertEqual(j.parameters['vars']['parent'], 0)
2343 self.assertEqual(j.parameters['vars']['child1'], 1)
2344 self.assertFalse('child2' in j.parameters['vars'])
2345 self.assertFalse('child3' in j.parameters['vars'])
2346 self.assertEqual(rp, set(['org/project', 'org/project0',
2347 'org/project1']))
2348 j = self.getJobFromHistory('child2')
2349 rp = set([p['name'] for p in j.parameters['projects']])
2350 self.assertEqual(j.parameters['vars']['override'], 2)
2351 self.assertEqual(j.parameters['vars']['child1override'], 0)
2352 self.assertEqual(j.parameters['vars']['parent'], 0)
2353 self.assertFalse('child1' in j.parameters['vars'])
2354 self.assertEqual(j.parameters['vars']['child2'], 2)
2355 self.assertFalse('child3' in j.parameters['vars'])
2356 self.assertEqual(rp, set(['org/project', 'org/project0',
2357 'org/project2']))
2358 j = self.getJobFromHistory('child3')
2359 rp = set([p['name'] for p in j.parameters['projects']])
2360 self.assertEqual(j.parameters['vars']['override'], 3)
2361 self.assertEqual(j.parameters['vars']['child1override'], 0)
2362 self.assertEqual(j.parameters['vars']['parent'], 0)
2363 self.assertFalse('child1' in j.parameters['vars'])
2364 self.assertFalse('child2' in j.parameters['vars'])
2365 self.assertEqual(j.parameters['vars']['child3'], 3)
2366 self.assertEqual(rp, set(['org/project', 'org/project0',
2367 'org/project3']))
2368
James E. Blairc8a1e052014-02-25 09:29:26 -08002369 def test_queue_names(self):
2370 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002371 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002372 (trusted, project1) = tenant.getProject('org/project1')
2373 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002374 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2375 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2376 self.assertEqual(q1.name, 'integrated')
2377 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002378
James E. Blair64ed6f22013-07-10 14:07:23 -07002379 def test_queue_precedence(self):
2380 "Test that queue precedence works"
2381
2382 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002383 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002384 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2385 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002386 A.addApproval('Code-Review', 2)
2387 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002388
2389 self.waitUntilSettled()
2390 self.gearman_server.hold_jobs_in_queue = False
2391 self.gearman_server.release()
2392 self.waitUntilSettled()
2393
James E. Blair8de58bd2013-07-18 16:23:33 -07002394 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002395 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002396 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002397 self.waitUntilSettled()
2398
James E. Blair64ed6f22013-07-10 14:07:23 -07002399 self.log.debug(self.history)
2400 self.assertEqual(self.history[0].pipeline, 'gate')
2401 self.assertEqual(self.history[1].pipeline, 'check')
2402 self.assertEqual(self.history[2].pipeline, 'gate')
2403 self.assertEqual(self.history[3].pipeline, 'gate')
2404 self.assertEqual(self.history[4].pipeline, 'check')
2405 self.assertEqual(self.history[5].pipeline, 'check')
2406
Clark Boylana5edbe42014-06-03 16:39:10 -07002407 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002408 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002409 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002410 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002411 A.addApproval('Code-Review', 2)
2412 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002413 self.waitUntilSettled()
2414
Paul Belanger174a8272017-03-14 13:20:10 -04002415 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002416 self.waitUntilSettled()
2417
James E. Blair1843a552013-07-03 14:19:52 -07002418 port = self.webapp.server.socket.getsockname()[1]
2419
Adam Gandelman77a12c72017-02-03 17:43:43 -08002420 req = urllib.request.Request(
2421 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002422 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002423 headers = f.info()
2424 self.assertIn('Content-Length', headers)
2425 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002426 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2427 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002428 self.assertIn('Access-Control-Allow-Origin', headers)
2429 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002430 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002431 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002432 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002433
Paul Belanger174a8272017-03-14 13:20:10 -04002434 self.executor_server.hold_jobs_in_build = False
2435 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002436 self.waitUntilSettled()
2437
2438 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002439 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002440 for p in data['pipelines']:
2441 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002442 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002443 self.assertEqual(q['window'], 20)
2444 else:
2445 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002446 for head in q['heads']:
2447 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002448 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002449 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002450 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002451 status_jobs.append(job)
2452 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002453 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002454 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002455 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2456 uuid=status_jobs[0]['uuid']),
2457 status_jobs[0]['url'])
2458 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002459 'finger://{hostname}/{uuid}'.format(
2460 hostname=self.executor_server.hostname,
2461 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002462 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002463 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002464 self.assertEqual(
2465 'finger://{hostname}/{uuid}'.format(
2466 hostname=self.executor_server.hostname,
2467 uuid=status_jobs[0]['uuid']),
2468 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002469 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002470 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002471 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2472 uuid=status_jobs[1]['uuid']),
2473 status_jobs[1]['url'])
2474 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002475 'finger://{hostname}/{uuid}'.format(
2476 hostname=self.executor_server.hostname,
2477 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002478 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002479 self.assertEqual(
2480 'finger://{hostname}/{uuid}'.format(
2481 hostname=self.executor_server.hostname,
2482 uuid=status_jobs[1]['uuid']),
2483 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002484
2485 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002486 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002487 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2488 uuid=status_jobs[2]['uuid']),
2489 status_jobs[2]['url'])
2490 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002491 'finger://{hostname}/{uuid}'.format(
2492 hostname=self.executor_server.hostname,
2493 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002494 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002495 self.assertEqual(
2496 'finger://{hostname}/{uuid}'.format(
2497 hostname=self.executor_server.hostname,
2498 uuid=status_jobs[2]['uuid']),
2499 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002500
Tobias Henkel65639f82017-07-10 10:25:42 +02002501 # check job dependencies
2502 self.assertIsNotNone(status_jobs[0]['dependencies'])
2503 self.assertIsNotNone(status_jobs[1]['dependencies'])
2504 self.assertIsNotNone(status_jobs[2]['dependencies'])
2505 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2506 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2507 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2508 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2509 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2510
James E. Blaircdccd972013-07-01 12:10:22 -07002511 def test_live_reconfiguration(self):
2512 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002513 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002515 A.addApproval('Code-Review', 2)
2516 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002517 self.waitUntilSettled()
2518
2519 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002520 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002521
Paul Belanger174a8272017-03-14 13:20:10 -04002522 self.executor_server.hold_jobs_in_build = False
2523 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002524 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002525 self.assertEqual(self.getJobFromHistory('project-merge').result,
2526 'SUCCESS')
2527 self.assertEqual(self.getJobFromHistory('project-test1').result,
2528 'SUCCESS')
2529 self.assertEqual(self.getJobFromHistory('project-test2').result,
2530 'SUCCESS')
2531 self.assertEqual(A.data['status'], 'MERGED')
2532 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002533
James E. Blair027ba992017-09-20 13:48:32 -07002534 def test_live_reconfiguration_abort(self):
2535 # Raise an exception during reconfiguration and verify we
2536 # still function.
2537 self.executor_server.hold_jobs_in_build = True
2538 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2539 A.addApproval('Code-Review', 2)
2540 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2541 self.waitUntilSettled()
2542
2543 tenant = self.sched.abide.tenants.get('tenant-one')
2544 pipeline = tenant.layout.pipelines['gate']
2545 change = pipeline.getAllItems()[0].change
2546 # Set this to an invalid value to cause an exception during
2547 # reconfiguration.
2548 change.branch = None
2549
2550 self.sched.reconfigure(self.config)
2551 self.waitUntilSettled()
2552
2553 self.executor_server.hold_jobs_in_build = False
2554 self.executor_server.release()
2555
2556 self.waitUntilSettled()
2557 self.assertEqual(self.getJobFromHistory('project-merge').result,
2558 'ABORTED')
2559 self.assertEqual(A.data['status'], 'NEW')
2560 # The final report fails because of the invalid value set above.
2561 self.assertEqual(A.reported, 1)
2562
James E. Blair6bc782d2015-07-17 16:20:21 -07002563 def test_live_reconfiguration_merge_conflict(self):
2564 # A real-world bug: a change in a gate queue has a merge
2565 # conflict and a job is added to its project while it's
2566 # sitting in the queue. The job gets added to the change and
2567 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002568 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002569
2570 # This change is fine. It's here to stop the queue long
2571 # enough for the next change to be subject to the
2572 # reconfiguration, as well as to provide a conflict for the
2573 # next change. This change will succeed and merge.
2574 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002575 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002576 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002577
2578 # This change will be in merge conflict. During the
2579 # reconfiguration, we will add a job. We want to make sure
2580 # that doesn't cause it to get stuck.
2581 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002582 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002583 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002584
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002585 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2586 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002587
2588 self.waitUntilSettled()
2589
2590 # No jobs have run yet
2591 self.assertEqual(A.data['status'], 'NEW')
2592 self.assertEqual(A.reported, 1)
2593 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002594 self.assertEqual(len(self.history), 0)
2595
2596 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002597 self.commitConfigUpdate('common-config',
2598 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002599 self.sched.reconfigure(self.config)
2600 self.waitUntilSettled()
2601
Paul Belanger174a8272017-03-14 13:20:10 -04002602 self.executor_server.hold_jobs_in_build = False
2603 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002604 self.waitUntilSettled()
2605
2606 self.assertEqual(A.data['status'], 'MERGED')
2607 self.assertEqual(A.reported, 2)
2608 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002609 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002610 self.assertEqual(self.getJobFromHistory('project-merge').result,
2611 'SUCCESS')
2612 self.assertEqual(self.getJobFromHistory('project-test1').result,
2613 'SUCCESS')
2614 self.assertEqual(self.getJobFromHistory('project-test2').result,
2615 'SUCCESS')
2616 self.assertEqual(self.getJobFromHistory('project-test3').result,
2617 'SUCCESS')
2618 self.assertEqual(len(self.history), 4)
2619
James E. Blair400e8fd2015-07-30 17:44:45 -07002620 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002621 # An extrapolation of test_live_reconfiguration_merge_conflict
2622 # that tests a job added to a job tree with a failed root does
2623 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002624 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002625
2626 # This change is fine. It's here to stop the queue long
2627 # enough for the next change to be subject to the
2628 # reconfiguration. This change will succeed and merge.
2629 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002630 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002631 A.addApproval('Code-Review', 2)
2632 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002633 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002634 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002635 self.waitUntilSettled()
2636
2637 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002638 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002639 B.addApproval('Code-Review', 2)
2640 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002641 self.waitUntilSettled()
2642
Paul Belanger174a8272017-03-14 13:20:10 -04002643 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002644 self.waitUntilSettled()
2645
2646 # Both -merge jobs have run, but no others.
2647 self.assertEqual(A.data['status'], 'NEW')
2648 self.assertEqual(A.reported, 1)
2649 self.assertEqual(B.data['status'], 'NEW')
2650 self.assertEqual(B.reported, 1)
2651 self.assertEqual(self.history[0].result, 'SUCCESS')
2652 self.assertEqual(self.history[0].name, 'project-merge')
2653 self.assertEqual(self.history[1].result, 'FAILURE')
2654 self.assertEqual(self.history[1].name, 'project-merge')
2655 self.assertEqual(len(self.history), 2)
2656
2657 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002658 self.commitConfigUpdate('common-config',
2659 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002660 self.sched.reconfigure(self.config)
2661 self.waitUntilSettled()
2662
Paul Belanger174a8272017-03-14 13:20:10 -04002663 self.executor_server.hold_jobs_in_build = False
2664 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002665 self.waitUntilSettled()
2666
2667 self.assertEqual(A.data['status'], 'MERGED')
2668 self.assertEqual(A.reported, 2)
2669 self.assertEqual(B.data['status'], 'NEW')
2670 self.assertEqual(B.reported, 2)
2671 self.assertEqual(self.history[0].result, 'SUCCESS')
2672 self.assertEqual(self.history[0].name, 'project-merge')
2673 self.assertEqual(self.history[1].result, 'FAILURE')
2674 self.assertEqual(self.history[1].name, 'project-merge')
2675 self.assertEqual(self.history[2].result, 'SUCCESS')
2676 self.assertEqual(self.history[3].result, 'SUCCESS')
2677 self.assertEqual(self.history[4].result, 'SUCCESS')
2678 self.assertEqual(len(self.history), 5)
2679
James E. Blair400e8fd2015-07-30 17:44:45 -07002680 def test_live_reconfiguration_failed_job(self):
2681 # Test that a change with a removed failing job does not
2682 # disrupt reconfiguration. If a change has a failed job and
2683 # that job is removed during a reconfiguration, we observed a
2684 # bug where the code to re-set build statuses would run on
2685 # that build and raise an exception because the job no longer
2686 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002687 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002688
2689 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2690
2691 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002692 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002693
2694 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2695 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002696 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002697 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002698 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002699 self.waitUntilSettled()
2700
2701 self.assertEqual(A.data['status'], 'NEW')
2702 self.assertEqual(A.reported, 0)
2703
2704 self.assertEqual(self.getJobFromHistory('project-merge').result,
2705 'SUCCESS')
2706 self.assertEqual(self.getJobFromHistory('project-test1').result,
2707 'FAILURE')
2708 self.assertEqual(len(self.history), 2)
2709
2710 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002711 self.commitConfigUpdate('common-config',
2712 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002713 self.sched.reconfigure(self.config)
2714 self.waitUntilSettled()
2715
Paul Belanger174a8272017-03-14 13:20:10 -04002716 self.executor_server.hold_jobs_in_build = False
2717 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002718 self.waitUntilSettled()
2719
2720 self.assertEqual(self.getJobFromHistory('project-test2').result,
2721 'SUCCESS')
2722 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2723 'SUCCESS')
2724 self.assertEqual(len(self.history), 4)
2725
2726 self.assertEqual(A.data['status'], 'NEW')
2727 self.assertEqual(A.reported, 1)
2728 self.assertIn('Build succeeded', A.messages[0])
2729 # Ensure the removed job was not included in the report.
2730 self.assertNotIn('project-test1', A.messages[0])
2731
James E. Blairfe707d12015-08-05 15:18:15 -07002732 def test_live_reconfiguration_shared_queue(self):
2733 # Test that a change with a failing job which was removed from
2734 # this project but otherwise still exists in the system does
2735 # not disrupt reconfiguration.
2736
Paul Belanger174a8272017-03-14 13:20:10 -04002737 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002738
2739 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2740
Paul Belanger174a8272017-03-14 13:20:10 -04002741 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002742
2743 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2744 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002745 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002746 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002747 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002748 self.waitUntilSettled()
2749
2750 self.assertEqual(A.data['status'], 'NEW')
2751 self.assertEqual(A.reported, 0)
2752
Clint Byrum69663d42017-05-01 14:51:04 -07002753 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002754 'SUCCESS')
2755 self.assertEqual(self.getJobFromHistory(
2756 'project1-project2-integration').result, 'FAILURE')
2757 self.assertEqual(len(self.history), 2)
2758
2759 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002760 self.commitConfigUpdate(
2761 'common-config',
2762 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002763 self.sched.reconfigure(self.config)
2764 self.waitUntilSettled()
2765
Paul Belanger174a8272017-03-14 13:20:10 -04002766 self.executor_server.hold_jobs_in_build = False
2767 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002768 self.waitUntilSettled()
2769
Clint Byrum69663d42017-05-01 14:51:04 -07002770 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002771 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002772 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002773 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002774 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002775 'SUCCESS')
2776 self.assertEqual(self.getJobFromHistory(
2777 'project1-project2-integration').result, 'FAILURE')
2778 self.assertEqual(len(self.history), 4)
2779
2780 self.assertEqual(A.data['status'], 'NEW')
2781 self.assertEqual(A.reported, 1)
2782 self.assertIn('Build succeeded', A.messages[0])
2783 # Ensure the removed job was not included in the report.
2784 self.assertNotIn('project1-project2-integration', A.messages[0])
2785
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002786 def test_double_live_reconfiguration_shared_queue(self):
2787 # This was a real-world regression. A change is added to
2788 # gate; a reconfigure happens, a second change which depends
2789 # on the first is added, and a second reconfiguration happens.
2790 # Ensure that both changes merge.
2791
2792 # A failure may indicate incorrect caching or cleaning up of
2793 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002794 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002795
2796 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2797 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2798 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002799 A.addApproval('Code-Review', 2)
2800 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002801
2802 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002803 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002804 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002805 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002806 self.waitUntilSettled()
2807
2808 # Reconfigure (with only one change in the pipeline).
2809 self.sched.reconfigure(self.config)
2810 self.waitUntilSettled()
2811
2812 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002813 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002814 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002815 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002816 self.waitUntilSettled()
2817
2818 # Reconfigure (with both in the pipeline).
2819 self.sched.reconfigure(self.config)
2820 self.waitUntilSettled()
2821
Paul Belanger174a8272017-03-14 13:20:10 -04002822 self.executor_server.hold_jobs_in_build = False
2823 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002824 self.waitUntilSettled()
2825
2826 self.assertEqual(len(self.history), 8)
2827
2828 self.assertEqual(A.data['status'], 'MERGED')
2829 self.assertEqual(A.reported, 2)
2830 self.assertEqual(B.data['status'], 'MERGED')
2831 self.assertEqual(B.reported, 2)
2832
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002833 def test_live_reconfiguration_del_project(self):
2834 # Test project deletion from layout
2835 # while changes are enqueued
2836
Paul Belanger174a8272017-03-14 13:20:10 -04002837 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2839 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2840 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2841
2842 # A Depends-On: B
2843 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2844 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002845 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002846
2847 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2848 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2849 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002850 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002851 self.waitUntilSettled()
2852 self.assertEqual(len(self.builds), 5)
2853
2854 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002855 self.commitConfigUpdate(
2856 'common-config',
2857 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002858 self.sched.reconfigure(self.config)
2859 self.waitUntilSettled()
2860
2861 # Builds for C aborted, builds for A succeed,
2862 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002863 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002864 self.assertEqual(job_c.changes, '3,1')
2865 self.assertEqual(job_c.result, 'ABORTED')
2866
Paul Belanger174a8272017-03-14 13:20:10 -04002867 self.executor_server.hold_jobs_in_build = False
2868 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002869 self.waitUntilSettled()
2870
Clint Byrum0149ada2017-04-04 12:22:01 -07002871 self.assertEqual(
2872 self.getJobFromHistory('project-test1', 'org/project').changes,
2873 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002874
2875 self.assertEqual(A.data['status'], 'NEW')
2876 self.assertEqual(B.data['status'], 'NEW')
2877 self.assertEqual(C.data['status'], 'NEW')
2878 self.assertEqual(A.reported, 1)
2879 self.assertEqual(B.reported, 0)
2880 self.assertEqual(C.reported, 0)
2881
Clint Byrum0149ada2017-04-04 12:22:01 -07002882 tenant = self.sched.abide.tenants.get('tenant-one')
2883 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002884 self.assertIn('Build succeeded', A.messages[0])
2885
James E. Blair287c06d2013-07-24 10:39:30 -07002886 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002887 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002888 files = {'README': ''}
2889 self.addCommitToRepo("org/new-project", 'Initial commit',
2890 files=files, tag='init')
2891 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2892 self.commitConfigUpdate(
2893 'common-config',
2894 'layouts/delayed-repo-init.yaml')
2895 self.sched.reconfigure(self.config)
2896 self.waitUntilSettled()
2897
James E. Blair287c06d2013-07-24 10:39:30 -07002898 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2899
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002900 A.addApproval('Code-Review', 2)
2901 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002902 self.waitUntilSettled()
2903 self.assertEqual(self.getJobFromHistory('project-merge').result,
2904 'SUCCESS')
2905 self.assertEqual(self.getJobFromHistory('project-test1').result,
2906 'SUCCESS')
2907 self.assertEqual(self.getJobFromHistory('project-test2').result,
2908 'SUCCESS')
2909 self.assertEqual(A.data['status'], 'MERGED')
2910 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002911
James E. Blair9ea70072017-04-19 16:05:30 -07002912 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002913 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002914 self.init_repo("org/delete-project")
2915 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2916
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002917 A.addApproval('Code-Review', 2)
2918 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002919 self.waitUntilSettled()
2920 self.assertEqual(self.getJobFromHistory('project-merge').result,
2921 'SUCCESS')
2922 self.assertEqual(self.getJobFromHistory('project-test1').result,
2923 'SUCCESS')
2924 self.assertEqual(self.getJobFromHistory('project-test2').result,
2925 'SUCCESS')
2926 self.assertEqual(A.data['status'], 'MERGED')
2927 self.assertEqual(A.reported, 2)
2928
2929 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002930 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002931 if os.path.exists(os.path.join(self.merger_src_root, p)):
2932 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002933 if os.path.exists(os.path.join(self.executor_src_root, p)):
2934 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002935
2936 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2937
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002938 B.addApproval('Code-Review', 2)
2939 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002940 self.waitUntilSettled()
2941 self.assertEqual(self.getJobFromHistory('project-merge').result,
2942 'SUCCESS')
2943 self.assertEqual(self.getJobFromHistory('project-test1').result,
2944 'SUCCESS')
2945 self.assertEqual(self.getJobFromHistory('project-test2').result,
2946 'SUCCESS')
2947 self.assertEqual(B.data['status'], 'MERGED')
2948 self.assertEqual(B.reported, 2)
2949
James E. Blair892cca62017-08-09 11:36:58 -07002950 @simple_layout('layouts/untrusted-secrets.yaml')
2951 def test_untrusted_secrets(self):
2952 "Test untrusted secrets"
2953 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2954 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2955 self.waitUntilSettled()
2956
2957 self.assertHistory([])
2958 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002959 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002960 A.messages[0])
2961
James E. Blaire68d43f2017-04-19 14:03:30 -07002962 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002963 def test_tags(self):
2964 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002965 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2966 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2967 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2968 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2969 self.waitUntilSettled()
2970
James E. Blaire68d43f2017-04-19 14:03:30 -07002971 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002972
2973 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002974 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002975 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002976 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002977
2978 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002979 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002980 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002981
James E. Blair63bb0ef2013-07-29 17:14:51 -07002982 def test_timer(self):
2983 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002984 # This test can not use simple_layout because it must start
2985 # with a configuration which does not include a
2986 # timer-triggered job so that we have an opportunity to set
2987 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002988 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002989 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002990 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002991 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002992
Clark Boylan3ee090a2014-04-03 20:55:09 -07002993 # The pipeline triggers every second, so we should have seen
2994 # several by now.
2995 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002996 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002997
2998 self.assertEqual(len(self.builds), 2)
2999
James E. Blair63bb0ef2013-07-29 17:14:51 -07003000 port = self.webapp.server.socket.getsockname()[1]
3001
Clint Byrum5e729e92017-02-08 06:34:57 -08003002 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003003 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07003004 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07003005 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07003006
Paul Belanger174a8272017-03-14 13:20:10 -04003007 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07003008 # Stop queuing timer triggered jobs so that the assertions
3009 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003010 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003011 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07003012 self.waitUntilSettled()
3013 # If APScheduler is in mid-event when we remove the job, we
3014 # can end up with one more event firing, so give it an extra
3015 # second to settle.
3016 time.sleep(1)
3017 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003018 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07003019 self.waitUntilSettled()
3020
James E. Blair21037782017-07-19 11:56:55 -07003021 self.assertHistory([
3022 dict(name='project-bitrot', result='SUCCESS',
3023 ref='refs/heads/master'),
3024 dict(name='project-bitrot', result='SUCCESS',
3025 ref='refs/heads/stable'),
3026 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07003027
3028 data = json.loads(data)
3029 status_jobs = set()
3030 for p in data['pipelines']:
3031 for q in p['change_queues']:
3032 for head in q['heads']:
3033 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07003034 for job in change['jobs']:
3035 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07003036 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003037
James E. Blair4f6033c2014-03-27 15:49:09 -07003038 def test_idle(self):
3039 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003040 # This test can not use simple_layout because it must start
3041 # with a configuration which does not include a
3042 # timer-triggered job so that we have an opportunity to set
3043 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003044 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003045
Clark Boylan3ee090a2014-04-03 20:55:09 -07003046 for x in range(1, 3):
3047 # Test that timer triggers periodic jobs even across
3048 # layout config reloads.
3049 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003050 self.commitConfigUpdate('common-config',
3051 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003052 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003053 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003054
Clark Boylan3ee090a2014-04-03 20:55:09 -07003055 # The pipeline triggers every second, so we should have seen
3056 # several by now.
3057 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003058
3059 # Stop queuing timer triggered jobs so that the assertions
3060 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003061 self.commitConfigUpdate('common-config',
3062 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003063 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003064 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003065 # If APScheduler is in mid-event when we remove the job,
3066 # we can end up with one more event firing, so give it an
3067 # extra second to settle.
3068 time.sleep(1)
3069 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003070 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003071 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003072 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003073 self.waitUntilSettled()
3074 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003075 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003076
James E. Blaircc324192017-04-19 14:36:08 -07003077 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003078 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003079 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3080 self.waitUntilSettled()
3081
3082 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3083 self.waitUntilSettled()
3084
James E. Blairff80a2f2013-12-27 13:24:06 -08003085 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003086
3087 # A.messages only holds what FakeGerrit places in it. Thus we
3088 # work on the knowledge of what the first message should be as
3089 # it is only configured to go to SMTP.
3090
3091 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003092 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003093 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003094 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003095 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003096 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003097
3098 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003099 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003100 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003101 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003102 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003103 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003104
James E. Blaire5910202013-12-27 09:50:31 -08003105 def test_timer_smtp(self):
3106 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003107 # This test can not use simple_layout because it must start
3108 # with a configuration which does not include a
3109 # timer-triggered job so that we have an opportunity to set
3110 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003111 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003112 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003113 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003114
Clark Boylan3ee090a2014-04-03 20:55:09 -07003115 # The pipeline triggers every second, so we should have seen
3116 # several by now.
3117 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003118 self.waitUntilSettled()
3119
Clark Boylan3ee090a2014-04-03 20:55:09 -07003120 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003121 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003122 self.waitUntilSettled()
3123 self.assertEqual(len(self.history), 2)
3124
James E. Blaire5910202013-12-27 09:50:31 -08003125 self.assertEqual(self.getJobFromHistory(
3126 'project-bitrot-stable-old').result, 'SUCCESS')
3127 self.assertEqual(self.getJobFromHistory(
3128 'project-bitrot-stable-older').result, 'SUCCESS')
3129
James E. Blairff80a2f2013-12-27 13:24:06 -08003130 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003131
3132 # A.messages only holds what FakeGerrit places in it. Thus we
3133 # work on the knowledge of what the first message should be as
3134 # it is only configured to go to SMTP.
3135
3136 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003137 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003138 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003139 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003140 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003141 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003142
Clark Boylan3ee090a2014-04-03 20:55:09 -07003143 # Stop queuing timer triggered jobs and let any that may have
3144 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003145 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003146 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003147 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003148 # If APScheduler is in mid-event when we remove the job, we
3149 # can end up with one more event firing, so give it an extra
3150 # second to settle.
3151 time.sleep(1)
3152 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003153 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003154 self.waitUntilSettled()
3155
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003156 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003157 def test_timer_sshkey(self):
3158 "Test that a periodic job can setup SSH key authentication"
3159 self.worker.hold_jobs_in_build = True
3160 self.config.set('zuul', 'layout_config',
3161 'tests/fixtures/layout-timer.yaml')
3162 self.sched.reconfigure(self.config)
3163 self.registerJobs()
3164
3165 # The pipeline triggers every second, so we should have seen
3166 # several by now.
3167 time.sleep(5)
3168 self.waitUntilSettled()
3169
3170 self.assertEqual(len(self.builds), 2)
3171
3172 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3173 self.assertTrue(os.path.isfile(ssh_wrapper))
3174 with open(ssh_wrapper) as f:
3175 ssh_wrapper_content = f.read()
3176 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3177 # In the unit tests Merger runs in the same process,
3178 # so we see its' environment variables
3179 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3180
3181 self.worker.release('.*')
3182 self.waitUntilSettled()
3183 self.assertEqual(len(self.history), 2)
3184
3185 self.assertEqual(self.getJobFromHistory(
3186 'project-bitrot-stable-old').result, 'SUCCESS')
3187 self.assertEqual(self.getJobFromHistory(
3188 'project-bitrot-stable-older').result, 'SUCCESS')
3189
3190 # Stop queuing timer triggered jobs and let any that may have
3191 # queued through so that end of test assertions pass.
3192 self.config.set('zuul', 'layout_config',
3193 'tests/fixtures/layout-no-timer.yaml')
3194 self.sched.reconfigure(self.config)
3195 self.registerJobs()
3196 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003197 # If APScheduler is in mid-event when we remove the job, we
3198 # can end up with one more event firing, so give it an extra
3199 # second to settle.
3200 time.sleep(1)
3201 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003202 self.worker.release('.*')
3203 self.waitUntilSettled()
3204
James E. Blair91e34592015-07-31 16:45:59 -07003205 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003206 "Test that the RPC client can enqueue a change"
3207 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003208 A.addApproval('Code-Review', 2)
3209 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003210
3211 client = zuul.rpcclient.RPCClient('127.0.0.1',
3212 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003213 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003214 r = client.enqueue(tenant='tenant-one',
3215 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003216 project='org/project',
3217 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003218 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003219 self.waitUntilSettled()
3220 self.assertEqual(self.getJobFromHistory('project-merge').result,
3221 'SUCCESS')
3222 self.assertEqual(self.getJobFromHistory('project-test1').result,
3223 'SUCCESS')
3224 self.assertEqual(self.getJobFromHistory('project-test2').result,
3225 'SUCCESS')
3226 self.assertEqual(A.data['status'], 'MERGED')
3227 self.assertEqual(A.reported, 2)
3228 self.assertEqual(r, True)
3229
James E. Blair91e34592015-07-31 16:45:59 -07003230 def test_client_enqueue_ref(self):
3231 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003232 p = "review.example.com/org/project"
3233 upstream = self.getUpstreamRepos([p])
3234 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3235 A.setMerged()
3236 A_commit = str(upstream[p].commit('master'))
3237 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003238
3239 client = zuul.rpcclient.RPCClient('127.0.0.1',
3240 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003241 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003242 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003243 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003244 pipeline='post',
3245 project='org/project',
3246 trigger='gerrit',
3247 ref='master',
3248 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003249 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003250 self.waitUntilSettled()
3251 job_names = [x.name for x in self.history]
3252 self.assertEqual(len(self.history), 1)
3253 self.assertIn('project-post', job_names)
3254 self.assertEqual(r, True)
3255
James E. Blairad28e912013-11-27 10:43:22 -08003256 def test_client_enqueue_negative(self):
3257 "Test that the RPC client returns errors"
3258 client = zuul.rpcclient.RPCClient('127.0.0.1',
3259 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003260 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003261 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003262 "Invalid tenant"):
3263 r = client.enqueue(tenant='tenant-foo',
3264 pipeline='gate',
3265 project='org/project',
3266 trigger='gerrit',
3267 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003268 self.assertEqual(r, False)
3269
3270 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003271 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003272 r = client.enqueue(tenant='tenant-one',
3273 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003274 project='project-does-not-exist',
3275 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003276 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003277 self.assertEqual(r, False)
3278
3279 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3280 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003281 r = client.enqueue(tenant='tenant-one',
3282 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003283 project='org/project',
3284 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003285 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003286 self.assertEqual(r, False)
3287
3288 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3289 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003290 r = client.enqueue(tenant='tenant-one',
3291 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003292 project='org/project',
3293 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003294 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003295 self.assertEqual(r, False)
3296
3297 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3298 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003299 r = client.enqueue(tenant='tenant-one',
3300 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003301 project='org/project',
3302 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003303 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003304 self.assertEqual(r, False)
3305
3306 self.waitUntilSettled()
3307 self.assertEqual(len(self.history), 0)
3308 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003309
3310 def test_client_promote(self):
3311 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003312 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003313 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3314 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3315 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003316 A.addApproval('Code-Review', 2)
3317 B.addApproval('Code-Review', 2)
3318 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003319
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003320 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3321 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3322 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003323
3324 self.waitUntilSettled()
3325
Paul Belangerbaca3132016-11-04 12:49:54 -04003326 tenant = self.sched.abide.tenants.get('tenant-one')
3327 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003328 enqueue_times = {}
3329 for item in items:
3330 enqueue_times[str(item.change)] = item.enqueue_time
3331
James E. Blair36658cf2013-12-06 17:53:48 -08003332 client = zuul.rpcclient.RPCClient('127.0.0.1',
3333 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003334 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003335 r = client.promote(tenant='tenant-one',
3336 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003337 change_ids=['2,1', '3,1'])
3338
Sean Daguef39b9ca2014-01-10 21:34:35 -05003339 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003340 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003341 for item in items:
3342 self.assertEqual(
3343 enqueue_times[str(item.change)], item.enqueue_time)
3344
James E. Blair78acec92014-02-06 07:11:32 -08003345 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003346 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003347 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003348 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003349 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003350 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003351 self.waitUntilSettled()
3352
3353 self.assertEqual(len(self.builds), 6)
3354 self.assertEqual(self.builds[0].name, 'project-test1')
3355 self.assertEqual(self.builds[1].name, 'project-test2')
3356 self.assertEqual(self.builds[2].name, 'project-test1')
3357 self.assertEqual(self.builds[3].name, 'project-test2')
3358 self.assertEqual(self.builds[4].name, 'project-test1')
3359 self.assertEqual(self.builds[5].name, 'project-test2')
3360
Paul Belangerbaca3132016-11-04 12:49:54 -04003361 self.assertTrue(self.builds[0].hasChanges(B))
3362 self.assertFalse(self.builds[0].hasChanges(A))
3363 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003364
Paul Belangerbaca3132016-11-04 12:49:54 -04003365 self.assertTrue(self.builds[2].hasChanges(B))
3366 self.assertTrue(self.builds[2].hasChanges(C))
3367 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003368
Paul Belangerbaca3132016-11-04 12:49:54 -04003369 self.assertTrue(self.builds[4].hasChanges(B))
3370 self.assertTrue(self.builds[4].hasChanges(C))
3371 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003372
Paul Belanger174a8272017-03-14 13:20:10 -04003373 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003374 self.waitUntilSettled()
3375
3376 self.assertEqual(A.data['status'], 'MERGED')
3377 self.assertEqual(A.reported, 2)
3378 self.assertEqual(B.data['status'], 'MERGED')
3379 self.assertEqual(B.reported, 2)
3380 self.assertEqual(C.data['status'], 'MERGED')
3381 self.assertEqual(C.reported, 2)
3382
James E. Blair36658cf2013-12-06 17:53:48 -08003383 self.assertEqual(r, True)
3384
3385 def test_client_promote_dependent(self):
3386 "Test that the RPC client can promote a dependent change"
3387 # C (depends on B) -> B -> A ; then promote C to get:
3388 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003389 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003390 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3391 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3392 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3393
3394 C.setDependsOn(B, 1)
3395
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003396 A.addApproval('Code-Review', 2)
3397 B.addApproval('Code-Review', 2)
3398 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003399
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003400 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3401 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3402 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003403
3404 self.waitUntilSettled()
3405
3406 client = zuul.rpcclient.RPCClient('127.0.0.1',
3407 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003408 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003409 r = client.promote(tenant='tenant-one',
3410 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003411 change_ids=['3,1'])
3412
James E. Blair78acec92014-02-06 07:11:32 -08003413 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003414 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003415 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003416 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003417 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003418 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003419 self.waitUntilSettled()
3420
3421 self.assertEqual(len(self.builds), 6)
3422 self.assertEqual(self.builds[0].name, 'project-test1')
3423 self.assertEqual(self.builds[1].name, 'project-test2')
3424 self.assertEqual(self.builds[2].name, 'project-test1')
3425 self.assertEqual(self.builds[3].name, 'project-test2')
3426 self.assertEqual(self.builds[4].name, 'project-test1')
3427 self.assertEqual(self.builds[5].name, 'project-test2')
3428
Paul Belangerbaca3132016-11-04 12:49:54 -04003429 self.assertTrue(self.builds[0].hasChanges(B))
3430 self.assertFalse(self.builds[0].hasChanges(A))
3431 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003432
Paul Belangerbaca3132016-11-04 12:49:54 -04003433 self.assertTrue(self.builds[2].hasChanges(B))
3434 self.assertTrue(self.builds[2].hasChanges(C))
3435 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003436
Paul Belangerbaca3132016-11-04 12:49:54 -04003437 self.assertTrue(self.builds[4].hasChanges(B))
3438 self.assertTrue(self.builds[4].hasChanges(C))
3439 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003440
Paul Belanger174a8272017-03-14 13:20:10 -04003441 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003442 self.waitUntilSettled()
3443
3444 self.assertEqual(A.data['status'], 'MERGED')
3445 self.assertEqual(A.reported, 2)
3446 self.assertEqual(B.data['status'], 'MERGED')
3447 self.assertEqual(B.reported, 2)
3448 self.assertEqual(C.data['status'], 'MERGED')
3449 self.assertEqual(C.reported, 2)
3450
James E. Blair36658cf2013-12-06 17:53:48 -08003451 self.assertEqual(r, True)
3452
3453 def test_client_promote_negative(self):
3454 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003455 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003456 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003457 A.addApproval('Code-Review', 2)
3458 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003459 self.waitUntilSettled()
3460
3461 client = zuul.rpcclient.RPCClient('127.0.0.1',
3462 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003463 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003464
3465 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003466 r = client.promote(tenant='tenant-one',
3467 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003468 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003469 self.assertEqual(r, False)
3470
3471 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003472 r = client.promote(tenant='tenant-one',
3473 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003474 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003475 self.assertEqual(r, False)
3476
Paul Belanger174a8272017-03-14 13:20:10 -04003477 self.executor_server.hold_jobs_in_build = False
3478 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003479 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003480
James E. Blaircc324192017-04-19 14:36:08 -07003481 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003482 def test_queue_rate_limiting(self):
3483 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003484 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003485 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3486 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3487 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3488
3489 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003490 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003491
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003492 A.addApproval('Code-Review', 2)
3493 B.addApproval('Code-Review', 2)
3494 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003495
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003496 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3497 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3498 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003499 self.waitUntilSettled()
3500
3501 # Only A and B will have their merge jobs queued because
3502 # window is 2.
3503 self.assertEqual(len(self.builds), 2)
3504 self.assertEqual(self.builds[0].name, 'project-merge')
3505 self.assertEqual(self.builds[1].name, 'project-merge')
3506
James E. Blair732ee862017-04-21 07:49:26 -07003507 # Release the merge jobs one at a time.
3508 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003509 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003510 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003511 self.waitUntilSettled()
3512
3513 # Only A and B will have their test jobs queued because
3514 # window is 2.
3515 self.assertEqual(len(self.builds), 4)
3516 self.assertEqual(self.builds[0].name, 'project-test1')
3517 self.assertEqual(self.builds[1].name, 'project-test2')
3518 self.assertEqual(self.builds[2].name, 'project-test1')
3519 self.assertEqual(self.builds[3].name, 'project-test2')
3520
Paul Belanger174a8272017-03-14 13:20:10 -04003521 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003522 self.waitUntilSettled()
3523
James E. Blaircc324192017-04-19 14:36:08 -07003524 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003525 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003526 # A failed so window is reduced by 1 to 1.
3527 self.assertEqual(queue.window, 1)
3528 self.assertEqual(queue.window_floor, 1)
3529 self.assertEqual(A.data['status'], 'NEW')
3530
3531 # Gate is reset and only B's merge job is queued because
3532 # window shrunk to 1.
3533 self.assertEqual(len(self.builds), 1)
3534 self.assertEqual(self.builds[0].name, 'project-merge')
3535
Paul Belanger174a8272017-03-14 13:20:10 -04003536 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003537 self.waitUntilSettled()
3538
3539 # Only B's test jobs are queued because window is still 1.
3540 self.assertEqual(len(self.builds), 2)
3541 self.assertEqual(self.builds[0].name, 'project-test1')
3542 self.assertEqual(self.builds[1].name, 'project-test2')
3543
Paul Belanger174a8272017-03-14 13:20:10 -04003544 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003545 self.waitUntilSettled()
3546
3547 # B was successfully merged so window is increased to 2.
3548 self.assertEqual(queue.window, 2)
3549 self.assertEqual(queue.window_floor, 1)
3550 self.assertEqual(B.data['status'], 'MERGED')
3551
3552 # Only C is left and its merge job is queued.
3553 self.assertEqual(len(self.builds), 1)
3554 self.assertEqual(self.builds[0].name, 'project-merge')
3555
Paul Belanger174a8272017-03-14 13:20:10 -04003556 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003557 self.waitUntilSettled()
3558
3559 # After successful merge job the test jobs for C are queued.
3560 self.assertEqual(len(self.builds), 2)
3561 self.assertEqual(self.builds[0].name, 'project-test1')
3562 self.assertEqual(self.builds[1].name, 'project-test2')
3563
Paul Belanger174a8272017-03-14 13:20:10 -04003564 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003565 self.waitUntilSettled()
3566
3567 # C successfully merged so window is bumped to 3.
3568 self.assertEqual(queue.window, 3)
3569 self.assertEqual(queue.window_floor, 1)
3570 self.assertEqual(C.data['status'], 'MERGED')
3571
James E. Blaircc324192017-04-19 14:36:08 -07003572 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003573 def test_queue_rate_limiting_dependent(self):
3574 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003575 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003576 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3577 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3578 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3579
3580 B.setDependsOn(A, 1)
3581
Paul Belanger174a8272017-03-14 13:20:10 -04003582 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003583
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003584 A.addApproval('Code-Review', 2)
3585 B.addApproval('Code-Review', 2)
3586 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003587
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003588 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3589 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3590 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003591 self.waitUntilSettled()
3592
3593 # Only A and B will have their merge jobs queued because
3594 # window is 2.
3595 self.assertEqual(len(self.builds), 2)
3596 self.assertEqual(self.builds[0].name, 'project-merge')
3597 self.assertEqual(self.builds[1].name, 'project-merge')
3598
James E. Blairf4a5f022017-04-18 14:01:10 -07003599 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003600
3601 # Only A and B will have their test jobs queued because
3602 # window is 2.
3603 self.assertEqual(len(self.builds), 4)
3604 self.assertEqual(self.builds[0].name, 'project-test1')
3605 self.assertEqual(self.builds[1].name, 'project-test2')
3606 self.assertEqual(self.builds[2].name, 'project-test1')
3607 self.assertEqual(self.builds[3].name, 'project-test2')
3608
Paul Belanger174a8272017-03-14 13:20:10 -04003609 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003610 self.waitUntilSettled()
3611
James E. Blaircc324192017-04-19 14:36:08 -07003612 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003613 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003614 # A failed so window is reduced by 1 to 1.
3615 self.assertEqual(queue.window, 1)
3616 self.assertEqual(queue.window_floor, 1)
3617 self.assertEqual(A.data['status'], 'NEW')
3618 self.assertEqual(B.data['status'], 'NEW')
3619
3620 # Gate is reset and only C's merge job is queued because
3621 # window shrunk to 1 and A and B were dequeued.
3622 self.assertEqual(len(self.builds), 1)
3623 self.assertEqual(self.builds[0].name, 'project-merge')
3624
James E. Blairf4a5f022017-04-18 14:01:10 -07003625 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003626
3627 # Only C's test jobs are queued because window is still 1.
3628 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003629 builds = self.getSortedBuilds()
3630 self.assertEqual(builds[0].name, 'project-test1')
3631 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003632
Paul Belanger174a8272017-03-14 13:20:10 -04003633 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003634 self.waitUntilSettled()
3635
3636 # C was successfully merged so window is increased to 2.
3637 self.assertEqual(queue.window, 2)
3638 self.assertEqual(queue.window_floor, 1)
3639 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003640
3641 def test_worker_update_metadata(self):
3642 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003643 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003644
3645 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003646 A.addApproval('Code-Review', 2)
3647 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003648 self.waitUntilSettled()
3649
Clint Byrumd4de79a2017-05-27 11:39:13 -07003650 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003651
3652 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003653 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003654
3655 start = time.time()
3656 while True:
3657 if time.time() - start > 10:
3658 raise Exception("Timeout waiting for gearman server to report "
3659 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003660 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003661 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003662 break
3663 else:
3664 time.sleep(0)
3665
3666 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003667 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003668
Paul Belanger174a8272017-03-14 13:20:10 -04003669 self.executor_server.hold_jobs_in_build = False
3670 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003671 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003672
James E. Blaircc324192017-04-19 14:36:08 -07003673 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003674 def test_footer_message(self):
3675 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003676 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003677 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003678 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003679 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003680 self.waitUntilSettled()
3681
3682 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003683 B.addApproval('Code-Review', 2)
3684 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003685 self.waitUntilSettled()
3686
3687 self.assertEqual(2, len(self.smtp_messages))
3688
Adam Gandelman82258fd2017-02-07 16:51:58 -08003689 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003690Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003691http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003692
Adam Gandelman82258fd2017-02-07 16:51:58 -08003693 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003694For CI problems and help debugging, contact ci@example.org"""
3695
Adam Gandelman82258fd2017-02-07 16:51:58 -08003696 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3697 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3698 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3699 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003700
Tobias Henkel9842bd72017-05-16 13:40:03 +02003701 @simple_layout('layouts/unmanaged-project.yaml')
3702 def test_unmanaged_project_start_message(self):
3703 "Test start reporting is not done for unmanaged projects."
3704 self.init_repo("org/project", tag='init')
3705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3706 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3707 self.waitUntilSettled()
3708
3709 self.assertEqual(0, len(A.messages))
3710
Clint Byrumb9de8be2017-05-27 13:49:19 -07003711 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003712 def test_merge_failure_reporters(self):
3713 """Check that the config is set up correctly"""
3714
Clint Byrumb9de8be2017-05-27 13:49:19 -07003715 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003716 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003717 "Merge Failed.\n\nThis change or one of its cross-repo "
3718 "dependencies was unable to be automatically merged with the "
3719 "current state of its repository. Please rebase the change and "
3720 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003721 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003722 self.assertEqual(
3723 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003724 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003725
3726 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003727 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003728 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003729 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003730
3731 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003732 tenant.layout.pipelines['check'].merge_failure_actions[0],
3733 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003734
3735 self.assertTrue(
3736 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003737 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003738 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003739 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3740 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003741 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003742 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003743 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003744 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003745 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003746 gerritreporter.GerritReporter) and
3747 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003748 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003749 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003750 )
3751 )
3752
James E. Blairec056492016-07-22 09:45:56 -07003753 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003754 def test_merge_failure_reports(self):
3755 """Check that when a change fails to merge the correct message is sent
3756 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003757 self.updateConfigLayout(
3758 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003759 self.sched.reconfigure(self.config)
3760 self.registerJobs()
3761
3762 # Check a test failure isn't reported to SMTP
3763 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003764 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003765 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003766 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003767 self.waitUntilSettled()
3768
3769 self.assertEqual(3, len(self.history)) # 3 jobs
3770 self.assertEqual(0, len(self.smtp_messages))
3771
3772 # Check a merge failure is reported to SMTP
3773 # B should be merged, but C will conflict with B
3774 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3775 B.addPatchset(['conflict'])
3776 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3777 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003778 B.addApproval('Code-Review', 2)
3779 C.addApproval('Code-Review', 2)
3780 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3781 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003782 self.waitUntilSettled()
3783
3784 self.assertEqual(6, len(self.history)) # A and B jobs
3785 self.assertEqual(1, len(self.smtp_messages))
3786 self.assertEqual('The merge failed! For more information...',
3787 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003788
James E. Blairec056492016-07-22 09:45:56 -07003789 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003790 def test_default_merge_failure_reports(self):
3791 """Check that the default merge failure reports are correct."""
3792
3793 # A should report success, B should report merge failure.
3794 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3795 A.addPatchset(['conflict'])
3796 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3797 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003798 A.addApproval('Code-Review', 2)
3799 B.addApproval('Code-Review', 2)
3800 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3801 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003802 self.waitUntilSettled()
3803
3804 self.assertEqual(3, len(self.history)) # A jobs
3805 self.assertEqual(A.reported, 2)
3806 self.assertEqual(B.reported, 2)
3807 self.assertEqual(A.data['status'], 'MERGED')
3808 self.assertEqual(B.data['status'], 'NEW')
3809 self.assertIn('Build succeeded', A.messages[1])
3810 self.assertIn('Merge Failed', B.messages[1])
3811 self.assertIn('automatically merged', B.messages[1])
3812 self.assertNotIn('logs.example.com', B.messages[1])
3813 self.assertNotIn('SKIPPED', B.messages[1])
3814
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003815 def test_client_get_running_jobs(self):
3816 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003817 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003819 A.addApproval('Code-Review', 2)
3820 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003821 self.waitUntilSettled()
3822
3823 client = zuul.rpcclient.RPCClient('127.0.0.1',
3824 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003825 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003826
3827 # Wait for gearman server to send the initial workData back to zuul
3828 start = time.time()
3829 while True:
3830 if time.time() - start > 10:
3831 raise Exception("Timeout waiting for gearman server to report "
3832 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003833 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003834 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003835 break
3836 else:
3837 time.sleep(0)
3838
3839 running_items = client.get_running_jobs()
3840
3841 self.assertEqual(1, len(running_items))
3842 running_item = running_items[0]
3843 self.assertEqual([], running_item['failing_reasons'])
3844 self.assertEqual([], running_item['items_behind'])
3845 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003846 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003847 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003848 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003849 self.assertEqual(True, running_item['active'])
3850 self.assertEqual('1,1', running_item['id'])
3851
3852 self.assertEqual(3, len(running_item['jobs']))
3853 for job in running_item['jobs']:
3854 if job['name'] == 'project-merge':
3855 self.assertEqual('project-merge', job['name'])
3856 self.assertEqual('gate', job['pipeline'])
3857 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003858 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003859 'static/stream.html?uuid={uuid}&logfile=console.log'
3860 .format(uuid=job['uuid']), job['url'])
3861 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003862 'finger://{hostname}/{uuid}'.format(
3863 hostname=self.executor_server.hostname,
3864 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003865 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003866 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003867 self.assertEqual(False, job['canceled'])
3868 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003869 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003870 self.assertEqual('gate', job['pipeline'])
3871 break
3872
Paul Belanger174a8272017-03-14 13:20:10 -04003873 self.executor_server.hold_jobs_in_build = False
3874 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003875 self.waitUntilSettled()
3876
3877 running_items = client.get_running_jobs()
3878 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003879
James E. Blairb70e55a2017-04-19 12:57:02 -07003880 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003881 def test_nonvoting_pipeline(self):
3882 "Test that a nonvoting pipeline (experimental) can still report"
3883
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003884 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3885 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003886 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3887 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003888
3889 self.assertEqual(self.getJobFromHistory('project-merge').result,
3890 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003891 self.assertEqual(
3892 self.getJobFromHistory('experimental-project-test').result,
3893 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003894 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003895
3896 def test_crd_gate(self):
3897 "Test cross-repo dependencies"
3898 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3899 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003900 A.addApproval('Code-Review', 2)
3901 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003902
3903 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3904 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3905 AM2.setMerged()
3906 AM1.setMerged()
3907
3908 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3909 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3910 BM2.setMerged()
3911 BM1.setMerged()
3912
3913 # A -> AM1 -> AM2
3914 # B -> BM1 -> BM2
3915 # A Depends-On: B
3916 # M2 is here to make sure it is never queried. If it is, it
3917 # means zuul is walking down the entire history of merged
3918 # changes.
3919
3920 B.setDependsOn(BM1, 1)
3921 BM1.setDependsOn(BM2, 1)
3922
3923 A.setDependsOn(AM1, 1)
3924 AM1.setDependsOn(AM2, 1)
3925
3926 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3927 A.subject, B.data['id'])
3928
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003929 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003930 self.waitUntilSettled()
3931
3932 self.assertEqual(A.data['status'], 'NEW')
3933 self.assertEqual(B.data['status'], 'NEW')
3934
Paul Belanger5dccbe72016-11-14 11:17:37 -05003935 for connection in self.connections.connections.values():
James E. Blairb9704302017-10-11 16:42:18 -07003936 if hasattr(connection, '_change_cache'):
3937 connection._change_cache.clear()
James E. Blair5ee24252014-12-30 10:12:29 -08003938
Paul Belanger174a8272017-03-14 13:20:10 -04003939 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003940 B.addApproval('Approved', 1)
3941 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003942 self.waitUntilSettled()
3943
Paul Belanger174a8272017-03-14 13:20:10 -04003944 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003945 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003946 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003947 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003948 self.executor_server.hold_jobs_in_build = False
3949 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003950 self.waitUntilSettled()
3951
3952 self.assertEqual(AM2.queried, 0)
3953 self.assertEqual(BM2.queried, 0)
3954 self.assertEqual(A.data['status'], 'MERGED')
3955 self.assertEqual(B.data['status'], 'MERGED')
3956 self.assertEqual(A.reported, 2)
3957 self.assertEqual(B.reported, 2)
3958
Paul Belanger5dccbe72016-11-14 11:17:37 -05003959 changes = self.getJobFromHistory(
3960 'project-merge', 'org/project1').changes
3961 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003962
3963 def test_crd_branch(self):
3964 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003965
3966 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003967 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3968 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003969 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3970 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3971 status='ABANDONED')
3972 C1.data['id'] = B.data['id']
3973 C2.data['id'] = B.data['id']
3974
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003975 A.addApproval('Code-Review', 2)
3976 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003977 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003978
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003979 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003980 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3981 A.subject, B.data['id'])
3982
Paul Belanger174a8272017-03-14 13:20:10 -04003983 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003984 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003985 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003986 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003987 self.waitUntilSettled()
3988
Paul Belanger174a8272017-03-14 13:20:10 -04003989 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003990 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003991 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003992 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003993 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003994 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003995 self.executor_server.hold_jobs_in_build = False
3996 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003997 self.waitUntilSettled()
3998
3999 self.assertEqual(A.data['status'], 'MERGED')
4000 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04004001 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08004002 self.assertEqual(A.reported, 2)
4003 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04004004 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08004005
Paul Belanger6379db12016-11-14 13:57:54 -05004006 changes = self.getJobFromHistory(
4007 'project-merge', 'org/project1').changes
4008 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08004009
4010 def test_crd_multiline(self):
4011 "Test multiple depends-on lines in commit"
4012 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4013 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4014 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004015 A.addApproval('Code-Review', 2)
4016 B.addApproval('Code-Review', 2)
4017 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08004018
4019 # A Depends-On: B+C
4020 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
4021 A.subject, B.data['id'], C.data['id'])
4022
Paul Belanger174a8272017-03-14 13:20:10 -04004023 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004024 B.addApproval('Approved', 1)
4025 C.addApproval('Approved', 1)
4026 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08004027 self.waitUntilSettled()
4028
Paul Belanger174a8272017-03-14 13:20:10 -04004029 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004030 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004031 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004032 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004033 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08004034 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004035 self.executor_server.hold_jobs_in_build = False
4036 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08004037 self.waitUntilSettled()
4038
4039 self.assertEqual(A.data['status'], 'MERGED')
4040 self.assertEqual(B.data['status'], 'MERGED')
4041 self.assertEqual(C.data['status'], 'MERGED')
4042 self.assertEqual(A.reported, 2)
4043 self.assertEqual(B.reported, 2)
4044 self.assertEqual(C.reported, 2)
4045
Paul Belanger5dccbe72016-11-14 11:17:37 -05004046 changes = self.getJobFromHistory(
4047 'project-merge', 'org/project1').changes
4048 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08004049
4050 def test_crd_unshared_gate(self):
4051 "Test cross-repo dependencies in unshared gate queues"
4052 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4053 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004054 A.addApproval('Code-Review', 2)
4055 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004056
4057 # A Depends-On: B
4058 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4059 A.subject, B.data['id'])
4060
4061 # A and B do not share a queue, make sure that A is unable to
4062 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004063 B.addApproval('Approved', 1)
4064 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004065 self.waitUntilSettled()
4066
4067 self.assertEqual(A.data['status'], 'NEW')
4068 self.assertEqual(B.data['status'], 'NEW')
4069 self.assertEqual(A.reported, 0)
4070 self.assertEqual(B.reported, 0)
4071 self.assertEqual(len(self.history), 0)
4072
4073 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004074 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004075 self.waitUntilSettled()
4076
4077 self.assertEqual(B.data['status'], 'MERGED')
4078 self.assertEqual(B.reported, 2)
4079
4080 # Now that B is merged, A should be able to be enqueued and
4081 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004082 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004083 self.waitUntilSettled()
4084
4085 self.assertEqual(A.data['status'], 'MERGED')
4086 self.assertEqual(A.reported, 2)
4087
James E. Blair96698e22015-04-02 07:48:21 -07004088 def test_crd_gate_reverse(self):
4089 "Test reverse cross-repo dependencies"
4090 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4091 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004092 A.addApproval('Code-Review', 2)
4093 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004094
4095 # A Depends-On: B
4096
4097 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4098 A.subject, B.data['id'])
4099
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004100 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004101 self.waitUntilSettled()
4102
4103 self.assertEqual(A.data['status'], 'NEW')
4104 self.assertEqual(B.data['status'], 'NEW')
4105
Paul Belanger174a8272017-03-14 13:20:10 -04004106 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004107 A.addApproval('Approved', 1)
4108 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004109 self.waitUntilSettled()
4110
Paul Belanger174a8272017-03-14 13:20:10 -04004111 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004112 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004113 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004114 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004115 self.executor_server.hold_jobs_in_build = False
4116 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004117 self.waitUntilSettled()
4118
4119 self.assertEqual(A.data['status'], 'MERGED')
4120 self.assertEqual(B.data['status'], 'MERGED')
4121 self.assertEqual(A.reported, 2)
4122 self.assertEqual(B.reported, 2)
4123
Paul Belanger5dccbe72016-11-14 11:17:37 -05004124 changes = self.getJobFromHistory(
4125 'project-merge', 'org/project1').changes
4126 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004127
James E. Blair5ee24252014-12-30 10:12:29 -08004128 def test_crd_cycle(self):
4129 "Test cross-repo dependency cycles"
4130 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4131 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004132 A.addApproval('Code-Review', 2)
4133 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004134
4135 # A -> B -> A (via commit-depends)
4136
4137 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4138 A.subject, B.data['id'])
4139 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4140 B.subject, A.data['id'])
4141
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004142 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004143 self.waitUntilSettled()
4144
4145 self.assertEqual(A.reported, 0)
4146 self.assertEqual(B.reported, 0)
4147 self.assertEqual(A.data['status'], 'NEW')
4148 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004149
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004150 def test_crd_gate_unknown(self):
4151 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004152 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4154 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004155 A.addApproval('Code-Review', 2)
4156 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004157
4158 # A Depends-On: B
4159 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4160 A.subject, B.data['id'])
4161
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004162 B.addApproval('Approved', 1)
4163 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004164 self.waitUntilSettled()
4165
4166 # Unknown projects cannot share a queue with any other
4167 # since they don't have common jobs with any other (they have no jobs).
4168 # Changes which depend on unknown project changes
4169 # should not be processed in dependent pipeline
4170 self.assertEqual(A.data['status'], 'NEW')
4171 self.assertEqual(B.data['status'], 'NEW')
4172 self.assertEqual(A.reported, 0)
4173 self.assertEqual(B.reported, 0)
4174 self.assertEqual(len(self.history), 0)
4175
James E. Blair7720b402017-06-07 08:51:49 -07004176 # Simulate change B being gated outside this layout Set the
4177 # change merged before submitting the event so that when the
4178 # event triggers a gerrit query to update the change, we get
4179 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004180 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004181 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004182 self.waitUntilSettled()
4183 self.assertEqual(len(self.history), 0)
4184
4185 # Now that B is merged, A should be able to be enqueued and
4186 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004187 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004188 self.waitUntilSettled()
4189
4190 self.assertEqual(A.data['status'], 'MERGED')
4191 self.assertEqual(A.reported, 2)
4192 self.assertEqual(B.data['status'], 'MERGED')
4193 self.assertEqual(B.reported, 0)
4194
James E. Blairbfb8e042014-12-30 17:01:44 -08004195 def test_crd_check(self):
4196 "Test cross-repo dependencies in independent pipelines"
4197
Paul Belanger174a8272017-03-14 13:20:10 -04004198 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004199 self.gearman_server.hold_jobs_in_queue = True
4200 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4201 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4202
4203 # A Depends-On: B
4204 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4205 A.subject, B.data['id'])
4206
4207 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4208 self.waitUntilSettled()
4209
James E. Blairbfb8e042014-12-30 17:01:44 -08004210 self.gearman_server.hold_jobs_in_queue = False
4211 self.gearman_server.release()
4212 self.waitUntilSettled()
4213
Paul Belanger174a8272017-03-14 13:20:10 -04004214 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004215 self.waitUntilSettled()
4216
James E. Blair2a535672017-04-27 12:03:15 -07004217 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004218
Paul Belanger174a8272017-03-14 13:20:10 -04004219 self.executor_server.hold_jobs_in_build = False
4220 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004221 self.waitUntilSettled()
4222
James E. Blairbfb8e042014-12-30 17:01:44 -08004223 self.assertEqual(A.data['status'], 'NEW')
4224 self.assertEqual(B.data['status'], 'NEW')
4225 self.assertEqual(A.reported, 1)
4226 self.assertEqual(B.reported, 0)
4227
4228 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004229 tenant = self.sched.abide.tenants.get('tenant-one')
4230 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004231
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004232 def test_crd_check_git_depends(self):
4233 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004234 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004235 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4236 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4237
4238 # Add two git-dependent changes and make sure they both report
4239 # success.
4240 B.setDependsOn(A, 1)
4241 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4242 self.waitUntilSettled()
4243 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4244 self.waitUntilSettled()
4245
James E. Blairb8c16472015-05-05 14:55:26 -07004246 self.orderedRelease()
4247 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004248 self.waitUntilSettled()
4249
4250 self.assertEqual(A.data['status'], 'NEW')
4251 self.assertEqual(B.data['status'], 'NEW')
4252 self.assertEqual(A.reported, 1)
4253 self.assertEqual(B.reported, 1)
4254
4255 self.assertEqual(self.history[0].changes, '1,1')
4256 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004257 tenant = self.sched.abide.tenants.get('tenant-one')
4258 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004259
4260 self.assertIn('Build succeeded', A.messages[0])
4261 self.assertIn('Build succeeded', B.messages[0])
4262
4263 def test_crd_check_duplicate(self):
4264 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004265 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004266 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4267 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004268 tenant = self.sched.abide.tenants.get('tenant-one')
4269 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004270
4271 # Add two git-dependent changes...
4272 B.setDependsOn(A, 1)
4273 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4274 self.waitUntilSettled()
4275 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4276
4277 # ...make sure the live one is not duplicated...
4278 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4279 self.waitUntilSettled()
4280 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4281
4282 # ...but the non-live one is able to be.
4283 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4284 self.waitUntilSettled()
4285 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4286
Clark Boylandd849822015-03-02 12:38:14 -08004287 # Release jobs in order to avoid races with change A jobs
4288 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004289 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004290 self.executor_server.hold_jobs_in_build = False
4291 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004292 self.waitUntilSettled()
4293
4294 self.assertEqual(A.data['status'], 'NEW')
4295 self.assertEqual(B.data['status'], 'NEW')
4296 self.assertEqual(A.reported, 1)
4297 self.assertEqual(B.reported, 1)
4298
4299 self.assertEqual(self.history[0].changes, '1,1 2,1')
4300 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004301 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004302
4303 self.assertIn('Build succeeded', A.messages[0])
4304 self.assertIn('Build succeeded', B.messages[0])
4305
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004306 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004307 "Test cross-repo dependencies re-enqueued in independent pipelines"
4308
4309 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004310 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4311 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004312
4313 # A Depends-On: B
4314 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4315 A.subject, B.data['id'])
4316
4317 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4318 self.waitUntilSettled()
4319
4320 self.sched.reconfigure(self.config)
4321
4322 # Make sure the items still share a change queue, and the
4323 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004324 tenant = self.sched.abide.tenants.get('tenant-one')
4325 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4326 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004327 first_item = queue.queue[0]
4328 for item in queue.queue:
4329 self.assertEqual(item.queue, first_item.queue)
4330 self.assertFalse(first_item.live)
4331 self.assertTrue(queue.queue[1].live)
4332
4333 self.gearman_server.hold_jobs_in_queue = False
4334 self.gearman_server.release()
4335 self.waitUntilSettled()
4336
4337 self.assertEqual(A.data['status'], 'NEW')
4338 self.assertEqual(B.data['status'], 'NEW')
4339 self.assertEqual(A.reported, 1)
4340 self.assertEqual(B.reported, 0)
4341
4342 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004343 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004344
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004345 def test_crd_check_reconfiguration(self):
4346 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4347
4348 def test_crd_undefined_project(self):
4349 """Test that undefined projects in dependencies are handled for
4350 independent pipelines"""
4351 # It's a hack for fake gerrit,
4352 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004353 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004354 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4355
Jesse Keatingea68ab42017-04-19 15:03:30 -07004356 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004357 def test_crd_check_ignore_dependencies(self):
4358 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004359
4360 self.gearman_server.hold_jobs_in_queue = True
4361 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4362 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4363 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4364
4365 # A Depends-On: B
4366 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4367 A.subject, B.data['id'])
4368 # C git-depends on B
4369 C.setDependsOn(B, 1)
4370 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4371 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4372 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4373 self.waitUntilSettled()
4374
4375 # Make sure none of the items share a change queue, and all
4376 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004377 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004378 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004379 self.assertEqual(len(check_pipeline.queues), 3)
4380 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4381 for item in check_pipeline.getAllItems():
4382 self.assertTrue(item.live)
4383
4384 self.gearman_server.hold_jobs_in_queue = False
4385 self.gearman_server.release()
4386 self.waitUntilSettled()
4387
4388 self.assertEqual(A.data['status'], 'NEW')
4389 self.assertEqual(B.data['status'], 'NEW')
4390 self.assertEqual(C.data['status'], 'NEW')
4391 self.assertEqual(A.reported, 1)
4392 self.assertEqual(B.reported, 1)
4393 self.assertEqual(C.reported, 1)
4394
4395 # Each job should have tested exactly one change
4396 for job in self.history:
4397 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004398
James E. Blair7cb84542017-04-19 13:35:05 -07004399 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004400 def test_crd_check_transitive(self):
4401 "Test transitive cross-repo dependencies"
4402 # Specifically, if A -> B -> C, and C gets a new patchset and
4403 # A gets a new patchset, ensure the test of A,2 includes B,1
4404 # and C,2 (not C,1 which would indicate stale data in the
4405 # cache for B).
4406 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4407 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4408 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4409
4410 # A Depends-On: B
4411 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4412 A.subject, B.data['id'])
4413
4414 # B Depends-On: C
4415 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4416 B.subject, C.data['id'])
4417
4418 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4419 self.waitUntilSettled()
4420 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4421
4422 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4423 self.waitUntilSettled()
4424 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4425
4426 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4427 self.waitUntilSettled()
4428 self.assertEqual(self.history[-1].changes, '3,1')
4429
4430 C.addPatchset()
4431 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4432 self.waitUntilSettled()
4433 self.assertEqual(self.history[-1].changes, '3,2')
4434
4435 A.addPatchset()
4436 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4437 self.waitUntilSettled()
4438 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004439
James E. Blaira8b90b32016-08-24 15:18:50 -07004440 def test_crd_check_unknown(self):
4441 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004442 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004443 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4444 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4445 # A Depends-On: B
4446 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4447 A.subject, B.data['id'])
4448
4449 # Make sure zuul has seen an event on B.
4450 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4451 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4452 self.waitUntilSettled()
4453
4454 self.assertEqual(A.data['status'], 'NEW')
4455 self.assertEqual(A.reported, 1)
4456 self.assertEqual(B.data['status'], 'NEW')
4457 self.assertEqual(B.reported, 0)
4458
James E. Blair92464a22016-04-05 10:21:26 -07004459 def test_crd_cycle_join(self):
4460 "Test an updated change creates a cycle"
4461 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4462
4463 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4464 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004465 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004466
4467 # Create B->A
4468 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4469 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4470 B.subject, A.data['id'])
4471 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4472 self.waitUntilSettled()
4473
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004474 # Dep is there so zuul should have reported on B
4475 self.assertEqual(B.reported, 1)
4476
James E. Blair92464a22016-04-05 10:21:26 -07004477 # Update A to add A->B (a cycle).
4478 A.addPatchset()
4479 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4480 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004481 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4482 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004483
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004484 # Dependency cycle injected so zuul should not have reported again on A
4485 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004486
4487 # Now if we update B to remove the depends-on, everything
4488 # should be okay. B; A->B
4489
4490 B.addPatchset()
4491 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004492 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4493 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004494
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004495 # Cycle was removed so now zuul should have reported again on A
4496 self.assertEqual(A.reported, 2)
4497
4498 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4499 self.waitUntilSettled()
4500 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004501
Jesse Keatingcc67b122017-04-19 14:54:57 -07004502 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004503 def test_disable_at(self):
4504 "Test a pipeline will only report to the disabled trigger when failing"
4505
Jesse Keatingcc67b122017-04-19 14:54:57 -07004506 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004507 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004508 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004509 0, tenant.layout.pipelines['check']._consecutive_failures)
4510 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004511
4512 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4513 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4514 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4515 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4516 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4517 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4518 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4519 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4520 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4521 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4522 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4523
Paul Belanger174a8272017-03-14 13:20:10 -04004524 self.executor_server.failJob('project-test1', A)
4525 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004526 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004527 self.executor_server.failJob('project-test1', D)
4528 self.executor_server.failJob('project-test1', E)
4529 self.executor_server.failJob('project-test1', F)
4530 self.executor_server.failJob('project-test1', G)
4531 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004532 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004533 self.executor_server.failJob('project-test1', J)
4534 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004535
4536 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4537 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4538 self.waitUntilSettled()
4539
4540 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004541 2, tenant.layout.pipelines['check']._consecutive_failures)
4542 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004543
4544 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4545 self.waitUntilSettled()
4546
4547 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004548 0, tenant.layout.pipelines['check']._consecutive_failures)
4549 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004550
4551 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4552 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4553 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4554 self.waitUntilSettled()
4555
4556 # We should be disabled now
4557 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004558 3, tenant.layout.pipelines['check']._consecutive_failures)
4559 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004560
4561 # We need to wait between each of these patches to make sure the
4562 # smtp messages come back in an expected order
4563 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4564 self.waitUntilSettled()
4565 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4566 self.waitUntilSettled()
4567 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4568 self.waitUntilSettled()
4569
4570 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4571 # leaving a message on each change
4572 self.assertEqual(1, len(A.messages))
4573 self.assertIn('Build failed.', A.messages[0])
4574 self.assertEqual(1, len(B.messages))
4575 self.assertIn('Build failed.', B.messages[0])
4576 self.assertEqual(1, len(C.messages))
4577 self.assertIn('Build succeeded.', C.messages[0])
4578 self.assertEqual(1, len(D.messages))
4579 self.assertIn('Build failed.', D.messages[0])
4580 self.assertEqual(1, len(E.messages))
4581 self.assertIn('Build failed.', E.messages[0])
4582 self.assertEqual(1, len(F.messages))
4583 self.assertIn('Build failed.', F.messages[0])
4584
4585 # The last 3 (GHI) would have only reported via smtp.
4586 self.assertEqual(3, len(self.smtp_messages))
4587 self.assertEqual(0, len(G.messages))
4588 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004589 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004590 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004591 self.assertEqual(0, len(H.messages))
4592 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004593 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004594 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004595 self.assertEqual(0, len(I.messages))
4596 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004597 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004598 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004599
4600 # Now reload the configuration (simulate a HUP) to check the pipeline
4601 # comes out of disabled
4602 self.sched.reconfigure(self.config)
4603
Jesse Keatingcc67b122017-04-19 14:54:57 -07004604 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004605
4606 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004607 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004608 0, tenant.layout.pipelines['check']._consecutive_failures)
4609 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004610
4611 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4612 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4613 self.waitUntilSettled()
4614
4615 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004616 2, tenant.layout.pipelines['check']._consecutive_failures)
4617 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004618
4619 # J and K went back to gerrit
4620 self.assertEqual(1, len(J.messages))
4621 self.assertIn('Build failed.', J.messages[0])
4622 self.assertEqual(1, len(K.messages))
4623 self.assertIn('Build failed.', K.messages[0])
4624 # No more messages reported via smtp
4625 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004626
James E. Blaircc020532017-04-19 13:04:44 -07004627 @simple_layout('layouts/one-job-project.yaml')
4628 def test_one_job_project(self):
4629 "Test that queueing works with one job"
4630 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4631 'master', 'A')
4632 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4633 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004634 A.addApproval('Code-Review', 2)
4635 B.addApproval('Code-Review', 2)
4636 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4637 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004638 self.waitUntilSettled()
4639
4640 self.assertEqual(A.data['status'], 'MERGED')
4641 self.assertEqual(A.reported, 2)
4642 self.assertEqual(B.data['status'], 'MERGED')
4643 self.assertEqual(B.reported, 2)
4644
Paul Belanger32023542017-10-06 14:48:50 -04004645 def test_job_aborted(self):
4646 "Test that if a execute server aborts a job, it is run again"
4647 self.executor_server.hold_jobs_in_build = True
4648 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4649 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4650 self.waitUntilSettled()
4651
4652 self.executor_server.release('.*-merge')
4653 self.waitUntilSettled()
4654
4655 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004656
4657 # first abort
4658 self.builds[0].aborted = True
4659 self.executor_server.release('.*-test*')
4660 self.waitUntilSettled()
4661 self.assertEqual(len(self.builds), 1)
4662
4663 # second abort
4664 self.builds[0].aborted = True
4665 self.executor_server.release('.*-test*')
4666 self.waitUntilSettled()
4667 self.assertEqual(len(self.builds), 1)
4668
4669 # third abort
4670 self.builds[0].aborted = True
4671 self.executor_server.release('.*-test*')
4672 self.waitUntilSettled()
4673 self.assertEqual(len(self.builds), 1)
4674
4675 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004676 self.builds[0].aborted = True
4677 self.executor_server.release('.*-test*')
4678 self.waitUntilSettled()
4679 self.assertEqual(len(self.builds), 1)
4680
4681 self.executor_server.hold_jobs_in_build = False
4682 self.executor_server.release()
4683 self.waitUntilSettled()
4684
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004685 self.assertEqual(len(self.history), 7)
4686 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004687 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4688
Paul Belanger71d98172016-11-08 10:56:31 -05004689 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004690 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004691
Paul Belanger174a8272017-03-14 13:20:10 -04004692 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004693 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4694 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4695 self.waitUntilSettled()
4696
Paul Belanger174a8272017-03-14 13:20:10 -04004697 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004698 self.waitUntilSettled()
4699
4700 self.assertEqual(len(self.builds), 2)
4701 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004702 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004703 self.waitUntilSettled()
4704
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004705 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004706 self.assertEqual(len(self.builds), 1,
4707 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004708 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004709 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004710 self.waitUntilSettled()
4711
Paul Belanger174a8272017-03-14 13:20:10 -04004712 self.executor_server.hold_jobs_in_build = False
4713 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004714 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004715 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004716 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4717 self.assertEqual(A.reported, 1)
4718 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004719
James E. Blair15be0e12017-01-03 13:45:20 -08004720 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004721 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004722
4723 self.fake_nodepool.paused = True
4724 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004725 A.addApproval('Code-Review', 2)
4726 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004727 self.waitUntilSettled()
4728
4729 self.zk.client.stop()
4730 self.zk.client.start()
4731 self.fake_nodepool.paused = False
4732 self.waitUntilSettled()
4733
4734 self.assertEqual(A.data['status'], 'MERGED')
4735 self.assertEqual(A.reported, 2)
4736
James E. Blair6ab79e02017-01-06 10:10:17 -08004737 def test_nodepool_failure(self):
4738 "Test that jobs are reported after a nodepool failure"
4739
4740 self.fake_nodepool.paused = True
4741 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004742 A.addApproval('Code-Review', 2)
4743 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004744 self.waitUntilSettled()
4745
4746 req = self.fake_nodepool.getNodeRequests()[0]
4747 self.fake_nodepool.addFailRequest(req)
4748
4749 self.fake_nodepool.paused = False
4750 self.waitUntilSettled()
4751
4752 self.assertEqual(A.data['status'], 'NEW')
4753 self.assertEqual(A.reported, 2)
4754 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4755 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4756 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4757
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004758 def test_nodepool_priority(self):
4759 "Test that nodes are requested at the correct priority"
4760
4761 self.fake_nodepool.paused = True
4762
4763 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4764 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4765
4766 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4767 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4768
4769 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4770 C.addApproval('Code-Review', 2)
4771 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4772
4773 self.waitUntilSettled()
4774
4775 reqs = self.fake_nodepool.getNodeRequests()
4776
4777 # The requests come back sorted by oid. Since we have three requests
4778 # for the three changes each with a different priority.
4779 # Also they get a serial number based on order they were received
4780 # so the number on the endof the oid should map to order submitted.
4781
4782 # * gate first - high priority - change C
4783 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4784 self.assertEqual(reqs[0]['node_types'], ['label1'])
4785 # * check second - normal priority - change B
4786 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4787 self.assertEqual(reqs[1]['node_types'], ['label1'])
4788 # * post third - low priority - change A
4789 # additionally, the post job defined uses an ubuntu-xenial node,
4790 # so we include that check just as an extra verification
4791 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4792 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4793
4794 self.fake_nodepool.paused = False
4795 self.waitUntilSettled()
4796
James E. Blairfceaf412017-09-29 13:44:57 -07004797 @simple_layout('layouts/multiple-templates.yaml')
4798 def test_multiple_project_templates(self):
4799 # Test that applying multiple project templates to a project
4800 # doesn't alter them when used for a second project.
4801 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4802 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4803 self.waitUntilSettled()
4804
4805 build = self.getJobFromHistory('py27')
4806 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4807
James E. Blair29c77002017-10-05 14:56:35 -07004808 def test_pending_merge_in_reconfig(self):
4809 # Test that if we are waiting for an outstanding merge on
4810 # reconfiguration that we continue to do so.
4811 self.gearman_server.hold_merge_jobs_in_queue = True
4812 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4813 A.setMerged()
4814 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4815 self.waitUntilSettled()
4816 # Reconfigure while we still have an outstanding merge job
4817 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4818 None)
4819 self.waitUntilSettled()
4820 # Verify the merge job is still running and that the item is
4821 # in the pipeline
4822 self.assertEqual(len(self.sched.merger.jobs), 1)
4823 tenant = self.sched.abide.tenants.get('tenant-one')
4824 pipeline = tenant.layout.pipelines['post']
4825 self.assertEqual(len(pipeline.getAllItems()), 1)
4826 self.gearman_server.hold_merge_jobs_in_queue = False
4827 self.gearman_server.release()
4828 self.waitUntilSettled()
4829
Arieb6f068c2016-10-09 13:11:06 +03004830
James E. Blaira002b032017-04-18 10:35:48 -07004831class TestExecutor(ZuulTestCase):
4832 tenant_config_file = 'config/single-tenant/main.yaml'
4833
4834 def assertFinalState(self):
4835 # In this test, we expect to shut down in a non-final state,
4836 # so skip these checks.
4837 pass
4838
4839 def assertCleanShutdown(self):
4840 self.log.debug("Assert clean shutdown")
4841
4842 # After shutdown, make sure no jobs are running
4843 self.assertEqual({}, self.executor_server.job_workers)
4844
4845 # Make sure that git.Repo objects have been garbage collected.
4846 repos = []
4847 gc.collect()
4848 for obj in gc.get_objects():
4849 if isinstance(obj, git.Repo):
4850 self.log.debug("Leaked git repo object: %s" % repr(obj))
4851 repos.append(obj)
4852 self.assertEqual(len(repos), 0)
4853
4854 def test_executor_shutdown(self):
4855 "Test that the executor can shut down with jobs running"
4856
4857 self.executor_server.hold_jobs_in_build = True
4858 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004859 A.addApproval('Code-Review', 2)
4860 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004861 self.waitUntilSettled()
4862
4863
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004864class TestDependencyGraph(ZuulTestCase):
4865 tenant_config_file = 'config/dependency-graph/main.yaml'
4866
4867 def test_dependeny_graph_dispatch_jobs_once(self):
4868 "Test a job in a dependency graph is queued only once"
4869 # Job dependencies, starting with A
4870 # A
4871 # / \
4872 # B C
4873 # / \ / \
4874 # D F E
4875 # |
4876 # G
4877
4878 self.executor_server.hold_jobs_in_build = True
4879 change = self.fake_gerrit.addFakeChange(
4880 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004881 change.addApproval('Code-Review', 2)
4882 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004883
4884 self.waitUntilSettled()
4885 self.assertEqual([b.name for b in self.builds], ['A'])
4886
4887 self.executor_server.release('A')
4888 self.waitUntilSettled()
4889 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4890
4891 self.executor_server.release('B')
4892 self.waitUntilSettled()
4893 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4894
4895 self.executor_server.release('D')
4896 self.waitUntilSettled()
4897 self.assertEqual([b.name for b in self.builds], ['C'])
4898
4899 self.executor_server.release('C')
4900 self.waitUntilSettled()
4901 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4902
4903 self.executor_server.release('F')
4904 self.waitUntilSettled()
4905 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4906
4907 self.executor_server.release('G')
4908 self.waitUntilSettled()
4909 self.assertEqual([b.name for b in self.builds], ['E'])
4910
4911 self.executor_server.release('E')
4912 self.waitUntilSettled()
4913 self.assertEqual(len(self.builds), 0)
4914
4915 self.executor_server.hold_jobs_in_build = False
4916 self.executor_server.release()
4917 self.waitUntilSettled()
4918
4919 self.assertEqual(len(self.builds), 0)
4920 self.assertEqual(len(self.history), 7)
4921
4922 self.assertEqual(change.data['status'], 'MERGED')
4923 self.assertEqual(change.reported, 2)
4924
4925 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4926 "Test that a job waits till all dependencies are successful"
4927 # Job dependencies, starting with A
4928 # A
4929 # / \
4930 # B C*
4931 # / \ / \
4932 # D F E
4933 # |
4934 # G
4935
4936 self.executor_server.hold_jobs_in_build = True
4937 change = self.fake_gerrit.addFakeChange(
4938 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004939 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004940
4941 self.executor_server.failJob('C', change)
4942
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004943 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004944
4945 self.waitUntilSettled()
4946 self.assertEqual([b.name for b in self.builds], ['A'])
4947
4948 self.executor_server.release('A')
4949 self.waitUntilSettled()
4950 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4951
4952 self.executor_server.release('B')
4953 self.waitUntilSettled()
4954 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4955
4956 self.executor_server.release('D')
4957 self.waitUntilSettled()
4958 self.assertEqual([b.name for b in self.builds], ['C'])
4959
4960 self.executor_server.release('C')
4961 self.waitUntilSettled()
4962 self.assertEqual(len(self.builds), 0)
4963
4964 self.executor_server.hold_jobs_in_build = False
4965 self.executor_server.release()
4966 self.waitUntilSettled()
4967
4968 self.assertEqual(len(self.builds), 0)
4969 self.assertEqual(len(self.history), 4)
4970
4971 self.assertEqual(change.data['status'], 'NEW')
4972 self.assertEqual(change.reported, 2)
4973
4974
Arieb6f068c2016-10-09 13:11:06 +03004975class TestDuplicatePipeline(ZuulTestCase):
4976 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4977
4978 def test_duplicate_pipelines(self):
4979 "Test that a change matching multiple pipelines works"
4980
4981 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4982 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4983 self.waitUntilSettled()
4984
4985 self.assertHistory([
4986 dict(name='project-test1', result='SUCCESS', changes='1,1',
4987 pipeline='dup1'),
4988 dict(name='project-test1', result='SUCCESS', changes='1,1',
4989 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004990 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004991
4992 self.assertEqual(len(A.messages), 2)
4993
Paul Belangera46a3742016-11-09 14:23:03 -05004994 if 'dup1' in A.messages[0]:
4995 self.assertIn('dup1', A.messages[0])
4996 self.assertNotIn('dup2', A.messages[0])
4997 self.assertIn('project-test1', A.messages[0])
4998 self.assertIn('dup2', A.messages[1])
4999 self.assertNotIn('dup1', A.messages[1])
5000 self.assertIn('project-test1', A.messages[1])
5001 else:
5002 self.assertIn('dup1', A.messages[1])
5003 self.assertNotIn('dup2', A.messages[1])
5004 self.assertIn('project-test1', A.messages[1])
5005 self.assertIn('dup2', A.messages[0])
5006 self.assertNotIn('dup1', A.messages[0])
5007 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04005008
5009
Paul Belanger86085b32016-11-03 12:48:57 -04005010class TestSchedulerTemplatedProject(ZuulTestCase):
5011 tenant_config_file = 'config/templated-project/main.yaml'
5012
Paul Belanger174a8272017-03-14 13:20:10 -04005013 def test_job_from_templates_executed(self):
5014 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04005015
5016 A = self.fake_gerrit.addFakeChange(
5017 'org/templated-project', 'master', 'A')
5018 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5019 self.waitUntilSettled()
5020
5021 self.assertEqual(self.getJobFromHistory('project-test1').result,
5022 'SUCCESS')
5023 self.assertEqual(self.getJobFromHistory('project-test2').result,
5024 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04005025
5026 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04005027 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04005028
5029 A = self.fake_gerrit.addFakeChange(
5030 'org/layered-project', 'master', 'A')
5031 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5032 self.waitUntilSettled()
5033
5034 self.assertEqual(self.getJobFromHistory('project-test1').result,
5035 'SUCCESS')
5036 self.assertEqual(self.getJobFromHistory('project-test2').result,
5037 'SUCCESS')
5038 self.assertEqual(self.getJobFromHistory('layered-project-test3'
5039 ).result, 'SUCCESS')
5040 self.assertEqual(self.getJobFromHistory('layered-project-test4'
5041 ).result, 'SUCCESS')
5042 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
5043 ).result, 'SUCCESS')
5044 self.assertEqual(self.getJobFromHistory('project-test6').result,
5045 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08005046
James E. Blaire74f5712017-09-29 15:14:31 -07005047 def test_unimplied_branch_matchers(self):
5048 # This tests that there are no implied branch matchers added
5049 # by project templates.
5050 self.create_branch('org/layered-project', 'stable')
5051
5052 A = self.fake_gerrit.addFakeChange(
5053 'org/layered-project', 'stable', 'A')
5054
5055 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5056 self.waitUntilSettled()
5057
5058 self.assertEqual(self.getJobFromHistory('project-test1').result,
5059 'SUCCESS')
5060 print(self.getJobFromHistory('project-test1').
5061 parameters['zuul']['_inheritance_path'])
5062
5063 def test_implied_branch_matchers(self):
5064 # This tests that there is an implied branch matcher when a
5065 # template is used on an in-repo project pipeline definition.
5066 self.create_branch('untrusted-config', 'stable')
5067 self.fake_gerrit.addEvent(
5068 self.fake_gerrit.getFakeBranchCreatedEvent(
5069 'untrusted-config', 'stable'))
5070 self.waitUntilSettled()
5071
5072 A = self.fake_gerrit.addFakeChange(
5073 'untrusted-config', 'stable', 'A')
5074
5075 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5076 self.waitUntilSettled()
5077
5078 self.assertEqual(self.getJobFromHistory('project-test1').result,
5079 'SUCCESS')
5080 print(self.getJobFromHistory('project-test1').
5081 parameters['zuul']['_inheritance_path'])
5082
Adam Gandelman94a60062016-11-21 16:43:14 -08005083
5084class TestSchedulerSuccessURL(ZuulTestCase):
5085 tenant_config_file = 'config/success-url/main.yaml'
5086
5087 def test_success_url(self):
5088 "Ensure bad build params are ignored"
5089 self.sched.reconfigure(self.config)
5090 self.init_repo('org/docs')
5091
5092 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
5093 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5094 self.waitUntilSettled()
5095
5096 # Both builds ran: docs-draft-test + docs-draft-test2
5097 self.assertEqual(len(self.history), 2)
5098
5099 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08005100 for build in self.history:
5101 if build.name == 'docs-draft-test':
5102 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06005103 elif build.name == 'docs-draft-test2':
5104 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08005105
5106 # Two msgs: 'Starting...' + results
5107 self.assertEqual(len(self.smtp_messages), 2)
5108 body = self.smtp_messages[1]['body'].splitlines()
5109 self.assertEqual('Build succeeded.', body[0])
5110
5111 self.assertIn(
5112 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
5113 'docs-draft-test/%s/publish-docs/' % uuid,
5114 body[2])
5115
Paul Belanger174a8272017-03-14 13:20:10 -04005116 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08005117 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05005118 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
5119 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06005120 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08005121 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08005122
5123
Adam Gandelman4da00f62016-12-09 15:47:33 -08005124class TestSchedulerMerges(ZuulTestCase):
5125 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08005126
5127 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04005128 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08005129 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04005130 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08005131 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
5132 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
5133 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005134 A.addApproval('Code-Review', 2)
5135 B.addApproval('Code-Review', 2)
5136 C.addApproval('Code-Review', 2)
5137 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5138 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5139 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08005140 self.waitUntilSettled()
5141
5142 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07005143 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5144 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08005145 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005146 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08005147 repo_messages.reverse()
5148
Paul Belanger174a8272017-03-14 13:20:10 -04005149 self.executor_server.hold_jobs_in_build = False
5150 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08005151 self.waitUntilSettled()
5152
5153 return repo_messages
5154
5155 def _test_merge(self, mode):
5156 us_path = os.path.join(
5157 self.upstream_root, 'org/project-%s' % mode)
5158 expected_messages = [
5159 'initial commit',
5160 'add content from fixture',
5161 # the intermediate commits order is nondeterministic
5162 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
5163 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
5164 ]
5165 result = self._test_project_merge_mode(mode)
5166 self.assertEqual(result[:2], expected_messages[:2])
5167 self.assertEqual(result[-2:], expected_messages[-2:])
5168
5169 def test_project_merge_mode_merge(self):
5170 self._test_merge('merge')
5171
5172 def test_project_merge_mode_merge_resolve(self):
5173 self._test_merge('merge-resolve')
5174
5175 def test_project_merge_mode_cherrypick(self):
5176 expected_messages = [
5177 'initial commit',
5178 'add content from fixture',
5179 'A-1',
5180 'B-1',
5181 'C-1']
5182 result = self._test_project_merge_mode('cherry-pick')
5183 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005184
5185 def test_merge_branch(self):
5186 "Test that the right commits are on alternate branches"
5187 self.create_branch('org/project-merge-branches', 'mp')
5188
Paul Belanger174a8272017-03-14 13:20:10 -04005189 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005190 A = self.fake_gerrit.addFakeChange(
5191 'org/project-merge-branches', 'mp', 'A')
5192 B = self.fake_gerrit.addFakeChange(
5193 'org/project-merge-branches', 'mp', 'B')
5194 C = self.fake_gerrit.addFakeChange(
5195 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005196 A.addApproval('Code-Review', 2)
5197 B.addApproval('Code-Review', 2)
5198 C.addApproval('Code-Review', 2)
5199 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5200 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5201 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005202 self.waitUntilSettled()
5203
Paul Belanger174a8272017-03-14 13:20:10 -04005204 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005205 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005206 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005207 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005208 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005209 self.waitUntilSettled()
5210
5211 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005212 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005213 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5214 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005215 repo = git.Repo(path)
5216
James E. Blair247cab72017-07-20 16:52:36 -07005217 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005218 repo_messages.reverse()
5219 correct_messages = [
5220 'initial commit',
5221 'add content from fixture',
5222 'mp commit',
5223 'A-1', 'B-1', 'C-1']
5224 self.assertEqual(repo_messages, correct_messages)
5225
Paul Belanger174a8272017-03-14 13:20:10 -04005226 self.executor_server.hold_jobs_in_build = False
5227 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005228 self.waitUntilSettled()
5229
5230 def test_merge_multi_branch(self):
5231 "Test that dependent changes on multiple branches are merged"
5232 self.create_branch('org/project-merge-branches', 'mp')
5233
Paul Belanger174a8272017-03-14 13:20:10 -04005234 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005235 A = self.fake_gerrit.addFakeChange(
5236 'org/project-merge-branches', 'master', 'A')
5237 B = self.fake_gerrit.addFakeChange(
5238 'org/project-merge-branches', 'mp', 'B')
5239 C = self.fake_gerrit.addFakeChange(
5240 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005241 A.addApproval('Code-Review', 2)
5242 B.addApproval('Code-Review', 2)
5243 C.addApproval('Code-Review', 2)
5244 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5245 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5246 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005247 self.waitUntilSettled()
5248
5249 job_A = None
5250 for job in self.builds:
5251 if 'project-merge' in job.name:
5252 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005253
James E. Blair2a535672017-04-27 12:03:15 -07005254 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5255 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005256 repo = git.Repo(path)
5257 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005258 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005259 repo_messages.reverse()
5260 correct_messages = [
5261 'initial commit', 'add content from fixture', 'A-1']
5262 self.assertEqual(repo_messages, correct_messages)
5263
Paul Belanger174a8272017-03-14 13:20:10 -04005264 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005265 self.waitUntilSettled()
5266
5267 job_B = None
5268 for job in self.builds:
5269 if 'project-merge' in job.name:
5270 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005271
James E. Blair2a535672017-04-27 12:03:15 -07005272 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5273 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005274 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005275 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005276 repo_messages.reverse()
5277 correct_messages = [
5278 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5279 self.assertEqual(repo_messages, correct_messages)
5280
Paul Belanger174a8272017-03-14 13:20:10 -04005281 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005282 self.waitUntilSettled()
5283
5284 job_C = None
5285 for job in self.builds:
5286 if 'project-merge' in job.name:
5287 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005288
James E. Blair2a535672017-04-27 12:03:15 -07005289 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5290 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005291 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005292 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005293
5294 repo_messages.reverse()
5295 correct_messages = [
5296 'initial commit', 'add content from fixture',
5297 'A-1', 'C-1']
5298 # Ensure the right commits are in the history for this ref
5299 self.assertEqual(repo_messages, correct_messages)
5300
Paul Belanger174a8272017-03-14 13:20:10 -04005301 self.executor_server.hold_jobs_in_build = False
5302 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005303 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005304
5305
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005306class TestSemaphore(ZuulTestCase):
5307 tenant_config_file = 'config/semaphore/main.yaml'
5308
5309 def test_semaphore_one(self):
5310 "Test semaphores with max=1 (mutex)"
5311 tenant = self.sched.abide.tenants.get('tenant-one')
5312
5313 self.executor_server.hold_jobs_in_build = True
5314
5315 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5316 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5317 self.assertFalse('test-semaphore' in
5318 tenant.semaphore_handler.semaphores)
5319
5320 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5321 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5322 self.waitUntilSettled()
5323
5324 self.assertEqual(len(self.builds), 3)
5325 self.assertEqual(self.builds[0].name, 'project-test1')
5326 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5327 self.assertEqual(self.builds[2].name, 'project-test1')
5328
5329 self.executor_server.release('semaphore-one-test1')
5330 self.waitUntilSettled()
5331
5332 self.assertEqual(len(self.builds), 3)
5333 self.assertEqual(self.builds[0].name, 'project-test1')
5334 self.assertEqual(self.builds[1].name, 'project-test1')
5335 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5336 self.assertTrue('test-semaphore' in
5337 tenant.semaphore_handler.semaphores)
5338
5339 self.executor_server.release('semaphore-one-test2')
5340 self.waitUntilSettled()
5341
5342 self.assertEqual(len(self.builds), 3)
5343 self.assertEqual(self.builds[0].name, 'project-test1')
5344 self.assertEqual(self.builds[1].name, 'project-test1')
5345 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5346 self.assertTrue('test-semaphore' in
5347 tenant.semaphore_handler.semaphores)
5348
5349 self.executor_server.release('semaphore-one-test1')
5350 self.waitUntilSettled()
5351
5352 self.assertEqual(len(self.builds), 3)
5353 self.assertEqual(self.builds[0].name, 'project-test1')
5354 self.assertEqual(self.builds[1].name, 'project-test1')
5355 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5356 self.assertTrue('test-semaphore' in
5357 tenant.semaphore_handler.semaphores)
5358
5359 self.executor_server.release('semaphore-one-test2')
5360 self.waitUntilSettled()
5361
5362 self.assertEqual(len(self.builds), 2)
5363 self.assertEqual(self.builds[0].name, 'project-test1')
5364 self.assertEqual(self.builds[1].name, 'project-test1')
5365 self.assertFalse('test-semaphore' in
5366 tenant.semaphore_handler.semaphores)
5367
5368 self.executor_server.hold_jobs_in_build = False
5369 self.executor_server.release()
5370
5371 self.waitUntilSettled()
5372 self.assertEqual(len(self.builds), 0)
5373
5374 self.assertEqual(A.reported, 1)
5375 self.assertEqual(B.reported, 1)
5376 self.assertFalse('test-semaphore' in
5377 tenant.semaphore_handler.semaphores)
5378
5379 def test_semaphore_two(self):
5380 "Test semaphores with max>1"
5381 tenant = self.sched.abide.tenants.get('tenant-one')
5382
5383 self.executor_server.hold_jobs_in_build = True
5384 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5385 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5386 self.assertFalse('test-semaphore-two' in
5387 tenant.semaphore_handler.semaphores)
5388
5389 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5390 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5391 self.waitUntilSettled()
5392
5393 self.assertEqual(len(self.builds), 4)
5394 self.assertEqual(self.builds[0].name, 'project-test1')
5395 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5396 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5397 self.assertEqual(self.builds[3].name, 'project-test1')
5398 self.assertTrue('test-semaphore-two' in
5399 tenant.semaphore_handler.semaphores)
5400 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5401 'test-semaphore-two', [])), 2)
5402
5403 self.executor_server.release('semaphore-two-test1')
5404 self.waitUntilSettled()
5405
5406 self.assertEqual(len(self.builds), 4)
5407 self.assertEqual(self.builds[0].name, 'project-test1')
5408 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5409 self.assertEqual(self.builds[2].name, 'project-test1')
5410 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
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', [])), 2)
5415
5416 self.executor_server.release('semaphore-two-test2')
5417 self.waitUntilSettled()
5418
5419 self.assertEqual(len(self.builds), 4)
5420 self.assertEqual(self.builds[0].name, 'project-test1')
5421 self.assertEqual(self.builds[1].name, 'project-test1')
5422 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5423 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5424 self.assertTrue('test-semaphore-two' in
5425 tenant.semaphore_handler.semaphores)
5426 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5427 'test-semaphore-two', [])), 2)
5428
5429 self.executor_server.release('semaphore-two-test1')
5430 self.waitUntilSettled()
5431
5432 self.assertEqual(len(self.builds), 3)
5433 self.assertEqual(self.builds[0].name, 'project-test1')
5434 self.assertEqual(self.builds[1].name, 'project-test1')
5435 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5436 self.assertTrue('test-semaphore-two' in
5437 tenant.semaphore_handler.semaphores)
5438 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5439 'test-semaphore-two', [])), 1)
5440
5441 self.executor_server.release('semaphore-two-test2')
5442 self.waitUntilSettled()
5443
5444 self.assertEqual(len(self.builds), 2)
5445 self.assertEqual(self.builds[0].name, 'project-test1')
5446 self.assertEqual(self.builds[1].name, 'project-test1')
5447 self.assertFalse('test-semaphore-two' in
5448 tenant.semaphore_handler.semaphores)
5449
5450 self.executor_server.hold_jobs_in_build = False
5451 self.executor_server.release()
5452
5453 self.waitUntilSettled()
5454 self.assertEqual(len(self.builds), 0)
5455
5456 self.assertEqual(A.reported, 1)
5457 self.assertEqual(B.reported, 1)
5458
5459 def test_semaphore_abandon(self):
5460 "Test abandon with job semaphores"
5461 self.executor_server.hold_jobs_in_build = True
5462 tenant = self.sched.abide.tenants.get('tenant-one')
5463 check_pipeline = tenant.layout.pipelines['check']
5464
5465 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5466 self.assertFalse('test-semaphore' in
5467 tenant.semaphore_handler.semaphores)
5468
5469 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5470 self.waitUntilSettled()
5471
5472 self.assertTrue('test-semaphore' in
5473 tenant.semaphore_handler.semaphores)
5474
5475 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5476 self.waitUntilSettled()
5477
5478 # The check pipeline should be empty
5479 items = check_pipeline.getAllItems()
5480 self.assertEqual(len(items), 0)
5481
5482 # The semaphore should be released
5483 self.assertFalse('test-semaphore' in
5484 tenant.semaphore_handler.semaphores)
5485
5486 self.executor_server.hold_jobs_in_build = False
5487 self.executor_server.release()
5488 self.waitUntilSettled()
5489
Tobias Henkel0f714002017-06-30 23:30:52 +02005490 def test_semaphore_new_patchset(self):
5491 "Test new patchset with job semaphores"
5492 self.executor_server.hold_jobs_in_build = True
5493 tenant = self.sched.abide.tenants.get('tenant-one')
5494 check_pipeline = tenant.layout.pipelines['check']
5495
5496 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5497 self.assertFalse('test-semaphore' in
5498 tenant.semaphore_handler.semaphores)
5499
5500 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5501 self.waitUntilSettled()
5502
5503 self.assertTrue('test-semaphore' in
5504 tenant.semaphore_handler.semaphores)
5505 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5506 self.assertEqual(len(semaphore), 1)
5507
5508 A.addPatchset()
5509 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5510 self.waitUntilSettled()
5511
5512 self.assertTrue('test-semaphore' in
5513 tenant.semaphore_handler.semaphores)
5514 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5515 self.assertEqual(len(semaphore), 1)
5516
5517 items = check_pipeline.getAllItems()
5518 self.assertEqual(items[0].change.number, '1')
5519 self.assertEqual(items[0].change.patchset, '2')
5520 self.assertTrue(items[0].live)
5521
5522 self.executor_server.hold_jobs_in_build = False
5523 self.executor_server.release()
5524 self.waitUntilSettled()
5525
5526 # The semaphore should be released
5527 self.assertFalse('test-semaphore' in
5528 tenant.semaphore_handler.semaphores)
5529
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005530 def test_semaphore_reconfigure(self):
5531 "Test reconfigure with job semaphores"
5532 self.executor_server.hold_jobs_in_build = True
5533 tenant = self.sched.abide.tenants.get('tenant-one')
5534 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5535 self.assertFalse('test-semaphore' in
5536 tenant.semaphore_handler.semaphores)
5537
5538 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5539 self.waitUntilSettled()
5540
5541 self.assertTrue('test-semaphore' in
5542 tenant.semaphore_handler.semaphores)
5543
5544 # reconfigure without layout change
5545 self.sched.reconfigure(self.config)
5546 self.waitUntilSettled()
5547 tenant = self.sched.abide.tenants.get('tenant-one')
5548
5549 # semaphore still must be held
5550 self.assertTrue('test-semaphore' in
5551 tenant.semaphore_handler.semaphores)
5552
5553 self.commitConfigUpdate(
5554 'common-config',
5555 'config/semaphore/zuul-reconfiguration.yaml')
5556 self.sched.reconfigure(self.config)
5557 self.waitUntilSettled()
5558 tenant = self.sched.abide.tenants.get('tenant-one')
5559
5560 self.executor_server.release('project-test1')
5561 self.waitUntilSettled()
5562
5563 # There should be no builds anymore
5564 self.assertEqual(len(self.builds), 0)
5565
5566 # The semaphore should be released
5567 self.assertFalse('test-semaphore' in
5568 tenant.semaphore_handler.semaphores)
5569
5570
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005571class TestSemaphoreMultiTenant(ZuulTestCase):
5572 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5573
5574 def test_semaphore_tenant_isolation(self):
5575 "Test semaphores in multiple tenants"
5576
5577 self.waitUntilSettled()
5578 tenant_one = self.sched.abide.tenants.get('tenant-one')
5579 tenant_two = self.sched.abide.tenants.get('tenant-two')
5580
5581 self.executor_server.hold_jobs_in_build = True
5582 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5583 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5584 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5585 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5586 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5587 self.assertFalse('test-semaphore' in
5588 tenant_one.semaphore_handler.semaphores)
5589 self.assertFalse('test-semaphore' in
5590 tenant_two.semaphore_handler.semaphores)
5591
5592 # add patches to project1 of tenant-one
5593 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5594 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5595 self.waitUntilSettled()
5596
5597 # one build of project1-test1 must run
5598 # semaphore of tenant-one must be acquired once
5599 # semaphore of tenant-two must not be acquired
5600 self.assertEqual(len(self.builds), 1)
5601 self.assertEqual(self.builds[0].name, 'project1-test1')
5602 self.assertTrue('test-semaphore' in
5603 tenant_one.semaphore_handler.semaphores)
5604 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5605 'test-semaphore', [])), 1)
5606 self.assertFalse('test-semaphore' in
5607 tenant_two.semaphore_handler.semaphores)
5608
5609 # add patches to project2 of tenant-two
5610 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5611 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5612 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5613 self.waitUntilSettled()
5614
5615 # one build of project1-test1 must run
5616 # two builds of project2-test1 must run
5617 # semaphore of tenant-one must be acquired once
5618 # semaphore of tenant-two must be acquired twice
5619 self.assertEqual(len(self.builds), 3)
5620 self.assertEqual(self.builds[0].name, 'project1-test1')
5621 self.assertEqual(self.builds[1].name, 'project2-test1')
5622 self.assertEqual(self.builds[2].name, 'project2-test1')
5623 self.assertTrue('test-semaphore' in
5624 tenant_one.semaphore_handler.semaphores)
5625 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5626 'test-semaphore', [])), 1)
5627 self.assertTrue('test-semaphore' in
5628 tenant_two.semaphore_handler.semaphores)
5629 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5630 'test-semaphore', [])), 2)
5631
5632 self.executor_server.release('project1-test1')
5633 self.waitUntilSettled()
5634
5635 # one build of project1-test1 must run
5636 # two builds of project2-test1 must run
5637 # semaphore of tenant-one must be acquired once
5638 # semaphore of tenant-two must be acquired twice
5639 self.assertEqual(len(self.builds), 3)
5640 self.assertEqual(self.builds[0].name, 'project2-test1')
5641 self.assertEqual(self.builds[1].name, 'project2-test1')
5642 self.assertEqual(self.builds[2].name, 'project1-test1')
5643 self.assertTrue('test-semaphore' in
5644 tenant_one.semaphore_handler.semaphores)
5645 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5646 'test-semaphore', [])), 1)
5647 self.assertTrue('test-semaphore' in
5648 tenant_two.semaphore_handler.semaphores)
5649 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5650 'test-semaphore', [])), 2)
5651
5652 self.executor_server.release('project2-test1')
5653 self.waitUntilSettled()
5654
5655 # one build of project1-test1 must run
5656 # one build of project2-test1 must run
5657 # semaphore of tenant-one must be acquired once
5658 # semaphore of tenant-two must be acquired once
5659 self.assertEqual(len(self.builds), 2)
5660 self.assertTrue('test-semaphore' in
5661 tenant_one.semaphore_handler.semaphores)
5662 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5663 'test-semaphore', [])), 1)
5664 self.assertTrue('test-semaphore' in
5665 tenant_two.semaphore_handler.semaphores)
5666 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5667 'test-semaphore', [])), 1)
5668
5669 self.executor_server.hold_jobs_in_build = False
5670 self.executor_server.release()
5671
5672 self.waitUntilSettled()
5673
5674 # no build must run
5675 # semaphore of tenant-one must not be acquired
5676 # semaphore of tenant-two must not be acquired
5677 self.assertEqual(len(self.builds), 0)
5678 self.assertFalse('test-semaphore' in
5679 tenant_one.semaphore_handler.semaphores)
5680 self.assertFalse('test-semaphore' in
5681 tenant_two.semaphore_handler.semaphores)
5682
5683 self.assertEqual(A.reported, 1)
5684 self.assertEqual(B.reported, 1)
5685
5686
5687class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005688 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005689 tenant_config_file = 'config/in-repo/main.yaml'
5690
5691 def test_semaphore_in_repo(self):
5692 "Test semaphores in repo config"
5693
5694 # This tests dynamic semaphore handling in project repos. The semaphore
5695 # max value should not be evaluated dynamically but must be updated
5696 # after the change lands.
5697
5698 self.waitUntilSettled()
5699 tenant = self.sched.abide.tenants.get('tenant-one')
5700
5701 in_repo_conf = textwrap.dedent(
5702 """
5703 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005704 name: project-test1
5705
5706 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005707 name: project-test2
5708 semaphore: test-semaphore
5709
5710 - project:
5711 name: org/project
5712 tenant-one-gate:
5713 jobs:
5714 - project-test2
5715
5716 # the max value in dynamic layout must be ignored
5717 - semaphore:
5718 name: test-semaphore
5719 max: 2
5720 """)
5721
5722 in_repo_playbook = textwrap.dedent(
5723 """
5724 - hosts: all
5725 tasks: []
5726 """)
5727
5728 file_dict = {'.zuul.yaml': in_repo_conf,
5729 'playbooks/project-test2.yaml': in_repo_playbook}
5730 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5731 files=file_dict)
5732 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5733 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5734 B.setDependsOn(A, 1)
5735 C.setDependsOn(A, 1)
5736
5737 self.executor_server.hold_jobs_in_build = True
5738
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005739 A.addApproval('Code-Review', 2)
5740 B.addApproval('Code-Review', 2)
5741 C.addApproval('Code-Review', 2)
5742 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5743 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5744 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005745 self.waitUntilSettled()
5746
5747 # check that the layout in a queue item still has max value of 1
5748 # for test-semaphore
5749 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5750 queue = None
5751 for queue_candidate in pipeline.queues:
5752 if queue_candidate.name == 'org/project':
5753 queue = queue_candidate
5754 break
5755 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005756 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005757 dynamic_test_semaphore = \
5758 item_dynamic_layout.semaphores.get('test-semaphore')
5759 self.assertEqual(dynamic_test_semaphore.max, 1)
5760
5761 # one build must be in queue, one semaphores acquired
5762 self.assertEqual(len(self.builds), 1)
5763 self.assertEqual(self.builds[0].name, 'project-test2')
5764 self.assertTrue('test-semaphore' in
5765 tenant.semaphore_handler.semaphores)
5766 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5767 'test-semaphore', [])), 1)
5768
5769 self.executor_server.release('project-test2')
5770 self.waitUntilSettled()
5771
5772 # change A must be merged
5773 self.assertEqual(A.data['status'], 'MERGED')
5774 self.assertEqual(A.reported, 2)
5775
5776 # send change-merged event as the gerrit mock doesn't send it
5777 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5778 self.waitUntilSettled()
5779
5780 # now that change A was merged, the new semaphore max must be effective
5781 tenant = self.sched.abide.tenants.get('tenant-one')
5782 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5783
5784 # two builds must be in queue, two semaphores acquired
5785 self.assertEqual(len(self.builds), 2)
5786 self.assertEqual(self.builds[0].name, 'project-test2')
5787 self.assertEqual(self.builds[1].name, 'project-test2')
5788 self.assertTrue('test-semaphore' in
5789 tenant.semaphore_handler.semaphores)
5790 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5791 'test-semaphore', [])), 2)
5792
5793 self.executor_server.release('project-test2')
5794 self.waitUntilSettled()
5795
5796 self.assertEqual(len(self.builds), 0)
5797 self.assertFalse('test-semaphore' in
5798 tenant.semaphore_handler.semaphores)
5799
5800 self.executor_server.hold_jobs_in_build = False
5801 self.executor_server.release()
5802
5803 self.waitUntilSettled()
5804 self.assertEqual(len(self.builds), 0)
5805
5806 self.assertEqual(A.reported, 2)
5807 self.assertEqual(B.reported, 2)
5808 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005809
5810
5811class TestSchedulerBranchMatcher(ZuulTestCase):
5812
5813 @simple_layout('layouts/matcher-test.yaml')
5814 def test_job_branch_ignored(self):
5815 '''
5816 Test that branch matching logic works.
5817
5818 The 'ignore-branch' job has a branch matcher that is supposed to
5819 match every branch except for the 'featureA' branch, so it should
5820 not be run on a change to that branch.
5821 '''
5822 self.create_branch('org/project', 'featureA')
5823 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5824 A.addApproval('Code-Review', 2)
5825 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5826 self.waitUntilSettled()
5827 self.printHistory()
5828 self.assertEqual(self.getJobFromHistory('project-test1').result,
5829 'SUCCESS')
5830 self.assertJobNotInHistory('ignore-branch')
5831 self.assertEqual(A.data['status'], 'MERGED')
5832 self.assertEqual(A.reported, 2,
5833 "A should report start and success")
5834 self.assertIn('gate', A.messages[1],
5835 "A should transit gate")