blob: ab0d0fd3e23605e7c66567eaa0eef0a4cb786c33 [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. Blair412e5582013-04-22 15:50:12 -0700122
James E. Blair2ef29e92017-07-21 15:25:05 -0700123 for build in self.history:
124 self.assertTrue(build.parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -0700125
James E. Blair3cb10702013-08-24 08:56:03 -0700126 def test_initial_pipeline_gauges(self):
127 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -0700128 self.assertReportedStat('zuul.pipeline.gate.current_changes',
129 value='0|g')
130 self.assertReportedStat('zuul.pipeline.check.current_changes',
131 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -0700132
James E. Blair34776ee2016-08-25 13:53:54 -0700133 def test_job_branch(self):
134 "Test the correct variant of a job runs on a branch"
135 self.create_branch('org/project', 'stable')
136 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200137 A.addApproval('Code-Review', 2)
138 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair34776ee2016-08-25 13:53:54 -0700139 self.waitUntilSettled()
140 self.assertEqual(self.getJobFromHistory('project-test1').result,
141 'SUCCESS')
142 self.assertEqual(self.getJobFromHistory('project-test2').result,
143 'SUCCESS')
144 self.assertEqual(A.data['status'], 'MERGED')
145 self.assertEqual(A.reported, 2,
146 "A should report start and success")
147 self.assertIn('gate', A.messages[1],
148 "A should transit gate")
149 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700150 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700151
James E. Blairb0fcae42012-07-17 11:12:10 -0700152 def test_parallel_changes(self):
153 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700154
Paul Belanger174a8272017-03-14 13:20:10 -0400155 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700156 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
157 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
158 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200159 A.addApproval('Code-Review', 2)
160 B.addApproval('Code-Review', 2)
161 C.addApproval('Code-Review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700162
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200163 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
164 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
165 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700166
167 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400168 self.assertEqual(len(self.builds), 1)
169 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700171
Paul Belanger174a8272017-03-14 13:20:10 -0400172 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700173 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(len(self.builds), 3)
175 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700176 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400177 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700178 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400179 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700180 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700181
Paul Belanger174a8272017-03-14 13:20:10 -0400182 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700183 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400184 self.assertEqual(len(self.builds), 5)
185 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700186 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400187 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700188 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700189
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400190 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700191 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400192 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700193 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700194
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400195 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700196 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700197
Paul Belanger174a8272017-03-14 13:20:10 -0400198 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700199 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400200 self.assertEqual(len(self.builds), 6)
201 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700202 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400203 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700204 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700205
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400206 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700207 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400208 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700209 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700210
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400211 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700212 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400213 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700214 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700215
Paul Belanger174a8272017-03-14 13:20:10 -0400216 self.executor_server.hold_jobs_in_build = False
217 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700218 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400219 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700220
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400221 self.assertEqual(len(self.history), 9)
222 self.assertEqual(A.data['status'], 'MERGED')
223 self.assertEqual(B.data['status'], 'MERGED')
224 self.assertEqual(C.data['status'], 'MERGED')
225 self.assertEqual(A.reported, 2)
226 self.assertEqual(B.reported, 2)
227 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700228
229 def test_failed_changes(self):
230 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400231 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700232
James E. Blairb02a3bb2012-07-30 17:49:55 -0700233 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
234 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200235 A.addApproval('Code-Review', 2)
236 B.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700237
Paul Belanger174a8272017-03-14 13:20:10 -0400238 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700239
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200240 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
241 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700242 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700243 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400244
Paul Belanger174a8272017-03-14 13:20:10 -0400245 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400246 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700247 # A/project-merge is complete
248 self.assertBuilds([
249 dict(name='project-test1', changes='1,1'),
250 dict(name='project-test2', changes='1,1'),
251 dict(name='project-merge', changes='1,1 2,1'),
252 ])
James E. Blaire2819012013-06-28 17:17:26 -0400253
Paul Belanger174a8272017-03-14 13:20:10 -0400254 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400255 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700256 # A/project-merge is complete
257 # B/project-merge is complete
258 self.assertBuilds([
259 dict(name='project-test1', changes='1,1'),
260 dict(name='project-test2', changes='1,1'),
261 dict(name='project-test1', changes='1,1 2,1'),
262 dict(name='project-test2', changes='1,1 2,1'),
263 ])
264
265 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400266 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700267 self.builds[0].release()
268 self.waitUntilSettled()
269
270 self.orderedRelease()
271 self.assertHistory([
272 dict(name='project-merge', result='SUCCESS', changes='1,1'),
273 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
274 dict(name='project-test1', result='FAILURE', changes='1,1'),
275 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
276 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
277 dict(name='project-test2', result='SUCCESS', changes='1,1'),
278 dict(name='project-merge', result='SUCCESS', changes='2,1'),
279 dict(name='project-test1', result='SUCCESS', changes='2,1'),
280 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700281 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700282
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400283 self.assertEqual(A.data['status'], 'NEW')
284 self.assertEqual(B.data['status'], 'MERGED')
285 self.assertEqual(A.reported, 2)
286 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700287
288 def test_independent_queues(self):
289 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700290
Paul Belanger174a8272017-03-14 13:20:10 -0400291 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900292 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700293 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
294 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200295 A.addApproval('Code-Review', 2)
296 B.addApproval('Code-Review', 2)
297 C.addApproval('Code-Review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700298
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200299 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800300 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700301
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200302 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800303 self.waitUntilSettled()
304
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200305 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700306 self.waitUntilSettled()
307
308 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700309 self.assertBuilds([
310 dict(name='project-merge', changes='1,1'),
311 dict(name='project-merge', changes='2,1'),
312 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700313
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700314 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700315 self.builds[0].release()
316 self.waitUntilSettled()
317 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700318 self.waitUntilSettled()
319 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400320 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700321 self.waitUntilSettled()
322
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700323 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700324 self.assertBuilds([
325 dict(name='project-test1', changes='1,1'),
326 dict(name='project-test2', changes='1,1'),
327 dict(name='project-test1', changes='2,1'),
328 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500329 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700330 dict(name='project-test1', changes='2,1 3,1'),
331 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800332 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700333 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700334
James E. Blair5f0d49e2016-08-19 09:36:50 -0700335 self.orderedRelease()
336 self.assertHistory([
337 dict(name='project-merge', result='SUCCESS', changes='1,1'),
338 dict(name='project-merge', result='SUCCESS', changes='2,1'),
339 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
340 dict(name='project-test1', result='SUCCESS', changes='1,1'),
341 dict(name='project-test2', result='SUCCESS', changes='1,1'),
342 dict(name='project-test1', result='SUCCESS', changes='2,1'),
343 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500344 dict(
345 name='project1-project2-integration',
346 result='SUCCESS',
347 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700348 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
349 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800350 dict(name='project1-project2-integration',
351 result='SUCCESS',
352 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700353 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700354
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400355 self.assertEqual(A.data['status'], 'MERGED')
356 self.assertEqual(B.data['status'], 'MERGED')
357 self.assertEqual(C.data['status'], 'MERGED')
358 self.assertEqual(A.reported, 2)
359 self.assertEqual(B.reported, 2)
360 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700361
362 def test_failed_change_at_head(self):
363 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700364
Paul Belanger174a8272017-03-14 13:20:10 -0400365 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700366 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
367 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
368 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200369 A.addApproval('Code-Review', 2)
370 B.addApproval('Code-Review', 2)
371 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700372
Paul Belanger174a8272017-03-14 13:20:10 -0400373 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700374
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200375 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
376 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
377 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700378
379 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700380
James E. Blairb536ecc2016-08-31 10:11:42 -0700381 self.assertBuilds([
382 dict(name='project-merge', changes='1,1'),
383 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700384
Paul Belanger174a8272017-03-14 13:20:10 -0400385 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700386 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400387 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700388 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400389 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700390 self.waitUntilSettled()
391
James E. Blairb536ecc2016-08-31 10:11:42 -0700392 self.assertBuilds([
393 dict(name='project-test1', changes='1,1'),
394 dict(name='project-test2', changes='1,1'),
395 dict(name='project-test1', changes='1,1 2,1'),
396 dict(name='project-test2', changes='1,1 2,1'),
397 dict(name='project-test1', changes='1,1 2,1 3,1'),
398 dict(name='project-test2', changes='1,1 2,1 3,1'),
399 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700400
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400401 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700402 self.waitUntilSettled()
403
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400404 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700405 self.assertBuilds([
406 dict(name='project-test2', changes='1,1'),
407 dict(name='project-merge', changes='2,1'),
408 ])
409 # Unordered history comparison because the aborts can finish
410 # in any order.
411 self.assertHistory([
412 dict(name='project-merge', result='SUCCESS',
413 changes='1,1'),
414 dict(name='project-merge', result='SUCCESS',
415 changes='1,1 2,1'),
416 dict(name='project-merge', result='SUCCESS',
417 changes='1,1 2,1 3,1'),
418 dict(name='project-test1', result='FAILURE',
419 changes='1,1'),
420 dict(name='project-test1', result='ABORTED',
421 changes='1,1 2,1'),
422 dict(name='project-test2', result='ABORTED',
423 changes='1,1 2,1'),
424 dict(name='project-test1', result='ABORTED',
425 changes='1,1 2,1 3,1'),
426 dict(name='project-test2', result='ABORTED',
427 changes='1,1 2,1 3,1'),
428 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700429
Paul Belanger174a8272017-03-14 13:20:10 -0400430 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700431 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400432 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700433 self.waitUntilSettled()
434 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700435
James E. Blairb536ecc2016-08-31 10:11:42 -0700436 self.assertBuilds([])
437 self.assertHistory([
438 dict(name='project-merge', result='SUCCESS',
439 changes='1,1'),
440 dict(name='project-merge', result='SUCCESS',
441 changes='1,1 2,1'),
442 dict(name='project-merge', result='SUCCESS',
443 changes='1,1 2,1 3,1'),
444 dict(name='project-test1', result='FAILURE',
445 changes='1,1'),
446 dict(name='project-test1', result='ABORTED',
447 changes='1,1 2,1'),
448 dict(name='project-test2', result='ABORTED',
449 changes='1,1 2,1'),
450 dict(name='project-test1', result='ABORTED',
451 changes='1,1 2,1 3,1'),
452 dict(name='project-test2', result='ABORTED',
453 changes='1,1 2,1 3,1'),
454 dict(name='project-merge', result='SUCCESS',
455 changes='2,1'),
456 dict(name='project-merge', result='SUCCESS',
457 changes='2,1 3,1'),
458 dict(name='project-test2', result='SUCCESS',
459 changes='1,1'),
460 dict(name='project-test1', result='SUCCESS',
461 changes='2,1'),
462 dict(name='project-test2', result='SUCCESS',
463 changes='2,1'),
464 dict(name='project-test1', result='SUCCESS',
465 changes='2,1 3,1'),
466 dict(name='project-test2', result='SUCCESS',
467 changes='2,1 3,1'),
468 ], ordered=False)
469
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400470 self.assertEqual(A.data['status'], 'NEW')
471 self.assertEqual(B.data['status'], 'MERGED')
472 self.assertEqual(C.data['status'], 'MERGED')
473 self.assertEqual(A.reported, 2)
474 self.assertEqual(B.reported, 2)
475 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700476
James E. Blair0aac4872013-08-23 14:02:38 -0700477 def test_failed_change_in_middle(self):
478 "Test a failed change in the middle of the queue"
479
Paul Belanger174a8272017-03-14 13:20:10 -0400480 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700481 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
482 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
483 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200484 A.addApproval('Code-Review', 2)
485 B.addApproval('Code-Review', 2)
486 C.addApproval('Code-Review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700487
Paul Belanger174a8272017-03-14 13:20:10 -0400488 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700489
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200490 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
491 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
492 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700493
494 self.waitUntilSettled()
495
Paul Belanger174a8272017-03-14 13:20:10 -0400496 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700497 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400498 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700499 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400500 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700501 self.waitUntilSettled()
502
503 self.assertEqual(len(self.builds), 6)
504 self.assertEqual(self.builds[0].name, 'project-test1')
505 self.assertEqual(self.builds[1].name, 'project-test2')
506 self.assertEqual(self.builds[2].name, 'project-test1')
507 self.assertEqual(self.builds[3].name, 'project-test2')
508 self.assertEqual(self.builds[4].name, 'project-test1')
509 self.assertEqual(self.builds[5].name, 'project-test2')
510
511 self.release(self.builds[2])
512 self.waitUntilSettled()
513
James E. Blair972e3c72013-08-29 12:04:55 -0700514 # project-test1 and project-test2 for A
515 # project-test2 for B
516 # project-merge for C (without B)
517 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700518 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
519
Paul Belanger174a8272017-03-14 13:20:10 -0400520 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700521 self.waitUntilSettled()
522
523 # project-test1 and project-test2 for A
524 # project-test2 for B
525 # project-test1 and project-test2 for C
526 self.assertEqual(len(self.builds), 5)
527
Paul Belanger2e2a0492016-10-30 18:33:37 -0400528 tenant = self.sched.abide.tenants.get('tenant-one')
529 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700530 builds = items[0].current_build_set.getBuilds()
531 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
532 self.assertEqual(self.countJobResults(builds, None), 2)
533 builds = items[1].current_build_set.getBuilds()
534 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
535 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
536 self.assertEqual(self.countJobResults(builds, None), 1)
537 builds = items[2].current_build_set.getBuilds()
538 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700539 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700540
Paul Belanger174a8272017-03-14 13:20:10 -0400541 self.executor_server.hold_jobs_in_build = False
542 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700543 self.waitUntilSettled()
544
545 self.assertEqual(len(self.builds), 0)
546 self.assertEqual(len(self.history), 12)
547 self.assertEqual(A.data['status'], 'MERGED')
548 self.assertEqual(B.data['status'], 'NEW')
549 self.assertEqual(C.data['status'], 'MERGED')
550 self.assertEqual(A.reported, 2)
551 self.assertEqual(B.reported, 2)
552 self.assertEqual(C.reported, 2)
553
James E. Blaird466dc42012-07-31 10:42:56 -0700554 def test_failed_change_at_head_with_queue(self):
555 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700556
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700557 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700558 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
559 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
560 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200561 A.addApproval('Code-Review', 2)
562 B.addApproval('Code-Review', 2)
563 C.addApproval('Code-Review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700564
Paul Belanger174a8272017-03-14 13:20:10 -0400565 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700566
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200567 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
568 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
569 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700570
571 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700572 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400573 self.assertEqual(len(self.builds), 0)
574 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700575 self.assertEqual(queue[0].name, b'executor:execute')
576 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800577 self.assertEqual(job_args['job'], 'project-merge')
578 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700579
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700580 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700581 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700582 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700583 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700584 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700585 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700586 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700587
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400588 self.assertEqual(len(self.builds), 0)
589 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800590
591 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700592 json.loads(queue[0].arguments.decode('utf8'))['job'],
593 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800594 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700595 json.loads(queue[1].arguments.decode('utf8'))['job'],
596 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800597 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700598 json.loads(queue[2].arguments.decode('utf8'))['job'],
599 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800600 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700601 json.loads(queue[3].arguments.decode('utf8'))['job'],
602 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800603 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700604 json.loads(queue[4].arguments.decode('utf8'))['job'],
605 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800606 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700607 json.loads(queue[5].arguments.decode('utf8'))['job'],
608 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700609
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700610 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700611 self.waitUntilSettled()
612
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400613 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700614 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400615 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
616 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700617
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700618 self.gearman_server.hold_jobs_in_queue = False
619 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700620 self.waitUntilSettled()
621
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400622 self.assertEqual(len(self.builds), 0)
623 self.assertEqual(len(self.history), 11)
624 self.assertEqual(A.data['status'], 'NEW')
625 self.assertEqual(B.data['status'], 'MERGED')
626 self.assertEqual(C.data['status'], 'MERGED')
627 self.assertEqual(A.reported, 2)
628 self.assertEqual(B.reported, 2)
629 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700630
James E. Blairce8a2132016-05-19 15:21:52 -0700631 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400632 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700633 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200634 A.addApproval('Code-Review', 2)
635 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700636 self.waitUntilSettled()
637 time.sleep(2)
638
James E. Blairae0f23c2017-09-13 10:55:15 -0600639 data = json.loads(self.sched.formatStatusJSON('tenant-one'))
James E. Blairce8a2132016-05-19 15:21:52 -0700640 found_job = None
641 for pipeline in data['pipelines']:
642 if pipeline['name'] != 'gate':
643 continue
644 for queue in pipeline['change_queues']:
645 for head in queue['heads']:
646 for item in head:
647 for job in item['jobs']:
648 if job['name'] == 'project-merge':
649 found_job = job
650 break
651
652 self.assertIsNotNone(found_job)
653 if iteration == 1:
654 self.assertIsNotNone(found_job['estimated_time'])
655 self.assertIsNone(found_job['remaining_time'])
656 else:
657 self.assertIsNotNone(found_job['estimated_time'])
658 self.assertTrue(found_job['estimated_time'] >= 2)
659 self.assertIsNotNone(found_job['remaining_time'])
660
Paul Belanger174a8272017-03-14 13:20:10 -0400661 self.executor_server.hold_jobs_in_build = False
662 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700663 self.waitUntilSettled()
664
665 def test_time_database(self):
666 "Test the time database"
667
668 self._test_time_database(1)
669 self._test_time_database(2)
670
James E. Blairfef71632013-09-23 11:15:47 -0700671 def test_two_failed_changes_at_head(self):
672 "Test that changes are reparented correctly if 2 fail at head"
673
Paul Belanger174a8272017-03-14 13:20:10 -0400674 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700675 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
676 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
677 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200678 A.addApproval('Code-Review', 2)
679 B.addApproval('Code-Review', 2)
680 C.addApproval('Code-Review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700681
Paul Belanger174a8272017-03-14 13:20:10 -0400682 self.executor_server.failJob('project-test1', A)
683 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700684
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200685 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
686 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
687 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700688 self.waitUntilSettled()
689
Paul Belanger174a8272017-03-14 13:20:10 -0400690 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700691 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400692 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700693 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400694 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700695 self.waitUntilSettled()
696
697 self.assertEqual(len(self.builds), 6)
698 self.assertEqual(self.builds[0].name, 'project-test1')
699 self.assertEqual(self.builds[1].name, 'project-test2')
700 self.assertEqual(self.builds[2].name, 'project-test1')
701 self.assertEqual(self.builds[3].name, 'project-test2')
702 self.assertEqual(self.builds[4].name, 'project-test1')
703 self.assertEqual(self.builds[5].name, 'project-test2')
704
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400705 self.assertTrue(self.builds[0].hasChanges(A))
706 self.assertTrue(self.builds[2].hasChanges(A))
707 self.assertTrue(self.builds[2].hasChanges(B))
708 self.assertTrue(self.builds[4].hasChanges(A))
709 self.assertTrue(self.builds[4].hasChanges(B))
710 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700711
712 # Fail change B first
713 self.release(self.builds[2])
714 self.waitUntilSettled()
715
716 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400717 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700718 self.waitUntilSettled()
719
720 self.assertEqual(len(self.builds), 5)
721 self.assertEqual(self.builds[0].name, 'project-test1')
722 self.assertEqual(self.builds[1].name, 'project-test2')
723 self.assertEqual(self.builds[2].name, 'project-test2')
724 self.assertEqual(self.builds[3].name, 'project-test1')
725 self.assertEqual(self.builds[4].name, 'project-test2')
726
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400727 self.assertTrue(self.builds[1].hasChanges(A))
728 self.assertTrue(self.builds[2].hasChanges(A))
729 self.assertTrue(self.builds[2].hasChanges(B))
730 self.assertTrue(self.builds[4].hasChanges(A))
731 self.assertFalse(self.builds[4].hasChanges(B))
732 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700733
734 # Finish running all passing jobs for change A
735 self.release(self.builds[1])
736 self.waitUntilSettled()
737 # Fail and report change A
738 self.release(self.builds[0])
739 self.waitUntilSettled()
740
741 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400742 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700743 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400744 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700745 self.waitUntilSettled()
746
747 self.assertEqual(len(self.builds), 4)
748 self.assertEqual(self.builds[0].name, 'project-test1') # B
749 self.assertEqual(self.builds[1].name, 'project-test2') # B
750 self.assertEqual(self.builds[2].name, 'project-test1') # C
751 self.assertEqual(self.builds[3].name, 'project-test2') # C
752
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400753 self.assertFalse(self.builds[1].hasChanges(A))
754 self.assertTrue(self.builds[1].hasChanges(B))
755 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700756
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400757 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700758 # After A failed and B and C restarted, B should be back in
759 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400760 self.assertTrue(self.builds[2].hasChanges(B))
761 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700762
Paul Belanger174a8272017-03-14 13:20:10 -0400763 self.executor_server.hold_jobs_in_build = False
764 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700765 self.waitUntilSettled()
766
767 self.assertEqual(len(self.builds), 0)
768 self.assertEqual(len(self.history), 21)
769 self.assertEqual(A.data['status'], 'NEW')
770 self.assertEqual(B.data['status'], 'NEW')
771 self.assertEqual(C.data['status'], 'MERGED')
772 self.assertEqual(A.reported, 2)
773 self.assertEqual(B.reported, 2)
774 self.assertEqual(C.reported, 2)
775
James E. Blair8c803f82012-07-31 16:25:42 -0700776 def test_patch_order(self):
777 "Test that dependent patches are tested in the right order"
778 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
779 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
780 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200781 A.addApproval('Code-Review', 2)
782 B.addApproval('Code-Review', 2)
783 C.addApproval('Code-Review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700784
785 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
786 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
787 M2.setMerged()
788 M1.setMerged()
789
790 # C -> B -> A -> M1 -> M2
791 # M2 is here to make sure it is never queried. If it is, it
792 # means zuul is walking down the entire history of merged
793 # changes.
794
795 C.setDependsOn(B, 1)
796 B.setDependsOn(A, 1)
797 A.setDependsOn(M1, 1)
798 M1.setDependsOn(M2, 1)
799
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200800 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700801
802 self.waitUntilSettled()
803
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400804 self.assertEqual(A.data['status'], 'NEW')
805 self.assertEqual(B.data['status'], 'NEW')
806 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700807
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200808 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
809 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700810
811 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400812 self.assertEqual(M2.queried, 0)
813 self.assertEqual(A.data['status'], 'MERGED')
814 self.assertEqual(B.data['status'], 'MERGED')
815 self.assertEqual(C.data['status'], 'MERGED')
816 self.assertEqual(A.reported, 2)
817 self.assertEqual(B.reported, 2)
818 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700819
James E. Blair063672f2015-01-29 13:09:12 -0800820 def test_needed_changes_enqueue(self):
821 "Test that a needed change is enqueued ahead"
822 # A Given a git tree like this, if we enqueue
823 # / \ change C, we should walk up and down the tree
824 # B G and enqueue changes in the order ABCDEFG.
825 # /|\ This is also the order that you would get if
826 # *C E F you enqueued changes in the order ABCDEFG, so
827 # / the ordering is stable across re-enqueue events.
828 # D
829
830 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
831 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
832 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
833 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
834 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
835 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
836 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
837 B.setDependsOn(A, 1)
838 C.setDependsOn(B, 1)
839 D.setDependsOn(C, 1)
840 E.setDependsOn(B, 1)
841 F.setDependsOn(B, 1)
842 G.setDependsOn(A, 1)
843
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200844 A.addApproval('Code-Review', 2)
845 B.addApproval('Code-Review', 2)
846 C.addApproval('Code-Review', 2)
847 D.addApproval('Code-Review', 2)
848 E.addApproval('Code-Review', 2)
849 F.addApproval('Code-Review', 2)
850 G.addApproval('Code-Review', 2)
851 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800852
853 self.waitUntilSettled()
854
855 self.assertEqual(A.data['status'], 'NEW')
856 self.assertEqual(B.data['status'], 'NEW')
857 self.assertEqual(C.data['status'], 'NEW')
858 self.assertEqual(D.data['status'], 'NEW')
859 self.assertEqual(E.data['status'], 'NEW')
860 self.assertEqual(F.data['status'], 'NEW')
861 self.assertEqual(G.data['status'], 'NEW')
862
863 # We're about to add approvals to changes without adding the
864 # triggering events to Zuul, so that we can be sure that it is
865 # enqueing the changes based on dependencies, not because of
866 # triggering events. Since it will have the changes cached
867 # already (without approvals), we need to clear the cache
868 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400869 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100870 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800871
Paul Belanger174a8272017-03-14 13:20:10 -0400872 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200873 A.addApproval('Approved', 1)
874 B.addApproval('Approved', 1)
875 D.addApproval('Approved', 1)
876 E.addApproval('Approved', 1)
877 F.addApproval('Approved', 1)
878 G.addApproval('Approved', 1)
879 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800880
881 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400882 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800883 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400884 self.executor_server.hold_jobs_in_build = False
885 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800886 self.waitUntilSettled()
887
888 self.assertEqual(A.data['status'], 'MERGED')
889 self.assertEqual(B.data['status'], 'MERGED')
890 self.assertEqual(C.data['status'], 'MERGED')
891 self.assertEqual(D.data['status'], 'MERGED')
892 self.assertEqual(E.data['status'], 'MERGED')
893 self.assertEqual(F.data['status'], 'MERGED')
894 self.assertEqual(G.data['status'], 'MERGED')
895 self.assertEqual(A.reported, 2)
896 self.assertEqual(B.reported, 2)
897 self.assertEqual(C.reported, 2)
898 self.assertEqual(D.reported, 2)
899 self.assertEqual(E.reported, 2)
900 self.assertEqual(F.reported, 2)
901 self.assertEqual(G.reported, 2)
902 self.assertEqual(self.history[6].changes,
903 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
904
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100905 def test_source_cache(self):
906 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400907 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700908
909 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
910 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
911 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200912 A.addApproval('Code-Review', 2)
913 B.addApproval('Code-Review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700914
915 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
916 M1.setMerged()
917
918 B.setDependsOn(A, 1)
919 A.setDependsOn(M1, 1)
920
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200921 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700922 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
923
924 self.waitUntilSettled()
925
926 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700927 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700928 build.release()
929 self.waitUntilSettled()
930 for build in self.builds:
James E. Blaire675d682017-07-21 15:29:35 -0700931 if build.pipeline == 'check':
James E. Blair0e933c52013-07-11 10:18:52 -0700932 build.release()
933 self.waitUntilSettled()
934
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200935 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700936 self.waitUntilSettled()
937
Joshua Hesketh352264b2015-08-11 23:42:08 +1000938 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700939 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000940 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700941
Paul Belanger174a8272017-03-14 13:20:10 -0400942 self.executor_server.hold_jobs_in_build = False
943 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700944 self.waitUntilSettled()
945
946 self.assertEqual(A.data['status'], 'MERGED')
947 self.assertEqual(B.data['status'], 'MERGED')
948 self.assertEqual(A.queried, 2) # Initial and isMerged
949 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
950
James E. Blair8c803f82012-07-31 16:25:42 -0700951 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700952 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700953 # TODO: move to test_gerrit (this is a unit test!)
954 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400955 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700956 (trusted, project) = tenant.getProject('org/project')
957 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400958
959 # TODO(pabelanger): As we add more source / trigger APIs we should make
960 # it easier for users to create events for testing.
961 event = zuul.model.TriggerEvent()
962 event.trigger_name = 'gerrit'
963 event.change_number = '1'
964 event.patch_number = '2'
965
966 a = source.getChange(event)
967 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700968 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700969
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200970 A.addApproval('Code-Review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400971 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700972 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700973
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200974 A.addApproval('Approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400975 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700976 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700977
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700978 def test_project_merge_conflict(self):
979 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700980
981 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700982 A = self.fake_gerrit.addFakeChange('org/project',
983 'master', 'A',
984 files={'conflict': 'foo'})
985 B = self.fake_gerrit.addFakeChange('org/project',
986 'master', 'B',
987 files={'conflict': 'bar'})
988 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700989 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +0200990 A.addApproval('Code-Review', 2)
991 B.addApproval('Code-Review', 2)
992 C.addApproval('Code-Review', 2)
993 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
994 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
995 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700996 self.waitUntilSettled()
997
James E. Blair6736beb2013-07-11 15:18:15 -0700998 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700999 self.assertEqual(C.reported, 1)
1000
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001001 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001002 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001003 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001004 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001005 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -07001006 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -07001007
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001008 self.gearman_server.hold_jobs_in_queue = False
1009 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -07001010 self.waitUntilSettled()
1011
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001012 self.assertEqual(A.data['status'], 'MERGED')
1013 self.assertEqual(B.data['status'], 'NEW')
1014 self.assertEqual(C.data['status'], 'MERGED')
1015 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +02001016 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001017 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001018
1019 self.assertHistory([
1020 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1021 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1022 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1023 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
1024 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
1025 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
1026 ], ordered=False)
1027
1028 def test_delayed_merge_conflict(self):
1029 "Test that delayed check merge conflicts are handled properly"
1030
1031 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -07001032 # the executor sucesfully merges a change based on an old
1033 # repo state (frozen by the scheduler) which would otherwise
1034 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001035 self.gearman_server.hold_jobs_in_queue = True
1036 A = self.fake_gerrit.addFakeChange('org/project',
1037 'master', 'A',
1038 files={'conflict': 'foo'})
1039 B = self.fake_gerrit.addFakeChange('org/project',
1040 'master', 'B',
1041 files={'conflict': 'bar'})
1042 C = self.fake_gerrit.addFakeChange('org/project',
1043 'master', 'C')
1044 C.setDependsOn(B, 1)
1045
1046 # A enters the gate queue; B and C enter the check queue
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001047 A.addApproval('Code-Review', 2)
1048 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001049 self.waitUntilSettled()
1050 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001051 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001052 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1053 self.waitUntilSettled()
1054
1055 self.assertEqual(A.reported, 1)
1056 self.assertEqual(B.reported, 0) # Check does not report start
1057 self.assertEqual(C.reported, 0) # Check does not report start
1058
1059 # A merges while B and C are queued in check
1060 # Release A project-merge
1061 queue = self.gearman_server.getQueue()
1062 self.release(queue[0])
1063 self.waitUntilSettled()
1064
1065 # Release A project-test*
1066 # gate has higher precedence, so A's test jobs are added in
1067 # front of the merge jobs for B and C
1068 queue = self.gearman_server.getQueue()
1069 self.release(queue[0])
1070 self.release(queue[1])
1071 self.waitUntilSettled()
1072
1073 self.assertEqual(A.data['status'], 'MERGED')
1074 self.assertEqual(B.data['status'], 'NEW')
1075 self.assertEqual(C.data['status'], 'NEW')
1076 self.assertEqual(A.reported, 2)
1077 self.assertEqual(B.reported, 0)
1078 self.assertEqual(C.reported, 0)
1079 self.assertHistory([
1080 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1081 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1082 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1083 ], ordered=False)
1084
1085 # B and C report merge conflicts
1086 # Release B project-merge
1087 queue = self.gearman_server.getQueue()
1088 self.release(queue[0])
1089 self.waitUntilSettled()
1090
1091 # Release C
1092 self.gearman_server.hold_jobs_in_queue = False
1093 self.gearman_server.release()
1094 self.waitUntilSettled()
1095
1096 self.assertEqual(A.data['status'], 'MERGED')
1097 self.assertEqual(B.data['status'], 'NEW')
1098 self.assertEqual(C.data['status'], 'NEW')
1099 self.assertEqual(A.reported, 2)
1100 self.assertEqual(B.reported, 1)
1101 self.assertEqual(C.reported, 1)
1102
1103 self.assertHistory([
1104 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1105 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1106 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001107 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1108 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1109 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1110 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1111 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1112 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001113 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001114
James E. Blairdaabed22012-08-15 15:38:57 -07001115 def test_post(self):
1116 "Test that post jobs run"
James E. Blair289f5932017-07-27 15:02:29 -07001117 p = "review.example.com/org/project"
1118 upstream = self.getUpstreamRepos([p])
1119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1120 A.setMerged()
1121 A_commit = str(upstream[p].commit('master'))
1122 self.log.debug("A commit: %s" % A_commit)
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001123
Zhongyue Luo5d556072012-09-21 02:00:47 +09001124 e = {
1125 "type": "ref-updated",
1126 "submitter": {
1127 "name": "User Name",
1128 },
1129 "refUpdate": {
1130 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
James E. Blair289f5932017-07-27 15:02:29 -07001131 "newRev": A_commit,
Zhongyue Luo5d556072012-09-21 02:00:47 +09001132 "refName": "master",
1133 "project": "org/project",
1134 }
1135 }
James E. Blairdaabed22012-08-15 15:38:57 -07001136 self.fake_gerrit.addEvent(e)
1137 self.waitUntilSettled()
1138
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001139 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001140 self.assertEqual(len(self.history), 1)
1141 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001142
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001143 def test_post_ignore_deletes(self):
1144 "Test that deleting refs does not trigger post jobs"
1145
1146 e = {
1147 "type": "ref-updated",
1148 "submitter": {
1149 "name": "User Name",
1150 },
1151 "refUpdate": {
1152 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1153 "newRev": "0000000000000000000000000000000000000000",
1154 "refName": "master",
1155 "project": "org/project",
1156 }
1157 }
1158 self.fake_gerrit.addEvent(e)
1159 self.waitUntilSettled()
1160
1161 job_names = [x.name for x in self.history]
1162 self.assertEqual(len(self.history), 0)
1163 self.assertNotIn('project-post', job_names)
1164
James E. Blaircc324192017-04-19 14:36:08 -07001165 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001166 def test_post_ignore_deletes_negative(self):
1167 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001168 e = {
1169 "type": "ref-updated",
1170 "submitter": {
1171 "name": "User Name",
1172 },
1173 "refUpdate": {
1174 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1175 "newRev": "0000000000000000000000000000000000000000",
James E. Blair289f5932017-07-27 15:02:29 -07001176 "refName": "testbranch",
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001177 "project": "org/project",
1178 }
1179 }
1180 self.fake_gerrit.addEvent(e)
1181 self.waitUntilSettled()
1182
1183 job_names = [x.name for x in self.history]
1184 self.assertEqual(len(self.history), 1)
1185 self.assertIn('project-post', job_names)
1186
James E. Blairec056492016-07-22 09:45:56 -07001187 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001188 def test_build_configuration_branch_interaction(self):
1189 "Test that switching between branches works"
1190 self.test_build_configuration()
1191 self.test_build_configuration_branch()
1192 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001193 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001194 repo = git.Repo(path)
1195 repo.heads.master.commit = repo.commit('init')
1196 self.test_build_configuration()
1197
James E. Blairdb93b302017-07-19 15:33:11 -07001198 def test_dependent_changes_rebase(self):
1199 # Test that no errors occur when we walk a dependency tree
1200 # with an unused leaf node due to a rebase.
1201 # Start by constructing: C -> B -> A
1202 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1203 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1204 B.setDependsOn(A, 1)
1205
1206 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1207 C.setDependsOn(B, 1)
1208
1209 # Then rebase to form: D -> C -> A
1210 C.addPatchset() # C,2
1211 C.setDependsOn(A, 1)
1212
1213 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1214 D.setDependsOn(C, 2)
1215
1216 # Walk the entire tree
1217 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1218 self.waitUntilSettled()
1219 self.assertEqual(len(self.history), 3)
1220
1221 # Verify that walking just part of the tree still works
1222 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
1223 self.waitUntilSettled()
1224 self.assertEqual(len(self.history), 6)
1225
James E. Blaircaec0c52012-08-22 14:52:22 -07001226 def test_dependent_changes_dequeue(self):
1227 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001228
James E. Blaircaec0c52012-08-22 14:52:22 -07001229 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1230 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1231 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001232 A.addApproval('Code-Review', 2)
1233 B.addApproval('Code-Review', 2)
1234 C.addApproval('Code-Review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001235
1236 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1237 M1.setMerged()
1238
1239 # C -> B -> A -> M1
1240
1241 C.setDependsOn(B, 1)
1242 B.setDependsOn(A, 1)
1243 A.setDependsOn(M1, 1)
1244
Paul Belanger174a8272017-03-14 13:20:10 -04001245 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001246
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001247 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1248 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1249 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001250
1251 self.waitUntilSettled()
1252
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001253 self.assertEqual(A.data['status'], 'NEW')
1254 self.assertEqual(A.reported, 2)
1255 self.assertEqual(B.data['status'], 'NEW')
1256 self.assertEqual(B.reported, 2)
1257 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001258 self.assertIn('This change depends on a change that failed to merge.',
1259 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001260 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001261
James E. Blair972e3c72013-08-29 12:04:55 -07001262 def test_failing_dependent_changes(self):
1263 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001264 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001265 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1266 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1267 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1268 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1269 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001270 A.addApproval('Code-Review', 2)
1271 B.addApproval('Code-Review', 2)
1272 C.addApproval('Code-Review', 2)
1273 D.addApproval('Code-Review', 2)
1274 E.addApproval('Code-Review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001275
1276 # E, D -> C -> B, A
1277
1278 D.setDependsOn(C, 1)
1279 C.setDependsOn(B, 1)
1280
Paul Belanger174a8272017-03-14 13:20:10 -04001281 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001282
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001283 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1284 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
1285 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1286 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1287 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001288
1289 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001290 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001291 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001292 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001293 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001294 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001295 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001296 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001297 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001298 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001299 self.waitUntilSettled()
1300
Paul Belanger174a8272017-03-14 13:20:10 -04001301 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001302 for build in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07001303 if build.parameters['zuul']['change'] != '1':
James E. Blair972e3c72013-08-29 12:04:55 -07001304 build.release()
1305 self.waitUntilSettled()
1306
Paul Belanger174a8272017-03-14 13:20:10 -04001307 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001308 self.waitUntilSettled()
1309
1310 self.assertEqual(A.data['status'], 'MERGED')
1311 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001312 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001313 self.assertEqual(B.data['status'], 'NEW')
1314 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001315 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001316 self.assertEqual(C.data['status'], 'NEW')
1317 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001318 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001319 self.assertEqual(D.data['status'], 'NEW')
1320 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001321 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001322 self.assertEqual(E.data['status'], 'MERGED')
1323 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001324 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001325 self.assertEqual(len(self.history), 18)
1326
James E. Blairec590122012-08-22 15:19:31 -07001327 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001328 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001329 # If it's dequeued more than once, we should see extra
1330 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001331
Paul Belanger174a8272017-03-14 13:20:10 -04001332 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001333 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1334 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1335 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001336 A.addApproval('Code-Review', 2)
1337 B.addApproval('Code-Review', 2)
1338 C.addApproval('Code-Review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001339
Paul Belanger174a8272017-03-14 13:20:10 -04001340 self.executor_server.failJob('project-test1', A)
1341 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001342
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001343 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1344 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1345 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001346
1347 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001348
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001349 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001350 self.assertEqual(self.builds[0].name, 'project-merge')
1351 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001352
Paul Belanger174a8272017-03-14 13:20:10 -04001353 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001354 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001355 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001356 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001357 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001358 self.waitUntilSettled()
1359
James E. Blair6df67132017-04-19 13:53:20 -07001360 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001361 self.assertEqual(self.builds[0].name, 'project-test1')
1362 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001363 self.assertEqual(self.builds[2].name, 'project-test1')
1364 self.assertEqual(self.builds[3].name, 'project-test2')
1365 self.assertEqual(self.builds[4].name, 'project-test1')
1366 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001367
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001368 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001369 self.waitUntilSettled()
1370
James E. Blair6df67132017-04-19 13:53:20 -07001371 self.assertEqual(len(self.builds), 2) # test2, merge for B
1372 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001373
Paul Belanger174a8272017-03-14 13:20:10 -04001374 self.executor_server.hold_jobs_in_build = False
1375 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001376 self.waitUntilSettled()
1377
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001378 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001379 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001380
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001381 self.assertEqual(A.data['status'], 'NEW')
1382 self.assertEqual(B.data['status'], 'MERGED')
1383 self.assertEqual(C.data['status'], 'MERGED')
1384 self.assertEqual(A.reported, 2)
1385 self.assertEqual(B.reported, 2)
1386 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001387
James E. Blaircc020532017-04-19 13:04:44 -07001388 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001389 def test_nonvoting_job(self):
1390 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001391
James E. Blair4ec821f2012-08-23 15:28:28 -07001392 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1393 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001394 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001395 self.executor_server.failJob('nonvoting-project-test2', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001396 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001397
1398 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001399
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001400 self.assertEqual(A.data['status'], 'MERGED')
1401 self.assertEqual(A.reported, 2)
1402 self.assertEqual(
1403 self.getJobFromHistory('nonvoting-project-merge').result,
1404 'SUCCESS')
1405 self.assertEqual(
1406 self.getJobFromHistory('nonvoting-project-test1').result,
1407 'SUCCESS')
1408 self.assertEqual(
1409 self.getJobFromHistory('nonvoting-project-test2').result,
1410 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001411
James E. Blair2ef29e92017-07-21 15:25:05 -07001412 self.assertTrue(self.getJobFromHistory('nonvoting-project-merge').
1413 parameters['zuul']['voting'])
1414 self.assertTrue(self.getJobFromHistory('nonvoting-project-test1').
1415 parameters['zuul']['voting'])
1416 self.assertFalse(self.getJobFromHistory('nonvoting-project-test2').
1417 parameters['zuul']['voting'])
James E. Blair5821bd92015-09-16 08:48:15 -07001418
James E. Blaire0487072012-08-29 17:38:31 -07001419 def test_check_queue_success(self):
1420 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001421
James E. Blaire0487072012-08-29 17:38:31 -07001422 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1423 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1424
1425 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001426
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001427 self.assertEqual(A.data['status'], 'NEW')
1428 self.assertEqual(A.reported, 1)
1429 self.assertEqual(self.getJobFromHistory('project-merge').result,
1430 'SUCCESS')
1431 self.assertEqual(self.getJobFromHistory('project-test1').result,
1432 'SUCCESS')
1433 self.assertEqual(self.getJobFromHistory('project-test2').result,
1434 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001435
1436 def test_check_queue_failure(self):
1437 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001438
James E. Blaire0487072012-08-29 17:38:31 -07001439 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001440 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001441 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1442
1443 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001444
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001445 self.assertEqual(A.data['status'], 'NEW')
1446 self.assertEqual(A.reported, 1)
1447 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001448 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001449 self.assertEqual(self.getJobFromHistory('project-test1').result,
1450 'SUCCESS')
1451 self.assertEqual(self.getJobFromHistory('project-test2').result,
1452 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001453
David Shrewsburyffab07a2017-07-24 12:45:07 -04001454 @simple_layout('layouts/autohold.yaml')
1455 def test_autohold(self):
1456 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1457
1458 client = zuul.rpcclient.RPCClient('127.0.0.1',
1459 self.gearman_server.port)
1460 self.addCleanup(client.shutdown)
David Shrewsbury36b2adf2017-07-31 15:40:13 -04001461 r = client.autohold('tenant-one', 'org/project', 'project-test2',
1462 "reason text", 1)
David Shrewsburyffab07a2017-07-24 12:45:07 -04001463 self.assertTrue(r)
1464
1465 self.executor_server.failJob('project-test2', A)
1466 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1467
1468 self.waitUntilSettled()
1469
1470 self.assertEqual(A.data['status'], 'NEW')
1471 self.assertEqual(A.reported, 1)
1472 self.assertEqual(self.getJobFromHistory('project-test2').result,
1473 'FAILURE')
1474
1475 # Check nodepool for a held node
1476 held_node = None
1477 for node in self.fake_nodepool.getNodes():
1478 if node['state'] == zuul.model.STATE_HOLD:
1479 held_node = node
1480 break
1481 self.assertIsNotNone(held_node)
1482
1483 # Validate node has recorded the failed job
1484 self.assertEqual(
1485 held_node['hold_job'],
1486 " ".join(['tenant-one',
1487 'review.example.com/org/project',
1488 'project-test2'])
1489 )
David Shrewsburyf9af9df2017-08-01 15:19:26 -04001490 self.assertEqual(held_node['comment'], "reason text")
David Shrewsburyffab07a2017-07-24 12:45:07 -04001491
1492 # Another failed change should not hold any more nodes
1493 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1494 self.executor_server.failJob('project-test2', B)
1495 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1496 self.waitUntilSettled()
1497 self.assertEqual(B.data['status'], 'NEW')
1498 self.assertEqual(B.reported, 1)
1499 self.assertEqual(self.getJobFromHistory('project-test2').result,
1500 'FAILURE')
1501
1502 held_nodes = 0
1503 for node in self.fake_nodepool.getNodes():
1504 if node['state'] == zuul.model.STATE_HOLD:
1505 held_nodes += 1
1506 self.assertEqual(held_nodes, 1)
1507
James E. Blair7cb84542017-04-19 13:35:05 -07001508 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001509 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001510 # This particular test does a large amount of merges and needs a little
1511 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001512 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001513 "test that dependent changes behind dequeued changes work"
1514 # This complicated test is a reproduction of a real life bug
1515 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001516
Paul Belanger174a8272017-03-14 13:20:10 -04001517 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001518 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1519 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1520 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1521 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1522 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1523 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1524 D.setDependsOn(C, 1)
1525 E.setDependsOn(D, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001526 A.addApproval('Code-Review', 2)
1527 B.addApproval('Code-Review', 2)
1528 C.addApproval('Code-Review', 2)
1529 D.addApproval('Code-Review', 2)
1530 E.addApproval('Code-Review', 2)
1531 F.addApproval('Code-Review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001532
1533 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001534
1535 # Change object re-use in the gerrit trigger is hidden if
1536 # changes are added in quick succession; waiting makes it more
1537 # like real life.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001538 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001539 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001540 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001541 self.waitUntilSettled()
1542
Paul Belanger174a8272017-03-14 13:20:10 -04001543 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001544 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001545 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001546 self.waitUntilSettled()
1547
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001548 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001549 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001550 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001551 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001552 self.fake_gerrit.addEvent(E.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001553 self.waitUntilSettled()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001554 self.fake_gerrit.addEvent(F.addApproval('Approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001555 self.waitUntilSettled()
1556
Paul Belanger174a8272017-03-14 13:20:10 -04001557 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001558 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001559 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001560 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001561 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001562 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001563 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001564 self.waitUntilSettled()
1565
1566 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001567
1568 # Grab pointers to the jobs we want to release before
1569 # releasing any, because list indexes may change as
1570 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001571 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001572 a.release()
1573 b.release()
1574 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001575 self.waitUntilSettled()
1576
Paul Belanger174a8272017-03-14 13:20:10 -04001577 self.executor_server.hold_jobs_in_build = False
1578 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001579 self.waitUntilSettled()
1580
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001581 self.assertEqual(A.data['status'], 'NEW')
1582 self.assertEqual(B.data['status'], 'MERGED')
1583 self.assertEqual(C.data['status'], 'MERGED')
1584 self.assertEqual(D.data['status'], 'MERGED')
1585 self.assertEqual(E.data['status'], 'MERGED')
1586 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001587
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001588 self.assertEqual(A.reported, 2)
1589 self.assertEqual(B.reported, 2)
1590 self.assertEqual(C.reported, 2)
1591 self.assertEqual(D.reported, 2)
1592 self.assertEqual(E.reported, 2)
1593 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001594
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001595 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1596 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001597
1598 def test_merger_repack(self):
1599 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001600
James E. Blair05fed602012-09-07 12:45:24 -07001601 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001602 A.addApproval('Code-Review', 2)
1603 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001604 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001605 self.assertEqual(self.getJobFromHistory('project-merge').result,
1606 'SUCCESS')
1607 self.assertEqual(self.getJobFromHistory('project-test1').result,
1608 'SUCCESS')
1609 self.assertEqual(self.getJobFromHistory('project-test2').result,
1610 'SUCCESS')
1611 self.assertEqual(A.data['status'], 'MERGED')
1612 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001613 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001614 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001615
James E. Blair2a535672017-04-27 12:03:15 -07001616 path = os.path.join(self.merger_src_root, "review.example.com",
1617 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001618 if os.path.exists(path):
1619 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001620 path = os.path.join(self.executor_src_root, "review.example.com",
1621 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001622 if os.path.exists(path):
1623 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001624
1625 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001626 A.addApproval('Code-Review', 2)
1627 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001628 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001629 self.assertEqual(self.getJobFromHistory('project-merge').result,
1630 'SUCCESS')
1631 self.assertEqual(self.getJobFromHistory('project-test1').result,
1632 'SUCCESS')
1633 self.assertEqual(self.getJobFromHistory('project-test2').result,
1634 'SUCCESS')
1635 self.assertEqual(A.data['status'], 'MERGED')
1636 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001637
James E. Blair4886f282012-11-15 09:27:33 -08001638 def test_merger_repack_large_change(self):
1639 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001640 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001641 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001642 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001643 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001644 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001645 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001646 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001647 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001648 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001649 # TODOv3(jeblair): add hostname to upstream root
1650 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001651 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001652 path = os.path.join(self.merger_src_root, 'review.example.com',
1653 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001654 if os.path.exists(path):
1655 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001656 path = os.path.join(self.executor_src_root, 'review.example.com',
1657 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001658 if os.path.exists(path):
1659 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001660
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001661 A.addApproval('Code-Review', 2)
1662 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001663 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001664 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001665 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001666 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001667 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001668 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001669 'SUCCESS')
1670 self.assertEqual(A.data['status'], 'MERGED')
1671 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001672
James E. Blair2fa50962013-01-30 21:50:41 -08001673 def test_new_patchset_dequeues_old(self):
1674 "Test that a new patchset causes the old to be dequeued"
1675 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001676 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001677 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1678 M.setMerged()
1679
1680 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1681 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1682 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1683 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001684 A.addApproval('Code-Review', 2)
1685 B.addApproval('Code-Review', 2)
1686 C.addApproval('Code-Review', 2)
1687 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001688
1689 C.setDependsOn(B, 1)
1690 B.setDependsOn(A, 1)
1691 A.setDependsOn(M, 1)
1692
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001693 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1694 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1695 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1696 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001697 self.waitUntilSettled()
1698
1699 B.addPatchset()
1700 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1701 self.waitUntilSettled()
1702
Paul Belanger174a8272017-03-14 13:20:10 -04001703 self.executor_server.hold_jobs_in_build = False
1704 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001705 self.waitUntilSettled()
1706
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001707 self.assertEqual(A.data['status'], 'MERGED')
1708 self.assertEqual(A.reported, 2)
1709 self.assertEqual(B.data['status'], 'NEW')
1710 self.assertEqual(B.reported, 2)
1711 self.assertEqual(C.data['status'], 'NEW')
1712 self.assertEqual(C.reported, 2)
1713 self.assertEqual(D.data['status'], 'MERGED')
1714 self.assertEqual(D.reported, 2)
1715 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001716
James E. Blairba437362015-02-07 11:41:52 -08001717 def test_new_patchset_check(self):
1718 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001719
Paul Belanger174a8272017-03-14 13:20:10 -04001720 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001721
1722 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001723 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001724 tenant = self.sched.abide.tenants.get('tenant-one')
1725 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001726
1727 # Add two git-dependent changes
1728 B.setDependsOn(A, 1)
1729 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1730 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001731 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1732 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001733
1734 # A live item, and a non-live/live pair
1735 items = check_pipeline.getAllItems()
1736 self.assertEqual(len(items), 3)
1737
1738 self.assertEqual(items[0].change.number, '1')
1739 self.assertEqual(items[0].change.patchset, '1')
1740 self.assertFalse(items[0].live)
1741
1742 self.assertEqual(items[1].change.number, '2')
1743 self.assertEqual(items[1].change.patchset, '1')
1744 self.assertTrue(items[1].live)
1745
1746 self.assertEqual(items[2].change.number, '1')
1747 self.assertEqual(items[2].change.patchset, '1')
1748 self.assertTrue(items[2].live)
1749
1750 # Add a new patchset to A
1751 A.addPatchset()
1752 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1753 self.waitUntilSettled()
1754
1755 # The live copy of A,1 should be gone, but the non-live and B
1756 # should continue, and we should have a new A,2
1757 items = check_pipeline.getAllItems()
1758 self.assertEqual(len(items), 3)
1759
1760 self.assertEqual(items[0].change.number, '1')
1761 self.assertEqual(items[0].change.patchset, '1')
1762 self.assertFalse(items[0].live)
1763
1764 self.assertEqual(items[1].change.number, '2')
1765 self.assertEqual(items[1].change.patchset, '1')
1766 self.assertTrue(items[1].live)
1767
1768 self.assertEqual(items[2].change.number, '1')
1769 self.assertEqual(items[2].change.patchset, '2')
1770 self.assertTrue(items[2].live)
1771
1772 # Add a new patchset to B
1773 B.addPatchset()
1774 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1775 self.waitUntilSettled()
1776
1777 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1778 # but we should have a new B,2 (still based on A,1)
1779 items = check_pipeline.getAllItems()
1780 self.assertEqual(len(items), 3)
1781
1782 self.assertEqual(items[0].change.number, '1')
1783 self.assertEqual(items[0].change.patchset, '2')
1784 self.assertTrue(items[0].live)
1785
1786 self.assertEqual(items[1].change.number, '1')
1787 self.assertEqual(items[1].change.patchset, '1')
1788 self.assertFalse(items[1].live)
1789
1790 self.assertEqual(items[2].change.number, '2')
1791 self.assertEqual(items[2].change.patchset, '2')
1792 self.assertTrue(items[2].live)
1793
1794 self.builds[0].release()
1795 self.waitUntilSettled()
1796 self.builds[0].release()
1797 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001798 self.executor_server.hold_jobs_in_build = False
1799 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001800 self.waitUntilSettled()
1801
1802 self.assertEqual(A.reported, 1)
1803 self.assertEqual(B.reported, 1)
1804 self.assertEqual(self.history[0].result, 'ABORTED')
1805 self.assertEqual(self.history[0].changes, '1,1')
1806 self.assertEqual(self.history[1].result, 'ABORTED')
1807 self.assertEqual(self.history[1].changes, '1,1 2,1')
1808 self.assertEqual(self.history[2].result, 'SUCCESS')
1809 self.assertEqual(self.history[2].changes, '1,2')
1810 self.assertEqual(self.history[3].result, 'SUCCESS')
1811 self.assertEqual(self.history[3].changes, '1,1 2,2')
1812
1813 def test_abandoned_gate(self):
1814 "Test that an abandoned change is dequeued from gate"
1815
Paul Belanger174a8272017-03-14 13:20:10 -04001816 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001817
1818 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001819 A.addApproval('Code-Review', 2)
1820 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001821 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001822 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1823 self.assertEqual(self.builds[0].name, 'project-merge')
1824
1825 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1826 self.waitUntilSettled()
1827
Paul Belanger174a8272017-03-14 13:20:10 -04001828 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001829 self.waitUntilSettled()
1830
Arie5b4048c2016-10-05 16:12:06 +03001831 self.assertBuilds([])
1832 self.assertHistory([
1833 dict(name='project-merge', result='ABORTED', changes='1,1')],
1834 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001835 self.assertEqual(A.reported, 1,
1836 "Abandoned gate change should report only start")
1837
1838 def test_abandoned_check(self):
1839 "Test that an abandoned change is dequeued from check"
1840
Paul Belanger174a8272017-03-14 13:20:10 -04001841 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001842
1843 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1844 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001845 tenant = self.sched.abide.tenants.get('tenant-one')
1846 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001847
1848 # Add two git-dependent changes
1849 B.setDependsOn(A, 1)
1850 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1851 self.waitUntilSettled()
1852 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1853 self.waitUntilSettled()
1854 # A live item, and a non-live/live pair
1855 items = check_pipeline.getAllItems()
1856 self.assertEqual(len(items), 3)
1857
1858 self.assertEqual(items[0].change.number, '1')
1859 self.assertFalse(items[0].live)
1860
1861 self.assertEqual(items[1].change.number, '2')
1862 self.assertTrue(items[1].live)
1863
1864 self.assertEqual(items[2].change.number, '1')
1865 self.assertTrue(items[2].live)
1866
1867 # Abandon A
1868 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1869 self.waitUntilSettled()
1870
1871 # The live copy of A should be gone, but the non-live and B
1872 # should continue
1873 items = check_pipeline.getAllItems()
1874 self.assertEqual(len(items), 2)
1875
1876 self.assertEqual(items[0].change.number, '1')
1877 self.assertFalse(items[0].live)
1878
1879 self.assertEqual(items[1].change.number, '2')
1880 self.assertTrue(items[1].live)
1881
Paul Belanger174a8272017-03-14 13:20:10 -04001882 self.executor_server.hold_jobs_in_build = False
1883 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001884 self.waitUntilSettled()
1885
1886 self.assertEqual(len(self.history), 4)
1887 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001888 'Build should have been aborted')
1889 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001890 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001891
Steve Varnau7b78b312015-04-03 14:49:46 -07001892 def test_abandoned_not_timer(self):
1893 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001894 # This test can not use simple_layout because it must start
1895 # with a configuration which does not include a
1896 # timer-triggered job so that we have an opportunity to set
1897 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001898 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001899 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001900 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001901 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001902 # The pipeline triggers every second, so we should have seen
1903 # several by now.
1904 time.sleep(5)
1905 self.waitUntilSettled()
1906 # Stop queuing timer triggered jobs so that the assertions
1907 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001908 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001909 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001910 self.sched.reconfigure(self.config)
James E. Blair0b137b42017-07-27 08:51:25 -07001911 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07001912 # If APScheduler is in mid-event when we remove the job, we
1913 # can end up with one more event firing, so give it an extra
1914 # second to settle.
1915 time.sleep(1)
1916 self.waitUntilSettled()
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001917
James E. Blair21037782017-07-19 11:56:55 -07001918 self.assertEqual(len(self.builds), 1, "One timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001919
1920 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1921 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1922 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07001923 self.assertEqual(len(self.builds), 2, "One change plus one timer job")
Steve Varnau7b78b312015-04-03 14:49:46 -07001924
1925 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1926 self.waitUntilSettled()
1927
James E. Blair21037782017-07-19 11:56:55 -07001928 self.assertEqual(len(self.builds), 1, "One timer job remains")
Steve Varnau7b78b312015-04-03 14:49:46 -07001929
Paul Belanger174a8272017-03-14 13:20:10 -04001930 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001931 self.waitUntilSettled()
1932
James E. Blair2fa50962013-01-30 21:50:41 -08001933 def test_new_patchset_dequeues_old_on_head(self):
1934 "Test that a new patchset causes the old to be dequeued (at head)"
1935 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001936 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001937 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1938 M.setMerged()
1939 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1940 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1941 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1942 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001943 A.addApproval('Code-Review', 2)
1944 B.addApproval('Code-Review', 2)
1945 C.addApproval('Code-Review', 2)
1946 D.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001947
1948 C.setDependsOn(B, 1)
1949 B.setDependsOn(A, 1)
1950 A.setDependsOn(M, 1)
1951
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001952 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
1953 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1954 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1955 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001956 self.waitUntilSettled()
1957
1958 A.addPatchset()
1959 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1960 self.waitUntilSettled()
1961
Paul Belanger174a8272017-03-14 13:20:10 -04001962 self.executor_server.hold_jobs_in_build = False
1963 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001964 self.waitUntilSettled()
1965
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001966 self.assertEqual(A.data['status'], 'NEW')
1967 self.assertEqual(A.reported, 2)
1968 self.assertEqual(B.data['status'], 'NEW')
1969 self.assertEqual(B.reported, 2)
1970 self.assertEqual(C.data['status'], 'NEW')
1971 self.assertEqual(C.reported, 2)
1972 self.assertEqual(D.data['status'], 'MERGED')
1973 self.assertEqual(D.reported, 2)
1974 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001975
1976 def test_new_patchset_dequeues_old_without_dependents(self):
1977 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001978 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001979 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1980 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1981 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001982 A.addApproval('Code-Review', 2)
1983 B.addApproval('Code-Review', 2)
1984 C.addApproval('Code-Review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001985
Tobias Henkelbf24fd12017-07-27 06:13:07 +02001986 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
1987 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
1988 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001989 self.waitUntilSettled()
1990
1991 B.addPatchset()
1992 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1993 self.waitUntilSettled()
1994
Paul Belanger174a8272017-03-14 13:20:10 -04001995 self.executor_server.hold_jobs_in_build = False
1996 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001997 self.waitUntilSettled()
1998
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001999 self.assertEqual(A.data['status'], 'MERGED')
2000 self.assertEqual(A.reported, 2)
2001 self.assertEqual(B.data['status'], 'NEW')
2002 self.assertEqual(B.reported, 2)
2003 self.assertEqual(C.data['status'], 'MERGED')
2004 self.assertEqual(C.reported, 2)
2005 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08002006
2007 def test_new_patchset_dequeues_old_independent_queue(self):
2008 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04002009 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08002010 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2011 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2012 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
2013 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2014 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2015 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2016 self.waitUntilSettled()
2017
2018 B.addPatchset()
2019 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
2020 self.waitUntilSettled()
2021
Paul Belanger174a8272017-03-14 13:20:10 -04002022 self.executor_server.hold_jobs_in_build = False
2023 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08002024 self.waitUntilSettled()
2025
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002026 self.assertEqual(A.data['status'], 'NEW')
2027 self.assertEqual(A.reported, 1)
2028 self.assertEqual(B.data['status'], 'NEW')
2029 self.assertEqual(B.reported, 1)
2030 self.assertEqual(C.data['status'], 'NEW')
2031 self.assertEqual(C.reported, 1)
2032 self.assertEqual(len(self.history), 10)
2033 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002034
James E. Blaircc020532017-04-19 13:04:44 -07002035 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07002036 def test_noop_job(self):
2037 "Test that the internal noop job works"
2038 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002039 A.addApproval('Code-Review', 2)
2040 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07002041 self.waitUntilSettled()
2042
2043 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2044 self.assertTrue(self.sched._areAllBuildsComplete())
2045 self.assertEqual(len(self.history), 0)
2046 self.assertEqual(A.data['status'], 'MERGED')
2047 self.assertEqual(A.reported, 2)
2048
James E. Blair06cc3922017-04-19 10:08:10 -07002049 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002050 def test_no_job_project(self):
2051 "Test that reports with no jobs don't get sent"
2052 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
2053 'master', 'A')
2054 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2055 self.waitUntilSettled()
2056
2057 # Change wasn't reported to
2058 self.assertEqual(A.reported, False)
2059
2060 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08002061 tenant = self.sched.abide.tenants.get('tenant-one')
2062 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00002063 items = check_pipeline.getAllItems()
2064 self.assertEqual(len(items), 0)
2065
2066 self.assertEqual(len(self.history), 0)
2067
James E. Blair7d0dedc2013-02-21 17:26:09 -08002068 def test_zuul_refs(self):
2069 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04002070 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08002071 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
2072 M1.setMerged()
2073 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
2074 M2.setMerged()
2075
2076 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2077 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2078 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
2079 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002080 A.addApproval('Code-Review', 2)
2081 B.addApproval('Code-Review', 2)
2082 C.addApproval('Code-Review', 2)
2083 D.addApproval('Code-Review', 2)
2084 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2085 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
2086 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
2087 self.fake_gerrit.addEvent(D.addApproval('Approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002088
2089 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002090 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002091 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002092 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002093 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002094 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002095 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002096 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08002097 self.waitUntilSettled()
2098
Clint Byrum3343e3e2016-11-15 16:05:03 -08002099 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002100 for x in self.builds:
James E. Blaire3db2952017-07-21 15:03:36 -07002101 if x.parameters['zuul']['change'] == '3':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002102 a_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002103 elif x.parameters['zuul']['change'] == '4':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002104 b_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002105 elif x.parameters['zuul']['change'] == '5':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002106 c_build = x
James E. Blaire3db2952017-07-21 15:03:36 -07002107 elif x.parameters['zuul']['change'] == '6':
Clint Byrum3343e3e2016-11-15 16:05:03 -08002108 d_build = x
2109 if a_build and b_build and c_build and d_build:
2110 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002111
Clint Byrum3343e3e2016-11-15 16:05:03 -08002112 # should have a, not b, and should not be in project2
2113 self.assertTrue(a_build.hasChanges(A))
2114 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002115
Clint Byrum3343e3e2016-11-15 16:05:03 -08002116 # should have a and b, and should not be in project2
2117 self.assertTrue(b_build.hasChanges(A, B))
2118 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002119
Clint Byrum3343e3e2016-11-15 16:05:03 -08002120 # should have a and b in 1, c in 2
2121 self.assertTrue(c_build.hasChanges(A, B, C))
2122 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002123
Clint Byrum3343e3e2016-11-15 16:05:03 -08002124 # should have a and b in 1, c and d in 2
2125 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002126
Paul Belanger174a8272017-03-14 13:20:10 -04002127 self.executor_server.hold_jobs_in_build = False
2128 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002129 self.waitUntilSettled()
2130
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002131 self.assertEqual(A.data['status'], 'MERGED')
2132 self.assertEqual(A.reported, 2)
2133 self.assertEqual(B.data['status'], 'MERGED')
2134 self.assertEqual(B.reported, 2)
2135 self.assertEqual(C.data['status'], 'MERGED')
2136 self.assertEqual(C.reported, 2)
2137 self.assertEqual(D.data['status'], 'MERGED')
2138 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002139
James E. Blair4a28a882013-08-23 15:17:33 -07002140 def test_rerun_on_error(self):
2141 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002142 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002143 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002144 A.addApproval('Code-Review', 2)
2145 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002146 self.waitUntilSettled()
2147
James E. Blair412fba82017-01-26 15:00:50 -08002148 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002149 self.executor_server.hold_jobs_in_build = False
2150 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002151 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002152 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002153 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2154
James E. Blair412e5582013-04-22 15:50:12 -07002155 def test_statsd(self):
2156 "Test each of the statsd methods used in the scheduler"
James E. Blairded241e2017-10-10 13:22:40 -07002157 statsd = self.sched.statsd
James E. Blair412e5582013-04-22 15:50:12 -07002158 statsd.incr('test-incr')
2159 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002160 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002161 self.assertReportedStat('test-incr', '1|c')
2162 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002163 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002164
James E. Blairdad52252014-02-07 16:59:17 -08002165 def test_stuck_job_cleanup(self):
2166 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002167
2168 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002169 self.gearman_server.hold_jobs_in_queue = True
2170 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002171 A.addApproval('Code-Review', 2)
2172 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002173 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002174 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002175 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2176
James E. Blair31cb3e82017-04-20 13:08:56 -07002177 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002178 self.sched.reconfigure(self.config)
2179 self.waitUntilSettled()
2180
James E. Blair18c64442014-03-18 10:14:45 -07002181 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002182 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002183 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002184 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2185 self.assertTrue(self.sched._areAllBuildsComplete())
2186
2187 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002188 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002189 self.assertEqual(self.history[0].result, 'SUCCESS')
2190
James E. Blair879dafb2015-07-17 14:04:49 -07002191 def test_file_head(self):
2192 # This is a regression test for an observed bug. A change
2193 # with a file named "HEAD" in the root directory of the repo
2194 # was processed by a merger. It then was unable to reset the
2195 # repo because of:
2196 # GitCommandError: 'git reset --hard HEAD' returned
2197 # with exit code 128
2198 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2199 # and filename
2200 # Use '--' to separate filenames from revisions'
2201
2202 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002203 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002204 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2205
2206 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2207 self.waitUntilSettled()
2208
2209 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2210 self.waitUntilSettled()
2211
2212 self.assertIn('Build succeeded', A.messages[0])
2213 self.assertIn('Build succeeded', B.messages[0])
2214
James E. Blair70c71582013-03-06 08:50:50 -08002215 def test_file_jobs(self):
2216 "Test that file jobs run only when appropriate"
2217 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002218 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002219 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002220 A.addApproval('Code-Review', 2)
2221 B.addApproval('Code-Review', 2)
2222 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2223 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002224 self.waitUntilSettled()
2225
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002226 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002227 if x.name == 'project-testfile']
2228
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002229 self.assertEqual(len(testfile_jobs), 1)
2230 self.assertEqual(testfile_jobs[0].changes, '1,2')
2231 self.assertEqual(A.data['status'], 'MERGED')
2232 self.assertEqual(A.reported, 2)
2233 self.assertEqual(B.data['status'], 'MERGED')
2234 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002235
Clint Byrum3ee39f32016-11-17 23:45:07 -08002236 def _test_irrelevant_files_jobs(self, should_skip):
2237 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002238 if should_skip:
2239 files = {'ignoreme': 'ignored\n'}
2240 else:
2241 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002242
2243 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002244 'master',
2245 'test irrelevant-files',
2246 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002247 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2248 self.waitUntilSettled()
2249
2250 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002251 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002252
2253 if should_skip:
2254 self.assertEqual([], tested_change_ids)
2255 else:
2256 self.assertIn(change.data['number'], tested_change_ids)
2257
James E. Blaircc324192017-04-19 14:36:08 -07002258 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002259 def test_irrelevant_files_match_skips_job(self):
2260 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002261
James E. Blaircc324192017-04-19 14:36:08 -07002262 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002263 def test_irrelevant_files_no_match_runs_job(self):
2264 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002265
James E. Blaircc324192017-04-19 14:36:08 -07002266 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002267 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002268 files = {'ignoreme': 'ignored\n'}
2269
2270 change = self.fake_gerrit.addFakeChange('org/project',
2271 'master',
2272 'test irrelevant-files',
2273 files=files)
2274 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2275 self.waitUntilSettled()
2276
2277 run_jobs = set([build.name for build in self.history])
2278
2279 self.assertEqual(set(['project-test-nomatch-starts-empty',
2280 'project-test-nomatch-starts-full']), run_jobs)
2281
James E. Blairaf8b2082017-10-03 15:38:27 -07002282 @simple_layout('layouts/job-vars.yaml')
2283 def test_inherited_job_variables(self):
2284 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2285 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2286 self.waitUntilSettled()
2287 self.assertHistory([
2288 dict(name='parentjob', result='SUCCESS'),
2289 dict(name='child1', result='SUCCESS'),
2290 dict(name='child2', result='SUCCESS'),
2291 dict(name='child3', result='SUCCESS'),
2292 ], ordered=False)
2293 j = self.getJobFromHistory('parentjob')
2294 rp = set([p['name'] for p in j.parameters['projects']])
2295 self.assertEqual(j.parameters['vars']['override'], 0)
2296 self.assertEqual(j.parameters['vars']['child1override'], 0)
2297 self.assertEqual(j.parameters['vars']['parent'], 0)
2298 self.assertFalse('child1' in j.parameters['vars'])
2299 self.assertFalse('child2' in j.parameters['vars'])
2300 self.assertFalse('child3' in j.parameters['vars'])
2301 self.assertEqual(rp, set(['org/project', 'org/project0',
2302 'org/project0']))
2303 j = self.getJobFromHistory('child1')
2304 rp = set([p['name'] for p in j.parameters['projects']])
2305 self.assertEqual(j.parameters['vars']['override'], 1)
2306 self.assertEqual(j.parameters['vars']['child1override'], 1)
2307 self.assertEqual(j.parameters['vars']['parent'], 0)
2308 self.assertEqual(j.parameters['vars']['child1'], 1)
2309 self.assertFalse('child2' in j.parameters['vars'])
2310 self.assertFalse('child3' in j.parameters['vars'])
2311 self.assertEqual(rp, set(['org/project', 'org/project0',
2312 'org/project1']))
2313 j = self.getJobFromHistory('child2')
2314 rp = set([p['name'] for p in j.parameters['projects']])
2315 self.assertEqual(j.parameters['vars']['override'], 2)
2316 self.assertEqual(j.parameters['vars']['child1override'], 0)
2317 self.assertEqual(j.parameters['vars']['parent'], 0)
2318 self.assertFalse('child1' in j.parameters['vars'])
2319 self.assertEqual(j.parameters['vars']['child2'], 2)
2320 self.assertFalse('child3' in j.parameters['vars'])
2321 self.assertEqual(rp, set(['org/project', 'org/project0',
2322 'org/project2']))
2323 j = self.getJobFromHistory('child3')
2324 rp = set([p['name'] for p in j.parameters['projects']])
2325 self.assertEqual(j.parameters['vars']['override'], 3)
2326 self.assertEqual(j.parameters['vars']['child1override'], 0)
2327 self.assertEqual(j.parameters['vars']['parent'], 0)
2328 self.assertFalse('child1' in j.parameters['vars'])
2329 self.assertFalse('child2' in j.parameters['vars'])
2330 self.assertEqual(j.parameters['vars']['child3'], 3)
2331 self.assertEqual(rp, set(['org/project', 'org/project0',
2332 'org/project3']))
2333
James E. Blairc8a1e052014-02-25 09:29:26 -08002334 def test_queue_names(self):
2335 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002336 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002337 (trusted, project1) = tenant.getProject('org/project1')
2338 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002339 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2340 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2341 self.assertEqual(q1.name, 'integrated')
2342 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002343
James E. Blair64ed6f22013-07-10 14:07:23 -07002344 def test_queue_precedence(self):
2345 "Test that queue precedence works"
2346
2347 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002348 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2350 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002351 A.addApproval('Code-Review', 2)
2352 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002353
2354 self.waitUntilSettled()
2355 self.gearman_server.hold_jobs_in_queue = False
2356 self.gearman_server.release()
2357 self.waitUntilSettled()
2358
James E. Blair8de58bd2013-07-18 16:23:33 -07002359 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002360 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002361 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002362 self.waitUntilSettled()
2363
James E. Blair64ed6f22013-07-10 14:07:23 -07002364 self.log.debug(self.history)
2365 self.assertEqual(self.history[0].pipeline, 'gate')
2366 self.assertEqual(self.history[1].pipeline, 'check')
2367 self.assertEqual(self.history[2].pipeline, 'gate')
2368 self.assertEqual(self.history[3].pipeline, 'gate')
2369 self.assertEqual(self.history[4].pipeline, 'check')
2370 self.assertEqual(self.history[5].pipeline, 'check')
2371
Clark Boylana5edbe42014-06-03 16:39:10 -07002372 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002373 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002374 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002375 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002376 A.addApproval('Code-Review', 2)
2377 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002378 self.waitUntilSettled()
2379
Paul Belanger174a8272017-03-14 13:20:10 -04002380 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002381 self.waitUntilSettled()
2382
James E. Blair1843a552013-07-03 14:19:52 -07002383 port = self.webapp.server.socket.getsockname()[1]
2384
Adam Gandelman77a12c72017-02-03 17:43:43 -08002385 req = urllib.request.Request(
2386 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002387 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002388 headers = f.info()
2389 self.assertIn('Content-Length', headers)
2390 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002391 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2392 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002393 self.assertIn('Access-Control-Allow-Origin', headers)
2394 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002395 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002396 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002397 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002398
Paul Belanger174a8272017-03-14 13:20:10 -04002399 self.executor_server.hold_jobs_in_build = False
2400 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002401 self.waitUntilSettled()
2402
2403 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002404 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002405 for p in data['pipelines']:
2406 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002407 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002408 self.assertEqual(q['window'], 20)
2409 else:
2410 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002411 for head in q['heads']:
2412 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002413 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002414 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002415 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002416 status_jobs.append(job)
2417 self.assertEqual('project-merge', status_jobs[0]['name'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002418 # TODO(mordred) pull uuids from self.builds
Monty Taylor51139a02016-05-24 11:28:10 -05002419 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002420 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2421 uuid=status_jobs[0]['uuid']),
2422 status_jobs[0]['url'])
2423 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002424 'finger://{hostname}/{uuid}'.format(
2425 hostname=self.executor_server.hostname,
2426 uuid=status_jobs[0]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002427 status_jobs[0]['finger_url'])
Monty Taylorde8242c2017-02-23 20:29:53 -06002428 # TOOD(mordred) configure a success-url on the base job
Monty Taylor51139a02016-05-24 11:28:10 -05002429 self.assertEqual(
2430 'finger://{hostname}/{uuid}'.format(
2431 hostname=self.executor_server.hostname,
2432 uuid=status_jobs[0]['uuid']),
2433 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002434 self.assertEqual('project-test1', status_jobs[1]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002435 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002436 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2437 uuid=status_jobs[1]['uuid']),
2438 status_jobs[1]['url'])
2439 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002440 'finger://{hostname}/{uuid}'.format(
2441 hostname=self.executor_server.hostname,
2442 uuid=status_jobs[1]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002443 status_jobs[1]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002444 self.assertEqual(
2445 'finger://{hostname}/{uuid}'.format(
2446 hostname=self.executor_server.hostname,
2447 uuid=status_jobs[1]['uuid']),
2448 status_jobs[1]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002449
2450 self.assertEqual('project-test2', status_jobs[2]['name'])
Monty Taylor51139a02016-05-24 11:28:10 -05002451 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002452 'static/stream.html?uuid={uuid}&logfile=console.log'.format(
2453 uuid=status_jobs[2]['uuid']),
2454 status_jobs[2]['url'])
2455 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05002456 'finger://{hostname}/{uuid}'.format(
2457 hostname=self.executor_server.hostname,
2458 uuid=status_jobs[2]['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02002459 status_jobs[2]['finger_url'])
Monty Taylor51139a02016-05-24 11:28:10 -05002460 self.assertEqual(
2461 'finger://{hostname}/{uuid}'.format(
2462 hostname=self.executor_server.hostname,
2463 uuid=status_jobs[2]['uuid']),
2464 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002465
Tobias Henkel65639f82017-07-10 10:25:42 +02002466 # check job dependencies
2467 self.assertIsNotNone(status_jobs[0]['dependencies'])
2468 self.assertIsNotNone(status_jobs[1]['dependencies'])
2469 self.assertIsNotNone(status_jobs[2]['dependencies'])
2470 self.assertEqual(len(status_jobs[0]['dependencies']), 0)
2471 self.assertEqual(len(status_jobs[1]['dependencies']), 1)
2472 self.assertEqual(len(status_jobs[2]['dependencies']), 1)
2473 self.assertIn('project-merge', status_jobs[1]['dependencies'])
2474 self.assertIn('project-merge', status_jobs[2]['dependencies'])
2475
James E. Blaircdccd972013-07-01 12:10:22 -07002476 def test_live_reconfiguration(self):
2477 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002478 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002479 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002480 A.addApproval('Code-Review', 2)
2481 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002482 self.waitUntilSettled()
2483
2484 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002485 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002486
Paul Belanger174a8272017-03-14 13:20:10 -04002487 self.executor_server.hold_jobs_in_build = False
2488 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002489 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002490 self.assertEqual(self.getJobFromHistory('project-merge').result,
2491 'SUCCESS')
2492 self.assertEqual(self.getJobFromHistory('project-test1').result,
2493 'SUCCESS')
2494 self.assertEqual(self.getJobFromHistory('project-test2').result,
2495 'SUCCESS')
2496 self.assertEqual(A.data['status'], 'MERGED')
2497 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002498
James E. Blair027ba992017-09-20 13:48:32 -07002499 def test_live_reconfiguration_abort(self):
2500 # Raise an exception during reconfiguration and verify we
2501 # still function.
2502 self.executor_server.hold_jobs_in_build = True
2503 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2504 A.addApproval('Code-Review', 2)
2505 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2506 self.waitUntilSettled()
2507
2508 tenant = self.sched.abide.tenants.get('tenant-one')
2509 pipeline = tenant.layout.pipelines['gate']
2510 change = pipeline.getAllItems()[0].change
2511 # Set this to an invalid value to cause an exception during
2512 # reconfiguration.
2513 change.branch = None
2514
2515 self.sched.reconfigure(self.config)
2516 self.waitUntilSettled()
2517
2518 self.executor_server.hold_jobs_in_build = False
2519 self.executor_server.release()
2520
2521 self.waitUntilSettled()
2522 self.assertEqual(self.getJobFromHistory('project-merge').result,
2523 'ABORTED')
2524 self.assertEqual(A.data['status'], 'NEW')
2525 # The final report fails because of the invalid value set above.
2526 self.assertEqual(A.reported, 1)
2527
James E. Blair6bc782d2015-07-17 16:20:21 -07002528 def test_live_reconfiguration_merge_conflict(self):
2529 # A real-world bug: a change in a gate queue has a merge
2530 # conflict and a job is added to its project while it's
2531 # sitting in the queue. The job gets added to the change and
2532 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002533 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002534
2535 # This change is fine. It's here to stop the queue long
2536 # enough for the next change to be subject to the
2537 # reconfiguration, as well as to provide a conflict for the
2538 # next change. This change will succeed and merge.
2539 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002540 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002541 A.addApproval('Code-Review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002542
2543 # This change will be in merge conflict. During the
2544 # reconfiguration, we will add a job. We want to make sure
2545 # that doesn't cause it to get stuck.
2546 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002547 B.addPatchset({'conflict': 'B'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002548 B.addApproval('Code-Review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002549
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002550 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
2551 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002552
2553 self.waitUntilSettled()
2554
2555 # No jobs have run yet
2556 self.assertEqual(A.data['status'], 'NEW')
2557 self.assertEqual(A.reported, 1)
2558 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002559 self.assertEqual(len(self.history), 0)
2560
2561 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002562 self.commitConfigUpdate('common-config',
2563 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002564 self.sched.reconfigure(self.config)
2565 self.waitUntilSettled()
2566
Paul Belanger174a8272017-03-14 13:20:10 -04002567 self.executor_server.hold_jobs_in_build = False
2568 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002569 self.waitUntilSettled()
2570
2571 self.assertEqual(A.data['status'], 'MERGED')
2572 self.assertEqual(A.reported, 2)
2573 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002574 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002575 self.assertEqual(self.getJobFromHistory('project-merge').result,
2576 'SUCCESS')
2577 self.assertEqual(self.getJobFromHistory('project-test1').result,
2578 'SUCCESS')
2579 self.assertEqual(self.getJobFromHistory('project-test2').result,
2580 'SUCCESS')
2581 self.assertEqual(self.getJobFromHistory('project-test3').result,
2582 'SUCCESS')
2583 self.assertEqual(len(self.history), 4)
2584
James E. Blair400e8fd2015-07-30 17:44:45 -07002585 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002586 # An extrapolation of test_live_reconfiguration_merge_conflict
2587 # that tests a job added to a job tree with a failed root does
2588 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002589 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002590
2591 # This change is fine. It's here to stop the queue long
2592 # enough for the next change to be subject to the
2593 # reconfiguration. This change will succeed and merge.
2594 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002595 A.addPatchset({'conflict': 'A'})
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002596 A.addApproval('Code-Review', 2)
2597 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002598 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002599 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002600 self.waitUntilSettled()
2601
2602 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002603 self.executor_server.failJob('project-merge', B)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002604 B.addApproval('Code-Review', 2)
2605 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002606 self.waitUntilSettled()
2607
Paul Belanger174a8272017-03-14 13:20:10 -04002608 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002609 self.waitUntilSettled()
2610
2611 # Both -merge jobs have run, but no others.
2612 self.assertEqual(A.data['status'], 'NEW')
2613 self.assertEqual(A.reported, 1)
2614 self.assertEqual(B.data['status'], 'NEW')
2615 self.assertEqual(B.reported, 1)
2616 self.assertEqual(self.history[0].result, 'SUCCESS')
2617 self.assertEqual(self.history[0].name, 'project-merge')
2618 self.assertEqual(self.history[1].result, 'FAILURE')
2619 self.assertEqual(self.history[1].name, 'project-merge')
2620 self.assertEqual(len(self.history), 2)
2621
2622 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002623 self.commitConfigUpdate('common-config',
2624 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002625 self.sched.reconfigure(self.config)
2626 self.waitUntilSettled()
2627
Paul Belanger174a8272017-03-14 13:20:10 -04002628 self.executor_server.hold_jobs_in_build = False
2629 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002630 self.waitUntilSettled()
2631
2632 self.assertEqual(A.data['status'], 'MERGED')
2633 self.assertEqual(A.reported, 2)
2634 self.assertEqual(B.data['status'], 'NEW')
2635 self.assertEqual(B.reported, 2)
2636 self.assertEqual(self.history[0].result, 'SUCCESS')
2637 self.assertEqual(self.history[0].name, 'project-merge')
2638 self.assertEqual(self.history[1].result, 'FAILURE')
2639 self.assertEqual(self.history[1].name, 'project-merge')
2640 self.assertEqual(self.history[2].result, 'SUCCESS')
2641 self.assertEqual(self.history[3].result, 'SUCCESS')
2642 self.assertEqual(self.history[4].result, 'SUCCESS')
2643 self.assertEqual(len(self.history), 5)
2644
James E. Blair400e8fd2015-07-30 17:44:45 -07002645 def test_live_reconfiguration_failed_job(self):
2646 # Test that a change with a removed failing job does not
2647 # disrupt reconfiguration. If a change has a failed job and
2648 # that job is removed during a reconfiguration, we observed a
2649 # bug where the code to re-set build statuses would run on
2650 # that build and raise an exception because the job no longer
2651 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002652 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002653
2654 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2655
2656 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002657 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002658
2659 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2660 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002661 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002662 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002663 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002664 self.waitUntilSettled()
2665
2666 self.assertEqual(A.data['status'], 'NEW')
2667 self.assertEqual(A.reported, 0)
2668
2669 self.assertEqual(self.getJobFromHistory('project-merge').result,
2670 'SUCCESS')
2671 self.assertEqual(self.getJobFromHistory('project-test1').result,
2672 'FAILURE')
2673 self.assertEqual(len(self.history), 2)
2674
2675 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002676 self.commitConfigUpdate('common-config',
2677 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002678 self.sched.reconfigure(self.config)
2679 self.waitUntilSettled()
2680
Paul Belanger174a8272017-03-14 13:20:10 -04002681 self.executor_server.hold_jobs_in_build = False
2682 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002683 self.waitUntilSettled()
2684
2685 self.assertEqual(self.getJobFromHistory('project-test2').result,
2686 'SUCCESS')
2687 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2688 'SUCCESS')
2689 self.assertEqual(len(self.history), 4)
2690
2691 self.assertEqual(A.data['status'], 'NEW')
2692 self.assertEqual(A.reported, 1)
2693 self.assertIn('Build succeeded', A.messages[0])
2694 # Ensure the removed job was not included in the report.
2695 self.assertNotIn('project-test1', A.messages[0])
2696
James E. Blairfe707d12015-08-05 15:18:15 -07002697 def test_live_reconfiguration_shared_queue(self):
2698 # Test that a change with a failing job which was removed from
2699 # this project but otherwise still exists in the system does
2700 # not disrupt reconfiguration.
2701
Paul Belanger174a8272017-03-14 13:20:10 -04002702 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002703
2704 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2705
Paul Belanger174a8272017-03-14 13:20:10 -04002706 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002707
2708 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2709 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002710 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002711 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002712 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002713 self.waitUntilSettled()
2714
2715 self.assertEqual(A.data['status'], 'NEW')
2716 self.assertEqual(A.reported, 0)
2717
Clint Byrum69663d42017-05-01 14:51:04 -07002718 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002719 'SUCCESS')
2720 self.assertEqual(self.getJobFromHistory(
2721 'project1-project2-integration').result, 'FAILURE')
2722 self.assertEqual(len(self.history), 2)
2723
2724 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002725 self.commitConfigUpdate(
2726 'common-config',
2727 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002728 self.sched.reconfigure(self.config)
2729 self.waitUntilSettled()
2730
Paul Belanger174a8272017-03-14 13:20:10 -04002731 self.executor_server.hold_jobs_in_build = False
2732 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002733 self.waitUntilSettled()
2734
Clint Byrum69663d42017-05-01 14:51:04 -07002735 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002736 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002737 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002738 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002739 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002740 'SUCCESS')
2741 self.assertEqual(self.getJobFromHistory(
2742 'project1-project2-integration').result, 'FAILURE')
2743 self.assertEqual(len(self.history), 4)
2744
2745 self.assertEqual(A.data['status'], 'NEW')
2746 self.assertEqual(A.reported, 1)
2747 self.assertIn('Build succeeded', A.messages[0])
2748 # Ensure the removed job was not included in the report.
2749 self.assertNotIn('project1-project2-integration', A.messages[0])
2750
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002751 def test_double_live_reconfiguration_shared_queue(self):
2752 # This was a real-world regression. A change is added to
2753 # gate; a reconfigure happens, a second change which depends
2754 # on the first is added, and a second reconfiguration happens.
2755 # Ensure that both changes merge.
2756
2757 # A failure may indicate incorrect caching or cleaning up of
2758 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002759 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002760
2761 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2762 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2763 B.setDependsOn(A, 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002764 A.addApproval('Code-Review', 2)
2765 B.addApproval('Code-Review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002766
2767 # Add the parent change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002768 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002769 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002770 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002771 self.waitUntilSettled()
2772
2773 # Reconfigure (with only one change in the pipeline).
2774 self.sched.reconfigure(self.config)
2775 self.waitUntilSettled()
2776
2777 # Add the child change.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002778 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002779 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002780 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002781 self.waitUntilSettled()
2782
2783 # Reconfigure (with both in the pipeline).
2784 self.sched.reconfigure(self.config)
2785 self.waitUntilSettled()
2786
Paul Belanger174a8272017-03-14 13:20:10 -04002787 self.executor_server.hold_jobs_in_build = False
2788 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002789 self.waitUntilSettled()
2790
2791 self.assertEqual(len(self.history), 8)
2792
2793 self.assertEqual(A.data['status'], 'MERGED')
2794 self.assertEqual(A.reported, 2)
2795 self.assertEqual(B.data['status'], 'MERGED')
2796 self.assertEqual(B.reported, 2)
2797
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002798 def test_live_reconfiguration_del_project(self):
2799 # Test project deletion from layout
2800 # while changes are enqueued
2801
Paul Belanger174a8272017-03-14 13:20:10 -04002802 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002803 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2804 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2805 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2806
2807 # A Depends-On: B
2808 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2809 A.subject, B.data['id'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002810 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002811
2812 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2813 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2814 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002815 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002816 self.waitUntilSettled()
2817 self.assertEqual(len(self.builds), 5)
2818
2819 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002820 self.commitConfigUpdate(
2821 'common-config',
2822 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002823 self.sched.reconfigure(self.config)
2824 self.waitUntilSettled()
2825
2826 # Builds for C aborted, builds for A succeed,
2827 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002828 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002829 self.assertEqual(job_c.changes, '3,1')
2830 self.assertEqual(job_c.result, 'ABORTED')
2831
Paul Belanger174a8272017-03-14 13:20:10 -04002832 self.executor_server.hold_jobs_in_build = False
2833 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002834 self.waitUntilSettled()
2835
Clint Byrum0149ada2017-04-04 12:22:01 -07002836 self.assertEqual(
2837 self.getJobFromHistory('project-test1', 'org/project').changes,
2838 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002839
2840 self.assertEqual(A.data['status'], 'NEW')
2841 self.assertEqual(B.data['status'], 'NEW')
2842 self.assertEqual(C.data['status'], 'NEW')
2843 self.assertEqual(A.reported, 1)
2844 self.assertEqual(B.reported, 0)
2845 self.assertEqual(C.reported, 0)
2846
Clint Byrum0149ada2017-04-04 12:22:01 -07002847 tenant = self.sched.abide.tenants.get('tenant-one')
2848 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002849 self.assertIn('Build succeeded', A.messages[0])
2850
James E. Blair287c06d2013-07-24 10:39:30 -07002851 def test_delayed_repo_init(self):
James E. Blair287c06d2013-07-24 10:39:30 -07002852 self.init_repo("org/new-project")
Clint Byrum627ba362017-08-14 13:20:40 -07002853 files = {'README': ''}
2854 self.addCommitToRepo("org/new-project", 'Initial commit',
2855 files=files, tag='init')
2856 self.newTenantConfig('tenants/delayed-repo-init.yaml')
2857 self.commitConfigUpdate(
2858 'common-config',
2859 'layouts/delayed-repo-init.yaml')
2860 self.sched.reconfigure(self.config)
2861 self.waitUntilSettled()
2862
James E. Blair287c06d2013-07-24 10:39:30 -07002863 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2864
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002865 A.addApproval('Code-Review', 2)
2866 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002867 self.waitUntilSettled()
2868 self.assertEqual(self.getJobFromHistory('project-merge').result,
2869 'SUCCESS')
2870 self.assertEqual(self.getJobFromHistory('project-test1').result,
2871 'SUCCESS')
2872 self.assertEqual(self.getJobFromHistory('project-test2').result,
2873 'SUCCESS')
2874 self.assertEqual(A.data['status'], 'MERGED')
2875 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002876
James E. Blair9ea70072017-04-19 16:05:30 -07002877 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002878 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002879 self.init_repo("org/delete-project")
2880 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2881
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002882 A.addApproval('Code-Review', 2)
2883 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002884 self.waitUntilSettled()
2885 self.assertEqual(self.getJobFromHistory('project-merge').result,
2886 'SUCCESS')
2887 self.assertEqual(self.getJobFromHistory('project-test1').result,
2888 'SUCCESS')
2889 self.assertEqual(self.getJobFromHistory('project-test2').result,
2890 'SUCCESS')
2891 self.assertEqual(A.data['status'], 'MERGED')
2892 self.assertEqual(A.reported, 2)
2893
2894 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002895 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002896 if os.path.exists(os.path.join(self.merger_src_root, p)):
2897 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002898 if os.path.exists(os.path.join(self.executor_src_root, p)):
2899 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002900
2901 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2902
Tobias Henkelbf24fd12017-07-27 06:13:07 +02002903 B.addApproval('Code-Review', 2)
2904 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002905 self.waitUntilSettled()
2906 self.assertEqual(self.getJobFromHistory('project-merge').result,
2907 'SUCCESS')
2908 self.assertEqual(self.getJobFromHistory('project-test1').result,
2909 'SUCCESS')
2910 self.assertEqual(self.getJobFromHistory('project-test2').result,
2911 'SUCCESS')
2912 self.assertEqual(B.data['status'], 'MERGED')
2913 self.assertEqual(B.reported, 2)
2914
James E. Blair892cca62017-08-09 11:36:58 -07002915 @simple_layout('layouts/untrusted-secrets.yaml')
2916 def test_untrusted_secrets(self):
2917 "Test untrusted secrets"
2918 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2919 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2920 self.waitUntilSettled()
2921
2922 self.assertHistory([])
2923 self.assertEqual(A.patchsets[0]['approvals'][0]['value'], "-1")
James E. Blair8eb564a2017-08-10 09:21:41 -07002924 self.assertIn('does not allow post-review job',
James E. Blair892cca62017-08-09 11:36:58 -07002925 A.messages[0])
2926
James E. Blaire68d43f2017-04-19 14:03:30 -07002927 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002928 def test_tags(self):
2929 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002930 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2931 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2932 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2933 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2934 self.waitUntilSettled()
2935
James E. Blaire68d43f2017-04-19 14:03:30 -07002936 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002937
2938 results = {self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002939 project='org/project1').uuid: ['extratag', 'merge'],
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002940 self.getJobFromHistory('merge',
James E. Blair9d46f092017-07-20 16:06:20 -07002941 project='org/project2').uuid: ['merge']}
James E. Blair456f2fb2016-02-09 09:29:33 -08002942
2943 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002944 self.assertEqual(results.get(build.uuid, ''),
James E. Blair9d46f092017-07-20 16:06:20 -07002945 build.parameters['zuul'].get('jobtags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002946
James E. Blair63bb0ef2013-07-29 17:14:51 -07002947 def test_timer(self):
2948 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002949 # This test can not use simple_layout because it must start
2950 # with a configuration which does not include a
2951 # timer-triggered job so that we have an opportunity to set
2952 # the hold flag before the first job.
James E. Blair21037782017-07-19 11:56:55 -07002953 self.create_branch('org/project', 'stable')
Paul Belanger174a8272017-03-14 13:20:10 -04002954 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002955 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002956 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002957
Clark Boylan3ee090a2014-04-03 20:55:09 -07002958 # The pipeline triggers every second, so we should have seen
2959 # several by now.
2960 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002961 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002962
2963 self.assertEqual(len(self.builds), 2)
2964
James E. Blair63bb0ef2013-07-29 17:14:51 -07002965 port = self.webapp.server.socket.getsockname()[1]
2966
Clint Byrum5e729e92017-02-08 06:34:57 -08002967 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002968 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002969 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002970 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002971
Paul Belanger174a8272017-03-14 13:20:10 -04002972 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002973 # Stop queuing timer triggered jobs so that the assertions
2974 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002975 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002976 self.sched.reconfigure(self.config)
James E. Blair78ae4782017-08-02 14:19:10 -07002977 self.waitUntilSettled()
2978 # If APScheduler is in mid-event when we remove the job, we
2979 # can end up with one more event firing, so give it an extra
2980 # second to settle.
2981 time.sleep(1)
2982 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002983 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002984 self.waitUntilSettled()
2985
James E. Blair21037782017-07-19 11:56:55 -07002986 self.assertHistory([
2987 dict(name='project-bitrot', result='SUCCESS',
2988 ref='refs/heads/master'),
2989 dict(name='project-bitrot', result='SUCCESS',
2990 ref='refs/heads/stable'),
2991 ], ordered=False)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002992
2993 data = json.loads(data)
2994 status_jobs = set()
2995 for p in data['pipelines']:
2996 for q in p['change_queues']:
2997 for head in q['heads']:
2998 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002999 for job in change['jobs']:
3000 status_jobs.add(job['name'])
James E. Blair21037782017-07-19 11:56:55 -07003001 self.assertIn('project-bitrot', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003002
James E. Blair4f6033c2014-03-27 15:49:09 -07003003 def test_idle(self):
3004 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003005 # This test can not use simple_layout because it must start
3006 # with a configuration which does not include a
3007 # timer-triggered job so that we have an opportunity to set
3008 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003009 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07003010
Clark Boylan3ee090a2014-04-03 20:55:09 -07003011 for x in range(1, 3):
3012 # Test that timer triggers periodic jobs even across
3013 # layout config reloads.
3014 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003015 self.commitConfigUpdate('common-config',
3016 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003017 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003018 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07003019
Clark Boylan3ee090a2014-04-03 20:55:09 -07003020 # The pipeline triggers every second, so we should have seen
3021 # several by now.
3022 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07003023
3024 # Stop queuing timer triggered jobs so that the assertions
3025 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003026 self.commitConfigUpdate('common-config',
3027 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003028 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08003029 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003030 # If APScheduler is in mid-event when we remove the job,
3031 # we can end up with one more event firing, so give it an
3032 # extra second to settle.
3033 time.sleep(1)
3034 self.waitUntilSettled()
James E. Blair21037782017-07-19 11:56:55 -07003035 self.assertEqual(len(self.builds), 1,
Clint Byrum58264dc2017-02-07 21:21:22 -08003036 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04003037 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003038 self.waitUntilSettled()
3039 self.assertEqual(len(self.builds), 0)
James E. Blair21037782017-07-19 11:56:55 -07003040 self.assertEqual(len(self.history), x)
James E. Blair4f6033c2014-03-27 15:49:09 -07003041
James E. Blaircc324192017-04-19 14:36:08 -07003042 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003043 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003044 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3045 self.waitUntilSettled()
3046
3047 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3048 self.waitUntilSettled()
3049
James E. Blairff80a2f2013-12-27 13:24:06 -08003050 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003051
3052 # A.messages only holds what FakeGerrit places in it. Thus we
3053 # work on the knowledge of what the first message should be as
3054 # it is only configured to go to SMTP.
3055
3056 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003057 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003058 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003059 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003060 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08003061 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003062
3063 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003064 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003065 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003066 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10003067 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08003068 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08003069
James E. Blaire5910202013-12-27 09:50:31 -08003070 def test_timer_smtp(self):
3071 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003072 # This test can not use simple_layout because it must start
3073 # with a configuration which does not include a
3074 # timer-triggered job so that we have an opportunity to set
3075 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04003076 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003077 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08003078 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08003079
Clark Boylan3ee090a2014-04-03 20:55:09 -07003080 # The pipeline triggers every second, so we should have seen
3081 # several by now.
3082 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08003083 self.waitUntilSettled()
3084
Clark Boylan3ee090a2014-04-03 20:55:09 -07003085 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003086 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003087 self.waitUntilSettled()
3088 self.assertEqual(len(self.history), 2)
3089
James E. Blaire5910202013-12-27 09:50:31 -08003090 self.assertEqual(self.getJobFromHistory(
3091 'project-bitrot-stable-old').result, 'SUCCESS')
3092 self.assertEqual(self.getJobFromHistory(
3093 'project-bitrot-stable-older').result, 'SUCCESS')
3094
James E. Blairff80a2f2013-12-27 13:24:06 -08003095 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08003096
3097 # A.messages only holds what FakeGerrit places in it. Thus we
3098 # work on the knowledge of what the first message should be as
3099 # it is only configured to go to SMTP.
3100
3101 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08003102 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003103 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08003104 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08003105 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08003106 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08003107
Clark Boylan3ee090a2014-04-03 20:55:09 -07003108 # Stop queuing timer triggered jobs and let any that may have
3109 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07003110 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003111 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07003112 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003113 # If APScheduler is in mid-event when we remove the job, we
3114 # can end up with one more event firing, so give it an extra
3115 # second to settle.
3116 time.sleep(1)
3117 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003118 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07003119 self.waitUntilSettled()
3120
Joshua Hesketh25695cb2017-03-06 12:50:04 +11003121 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003122 def test_timer_sshkey(self):
3123 "Test that a periodic job can setup SSH key authentication"
3124 self.worker.hold_jobs_in_build = True
3125 self.config.set('zuul', 'layout_config',
3126 'tests/fixtures/layout-timer.yaml')
3127 self.sched.reconfigure(self.config)
3128 self.registerJobs()
3129
3130 # The pipeline triggers every second, so we should have seen
3131 # several by now.
3132 time.sleep(5)
3133 self.waitUntilSettled()
3134
3135 self.assertEqual(len(self.builds), 2)
3136
3137 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
3138 self.assertTrue(os.path.isfile(ssh_wrapper))
3139 with open(ssh_wrapper) as f:
3140 ssh_wrapper_content = f.read()
3141 self.assertIn("fake_id_rsa", ssh_wrapper_content)
3142 # In the unit tests Merger runs in the same process,
3143 # so we see its' environment variables
3144 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
3145
3146 self.worker.release('.*')
3147 self.waitUntilSettled()
3148 self.assertEqual(len(self.history), 2)
3149
3150 self.assertEqual(self.getJobFromHistory(
3151 'project-bitrot-stable-old').result, 'SUCCESS')
3152 self.assertEqual(self.getJobFromHistory(
3153 'project-bitrot-stable-older').result, 'SUCCESS')
3154
3155 # Stop queuing timer triggered jobs and let any that may have
3156 # queued through so that end of test assertions pass.
3157 self.config.set('zuul', 'layout_config',
3158 'tests/fixtures/layout-no-timer.yaml')
3159 self.sched.reconfigure(self.config)
3160 self.registerJobs()
3161 self.waitUntilSettled()
James E. Blair78ae4782017-08-02 14:19:10 -07003162 # If APScheduler is in mid-event when we remove the job, we
3163 # can end up with one more event firing, so give it an extra
3164 # second to settle.
3165 time.sleep(1)
3166 self.waitUntilSettled()
Evgeny Antyshevda90a502017-02-08 08:42:59 +00003167 self.worker.release('.*')
3168 self.waitUntilSettled()
3169
James E. Blair91e34592015-07-31 16:45:59 -07003170 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08003171 "Test that the RPC client can enqueue a change"
3172 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003173 A.addApproval('Code-Review', 2)
3174 A.addApproval('Approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08003175
3176 client = zuul.rpcclient.RPCClient('127.0.0.1',
3177 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003178 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003179 r = client.enqueue(tenant='tenant-one',
3180 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003181 project='org/project',
3182 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003183 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003184 self.waitUntilSettled()
3185 self.assertEqual(self.getJobFromHistory('project-merge').result,
3186 'SUCCESS')
3187 self.assertEqual(self.getJobFromHistory('project-test1').result,
3188 'SUCCESS')
3189 self.assertEqual(self.getJobFromHistory('project-test2').result,
3190 'SUCCESS')
3191 self.assertEqual(A.data['status'], 'MERGED')
3192 self.assertEqual(A.reported, 2)
3193 self.assertEqual(r, True)
3194
James E. Blair91e34592015-07-31 16:45:59 -07003195 def test_client_enqueue_ref(self):
3196 "Test that the RPC client can enqueue a ref"
James E. Blair289f5932017-07-27 15:02:29 -07003197 p = "review.example.com/org/project"
3198 upstream = self.getUpstreamRepos([p])
3199 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3200 A.setMerged()
3201 A_commit = str(upstream[p].commit('master'))
3202 self.log.debug("A commit: %s" % A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003203
3204 client = zuul.rpcclient.RPCClient('127.0.0.1',
3205 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003206 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07003207 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04003208 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07003209 pipeline='post',
3210 project='org/project',
3211 trigger='gerrit',
3212 ref='master',
3213 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
James E. Blair289f5932017-07-27 15:02:29 -07003214 newrev=A_commit)
James E. Blair91e34592015-07-31 16:45:59 -07003215 self.waitUntilSettled()
3216 job_names = [x.name for x in self.history]
3217 self.assertEqual(len(self.history), 1)
3218 self.assertIn('project-post', job_names)
3219 self.assertEqual(r, True)
3220
James E. Blairad28e912013-11-27 10:43:22 -08003221 def test_client_enqueue_negative(self):
3222 "Test that the RPC client returns errors"
3223 client = zuul.rpcclient.RPCClient('127.0.0.1',
3224 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003225 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08003226 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04003227 "Invalid tenant"):
3228 r = client.enqueue(tenant='tenant-foo',
3229 pipeline='gate',
3230 project='org/project',
3231 trigger='gerrit',
3232 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04003233 self.assertEqual(r, False)
3234
3235 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08003236 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003237 r = client.enqueue(tenant='tenant-one',
3238 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003239 project='project-does-not-exist',
3240 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003241 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003242 self.assertEqual(r, False)
3243
3244 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3245 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003246 r = client.enqueue(tenant='tenant-one',
3247 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08003248 project='org/project',
3249 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003250 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003251 self.assertEqual(r, False)
3252
3253 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3254 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003255 r = client.enqueue(tenant='tenant-one',
3256 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003257 project='org/project',
3258 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08003259 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003260 self.assertEqual(r, False)
3261
3262 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
3263 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04003264 r = client.enqueue(tenant='tenant-one',
3265 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08003266 project='org/project',
3267 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003268 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003269 self.assertEqual(r, False)
3270
3271 self.waitUntilSettled()
3272 self.assertEqual(len(self.history), 0)
3273 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003274
3275 def test_client_promote(self):
3276 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003277 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003278 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3279 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3280 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003281 A.addApproval('Code-Review', 2)
3282 B.addApproval('Code-Review', 2)
3283 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003284
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003285 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3286 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3287 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003288
3289 self.waitUntilSettled()
3290
Paul Belangerbaca3132016-11-04 12:49:54 -04003291 tenant = self.sched.abide.tenants.get('tenant-one')
3292 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003293 enqueue_times = {}
3294 for item in items:
3295 enqueue_times[str(item.change)] = item.enqueue_time
3296
James E. Blair36658cf2013-12-06 17:53:48 -08003297 client = zuul.rpcclient.RPCClient('127.0.0.1',
3298 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003299 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003300 r = client.promote(tenant='tenant-one',
3301 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003302 change_ids=['2,1', '3,1'])
3303
Sean Daguef39b9ca2014-01-10 21:34:35 -05003304 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003305 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003306 for item in items:
3307 self.assertEqual(
3308 enqueue_times[str(item.change)], item.enqueue_time)
3309
James E. Blair78acec92014-02-06 07:11:32 -08003310 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003311 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003312 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003313 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003314 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003315 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003316 self.waitUntilSettled()
3317
3318 self.assertEqual(len(self.builds), 6)
3319 self.assertEqual(self.builds[0].name, 'project-test1')
3320 self.assertEqual(self.builds[1].name, 'project-test2')
3321 self.assertEqual(self.builds[2].name, 'project-test1')
3322 self.assertEqual(self.builds[3].name, 'project-test2')
3323 self.assertEqual(self.builds[4].name, 'project-test1')
3324 self.assertEqual(self.builds[5].name, 'project-test2')
3325
Paul Belangerbaca3132016-11-04 12:49:54 -04003326 self.assertTrue(self.builds[0].hasChanges(B))
3327 self.assertFalse(self.builds[0].hasChanges(A))
3328 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003329
Paul Belangerbaca3132016-11-04 12:49:54 -04003330 self.assertTrue(self.builds[2].hasChanges(B))
3331 self.assertTrue(self.builds[2].hasChanges(C))
3332 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003333
Paul Belangerbaca3132016-11-04 12:49:54 -04003334 self.assertTrue(self.builds[4].hasChanges(B))
3335 self.assertTrue(self.builds[4].hasChanges(C))
3336 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003337
Paul Belanger174a8272017-03-14 13:20:10 -04003338 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003339 self.waitUntilSettled()
3340
3341 self.assertEqual(A.data['status'], 'MERGED')
3342 self.assertEqual(A.reported, 2)
3343 self.assertEqual(B.data['status'], 'MERGED')
3344 self.assertEqual(B.reported, 2)
3345 self.assertEqual(C.data['status'], 'MERGED')
3346 self.assertEqual(C.reported, 2)
3347
James E. Blair36658cf2013-12-06 17:53:48 -08003348 self.assertEqual(r, True)
3349
3350 def test_client_promote_dependent(self):
3351 "Test that the RPC client can promote a dependent change"
3352 # C (depends on B) -> B -> A ; then promote C to get:
3353 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003354 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003355 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3356 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3357 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3358
3359 C.setDependsOn(B, 1)
3360
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003361 A.addApproval('Code-Review', 2)
3362 B.addApproval('Code-Review', 2)
3363 C.addApproval('Code-Review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003364
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003365 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3366 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3367 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003368
3369 self.waitUntilSettled()
3370
3371 client = zuul.rpcclient.RPCClient('127.0.0.1',
3372 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003373 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003374 r = client.promote(tenant='tenant-one',
3375 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003376 change_ids=['3,1'])
3377
James E. Blair78acec92014-02-06 07:11:32 -08003378 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003379 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003380 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003381 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003382 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003383 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003384 self.waitUntilSettled()
3385
3386 self.assertEqual(len(self.builds), 6)
3387 self.assertEqual(self.builds[0].name, 'project-test1')
3388 self.assertEqual(self.builds[1].name, 'project-test2')
3389 self.assertEqual(self.builds[2].name, 'project-test1')
3390 self.assertEqual(self.builds[3].name, 'project-test2')
3391 self.assertEqual(self.builds[4].name, 'project-test1')
3392 self.assertEqual(self.builds[5].name, 'project-test2')
3393
Paul Belangerbaca3132016-11-04 12:49:54 -04003394 self.assertTrue(self.builds[0].hasChanges(B))
3395 self.assertFalse(self.builds[0].hasChanges(A))
3396 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003397
Paul Belangerbaca3132016-11-04 12:49:54 -04003398 self.assertTrue(self.builds[2].hasChanges(B))
3399 self.assertTrue(self.builds[2].hasChanges(C))
3400 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003401
Paul Belangerbaca3132016-11-04 12:49:54 -04003402 self.assertTrue(self.builds[4].hasChanges(B))
3403 self.assertTrue(self.builds[4].hasChanges(C))
3404 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003405
Paul Belanger174a8272017-03-14 13:20:10 -04003406 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003407 self.waitUntilSettled()
3408
3409 self.assertEqual(A.data['status'], 'MERGED')
3410 self.assertEqual(A.reported, 2)
3411 self.assertEqual(B.data['status'], 'MERGED')
3412 self.assertEqual(B.reported, 2)
3413 self.assertEqual(C.data['status'], 'MERGED')
3414 self.assertEqual(C.reported, 2)
3415
James E. Blair36658cf2013-12-06 17:53:48 -08003416 self.assertEqual(r, True)
3417
3418 def test_client_promote_negative(self):
3419 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003420 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003421 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003422 A.addApproval('Code-Review', 2)
3423 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003424 self.waitUntilSettled()
3425
3426 client = zuul.rpcclient.RPCClient('127.0.0.1',
3427 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003428 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003429
3430 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003431 r = client.promote(tenant='tenant-one',
3432 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003433 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003434 self.assertEqual(r, False)
3435
3436 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003437 r = client.promote(tenant='tenant-one',
3438 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003439 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003440 self.assertEqual(r, False)
3441
Paul Belanger174a8272017-03-14 13:20:10 -04003442 self.executor_server.hold_jobs_in_build = False
3443 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003444 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003445
James E. Blaircc324192017-04-19 14:36:08 -07003446 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003447 def test_queue_rate_limiting(self):
3448 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003449 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003450 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3451 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3452 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3453
3454 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003455 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003456
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003457 A.addApproval('Code-Review', 2)
3458 B.addApproval('Code-Review', 2)
3459 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003460
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003461 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3462 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3463 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003464 self.waitUntilSettled()
3465
3466 # Only A and B will have their merge jobs queued because
3467 # window is 2.
3468 self.assertEqual(len(self.builds), 2)
3469 self.assertEqual(self.builds[0].name, 'project-merge')
3470 self.assertEqual(self.builds[1].name, 'project-merge')
3471
James E. Blair732ee862017-04-21 07:49:26 -07003472 # Release the merge jobs one at a time.
3473 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003474 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003475 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003476 self.waitUntilSettled()
3477
3478 # Only A and B will have their test jobs queued because
3479 # window is 2.
3480 self.assertEqual(len(self.builds), 4)
3481 self.assertEqual(self.builds[0].name, 'project-test1')
3482 self.assertEqual(self.builds[1].name, 'project-test2')
3483 self.assertEqual(self.builds[2].name, 'project-test1')
3484 self.assertEqual(self.builds[3].name, 'project-test2')
3485
Paul Belanger174a8272017-03-14 13:20:10 -04003486 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003487 self.waitUntilSettled()
3488
James E. Blaircc324192017-04-19 14:36:08 -07003489 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003490 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003491 # A failed so window is reduced by 1 to 1.
3492 self.assertEqual(queue.window, 1)
3493 self.assertEqual(queue.window_floor, 1)
3494 self.assertEqual(A.data['status'], 'NEW')
3495
3496 # Gate is reset and only B's merge job is queued because
3497 # window shrunk to 1.
3498 self.assertEqual(len(self.builds), 1)
3499 self.assertEqual(self.builds[0].name, 'project-merge')
3500
Paul Belanger174a8272017-03-14 13:20:10 -04003501 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003502 self.waitUntilSettled()
3503
3504 # Only B's test jobs are queued because window is still 1.
3505 self.assertEqual(len(self.builds), 2)
3506 self.assertEqual(self.builds[0].name, 'project-test1')
3507 self.assertEqual(self.builds[1].name, 'project-test2')
3508
Paul Belanger174a8272017-03-14 13:20:10 -04003509 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003510 self.waitUntilSettled()
3511
3512 # B was successfully merged so window is increased to 2.
3513 self.assertEqual(queue.window, 2)
3514 self.assertEqual(queue.window_floor, 1)
3515 self.assertEqual(B.data['status'], 'MERGED')
3516
3517 # Only C is left and its merge job is queued.
3518 self.assertEqual(len(self.builds), 1)
3519 self.assertEqual(self.builds[0].name, 'project-merge')
3520
Paul Belanger174a8272017-03-14 13:20:10 -04003521 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003522 self.waitUntilSettled()
3523
3524 # After successful merge job the test jobs for C are queued.
3525 self.assertEqual(len(self.builds), 2)
3526 self.assertEqual(self.builds[0].name, 'project-test1')
3527 self.assertEqual(self.builds[1].name, 'project-test2')
3528
Paul Belanger174a8272017-03-14 13:20:10 -04003529 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003530 self.waitUntilSettled()
3531
3532 # C successfully merged so window is bumped to 3.
3533 self.assertEqual(queue.window, 3)
3534 self.assertEqual(queue.window_floor, 1)
3535 self.assertEqual(C.data['status'], 'MERGED')
3536
James E. Blaircc324192017-04-19 14:36:08 -07003537 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003538 def test_queue_rate_limiting_dependent(self):
3539 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003540 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003541 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3542 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3543 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3544
3545 B.setDependsOn(A, 1)
3546
Paul Belanger174a8272017-03-14 13:20:10 -04003547 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003548
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003549 A.addApproval('Code-Review', 2)
3550 B.addApproval('Code-Review', 2)
3551 C.addApproval('Code-Review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003552
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003553 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3554 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3555 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003556 self.waitUntilSettled()
3557
3558 # Only A and B will have their merge jobs queued because
3559 # window is 2.
3560 self.assertEqual(len(self.builds), 2)
3561 self.assertEqual(self.builds[0].name, 'project-merge')
3562 self.assertEqual(self.builds[1].name, 'project-merge')
3563
James E. Blairf4a5f022017-04-18 14:01:10 -07003564 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003565
3566 # Only A and B will have their test jobs queued because
3567 # window is 2.
3568 self.assertEqual(len(self.builds), 4)
3569 self.assertEqual(self.builds[0].name, 'project-test1')
3570 self.assertEqual(self.builds[1].name, 'project-test2')
3571 self.assertEqual(self.builds[2].name, 'project-test1')
3572 self.assertEqual(self.builds[3].name, 'project-test2')
3573
Paul Belanger174a8272017-03-14 13:20:10 -04003574 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003575 self.waitUntilSettled()
3576
James E. Blaircc324192017-04-19 14:36:08 -07003577 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003578 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003579 # A failed so window is reduced by 1 to 1.
3580 self.assertEqual(queue.window, 1)
3581 self.assertEqual(queue.window_floor, 1)
3582 self.assertEqual(A.data['status'], 'NEW')
3583 self.assertEqual(B.data['status'], 'NEW')
3584
3585 # Gate is reset and only C's merge job is queued because
3586 # window shrunk to 1 and A and B were dequeued.
3587 self.assertEqual(len(self.builds), 1)
3588 self.assertEqual(self.builds[0].name, 'project-merge')
3589
James E. Blairf4a5f022017-04-18 14:01:10 -07003590 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003591
3592 # Only C's test jobs are queued because window is still 1.
3593 self.assertEqual(len(self.builds), 2)
James E. Blairdf25ddc2017-07-08 07:57:09 -07003594 builds = self.getSortedBuilds()
3595 self.assertEqual(builds[0].name, 'project-test1')
3596 self.assertEqual(builds[1].name, 'project-test2')
Clark Boylan7603a372014-01-21 11:43:20 -08003597
Paul Belanger174a8272017-03-14 13:20:10 -04003598 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003599 self.waitUntilSettled()
3600
3601 # C was successfully merged so window is increased to 2.
3602 self.assertEqual(queue.window, 2)
3603 self.assertEqual(queue.window_floor, 1)
3604 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003605
3606 def test_worker_update_metadata(self):
3607 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003608 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003609
3610 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003611 A.addApproval('Code-Review', 2)
3612 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003613 self.waitUntilSettled()
3614
Clint Byrumd4de79a2017-05-27 11:39:13 -07003615 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003616
3617 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003618 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003619
3620 start = time.time()
3621 while True:
3622 if time.time() - start > 10:
3623 raise Exception("Timeout waiting for gearman server to report "
3624 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003625 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003626 if build.worker.name == self.executor_server.hostname:
Joshua Heskethba8776a2014-01-12 14:35:40 +08003627 break
3628 else:
3629 time.sleep(0)
3630
3631 self.log.debug(build)
Monty Tayloreea6bb92017-06-14 11:56:54 -05003632 self.assertEqual(self.executor_server.hostname, build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003633
Paul Belanger174a8272017-03-14 13:20:10 -04003634 self.executor_server.hold_jobs_in_build = False
3635 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003636 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003637
James E. Blaircc324192017-04-19 14:36:08 -07003638 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003639 def test_footer_message(self):
3640 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003641 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003642 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003643 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003644 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003645 self.waitUntilSettled()
3646
3647 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003648 B.addApproval('Code-Review', 2)
3649 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003650 self.waitUntilSettled()
3651
3652 self.assertEqual(2, len(self.smtp_messages))
3653
Adam Gandelman82258fd2017-02-07 16:51:58 -08003654 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003655Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003656http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003657
Adam Gandelman82258fd2017-02-07 16:51:58 -08003658 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003659For CI problems and help debugging, contact ci@example.org"""
3660
Adam Gandelman82258fd2017-02-07 16:51:58 -08003661 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3662 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3663 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3664 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003665
Tobias Henkel9842bd72017-05-16 13:40:03 +02003666 @simple_layout('layouts/unmanaged-project.yaml')
3667 def test_unmanaged_project_start_message(self):
3668 "Test start reporting is not done for unmanaged projects."
3669 self.init_repo("org/project", tag='init')
3670 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3671 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3672 self.waitUntilSettled()
3673
3674 self.assertEqual(0, len(A.messages))
3675
Clint Byrumb9de8be2017-05-27 13:49:19 -07003676 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003677 def test_merge_failure_reporters(self):
3678 """Check that the config is set up correctly"""
3679
Clint Byrumb9de8be2017-05-27 13:49:19 -07003680 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003681 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003682 "Merge Failed.\n\nThis change or one of its cross-repo "
3683 "dependencies was unable to be automatically merged with the "
3684 "current state of its repository. Please rebase the change and "
3685 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003686 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003687 self.assertEqual(
3688 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003689 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003690
3691 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003692 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003693 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003694 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003695
3696 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003697 tenant.layout.pipelines['check'].merge_failure_actions[0],
3698 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003699
3700 self.assertTrue(
3701 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003702 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003703 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003704 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3705 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003706 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003707 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003708 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003709 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003710 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003711 gerritreporter.GerritReporter) and
3712 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003713 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003714 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003715 )
3716 )
3717
James E. Blairec056492016-07-22 09:45:56 -07003718 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003719 def test_merge_failure_reports(self):
3720 """Check that when a change fails to merge the correct message is sent
3721 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003722 self.updateConfigLayout(
3723 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003724 self.sched.reconfigure(self.config)
3725 self.registerJobs()
3726
3727 # Check a test failure isn't reported to SMTP
3728 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003729 A.addApproval('Code-Review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003730 self.executor_server.failJob('project-test1', A)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003731 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003732 self.waitUntilSettled()
3733
3734 self.assertEqual(3, len(self.history)) # 3 jobs
3735 self.assertEqual(0, len(self.smtp_messages))
3736
3737 # Check a merge failure is reported to SMTP
3738 # B should be merged, but C will conflict with B
3739 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3740 B.addPatchset(['conflict'])
3741 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3742 C.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003743 B.addApproval('Code-Review', 2)
3744 C.addApproval('Code-Review', 2)
3745 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
3746 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003747 self.waitUntilSettled()
3748
3749 self.assertEqual(6, len(self.history)) # A and B jobs
3750 self.assertEqual(1, len(self.smtp_messages))
3751 self.assertEqual('The merge failed! For more information...',
3752 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003753
James E. Blairec056492016-07-22 09:45:56 -07003754 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003755 def test_default_merge_failure_reports(self):
3756 """Check that the default merge failure reports are correct."""
3757
3758 # A should report success, B should report merge failure.
3759 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3760 A.addPatchset(['conflict'])
3761 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3762 B.addPatchset(['conflict'])
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003763 A.addApproval('Code-Review', 2)
3764 B.addApproval('Code-Review', 2)
3765 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
3766 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003767 self.waitUntilSettled()
3768
3769 self.assertEqual(3, len(self.history)) # A jobs
3770 self.assertEqual(A.reported, 2)
3771 self.assertEqual(B.reported, 2)
3772 self.assertEqual(A.data['status'], 'MERGED')
3773 self.assertEqual(B.data['status'], 'NEW')
3774 self.assertIn('Build succeeded', A.messages[1])
3775 self.assertIn('Merge Failed', B.messages[1])
3776 self.assertIn('automatically merged', B.messages[1])
3777 self.assertNotIn('logs.example.com', B.messages[1])
3778 self.assertNotIn('SKIPPED', B.messages[1])
3779
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003780 def test_client_get_running_jobs(self):
3781 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003782 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003783 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003784 A.addApproval('Code-Review', 2)
3785 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003786 self.waitUntilSettled()
3787
3788 client = zuul.rpcclient.RPCClient('127.0.0.1',
3789 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003790 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003791
3792 # Wait for gearman server to send the initial workData back to zuul
3793 start = time.time()
3794 while True:
3795 if time.time() - start > 10:
3796 raise Exception("Timeout waiting for gearman server to report "
3797 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003798 build = list(self.executor_client.builds.values())[0]
Monty Tayloreea6bb92017-06-14 11:56:54 -05003799 if build.worker.name == self.executor_server.hostname:
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003800 break
3801 else:
3802 time.sleep(0)
3803
3804 running_items = client.get_running_jobs()
3805
3806 self.assertEqual(1, len(running_items))
3807 running_item = running_items[0]
3808 self.assertEqual([], running_item['failing_reasons'])
3809 self.assertEqual([], running_item['items_behind'])
3810 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003811 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003812 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003813 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003814 self.assertEqual(True, running_item['active'])
3815 self.assertEqual('1,1', running_item['id'])
3816
3817 self.assertEqual(3, len(running_item['jobs']))
3818 for job in running_item['jobs']:
3819 if job['name'] == 'project-merge':
3820 self.assertEqual('project-merge', job['name'])
3821 self.assertEqual('gate', job['pipeline'])
3822 self.assertEqual(False, job['retry'])
Monty Taylor51139a02016-05-24 11:28:10 -05003823 self.assertEqual(
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003824 'static/stream.html?uuid={uuid}&logfile=console.log'
3825 .format(uuid=job['uuid']), job['url'])
3826 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -05003827 'finger://{hostname}/{uuid}'.format(
3828 hostname=self.executor_server.hostname,
3829 uuid=job['uuid']),
Tobias Henkelb4407fc2017-07-07 13:52:56 +02003830 job['finger_url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003831 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003832 self.assertEqual(False, job['canceled'])
3833 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003834 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003835 self.assertEqual('gate', job['pipeline'])
3836 break
3837
Paul Belanger174a8272017-03-14 13:20:10 -04003838 self.executor_server.hold_jobs_in_build = False
3839 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003840 self.waitUntilSettled()
3841
3842 running_items = client.get_running_jobs()
3843 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003844
James E. Blairb70e55a2017-04-19 12:57:02 -07003845 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003846 def test_nonvoting_pipeline(self):
3847 "Test that a nonvoting pipeline (experimental) can still report"
3848
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003849 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3850 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003851 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3852 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003853
3854 self.assertEqual(self.getJobFromHistory('project-merge').result,
3855 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003856 self.assertEqual(
3857 self.getJobFromHistory('experimental-project-test').result,
3858 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003859 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003860
3861 def test_crd_gate(self):
3862 "Test cross-repo dependencies"
3863 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3864 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003865 A.addApproval('Code-Review', 2)
3866 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003867
3868 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3869 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3870 AM2.setMerged()
3871 AM1.setMerged()
3872
3873 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3874 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3875 BM2.setMerged()
3876 BM1.setMerged()
3877
3878 # A -> AM1 -> AM2
3879 # B -> BM1 -> BM2
3880 # A Depends-On: B
3881 # M2 is here to make sure it is never queried. If it is, it
3882 # means zuul is walking down the entire history of merged
3883 # changes.
3884
3885 B.setDependsOn(BM1, 1)
3886 BM1.setDependsOn(BM2, 1)
3887
3888 A.setDependsOn(AM1, 1)
3889 AM1.setDependsOn(AM2, 1)
3890
3891 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3892 A.subject, B.data['id'])
3893
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003894 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003895 self.waitUntilSettled()
3896
3897 self.assertEqual(A.data['status'], 'NEW')
3898 self.assertEqual(B.data['status'], 'NEW')
3899
Paul Belanger5dccbe72016-11-14 11:17:37 -05003900 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003901 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003902
Paul Belanger174a8272017-03-14 13:20:10 -04003903 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003904 B.addApproval('Approved', 1)
3905 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003906 self.waitUntilSettled()
3907
Paul Belanger174a8272017-03-14 13:20:10 -04003908 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003909 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003910 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003911 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003912 self.executor_server.hold_jobs_in_build = False
3913 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003914 self.waitUntilSettled()
3915
3916 self.assertEqual(AM2.queried, 0)
3917 self.assertEqual(BM2.queried, 0)
3918 self.assertEqual(A.data['status'], 'MERGED')
3919 self.assertEqual(B.data['status'], 'MERGED')
3920 self.assertEqual(A.reported, 2)
3921 self.assertEqual(B.reported, 2)
3922
Paul Belanger5dccbe72016-11-14 11:17:37 -05003923 changes = self.getJobFromHistory(
3924 'project-merge', 'org/project1').changes
3925 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003926
3927 def test_crd_branch(self):
3928 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003929
3930 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003931 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3932 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003933 C1 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C1')
3934 C2 = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C2',
3935 status='ABANDONED')
3936 C1.data['id'] = B.data['id']
3937 C2.data['id'] = B.data['id']
3938
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003939 A.addApproval('Code-Review', 2)
3940 B.addApproval('Code-Review', 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003941 C1.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003942
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003943 # A Depends-On: B+C1
James E. Blair8f78d882015-02-05 08:51:37 -08003944 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3945 A.subject, B.data['id'])
3946
Paul Belanger174a8272017-03-14 13:20:10 -04003947 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003948 B.addApproval('Approved', 1)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003949 C1.addApproval('Approved', 1)
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003950 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003951 self.waitUntilSettled()
3952
Paul Belanger174a8272017-03-14 13:20:10 -04003953 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003954 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003955 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003956 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003957 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003958 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003959 self.executor_server.hold_jobs_in_build = False
3960 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003961 self.waitUntilSettled()
3962
3963 self.assertEqual(A.data['status'], 'MERGED')
3964 self.assertEqual(B.data['status'], 'MERGED')
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003965 self.assertEqual(C1.data['status'], 'MERGED')
James E. Blair8f78d882015-02-05 08:51:37 -08003966 self.assertEqual(A.reported, 2)
3967 self.assertEqual(B.reported, 2)
K Jonathan Harkerd1d51d22017-08-23 11:57:40 -04003968 self.assertEqual(C1.reported, 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003969
Paul Belanger6379db12016-11-14 13:57:54 -05003970 changes = self.getJobFromHistory(
3971 'project-merge', 'org/project1').changes
3972 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003973
3974 def test_crd_multiline(self):
3975 "Test multiple depends-on lines in commit"
3976 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3977 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3978 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003979 A.addApproval('Code-Review', 2)
3980 B.addApproval('Code-Review', 2)
3981 C.addApproval('Code-Review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003982
3983 # A Depends-On: B+C
3984 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3985 A.subject, B.data['id'], C.data['id'])
3986
Paul Belanger174a8272017-03-14 13:20:10 -04003987 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02003988 B.addApproval('Approved', 1)
3989 C.addApproval('Approved', 1)
3990 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003991 self.waitUntilSettled()
3992
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.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003996 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003997 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003998 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003999 self.executor_server.hold_jobs_in_build = False
4000 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08004001 self.waitUntilSettled()
4002
4003 self.assertEqual(A.data['status'], 'MERGED')
4004 self.assertEqual(B.data['status'], 'MERGED')
4005 self.assertEqual(C.data['status'], 'MERGED')
4006 self.assertEqual(A.reported, 2)
4007 self.assertEqual(B.reported, 2)
4008 self.assertEqual(C.reported, 2)
4009
Paul Belanger5dccbe72016-11-14 11:17:37 -05004010 changes = self.getJobFromHistory(
4011 'project-merge', 'org/project1').changes
4012 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08004013
4014 def test_crd_unshared_gate(self):
4015 "Test cross-repo dependencies in unshared gate queues"
4016 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4017 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004018 A.addApproval('Code-Review', 2)
4019 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004020
4021 # A Depends-On: B
4022 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4023 A.subject, B.data['id'])
4024
4025 # A and B do not share a queue, make sure that A is unable to
4026 # enqueue B (and therefore, A is unable to be enqueued).
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004027 B.addApproval('Approved', 1)
4028 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004029 self.waitUntilSettled()
4030
4031 self.assertEqual(A.data['status'], 'NEW')
4032 self.assertEqual(B.data['status'], 'NEW')
4033 self.assertEqual(A.reported, 0)
4034 self.assertEqual(B.reported, 0)
4035 self.assertEqual(len(self.history), 0)
4036
4037 # Enqueue and merge B alone.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004038 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004039 self.waitUntilSettled()
4040
4041 self.assertEqual(B.data['status'], 'MERGED')
4042 self.assertEqual(B.reported, 2)
4043
4044 # Now that B is merged, A should be able to be enqueued and
4045 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004046 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004047 self.waitUntilSettled()
4048
4049 self.assertEqual(A.data['status'], 'MERGED')
4050 self.assertEqual(A.reported, 2)
4051
James E. Blair96698e22015-04-02 07:48:21 -07004052 def test_crd_gate_reverse(self):
4053 "Test reverse cross-repo dependencies"
4054 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4055 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004056 A.addApproval('Code-Review', 2)
4057 B.addApproval('Code-Review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07004058
4059 # A Depends-On: B
4060
4061 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4062 A.subject, B.data['id'])
4063
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004064 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004065 self.waitUntilSettled()
4066
4067 self.assertEqual(A.data['status'], 'NEW')
4068 self.assertEqual(B.data['status'], 'NEW')
4069
Paul Belanger174a8272017-03-14 13:20:10 -04004070 self.executor_server.hold_jobs_in_build = True
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004071 A.addApproval('Approved', 1)
4072 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07004073 self.waitUntilSettled()
4074
Paul Belanger174a8272017-03-14 13:20:10 -04004075 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004076 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004077 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07004078 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004079 self.executor_server.hold_jobs_in_build = False
4080 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07004081 self.waitUntilSettled()
4082
4083 self.assertEqual(A.data['status'], 'MERGED')
4084 self.assertEqual(B.data['status'], 'MERGED')
4085 self.assertEqual(A.reported, 2)
4086 self.assertEqual(B.reported, 2)
4087
Paul Belanger5dccbe72016-11-14 11:17:37 -05004088 changes = self.getJobFromHistory(
4089 'project-merge', 'org/project1').changes
4090 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07004091
James E. Blair5ee24252014-12-30 10:12:29 -08004092 def test_crd_cycle(self):
4093 "Test cross-repo dependency cycles"
4094 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4095 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004096 A.addApproval('Code-Review', 2)
4097 B.addApproval('Code-Review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08004098
4099 # A -> B -> A (via commit-depends)
4100
4101 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4102 A.subject, B.data['id'])
4103 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4104 B.subject, A.data['id'])
4105
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004106 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08004107 self.waitUntilSettled()
4108
4109 self.assertEqual(A.reported, 0)
4110 self.assertEqual(B.reported, 0)
4111 self.assertEqual(A.data['status'], 'NEW')
4112 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08004113
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004114 def test_crd_gate_unknown(self):
4115 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004116 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004117 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4118 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004119 A.addApproval('Code-Review', 2)
4120 B.addApproval('Code-Review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004121
4122 # A Depends-On: B
4123 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4124 A.subject, B.data['id'])
4125
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004126 B.addApproval('Approved', 1)
4127 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004128 self.waitUntilSettled()
4129
4130 # Unknown projects cannot share a queue with any other
4131 # since they don't have common jobs with any other (they have no jobs).
4132 # Changes which depend on unknown project changes
4133 # should not be processed in dependent pipeline
4134 self.assertEqual(A.data['status'], 'NEW')
4135 self.assertEqual(B.data['status'], 'NEW')
4136 self.assertEqual(A.reported, 0)
4137 self.assertEqual(B.reported, 0)
4138 self.assertEqual(len(self.history), 0)
4139
James E. Blair7720b402017-06-07 08:51:49 -07004140 # Simulate change B being gated outside this layout Set the
4141 # change merged before submitting the event so that when the
4142 # event triggers a gerrit query to update the change, we get
4143 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004144 B.setMerged()
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004145 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004146 self.waitUntilSettled()
4147 self.assertEqual(len(self.history), 0)
4148
4149 # Now that B is merged, A should be able to be enqueued and
4150 # merged.
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004151 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004152 self.waitUntilSettled()
4153
4154 self.assertEqual(A.data['status'], 'MERGED')
4155 self.assertEqual(A.reported, 2)
4156 self.assertEqual(B.data['status'], 'MERGED')
4157 self.assertEqual(B.reported, 0)
4158
James E. Blairbfb8e042014-12-30 17:01:44 -08004159 def test_crd_check(self):
4160 "Test cross-repo dependencies in independent pipelines"
4161
Paul Belanger174a8272017-03-14 13:20:10 -04004162 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08004163 self.gearman_server.hold_jobs_in_queue = True
4164 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4165 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4166
4167 # A Depends-On: B
4168 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4169 A.subject, B.data['id'])
4170
4171 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4172 self.waitUntilSettled()
4173
James E. Blairbfb8e042014-12-30 17:01:44 -08004174 self.gearman_server.hold_jobs_in_queue = False
4175 self.gearman_server.release()
4176 self.waitUntilSettled()
4177
Paul Belanger174a8272017-03-14 13:20:10 -04004178 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05004179 self.waitUntilSettled()
4180
James E. Blair2a535672017-04-27 12:03:15 -07004181 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08004182
Paul Belanger174a8272017-03-14 13:20:10 -04004183 self.executor_server.hold_jobs_in_build = False
4184 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05004185 self.waitUntilSettled()
4186
James E. Blairbfb8e042014-12-30 17:01:44 -08004187 self.assertEqual(A.data['status'], 'NEW')
4188 self.assertEqual(B.data['status'], 'NEW')
4189 self.assertEqual(A.reported, 1)
4190 self.assertEqual(B.reported, 0)
4191
4192 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05004193 tenant = self.sched.abide.tenants.get('tenant-one')
4194 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08004195
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004196 def test_crd_check_git_depends(self):
4197 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07004198 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004199 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4200 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4201
4202 # Add two git-dependent changes and make sure they both report
4203 # success.
4204 B.setDependsOn(A, 1)
4205 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4206 self.waitUntilSettled()
4207 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4208 self.waitUntilSettled()
4209
James E. Blairb8c16472015-05-05 14:55:26 -07004210 self.orderedRelease()
4211 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004212 self.waitUntilSettled()
4213
4214 self.assertEqual(A.data['status'], 'NEW')
4215 self.assertEqual(B.data['status'], 'NEW')
4216 self.assertEqual(A.reported, 1)
4217 self.assertEqual(B.reported, 1)
4218
4219 self.assertEqual(self.history[0].changes, '1,1')
4220 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04004221 tenant = self.sched.abide.tenants.get('tenant-one')
4222 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004223
4224 self.assertIn('Build succeeded', A.messages[0])
4225 self.assertIn('Build succeeded', B.messages[0])
4226
4227 def test_crd_check_duplicate(self):
4228 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04004229 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004230 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4231 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04004232 tenant = self.sched.abide.tenants.get('tenant-one')
4233 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004234
4235 # Add two git-dependent changes...
4236 B.setDependsOn(A, 1)
4237 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4238 self.waitUntilSettled()
4239 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4240
4241 # ...make sure the live one is not duplicated...
4242 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4243 self.waitUntilSettled()
4244 self.assertEqual(len(check_pipeline.getAllItems()), 2)
4245
4246 # ...but the non-live one is able to be.
4247 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4248 self.waitUntilSettled()
4249 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4250
Clark Boylandd849822015-03-02 12:38:14 -08004251 # Release jobs in order to avoid races with change A jobs
4252 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07004253 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04004254 self.executor_server.hold_jobs_in_build = False
4255 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004256 self.waitUntilSettled()
4257
4258 self.assertEqual(A.data['status'], 'NEW')
4259 self.assertEqual(B.data['status'], 'NEW')
4260 self.assertEqual(A.reported, 1)
4261 self.assertEqual(B.reported, 1)
4262
4263 self.assertEqual(self.history[0].changes, '1,1 2,1')
4264 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04004265 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08004266
4267 self.assertIn('Build succeeded', A.messages[0])
4268 self.assertIn('Build succeeded', B.messages[0])
4269
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004270 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08004271 "Test cross-repo dependencies re-enqueued in independent pipelines"
4272
4273 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004274 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
4275 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08004276
4277 # A Depends-On: B
4278 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4279 A.subject, B.data['id'])
4280
4281 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4282 self.waitUntilSettled()
4283
4284 self.sched.reconfigure(self.config)
4285
4286 # Make sure the items still share a change queue, and the
4287 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004288 tenant = self.sched.abide.tenants.get('tenant-one')
4289 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4290 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004291 first_item = queue.queue[0]
4292 for item in queue.queue:
4293 self.assertEqual(item.queue, first_item.queue)
4294 self.assertFalse(first_item.live)
4295 self.assertTrue(queue.queue[1].live)
4296
4297 self.gearman_server.hold_jobs_in_queue = False
4298 self.gearman_server.release()
4299 self.waitUntilSettled()
4300
4301 self.assertEqual(A.data['status'], 'NEW')
4302 self.assertEqual(B.data['status'], 'NEW')
4303 self.assertEqual(A.reported, 1)
4304 self.assertEqual(B.reported, 0)
4305
4306 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004307 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004308
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004309 def test_crd_check_reconfiguration(self):
4310 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4311
4312 def test_crd_undefined_project(self):
4313 """Test that undefined projects in dependencies are handled for
4314 independent pipelines"""
4315 # It's a hack for fake gerrit,
4316 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004317 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004318 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4319
Jesse Keatingea68ab42017-04-19 15:03:30 -07004320 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004321 def test_crd_check_ignore_dependencies(self):
4322 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004323
4324 self.gearman_server.hold_jobs_in_queue = True
4325 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4326 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4327 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4328
4329 # A Depends-On: B
4330 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4331 A.subject, B.data['id'])
4332 # C git-depends on B
4333 C.setDependsOn(B, 1)
4334 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4335 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4336 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4337 self.waitUntilSettled()
4338
4339 # Make sure none of the items share a change queue, and all
4340 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004341 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004342 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004343 self.assertEqual(len(check_pipeline.queues), 3)
4344 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4345 for item in check_pipeline.getAllItems():
4346 self.assertTrue(item.live)
4347
4348 self.gearman_server.hold_jobs_in_queue = False
4349 self.gearman_server.release()
4350 self.waitUntilSettled()
4351
4352 self.assertEqual(A.data['status'], 'NEW')
4353 self.assertEqual(B.data['status'], 'NEW')
4354 self.assertEqual(C.data['status'], 'NEW')
4355 self.assertEqual(A.reported, 1)
4356 self.assertEqual(B.reported, 1)
4357 self.assertEqual(C.reported, 1)
4358
4359 # Each job should have tested exactly one change
4360 for job in self.history:
4361 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004362
James E. Blair7cb84542017-04-19 13:35:05 -07004363 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004364 def test_crd_check_transitive(self):
4365 "Test transitive cross-repo dependencies"
4366 # Specifically, if A -> B -> C, and C gets a new patchset and
4367 # A gets a new patchset, ensure the test of A,2 includes B,1
4368 # and C,2 (not C,1 which would indicate stale data in the
4369 # cache for B).
4370 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4371 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4372 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4373
4374 # A Depends-On: B
4375 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4376 A.subject, B.data['id'])
4377
4378 # B Depends-On: C
4379 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4380 B.subject, C.data['id'])
4381
4382 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4383 self.waitUntilSettled()
4384 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4385
4386 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4387 self.waitUntilSettled()
4388 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4389
4390 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4391 self.waitUntilSettled()
4392 self.assertEqual(self.history[-1].changes, '3,1')
4393
4394 C.addPatchset()
4395 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4396 self.waitUntilSettled()
4397 self.assertEqual(self.history[-1].changes, '3,2')
4398
4399 A.addPatchset()
4400 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4401 self.waitUntilSettled()
4402 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004403
James E. Blaira8b90b32016-08-24 15:18:50 -07004404 def test_crd_check_unknown(self):
4405 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004406 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004407 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4408 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4409 # A Depends-On: B
4410 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4411 A.subject, B.data['id'])
4412
4413 # Make sure zuul has seen an event on B.
4414 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4415 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4416 self.waitUntilSettled()
4417
4418 self.assertEqual(A.data['status'], 'NEW')
4419 self.assertEqual(A.reported, 1)
4420 self.assertEqual(B.data['status'], 'NEW')
4421 self.assertEqual(B.reported, 0)
4422
James E. Blair92464a22016-04-05 10:21:26 -07004423 def test_crd_cycle_join(self):
4424 "Test an updated change creates a cycle"
4425 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4426
4427 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4428 self.waitUntilSettled()
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004429 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004430
4431 # Create B->A
4432 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4433 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4434 B.subject, A.data['id'])
4435 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4436 self.waitUntilSettled()
4437
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004438 # Dep is there so zuul should have reported on B
4439 self.assertEqual(B.reported, 1)
4440
James E. Blair92464a22016-04-05 10:21:26 -07004441 # Update A to add A->B (a cycle).
4442 A.addPatchset()
4443 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4444 A.subject, B.data['id'])
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004445 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4446 self.waitUntilSettled()
James E. Blair92464a22016-04-05 10:21:26 -07004447
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004448 # Dependency cycle injected so zuul should not have reported again on A
4449 self.assertEqual(A.reported, 1)
James E. Blair92464a22016-04-05 10:21:26 -07004450
4451 # Now if we update B to remove the depends-on, everything
4452 # should be okay. B; A->B
4453
4454 B.addPatchset()
4455 B.data['commitMessage'] = '%s\n' % (B.subject,)
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004456 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4457 self.waitUntilSettled()
Paul Belanger0e155e22016-11-14 14:12:23 -05004458
Tobias Henkel6b9390f2017-03-28 11:23:21 +02004459 # Cycle was removed so now zuul should have reported again on A
4460 self.assertEqual(A.reported, 2)
4461
4462 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
4463 self.waitUntilSettled()
4464 self.assertEqual(B.reported, 2)
James E. Blair92464a22016-04-05 10:21:26 -07004465
Jesse Keatingcc67b122017-04-19 14:54:57 -07004466 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004467 def test_disable_at(self):
4468 "Test a pipeline will only report to the disabled trigger when failing"
4469
Jesse Keatingcc67b122017-04-19 14:54:57 -07004470 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004471 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004472 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004473 0, tenant.layout.pipelines['check']._consecutive_failures)
4474 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004475
4476 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4477 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4478 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4479 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4480 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4481 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4482 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4483 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4484 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4485 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4486 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4487
Paul Belanger174a8272017-03-14 13:20:10 -04004488 self.executor_server.failJob('project-test1', A)
4489 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004490 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004491 self.executor_server.failJob('project-test1', D)
4492 self.executor_server.failJob('project-test1', E)
4493 self.executor_server.failJob('project-test1', F)
4494 self.executor_server.failJob('project-test1', G)
4495 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004496 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004497 self.executor_server.failJob('project-test1', J)
4498 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004499
4500 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4501 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4502 self.waitUntilSettled()
4503
4504 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004505 2, tenant.layout.pipelines['check']._consecutive_failures)
4506 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004507
4508 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4509 self.waitUntilSettled()
4510
4511 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004512 0, tenant.layout.pipelines['check']._consecutive_failures)
4513 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004514
4515 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4516 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4517 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4518 self.waitUntilSettled()
4519
4520 # We should be disabled now
4521 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004522 3, tenant.layout.pipelines['check']._consecutive_failures)
4523 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004524
4525 # We need to wait between each of these patches to make sure the
4526 # smtp messages come back in an expected order
4527 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4528 self.waitUntilSettled()
4529 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4530 self.waitUntilSettled()
4531 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4532 self.waitUntilSettled()
4533
4534 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4535 # leaving a message on each change
4536 self.assertEqual(1, len(A.messages))
4537 self.assertIn('Build failed.', A.messages[0])
4538 self.assertEqual(1, len(B.messages))
4539 self.assertIn('Build failed.', B.messages[0])
4540 self.assertEqual(1, len(C.messages))
4541 self.assertIn('Build succeeded.', C.messages[0])
4542 self.assertEqual(1, len(D.messages))
4543 self.assertIn('Build failed.', D.messages[0])
4544 self.assertEqual(1, len(E.messages))
4545 self.assertIn('Build failed.', E.messages[0])
4546 self.assertEqual(1, len(F.messages))
4547 self.assertIn('Build failed.', F.messages[0])
4548
4549 # The last 3 (GHI) would have only reported via smtp.
4550 self.assertEqual(3, len(self.smtp_messages))
4551 self.assertEqual(0, len(G.messages))
4552 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004553 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004554 'project-test1 finger://', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004555 self.assertEqual(0, len(H.messages))
4556 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004557 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004558 'project-test1 finger://', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004559 self.assertEqual(0, len(I.messages))
4560 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004561 self.assertIn(
Monty Taylorde8242c2017-02-23 20:29:53 -06004562 'project-test1 finger://', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004563
4564 # Now reload the configuration (simulate a HUP) to check the pipeline
4565 # comes out of disabled
4566 self.sched.reconfigure(self.config)
4567
Jesse Keatingcc67b122017-04-19 14:54:57 -07004568 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004569
4570 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004571 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004572 0, tenant.layout.pipelines['check']._consecutive_failures)
4573 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004574
4575 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4576 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4577 self.waitUntilSettled()
4578
4579 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004580 2, tenant.layout.pipelines['check']._consecutive_failures)
4581 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004582
4583 # J and K went back to gerrit
4584 self.assertEqual(1, len(J.messages))
4585 self.assertIn('Build failed.', J.messages[0])
4586 self.assertEqual(1, len(K.messages))
4587 self.assertIn('Build failed.', K.messages[0])
4588 # No more messages reported via smtp
4589 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004590
James E. Blaircc020532017-04-19 13:04:44 -07004591 @simple_layout('layouts/one-job-project.yaml')
4592 def test_one_job_project(self):
4593 "Test that queueing works with one job"
4594 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4595 'master', 'A')
4596 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4597 'master', 'B')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004598 A.addApproval('Code-Review', 2)
4599 B.addApproval('Code-Review', 2)
4600 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
4601 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
James E. Blaircc020532017-04-19 13:04:44 -07004602 self.waitUntilSettled()
4603
4604 self.assertEqual(A.data['status'], 'MERGED')
4605 self.assertEqual(A.reported, 2)
4606 self.assertEqual(B.data['status'], 'MERGED')
4607 self.assertEqual(B.reported, 2)
4608
Paul Belanger32023542017-10-06 14:48:50 -04004609 def test_job_aborted(self):
4610 "Test that if a execute server aborts a job, it is run again"
4611 self.executor_server.hold_jobs_in_build = True
4612 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4613 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4614 self.waitUntilSettled()
4615
4616 self.executor_server.release('.*-merge')
4617 self.waitUntilSettled()
4618
4619 self.assertEqual(len(self.builds), 2)
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004620
4621 # first abort
4622 self.builds[0].aborted = True
4623 self.executor_server.release('.*-test*')
4624 self.waitUntilSettled()
4625 self.assertEqual(len(self.builds), 1)
4626
4627 # second abort
4628 self.builds[0].aborted = True
4629 self.executor_server.release('.*-test*')
4630 self.waitUntilSettled()
4631 self.assertEqual(len(self.builds), 1)
4632
4633 # third abort
4634 self.builds[0].aborted = True
4635 self.executor_server.release('.*-test*')
4636 self.waitUntilSettled()
4637 self.assertEqual(len(self.builds), 1)
4638
4639 # fourth abort
Paul Belanger32023542017-10-06 14:48:50 -04004640 self.builds[0].aborted = True
4641 self.executor_server.release('.*-test*')
4642 self.waitUntilSettled()
4643 self.assertEqual(len(self.builds), 1)
4644
4645 self.executor_server.hold_jobs_in_build = False
4646 self.executor_server.release()
4647 self.waitUntilSettled()
4648
Tobias Henkel26fa8a32017-10-07 07:08:36 +00004649 self.assertEqual(len(self.history), 7)
4650 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
Paul Belanger32023542017-10-06 14:48:50 -04004651 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
4652
Paul Belanger71d98172016-11-08 10:56:31 -05004653 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004654 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004655
Paul Belanger174a8272017-03-14 13:20:10 -04004656 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004657 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4658 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4659 self.waitUntilSettled()
4660
Paul Belanger174a8272017-03-14 13:20:10 -04004661 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004662 self.waitUntilSettled()
4663
4664 self.assertEqual(len(self.builds), 2)
4665 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004666 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004667 self.waitUntilSettled()
4668
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004669 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004670 self.assertEqual(len(self.builds), 1,
4671 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004672 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004673 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004674 self.waitUntilSettled()
4675
Paul Belanger174a8272017-03-14 13:20:10 -04004676 self.executor_server.hold_jobs_in_build = False
4677 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004678 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004679 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004680 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4681 self.assertEqual(A.reported, 1)
4682 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004683
James E. Blair15be0e12017-01-03 13:45:20 -08004684 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004685 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004686
4687 self.fake_nodepool.paused = True
4688 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004689 A.addApproval('Code-Review', 2)
4690 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair15be0e12017-01-03 13:45:20 -08004691 self.waitUntilSettled()
4692
4693 self.zk.client.stop()
4694 self.zk.client.start()
4695 self.fake_nodepool.paused = False
4696 self.waitUntilSettled()
4697
4698 self.assertEqual(A.data['status'], 'MERGED')
4699 self.assertEqual(A.reported, 2)
4700
James E. Blair6ab79e02017-01-06 10:10:17 -08004701 def test_nodepool_failure(self):
4702 "Test that jobs are reported after a nodepool failure"
4703
4704 self.fake_nodepool.paused = True
4705 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004706 A.addApproval('Code-Review', 2)
4707 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blair6ab79e02017-01-06 10:10:17 -08004708 self.waitUntilSettled()
4709
4710 req = self.fake_nodepool.getNodeRequests()[0]
4711 self.fake_nodepool.addFailRequest(req)
4712
4713 self.fake_nodepool.paused = False
4714 self.waitUntilSettled()
4715
4716 self.assertEqual(A.data['status'], 'NEW')
4717 self.assertEqual(A.reported, 2)
4718 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4719 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4720 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4721
Monty Taylor6dc5bc12017-09-29 15:47:31 -05004722 def test_nodepool_priority(self):
4723 "Test that nodes are requested at the correct priority"
4724
4725 self.fake_nodepool.paused = True
4726
4727 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4728 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4729
4730 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4731 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4732
4733 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4734 C.addApproval('Code-Review', 2)
4735 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
4736
4737 self.waitUntilSettled()
4738
4739 reqs = self.fake_nodepool.getNodeRequests()
4740
4741 # The requests come back sorted by oid. Since we have three requests
4742 # for the three changes each with a different priority.
4743 # Also they get a serial number based on order they were received
4744 # so the number on the endof the oid should map to order submitted.
4745
4746 # * gate first - high priority - change C
4747 self.assertEqual(reqs[0]['_oid'], '100-0000000002')
4748 self.assertEqual(reqs[0]['node_types'], ['label1'])
4749 # * check second - normal priority - change B
4750 self.assertEqual(reqs[1]['_oid'], '200-0000000001')
4751 self.assertEqual(reqs[1]['node_types'], ['label1'])
4752 # * post third - low priority - change A
4753 # additionally, the post job defined uses an ubuntu-xenial node,
4754 # so we include that check just as an extra verification
4755 self.assertEqual(reqs[2]['_oid'], '300-0000000000')
4756 self.assertEqual(reqs[2]['node_types'], ['ubuntu-xenial'])
4757
4758 self.fake_nodepool.paused = False
4759 self.waitUntilSettled()
4760
James E. Blairfceaf412017-09-29 13:44:57 -07004761 @simple_layout('layouts/multiple-templates.yaml')
4762 def test_multiple_project_templates(self):
4763 # Test that applying multiple project templates to a project
4764 # doesn't alter them when used for a second project.
4765 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4766 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4767 self.waitUntilSettled()
4768
4769 build = self.getJobFromHistory('py27')
4770 self.assertEqual(build.parameters['zuul']['jobtags'], [])
4771
James E. Blair29c77002017-10-05 14:56:35 -07004772 def test_pending_merge_in_reconfig(self):
4773 # Test that if we are waiting for an outstanding merge on
4774 # reconfiguration that we continue to do so.
4775 self.gearman_server.hold_merge_jobs_in_queue = True
4776 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4777 A.setMerged()
4778 self.fake_gerrit.addEvent(A.getRefUpdatedEvent())
4779 self.waitUntilSettled()
4780 # Reconfigure while we still have an outstanding merge job
4781 self.sched.reconfigureTenant(self.sched.abide.tenants['tenant-one'],
4782 None)
4783 self.waitUntilSettled()
4784 # Verify the merge job is still running and that the item is
4785 # in the pipeline
4786 self.assertEqual(len(self.sched.merger.jobs), 1)
4787 tenant = self.sched.abide.tenants.get('tenant-one')
4788 pipeline = tenant.layout.pipelines['post']
4789 self.assertEqual(len(pipeline.getAllItems()), 1)
4790 self.gearman_server.hold_merge_jobs_in_queue = False
4791 self.gearman_server.release()
4792 self.waitUntilSettled()
4793
Arieb6f068c2016-10-09 13:11:06 +03004794
James E. Blaira002b032017-04-18 10:35:48 -07004795class TestExecutor(ZuulTestCase):
4796 tenant_config_file = 'config/single-tenant/main.yaml'
4797
4798 def assertFinalState(self):
4799 # In this test, we expect to shut down in a non-final state,
4800 # so skip these checks.
4801 pass
4802
4803 def assertCleanShutdown(self):
4804 self.log.debug("Assert clean shutdown")
4805
4806 # After shutdown, make sure no jobs are running
4807 self.assertEqual({}, self.executor_server.job_workers)
4808
4809 # Make sure that git.Repo objects have been garbage collected.
4810 repos = []
4811 gc.collect()
4812 for obj in gc.get_objects():
4813 if isinstance(obj, git.Repo):
4814 self.log.debug("Leaked git repo object: %s" % repr(obj))
4815 repos.append(obj)
4816 self.assertEqual(len(repos), 0)
4817
4818 def test_executor_shutdown(self):
4819 "Test that the executor can shut down with jobs running"
4820
4821 self.executor_server.hold_jobs_in_build = True
4822 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004823 A.addApproval('Code-Review', 2)
4824 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
James E. Blaira002b032017-04-18 10:35:48 -07004825 self.waitUntilSettled()
4826
4827
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004828class TestDependencyGraph(ZuulTestCase):
4829 tenant_config_file = 'config/dependency-graph/main.yaml'
4830
4831 def test_dependeny_graph_dispatch_jobs_once(self):
4832 "Test a job in a dependency graph is queued only once"
4833 # Job dependencies, starting with A
4834 # A
4835 # / \
4836 # B C
4837 # / \ / \
4838 # D F E
4839 # |
4840 # G
4841
4842 self.executor_server.hold_jobs_in_build = True
4843 change = self.fake_gerrit.addFakeChange(
4844 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004845 change.addApproval('Code-Review', 2)
4846 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004847
4848 self.waitUntilSettled()
4849 self.assertEqual([b.name for b in self.builds], ['A'])
4850
4851 self.executor_server.release('A')
4852 self.waitUntilSettled()
4853 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4854
4855 self.executor_server.release('B')
4856 self.waitUntilSettled()
4857 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4858
4859 self.executor_server.release('D')
4860 self.waitUntilSettled()
4861 self.assertEqual([b.name for b in self.builds], ['C'])
4862
4863 self.executor_server.release('C')
4864 self.waitUntilSettled()
4865 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4866
4867 self.executor_server.release('F')
4868 self.waitUntilSettled()
4869 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4870
4871 self.executor_server.release('G')
4872 self.waitUntilSettled()
4873 self.assertEqual([b.name for b in self.builds], ['E'])
4874
4875 self.executor_server.release('E')
4876 self.waitUntilSettled()
4877 self.assertEqual(len(self.builds), 0)
4878
4879 self.executor_server.hold_jobs_in_build = False
4880 self.executor_server.release()
4881 self.waitUntilSettled()
4882
4883 self.assertEqual(len(self.builds), 0)
4884 self.assertEqual(len(self.history), 7)
4885
4886 self.assertEqual(change.data['status'], 'MERGED')
4887 self.assertEqual(change.reported, 2)
4888
4889 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4890 "Test that a job waits till all dependencies are successful"
4891 # Job dependencies, starting with A
4892 # A
4893 # / \
4894 # B C*
4895 # / \ / \
4896 # D F E
4897 # |
4898 # G
4899
4900 self.executor_server.hold_jobs_in_build = True
4901 change = self.fake_gerrit.addFakeChange(
4902 'org/project', 'master', 'change')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004903 change.addApproval('Code-Review', 2)
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004904
4905 self.executor_server.failJob('C', change)
4906
Tobias Henkelbf24fd12017-07-27 06:13:07 +02004907 self.fake_gerrit.addEvent(change.addApproval('Approved', 1))
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004908
4909 self.waitUntilSettled()
4910 self.assertEqual([b.name for b in self.builds], ['A'])
4911
4912 self.executor_server.release('A')
4913 self.waitUntilSettled()
4914 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4915
4916 self.executor_server.release('B')
4917 self.waitUntilSettled()
4918 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4919
4920 self.executor_server.release('D')
4921 self.waitUntilSettled()
4922 self.assertEqual([b.name for b in self.builds], ['C'])
4923
4924 self.executor_server.release('C')
4925 self.waitUntilSettled()
4926 self.assertEqual(len(self.builds), 0)
4927
4928 self.executor_server.hold_jobs_in_build = False
4929 self.executor_server.release()
4930 self.waitUntilSettled()
4931
4932 self.assertEqual(len(self.builds), 0)
4933 self.assertEqual(len(self.history), 4)
4934
4935 self.assertEqual(change.data['status'], 'NEW')
4936 self.assertEqual(change.reported, 2)
4937
4938
Arieb6f068c2016-10-09 13:11:06 +03004939class TestDuplicatePipeline(ZuulTestCase):
4940 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4941
4942 def test_duplicate_pipelines(self):
4943 "Test that a change matching multiple pipelines works"
4944
4945 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4946 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4947 self.waitUntilSettled()
4948
4949 self.assertHistory([
4950 dict(name='project-test1', result='SUCCESS', changes='1,1',
4951 pipeline='dup1'),
4952 dict(name='project-test1', result='SUCCESS', changes='1,1',
4953 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004954 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004955
4956 self.assertEqual(len(A.messages), 2)
4957
Paul Belangera46a3742016-11-09 14:23:03 -05004958 if 'dup1' in A.messages[0]:
4959 self.assertIn('dup1', A.messages[0])
4960 self.assertNotIn('dup2', A.messages[0])
4961 self.assertIn('project-test1', A.messages[0])
4962 self.assertIn('dup2', A.messages[1])
4963 self.assertNotIn('dup1', A.messages[1])
4964 self.assertIn('project-test1', A.messages[1])
4965 else:
4966 self.assertIn('dup1', A.messages[1])
4967 self.assertNotIn('dup2', A.messages[1])
4968 self.assertIn('project-test1', A.messages[1])
4969 self.assertIn('dup2', A.messages[0])
4970 self.assertNotIn('dup1', A.messages[0])
4971 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004972
4973
Paul Belanger86085b32016-11-03 12:48:57 -04004974class TestSchedulerTemplatedProject(ZuulTestCase):
4975 tenant_config_file = 'config/templated-project/main.yaml'
4976
Paul Belanger174a8272017-03-14 13:20:10 -04004977 def test_job_from_templates_executed(self):
4978 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004979
4980 A = self.fake_gerrit.addFakeChange(
4981 'org/templated-project', 'master', 'A')
4982 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4983 self.waitUntilSettled()
4984
4985 self.assertEqual(self.getJobFromHistory('project-test1').result,
4986 'SUCCESS')
4987 self.assertEqual(self.getJobFromHistory('project-test2').result,
4988 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004989
4990 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004991 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004992
4993 A = self.fake_gerrit.addFakeChange(
4994 'org/layered-project', 'master', 'A')
4995 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4996 self.waitUntilSettled()
4997
4998 self.assertEqual(self.getJobFromHistory('project-test1').result,
4999 'SUCCESS')
5000 self.assertEqual(self.getJobFromHistory('project-test2').result,
5001 'SUCCESS')
5002 self.assertEqual(self.getJobFromHistory('layered-project-test3'
5003 ).result, 'SUCCESS')
5004 self.assertEqual(self.getJobFromHistory('layered-project-test4'
5005 ).result, 'SUCCESS')
5006 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
5007 ).result, 'SUCCESS')
5008 self.assertEqual(self.getJobFromHistory('project-test6').result,
5009 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08005010
James E. Blaire74f5712017-09-29 15:14:31 -07005011 def test_unimplied_branch_matchers(self):
5012 # This tests that there are no implied branch matchers added
5013 # by project templates.
5014 self.create_branch('org/layered-project', 'stable')
5015
5016 A = self.fake_gerrit.addFakeChange(
5017 'org/layered-project', 'stable', 'A')
5018
5019 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5020 self.waitUntilSettled()
5021
5022 self.assertEqual(self.getJobFromHistory('project-test1').result,
5023 'SUCCESS')
5024 print(self.getJobFromHistory('project-test1').
5025 parameters['zuul']['_inheritance_path'])
5026
5027 def test_implied_branch_matchers(self):
5028 # This tests that there is an implied branch matcher when a
5029 # template is used on an in-repo project pipeline definition.
5030 self.create_branch('untrusted-config', 'stable')
5031 self.fake_gerrit.addEvent(
5032 self.fake_gerrit.getFakeBranchCreatedEvent(
5033 'untrusted-config', 'stable'))
5034 self.waitUntilSettled()
5035
5036 A = self.fake_gerrit.addFakeChange(
5037 'untrusted-config', 'stable', 'A')
5038
5039 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5040 self.waitUntilSettled()
5041
5042 self.assertEqual(self.getJobFromHistory('project-test1').result,
5043 'SUCCESS')
5044 print(self.getJobFromHistory('project-test1').
5045 parameters['zuul']['_inheritance_path'])
5046
Adam Gandelman94a60062016-11-21 16:43:14 -08005047
5048class TestSchedulerSuccessURL(ZuulTestCase):
5049 tenant_config_file = 'config/success-url/main.yaml'
5050
5051 def test_success_url(self):
5052 "Ensure bad build params are ignored"
5053 self.sched.reconfigure(self.config)
5054 self.init_repo('org/docs')
5055
5056 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
5057 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5058 self.waitUntilSettled()
5059
5060 # Both builds ran: docs-draft-test + docs-draft-test2
5061 self.assertEqual(len(self.history), 2)
5062
5063 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08005064 for build in self.history:
5065 if build.name == 'docs-draft-test':
5066 uuid = build.uuid[:7]
Monty Taylorde8242c2017-02-23 20:29:53 -06005067 elif build.name == 'docs-draft-test2':
5068 uuid_test2 = build.uuid
Adam Gandelman94a60062016-11-21 16:43:14 -08005069
5070 # Two msgs: 'Starting...' + results
5071 self.assertEqual(len(self.smtp_messages), 2)
5072 body = self.smtp_messages[1]['body'].splitlines()
5073 self.assertEqual('Build succeeded.', body[0])
5074
5075 self.assertIn(
5076 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
5077 'docs-draft-test/%s/publish-docs/' % uuid,
5078 body[2])
5079
Paul Belanger174a8272017-03-14 13:20:10 -04005080 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08005081 self.assertIn(
Monty Taylor51139a02016-05-24 11:28:10 -05005082 '- docs-draft-test2 finger://{hostname}/{uuid}'.format(
5083 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -06005084 uuid=uuid_test2),
Adam Gandelman94a60062016-11-21 16:43:14 -08005085 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08005086
5087
Adam Gandelman4da00f62016-12-09 15:47:33 -08005088class TestSchedulerMerges(ZuulTestCase):
5089 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08005090
5091 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04005092 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08005093 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04005094 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08005095 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
5096 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
5097 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005098 A.addApproval('Code-Review', 2)
5099 B.addApproval('Code-Review', 2)
5100 C.addApproval('Code-Review', 2)
5101 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5102 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5103 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman8bd57102016-12-02 12:58:42 -08005104 self.waitUntilSettled()
5105
5106 build = self.builds[-1]
James E. Blair2a535672017-04-27 12:03:15 -07005107 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5108 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08005109 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005110 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman8bd57102016-12-02 12:58:42 -08005111 repo_messages.reverse()
5112
Paul Belanger174a8272017-03-14 13:20:10 -04005113 self.executor_server.hold_jobs_in_build = False
5114 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08005115 self.waitUntilSettled()
5116
5117 return repo_messages
5118
5119 def _test_merge(self, mode):
5120 us_path = os.path.join(
5121 self.upstream_root, 'org/project-%s' % mode)
5122 expected_messages = [
5123 'initial commit',
5124 'add content from fixture',
5125 # the intermediate commits order is nondeterministic
5126 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
5127 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
5128 ]
5129 result = self._test_project_merge_mode(mode)
5130 self.assertEqual(result[:2], expected_messages[:2])
5131 self.assertEqual(result[-2:], expected_messages[-2:])
5132
5133 def test_project_merge_mode_merge(self):
5134 self._test_merge('merge')
5135
5136 def test_project_merge_mode_merge_resolve(self):
5137 self._test_merge('merge-resolve')
5138
5139 def test_project_merge_mode_cherrypick(self):
5140 expected_messages = [
5141 'initial commit',
5142 'add content from fixture',
5143 'A-1',
5144 'B-1',
5145 'C-1']
5146 result = self._test_project_merge_mode('cherry-pick')
5147 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08005148
5149 def test_merge_branch(self):
5150 "Test that the right commits are on alternate branches"
5151 self.create_branch('org/project-merge-branches', 'mp')
5152
Paul Belanger174a8272017-03-14 13:20:10 -04005153 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005154 A = self.fake_gerrit.addFakeChange(
5155 'org/project-merge-branches', 'mp', 'A')
5156 B = self.fake_gerrit.addFakeChange(
5157 'org/project-merge-branches', 'mp', 'B')
5158 C = self.fake_gerrit.addFakeChange(
5159 'org/project-merge-branches', 'mp', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005160 A.addApproval('Code-Review', 2)
5161 B.addApproval('Code-Review', 2)
5162 C.addApproval('Code-Review', 2)
5163 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5164 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5165 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005166 self.waitUntilSettled()
5167
Paul Belanger174a8272017-03-14 13:20:10 -04005168 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005169 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005170 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005171 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04005172 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005173 self.waitUntilSettled()
5174
5175 build = self.builds[-1]
James E. Blair3b222492017-07-21 15:17:37 -07005176 self.assertEqual(build.parameters['zuul']['branch'], 'mp')
James E. Blair2a535672017-04-27 12:03:15 -07005177 path = os.path.join(build.jobdir.src_root, 'review.example.com',
5178 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005179 repo = git.Repo(path)
5180
James E. Blair247cab72017-07-20 16:52:36 -07005181 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005182 repo_messages.reverse()
5183 correct_messages = [
5184 'initial commit',
5185 'add content from fixture',
5186 'mp commit',
5187 'A-1', 'B-1', 'C-1']
5188 self.assertEqual(repo_messages, correct_messages)
5189
Paul Belanger174a8272017-03-14 13:20:10 -04005190 self.executor_server.hold_jobs_in_build = False
5191 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005192 self.waitUntilSettled()
5193
5194 def test_merge_multi_branch(self):
5195 "Test that dependent changes on multiple branches are merged"
5196 self.create_branch('org/project-merge-branches', 'mp')
5197
Paul Belanger174a8272017-03-14 13:20:10 -04005198 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08005199 A = self.fake_gerrit.addFakeChange(
5200 'org/project-merge-branches', 'master', 'A')
5201 B = self.fake_gerrit.addFakeChange(
5202 'org/project-merge-branches', 'mp', 'B')
5203 C = self.fake_gerrit.addFakeChange(
5204 'org/project-merge-branches', 'master', 'C')
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005205 A.addApproval('Code-Review', 2)
5206 B.addApproval('Code-Review', 2)
5207 C.addApproval('Code-Review', 2)
5208 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5209 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5210 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Adam Gandelman4da00f62016-12-09 15:47:33 -08005211 self.waitUntilSettled()
5212
5213 job_A = None
5214 for job in self.builds:
5215 if 'project-merge' in job.name:
5216 job_A = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005217
James E. Blair2a535672017-04-27 12:03:15 -07005218 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
5219 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005220 repo = git.Repo(path)
5221 repo_messages = [c.message.strip()
James E. Blair247cab72017-07-20 16:52:36 -07005222 for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005223 repo_messages.reverse()
5224 correct_messages = [
5225 'initial commit', 'add content from fixture', 'A-1']
5226 self.assertEqual(repo_messages, correct_messages)
5227
Paul Belanger174a8272017-03-14 13:20:10 -04005228 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005229 self.waitUntilSettled()
5230
5231 job_B = None
5232 for job in self.builds:
5233 if 'project-merge' in job.name:
5234 job_B = job
Adam Gandelman4da00f62016-12-09 15:47:33 -08005235
James E. Blair2a535672017-04-27 12:03:15 -07005236 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
5237 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005238 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005239 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005240 repo_messages.reverse()
5241 correct_messages = [
5242 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
5243 self.assertEqual(repo_messages, correct_messages)
5244
Paul Belanger174a8272017-03-14 13:20:10 -04005245 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005246 self.waitUntilSettled()
5247
5248 job_C = None
5249 for job in self.builds:
5250 if 'project-merge' in job.name:
5251 job_C = job
James E. Blairb1345832017-07-21 14:51:35 -07005252
James E. Blair2a535672017-04-27 12:03:15 -07005253 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
5254 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08005255 repo = git.Repo(path)
James E. Blair247cab72017-07-20 16:52:36 -07005256 repo_messages = [c.message.strip() for c in repo.iter_commits()]
Adam Gandelman4da00f62016-12-09 15:47:33 -08005257
5258 repo_messages.reverse()
5259 correct_messages = [
5260 'initial commit', 'add content from fixture',
5261 'A-1', 'C-1']
5262 # Ensure the right commits are in the history for this ref
5263 self.assertEqual(repo_messages, correct_messages)
5264
Paul Belanger174a8272017-03-14 13:20:10 -04005265 self.executor_server.hold_jobs_in_build = False
5266 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08005267 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005268
5269
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005270class TestSemaphore(ZuulTestCase):
5271 tenant_config_file = 'config/semaphore/main.yaml'
5272
5273 def test_semaphore_one(self):
5274 "Test semaphores with max=1 (mutex)"
5275 tenant = self.sched.abide.tenants.get('tenant-one')
5276
5277 self.executor_server.hold_jobs_in_build = True
5278
5279 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5280 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5281 self.assertFalse('test-semaphore' in
5282 tenant.semaphore_handler.semaphores)
5283
5284 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5285 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5286 self.waitUntilSettled()
5287
5288 self.assertEqual(len(self.builds), 3)
5289 self.assertEqual(self.builds[0].name, 'project-test1')
5290 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
5291 self.assertEqual(self.builds[2].name, 'project-test1')
5292
5293 self.executor_server.release('semaphore-one-test1')
5294 self.waitUntilSettled()
5295
5296 self.assertEqual(len(self.builds), 3)
5297 self.assertEqual(self.builds[0].name, 'project-test1')
5298 self.assertEqual(self.builds[1].name, 'project-test1')
5299 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5300 self.assertTrue('test-semaphore' in
5301 tenant.semaphore_handler.semaphores)
5302
5303 self.executor_server.release('semaphore-one-test2')
5304 self.waitUntilSettled()
5305
5306 self.assertEqual(len(self.builds), 3)
5307 self.assertEqual(self.builds[0].name, 'project-test1')
5308 self.assertEqual(self.builds[1].name, 'project-test1')
5309 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
5310 self.assertTrue('test-semaphore' in
5311 tenant.semaphore_handler.semaphores)
5312
5313 self.executor_server.release('semaphore-one-test1')
5314 self.waitUntilSettled()
5315
5316 self.assertEqual(len(self.builds), 3)
5317 self.assertEqual(self.builds[0].name, 'project-test1')
5318 self.assertEqual(self.builds[1].name, 'project-test1')
5319 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
5320 self.assertTrue('test-semaphore' in
5321 tenant.semaphore_handler.semaphores)
5322
5323 self.executor_server.release('semaphore-one-test2')
5324 self.waitUntilSettled()
5325
5326 self.assertEqual(len(self.builds), 2)
5327 self.assertEqual(self.builds[0].name, 'project-test1')
5328 self.assertEqual(self.builds[1].name, 'project-test1')
5329 self.assertFalse('test-semaphore' in
5330 tenant.semaphore_handler.semaphores)
5331
5332 self.executor_server.hold_jobs_in_build = False
5333 self.executor_server.release()
5334
5335 self.waitUntilSettled()
5336 self.assertEqual(len(self.builds), 0)
5337
5338 self.assertEqual(A.reported, 1)
5339 self.assertEqual(B.reported, 1)
5340 self.assertFalse('test-semaphore' in
5341 tenant.semaphore_handler.semaphores)
5342
5343 def test_semaphore_two(self):
5344 "Test semaphores with max>1"
5345 tenant = self.sched.abide.tenants.get('tenant-one')
5346
5347 self.executor_server.hold_jobs_in_build = True
5348 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5349 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5350 self.assertFalse('test-semaphore-two' in
5351 tenant.semaphore_handler.semaphores)
5352
5353 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5354 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5355 self.waitUntilSettled()
5356
5357 self.assertEqual(len(self.builds), 4)
5358 self.assertEqual(self.builds[0].name, 'project-test1')
5359 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
5360 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5361 self.assertEqual(self.builds[3].name, 'project-test1')
5362 self.assertTrue('test-semaphore-two' in
5363 tenant.semaphore_handler.semaphores)
5364 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5365 'test-semaphore-two', [])), 2)
5366
5367 self.executor_server.release('semaphore-two-test1')
5368 self.waitUntilSettled()
5369
5370 self.assertEqual(len(self.builds), 4)
5371 self.assertEqual(self.builds[0].name, 'project-test1')
5372 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
5373 self.assertEqual(self.builds[2].name, 'project-test1')
5374 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
5375 self.assertTrue('test-semaphore-two' in
5376 tenant.semaphore_handler.semaphores)
5377 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5378 'test-semaphore-two', [])), 2)
5379
5380 self.executor_server.release('semaphore-two-test2')
5381 self.waitUntilSettled()
5382
5383 self.assertEqual(len(self.builds), 4)
5384 self.assertEqual(self.builds[0].name, 'project-test1')
5385 self.assertEqual(self.builds[1].name, 'project-test1')
5386 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
5387 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
5388 self.assertTrue('test-semaphore-two' in
5389 tenant.semaphore_handler.semaphores)
5390 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5391 'test-semaphore-two', [])), 2)
5392
5393 self.executor_server.release('semaphore-two-test1')
5394 self.waitUntilSettled()
5395
5396 self.assertEqual(len(self.builds), 3)
5397 self.assertEqual(self.builds[0].name, 'project-test1')
5398 self.assertEqual(self.builds[1].name, 'project-test1')
5399 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
5400 self.assertTrue('test-semaphore-two' in
5401 tenant.semaphore_handler.semaphores)
5402 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5403 'test-semaphore-two', [])), 1)
5404
5405 self.executor_server.release('semaphore-two-test2')
5406 self.waitUntilSettled()
5407
5408 self.assertEqual(len(self.builds), 2)
5409 self.assertEqual(self.builds[0].name, 'project-test1')
5410 self.assertEqual(self.builds[1].name, 'project-test1')
5411 self.assertFalse('test-semaphore-two' in
5412 tenant.semaphore_handler.semaphores)
5413
5414 self.executor_server.hold_jobs_in_build = False
5415 self.executor_server.release()
5416
5417 self.waitUntilSettled()
5418 self.assertEqual(len(self.builds), 0)
5419
5420 self.assertEqual(A.reported, 1)
5421 self.assertEqual(B.reported, 1)
5422
5423 def test_semaphore_abandon(self):
5424 "Test abandon with job semaphores"
5425 self.executor_server.hold_jobs_in_build = True
5426 tenant = self.sched.abide.tenants.get('tenant-one')
5427 check_pipeline = tenant.layout.pipelines['check']
5428
5429 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5430 self.assertFalse('test-semaphore' in
5431 tenant.semaphore_handler.semaphores)
5432
5433 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5434 self.waitUntilSettled()
5435
5436 self.assertTrue('test-semaphore' in
5437 tenant.semaphore_handler.semaphores)
5438
5439 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5440 self.waitUntilSettled()
5441
5442 # The check pipeline should be empty
5443 items = check_pipeline.getAllItems()
5444 self.assertEqual(len(items), 0)
5445
5446 # The semaphore should be released
5447 self.assertFalse('test-semaphore' in
5448 tenant.semaphore_handler.semaphores)
5449
5450 self.executor_server.hold_jobs_in_build = False
5451 self.executor_server.release()
5452 self.waitUntilSettled()
5453
Tobias Henkel0f714002017-06-30 23:30:52 +02005454 def test_semaphore_new_patchset(self):
5455 "Test new patchset with job semaphores"
5456 self.executor_server.hold_jobs_in_build = True
5457 tenant = self.sched.abide.tenants.get('tenant-one')
5458 check_pipeline = tenant.layout.pipelines['check']
5459
5460 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5461 self.assertFalse('test-semaphore' in
5462 tenant.semaphore_handler.semaphores)
5463
5464 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5465 self.waitUntilSettled()
5466
5467 self.assertTrue('test-semaphore' in
5468 tenant.semaphore_handler.semaphores)
5469 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5470 self.assertEqual(len(semaphore), 1)
5471
5472 A.addPatchset()
5473 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
5474 self.waitUntilSettled()
5475
5476 self.assertTrue('test-semaphore' in
5477 tenant.semaphore_handler.semaphores)
5478 semaphore = tenant.semaphore_handler.semaphores['test-semaphore']
5479 self.assertEqual(len(semaphore), 1)
5480
5481 items = check_pipeline.getAllItems()
5482 self.assertEqual(items[0].change.number, '1')
5483 self.assertEqual(items[0].change.patchset, '2')
5484 self.assertTrue(items[0].live)
5485
5486 self.executor_server.hold_jobs_in_build = False
5487 self.executor_server.release()
5488 self.waitUntilSettled()
5489
5490 # The semaphore should be released
5491 self.assertFalse('test-semaphore' in
5492 tenant.semaphore_handler.semaphores)
5493
James E. Blair9ea0d0b2017-04-20 09:27:15 -07005494 def test_semaphore_reconfigure(self):
5495 "Test reconfigure with job semaphores"
5496 self.executor_server.hold_jobs_in_build = True
5497 tenant = self.sched.abide.tenants.get('tenant-one')
5498 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5499 self.assertFalse('test-semaphore' in
5500 tenant.semaphore_handler.semaphores)
5501
5502 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5503 self.waitUntilSettled()
5504
5505 self.assertTrue('test-semaphore' in
5506 tenant.semaphore_handler.semaphores)
5507
5508 # reconfigure without layout change
5509 self.sched.reconfigure(self.config)
5510 self.waitUntilSettled()
5511 tenant = self.sched.abide.tenants.get('tenant-one')
5512
5513 # semaphore still must be held
5514 self.assertTrue('test-semaphore' in
5515 tenant.semaphore_handler.semaphores)
5516
5517 self.commitConfigUpdate(
5518 'common-config',
5519 'config/semaphore/zuul-reconfiguration.yaml')
5520 self.sched.reconfigure(self.config)
5521 self.waitUntilSettled()
5522 tenant = self.sched.abide.tenants.get('tenant-one')
5523
5524 self.executor_server.release('project-test1')
5525 self.waitUntilSettled()
5526
5527 # There should be no builds anymore
5528 self.assertEqual(len(self.builds), 0)
5529
5530 # The semaphore should be released
5531 self.assertFalse('test-semaphore' in
5532 tenant.semaphore_handler.semaphores)
5533
5534
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005535class TestSemaphoreMultiTenant(ZuulTestCase):
5536 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5537
5538 def test_semaphore_tenant_isolation(self):
5539 "Test semaphores in multiple tenants"
5540
5541 self.waitUntilSettled()
5542 tenant_one = self.sched.abide.tenants.get('tenant-one')
5543 tenant_two = self.sched.abide.tenants.get('tenant-two')
5544
5545 self.executor_server.hold_jobs_in_build = True
5546 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5547 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5548 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5549 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5550 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5551 self.assertFalse('test-semaphore' in
5552 tenant_one.semaphore_handler.semaphores)
5553 self.assertFalse('test-semaphore' in
5554 tenant_two.semaphore_handler.semaphores)
5555
5556 # add patches to project1 of tenant-one
5557 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5558 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5559 self.waitUntilSettled()
5560
5561 # one build of project1-test1 must run
5562 # semaphore of tenant-one must be acquired once
5563 # semaphore of tenant-two must not be acquired
5564 self.assertEqual(len(self.builds), 1)
5565 self.assertEqual(self.builds[0].name, 'project1-test1')
5566 self.assertTrue('test-semaphore' in
5567 tenant_one.semaphore_handler.semaphores)
5568 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5569 'test-semaphore', [])), 1)
5570 self.assertFalse('test-semaphore' in
5571 tenant_two.semaphore_handler.semaphores)
5572
5573 # add patches to project2 of tenant-two
5574 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5575 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5576 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5577 self.waitUntilSettled()
5578
5579 # one build of project1-test1 must run
5580 # two builds of project2-test1 must run
5581 # semaphore of tenant-one must be acquired once
5582 # semaphore of tenant-two must be acquired twice
5583 self.assertEqual(len(self.builds), 3)
5584 self.assertEqual(self.builds[0].name, 'project1-test1')
5585 self.assertEqual(self.builds[1].name, 'project2-test1')
5586 self.assertEqual(self.builds[2].name, 'project2-test1')
5587 self.assertTrue('test-semaphore' in
5588 tenant_one.semaphore_handler.semaphores)
5589 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5590 'test-semaphore', [])), 1)
5591 self.assertTrue('test-semaphore' in
5592 tenant_two.semaphore_handler.semaphores)
5593 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5594 'test-semaphore', [])), 2)
5595
5596 self.executor_server.release('project1-test1')
5597 self.waitUntilSettled()
5598
5599 # one build of project1-test1 must run
5600 # two builds of project2-test1 must run
5601 # semaphore of tenant-one must be acquired once
5602 # semaphore of tenant-two must be acquired twice
5603 self.assertEqual(len(self.builds), 3)
5604 self.assertEqual(self.builds[0].name, 'project2-test1')
5605 self.assertEqual(self.builds[1].name, 'project2-test1')
5606 self.assertEqual(self.builds[2].name, 'project1-test1')
5607 self.assertTrue('test-semaphore' in
5608 tenant_one.semaphore_handler.semaphores)
5609 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5610 'test-semaphore', [])), 1)
5611 self.assertTrue('test-semaphore' in
5612 tenant_two.semaphore_handler.semaphores)
5613 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5614 'test-semaphore', [])), 2)
5615
5616 self.executor_server.release('project2-test1')
5617 self.waitUntilSettled()
5618
5619 # one build of project1-test1 must run
5620 # one build of project2-test1 must run
5621 # semaphore of tenant-one must be acquired once
5622 # semaphore of tenant-two must be acquired once
5623 self.assertEqual(len(self.builds), 2)
5624 self.assertTrue('test-semaphore' in
5625 tenant_one.semaphore_handler.semaphores)
5626 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5627 'test-semaphore', [])), 1)
5628 self.assertTrue('test-semaphore' in
5629 tenant_two.semaphore_handler.semaphores)
5630 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5631 'test-semaphore', [])), 1)
5632
5633 self.executor_server.hold_jobs_in_build = False
5634 self.executor_server.release()
5635
5636 self.waitUntilSettled()
5637
5638 # no build must run
5639 # semaphore of tenant-one must not be acquired
5640 # semaphore of tenant-two must not be acquired
5641 self.assertEqual(len(self.builds), 0)
5642 self.assertFalse('test-semaphore' in
5643 tenant_one.semaphore_handler.semaphores)
5644 self.assertFalse('test-semaphore' in
5645 tenant_two.semaphore_handler.semaphores)
5646
5647 self.assertEqual(A.reported, 1)
5648 self.assertEqual(B.reported, 1)
5649
5650
5651class TestSemaphoreInRepo(ZuulTestCase):
Tobias Henkelabf973e2017-07-28 10:07:34 +02005652 config_file = 'zuul-connections-gerrit-and-github.conf'
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005653 tenant_config_file = 'config/in-repo/main.yaml'
5654
5655 def test_semaphore_in_repo(self):
5656 "Test semaphores in repo config"
5657
5658 # This tests dynamic semaphore handling in project repos. The semaphore
5659 # max value should not be evaluated dynamically but must be updated
5660 # after the change lands.
5661
5662 self.waitUntilSettled()
5663 tenant = self.sched.abide.tenants.get('tenant-one')
5664
5665 in_repo_conf = textwrap.dedent(
5666 """
5667 - job:
Tobias Henkelf02cf512017-07-21 22:55:34 +02005668 name: project-test1
5669
5670 - job:
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005671 name: project-test2
5672 semaphore: test-semaphore
5673
5674 - project:
5675 name: org/project
5676 tenant-one-gate:
5677 jobs:
5678 - project-test2
5679
5680 # the max value in dynamic layout must be ignored
5681 - semaphore:
5682 name: test-semaphore
5683 max: 2
5684 """)
5685
5686 in_repo_playbook = textwrap.dedent(
5687 """
5688 - hosts: all
5689 tasks: []
5690 """)
5691
5692 file_dict = {'.zuul.yaml': in_repo_conf,
5693 'playbooks/project-test2.yaml': in_repo_playbook}
5694 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5695 files=file_dict)
5696 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5697 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5698 B.setDependsOn(A, 1)
5699 C.setDependsOn(A, 1)
5700
5701 self.executor_server.hold_jobs_in_build = True
5702
Tobias Henkelbf24fd12017-07-27 06:13:07 +02005703 A.addApproval('Code-Review', 2)
5704 B.addApproval('Code-Review', 2)
5705 C.addApproval('Code-Review', 2)
5706 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5707 self.fake_gerrit.addEvent(B.addApproval('Approved', 1))
5708 self.fake_gerrit.addEvent(C.addApproval('Approved', 1))
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005709 self.waitUntilSettled()
5710
5711 # check that the layout in a queue item still has max value of 1
5712 # for test-semaphore
5713 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5714 queue = None
5715 for queue_candidate in pipeline.queues:
5716 if queue_candidate.name == 'org/project':
5717 queue = queue_candidate
5718 break
5719 queue_item = queue.queue[0]
James E. Blair29a24fd2017-10-02 15:04:56 -07005720 item_dynamic_layout = queue_item.layout
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005721 dynamic_test_semaphore = \
5722 item_dynamic_layout.semaphores.get('test-semaphore')
5723 self.assertEqual(dynamic_test_semaphore.max, 1)
5724
5725 # one build must be in queue, one semaphores acquired
5726 self.assertEqual(len(self.builds), 1)
5727 self.assertEqual(self.builds[0].name, 'project-test2')
5728 self.assertTrue('test-semaphore' in
5729 tenant.semaphore_handler.semaphores)
5730 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5731 'test-semaphore', [])), 1)
5732
5733 self.executor_server.release('project-test2')
5734 self.waitUntilSettled()
5735
5736 # change A must be merged
5737 self.assertEqual(A.data['status'], 'MERGED')
5738 self.assertEqual(A.reported, 2)
5739
5740 # send change-merged event as the gerrit mock doesn't send it
5741 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5742 self.waitUntilSettled()
5743
5744 # now that change A was merged, the new semaphore max must be effective
5745 tenant = self.sched.abide.tenants.get('tenant-one')
5746 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5747
5748 # two builds must be in queue, two semaphores acquired
5749 self.assertEqual(len(self.builds), 2)
5750 self.assertEqual(self.builds[0].name, 'project-test2')
5751 self.assertEqual(self.builds[1].name, 'project-test2')
5752 self.assertTrue('test-semaphore' in
5753 tenant.semaphore_handler.semaphores)
5754 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5755 'test-semaphore', [])), 2)
5756
5757 self.executor_server.release('project-test2')
5758 self.waitUntilSettled()
5759
5760 self.assertEqual(len(self.builds), 0)
5761 self.assertFalse('test-semaphore' in
5762 tenant.semaphore_handler.semaphores)
5763
5764 self.executor_server.hold_jobs_in_build = False
5765 self.executor_server.release()
5766
5767 self.waitUntilSettled()
5768 self.assertEqual(len(self.builds), 0)
5769
5770 self.assertEqual(A.reported, 2)
5771 self.assertEqual(B.reported, 2)
5772 self.assertEqual(C.reported, 2)
David Shrewsburyf6dc1762017-10-02 13:34:37 -04005773
5774
5775class TestSchedulerBranchMatcher(ZuulTestCase):
5776
5777 @simple_layout('layouts/matcher-test.yaml')
5778 def test_job_branch_ignored(self):
5779 '''
5780 Test that branch matching logic works.
5781
5782 The 'ignore-branch' job has a branch matcher that is supposed to
5783 match every branch except for the 'featureA' branch, so it should
5784 not be run on a change to that branch.
5785 '''
5786 self.create_branch('org/project', 'featureA')
5787 A = self.fake_gerrit.addFakeChange('org/project', 'featureA', 'A')
5788 A.addApproval('Code-Review', 2)
5789 self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
5790 self.waitUntilSettled()
5791 self.printHistory()
5792 self.assertEqual(self.getJobFromHistory('project-test1').result,
5793 'SUCCESS')
5794 self.assertJobNotInHistory('ignore-branch')
5795 self.assertEqual(A.data['status'], 'MERGED')
5796 self.assertEqual(A.reported, 2,
5797 "A should report start and success")
5798 self.assertIn('gate', A.messages[1],
5799 "A should transit gate")