blob: 839007d932ccf2cc4987fffa867744d8191adf9b [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
Morgan Fainberg293f7f82016-05-30 14:01:22 -070028from six.moves import urllib
Monty Taylorbc758832013-06-17 17:22:42 -040029import testtools
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 (
Maru Newby3fe5f852015-01-13 04:22:14 +000038 ZuulTestCase,
39 repack_repo,
James E. Blair06cc3922017-04-19 10:08:10 -070040 simple_layout,
Maru Newby3fe5f852015-01-13 04:22:14 +000041)
James E. Blairb0fcae42012-07-17 11:12:10 -070042
James E. Blairb0fcae42012-07-17 11:12:10 -070043
Clark Boylanb640e052014-04-03 16:41:46 -070044class TestScheduler(ZuulTestCase):
James E. Blair552b54f2016-07-22 13:55:32 -070045 tenant_config_file = 'config/single-tenant/main.yaml'
Antoine Mussobd86a312014-01-08 14:51:33 +010046
Paul Belanger174a8272017-03-14 13:20:10 -040047 def test_jobs_executed(self):
48 "Test that jobs are executed and a change is merged"
James E. Blair1f4c2bb2013-04-26 08:40:46 -070049
James E. Blairb0fcae42012-07-17 11:12:10 -070050 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070051 A.addApproval('code-review', 2)
52 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -070053 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -040054 self.assertEqual(self.getJobFromHistory('project-merge').result,
55 'SUCCESS')
56 self.assertEqual(self.getJobFromHistory('project-test1').result,
57 'SUCCESS')
58 self.assertEqual(self.getJobFromHistory('project-test2').result,
59 'SUCCESS')
60 self.assertEqual(A.data['status'], 'MERGED')
61 self.assertEqual(A.reported, 2)
James E. Blair34776ee2016-08-25 13:53:54 -070062 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -070063 'label1')
Adam Gandelmaneace4692017-02-07 15:18:36 -080064 self.assertIsNone(self.getJobFromHistory('project-test2').node)
James E. Blairb0fcae42012-07-17 11:12:10 -070065
James E. Blair552b54f2016-07-22 13:55:32 -070066 # TODOv3(jeblair): we may want to report stats by tenant (also?).
Monty Taylorf1385042017-05-27 12:25:05 -050067 # Per-driver
68 self.assertReportedStat('zuul.event.gerrit.comment-added', value='1|c')
69 # Per-driver per-connection
70 self.assertReportedStat('zuul.event.gerrit.gerrit.comment-added',
71 value='1|c')
James E. Blair66eeebf2013-07-27 17:44:32 -070072 self.assertReportedStat('zuul.pipeline.gate.current_changes',
73 value='1|g')
74 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
75 kind='ms')
76 self.assertReportedStat('zuul.pipeline.gate.job.project-merge.SUCCESS',
77 value='1|c')
78 self.assertReportedStat('zuul.pipeline.gate.resident_time', kind='ms')
79 self.assertReportedStat('zuul.pipeline.gate.total_changes',
80 value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070081 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070082 'zuul.pipeline.gate.org.project.resident_time', kind='ms')
James E. Blair412e5582013-04-22 15:50:12 -070083 self.assertReportedStat(
James E. Blair66eeebf2013-07-27 17:44:32 -070084 'zuul.pipeline.gate.org.project.total_changes', value='1|c')
James E. Blair412e5582013-04-22 15:50:12 -070085
James E. Blair5821bd92015-09-16 08:48:15 -070086 for build in self.builds:
87 self.assertEqual(build.parameters['ZUUL_VOTING'], '1')
88
James E. Blair3cb10702013-08-24 08:56:03 -070089 def test_initial_pipeline_gauges(self):
90 "Test that each pipeline reported its length on start"
James E. Blair7da51b72016-07-25 15:29:43 -070091 self.assertReportedStat('zuul.pipeline.gate.current_changes',
92 value='0|g')
93 self.assertReportedStat('zuul.pipeline.check.current_changes',
94 value='0|g')
James E. Blair3cb10702013-08-24 08:56:03 -070095
James E. Blair34776ee2016-08-25 13:53:54 -070096 def test_job_branch(self):
97 "Test the correct variant of a job runs on a branch"
98 self.create_branch('org/project', 'stable')
99 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A')
100 A.addApproval('code-review', 2)
101 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
102 self.waitUntilSettled()
103 self.assertEqual(self.getJobFromHistory('project-test1').result,
104 'SUCCESS')
105 self.assertEqual(self.getJobFromHistory('project-test2').result,
106 'SUCCESS')
107 self.assertEqual(A.data['status'], 'MERGED')
108 self.assertEqual(A.reported, 2,
109 "A should report start and success")
110 self.assertIn('gate', A.messages[1],
111 "A should transit gate")
112 self.assertEqual(self.getJobFromHistory('project-test1').node,
James E. Blair16d96a02017-06-08 11:32:56 -0700113 'label2')
James E. Blair34776ee2016-08-25 13:53:54 -0700114
James E. Blairb0fcae42012-07-17 11:12:10 -0700115 def test_parallel_changes(self):
116 "Test that changes are tested in parallel and merged in series"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700117
Paul Belanger174a8272017-03-14 13:20:10 -0400118 self.executor_server.hold_jobs_in_build = True
James E. Blairb0fcae42012-07-17 11:12:10 -0700119 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
120 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
121 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700122 A.addApproval('code-review', 2)
123 B.addApproval('code-review', 2)
124 C.addApproval('code-review', 2)
James E. Blairb0fcae42012-07-17 11:12:10 -0700125
James E. Blair8b5408c2016-08-08 15:37:46 -0700126 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
127 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
128 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb0fcae42012-07-17 11:12:10 -0700129
130 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400131 self.assertEqual(len(self.builds), 1)
132 self.assertEqual(self.builds[0].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700133 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700134
Paul Belanger174a8272017-03-14 13:20:10 -0400135 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700136 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400137 self.assertEqual(len(self.builds), 3)
138 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700139 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400140 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700141 self.assertTrue(self.builds[1].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400142 self.assertEqual(self.builds[2].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700143 self.assertTrue(self.builds[2].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700144
Paul Belanger174a8272017-03-14 13:20:10 -0400145 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700146 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400147 self.assertEqual(len(self.builds), 5)
148 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700149 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400150 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700151 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700152
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400153 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700154 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400155 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700156 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700157
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400158 self.assertEqual(self.builds[4].name, 'project-merge')
James E. Blair791b5392016-08-03 11:25:56 -0700159 self.assertTrue(self.builds[4].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700160
Paul Belanger174a8272017-03-14 13:20:10 -0400161 self.executor_server.release('.*-merge')
James E. Blairb0fcae42012-07-17 11:12:10 -0700162 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400163 self.assertEqual(len(self.builds), 6)
164 self.assertEqual(self.builds[0].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700165 self.assertTrue(self.builds[0].hasChanges(A))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400166 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700167 self.assertTrue(self.builds[1].hasChanges(A))
James E. Blairb0fcae42012-07-17 11:12:10 -0700168
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400169 self.assertEqual(self.builds[2].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700170 self.assertTrue(self.builds[2].hasChanges(A, B))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400171 self.assertEqual(self.builds[3].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700172 self.assertTrue(self.builds[3].hasChanges(A, B))
James E. Blairb0fcae42012-07-17 11:12:10 -0700173
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400174 self.assertEqual(self.builds[4].name, 'project-test1')
James E. Blair791b5392016-08-03 11:25:56 -0700175 self.assertTrue(self.builds[4].hasChanges(A, B, C))
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400176 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blair791b5392016-08-03 11:25:56 -0700177 self.assertTrue(self.builds[5].hasChanges(A, B, C))
James E. Blairb0fcae42012-07-17 11:12:10 -0700178
Paul Belanger174a8272017-03-14 13:20:10 -0400179 self.executor_server.hold_jobs_in_build = False
180 self.executor_server.release()
James E. Blairb0fcae42012-07-17 11:12:10 -0700181 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400182 self.assertEqual(len(self.builds), 0)
James E. Blairb0fcae42012-07-17 11:12:10 -0700183
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400184 self.assertEqual(len(self.history), 9)
185 self.assertEqual(A.data['status'], 'MERGED')
186 self.assertEqual(B.data['status'], 'MERGED')
187 self.assertEqual(C.data['status'], 'MERGED')
188 self.assertEqual(A.reported, 2)
189 self.assertEqual(B.reported, 2)
190 self.assertEqual(C.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700191
192 def test_failed_changes(self):
193 "Test that a change behind a failed change is retested"
Paul Belanger174a8272017-03-14 13:20:10 -0400194 self.executor_server.hold_jobs_in_build = True
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700195
James E. Blairb02a3bb2012-07-30 17:49:55 -0700196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
197 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -0700198 A.addApproval('code-review', 2)
199 B.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700200
Paul Belanger174a8272017-03-14 13:20:10 -0400201 self.executor_server.failJob('project-test1', A)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700202
James E. Blair8b5408c2016-08-08 15:37:46 -0700203 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
204 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700205 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700206 self.assertBuilds([dict(name='project-merge', changes='1,1')])
James E. Blaire2819012013-06-28 17:17:26 -0400207
Paul Belanger174a8272017-03-14 13:20:10 -0400208 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400209 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700210 # A/project-merge is complete
211 self.assertBuilds([
212 dict(name='project-test1', changes='1,1'),
213 dict(name='project-test2', changes='1,1'),
214 dict(name='project-merge', changes='1,1 2,1'),
215 ])
James E. Blaire2819012013-06-28 17:17:26 -0400216
Paul Belanger174a8272017-03-14 13:20:10 -0400217 self.executor_server.release('.*-merge')
James E. Blaire2819012013-06-28 17:17:26 -0400218 self.waitUntilSettled()
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700219 # A/project-merge is complete
220 # B/project-merge is complete
221 self.assertBuilds([
222 dict(name='project-test1', changes='1,1'),
223 dict(name='project-test2', changes='1,1'),
224 dict(name='project-test1', changes='1,1 2,1'),
225 dict(name='project-test2', changes='1,1 2,1'),
226 ])
227
228 # Release project-test1 for A which will fail. This will
Paul Belanger174a8272017-03-14 13:20:10 -0400229 # abort both running B jobs and reexecute project-merge for B.
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700230 self.builds[0].release()
231 self.waitUntilSettled()
232
233 self.orderedRelease()
234 self.assertHistory([
235 dict(name='project-merge', result='SUCCESS', changes='1,1'),
236 dict(name='project-merge', result='SUCCESS', changes='1,1 2,1'),
237 dict(name='project-test1', result='FAILURE', changes='1,1'),
238 dict(name='project-test1', result='ABORTED', changes='1,1 2,1'),
239 dict(name='project-test2', result='ABORTED', changes='1,1 2,1'),
240 dict(name='project-test2', result='SUCCESS', changes='1,1'),
241 dict(name='project-merge', result='SUCCESS', changes='2,1'),
242 dict(name='project-test1', result='SUCCESS', changes='2,1'),
243 dict(name='project-test2', result='SUCCESS', changes='2,1'),
James E. Blairb536ecc2016-08-31 10:11:42 -0700244 ], ordered=False)
James E. Blair2b2a8ab2016-08-11 14:39:11 -0700245
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400246 self.assertEqual(A.data['status'], 'NEW')
247 self.assertEqual(B.data['status'], 'MERGED')
248 self.assertEqual(A.reported, 2)
249 self.assertEqual(B.reported, 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700250
251 def test_independent_queues(self):
252 "Test that changes end up in the right queues"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700253
Paul Belanger174a8272017-03-14 13:20:10 -0400254 self.executor_server.hold_jobs_in_build = True
Zhongyue Luo5d556072012-09-21 02:00:47 +0900255 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700256 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
257 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700258 A.addApproval('code-review', 2)
259 B.addApproval('code-review', 2)
260 C.addApproval('code-review', 2)
James E. Blairb02a3bb2012-07-30 17:49:55 -0700261
James E. Blair8b5408c2016-08-08 15:37:46 -0700262 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairf59f3cf2017-02-19 14:50:26 -0800263 self.waitUntilSettled()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700264
James E. Blairf59f3cf2017-02-19 14:50:26 -0800265 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
266 self.waitUntilSettled()
267
268 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairb02a3bb2012-07-30 17:49:55 -0700269 self.waitUntilSettled()
270
271 # There should be one merge job at the head of each queue running
James E. Blair5f0d49e2016-08-19 09:36:50 -0700272 self.assertBuilds([
273 dict(name='project-merge', changes='1,1'),
274 dict(name='project-merge', changes='2,1'),
275 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700276
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700277 # Release the current merge builds
James E. Blair5f0d49e2016-08-19 09:36:50 -0700278 self.builds[0].release()
279 self.waitUntilSettled()
280 self.builds[0].release()
James E. Blairb02a3bb2012-07-30 17:49:55 -0700281 self.waitUntilSettled()
282 # Release the merge job for project2 which is behind project1
Paul Belanger174a8272017-03-14 13:20:10 -0400283 self.executor_server.release('.*-merge')
James E. Blairb02a3bb2012-07-30 17:49:55 -0700284 self.waitUntilSettled()
285
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700286 # All the test builds should be running:
James E. Blair5f0d49e2016-08-19 09:36:50 -0700287 self.assertBuilds([
288 dict(name='project-test1', changes='1,1'),
289 dict(name='project-test2', changes='1,1'),
290 dict(name='project-test1', changes='2,1'),
291 dict(name='project-test2', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500292 dict(name='project1-project2-integration', changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700293 dict(name='project-test1', changes='2,1 3,1'),
294 dict(name='project-test2', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800295 dict(name='project1-project2-integration', changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700296 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700297
James E. Blair5f0d49e2016-08-19 09:36:50 -0700298 self.orderedRelease()
299 self.assertHistory([
300 dict(name='project-merge', result='SUCCESS', changes='1,1'),
301 dict(name='project-merge', result='SUCCESS', changes='2,1'),
302 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
303 dict(name='project-test1', result='SUCCESS', changes='1,1'),
304 dict(name='project-test2', result='SUCCESS', changes='1,1'),
305 dict(name='project-test1', result='SUCCESS', changes='2,1'),
306 dict(name='project-test2', result='SUCCESS', changes='2,1'),
Paul Belanger58bf6912016-11-11 19:36:01 -0500307 dict(
308 name='project1-project2-integration',
309 result='SUCCESS',
310 changes='2,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700311 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
312 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
Clint Byrumcd9393f2016-12-21 12:21:07 -0800313 dict(name='project1-project2-integration',
314 result='SUCCESS',
315 changes='2,1 3,1'),
James E. Blair5f0d49e2016-08-19 09:36:50 -0700316 ])
James E. Blairb02a3bb2012-07-30 17:49:55 -0700317
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400318 self.assertEqual(A.data['status'], 'MERGED')
319 self.assertEqual(B.data['status'], 'MERGED')
320 self.assertEqual(C.data['status'], 'MERGED')
321 self.assertEqual(A.reported, 2)
322 self.assertEqual(B.reported, 2)
323 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700324
325 def test_failed_change_at_head(self):
326 "Test that if a change at the head fails, jobs behind it are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700327
Paul Belanger174a8272017-03-14 13:20:10 -0400328 self.executor_server.hold_jobs_in_build = True
James E. Blaird466dc42012-07-31 10:42:56 -0700329 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
330 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
331 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700332 A.addApproval('code-review', 2)
333 B.addApproval('code-review', 2)
334 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700335
Paul Belanger174a8272017-03-14 13:20:10 -0400336 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700337
James E. Blair8b5408c2016-08-08 15:37:46 -0700338 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
339 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
340 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700341
342 self.waitUntilSettled()
James E. Blaird466dc42012-07-31 10:42:56 -0700343
James E. Blairb536ecc2016-08-31 10:11:42 -0700344 self.assertBuilds([
345 dict(name='project-merge', changes='1,1'),
346 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700347
Paul Belanger174a8272017-03-14 13:20:10 -0400348 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700349 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400350 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700351 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400352 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700353 self.waitUntilSettled()
354
James E. Blairb536ecc2016-08-31 10:11:42 -0700355 self.assertBuilds([
356 dict(name='project-test1', changes='1,1'),
357 dict(name='project-test2', changes='1,1'),
358 dict(name='project-test1', changes='1,1 2,1'),
359 dict(name='project-test2', changes='1,1 2,1'),
360 dict(name='project-test1', changes='1,1 2,1 3,1'),
361 dict(name='project-test2', changes='1,1 2,1 3,1'),
362 ])
James E. Blaird466dc42012-07-31 10:42:56 -0700363
Monty Taylor6bef8ef2013-06-02 08:17:12 -0400364 self.release(self.builds[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700365 self.waitUntilSettled()
366
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400367 # project-test2, project-merge for B
James E. Blairb536ecc2016-08-31 10:11:42 -0700368 self.assertBuilds([
369 dict(name='project-test2', changes='1,1'),
370 dict(name='project-merge', changes='2,1'),
371 ])
372 # Unordered history comparison because the aborts can finish
373 # in any order.
374 self.assertHistory([
375 dict(name='project-merge', result='SUCCESS',
376 changes='1,1'),
377 dict(name='project-merge', result='SUCCESS',
378 changes='1,1 2,1'),
379 dict(name='project-merge', result='SUCCESS',
380 changes='1,1 2,1 3,1'),
381 dict(name='project-test1', result='FAILURE',
382 changes='1,1'),
383 dict(name='project-test1', result='ABORTED',
384 changes='1,1 2,1'),
385 dict(name='project-test2', result='ABORTED',
386 changes='1,1 2,1'),
387 dict(name='project-test1', result='ABORTED',
388 changes='1,1 2,1 3,1'),
389 dict(name='project-test2', result='ABORTED',
390 changes='1,1 2,1 3,1'),
391 ], ordered=False)
James E. Blaird466dc42012-07-31 10:42:56 -0700392
Paul Belanger174a8272017-03-14 13:20:10 -0400393 self.executor_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700394 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400395 self.executor_server.release('.*-merge')
James E. Blairb536ecc2016-08-31 10:11:42 -0700396 self.waitUntilSettled()
397 self.orderedRelease()
James E. Blaird466dc42012-07-31 10:42:56 -0700398
James E. Blairb536ecc2016-08-31 10:11:42 -0700399 self.assertBuilds([])
400 self.assertHistory([
401 dict(name='project-merge', result='SUCCESS',
402 changes='1,1'),
403 dict(name='project-merge', result='SUCCESS',
404 changes='1,1 2,1'),
405 dict(name='project-merge', result='SUCCESS',
406 changes='1,1 2,1 3,1'),
407 dict(name='project-test1', result='FAILURE',
408 changes='1,1'),
409 dict(name='project-test1', result='ABORTED',
410 changes='1,1 2,1'),
411 dict(name='project-test2', result='ABORTED',
412 changes='1,1 2,1'),
413 dict(name='project-test1', result='ABORTED',
414 changes='1,1 2,1 3,1'),
415 dict(name='project-test2', result='ABORTED',
416 changes='1,1 2,1 3,1'),
417 dict(name='project-merge', result='SUCCESS',
418 changes='2,1'),
419 dict(name='project-merge', result='SUCCESS',
420 changes='2,1 3,1'),
421 dict(name='project-test2', result='SUCCESS',
422 changes='1,1'),
423 dict(name='project-test1', result='SUCCESS',
424 changes='2,1'),
425 dict(name='project-test2', result='SUCCESS',
426 changes='2,1'),
427 dict(name='project-test1', result='SUCCESS',
428 changes='2,1 3,1'),
429 dict(name='project-test2', result='SUCCESS',
430 changes='2,1 3,1'),
431 ], ordered=False)
432
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400433 self.assertEqual(A.data['status'], 'NEW')
434 self.assertEqual(B.data['status'], 'MERGED')
435 self.assertEqual(C.data['status'], 'MERGED')
436 self.assertEqual(A.reported, 2)
437 self.assertEqual(B.reported, 2)
438 self.assertEqual(C.reported, 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700439
James E. Blair0aac4872013-08-23 14:02:38 -0700440 def test_failed_change_in_middle(self):
441 "Test a failed change in the middle of the queue"
442
Paul Belanger174a8272017-03-14 13:20:10 -0400443 self.executor_server.hold_jobs_in_build = True
James E. Blair0aac4872013-08-23 14:02:38 -0700444 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
445 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
446 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700447 A.addApproval('code-review', 2)
448 B.addApproval('code-review', 2)
449 C.addApproval('code-review', 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700450
Paul Belanger174a8272017-03-14 13:20:10 -0400451 self.executor_server.failJob('project-test1', B)
James E. Blair0aac4872013-08-23 14:02:38 -0700452
James E. Blair8b5408c2016-08-08 15:37:46 -0700453 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
454 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
455 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair0aac4872013-08-23 14:02:38 -0700456
457 self.waitUntilSettled()
458
Paul Belanger174a8272017-03-14 13:20:10 -0400459 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700460 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400461 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700462 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400463 self.executor_server.release('.*-merge')
James E. Blair0aac4872013-08-23 14:02:38 -0700464 self.waitUntilSettled()
465
466 self.assertEqual(len(self.builds), 6)
467 self.assertEqual(self.builds[0].name, 'project-test1')
468 self.assertEqual(self.builds[1].name, 'project-test2')
469 self.assertEqual(self.builds[2].name, 'project-test1')
470 self.assertEqual(self.builds[3].name, 'project-test2')
471 self.assertEqual(self.builds[4].name, 'project-test1')
472 self.assertEqual(self.builds[5].name, 'project-test2')
473
474 self.release(self.builds[2])
475 self.waitUntilSettled()
476
James E. Blair972e3c72013-08-29 12:04:55 -0700477 # project-test1 and project-test2 for A
478 # project-test2 for B
479 # project-merge for C (without B)
480 self.assertEqual(len(self.builds), 4)
James E. Blair0aac4872013-08-23 14:02:38 -0700481 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
482
Paul Belanger174a8272017-03-14 13:20:10 -0400483 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -0700484 self.waitUntilSettled()
485
486 # project-test1 and project-test2 for A
487 # project-test2 for B
488 # project-test1 and project-test2 for C
489 self.assertEqual(len(self.builds), 5)
490
Paul Belanger2e2a0492016-10-30 18:33:37 -0400491 tenant = self.sched.abide.tenants.get('tenant-one')
492 items = tenant.layout.pipelines['gate'].getAllItems()
James E. Blair0aac4872013-08-23 14:02:38 -0700493 builds = items[0].current_build_set.getBuilds()
494 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
495 self.assertEqual(self.countJobResults(builds, None), 2)
496 builds = items[1].current_build_set.getBuilds()
497 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
498 self.assertEqual(self.countJobResults(builds, 'FAILURE'), 1)
499 self.assertEqual(self.countJobResults(builds, None), 1)
500 builds = items[2].current_build_set.getBuilds()
501 self.assertEqual(self.countJobResults(builds, 'SUCCESS'), 1)
James E. Blair972e3c72013-08-29 12:04:55 -0700502 self.assertEqual(self.countJobResults(builds, None), 2)
James E. Blair0aac4872013-08-23 14:02:38 -0700503
Paul Belanger174a8272017-03-14 13:20:10 -0400504 self.executor_server.hold_jobs_in_build = False
505 self.executor_server.release()
James E. Blair0aac4872013-08-23 14:02:38 -0700506 self.waitUntilSettled()
507
508 self.assertEqual(len(self.builds), 0)
509 self.assertEqual(len(self.history), 12)
510 self.assertEqual(A.data['status'], 'MERGED')
511 self.assertEqual(B.data['status'], 'NEW')
512 self.assertEqual(C.data['status'], 'MERGED')
513 self.assertEqual(A.reported, 2)
514 self.assertEqual(B.reported, 2)
515 self.assertEqual(C.reported, 2)
516
James E. Blaird466dc42012-07-31 10:42:56 -0700517 def test_failed_change_at_head_with_queue(self):
518 "Test that if a change at the head fails, queued jobs are canceled"
James E. Blaird466dc42012-07-31 10:42:56 -0700519
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700520 self.gearman_server.hold_jobs_in_queue = True
James E. Blaird466dc42012-07-31 10:42:56 -0700521 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
522 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
523 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700524 A.addApproval('code-review', 2)
525 B.addApproval('code-review', 2)
526 C.addApproval('code-review', 2)
James E. Blaird466dc42012-07-31 10:42:56 -0700527
Paul Belanger174a8272017-03-14 13:20:10 -0400528 self.executor_server.failJob('project-test1', A)
James E. Blaird466dc42012-07-31 10:42:56 -0700529
James E. Blair8b5408c2016-08-08 15:37:46 -0700530 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
531 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
532 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blaird466dc42012-07-31 10:42:56 -0700533
534 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700535 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400536 self.assertEqual(len(self.builds), 0)
537 self.assertEqual(len(queue), 1)
Clint Byrumf322fe22017-05-10 20:53:12 -0700538 self.assertEqual(queue[0].name, b'executor:execute')
539 job_args = json.loads(queue[0].arguments.decode('utf8'))
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800540 self.assertEqual(job_args['job'], 'project-merge')
541 self.assertEqual(job_args['items'][0]['number'], '%d' % A.number)
James E. Blaird466dc42012-07-31 10:42:56 -0700542
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700543 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700544 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700545 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700546 self.waitUntilSettled()
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700547 self.gearman_server.release('.*-merge')
James E. Blaird466dc42012-07-31 10:42:56 -0700548 self.waitUntilSettled()
James E. Blair701c5b42013-06-06 09:34:59 -0700549 queue = self.gearman_server.getQueue()
James E. Blaird466dc42012-07-31 10:42:56 -0700550
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400551 self.assertEqual(len(self.builds), 0)
552 self.assertEqual(len(queue), 6)
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800553
554 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700555 json.loads(queue[0].arguments.decode('utf8'))['job'],
556 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800557 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700558 json.loads(queue[1].arguments.decode('utf8'))['job'],
559 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800560 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700561 json.loads(queue[2].arguments.decode('utf8'))['job'],
562 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800563 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700564 json.loads(queue[3].arguments.decode('utf8'))['job'],
565 'project-test2')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800566 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700567 json.loads(queue[4].arguments.decode('utf8'))['job'],
568 'project-test1')
Clint Byrum8b2bc0c2016-11-16 16:28:50 -0800569 self.assertEqual(
Clint Byrumf322fe22017-05-10 20:53:12 -0700570 json.loads(queue[5].arguments.decode('utf8'))['job'],
571 'project-test2')
James E. Blaird466dc42012-07-31 10:42:56 -0700572
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700573 self.release(queue[0])
James E. Blaird466dc42012-07-31 10:42:56 -0700574 self.waitUntilSettled()
575
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400576 self.assertEqual(len(self.builds), 0)
James E. Blair701c5b42013-06-06 09:34:59 -0700577 queue = self.gearman_server.getQueue()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400578 self.assertEqual(len(queue), 2) # project-test2, project-merge for B
579 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 0)
James E. Blaird466dc42012-07-31 10:42:56 -0700580
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700581 self.gearman_server.hold_jobs_in_queue = False
582 self.gearman_server.release()
James E. Blaird466dc42012-07-31 10:42:56 -0700583 self.waitUntilSettled()
584
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400585 self.assertEqual(len(self.builds), 0)
586 self.assertEqual(len(self.history), 11)
587 self.assertEqual(A.data['status'], 'NEW')
588 self.assertEqual(B.data['status'], 'MERGED')
589 self.assertEqual(C.data['status'], 'MERGED')
590 self.assertEqual(A.reported, 2)
591 self.assertEqual(B.reported, 2)
592 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700593
James E. Blairec056492016-07-22 09:45:56 -0700594 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700595 def _test_time_database(self, iteration):
Paul Belanger174a8272017-03-14 13:20:10 -0400596 self.executor_server.hold_jobs_in_build = True
James E. Blairce8a2132016-05-19 15:21:52 -0700597 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -0700598 A.addApproval('code-review', 2)
599 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairce8a2132016-05-19 15:21:52 -0700600 self.waitUntilSettled()
601 time.sleep(2)
602
603 data = json.loads(self.sched.formatStatusJSON())
604 found_job = None
605 for pipeline in data['pipelines']:
606 if pipeline['name'] != 'gate':
607 continue
608 for queue in pipeline['change_queues']:
609 for head in queue['heads']:
610 for item in head:
611 for job in item['jobs']:
612 if job['name'] == 'project-merge':
613 found_job = job
614 break
615
616 self.assertIsNotNone(found_job)
617 if iteration == 1:
618 self.assertIsNotNone(found_job['estimated_time'])
619 self.assertIsNone(found_job['remaining_time'])
620 else:
621 self.assertIsNotNone(found_job['estimated_time'])
622 self.assertTrue(found_job['estimated_time'] >= 2)
623 self.assertIsNotNone(found_job['remaining_time'])
624
Paul Belanger174a8272017-03-14 13:20:10 -0400625 self.executor_server.hold_jobs_in_build = False
626 self.executor_server.release()
James E. Blairce8a2132016-05-19 15:21:52 -0700627 self.waitUntilSettled()
628
James E. Blairec056492016-07-22 09:45:56 -0700629 @skip("Disabled for early v3 development")
James E. Blairce8a2132016-05-19 15:21:52 -0700630 def test_time_database(self):
631 "Test the time database"
632
633 self._test_time_database(1)
634 self._test_time_database(2)
635
James E. Blairfef71632013-09-23 11:15:47 -0700636 def test_two_failed_changes_at_head(self):
637 "Test that changes are reparented correctly if 2 fail at head"
638
Paul Belanger174a8272017-03-14 13:20:10 -0400639 self.executor_server.hold_jobs_in_build = True
James E. Blairfef71632013-09-23 11:15:47 -0700640 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
641 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
642 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700643 A.addApproval('code-review', 2)
644 B.addApproval('code-review', 2)
645 C.addApproval('code-review', 2)
James E. Blairfef71632013-09-23 11:15:47 -0700646
Paul Belanger174a8272017-03-14 13:20:10 -0400647 self.executor_server.failJob('project-test1', A)
648 self.executor_server.failJob('project-test1', B)
James E. Blairfef71632013-09-23 11:15:47 -0700649
James E. Blair8b5408c2016-08-08 15:37:46 -0700650 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
651 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
652 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairfef71632013-09-23 11:15:47 -0700653 self.waitUntilSettled()
654
Paul Belanger174a8272017-03-14 13:20:10 -0400655 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700656 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400657 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700658 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400659 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700660 self.waitUntilSettled()
661
662 self.assertEqual(len(self.builds), 6)
663 self.assertEqual(self.builds[0].name, 'project-test1')
664 self.assertEqual(self.builds[1].name, 'project-test2')
665 self.assertEqual(self.builds[2].name, 'project-test1')
666 self.assertEqual(self.builds[3].name, 'project-test2')
667 self.assertEqual(self.builds[4].name, 'project-test1')
668 self.assertEqual(self.builds[5].name, 'project-test2')
669
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400670 self.assertTrue(self.builds[0].hasChanges(A))
671 self.assertTrue(self.builds[2].hasChanges(A))
672 self.assertTrue(self.builds[2].hasChanges(B))
673 self.assertTrue(self.builds[4].hasChanges(A))
674 self.assertTrue(self.builds[4].hasChanges(B))
675 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700676
677 # Fail change B first
678 self.release(self.builds[2])
679 self.waitUntilSettled()
680
681 # restart of C after B failure
Paul Belanger174a8272017-03-14 13:20:10 -0400682 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700683 self.waitUntilSettled()
684
685 self.assertEqual(len(self.builds), 5)
686 self.assertEqual(self.builds[0].name, 'project-test1')
687 self.assertEqual(self.builds[1].name, 'project-test2')
688 self.assertEqual(self.builds[2].name, 'project-test2')
689 self.assertEqual(self.builds[3].name, 'project-test1')
690 self.assertEqual(self.builds[4].name, 'project-test2')
691
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400692 self.assertTrue(self.builds[1].hasChanges(A))
693 self.assertTrue(self.builds[2].hasChanges(A))
694 self.assertTrue(self.builds[2].hasChanges(B))
695 self.assertTrue(self.builds[4].hasChanges(A))
696 self.assertFalse(self.builds[4].hasChanges(B))
697 self.assertTrue(self.builds[4].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700698
699 # Finish running all passing jobs for change A
700 self.release(self.builds[1])
701 self.waitUntilSettled()
702 # Fail and report change A
703 self.release(self.builds[0])
704 self.waitUntilSettled()
705
706 # restart of B,C after A failure
Paul Belanger174a8272017-03-14 13:20:10 -0400707 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700708 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400709 self.executor_server.release('.*-merge')
James E. Blairfef71632013-09-23 11:15:47 -0700710 self.waitUntilSettled()
711
712 self.assertEqual(len(self.builds), 4)
713 self.assertEqual(self.builds[0].name, 'project-test1') # B
714 self.assertEqual(self.builds[1].name, 'project-test2') # B
715 self.assertEqual(self.builds[2].name, 'project-test1') # C
716 self.assertEqual(self.builds[3].name, 'project-test2') # C
717
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400718 self.assertFalse(self.builds[1].hasChanges(A))
719 self.assertTrue(self.builds[1].hasChanges(B))
720 self.assertFalse(self.builds[1].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700721
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400722 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blairfef71632013-09-23 11:15:47 -0700723 # After A failed and B and C restarted, B should be back in
724 # C's tests because it has not failed yet.
Paul Belanger71dd2fc2016-11-04 19:05:55 -0400725 self.assertTrue(self.builds[2].hasChanges(B))
726 self.assertTrue(self.builds[2].hasChanges(C))
James E. Blairfef71632013-09-23 11:15:47 -0700727
Paul Belanger174a8272017-03-14 13:20:10 -0400728 self.executor_server.hold_jobs_in_build = False
729 self.executor_server.release()
James E. Blairfef71632013-09-23 11:15:47 -0700730 self.waitUntilSettled()
731
732 self.assertEqual(len(self.builds), 0)
733 self.assertEqual(len(self.history), 21)
734 self.assertEqual(A.data['status'], 'NEW')
735 self.assertEqual(B.data['status'], 'NEW')
736 self.assertEqual(C.data['status'], 'MERGED')
737 self.assertEqual(A.reported, 2)
738 self.assertEqual(B.reported, 2)
739 self.assertEqual(C.reported, 2)
740
James E. Blair8c803f82012-07-31 16:25:42 -0700741 def test_patch_order(self):
742 "Test that dependent patches are tested in the right order"
743 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
744 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
745 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700746 A.addApproval('code-review', 2)
747 B.addApproval('code-review', 2)
748 C.addApproval('code-review', 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700749
750 M2 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M2')
751 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
752 M2.setMerged()
753 M1.setMerged()
754
755 # C -> B -> A -> M1 -> M2
756 # M2 is here to make sure it is never queried. If it is, it
757 # means zuul is walking down the entire history of merged
758 # changes.
759
760 C.setDependsOn(B, 1)
761 B.setDependsOn(A, 1)
762 A.setDependsOn(M1, 1)
763 M1.setDependsOn(M2, 1)
764
James E. Blair8b5408c2016-08-08 15:37:46 -0700765 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700766
767 self.waitUntilSettled()
768
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400769 self.assertEqual(A.data['status'], 'NEW')
770 self.assertEqual(B.data['status'], 'NEW')
771 self.assertEqual(C.data['status'], 'NEW')
James E. Blair8c803f82012-07-31 16:25:42 -0700772
James E. Blair8b5408c2016-08-08 15:37:46 -0700773 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
774 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8c803f82012-07-31 16:25:42 -0700775
776 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400777 self.assertEqual(M2.queried, 0)
778 self.assertEqual(A.data['status'], 'MERGED')
779 self.assertEqual(B.data['status'], 'MERGED')
780 self.assertEqual(C.data['status'], 'MERGED')
781 self.assertEqual(A.reported, 2)
782 self.assertEqual(B.reported, 2)
783 self.assertEqual(C.reported, 2)
James E. Blair8c803f82012-07-31 16:25:42 -0700784
James E. Blair063672f2015-01-29 13:09:12 -0800785 def test_needed_changes_enqueue(self):
786 "Test that a needed change is enqueued ahead"
787 # A Given a git tree like this, if we enqueue
788 # / \ change C, we should walk up and down the tree
789 # B G and enqueue changes in the order ABCDEFG.
790 # /|\ This is also the order that you would get if
791 # *C E F you enqueued changes in the order ABCDEFG, so
792 # / the ordering is stable across re-enqueue events.
793 # D
794
795 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
796 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
797 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
798 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
799 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
800 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
801 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
802 B.setDependsOn(A, 1)
803 C.setDependsOn(B, 1)
804 D.setDependsOn(C, 1)
805 E.setDependsOn(B, 1)
806 F.setDependsOn(B, 1)
807 G.setDependsOn(A, 1)
808
James E. Blair8b5408c2016-08-08 15:37:46 -0700809 A.addApproval('code-review', 2)
810 B.addApproval('code-review', 2)
811 C.addApproval('code-review', 2)
812 D.addApproval('code-review', 2)
813 E.addApproval('code-review', 2)
814 F.addApproval('code-review', 2)
815 G.addApproval('code-review', 2)
816 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800817
818 self.waitUntilSettled()
819
820 self.assertEqual(A.data['status'], 'NEW')
821 self.assertEqual(B.data['status'], 'NEW')
822 self.assertEqual(C.data['status'], 'NEW')
823 self.assertEqual(D.data['status'], 'NEW')
824 self.assertEqual(E.data['status'], 'NEW')
825 self.assertEqual(F.data['status'], 'NEW')
826 self.assertEqual(G.data['status'], 'NEW')
827
828 # We're about to add approvals to changes without adding the
829 # triggering events to Zuul, so that we can be sure that it is
830 # enqueing the changes based on dependencies, not because of
831 # triggering events. Since it will have the changes cached
832 # already (without approvals), we need to clear the cache
833 # first.
Paul Belanger593480a2016-11-02 19:06:28 -0400834 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +1100835 connection.maintainCache([])
James E. Blair063672f2015-01-29 13:09:12 -0800836
Paul Belanger174a8272017-03-14 13:20:10 -0400837 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -0700838 A.addApproval('approved', 1)
839 B.addApproval('approved', 1)
840 D.addApproval('approved', 1)
841 E.addApproval('approved', 1)
842 F.addApproval('approved', 1)
843 G.addApproval('approved', 1)
844 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair063672f2015-01-29 13:09:12 -0800845
846 for x in range(8):
Paul Belanger174a8272017-03-14 13:20:10 -0400847 self.executor_server.release('.*-merge')
James E. Blair063672f2015-01-29 13:09:12 -0800848 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -0400849 self.executor_server.hold_jobs_in_build = False
850 self.executor_server.release()
James E. Blair063672f2015-01-29 13:09:12 -0800851 self.waitUntilSettled()
852
853 self.assertEqual(A.data['status'], 'MERGED')
854 self.assertEqual(B.data['status'], 'MERGED')
855 self.assertEqual(C.data['status'], 'MERGED')
856 self.assertEqual(D.data['status'], 'MERGED')
857 self.assertEqual(E.data['status'], 'MERGED')
858 self.assertEqual(F.data['status'], 'MERGED')
859 self.assertEqual(G.data['status'], 'MERGED')
860 self.assertEqual(A.reported, 2)
861 self.assertEqual(B.reported, 2)
862 self.assertEqual(C.reported, 2)
863 self.assertEqual(D.reported, 2)
864 self.assertEqual(E.reported, 2)
865 self.assertEqual(F.reported, 2)
866 self.assertEqual(G.reported, 2)
867 self.assertEqual(self.history[6].changes,
868 '1,1 2,1 3,1 4,1 5,1 6,1 7,1')
869
Joshua Hesketh850ccb62014-11-27 11:31:02 +1100870 def test_source_cache(self):
871 "Test that the source cache operates correctly"
Paul Belanger174a8272017-03-14 13:20:10 -0400872 self.executor_server.hold_jobs_in_build = True
James E. Blair0e933c52013-07-11 10:18:52 -0700873
874 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
875 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
876 X = self.fake_gerrit.addFakeChange('org/project', 'master', 'X')
James E. Blair8b5408c2016-08-08 15:37:46 -0700877 A.addApproval('code-review', 2)
878 B.addApproval('code-review', 2)
James E. Blair0e933c52013-07-11 10:18:52 -0700879
880 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
881 M1.setMerged()
882
883 B.setDependsOn(A, 1)
884 A.setDependsOn(M1, 1)
885
James E. Blair8b5408c2016-08-08 15:37:46 -0700886 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700887 self.fake_gerrit.addEvent(X.getPatchsetCreatedEvent(1))
888
889 self.waitUntilSettled()
890
891 for build in self.builds:
892 if build.parameters['ZUUL_PIPELINE'] == 'check':
893 build.release()
894 self.waitUntilSettled()
895 for build in self.builds:
896 if build.parameters['ZUUL_PIPELINE'] == 'check':
897 build.release()
898 self.waitUntilSettled()
899
James E. Blair8b5408c2016-08-08 15:37:46 -0700900 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair0e933c52013-07-11 10:18:52 -0700901 self.waitUntilSettled()
902
Joshua Hesketh352264b2015-08-11 23:42:08 +1000903 self.log.debug("len %s" % self.fake_gerrit._change_cache.keys())
James E. Blair0e933c52013-07-11 10:18:52 -0700904 # there should still be changes in the cache
Joshua Hesketh352264b2015-08-11 23:42:08 +1000905 self.assertNotEqual(len(self.fake_gerrit._change_cache.keys()), 0)
James E. Blair0e933c52013-07-11 10:18:52 -0700906
Paul Belanger174a8272017-03-14 13:20:10 -0400907 self.executor_server.hold_jobs_in_build = False
908 self.executor_server.release()
James E. Blair0e933c52013-07-11 10:18:52 -0700909 self.waitUntilSettled()
910
911 self.assertEqual(A.data['status'], 'MERGED')
912 self.assertEqual(B.data['status'], 'MERGED')
913 self.assertEqual(A.queried, 2) # Initial and isMerged
914 self.assertEqual(B.queried, 3) # Initial A, refresh from B, isMerged
915
James E. Blair8c803f82012-07-31 16:25:42 -0700916 def test_can_merge(self):
James E. Blair4886cc12012-07-18 15:39:41 -0700917 "Test whether a change is ready to merge"
James E. Blair8c803f82012-07-31 16:25:42 -0700918 # TODO: move to test_gerrit (this is a unit test!)
919 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger9bba4902016-11-02 16:07:33 -0400920 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -0700921 (trusted, project) = tenant.getProject('org/project')
922 source = project.source
Paul Belanger9bba4902016-11-02 16:07:33 -0400923
924 # TODO(pabelanger): As we add more source / trigger APIs we should make
925 # it easier for users to create events for testing.
926 event = zuul.model.TriggerEvent()
927 event.trigger_name = 'gerrit'
928 event.change_number = '1'
929 event.patch_number = '2'
930
931 a = source.getChange(event)
932 mgr = tenant.layout.pipelines['gate'].manager
James E. Blairc0dedf82014-08-06 09:37:52 -0700933 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700934
James E. Blair8b5408c2016-08-08 15:37:46 -0700935 A.addApproval('code-review', 2)
Paul Belanger9bba4902016-11-02 16:07:33 -0400936 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700937 self.assertFalse(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair8c803f82012-07-31 16:25:42 -0700938
James E. Blair8b5408c2016-08-08 15:37:46 -0700939 A.addApproval('approved', 1)
Paul Belanger9bba4902016-11-02 16:07:33 -0400940 a = source.getChange(event, refresh=True)
James E. Blairc0dedf82014-08-06 09:37:52 -0700941 self.assertTrue(source.canMerge(a, mgr.getSubmitAllowNeeds()))
James E. Blair4886cc12012-07-18 15:39:41 -0700942
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700943 def test_project_merge_conflict(self):
944 "Test that gate merge conflicts are handled properly"
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700945
946 self.gearman_server.hold_jobs_in_queue = True
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700947 A = self.fake_gerrit.addFakeChange('org/project',
948 'master', 'A',
949 files={'conflict': 'foo'})
950 B = self.fake_gerrit.addFakeChange('org/project',
951 'master', 'B',
952 files={'conflict': 'bar'})
953 C = self.fake_gerrit.addFakeChange('org/project',
James E. Blair6736beb2013-07-11 15:18:15 -0700954 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -0700955 A.addApproval('code-review', 2)
956 B.addApproval('code-review', 2)
957 C.addApproval('code-review', 2)
958 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
959 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
960 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair973721f2012-08-15 10:19:43 -0700961 self.waitUntilSettled()
962
James E. Blair6736beb2013-07-11 15:18:15 -0700963 self.assertEqual(A.reported, 1)
James E. Blair6736beb2013-07-11 15:18:15 -0700964 self.assertEqual(C.reported, 1)
965
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700966 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700967 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700968 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700969 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700970 self.gearman_server.release('project-merge')
James E. Blair973721f2012-08-15 10:19:43 -0700971 self.waitUntilSettled()
James E. Blair972e3c72013-08-29 12:04:55 -0700972
James E. Blair1f4c2bb2013-04-26 08:40:46 -0700973 self.gearman_server.hold_jobs_in_queue = False
974 self.gearman_server.release()
James E. Blair973721f2012-08-15 10:19:43 -0700975 self.waitUntilSettled()
976
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400977 self.assertEqual(A.data['status'], 'MERGED')
978 self.assertEqual(B.data['status'], 'NEW')
979 self.assertEqual(C.data['status'], 'MERGED')
980 self.assertEqual(A.reported, 2)
Tobias Henkel9842bd72017-05-16 13:40:03 +0200981 self.assertIn('Merge Failed', B.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -0400982 self.assertEqual(C.reported, 2)
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -0700983
984 self.assertHistory([
985 dict(name='project-merge', result='SUCCESS', changes='1,1'),
986 dict(name='project-test1', result='SUCCESS', changes='1,1'),
987 dict(name='project-test2', result='SUCCESS', changes='1,1'),
988 dict(name='project-merge', result='SUCCESS', changes='1,1 3,1'),
989 dict(name='project-test1', result='SUCCESS', changes='1,1 3,1'),
990 dict(name='project-test2', result='SUCCESS', changes='1,1 3,1'),
991 ], ordered=False)
992
993 def test_delayed_merge_conflict(self):
994 "Test that delayed check merge conflicts are handled properly"
995
996 # Hold jobs in the gearman queue so that we can test whether
James E. Blair1960d682017-04-28 15:44:14 -0700997 # the executor sucesfully merges a change based on an old
998 # repo state (frozen by the scheduler) which would otherwise
999 # conflict.
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001000 self.gearman_server.hold_jobs_in_queue = True
1001 A = self.fake_gerrit.addFakeChange('org/project',
1002 'master', 'A',
1003 files={'conflict': 'foo'})
1004 B = self.fake_gerrit.addFakeChange('org/project',
1005 'master', 'B',
1006 files={'conflict': 'bar'})
1007 C = self.fake_gerrit.addFakeChange('org/project',
1008 'master', 'C')
1009 C.setDependsOn(B, 1)
1010
1011 # A enters the gate queue; B and C enter the check queue
1012 A.addApproval('code-review', 2)
1013 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1014 self.waitUntilSettled()
1015 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
James E. Blair8d144dc2017-05-05 10:13:45 -07001016 self.waitUntilSettled()
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001017 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1018 self.waitUntilSettled()
1019
1020 self.assertEqual(A.reported, 1)
1021 self.assertEqual(B.reported, 0) # Check does not report start
1022 self.assertEqual(C.reported, 0) # Check does not report start
1023
1024 # A merges while B and C are queued in check
1025 # Release A project-merge
1026 queue = self.gearman_server.getQueue()
1027 self.release(queue[0])
1028 self.waitUntilSettled()
1029
1030 # Release A project-test*
1031 # gate has higher precedence, so A's test jobs are added in
1032 # front of the merge jobs for B and C
1033 queue = self.gearman_server.getQueue()
1034 self.release(queue[0])
1035 self.release(queue[1])
1036 self.waitUntilSettled()
1037
1038 self.assertEqual(A.data['status'], 'MERGED')
1039 self.assertEqual(B.data['status'], 'NEW')
1040 self.assertEqual(C.data['status'], 'NEW')
1041 self.assertEqual(A.reported, 2)
1042 self.assertEqual(B.reported, 0)
1043 self.assertEqual(C.reported, 0)
1044 self.assertHistory([
1045 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1046 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1047 dict(name='project-test2', result='SUCCESS', changes='1,1'),
1048 ], ordered=False)
1049
1050 # B and C report merge conflicts
1051 # Release B project-merge
1052 queue = self.gearman_server.getQueue()
1053 self.release(queue[0])
1054 self.waitUntilSettled()
1055
1056 # Release C
1057 self.gearman_server.hold_jobs_in_queue = False
1058 self.gearman_server.release()
1059 self.waitUntilSettled()
1060
1061 self.assertEqual(A.data['status'], 'MERGED')
1062 self.assertEqual(B.data['status'], 'NEW')
1063 self.assertEqual(C.data['status'], 'NEW')
1064 self.assertEqual(A.reported, 2)
1065 self.assertEqual(B.reported, 1)
1066 self.assertEqual(C.reported, 1)
1067
1068 self.assertHistory([
1069 dict(name='project-merge', result='SUCCESS', changes='1,1'),
1070 dict(name='project-test1', result='SUCCESS', changes='1,1'),
1071 dict(name='project-test2', result='SUCCESS', changes='1,1'),
James E. Blair1960d682017-04-28 15:44:14 -07001072 dict(name='project-merge', result='SUCCESS', changes='2,1'),
1073 dict(name='project-test1', result='SUCCESS', changes='2,1'),
1074 dict(name='project-test2', result='SUCCESS', changes='2,1'),
1075 dict(name='project-merge', result='SUCCESS', changes='2,1 3,1'),
1076 dict(name='project-test1', result='SUCCESS', changes='2,1 3,1'),
1077 dict(name='project-test2', result='SUCCESS', changes='2,1 3,1'),
K Jonathan Harkerae04e4c2017-03-15 19:07:11 -07001078 ], ordered=False)
James E. Blair6736beb2013-07-11 15:18:15 -07001079
James E. Blairdaabed22012-08-15 15:38:57 -07001080 def test_post(self):
1081 "Test that post jobs run"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001082
Zhongyue Luo5d556072012-09-21 02:00:47 +09001083 e = {
1084 "type": "ref-updated",
1085 "submitter": {
1086 "name": "User Name",
1087 },
1088 "refUpdate": {
1089 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1090 "newRev": "d479a0bfcb34da57a31adb2a595c0cf687812543",
1091 "refName": "master",
1092 "project": "org/project",
1093 }
1094 }
James E. Blairdaabed22012-08-15 15:38:57 -07001095 self.fake_gerrit.addEvent(e)
1096 self.waitUntilSettled()
1097
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001098 job_names = [x.name for x in self.history]
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001099 self.assertEqual(len(self.history), 1)
1100 self.assertIn('project-post', job_names)
James E. Blairc6294a52012-08-17 10:19:48 -07001101
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001102 def test_post_ignore_deletes(self):
1103 "Test that deleting refs does not trigger post jobs"
1104
1105 e = {
1106 "type": "ref-updated",
1107 "submitter": {
1108 "name": "User Name",
1109 },
1110 "refUpdate": {
1111 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1112 "newRev": "0000000000000000000000000000000000000000",
1113 "refName": "master",
1114 "project": "org/project",
1115 }
1116 }
1117 self.fake_gerrit.addEvent(e)
1118 self.waitUntilSettled()
1119
1120 job_names = [x.name for x in self.history]
1121 self.assertEqual(len(self.history), 0)
1122 self.assertNotIn('project-post', job_names)
1123
James E. Blaircc324192017-04-19 14:36:08 -07001124 @simple_layout('layouts/dont-ignore-ref-deletes.yaml')
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001125 def test_post_ignore_deletes_negative(self):
1126 "Test that deleting refs does trigger post jobs"
K Jonathan Harkerf95e7232015-04-29 13:33:16 -07001127 e = {
1128 "type": "ref-updated",
1129 "submitter": {
1130 "name": "User Name",
1131 },
1132 "refUpdate": {
1133 "oldRev": "90f173846e3af9154517b88543ffbd1691f31366",
1134 "newRev": "0000000000000000000000000000000000000000",
1135 "refName": "master",
1136 "project": "org/project",
1137 }
1138 }
1139 self.fake_gerrit.addEvent(e)
1140 self.waitUntilSettled()
1141
1142 job_names = [x.name for x in self.history]
1143 self.assertEqual(len(self.history), 1)
1144 self.assertIn('project-post', job_names)
1145
James E. Blairec056492016-07-22 09:45:56 -07001146 @skip("Disabled for early v3 development")
James E. Blairc6294a52012-08-17 10:19:48 -07001147 def test_build_configuration_branch_interaction(self):
1148 "Test that switching between branches works"
1149 self.test_build_configuration()
1150 self.test_build_configuration_branch()
1151 # C has been merged, undo that
Monty Taylorbc758832013-06-17 17:22:42 -04001152 path = os.path.join(self.upstream_root, "org/project")
James E. Blairc6294a52012-08-17 10:19:48 -07001153 repo = git.Repo(path)
1154 repo.heads.master.commit = repo.commit('init')
1155 self.test_build_configuration()
1156
James E. Blaircaec0c52012-08-22 14:52:22 -07001157 def test_dependent_changes_dequeue(self):
1158 "Test that dependent patches are not needlessly tested"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001159
James E. Blaircaec0c52012-08-22 14:52:22 -07001160 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1161 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1162 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001163 A.addApproval('code-review', 2)
1164 B.addApproval('code-review', 2)
1165 C.addApproval('code-review', 2)
James E. Blaircaec0c52012-08-22 14:52:22 -07001166
1167 M1 = self.fake_gerrit.addFakeChange('org/project', 'master', 'M1')
1168 M1.setMerged()
1169
1170 # C -> B -> A -> M1
1171
1172 C.setDependsOn(B, 1)
1173 B.setDependsOn(A, 1)
1174 A.setDependsOn(M1, 1)
1175
Paul Belanger174a8272017-03-14 13:20:10 -04001176 self.executor_server.failJob('project-merge', A)
James E. Blaircaec0c52012-08-22 14:52:22 -07001177
James E. Blair8b5408c2016-08-08 15:37:46 -07001178 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1179 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1180 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircaec0c52012-08-22 14:52:22 -07001181
1182 self.waitUntilSettled()
1183
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001184 self.assertEqual(A.data['status'], 'NEW')
1185 self.assertEqual(A.reported, 2)
1186 self.assertEqual(B.data['status'], 'NEW')
1187 self.assertEqual(B.reported, 2)
1188 self.assertEqual(C.data['status'], 'NEW')
James E. Blair0d3e83b2017-06-05 13:51:57 -07001189 self.assertIn('This change depends on a change that failed to merge.',
1190 C.messages[-1])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001191 self.assertEqual(len(self.history), 1)
James E. Blairec590122012-08-22 15:19:31 -07001192
James E. Blair972e3c72013-08-29 12:04:55 -07001193 def test_failing_dependent_changes(self):
1194 "Test that failing dependent patches are taken out of stream"
Paul Belanger174a8272017-03-14 13:20:10 -04001195 self.executor_server.hold_jobs_in_build = True
James E. Blair972e3c72013-08-29 12:04:55 -07001196 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1197 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1198 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1199 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
1200 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
James E. Blair8b5408c2016-08-08 15:37:46 -07001201 A.addApproval('code-review', 2)
1202 B.addApproval('code-review', 2)
1203 C.addApproval('code-review', 2)
1204 D.addApproval('code-review', 2)
1205 E.addApproval('code-review', 2)
James E. Blair972e3c72013-08-29 12:04:55 -07001206
1207 # E, D -> C -> B, A
1208
1209 D.setDependsOn(C, 1)
1210 C.setDependsOn(B, 1)
1211
Paul Belanger174a8272017-03-14 13:20:10 -04001212 self.executor_server.failJob('project-test1', B)
James E. Blair972e3c72013-08-29 12:04:55 -07001213
James E. Blair8b5408c2016-08-08 15:37:46 -07001214 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1215 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
1216 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1217 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1218 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair972e3c72013-08-29 12:04:55 -07001219
1220 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001221 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001222 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001223 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001224 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001225 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001226 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001227 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001228 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001229 self.executor_server.release('.*-merge')
James E. Blair972e3c72013-08-29 12:04:55 -07001230 self.waitUntilSettled()
1231
Paul Belanger174a8272017-03-14 13:20:10 -04001232 self.executor_server.hold_jobs_in_build = False
James E. Blair972e3c72013-08-29 12:04:55 -07001233 for build in self.builds:
1234 if build.parameters['ZUUL_CHANGE'] != '1':
1235 build.release()
1236 self.waitUntilSettled()
1237
Paul Belanger174a8272017-03-14 13:20:10 -04001238 self.executor_server.release()
James E. Blair972e3c72013-08-29 12:04:55 -07001239 self.waitUntilSettled()
1240
1241 self.assertEqual(A.data['status'], 'MERGED')
1242 self.assertEqual(A.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001243 self.assertIn('Build succeeded', A.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001244 self.assertEqual(B.data['status'], 'NEW')
1245 self.assertEqual(B.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001246 self.assertIn('Build failed', B.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001247 self.assertEqual(C.data['status'], 'NEW')
1248 self.assertEqual(C.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001249 self.assertIn('depends on a change', C.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001250 self.assertEqual(D.data['status'], 'NEW')
1251 self.assertEqual(D.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001252 self.assertIn('depends on a change', D.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001253 self.assertEqual(E.data['status'], 'MERGED')
1254 self.assertEqual(E.reported, 2)
Jeremy Stanley10837132014-08-02 16:10:56 +00001255 self.assertIn('Build succeeded', E.messages[1])
James E. Blair972e3c72013-08-29 12:04:55 -07001256 self.assertEqual(len(self.history), 18)
1257
James E. Blairec590122012-08-22 15:19:31 -07001258 def test_head_is_dequeued_once(self):
James E. Blair2fa50962013-01-30 21:50:41 -08001259 "Test that if a change at the head fails it is dequeued only once"
James E. Blairec590122012-08-22 15:19:31 -07001260 # If it's dequeued more than once, we should see extra
1261 # aborted jobs.
James E. Blairec590122012-08-22 15:19:31 -07001262
Paul Belanger174a8272017-03-14 13:20:10 -04001263 self.executor_server.hold_jobs_in_build = True
James E. Blair6df67132017-04-19 13:53:20 -07001264 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1265 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1266 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001267 A.addApproval('code-review', 2)
1268 B.addApproval('code-review', 2)
1269 C.addApproval('code-review', 2)
James E. Blairec590122012-08-22 15:19:31 -07001270
Paul Belanger174a8272017-03-14 13:20:10 -04001271 self.executor_server.failJob('project-test1', A)
1272 self.executor_server.failJob('project-test2', A)
James E. Blairec590122012-08-22 15:19:31 -07001273
James E. Blair8b5408c2016-08-08 15:37:46 -07001274 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1275 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1276 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blairec590122012-08-22 15:19:31 -07001277
1278 self.waitUntilSettled()
James E. Blairec590122012-08-22 15:19:31 -07001279
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001280 self.assertEqual(len(self.builds), 1)
Paul Belanger58bf6912016-11-11 19:36:01 -05001281 self.assertEqual(self.builds[0].name, 'project-merge')
1282 self.assertTrue(self.builds[0].hasChanges(A))
James E. Blairec590122012-08-22 15:19:31 -07001283
Paul Belanger174a8272017-03-14 13:20:10 -04001284 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001285 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001286 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001287 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001288 self.executor_server.release('.*-merge')
James E. Blairec590122012-08-22 15:19:31 -07001289 self.waitUntilSettled()
1290
James E. Blair6df67132017-04-19 13:53:20 -07001291 self.assertEqual(len(self.builds), 6)
Paul Belanger58bf6912016-11-11 19:36:01 -05001292 self.assertEqual(self.builds[0].name, 'project-test1')
1293 self.assertEqual(self.builds[1].name, 'project-test2')
James E. Blair6df67132017-04-19 13:53:20 -07001294 self.assertEqual(self.builds[2].name, 'project-test1')
1295 self.assertEqual(self.builds[3].name, 'project-test2')
1296 self.assertEqual(self.builds[4].name, 'project-test1')
1297 self.assertEqual(self.builds[5].name, 'project-test2')
James E. Blairec590122012-08-22 15:19:31 -07001298
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001299 self.release(self.builds[0])
James E. Blairec590122012-08-22 15:19:31 -07001300 self.waitUntilSettled()
1301
James E. Blair6df67132017-04-19 13:53:20 -07001302 self.assertEqual(len(self.builds), 2) # test2, merge for B
1303 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 4)
James E. Blairec590122012-08-22 15:19:31 -07001304
Paul Belanger174a8272017-03-14 13:20:10 -04001305 self.executor_server.hold_jobs_in_build = False
1306 self.executor_server.release()
James E. Blairec590122012-08-22 15:19:31 -07001307 self.waitUntilSettled()
1308
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001309 self.assertEqual(len(self.builds), 0)
James E. Blair6df67132017-04-19 13:53:20 -07001310 self.assertEqual(len(self.history), 15)
James E. Blaircaec0c52012-08-22 14:52:22 -07001311
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001312 self.assertEqual(A.data['status'], 'NEW')
1313 self.assertEqual(B.data['status'], 'MERGED')
1314 self.assertEqual(C.data['status'], 'MERGED')
1315 self.assertEqual(A.reported, 2)
1316 self.assertEqual(B.reported, 2)
1317 self.assertEqual(C.reported, 2)
James E. Blair4ec821f2012-08-23 15:28:28 -07001318
James E. Blaircc020532017-04-19 13:04:44 -07001319 @simple_layout('layouts/nonvoting-job.yaml')
James E. Blair4ec821f2012-08-23 15:28:28 -07001320 def test_nonvoting_job(self):
1321 "Test that non-voting jobs don't vote."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001322
James E. Blair4ec821f2012-08-23 15:28:28 -07001323 A = self.fake_gerrit.addFakeChange('org/nonvoting-project',
1324 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001325 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04001326 self.executor_server.failJob('nonvoting-project-test2', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07001327 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4ec821f2012-08-23 15:28:28 -07001328
1329 self.waitUntilSettled()
James E. Blair4ec821f2012-08-23 15:28:28 -07001330
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001331 self.assertEqual(A.data['status'], 'MERGED')
1332 self.assertEqual(A.reported, 2)
1333 self.assertEqual(
1334 self.getJobFromHistory('nonvoting-project-merge').result,
1335 'SUCCESS')
1336 self.assertEqual(
1337 self.getJobFromHistory('nonvoting-project-test1').result,
1338 'SUCCESS')
1339 self.assertEqual(
1340 self.getJobFromHistory('nonvoting-project-test2').result,
1341 'FAILURE')
James E. Blaire0487072012-08-29 17:38:31 -07001342
James E. Blair5821bd92015-09-16 08:48:15 -07001343 for build in self.builds:
1344 self.assertEqual(build.parameters['ZUUL_VOTING'], '0')
1345
James E. Blaire0487072012-08-29 17:38:31 -07001346 def test_check_queue_success(self):
1347 "Test successful check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001348
James E. Blaire0487072012-08-29 17:38:31 -07001349 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1350 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1351
1352 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001353
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001354 self.assertEqual(A.data['status'], 'NEW')
1355 self.assertEqual(A.reported, 1)
1356 self.assertEqual(self.getJobFromHistory('project-merge').result,
1357 'SUCCESS')
1358 self.assertEqual(self.getJobFromHistory('project-test1').result,
1359 'SUCCESS')
1360 self.assertEqual(self.getJobFromHistory('project-test2').result,
1361 'SUCCESS')
James E. Blaire0487072012-08-29 17:38:31 -07001362
1363 def test_check_queue_failure(self):
1364 "Test failed check queue jobs."
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001365
James E. Blaire0487072012-08-29 17:38:31 -07001366 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger174a8272017-03-14 13:20:10 -04001367 self.executor_server.failJob('project-test2', A)
James E. Blaire0487072012-08-29 17:38:31 -07001368 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1369
1370 self.waitUntilSettled()
James E. Blaire0487072012-08-29 17:38:31 -07001371
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001372 self.assertEqual(A.data['status'], 'NEW')
1373 self.assertEqual(A.reported, 1)
1374 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blair78e31b32013-07-09 09:11:34 -07001375 'SUCCESS')
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001376 self.assertEqual(self.getJobFromHistory('project-test1').result,
1377 'SUCCESS')
1378 self.assertEqual(self.getJobFromHistory('project-test2').result,
1379 'FAILURE')
James E. Blair127bc182012-08-28 15:55:15 -07001380
James E. Blair7cb84542017-04-19 13:35:05 -07001381 @simple_layout('layouts/three-projects.yaml')
James E. Blair127bc182012-08-28 15:55:15 -07001382 def test_dependent_behind_dequeue(self):
Clint Byruma9626572017-02-22 14:04:00 -05001383 # This particular test does a large amount of merges and needs a little
1384 # more time to complete
James E. Blair267e5162017-04-07 10:08:20 -07001385 self.wait_timeout = 120
James E. Blair127bc182012-08-28 15:55:15 -07001386 "test that dependent changes behind dequeued changes work"
1387 # This complicated test is a reproduction of a real life bug
1388 self.sched.reconfigure(self.config)
James E. Blair127bc182012-08-28 15:55:15 -07001389
Paul Belanger174a8272017-03-14 13:20:10 -04001390 self.executor_server.hold_jobs_in_build = True
James E. Blair127bc182012-08-28 15:55:15 -07001391 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1392 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1393 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1394 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
1395 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
1396 F = self.fake_gerrit.addFakeChange('org/project3', 'master', 'F')
1397 D.setDependsOn(C, 1)
1398 E.setDependsOn(D, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07001399 A.addApproval('code-review', 2)
1400 B.addApproval('code-review', 2)
1401 C.addApproval('code-review', 2)
1402 D.addApproval('code-review', 2)
1403 E.addApproval('code-review', 2)
1404 F.addApproval('code-review', 2)
James E. Blair127bc182012-08-28 15:55:15 -07001405
1406 A.fail_merge = True
James E. Blair127bc182012-08-28 15:55:15 -07001407
1408 # Change object re-use in the gerrit trigger is hidden if
1409 # changes are added in quick succession; waiting makes it more
1410 # like real life.
James E. Blair8b5408c2016-08-08 15:37:46 -07001411 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001412 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001413 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001414 self.waitUntilSettled()
1415
Paul Belanger174a8272017-03-14 13:20:10 -04001416 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001417 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001418 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001419 self.waitUntilSettled()
1420
James E. Blair8b5408c2016-08-08 15:37:46 -07001421 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001422 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001423 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001424 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001425 self.fake_gerrit.addEvent(E.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001426 self.waitUntilSettled()
James E. Blair8b5408c2016-08-08 15:37:46 -07001427 self.fake_gerrit.addEvent(F.addApproval('approved', 1))
James E. Blair127bc182012-08-28 15:55:15 -07001428 self.waitUntilSettled()
1429
Paul Belanger174a8272017-03-14 13:20:10 -04001430 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001431 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001432 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001433 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001434 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001435 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001436 self.executor_server.release('.*-merge')
James E. Blair127bc182012-08-28 15:55:15 -07001437 self.waitUntilSettled()
1438
1439 # all jobs running
James E. Blaire955e062012-10-08 09:49:03 -07001440
1441 # Grab pointers to the jobs we want to release before
1442 # releasing any, because list indexes may change as
1443 # the jobs complete.
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001444 a, b, c = self.builds[:3]
James E. Blaire955e062012-10-08 09:49:03 -07001445 a.release()
1446 b.release()
1447 c.release()
James E. Blair127bc182012-08-28 15:55:15 -07001448 self.waitUntilSettled()
1449
Paul Belanger174a8272017-03-14 13:20:10 -04001450 self.executor_server.hold_jobs_in_build = False
1451 self.executor_server.release()
James E. Blair127bc182012-08-28 15:55:15 -07001452 self.waitUntilSettled()
1453
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001454 self.assertEqual(A.data['status'], 'NEW')
1455 self.assertEqual(B.data['status'], 'MERGED')
1456 self.assertEqual(C.data['status'], 'MERGED')
1457 self.assertEqual(D.data['status'], 'MERGED')
1458 self.assertEqual(E.data['status'], 'MERGED')
1459 self.assertEqual(F.data['status'], 'MERGED')
James E. Blair127bc182012-08-28 15:55:15 -07001460
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001461 self.assertEqual(A.reported, 2)
1462 self.assertEqual(B.reported, 2)
1463 self.assertEqual(C.reported, 2)
1464 self.assertEqual(D.reported, 2)
1465 self.assertEqual(E.reported, 2)
1466 self.assertEqual(F.reported, 2)
James E. Blair127bc182012-08-28 15:55:15 -07001467
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001468 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 15)
1469 self.assertEqual(len(self.history), 44)
James E. Blair05fed602012-09-07 12:45:24 -07001470
1471 def test_merger_repack(self):
1472 "Test that the merger works after a repack"
James E. Blair1f4c2bb2013-04-26 08:40:46 -07001473
James E. Blair05fed602012-09-07 12:45:24 -07001474 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001475 A.addApproval('code-review', 2)
1476 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001477 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001478 self.assertEqual(self.getJobFromHistory('project-merge').result,
1479 'SUCCESS')
1480 self.assertEqual(self.getJobFromHistory('project-test1').result,
1481 'SUCCESS')
1482 self.assertEqual(self.getJobFromHistory('project-test2').result,
1483 'SUCCESS')
1484 self.assertEqual(A.data['status'], 'MERGED')
1485 self.assertEqual(A.reported, 2)
James E. Blair05fed602012-09-07 12:45:24 -07001486 self.assertEmptyQueues()
Paul Belanger5bfe9e72016-11-04 19:20:04 -04001487 self.build_history = []
James E. Blair05fed602012-09-07 12:45:24 -07001488
James E. Blair2a535672017-04-27 12:03:15 -07001489 path = os.path.join(self.merger_src_root, "review.example.com",
1490 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001491 if os.path.exists(path):
1492 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001493 path = os.path.join(self.executor_src_root, "review.example.com",
1494 "org/project")
James E. Blair8c1be532017-02-07 14:04:12 -08001495 if os.path.exists(path):
1496 repack_repo(path)
James E. Blair05fed602012-09-07 12:45:24 -07001497
1498 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001499 A.addApproval('code-review', 2)
1500 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair05fed602012-09-07 12:45:24 -07001501 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001502 self.assertEqual(self.getJobFromHistory('project-merge').result,
1503 'SUCCESS')
1504 self.assertEqual(self.getJobFromHistory('project-test1').result,
1505 'SUCCESS')
1506 self.assertEqual(self.getJobFromHistory('project-test2').result,
1507 'SUCCESS')
1508 self.assertEqual(A.data['status'], 'MERGED')
1509 self.assertEqual(A.reported, 2)
James E. Blair7ee88a22012-09-12 18:59:31 +02001510
James E. Blair4886f282012-11-15 09:27:33 -08001511 def test_merger_repack_large_change(self):
1512 "Test that the merger works with large changes after a repack"
Paul Belanger174a8272017-03-14 13:20:10 -04001513 # https://bugs.executepad.net/zuul/+bug/1078946
James E. Blairac2c3242014-01-24 13:38:51 -08001514 # This test assumes the repo is already cloned; make sure it is
Paul Belanger503bded2016-11-11 19:43:38 -05001515 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6df67132017-04-19 13:53:20 -07001516 trusted, project = tenant.getProject('org/project')
James E. Blair0ffa0102017-03-30 13:11:33 -07001517 url = self.fake_gerrit.getGitUrl(project)
James E. Blair8d144dc2017-05-05 10:13:45 -07001518 self.executor_server.merger._addProject('review.example.com',
James E. Blair197e8202017-06-09 12:54:28 -07001519 'org/project', url, None)
James E. Blair6df67132017-04-19 13:53:20 -07001520 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair4886f282012-11-15 09:27:33 -08001521 A.addPatchset(large=True)
James E. Blair2a535672017-04-27 12:03:15 -07001522 # TODOv3(jeblair): add hostname to upstream root
1523 path = os.path.join(self.upstream_root, 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001524 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001525 path = os.path.join(self.merger_src_root, 'review.example.com',
1526 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001527 if os.path.exists(path):
1528 repack_repo(path)
James E. Blair2a535672017-04-27 12:03:15 -07001529 path = os.path.join(self.executor_src_root, 'review.example.com',
1530 'org/project')
James E. Blair8c1be532017-02-07 14:04:12 -08001531 if os.path.exists(path):
1532 repack_repo(path)
James E. Blair4886f282012-11-15 09:27:33 -08001533
James E. Blair8b5408c2016-08-08 15:37:46 -07001534 A.addApproval('code-review', 2)
1535 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4886f282012-11-15 09:27:33 -08001536 self.waitUntilSettled()
Paul Belanger503bded2016-11-11 19:43:38 -05001537 self.assertEqual(self.getJobFromHistory('project-merge').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001538 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001539 self.assertEqual(self.getJobFromHistory('project-test1').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001540 'SUCCESS')
Paul Belanger503bded2016-11-11 19:43:38 -05001541 self.assertEqual(self.getJobFromHistory('project-test2').result,
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001542 'SUCCESS')
1543 self.assertEqual(A.data['status'], 'MERGED')
1544 self.assertEqual(A.reported, 2)
James E. Blair4886f282012-11-15 09:27:33 -08001545
James E. Blair2fa50962013-01-30 21:50:41 -08001546 def test_new_patchset_dequeues_old(self):
1547 "Test that a new patchset causes the old to be dequeued"
1548 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001549 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001550 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1551 M.setMerged()
1552
1553 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1554 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1555 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1556 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001557 A.addApproval('code-review', 2)
1558 B.addApproval('code-review', 2)
1559 C.addApproval('code-review', 2)
1560 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001561
1562 C.setDependsOn(B, 1)
1563 B.setDependsOn(A, 1)
1564 A.setDependsOn(M, 1)
1565
James E. Blair8b5408c2016-08-08 15:37:46 -07001566 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1567 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1568 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1569 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001570 self.waitUntilSettled()
1571
1572 B.addPatchset()
1573 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1574 self.waitUntilSettled()
1575
Paul Belanger174a8272017-03-14 13:20:10 -04001576 self.executor_server.hold_jobs_in_build = False
1577 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001578 self.waitUntilSettled()
1579
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001580 self.assertEqual(A.data['status'], 'MERGED')
1581 self.assertEqual(A.reported, 2)
1582 self.assertEqual(B.data['status'], 'NEW')
1583 self.assertEqual(B.reported, 2)
1584 self.assertEqual(C.data['status'], 'NEW')
1585 self.assertEqual(C.reported, 2)
1586 self.assertEqual(D.data['status'], 'MERGED')
1587 self.assertEqual(D.reported, 2)
1588 self.assertEqual(len(self.history), 9) # 3 each for A, B, D.
James E. Blair2fa50962013-01-30 21:50:41 -08001589
James E. Blairba437362015-02-07 11:41:52 -08001590 def test_new_patchset_check(self):
1591 "Test a new patchset in check"
Antoine Mussobd86a312014-01-08 14:51:33 +01001592
Paul Belanger174a8272017-03-14 13:20:10 -04001593 self.executor_server.hold_jobs_in_build = True
Antoine Mussobd86a312014-01-08 14:51:33 +01001594
1595 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blairba437362015-02-07 11:41:52 -08001596 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001597 tenant = self.sched.abide.tenants.get('tenant-one')
1598 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001599
1600 # Add two git-dependent changes
1601 B.setDependsOn(A, 1)
1602 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1603 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001604 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1605 self.waitUntilSettled()
James E. Blairba437362015-02-07 11:41:52 -08001606
1607 # A live item, and a non-live/live pair
1608 items = check_pipeline.getAllItems()
1609 self.assertEqual(len(items), 3)
1610
1611 self.assertEqual(items[0].change.number, '1')
1612 self.assertEqual(items[0].change.patchset, '1')
1613 self.assertFalse(items[0].live)
1614
1615 self.assertEqual(items[1].change.number, '2')
1616 self.assertEqual(items[1].change.patchset, '1')
1617 self.assertTrue(items[1].live)
1618
1619 self.assertEqual(items[2].change.number, '1')
1620 self.assertEqual(items[2].change.patchset, '1')
1621 self.assertTrue(items[2].live)
1622
1623 # Add a new patchset to A
1624 A.addPatchset()
1625 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1626 self.waitUntilSettled()
1627
1628 # The live copy of A,1 should be gone, but the non-live and B
1629 # should continue, and we should have a new A,2
1630 items = check_pipeline.getAllItems()
1631 self.assertEqual(len(items), 3)
1632
1633 self.assertEqual(items[0].change.number, '1')
1634 self.assertEqual(items[0].change.patchset, '1')
1635 self.assertFalse(items[0].live)
1636
1637 self.assertEqual(items[1].change.number, '2')
1638 self.assertEqual(items[1].change.patchset, '1')
1639 self.assertTrue(items[1].live)
1640
1641 self.assertEqual(items[2].change.number, '1')
1642 self.assertEqual(items[2].change.patchset, '2')
1643 self.assertTrue(items[2].live)
1644
1645 # Add a new patchset to B
1646 B.addPatchset()
1647 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1648 self.waitUntilSettled()
1649
1650 # The live copy of B,1 should be gone, and it's non-live copy of A,1
1651 # but we should have a new B,2 (still based on A,1)
1652 items = check_pipeline.getAllItems()
1653 self.assertEqual(len(items), 3)
1654
1655 self.assertEqual(items[0].change.number, '1')
1656 self.assertEqual(items[0].change.patchset, '2')
1657 self.assertTrue(items[0].live)
1658
1659 self.assertEqual(items[1].change.number, '1')
1660 self.assertEqual(items[1].change.patchset, '1')
1661 self.assertFalse(items[1].live)
1662
1663 self.assertEqual(items[2].change.number, '2')
1664 self.assertEqual(items[2].change.patchset, '2')
1665 self.assertTrue(items[2].live)
1666
1667 self.builds[0].release()
1668 self.waitUntilSettled()
1669 self.builds[0].release()
1670 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001671 self.executor_server.hold_jobs_in_build = False
1672 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001673 self.waitUntilSettled()
1674
1675 self.assertEqual(A.reported, 1)
1676 self.assertEqual(B.reported, 1)
1677 self.assertEqual(self.history[0].result, 'ABORTED')
1678 self.assertEqual(self.history[0].changes, '1,1')
1679 self.assertEqual(self.history[1].result, 'ABORTED')
1680 self.assertEqual(self.history[1].changes, '1,1 2,1')
1681 self.assertEqual(self.history[2].result, 'SUCCESS')
1682 self.assertEqual(self.history[2].changes, '1,2')
1683 self.assertEqual(self.history[3].result, 'SUCCESS')
1684 self.assertEqual(self.history[3].changes, '1,1 2,2')
1685
1686 def test_abandoned_gate(self):
1687 "Test that an abandoned change is dequeued from gate"
1688
Paul Belanger174a8272017-03-14 13:20:10 -04001689 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001690
1691 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001692 A.addApproval('code-review', 2)
1693 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairba437362015-02-07 11:41:52 -08001694 self.waitUntilSettled()
Antoine Mussobd86a312014-01-08 14:51:33 +01001695 self.assertEqual(len(self.builds), 1, "One job being built (on hold)")
1696 self.assertEqual(self.builds[0].name, 'project-merge')
1697
1698 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1699 self.waitUntilSettled()
1700
Paul Belanger174a8272017-03-14 13:20:10 -04001701 self.executor_server.release('.*-merge')
Antoine Mussobd86a312014-01-08 14:51:33 +01001702 self.waitUntilSettled()
1703
Arie5b4048c2016-10-05 16:12:06 +03001704 self.assertBuilds([])
1705 self.assertHistory([
1706 dict(name='project-merge', result='ABORTED', changes='1,1')],
1707 ordered=False)
James E. Blairba437362015-02-07 11:41:52 -08001708 self.assertEqual(A.reported, 1,
1709 "Abandoned gate change should report only start")
1710
1711 def test_abandoned_check(self):
1712 "Test that an abandoned change is dequeued from check"
1713
Paul Belanger174a8272017-03-14 13:20:10 -04001714 self.executor_server.hold_jobs_in_build = True
James E. Blairba437362015-02-07 11:41:52 -08001715
1716 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1717 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger18e5f632016-11-03 15:24:16 -04001718 tenant = self.sched.abide.tenants.get('tenant-one')
1719 check_pipeline = tenant.layout.pipelines['check']
James E. Blairba437362015-02-07 11:41:52 -08001720
1721 # Add two git-dependent changes
1722 B.setDependsOn(A, 1)
1723 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1724 self.waitUntilSettled()
1725 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1726 self.waitUntilSettled()
1727 # A live item, and a non-live/live pair
1728 items = check_pipeline.getAllItems()
1729 self.assertEqual(len(items), 3)
1730
1731 self.assertEqual(items[0].change.number, '1')
1732 self.assertFalse(items[0].live)
1733
1734 self.assertEqual(items[1].change.number, '2')
1735 self.assertTrue(items[1].live)
1736
1737 self.assertEqual(items[2].change.number, '1')
1738 self.assertTrue(items[2].live)
1739
1740 # Abandon A
1741 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1742 self.waitUntilSettled()
1743
1744 # The live copy of A should be gone, but the non-live and B
1745 # should continue
1746 items = check_pipeline.getAllItems()
1747 self.assertEqual(len(items), 2)
1748
1749 self.assertEqual(items[0].change.number, '1')
1750 self.assertFalse(items[0].live)
1751
1752 self.assertEqual(items[1].change.number, '2')
1753 self.assertTrue(items[1].live)
1754
Paul Belanger174a8272017-03-14 13:20:10 -04001755 self.executor_server.hold_jobs_in_build = False
1756 self.executor_server.release()
James E. Blairba437362015-02-07 11:41:52 -08001757 self.waitUntilSettled()
1758
1759 self.assertEqual(len(self.history), 4)
1760 self.assertEqual(self.history[0].result, 'ABORTED',
Antoine Mussobd86a312014-01-08 14:51:33 +01001761 'Build should have been aborted')
1762 self.assertEqual(A.reported, 0, "Abandoned change should not report")
James E. Blairba437362015-02-07 11:41:52 -08001763 self.assertEqual(B.reported, 1, "Change should report")
Antoine Mussobd86a312014-01-08 14:51:33 +01001764
Steve Varnau7b78b312015-04-03 14:49:46 -07001765 def test_abandoned_not_timer(self):
1766 "Test that an abandoned change does not cancel timer jobs"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001767 # This test can not use simple_layout because it must start
1768 # with a configuration which does not include a
1769 # timer-triggered job so that we have an opportunity to set
1770 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04001771 self.executor_server.hold_jobs_in_build = True
Steve Varnau7b78b312015-04-03 14:49:46 -07001772 # Start timer trigger - also org/project
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001773 self.commitConfigUpdate('common-config', 'layouts/idle.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001774 self.sched.reconfigure(self.config)
Steve Varnau7b78b312015-04-03 14:49:46 -07001775 # The pipeline triggers every second, so we should have seen
1776 # several by now.
1777 time.sleep(5)
1778 self.waitUntilSettled()
1779 # Stop queuing timer triggered jobs so that the assertions
1780 # below don't race against more jobs being queued.
Clint Byrumead6c562017-02-01 16:34:04 -08001781 # Must be in same repo, so overwrite config with another one
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001782 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Steve Varnau7b78b312015-04-03 14:49:46 -07001783 self.sched.reconfigure(self.config)
James E. Blairdfdfcfc2017-04-20 10:19:20 -07001784
Steve Varnau7b78b312015-04-03 14:49:46 -07001785 self.assertEqual(len(self.builds), 2, "Two timer jobs")
1786
1787 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1788 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1789 self.waitUntilSettled()
1790 self.assertEqual(len(self.builds), 3, "One change plus two timer jobs")
1791
1792 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
1793 self.waitUntilSettled()
1794
1795 self.assertEqual(len(self.builds), 2, "Two timer jobs remain")
1796
Paul Belanger174a8272017-03-14 13:20:10 -04001797 self.executor_server.release()
Steve Varnau7b78b312015-04-03 14:49:46 -07001798 self.waitUntilSettled()
1799
Arx Cruzb1b010d2013-10-28 19:49:59 -02001800 def test_zuul_url_return(self):
1801 "Test if ZUUL_URL is returning when zuul_url is set in zuul.conf"
James E. Blair4076e2b2014-01-28 12:42:20 -08001802 self.assertTrue(self.sched.config.has_option('merger', 'zuul_url'))
Paul Belanger174a8272017-03-14 13:20:10 -04001803 self.executor_server.hold_jobs_in_build = True
Arx Cruzb1b010d2013-10-28 19:49:59 -02001804
1805 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001806 A.addApproval('code-review', 2)
1807 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Arx Cruzb1b010d2013-10-28 19:49:59 -02001808 self.waitUntilSettled()
1809
1810 self.assertEqual(len(self.builds), 1)
1811 for build in self.builds:
1812 self.assertTrue('ZUUL_URL' in build.parameters)
1813
Paul Belanger174a8272017-03-14 13:20:10 -04001814 self.executor_server.hold_jobs_in_build = False
1815 self.executor_server.release()
Arx Cruzb1b010d2013-10-28 19:49:59 -02001816 self.waitUntilSettled()
1817
James E. Blair2fa50962013-01-30 21:50:41 -08001818 def test_new_patchset_dequeues_old_on_head(self):
1819 "Test that a new patchset causes the old to be dequeued (at head)"
1820 # D -> C (depends on B) -> B (depends on A) -> A -> M
Paul Belanger174a8272017-03-14 13:20:10 -04001821 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001822 M = self.fake_gerrit.addFakeChange('org/project', 'master', 'M')
1823 M.setMerged()
1824 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1825 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1826 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1827 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001828 A.addApproval('code-review', 2)
1829 B.addApproval('code-review', 2)
1830 C.addApproval('code-review', 2)
1831 D.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001832
1833 C.setDependsOn(B, 1)
1834 B.setDependsOn(A, 1)
1835 A.setDependsOn(M, 1)
1836
James E. Blair8b5408c2016-08-08 15:37:46 -07001837 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1838 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1839 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1840 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001841 self.waitUntilSettled()
1842
1843 A.addPatchset()
1844 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
1845 self.waitUntilSettled()
1846
Paul Belanger174a8272017-03-14 13:20:10 -04001847 self.executor_server.hold_jobs_in_build = False
1848 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001849 self.waitUntilSettled()
1850
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001851 self.assertEqual(A.data['status'], 'NEW')
1852 self.assertEqual(A.reported, 2)
1853 self.assertEqual(B.data['status'], 'NEW')
1854 self.assertEqual(B.reported, 2)
1855 self.assertEqual(C.data['status'], 'NEW')
1856 self.assertEqual(C.reported, 2)
1857 self.assertEqual(D.data['status'], 'MERGED')
1858 self.assertEqual(D.reported, 2)
1859 self.assertEqual(len(self.history), 7)
James E. Blair2fa50962013-01-30 21:50:41 -08001860
1861 def test_new_patchset_dequeues_old_without_dependents(self):
1862 "Test that a new patchset causes only the old to be dequeued"
Paul Belanger174a8272017-03-14 13:20:10 -04001863 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001864 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1865 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1866 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07001867 A.addApproval('code-review', 2)
1868 B.addApproval('code-review', 2)
1869 C.addApproval('code-review', 2)
James E. Blair2fa50962013-01-30 21:50:41 -08001870
James E. Blair8b5408c2016-08-08 15:37:46 -07001871 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1872 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1873 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair2fa50962013-01-30 21:50:41 -08001874 self.waitUntilSettled()
1875
1876 B.addPatchset()
1877 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1878 self.waitUntilSettled()
1879
Paul Belanger174a8272017-03-14 13:20:10 -04001880 self.executor_server.hold_jobs_in_build = False
1881 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001882 self.waitUntilSettled()
1883
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001884 self.assertEqual(A.data['status'], 'MERGED')
1885 self.assertEqual(A.reported, 2)
1886 self.assertEqual(B.data['status'], 'NEW')
1887 self.assertEqual(B.reported, 2)
1888 self.assertEqual(C.data['status'], 'MERGED')
1889 self.assertEqual(C.reported, 2)
1890 self.assertEqual(len(self.history), 9)
James E. Blair2fa50962013-01-30 21:50:41 -08001891
1892 def test_new_patchset_dequeues_old_independent_queue(self):
1893 "Test that a new patchset causes the old to be dequeued (independent)"
Paul Belanger174a8272017-03-14 13:20:10 -04001894 self.executor_server.hold_jobs_in_build = True
James E. Blair2fa50962013-01-30 21:50:41 -08001895 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
1896 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
1897 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
1898 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1899 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
1900 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
1901 self.waitUntilSettled()
1902
1903 B.addPatchset()
1904 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(2))
1905 self.waitUntilSettled()
1906
Paul Belanger174a8272017-03-14 13:20:10 -04001907 self.executor_server.hold_jobs_in_build = False
1908 self.executor_server.release()
James E. Blair2fa50962013-01-30 21:50:41 -08001909 self.waitUntilSettled()
1910
Monty Taylor98f0f3e2013-07-06 16:02:31 -04001911 self.assertEqual(A.data['status'], 'NEW')
1912 self.assertEqual(A.reported, 1)
1913 self.assertEqual(B.data['status'], 'NEW')
1914 self.assertEqual(B.reported, 1)
1915 self.assertEqual(C.data['status'], 'NEW')
1916 self.assertEqual(C.reported, 1)
1917 self.assertEqual(len(self.history), 10)
1918 self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 1)
James E. Blair7d0dedc2013-02-21 17:26:09 -08001919
James E. Blaircc020532017-04-19 13:04:44 -07001920 @simple_layout('layouts/noop-job.yaml')
James E. Blair18c64442014-03-18 10:14:45 -07001921 def test_noop_job(self):
1922 "Test that the internal noop job works"
1923 A = self.fake_gerrit.addFakeChange('org/noop-project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07001924 A.addApproval('code-review', 2)
1925 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair18c64442014-03-18 10:14:45 -07001926 self.waitUntilSettled()
1927
1928 self.assertEqual(len(self.gearman_server.getQueue()), 0)
1929 self.assertTrue(self.sched._areAllBuildsComplete())
1930 self.assertEqual(len(self.history), 0)
1931 self.assertEqual(A.data['status'], 'MERGED')
1932 self.assertEqual(A.reported, 2)
1933
James E. Blair06cc3922017-04-19 10:08:10 -07001934 @simple_layout('layouts/no-jobs-project.yaml')
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001935 def test_no_job_project(self):
1936 "Test that reports with no jobs don't get sent"
1937 A = self.fake_gerrit.addFakeChange('org/no-jobs-project',
1938 'master', 'A')
1939 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
1940 self.waitUntilSettled()
1941
1942 # Change wasn't reported to
1943 self.assertEqual(A.reported, False)
1944
1945 # Check queue is empty afterwards
Clint Byrum883c30e2016-12-13 11:54:33 -08001946 tenant = self.sched.abide.tenants.get('tenant-one')
1947 check_pipeline = tenant.layout.pipelines['check']
Evgeny Antyshevd6e546c2015-06-11 15:13:57 +00001948 items = check_pipeline.getAllItems()
1949 self.assertEqual(len(items), 0)
1950
1951 self.assertEqual(len(self.history), 0)
1952
James E. Blair7d0dedc2013-02-21 17:26:09 -08001953 def test_zuul_refs(self):
1954 "Test that zuul refs exist and have the right changes"
Paul Belanger174a8272017-03-14 13:20:10 -04001955 self.executor_server.hold_jobs_in_build = True
James E. Blair7d0dedc2013-02-21 17:26:09 -08001956 M1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'M1')
1957 M1.setMerged()
1958 M2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'M2')
1959 M2.setMerged()
1960
1961 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
1962 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
1963 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
1964 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
James E. Blair8b5408c2016-08-08 15:37:46 -07001965 A.addApproval('code-review', 2)
1966 B.addApproval('code-review', 2)
1967 C.addApproval('code-review', 2)
1968 D.addApproval('code-review', 2)
1969 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
1970 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
1971 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
1972 self.fake_gerrit.addEvent(D.addApproval('approved', 1))
James E. Blair7d0dedc2013-02-21 17:26:09 -08001973
1974 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001975 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001976 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001977 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001978 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001979 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001980 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04001981 self.executor_server.release('.*-merge')
James E. Blair7d0dedc2013-02-21 17:26:09 -08001982 self.waitUntilSettled()
1983
James E. Blair7d0dedc2013-02-21 17:26:09 -08001984 a_zref = b_zref = c_zref = d_zref = None
Clint Byrum3343e3e2016-11-15 16:05:03 -08001985 a_build = b_build = c_build = d_build = None
Monty Taylor6bef8ef2013-06-02 08:17:12 -04001986 for x in self.builds:
James E. Blair7d0dedc2013-02-21 17:26:09 -08001987 if x.parameters['ZUUL_CHANGE'] == '3':
1988 a_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001989 a_build = x
1990 elif x.parameters['ZUUL_CHANGE'] == '4':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001991 b_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001992 b_build = x
1993 elif x.parameters['ZUUL_CHANGE'] == '5':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001994 c_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001995 c_build = x
1996 elif x.parameters['ZUUL_CHANGE'] == '6':
James E. Blair7d0dedc2013-02-21 17:26:09 -08001997 d_zref = x.parameters['ZUUL_REF']
Clint Byrum3343e3e2016-11-15 16:05:03 -08001998 d_build = x
1999 if a_build and b_build and c_build and d_build:
2000 break
James E. Blair7d0dedc2013-02-21 17:26:09 -08002001
2002 # There are... four... refs.
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002003 self.assertIsNotNone(a_zref)
2004 self.assertIsNotNone(b_zref)
2005 self.assertIsNotNone(c_zref)
2006 self.assertIsNotNone(d_zref)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002007
2008 # And they should all be different
2009 refs = set([a_zref, b_zref, c_zref, d_zref])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002010 self.assertEqual(len(refs), 4)
James E. Blair7d0dedc2013-02-21 17:26:09 -08002011
Clint Byrum3343e3e2016-11-15 16:05:03 -08002012 # should have a, not b, and should not be in project2
2013 self.assertTrue(a_build.hasChanges(A))
2014 self.assertFalse(a_build.hasChanges(B, M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002015
Clint Byrum3343e3e2016-11-15 16:05:03 -08002016 # should have a and b, and should not be in project2
2017 self.assertTrue(b_build.hasChanges(A, B))
2018 self.assertFalse(b_build.hasChanges(M2))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002019
Clint Byrum3343e3e2016-11-15 16:05:03 -08002020 # should have a and b in 1, c in 2
2021 self.assertTrue(c_build.hasChanges(A, B, C))
2022 self.assertFalse(c_build.hasChanges(D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002023
Clint Byrum3343e3e2016-11-15 16:05:03 -08002024 # should have a and b in 1, c and d in 2
2025 self.assertTrue(d_build.hasChanges(A, B, C, D))
James E. Blair7d0dedc2013-02-21 17:26:09 -08002026
Paul Belanger174a8272017-03-14 13:20:10 -04002027 self.executor_server.hold_jobs_in_build = False
2028 self.executor_server.release()
James E. Blair7d0dedc2013-02-21 17:26:09 -08002029 self.waitUntilSettled()
2030
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002031 self.assertEqual(A.data['status'], 'MERGED')
2032 self.assertEqual(A.reported, 2)
2033 self.assertEqual(B.data['status'], 'MERGED')
2034 self.assertEqual(B.reported, 2)
2035 self.assertEqual(C.data['status'], 'MERGED')
2036 self.assertEqual(C.reported, 2)
2037 self.assertEqual(D.data['status'], 'MERGED')
2038 self.assertEqual(D.reported, 2)
James E. Blair70c71582013-03-06 08:50:50 -08002039
James E. Blair4a28a882013-08-23 15:17:33 -07002040 def test_rerun_on_error(self):
2041 "Test that if a worker fails to run a job, it is run again"
Paul Belanger174a8272017-03-14 13:20:10 -04002042 self.executor_server.hold_jobs_in_build = True
James E. Blair4a28a882013-08-23 15:17:33 -07002043 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002044 A.addApproval('code-review', 2)
2045 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair4a28a882013-08-23 15:17:33 -07002046 self.waitUntilSettled()
2047
James E. Blair412fba82017-01-26 15:00:50 -08002048 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002049 self.executor_server.hold_jobs_in_build = False
2050 self.executor_server.release()
James E. Blair4a28a882013-08-23 15:17:33 -07002051 self.waitUntilSettled()
James E. Blair412fba82017-01-26 15:00:50 -08002052 self.assertEqual(self.countJobResults(self.history, None), 1)
James E. Blair4a28a882013-08-23 15:17:33 -07002053 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 3)
2054
James E. Blair412e5582013-04-22 15:50:12 -07002055 def test_statsd(self):
2056 "Test each of the statsd methods used in the scheduler"
2057 import extras
2058 statsd = extras.try_import('statsd.statsd')
2059 statsd.incr('test-incr')
2060 statsd.timing('test-timing', 3)
Alex Gaynor813d39b2014-05-17 16:17:16 -07002061 statsd.gauge('test-gauge', 12)
James E. Blair412e5582013-04-22 15:50:12 -07002062 self.assertReportedStat('test-incr', '1|c')
2063 self.assertReportedStat('test-timing', '3|ms')
Alex Gaynor813d39b2014-05-17 16:17:16 -07002064 self.assertReportedStat('test-gauge', '12|g')
James E. Blair412e5582013-04-22 15:50:12 -07002065
James E. Blairdad52252014-02-07 16:59:17 -08002066 def test_stuck_job_cleanup(self):
2067 "Test that pending jobs are cleaned up if removed from layout"
Clint Byrum678e2c32017-03-16 16:27:21 -07002068
2069 # We want to hold the project-merge job that the fake change enqueues
James E. Blairdad52252014-02-07 16:59:17 -08002070 self.gearman_server.hold_jobs_in_queue = True
2071 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002072 A.addApproval('code-review', 2)
2073 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blairdad52252014-02-07 16:59:17 -08002074 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002075 # The assertion is that we have one job in the queue, project-merge
James E. Blairdad52252014-02-07 16:59:17 -08002076 self.assertEqual(len(self.gearman_server.getQueue()), 1)
2077
James E. Blair31cb3e82017-04-20 13:08:56 -07002078 self.commitConfigUpdate('common-config', 'layouts/no-jobs.yaml')
James E. Blairdad52252014-02-07 16:59:17 -08002079 self.sched.reconfigure(self.config)
2080 self.waitUntilSettled()
2081
James E. Blair18c64442014-03-18 10:14:45 -07002082 self.gearman_server.release('gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002083 self.waitUntilSettled()
Clint Byrum678e2c32017-03-16 16:27:21 -07002084 # asserting that project-merge is removed from queue
James E. Blairdad52252014-02-07 16:59:17 -08002085 self.assertEqual(len(self.gearman_server.getQueue()), 0)
2086 self.assertTrue(self.sched._areAllBuildsComplete())
2087
2088 self.assertEqual(len(self.history), 1)
James E. Blair18c64442014-03-18 10:14:45 -07002089 self.assertEqual(self.history[0].name, 'gate-noop')
James E. Blairdad52252014-02-07 16:59:17 -08002090 self.assertEqual(self.history[0].result, 'SUCCESS')
2091
James E. Blair879dafb2015-07-17 14:04:49 -07002092 def test_file_head(self):
2093 # This is a regression test for an observed bug. A change
2094 # with a file named "HEAD" in the root directory of the repo
2095 # was processed by a merger. It then was unable to reset the
2096 # repo because of:
2097 # GitCommandError: 'git reset --hard HEAD' returned
2098 # with exit code 128
2099 # stderr: 'fatal: ambiguous argument 'HEAD': both revision
2100 # and filename
2101 # Use '--' to separate filenames from revisions'
2102
2103 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belangera91095f2016-11-04 19:28:48 -04002104 A.addPatchset({'HEAD': ''})
James E. Blair879dafb2015-07-17 14:04:49 -07002105 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
2106
2107 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
2108 self.waitUntilSettled()
2109
2110 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2111 self.waitUntilSettled()
2112
2113 self.assertIn('Build succeeded', A.messages[0])
2114 self.assertIn('Build succeeded', B.messages[0])
2115
James E. Blair70c71582013-03-06 08:50:50 -08002116 def test_file_jobs(self):
2117 "Test that file jobs run only when appropriate"
2118 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Adam Gandelman5661bda2017-02-03 15:58:03 -08002119 A.addPatchset({'pip-requires': 'foo'})
James E. Blair70c71582013-03-06 08:50:50 -08002120 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07002121 A.addApproval('code-review', 2)
2122 B.addApproval('code-review', 2)
2123 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2124 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair70c71582013-03-06 08:50:50 -08002125 self.waitUntilSettled()
2126
Monty Taylor6bef8ef2013-06-02 08:17:12 -04002127 testfile_jobs = [x for x in self.history
James E. Blair70c71582013-03-06 08:50:50 -08002128 if x.name == 'project-testfile']
2129
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002130 self.assertEqual(len(testfile_jobs), 1)
2131 self.assertEqual(testfile_jobs[0].changes, '1,2')
2132 self.assertEqual(A.data['status'], 'MERGED')
2133 self.assertEqual(A.reported, 2)
2134 self.assertEqual(B.data['status'], 'MERGED')
2135 self.assertEqual(B.reported, 2)
James E. Blair3c5e5b52013-04-26 11:17:03 -07002136
Clint Byrum3ee39f32016-11-17 23:45:07 -08002137 def _test_irrelevant_files_jobs(self, should_skip):
2138 "Test that jobs with irrelevant-files filter run only when appropriate"
Clint Byrum3ee39f32016-11-17 23:45:07 -08002139 if should_skip:
2140 files = {'ignoreme': 'ignored\n'}
2141 else:
2142 files = {'respectme': 'please!\n'}
Maru Newby3fe5f852015-01-13 04:22:14 +00002143
2144 change = self.fake_gerrit.addFakeChange('org/project',
Clint Byrum3ee39f32016-11-17 23:45:07 -08002145 'master',
2146 'test irrelevant-files',
2147 files=files)
Maru Newby3fe5f852015-01-13 04:22:14 +00002148 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2149 self.waitUntilSettled()
2150
2151 tested_change_ids = [x.changes[0] for x in self.history
Clint Byrum3ee39f32016-11-17 23:45:07 -08002152 if x.name == 'project-test-irrelevant-files']
Maru Newby3fe5f852015-01-13 04:22:14 +00002153
2154 if should_skip:
2155 self.assertEqual([], tested_change_ids)
2156 else:
2157 self.assertIn(change.data['number'], tested_change_ids)
2158
James E. Blaircc324192017-04-19 14:36:08 -07002159 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002160 def test_irrelevant_files_match_skips_job(self):
2161 self._test_irrelevant_files_jobs(should_skip=True)
Maru Newby3fe5f852015-01-13 04:22:14 +00002162
James E. Blaircc324192017-04-19 14:36:08 -07002163 @simple_layout('layouts/irrelevant-files.yaml')
Clint Byrum3ee39f32016-11-17 23:45:07 -08002164 def test_irrelevant_files_no_match_runs_job(self):
2165 self._test_irrelevant_files_jobs(should_skip=False)
Maru Newby3fe5f852015-01-13 04:22:14 +00002166
James E. Blaircc324192017-04-19 14:36:08 -07002167 @simple_layout('layouts/inheritance.yaml')
Clint Byrum85493602016-11-18 11:59:47 -08002168 def test_inherited_jobs_keep_matchers(self):
Clint Byrum85493602016-11-18 11:59:47 -08002169 files = {'ignoreme': 'ignored\n'}
2170
2171 change = self.fake_gerrit.addFakeChange('org/project',
2172 'master',
2173 'test irrelevant-files',
2174 files=files)
2175 self.fake_gerrit.addEvent(change.getPatchsetCreatedEvent(1))
2176 self.waitUntilSettled()
2177
2178 run_jobs = set([build.name for build in self.history])
2179
2180 self.assertEqual(set(['project-test-nomatch-starts-empty',
2181 'project-test-nomatch-starts-full']), run_jobs)
2182
James E. Blairc8a1e052014-02-25 09:29:26 -08002183 def test_queue_names(self):
2184 "Test shared change queue names"
Paul Belanger8e68fc22017-02-06 16:24:49 -05002185 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07002186 (trusted, project1) = tenant.getProject('org/project1')
2187 (trusted, project2) = tenant.getProject('org/project2')
Paul Belanger8e68fc22017-02-06 16:24:49 -05002188 q1 = tenant.layout.pipelines['gate'].getQueue(project1)
2189 q2 = tenant.layout.pipelines['gate'].getQueue(project2)
2190 self.assertEqual(q1.name, 'integrated')
2191 self.assertEqual(q2.name, 'integrated')
James E. Blairc8a1e052014-02-25 09:29:26 -08002192
James E. Blair64ed6f22013-07-10 14:07:23 -07002193 def test_queue_precedence(self):
2194 "Test that queue precedence works"
2195
2196 self.gearman_server.hold_jobs_in_queue = True
Paul Belanger174a8272017-03-14 13:20:10 -04002197 self.executor_server.hold_jobs_in_build = True
James E. Blair64ed6f22013-07-10 14:07:23 -07002198 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2199 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
James E. Blair8b5408c2016-08-08 15:37:46 -07002200 A.addApproval('code-review', 2)
2201 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair64ed6f22013-07-10 14:07:23 -07002202
2203 self.waitUntilSettled()
2204 self.gearman_server.hold_jobs_in_queue = False
2205 self.gearman_server.release()
2206 self.waitUntilSettled()
2207
James E. Blair8de58bd2013-07-18 16:23:33 -07002208 # Run one build at a time to ensure non-race order:
James E. Blairb8c16472015-05-05 14:55:26 -07002209 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04002210 self.executor_server.hold_jobs_in_build = False
James E. Blair8de58bd2013-07-18 16:23:33 -07002211 self.waitUntilSettled()
2212
James E. Blair64ed6f22013-07-10 14:07:23 -07002213 self.log.debug(self.history)
2214 self.assertEqual(self.history[0].pipeline, 'gate')
2215 self.assertEqual(self.history[1].pipeline, 'check')
2216 self.assertEqual(self.history[2].pipeline, 'gate')
2217 self.assertEqual(self.history[3].pipeline, 'gate')
2218 self.assertEqual(self.history[4].pipeline, 'check')
2219 self.assertEqual(self.history[5].pipeline, 'check')
2220
Clark Boylana5edbe42014-06-03 16:39:10 -07002221 def test_json_status(self):
James E. Blair1843a552013-07-03 14:19:52 -07002222 "Test that we can retrieve JSON status info"
Paul Belanger174a8272017-03-14 13:20:10 -04002223 self.executor_server.hold_jobs_in_build = True
James E. Blair1843a552013-07-03 14:19:52 -07002224 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002225 A.addApproval('code-review', 2)
2226 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair1843a552013-07-03 14:19:52 -07002227 self.waitUntilSettled()
2228
Paul Belanger174a8272017-03-14 13:20:10 -04002229 self.executor_server.release('project-merge')
James E. Blairb7273ef2016-04-19 08:58:51 -07002230 self.waitUntilSettled()
2231
James E. Blair1843a552013-07-03 14:19:52 -07002232 port = self.webapp.server.socket.getsockname()[1]
2233
Adam Gandelman77a12c72017-02-03 17:43:43 -08002234 req = urllib.request.Request(
2235 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002236 f = urllib.request.urlopen(req)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002237 headers = f.info()
2238 self.assertIn('Content-Length', headers)
2239 self.assertIn('Content-Type', headers)
Sachi Kingdc963fc2016-03-23 16:00:33 +11002240 self.assertIsNotNone(re.match('^application/json(; charset=UTF-8)?$',
2241 headers['Content-Type']))
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002242 self.assertIn('Access-Control-Allow-Origin', headers)
2243 self.assertIn('Cache-Control', headers)
Clark Boylanaa4f2e72014-06-03 21:22:40 -07002244 self.assertIn('Last-Modified', headers)
Timo Tijhof0ebd2932015-04-02 12:11:21 +01002245 self.assertIn('Expires', headers)
Clint Byrumf322fe22017-05-10 20:53:12 -07002246 data = f.read().decode('utf8')
James E. Blair1843a552013-07-03 14:19:52 -07002247
Paul Belanger174a8272017-03-14 13:20:10 -04002248 self.executor_server.hold_jobs_in_build = False
2249 self.executor_server.release()
James E. Blair1843a552013-07-03 14:19:52 -07002250 self.waitUntilSettled()
2251
2252 data = json.loads(data)
James E. Blairb7273ef2016-04-19 08:58:51 -07002253 status_jobs = []
James E. Blair1843a552013-07-03 14:19:52 -07002254 for p in data['pipelines']:
2255 for q in p['change_queues']:
James E. Blairbfb8e042014-12-30 17:01:44 -08002256 if p['name'] in ['gate', 'conflict']:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002257 self.assertEqual(q['window'], 20)
2258 else:
2259 self.assertEqual(q['window'], 0)
James E. Blair1843a552013-07-03 14:19:52 -07002260 for head in q['heads']:
2261 for change in head:
Clark Boylanaf2476f2014-01-23 14:47:36 -08002262 self.assertTrue(change['active'])
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002263 self.assertEqual(change['id'], '1,1')
James E. Blair1843a552013-07-03 14:19:52 -07002264 for job in change['jobs']:
James E. Blairb7273ef2016-04-19 08:58:51 -07002265 status_jobs.append(job)
2266 self.assertEqual('project-merge', status_jobs[0]['name'])
2267 self.assertEqual('https://server/job/project-merge/0/',
2268 status_jobs[0]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002269 self.assertEqual('https://server/job/project-merge/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002270 status_jobs[0]['report_url'])
James E. Blairb7273ef2016-04-19 08:58:51 -07002271 self.assertEqual('project-test1', status_jobs[1]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002272 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002273 status_jobs[1]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002274 self.assertEqual('https://server/job/project-test1/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002275 status_jobs[1]['report_url'])
2276
2277 self.assertEqual('project-test2', status_jobs[2]['name'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002278 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002279 status_jobs[2]['url'])
Adam Gandelman77a12c72017-02-03 17:43:43 -08002280 self.assertEqual('https://server/job/project-test2/0/',
James E. Blairb7273ef2016-04-19 08:58:51 -07002281 status_jobs[2]['report_url'])
James E. Blair1843a552013-07-03 14:19:52 -07002282
James E. Blaircdccd972013-07-01 12:10:22 -07002283 def test_live_reconfiguration(self):
2284 "Test that live reconfiguration works"
Paul Belanger174a8272017-03-14 13:20:10 -04002285 self.executor_server.hold_jobs_in_build = True
James E. Blaircdccd972013-07-01 12:10:22 -07002286 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002287 A.addApproval('code-review', 2)
2288 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blaircdccd972013-07-01 12:10:22 -07002289 self.waitUntilSettled()
2290
2291 self.sched.reconfigure(self.config)
James E. Blairbd963632017-02-01 09:58:14 -08002292 self.waitUntilSettled()
James E. Blaircdccd972013-07-01 12:10:22 -07002293
Paul Belanger174a8272017-03-14 13:20:10 -04002294 self.executor_server.hold_jobs_in_build = False
2295 self.executor_server.release()
James E. Blaircdccd972013-07-01 12:10:22 -07002296 self.waitUntilSettled()
Monty Taylor98f0f3e2013-07-06 16:02:31 -04002297 self.assertEqual(self.getJobFromHistory('project-merge').result,
2298 'SUCCESS')
2299 self.assertEqual(self.getJobFromHistory('project-test1').result,
2300 'SUCCESS')
2301 self.assertEqual(self.getJobFromHistory('project-test2').result,
2302 'SUCCESS')
2303 self.assertEqual(A.data['status'], 'MERGED')
2304 self.assertEqual(A.reported, 2)
James E. Blair287c06d2013-07-24 10:39:30 -07002305
James E. Blair6bc782d2015-07-17 16:20:21 -07002306 def test_live_reconfiguration_merge_conflict(self):
2307 # A real-world bug: a change in a gate queue has a merge
2308 # conflict and a job is added to its project while it's
2309 # sitting in the queue. The job gets added to the change and
2310 # enqueued and the change gets stuck.
Paul Belanger174a8272017-03-14 13:20:10 -04002311 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002312
2313 # This change is fine. It's here to stop the queue long
2314 # enough for the next change to be subject to the
2315 # reconfiguration, as well as to provide a conflict for the
2316 # next change. This change will succeed and merge.
2317 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum073c00b2017-05-01 13:41:23 -07002318 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002319 A.addApproval('code-review', 2)
James E. Blair6bc782d2015-07-17 16:20:21 -07002320
2321 # This change will be in merge conflict. During the
2322 # reconfiguration, we will add a job. We want to make sure
2323 # that doesn't cause it to get stuck.
2324 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Clint Byrum073c00b2017-05-01 13:41:23 -07002325 B.addPatchset({'conflict': 'B'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002326 B.addApproval('code-review', 2)
James E. Blair4eb21fa2015-07-27 14:56:47 -07002327
James E. Blair8b5408c2016-08-08 15:37:46 -07002328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
2329 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002330
2331 self.waitUntilSettled()
2332
2333 # No jobs have run yet
2334 self.assertEqual(A.data['status'], 'NEW')
2335 self.assertEqual(A.reported, 1)
2336 self.assertEqual(B.data['status'], 'NEW')
James E. Blair6bc782d2015-07-17 16:20:21 -07002337 self.assertEqual(len(self.history), 0)
2338
2339 # Add the "project-test3" job.
Clint Byrum073c00b2017-05-01 13:41:23 -07002340 self.commitConfigUpdate('common-config',
2341 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002342 self.sched.reconfigure(self.config)
2343 self.waitUntilSettled()
2344
Paul Belanger174a8272017-03-14 13:20:10 -04002345 self.executor_server.hold_jobs_in_build = False
2346 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002347 self.waitUntilSettled()
2348
2349 self.assertEqual(A.data['status'], 'MERGED')
2350 self.assertEqual(A.reported, 2)
2351 self.assertEqual(B.data['status'], 'NEW')
Tobias Henkel9842bd72017-05-16 13:40:03 +02002352 self.assertIn('Merge Failed', B.messages[-1])
James E. Blair6bc782d2015-07-17 16:20:21 -07002353 self.assertEqual(self.getJobFromHistory('project-merge').result,
2354 'SUCCESS')
2355 self.assertEqual(self.getJobFromHistory('project-test1').result,
2356 'SUCCESS')
2357 self.assertEqual(self.getJobFromHistory('project-test2').result,
2358 'SUCCESS')
2359 self.assertEqual(self.getJobFromHistory('project-test3').result,
2360 'SUCCESS')
2361 self.assertEqual(len(self.history), 4)
2362
James E. Blair400e8fd2015-07-30 17:44:45 -07002363 def test_live_reconfiguration_failed_root(self):
James E. Blair6bc782d2015-07-17 16:20:21 -07002364 # An extrapolation of test_live_reconfiguration_merge_conflict
2365 # that tests a job added to a job tree with a failed root does
2366 # not run.
Paul Belanger174a8272017-03-14 13:20:10 -04002367 self.executor_server.hold_jobs_in_build = True
James E. Blair6bc782d2015-07-17 16:20:21 -07002368
2369 # This change is fine. It's here to stop the queue long
2370 # enough for the next change to be subject to the
2371 # reconfiguration. This change will succeed and merge.
2372 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Clint Byrum3147d652017-05-01 14:01:42 -07002373 A.addPatchset({'conflict': 'A'})
James E. Blair8b5408c2016-08-08 15:37:46 -07002374 A.addApproval('code-review', 2)
2375 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002376 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002377 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002378 self.waitUntilSettled()
2379
2380 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -04002381 self.executor_server.failJob('project-merge', B)
James E. Blair8b5408c2016-08-08 15:37:46 -07002382 B.addApproval('code-review', 2)
2383 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair6bc782d2015-07-17 16:20:21 -07002384 self.waitUntilSettled()
2385
Paul Belanger174a8272017-03-14 13:20:10 -04002386 self.executor_server.release('.*-merge')
James E. Blair6bc782d2015-07-17 16:20:21 -07002387 self.waitUntilSettled()
2388
2389 # Both -merge jobs have run, but no others.
2390 self.assertEqual(A.data['status'], 'NEW')
2391 self.assertEqual(A.reported, 1)
2392 self.assertEqual(B.data['status'], 'NEW')
2393 self.assertEqual(B.reported, 1)
2394 self.assertEqual(self.history[0].result, 'SUCCESS')
2395 self.assertEqual(self.history[0].name, 'project-merge')
2396 self.assertEqual(self.history[1].result, 'FAILURE')
2397 self.assertEqual(self.history[1].name, 'project-merge')
2398 self.assertEqual(len(self.history), 2)
2399
2400 # Add the "project-test3" job.
Clint Byrum3147d652017-05-01 14:01:42 -07002401 self.commitConfigUpdate('common-config',
2402 'layouts/live-reconfiguration-add-job.yaml')
James E. Blair6bc782d2015-07-17 16:20:21 -07002403 self.sched.reconfigure(self.config)
2404 self.waitUntilSettled()
2405
Paul Belanger174a8272017-03-14 13:20:10 -04002406 self.executor_server.hold_jobs_in_build = False
2407 self.executor_server.release()
James E. Blair6bc782d2015-07-17 16:20:21 -07002408 self.waitUntilSettled()
2409
2410 self.assertEqual(A.data['status'], 'MERGED')
2411 self.assertEqual(A.reported, 2)
2412 self.assertEqual(B.data['status'], 'NEW')
2413 self.assertEqual(B.reported, 2)
2414 self.assertEqual(self.history[0].result, 'SUCCESS')
2415 self.assertEqual(self.history[0].name, 'project-merge')
2416 self.assertEqual(self.history[1].result, 'FAILURE')
2417 self.assertEqual(self.history[1].name, 'project-merge')
2418 self.assertEqual(self.history[2].result, 'SUCCESS')
2419 self.assertEqual(self.history[3].result, 'SUCCESS')
2420 self.assertEqual(self.history[4].result, 'SUCCESS')
2421 self.assertEqual(len(self.history), 5)
2422
James E. Blair400e8fd2015-07-30 17:44:45 -07002423 def test_live_reconfiguration_failed_job(self):
2424 # Test that a change with a removed failing job does not
2425 # disrupt reconfiguration. If a change has a failed job and
2426 # that job is removed during a reconfiguration, we observed a
2427 # bug where the code to re-set build statuses would run on
2428 # that build and raise an exception because the job no longer
2429 # existed.
Paul Belanger174a8272017-03-14 13:20:10 -04002430 self.executor_server.hold_jobs_in_build = True
James E. Blair400e8fd2015-07-30 17:44:45 -07002431
2432 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2433
2434 # This change will fail and later be removed by the reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002435 self.executor_server.failJob('project-test1', A)
James E. Blair400e8fd2015-07-30 17:44:45 -07002436
2437 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2438 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002439 self.executor_server.release('.*-merge')
James E. Blair400e8fd2015-07-30 17:44:45 -07002440 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002441 self.executor_server.release('project-test1')
James E. Blair400e8fd2015-07-30 17:44:45 -07002442 self.waitUntilSettled()
2443
2444 self.assertEqual(A.data['status'], 'NEW')
2445 self.assertEqual(A.reported, 0)
2446
2447 self.assertEqual(self.getJobFromHistory('project-merge').result,
2448 'SUCCESS')
2449 self.assertEqual(self.getJobFromHistory('project-test1').result,
2450 'FAILURE')
2451 self.assertEqual(len(self.history), 2)
2452
2453 # Remove the test1 job.
Clint Byrum841a58c2017-05-01 14:17:21 -07002454 self.commitConfigUpdate('common-config',
2455 'layouts/live-reconfiguration-failed-job.yaml')
James E. Blair400e8fd2015-07-30 17:44:45 -07002456 self.sched.reconfigure(self.config)
2457 self.waitUntilSettled()
2458
Paul Belanger174a8272017-03-14 13:20:10 -04002459 self.executor_server.hold_jobs_in_build = False
2460 self.executor_server.release()
James E. Blair400e8fd2015-07-30 17:44:45 -07002461 self.waitUntilSettled()
2462
2463 self.assertEqual(self.getJobFromHistory('project-test2').result,
2464 'SUCCESS')
2465 self.assertEqual(self.getJobFromHistory('project-testfile').result,
2466 'SUCCESS')
2467 self.assertEqual(len(self.history), 4)
2468
2469 self.assertEqual(A.data['status'], 'NEW')
2470 self.assertEqual(A.reported, 1)
2471 self.assertIn('Build succeeded', A.messages[0])
2472 # Ensure the removed job was not included in the report.
2473 self.assertNotIn('project-test1', A.messages[0])
2474
James E. Blairfe707d12015-08-05 15:18:15 -07002475 def test_live_reconfiguration_shared_queue(self):
2476 # Test that a change with a failing job which was removed from
2477 # this project but otherwise still exists in the system does
2478 # not disrupt reconfiguration.
2479
Paul Belanger174a8272017-03-14 13:20:10 -04002480 self.executor_server.hold_jobs_in_build = True
James E. Blairfe707d12015-08-05 15:18:15 -07002481
2482 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2483
Paul Belanger174a8272017-03-14 13:20:10 -04002484 self.executor_server.failJob('project1-project2-integration', A)
James E. Blairfe707d12015-08-05 15:18:15 -07002485
2486 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2487 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002488 self.executor_server.release('.*-merge')
James E. Blairfe707d12015-08-05 15:18:15 -07002489 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002490 self.executor_server.release('project1-project2-integration')
James E. Blairfe707d12015-08-05 15:18:15 -07002491 self.waitUntilSettled()
2492
2493 self.assertEqual(A.data['status'], 'NEW')
2494 self.assertEqual(A.reported, 0)
2495
Clint Byrum69663d42017-05-01 14:51:04 -07002496 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002497 'SUCCESS')
2498 self.assertEqual(self.getJobFromHistory(
2499 'project1-project2-integration').result, 'FAILURE')
2500 self.assertEqual(len(self.history), 2)
2501
2502 # Remove the integration job.
Clint Byrum69663d42017-05-01 14:51:04 -07002503 self.commitConfigUpdate(
2504 'common-config',
2505 'layouts/live-reconfiguration-shared-queue.yaml')
James E. Blairfe707d12015-08-05 15:18:15 -07002506 self.sched.reconfigure(self.config)
2507 self.waitUntilSettled()
2508
Paul Belanger174a8272017-03-14 13:20:10 -04002509 self.executor_server.hold_jobs_in_build = False
2510 self.executor_server.release()
James E. Blairfe707d12015-08-05 15:18:15 -07002511 self.waitUntilSettled()
2512
Clint Byrum69663d42017-05-01 14:51:04 -07002513 self.assertEqual(self.getJobFromHistory('project-merge').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002514 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002515 self.assertEqual(self.getJobFromHistory('project-test1').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002516 'SUCCESS')
Clint Byrum69663d42017-05-01 14:51:04 -07002517 self.assertEqual(self.getJobFromHistory('project-test2').result,
James E. Blairfe707d12015-08-05 15:18:15 -07002518 'SUCCESS')
2519 self.assertEqual(self.getJobFromHistory(
2520 'project1-project2-integration').result, 'FAILURE')
2521 self.assertEqual(len(self.history), 4)
2522
2523 self.assertEqual(A.data['status'], 'NEW')
2524 self.assertEqual(A.reported, 1)
2525 self.assertIn('Build succeeded', A.messages[0])
2526 # Ensure the removed job was not included in the report.
2527 self.assertNotIn('project1-project2-integration', A.messages[0])
2528
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002529 def test_double_live_reconfiguration_shared_queue(self):
2530 # This was a real-world regression. A change is added to
2531 # gate; a reconfigure happens, a second change which depends
2532 # on the first is added, and a second reconfiguration happens.
2533 # Ensure that both changes merge.
2534
2535 # A failure may indicate incorrect caching or cleaning up of
2536 # references during a reconfiguration.
Paul Belanger174a8272017-03-14 13:20:10 -04002537 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002538
2539 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2540 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2541 B.setDependsOn(A, 1)
James E. Blair8b5408c2016-08-08 15:37:46 -07002542 A.addApproval('code-review', 2)
2543 B.addApproval('code-review', 2)
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002544
2545 # Add the parent change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002546 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002547 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002548 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002549 self.waitUntilSettled()
2550
2551 # Reconfigure (with only one change in the pipeline).
2552 self.sched.reconfigure(self.config)
2553 self.waitUntilSettled()
2554
2555 # Add the child change.
James E. Blair8b5408c2016-08-08 15:37:46 -07002556 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002557 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002558 self.executor_server.release('.*-merge')
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002559 self.waitUntilSettled()
2560
2561 # Reconfigure (with both in the pipeline).
2562 self.sched.reconfigure(self.config)
2563 self.waitUntilSettled()
2564
Paul Belanger174a8272017-03-14 13:20:10 -04002565 self.executor_server.hold_jobs_in_build = False
2566 self.executor_server.release()
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11002567 self.waitUntilSettled()
2568
2569 self.assertEqual(len(self.history), 8)
2570
2571 self.assertEqual(A.data['status'], 'MERGED')
2572 self.assertEqual(A.reported, 2)
2573 self.assertEqual(B.data['status'], 'MERGED')
2574 self.assertEqual(B.reported, 2)
2575
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002576 def test_live_reconfiguration_del_project(self):
2577 # Test project deletion from layout
2578 # while changes are enqueued
2579
Paul Belanger174a8272017-03-14 13:20:10 -04002580 self.executor_server.hold_jobs_in_build = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002581 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2582 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
2583 C = self.fake_gerrit.addFakeChange('org/project1', 'master', 'C')
2584
2585 # A Depends-On: B
2586 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
2587 A.subject, B.data['id'])
James E. Blair8b5408c2016-08-08 15:37:46 -07002588 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002589
2590 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2591 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
2592 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002593 self.executor_server.release('.*-merge')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002594 self.waitUntilSettled()
2595 self.assertEqual(len(self.builds), 5)
2596
2597 # This layout defines only org/project, not org/project1
James E. Blair31cb3e82017-04-20 13:08:56 -07002598 self.commitConfigUpdate(
2599 'common-config',
2600 'layouts/live-reconfiguration-del-project.yaml')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002601 self.sched.reconfigure(self.config)
2602 self.waitUntilSettled()
2603
2604 # Builds for C aborted, builds for A succeed,
2605 # and have change B applied ahead
Clint Byrum0149ada2017-04-04 12:22:01 -07002606 job_c = self.getJobFromHistory('project-test1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002607 self.assertEqual(job_c.changes, '3,1')
2608 self.assertEqual(job_c.result, 'ABORTED')
2609
Paul Belanger174a8272017-03-14 13:20:10 -04002610 self.executor_server.hold_jobs_in_build = False
2611 self.executor_server.release()
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002612 self.waitUntilSettled()
2613
Clint Byrum0149ada2017-04-04 12:22:01 -07002614 self.assertEqual(
2615 self.getJobFromHistory('project-test1', 'org/project').changes,
2616 '2,1 1,1')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002617
2618 self.assertEqual(A.data['status'], 'NEW')
2619 self.assertEqual(B.data['status'], 'NEW')
2620 self.assertEqual(C.data['status'], 'NEW')
2621 self.assertEqual(A.reported, 1)
2622 self.assertEqual(B.reported, 0)
2623 self.assertEqual(C.reported, 0)
2624
Clint Byrum0149ada2017-04-04 12:22:01 -07002625 tenant = self.sched.abide.tenants.get('tenant-one')
2626 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00002627 self.assertIn('Build succeeded', A.messages[0])
2628
James E. Blairec056492016-07-22 09:45:56 -07002629 @skip("Disabled for early v3 development")
James E. Blair287c06d2013-07-24 10:39:30 -07002630 def test_delayed_repo_init(self):
James E. Blairf84026c2015-12-08 16:11:46 -08002631 self.updateConfigLayout(
2632 'tests/fixtures/layout-delayed-repo-init.yaml')
James E. Blair287c06d2013-07-24 10:39:30 -07002633 self.sched.reconfigure(self.config)
2634
2635 self.init_repo("org/new-project")
2636 A = self.fake_gerrit.addFakeChange('org/new-project', 'master', 'A')
2637
James E. Blair8b5408c2016-08-08 15:37:46 -07002638 A.addApproval('code-review', 2)
2639 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair287c06d2013-07-24 10:39:30 -07002640 self.waitUntilSettled()
2641 self.assertEqual(self.getJobFromHistory('project-merge').result,
2642 'SUCCESS')
2643 self.assertEqual(self.getJobFromHistory('project-test1').result,
2644 'SUCCESS')
2645 self.assertEqual(self.getJobFromHistory('project-test2').result,
2646 'SUCCESS')
2647 self.assertEqual(A.data['status'], 'MERGED')
2648 self.assertEqual(A.reported, 2)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002649
James E. Blair9ea70072017-04-19 16:05:30 -07002650 @simple_layout('layouts/repo-deleted.yaml')
Clark Boylan6dbbc482013-10-18 10:57:31 -07002651 def test_repo_deleted(self):
Clark Boylan6dbbc482013-10-18 10:57:31 -07002652 self.init_repo("org/delete-project")
2653 A = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'A')
2654
James E. Blair8b5408c2016-08-08 15:37:46 -07002655 A.addApproval('code-review', 2)
2656 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002657 self.waitUntilSettled()
2658 self.assertEqual(self.getJobFromHistory('project-merge').result,
2659 'SUCCESS')
2660 self.assertEqual(self.getJobFromHistory('project-test1').result,
2661 'SUCCESS')
2662 self.assertEqual(self.getJobFromHistory('project-test2').result,
2663 'SUCCESS')
2664 self.assertEqual(A.data['status'], 'MERGED')
2665 self.assertEqual(A.reported, 2)
2666
2667 # Delete org/new-project zuul repo. Should be recloned.
James E. Blair8c1be532017-02-07 14:04:12 -08002668 p = 'org/delete-project'
Monty Taylord642d852017-02-23 14:05:42 -05002669 if os.path.exists(os.path.join(self.merger_src_root, p)):
2670 shutil.rmtree(os.path.join(self.merger_src_root, p))
Paul Belanger174a8272017-03-14 13:20:10 -04002671 if os.path.exists(os.path.join(self.executor_src_root, p)):
2672 shutil.rmtree(os.path.join(self.executor_src_root, p))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002673
2674 B = self.fake_gerrit.addFakeChange('org/delete-project', 'master', 'B')
2675
James E. Blair8b5408c2016-08-08 15:37:46 -07002676 B.addApproval('code-review', 2)
2677 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Clark Boylan6dbbc482013-10-18 10:57:31 -07002678 self.waitUntilSettled()
2679 self.assertEqual(self.getJobFromHistory('project-merge').result,
2680 'SUCCESS')
2681 self.assertEqual(self.getJobFromHistory('project-test1').result,
2682 'SUCCESS')
2683 self.assertEqual(self.getJobFromHistory('project-test2').result,
2684 'SUCCESS')
2685 self.assertEqual(B.data['status'], 'MERGED')
2686 self.assertEqual(B.reported, 2)
2687
James E. Blaire68d43f2017-04-19 14:03:30 -07002688 @simple_layout('layouts/tags.yaml')
James E. Blair456f2fb2016-02-09 09:29:33 -08002689 def test_tags(self):
2690 "Test job tags"
James E. Blair456f2fb2016-02-09 09:29:33 -08002691 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
2692 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
2693 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2694 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
2695 self.waitUntilSettled()
2696
James E. Blaire68d43f2017-04-19 14:03:30 -07002697 self.assertEqual(len(self.history), 2)
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002698
2699 results = {self.getJobFromHistory('merge',
2700 project='org/project1').uuid: 'extratag merge',
2701 self.getJobFromHistory('merge',
2702 project='org/project2').uuid: 'merge'}
James E. Blair456f2fb2016-02-09 09:29:33 -08002703
2704 for build in self.history:
Cullen Taylor3aa6d472017-03-01 15:22:09 -06002705 self.assertEqual(results.get(build.uuid, ''),
2706 build.parameters['vars']['zuul'].get('tags'))
James E. Blair456f2fb2016-02-09 09:29:33 -08002707
James E. Blair63bb0ef2013-07-29 17:14:51 -07002708 def test_timer(self):
2709 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002710 # This test can not use simple_layout because it must start
2711 # with a configuration which does not include a
2712 # timer-triggered job so that we have an opportunity to set
2713 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002714 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002715 self.commitConfigUpdate('common-config', 'layouts/timer.yaml')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002716 self.sched.reconfigure(self.config)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002717
Clark Boylan3ee090a2014-04-03 20:55:09 -07002718 # The pipeline triggers every second, so we should have seen
2719 # several by now.
2720 time.sleep(5)
James E. Blair63bb0ef2013-07-29 17:14:51 -07002721 self.waitUntilSettled()
Clark Boylan3ee090a2014-04-03 20:55:09 -07002722
2723 self.assertEqual(len(self.builds), 2)
2724
James E. Blair63bb0ef2013-07-29 17:14:51 -07002725 port = self.webapp.server.socket.getsockname()[1]
2726
Clint Byrum5e729e92017-02-08 06:34:57 -08002727 req = urllib.request.Request(
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002728 "http://localhost:%s/tenant-one/status" % port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -07002729 f = urllib.request.urlopen(req)
Clint Byrumf322fe22017-05-10 20:53:12 -07002730 data = f.read().decode('utf8')
James E. Blair63bb0ef2013-07-29 17:14:51 -07002731
Paul Belanger174a8272017-03-14 13:20:10 -04002732 self.executor_server.hold_jobs_in_build = False
Clark Boylan3ee090a2014-04-03 20:55:09 -07002733 # Stop queuing timer triggered jobs so that the assertions
2734 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002735 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002736 self.sched.reconfigure(self.config)
Paul Belanger174a8272017-03-14 13:20:10 -04002737 self.executor_server.release()
James E. Blair63bb0ef2013-07-29 17:14:51 -07002738 self.waitUntilSettled()
2739
2740 self.assertEqual(self.getJobFromHistory(
2741 'project-bitrot-stable-old').result, 'SUCCESS')
2742 self.assertEqual(self.getJobFromHistory(
2743 'project-bitrot-stable-older').result, 'SUCCESS')
2744
2745 data = json.loads(data)
2746 status_jobs = set()
2747 for p in data['pipelines']:
2748 for q in p['change_queues']:
2749 for head in q['heads']:
2750 for change in head:
James E. Blair63bb0ef2013-07-29 17:14:51 -07002751 for job in change['jobs']:
2752 status_jobs.add(job['name'])
2753 self.assertIn('project-bitrot-stable-old', status_jobs)
2754 self.assertIn('project-bitrot-stable-older', status_jobs)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002755
James E. Blair4f6033c2014-03-27 15:49:09 -07002756 def test_idle(self):
2757 "Test that frequent periodic jobs work"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002758 # This test can not use simple_layout because it must start
2759 # with a configuration which does not include a
2760 # timer-triggered job so that we have an opportunity to set
2761 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002762 self.executor_server.hold_jobs_in_build = True
James E. Blair4f6033c2014-03-27 15:49:09 -07002763
Clark Boylan3ee090a2014-04-03 20:55:09 -07002764 for x in range(1, 3):
2765 # Test that timer triggers periodic jobs even across
2766 # layout config reloads.
2767 # Start timer trigger
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002768 self.commitConfigUpdate('common-config',
2769 'layouts/idle.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002770 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002771 self.waitUntilSettled()
James E. Blair4f6033c2014-03-27 15:49:09 -07002772
Clark Boylan3ee090a2014-04-03 20:55:09 -07002773 # The pipeline triggers every second, so we should have seen
2774 # several by now.
2775 time.sleep(5)
Clark Boylan3ee090a2014-04-03 20:55:09 -07002776
2777 # Stop queuing timer triggered jobs so that the assertions
2778 # below don't race against more jobs being queued.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002779 self.commitConfigUpdate('common-config',
2780 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002781 self.sched.reconfigure(self.config)
James E. Blair995fc0f2016-02-04 16:48:31 -08002782 self.waitUntilSettled()
Clint Byrum58264dc2017-02-07 21:21:22 -08002783 self.assertEqual(len(self.builds), 2,
2784 'Timer builds iteration #%d' % x)
Paul Belanger174a8272017-03-14 13:20:10 -04002785 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002786 self.waitUntilSettled()
2787 self.assertEqual(len(self.builds), 0)
2788 self.assertEqual(len(self.history), x * 2)
James E. Blair4f6033c2014-03-27 15:49:09 -07002789
James E. Blaircc324192017-04-19 14:36:08 -07002790 @simple_layout('layouts/smtp.yaml')
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002791 def test_check_smtp_pool(self):
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002792 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
2793 self.waitUntilSettled()
2794
2795 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
2796 self.waitUntilSettled()
2797
James E. Blairff80a2f2013-12-27 13:24:06 -08002798 self.assertEqual(len(self.smtp_messages), 2)
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002799
2800 # A.messages only holds what FakeGerrit places in it. Thus we
2801 # work on the knowledge of what the first message should be as
2802 # it is only configured to go to SMTP.
2803
2804 self.assertEqual('zuul@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002805 self.smtp_messages[0]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002806 self.assertEqual(['you@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002807 self.smtp_messages[0]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002808 self.assertEqual('Starting check jobs.',
James E. Blairff80a2f2013-12-27 13:24:06 -08002809 self.smtp_messages[0]['body'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002810
2811 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002812 self.smtp_messages[1]['from_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002813 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002814 self.smtp_messages[1]['to_email'])
Joshua Hesketh5fea8672013-08-19 17:32:01 +10002815 self.assertEqual(A.messages[0],
James E. Blairff80a2f2013-12-27 13:24:06 -08002816 self.smtp_messages[1]['body'])
James E. Blairad28e912013-11-27 10:43:22 -08002817
James E. Blaire5910202013-12-27 09:50:31 -08002818 def test_timer_smtp(self):
2819 "Test that a periodic job is triggered"
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002820 # This test can not use simple_layout because it must start
2821 # with a configuration which does not include a
2822 # timer-triggered job so that we have an opportunity to set
2823 # the hold flag before the first job.
Paul Belanger174a8272017-03-14 13:20:10 -04002824 self.executor_server.hold_jobs_in_build = True
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002825 self.commitConfigUpdate('common-config', 'layouts/timer-smtp.yaml')
James E. Blaire5910202013-12-27 09:50:31 -08002826 self.sched.reconfigure(self.config)
James E. Blaire5910202013-12-27 09:50:31 -08002827
Clark Boylan3ee090a2014-04-03 20:55:09 -07002828 # The pipeline triggers every second, so we should have seen
2829 # several by now.
2830 time.sleep(5)
James E. Blaire5910202013-12-27 09:50:31 -08002831 self.waitUntilSettled()
2832
Clark Boylan3ee090a2014-04-03 20:55:09 -07002833 self.assertEqual(len(self.builds), 2)
Paul Belanger174a8272017-03-14 13:20:10 -04002834 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002835 self.waitUntilSettled()
2836 self.assertEqual(len(self.history), 2)
2837
James E. Blaire5910202013-12-27 09:50:31 -08002838 self.assertEqual(self.getJobFromHistory(
2839 'project-bitrot-stable-old').result, 'SUCCESS')
2840 self.assertEqual(self.getJobFromHistory(
2841 'project-bitrot-stable-older').result, 'SUCCESS')
2842
James E. Blairff80a2f2013-12-27 13:24:06 -08002843 self.assertEqual(len(self.smtp_messages), 1)
James E. Blaire5910202013-12-27 09:50:31 -08002844
2845 # A.messages only holds what FakeGerrit places in it. Thus we
2846 # work on the knowledge of what the first message should be as
2847 # it is only configured to go to SMTP.
2848
2849 self.assertEqual('zuul_from@example.com',
James E. Blairff80a2f2013-12-27 13:24:06 -08002850 self.smtp_messages[0]['from_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002851 self.assertEqual(['alternative_me@example.com'],
James E. Blairff80a2f2013-12-27 13:24:06 -08002852 self.smtp_messages[0]['to_email'])
James E. Blaire5910202013-12-27 09:50:31 -08002853 self.assertIn('Subject: Periodic check for org/project succeeded',
James E. Blairff80a2f2013-12-27 13:24:06 -08002854 self.smtp_messages[0]['headers'])
James E. Blaire5910202013-12-27 09:50:31 -08002855
Clark Boylan3ee090a2014-04-03 20:55:09 -07002856 # Stop queuing timer triggered jobs and let any that may have
2857 # queued through so that end of test assertions pass.
James E. Blairdfdfcfc2017-04-20 10:19:20 -07002858 self.commitConfigUpdate('common-config', 'layouts/no-timer.yaml')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002859 self.sched.reconfigure(self.config)
James E. Blairf8058972014-08-15 16:09:16 -07002860 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04002861 self.executor_server.release('.*')
Clark Boylan3ee090a2014-04-03 20:55:09 -07002862 self.waitUntilSettled()
2863
Joshua Hesketh25695cb2017-03-06 12:50:04 +11002864 @skip("Disabled for early v3 development")
Evgeny Antyshevda90a502017-02-08 08:42:59 +00002865 def test_timer_sshkey(self):
2866 "Test that a periodic job can setup SSH key authentication"
2867 self.worker.hold_jobs_in_build = True
2868 self.config.set('zuul', 'layout_config',
2869 'tests/fixtures/layout-timer.yaml')
2870 self.sched.reconfigure(self.config)
2871 self.registerJobs()
2872
2873 # The pipeline triggers every second, so we should have seen
2874 # several by now.
2875 time.sleep(5)
2876 self.waitUntilSettled()
2877
2878 self.assertEqual(len(self.builds), 2)
2879
2880 ssh_wrapper = os.path.join(self.git_root, ".ssh_wrapper_gerrit")
2881 self.assertTrue(os.path.isfile(ssh_wrapper))
2882 with open(ssh_wrapper) as f:
2883 ssh_wrapper_content = f.read()
2884 self.assertIn("fake_id_rsa", ssh_wrapper_content)
2885 # In the unit tests Merger runs in the same process,
2886 # so we see its' environment variables
2887 self.assertEqual(os.environ['GIT_SSH'], ssh_wrapper)
2888
2889 self.worker.release('.*')
2890 self.waitUntilSettled()
2891 self.assertEqual(len(self.history), 2)
2892
2893 self.assertEqual(self.getJobFromHistory(
2894 'project-bitrot-stable-old').result, 'SUCCESS')
2895 self.assertEqual(self.getJobFromHistory(
2896 'project-bitrot-stable-older').result, 'SUCCESS')
2897
2898 # Stop queuing timer triggered jobs and let any that may have
2899 # queued through so that end of test assertions pass.
2900 self.config.set('zuul', 'layout_config',
2901 'tests/fixtures/layout-no-timer.yaml')
2902 self.sched.reconfigure(self.config)
2903 self.registerJobs()
2904 self.waitUntilSettled()
2905 self.worker.release('.*')
2906 self.waitUntilSettled()
2907
James E. Blair91e34592015-07-31 16:45:59 -07002908 def test_client_enqueue_change(self):
James E. Blairad28e912013-11-27 10:43:22 -08002909 "Test that the RPC client can enqueue a change"
2910 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07002911 A.addApproval('code-review', 2)
2912 A.addApproval('approved', 1)
James E. Blairad28e912013-11-27 10:43:22 -08002913
2914 client = zuul.rpcclient.RPCClient('127.0.0.1',
2915 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002916 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04002917 r = client.enqueue(tenant='tenant-one',
2918 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002919 project='org/project',
2920 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002921 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002922 self.waitUntilSettled()
2923 self.assertEqual(self.getJobFromHistory('project-merge').result,
2924 'SUCCESS')
2925 self.assertEqual(self.getJobFromHistory('project-test1').result,
2926 'SUCCESS')
2927 self.assertEqual(self.getJobFromHistory('project-test2').result,
2928 'SUCCESS')
2929 self.assertEqual(A.data['status'], 'MERGED')
2930 self.assertEqual(A.reported, 2)
2931 self.assertEqual(r, True)
2932
James E. Blair91e34592015-07-31 16:45:59 -07002933 def test_client_enqueue_ref(self):
2934 "Test that the RPC client can enqueue a ref"
2935
2936 client = zuul.rpcclient.RPCClient('127.0.0.1',
2937 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002938 self.addCleanup(client.shutdown)
James E. Blair91e34592015-07-31 16:45:59 -07002939 r = client.enqueue_ref(
Paul Belangerbaca3132016-11-04 12:49:54 -04002940 tenant='tenant-one',
James E. Blair91e34592015-07-31 16:45:59 -07002941 pipeline='post',
2942 project='org/project',
2943 trigger='gerrit',
2944 ref='master',
2945 oldrev='90f173846e3af9154517b88543ffbd1691f31366',
2946 newrev='d479a0bfcb34da57a31adb2a595c0cf687812543')
2947 self.waitUntilSettled()
2948 job_names = [x.name for x in self.history]
2949 self.assertEqual(len(self.history), 1)
2950 self.assertIn('project-post', job_names)
2951 self.assertEqual(r, True)
2952
James E. Blairad28e912013-11-27 10:43:22 -08002953 def test_client_enqueue_negative(self):
2954 "Test that the RPC client returns errors"
2955 client = zuul.rpcclient.RPCClient('127.0.0.1',
2956 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07002957 self.addCleanup(client.shutdown)
James E. Blairad28e912013-11-27 10:43:22 -08002958 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
Paul Belangerbaca3132016-11-04 12:49:54 -04002959 "Invalid tenant"):
2960 r = client.enqueue(tenant='tenant-foo',
2961 pipeline='gate',
2962 project='org/project',
2963 trigger='gerrit',
2964 change='1,1')
Paul Belangerbaca3132016-11-04 12:49:54 -04002965 self.assertEqual(r, False)
2966
2967 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
James E. Blairad28e912013-11-27 10:43:22 -08002968 "Invalid project"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002969 r = client.enqueue(tenant='tenant-one',
2970 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002971 project='project-does-not-exist',
2972 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002973 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002974 self.assertEqual(r, False)
2975
2976 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2977 "Invalid pipeline"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002978 r = client.enqueue(tenant='tenant-one',
2979 pipeline='pipeline-does-not-exist',
James E. Blairad28e912013-11-27 10:43:22 -08002980 project='org/project',
2981 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08002982 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002983 self.assertEqual(r, False)
2984
2985 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2986 "Invalid trigger"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002987 r = client.enqueue(tenant='tenant-one',
2988 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002989 project='org/project',
2990 trigger='trigger-does-not-exist',
James E. Blair36658cf2013-12-06 17:53:48 -08002991 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08002992 self.assertEqual(r, False)
2993
2994 with testtools.ExpectedException(zuul.rpcclient.RPCFailure,
2995 "Invalid change"):
Paul Belangerbaca3132016-11-04 12:49:54 -04002996 r = client.enqueue(tenant='tenant-one',
2997 pipeline='gate',
James E. Blairad28e912013-11-27 10:43:22 -08002998 project='org/project',
2999 trigger='gerrit',
James E. Blair36658cf2013-12-06 17:53:48 -08003000 change='1,1')
James E. Blairad28e912013-11-27 10:43:22 -08003001 self.assertEqual(r, False)
3002
3003 self.waitUntilSettled()
3004 self.assertEqual(len(self.history), 0)
3005 self.assertEqual(len(self.builds), 0)
James E. Blair36658cf2013-12-06 17:53:48 -08003006
3007 def test_client_promote(self):
3008 "Test that the RPC client can promote a change"
Paul Belanger174a8272017-03-14 13:20:10 -04003009 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003010 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3011 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3012 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003013 A.addApproval('code-review', 2)
3014 B.addApproval('code-review', 2)
3015 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003016
James E. Blair8b5408c2016-08-08 15:37:46 -07003017 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3018 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3019 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003020
3021 self.waitUntilSettled()
3022
Paul Belangerbaca3132016-11-04 12:49:54 -04003023 tenant = self.sched.abide.tenants.get('tenant-one')
3024 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003025 enqueue_times = {}
3026 for item in items:
3027 enqueue_times[str(item.change)] = item.enqueue_time
3028
James E. Blair36658cf2013-12-06 17:53:48 -08003029 client = zuul.rpcclient.RPCClient('127.0.0.1',
3030 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003031 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003032 r = client.promote(tenant='tenant-one',
3033 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003034 change_ids=['2,1', '3,1'])
3035
Sean Daguef39b9ca2014-01-10 21:34:35 -05003036 # ensure that enqueue times are durable
Paul Belangerbaca3132016-11-04 12:49:54 -04003037 items = tenant.layout.pipelines['gate'].getAllItems()
Sean Daguef39b9ca2014-01-10 21:34:35 -05003038 for item in items:
3039 self.assertEqual(
3040 enqueue_times[str(item.change)], item.enqueue_time)
3041
James E. Blair78acec92014-02-06 07:11:32 -08003042 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003043 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003044 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003045 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003046 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003047 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003048 self.waitUntilSettled()
3049
3050 self.assertEqual(len(self.builds), 6)
3051 self.assertEqual(self.builds[0].name, 'project-test1')
3052 self.assertEqual(self.builds[1].name, 'project-test2')
3053 self.assertEqual(self.builds[2].name, 'project-test1')
3054 self.assertEqual(self.builds[3].name, 'project-test2')
3055 self.assertEqual(self.builds[4].name, 'project-test1')
3056 self.assertEqual(self.builds[5].name, 'project-test2')
3057
Paul Belangerbaca3132016-11-04 12:49:54 -04003058 self.assertTrue(self.builds[0].hasChanges(B))
3059 self.assertFalse(self.builds[0].hasChanges(A))
3060 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003061
Paul Belangerbaca3132016-11-04 12:49:54 -04003062 self.assertTrue(self.builds[2].hasChanges(B))
3063 self.assertTrue(self.builds[2].hasChanges(C))
3064 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003065
Paul Belangerbaca3132016-11-04 12:49:54 -04003066 self.assertTrue(self.builds[4].hasChanges(B))
3067 self.assertTrue(self.builds[4].hasChanges(C))
3068 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003069
Paul Belanger174a8272017-03-14 13:20:10 -04003070 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003071 self.waitUntilSettled()
3072
3073 self.assertEqual(A.data['status'], 'MERGED')
3074 self.assertEqual(A.reported, 2)
3075 self.assertEqual(B.data['status'], 'MERGED')
3076 self.assertEqual(B.reported, 2)
3077 self.assertEqual(C.data['status'], 'MERGED')
3078 self.assertEqual(C.reported, 2)
3079
James E. Blair36658cf2013-12-06 17:53:48 -08003080 self.assertEqual(r, True)
3081
3082 def test_client_promote_dependent(self):
3083 "Test that the RPC client can promote a dependent change"
3084 # C (depends on B) -> B -> A ; then promote C to get:
3085 # A -> C (depends on B) -> B
Paul Belanger174a8272017-03-14 13:20:10 -04003086 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003087 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3088 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3089 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3090
3091 C.setDependsOn(B, 1)
3092
James E. Blair8b5408c2016-08-08 15:37:46 -07003093 A.addApproval('code-review', 2)
3094 B.addApproval('code-review', 2)
3095 C.addApproval('code-review', 2)
James E. Blair36658cf2013-12-06 17:53:48 -08003096
James E. Blair8b5408c2016-08-08 15:37:46 -07003097 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3098 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3099 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003100
3101 self.waitUntilSettled()
3102
3103 client = zuul.rpcclient.RPCClient('127.0.0.1',
3104 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003105 self.addCleanup(client.shutdown)
Paul Belangerbaca3132016-11-04 12:49:54 -04003106 r = client.promote(tenant='tenant-one',
3107 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003108 change_ids=['3,1'])
3109
James E. Blair78acec92014-02-06 07:11:32 -08003110 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003111 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003112 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003113 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003114 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003115 self.executor_server.release('.*-merge')
James E. Blair36658cf2013-12-06 17:53:48 -08003116 self.waitUntilSettled()
3117
3118 self.assertEqual(len(self.builds), 6)
3119 self.assertEqual(self.builds[0].name, 'project-test1')
3120 self.assertEqual(self.builds[1].name, 'project-test2')
3121 self.assertEqual(self.builds[2].name, 'project-test1')
3122 self.assertEqual(self.builds[3].name, 'project-test2')
3123 self.assertEqual(self.builds[4].name, 'project-test1')
3124 self.assertEqual(self.builds[5].name, 'project-test2')
3125
Paul Belangerbaca3132016-11-04 12:49:54 -04003126 self.assertTrue(self.builds[0].hasChanges(B))
3127 self.assertFalse(self.builds[0].hasChanges(A))
3128 self.assertFalse(self.builds[0].hasChanges(C))
James E. Blair36658cf2013-12-06 17:53:48 -08003129
Paul Belangerbaca3132016-11-04 12:49:54 -04003130 self.assertTrue(self.builds[2].hasChanges(B))
3131 self.assertTrue(self.builds[2].hasChanges(C))
3132 self.assertFalse(self.builds[2].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003133
Paul Belangerbaca3132016-11-04 12:49:54 -04003134 self.assertTrue(self.builds[4].hasChanges(B))
3135 self.assertTrue(self.builds[4].hasChanges(C))
3136 self.assertTrue(self.builds[4].hasChanges(A))
James E. Blair36658cf2013-12-06 17:53:48 -08003137
Paul Belanger174a8272017-03-14 13:20:10 -04003138 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003139 self.waitUntilSettled()
3140
3141 self.assertEqual(A.data['status'], 'MERGED')
3142 self.assertEqual(A.reported, 2)
3143 self.assertEqual(B.data['status'], 'MERGED')
3144 self.assertEqual(B.reported, 2)
3145 self.assertEqual(C.data['status'], 'MERGED')
3146 self.assertEqual(C.reported, 2)
3147
James E. Blair36658cf2013-12-06 17:53:48 -08003148 self.assertEqual(r, True)
3149
3150 def test_client_promote_negative(self):
3151 "Test that the RPC client returns errors for promotion"
Paul Belanger174a8272017-03-14 13:20:10 -04003152 self.executor_server.hold_jobs_in_build = True
James E. Blair36658cf2013-12-06 17:53:48 -08003153 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003154 A.addApproval('code-review', 2)
3155 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair36658cf2013-12-06 17:53:48 -08003156 self.waitUntilSettled()
3157
3158 client = zuul.rpcclient.RPCClient('127.0.0.1',
3159 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003160 self.addCleanup(client.shutdown)
James E. Blair36658cf2013-12-06 17:53:48 -08003161
3162 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003163 r = client.promote(tenant='tenant-one',
3164 pipeline='nonexistent',
James E. Blair36658cf2013-12-06 17:53:48 -08003165 change_ids=['2,1', '3,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003166 self.assertEqual(r, False)
3167
3168 with testtools.ExpectedException(zuul.rpcclient.RPCFailure):
Paul Belangerbaca3132016-11-04 12:49:54 -04003169 r = client.promote(tenant='tenant-one',
3170 pipeline='gate',
James E. Blair36658cf2013-12-06 17:53:48 -08003171 change_ids=['4,1'])
James E. Blair36658cf2013-12-06 17:53:48 -08003172 self.assertEqual(r, False)
3173
Paul Belanger174a8272017-03-14 13:20:10 -04003174 self.executor_server.hold_jobs_in_build = False
3175 self.executor_server.release()
James E. Blair36658cf2013-12-06 17:53:48 -08003176 self.waitUntilSettled()
Clark Boylan7603a372014-01-21 11:43:20 -08003177
James E. Blaircc324192017-04-19 14:36:08 -07003178 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003179 def test_queue_rate_limiting(self):
3180 "Test that DependentPipelines are rate limited with dep across window"
Paul Belanger174a8272017-03-14 13:20:10 -04003181 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003182 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3183 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3184 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3185
3186 C.setDependsOn(B, 1)
Paul Belanger174a8272017-03-14 13:20:10 -04003187 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003188
James E. Blair8b5408c2016-08-08 15:37:46 -07003189 A.addApproval('code-review', 2)
3190 B.addApproval('code-review', 2)
3191 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003192
James E. Blair8b5408c2016-08-08 15:37:46 -07003193 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3194 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3195 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Clark Boylan7603a372014-01-21 11:43:20 -08003196 self.waitUntilSettled()
3197
3198 # Only A and B will have their merge jobs queued because
3199 # window is 2.
3200 self.assertEqual(len(self.builds), 2)
3201 self.assertEqual(self.builds[0].name, 'project-merge')
3202 self.assertEqual(self.builds[1].name, 'project-merge')
3203
James E. Blair732ee862017-04-21 07:49:26 -07003204 # Release the merge jobs one at a time.
3205 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003206 self.waitUntilSettled()
James E. Blair732ee862017-04-21 07:49:26 -07003207 self.builds[0].release()
Clark Boylan7603a372014-01-21 11:43:20 -08003208 self.waitUntilSettled()
3209
3210 # Only A and B will have their test jobs queued because
3211 # window is 2.
3212 self.assertEqual(len(self.builds), 4)
3213 self.assertEqual(self.builds[0].name, 'project-test1')
3214 self.assertEqual(self.builds[1].name, 'project-test2')
3215 self.assertEqual(self.builds[2].name, 'project-test1')
3216 self.assertEqual(self.builds[3].name, 'project-test2')
3217
Paul Belanger174a8272017-03-14 13:20:10 -04003218 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003219 self.waitUntilSettled()
3220
James E. Blaircc324192017-04-19 14:36:08 -07003221 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrumb01f1ca2017-04-02 10:19:52 -07003222 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003223 # A failed so window is reduced by 1 to 1.
3224 self.assertEqual(queue.window, 1)
3225 self.assertEqual(queue.window_floor, 1)
3226 self.assertEqual(A.data['status'], 'NEW')
3227
3228 # Gate is reset and only B's merge job is queued because
3229 # window shrunk to 1.
3230 self.assertEqual(len(self.builds), 1)
3231 self.assertEqual(self.builds[0].name, 'project-merge')
3232
Paul Belanger174a8272017-03-14 13:20:10 -04003233 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003234 self.waitUntilSettled()
3235
3236 # Only B's test jobs are queued because window is still 1.
3237 self.assertEqual(len(self.builds), 2)
3238 self.assertEqual(self.builds[0].name, 'project-test1')
3239 self.assertEqual(self.builds[1].name, 'project-test2')
3240
Paul Belanger174a8272017-03-14 13:20:10 -04003241 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003242 self.waitUntilSettled()
3243
3244 # B was successfully merged so window is increased to 2.
3245 self.assertEqual(queue.window, 2)
3246 self.assertEqual(queue.window_floor, 1)
3247 self.assertEqual(B.data['status'], 'MERGED')
3248
3249 # Only C is left and its merge job is queued.
3250 self.assertEqual(len(self.builds), 1)
3251 self.assertEqual(self.builds[0].name, 'project-merge')
3252
Paul Belanger174a8272017-03-14 13:20:10 -04003253 self.executor_server.release('.*-merge')
Clark Boylan7603a372014-01-21 11:43:20 -08003254 self.waitUntilSettled()
3255
3256 # After successful merge job the test jobs for C are queued.
3257 self.assertEqual(len(self.builds), 2)
3258 self.assertEqual(self.builds[0].name, 'project-test1')
3259 self.assertEqual(self.builds[1].name, 'project-test2')
3260
Paul Belanger174a8272017-03-14 13:20:10 -04003261 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003262 self.waitUntilSettled()
3263
3264 # C successfully merged so window is bumped to 3.
3265 self.assertEqual(queue.window, 3)
3266 self.assertEqual(queue.window_floor, 1)
3267 self.assertEqual(C.data['status'], 'MERGED')
3268
James E. Blaircc324192017-04-19 14:36:08 -07003269 @simple_layout('layouts/rate-limit.yaml')
Clark Boylan7603a372014-01-21 11:43:20 -08003270 def test_queue_rate_limiting_dependent(self):
3271 "Test that DependentPipelines are rate limited with dep in window"
Paul Belanger174a8272017-03-14 13:20:10 -04003272 self.executor_server.hold_jobs_in_build = True
Clark Boylan7603a372014-01-21 11:43:20 -08003273 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3274 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3275 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3276
3277 B.setDependsOn(A, 1)
3278
Paul Belanger174a8272017-03-14 13:20:10 -04003279 self.executor_server.failJob('project-test1', A)
Clark Boylan7603a372014-01-21 11:43:20 -08003280
James E. Blair8b5408c2016-08-08 15:37:46 -07003281 A.addApproval('code-review', 2)
3282 B.addApproval('code-review', 2)
3283 C.addApproval('code-review', 2)
Clark Boylan7603a372014-01-21 11:43:20 -08003284
James E. Blair8b5408c2016-08-08 15:37:46 -07003285 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))
Clark Boylan7603a372014-01-21 11:43:20 -08003288 self.waitUntilSettled()
3289
3290 # Only A and B will have their merge jobs queued because
3291 # window is 2.
3292 self.assertEqual(len(self.builds), 2)
3293 self.assertEqual(self.builds[0].name, 'project-merge')
3294 self.assertEqual(self.builds[1].name, 'project-merge')
3295
James E. Blairf4a5f022017-04-18 14:01:10 -07003296 self.orderedRelease(2)
Clark Boylan7603a372014-01-21 11:43:20 -08003297
3298 # Only A and B will have their test jobs queued because
3299 # window is 2.
3300 self.assertEqual(len(self.builds), 4)
3301 self.assertEqual(self.builds[0].name, 'project-test1')
3302 self.assertEqual(self.builds[1].name, 'project-test2')
3303 self.assertEqual(self.builds[2].name, 'project-test1')
3304 self.assertEqual(self.builds[3].name, 'project-test2')
3305
Paul Belanger174a8272017-03-14 13:20:10 -04003306 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003307 self.waitUntilSettled()
3308
James E. Blaircc324192017-04-19 14:36:08 -07003309 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum14ab6ca2017-04-03 10:15:10 -07003310 queue = tenant.layout.pipelines['gate'].queues[0]
Clark Boylan7603a372014-01-21 11:43:20 -08003311 # A failed so window is reduced by 1 to 1.
3312 self.assertEqual(queue.window, 1)
3313 self.assertEqual(queue.window_floor, 1)
3314 self.assertEqual(A.data['status'], 'NEW')
3315 self.assertEqual(B.data['status'], 'NEW')
3316
3317 # Gate is reset and only C's merge job is queued because
3318 # window shrunk to 1 and A and B were dequeued.
3319 self.assertEqual(len(self.builds), 1)
3320 self.assertEqual(self.builds[0].name, 'project-merge')
3321
James E. Blairf4a5f022017-04-18 14:01:10 -07003322 self.orderedRelease(1)
Clark Boylan7603a372014-01-21 11:43:20 -08003323
3324 # Only C's test jobs are queued because window is still 1.
3325 self.assertEqual(len(self.builds), 2)
3326 self.assertEqual(self.builds[0].name, 'project-test1')
3327 self.assertEqual(self.builds[1].name, 'project-test2')
3328
Paul Belanger174a8272017-03-14 13:20:10 -04003329 self.executor_server.release('project-.*')
Clark Boylan7603a372014-01-21 11:43:20 -08003330 self.waitUntilSettled()
3331
3332 # C was successfully merged so window is increased to 2.
3333 self.assertEqual(queue.window, 2)
3334 self.assertEqual(queue.window_floor, 1)
3335 self.assertEqual(C.data['status'], 'MERGED')
Joshua Heskethba8776a2014-01-12 14:35:40 +08003336
3337 def test_worker_update_metadata(self):
3338 "Test if a worker can send back metadata about itself"
Paul Belanger174a8272017-03-14 13:20:10 -04003339 self.executor_server.hold_jobs_in_build = True
Joshua Heskethba8776a2014-01-12 14:35:40 +08003340
3341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003342 A.addApproval('code-review', 2)
3343 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethba8776a2014-01-12 14:35:40 +08003344 self.waitUntilSettled()
3345
Clint Byrumd4de79a2017-05-27 11:39:13 -07003346 self.assertEqual(len(self.executor_client.builds), 1)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003347
3348 self.log.debug('Current builds:')
Clint Byrumd4de79a2017-05-27 11:39:13 -07003349 self.log.debug(self.executor_client.builds)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003350
3351 start = time.time()
3352 while True:
3353 if time.time() - start > 10:
3354 raise Exception("Timeout waiting for gearman server to report "
3355 + "back to the client")
Clint Byrumd4de79a2017-05-27 11:39:13 -07003356 build = list(self.executor_client.builds.values())[0]
Joshua Heskethba8776a2014-01-12 14:35:40 +08003357 if build.worker.name == "My Worker":
3358 break
3359 else:
3360 time.sleep(0)
3361
3362 self.log.debug(build)
3363 self.assertEqual("My Worker", build.worker.name)
Joshua Heskethba8776a2014-01-12 14:35:40 +08003364
Paul Belanger174a8272017-03-14 13:20:10 -04003365 self.executor_server.hold_jobs_in_build = False
3366 self.executor_server.release()
Joshua Heskethba8776a2014-01-12 14:35:40 +08003367 self.waitUntilSettled()
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003368
James E. Blaircc324192017-04-19 14:36:08 -07003369 @simple_layout('layouts/footer-message.yaml')
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003370 def test_footer_message(self):
3371 "Test a pipeline's footer message is correctly added to the report."
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003372 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003373 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003374 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003375 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003376 self.waitUntilSettled()
3377
3378 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003379 B.addApproval('code-review', 2)
3380 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003381 self.waitUntilSettled()
3382
3383 self.assertEqual(2, len(self.smtp_messages))
3384
Adam Gandelman82258fd2017-02-07 16:51:58 -08003385 failure_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003386Build failed. For information on how to proceed, see \
Adam Gandelman82258fd2017-02-07 16:51:58 -08003387http://wiki.example.org/Test_Failures"""
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003388
Adam Gandelman82258fd2017-02-07 16:51:58 -08003389 footer_msg = """\
Joshua Hesketh3979e3e2014-03-04 11:21:10 +11003390For CI problems and help debugging, contact ci@example.org"""
3391
Adam Gandelman82258fd2017-02-07 16:51:58 -08003392 self.assertTrue(self.smtp_messages[0]['body'].startswith(failure_msg))
3393 self.assertTrue(self.smtp_messages[0]['body'].endswith(footer_msg))
3394 self.assertFalse(self.smtp_messages[1]['body'].startswith(failure_msg))
3395 self.assertTrue(self.smtp_messages[1]['body'].endswith(footer_msg))
Joshua Heskethb7179772014-01-30 23:30:46 +11003396
Tobias Henkel9842bd72017-05-16 13:40:03 +02003397 @simple_layout('layouts/unmanaged-project.yaml')
3398 def test_unmanaged_project_start_message(self):
3399 "Test start reporting is not done for unmanaged projects."
3400 self.init_repo("org/project", tag='init')
3401 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3402 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3403 self.waitUntilSettled()
3404
3405 self.assertEqual(0, len(A.messages))
3406
Clint Byrumb9de8be2017-05-27 13:49:19 -07003407 @simple_layout('layouts/merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003408 def test_merge_failure_reporters(self):
3409 """Check that the config is set up correctly"""
3410
Clint Byrumb9de8be2017-05-27 13:49:19 -07003411 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethb7179772014-01-30 23:30:46 +11003412 self.assertEqual(
Jeremy Stanley1c2c3c22015-06-15 21:23:19 +00003413 "Merge Failed.\n\nThis change or one of its cross-repo "
3414 "dependencies was unable to be automatically merged with the "
3415 "current state of its repository. Please rebase the change and "
3416 "upload a new patchset.",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003417 tenant.layout.pipelines['check'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003418 self.assertEqual(
3419 "The merge failed! For more information...",
Clint Byrumb9de8be2017-05-27 13:49:19 -07003420 tenant.layout.pipelines['gate'].merge_failure_message)
Joshua Heskethb7179772014-01-30 23:30:46 +11003421
3422 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003423 len(tenant.layout.pipelines['check'].merge_failure_actions), 1)
Joshua Heskethb7179772014-01-30 23:30:46 +11003424 self.assertEqual(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003425 len(tenant.layout.pipelines['gate'].merge_failure_actions), 2)
Joshua Heskethb7179772014-01-30 23:30:46 +11003426
3427 self.assertTrue(isinstance(
Clint Byrumb9de8be2017-05-27 13:49:19 -07003428 tenant.layout.pipelines['check'].merge_failure_actions[0],
3429 gerritreporter.GerritReporter))
Joshua Heskethb7179772014-01-30 23:30:46 +11003430
3431 self.assertTrue(
3432 (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003433 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003434 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003435 zuul.driver.smtp.smtpreporter.SMTPReporter) and
3436 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003437 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003438 gerritreporter.GerritReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003439 ) or (
Clint Byrumb9de8be2017-05-27 13:49:19 -07003440 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003441 merge_failure_actions[0],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003442 gerritreporter.GerritReporter) and
3443 isinstance(tenant.layout.pipelines['gate'].
Joshua Heskethde958652015-11-10 19:19:50 +11003444 merge_failure_actions[1],
Clint Byrumb9de8be2017-05-27 13:49:19 -07003445 zuul.driver.smtp.smtpreporter.SMTPReporter)
Joshua Heskethb7179772014-01-30 23:30:46 +11003446 )
3447 )
3448
James E. Blairec056492016-07-22 09:45:56 -07003449 @skip("Disabled for early v3 development")
Joshua Heskethb7179772014-01-30 23:30:46 +11003450 def test_merge_failure_reports(self):
3451 """Check that when a change fails to merge the correct message is sent
3452 to the correct reporter"""
James E. Blairf84026c2015-12-08 16:11:46 -08003453 self.updateConfigLayout(
3454 'tests/fixtures/layout-merge-failure.yaml')
Joshua Heskethb7179772014-01-30 23:30:46 +11003455 self.sched.reconfigure(self.config)
3456 self.registerJobs()
3457
3458 # Check a test failure isn't reported to SMTP
3459 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003460 A.addApproval('code-review', 2)
Paul Belanger174a8272017-03-14 13:20:10 -04003461 self.executor_server.failJob('project-test1', A)
James E. Blair8b5408c2016-08-08 15:37:46 -07003462 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003463 self.waitUntilSettled()
3464
3465 self.assertEqual(3, len(self.history)) # 3 jobs
3466 self.assertEqual(0, len(self.smtp_messages))
3467
3468 # Check a merge failure is reported to SMTP
3469 # B should be merged, but C will conflict with B
3470 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3471 B.addPatchset(['conflict'])
3472 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
3473 C.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003474 B.addApproval('code-review', 2)
3475 C.addApproval('code-review', 2)
3476 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
3477 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
Joshua Heskethb7179772014-01-30 23:30:46 +11003478 self.waitUntilSettled()
3479
3480 self.assertEqual(6, len(self.history)) # A and B jobs
3481 self.assertEqual(1, len(self.smtp_messages))
3482 self.assertEqual('The merge failed! For more information...',
3483 self.smtp_messages[0]['body'])
Joshua Hesketh36c3fa52014-01-22 11:40:52 +11003484
James E. Blairec056492016-07-22 09:45:56 -07003485 @skip("Disabled for early v3 development")
James E. Blairf760f0e2016-02-09 08:44:52 -08003486 def test_default_merge_failure_reports(self):
3487 """Check that the default merge failure reports are correct."""
3488
3489 # A should report success, B should report merge failure.
3490 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3491 A.addPatchset(['conflict'])
3492 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
3493 B.addPatchset(['conflict'])
James E. Blair8b5408c2016-08-08 15:37:46 -07003494 A.addApproval('code-review', 2)
3495 B.addApproval('code-review', 2)
3496 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
3497 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blairf760f0e2016-02-09 08:44:52 -08003498 self.waitUntilSettled()
3499
3500 self.assertEqual(3, len(self.history)) # A jobs
3501 self.assertEqual(A.reported, 2)
3502 self.assertEqual(B.reported, 2)
3503 self.assertEqual(A.data['status'], 'MERGED')
3504 self.assertEqual(B.data['status'], 'NEW')
3505 self.assertIn('Build succeeded', A.messages[1])
3506 self.assertIn('Merge Failed', B.messages[1])
3507 self.assertIn('automatically merged', B.messages[1])
3508 self.assertNotIn('logs.example.com', B.messages[1])
3509 self.assertNotIn('SKIPPED', B.messages[1])
3510
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003511 def test_client_get_running_jobs(self):
3512 "Test that the RPC client can get a list of running jobs"
Paul Belanger174a8272017-03-14 13:20:10 -04003513 self.executor_server.hold_jobs_in_build = True
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003514 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -07003515 A.addApproval('code-review', 2)
3516 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003517 self.waitUntilSettled()
3518
3519 client = zuul.rpcclient.RPCClient('127.0.0.1',
3520 self.gearman_server.port)
Clark Boylandf4eba22017-04-24 17:37:58 -07003521 self.addCleanup(client.shutdown)
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003522
3523 # Wait for gearman server to send the initial workData back to zuul
3524 start = time.time()
3525 while True:
3526 if time.time() - start > 10:
3527 raise Exception("Timeout waiting for gearman server to report "
3528 + "back to the client")
Clint Byrum1d0c7d12017-05-10 19:40:53 -07003529 build = list(self.executor_client.builds.values())[0]
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003530 if build.worker.name == "My Worker":
3531 break
3532 else:
3533 time.sleep(0)
3534
3535 running_items = client.get_running_jobs()
3536
3537 self.assertEqual(1, len(running_items))
3538 running_item = running_items[0]
3539 self.assertEqual([], running_item['failing_reasons'])
3540 self.assertEqual([], running_item['items_behind'])
3541 self.assertEqual('https://hostname/1', running_item['url'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003542 self.assertIsNone(running_item['item_ahead'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003543 self.assertEqual('org/project', running_item['project'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003544 self.assertIsNone(running_item['remaining_time'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003545 self.assertEqual(True, running_item['active'])
3546 self.assertEqual('1,1', running_item['id'])
3547
3548 self.assertEqual(3, len(running_item['jobs']))
3549 for job in running_item['jobs']:
3550 if job['name'] == 'project-merge':
3551 self.assertEqual('project-merge', job['name'])
3552 self.assertEqual('gate', job['pipeline'])
3553 self.assertEqual(False, job['retry'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003554 self.assertEqual('https://server/job/project-merge/0/',
3555 job['url'])
Clint Byrumd4de79a2017-05-27 11:39:13 -07003556 self.assertEqual(2, len(job['worker']))
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003557 self.assertEqual(False, job['canceled'])
3558 self.assertEqual(True, job['voting'])
Monty Taylor38b553a2017-06-05 13:06:10 -05003559 self.assertIsNone(job['result'])
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003560 self.assertEqual('gate', job['pipeline'])
3561 break
3562
Paul Belanger174a8272017-03-14 13:20:10 -04003563 self.executor_server.hold_jobs_in_build = False
3564 self.executor_server.release()
Joshua Hesketh85af4e92014-02-21 08:28:58 -08003565 self.waitUntilSettled()
3566
3567 running_items = client.get_running_jobs()
3568 self.assertEqual(0, len(running_items))
James E. Blairbadc1ad2014-04-28 13:55:14 -07003569
James E. Blairb70e55a2017-04-19 12:57:02 -07003570 @simple_layout('layouts/nonvoting-pipeline.yaml')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003571 def test_nonvoting_pipeline(self):
3572 "Test that a nonvoting pipeline (experimental) can still report"
3573
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003574 A = self.fake_gerrit.addFakeChange('org/experimental-project',
3575 'master', 'A')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003576 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3577 self.waitUntilSettled()
Paul Belanger15e3e202016-10-14 16:27:34 -04003578
3579 self.assertEqual(self.getJobFromHistory('project-merge').result,
3580 'SUCCESS')
Joshua Heskethcc017ea2014-04-30 19:55:25 +10003581 self.assertEqual(
3582 self.getJobFromHistory('experimental-project-test').result,
3583 'SUCCESS')
James E. Blairbadc1ad2014-04-28 13:55:14 -07003584 self.assertEqual(A.reported, 1)
James E. Blair5ee24252014-12-30 10:12:29 -08003585
3586 def test_crd_gate(self):
3587 "Test cross-repo dependencies"
3588 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3589 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003590 A.addApproval('code-review', 2)
3591 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003592
3593 AM2 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM2')
3594 AM1 = self.fake_gerrit.addFakeChange('org/project1', 'master', 'AM1')
3595 AM2.setMerged()
3596 AM1.setMerged()
3597
3598 BM2 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM2')
3599 BM1 = self.fake_gerrit.addFakeChange('org/project2', 'master', 'BM1')
3600 BM2.setMerged()
3601 BM1.setMerged()
3602
3603 # A -> AM1 -> AM2
3604 # B -> BM1 -> BM2
3605 # A Depends-On: B
3606 # M2 is here to make sure it is never queried. If it is, it
3607 # means zuul is walking down the entire history of merged
3608 # changes.
3609
3610 B.setDependsOn(BM1, 1)
3611 BM1.setDependsOn(BM2, 1)
3612
3613 A.setDependsOn(AM1, 1)
3614 AM1.setDependsOn(AM2, 1)
3615
3616 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3617 A.subject, B.data['id'])
3618
James E. Blair8b5408c2016-08-08 15:37:46 -07003619 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003620 self.waitUntilSettled()
3621
3622 self.assertEqual(A.data['status'], 'NEW')
3623 self.assertEqual(B.data['status'], 'NEW')
3624
Paul Belanger5dccbe72016-11-14 11:17:37 -05003625 for connection in self.connections.connections.values():
Joshua Hesketh4bd7da32016-02-17 20:58:47 +11003626 connection.maintainCache([])
James E. Blair5ee24252014-12-30 10:12:29 -08003627
Paul Belanger174a8272017-03-14 13:20:10 -04003628 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003629 B.addApproval('approved', 1)
3630 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003631 self.waitUntilSettled()
3632
Paul Belanger174a8272017-03-14 13:20:10 -04003633 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003634 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003635 self.executor_server.release('.*-merge')
James E. Blair5ee24252014-12-30 10:12:29 -08003636 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003637 self.executor_server.hold_jobs_in_build = False
3638 self.executor_server.release()
James E. Blair5ee24252014-12-30 10:12:29 -08003639 self.waitUntilSettled()
3640
3641 self.assertEqual(AM2.queried, 0)
3642 self.assertEqual(BM2.queried, 0)
3643 self.assertEqual(A.data['status'], 'MERGED')
3644 self.assertEqual(B.data['status'], 'MERGED')
3645 self.assertEqual(A.reported, 2)
3646 self.assertEqual(B.reported, 2)
3647
Paul Belanger5dccbe72016-11-14 11:17:37 -05003648 changes = self.getJobFromHistory(
3649 'project-merge', 'org/project1').changes
3650 self.assertEqual(changes, '2,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003651
3652 def test_crd_branch(self):
3653 "Test cross-repo dependencies in multiple branches"
Paul Belanger6379db12016-11-14 13:57:54 -05003654
3655 self.create_branch('org/project2', 'mp')
James E. Blair8f78d882015-02-05 08:51:37 -08003656 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3657 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3658 C = self.fake_gerrit.addFakeChange('org/project2', 'mp', 'C')
3659 C.data['id'] = B.data['id']
James E. Blair8b5408c2016-08-08 15:37:46 -07003660 A.addApproval('code-review', 2)
3661 B.addApproval('code-review', 2)
3662 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003663
3664 # A Depends-On: B+C
3665 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3666 A.subject, B.data['id'])
3667
Paul Belanger174a8272017-03-14 13:20:10 -04003668 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003669 B.addApproval('approved', 1)
3670 C.addApproval('approved', 1)
3671 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003672 self.waitUntilSettled()
3673
Paul Belanger174a8272017-03-14 13:20:10 -04003674 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003675 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003676 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003677 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003678 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003679 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003680 self.executor_server.hold_jobs_in_build = False
3681 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003682 self.waitUntilSettled()
3683
3684 self.assertEqual(A.data['status'], 'MERGED')
3685 self.assertEqual(B.data['status'], 'MERGED')
3686 self.assertEqual(C.data['status'], 'MERGED')
3687 self.assertEqual(A.reported, 2)
3688 self.assertEqual(B.reported, 2)
3689 self.assertEqual(C.reported, 2)
3690
Paul Belanger6379db12016-11-14 13:57:54 -05003691 changes = self.getJobFromHistory(
3692 'project-merge', 'org/project1').changes
3693 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair8f78d882015-02-05 08:51:37 -08003694
3695 def test_crd_multiline(self):
3696 "Test multiple depends-on lines in commit"
3697 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3698 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3699 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
James E. Blair8b5408c2016-08-08 15:37:46 -07003700 A.addApproval('code-review', 2)
3701 B.addApproval('code-review', 2)
3702 C.addApproval('code-review', 2)
James E. Blair8f78d882015-02-05 08:51:37 -08003703
3704 # A Depends-On: B+C
3705 A.data['commitMessage'] = '%s\n\nDepends-On: %s\nDepends-On: %s\n' % (
3706 A.subject, B.data['id'], C.data['id'])
3707
Paul Belanger174a8272017-03-14 13:20:10 -04003708 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003709 B.addApproval('approved', 1)
3710 C.addApproval('approved', 1)
3711 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8f78d882015-02-05 08:51:37 -08003712 self.waitUntilSettled()
3713
Paul Belanger174a8272017-03-14 13:20:10 -04003714 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003715 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003716 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003717 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003718 self.executor_server.release('.*-merge')
James E. Blair8f78d882015-02-05 08:51:37 -08003719 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003720 self.executor_server.hold_jobs_in_build = False
3721 self.executor_server.release()
James E. Blair8f78d882015-02-05 08:51:37 -08003722 self.waitUntilSettled()
3723
3724 self.assertEqual(A.data['status'], 'MERGED')
3725 self.assertEqual(B.data['status'], 'MERGED')
3726 self.assertEqual(C.data['status'], 'MERGED')
3727 self.assertEqual(A.reported, 2)
3728 self.assertEqual(B.reported, 2)
3729 self.assertEqual(C.reported, 2)
3730
Paul Belanger5dccbe72016-11-14 11:17:37 -05003731 changes = self.getJobFromHistory(
3732 'project-merge', 'org/project1').changes
3733 self.assertEqual(changes, '2,1 3,1 1,1')
James E. Blair5ee24252014-12-30 10:12:29 -08003734
3735 def test_crd_unshared_gate(self):
3736 "Test cross-repo dependencies in unshared gate queues"
3737 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3738 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003739 A.addApproval('code-review', 2)
3740 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003741
3742 # A Depends-On: B
3743 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3744 A.subject, B.data['id'])
3745
3746 # A and B do not share a queue, make sure that A is unable to
3747 # enqueue B (and therefore, A is unable to be enqueued).
James E. Blair8b5408c2016-08-08 15:37:46 -07003748 B.addApproval('approved', 1)
3749 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003750 self.waitUntilSettled()
3751
3752 self.assertEqual(A.data['status'], 'NEW')
3753 self.assertEqual(B.data['status'], 'NEW')
3754 self.assertEqual(A.reported, 0)
3755 self.assertEqual(B.reported, 0)
3756 self.assertEqual(len(self.history), 0)
3757
3758 # Enqueue and merge B alone.
James E. Blair8b5408c2016-08-08 15:37:46 -07003759 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003760 self.waitUntilSettled()
3761
3762 self.assertEqual(B.data['status'], 'MERGED')
3763 self.assertEqual(B.reported, 2)
3764
3765 # Now that B is merged, A should be able to be enqueued and
3766 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003767 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003768 self.waitUntilSettled()
3769
3770 self.assertEqual(A.data['status'], 'MERGED')
3771 self.assertEqual(A.reported, 2)
3772
James E. Blair96698e22015-04-02 07:48:21 -07003773 def test_crd_gate_reverse(self):
3774 "Test reverse cross-repo dependencies"
3775 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3776 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003777 A.addApproval('code-review', 2)
3778 B.addApproval('code-review', 2)
James E. Blair96698e22015-04-02 07:48:21 -07003779
3780 # A Depends-On: B
3781
3782 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3783 A.subject, B.data['id'])
3784
James E. Blair8b5408c2016-08-08 15:37:46 -07003785 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003786 self.waitUntilSettled()
3787
3788 self.assertEqual(A.data['status'], 'NEW')
3789 self.assertEqual(B.data['status'], 'NEW')
3790
Paul Belanger174a8272017-03-14 13:20:10 -04003791 self.executor_server.hold_jobs_in_build = True
James E. Blair8b5408c2016-08-08 15:37:46 -07003792 A.addApproval('approved', 1)
3793 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96698e22015-04-02 07:48:21 -07003794 self.waitUntilSettled()
3795
Paul Belanger174a8272017-03-14 13:20:10 -04003796 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003797 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003798 self.executor_server.release('.*-merge')
James E. Blair96698e22015-04-02 07:48:21 -07003799 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04003800 self.executor_server.hold_jobs_in_build = False
3801 self.executor_server.release()
James E. Blair96698e22015-04-02 07:48:21 -07003802 self.waitUntilSettled()
3803
3804 self.assertEqual(A.data['status'], 'MERGED')
3805 self.assertEqual(B.data['status'], 'MERGED')
3806 self.assertEqual(A.reported, 2)
3807 self.assertEqual(B.reported, 2)
3808
Paul Belanger5dccbe72016-11-14 11:17:37 -05003809 changes = self.getJobFromHistory(
3810 'project-merge', 'org/project1').changes
3811 self.assertEqual(changes, '2,1 1,1')
James E. Blair96698e22015-04-02 07:48:21 -07003812
James E. Blair5ee24252014-12-30 10:12:29 -08003813 def test_crd_cycle(self):
3814 "Test cross-repo dependency cycles"
3815 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3816 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003817 A.addApproval('code-review', 2)
3818 B.addApproval('code-review', 2)
James E. Blair5ee24252014-12-30 10:12:29 -08003819
3820 # A -> B -> A (via commit-depends)
3821
3822 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3823 A.subject, B.data['id'])
3824 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3825 B.subject, A.data['id'])
3826
James E. Blair8b5408c2016-08-08 15:37:46 -07003827 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair5ee24252014-12-30 10:12:29 -08003828 self.waitUntilSettled()
3829
3830 self.assertEqual(A.reported, 0)
3831 self.assertEqual(B.reported, 0)
3832 self.assertEqual(A.data['status'], 'NEW')
3833 self.assertEqual(B.data['status'], 'NEW')
James E. Blairbfb8e042014-12-30 17:01:44 -08003834
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003835 def test_crd_gate_unknown(self):
3836 "Test unknown projects in dependent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07003837 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003838 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
3839 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -07003840 A.addApproval('code-review', 2)
3841 B.addApproval('code-review', 2)
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003842
3843 # A Depends-On: B
3844 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3845 A.subject, B.data['id'])
3846
James E. Blair8b5408c2016-08-08 15:37:46 -07003847 B.addApproval('approved', 1)
3848 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003849 self.waitUntilSettled()
3850
3851 # Unknown projects cannot share a queue with any other
3852 # since they don't have common jobs with any other (they have no jobs).
3853 # Changes which depend on unknown project changes
3854 # should not be processed in dependent pipeline
3855 self.assertEqual(A.data['status'], 'NEW')
3856 self.assertEqual(B.data['status'], 'NEW')
3857 self.assertEqual(A.reported, 0)
3858 self.assertEqual(B.reported, 0)
3859 self.assertEqual(len(self.history), 0)
3860
James E. Blair7720b402017-06-07 08:51:49 -07003861 # Simulate change B being gated outside this layout Set the
3862 # change merged before submitting the event so that when the
3863 # event triggers a gerrit query to update the change, we get
3864 # the information that it was merged.
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003865 B.setMerged()
James E. Blair7720b402017-06-07 08:51:49 -07003866 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003867 self.waitUntilSettled()
3868 self.assertEqual(len(self.history), 0)
3869
3870 # Now that B is merged, A should be able to be enqueued and
3871 # merged.
James E. Blair8b5408c2016-08-08 15:37:46 -07003872 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003873 self.waitUntilSettled()
3874
3875 self.assertEqual(A.data['status'], 'MERGED')
3876 self.assertEqual(A.reported, 2)
3877 self.assertEqual(B.data['status'], 'MERGED')
3878 self.assertEqual(B.reported, 0)
3879
James E. Blairbfb8e042014-12-30 17:01:44 -08003880 def test_crd_check(self):
3881 "Test cross-repo dependencies in independent pipelines"
3882
Paul Belanger174a8272017-03-14 13:20:10 -04003883 self.executor_server.hold_jobs_in_build = True
James E. Blairbfb8e042014-12-30 17:01:44 -08003884 self.gearman_server.hold_jobs_in_queue = True
3885 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3886 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
3887
3888 # A Depends-On: B
3889 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
3890 A.subject, B.data['id'])
3891
3892 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3893 self.waitUntilSettled()
3894
James E. Blairbfb8e042014-12-30 17:01:44 -08003895 self.gearman_server.hold_jobs_in_queue = False
3896 self.gearman_server.release()
3897 self.waitUntilSettled()
3898
Paul Belanger174a8272017-03-14 13:20:10 -04003899 self.executor_server.release('.*-merge')
Paul Belangerb30342b2016-11-14 12:30:43 -05003900 self.waitUntilSettled()
3901
James E. Blair2a535672017-04-27 12:03:15 -07003902 self.assertTrue(self.builds[0].hasChanges(A, B))
James E. Blairbfb8e042014-12-30 17:01:44 -08003903
Paul Belanger174a8272017-03-14 13:20:10 -04003904 self.executor_server.hold_jobs_in_build = False
3905 self.executor_server.release()
Paul Belangerb30342b2016-11-14 12:30:43 -05003906 self.waitUntilSettled()
3907
James E. Blairbfb8e042014-12-30 17:01:44 -08003908 self.assertEqual(A.data['status'], 'NEW')
3909 self.assertEqual(B.data['status'], 'NEW')
3910 self.assertEqual(A.reported, 1)
3911 self.assertEqual(B.reported, 0)
3912
3913 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerb30342b2016-11-14 12:30:43 -05003914 tenant = self.sched.abide.tenants.get('tenant-one')
3915 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair8f78d882015-02-05 08:51:37 -08003916
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003917 def test_crd_check_git_depends(self):
3918 "Test single-repo dependencies in independent pipelines"
James E. Blairb8c16472015-05-05 14:55:26 -07003919 self.gearman_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003920 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3921 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
3922
3923 # Add two git-dependent changes and make sure they both report
3924 # success.
3925 B.setDependsOn(A, 1)
3926 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3927 self.waitUntilSettled()
3928 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3929 self.waitUntilSettled()
3930
James E. Blairb8c16472015-05-05 14:55:26 -07003931 self.orderedRelease()
3932 self.gearman_server.hold_jobs_in_build = False
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003933 self.waitUntilSettled()
3934
3935 self.assertEqual(A.data['status'], 'NEW')
3936 self.assertEqual(B.data['status'], 'NEW')
3937 self.assertEqual(A.reported, 1)
3938 self.assertEqual(B.reported, 1)
3939
3940 self.assertEqual(self.history[0].changes, '1,1')
3941 self.assertEqual(self.history[-1].changes, '1,1 2,1')
Paul Belanger08136962016-11-03 17:57:38 -04003942 tenant = self.sched.abide.tenants.get('tenant-one')
3943 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003944
3945 self.assertIn('Build succeeded', A.messages[0])
3946 self.assertIn('Build succeeded', B.messages[0])
3947
3948 def test_crd_check_duplicate(self):
3949 "Test duplicate check in independent pipelines"
Paul Belanger174a8272017-03-14 13:20:10 -04003950 self.executor_server.hold_jobs_in_build = True
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003951 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
3952 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Paul Belanger08136962016-11-03 17:57:38 -04003953 tenant = self.sched.abide.tenants.get('tenant-one')
3954 check_pipeline = tenant.layout.pipelines['check']
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003955
3956 # Add two git-dependent changes...
3957 B.setDependsOn(A, 1)
3958 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3959 self.waitUntilSettled()
3960 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3961
3962 # ...make sure the live one is not duplicated...
3963 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
3964 self.waitUntilSettled()
3965 self.assertEqual(len(check_pipeline.getAllItems()), 2)
3966
3967 # ...but the non-live one is able to be.
3968 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
3969 self.waitUntilSettled()
3970 self.assertEqual(len(check_pipeline.getAllItems()), 3)
3971
Clark Boylandd849822015-03-02 12:38:14 -08003972 # Release jobs in order to avoid races with change A jobs
3973 # finishing before change B jobs.
James E. Blaird7650852015-05-07 15:47:37 -07003974 self.orderedRelease()
Paul Belanger174a8272017-03-14 13:20:10 -04003975 self.executor_server.hold_jobs_in_build = False
3976 self.executor_server.release()
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003977 self.waitUntilSettled()
3978
3979 self.assertEqual(A.data['status'], 'NEW')
3980 self.assertEqual(B.data['status'], 'NEW')
3981 self.assertEqual(A.reported, 1)
3982 self.assertEqual(B.reported, 1)
3983
3984 self.assertEqual(self.history[0].changes, '1,1 2,1')
3985 self.assertEqual(self.history[1].changes, '1,1')
Paul Belanger08136962016-11-03 17:57:38 -04003986 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blairdbfe1cd2015-02-07 11:41:19 -08003987
3988 self.assertIn('Build succeeded', A.messages[0])
3989 self.assertIn('Build succeeded', B.messages[0])
3990
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003991 def _test_crd_check_reconfiguration(self, project1, project2):
James E. Blair8f78d882015-02-05 08:51:37 -08003992 "Test cross-repo dependencies re-enqueued in independent pipelines"
3993
3994 self.gearman_server.hold_jobs_in_queue = True
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00003995 A = self.fake_gerrit.addFakeChange(project1, 'master', 'A')
3996 B = self.fake_gerrit.addFakeChange(project2, 'master', 'B')
James E. Blair8f78d882015-02-05 08:51:37 -08003997
3998 # A Depends-On: B
3999 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4000 A.subject, B.data['id'])
4001
4002 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4003 self.waitUntilSettled()
4004
4005 self.sched.reconfigure(self.config)
4006
4007 # Make sure the items still share a change queue, and the
4008 # first one is not live.
Paul Belangerce4485f2016-11-11 17:01:56 -05004009 tenant = self.sched.abide.tenants.get('tenant-one')
4010 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 1)
4011 queue = tenant.layout.pipelines['check'].queues[0]
James E. Blair8f78d882015-02-05 08:51:37 -08004012 first_item = queue.queue[0]
4013 for item in queue.queue:
4014 self.assertEqual(item.queue, first_item.queue)
4015 self.assertFalse(first_item.live)
4016 self.assertTrue(queue.queue[1].live)
4017
4018 self.gearman_server.hold_jobs_in_queue = False
4019 self.gearman_server.release()
4020 self.waitUntilSettled()
4021
4022 self.assertEqual(A.data['status'], 'NEW')
4023 self.assertEqual(B.data['status'], 'NEW')
4024 self.assertEqual(A.reported, 1)
4025 self.assertEqual(B.reported, 0)
4026
4027 self.assertEqual(self.history[0].changes, '2,1 1,1')
Paul Belangerce4485f2016-11-11 17:01:56 -05004028 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
James E. Blair17dd6772015-02-09 14:45:18 -08004029
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004030 def test_crd_check_reconfiguration(self):
4031 self._test_crd_check_reconfiguration('org/project1', 'org/project2')
4032
4033 def test_crd_undefined_project(self):
4034 """Test that undefined projects in dependencies are handled for
4035 independent pipelines"""
4036 # It's a hack for fake gerrit,
4037 # as it implies repo creation upon the creation of any change
James E. Blairc4ba97a2017-04-19 16:26:24 -07004038 self.init_repo("org/unknown", tag='init')
Evgeny Antyshev0deaaad2015-08-03 20:22:56 +00004039 self._test_crd_check_reconfiguration('org/project1', 'org/unknown')
4040
Jesse Keatingea68ab42017-04-19 15:03:30 -07004041 @simple_layout('layouts/ignore-dependencies.yaml')
James E. Blair17dd6772015-02-09 14:45:18 -08004042 def test_crd_check_ignore_dependencies(self):
4043 "Test cross-repo dependencies can be ignored"
James E. Blair17dd6772015-02-09 14:45:18 -08004044
4045 self.gearman_server.hold_jobs_in_queue = True
4046 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4047 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4048 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
4049
4050 # A Depends-On: B
4051 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4052 A.subject, B.data['id'])
4053 # C git-depends on B
4054 C.setDependsOn(B, 1)
4055 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4056 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4057 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4058 self.waitUntilSettled()
4059
4060 # Make sure none of the items share a change queue, and all
4061 # are live.
Jesse Keatingea68ab42017-04-19 15:03:30 -07004062 tenant = self.sched.abide.tenants.get('tenant-one')
Clint Byrum12282ca2017-04-03 11:04:02 -07004063 check_pipeline = tenant.layout.pipelines['check']
James E. Blair17dd6772015-02-09 14:45:18 -08004064 self.assertEqual(len(check_pipeline.queues), 3)
4065 self.assertEqual(len(check_pipeline.getAllItems()), 3)
4066 for item in check_pipeline.getAllItems():
4067 self.assertTrue(item.live)
4068
4069 self.gearman_server.hold_jobs_in_queue = False
4070 self.gearman_server.release()
4071 self.waitUntilSettled()
4072
4073 self.assertEqual(A.data['status'], 'NEW')
4074 self.assertEqual(B.data['status'], 'NEW')
4075 self.assertEqual(C.data['status'], 'NEW')
4076 self.assertEqual(A.reported, 1)
4077 self.assertEqual(B.reported, 1)
4078 self.assertEqual(C.reported, 1)
4079
4080 # Each job should have tested exactly one change
4081 for job in self.history:
4082 self.assertEqual(len(job.changes.split()), 1)
James E. Blair96698e22015-04-02 07:48:21 -07004083
James E. Blair7cb84542017-04-19 13:35:05 -07004084 @simple_layout('layouts/three-projects.yaml')
James E. Blair96698e22015-04-02 07:48:21 -07004085 def test_crd_check_transitive(self):
4086 "Test transitive cross-repo dependencies"
4087 # Specifically, if A -> B -> C, and C gets a new patchset and
4088 # A gets a new patchset, ensure the test of A,2 includes B,1
4089 # and C,2 (not C,1 which would indicate stale data in the
4090 # cache for B).
4091 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4092 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
4093 C = self.fake_gerrit.addFakeChange('org/project3', 'master', 'C')
4094
4095 # A Depends-On: B
4096 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4097 A.subject, B.data['id'])
4098
4099 # B Depends-On: C
4100 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4101 B.subject, C.data['id'])
4102
4103 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4104 self.waitUntilSettled()
4105 self.assertEqual(self.history[-1].changes, '3,1 2,1 1,1')
4106
4107 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4108 self.waitUntilSettled()
4109 self.assertEqual(self.history[-1].changes, '3,1 2,1')
4110
4111 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4112 self.waitUntilSettled()
4113 self.assertEqual(self.history[-1].changes, '3,1')
4114
4115 C.addPatchset()
4116 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(2))
4117 self.waitUntilSettled()
4118 self.assertEqual(self.history[-1].changes, '3,2')
4119
4120 A.addPatchset()
4121 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(2))
4122 self.waitUntilSettled()
4123 self.assertEqual(self.history[-1].changes, '3,2 2,1 1,2')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004124
James E. Blaira8b90b32016-08-24 15:18:50 -07004125 def test_crd_check_unknown(self):
4126 "Test unknown projects in independent pipeline"
James E. Blairc4ba97a2017-04-19 16:26:24 -07004127 self.init_repo("org/unknown", tag='init')
James E. Blaira8b90b32016-08-24 15:18:50 -07004128 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4129 B = self.fake_gerrit.addFakeChange('org/unknown', 'master', 'D')
4130 # A Depends-On: B
4131 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4132 A.subject, B.data['id'])
4133
4134 # Make sure zuul has seen an event on B.
4135 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4136 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4137 self.waitUntilSettled()
4138
4139 self.assertEqual(A.data['status'], 'NEW')
4140 self.assertEqual(A.reported, 1)
4141 self.assertEqual(B.data['status'], 'NEW')
4142 self.assertEqual(B.reported, 0)
4143
James E. Blair92464a22016-04-05 10:21:26 -07004144 def test_crd_cycle_join(self):
4145 "Test an updated change creates a cycle"
4146 A = self.fake_gerrit.addFakeChange('org/project2', 'master', 'A')
4147
4148 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4149 self.waitUntilSettled()
4150
4151 # Create B->A
4152 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4153 B.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4154 B.subject, A.data['id'])
4155 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4156 self.waitUntilSettled()
4157
4158 # Update A to add A->B (a cycle).
4159 A.addPatchset()
4160 A.data['commitMessage'] = '%s\n\nDepends-On: %s\n' % (
4161 A.subject, B.data['id'])
4162 # Normally we would submit the patchset-created event for
4163 # processing here, however, we have no way of noting whether
4164 # the dependency cycle detection correctly raised an
4165 # exception, so instead, we reach into the source driver and
4166 # call the method that would ultimately be called by the event
4167 # processing.
4168
Paul Belanger0e155e22016-11-14 14:12:23 -05004169 tenant = self.sched.abide.tenants.get('tenant-one')
James E. Blair6053de42017-04-05 11:27:11 -07004170 (trusted, project) = tenant.getProject('org/project')
4171 source = project.source
Paul Belanger0e155e22016-11-14 14:12:23 -05004172
4173 # TODO(pabelanger): As we add more source / trigger APIs we should make
4174 # it easier for users to create events for testing.
4175 event = zuul.model.TriggerEvent()
4176 event.trigger_name = 'gerrit'
4177 event.change_number = '1'
4178 event.patch_number = '2'
James E. Blair92464a22016-04-05 10:21:26 -07004179 with testtools.ExpectedException(
4180 Exception, "Dependency cycle detected"):
Paul Belanger0e155e22016-11-14 14:12:23 -05004181 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004182 self.log.debug("Got expected dependency cycle exception")
4183
4184 # Now if we update B to remove the depends-on, everything
4185 # should be okay. B; A->B
4186
4187 B.addPatchset()
4188 B.data['commitMessage'] = '%s\n' % (B.subject,)
Paul Belanger0e155e22016-11-14 14:12:23 -05004189
4190 source.getChange(event, True)
4191 event.change_number = '2'
4192 source.getChange(event, True)
James E. Blair92464a22016-04-05 10:21:26 -07004193
Jesse Keatingcc67b122017-04-19 14:54:57 -07004194 @simple_layout('layouts/disable_at.yaml')
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004195 def test_disable_at(self):
4196 "Test a pipeline will only report to the disabled trigger when failing"
4197
Jesse Keatingcc67b122017-04-19 14:54:57 -07004198 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004199 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004200 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004201 0, tenant.layout.pipelines['check']._consecutive_failures)
4202 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004203
4204 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4205 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4206 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
4207 D = self.fake_gerrit.addFakeChange('org/project', 'master', 'D')
4208 E = self.fake_gerrit.addFakeChange('org/project', 'master', 'E')
4209 F = self.fake_gerrit.addFakeChange('org/project', 'master', 'F')
4210 G = self.fake_gerrit.addFakeChange('org/project', 'master', 'G')
4211 H = self.fake_gerrit.addFakeChange('org/project', 'master', 'H')
4212 I = self.fake_gerrit.addFakeChange('org/project', 'master', 'I')
4213 J = self.fake_gerrit.addFakeChange('org/project', 'master', 'J')
4214 K = self.fake_gerrit.addFakeChange('org/project', 'master', 'K')
4215
Paul Belanger174a8272017-03-14 13:20:10 -04004216 self.executor_server.failJob('project-test1', A)
4217 self.executor_server.failJob('project-test1', B)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004218 # Let C pass, resetting the counter
Paul Belanger174a8272017-03-14 13:20:10 -04004219 self.executor_server.failJob('project-test1', D)
4220 self.executor_server.failJob('project-test1', E)
4221 self.executor_server.failJob('project-test1', F)
4222 self.executor_server.failJob('project-test1', G)
4223 self.executor_server.failJob('project-test1', H)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004224 # I also passes but should only report to the disabled reporters
Paul Belanger174a8272017-03-14 13:20:10 -04004225 self.executor_server.failJob('project-test1', J)
4226 self.executor_server.failJob('project-test1', K)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004227
4228 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4229 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4230 self.waitUntilSettled()
4231
4232 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004233 2, tenant.layout.pipelines['check']._consecutive_failures)
4234 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004235
4236 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
4237 self.waitUntilSettled()
4238
4239 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004240 0, tenant.layout.pipelines['check']._consecutive_failures)
4241 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004242
4243 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
4244 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
4245 self.fake_gerrit.addEvent(F.getPatchsetCreatedEvent(1))
4246 self.waitUntilSettled()
4247
4248 # We should be disabled now
4249 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004250 3, tenant.layout.pipelines['check']._consecutive_failures)
4251 self.assertTrue(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004252
4253 # We need to wait between each of these patches to make sure the
4254 # smtp messages come back in an expected order
4255 self.fake_gerrit.addEvent(G.getPatchsetCreatedEvent(1))
4256 self.waitUntilSettled()
4257 self.fake_gerrit.addEvent(H.getPatchsetCreatedEvent(1))
4258 self.waitUntilSettled()
4259 self.fake_gerrit.addEvent(I.getPatchsetCreatedEvent(1))
4260 self.waitUntilSettled()
4261
4262 # The first 6 (ABCDEF) jobs should have reported back to gerrt thus
4263 # leaving a message on each change
4264 self.assertEqual(1, len(A.messages))
4265 self.assertIn('Build failed.', A.messages[0])
4266 self.assertEqual(1, len(B.messages))
4267 self.assertIn('Build failed.', B.messages[0])
4268 self.assertEqual(1, len(C.messages))
4269 self.assertIn('Build succeeded.', C.messages[0])
4270 self.assertEqual(1, len(D.messages))
4271 self.assertIn('Build failed.', D.messages[0])
4272 self.assertEqual(1, len(E.messages))
4273 self.assertIn('Build failed.', E.messages[0])
4274 self.assertEqual(1, len(F.messages))
4275 self.assertIn('Build failed.', F.messages[0])
4276
4277 # The last 3 (GHI) would have only reported via smtp.
4278 self.assertEqual(3, len(self.smtp_messages))
4279 self.assertEqual(0, len(G.messages))
4280 self.assertIn('Build failed.', self.smtp_messages[0]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004281 self.assertIn(
4282 'project-test1 https://server/job', self.smtp_messages[0]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004283 self.assertEqual(0, len(H.messages))
4284 self.assertIn('Build failed.', self.smtp_messages[1]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004285 self.assertIn(
4286 'project-test1 https://server/job', self.smtp_messages[1]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004287 self.assertEqual(0, len(I.messages))
4288 self.assertIn('Build succeeded.', self.smtp_messages[2]['body'])
Paul Belanger7dc76e82016-11-11 16:51:08 -05004289 self.assertIn(
4290 'project-test1 https://server/job', self.smtp_messages[2]['body'])
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004291
4292 # Now reload the configuration (simulate a HUP) to check the pipeline
4293 # comes out of disabled
4294 self.sched.reconfigure(self.config)
4295
Jesse Keatingcc67b122017-04-19 14:54:57 -07004296 tenant = self.sched.abide.tenants.get('tenant-one')
Paul Belanger7dc76e82016-11-11 16:51:08 -05004297
4298 self.assertEqual(3, tenant.layout.pipelines['check'].disable_at)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004299 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004300 0, tenant.layout.pipelines['check']._consecutive_failures)
4301 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004302
4303 self.fake_gerrit.addEvent(J.getPatchsetCreatedEvent(1))
4304 self.fake_gerrit.addEvent(K.getPatchsetCreatedEvent(1))
4305 self.waitUntilSettled()
4306
4307 self.assertEqual(
Paul Belanger7dc76e82016-11-11 16:51:08 -05004308 2, tenant.layout.pipelines['check']._consecutive_failures)
4309 self.assertFalse(tenant.layout.pipelines['check']._disabled)
Joshua Hesketh89e829d2015-02-10 16:29:45 +11004310
4311 # J and K went back to gerrit
4312 self.assertEqual(1, len(J.messages))
4313 self.assertIn('Build failed.', J.messages[0])
4314 self.assertEqual(1, len(K.messages))
4315 self.assertIn('Build failed.', K.messages[0])
4316 # No more messages reported via smtp
4317 self.assertEqual(3, len(self.smtp_messages))
Joshua Heskethd6dbd682015-12-22 10:06:54 +11004318
James E. Blaircc020532017-04-19 13:04:44 -07004319 @simple_layout('layouts/one-job-project.yaml')
4320 def test_one_job_project(self):
4321 "Test that queueing works with one job"
4322 A = self.fake_gerrit.addFakeChange('org/one-job-project',
4323 'master', 'A')
4324 B = self.fake_gerrit.addFakeChange('org/one-job-project',
4325 'master', 'B')
4326 A.addApproval('code-review', 2)
4327 B.addApproval('code-review', 2)
4328 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4329 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4330 self.waitUntilSettled()
4331
4332 self.assertEqual(A.data['status'], 'MERGED')
4333 self.assertEqual(A.reported, 2)
4334 self.assertEqual(B.data['status'], 'MERGED')
4335 self.assertEqual(B.reported, 2)
4336
Paul Belanger71d98172016-11-08 10:56:31 -05004337 def test_rerun_on_abort(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004338 "Test that if a execute server fails to run a job, it is run again"
Paul Belanger71d98172016-11-08 10:56:31 -05004339
Paul Belanger174a8272017-03-14 13:20:10 -04004340 self.executor_server.hold_jobs_in_build = True
Paul Belanger71d98172016-11-08 10:56:31 -05004341 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4342 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4343 self.waitUntilSettled()
4344
Paul Belanger174a8272017-03-14 13:20:10 -04004345 self.executor_server.release('.*-merge')
Paul Belanger71d98172016-11-08 10:56:31 -05004346 self.waitUntilSettled()
4347
4348 self.assertEqual(len(self.builds), 2)
4349 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004350 self.executor_server.release('.*-test*')
Paul Belanger71d98172016-11-08 10:56:31 -05004351 self.waitUntilSettled()
4352
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004353 for x in range(3):
Clint Byrumdf0a55b2016-12-05 06:39:11 -08004354 self.assertEqual(len(self.builds), 1,
4355 'len of builds at x=%d is wrong' % x)
Paul Belanger71d98172016-11-08 10:56:31 -05004356 self.builds[0].requeue = True
Paul Belanger174a8272017-03-14 13:20:10 -04004357 self.executor_server.release('.*-test1')
Paul Belanger71d98172016-11-08 10:56:31 -05004358 self.waitUntilSettled()
4359
Paul Belanger174a8272017-03-14 13:20:10 -04004360 self.executor_server.hold_jobs_in_build = False
4361 self.executor_server.release()
Paul Belanger71d98172016-11-08 10:56:31 -05004362 self.waitUntilSettled()
Clint Byrum5bb5feb2016-12-08 12:39:05 -08004363 self.assertEqual(len(self.history), 6)
Paul Belanger71d98172016-11-08 10:56:31 -05004364 self.assertEqual(self.countJobResults(self.history, 'SUCCESS'), 2)
4365 self.assertEqual(A.reported, 1)
4366 self.assertIn('RETRY_LIMIT', A.messages[0])
Joshua Hesketh3f7def32016-11-21 17:36:44 +11004367
James E. Blair15be0e12017-01-03 13:45:20 -08004368 def test_zookeeper_disconnect(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004369 "Test that jobs are executed after a zookeeper disconnect"
James E. Blair15be0e12017-01-03 13:45:20 -08004370
4371 self.fake_nodepool.paused = True
4372 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4373 A.addApproval('code-review', 2)
4374 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4375 self.waitUntilSettled()
4376
4377 self.zk.client.stop()
4378 self.zk.client.start()
4379 self.fake_nodepool.paused = False
4380 self.waitUntilSettled()
4381
4382 self.assertEqual(A.data['status'], 'MERGED')
4383 self.assertEqual(A.reported, 2)
4384
James E. Blair6ab79e02017-01-06 10:10:17 -08004385 def test_nodepool_failure(self):
4386 "Test that jobs are reported after a nodepool failure"
4387
4388 self.fake_nodepool.paused = True
4389 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4390 A.addApproval('code-review', 2)
4391 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4392 self.waitUntilSettled()
4393
4394 req = self.fake_nodepool.getNodeRequests()[0]
4395 self.fake_nodepool.addFailRequest(req)
4396
4397 self.fake_nodepool.paused = False
4398 self.waitUntilSettled()
4399
4400 self.assertEqual(A.data['status'], 'NEW')
4401 self.assertEqual(A.reported, 2)
4402 self.assertIn('project-merge : NODE_FAILURE', A.messages[1])
4403 self.assertIn('project-test1 : SKIPPED', A.messages[1])
4404 self.assertIn('project-test2 : SKIPPED', A.messages[1])
4405
Arieb6f068c2016-10-09 13:11:06 +03004406
James E. Blaira002b032017-04-18 10:35:48 -07004407class TestExecutor(ZuulTestCase):
4408 tenant_config_file = 'config/single-tenant/main.yaml'
4409
4410 def assertFinalState(self):
4411 # In this test, we expect to shut down in a non-final state,
4412 # so skip these checks.
4413 pass
4414
4415 def assertCleanShutdown(self):
4416 self.log.debug("Assert clean shutdown")
4417
4418 # After shutdown, make sure no jobs are running
4419 self.assertEqual({}, self.executor_server.job_workers)
4420
4421 # Make sure that git.Repo objects have been garbage collected.
4422 repos = []
4423 gc.collect()
4424 for obj in gc.get_objects():
4425 if isinstance(obj, git.Repo):
4426 self.log.debug("Leaked git repo object: %s" % repr(obj))
4427 repos.append(obj)
4428 self.assertEqual(len(repos), 0)
4429
4430 def test_executor_shutdown(self):
4431 "Test that the executor can shut down with jobs running"
4432
4433 self.executor_server.hold_jobs_in_build = True
4434 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4435 A.addApproval('code-review', 2)
4436 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4437 self.waitUntilSettled()
4438
4439
Fredrik Medleyf8aec832015-09-28 13:40:20 +02004440class TestDependencyGraph(ZuulTestCase):
4441 tenant_config_file = 'config/dependency-graph/main.yaml'
4442
4443 def test_dependeny_graph_dispatch_jobs_once(self):
4444 "Test a job in a dependency graph is queued only once"
4445 # Job dependencies, starting with A
4446 # A
4447 # / \
4448 # B C
4449 # / \ / \
4450 # D F E
4451 # |
4452 # G
4453
4454 self.executor_server.hold_jobs_in_build = True
4455 change = self.fake_gerrit.addFakeChange(
4456 'org/project', 'master', 'change')
4457 change.addApproval('code-review', 2)
4458 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4459
4460 self.waitUntilSettled()
4461 self.assertEqual([b.name for b in self.builds], ['A'])
4462
4463 self.executor_server.release('A')
4464 self.waitUntilSettled()
4465 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4466
4467 self.executor_server.release('B')
4468 self.waitUntilSettled()
4469 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4470
4471 self.executor_server.release('D')
4472 self.waitUntilSettled()
4473 self.assertEqual([b.name for b in self.builds], ['C'])
4474
4475 self.executor_server.release('C')
4476 self.waitUntilSettled()
4477 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'F'])
4478
4479 self.executor_server.release('F')
4480 self.waitUntilSettled()
4481 self.assertEqual(sorted(b.name for b in self.builds), ['E', 'G'])
4482
4483 self.executor_server.release('G')
4484 self.waitUntilSettled()
4485 self.assertEqual([b.name for b in self.builds], ['E'])
4486
4487 self.executor_server.release('E')
4488 self.waitUntilSettled()
4489 self.assertEqual(len(self.builds), 0)
4490
4491 self.executor_server.hold_jobs_in_build = False
4492 self.executor_server.release()
4493 self.waitUntilSettled()
4494
4495 self.assertEqual(len(self.builds), 0)
4496 self.assertEqual(len(self.history), 7)
4497
4498 self.assertEqual(change.data['status'], 'MERGED')
4499 self.assertEqual(change.reported, 2)
4500
4501 def test_jobs_launched_only_if_all_dependencies_are_successful(self):
4502 "Test that a job waits till all dependencies are successful"
4503 # Job dependencies, starting with A
4504 # A
4505 # / \
4506 # B C*
4507 # / \ / \
4508 # D F E
4509 # |
4510 # G
4511
4512 self.executor_server.hold_jobs_in_build = True
4513 change = self.fake_gerrit.addFakeChange(
4514 'org/project', 'master', 'change')
4515 change.addApproval('code-review', 2)
4516
4517 self.executor_server.failJob('C', change)
4518
4519 self.fake_gerrit.addEvent(change.addApproval('approved', 1))
4520
4521 self.waitUntilSettled()
4522 self.assertEqual([b.name for b in self.builds], ['A'])
4523
4524 self.executor_server.release('A')
4525 self.waitUntilSettled()
4526 self.assertEqual(sorted(b.name for b in self.builds), ['B', 'C'])
4527
4528 self.executor_server.release('B')
4529 self.waitUntilSettled()
4530 self.assertEqual(sorted(b.name for b in self.builds), ['C', 'D'])
4531
4532 self.executor_server.release('D')
4533 self.waitUntilSettled()
4534 self.assertEqual([b.name for b in self.builds], ['C'])
4535
4536 self.executor_server.release('C')
4537 self.waitUntilSettled()
4538 self.assertEqual(len(self.builds), 0)
4539
4540 self.executor_server.hold_jobs_in_build = False
4541 self.executor_server.release()
4542 self.waitUntilSettled()
4543
4544 self.assertEqual(len(self.builds), 0)
4545 self.assertEqual(len(self.history), 4)
4546
4547 self.assertEqual(change.data['status'], 'NEW')
4548 self.assertEqual(change.reported, 2)
4549
4550
Arieb6f068c2016-10-09 13:11:06 +03004551class TestDuplicatePipeline(ZuulTestCase):
4552 tenant_config_file = 'config/duplicate-pipeline/main.yaml'
4553
4554 def test_duplicate_pipelines(self):
4555 "Test that a change matching multiple pipelines works"
4556
4557 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4558 self.fake_gerrit.addEvent(A.getChangeRestoredEvent())
4559 self.waitUntilSettled()
4560
4561 self.assertHistory([
4562 dict(name='project-test1', result='SUCCESS', changes='1,1',
4563 pipeline='dup1'),
4564 dict(name='project-test1', result='SUCCESS', changes='1,1',
4565 pipeline='dup2'),
Paul Belanger73a7d8e2016-11-09 11:12:42 -05004566 ], ordered=False)
Arieb6f068c2016-10-09 13:11:06 +03004567
4568 self.assertEqual(len(A.messages), 2)
4569
Paul Belangera46a3742016-11-09 14:23:03 -05004570 if 'dup1' in A.messages[0]:
4571 self.assertIn('dup1', A.messages[0])
4572 self.assertNotIn('dup2', A.messages[0])
4573 self.assertIn('project-test1', A.messages[0])
4574 self.assertIn('dup2', A.messages[1])
4575 self.assertNotIn('dup1', A.messages[1])
4576 self.assertIn('project-test1', A.messages[1])
4577 else:
4578 self.assertIn('dup1', A.messages[1])
4579 self.assertNotIn('dup2', A.messages[1])
4580 self.assertIn('project-test1', A.messages[1])
4581 self.assertIn('dup2', A.messages[0])
4582 self.assertNotIn('dup1', A.messages[0])
4583 self.assertIn('project-test1', A.messages[0])
Paul Belangerfac69ba2016-11-03 09:03:13 -04004584
4585
Paul Belanger86085b32016-11-03 12:48:57 -04004586class TestSchedulerTemplatedProject(ZuulTestCase):
4587 tenant_config_file = 'config/templated-project/main.yaml'
4588
Paul Belanger174a8272017-03-14 13:20:10 -04004589 def test_job_from_templates_executed(self):
4590 "Test whether a job generated via a template can be executed"
Paul Belanger86085b32016-11-03 12:48:57 -04004591
4592 A = self.fake_gerrit.addFakeChange(
4593 'org/templated-project', 'master', 'A')
4594 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4595 self.waitUntilSettled()
4596
4597 self.assertEqual(self.getJobFromHistory('project-test1').result,
4598 'SUCCESS')
4599 self.assertEqual(self.getJobFromHistory('project-test2').result,
4600 'SUCCESS')
Paul Belanger3adf72f2016-11-03 14:57:31 -04004601
4602 def test_layered_templates(self):
Paul Belanger174a8272017-03-14 13:20:10 -04004603 "Test whether a job generated via a template can be executed"
Paul Belanger3adf72f2016-11-03 14:57:31 -04004604
4605 A = self.fake_gerrit.addFakeChange(
4606 'org/layered-project', 'master', 'A')
4607 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4608 self.waitUntilSettled()
4609
4610 self.assertEqual(self.getJobFromHistory('project-test1').result,
4611 'SUCCESS')
4612 self.assertEqual(self.getJobFromHistory('project-test2').result,
4613 'SUCCESS')
4614 self.assertEqual(self.getJobFromHistory('layered-project-test3'
4615 ).result, 'SUCCESS')
4616 self.assertEqual(self.getJobFromHistory('layered-project-test4'
4617 ).result, 'SUCCESS')
4618 self.assertEqual(self.getJobFromHistory('layered-project-foo-test5'
4619 ).result, 'SUCCESS')
4620 self.assertEqual(self.getJobFromHistory('project-test6').result,
4621 'SUCCESS')
Adam Gandelman94a60062016-11-21 16:43:14 -08004622
4623
4624class TestSchedulerSuccessURL(ZuulTestCase):
4625 tenant_config_file = 'config/success-url/main.yaml'
4626
4627 def test_success_url(self):
4628 "Ensure bad build params are ignored"
4629 self.sched.reconfigure(self.config)
4630 self.init_repo('org/docs')
4631
4632 A = self.fake_gerrit.addFakeChange('org/docs', 'master', 'A')
4633 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4634 self.waitUntilSettled()
4635
4636 # Both builds ran: docs-draft-test + docs-draft-test2
4637 self.assertEqual(len(self.history), 2)
4638
4639 # Grab build id
James E. Blaire7576802016-12-21 16:15:00 -08004640 for build in self.history:
4641 if build.name == 'docs-draft-test':
4642 uuid = build.uuid[:7]
4643 break
Adam Gandelman94a60062016-11-21 16:43:14 -08004644
4645 # Two msgs: 'Starting...' + results
4646 self.assertEqual(len(self.smtp_messages), 2)
4647 body = self.smtp_messages[1]['body'].splitlines()
4648 self.assertEqual('Build succeeded.', body[0])
4649
4650 self.assertIn(
4651 '- docs-draft-test http://docs-draft.example.org/1/1/1/check/'
4652 'docs-draft-test/%s/publish-docs/' % uuid,
4653 body[2])
4654
Paul Belanger174a8272017-03-14 13:20:10 -04004655 # NOTE: This default URL is currently hard-coded in executor/server.py
Adam Gandelman94a60062016-11-21 16:43:14 -08004656 self.assertIn(
4657 '- docs-draft-test2 https://server/job',
4658 body[3])
Adam Gandelman8bd57102016-12-02 12:58:42 -08004659
4660
Adam Gandelman4da00f62016-12-09 15:47:33 -08004661class TestSchedulerMerges(ZuulTestCase):
4662 tenant_config_file = 'config/merges/main.yaml'
Adam Gandelman8bd57102016-12-02 12:58:42 -08004663
4664 def _test_project_merge_mode(self, mode):
Paul Belanger174a8272017-03-14 13:20:10 -04004665 self.executor_server.keep_jobdir = False
Adam Gandelman8bd57102016-12-02 12:58:42 -08004666 project = 'org/project-%s' % mode
Paul Belanger174a8272017-03-14 13:20:10 -04004667 self.executor_server.hold_jobs_in_build = True
Adam Gandelman8bd57102016-12-02 12:58:42 -08004668 A = self.fake_gerrit.addFakeChange(project, 'master', 'A')
4669 B = self.fake_gerrit.addFakeChange(project, 'master', 'B')
4670 C = self.fake_gerrit.addFakeChange(project, 'master', 'C')
4671 A.addApproval('code-review', 2)
4672 B.addApproval('code-review', 2)
4673 C.addApproval('code-review', 2)
4674 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4675 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4676 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4677 self.waitUntilSettled()
4678
4679 build = self.builds[-1]
4680 ref = self.getParameter(build, 'ZUUL_REF')
4681
James E. Blair2a535672017-04-27 12:03:15 -07004682 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4683 project)
Adam Gandelman8bd57102016-12-02 12:58:42 -08004684 repo = git.Repo(path)
4685 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4686 repo_messages.reverse()
4687
Paul Belanger174a8272017-03-14 13:20:10 -04004688 self.executor_server.hold_jobs_in_build = False
4689 self.executor_server.release()
Adam Gandelman8bd57102016-12-02 12:58:42 -08004690 self.waitUntilSettled()
4691
4692 return repo_messages
4693
4694 def _test_merge(self, mode):
4695 us_path = os.path.join(
4696 self.upstream_root, 'org/project-%s' % mode)
4697 expected_messages = [
4698 'initial commit',
4699 'add content from fixture',
4700 # the intermediate commits order is nondeterministic
4701 "Merge commit 'refs/changes/1/2/1' of %s into HEAD" % us_path,
4702 "Merge commit 'refs/changes/1/3/1' of %s into HEAD" % us_path,
4703 ]
4704 result = self._test_project_merge_mode(mode)
4705 self.assertEqual(result[:2], expected_messages[:2])
4706 self.assertEqual(result[-2:], expected_messages[-2:])
4707
4708 def test_project_merge_mode_merge(self):
4709 self._test_merge('merge')
4710
4711 def test_project_merge_mode_merge_resolve(self):
4712 self._test_merge('merge-resolve')
4713
4714 def test_project_merge_mode_cherrypick(self):
4715 expected_messages = [
4716 'initial commit',
4717 'add content from fixture',
4718 'A-1',
4719 'B-1',
4720 'C-1']
4721 result = self._test_project_merge_mode('cherry-pick')
4722 self.assertEqual(result, expected_messages)
Adam Gandelman4da00f62016-12-09 15:47:33 -08004723
4724 def test_merge_branch(self):
4725 "Test that the right commits are on alternate branches"
4726 self.create_branch('org/project-merge-branches', 'mp')
4727
Paul Belanger174a8272017-03-14 13:20:10 -04004728 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004729 A = self.fake_gerrit.addFakeChange(
4730 'org/project-merge-branches', 'mp', 'A')
4731 B = self.fake_gerrit.addFakeChange(
4732 'org/project-merge-branches', 'mp', 'B')
4733 C = self.fake_gerrit.addFakeChange(
4734 'org/project-merge-branches', 'mp', 'C')
4735 A.addApproval('code-review', 2)
4736 B.addApproval('code-review', 2)
4737 C.addApproval('code-review', 2)
4738 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4739 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4740 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4741 self.waitUntilSettled()
4742
Paul Belanger174a8272017-03-14 13:20:10 -04004743 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004744 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004745 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004746 self.waitUntilSettled()
Paul Belanger174a8272017-03-14 13:20:10 -04004747 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004748 self.waitUntilSettled()
4749
4750 build = self.builds[-1]
4751 self.assertEqual(self.getParameter(build, 'ZUUL_BRANCH'), 'mp')
4752 ref = self.getParameter(build, 'ZUUL_REF')
James E. Blair2a535672017-04-27 12:03:15 -07004753 path = os.path.join(build.jobdir.src_root, 'review.example.com',
4754 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004755 repo = git.Repo(path)
4756
4757 repo_messages = [c.message.strip() for c in repo.iter_commits(ref)]
4758 repo_messages.reverse()
4759 correct_messages = [
4760 'initial commit',
4761 'add content from fixture',
4762 'mp commit',
4763 'A-1', 'B-1', 'C-1']
4764 self.assertEqual(repo_messages, correct_messages)
4765
Paul Belanger174a8272017-03-14 13:20:10 -04004766 self.executor_server.hold_jobs_in_build = False
4767 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004768 self.waitUntilSettled()
4769
4770 def test_merge_multi_branch(self):
4771 "Test that dependent changes on multiple branches are merged"
4772 self.create_branch('org/project-merge-branches', 'mp')
4773
Paul Belanger174a8272017-03-14 13:20:10 -04004774 self.executor_server.hold_jobs_in_build = True
Adam Gandelman4da00f62016-12-09 15:47:33 -08004775 A = self.fake_gerrit.addFakeChange(
4776 'org/project-merge-branches', 'master', 'A')
4777 B = self.fake_gerrit.addFakeChange(
4778 'org/project-merge-branches', 'mp', 'B')
4779 C = self.fake_gerrit.addFakeChange(
4780 'org/project-merge-branches', 'master', 'C')
4781 A.addApproval('code-review', 2)
4782 B.addApproval('code-review', 2)
4783 C.addApproval('code-review', 2)
4784 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
4785 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
4786 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
4787 self.waitUntilSettled()
4788
4789 job_A = None
4790 for job in self.builds:
4791 if 'project-merge' in job.name:
4792 job_A = job
4793 ref_A = self.getParameter(job_A, 'ZUUL_REF')
4794 commit_A = self.getParameter(job_A, 'ZUUL_COMMIT')
4795 self.log.debug("Got Zuul ref for change A: %s" % ref_A)
4796 self.log.debug("Got Zuul commit for change A: %s" % commit_A)
4797
James E. Blair2a535672017-04-27 12:03:15 -07004798 path = os.path.join(job_A.jobdir.src_root, 'review.example.com',
4799 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004800 repo = git.Repo(path)
4801 repo_messages = [c.message.strip()
4802 for c in repo.iter_commits(ref_A)]
4803 repo_messages.reverse()
4804 correct_messages = [
4805 'initial commit', 'add content from fixture', 'A-1']
4806 self.assertEqual(repo_messages, correct_messages)
4807
Paul Belanger174a8272017-03-14 13:20:10 -04004808 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004809 self.waitUntilSettled()
4810
4811 job_B = None
4812 for job in self.builds:
4813 if 'project-merge' in job.name:
4814 job_B = job
4815 ref_B = self.getParameter(job_B, 'ZUUL_REF')
4816 commit_B = self.getParameter(job_B, 'ZUUL_COMMIT')
4817 self.log.debug("Got Zuul ref for change B: %s" % ref_B)
4818 self.log.debug("Got Zuul commit for change B: %s" % commit_B)
4819
James E. Blair2a535672017-04-27 12:03:15 -07004820 path = os.path.join(job_B.jobdir.src_root, 'review.example.com',
4821 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004822 repo = git.Repo(path)
4823 repo_messages = [c.message.strip()
4824 for c in repo.iter_commits(ref_B)]
4825 repo_messages.reverse()
4826 correct_messages = [
4827 'initial commit', 'add content from fixture', 'mp commit', 'B-1']
4828 self.assertEqual(repo_messages, correct_messages)
4829
Paul Belanger174a8272017-03-14 13:20:10 -04004830 self.executor_server.release('.*-merge')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004831 self.waitUntilSettled()
4832
4833 job_C = None
4834 for job in self.builds:
4835 if 'project-merge' in job.name:
4836 job_C = job
4837 ref_C = self.getParameter(job_C, 'ZUUL_REF')
4838 commit_C = self.getParameter(job_C, 'ZUUL_COMMIT')
4839 self.log.debug("Got Zuul ref for change C: %s" % ref_C)
4840 self.log.debug("Got Zuul commit for change C: %s" % commit_C)
James E. Blair2a535672017-04-27 12:03:15 -07004841 path = os.path.join(job_C.jobdir.src_root, 'review.example.com',
4842 'org/project-merge-branches')
Adam Gandelman4da00f62016-12-09 15:47:33 -08004843 repo = git.Repo(path)
4844 repo_messages = [c.message.strip()
4845 for c in repo.iter_commits(ref_C)]
4846
4847 repo_messages.reverse()
4848 correct_messages = [
4849 'initial commit', 'add content from fixture',
4850 'A-1', 'C-1']
4851 # Ensure the right commits are in the history for this ref
4852 self.assertEqual(repo_messages, correct_messages)
4853
Paul Belanger174a8272017-03-14 13:20:10 -04004854 self.executor_server.hold_jobs_in_build = False
4855 self.executor_server.release()
Adam Gandelman4da00f62016-12-09 15:47:33 -08004856 self.waitUntilSettled()
Tobias Henkel9a0e1942017-03-20 16:16:02 +01004857
4858
James E. Blair9ea0d0b2017-04-20 09:27:15 -07004859class TestSemaphore(ZuulTestCase):
4860 tenant_config_file = 'config/semaphore/main.yaml'
4861
4862 def test_semaphore_one(self):
4863 "Test semaphores with max=1 (mutex)"
4864 tenant = self.sched.abide.tenants.get('tenant-one')
4865
4866 self.executor_server.hold_jobs_in_build = True
4867
4868 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
4869 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
4870 self.assertFalse('test-semaphore' in
4871 tenant.semaphore_handler.semaphores)
4872
4873 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4874 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4875 self.waitUntilSettled()
4876
4877 self.assertEqual(len(self.builds), 3)
4878 self.assertEqual(self.builds[0].name, 'project-test1')
4879 self.assertEqual(self.builds[1].name, 'semaphore-one-test1')
4880 self.assertEqual(self.builds[2].name, 'project-test1')
4881
4882 self.executor_server.release('semaphore-one-test1')
4883 self.waitUntilSettled()
4884
4885 self.assertEqual(len(self.builds), 3)
4886 self.assertEqual(self.builds[0].name, 'project-test1')
4887 self.assertEqual(self.builds[1].name, 'project-test1')
4888 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4889 self.assertTrue('test-semaphore' in
4890 tenant.semaphore_handler.semaphores)
4891
4892 self.executor_server.release('semaphore-one-test2')
4893 self.waitUntilSettled()
4894
4895 self.assertEqual(len(self.builds), 3)
4896 self.assertEqual(self.builds[0].name, 'project-test1')
4897 self.assertEqual(self.builds[1].name, 'project-test1')
4898 self.assertEqual(self.builds[2].name, 'semaphore-one-test1')
4899 self.assertTrue('test-semaphore' in
4900 tenant.semaphore_handler.semaphores)
4901
4902 self.executor_server.release('semaphore-one-test1')
4903 self.waitUntilSettled()
4904
4905 self.assertEqual(len(self.builds), 3)
4906 self.assertEqual(self.builds[0].name, 'project-test1')
4907 self.assertEqual(self.builds[1].name, 'project-test1')
4908 self.assertEqual(self.builds[2].name, 'semaphore-one-test2')
4909 self.assertTrue('test-semaphore' in
4910 tenant.semaphore_handler.semaphores)
4911
4912 self.executor_server.release('semaphore-one-test2')
4913 self.waitUntilSettled()
4914
4915 self.assertEqual(len(self.builds), 2)
4916 self.assertEqual(self.builds[0].name, 'project-test1')
4917 self.assertEqual(self.builds[1].name, 'project-test1')
4918 self.assertFalse('test-semaphore' in
4919 tenant.semaphore_handler.semaphores)
4920
4921 self.executor_server.hold_jobs_in_build = False
4922 self.executor_server.release()
4923
4924 self.waitUntilSettled()
4925 self.assertEqual(len(self.builds), 0)
4926
4927 self.assertEqual(A.reported, 1)
4928 self.assertEqual(B.reported, 1)
4929 self.assertFalse('test-semaphore' in
4930 tenant.semaphore_handler.semaphores)
4931
4932 def test_semaphore_two(self):
4933 "Test semaphores with max>1"
4934 tenant = self.sched.abide.tenants.get('tenant-one')
4935
4936 self.executor_server.hold_jobs_in_build = True
4937 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
4938 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
4939 self.assertFalse('test-semaphore-two' in
4940 tenant.semaphore_handler.semaphores)
4941
4942 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
4943 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
4944 self.waitUntilSettled()
4945
4946 self.assertEqual(len(self.builds), 4)
4947 self.assertEqual(self.builds[0].name, 'project-test1')
4948 self.assertEqual(self.builds[1].name, 'semaphore-two-test1')
4949 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4950 self.assertEqual(self.builds[3].name, 'project-test1')
4951 self.assertTrue('test-semaphore-two' in
4952 tenant.semaphore_handler.semaphores)
4953 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4954 'test-semaphore-two', [])), 2)
4955
4956 self.executor_server.release('semaphore-two-test1')
4957 self.waitUntilSettled()
4958
4959 self.assertEqual(len(self.builds), 4)
4960 self.assertEqual(self.builds[0].name, 'project-test1')
4961 self.assertEqual(self.builds[1].name, 'semaphore-two-test2')
4962 self.assertEqual(self.builds[2].name, 'project-test1')
4963 self.assertEqual(self.builds[3].name, 'semaphore-two-test1')
4964 self.assertTrue('test-semaphore-two' in
4965 tenant.semaphore_handler.semaphores)
4966 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4967 'test-semaphore-two', [])), 2)
4968
4969 self.executor_server.release('semaphore-two-test2')
4970 self.waitUntilSettled()
4971
4972 self.assertEqual(len(self.builds), 4)
4973 self.assertEqual(self.builds[0].name, 'project-test1')
4974 self.assertEqual(self.builds[1].name, 'project-test1')
4975 self.assertEqual(self.builds[2].name, 'semaphore-two-test1')
4976 self.assertEqual(self.builds[3].name, 'semaphore-two-test2')
4977 self.assertTrue('test-semaphore-two' in
4978 tenant.semaphore_handler.semaphores)
4979 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4980 'test-semaphore-two', [])), 2)
4981
4982 self.executor_server.release('semaphore-two-test1')
4983 self.waitUntilSettled()
4984
4985 self.assertEqual(len(self.builds), 3)
4986 self.assertEqual(self.builds[0].name, 'project-test1')
4987 self.assertEqual(self.builds[1].name, 'project-test1')
4988 self.assertEqual(self.builds[2].name, 'semaphore-two-test2')
4989 self.assertTrue('test-semaphore-two' in
4990 tenant.semaphore_handler.semaphores)
4991 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
4992 'test-semaphore-two', [])), 1)
4993
4994 self.executor_server.release('semaphore-two-test2')
4995 self.waitUntilSettled()
4996
4997 self.assertEqual(len(self.builds), 2)
4998 self.assertEqual(self.builds[0].name, 'project-test1')
4999 self.assertEqual(self.builds[1].name, 'project-test1')
5000 self.assertFalse('test-semaphore-two' in
5001 tenant.semaphore_handler.semaphores)
5002
5003 self.executor_server.hold_jobs_in_build = False
5004 self.executor_server.release()
5005
5006 self.waitUntilSettled()
5007 self.assertEqual(len(self.builds), 0)
5008
5009 self.assertEqual(A.reported, 1)
5010 self.assertEqual(B.reported, 1)
5011
5012 def test_semaphore_abandon(self):
5013 "Test abandon with job semaphores"
5014 self.executor_server.hold_jobs_in_build = True
5015 tenant = self.sched.abide.tenants.get('tenant-one')
5016 check_pipeline = tenant.layout.pipelines['check']
5017
5018 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5019 self.assertFalse('test-semaphore' in
5020 tenant.semaphore_handler.semaphores)
5021
5022 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5023 self.waitUntilSettled()
5024
5025 self.assertTrue('test-semaphore' in
5026 tenant.semaphore_handler.semaphores)
5027
5028 self.fake_gerrit.addEvent(A.getChangeAbandonedEvent())
5029 self.waitUntilSettled()
5030
5031 # The check pipeline should be empty
5032 items = check_pipeline.getAllItems()
5033 self.assertEqual(len(items), 0)
5034
5035 # The semaphore should be released
5036 self.assertFalse('test-semaphore' in
5037 tenant.semaphore_handler.semaphores)
5038
5039 self.executor_server.hold_jobs_in_build = False
5040 self.executor_server.release()
5041 self.waitUntilSettled()
5042
5043 def test_semaphore_reconfigure(self):
5044 "Test reconfigure with job semaphores"
5045 self.executor_server.hold_jobs_in_build = True
5046 tenant = self.sched.abide.tenants.get('tenant-one')
5047 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
5048 self.assertFalse('test-semaphore' in
5049 tenant.semaphore_handler.semaphores)
5050
5051 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5052 self.waitUntilSettled()
5053
5054 self.assertTrue('test-semaphore' in
5055 tenant.semaphore_handler.semaphores)
5056
5057 # reconfigure without layout change
5058 self.sched.reconfigure(self.config)
5059 self.waitUntilSettled()
5060 tenant = self.sched.abide.tenants.get('tenant-one')
5061
5062 # semaphore still must be held
5063 self.assertTrue('test-semaphore' in
5064 tenant.semaphore_handler.semaphores)
5065
5066 self.commitConfigUpdate(
5067 'common-config',
5068 'config/semaphore/zuul-reconfiguration.yaml')
5069 self.sched.reconfigure(self.config)
5070 self.waitUntilSettled()
5071 tenant = self.sched.abide.tenants.get('tenant-one')
5072
5073 self.executor_server.release('project-test1')
5074 self.waitUntilSettled()
5075
5076 # There should be no builds anymore
5077 self.assertEqual(len(self.builds), 0)
5078
5079 # The semaphore should be released
5080 self.assertFalse('test-semaphore' in
5081 tenant.semaphore_handler.semaphores)
5082
5083
Tobias Henkel9a0e1942017-03-20 16:16:02 +01005084class TestSemaphoreMultiTenant(ZuulTestCase):
5085 tenant_config_file = 'config/multi-tenant-semaphore/main.yaml'
5086
5087 def test_semaphore_tenant_isolation(self):
5088 "Test semaphores in multiple tenants"
5089
5090 self.waitUntilSettled()
5091 tenant_one = self.sched.abide.tenants.get('tenant-one')
5092 tenant_two = self.sched.abide.tenants.get('tenant-two')
5093
5094 self.executor_server.hold_jobs_in_build = True
5095 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
5096 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
5097 C = self.fake_gerrit.addFakeChange('org/project2', 'master', 'C')
5098 D = self.fake_gerrit.addFakeChange('org/project2', 'master', 'D')
5099 E = self.fake_gerrit.addFakeChange('org/project2', 'master', 'E')
5100 self.assertFalse('test-semaphore' in
5101 tenant_one.semaphore_handler.semaphores)
5102 self.assertFalse('test-semaphore' in
5103 tenant_two.semaphore_handler.semaphores)
5104
5105 # add patches to project1 of tenant-one
5106 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
5107 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
5108 self.waitUntilSettled()
5109
5110 # one build of project1-test1 must run
5111 # semaphore of tenant-one must be acquired once
5112 # semaphore of tenant-two must not be acquired
5113 self.assertEqual(len(self.builds), 1)
5114 self.assertEqual(self.builds[0].name, 'project1-test1')
5115 self.assertTrue('test-semaphore' in
5116 tenant_one.semaphore_handler.semaphores)
5117 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5118 'test-semaphore', [])), 1)
5119 self.assertFalse('test-semaphore' in
5120 tenant_two.semaphore_handler.semaphores)
5121
5122 # add patches to project2 of tenant-two
5123 self.fake_gerrit.addEvent(C.getPatchsetCreatedEvent(1))
5124 self.fake_gerrit.addEvent(D.getPatchsetCreatedEvent(1))
5125 self.fake_gerrit.addEvent(E.getPatchsetCreatedEvent(1))
5126 self.waitUntilSettled()
5127
5128 # one build of project1-test1 must run
5129 # two builds of project2-test1 must run
5130 # semaphore of tenant-one must be acquired once
5131 # semaphore of tenant-two must be acquired twice
5132 self.assertEqual(len(self.builds), 3)
5133 self.assertEqual(self.builds[0].name, 'project1-test1')
5134 self.assertEqual(self.builds[1].name, 'project2-test1')
5135 self.assertEqual(self.builds[2].name, 'project2-test1')
5136 self.assertTrue('test-semaphore' in
5137 tenant_one.semaphore_handler.semaphores)
5138 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5139 'test-semaphore', [])), 1)
5140 self.assertTrue('test-semaphore' in
5141 tenant_two.semaphore_handler.semaphores)
5142 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5143 'test-semaphore', [])), 2)
5144
5145 self.executor_server.release('project1-test1')
5146 self.waitUntilSettled()
5147
5148 # one build of project1-test1 must run
5149 # two builds of project2-test1 must run
5150 # semaphore of tenant-one must be acquired once
5151 # semaphore of tenant-two must be acquired twice
5152 self.assertEqual(len(self.builds), 3)
5153 self.assertEqual(self.builds[0].name, 'project2-test1')
5154 self.assertEqual(self.builds[1].name, 'project2-test1')
5155 self.assertEqual(self.builds[2].name, 'project1-test1')
5156 self.assertTrue('test-semaphore' in
5157 tenant_one.semaphore_handler.semaphores)
5158 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5159 'test-semaphore', [])), 1)
5160 self.assertTrue('test-semaphore' in
5161 tenant_two.semaphore_handler.semaphores)
5162 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5163 'test-semaphore', [])), 2)
5164
5165 self.executor_server.release('project2-test1')
5166 self.waitUntilSettled()
5167
5168 # one build of project1-test1 must run
5169 # one build of project2-test1 must run
5170 # semaphore of tenant-one must be acquired once
5171 # semaphore of tenant-two must be acquired once
5172 self.assertEqual(len(self.builds), 2)
5173 self.assertTrue('test-semaphore' in
5174 tenant_one.semaphore_handler.semaphores)
5175 self.assertEqual(len(tenant_one.semaphore_handler.semaphores.get(
5176 'test-semaphore', [])), 1)
5177 self.assertTrue('test-semaphore' in
5178 tenant_two.semaphore_handler.semaphores)
5179 self.assertEqual(len(tenant_two.semaphore_handler.semaphores.get(
5180 'test-semaphore', [])), 1)
5181
5182 self.executor_server.hold_jobs_in_build = False
5183 self.executor_server.release()
5184
5185 self.waitUntilSettled()
5186
5187 # no build must run
5188 # semaphore of tenant-one must not be acquired
5189 # semaphore of tenant-two must not be acquired
5190 self.assertEqual(len(self.builds), 0)
5191 self.assertFalse('test-semaphore' in
5192 tenant_one.semaphore_handler.semaphores)
5193 self.assertFalse('test-semaphore' in
5194 tenant_two.semaphore_handler.semaphores)
5195
5196 self.assertEqual(A.reported, 1)
5197 self.assertEqual(B.reported, 1)
5198
5199
5200class TestSemaphoreInRepo(ZuulTestCase):
5201 tenant_config_file = 'config/in-repo/main.yaml'
5202
5203 def test_semaphore_in_repo(self):
5204 "Test semaphores in repo config"
5205
5206 # This tests dynamic semaphore handling in project repos. The semaphore
5207 # max value should not be evaluated dynamically but must be updated
5208 # after the change lands.
5209
5210 self.waitUntilSettled()
5211 tenant = self.sched.abide.tenants.get('tenant-one')
5212
5213 in_repo_conf = textwrap.dedent(
5214 """
5215 - job:
5216 name: project-test2
5217 semaphore: test-semaphore
5218
5219 - project:
5220 name: org/project
5221 tenant-one-gate:
5222 jobs:
5223 - project-test2
5224
5225 # the max value in dynamic layout must be ignored
5226 - semaphore:
5227 name: test-semaphore
5228 max: 2
5229 """)
5230
5231 in_repo_playbook = textwrap.dedent(
5232 """
5233 - hosts: all
5234 tasks: []
5235 """)
5236
5237 file_dict = {'.zuul.yaml': in_repo_conf,
5238 'playbooks/project-test2.yaml': in_repo_playbook}
5239 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
5240 files=file_dict)
5241 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
5242 C = self.fake_gerrit.addFakeChange('org/project', 'master', 'C')
5243 B.setDependsOn(A, 1)
5244 C.setDependsOn(A, 1)
5245
5246 self.executor_server.hold_jobs_in_build = True
5247
5248 A.addApproval('code-review', 2)
5249 B.addApproval('code-review', 2)
5250 C.addApproval('code-review', 2)
5251 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
5252 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
5253 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
5254 self.waitUntilSettled()
5255
5256 # check that the layout in a queue item still has max value of 1
5257 # for test-semaphore
5258 pipeline = tenant.layout.pipelines.get('tenant-one-gate')
5259 queue = None
5260 for queue_candidate in pipeline.queues:
5261 if queue_candidate.name == 'org/project':
5262 queue = queue_candidate
5263 break
5264 queue_item = queue.queue[0]
5265 item_dynamic_layout = queue_item.current_build_set.layout
5266 dynamic_test_semaphore = \
5267 item_dynamic_layout.semaphores.get('test-semaphore')
5268 self.assertEqual(dynamic_test_semaphore.max, 1)
5269
5270 # one build must be in queue, one semaphores acquired
5271 self.assertEqual(len(self.builds), 1)
5272 self.assertEqual(self.builds[0].name, 'project-test2')
5273 self.assertTrue('test-semaphore' in
5274 tenant.semaphore_handler.semaphores)
5275 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5276 'test-semaphore', [])), 1)
5277
5278 self.executor_server.release('project-test2')
5279 self.waitUntilSettled()
5280
5281 # change A must be merged
5282 self.assertEqual(A.data['status'], 'MERGED')
5283 self.assertEqual(A.reported, 2)
5284
5285 # send change-merged event as the gerrit mock doesn't send it
5286 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
5287 self.waitUntilSettled()
5288
5289 # now that change A was merged, the new semaphore max must be effective
5290 tenant = self.sched.abide.tenants.get('tenant-one')
5291 self.assertEqual(tenant.layout.semaphores.get('test-semaphore').max, 2)
5292
5293 # two builds must be in queue, two semaphores acquired
5294 self.assertEqual(len(self.builds), 2)
5295 self.assertEqual(self.builds[0].name, 'project-test2')
5296 self.assertEqual(self.builds[1].name, 'project-test2')
5297 self.assertTrue('test-semaphore' in
5298 tenant.semaphore_handler.semaphores)
5299 self.assertEqual(len(tenant.semaphore_handler.semaphores.get(
5300 'test-semaphore', [])), 2)
5301
5302 self.executor_server.release('project-test2')
5303 self.waitUntilSettled()
5304
5305 self.assertEqual(len(self.builds), 0)
5306 self.assertFalse('test-semaphore' in
5307 tenant.semaphore_handler.semaphores)
5308
5309 self.executor_server.hold_jobs_in_build = False
5310 self.executor_server.release()
5311
5312 self.waitUntilSettled()
5313 self.assertEqual(len(self.builds), 0)
5314
5315 self.assertEqual(A.reported, 2)
5316 self.assertEqual(B.reported, 2)
5317 self.assertEqual(C.reported, 2)